1 | == SRFI-117: Queues based on lists |
---|
2 | |
---|
3 | List queues are mutable ordered collections that can contain any Scheme object. |
---|
4 | Each list queue is based on an ordinary Scheme list containing the elements of |
---|
5 | the list queue by maintaining pointers to the first and last pairs of the list. |
---|
6 | It's cheap to add or remove elements from the front of the list or to add |
---|
7 | elements to the back, but not to remove elements from the back. List queues are |
---|
8 | disjoint from other types of Scheme objects. |
---|
9 | |
---|
10 | [[toc:]] |
---|
11 | |
---|
12 | == Installation |
---|
13 | |
---|
14 | $ chicken-install srfi-117 |
---|
15 | |
---|
16 | or |
---|
17 | |
---|
18 | $ chicken-install srfi-117 -test |
---|
19 | |
---|
20 | if you want to run the tests for the egg in addition. |
---|
21 | |
---|
22 | == SRFI Description |
---|
23 | |
---|
24 | For a full description of this SRFI, see the full SRFI |
---|
25 | [[http://srfi.schemers.org/srfi-117/srfi-117.html|document]]. This |
---|
26 | documentation covers the API only. |
---|
27 | |
---|
28 | == List Queues |
---|
29 | |
---|
30 | === Constructors |
---|
31 | |
---|
32 | <procedure>(make-list-queue list [last])</procedure> |
---|
33 | |
---|
34 | Returns a newly allocated list queue containing the elements of {{list}} in |
---|
35 | order. The result shares storage with {{list}}. If the last argument is not |
---|
36 | provided, this operation is O(n) where n is the length of {{list}}. |
---|
37 | |
---|
38 | However, if last is provided, {{make-list-queue}} returns a newly allocated |
---|
39 | list queue containing the elements of the {{list}} whose first pair is first |
---|
40 | and whose last pair is last. It is an error if the pairs do not belong to the |
---|
41 | same list. Alternatively, both first and last can be the empty list. In either |
---|
42 | case, the operation is O(1). |
---|
43 | |
---|
44 | '''Note:''' To apply a non-destructive list procedure to a list queue and return a |
---|
45 | new list queue, use {{(make-list-queue (proc (list-queue-list list-queue)))}}. |
---|
46 | |
---|
47 | <procedure>(list-queue element ...)</procedure> |
---|
48 | |
---|
49 | Returns a newly allocated list queue containing the {{elements}}. This |
---|
50 | operation is O(n) where n is the number of elements. |
---|
51 | |
---|
52 | <procedure>(list-queue-copy list-queue)</procedure> |
---|
53 | |
---|
54 | Returns a newly allocated list queue containing the elements of {{list-queue}}. |
---|
55 | This operation is O(n) where n is the length of {{list-queue}}. |
---|
56 | |
---|
57 | <procedure>(list-queue-unfold stop? mapper successor seed [queue])</procedure> |
---|
58 | |
---|
59 | Performs the following algorithm: |
---|
60 | |
---|
61 | If the result of applying the predicate {{stop?}} to {{seed}} is true, return |
---|
62 | {{queue}}. Otherwise, apply the procedure {{mapper}} to {{seed}}, returning a |
---|
63 | value which is added to the front of {{queue}}. Then get a new seed by applying |
---|
64 | the procedure {{successor}} to {{seed}}, and repeat this algorithm. |
---|
65 | |
---|
66 | If {{queue}} is omitted, a newly allocated list queue is used. |
---|
67 | |
---|
68 | <procedure>(list-queue-unfold-right stop? mapper successor seed [queue])</procedure> |
---|
69 | |
---|
70 | Performs the following algorithm: |
---|
71 | |
---|
72 | If the result of applying the predicate {{stop?}} to {{seed}} is true, return |
---|
73 | the list queue. Otherwise, apply the procedure {{mapper}} to {{seed}}, |
---|
74 | returning a value which is added to the back of the list queue. Then get a new |
---|
75 | seed by applying the procedure {{successor}} to {{seed}}, and repeat this |
---|
76 | algorithm. |
---|
77 | |
---|
78 | If {{queue}} is omitted, a newly allocated list queue is used. |
---|
79 | |
---|
80 | === Predicates |
---|
81 | |
---|
82 | <procedure>(list-queue? obj)</procedure> |
---|
83 | |
---|
84 | Returns {{#t}} if {{obj}} is a list queue, and {{#f}} otherwise. This operation |
---|
85 | is O(1). |
---|
86 | |
---|
87 | <procedure>(list-queue-empty? list-queue)</procedure> |
---|
88 | |
---|
89 | Returns {{#t}} if {{list-queue}} has no elements, and {{#f}} otherwise. This |
---|
90 | operation is O(1). |
---|
91 | |
---|
92 | === Accessors |
---|
93 | |
---|
94 | <procedure>(list-queue-front list-queue)</procedure> |
---|
95 | |
---|
96 | Returns the first element of {{list-queue}}. If the list queue is empty, it is |
---|
97 | an error. This operation is O(1). |
---|
98 | |
---|
99 | <procedure>(list-queue-back list-queue)</procedure> |
---|
100 | |
---|
101 | Returns the last element of {{list-queue}}. If the list queue is empty, it is |
---|
102 | an error. This operation is O(1). |
---|
103 | |
---|
104 | <procedure>(list-queue-list list-queue)</procedure> |
---|
105 | |
---|
106 | Returns the list that contains the members of {{list-queue}} in order. The |
---|
107 | result shares storage with {{list-queue}}. This operation is O(1). |
---|
108 | |
---|
109 | <procedure>(list-queue-first-last list-queue)</procedure> |
---|
110 | |
---|
111 | Returns two values, the first and last pairs of the list that contains the |
---|
112 | members of {{list-queue}} in order. If {{list-queue}} is empty, returns two |
---|
113 | empty lists. The results share storage with {{list-queue}}. This operation is |
---|
114 | O(1). |
---|
115 | |
---|
116 | === Mutators |
---|
117 | |
---|
118 | <procedure>(list-queue-add-front! list-queue element)</procedure> |
---|
119 | |
---|
120 | Adds element to the beginning of {{list-queue}}. Returns an unspecified value. |
---|
121 | This operation is O(1). |
---|
122 | |
---|
123 | <procedure>(list-queue-add-back! list-queue element)</procedure> |
---|
124 | |
---|
125 | Adds element to the end of {{list-queue}}. Returns an unspecified value. This |
---|
126 | operation is O(1). |
---|
127 | |
---|
128 | <procedure>(list-queue-remove-front! list-queue)</procedure> |
---|
129 | |
---|
130 | Removes the first element of {{list-queue}} and returns it. If the list queue |
---|
131 | is empty, it is an error. This operation is O(1). |
---|
132 | |
---|
133 | <procedure>(list-queue-remove-back! list-queue)</procedure> |
---|
134 | |
---|
135 | Removes the last element of {{list-queue}} and returns it. If the list queue is |
---|
136 | empty, it is an error. This operation is O(n) where n is the length of |
---|
137 | {{list-queue}}, because queues do not not have backward links. |
---|
138 | |
---|
139 | <procedure>(list-queue-remove-all! list-queue)</procedure> |
---|
140 | |
---|
141 | Removes all the elements of {{list-queue}} and returns them in order as a list. |
---|
142 | This operation is O(1). |
---|
143 | |
---|
144 | <procedure>(list-queue-set-list! list-queue list [last])</procedure> |
---|
145 | |
---|
146 | Replaces the list associated with {{list-queue}} with {{list}}, effectively |
---|
147 | discarding all the elements of {{list-queue}} in favor of those in {{list}}. |
---|
148 | Returns an unspecified value. This operation is O(n) where n is the length of |
---|
149 | {{list}}. If {{last}} is provided, it is treated in the same way as in |
---|
150 | {{make-list-queue}}, and the operation is O(1). |
---|
151 | |
---|
152 | '''Note:''' To apply a destructive list procedure to a list queue, use |
---|
153 | {{(list-queue-set-list! (proc (list-queue-list list-queue)))}}. |
---|
154 | |
---|
155 | <procedure>(list-queue-append list-queue ...)</procedure> |
---|
156 | |
---|
157 | Returns a list queue which contains all the elements in front-to-back order |
---|
158 | from all the {{list-queues}} in front-to-back order. The result does not share |
---|
159 | storage with any of the arguments. This operation is O(n) in the total number |
---|
160 | of elements in all queues. |
---|
161 | |
---|
162 | <procedure>(list-queue-append! list-queue ...)</procedure> |
---|
163 | |
---|
164 | Returns a list queue which contains all the elements in front-to-back order |
---|
165 | from all the {{list-queue}}s in front-to-back order. It is an error to assume |
---|
166 | anything about the contents of the {{list-queue}}s after the procedure returns. |
---|
167 | This operation is O(n) in the total number of queues, not elements. It is not |
---|
168 | part of the R7RS-small list API, but is included here for efficiency when pure |
---|
169 | functional append is not required. |
---|
170 | |
---|
171 | <procedure>(list-queue-concatenate list-of-list-queues)</procedure> |
---|
172 | |
---|
173 | Returns a list queue which contains all the elements in front-to-back order |
---|
174 | from all the list queues which are members of {{list-of-list-queues}} in |
---|
175 | front-to-back order. The result does not share storage with any of the |
---|
176 | arguments. This operation is O(n) in the total number of elements in all |
---|
177 | queues. It is not part of the R7RS-small list API, but is included here to make |
---|
178 | appending a large number of queues possible in Schemes that limit the number of |
---|
179 | arguments to apply. |
---|
180 | |
---|
181 | === Mapping |
---|
182 | |
---|
183 | <procedure>(list-queue-map proc list-queue)</procedure> |
---|
184 | |
---|
185 | Applies {{proc}} to each element of {{list-queue}} in unspecified order and |
---|
186 | returns a newly allocated list queue containing the results. This operation is |
---|
187 | O(n) where n is the length of {{list-queue}}. |
---|
188 | |
---|
189 | <procedure>(list-queue-map! proc list-queue)</procedure> |
---|
190 | |
---|
191 | Applies {{proc}} to each element of {{list-queue}} in front-to-back order and |
---|
192 | modifies {{list-queue}} to contain the results. This operation is O(n) in the |
---|
193 | length of {{list-queue}}. It is not part of the R7RS-small list API, but is |
---|
194 | included here to make transformation of a list queue by mutation more |
---|
195 | efficient. |
---|
196 | |
---|
197 | <procedure>(list-queue-for-each proc list-queue)</procedure> |
---|
198 | |
---|
199 | Applies {{proc}} to each element of {{list-queue}} in front-to-back order, |
---|
200 | discarding the returned values. Returns an unspecified value. This operation is |
---|
201 | O(n) where n is the length of {{list-queue}}. |
---|
202 | |
---|
203 | == Repository |
---|
204 | |
---|
205 | [[https://github.com/ThatGeoGuy/srfi-117|Github]] |
---|
206 | |
---|
207 | == Version History |
---|
208 | |
---|
209 | ; 1.4 : Fixes egg synopsis for C5 |
---|
210 | ; 1.3 : Adds CHICKEN 5 support |
---|
211 | ; 1.2 : Adds standard README.org to SRFI and fixes hardcoded .so in setup |
---|
212 | ; 1.1 : Packages egg without extraneous files |
---|
213 | ; 1.0 : Initial release |
---|
214 | |
---|
215 | == License |
---|
216 | |
---|
217 | Copyright (C) John Cowan (2014-2016). All Rights Reserved. |
---|
218 | |
---|
219 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
---|
220 | this software and associated documentation files (the "Software"), to deal in |
---|
221 | the Software without restriction, including without limitation the rights to |
---|
222 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
---|
223 | of the Software, and to permit persons to whom the Software is furnished to do |
---|
224 | so, subject to the following conditions: |
---|
225 | |
---|
226 | The above copyright notice and this permission notice shall be included in all |
---|
227 | copies or substantial portions of the Software. |
---|
228 | |
---|
229 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
230 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
231 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
232 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
233 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
234 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
---|
235 | SOFTWARE. |
---|