q.scm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. ;;;; q.scm --- Queues
  2. ;;;;
  3. ;;;; Copyright (C) 1995 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This program is free software; you can redistribute it and/or modify
  6. ;;;; it under the terms of the GNU General Public License as published by
  7. ;;;; the Free Software Foundation; either version 2, or (at your option)
  8. ;;;; any later version.
  9. ;;;;
  10. ;;;; This program 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
  13. ;;;; GNU General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU General Public License
  16. ;;;; along with this software; see the file COPYING. If not, write to
  17. ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  18. ;;;; Boston, MA 02111-1307 USA
  19. ;;;;
  20. (define-module (ice-9 q))
  21. ;;;;
  22. ;;; Q: Based on the interface to
  23. ;;;
  24. ;;; "queue.scm" Queues/Stacks for Scheme
  25. ;;; Written by Andrew Wilcox (awilcox@astro.psu.edu) on April 1, 1992.
  26. ;;;
  27. ;;;;
  28. ;;; {Q}
  29. ;;;
  30. ;;; A list is just a bunch of cons pairs that follows some constrains,
  31. ;;; right? Association lists are the same. Hash tables are just
  32. ;;; vectors and association lists. You can print them, read them,
  33. ;;; write them as constants, pun them off as other data structures
  34. ;;; etc. This is good. This is lisp. These structures are fast and
  35. ;;; compact and easy to manipulate arbitrarily because of their
  36. ;;; simple, regular structure and non-disjointedness (associations
  37. ;;; being lists and so forth).
  38. ;;;
  39. ;;; So I figured, queues should be the same -- just a "subtype" of cons-pair
  40. ;;; structures in general.
  41. ;;;
  42. ;;; A queue is a cons pair:
  43. ;;; ( <the-q> . <last-pair> )
  44. ;;;
  45. ;;; <the-q> is a list of things in the q. New elements go at the end
  46. ;;; of that list.
  47. ;;;
  48. ;;; <last-pair> is #f if the q is empty, and otherwise is the last
  49. ;;;pair of <the-q>.
  50. ;;;
  51. ;;; q's print nicely, but alas, they do not read well because the
  52. ;;; eq?-ness of <last-pair> and (last-pair <the-q>) is lost by read.
  53. ;;;
  54. ;;; All the functions that aren't explicitly defined to return
  55. ;;; something else (a queue element; a boolean value) return the queue
  56. ;;; object itself.
  57. ;;;
  58. ;;; The procedure
  59. ;;;
  60. ;;; (sync-q! q)
  61. ;;;
  62. ;;; recomputes and resets the <last-pair> component of a queue.
  63. ;;;
  64. (define-public (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-public (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-public (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-public (q-empty? obj) (null? (car obj)))
  87. ;;; q-empty-check q
  88. ;;; Throw a q-empty exception if Q is empty.
  89. (define-public (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-public (q-front q) (q-empty-check q) (caar q))
  93. ;;; q-rear q
  94. ;;; Return the last element of Q.
  95. (define-public (q-rear q) (q-empty-check q) (cadr q))
  96. ;;; q-remove! q obj
  97. ;;; Remove all occurences of obj from Q.
  98. (define-public (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-public (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-public (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-public (q-pop! q)
  120. (q-empty-check q)
  121. (let ((it (caar q))
  122. (next (cdar q)))
  123. (if (not 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-public deq! q-pop!)
  130. ;;; q-length q
  131. ;;; Return the number of enqueued elements.
  132. ;;;
  133. (define-public (q-length q) (length (car q)))