ArraysRRBVector (ArraysRRBVector v0.3.0) View Source

A NIF-based immutable array, based on a 'Relaxed Radix Balanced Trie Vector'.

This Elixir module wraps the vector exposed by the Rust library im. This is a persistent vector datastructure that contains the following optimizations:

  • Smart head/tail chunking
  • The ability to do updates in-place when there is only one owner of a particular (part of a) vector.

Most operations run either in O(1), amortized O(1), or O(log64(n)).

Dealing with NIFs

Because this library is implemented in Rust code, wrapped by a bunch of 'Natively Implemented Function's, using the Rustler library for interop, there are some things to keep in mind:

  • This module does not support hot-code reloading. (NIFs can theoretically support it, but Rustler currently does not)
  • The arrays created in this module support all Elixir/Erlang terms. For most terms this is performant. Because of limitations of the NIF interface, storing/reading the following terms to/from arrays has a bit more overhead:
  • References
  • Functions
  • Integers which are larger than what fits in a 64-bit signed number.
  • Ports

Link to this section Summary

Functions

Appends an element to an RRBVector.

Returns an empty RRBVector.

Maps a function over a vector, returning another vector.

The number of elements in vector.

Extracts a contiguous subsequence of elements from the RRBVector, and returns it as its own RRBVector.

Link to this section Types

Specs

t() :: %ArraysRRBVector{handle: term()}

Link to this section Functions

Appends an element to an RRBVector.

Link to this function

append_impl(vector, item)

View Source

Returns an empty RRBVector.

iex> ArraysRRBVector.empty() #ArraysRRBVector<[]>

Link to this function

get(arrays_rrb_vector, index)

View Source

Maps a function over a vector, returning another vector.

Calls the elixir function by transferring the vector in chunks between Rust and Elixir, which means that it is reasonably performant.

(Internally, uses RustlerElixirFun to accomplish this.)

iex> ArraysRRBVector.new(1..10) |> ArraysRRBVector.map(fn x -> x + 2 end) #ArraysRRBVector<[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]>

Link to this function

reduce(vector, acc, fun)

View Source
Link to this function

reduce_right(vector, acc, fun)

View Source
Link to this function

replace(arrays_rrb_vector, index, value)

View Source
Link to this function

resize(arrays_rrb_vector, size, default \\ nil)

View Source

The number of elements in vector.

iex> ArraysRRBVector.size(ArraysRRBVector.empty()) 0

iex> ArraysRRBVector.size(ArraysRRBVector.append(ArraysRRBVector.empty(), 42)) 1

Link to this function

slice(arrays_rrb_vector, lower, amount)

View Source

Extracts a contiguous subsequence of elements from the RRBVector, and returns it as its own RRBVector.

iex> ArraysRRBVector.new(1..10) |> ArraysRRBVector.slice(2, 3) #ArraysRRBVector<[3, 4, 5]>