gedeng/Vector.h
2021-03-05 15:33:41 +01:00

156 lines
4.8 KiB
C++

#pragma once
#include <algorithm>
#include <cassert>
#include <iostream>
template <class T>
class Vector {
public:
// Constructor
// If the needed capacity is known, it can be passed; other wise, a default
// capacity is reserved.
Vector(unsigned int capacity = 10)
: capacity(capacity), element_count(0), data(new T[capacity]) {
}
// Copy Constructor
Vector(const Vector &other)
: capacity(other.capacity), element_count(other.element_count),
data(new T[capacity]) {
// `std::copy` is used because it is more flexible than `std::memcpy`,
// and the compiler will replace it with `memcpy` anyway if appropriate,
// so there is no performance loss.
std::copy(other.data, other.data + element_count, data);
}
// Move Constructor using the copy-and-swap-idiom
Vector(Vector &&other) : data(new T[capacity]) {
swap(*this, other);
}
// Copy Assignment Operator using the copy-and-swap-idiom
// Since this takes a value rather than a const reference due to
// copy-and-swap, this is also the Move Assignment Operator.
// This works because we pass-by-value, causing the copy constructor (which
// does the actual copying) to be called, and then we swap the resulting
// temporary into `this`.
Vector &operator=(Vector other) {
swap(*this, other);
return *this;
}
// Destructor
~Vector() {
delete[] data;
}
// Bracket Operator for accessing elements
T &operator[](unsigned int position) const {
return at(position);
}
// Equals Operator: Returns true if the number of elements is identical and
// each of these elements are equal
bool operator==(const Vector &other) {
if (size() != other.size()) return false;
for (unsigned int i = 0; i < size(); i++) {
if (at(i) != other[i]) {
return false;
}
}
return true;
}
// Swap function for the copy-and-swap idiom.
// See also:
// https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom
// The `friend` keyword is used so it is found through ADL.
friend void swap(Vector &first, Vector &second) {
// Enable ADL (good practice)
using std::swap;
swap(first.capacity, second.capacity);
swap(first.element_count, second.element_count);
swap(first.data, second.data);
}
void push_back(const T &element) {
// TODO: Allocate additional space if needed
// Use placement new to directly use pre-allocated memory
new (data + element_count) T(element);
element_count++;
}
void erase(unsigned int position) {
assert(position < element_count);
// Call the destructor on the given element
data[position].~T();
// Copy the other elements forwards
std::copy_backward(data + position + 1, data + element_count,
data + element_count - 1);
element_count--;
// TODO: Consider deallocating memory if a certain threshold was reached
}
// Returns the number of elements in the vector, regardless of the actually
// reserved memory.
unsigned int size() const {
return element_count;
}
// Returns the number of elements which could be in the vector using the
// currently allocated memory, regardless of how many of these slots are
// currently actually used.
unsigned int length() const {
return capacity;
}
// Returns a reference to the element at the given position
T &at(unsigned int position) const {
assert(position < element_count);
return data[position];
}
// Expand the capacity of the vector by the given addition
void reserve(unsigned int addition) {
// TODO: Actual memory allocation
capacity += addition;
}
// Resize the size of the vector to the given new_size
// If this decreases the size, some elements are deleted
// If this increases the size, some default-constructed elements are added
void resize(unsigned int new_size) {
int difference = new_size - size();
if (difference > 0) {
// TODO: Reserve more space if needed
// Add additional default-constructed items
for (int i = 0; i < difference; i++) {
data[element_count + i] = T();
}
} else if (difference < 0) {
// Call the destructor on all excess items
for (int i = -1; i > difference; i--) {
data[element_count + i].~T();
}
// TODO: Consider deallocating space
}
element_count += difference;
}
private:
T *data;
unsigned int element_count;
unsigned int capacity;
};