Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement