1 | [[tags:egg]] |
---|
2 | [[toc:]] |
---|
3 | |
---|
4 | == rb-tree |
---|
5 | |
---|
6 | Sorted dictionary data structures based on red-black trees. |
---|
7 | |
---|
8 | |
---|
9 | == Documentation |
---|
10 | |
---|
11 | The {{rb-tree}} library is based on the SML/NJ library implementation |
---|
12 | of red-black trees, which is in turn based on Chris Okasaki's |
---|
13 | implementation of red-black trees. The delete function is based on |
---|
14 | the description in Cormen, Leiserson, and Rivest. |
---|
15 | |
---|
16 | The present implementation code defines a persistent map [[typeclass]] |
---|
17 | that implements an ordered dictionary mapping of keys to values. |
---|
18 | |
---|
19 | Looking up an arbitrary or the min/max keys, and deleting the min/max |
---|
20 | keys require no more key comparisons than the depth of the tree, which |
---|
21 | is {{O(log n)}} where {{n}} is the total number of keys in the tree. |
---|
22 | |
---|
23 | === Procedures |
---|
24 | |
---|
25 | The persistent map instance is created by procedure {{rb-tree-map}}: |
---|
26 | |
---|
27 | <procedure>rb-tree-map:: KEY-COMPARE-PROC [insdel-key-compare: KEY-COMPARE-PROC] -> <PersistentMap></procedure> |
---|
28 | |
---|
29 | where KEY-COMPARE-PROC is a user-supplied function that takes two keys |
---|
30 | and returns a negative, positive, or zero number depending on how the |
---|
31 | first key compares to the second. |
---|
32 | |
---|
33 | Optional keyword argument {{insdel-key-compare}} can be used to |
---|
34 | specify different key comparison predicates for the insertion and |
---|
35 | deletion operations. |
---|
36 | |
---|
37 | The {{<PersistentMap>}} typeclass contains the following operations: |
---|
38 | |
---|
39 | ; {{empty}} : returns a new empty tree |
---|
40 | ; {{empty? TREE}} : returns {{#t}} if the given tree is empty |
---|
41 | ; {{get TREE}} : returns a procedure of the form {{(LAMBDA KEY . DEFAULT-CLAUSE}} which searches the given tree for an association with a given {{KEY}}, and returns a (key . value) pair of the found association. If an association with {{KEY}} cannot be located in the tree, the procedure returns the result of evaluating the {{DEFAULT-CLAUSE}}. If the default clause is omitted, an error is signalled. {{KEY}} must be comparable to the keys in the tree by a key-compare predicate (which has been specified when the tree was created) |
---|
42 | ; {{get-value TREE}} : returns a procedure of the form {{(LAMBDA KEY . DEFAULT-CLAUSE}} which searches the tree for an association with a given {{KEY}}, and returns the value of (key . value) pair of the found association. If an association with {{KEY}} cannot be located in the tree, the procedure returns the result of evaluating the {{DEFAULT-CLAUSE}}. If the default clause is omitted, an error is signalled. {{KEY}} must be comparable to the keys in the tree by a key-compare predicate (which has been specified when the tree was created) |
---|
43 | ; {{get-min TREE}} : returns a (key . value) pair for an association in the tree with the smallest key. If the tree is empty, an error is signalled. |
---|
44 | ; {{get-max TREE}} : returns a (key . value) pair for an association in the tree with the largest key. If the tree is empty, an error is signalled. |
---|
45 | ; {{size TREE}} : returns the size (the number of associations) in the tree |
---|
46 | ; {{put TREE KEY VALUE}} : returns a new tree object that contains the given association |
---|
47 | ; {{delete TREE KEY . DEFAULT-CLAUSE}} : if the specified key is found, it returns a new tree object that no longer contains the association specified by that key, while the original tree object is unmodified. If the key is not found, the procedure returns the result of evaluating {{DEFAULT-CLAUSE}} |
---|
48 | ; {{for-each-ascending TREE}} : returns a procedure {{LAMBDA PROC}} that will apply the given procedure PROC to each (key . value) association of the tree, from the one with the smallest key all the way to the one with the max key, in an ascending order of keys. |
---|
49 | ; {{for-each-descending TREE}} : returns a procedure {{LAMBDA PROC}} that will apply the given procedure {{PROC}}to each (key . value) association of the tree, in the descending order of keys. |
---|
50 | ; {{map TREE}} : returns a procedure {{LAMBDA PROC}} that will apply the given procedure {{PROC}}to the value component of each association in the tree, in the ascending order of keys, and will construct a copy of the tree that contains the values returned by that procedure. |
---|
51 | ; {{mapi TREE}} : returns a procedure {{LAMBDA PROC}} that will apply the given procedure {{PROC}}to each (key . value) association in the tree, in the ascending order of keys, and will construct a copy of the tree that contains the values returned by that procedure. |
---|
52 | ; {{fold TREE}} : returns a procedure {{LAMBDA PROC INITIAL}} such that, given the associations in the tree ordered by the descending order of keys: {{(key-n . value-n) ... (key-2 . value-2) (key-1 . value-1) }} the procedure returns the result of the successive function applications {{(PROC value-1 (PROC value-2 ... (PROC value-n INITIAL)}}. |
---|
53 | ; {{foldi TREE}} : returns a procedure {{LAMBDA PROC INITIAL}} such that, given the associations in the tree ordered by the descending order of keys: {{(key-n . value-n) ... (key-2 . value-2) (key-1 . value-1) }} the procedure returns the result of the successive function applications {{(PROC key-1 value-1 (PROC key-2 value-2 ... (PROC key-n value-n INITIAL)}}. |
---|
54 | ; {{fold-right TREE}} : returns a procedure {{LAMBDA PROC INITIAL}} such that, given the associations in the tree ordered by the ascending order of keys: {{(key-1 . value-1) (key-2 . value-2) ... (key-n . value-n) }} the procedure returns the result of the successive function applications {{(PROC value-n ... (PROC value-2 (PROC value-1 INITIAL)}}. |
---|
55 | ; {{foldi-right TREE}} : returns a procedure {{LAMBDA PROC INITIAL}} such that, given the associations in the tree ordered by the ascending order of keys: {{(key-1 . value-1) (key-2 . value-2) ... (key-n . value-n) }} the procedure returns the result of the successive function applications {{(PROC key-n value-n ... (PROC key-2 value-2 (PROC key-1 value-1 INITIAL)}}. |
---|
56 | ; {{fold-partial TREE}} : returns a procedure {{LAMBDA PRED PROC INITIAL}} such that, given the associations in the tree ordered by the descending order of keys: {{(key-n . value-n) ... (key-2 . value-2) (key-1 . value-1) }} the procedure returns the result of the successive function applications {{(PROC value-i ... (PROC value-n INITIAL)}}, where {{i <= n}} and {{(PRED x)}} holds true for all {{x = (value-n) ... (value-i)}}. In other words, this function acts like {{fold}} on the ordered subset of the values {{x}} in the tree such that {{(PRED x)}} is true. |
---|
57 | ; {{foldi-partial TREE}} : returns a procedure {{LAMBDA PRED PROC INITIAL}} such that, given the associations in the tree ordered by the descending order of keys: {{(key-n . value-n) ... (key-2 . value-2) (key-1 . value-1) }} the procedure returns the result of the successive function applications {{(PROC key-i value-i ... (PROC key-n value-n INITIAL)}}, where {{i <= n}} and {{(PRED xk x)}} holds true for all {{x = (value-n) ... (value-i)}} and {{xk = (key-n) ... (key-i)}}. In other words, this function acts like {{foldi}} on the ordered subset of the key-value pairs {{(k . x)}} in the tree such that {{(PRED k x)}} is true. |
---|
58 | ; {{fold-right-partial TREE}} : returns a procedure {{LAMBDA PRED PROC INITIAL}} such that, given the associations in the tree ordered by the ascending order of keys: {{(key-1 . value-1) (key-2 . value-2) ... (key-n . value-n) }} the procedure returns the result of the successive function applications {{(PROC value-1 ... (PROC value-i INITIAL)}}, where {{i <= n}} and {{(PRED x)}} holds true for all {{x = (value-1) ... (value-i)}}. In other words, this function acts like {{fold-right}} on the ordered subset of the values {{x}} in the tree such that {{(PRED x)}} is true. |
---|
59 | ; {{foldi-right-partial TREE}} : returns a procedure {{LAMBDA PRED PROC INITIAL}} such that, given the associations in the tree ordered by the descending order of keys: {{(key-1 . value-1) (key-2 . value-2) ... (key-1 . value-1) }} the procedure returns the result of the successive function applications {{(PROC key-1 value-1 ... (PROC key-i value-i INITIAL)}}, where {{i <= n}} and {{(PRED xk x)}} holds true for all {{x = (value-1) ... (value-i)}} and {{xk = (key-1) ... (key-i)}}. In other words, this function acts like {{foldi-right}} on the ordered subset of the key-value pairs {{(k . x)}} in the tree such that {{(PRED k x)}} is true. |
---|
60 | ; {{fold-limit TREE}} : returns a procedure {{LAMBDA PRED PROC INITIAL}} such that, given the associations in the tree ordered by the descending order of keys: {{(key-n . value-n) ... (key-2 . value-2) (key-1 . value-1) }} the procedure returns the result of the successive function applications {{(PROC value-i ... (PROC value-n INITIAL)}}, where {{i <= n}} and {{(PRED x)}} does not hold true for all {{x = (PROC value-n INITIAL) ... (PROC (value-i) (PROC value-(i-1)...}}. |
---|
61 | ; {{fold-right-limit TREE}} : returns a procedure {{LAMBDA PRED PROC INITIAL}} such that, given the associations in the tree ordered by the descending order of keys: {{(key-1 . value-1) (key-2 . value-2) ... (key-i . value-1) }} the procedure returns the result of the successive function applications {{(PROC value-i ... (PROC value-1 INITIAL)}}, where {{i <= n}} and {{(PRED x)}} does not hold true for all {{x = (PROC value-1 INITIAL) ... (PROC (value-i) (PROC value-(i-1)...}}. |
---|
62 | |
---|
63 | |
---|
64 | == Examples |
---|
65 | |
---|
66 | (import typeclass rb-tree) |
---|
67 | |
---|
68 | (define (++ x) (+ 1 x)) |
---|
69 | (define (-- x) (- x 1)) |
---|
70 | |
---|
71 | (let ((m (rb-tree-map (lambda (x y) (- x y))))) |
---|
72 | (with-instance ((<PersistentMap> m)) |
---|
73 | |
---|
74 | (let* ((compute-assoc (lambda (key) (cons key (++ key)))) |
---|
75 | (min-key -1) (max-key 10) |
---|
76 | (t |
---|
77 | (let recur ((t (empty)) (i min-key)) |
---|
78 | (let ((t1 (put t i (cdr (compute-assoc i))))) |
---|
79 | (if (< i max-key) (recur t1 (++ i)) t1)))) |
---|
80 | ) |
---|
81 | |
---|
82 | (print ((get t) (++ min-key))) |
---|
83 | |
---|
84 | (print ((get t) (-- min-key) 'notfound)) |
---|
85 | |
---|
86 | ;; checking traversing in ascending order |
---|
87 | (let ((expected-key min-key)) |
---|
88 | ((for-each-ascending t) |
---|
89 | (lambda (association) |
---|
90 | (print (equal? association (compute-assoc expected-key))) |
---|
91 | (set! expected-key (++ expected-key))))) |
---|
92 | ) |
---|
93 | )) |
---|
94 | |
---|
95 | == About this egg |
---|
96 | |
---|
97 | |
---|
98 | === Author |
---|
99 | |
---|
100 | [[/users/ivan-raikov|Ivan Raikov]] |
---|
101 | |
---|
102 | === Version history |
---|
103 | |
---|
104 | ; 5.3 : Ported to CHICKEN 5 |
---|
105 | ; 5.0 : Using typeclass interface, discarded ephemeral map |
---|
106 | ; 4.2 : Ensure test script returns proper exit status |
---|
107 | ; 4.0 : Divided API in persistent and ephemeral maps |
---|
108 | ; 3.1 : Added get-value operation |
---|
109 | ; 3.0 : Ability to specify different predicates for lookup, insert, delete operations |
---|
110 | ; 2.9 : Documentation converted to wiki format |
---|
111 | ; 2.8 : Added matchable as dependency |
---|
112 | ; 2.7 : Bug fix in dispatch-on-key |
---|
113 | ; 2.6 : Ported to Chicken 4 |
---|
114 | ; 2.5 : Fixes to for-each-ascending/descending |
---|
115 | ; 2.3 : Build script updated for better cross-platform compatibility |
---|
116 | ; 2.2 : Added fold-limit procedures |
---|
117 | ; 2.1 : Added fold-partial procedures |
---|
118 | ; 2.0 : Added side-effect-free put and delete procedures |
---|
119 | ; 1.0 : Initial release |
---|
120 | |
---|
121 | === License |
---|
122 | |
---|
123 | |
---|
124 | Copyright 2007-2018 Ivan Raikov. |
---|
125 | |
---|
126 | This program is free software: you can redistribute it and/or modify |
---|
127 | it under the terms of the GNU General Public License as published by |
---|
128 | the Free Software Foundation, either version 3 of the License, or (at |
---|
129 | your option) any later version. |
---|
130 | |
---|
131 | This program is distributed in the hope that it will be useful, but |
---|
132 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
133 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
134 | General Public License for more details. |
---|
135 | |
---|
136 | A full copy of the GPL license can be found at |
---|
137 | <http://www.gnu.org/licenses/>. |
---|
138 | |
---|