Skip to content

Commit 611764a

Browse files
committed
separate chaining hashmap implementation
1 parent 3815f4e commit 611764a

File tree

4 files changed

+119
-31
lines changed

4 files changed

+119
-31
lines changed

src/data/hashedmap.lua

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/data/hashmap.lua

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
--
2+
-- Created by IntelliJ IDEA.
3+
-- User: chen0
4+
-- Date: 26/6/2017
5+
-- Time: 12:48 AM
6+
-- To change this template use File | Settings | File Templates.
7+
--
8+
9+
local hashmap = {}
10+
11+
hashmap.__index = hashmap
12+
13+
hashmap.Node = {}
14+
hashmap.Node.__index = hashmap.Node
15+
16+
function hashmap.Node.create(key, value)
17+
local s = {}
18+
setmetatable(s, hashmap.Node)
19+
20+
s.key = key
21+
s.value = value
22+
s.next = nil
23+
24+
return s
25+
end
26+
27+
function hashmap.create(hash)
28+
local s = {}
29+
setmetatable(s, hashmap)
30+
31+
if hash == nil then
32+
hash = function(x) return x end
33+
end
34+
35+
s.s = {}
36+
s.N = 0
37+
38+
return s
39+
end
40+
41+
function hashmap:put(key, value)
42+
local h = self:hash(key)
43+
local x = self.s[h]
44+
local found = false
45+
while x ~= nil do
46+
if x.key == key then
47+
found = true
48+
x.value = value
49+
break
50+
end
51+
x = x.next
52+
end
53+
54+
if found == false then
55+
local old = self.s[h]
56+
self.s[h] = hashmap.Node.create(key,value)
57+
self.s[h].next = old
58+
self.N = self.N + 1
59+
end
60+
61+
end
62+
63+
function hashmap:get(key)
64+
local h = self:hash(key)
65+
local x = self.s[h]
66+
while x ~= nil do
67+
if x.key == key then
68+
return x.value
69+
end
70+
x = x.next
71+
end
72+
return nil
73+
end
74+
75+
function hashmap:containsKey(key)
76+
local h = self:hash(key)
77+
local x = self.s[h]
78+
while x ~= nil do
79+
if x.key == key then
80+
return true
81+
end
82+
x = x.next
83+
end
84+
return false
85+
end
86+
87+
function hashmap:size()
88+
return self.N
89+
end
90+
91+
function hashmap:isEmpy()
92+
return self.N == 0
93+
end
94+
95+
function hashmap:remove(key)
96+
local h = self:hash(key)
97+
local x = self.s[h]
98+
local prev_x = nil
99+
while x ~= nil do
100+
if x.key == key then
101+
local value = x.value
102+
if prev_x == nil then
103+
self.s[h] = x.next
104+
else
105+
prev_x.next = x.next
106+
end
107+
return value
108+
end
109+
prev_x = x
110+
x = x.next
111+
end
112+
113+
return nil
114+
end
115+
116+
return hashmap
117+

src/data/list.lua

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,7 @@ end
9999
function list.ArrayList:resize(newSize)
100100
local temp = {}
101101
for i = 0,(newSize-1) do
102-
if self.a[i] == nil then
103-
temp[i] = nil
104-
else
105-
temp[i] = self.a[i]
106-
end
102+
temp[i] = self.a[i]
107103
end
108104

109105
self.a = temp

src/data/stack.lua

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,7 @@ end
100100
function stack.ArrayStack:resize(newSize)
101101
local temp = {}
102102
for i = 0,(newSize-1) do
103-
if self.a[i] == nil then
104-
temp[i] = nil
105-
else
106-
temp[i] = self.a[i]
107-
end
103+
temp[i] = self.a[i]
108104
end
109105

110106
self.a = temp

0 commit comments

Comments
 (0)