added copy ctor & copy assign to Vector<T>

This commit is contained in:
nemeth_mihaly 2022-12-16 11:16:56 +01:00
parent 6b4a4441ba
commit c712e48357
5 changed files with 112 additions and 28 deletions

View File

@ -6,7 +6,7 @@ SET target_name=Kraken-Core.exe
SET target_dir=target\ SET target_dir=target\
SET compiler=g++ SET compiler=g++
SET compiler_flags=-ggdb -Wall -Wextra SET compiler_flags=-ggdb -Wall -Wextra -O3
SET linker_flags=-L. SET linker_flags=-L.

View File

@ -4,44 +4,45 @@
class OutOfRange {}; class OutOfRange {};
// @todo: implementing copy ctor & copy assignment
template<class Type> template<class Type>
class Vector { class Vector {
public: public:
Vector(); Vector();
Vector(const Vector& other);
~Vector(); ~Vector();
Vector& operator= (const Vector& other);
/** /**
* Element access * Element access
*/ */
Type& operator[] (i32 index); Type& operator[] (i32 index);
const Type& operator[] (i32 index) const; const Type& operator[] (i32 index) const;
Type* data() { return M_data; } Type* data();
const Type* data() const { return M_data; } const Type* data() const;
/** /**
* Capacity * Capacity
*/ */
bool is_empty() const { return (M_length == 0); } bool is_empty() const;
i32 length() const { return M_length; } i32 length() const;
void reserve(i32 new_capacity);
void reserve(i32 new_capacity); i32 capacity() const;
i32 capacity() const { return M_capacity; }
/** /**
* Modifiers * Modifiers
*/ */
void clear(); void clear();
void insert(const Type& value, i32 index); void insert(const Type& value, i32 index);
void erase(i32 index); void erase(i32 index);
void push(const Type& value); void push(const Type& value);
void pop(); void pop();
void resize(i32 new_capacity); void resize(i32 new_capacity);
private: private:
Type* M_data; Type* M_data;
@ -52,7 +53,7 @@ class Vector {
template<class Type> template<class Type>
inline Vector<Type>::Vector() { inline Vector<Type>::Vector() {
i32 initial_capacity = 8; const i32 initial_capacity = 8;
M_data = new Type[initial_capacity]; M_data = new Type[initial_capacity];
M_capacity = 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> template<class Type>
inline Vector<Type>::~Vector() { inline Vector<Type>::~Vector() {
delete[] M_data; if(M_data) {
delete[] M_data;
}
M_data = nullptr; M_data = nullptr;
M_capacity = 0; 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> template<class Type>
inline Type& Vector<Type>::operator[] (i32 index) { inline Type& Vector<Type>::operator[] (i32 index) {
if(index < 0 || index > M_length) { if(index < 0 || index > M_length) {
@ -87,7 +115,35 @@ inline const Type& Vector<Type>::operator[] (i32 index) const {
} }
return M_data[index]; 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> template<class Type>
@ -104,15 +160,25 @@ inline void Vector<Type>::reserve(i32 new_capacity) {
new_data[i] = M_data[i]; new_data[i] = M_data[i];
} }
delete[] M_data; if(M_data) {
delete[] M_data;
}
M_data = new_data; M_data = new_data;
} }
template<class Type>
inline i32 Vector<Type>::capacity() const {
return M_capacity;
}
template<class Type> template<class Type>
inline void Vector<Type>::clear() { inline void Vector<Type>::clear() {
delete[] M_data; if(M_data) {
delete[] M_data;
}
i32 initial_capacity = 8; i32 initial_capacity = 8;
@ -185,7 +251,9 @@ inline void Vector<Type>::resize(i32 new_capacity) {
new_data[i] = M_data[i]; new_data[i] = M_data[i];
} }
delete[] M_data; if(M_data) {
delete[] M_data;
}
M_data = new_data; M_data = new_data;
} }

View File

@ -2,7 +2,7 @@
/** /**
* Integer Types * Integer Types
* **/ */
typedef signed char i8; typedef signed char i8;
typedef signed short i16; typedef signed short i16;
typedef signed int i32; typedef signed int i32;
@ -19,6 +19,6 @@ typedef u64 usize;
/** /**
* Floating-Point Types * Floating-Point Types
* **/ */
typedef float f32; typedef float f32;
typedef double f64; typedef double f64;

View File

@ -18,9 +18,8 @@ class Timer {
M_stop_time = high_resolution_clock::now(); M_stop_time = high_resolution_clock::now();
} }
std::chrono::nanoseconds duration() { std::chrono::microseconds duration() const {
auto d = duration_cast<microseconds>(M_stop_time - M_start_time); return duration_cast<microseconds>(M_stop_time - M_start_time);;
return d;
} }
private: private:
@ -31,11 +30,28 @@ void print_vector(const Vector<i32>& v) {
for(i32 i = 0; i < v.length(); i += 1) { for(i32 i = 0; i < v.length(); i += 1) {
std::cout << v[i] << ' '; std::cout << v[i] << ' ';
} }
std::cout << std::endl; std::cout << std::endl;
} }
i32 main() { i32 main() {
{ {
Vector<i32> v; 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.