Hypex v1.1.0 Hypex.Array

This module provides a Hypex register implementation using an Erlang Array under the hood.

Using an Array switches out the memory efficiency of the Bitstring implementation for performance, operating at 10x the throughput of Bitstring on updates.

Even though this implementation uses higher amounts of memory, it’s still pretty low-cost and as such is the default register module for Hypex. Typically only those working in memory-constrained environments should consider the Bitstring register.

Summary

Functions

Takes a list of bits and converts them to an Array

Returns a bit from the list of registers

Creates a new Array with a size of 2 ^ width with all elements initialized to 0

Converts a list of registers into a provided accumulator

Sets a bit inside the list of registers

Converts an Array register implementation to a list of bits

Types

array :: :array.array(number)

Functions

from_list(bits)

Specs

from_list([bit :: number]) :: array

Takes a list of bits and converts them to an Array.

The Array has it’s size fixed before being returned just for some extra safety.

get_value(registers, idx, width)

Specs

get_value(array, idx :: number, width :: number) :: result :: number

Returns a bit from the list of registers.

init(width)

Specs

init(width :: number) :: array

Creates a new Array with a size of 2 ^ width with all elements initialized to 0.

reduce(registers, width, acc, fun)

Specs

reduce(array, width :: number, accumulator :: any, (number, any -> any)) :: accumulator :: any

Converts a list of registers into a provided accumulator.

Internally we pass everything to :array.foldl/3, as there’s already a native implementation for accumulation.

set_value(registers, idx, width, value)

Specs

set_value(array, idx :: number, width :: number, value :: number) :: array

Sets a bit inside the list of registers.

to_list(registers)

Specs

to_list(array) :: [bit :: number]

Converts an Array register implementation to a list of bits.

We can just delegate to the internal Array implementation as it provides the functionality we need built in.