class Pf::Kit::HybridArray(T, N)

Overview

A dynamic array that stores N x Ts in a fixed-size buffer (most often allocated on the stack), and spills over to heap.

Inspiration: One of Walter Bright talks, at https://www.youtube.com/watch?v=_PB6Hdi4R7M&t=2606s

HybridArray is a reference type to limit hazardous usage by default. Note that it is a hazardous type anyway, so you're probably better off using a normal Array, unless you're working on the moderate to deep sub-microsecond time-scale.

The sub-microsecond time-scale starts to require things like these the lower you go. Consider HybridArray that odd-looking fish living at the bottom of the Mariana Trench, hyper-optimized for its particular niche but otherwise an abomination.

If you are using a HybridArray with a stack-allocated buffer, it makes little sense to not stack-allocate the HybridArray itself as well.

You can use the Pf::Kit.stack_array macro to allocate a hybrid array and its buffer on the stack. See it for more info.

Included Modules

Defined in:

permafrost/kit/hybrid_array.cr

Constructors

Instance Method Summary

Instance methods inherited from module Enumerable(T)

to_pf_bidi to_pf_bidi, to_pf_map(& : T -> Tuple(K, V)) : Pf::Map(K, V) forall K, V
to_pf_map
to_pf_map
, to_pf_set : Pf::Set(T) to_pf_set, to_pf_uset32 : Pf::USet32 to_pf_uset32

Constructor Detail

def self.new(buffer : Pointer(T)) #

WARNING There are no checks making sure N = buffer size (in fact, buffer has no known size or "size" at all at this point!)


[View source]

Instance Method Detail

def <<(value : T) : self #

Inserts value at the back of this array.


[View source]
def ==(other : HybridArray(T, N)) : Bool #

[View source]
def clear : Nil #

Removes all values from this array.


[View source]
def inspect(io) #

[View source]
def pop : T #

Removes and returns the last value of this array. Raises IndexError if this array is empty.


[View source]
def pop? : T | Nil #

Removes and returns the last value of this array. Returns nil if this array is empty.


[View source]
def pretty_print(pp) : Nil #

[View source]
def push(value : T) : self #

Inserts value at the back of this array.


[View source]
def size : Int32 #
Description copied from module Indexable(T)

Returns the number of elements in this container.


[View source]
def to_s(io : IO) : Nil #
Description copied from class Reference

Appends a short String representation of this object which includes its class name and its object address.

class Person
  def initialize(@name : String, @age : Int32)
  end
end

Person.new("John", 32).to_s # => #<Person:0x10a199f20>

[View source]
def unsafe_fetch(index : Int) : T #
Description copied from module Indexable(T)

Returns the element at the given index, without doing any bounds check.

Indexable makes sure to invoke this method with index in 0...size, so converting negative indices to positive ones is not needed here.

Clients never invoke this method directly. Instead, they access elements with #[](index) and #[]?(index).

This method should only be directly invoked if you are absolutely sure the index is in bounds, to avoid a bounds check for a small boost of performance.


[View source]
def unsafe_put(index : Int, value : T) : Nil #
Description copied from module Indexable::Mutable(T)

Sets the element at the given index to value, without doing any bounds check.

Indexable::Mutable makes sure to invoke this method with index in 0...size, so converting negative indices to positive ones is not needed here.

Clients never invoke this method directly. Instead, they modify elements with #[]=(index, value).

This method should only be directly invoked if you are absolutely sure the index is in bounds, to avoid a bounds check for a small boost of performance.


[View source]
def usize : UInt32 #

Returns the underlying UInt32 size of this array.

#size simply converts it to Int32, which is what Crystal's standard library expects.


[View source]