forked from DoooReyn/LuaHashMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.lua
More file actions
130 lines (120 loc) · 3.15 KB
/
map.lua
File metadata and controls
130 lines (120 loc) · 3.15 KB
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
---------------------------------
-- Author: Reyn
-- Date: 2016-07-01
-- Comment: HashMap
---------------------------------
function table.keyAsValue(...)
local arr = {...}
local ret = {}
for _,v in ipairs(arr) do
ret[v] = v
end
return ret
end
function table.len(tbl)
local count = 0
for k, v in pairs(tbl) do
count = count + 1
end
return count
end
function table.print(tbl)
local format = string.format
for k,v in pairs(tbl) do
print(format('[%s] => ', k), v)
end
end
DATA_TYPE = table.keyAsValue('boolean', 'number', 'string', 'function', 'table', 'thread', 'nil')
function checkType(v, type)
return v == DATA_TYPE[type]
end
local function checkHashType(tp)
if not (tp == 'Mixed' or DATA_TYPE[tp]) then
tp = 'Mixed'
end
return tp
end
function Map(ktype, vtype)
local new_map = {}
local __map__ = {}
local __methods__ = {}
local __key_type__, __value_type__ = checkHashType(ktype), checkHashType(vtype)
function __methods__:typeOf()
return string.format('HashMap<%s, %s>',__key_type__,__value_type__)
end
function __methods__:len()
return table.len(__map__)
end
function __methods__:set(k, v)
if (__key_type__ == 'Mixed' or type(k) == __key_type__)
and (__value_type__ == 'Mixed' or type(v) == __value_type__) then
__map__[k] = v
end
end
function __methods__:unset(k)
__map__[k] = nil
end
function __methods__:print()
table.print(__map__)
end
function __methods__:filterKey(tp)
print('filter key type:',tp)
for k,v in pairs(__map__) do
if not checkType(type(k), tp) then
__map__[k] = nil
end
end
end
function __methods__:filterValue(tp)
print('filter value type:',tp)
for k,v in pairs(__map__) do
if not checkType(type(v), tp) then
__map__[k] = nil
end
end
end
function __methods__:setKeyType(type)
if not checkType(type, nil) then
if __key_type__ == type then
return
end
__key_type__ = type
self:filterKey(type)
end
end
function __methods__:setValueType(type)
if not checkType(type, nil) then
if __value_type__ == type then
return
end
__value_type__ = type
self:filterValue(type)
end
end
function __methods__:filter(val)
for k,v in pairs(__map__) do
if v == val then
__map[k] = nil
end
end
end
local mt = {
__index = function(t, k)
if __map__[k] then
return __map__[k]
end
if __methods__[k] then
return __methods__[k]
end
end,
__newindex = function(t, k, v)
if __methods__[k] then
print('[warning] can not override native method.')
return
end
__methods__:set(k, v)
end
}
setmetatable(new_map, mt)
return new_map
end