Class: LSH::Storage::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/lsh/storage/memory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bucketsObject (readonly)

Returns the value of attribute buckets.



25
26
27
# File 'lib/lsh/storage/memory.rb', line 25

def buckets
  @buckets
end

#parametersObject

Returns the value of attribute parameters.



24
25
26
# File 'lib/lsh/storage/memory.rb', line 24

def parameters
  @parameters
end

#projectionsObject

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, vector_hash) ⇒ Object



43
44
45
46
# File 'lib/lsh/storage/memory.rb', line 43

def add_vector(vector, vector_hash)
  @vectors ||= {}
  @vectors[vector_hash] = vector
end

#add_vector_hash_to_bucket(bucket, hash, vector_hash) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/lsh/storage/memory.rb', line 48

def add_vector_hash_to_bucket(bucket, hash, vector_hash)
  if bucket.has_key? hash
    bucket[hash] << vector_hash
  else
    bucket[hash] = [vector_hash]
  end
end

#add_vector_id(vector_hash, id) ⇒ Object



56
57
58
59
60
61
# File 'lib/lsh/storage/memory.rb', line 56

def add_vector_id(vector_hash, id)
  @vector_hash_to_id ||= {}
  @vector_hash_to_id[vector_hash] = id
  @id_to_vector ||= {}
  @id_to_vector[id] = vector_hash
end

#create_new_bucketObject



38
39
40
41
# File 'lib/lsh/storage/memory.rb', line 38

def create_new_bucket
  @buckets ||= []
  @buckets << {}
end

#find_bucket(i) ⇒ Object



71
72
73
# File 'lib/lsh/storage/memory.rb', line 71

def find_bucket(i)
  @buckets[i]
end

#has_index?Boolean

Returns:

  • (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



67
68
69
# File 'lib/lsh/storage/memory.rb', line 67

def id_to_vector(id)
  @vectors[@id_to_vector[id]] if @id_to_vector
end

#query_buckets(hashes) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/lsh/storage/memory.rb', line 75

def query_buckets(hashes)
  results_hashes = {}
  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_hash|
        results_hashes[vector_hash] = true
      end
    end
  end
  results_hashes.keys.map do |vector_hash|
    { 
      :data => @vectors[vector_hash], 
      :hash => vector_hash, 
      :id => vector_hash_to_id(vector_hash)
    }
  end
end

#reset!Object



31
32
33
34
35
36
# File 'lib/lsh/storage/memory.rb', line 31

def reset!
  @buckets = nil
  @vectors = nil
  @vector_hash_to_id = nil
  @id_to_vector = nil
end

#vector_hash_to_id(vector_hash) ⇒ Object



63
64
65
# File 'lib/lsh/storage/memory.rb', line 63

def vector_hash_to_id(vector_hash)
  @vector_hash_to_id[vector_hash] if @vector_hash_to_id
end