q.scm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ;;;; q.scm --- Queues
  2. ;;;;
  3. ;;;; Copyright (C) 1995, 2001, 2004, 2006 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 2.1 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;;;
  19. ;;; Commentary:
  20. ;;; Q: Based on the interface to
  21. ;;;
  22. ;;; "queue.scm" Queues/Stacks for Scheme
  23. ;;; Written by Andrew Wilcox (awilcox@astro.psu.edu) on April 1, 1992.
  24. ;;; {Q}
  25. ;;;
  26. ;;; A list is just a bunch of cons pairs that follows some constrains,
  27. ;;; right? Association lists are the same. Hash tables are just
  28. ;;; vectors and association lists. You can print them, read them,
  29. ;;; write them as constants, pun them off as other data structures
  30. ;;; etc. This is good. This is lisp. These structures are fast and
  31. ;;; compact and easy to manipulate arbitrarily because of their
  32. ;;; simple, regular structure and non-disjointedness (associations
  33. ;;; being lists and so forth).
  34. ;;;
  35. ;;; So I figured, queues should be the same -- just a "subtype" of cons-pair
  36. ;;; structures in general.
  37. ;;;
  38. ;;; A queue is a cons pair:
  39. ;;; ( <the-q> . <last-pair> )
  40. ;;;
  41. ;;; <the-q> is a list of things in the q. New elements go at the end
  42. ;;; of that list.
  43. ;;;
  44. ;;; <last-pair> is #f if the q is empty, and otherwise is the last
  45. ;;; pair of <the-q>.
  46. ;;;
  47. ;;; q's print nicely, but alas, they do not read well because the
  48. ;;; eq?-ness of <last-pair> and (last-pair <the-q>) is lost by read.
  49. ;;;
  50. ;;; All the functions that aren't explicitly defined to return
  51. ;;; something else (a queue element; a boolean value) return the queue
  52. ;;; object itself.
  53. ;;; Code:
  54. (define-module (ice-9 q)
  55. :export (sync-q! make-q q? q-empty? q-empty-check q-front q-rear
  56. q-remove! q-push! enq! q-pop! deq! q-length))
  57. ;;; sync-q!
  58. ;;; The procedure
  59. ;;;
  60. ;;; (sync-q! q)
  61. ;;;
  62. ;;; recomputes and resets the <last-pair> component of a queue.
  63. ;;;
  64. (define (sync-q! q)
  65. (set-cdr! q (if (pair? (car q)) (last-pair (car q))
  66. #f))
  67. q)
  68. ;;; make-q
  69. ;;; return a new q.
  70. ;;;
  71. (define (make-q) (cons '() #f))
  72. ;;; q? obj
  73. ;;; Return true if obj is a Q.
  74. ;;; An object is a queue if it is equal? to '(() . #f)
  75. ;;; or it is a pair P with (list? (car P))
  76. ;;; and (eq? (cdr P) (last-pair (car P))).
  77. ;;;
  78. (define (q? obj)
  79. (and (pair? obj)
  80. (if (pair? (car obj))
  81. (eq? (cdr obj) (last-pair (car obj)))
  82. (and (null? (car obj))
  83. (not (cdr obj))))))
  84. ;;; q-empty? obj
  85. ;;;
  86. (define (q-empty? obj) (null? (car obj)))
  87. ;;; q-empty-check q
  88. ;;; Throw a q-empty exception if Q is empty.
  89. (define (q-empty-check q) (if (q-empty? q) (throw 'q-empty q)))
  90. ;;; q-front q
  91. ;;; Return the first element of Q.
  92. (define (q-front q) (q-empty-check q) (caar q))
  93. ;;; q-rear q
  94. ;;; Return the last element of Q.
  95. (define (q-rear q) (q-empty-check q) (cadr q))
  96. ;;; q-remove! q obj
  97. ;;; Remove all occurences of obj from Q.
  98. (define (q-remove! q obj)
  99. (set-car! q (delq! obj (car q)))
  100. (sync-q! q))
  101. ;;; q-push! q obj
  102. ;;; Add obj to the front of Q
  103. (define (q-push! q obj)
  104. (let ((h (cons obj (car q))))
  105. (set-car! q h)
  106. (or (cdr q) (set-cdr! q h)))
  107. q)
  108. ;;; enq! q obj
  109. ;;; Add obj to the rear of Q
  110. (define (enq! q obj)
  111. (let ((h (cons obj '())))
  112. (if (null? (car q))
  113. (set-car! q h)
  114. (set-cdr! (cdr q) h))
  115. (set-cdr! q h))
  116. q)
  117. ;;; q-pop! q
  118. ;;; Take the front of Q and return it.
  119. (define (q-pop! q)
  120. (q-empty-check q)
  121. (let ((it (caar q))
  122. (next (cdar q)))
  123. (if (null? next)
  124. (set-cdr! q #f))
  125. (set-car! q next)
  126. it))
  127. ;;; deq! q
  128. ;;; Take the front of Q and return it.
  129. (define deq! q-pop!)
  130. ;;; q-length q
  131. ;;; Return the number of enqueued elements.
  132. ;;;
  133. (define (q-length q) (length (car q)))
  134. ;;; q.scm ends here