Skip to content

Commit a3f01f4

Browse files
committed
hashset implementation
1 parent fd44949 commit a3f01f4

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ print(s:delMax()) -- return 10
117117
print(s:isEmpty()) -- return true
118118
```
119119

120+
### HashSet
121+
122+
```lua
123+
local hashset = require('lualgorithms.data.hashset')
124+
local hash_func = function(x) return x % 1000 end
125+
local s = hashset.create(hash_func)
126+
s:add(100, 2)
127+
s:add(200, 4)
128+
s:add(450, 2)
129+
130+
print(s:contains(99)) -- return false
131+
print(s:contains(100)) -- return true
132+
print(s:size()) -- return 3
133+
print(s:isEmpty()) -- return false
134+
s:remove(100)
135+
print(s:contains(100)) -- return false)
136+
```
137+
120138
### HashMap
121139

122140
```lua

rockspecs/lualgorithms-1.0-5.rockspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ build = {
2626
["lualgorithms.data.minpq"] = "src/data/minpq.lua",
2727
["lualgorithms.data.maxpq"] = "src/data/maxpq.lua",
2828
["lualgorithms.data.hashmap"] = "src/data/hashmap.lua",
29+
["lualgorithms.data.hashset"] = "src/data/hashset.lua",
2930
["lualgorithms.data.redblacktree"] = "src/data/redblacktree.lua",
3031
["lualgorithms.sorting.selection"] = "src/sorting/selection.lua",
3132
["lualgorithms.sorting.insertion"] = "src/sorting/insertion.lua",

spec/hashset_spec.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--
2+
-- Created by IntelliJ IDEA.
3+
-- User: chen0
4+
-- Date: 26/6/2017
5+
-- Time: 12:03 PM
6+
-- To change this template use File | Settings | File Templates.
7+
--
8+
9+
describe('hashset', function()
10+
describe('hashset()', function()
11+
it('should put and get and remove correctly', function()
12+
local hashset = require('data.hashset')
13+
local hash_func = function(x) return x % 1000 end
14+
local s = hashset.create(hash_func)
15+
s:add(100, 2)
16+
s:add(200, 4)
17+
s:add(450, 2)
18+
19+
assert.equal(s:contains(99), false)
20+
assert.equal(s:contains(100), true)
21+
assert.equal(s:size(), 3)
22+
assert.equal(s:isEmpty(), false)
23+
s:remove(100)
24+
assert.equal(s:contains(100), false)
25+
assert.equal(s:size(), 2)
26+
s:remove(200)
27+
s:remove(450)
28+
assert.equal(s:isEmpty(), true)
29+
end)
30+
end)
31+
end)
32+

src/data/hashset.lua

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

0 commit comments

Comments
 (0)