Fw/DataStructures/docs/Array.md
Array is a final class template defined in
Fw/DataStructures.
It represents an array with internal storage.
It maintains the backing memory M as a member variable.
Array has the following template parameters.
| Kind | Name | Purpose |
|---|---|---|
typename | T | The type of an array element |
FwSizeType | S | The array size in elements |
Array statically asserts the following:
T is default constructible.S > 0.Array defines the type Elements.
It is an alias of T[S].
Array has the following private member variables.
| Name | Type | Purpose | Default Value |
|---|---|---|---|
m_elements | Elements | The array elements | C++ default initialization |
Array()
Defined as = default.
Example:
Array<U32, 3> a;
Array(const std::initializer_list<T>& il)
Assert that il.size() == S.
Initialize m_elements from il.
Examples:
Array<U32, 3> a({ 1, 2, 3 });
Array(const Elements& elements)
Statically assert that S1 == S.
Set *this = elements.
Example:
U32 elements[3] = { 1, 2, 3 };
Array<U32, 3> a(elements);
explicit Array(const T& element)
Initialize each element of m_elements with element.
Example:
// Explicit call to constructor in variable declaration
Array<U32, 3> a(1);
// Explicit call to constructor in assignment
a = Array<U32, 3>(2);
Array(const Array<T, S>& a)
Initialize the elements of m_elements with the
elements of a.m_elements.
Example:
// Call the single-item constructor
Array<U32, 3> a1(3);
// Call the copy constructor
Array<U32, 3> a2(a1);
~Array()
Defined as = default.
T& operator[](FwSizeType i)
const T& operator[](FwSizeType i) const
Assert that i < S.
Return m_elements[i].
Example:
constexpr FwSizeType size = 3;
Array<U32, size> a;
// Constant access
ASSERT_EQ(a[0], 0);
// Mutable access
a[0]++;
ASSERT_EQ(a[0], 1);
// Out-of-bounds access
ASSERT_DEATH(a[size], "Assert");
Array<T, S>& operator=(const std::initializer_list<T>& il)
Assert that il.size() == S.
Copy each element of il into m_elements.
Return *this.
Example:
Array<U32, 3> a;
a = { 1, 2, 3 };
Array<T, S>& operator=(const Elements& elements)
Copy each element of elements into m_elements.
Return *this.
Example:
U32 elements[3] = { 1, 2, 3 };
Array<U32, 3> a;
a = elements;
Array<T, S>& operator=(const T& element)
Copy element into each element of m_elements.
Return *this.
Example:
Array<U32, 3> a;
a = 5;
Array<T, S>& operator=(const Array<T, S>& a)
If &a != this, overwrite each element of m_elements with the
corresponding element of a.
Return *this.
Example:
Array<U32, 3> a1(1);
Array<U32, 3> a2(2);
a1 = a2;
Elements& getElements()
const Elements& getElements() const
Return m_elements.
Example:
constexpr FwSizeType size = 3;
Array<U32, size> a;
// Mutable reference
auto& elements1 = a.getElements();
ASSERT_EQ(elements1[0], 0);
elements1[0] = 1;
// Constant reference
const auto& elements2 = a.getElements();
ASSERT_EQ(elements2[0], 1);
ExternalArray<T> asExternalArray()
Return ExternalArray<T>(m_elements, S)
Example:
constexpr FwSizeType size = 3;
Array<U32, size> a = { 1, 2, 3 };
ExternalArray<U32> ea = a.asExternalArray();
ASSERT_EQ(ea[0], 1);