-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathdirect_addressing.rb
149 lines (142 loc) · 3.83 KB
/
direct_addressing.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
module HashTables
# INDEX
# direct_addressing_search
# direct_addressing_insert
# direct_addressing_delete
#
# chained_hash_insert
# chained_hash_search
# chained_hash_delete
#
# h
class << self
# Public: Get an element from provided index location
#
# t - Direct address table (Array)
# k - Key (array index)
# COMPLEXITY: O(1) BIG-O
#
# Examples
# t = [1, 2, 3, 4, 5, 6, 7]
# direct_addressing_search(t, 3)
# => 4
def direct_addressing_search(t, k)
t[k]
end
# Public: Inserts an element at the provided index location
#
# t - Direct address table (Array)
# x - Pointer to the structure/object to be INSERTED
# COMPLEXITY: O(1) BIG-O
#
# Examples
# t = [nil, nil, nil]
# x = Node.new(2, "HELLO", nil, nil)
# direct_addressing_insert(t, x)
# => #<Node:0x007ffeac99e0c8 @key=2, @satellite_data="HELLO", @prev=nil, @next=nil>
# NOTE: t is changed to [nil, nil, #<Node:0x007ffeac99e0c8 @key=2, @satellite_data="HELLO", @prev=nil, @next=nil>]
def direct_addressing_insert(t, x)
t[x.key] = x
end
# Public: Remove the element from the direct address table
#
# t - Direct address table (Array)
# x - Pointer to the structure/object to be DELETED
# COMPLEXITY: O(1) BIG-O
#
# Examples
# t = [nil, nil, x]
# direct_addressing_delete(t, x)
# => nil
# NOTE: t is changed to [nil, nil, nil]
def direct_addressing_delete(t, x)
t[x.key] = nil
end
# Public: Return the remainder of a value when divided by another value
#
# k - Dividend
# m - Divisor
#
# Examples
#
# h(10, 13)
# => 10
def h(k, m=5)
k % m
end
# Public: Inserts the structure at the index value obtained from the hash function
# Achieves chaining using linked list strategy
# Inserts element at the start of the list
#
# t - Direct address table (Array)
# x - Pointer to the structure/object to be INSERTED
# COMPLEXITY: O(1) BIG-O
#
# Examples
#
# t = [nil, nil, nil]
# x = Node.new(2, "HELLO", nil, nil)
# chained_hash_insert(t, x)
# => nil
def chained_hash_insert(t, x)
index = h(x.key)
head = t[index]
if head
t[index] = x
x.next = head
head.prev = x
else
t[index] = x
x.next = nil
x.prev = nil
end
end
# Public: Searches the Direct addressing structure for the presence of an entry
# whose key property == k
#
# t - Direct address table (Array)
# k - Key
# COMPLEXITY: Θ(1+α) BIG-THETA; α = n/m; n=total number of elements; m=table size
#
# Examples
#
# t = [nil, nil, nil]
# x = Node.new(2, "HELLO", nil, nil)
# t[1] = x
# chained_hash_search(t, 2)
# => true
def chained_hash_search(t, k)
head = t[h(k)]
while !head.nil?
return true if head.key == k
head = head.next
end
false
end
# Public: Deletes the provided node from the chained list
#
# t - Direct address table (Array)
# x - Pointer to the structure/object to be DELETED
# COMPLEXITY: O(1) BIG-O
# NOTE: O(1) is achieved because a DOUBLE LINKED LIST is used
#
# Examples
#
# t = [nil, nil, #<Node:0x007ffeac99e0c8 @key=2, @satellite_data="HELLO", @prev=nil, @next=nil>]
# x = t[2]
# chained_hash_delete(t, x)
# => nil
# NOTE: t is changed to [nil, nil, nil]
def chained_hash_delete(t, x)
index = h(x.key)
if t[index] != x
x.prev.next = x.next
x.next.prev = x.prev unless x.next.nil?
else
t[index] = x.next
x.next.prev = nil if x.next
end
x = nil
end
end
end