1 | [[tags: egg]] |
---|
2 | [[toc:]] |
---|
3 | |
---|
4 | |
---|
5 | == Skiplists |
---|
6 | |
---|
7 | Skiplists are data-types, which can replace balanced search-trees. They |
---|
8 | are invented by Pugh. The idea is as follows: |
---|
9 | |
---|
10 | Contrary to listnodes, which are pairs of an item and a next pointer, |
---|
11 | skipnodes are pairs of an item and a vector of next pointers. The |
---|
12 | length' of these vectors depend on each skipnode itself. They are |
---|
13 | choosen randomly in such a way, that, in the average, the number of |
---|
14 | nodes with at least k links is half the number of links with at least |
---|
15 | k-1 links, for k>1. Following the next pointers at a fixed link-level, |
---|
16 | k say, one skips all nodes with less than k pointers. |
---|
17 | |
---|
18 | Inserting an item into a skiplist now works as follows. First one |
---|
19 | packages the item into a skipnode, where the number of links is |
---|
20 | generated randomly as described above. Second, one follows the skiplist |
---|
21 | along the highest occupied number of links as long as the skiplist's |
---|
22 | nodes point to items less then the item of the node to be inserted. |
---|
23 | Third, one steps down one level and continues following the skiplist's |
---|
24 | nodes at this new smaller level. Repeating this process until level 0 |
---|
25 | is reached we eventually find the place where our new node is to be |
---|
26 | inserted. |
---|
27 | |
---|
28 | Some additional remarks are in order. |
---|
29 | |
---|
30 | We described the process with a width of two, i.e. at each level in the |
---|
31 | average one node of the level below is skipped. A higher value than two |
---|
32 | for the width is possible as well, trading performance against space. |
---|
33 | |
---|
34 | We have to decide, what to do with duplicates. We choose the following |
---|
35 | approach: The skiplist itself stores a list of either one or several |
---|
36 | numerical comparison operators. Only if the last of those operators is |
---|
37 | the special comparison operator dups (which returns constantly 0, i.e. |
---|
38 | it compares nothing) duplicates are allowed. Moreover, we arrage |
---|
39 | matters in such a way, that all nodes of duplicates with the same key |
---|
40 | have the same height, so that a search for the item which was inserted |
---|
41 | last will be found first. |
---|
42 | |
---|
43 | === Documentation |
---|
44 | |
---|
45 | In this implementation skiplists are implemented in two modules, |
---|
46 | %skiplists and skiplists. They both have the same interface. |
---|
47 | The former implements all routines without any checks, the latter |
---|
48 | imports the former with routines prefixed with % and calling those |
---|
49 | routines with checked arguments. This way you can trade security against |
---|
50 | speed ... |
---|
51 | |
---|
52 | ==== skiplists |
---|
53 | |
---|
54 | <procedure>(skiplists) |
---|
55 | <procedure>(skiplists sym) |
---|
56 | |
---|
57 | documentation procedure. |
---|
58 | The first call shows the list of exported symbols, |
---|
59 | the second documentation of symbol sym. |
---|
60 | |
---|
61 | ==== skiplist |
---|
62 | |
---|
63 | <procedure>(skiplist width max-height item? order . orders)</procedure> |
---|
64 | <procedure>(skiplist max-height item? order . orders)</procedure> |
---|
65 | <procedure>(skiplist item? order . orders))</procedure> |
---|
66 | |
---|
67 | constructors: |
---|
68 | width is the jump width, |
---|
69 | max-height the maximum allowed length of pointers of an item, |
---|
70 | item? checks |
---|
71 | |
---|
72 | ==== skiplist? |
---|
73 | |
---|
74 | <procedure>(skiplist? xpr)</procedure> |
---|
75 | |
---|
76 | type predicate. |
---|
77 | |
---|
78 | ==== skiplist->list |
---|
79 | |
---|
80 | <procedure>(skiplist->list sls)</procedure> |
---|
81 | <procedure>(skiplist->list sls level)</procedure> |
---|
82 | |
---|
83 | the list of items stored in each level |
---|
84 | |
---|
85 | ==== sl-null? |
---|
86 | |
---|
87 | <procedure>(sl-null? sls)</procedure> |
---|
88 | |
---|
89 | is skiplist empty? |
---|
90 | |
---|
91 | ==== sl-dups? |
---|
92 | |
---|
93 | <procedure>(sl-dups? sls)</procedure> |
---|
94 | |
---|
95 | are duplicates allowed? |
---|
96 | |
---|
97 | ==== sl-item? |
---|
98 | |
---|
99 | <procedure>(sl-item? sls)</procedure> |
---|
100 | |
---|
101 | item type predicate |
---|
102 | |
---|
103 | ==== dups |
---|
104 | |
---|
105 | <procedure>(dups x y)</procedure> |
---|
106 | |
---|
107 | trivial numerical comparison operator to allow for duplicates |
---|
108 | |
---|
109 | transformer. |
---|
110 | |
---|
111 | ==== sl-compare |
---|
112 | |
---|
113 | <procedure>(sl-compare sls)</procedure> |
---|
114 | |
---|
115 | combined comparison function |
---|
116 | |
---|
117 | ==== sl-count |
---|
118 | |
---|
119 | <procedure>(sl-count sls)</procedure> |
---|
120 | |
---|
121 | number of items stored in skiplist |
---|
122 | |
---|
123 | ==== sl-found |
---|
124 | |
---|
125 | <procedure>(sl-found sls)</procedure> |
---|
126 | |
---|
127 | list of found items, to be called after search! |
---|
128 | |
---|
129 | ==== sl-found? |
---|
130 | |
---|
131 | <procedure>(sl-found? sls item)</procedure> |
---|
132 | |
---|
133 | is item found? |
---|
134 | |
---|
135 | ==== sl-height |
---|
136 | |
---|
137 | <procedure>(sl-height sls)</procedure> |
---|
138 | |
---|
139 | actual maximal height of nodes (can be changed) |
---|
140 | |
---|
141 | ==== sl-map |
---|
142 | |
---|
143 | <procedure>(sl-map fn sls)</procedure> |
---|
144 | <procedure>(sl-map fn sls order . orders)</procedure> |
---|
145 | <procedure>(sl-map fn sls width)</procedure> |
---|
146 | <procedure>(sl-map fn sls width order . orders)</procedure> |
---|
147 | |
---|
148 | depending on the mapping function, different order |
---|
149 | procedures might be necessary |
---|
150 | |
---|
151 | ==== sl-for-each |
---|
152 | |
---|
153 | <procedure>(sl-for-each proc sls)</procedure> |
---|
154 | |
---|
155 | apply proc to each item in skiplist |
---|
156 | |
---|
157 | ==== sl-filter |
---|
158 | |
---|
159 | <procedure>(sl-filter ok? sls)</procedure> |
---|
160 | |
---|
161 | filtering |
---|
162 | |
---|
163 | ==== sl-max-height |
---|
164 | |
---|
165 | <procedure>(sl-max-height sls)</procedure> |
---|
166 | |
---|
167 | absolute maximum heigth of nodes in skiplist (not changeble) |
---|
168 | |
---|
169 | ==== sl-max |
---|
170 | |
---|
171 | <procedure>(sl-max sls)</procedure> |
---|
172 | |
---|
173 | biggest items stored in skiplist |
---|
174 | |
---|
175 | ==== sl-min |
---|
176 | |
---|
177 | <procedure>(sl-min sls)</procedure> |
---|
178 | |
---|
179 | smallest item stored in skiplist |
---|
180 | |
---|
181 | ==== sl-orders |
---|
182 | |
---|
183 | <procedure>(sl-orders sls)</procedure> |
---|
184 | |
---|
185 | list of orders defined in the constructor |
---|
186 | |
---|
187 | ==== sl-search-level |
---|
188 | |
---|
189 | <procedure>(sl-search-level sls)</procedure> |
---|
190 | |
---|
191 | down to which level a previous search descended? |
---|
192 | |
---|
193 | ==== sl-width |
---|
194 | |
---|
195 | <procedure>(sl-width sls)</procedure> |
---|
196 | |
---|
197 | width skipped on average at each search level supplied by constructor |
---|
198 | |
---|
199 | ==== sl-reorder |
---|
200 | |
---|
201 | <procedure>(sl-reorder sls order . orders)</procedure> |
---|
202 | |
---|
203 | changing orders |
---|
204 | |
---|
205 | ==== sl-restructure |
---|
206 | |
---|
207 | <procedure>(sl-restructure sls width max-height)</procedure> |
---|
208 | |
---|
209 | changing width |
---|
210 | |
---|
211 | ==== sl-insert! |
---|
212 | |
---|
213 | <procedure>(sl-insert! sls item . items)</procedure> |
---|
214 | |
---|
215 | insert items into skiplist |
---|
216 | |
---|
217 | ==== sl-remove! |
---|
218 | |
---|
219 | <procedure>(sl-remove! sls item . items)</procedure> |
---|
220 | |
---|
221 | remove items from skiplist |
---|
222 | |
---|
223 | ==== sl-search! |
---|
224 | |
---|
225 | <procedure>(sl-search! sls item)</procedure> |
---|
226 | |
---|
227 | searching for an item changes cursor transparently |
---|
228 | |
---|
229 | ==== sl-clear! |
---|
230 | |
---|
231 | <procedure>(sl-clear! sls)</procedure> |
---|
232 | |
---|
233 | reset skiplist |
---|
234 | |
---|
235 | === Examples |
---|
236 | |
---|
237 | <enscript highlight=scheme> |
---|
238 | |
---|
239 | (import skiplists) |
---|
240 | |
---|
241 | A skiplist with primary and secondary search order, allowing duplicates |
---|
242 | ;; some constructors |
---|
243 | |
---|
244 | (define sls1 (skiplist 15 fixnum? -)) |
---|
245 | (sl-width sls1) ;-> 2 |
---|
246 | (sl-max-height sls1) ;-> 15 |
---|
247 | (sl-dups? sls1) ;-> #f |
---|
248 | |
---|
249 | (define sls2 (skiplist 4 20 fixnum? - dups)) |
---|
250 | (fx= (sl-width sls2) ;-> 4 |
---|
251 | (fx= (sl-max-height sls2) ;-> 20 |
---|
252 | (sl-dups? sls2) ;-> #t |
---|
253 | |
---|
254 | ;; create ... |
---|
255 | |
---|
256 | (define item-type (lambda (x) |
---|
257 | (and ((list-of? integer?) x) (> (length x) 2)))) |
---|
258 | |
---|
259 | (define primary-order (lambda (x y) (- (car x) (car y)))) |
---|
260 | |
---|
261 | (define secondary-order (lambda (x y) (- (cadr x) (cadr y)))) |
---|
262 | |
---|
263 | (define sls3 (skiplist 3 |
---|
264 | 15 |
---|
265 | item-type |
---|
266 | primary-order |
---|
267 | secondary-order |
---|
268 | dups)) |
---|
269 | |
---|
270 | ;; and populate ... |
---|
271 | |
---|
272 | (define lst1 |
---|
273 | (let loop ((k 0) (lst '())) |
---|
274 | (if (= k 100) |
---|
275 | lst |
---|
276 | (loop (+ k 1) (cons (pseudo-random-integer 10) lst))))) |
---|
277 | |
---|
278 | (define lst2 |
---|
279 | (let loop ((k 0) (lst '())) |
---|
280 | (if (= k 100) |
---|
281 | lst |
---|
282 | (loop (+ k 1) (cons (pseudo-random-integer 10) lst))))) |
---|
283 | |
---|
284 | (define lst3 |
---|
285 | (let loop ((k 0) (lst '())) |
---|
286 | (if (= k 100) |
---|
287 | lst |
---|
288 | (loop (+ k 1) (cons (pseudo-random-integer 100) lst))))) |
---|
289 | |
---|
290 | (apply sl-insert! sls3 |
---|
291 | (map (lambda (x y z) (list x y z)) |
---|
292 | lst1 lst2 lst3)) |
---|
293 | |
---|
294 | (sl-count sls3) ;-> 100 |
---|
295 | |
---|
296 | (sl-width sls3) ;-> 3 |
---|
297 | |
---|
298 | ;; inserting item and removing all with same key ... |
---|
299 | |
---|
300 | ((sl-item? sls3) '(1 2 3)) ;-> #t |
---|
301 | |
---|
302 | (sl-search! sls3 '(1 2 3)) |
---|
303 | |
---|
304 | (if (sl-found? sls3 '(1 2 3)) |
---|
305 | (apply sl-remove! sls3 (sl-found sls3))) |
---|
306 | |
---|
307 | (sl-insert! sls3 '(1 2 3)) |
---|
308 | |
---|
309 | (sl-search! sls3 '(1 2 3)) |
---|
310 | |
---|
311 | (member '(1 2 3) (sl-found sls3)) |
---|
312 | |
---|
313 | (apply sl-remove! sls3 (sl-found sls3)) |
---|
314 | |
---|
315 | (sl-search! sls3 '(1 2 3)) |
---|
316 | |
---|
317 | (null? (sl-found sls3)) |
---|
318 | |
---|
319 | ;; produce duplicates at the ends ... |
---|
320 | |
---|
321 | (sl-insert! sls3 '(-1 2 3) '(-1 2 3 4)) |
---|
322 | |
---|
323 | (sl-min sls3) ;-> '((-1 2 3 4) (-1 2 3)) |
---|
324 | |
---|
325 | (sl-insert! sls3 '(10 1 2) '(10 1 2 3) '(10 1 3)) |
---|
326 | |
---|
327 | (sl-found sls3) ;-> '((10 1 3) (10 1 2 3) (10 1 2)) |
---|
328 | |
---|
329 | (sl-max sls3) ;-> '((10 1 3) (10 1 2 3) (10 1 2)) |
---|
330 | |
---|
331 | ;; and remove them again ... |
---|
332 | |
---|
333 | (sl-search! sls3 '(-1 2 3 4)) |
---|
334 | |
---|
335 | (apply sl-remove! sls3 (sl-found sls3)) |
---|
336 | |
---|
337 | (sl-search! sls3 '(-1 2 3 4)) |
---|
338 | |
---|
339 | (null? (sl-found sls3)) ;-> #t |
---|
340 | |
---|
341 | (sl-search! sls3 '(10 1 3)) |
---|
342 | |
---|
343 | (apply sl-remove! sls3 (sl-found sls3)) |
---|
344 | |
---|
345 | (null? (sl-found sls3)) ;-> #t |
---|
346 | |
---|
347 | ;; reorder removing all dups ... |
---|
348 | |
---|
349 | (apply <= (map car |
---|
350 | (skiplist->list |
---|
351 | (sl-reorder sls3 primary-order secondary-order)))) |
---|
352 | |
---|
353 | (<= (sl-count (sl-reorder sls3 primary-order secondary-order)) |
---|
354 | (sl-count sls3)) |
---|
355 | |
---|
356 | reorder using only secondary order ... |
---|
357 | |
---|
358 | (apply < (map cadr |
---|
359 | (skiplist->list |
---|
360 | (sl-reorder sls3 secondary-order)))) |
---|
361 | |
---|
362 | (>= 10 (sl-count |
---|
363 | (sl-reorder sls3 secondary-order))) |
---|
364 | |
---|
365 | ;; restructure ... |
---|
366 | |
---|
367 | (equal? (skiplist->list sls3) |
---|
368 | (skiplist->list (sl-restructure sls3 2 10))) |
---|
369 | |
---|
370 | ;; filter ... |
---|
371 | |
---|
372 | ((list-of? odd?) (map caddr |
---|
373 | (skiplist->list |
---|
374 | (sl-filter sls3 (lambda (x) (odd? (caddr x))))))) |
---|
375 | |
---|
376 | ;; map ... |
---|
377 | |
---|
378 | (let ((fn (lambda (x) (* 2 x)))) |
---|
379 | (equal? |
---|
380 | (map fn (skiplist->list sls3)) |
---|
381 | (skiplist->list (sl-map sls3 fn)))) |
---|
382 | |
---|
383 | </enscript> |
---|
384 | |
---|
385 | === Requirements |
---|
386 | |
---|
387 | none |
---|
388 | |
---|
389 | === Last update |
---|
390 | |
---|
391 | Mar 25, 2019 |
---|
392 | |
---|
393 | == Author |
---|
394 | |
---|
395 | [[/users/juergen-lorenz|Juergen Lorenz]] |
---|
396 | |
---|
397 | == License |
---|
398 | |
---|
399 | Copyright (c) 2012-2019, Juergen Lorenz |
---|
400 | All rights reserved. |
---|
401 | Redistribution and use in source and binary forms, with or without |
---|
402 | modification, are permitted provided that the following conditions are |
---|
403 | met: |
---|
404 | |
---|
405 | Redistributions of source code must retain the above copyright |
---|
406 | notice, this list of conditions and the following disclaimer. |
---|
407 | |
---|
408 | Redistributions in binary form must reproduce the above copyright |
---|
409 | notice, this list of conditions and the following disclaimer in the |
---|
410 | documentation and/or other materials provided with the distribution. |
---|
411 | Neither the name of the author nor the names of its contributors may be |
---|
412 | used to endorse or promote products derived from this software without |
---|
413 | specific prior written permission. |
---|
414 | |
---|
415 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
---|
416 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
---|
417 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
---|
418 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
419 | HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
420 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
---|
421 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
422 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
423 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
424 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
425 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
426 | |
---|
427 | == Version History |
---|
428 | |
---|
429 | ; 1.0 : port from chicken-4 |
---|