added Vector container
This commit is contained in:
parent
5df2481fee
commit
6b4a4441ba
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
target\
|
@ -1,9 +1,9 @@
|
||||
@ECHO OFF
|
||||
SETLOCAL EnableDelayedExpansion
|
||||
|
||||
SET target_name=Test.exe
|
||||
SET target_name=Kraken-Core.exe
|
||||
|
||||
SET target_dir=Target\
|
||||
SET target_dir=target\
|
||||
|
||||
SET compiler=g++
|
||||
SET compiler_flags=-ggdb -Wall -Wextra
|
||||
|
4
Run.bat
4
Run.bat
@ -1,9 +1,9 @@
|
||||
@ECHO OFF
|
||||
SETLOCAL EnableDelayedExpansion
|
||||
|
||||
SET target_name=Test.exe
|
||||
SET target_name=Kraken-Core.exe
|
||||
|
||||
SET target_dir=Target\
|
||||
SET target_dir=target\
|
||||
|
||||
CALL Build.bat
|
||||
|
||||
|
@ -1,5 +0,0 @@
|
||||
#include <Source/Core/ScalarDataTypes.hpp>
|
||||
|
||||
i32 main() {
|
||||
|
||||
}
|
197
src/Core/Containers/Vector.hpp
Normal file
197
src/Core/Containers/Vector.hpp
Normal file
@ -0,0 +1,197 @@
|
||||
#pragma once
|
||||
|
||||
#include <src/Core/ScalarDataTypes.hpp>
|
||||
|
||||
class OutOfRange {};
|
||||
|
||||
// @todo: implementing copy ctor & copy assignment
|
||||
template<class Type>
|
||||
class Vector {
|
||||
public:
|
||||
Vector();
|
||||
~Vector();
|
||||
|
||||
/**
|
||||
* Element access
|
||||
*/
|
||||
Type& operator[] (i32 index);
|
||||
const Type& operator[] (i32 index) const;
|
||||
|
||||
Type* data() { return M_data; }
|
||||
const Type* data() const { return M_data; }
|
||||
|
||||
/**
|
||||
* 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; }
|
||||
|
||||
/**
|
||||
* Modifiers
|
||||
*/
|
||||
void clear();
|
||||
|
||||
void insert(const Type& value, i32 index);
|
||||
void erase(i32 index);
|
||||
|
||||
void push(const Type& value);
|
||||
void pop();
|
||||
|
||||
void resize(i32 new_capacity);
|
||||
|
||||
private:
|
||||
Type* M_data;
|
||||
i32 M_capacity;
|
||||
i32 M_length;
|
||||
};
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline Vector<Type>::Vector() {
|
||||
i32 initial_capacity = 8;
|
||||
|
||||
M_data = new Type[initial_capacity];
|
||||
M_capacity = initial_capacity;
|
||||
M_length = 0;
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline Vector<Type>::~Vector() {
|
||||
delete[] M_data;
|
||||
|
||||
M_data = nullptr;
|
||||
M_capacity = 0;
|
||||
M_length = 0;
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline Type& Vector<Type>::operator[] (i32 index) {
|
||||
if(index < 0 || index > M_length) {
|
||||
throw OutOfRange();
|
||||
}
|
||||
|
||||
return M_data[index];
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline const Type& Vector<Type>::operator[] (i32 index) const {
|
||||
if(index < 0 || index > M_length) {
|
||||
throw OutOfRange();
|
||||
}
|
||||
|
||||
return M_data[index];
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline void Vector<Type>::reserve(i32 new_capacity) {
|
||||
if(new_capacity < M_capacity) {
|
||||
return;
|
||||
}
|
||||
|
||||
M_capacity = new_capacity;
|
||||
|
||||
Type* new_data = new Type[new_capacity];
|
||||
|
||||
for(i32 i = 0; i < M_length; i += 1) {
|
||||
new_data[i] = M_data[i];
|
||||
}
|
||||
|
||||
delete[] M_data;
|
||||
|
||||
M_data = new_data;
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline void Vector<Type>::clear() {
|
||||
delete[] M_data;
|
||||
|
||||
i32 initial_capacity = 8;
|
||||
|
||||
M_data = new Type[initial_capacity];
|
||||
M_capacity = initial_capacity;
|
||||
M_length = 0;
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline void Vector<Type>::insert(const Type& value, i32 index) {
|
||||
if(index < 0 || index > M_length) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(M_length + 1 > M_capacity) {
|
||||
resize(M_capacity *= 2);
|
||||
}
|
||||
|
||||
for(i32 i = M_length; i > index; i -= 1) {
|
||||
M_data[i] = M_data[i - 1];
|
||||
}
|
||||
|
||||
M_length += 1;
|
||||
|
||||
M_data[index] = value;
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline void Vector<Type>::erase(i32 index) {
|
||||
if(index < 0 || index > M_length) {
|
||||
return;
|
||||
}
|
||||
|
||||
M_length -= 1;
|
||||
|
||||
for(i32 i = index; i < M_length; i += 1) {
|
||||
M_data[i] = M_data[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline void Vector<Type>::push(const Type& value) {
|
||||
if(M_length + 1 > M_capacity) {
|
||||
resize(M_capacity *= 2);
|
||||
}
|
||||
|
||||
M_data[M_length] = value;
|
||||
M_length += 1;
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline void Vector<Type>::resize(i32 new_capacity) {
|
||||
if(new_capacity < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
M_capacity = new_capacity;
|
||||
|
||||
Type* new_data = new Type[new_capacity];
|
||||
|
||||
if(new_capacity < M_length) {
|
||||
M_length = new_capacity;
|
||||
}
|
||||
|
||||
for(i32 i = 0; i < M_length; i += 1) {
|
||||
new_data[i] = M_data[i];
|
||||
}
|
||||
|
||||
delete[] M_data;
|
||||
|
||||
M_data = new_data;
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
inline void Vector<Type>::pop() {
|
||||
--M_length;
|
||||
}
|
41
src/Main.cpp
Normal file
41
src/Main.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <chrono>
|
||||
using namespace std::chrono;
|
||||
|
||||
#define KRAKEN_DEBUG 1
|
||||
|
||||
#include <src/Core/ScalarDataTypes.hpp>
|
||||
#include <src/Core/Containers/Vector.hpp>
|
||||
|
||||
class Timer {
|
||||
public:
|
||||
void start() {
|
||||
M_start_time = high_resolution_clock::now();;
|
||||
}
|
||||
|
||||
void stop() {
|
||||
M_stop_time = high_resolution_clock::now();
|
||||
}
|
||||
|
||||
std::chrono::nanoseconds duration() {
|
||||
auto d = duration_cast<microseconds>(M_stop_time - M_start_time);
|
||||
return d;
|
||||
}
|
||||
|
||||
private:
|
||||
std::chrono::_V2::system_clock::time_point M_start_time, M_stop_time;
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
BIN
target/Kraken-Core.exe
Normal file
BIN
target/Kraken-Core.exe
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user