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(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
-
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 fromother
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 fromother
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
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 thepast-the-end
element of the container
Element Access
-
T *data() noexcept¶
Complexity: Constant
Note
If
empty() = true
,data()
returns anullptr
.- Returns:
Raw pointer to the internal memory allocation
-
T *data() const noexcept¶
Complexity: Constant
Note
If
empty() = true
,data()
returns anullptr
.- Returns:
Raw pointer to the internal memory allocation
-
T &front() const¶
Complexity: Constant
- Returns:
a
const
reference to the first element of the container
-
T &back() const¶
Complexity: Constant
- Returns:
a
const
reference to the last element of the container
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
ifnew_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, useclear()
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 resize(index_t new_size)¶
Resizes the container to
new_size
. Ifnew_size > capacity()
, this routine automatically reallocates the object to contain the additional elements.Complexity: Linear in
size()
in case of reallocation, constant otherwiseNote
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
. Ifnew_size > capacity()
, this routine automatically reallocates the object to contain the additional elements. Initialise the new elements withval
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 currentcapacity()
, 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 positionpos
in the container. If the resulting size is greater than the currentcapacity()
, this routine automatically reallocates the container.Complexity: Linear in the distance between
pos
and the end of the containerNote
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 ofvalue
before the positionpos
in the container. If the resulting size is greater than the currentcapacity()
, this routine automatically reallocates the container.Complexity: Linear in
count
+ Linear in the distance betweenpos
and the end of the containerNote
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
, ifcount <= 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 positionpos
in the container. If the resulting size is greater than the currentcapacity()
, this routine automatically reallocates the container.Complexity: Linear in
std::distance(first, last)
+ Linear in the distance betweenpos
and the end of the containerNote
pos
must be in range [0
,size()
] and thefirst
andlast
iterators must not be from the container whichinsert()
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
, iffirst == 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