Doxygen Samurai Engine 0.0.1
Doxygen Samurai Engine Documentation
Loading...
Searching...
No Matches
staticVector.h
Go to the documentation of this file.
1#pragma once
2
3#include "assert/assert.h"
4
5
6namespace samurai
7{
8
9
10 template<class T, size_t N>
12 {
13 typedef T *iterator;
14 typedef const T *constIterator;
15
16 iterator begin() { return &((T *)beg_)[0]; }
17 constIterator begin() const { return &((T *)beg_)[0]; }
18 iterator end() { return &((T *)beg_)[size_]; }
19 constIterator end() const { return &((T *)beg_)[size_]; }
20
21 static constexpr unsigned int MAX_SIZE = N;
22 static constexpr unsigned int capacity = N;
23
25
26 StaticVector(std::initializer_list<T> &&l)
27 {
28 for (auto &i : l)
29 {
30 push_back(i);
31 }
32 };
33
35 {
36 for (size_t i = 0; i < other.size_; i++)
37 {
38 beg_[i] = std::move(other.beg_[i]);
39 }
40
41 this->size_ = other.size_;
42 other.size_ = 0;
43 }
44
46 {
47 for (size_t i = 0; i < other.size_; i++)
48 {
49 beg_[i] = other.beg_[i];
50 }
51
52 this->size_ = other.size_;
53 }
54
55 size_t size()const { return size_; }
56
57 bool empty() const
58 {
59 return (size_ == 0);
60 }
61
62 T *data()
63 {
64 return beg_;
65 }
66
68 {
69 if (this == &other)
70 {
71 return *this;
72 }
73
74 for (size_t i = 0; i < other.size_; i++)
75 {
76 beg_[i] = other.beg_[i];
77 }
78 this->size_ = other.size_;
79
80 return *this;
81 }
82
84 {
85 if (this == &other)
86 {
87 return *this;
88 }
89
90 for (size_t i = 0; i < other.size_; i++)
91 {
92 beg_[i] = std::move(other.beg_[i]);
93 }
94
95 this->size_ = other.size_;
96 other.size_ = 0;
97
98 return *this;
99 }
100
101 bool operator==(const StaticVector &other)
102 {
103 if (this == &other) { return true; }
104
105 if (this->size_ != other.size_) { return false; }
106
107 for (int i = 0; i < size_; i++)
108 {
109 if ( (*this)[i] != other[i])
110 {
111 return false;
112 }
113 }
114
115 return true;
116 }
117
118 T &operator[] (size_t index)
119 {
120 SAMURAI_ASSERT(index < size_, "buffer overflow on acces");
121 return (beg_)[index];
122 }
123
124 T operator[] (size_t index) const
125 {
126 SAMURAI_ASSERT(index < size_, "buffer overflow on acces");
127 return (beg_)[index];
128 }
129
130 T &back()
131 {
132 return (*this)[size_ - 1];
133 }
134
135 const T &back() const
136 {
137 return (*this)[size_ - 1];
138 }
139
140 void clear() { size_ = 0; }
141
142 void push_back(const T &el)
143 {
144 SAMURAI_ASSERT(size_ < MAX_SIZE, "exceded max size in push back");
145 beg_[size_] = el;
146 size_++;
147 }
148
149 void push_back(T &&el)
150 {
151 SAMURAI_ASSERT(size_ < MAX_SIZE, "exceded max size in push back");
152 beg_[size_] = std::forward<T>(el);
153 size_++;
154 }
155
156 void pop_back()
157 {
158 SAMURAI_ASSERT(size_ > 0, "buffer underflow on pop back");
159 size_--;
160 }
161
162
163 size_t size_ = 0;
164 T beg_[N];
165
166 };
167
168
169
170}
#define SAMURAI_ASSERT(expression, comment)
Definition assert.h:40
StaticVector(std::initializer_list< T > &&l)
void push_back(const T &el)
size_t size() const
StaticVector & operator=(const StaticVector &other)
static constexpr unsigned int capacity
StaticVector(const StaticVector &other)
constIterator begin() const
StaticVector(StaticVector &&other)
constIterator end() const
T & operator[](size_t index)
const T & back() const
static constexpr unsigned int MAX_SIZE
bool operator==(const StaticVector &other)