Skip to content

Instantly share code, notes, and snippets.

@mrampton
Last active October 26, 2016 19:28
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 mrampton/46c8f1131cb011531bf961d8613d0cff to your computer and use it in GitHub Desktop.
Save mrampton/46c8f1131cb011531bf961d8613d0cff to your computer and use it in GitHub Desktop.
class HMap
attr_accessor :values
@values
def initialize
@values = {}
end
def set(args)
if @values[args[:key]].nil? or @values[args[:key]].empty?
@values[args[:key]] = {args[:time] => args[:value]}
else
@values[args[:key]][args[:time]] = args[:value]
end
end
def get(args)
return nil if @values[args[:key]].nil?
times = @values[args[:key]].keys
val = @values[args[:key]][times[0]]
times[1..-1].each do |t|
if t > args[:time]
break
else
val = @values[args[:key]][t]
end
end
val
end
def to_string
p @values
end
end
# set( time: 0, key: "a", value: 1 )
# set( time: 2, key: "a", value: 2 )
# get( time: 1, key: "a" ) --> 1
# get( time: 3, key: "a" ) --> 2
# store values by key
# access the values by time
# i.e.:
# h['a'][1] where [1] is time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment