-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_set.rb
74 lines (58 loc) · 1.23 KB
/
hash_set.rb
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
# The provided implementation is a basic hash set that uses separate
# chaining for handling hash collisions.
class HashSet
attr_reader :count
def initialize(num_buckets = 8)
@store = Array.new(num_buckets) { [] }
@count = 0
@load_factor = 0.75
end
def insert(key)
return false if include?(key)
self[bucket_index(key)] << key
@count += 1
resize! if needs_resizing?
key
end
def include?(key)
self[bucket_index(key)].include?(key)
end
def remove(key)
return nil unless include?(key)
self[bucket_index(key)].delete(key)
@count -= 1
end
private
def num_buckets
@store.length
end
def needs_resizing?
@count.to_f / num_buckets >= @load_factor
end
def resize!
old_store = @store
@count = 0
@store = Array.new(num_buckets * 2) { [] }
old_store.flatten.each { |key| insert(key) }
end
def bucket_index(key)
key.hash % num_buckets
end
def [](num)
@store[num]
end
end
if $PROGRAM_NAME == __FILE__
set = HashSet.new
set.insert(1)
set.insert('hey')
puts set.include?(1) # => true
puts set.include?(2) # => false
set.insert(-300)
set.insert(2)
set.insert(3)
set.insert(400)
set.insert(-500)
set.insert('hi')
p set
end