Array

template<typename T>
class Array

The Array class correspond to a plain contiguous array that supports 64-bit indexing and is aligned to the SIMD width (e.g., 256-bit for AVX2). The Array have a similar behaviour as the std::vector class.

This object also provides a (random access) iterator to work with the STL routines, and range-based loops.

See Array in Basic Datatypes for more information.

Template Parameters:

  • T - Type of the elements in the container.

Constructor

Array()

Default Constructor. Creates an empty Array.

Complexity: Constant

Array(index_t size)

Creates an Array and then initialise the elements to its default value.

Complexity: Constant

Parameters:

size[in] Array size

Throws:

std::length_error – if `size < 0`

Array(index_t size, const T &val)

Creates an Array and then fills the container with val.

Complexity: Linear in size

Parameters:
  • size[in] Array size

  • val[in] value to initialize the Array with

Throws:

std::length_error – if `size < 0`

Array(std::initializer_list<T> ilist)

Creates an Array from an initializer list.

Complexity: Linear in ilist size

Parameters:

ilist[in] initializer list to use as data source

template<typename OtherT>
Array(const Array<OtherT> &other)

Copy Constructor. Creates an Array and then copies the content from other.

Complexity: Linear in other.size()

Parameters:

other[in] another Array to use as data source

Array(const Array &other)

Copy Constructor. Creates an Array and then copies the content from other.

Complexity: Linear in other.size()

Parameters:

other[in] another Array to use as data source

Array(Array &&other)

Move Constructor. Creates an Array and then moves the content from other, following the move semantics (i.e., the data is moved from other to this container). other is valid but left in a unspecified state afterwards.

Complexity: Constant

Parameters:

other[in] another Array to use as data source

template<typename InIter, internal::require_input_iterator<InIter> = true>
Array(InIter first, InIter last)

Creates an Array with the content of the range [first, last[.

Complexity: Linear in distance between first and last

Parameters:
  • first[in] iterator to the first element in the range

  • last[in] iterator to the “past-the-end” element in the range

Destructor

virtual ~Array()

Default Destructor. Deallocates the container and set its size to zero.

Complexity: Constant

Assignment

template<typename OtherT>
Array &operator=(const Array<OtherT> &other)

Copy Assignment Operator. Replaces its content with a copy of the contents of other. This routine will automatically reallocate the Array if needed.

Complexity: Linear in other.size()

Parameters:

other[in] another Array to use as data source

Returns:

*this

Array &operator=(const Array &other)

Copy Assignment Operator. Replaces its content with a copy of the contents of other. This routine will automatically reallocate the Array if needed.

Complexity: Linear in other.size()

Parameters:

other[in] another Array to use as data source

Returns:

*this

Array &operator=(Array &&other) noexcept

Move Assignment Operator. Replaces the container’s content with the contents of other Array, following the move semantics (i.e., the data is moved from other to this container). other is valid but left in a unspecified state afterwards.

Complexity: Constant

Parameters:

other[in] another Array to use as data source

Returns:

*this

Array &operator=(std::initializer_list<T> ilist)

Copy Assignment Operator. Replaces the container’s content with the values defined in the initializer list.

Complexity: Linear in ilist size

Parameters:

ilist[in] initializer list to use as data source

Returns:

*this

template<typename InIter, internal::require_input_iterator<InIter> = true>
void fill(InIter first, InIter last)

Replaces Array content with values defined in [first, last[. This routine will automatically reallocate the Array if needed.

Complexity: Linear in distance between first and last

Parameters:
  • first[in] iterator to the first element in the range

  • last[in] iterator to the “past-the-end” element in the range

void fill(index_t n, const T &val)

Fill the Array with n copies of val. This routine will automatically reallocate the Array if needed.

Complexity: Linear in this->size()

Parameters:
  • n[in] number of copies

  • val[in] value to replace the Array content with

void fill(const T &val)

Set all elements in the Array to val.

Complexity: Linear in this->size()

Parameters:

val[in] value to replace the Array content with

Iterators

iterator begin() noexcept

Complexity: Constant

Returns:

an iterator to the beginning of the container

iterator end() noexcept

Complexity: Constant

Returns:

an iterator to past-the-end element of the container

const_iterator cbegin() const noexcept

Complexity: Constant

Returns:

a const_iterator to the beginning of the container

const_iterator cend() const noexcept

Complexity: Constant

Returns:

a const_iterator to the past-the-end element of the container

Element Access

T *data() noexcept

Complexity: Constant

Note

If empty() = true, data() returns a nullptr.

Returns:

Raw pointer to the internal memory allocation

T *data() const noexcept

Complexity: Constant

Note

If empty() = true, data() returns a nullptr.

Returns:

Raw pointer to the internal memory allocation

T &front()

Complexity: Constant

Returns:

a reference to the first element of the container

T &front() const

Complexity: Constant

Returns:

a const reference to the first element of the container

T &back()

Complexity: Constant

Returns:

a reference to the last element of the container

T &back() const

Complexity: Constant

Returns:

a const reference to the last element of the container

T &operator[](index_t idx)

Accesses element at idx position.

Complexity: Constant

Parameters:

idx[in] position of the element to return

Returns:

a reference to the requested element

const T &operator[](index_t idx) const

Accesses element at idx position.

Complexity: Constant

Parameters:

idx[in] position of the element to return

Returns:

the requested element

Capacity

constexpr bool is_empty() const noexcept

Complexity: Constant

Returns:

true if the container is empty, false otherwise.

constexpr index_t size() const noexcept

Complexity: Constant

Returns:

the number of elements in the container.

constexpr index_t capacity() const noexcept

Complexity: Constant

Returns:

the capacity of the currently allocated storage.

void reserve(index_t new_cap)

Reallocates the container to new_cap if new_cap > capacity(). Otherwise, this routine does nothing.

Complexity: Linear in size()

Warning

During the reallocation, this routine will allocate additional memory (O(size())) in order to preserve the array’s content. If this behaviour is undesirable, use clear() before calling this routine.

Parameters:

new_cap[in] new capacity

void shrink_to_fit()

Reallocates the container based on the number of elements within. This routine will reduce the capacity() accordingly.

Complexity: Linear in size()

Modifiers

void deallocate() noexcept

Deallocates the container from memory.

Complexity: Constant

void clear() noexcept

Clears the content of the Array and sets its size to 0.

Complexity: Constant

void resize(index_t new_size)

Resizes the container to new_size. If new_size > capacity(), this routine automatically reallocates the object to contain the additional elements.

Complexity: Linear in size() in case of reallocation, constant otherwise

Note

See reserve() in case of reallocation.

Parameters:

new_size[in] new size of the container

Throws:

std::length_error – if `new_size < 0`

void resize(index_t new_size, const T &val)

Resizes the container to new_size. If new_size > capacity(), this routine automatically reallocates the object to contain the additional elements. Initialise the new elements with val afterwards.

Complexity: Linear in size()

Note

See reserve() in case of reallocation.

Parameters:
  • new_size[in] new size of the container

  • val[in] value to initialise the new elements

Throws:

std::length_error – if `new_size < 0`

void push_back(const T &value)

Inserts value at the end of the Array. If the resulting size is greater than the current capacity(), this routine automatically reallocates the container.

Complexity: Linear in size() in case of reallocation, constant otherwise

Parameters:

value[in] value to be inserted at the end of the Array

T pop_back()

Removes the last element of the container, decreasing the Array size by 1.

Complexity: Constant

Returns:

the removed element

iterator insert(index_t pos, const T &value)

Inserts value before the position pos in the container. If the resulting size is greater than the current capacity(), this routine automatically reallocates the container.

Complexity: Linear in the distance between pos and the end of the container

Note

pos must be in range [0,size()], otherwise this routine has undefined behaviour.

Parameters:
  • pos[in] position to the inserted element

  • value[in] value to be inserted

Returns:

an iterator to the inserted value

iterator insert(index_t pos, index_t count, const T &value)

Inserts count copies of value before the position pos in the container. If the resulting size is greater than the current capacity(), this routine automatically reallocates the container.

Complexity: Linear in count + Linear in the distance between pos and the end of the container

Note

pos must be in range [0,size()], otherwise this routine has undefined behaviour.

Parameters:
  • pos[in] position to the inserted element

  • count[in] number of copies

  • value[in] value to be inserted

Returns:

an iterator pointing to the first element inserted. An iterator to pos, if count <= 0.

template<typename InIter, internal::require_input_iterator<InIter> = true>
iterator insert(index_t pos, InIter first, InIter last)

Inserts values from the range [first, last[ before the position pos in the container. If the resulting size is greater than the current capacity(), this routine automatically reallocates the container.

Complexity: Linear in std::distance(first, last) + Linear in the distance between pos and the end of the container

Note

pos must be in range [0,size()] and the first and last iterators must not be from the container which insert() is called, otherwise this routine has undefined behaviour.

Parameters:
  • pos[in] position to the inserted element

  • first[in] iterator to the first element in the range

  • last[in] iterator to the “past-the-end” element of the range

Returns:

an iterator pointing to the first element inserted. An iterator to pos, if first == last.

iterator erase(index_t pos)

Erases element located at pos.

Complexity: Linear in the distance between pos and the end of the container

Parameters:

pos[in] position to where the element will be erased

Returns:

an iterator following the last element removed

iterator erase(index_t first, index_t last)

Erases elements in range [first, last[

Complexity: Linear in the distance between last and the end of the container

Parameters:
  • first[in] index of the first element in range

  • last[in] index of “past-the-end” element in range

Returns:

an iterator following the last element removed

void swap(Array &other) noexcept

Swaps the Array content with other, including any memory allocation.

Complexity: Constant

Parameters:

other – another Array to exchange content with