Transients

Transients is a concept borrowed from Clojure, with some twists to turn make more idiomatic in C++. Essentially, they are a mutable interface built on top of the same data structures the implements the immutable containers under the hood.

These can be useful for performing efficient batch updates or interfacing with standard algorithms.

array_transient

template <typename T, typename MemoryPolicy = default_memory_policy>
class immer::array_transient

Mutable version of immer::array.

Refer to Transients to learn more about when and how to use the mutable versions of immutable containers.

Public Types

using value_type = T
using reference = const T&
using size_type = std::size_t
using difference_type = std::ptrdiff_t
using const_reference = const T&
using iterator = const T *
using const_iterator = iterator
using reverse_iterator = std::reverse_iterator<iterator>
using memory_policy = MemoryPolicy
using persistent_type = array<T, MemoryPolicy>

Public Functions

array_transient()

Default constructor. It creates a mutable array of size() == 0. It does not allocate memory and its complexity is \( O(1) \).

iterator begin() const

Returns an iterator pointing at the first element of the collection. It does not allocate memory and its complexity is \( O(1) \).

iterator end() const

Returns an iterator pointing just after the last element of the collection. It does not allocate and its complexity is \( O(1) \).

reverse_iterator rbegin() const

Returns an iterator that traverses the collection backwards, pointing at the first element of the reversed collection. It does not allocate memory and its complexity is \( O(1) \).

reverse_iterator rend() const

Returns an iterator that traverses the collection backwards, pointing after the last element of the reversed collection. It does not allocate memory and its complexity is \( O(1) \).

std::size_t size() const

Returns the number of elements in the container. It does not allocate memory and its complexity is \( O(1) \).

bool empty() const

Returns true if there are no elements in the container. It does not allocate memory and its complexity is \( O(1) \).

const T *data() const

Access the raw data.

T *data_mut()

Provide mutable access to the raw underlaying data.

const T &back() const

Access the last element.

const T &front() const

Access the first element.

reference operator[](size_type index) const

Returns a const reference to the element at position index. It is undefined when \( 0 index \geq size() \). It does not allocate memory and its complexity is effectively \( O(1) \).

reference at(size_type index) const

Returns a const reference to the element at position index. It throws an std::out_of_range exception when \( index \geq size() \). It does not allocate memory and its complexity is effectively \( O(1) \).

void push_back(value_type value)

Inserts value at the end. It may allocate memory and its complexity is effectively \( O(1) \).

void set(size_type index, value_type value)

Sets to the value value at position idx. Undefined for index >= size(). It may allocate memory and its complexity is effectively \( O(1) \).

template <typename FnT>
void update(size_type index, FnT &&fn)

Updates the array to contain the result of the expression fn((*this)[idx]) at position idx. Undefined for 0 >= size(). It may allocate memory and its complexity is effectively \( O(1) \).

void take(size_type elems)

Resizes the array to only contain the first min(elems, size()) elements. It may allocate memory and its complexity is effectively \( O(1) \).

persistent_type persistent()

Returns an immutable form of this container, an immer::array.

persistent_type persistent()

vector_transient

template <typename T, typename MemoryPolicy = default_memory_policy, detail::rbts::bits_t B = default_bits, detail::rbts::bits_t BL = detail::rbts::derive_bits_leaf<T, MemoryPolicy, B>>
class immer::vector_transient

Mutable version of immer::vector.

Refer to Transients to learn more about when and how to use the mutable versions of immutable containers.

Public Types

using memory_policy = MemoryPolicy
using value_type = T
using reference = const T&
using size_type = detail::rbts::size_t
using difference_type = std::ptrdiff_t
using const_reference = const T&
using iterator = detail::rbts::rbtree_iterator<T, MemoryPolicy, B, BL>
using const_iterator = iterator
using reverse_iterator = std::reverse_iterator<iterator>
using persistent_type = vector<T, MemoryPolicy, B, BL>

Public Functions

vector_transient()

Default constructor. It creates a mutable vector of size() == 0. It does not allocate memory and its complexity is \( O(1) \).

iterator begin() const

Returns an iterator pointing at the first element of the collection. It does not allocate memory and its complexity is \( O(1) \).

iterator end() const

Returns an iterator pointing just after the last element of the collection. It does not allocate and its complexity is \( O(1) \).

reverse_iterator rbegin() const

Returns an iterator that traverses the collection backwards, pointing at the first element of the reversed collection. It does not allocate memory and its complexity is \( O(1) \).

reverse_iterator rend() const

Returns an iterator that traverses the collection backwards, pointing after the last element of the reversed collection. It does not allocate memory and its complexity is \( O(1) \).

size_type size() const

Returns the number of elements in the container. It does not allocate memory and its complexity is \( O(1) \).

bool empty() const

Returns true if there are no elements in the container. It does not allocate memory and its complexity is \( O(1) \).

reference operator[](size_type index) const

Returns a const reference to the element at position index. It is undefined when \( 0 index \geq size() \). It does not allocate memory and its complexity is effectively \( O(1) \).

reference at(size_type index) const

Returns a const reference to the element at position index. It throws an std::out_of_range exception when \( index \geq size() \). It does not allocate memory and its complexity is effectively \( O(1) \).

void push_back(value_type value)

Inserts value at the end. It may allocate memory and its complexity is effectively \( O(1) \).

void set(size_type index, value_type value)

Sets to the value value at position idx. Undefined for index >= size(). It may allocate memory and its complexity is effectively \( O(1) \).

template <typename FnT>
void update(size_type index, FnT &&fn)

Updates the vector to contain the result of the expression fn((*this)[idx]) at position idx. Undefined for 0 >= size(). It may allocate memory and its complexity is effectively \( O(1) \).

void take(size_type elems)

Resizes the vector to only contain the first min(elems, size()) elements. It may allocate memory and its complexity is effectively \( O(1) \).

persistent_type persistent()

Returns an immutable form of this container, an immer::vector.

persistent_type persistent()

Public Static Attributes

constexpr auto bits = B
constexpr auto bits_leaf = BL

flex_vector_transient

template <typename T, typename MemoryPolicy = default_memory_policy, detail::rbts::bits_t B = default_bits, detail::rbts::bits_t BL = detail::rbts::derive_bits_leaf<T, MemoryPolicy, B>>
class immer::flex_vector_transient

Mutable version of immer::flex_vector.

Refer to Transients to learn more about when and how to use the mutable versions of immutable containers.

Public Types

using memory_policy = MemoryPolicy
using value_type = T
using reference = const T&
using size_type = detail::rbts::size_t
using difference_type = std::ptrdiff_t
using const_reference = const T&
using iterator = detail::rbts::rrbtree_iterator<T, MemoryPolicy, B, BL>
using const_iterator = iterator
using reverse_iterator = std::reverse_iterator<iterator>
using persistent_type = flex_vector<T, MemoryPolicy, B, BL>

Public Functions

flex_vector_transient()

Default constructor. It creates a flex_vector of size() == 0. It does not allocate memory and its complexity is \( O(1) \).

flex_vector_transient(vector_transient<T, MemoryPolicy, B, BL> v)

Default constructor. It creates a flex_vector with the same contents as v. It does not allocate memory and is \( O(1) \).

iterator begin() const

Returns an iterator pointing at the first element of the collection. It does not allocate memory and its complexity is \( O(1) \).

iterator end() const

Returns an iterator pointing just after the last element of the collection. It does not allocate and its complexity is \( O(1) \).

reverse_iterator rbegin() const

Returns an iterator that traverses the collection backwards, pointing at the first element of the reversed collection. It does not allocate memory and its complexity is \( O(1) \).

reverse_iterator rend() const

Returns an iterator that traverses the collection backwards, pointing after the last element of the reversed collection. It does not allocate memory and its complexity is \( O(1) \).

size_type size() const

Returns the number of elements in the container. It does not allocate memory and its complexity is \( O(1) \).

bool empty() const

Returns true if there are no elements in the container. It does not allocate memory and its complexity is \( O(1) \).

reference operator[](size_type index) const

Returns a const reference to the element at position index. It is undefined when \( 0 index \geq size() \). It does not allocate memory and its complexity is effectively \( O(1) \).

reference at(size_type index) const

Returns a const reference to the element at position index. It throws an std::out_of_range exception when \( index \geq size() \). It does not allocate memory and its complexity is effectively \( O(1) \).

void push_back(value_type value)

Inserts value at the end. It may allocate memory and its complexity is effectively \( O(1) \).

void set(size_type index, value_type value)

Sets to the value value at position idx. Undefined for index >= size(). It may allocate memory and its complexity is effectively \( O(1) \).

template <typename FnT>
void update(size_type index, FnT &&fn)

Updates the vector to contain the result of the expression fn((*this)[idx]) at position idx. Undefined for 0 >= size(). It may allocate memory and its complexity is effectively \( O(1) \).

void take(size_type elems)

Resizes the vector to only contain the first min(elems, size()) elements. It may allocate memory and its complexity is effectively \( O(1) \).

void drop(size_type elems)

Removes the first the first min(elems, size()) elements. It may allocate memory and its complexity is effectively \( O(1) \).

void append(flex_vector_transient &r)

Appends the contents of the r at the end. It may allocate memory and its complexity is: \( O(log(max(size_r, size_l))) \)

void append(flex_vector_transient &&r)
void prepend(flex_vector_transient &l)

Prepends the contents of the l at the beginning. It may allocate memory and its complexity is: \( O(log(max(size_r, size_l))) \)

void prepend(flex_vector_transient &&l)
persistent_type persistent()

Returns an immutable form of this container, an immer::flex_vector.

persistent_type persistent()

Public Static Attributes

constexpr auto bits = B
constexpr auto bits_leaf = BL

set_transient

template <typename T, typename Hash, typename Equal, typename MemoryPolicy, detail::hamts::bits_t B>
class immer::set_transient

Become a sponsor!

This component is planned but it has not been implemented yet.

Transiens can critically improve the performance of applications intensively using set and map. If you are working for an organization using the library in a commercial project, please consider sponsoring this work: juanpe@sinusoid.al

map_transient

template <typename K, typename T, typename Hash, typename Equal, typename MemoryPolicy, detail::hamts::bits_t B>
class immer::map_transient

Become a sponsor!

This component is planned but it has not been implemented yet.

Transiens can critically improve the performance of applications intensively using set and map. If you are working for an organization using the library in a commercial project, please consider sponsoring this work: juanpe@sinusoid.al