Files
2026-05-19 09:56:36 +05:30

336 lines
8.7 KiB
C++

/*
* File: Vector.h
* Description: Provides a generic dynamic array container implementation.
* Author: Trenser
* Created: 18-May-2026
*/
#pragma once
#include <stdexcept>
namespace util
{
template<typename T>
class Vector
{
private:
T* data;
int size;
int capacity;
/*
* Function: resize
* Description: Resizes the internal storage capacity of the vector.
* Parameters:
* newCapacity - new storage capacity for the vector
* Returns:
* void - no return value
*/
void resize(int newCapacity)
{
T* newData = new T[newCapacity];
for (int index = 0; index < size; index++)
{
newData[index] = data[index];
}
delete[] data;
data = newData;
capacity = newCapacity;
}
public:
/*
* Function: Vector
* Description: Default constructor for Vector.
* Parameters: None
* Returns:
* Vector object
*/
Vector()
{
capacity = 4;
size = 0;
data = new T[capacity];
}
/*
* Function: Vector
* Description: Copy constructor for Vector.
* Parameters:
* other - Vector object to copy from
* Returns:
* Vector object
*/
Vector(const Vector<T>& other)
{
size = other.size;
capacity = other.capacity;
data = new T[capacity];
for (int index = 0; index < size; index++)
{
data[index] = other.data[index];
}
}
/*
* Function: Vector
* Description: Move constructor for Vector.
* Parameters:
* other - Vector object to move from
* Returns:
* Vector object
*/
Vector(Vector<T>&& other)
{
data = other.data;
size = other.size;
capacity = other.capacity;
other.data = nullptr;
other.size = 0;
other.capacity = 0;
}
/*
* Function: operator=
* Description: Copy assignment operator for Vector.
* Parameters:
* other - Vector object to copy from
* Returns:
* Vector<T>& - reference to the current Vector object
*/
Vector<T>& operator=(const Vector<T>& other)
{
if (this == &other)
{
return *this;
}
delete[] data;
size = other.size;
capacity = other.capacity;
data = new T[capacity];
for (int index = 0; index < size; index++)
{
data[index] = other.data[index];
}
return *this;
}
/*
* Function: operator=
* Description: Move assignment operator for Vector.
* Parameters:
* other - Vector object to move from
* Returns:
* Vector<T>& - reference to the current Vector object
*/
Vector<T>& operator=(Vector<T>&& other)
{
if (this == &other)
{
return *this;
}
delete[] data;
data = other.data;
size = other.size;
capacity = other.capacity;
other.data = nullptr;
other.size = 0;
other.capacity = 0;
return *this;
}
/*
* Function: ~Vector
* Description: Destructor for Vector.
* Parameters: None
* Returns:
* void - no return value
*/
~Vector()
{
delete[] data;
}
/*
* Function: push_back
* Description: Appends an element to the end of the vector.
* Parameters:
* value - element to append
* Returns:
* void - no return value
*/
void push_back(const T& value)
{
if (size >= capacity)
{
resize(capacity * 2);
}
data[size++] = value;
}
/*
* Function: removeAt
* Description: Removes the element at the specified position.
* Parameters:
* position - index of the element to remove
* Returns:
* void - no return value
*/
void removeAt(int position)
{
if (position < 0 || position >= size)
{
return;
}
for (int index = position; index < size - 1; index++)
{
data[index] = data[index + 1];
}
size--;
}
/*
* Function: remove
* Description: Removes the first occurrence of the specified value.
* Parameters:
* value - element to remove
* Returns:
* bool - true if the element was removed, false otherwise
*/
bool remove(const T& value)
{
int index = find(value);
if (index == -1)
{
return false;
}
removeAt(index);
return true;
}
/*
* Function: clear
* Description: Removes all elements from the vector.
* Parameters: None
* Returns:
* void - no return value
*/
void clear()
{
size = 0;
}
/*
* Function: getSize
* Description: Returns the number of elements in the vector.
* Parameters: None
* Returns:
* int - number of elements in the vector
*/
int getSize() const
{
return size;
}
/*
* Function: isEmpty
* Description: Checks whether the vector is empty.
* Parameters: None
* Returns:
* bool - true if the vector is empty, false otherwise
*/
bool isEmpty() const
{
return size == 0;
}
/*
* Function: contains
* Description: Checks whether the vector contains the specified value.
* Parameters:
* value - value to search for
* Returns:
* bool - true if the value exists, false otherwise
*/
bool contains(const T& value) const
{
return find(value) != -1;
}
/*
* Function: find
* Description: Finds the index of the specified value.
* Parameters:
* value - value to search for
* Returns:
* int - index of the value if found, otherwise -1
*/
int find(const T& value) const
{
for (int index = 0; index < size; index++)
{
if (data[index] == value)
{
return index;
}
}
return -1;
}
/*
* Function: findIf
* Description: Finds the index of an element matching a custom predicate.
* Parameters:
* predicate - callable object used for matching elements
* Returns:
* int - index of the matching element if found, otherwise -1
*/
template<typename Predicate>
int findIf(Predicate predicate) const
{
for (int index = 0; index < size; index++)
{
if (predicate(data[index]))
{
return index;
}
}
return -1;
}
/*
* Function: operator[]
* Description: Returns a reference to the element at the specified index.
* Parameters:
* index - index of the element
* Returns:
* T& - reference to the element
*/
T& operator[](int index)
{
if (index < 0 || index >= size)
{
throw std::out_of_range("Index out of range");
}
return data[index];
}
/*
* Function: operator[]
* Description: Returns a constant reference to the element
* at the specified index.
* Parameters:
* index - index of the element
* Returns:
* const T& - constant reference to the element
*/
const T& operator[](int index) const
{
if (index < 0 || index >= size)
{
throw std::out_of_range("Index out of range");
}
return data[index];
}
};
}