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.
Returns an empty RRBVector.
iex> ArraysRRBVector.empty() #ArraysRRBVector<[]>
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! 😉)
The number of elements in vector
.
iex> ArraysRRBVector.size(ArraysRRBVector.empty()) 0
iex> ArraysRRBVector.size(ArraysRRBVector.append(ArraysRRBVector.empty(), 42)) 1
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]>