ArraysRRBVector (ArraysRRBVector v0.1.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 value 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 value over a vector, returning another vector.

Note that the current implementation is relatively slow, as we turn the whole vector in a list, map over this list, and then turn the list back into a vector. A more effient implementation is probably possible, but a bit tricky to write. (PR's welcome! 😉)

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]>