Skip to content

Instantly share code, notes, and snippets.

/dict.rb Secret

Created September 15, 2015 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1583dee8ad7f81a81792 to your computer and use it in GitHub Desktop.
Save anonymous/1583dee8ad7f81a81792 to your computer and use it in GitHub Desktop.
module Dict
def Dict.new(num_buckets=256)
# creats an empty array and then creates 255 empty arrays inside that array
aDict = []
(0...num_buckets).each do |i|
aDict.push([])
end
return aDict
end
def Dict.hash_key(aDict, key)
# creates a random number by turning the key string into a number using hash method and diving it by 256
# the remainder is a number that will be used for index value in the later function
return key.hash % aDict.length
end
def Dict.get_bucket(aDict, key)
# takes the number above and finds a corresponding bucket by passing it as an index value
bucket_id = Dict.hash_key(aDict, key)
return aDict[bucket_id]
end
# here is where I am staring having issues, and I can't follow it
def Dict.get_slot(aDict, key, default=nil)
# takes whatever is in the corresponding bucket and assigns it to bucket variable (which should be empty at first?)
bucket = Dict.get_bucket(aDict, key)
# goes through each element of the bucket array
# i dont understand the k, v = kv bit.
# lets say my key is "Florida", so kv = "Florida".
# k,v = kv
# k = "Florida"
# v = nil
# where is the value of v coming from??????
# if key is equal to k then returns index of the key (yep), key itsemf (yep), and value of the key (whaat?? where is it getting it from?)
bucket.each_with_index do |kv, i|
k, v = kv
if key == k
return i, k, v
end
end
# what is the point of -1 if key is not found? isn't that an element just from the end of the array?
return -1, key, default
end
def Dict.get(aDict, key, default=nil)
# if I will pretend I understand where v value comes from, this function is easy
# it just returns the value of the key which is what we are interested in most of the time
i, k, v = Dict.get_slot(aDict, key, default=default)
return v
end
def Dict.set(aDict, key, value)
# I have very little idea of what is going on here.
# I understand what it does, I dont understand HOW
# it looks up the appropriate bucket for the key-value pair based on the key, and inserts it into the array (or hash?)
bucket = Dict.get_bucket(aDict, key)
# I don't quite understand the need for this line
i, k, v = Dict.get_slot(aDict, key)
# I can't grasp this 100%
if i >= 0
bucket[i] = [key, value]
else
bucket.push([key, value])
end
end
# again if I understood where v is coming from, maybe I could understand this.
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
# again, if I pretend I understand where the script gets v value from, this one is easy
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment