class HClust::IndexPriorityQueue
- HClust::IndexPriorityQueue
- Reference
- Object
Overview
An IndexPriorityQueue
is a priority queue of contiguous zero-based
indexes.
It supports efficient access to and removal of the element with highest priority (defined as the smallest value). It is designed for optimally searching nearest neighbors (priority = dissimilarity).
It is implemented as a min binary heap (where the top is the minimum element), where the indexes and priorities are also stored separately. Note that deleted indexes are marked as inactive, but otherwise kept in memory. Therefore, indexing is not supported.
Defined in:
hclust/queue.crConstructors
-
.new(size : Int32, &)
Creates a new
IndexPriorityQueue
with indexes in the range[0, size)
, invoking the given block for each index and setting its priority to the block's return value. -
.new(priorities : Array(Number)) : self
Creates a new
IndexPriorityQueue
from the given priorities with indexes in the range[0, priorities.size)
.
Instance Method Summary
-
#empty? : Bool
Returns
true
if the queue is empty, elsefalse
. -
#first : Int32
Returns the index with highest priority (smallest value).
-
#first? : Int32 | Nil
Returns the index with highest priority (smallest value), or
nil
if the queue is empty. -
#pop : Int32 | Nil
Removes and returns the index with highest priority (smallest value), or
nil
if the queue is empty. -
#priority_at(index : Int32) : Float64
Returns the priority of the element at index.
-
#set_priority_at(index : Int, priority : Float64) : Nil
Updates the priority of the element at index with the given value.
-
#size : Int32
Returns the number of indexes in the queue.
-
#to_a : Array(Int32)
Returns the indexes as an array.
Constructor Detail
Creates a new IndexPriorityQueue
with indexes in the range [0, size)
, invoking the given block for each index and setting its
priority to the block's return value.
Creates a new IndexPriorityQueue
from the given priorities with
indexes in the range [0, priorities.size)
.
Instance Method Detail
Returns the index with highest priority (smallest value). Raises
Enumerable::EmptyError
if the list is empty.
Returns the index with highest priority (smallest value), or nil
if the queue is empty.
Removes and returns the index with highest priority (smallest
value), or nil
if the queue is empty.
NOTE The queue is updated internally to restore the heap property.
Returns the priority of the element at index. Raises IndexError
if index is out of bounds or inactive (removed).
Updates the priority of the element at index with the given value.
NOTE The queue is updated internally to restore the heap property.