Class: HomeQ::OHash

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/homeq/base/ohash.rb

Instance Method Summary collapse

Constructor Details

#initializeOHash

Returns a new instance of OHash.



35
36
37
38
# File 'lib/homeq/base/ohash.rb', line 35

def initialize
  @keys = {}
  @queue = []
end

Instance Method Details

#[](key) ⇒ Object



51
52
53
# File 'lib/homeq/base/ohash.rb', line 51

def [](key)
  @keys[key]
end

#[]=(key, val) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/homeq/base/ohash.rb', line 54

def []=(key, val)
  if @keys.member?(key)
    @keys[key] = val
  else
    insert(key, val)
  end
end

#delete(key) ⇒ Object



47
48
49
50
# File 'lib/homeq/base/ohash.rb', line 47

def delete(key)
  @queue.delete(key)
  @keys.delete(key)
end

#eachObject



61
62
63
64
65
# File 'lib/homeq/base/ohash.rb', line 61

def each
  @queue.each {|k|
    yield k, @keys[k]
  }
end

#firstObject



95
96
97
# File 'lib/homeq/base/ohash.rb', line 95

def first
  [@queue.first, self[@queue.first]]
end

#insert(key, value) ⇒ Object Also known as: push



39
40
41
42
43
44
45
# File 'lib/homeq/base/ohash.rb', line 39

def insert(key, value)
  if @keys.member?(key)
    raise ArgumentException.new("Duplicate key")
  end
  @queue << key
  @keys[key] = value
end

#keysObject



66
67
68
# File 'lib/homeq/base/ohash.rb', line 66

def keys
  @keys.keys
end

#lastObject



98
99
100
# File 'lib/homeq/base/ohash.rb', line 98

def last
  [@queue.last, self[@queue.last]]
end

#lengthObject Also known as: size



69
70
71
# File 'lib/homeq/base/ohash.rb', line 69

def length
  @keys.length
end

#member?(key) ⇒ Boolean Also known as: has_key?

Returns:

  • (Boolean)


73
74
75
# File 'lib/homeq/base/ohash.rb', line 73

def member?(key)
  @keys.member?(key)
end

#popObject



77
78
79
80
# File 'lib/homeq/base/ohash.rb', line 77

def pop
  k = @queue.slice!(-1)
  [k,@keys.delete(k)]
end

#shiftObject



91
92
93
94
# File 'lib/homeq/base/ohash.rb', line 91

def shift
  k = @queue.shift
  [k,@keys.delete(k)]
end

#unshift(k, v) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/homeq/base/ohash.rb', line 81

def unshift(k,v)
  # Yes, could have called insert here and then manipulated @queue
  # after, that would be more DRY.  But this is *much* faster.
  if @keys.member?(k)
    raise ArgumentException.new("Duplicate key")
  end
  @queue.unshift(k)
  @keys[k] = v
  [k,v]
end