Skip to content

Instantly share code, notes, and snippets.

@yotamN
Created November 16, 2015 17:21
Show Gist options
  • Save yotamN/4d5b049b76209e5f8cc0 to your computer and use it in GitHub Desktop.
Save yotamN/4d5b049b76209e5f8cc0 to your computer and use it in GitHub Desktop.
#include "Vector.h"
#include <iostream>
#define RESET 0
Vector::Vector(int n)
{
int num = n < 2 ? 2 : n;
this->_elements = new int[num];
this->_capacity = num;
this->_size = RESET;
this->_resizeFactor = num;
}
Vector::Vector(const Vector& other)
{
this->_size = other._size;
this->_capacity = other._capacity;
this->_resizeFactor = other._resizeFactor;
this->_elements = new int[other._capacity];
for (int i = RESET; i<other._size; i++)
{
this->_elements[i] = other._elements[i];
}
}
Vector::~Vector()
{
delete[] this->_elements;
}
Vector& Vector::operator=(const Vector& other)
{
delete[] this->_elements;
this->_size = other._size;
this->_capacity = other._capacity;
this->_resizeFactor = other._resizeFactor;
this->_elements = new int[other._capacity];
for (int i = RESET; i<this->_size; i++)
{
this->_elements[i] = other._elements[i];
}
return *this;
}
int Vector::size() const
{
return this->_size;
}
int Vector::capacity() const
{
return this->_capacity;
}
bool Vector::empty() const
{
return (this->_size == RESET);
}
void Vector::assign(int val)
{
int size = this->_size;
for (int i = RESET; i <= size; i++)
{
this->_elements[i] = val;
}
}
void Vector::push_back(const int& val)
{
const int cap = this->_capacity;
const int size = this->_size;
if (cap == size)
{
const int newsize = (this->_capacity + this->_resizeFactor);
int* newarray = new int[newsize]; //create new array to replace old small one
for (int i = RESET; i < size; i++) //move data to array
{
newarray[i] = this->_elements[i];
}
delete[] this->_elements; //delete old array with d'tor
this->_elements = newarray; //make the pointer point to the new array
this->_capacity = newsize; //because the new array is bigger so is the capacity
}
this->_elements[size] = val; //current index
this->_size++; //more space use in array
}
void Vector::pop_back()
{
if (!(this->empty()))
{
this->_size--;
}
}
void Vector::reserve(int n)
{
if (this->_capacity < n)
{
const int size = this->_size;
const int adder = this->_resizeFactor;
int cap = this->_capacity;
int* newarray;
while (n < cap)
{
cap = cap + adder;
}
newarray = new int[cap];
for (int i = RESET; i < size; i++)
{
newarray[i] = this->_elements[i];
}
delete[] this->_elements;
this->_elements = newarray;
this->_capacity = cap;
}
}
void Vector::resize(int n)
{
int cap = this->_capacity;
if (cap < n)
{
this->reserve(n);
}
else
{
this->_size = n;
}
}
void Vector::resize(int n, const int& val)
{
int size = this->_size;
int cap = this->_capacity;
if ( n > cap)
{
this->reserve(n);
for (; size < cap; size++)
{
this->_elements[size] = val;
}
this->_size = cap;
}
else
{
this->_size = n;
}
}
int& Vector::operator[](int n) const
{
if (n < 0 || n >= this->_size)
{
std::cout << "ERROR. Now I will return first element instead!" << std::endl;
return this->_elements[0];
}
return this->_elements[n];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment