Mercurial > repos > tabletprog
annotate modules/dict.tp @ 267:d2b70cba661e
Warning cleanup
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 18 Jul 2014 00:14:22 -0700 |
parents | 697c2c562af2 |
children | bb4723fec05e |
rev | line source |
---|---|
202
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
1 { |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
2 linearWithEls <- :els { |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 key:val <- :k v { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 #{ |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 key <- k |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 val <- v |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 find <- :tofind { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 idx <- 0 |
164 | 11 while: { |
12 if: idx < (els length) { | |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 ((els get: idx) key: ) != tofind |
164 | 14 } else: {false} |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 } do: { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 idx <- idx + 1 |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 if: idx < (els length) {idx} else: {-1} |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 #{ |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 set <- :k v { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 idx <- find: k |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 if: idx < 0 { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 els append: (key: k val: v) |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 } else: { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 (els get: idx) val!: v |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 self |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 } |
164 | 30 |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 get <- :k { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 get: k withDefault: false |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 } |
164 | 34 |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 get:withDefault <- :k default { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 idx <- find: k |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 if: idx < 0 { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 default |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 } else: { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 (els get: idx) val |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
42 } |
164 | 43 |
44 get:elseSet <- :k :else { | |
248
96fdc5b37ceb
Added get:else method to linear dict
Michael Pavone <pavone@retrodev.com>
parents:
202
diff
changeset
|
45 get: k else: { |
164 | 46 v <- else: |
47 els append: (key: k val: v) | |
48 v | |
248
96fdc5b37ceb
Added get:else method to linear dict
Michael Pavone <pavone@retrodev.com>
parents:
202
diff
changeset
|
49 } |
96fdc5b37ceb
Added get:else method to linear dict
Michael Pavone <pavone@retrodev.com>
parents:
202
diff
changeset
|
50 } |
96fdc5b37ceb
Added get:else method to linear dict
Michael Pavone <pavone@retrodev.com>
parents:
202
diff
changeset
|
51 |
96fdc5b37ceb
Added get:else method to linear dict
Michael Pavone <pavone@retrodev.com>
parents:
202
diff
changeset
|
52 get:else <- :k :else { |
96fdc5b37ceb
Added get:else method to linear dict
Michael Pavone <pavone@retrodev.com>
parents:
202
diff
changeset
|
53 idx <- find: k |
96fdc5b37ceb
Added get:else method to linear dict
Michael Pavone <pavone@retrodev.com>
parents:
202
diff
changeset
|
54 if: idx < 0 { |
96fdc5b37ceb
Added get:else method to linear dict
Michael Pavone <pavone@retrodev.com>
parents:
202
diff
changeset
|
55 else: |
164 | 56 } else: { |
57 (els get: idx) val | |
58 } | |
59 } | |
60 | |
192
a868a2aec930
Add contains? method to linear dictionary
Mike Pavone <pavone@retrodev.com>
parents:
164
diff
changeset
|
61 contains? <- :k { |
a868a2aec930
Add contains? method to linear dictionary
Mike Pavone <pavone@retrodev.com>
parents:
164
diff
changeset
|
62 (find: k) >= 0 |
a868a2aec930
Add contains? method to linear dictionary
Mike Pavone <pavone@retrodev.com>
parents:
164
diff
changeset
|
63 } |
a868a2aec930
Add contains? method to linear dictionary
Mike Pavone <pavone@retrodev.com>
parents:
164
diff
changeset
|
64 |
91 | 65 foreach <- :l { |
66 foreach: els :idx el { | |
94
6735db9b44ba
Fixed my foreach on dict. Added test for it.
William Morgan <bill@mrgn.org>
parents:
91
diff
changeset
|
67 l: (el key) (el val) |
91 | 68 } |
69 } | |
94
6735db9b44ba
Fixed my foreach on dict. Added test for it.
William Morgan <bill@mrgn.org>
parents:
91
diff
changeset
|
70 |
202
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
71 map <- :fun { |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
72 newels <- #[] |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
73 foreach: els :idx el { |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
74 newels append: (key: (el key) val: (fun: (el val))) |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
75 } |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
76 linearWithEls: newels |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
77 } |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
78 |
164 | 79 length <- { els length } |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
80 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
81 } |
250
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
82 _empty <- #{ |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
83 empty? <- { true } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
84 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
85 _makeBucket <- :key val { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
86 #{ |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
87 empty? <- { false } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
88 k <- key |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
89 v <- val |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
90 = <- :other { k = other } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
91 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
92 } |
202
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
93 #{ |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
94 //requires only that keys support equality |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
95 linear <- { |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
96 linearWithEls: #[] |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
97 } |
250
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
98 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
99 //requires that keys support equality and hash |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
100 hash <- { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
101 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
102 _buckets <- #[ |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
103 _empty |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
104 _empty |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
105 _empty |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
106 _empty] |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
107 _size <- 0 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
108 _hashdiffs <- #[0] |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
109 #{ |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
110 size <- { size } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
111 ifget:else <- :key ifpres :ifnot { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
112 basehash <- key hash |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
113 notdone <- true |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
114 i <- 0 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
115 ret <- _empty |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
116 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
117 while: { if: notdone { i < (_hashdiffs length)}} do: { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
118 hv <- basehash + (_hashdiffs get: i) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
119 trunc <- hv % (_buckets length) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
120 if: trunc < 0 { trunc <- 0 - trunc } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
121 bucket <- _buckets get: trunc |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
122 if: (bucket empty?) { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
123 notdone <- false |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
124 } else: { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
125 if: bucket = key { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
126 ret <- bucket |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
127 notdone <- false |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
128 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
129 } |
253
697c2c562af2
Fix infinite loop in hash dict
Michael Pavone <pavone@retrodev.com>
parents:
250
diff
changeset
|
130 i <- i + 1 |
250
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
131 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
132 if: (ret empty?) ifnot else: { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
133 ifpres: (ret v) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
134 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
135 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
136 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
137 get:else <- :key :else { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
138 ifget: key :val { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
139 val |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
140 } else: else |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
141 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
142 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
143 contains? <- :key { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
144 ifget: key :_ { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
145 true |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
146 } else: { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
147 false |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
148 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
149 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
150 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
151 set <- :key val { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
152 notdone <- true |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
153 basehash <- key hash |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
154 i <- 0 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
155 while: { if: notdone { i < (_hashdiffs length) } } do: { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
156 hv <- basehash + (_hashdiffs get: i) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
157 trunc <- hv % (_buckets length) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
158 if: trunc < 0 { trunc <- 0 - trunc } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
159 bucket <- (_buckets get: trunc) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
160 if: (bucket empty?) { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
161 _size <- _size + 1 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
162 _buckets set: trunc (_makeBucket: key val) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
163 notdone <- false |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
164 } else: { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
165 if: bucket = key { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
166 bucket v!: val |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
167 notdone <- false |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
168 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
169 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
170 i <- i + 1 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
171 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
172 if: notdone { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
173 newsize <- (_buckets length) * 3 + 1 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
174 lastdiff <- _hashdiffs get: ((_hashdiffs length) - 1) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
175 if: lastdiff <= 0 { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
176 _hashdiffs append: 0 - lastdiff + 1 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
177 } else: { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
178 _hashdiffs append: 0 - lastdiff |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
179 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
180 newbucks <- #[] |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
181 newbucks resize: newsize |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
182 while: { (newbucks length) < newsize } do: { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
183 newbucks append: _empty |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
184 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
185 oldbucks <- _buckets |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
186 _buckets <- newbucks |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
187 _size <- 0 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
188 foreach: oldbucks :idx el { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
189 if: (not: (el empty?)) { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
190 set: (el k) (el v) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
191 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
192 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
193 set: key val |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
194 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
195 self |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
196 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
197 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
198 foreach <- :fun { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
199 foreach: _buckets :idx el { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
200 if: (not: (el empty?)) { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
201 fun: (el k) (el v) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
202 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
203 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
204 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
205 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
206 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
207 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
208 main <- { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
209 d <- hash |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
210 d set: "foo" "bar" |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
211 d set: "baz" "qux" |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
212 i <- 0 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
213 while: { i < 32 } do: { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
214 d set: (string: i) "blah " . (string: i) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
215 i <- i + 1 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
216 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
217 foreach: d :k v { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
218 print: "k: " . k . ", v: " . v . "\n" |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
219 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
220 0 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
221 } |
202
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
222 } |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
223 } |