Class: Matching::HashIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/matching/hash_index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHashIndex

Returns a new instance of HashIndex.



6
7
8
9
# File 'lib/matching/hash_index.rb', line 6

def initialize
  #one hash for each attribute
  @hashes = {}
end

Instance Attribute Details

#hashesObject (readonly)

Returns the value of attribute hashes.



4
5
6
# File 'lib/matching/hash_index.rb', line 4

def hashes
  @hashes
end

Instance Method Details

#get(attr, val) ⇒ Object

Return an array of object ids for a given attribute and value



20
21
22
# File 'lib/matching/hash_index.rb', line 20

def get(attr, val)
  (@hashes[attr] ? @hashes[attr][val] : nil)
end

#put(attr, val, id) ⇒ Object

Add a value to the index for a given attribute and object id



12
13
14
15
16
17
# File 'lib/matching/hash_index.rb', line 12

def put(attr, val, id)
  unless val.nil?
    h = @hashes[attr] || (@hashes[attr] = {})
    (h[val] ? h[val] << id : h[val] = [id])
  end
end