forked from JuliaLang/julia
/
table.j
351 lines (292 loc) · 8.56 KB
/
table.j
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
function _tablesz(i::Integer)
if i < 16
return 16
end
if i&(i-1) == 0
return i
end
while (i&(i-1) != 0)
i = i&(i-1)
end
return i<<1
end
const _jl_secret_table_token = :__c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
function ref(t::Associative, key)
v = get(t, key, _jl_secret_table_token)
if is(v,_jl_secret_table_token)
throw(KeyError(key))
end
return v
end
has(t::Associative, key) = !is(get(t, key, _jl_secret_table_token),
_jl_secret_table_token)
function show(t::Associative)
if isempty(t)
print(typeof(t).name.name,"()")
else
print("{")
for (k, v) = t
show(k)
print("=>")
show(v)
print(",")
end
print("}")
end
end
type IdTable <: Associative
ht::Array{Any,1}
IdTable(sz::Integer) = new(cell(2*_tablesz(sz)))
IdTable() = IdTable(0)
end
function assign(t::IdTable, v::ANY, k::ANY)
t.ht = ccall(:jl_eqtable_put,
Any, (Any, Any, Any), t.ht, k, v)::Array{Any,1}
return t
end
get(t::IdTable, key::ANY, default::ANY) =
ccall(:jl_eqtable_get, Any, (Any, Any, Any), t.ht, key, default)
del(t::IdTable, key::ANY) =
(ccall(:jl_eqtable_del, Int32, (Any, Any), t.ht, key); t)
del_all(t::IdTable) = (t.ht = cell(length(t.ht)); t)
start(t::IdTable) = 0
done(t::IdTable, i) = is(next(t,i),())
next(t::IdTable, i) = ccall(:jl_eqtable_next, Any, (Any, Uint32), t.ht, uint32(i))
isempty(t::IdTable) = is(next(t,0),())
# hashing
bitmix(a::Union(Int32,Uint32), b::Union(Int32,Uint32)) =
ccall(:int64to32hash, Uint32, (Uint64,),
or_int(shl_int(zext64(unbox32(a)), 32), zext64(unbox32(b))))
bitmix(a::Union(Int64,Uint64), b::Union(Int64, Uint64)) =
ccall(:int64hash, Uint64, (Uint64,),
xor_int(unbox64(a), or_int(lshr_int(unbox64(b), 32),
shl_int(unbox64(b), 32))))
if WORD_SIZE == 64
_jl_hash64(x::Union(Int64,Uint64,Float64)) =
ccall(:int64hash, Uint64, (Uint64,), boxui64(unbox64(x)))
else
_jl_hash64(x::Union(Int64,Uint64,Float64)) =
ccall(:int64to32hash, Uint32, (Uint64,), boxui64(unbox64(x)))
end
hash(x::Integer) = _jl_hash64(uint64(x))
@eval function hash(x::Float)
if trunc(x) == x
# hash as integer if equal to some integer. note the result of
# float to int conversion is only defined for in-range values.
if x < 0
if $float64(typemin(Int64)) <= x
return hash(int64(x))
end
else
# note: float64(typemax(Uint64)) == 2^64
if x < $float64(typemax(Uint64))
return hash(uint64(x))
end
end
end
isnan(x) ? $_jl_hash64(NaN) : _jl_hash64(float64(x))
end
hash(s::Symbol) = ccall(:jl_hash_symbol, Uint, (Any,), s)
function hash(t::Tuple)
h = int(0)
for i=1:length(t)
h = bitmix(h,int(hash(t[i])))
end
return uint(h)
end
function hash(a::Array)
h = hash(size(a))+1
for i=1:length(a)
h = bitmix(h,int(hash(a[i])))
end
return uint(h)
end
hash(x::Any) = uid(x)
if WORD_SIZE == 64
hash(s::ByteString) = ccall(:memhash, Uint64, (Ptr{Void}, Int), s.data, length(s.data))
hash(s::ByteString, seed::Union(Int,Uint)) = ccall(:memhash_seed, Uint64, (Ptr{Void}, Int, Uint32), s.data, length(s.data), uint32(seed))
else
hash(s::ByteString) = ccall(:memhash32, Uint32, (Ptr{Void}, Int), s.data, length(s.data))
hash(s::ByteString, seed::Union(Int,Uint)) = ccall(:memhash32_seed, Uint32, (Ptr{Void}, Int, Uint32), s.data, length(s.data), uint32(seed))
end
# hash table
type HashTable{K,V} <: Associative
keys::Array{K,1}
vals::Array{V,1}
used::IntSet
deleted::IntSet
deleter::Function
HashTable() = HashTable{K,V}(0)
HashTable(n) = (n = _tablesz(n);
new(Array(K,n), Array(V,n), IntSet(n+1), IntSet(n+1),
identity))
function HashTable(ks::Tuple, vs::Tuple)
n = length(ks)
h = HashTable{K,V}(n)
for i=1:n
h[ks[i]] = vs[i]
end
return h
end
end
HashTable() = HashTable(0)
HashTable(n::Integer) = HashTable{Any,Any}(n)
# syntax entry point
hashtable{K,V}(ks::(K...), vs::(V...)) = HashTable{K,V} (ks, vs)
hashtable{K} (ks::(K...), vs::Tuple ) = HashTable{K,Any} (ks, vs)
hashtable{V} (ks::Tuple , vs::(V...)) = HashTable{Any,V} (ks, vs)
hashtable (ks::Tuple , vs::Tuple) = HashTable{Any,Any}(ks, vs)
hashindex(key, sz) = (int(hash(key)) & (sz-1)) + 1
function rehash{K,V}(h::HashTable{K,V}, newsz)
oldk = h.keys
oldv = h.vals
oldu = h.used
oldd = h.deleted
newht = HashTable{K,V}(newsz)
for i in oldu
if !has(oldd,i)
newht[oldk[i]] = oldv[i]
end
end
h.keys = newht.keys
h.vals = newht.vals
h.used = newht.used
h.deleted = newht.deleted
return h
end
function del_all{K,V}(h::HashTable{K,V})
sz = length(h.keys)
newht = HashTable{K,V}(sz)
h.keys = newht.keys
h.vals = newht.vals
h.used = newht.used
h.deleted = newht.deleted
return h
end
function assign{K,V}(h::HashTable{K,V}, v, key)
key = convert(K,key)
v = convert(V,v)
sz = length(h.keys)
if numel(h.deleted) >= ((3*sz)>>2)
rehash(h, sz)
end
iter = 0
maxprobe = sz>>3
index = hashindex(key, sz)
orig = index
while true
if !has(h.used,index)
h.keys[index] = key
h.vals[index] = v
add(h.used, index)
return h
end
if has(h.deleted,index)
h.keys[index] = key
h.vals[index] = v
del(h.deleted, index)
return h
end
if isequal(key, h.keys[index])
h.vals[index] = v
return h
end
index = (index & (sz-1)) + 1
iter+=1
if iter > maxprobe || index==orig
break
end
end
rehash(h, sz*2)
assign(h, v, key)
end
# get the index where a key is stored, or -1 if not present
function ht_keyindex{K,V}(h::HashTable{K,V}, key)
key = convert(K,key)
sz = length(h.keys)
iter = 0
maxprobe = sz>>3
index = hashindex(key, sz)
orig = index
while true
if !has(h.used,index)
break
end
if !has(h.deleted,index) && isequal(key, h.keys[index])
return index
end
index = (index & (sz-1)) + 1
iter+=1
if iter > maxprobe || index==orig
break
end
end
return -1
end
function get(h::HashTable, key, deflt)
index = ht_keyindex(h, key)
return (index<0) ? deflt : h.vals[index]
end
function key(h::HashTable, key, deflt)
index = ht_keyindex(h, key)
return (index<0) ? deflt : h.keys[index]
end
function del(h::HashTable, key)
index = ht_keyindex(h, key)
if index > 0
add(h.deleted, index)
end
return h
end
function skip_deleted(used, deleted, i)
while !done(used, i)
(i, ip1) = next(used, i)
if !has(deleted,i)
break
end
i = ip1
end
return i
end
start(t::HashTable) = skip_deleted(t.used, t.deleted, 0)
done(t::HashTable, i) = done(t.used, i)
next(t::HashTable, i) = ((n, nxt) = next(t.used, i);
((t.keys[n],t.vals[n]),
skip_deleted(t.used,t.deleted,nxt)))
isempty(t::HashTable) = done(t, start(t))
numel(t::HashTable) = numel(t.used)-numel(t.deleted)
function add_weak_key(t::HashTable, k, v)
if is(t.deleter, identity)
t.deleter = x->del(t, x)
end
t[WeakRef(k)] = v
finalizer(k, t.deleter)
return t
end
function add_weak_value(t::HashTable, k, v)
t[k] = WeakRef(v)
finalizer(v, x->del(t, k))
return t
end
type WeakKeyHashTable{K,V} <: Associative
ht::HashTable{K,V}
WeakKeyHashTable() = new(HashTable{K,V}())
end
WeakKeyHashTable() = WeakKeyHashTable{Any,Any}()
assign(wkh::WeakKeyHashTable, v, key) = add_weak_key(wkh.ht, key, v)
function key(wkh::WeakKeyHashTable, kk, deflt)
k = key(wkh.ht, kk, _jl_secret_table_token)
if is(k, _jl_secret_table_token)
return deflt
end
return k.value
end
get(wkh::WeakKeyHashTable, key, deflt) = get(wkh.ht, key, deflt)
del(wkh::WeakKeyHashTable, key) = del(wkh.ht, key)
del_all(wkh::WeakKeyHashTable) = (del_all(wkh.ht); wkh)
has(wkh::WeakKeyHashTable, key) = has(wkh.ht, key)
ref(wkh::WeakKeyHashTable, key) = ref(wkh.ht, key)
isempty(wkh::WeakKeyHashTable) = isempty(wkh.ht)
start(t::WeakKeyHashTable) = start(t.ht)
done(t::WeakKeyHashTable, i) = done(t.ht, i)
next(t::WeakKeyHashTable, i) = next(t.ht, i)