added copy ctor & copy assign to Vector<T>
This commit is contained in:
parent
6b4a4441ba
commit
c712e48357
@ -6,7 +6,7 @@ SET target_name=Kraken-Core.exe
|
||||
SET target_dir=target\
|
||||
|
||||
SET compiler=g++
|
||||
SET compiler_flags=-ggdb -Wall -Wextra
|
||||
SET compiler_flags=-ggdb -Wall -Wextra -O3
|
||||
|
||||
SET linker_flags=-L.
|
||||
|
||||
|
@ -4,44 +4,45 @@
|
||||
|
||||
class OutOfRange {};
|
||||
|
||||
// @todo: implementing copy ctor & copy assignment
|
||||
template<class Type>
|
||||
class Vector {
|
||||
public:
|
||||
Vector();
|
||||
Vector(const Vector& other);
|
||||
|
||||
~Vector();
|
||||
|
||||
Vector& operator= (const Vector& other);
|
||||
|
||||
/**
|
||||
* Element access
|
||||
*/
|
||||
*/
|
||||
Type& operator[] (i32 index);
|
||||
const Type& operator[] (i32 index) const;
|
||||
|
||||
Type* data() { return M_data; }
|
||||
const Type* data() const { return M_data; }
|
||||
Type* data();
|
||||
const Type* data() const;
|
||||
|
||||
/**
|
||||
* Capacity
|
||||
*/
|
||||
bool is_empty() const { return (M_length == 0); }
|
||||
i32 length() const { return M_length; }
|
||||
|
||||
void reserve(i32 new_capacity);
|
||||
|
||||
i32 capacity() const { return M_capacity; }
|
||||
bool is_empty() const;
|
||||
i32 length() const;
|
||||
void reserve(i32 new_capacity);
|
||||
i32 capacity() const;
|
||||
|
||||
/**
|
||||
* Modifiers
|
||||
*/
|
||||
void clear();
|
||||
void clear();
|
||||
|
||||
void insert(const Type& value, i32 index);
|
||||
void erase(i32 index);
|
||||
void insert(const Type& value, i32 index);
|
||||
void erase(i32 index);
|
||||
|
||||
void push(const Type& value);
|
||||
void pop();
|
||||
void push(const Type& value);
|
||||
void pop();
|
||||
|
||||
void resize(i32 new_capacity);
|
||||
void resize(i32 new_capacity);
|
||||
|
||||
private:
|
||||
Type* M_data;
|
||||
@ -52,7 +53,7 @@ class Vector {
|
||||
|
||||
template<class Type>
|
||||
inline Vector<Type>::Vector() {
|
||||
i32 initial_capacity = 8;
|
||||
const i32 initial_capacity = 8;
|
||||
|
||||
M_data = new Type[initial_capacity];
|
||||
M_capacity = initial_capacity;
|
||||
@ -60,9 +61,18 @@ inline Vector<Type>::Vector() {
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline Vector<Type>::Vector(const Vector<Type>& other) {
|
||||
M_data = nullptr;
|
||||
*this = other;
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline Vector<Type>::~Vector() {
|
||||
delete[] M_data;
|
||||
if(M_data) {
|
||||
delete[] M_data;
|
||||
}
|
||||
|
||||
M_data = nullptr;
|
||||
M_capacity = 0;
|
||||
@ -70,6 +80,24 @@ inline Vector<Type>::~Vector() {
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline Vector<Type>& Vector<Type>::operator= (const Vector<Type>& other) {
|
||||
clear();
|
||||
|
||||
const i32 new_capacity = other.M_capacity;
|
||||
|
||||
M_data = new Type[new_capacity];
|
||||
M_capacity = new_capacity;
|
||||
M_length = other.M_length;
|
||||
|
||||
for(i32 i = 0; i < other.M_length; i += 1) {
|
||||
M_data[i] = other.M_data[i];
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline Type& Vector<Type>::operator[] (i32 index) {
|
||||
if(index < 0 || index > M_length) {
|
||||
@ -87,7 +115,35 @@ inline const Type& Vector<Type>::operator[] (i32 index) const {
|
||||
}
|
||||
|
||||
return M_data[index];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline Type* Vector<Type>::data() {
|
||||
return M_data;
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline const Type* Vector<Type>::data() const {
|
||||
return M_data;
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline bool Vector<Type>::is_empty() const {
|
||||
if(M_length > 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline i32 Vector<Type>::length() const {
|
||||
return M_length;
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
@ -104,15 +160,25 @@ inline void Vector<Type>::reserve(i32 new_capacity) {
|
||||
new_data[i] = M_data[i];
|
||||
}
|
||||
|
||||
delete[] M_data;
|
||||
if(M_data) {
|
||||
delete[] M_data;
|
||||
}
|
||||
|
||||
M_data = new_data;
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline i32 Vector<Type>::capacity() const {
|
||||
return M_capacity;
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline void Vector<Type>::clear() {
|
||||
delete[] M_data;
|
||||
if(M_data) {
|
||||
delete[] M_data;
|
||||
}
|
||||
|
||||
i32 initial_capacity = 8;
|
||||
|
||||
@ -185,7 +251,9 @@ inline void Vector<Type>::resize(i32 new_capacity) {
|
||||
new_data[i] = M_data[i];
|
||||
}
|
||||
|
||||
delete[] M_data;
|
||||
if(M_data) {
|
||||
delete[] M_data;
|
||||
}
|
||||
|
||||
M_data = new_data;
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* Integer Types
|
||||
* **/
|
||||
*/
|
||||
typedef signed char i8;
|
||||
typedef signed short i16;
|
||||
typedef signed int i32;
|
||||
@ -19,6 +19,6 @@ typedef u64 usize;
|
||||
|
||||
/**
|
||||
* Floating-Point Types
|
||||
* **/
|
||||
*/
|
||||
typedef float f32;
|
||||
typedef double f64;
|
22
src/Main.cpp
22
src/Main.cpp
@ -18,9 +18,8 @@ class Timer {
|
||||
M_stop_time = high_resolution_clock::now();
|
||||
}
|
||||
|
||||
std::chrono::nanoseconds duration() {
|
||||
auto d = duration_cast<microseconds>(M_stop_time - M_start_time);
|
||||
return d;
|
||||
std::chrono::microseconds duration() const {
|
||||
return duration_cast<microseconds>(M_stop_time - M_start_time);;
|
||||
}
|
||||
|
||||
private:
|
||||
@ -31,11 +30,28 @@ void print_vector(const Vector<i32>& v) {
|
||||
for(i32 i = 0; i < v.length(); i += 1) {
|
||||
std::cout << v[i] << ' ';
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
i32 main() {
|
||||
{
|
||||
Vector<i32> v;
|
||||
|
||||
v.push(1);
|
||||
v.push(2);
|
||||
v.push(3);
|
||||
|
||||
Vector<i32> w;
|
||||
|
||||
w.push(100);
|
||||
w.push(99);
|
||||
|
||||
print_vector(w);
|
||||
|
||||
w = v;
|
||||
|
||||
print_vector(v);
|
||||
print_vector(w);
|
||||
}
|
||||
}
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user