Class: DoubleKeyMap
Overview
Sometimes we need to map a key to a value but key is two pieces of data.
This nested hash table saves creating a single key each time we access
map; avoids mem creation.
Instance Method Summary collapse
- #get(k1, k2 = nil) ⇒ Object
-
#initialize ⇒ DoubleKeyMap
constructor
A new instance of DoubleKeyMap.
-
#keySet(k1 = nil) ⇒ Object
/** get all secondary keys associated with a primary key */.
- #put(k1, k2, v) ⇒ Object
-
#values(k1) ⇒ Object
/** Get all values associated with primary key */.
Constructor Details
#initialize ⇒ DoubleKeyMap
Returns a new instance of DoubleKeyMap.
35 36 37 38 |
# File 'lib/double_key_map.rb', line 35 def initialize # Map<Key1, Map<Key2, Value>> data = new LinkedHashMap<Key1, Map<Key2, Value>>(); @data = Hash.new end |
Instance Method Details
#get(k1, k2 = nil) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/double_key_map.rb', line 53 def get(k1, k2=nil) data2 = @data[k1] return nil if data2.nil? if k2.nil? then data2 else data2[k2] end end |
#keySet(k1 = nil) ⇒ Object
/** get all secondary keys associated with a primary key */
71 72 73 74 75 76 77 |
# File 'lib/double_key_map.rb', line 71 def keySet(k1=nil) # /** get all primary keys return @data.keys if k1.nil? data2 = @data[k1] return nil if data2.nil? data2.keys end |
#put(k1, k2, v) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/double_key_map.rb', line 40 def put(k1, k2, v) data2 = @data.get(k1) prev = nil if data2.nil? then data2 = Hash.new @data[k1] = data2 else prev = data2[k2] end data2[k2] = v return prev; end |
#values(k1) ⇒ Object
/** Get all values associated with primary key */
64 65 66 67 68 |
# File 'lib/double_key_map.rb', line 64 def values(k1) data2 = @data[k1] return nil if data2.nil? data2.values(); end |