-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdict.rb
More file actions
98 lines (75 loc) · 3.11 KB
/
dict.rb
File metadata and controls
98 lines (75 loc) · 3.11 KB
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
module Dict
def Dict.new(num_buckets=256)
# Initializes a Dict with the given number of buckets.
aDict = []
(0...num_buckets).each do |i|
aDict.push([])
end
return aDict
end
def Dict.hash_key(aDict, key)
# Given a key, this will create a number and then convert it to an index for the aDict's buckets.
return key.hash % aDict.length
end
def Dict.get_bucket(aDict, key)
# Given a key, find the bucket where it would go.
bucket_id = Dict.hash_key(aDict, key)
return aDict[bucket_id]
end
def Dict.get_slot(aDict, key, default=nil)
# Returns the index, key, and value of a slot found in a bucket.
bucket = Dict.get_bucket(aDict, key)
bucket.each_with_index do |kv, i|
k, v = kv
if key == k
return i, k, v
end
end
return -1, key, default
end
def Dict.get(aDict, key, default=nil)
# Gets the value in a bucket for the given key, or default.
i, k, v = Dict.get_slot(aDict, key, default=default)
return v
end
def Dict.set(aDict, key, value)
# Sets the key to the value, replacing any existing value.
bucket = Dict.get_bucket(aDict, key)
i, k, v = Dict.get_slot(aDict, key)
if i >= 0
bucket[i] = [key, value]
else
bucket.push([key, value])
end
end
def Dict.delete(aDict, key)
# Deletes the given key from the Dict.
bucket = Dict.get_bucket(aDict, key)
(0...bucket_length).each do |i|
k, v = bucket[i]
if key == k
bucket.delete_at(i)
break
end
end
end
def Dict.list(aDict)
# prints out what's in the Dict.
aDict.each do |bucket|
if bucket
bucket.each {|k, v| puts k, v}
end
end
end
end
# This code is an array of buckets, which themselves are an array of slots containing key/value pairs.
# The data structure looks like this - aDict(bucket(slot(key/value)))
# So in the code above, we are converting a key to an integer, using the hash function to divide the hash by the number of buckets existing currently in aDict.
# This gives it a number to be used as an index (location) It does this by using the length of the key. Then we have code to try get this bucket from aDict array of buckets, then navigate THAT so we can find the slot containing the key we are looking for.
# aDict.length determines the number of buckets inside aDict
# get_bucket will use the hash key to find the bucket the key is in.
# get_slot will cycle through every possible slot until it finds a matching key (if key == k line of code)
# get will use get_slot to find a value
# set will allow you to set a key/value pair. It does this by first checking to see if it exists already, and if it doesn't, then it appends the k/v. If it already exists, it will replace what is already there.
# delete will look for the correct bucket then remove the k/v from it.
# list just iterates over everything and prints them all out.