Last active
October 26, 2016 19:28
-
-
Save mrampton/46c8f1131cb011531bf961d8613d0cff 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
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