Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
268 changes: 268 additions & 0 deletions data_structures/vector
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
/**
* @file
* @brief Basic implementation of a dynamic array
* ([Vector](https://en.cppreference.com/cpp/container/vector)) with core methods.
* @details Vector implemented using smart pointers (`std::unique_ptr`),
* automatically reallocating memory when the buffer reaches capacity == size.
* Time complexity: random access \f$O(1)\f$, insertion at the end \f$O(1)\f$
* amortized (\f$O(n)\f$ during reallocation), and removal from the end \f$O(1)\f$.
* @author [VildanWW](https://github.com/VildanWW)
*/
#include <iostream> /// For IO operation
#include <cassert> /// For assert
#include <memory> /// For std::unique_ptr
#include <cstddef> /// For std::size_t
#include <utility> /// For std::move
#include <string> /// For std::string

static constexpr std::size_t DEFAULT_CAPACITY = 1; ///< Default initial capacity of the vector
static constexpr std::size_t DEFAULT_FACTOR = 2; ///< The value by which the size of the new vector is multiplied

/**
* @namespace data_structures
* @brief Algorithms and Data Structures
*/
namespace data_structures {
/// Dynamic array class
template<typename T>
class Vector {
private:
/// Smart pointer to internal array
std::unique_ptr<T[]> massive = nullptr;

/// Current number of elements
std::size_t size = 0;

/// Current allocated capacity
std::size_t capacity = 0;

/// Reallocates memory when load threshold is reached
void reallocate() {
if (capacity == size) {
std::size_t new_capacity = (capacity == 0) ? DEFAULT_CAPACITY : capacity * DEFAULT_FACTOR;

auto new_massive = std::make_unique<T[]>(new_capacity);

for (std::size_t ind = 0; ind < size; ind++) {
new_massive[ind] = std::move(massive[ind]);
}

massive = std::move(new_massive);

capacity = new_capacity;
}
}
public:
/// Base constructor
Vector() :
massive(std::make_unique<T[]>(DEFAULT_CAPACITY)),
capacity(DEFAULT_CAPACITY) {
}

/// Constructor with 1 parameter
explicit Vector(const std::size_t size) :
massive(std::make_unique<T[]>(size)),
size(size),
capacity(size) {
}

/// Copy constructor
Vector(const Vector& other) :
massive(std::make_unique<T[]>(other.capacity)),
size(other.size),
capacity(other.capacity) {

for (std::size_t ind = 0; ind < size; ind++) {
massive[ind] = other.massive[ind];
}
}

/// Copy assignment operator
Vector& operator=(const Vector& other) {
if (this == &other) return *this;

massive = std::make_unique<T[]>(other.capacity);

for (std::size_t ind = 0; ind < other.size; ind++) {
massive[ind] = other.massive[ind];
}

size = other.size;
capacity = other.capacity;

return *this;
}

/// Move constructor
Vector(Vector&& other) noexcept :
massive(std::move(other.massive)),
size(other.size),
capacity(other.capacity) {
other.size = 0;
other.capacity = 0;
}

/// Move assignment operator
Vector& operator=(Vector&& other) noexcept {
if (this == &other) return *this;

massive = std::move(other.massive);
size = other.size;
capacity = other.capacity;

other.size = 0;
other.capacity = 0;

return *this;
}

/// Default destructor
~Vector() = default;

/// Add element at the end of Vector
void push_back(const T& value) {
reallocate();

massive[size++] = value;
}

/// Add element at the end of Vector
void push_back(T&& value) {
reallocate();

massive[size++] = std::move(value);
}

/// Delete element at the end of Vector
void pop_back() {
if (size == 0) return;

massive[--size] = T{};
}

/// Clear all Vector
void clear() {
for (std::size_t ind = 0; ind < size; ind++) {
massive[ind] = T{};
}

size = 0;
}

/// Check if Vector is empty
bool empty() const {
return size == 0;
}

/// Access element by index
T& operator[](const std::size_t ind) {
return massive[ind];
}

/// Access element by index
const T& operator[](const std::size_t ind) const {
return massive[ind];
}

/// Return current size
std::size_t get_size() const { return size; }

/// Return current capacity
std::size_t get_capacity() const { return capacity; }
};
} // namespace data_structures

/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
data_structures::Vector<int> v1(1'000'000);

assert(v1.get_capacity() == 1'000'000);
assert(v1.get_size() == 1'000'000);
assert(v1.empty() == false);

v1.clear();

assert(v1.get_capacity() == 1'000'000);
assert(v1.get_size() == 0);
assert(v1.empty() == true);

v1.pop_back();
v1.pop_back();

v1.push_back(123);
v1.push_back(-123);

assert(v1.get_size() == 2);

v1.pop_back();

assert(v1.get_size() == 1);

v1.pop_back();

assert(v1.get_size() == 0);

data_structures::Vector<int> v2;

for (int ind = 0; ind < 10'000'000; ind++) {
v1.push_back(ind / 10);
v2.push_back(ind / 10);
}

assert(v1.get_size() == v2.get_size());

for (int ind = 0; ind < 10'000'000; ind++) {
assert(v1[ind] == v2[ind]);
}

v2.pop_back();

v1 = v1 = v2;

v2[0] = v2[1] = v2[v2.get_size() - 1] = -999;

assert(v1[0] != v2[0]);
assert(v1[1] != v2[1]);
assert(v1[v1.get_size() - 1] != v2[v2.get_size() - 1]);

data_structures::Vector<int> v_copy(v1);
assert(v_copy.get_size() == v1.get_size());
assert(v_copy[0] == v1[0]);

data_structures::Vector<std::string> v3;
std::string str = "hello";
v3.push_back(str);
v3.push_back("world");
assert(v3.get_size() == 2);
assert(v3[0] == "hello");
assert(v3[1] == "world");

data_structures::Vector<std::string> v4 = std::move(v3);
assert(v4.get_size() == 2);
assert(v4[0] == "hello");
assert(v4[1] == "world");
assert(v3.get_size() == 0);
assert(v3.get_capacity() == 0);

data_structures::Vector<std::string> v5;
v5 = std::move(v4);
assert(v5.get_size() == 2);
assert(v5[0] == "hello");
assert(v5[1] == "world");
assert(v4.get_size() == 0);
assert(v4.get_capacity() == 0);

std::cout << "All tests have successfully passed!\n";
}

/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // run self test implementation.
return 0;
}
Loading