Created
June 19, 2014 23:59
-
-
Save EmmanuelOga/349104000d3c895acbe9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Insertions, Removals and Random Access in O(1), | |
# assuming the complexity of growing an array is negligible. | |
# Otherwise a constant size array with a max-size could be used. | |
class LoadBalancer | |
def initialize | |
@map, @vector = {}, [] | |
end | |
def insert(val) | |
@map[val] = @vector.length | |
@vector << val | |
end | |
def remove(val) | |
# remove the value and the last element from the vector | |
index, last, @map[val] = @map[val], @vector.pop, nil | |
# re-insert the last element and update the map, only if it is not the one we are removing. | |
@vector[index], @map[last] = last, index if last != val | |
end | |
def getRand | |
@vector[rand @vector.length] | |
end | |
end | |
l = LoadBalancer.new | |
l.insert :a | |
l.insert :b | |
l.insert :c | |
puts '---' | |
puts l.getRand | |
puts l.getRand | |
puts l.getRand | |
puts l.getRand | |
puts l.getRand | |
puts l.getRand | |
l.remove :b | |
puts '---' | |
puts l.getRand | |
puts l.getRand | |
puts l.getRand | |
puts l.getRand | |
puts l.getRand | |
puts l.getRand | |
l.remove :a | |
puts '---' | |
puts l.getRand | |
puts l.getRand | |
puts l.getRand | |
puts l.getRand | |
puts l.getRand | |
puts l.getRand | |
l.remove :c | |
puts '---' | |
puts l.getRand | |
puts l.getRand | |
puts l.getRand | |
puts l.getRand | |
puts l.getRand | |
puts l.getRand |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment