Class: Roseflow::VectorStores::HNSWMemoryStore::BoundedPriorityQueue
- Inherits:
-
Object
- Object
- Roseflow::VectorStores::HNSWMemoryStore::BoundedPriorityQueue
- Defined in:
- lib/roseflow/vector_stores/hnsw_memory_store.rb
Overview
BoundedPriorityQueue is a data structure that keeps a priority queue of a bounded size. It maintains the top-k elements with the smallest priorities. It uses an underlying PriorityQueue to store elements.
Instance Method Summary collapse
-
#initialize(max_size) ⇒ BoundedPriorityQueue
constructor
A new instance of BoundedPriorityQueue.
-
#peek ⇒ Object
Returns the item with the smallest priority without removing it from the BoundedPriorityQueue.
-
#push(item) ⇒ Object
Inserts an item into the BoundedPriorityQueue.
- #size ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(max_size) ⇒ BoundedPriorityQueue
Returns a new instance of BoundedPriorityQueue.
336 337 338 339 |
# File 'lib/roseflow/vector_stores/hnsw_memory_store.rb', line 336 def initialize(max_size) @max_size = max_size @queue = PriorityQueue.new end |
Instance Method Details
#peek ⇒ Object
Returns the item with the smallest priority without removing it from the BoundedPriorityQueue.
360 361 362 |
# File 'lib/roseflow/vector_stores/hnsw_memory_store.rb', line 360 def peek @queue.peek end |
#push(item) ⇒ Object
Inserts an item into the BoundedPriorityQueue. If the queue is full and the new item has a smaller priority than the item with the highest priority, the highest priority item is removed and the new item is added.
349 350 351 352 353 354 355 356 |
# File 'lib/roseflow/vector_stores/hnsw_memory_store.rb', line 349 def push(item) if size < @max_size @queue.push(item) elsif item[0] < @queue.peek[0] @queue.pop @queue.push(item) end end |
#size ⇒ Object
341 342 343 |
# File 'lib/roseflow/vector_stores/hnsw_memory_store.rb', line 341 def size @queue.size end |
#to_a ⇒ Object
364 365 366 |
# File 'lib/roseflow/vector_stores/hnsw_memory_store.rb', line 364 def to_a @queue.to_a end |