forked from Matway/mpl-sl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTable.mpl
executable file
·216 lines (183 loc) · 4.81 KB
/
HashTable.mpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"HashTable" module
"Array" includeModule
HashTable: [
value:;
key:;
{
virtual HASH_TABLE: ();
schema keyType: @key;
schema valueType: @value;
Node: [{
key: key newVarOfTheSameType;
keyHash: 0n32 dynamic;
value: value newVarOfTheSameType;
}];
data: Node Array Array;
dataSize: 0 dynamic;
getSize: [dataSize copy];
rebuild: [
copy newBucketSize:;
newBucketSize @data.resize
b: 0 dynamic;
[b data.dataSize <] [
current: b @data.at;
i: 0 dynamic;
j: 0 dynamic;
[i current.dataSize <] [
h: i current.at.keyHash copy;
newB: h newBucketSize 1 - 0n32 cast and 0i32 cast;
newB b = [
i j = not [
i @current.at move
j @current.at set
] when
j 1 + @j set
] [
pushTo: newB @data.at;
i @current.at move @pushTo.pushBack
] if
i 1 + @i set
] while
j @current.resize
b 1 + @b set
] while
];
find: [
key:;
keyHash: @key hash dynamic;
[
result: {
success: FALSE dynamic;
value: 0nx @valueType addressToReference;
};
dataSize 0 = not [
bucketIndex: keyHash data.dataSize 1 - 0n32 cast and 0 cast;
curBucket: bucketIndex @data.at;
i: 0 dynamic;
[
i curBucket.dataSize < [
node: i @curBucket.at;
node.key key = [
{
success: TRUE dynamic;
value: @node.@value;
} @result set
FALSE
] [
i 1 + @i set TRUE
] if
] &&
] loop
] when
@result
] call
];
erase: [
key:;
keyHash: @key hash dynamic;
[
dataSize 0 = not [
bucketIndex: keyHash data.dataSize 1 - 0n32 cast and 0 cast;
curBucket: bucketIndex @data.at;
i: 0 dynamic;
[
i curBucket.dataSize < [
node: i @curBucket.at;
node.key key = [
i @curBucket.erase
FALSE
] [
i 1 + @i set TRUE
] if
] [
[FALSE] "Erasing unexisting element!" assert
FALSE
] if
] loop
] when
] call
];
insertUnsafe: [ # make find before please
valueIsMoved: isMoved;
value:;
keyIsMoved: isMoved;
key:;
keyHash: key hash dynamic;
[
dataSize data.dataSize = [
newBucketSize: dataSize 0 = [16][dataSize 2 *] if;
newBucketSize rebuild
] when
bucketIndex: keyHash data.dataSize 1 - 0n32 cast and 0 cast;
newNode: {
key: @key keyIsMoved moveIf copy;
keyHash: key hash;
value: @value valueIsMoved moveIf copy;
};
pushTo: bucketIndex @data.at;
@newNode move @pushTo.pushBack
dataSize 1 + @dataSize set
] call
];
insert: [
DEBUG [
valueIsMoved: isMoved;
value:;
keyIsMoved: isMoved;
key:;
fr: key find;
[fr.success not] "Inserting existing element!" assert
@key keyIsMoved moveIf @value valueIsMoved moveIf insertUnsafe
] [
insertUnsafe
] if
];
clear: [
@data.clear
0 dynamic @dataSize set
];
release: [
@data.release
0 dynamic @dataSize set
];
INIT: [
0 dynamic @dataSize set
];
ASSIGN: [
other:;
other.data @data set
other.dataSize @dataSize set
];
DIE: [
#default
];
}];
each: [b:; "HASH_TABLE" has] [
eachInTableBody:;
eachInTableTable:;
eachInTableBucketIndex: 0;
[
eachInTableBucketIndex eachInTableTable.data.dataSize < [
eachInTableCurrentBucket: eachInTableBucketIndex @eachInTableTable.@data.at;
eachInTableI: 0;
[
eachInTableI eachInTableCurrentBucket.dataSize < [
eachInTableNode: eachInTableI @eachInTableCurrentBucket.at;
{key: eachInTableNode.key; value: @eachInTableNode.@value;} @eachInTableBody call
eachInTableI 1 + @eachInTableI set TRUE
] &&
] loop
eachInTableBucketIndex 1 + @eachInTableBucketIndex set TRUE
] &&
] loop
] pfunc;
hash: [0n8 same] [0n32 cast] pfunc;
hash: [0n16 same] [0n32 cast] pfunc;
hash: [0n32 same] [0n32 cast] pfunc;
hash: [0n64 same] [0n32 cast] pfunc;
hash: [0nx same] [0n32 cast] pfunc;
hash: [0i8 same] [0i32 cast 0n32 cast] pfunc;
hash: [0i16 same] [0i32 cast 0n32 cast] pfunc;
hash: [0i32 same] [0i32 cast 0n32 cast] pfunc;
hash: [0i64 same] [0i32 cast 0n32 cast] pfunc;
hash: [0ix same] [0i32 cast 0n32 cast] pfunc;