class HClust::IndexList

Overview

An IndexList is an ordered collection of contiguous zero-based indexes.

It supports efficient iteration and removal so it performs better than an Array when there are frequent deletions.

Internally, it is implemented as a double-linked list such that deleted indexes are marked as inactive, but otherwise kept in memory. Therefore, indexing is not supported. Traversal is performed using the #each methods.

The most typical use case of a IndexList is for representing a list of nodes, where finding the closest pair is often desired. Hence, convenience methods for finding the nearest index based on a given distance metric are provided.

Defined in:

hclust/index_list.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(size : Int32) #

Creates a new IndexList with indexes in the range [0, size).


[View source]

Instance Method Detail

def delete(index : Int32) : Nil #

Removes the given index from the list if present.


[View source]
def each(& : Int32 -> ) : Nil #

Yields each index in the list.


[View source]
def each(*, within range : Range(Int32 | Nil, Int32 | Nil), skip : Int32 = 0, & : Int32 -> ) : Nil #

Yields each index in the list within the given range. It discards skip elements before yielding.


[View source]
def each(*, omit index : Int32, & : Int32 -> ) : Nil #

Yields each index except index. Useful for iterating in pairs.


[View source]
def first : Int32 #

Returns the first index. Raises Enumerable::EmptyError if the list is empty.


[View source]
def first? : Int32 | Nil #

Returns the first index or nil if the list is empty.


[View source]
def includes?(index : Int32) : Bool #

Returns true if the list includes the given index, else false.


[View source]
def nearest_to(index : Int32, dism : DistanceMatrix, & : Int32, Float64 -> Float64) : Tuple(Int32, Float64) #

Returns the nearest index to the given index based on the block's returns value. Both the index and distance taken from the distance matrix are yielded such that the block may compute a new distance if needed.


[View source]
def nearest_to(index : Int32, & : Int32 -> T) : Tuple(Int32, T) forall T #

Returns the nearest index to the given index based on the block's returns value.


[View source]
def nearest_to(index : Int32, dism : DistanceMatrix) : Tuple(Int32, Float64) #

Returns the nearest index to the given index based on the distance matrix.


[View source]
def size : Int32 #

Returns the total number of indexes in the list.


[View source]
def to_a : Array(Int32) #

Returns an Array with all the indexes in the list.


[View source]
def unsafe_succ(index) : Int32 #

Returns the succeeding index to index, without doing any bounds check.


[View source]