diff --git a/Build.bat b/Build.bat index 0359a35..beb97e4 100644 --- a/Build.bat +++ b/Build.bat @@ -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. diff --git a/src/Core/Containers/Vector.hpp b/src/Core/Containers/Vector.hpp index 664d6ff..dbbd095 100644 --- a/src/Core/Containers/Vector.hpp +++ b/src/Core/Containers/Vector.hpp @@ -4,44 +4,45 @@ class OutOfRange {}; -// @todo: implementing copy ctor & copy assignment template 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 inline Vector::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::Vector() { } +template +inline Vector::Vector(const Vector& other) { + M_data = nullptr; + *this = other; +} + + template inline Vector::~Vector() { - delete[] M_data; + if(M_data) { + delete[] M_data; + } M_data = nullptr; M_capacity = 0; @@ -70,6 +80,24 @@ inline Vector::~Vector() { } +template +inline Vector& Vector::operator= (const Vector& 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 inline Type& Vector::operator[] (i32 index) { if(index < 0 || index > M_length) { @@ -87,7 +115,35 @@ inline const Type& Vector::operator[] (i32 index) const { } return M_data[index]; -} +} + + +template +inline Type* Vector::data() { + return M_data; +} + + +template +inline const Type* Vector::data() const { + return M_data; +} + + +template +inline bool Vector::is_empty() const { + if(M_length > 0) { + return false; + } + + return true; +} + + +template +inline i32 Vector::length() const { + return M_length; +} template @@ -104,15 +160,25 @@ inline void Vector::reserve(i32 new_capacity) { new_data[i] = M_data[i]; } - delete[] M_data; + if(M_data) { + delete[] M_data; + } M_data = new_data; } +template +inline i32 Vector::capacity() const { + return M_capacity; +} + + template inline void Vector::clear() { - delete[] M_data; + if(M_data) { + delete[] M_data; + } i32 initial_capacity = 8; @@ -185,7 +251,9 @@ inline void Vector::resize(i32 new_capacity) { new_data[i] = M_data[i]; } - delete[] M_data; + if(M_data) { + delete[] M_data; + } M_data = new_data; } diff --git a/src/Core/ScalarDataTypes.hpp b/src/Core/ScalarDataTypes.hpp index 5bc9edc..646ccd5 100644 --- a/src/Core/ScalarDataTypes.hpp +++ b/src/Core/ScalarDataTypes.hpp @@ -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; \ No newline at end of file diff --git a/src/Main.cpp b/src/Main.cpp index 44cff64..78732b3 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -18,9 +18,8 @@ class Timer { M_stop_time = high_resolution_clock::now(); } - std::chrono::nanoseconds duration() { - auto d = duration_cast(M_stop_time - M_start_time); - return d; + std::chrono::microseconds duration() const { + return duration_cast(M_stop_time - M_start_time);; } private: @@ -31,11 +30,28 @@ void print_vector(const Vector& v) { for(i32 i = 0; i < v.length(); i += 1) { std::cout << v[i] << ' '; } + std::cout << std::endl; } i32 main() { { Vector v; + + v.push(1); + v.push(2); + v.push(3); + + Vector w; + + w.push(100); + w.push(99); + + print_vector(w); + + w = v; + + print_vector(v); + print_vector(w); } } \ No newline at end of file diff --git a/target/Kraken-Core.exe b/target/Kraken-Core.exe index 0441bfc..f06d6f8 100644 Binary files a/target/Kraken-Core.exe and b/target/Kraken-Core.exe differ