Template Class Vector¶
- Defined in File Vector.h
Class Documentation¶
-
template <typename Type>
classVector
¶ Class for customized vector.
This class is used to construct a customized Azura vector. Azura vectors take advantage of a custom allocator. The custom allocators allows them to be created on the Stack or the Heap.
They also don’t “allocate” new memory. Allocation is handled by the supplied Azura::Memory::Allocator class.
Azura Vectors are also “reserve” first. This basically means that the vector reserves a chunk first, instead of reserving and initializing like std::vector. This is probably the main difference between the two. Here is an example:
Containers::Vector<int> arr(5, allocator); // above code allocates a memory upto 5 ints. But arr[0 to 4] doesn't exist. // arr.GetSize() will be 0 // arr.GetMaxSize() will be 5 std::vector<int> arr(5); // above code allocates and initializes a memory upto 5 ints. // arr.size() will be 5 // arr.capacity() will be 5
Both vectors tend to grow. But growing vectors are bad so watch out. Pre-allocate as much as you can beforehand.
The API for Azura Vector is similar but Pascal Case’d mostly. Example:
Pascal case was selected because Azura keeps the API similar across all C++ code. And most of the Game Engine code that was initially built followed this scheme.Containers::Vector<int> arr(5, allocator); arr.PushBack(0); // similar to push_back arr.Begin(); arr.End();
- Template Parameters
Type
: Datatype of the vector
Public Functions
-
Vector
(Memory::Allocator &alloc)¶ Constructs a 0 sized vector with an allocator.
An empty vector doesn’t reserve any space on the allocator. It is required to reserve the vector before you use it.
Example:
Containers::Vector<int> arr(allocator); // The vector is still of size 0. The capacity is 0. // ... // Later in code // ... arr.Resize(5); // This allocates a 5 sized int vector // The vector is of size 5. The capacity is 5. // ---- OR ---- arr.Reserve(5); // This allocates a 5 sized int vector // The vector is still of size 0. The capacity is 5.
- Parameters
alloc
: The allocator
-
Vector
(UINT maxSize, Memory::Allocator &alloc)¶ Constructs a vector accepting reserved size and allocator.
This constructor reserves the specified size in the vector. The vector is of still size 0.
Example:
Containers::Vector<int> arr(5, allocator); // This allocates a 5 sized int vector // The vector is still of size 0. The capacity is 5.
- Parameters
maxSize
: The maximum sizealloc
: The allocator
-
Vector
(UINT currentSize, UINT maxSize, Memory::Allocator &alloc)¶ Construct a vector using size and max size.
This constructor reserves and constructs the specified sizes in the vector.
Example:
Containers::Vector<int> arr(2, 5, allocator); // This allocates a 5 sized int vector // The vector is of size 2. The capacity is 5.
- Parameters
currentSize
: The current sizemaxSize
: The maximum sizealloc
: The allocator
-
Vector
(const std::initializer_list<Type> &list, Memory::Allocator &alloc)¶ Construct a vector using an initializer list.
Example:
Containers::Vector<int> arr({1, 2, 3, 4, 5}, allocator); // This allocates a 5 sized int vector with that values // The vector is of size 5. The capacity is 5.
- Parameters
list
: The listalloc
: The allocator
-
template <typename... Args>
Vector
(ContainerExtent extent, Memory::Allocator &alloc, Args&&... args)¶ Constructs the vector using the extent.
Also, you can provide initialization arguments along with the extent.
Example:
Containers::Vector<int> arr(ContainerExtent{5}, allocator, 6); // This allocates a 5 sized int vector with the value 6. // The vector is of size 5. The capacity is 5.
- Parameters
extent
: The extentalloc
: The allocatorargs
: Initialization arguments
- Template Parameters
Args
: Arguments
-
~Vector
()¶ Destroys the vector and frees the memory in the allocator.
-
void
PushBack
(const Type &data)¶ Appends data to the end of the vector.
The vector also doubles its size incase it fills up. Recommended to pre-allocate as much as you can.
Example:
Containers::Vector<int> arr(5, allocator); // The vector is of size 0. The capacity is 5. arr.PushBack(1); // The vector is of size 1. The capacity is 5. arr.PushBack(2); // The vector is of size 2. The capacity is 5. arr.PushBack(3); // The vector is of size 3. The capacity is 5. arr.PushBack(4); // The vector is of size 4. The capacity is 5.
- Parameters
data
: Data to push
-
void
PushBack
(Type &&data)¶ Appends data to the end of the vector.
The vector also doubles its size incase it fills up. Recommended to pre-allocate as much as you can.
Example:
Containers::Vector<int> arr(5, allocator); // The vector is of size 0. The capacity is 5. arr.PushBack(1); // The vector is of size 1. The capacity is 5. arr.PushBack(2); // The vector is of size 2. The capacity is 5. arr.PushBack(3); // The vector is of size 3. The capacity is 5. arr.PushBack(4); // The vector is of size 4. The capacity is 5.
- Parameters
data
: Data to push
-
template <typename... Args>
voidEmplaceBack
(Args... args)¶ Appends data to the end of the vector by emplacing (like regular vector)
The vector also doubles its size incase it fills up. Recommended to pre-allocate as much as you can.
The arguments are the initialization parameters for the Type.
Example:
MyClass { public: MyClass(int a) : data(a) {} private: int data; }; Containers::Vector<MyClass> arr(5, allocator); // The vector is of size 0. The capacity is 5. arr.EmplaceBack(1); // Creates a MyClass{1} at the memory location // The vector is of size 1. The capacity is 5. arr.EmplaceBack(2); // Creates a MyClass{2} at the memory location // The vector is of size 2. The capacity is 5.
- Parameters
args
: Arguments to push
- Template Parameters
Args
: Arguments
-
void
PopBack
()¶ Removes the last element in the vector array.
-
int
FindFirst
(const Type &data)¶ Searches for the data in the vector.
- Return
- Index if Found, else -1
- Parameters
data
: Data to search for
-
void
Remove
(const Type &data)¶ Searches for the given data in the vector and removes it.
- Parameters
data
: Data to Search for and Remove
-
void
Reserve
(U32 requiredSize)¶ Reserves a contiguous block for the vector.
Use this when you didn’t provide an initial size for the vector. The growth of the vector is not controlled by this. Using Reserve after storing the data in the vector will lead an undefined behavior. Use this only once.
Example:
Containers::Vector<int> arr(allocator); // The vector is still of size 0. The capacity is 0. // ... // Later in code // ... arr.Reserve(5); // This allocates a 5 sized int vector // The vector is still of size 0. The capacity is 5.
- Parameters
requiredSize
: Required Size
-
void
Resize
(U32 requiredSize)¶ Reserves a contiguous block for the vector and sets the size to the value specified.
Also, sets the size to the specified value. Use this when you didn’t provide an initial size for the vector. The growth of the vector is not controlled by this. Using Resize after storing the data in the vector will lead an undefined behavior. Use this only once.
Example:
Containers::Vector<int> arr(allocator); // The vector is still of size 0. The capacity is 0. // ... // Later in code // ... arr.Resize(5); // This allocates a 5 sized int vector // The vector is now of size 5. The capacity is 5.
- Parameters
requiredSize
: Required Size
-
bool
IsEmpty
() const¶ Checks if the container is empty.
- Return
true
if empty
-
void
InsertAt
(U32 idx, const Type &data)¶ Inserts the supplied data at the index.
- Parameters
idx
: Target Indexdata
: Data to insert
-
Type *
Data
()¶ Gets the Data pointer.
Example:
Containers::Vector<int> arr(5, allocator); // The vector is still of size 0. The capacity is 5. arr.Data() // is of type int*
-
const Type *
Data
() const¶ Gets the Data pointer.
Example:
Useful as a constant pointer when compared to its other overload.Containers::Vector<int> arr(5, allocator); // The vector is still of size 0. The capacity is 5. arr.Data() // is of type int*
-
void
Reset
()¶ Empties the vector.
But, it doesn’t deallocate anything. It is meant to reuse the allocated memory.
Example:
Containers::Vector<int> arr(5, allocator); // The vector is of size 0. The capacity is 5. arr.PushBack(1); // The vector is of size 1. The capacity is 5. arr.PushBack(2); // The vector is of size 2. The capacity is 5. arr.PushBack(3); // The vector is of size 3. The capacity is 5. arr.PushBack(4); // The vector is of size 4. The capacity is 5. arr.Reset(); // Or Clear(); // The vector is of size 0. The capacity is 5.
-
void
Clear
()¶
-
Type &
operator[]
(U32 idx)¶
-
Type &
operator[]
(U32 idx) const¶
-
Type &
Last
()¶
-
Type &
Last
() const¶
-
U32
GetSize
() const¶
-
U32
GetMaxSize
() const¶
-
template <class InputIt>
voidAssign
(InputIt first, InputIt last)¶
-
Vector<Type>::Iterator
Begin
() const¶ Returns an Iterator pointing to the beginning of the vector. This is similar to begin() of a std::vector.
- Return
- Iterator
-
Vector<Type>::Iterator
End
() const¶ Returns an Iterator pointing to the end of the vector. This is similar to end() of a std::vector.
- Return
- Iterator
-
class
Iterator
¶ Public Types
-
template<>
usingiterator_category
= std::random_access_iterator_tag¶
-
template<>
usingvalue_type
= Type¶
-
template<>
usingdifference_type
= int¶
-
template<>
usingpointer
= Type *¶
-
template<>
usingreference
= Type&¶
Public Functions
-
template<>
Iterator
()¶
-
template<>
~Iterator
()¶
-
template<>
Iterator &operator=
(const Iterator &other)¶
-
template<>
Iterator
(const Vector *ptr, int index)¶
-
template<>
Iterator &operator=
(Iterator &&other)¶
-
template<>
Iterator &operator++
()¶
-
template<>
Iteratoroperator++
(int)¶
-
template<>
Iterator &operator--
()¶
-
template<>
Iteratoroperator--
(int)¶
-
template<>
booloperator==
(const Iterator &rhs)¶
-
template<>
booloperator!=
(const Iterator &rhs)¶
-
template<>
booloperator<
(const Iterator &rhs)¶
-
template<>
booloperator<=
(const Iterator &rhs)¶
-
template<>
booloperator>
(const Iterator &rhs)¶
-
template<>
booloperator>=
(const Iterator &rhs)¶
-
template<>
Iteratoroperator+
(const int &idx)¶
-
template<>
Iterator &operator+=
(const int &idx)¶
-
template<>
Iteratoroperator-
(const int &idx)¶
-
template<>
Iterator &operator-=
(const int &idx)¶
-
template<>
intoperator-
(const Iterator &rhs)¶
-
template<>
Type &operator*
()¶
-
template<>
Type *operator->
()¶
-
template<>
Type &operator[]
(const int &idx)¶
Friends
-
int
operator-
(const Iterator &lhs, const Iterator &rhs)
-
template<>