Class: LSH::Storage::Memory
- Inherits:
-
Object
- Object
- LSH::Storage::Memory
- Defined in:
- lib/lsh/storage/memory.rb
Instance Attribute Summary collapse
-
#buckets ⇒ Object
readonly
Returns the value of attribute buckets.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
-
#projections ⇒ Object
Returns the value of attribute projections.
Instance Method Summary collapse
- #add_vector_id(vector, id) ⇒ Object
- #add_vector_to_bucket(bucket, hash, vector) ⇒ Object
- #create_new_bucket ⇒ Object
- #find_bucket(i) ⇒ Object
- #has_index? ⇒ Boolean
- #id_to_vector(id) ⇒ Object
- #query_buckets(hashes) ⇒ Object
- #reset! ⇒ Object
- #vector_to_id(vector) ⇒ Object
Instance Attribute Details
#buckets ⇒ Object (readonly)
Returns the value of attribute buckets.
25 26 27 |
# File 'lib/lsh/storage/memory.rb', line 25 def buckets @buckets end |
#parameters ⇒ Object
Returns the value of attribute parameters.
24 25 26 |
# File 'lib/lsh/storage/memory.rb', line 24 def parameters @parameters end |
#projections ⇒ Object
Returns the value of attribute projections.
23 24 25 |
# File 'lib/lsh/storage/memory.rb', line 23 def projections @projections end |
Instance Method Details
#add_vector_id(vector, id) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/lsh/storage/memory.rb', line 48 def add_vector_id(vector, id) @vector_to_id ||= {} @vector_to_id[vector.hash] = id @id_to_vector ||= {} @id_to_vector[id] = vector end |
#add_vector_to_bucket(bucket, hash, vector) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/lsh/storage/memory.rb', line 40 def add_vector_to_bucket(bucket, hash, vector) if bucket.has_key? hash bucket[hash] << vector else bucket[hash] = [vector] end end |
#create_new_bucket ⇒ Object
35 36 37 38 |
# File 'lib/lsh/storage/memory.rb', line 35 def create_new_bucket @buckets ||= [] @buckets << {} end |
#find_bucket(i) ⇒ Object
63 64 65 |
# File 'lib/lsh/storage/memory.rb', line 63 def find_bucket(i) @buckets[i] end |
#has_index? ⇒ Boolean
27 28 29 |
# File 'lib/lsh/storage/memory.rb', line 27 def has_index? projections and parameters and @buckets end |
#id_to_vector(id) ⇒ Object
59 60 61 |
# File 'lib/lsh/storage/memory.rb', line 59 def id_to_vector(id) @id_to_vector[id] if @id_to_vector end |
#query_buckets(hashes) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/lsh/storage/memory.rb', line 67 def query_buckets(hashes) results = [] hashes.each_with_index do |hash, i| bucket = find_bucket(i) in_bucket = bucket[hash] results += in_bucket if in_bucket end results end |
#reset! ⇒ Object
31 32 33 |
# File 'lib/lsh/storage/memory.rb', line 31 def reset! @buckets = nil end |
#vector_to_id(vector) ⇒ Object
55 56 57 |
# File 'lib/lsh/storage/memory.rb', line 55 def vector_to_id(vector) @vector_to_id[vector.hash] if @vector_to_id end |