Commit | Line | Data |
---|---|---|
6a4d3cfd JB |
1 | ;;;; q.scm --- Queues |
2 | ;;;; | |
a6401ee0 JB |
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 | ||
6a4d3cfd JB |
21 | (define-module (ice-9 q)) |
22 | ||
a6401ee0 JB |
23 | ;;;; |
24 | ;;; Q: Based on the interface to | |
25 | ;;; | |
26 | ;;; "queue.scm" Queues/Stacks for Scheme | |
27 | ;;; Written by Andrew Wilcox (awilcox@astro.psu.edu) on April 1, 1992. | |
28 | ;;; | |
29 | ||
30 | ;;;; | |
31 | ;;; {Q} | |
32 | ;;; | |
33 | ;;; A list is just a bunch of cons pairs that follows some constrains, right? | |
34 | ;;; Association lists are the same. Hash tables are just vectors and association | |
35 | ;;; lists. You can print them, read them, write them as constants, pun them off as other data | |
36 | ;;; structures etc. This is good. This is lisp. These structures are fast and compact | |
37 | ;;; and easy to manipulate arbitrarily because of their simple, regular structure and | |
38 | ;;; non-disjointedness (associations being lists and so forth). | |
39 | ;;; | |
40 | ;;; So I figured, queues should be the same -- just a "subtype" of cons-pair | |
41 | ;;; structures in general. | |
42 | ;;; | |
43 | ;;; A queue is a cons pair: | |
44 | ;;; ( <the-q> . <last-pair> ) | |
45 | ;;; | |
46 | ;;; <the-q> is a list of things in the q. New elements go at the end of that list. | |
47 | ;;; | |
48 | ;;; <last-pair> is #f if the q is empty, and otherwise is the last pair of <the-q>. | |
49 | ;;; | |
50 | ;;; q's print nicely, but alas, they do not read well because the eq?-ness of | |
51 | ;;; <last-pair> and (last-pair <the-q>) is lost by read. The procedure | |
52 | ;;; | |
53 | ;;; (sync-q! q) | |
54 | ;;; | |
55 | ;;; recomputes and resets the <last-pair> component of a queue. | |
56 | ;;; | |
57 | ||
58 | (define-public (sync-q! obj) (set-cdr! obj (and (car obj) (last-pair (car obj))))) | |
59 | ||
60 | ;;; make-q | |
61 | ;;; return a new q. | |
62 | ;;; | |
63 | (define-public (make-q) (cons '() '())) | |
64 | ||
65 | ;;; q? obj | |
66 | ;;; Return true if obj is a Q. | |
67 | ;;; An object is a queue if it is equal? to '(#f . #f) or | |
68 | ;;; if it is a pair P with (list? (car P)) and (eq? (cdr P) (last-pair P)). | |
69 | ;;; | |
70 | (define-public (q? obj) (and (pair? obj) | |
71 | (or (and (null? (car obj)) | |
72 | (null? (cdr obj))) | |
73 | (and | |
74 | (list? (car obj)) | |
75 | (eq? (cdr obj) (last-pair (car obj))))))) | |
76 | ||
77 | ;;; q-empty? obj | |
78 | ;;; | |
79 | (define-public (q-empty? obj) (null? (car obj))) | |
80 | ||
81 | ;;; q-empty-check q | |
82 | ;;; Throw a q-empty exception if Q is empty. | |
83 | (define-public (q-empty-check q) (if (q-empty? q) (throw 'q-empty q))) | |
84 | ||
85 | ||
86 | ;;; q-front q | |
87 | ;;; Return the first element of Q. | |
88 | (define-public (q-front q) (q-empty-check q) (caar q)) | |
89 | ||
90 | ;;; q-rear q | |
91 | ;;; Return the last element of Q. | |
92 | (define-public (q-rear q) (q-empty-check q) (cadr q)) | |
93 | ||
94 | ;;; q-remove! q obj | |
95 | ;;; Remove all occurences of obj from Q. | |
96 | (define-public (q-remove! q obj) | |
97 | (while (memq obj (car q)) | |
98 | (set-car! q (delq! obj (car q)))) | |
99 | (set-cdr! q (last-pair (car q)))) | |
100 | ||
101 | ;;; q-push! q obj | |
102 | ;;; Add obj to the front of Q | |
103 | (define-public (q-push! q d) | |
104 | (let ((h (cons d (car q)))) | |
105 | (set-car! q h) | |
106 | (if (null? (cdr q)) | |
107 | (set-cdr! q h)))) | |
108 | ||
109 | ;;; enq! q obj | |
110 | ;;; Add obj to the rear of Q | |
111 | (define-public (enq! q d) | |
112 | (let ((h (cons d '()))) | |
113 | (if (not (null? (cdr q))) | |
114 | (set-cdr! (cdr q) h) | |
115 | (set-car! q h)) | |
116 | (set-cdr! q h))) | |
117 | ||
118 | ;;; q-pop! q | |
119 | ;;; Take the front of Q and return it. | |
120 | (define-public (q-pop! q) | |
121 | (q-empty-check q) | |
122 | (let ((it (caar q)) | |
123 | (next (cdar q))) | |
124 | (if (not next) | |
125 | (set-cdr! q #f)) | |
126 | (set-car! q next) | |
127 | it)) | |
128 | ||
129 | ;;; deq! q | |
130 | ;;; Take the front of Q and return it. | |
131 | (define-public deq! q-pop!) | |
132 | ||
133 | ;;; q-length q | |
134 | ;;; Return the number of enqueued elements. | |
135 | ;;; | |
136 | (define-public (q-length q) (length (car q))) | |
137 | ||
138 | ||
139 |