Template Class DynamicArray
Defined in File dynamic_array.h
Class Documentation
-
template<typename T>
class DynamicArray Dynamic array class with all related functionality.
- Template Parameters:
T – Type of the implementation class.
Public Functions
-
inline DynamicArray()
Default constructor for the DynamicArray class.
Initializes an array of type T and size default initial capacity. The current size is set to zero and the capacity is initializd to the default initial capacity.
-
inline DynamicArray(int initial_capacity)
Constructor for DynamicArray class with specified initial capacity.
Initalizes an array of type T and size specified initial capacity. The current size is set to zero and the capacity is initialized to the specified initial capacity.
- Parameters:
init_cap – Desired initial capacity of the dynamic array.
-
inline ~DynamicArray()
Default destructor for the DynamicArray class.
Frees the memory used by the array.
-
inline void add_last(const T &element)
Adds an element at the end of the array.
Add functions
This is achieved by first checking if the array is full, doubling its size if it is and then adding the element and incrementing the current size by unity.
- Parameters:
element – The element to be inserted.
-
inline void add(int pos_index, const T &element)
Adds an element at a specified index.
This is achieved by first checking if the position index is valid using the private check_position_index() function. If the array is full then its size is doubled. The elements to the right of the desired position are shifted to the right, the new element added and the size incremented by unity.
- Parameters:
pos_index – Position index where the new element is desired to be added.
element – The element to be added.
-
inline void add_first(const T &element)
Adds an element at the start of the array.
Internally uses the add() function with
pos_indexzero to add the element to the start of the array.- Parameters:
element – The element to be added.
-
inline T del_last()
Deletes the last element of the array and returns its value.
This is achieved by first checking if the array is empty, if the current size is one-fourth the capacity alloted then the array is resized to half its size. The last element is cleared, the current size of the array updated and the last element’s value is returned.
- Returns:
The deleted last element.
-
inline T del(int index)
Deletes the element at a particular index.
This is achieved by first checking if the index is valid, halfing the size of the array if its current size is one-fourth its capacity, then shifting all elements to the right of the index to the left, clearing its value, updating the size and finally returning the deleted element.
- Parameters:
index – The index of the element to be erased.
- Returns:
The deleted element.
-
inline T del_first()
Deletes the first element in the array.
Internally uses the del() function with
indexzero to delete the first element in the array.- Returns:
The deleted first element.
-
inline T get(int index) const
Get the element at a specified index.
Checks if the requested element’s index is valid using the private check_element_index() function and returns the element if it is.
- Parameters:
index – Index of the element.
- Returns:
Element at the specified index.
-
inline T set(int index, const T &val)
Sets the value of an element at a particular index.
This is achieved by first checking if the index is valid using the private check_element_index(), setting it’s value and then returning the old value.
- Parameters:
index – The index of the element whose value is to be set.
val – The new value to be set to.
- Returns:
The old value.
-
inline size_t get_size() const
Get the current size of the array.
- Returns:
Size of the array.
-
inline bool is_empty() const
Check if the array is empty.
- Returns:
true if the array is empty, false otherwise.
-
inline void resize(int new_cap)
Resize the array to a new capacity.
Creates an array with the new capacity, copies the old data to it and makes the old array pointer point to this new array. The capacity is updated accordingly.
-
inline bool is_element_index(int index) const
Checks if the given element index is valid i.e in [0, size).
- Parameters:
index – Index to be checked.
- Returns:
true if the index is valid, false otherwise.
-
inline bool is_position_index(int pos_index) const
Checks if the given position index is valid i.e in [0, size].
- Parameters:
pos_index – Position index to be checked.
- Returns:
true if the position index is valid, false otherwise.
-
inline void check_element_index(int index) const
Checks the given element index and throws an exception if out of range.
- Parameters:
index – Index to be checked.
- Throws:
std::out_of_range – thrown when the index is out of the valid range i.e [0, size). The exception message states that the “Element index is out of bounds”.
-
inline void check_position_index(int pos_index) const
Checks the given position index and throws an exception if out of range.
- Parameters:
pos_index – Position index to be checked.
- Throws:
std::out_of_range – thrown when the position index is out of the valid range i.e [0, size]. The exception message states that the “Position index is out of bounds”.
-
inline void display()
Report the current size and capacity of the array and display the values of its elements.