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(vector, id) ⇒ Object
- #add_vector_id_to_bucket(bucket, hash, vector_id) ⇒ Object
- #create_new_bucket ⇒ Object
- #find_bucket(i) ⇒ Object
- #generate_id ⇒ Object
- #has_index? ⇒ Boolean
- #id_to_vector(id) ⇒ Object
-
#initialize ⇒ Memory
constructor
A new instance of Memory.
- #query_buckets(hashes) ⇒ Object
- #reset! ⇒ Object
Constructor Details
#initialize ⇒ Memory
Returns a new instance of Memory.
27 28 29 |
# File 'lib/lsh/storage/memory.rb', line 27 def initialize reset! end |
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(vector, id) ⇒ Object
49 50 51 |
# File 'lib/lsh/storage/memory.rb', line 49 def add_vector(vector, id) @vectors[id] = vector end |
#add_vector_id_to_bucket(bucket, hash, vector_id) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/lsh/storage/memory.rb', line 53 def add_vector_id_to_bucket(bucket, hash, vector_id) if bucket.has_key? hash bucket[hash] << vector_id else bucket[hash] = [vector_id] end end |
#create_new_bucket ⇒ Object
41 42 43 |
# File 'lib/lsh/storage/memory.rb', line 41 def create_new_bucket @buckets << {} end |
#find_bucket(i) ⇒ Object
65 66 67 |
# File 'lib/lsh/storage/memory.rb', line 65 def find_bucket(i) @buckets[i] end |
#generate_id ⇒ Object
45 46 47 |
# File 'lib/lsh/storage/memory.rb', line 45 def generate_id @next_id += 1 end |
#has_index? ⇒ Boolean
31 32 33 |
# File 'lib/lsh/storage/memory.rb', line 31 def has_index? projections and parameters and @buckets end |
#id_to_vector(id) ⇒ Object
61 62 63 |
# File 'lib/lsh/storage/memory.rb', line 61 def id_to_vector(id) @vectors[id] end |
#query_buckets(hashes) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/lsh/storage/memory.rb', line 69 def query_buckets(hashes) result_ids = {} hashes.each_with_index do |hash, i| vectors_hashes_in_bucket = @buckets[i][hash] if vectors_hashes_in_bucket vectors_hashes_in_bucket.each do |vector_id| result_ids[vector_id] = true end end end result_ids.keys.map do |vector_id| { :data => @vectors[vector_id], :id => vector_id, } end end |
#reset! ⇒ Object
35 36 37 38 39 |
# File 'lib/lsh/storage/memory.rb', line 35 def reset! @buckets = [] @vectors = {} @next_id = 0 end |