Class: Roseflow::VectorStores::HNSWMemoryStore::PriorityQueue
- Inherits:
-
Object
- Object
- Roseflow::VectorStores::HNSWMemoryStore::PriorityQueue
- 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
- #empty? ⇒ Boolean
-
#initialize ⇒ PriorityQueue
constructor
A new instance of PriorityQueue.
-
#peek ⇒ Object
Returns the element with the smallest priority without removing it from the PriorityQueue.
-
#pop ⇒ Object
Removes and returns the element with the smallest priority.
- #push(item) ⇒ Object
- #size ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize ⇒ PriorityQueue
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
382 383 384 |
# File 'lib/roseflow/vector_stores/hnsw_memory_store.rb', line 382 def empty? @elements.empty? end |
#peek ⇒ Object
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 |
#pop ⇒ Object
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 |
#size ⇒ Object
378 379 380 |
# File 'lib/roseflow/vector_stores/hnsw_memory_store.rb', line 378 def size @elements.size end |
#to_a ⇒ Object
408 409 410 |
# File 'lib/roseflow/vector_stores/hnsw_memory_store.rb', line 408 def to_a @elements.dup end |