-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_table.rb
131 lines (107 loc) · 2.12 KB
/
hash_table.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
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
# Basic Hash Table
#
# Usage:
# => ht = HashTable.new
#
# Add a key value pair
# => ht['key'] = 'value'
# value
#
# Find a value with a key
# => ht['key']
# value
#
class HashTable
attr :array, :prime
# Prime numbers used in Hashing
PRIMES = [
2, 3, 5, 7, 11, 13, 17, 19, 23, 29
]
# Initial size of Array
SIZE = 25
# Initialize the HashTable
def initialize
@array = Array.new(SIZE)
@prime = PRIMES[rand(PRIMES.count)]
@resizing = false
end
# Lookup keys
#
# Returns value of key or nil
def lookup(key)
if key_pair = pair(key, hash(key))
key_pair[1]
end
end
alias_method :[], :lookup
# Insert value with key
#
# key: String
# value: Any Object
#
# Returns value
def insert(key, value)
index = hash(key)
if array[index].nil?
@array[index] = []
end
if key_pair = pair(key, index)
key_pair[1] = value
else
@array[index] << [key, value]
end
if should_resize?
puts "resizing: #{collisions?} - #{fifty_percent_usage?}"
resize
end
value
end
alias_method :[]=, :insert
# Any collisions?
#
# Returns Boolean
def collisions?
array.any? { |pairs| pairs && pairs.count > 1 }
end
private
def pair(key, index)
row = array[index]
if row
row.detect { |store| key == store[0] }
end
end
def hash(value)
# Simple hashing function
index = 1
int_value = value.chars.inject(0) do |sum, char|
index += 1
sum += char.ord + (prime ** index)
sum
end
int_value % @array.count
end
def high_usage?
unused_indexes = array.select { |pairs| pairs.nil? }.count
(unused_indexes.to_f / array.count.to_f) < 0.5
end
def should_resize?
high_usage?
end
def resize
resizing do
@prime = PRIMES[rand(PRIMES.count)]
old_array = @array
@array = Array.new(SIZE + old_array.count)
old_array.each do |row|
next if row.nil?
row.each { |key_pair| insert(*key_pair) }
end
end
end
def resizing(&block)
return if @resizing
@resizing = true
block.yield
@resizing = false
end
end