Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. class HMap
  2.  
  3. attr_accessor :values
  4. @values
  5.  
  6. def initialize
  7. @values = {}
  8. end
  9.  
  10. def set(args)
  11. if @values[args[:key]].nil? or @values[args[:key]].empty?
  12. @values[args[:key]] = {args[:time] => args[:value]}
  13. else
  14. @values[args[:key]][args[:time]] = args[:value]
  15. end
  16.  
  17. end
  18.  
  19. def get(args)
  20. return nil if @values[args[:key]].nil?
  21. times = @values[args[:key]].keys
  22. val = @values[args[:key]][times[0]]
  23. times[1..-1].each do |t|
  24. if t > args[:time]
  25. break
  26. else
  27. val = @values[args[:key]][t]
  28. end
  29. end
  30. val
  31. end
  32.  
  33. def to_string
  34. p @values
  35. end
  36.  
  37. end
  38.  
  39.  
  40. # set( time: 0, key: "a", value: 1 )
  41. # set( time: 2, key: "a", value: 2 )
  42. # get( time: 1, key: "a" ) --> 1
  43. # get( time: 3, key: "a" ) --> 2
  44.  
  45. # store values by key
  46. # access the values by time
  47.  
  48. # i.e.:
  49. # h['a'][1] where [1] is time
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement