-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.lua
200 lines (179 loc) · 4.4 KB
/
hash.lua
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
local ffi = require 'ffi'
local tds = require 'tds.env'
local elem = require 'tds.elem'
local C = tds.C
-- hash-independent temporary buffers
local key__ = C.tds_elem_new()
local val__ = C.tds_elem_new()
ffi.gc(key__, C.tds_elem_free)
ffi.gc(val__, C.tds_elem_free)
local hash = {}
local NULL = not jit and ffi.C.NULL or nil
local function isvec(tbl)
for k, v in pairs(tbl) do
if type(k) ~= 'number' then
return false
end
end
return true
end
local function fill(self, tbl)
for key, val in pairs(tbl) do
if type(key) == 'table' then
if isvec(key) then
key = tds.Vec(key)
else
key = tds.Hash(key)
end
end
if type(val) == 'table' then
if isvec(val) then
self[key] = tds.Vec(val)
else
self[key] = tds.Hash(val)
end
else
self[key] = val
end
end
end
function hash:__new(...) -- beware of the :
local self = C.tds_hash_new()
if self == NULL then
error('unable to allocate hash')
end
self = ffi.cast('tds_hash&', self)
if select('#', ...) == 1 and type(select(1, ...)) == 'table' then
fill(self, select(1, ...))
elseif select('#', ...) > 0 then
error('lua table or nothing expected')
end
ffi.gc(self, C.tds_hash_free)
return self
end
function hash:__newindex(lkey, lval)
assert(self)
assert(lkey or type(lkey) == 'boolean', 'hash index is nil')
elem.set(key__, lkey)
local notnil
if lval or type(lval) == 'boolean' then
elem.set(val__, lval); notnil = true
end
if C.tds_hash_insert(self, key__, notnil and val__ or NULL) == 1 then
error('out of memory')
end
end
function hash:__index(lkey)
local lval
assert(self)
assert(lkey or type(lkey) == 'boolean', 'hash index is nil')
elem.set(key__, lkey)
if C.tds_hash_search(self, key__, val__) == 0 then
lval = elem.get(val__)
end
return lval
end
function hash:__len()
assert(self)
return tonumber(C.tds_hash_size(self))
end
function hash:__pairs()
assert(self)
local iterator = C.tds_hash_iterator_new(self)
ffi.gc(iterator, C.tds_hash_iterator_free)
return function()
if C.tds_hash_iterator_next(iterator, key__, val__) == 0 then
local lkey = elem.get(key__)
local lval = elem.get(val__)
return lkey, lval
end
end
end
ffi.metatype('tds_hash', hash)
if pcall(require, 'torch') and torch.metatype then
function hash:__write(f)
f:writeLong(#self)
for k,v in pairs(self) do
f:writeObject(k)
f:writeObject(v)
end
end
function hash:__read(f)
local n = f:readLong()
for i=1,n do
local k = f:readObject()
local v = f:readObject()
self[k] = v
end
end
hash.__factory = hash.__new
hash.__version = 0
torch.metatype('tds.Hash', hash, 'tds_hash&')
-- legacy support (loading old models)
local old_hash = {
__factory = hash.__new,
}
torch.metatype('tds_hash', old_hash)
end
function hash:__tostring()
local str = {}
table.insert(str, string.format('tds.Hash[%d]{', #self))
local function key2str(k)
if type(k) == 'string' or type(k) == 'number' or type(k) == 'boolean' then
return tostring(k)
elseif torch then
return torch.type(k)
else
return type(k)
end
end
local ksz = 0
local idx = 0
for k,v in pairs(self) do
ksz = math.max(ksz, #key2str(k))
idx = idx + 1
if idx == 20 then
break
end
end
idx = 0
for k,v in pairs(self) do
local kstr = key2str(k)
kstr = string.format("%s%s : ", kstr, string.rep(' ', ksz-#kstr))
local vstr = tostring(v) or type(v)
local sp = string.rep(' ', ksz+3)
local i = 0
vstr = vstr:gsub(
'([^\n]+)',
function(line)
i = i + 1
if i == 1 then
return kstr .. line
else
return sp .. line
end
end
)
table.insert(str, vstr)
idx = idx + 1
if idx == 20 then
table.insert(str, '...')
break
end
end
table.insert(str, '}')
return table.concat(str, '\n')
end
-- table constructor
local hash_ctr = {}
setmetatable(
hash_ctr,
{
__index = hash,
__newindex = hash,
__call = hash.__new
}
)
tds.hash = hash_ctr
tds.Hash = hash_ctr
return hash_ctr