Class: Roseflow::VectorStores::HNSWMemoryStore::PriorityQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/roseflow/vector_stores/hnsw_memory_store.rb

Overview

PriorityQueue is a data structure that keeps elements ordered by priority. It supports inserting elements, removing the element with the smallest priority, and peeking at the element with the smallest priority. It uses a binary heap as the underlying data structure.

Instance Method Summary collapse

Constructor Details

#initializePriorityQueue

Returns a new instance of PriorityQueue.



374
375
376
# File 'lib/roseflow/vector_stores/hnsw_memory_store.rb', line 374

def initialize
  @elements = []
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


382
383
384
# File 'lib/roseflow/vector_stores/hnsw_memory_store.rb', line 382

def empty?
  @elements.empty?
end

#peekObject

Returns the element with the smallest priority without removing it from the PriorityQueue.



404
405
406
# File 'lib/roseflow/vector_stores/hnsw_memory_store.rb', line 404

def peek
  @elements.first
end

#popObject

Removes and returns the element with the smallest priority. Returns nil if the PriorityQueue is empty.



393
394
395
396
397
398
399
400
# File 'lib/roseflow/vector_stores/hnsw_memory_store.rb', line 393

def pop
  return if empty?

  swap(0, @elements.size - 1)
  element = @elements.pop
  shift_down(0)
  element
end

#push(item) ⇒ Object



386
387
388
389
# File 'lib/roseflow/vector_stores/hnsw_memory_store.rb', line 386

def push(item)
  @elements << item
  shift_up(@elements.size - 1)
end

#sizeObject



378
379
380
# File 'lib/roseflow/vector_stores/hnsw_memory_store.rb', line 378

def size
  @elements.size
end

#to_aObject



408
409
410
# File 'lib/roseflow/vector_stores/hnsw_memory_store.rb', line 408

def to_a
  @elements.dup
end