Mercurial > repos > tabletprog
annotate modules/dict.tp @ 331:61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 28 Mar 2015 14:21:04 -0700 |
parents | eef8a5cea812 |
children | 7de6ac24eb64 |
rev | line source |
---|---|
202
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
1 { |
271
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
253
diff
changeset
|
2 _jsonEncode <- :dict { |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
253
diff
changeset
|
3 parts <- #[] |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
253
diff
changeset
|
4 foreach: dict :key val { |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
253
diff
changeset
|
5 //TODO: escape field names |
329
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
271
diff
changeset
|
6 parts append: (key jsonEncode) . ":" . (jsonEncoder encode: val) |
271
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
253
diff
changeset
|
7 } |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
253
diff
changeset
|
8 "{" . (parts join: ",") . "}" |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
253
diff
changeset
|
9 } |
202
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
10 linearWithEls <- :els { |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 key:val <- :k v { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 #{ |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 key <- k |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 val <- v |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 find <- :tofind { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 idx <- 0 |
164 | 19 while: { |
20 if: idx < (els length) { | |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 ((els get: idx) key: ) != tofind |
164 | 22 } else: {false} |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 } do: { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 idx <- idx + 1 |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 if: idx < (els length) {idx} else: {-1} |
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 #{ |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 set <- :k v { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 idx <- find: k |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 if: idx < 0 { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 els append: (key: k val: v) |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 } else: { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 (els get: idx) val!: v |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 self |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 } |
164 | 38 |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 get <- :k { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 get: k withDefault: false |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 } |
164 | 42 |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 get:withDefault <- :k default { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 idx <- find: k |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 if: idx < 0 { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 default |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 } else: { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 (els get: idx) val |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 } |
164 | 51 |
52 get:elseSet <- :k :else { | |
248
96fdc5b37ceb
Added get:else method to linear dict
Michael Pavone <pavone@retrodev.com>
parents:
202
diff
changeset
|
53 get: k else: { |
164 | 54 v <- else: |
55 els append: (key: k val: v) | |
56 v | |
248
96fdc5b37ceb
Added get:else method to linear dict
Michael Pavone <pavone@retrodev.com>
parents:
202
diff
changeset
|
57 } |
96fdc5b37ceb
Added get:else method to linear dict
Michael Pavone <pavone@retrodev.com>
parents:
202
diff
changeset
|
58 } |
96fdc5b37ceb
Added get:else method to linear dict
Michael Pavone <pavone@retrodev.com>
parents:
202
diff
changeset
|
59 |
96fdc5b37ceb
Added get:else method to linear dict
Michael Pavone <pavone@retrodev.com>
parents:
202
diff
changeset
|
60 get:else <- :k :else { |
96fdc5b37ceb
Added get:else method to linear dict
Michael Pavone <pavone@retrodev.com>
parents:
202
diff
changeset
|
61 idx <- find: k |
96fdc5b37ceb
Added get:else method to linear dict
Michael Pavone <pavone@retrodev.com>
parents:
202
diff
changeset
|
62 if: idx < 0 { |
96fdc5b37ceb
Added get:else method to linear dict
Michael Pavone <pavone@retrodev.com>
parents:
202
diff
changeset
|
63 else: |
164 | 64 } else: { |
65 (els get: idx) val | |
66 } | |
67 } | |
68 | |
192
a868a2aec930
Add contains? method to linear dictionary
Mike Pavone <pavone@retrodev.com>
parents:
164
diff
changeset
|
69 contains? <- :k { |
a868a2aec930
Add contains? method to linear dictionary
Mike Pavone <pavone@retrodev.com>
parents:
164
diff
changeset
|
70 (find: k) >= 0 |
a868a2aec930
Add contains? method to linear dictionary
Mike Pavone <pavone@retrodev.com>
parents:
164
diff
changeset
|
71 } |
a868a2aec930
Add contains? method to linear dictionary
Mike Pavone <pavone@retrodev.com>
parents:
164
diff
changeset
|
72 |
91 | 73 foreach <- :l { |
74 foreach: els :idx el { | |
94
6735db9b44ba
Fixed my foreach on dict. Added test for it.
William Morgan <bill@mrgn.org>
parents:
91
diff
changeset
|
75 l: (el key) (el val) |
91 | 76 } |
77 } | |
94
6735db9b44ba
Fixed my foreach on dict. Added test for it.
William Morgan <bill@mrgn.org>
parents:
91
diff
changeset
|
78 |
202
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
79 map <- :fun { |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
80 newels <- #[] |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
81 foreach: els :idx el { |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
82 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
|
83 } |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
84 linearWithEls: newels |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
85 } |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
86 |
164 | 87 length <- { els length } |
271
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
253
diff
changeset
|
88 |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
253
diff
changeset
|
89 jsonEncode <- { |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
253
diff
changeset
|
90 _jsonEncode: self |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
253
diff
changeset
|
91 } |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
92 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
93 } |
250
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
94 _empty <- #{ |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
95 empty? <- { true } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
96 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
97 _makeBucket <- :key val { |
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 empty? <- { false } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
100 k <- key |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
101 v <- val |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
102 = <- :other { k = other } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
103 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
104 } |
202
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
105 #{ |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
106 //requires only that keys support equality |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
107 linear <- { |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
108 linearWithEls: #[] |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
109 } |
250
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
110 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
111 //requires that keys support equality and hash |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
112 hash <- { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
113 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
114 _buckets <- #[ |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
115 _empty |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
116 _empty |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
117 _empty |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
118 _empty] |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
119 _size <- 0 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
120 _hashdiffs <- #[0] |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
121 #{ |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
122 size <- { size } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
123 ifget:else <- :key ifpres :ifnot { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
124 basehash <- key hash |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
125 notdone <- true |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
126 i <- 0 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
127 ret <- _empty |
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 while: { if: notdone { i < (_hashdiffs length)}} do: { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
130 hv <- basehash + (_hashdiffs get: i) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
131 trunc <- hv % (_buckets length) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
132 if: trunc < 0 { trunc <- 0 - trunc } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
133 bucket <- _buckets get: trunc |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
134 if: (bucket empty?) { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
135 notdone <- false |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
136 } else: { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
137 if: bucket = key { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
138 ret <- bucket |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
139 notdone <- false |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
140 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
141 } |
253
697c2c562af2
Fix infinite loop in hash dict
Michael Pavone <pavone@retrodev.com>
parents:
250
diff
changeset
|
142 i <- i + 1 |
250
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
143 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
144 if: (ret empty?) ifnot else: { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
145 ifpres: (ret v) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
146 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
147 } |
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 get:else <- :key :else { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
150 ifget: key :val { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
151 val |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
152 } else: else |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
153 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
154 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
155 contains? <- :key { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
156 ifget: key :_ { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
157 true |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
158 } else: { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
159 false |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
160 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
161 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
162 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
163 set <- :key val { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
164 notdone <- true |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
165 basehash <- key hash |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
166 i <- 0 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
167 while: { if: notdone { i < (_hashdiffs length) } } do: { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
168 hv <- basehash + (_hashdiffs get: i) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
169 trunc <- hv % (_buckets length) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
170 if: trunc < 0 { trunc <- 0 - trunc } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
171 bucket <- (_buckets get: trunc) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
172 if: (bucket empty?) { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
173 _size <- _size + 1 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
174 _buckets set: trunc (_makeBucket: key val) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
175 notdone <- false |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
176 } else: { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
177 if: bucket = key { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
178 bucket v!: val |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
179 notdone <- false |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
180 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
181 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
182 i <- i + 1 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
183 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
184 if: notdone { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
185 newsize <- (_buckets length) * 3 + 1 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
186 lastdiff <- _hashdiffs get: ((_hashdiffs length) - 1) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
187 if: lastdiff <= 0 { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
188 _hashdiffs append: 0 - lastdiff + 1 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
189 } else: { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
190 _hashdiffs append: 0 - lastdiff |
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 newbucks <- #[] |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
193 newbucks resize: newsize |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
194 while: { (newbucks length) < newsize } do: { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
195 newbucks append: _empty |
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 oldbucks <- _buckets |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
198 _buckets <- newbucks |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
199 _size <- 0 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
200 foreach: oldbucks :idx el { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
201 if: (not: (el empty?)) { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
202 set: (el k) (el v) |
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 set: key val |
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 self |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
208 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
209 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
210 foreach <- :fun { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
211 foreach: _buckets :idx el { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
212 if: (not: (el empty?)) { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
213 fun: (el k) (el v) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
214 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
215 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
216 } |
271
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
253
diff
changeset
|
217 |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
253
diff
changeset
|
218 jsonEncode <- { |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
253
diff
changeset
|
219 _jsonEncode: self |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
253
diff
changeset
|
220 } |
250
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
221 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
222 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
223 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
224 main <- { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
225 d <- hash |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
226 d set: "foo" "bar" |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
227 d set: "baz" "qux" |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
228 i <- 0 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
229 while: { i < 32 } do: { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
230 d set: (string: i) "blah " . (string: i) |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
231 i <- i + 1 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
232 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
233 foreach: d :k v { |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
234 print: "k: " . k . ", v: " . v . "\n" |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
235 } |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
236 0 |
c58e17f5c0f6
Added hash dictionary to dict module
Michael Pavone <pavone@retrodev.com>
parents:
248
diff
changeset
|
237 } |
202
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
238 } |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
239 } |