-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_array.h
79 lines (66 loc) · 2.66 KB
/
static_array.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <initializer_list>
#include <algorithm>
//static array of fixed size allocated on the stack
template<typename T, size_t S>
class static_array
{
public:
////////////////////////////////////////////////////////////////////////////////
//constructors
////////////////////////////////////////////////////////////////////////////////
static_array(){}
//constructor with member initializer list
//use case: static_array<T> arr{elem1,elem2...};
static_array(std::initializer_list<T> il)
{ std::copy(il.begin(),il.end(), data_); }
////////////////////////////////////////////////////////////////////////////////
//operators - todo
////////////////////////////////////////////////////////////////////////////////
// inline bool
// operator<(static_array<T,S> a, static_array<T,S> b)
// {}
// inline bool
// operator>(static_array<T,S> a, static_array<T,S> b)
// {}
// inline bool
// operator<=(static_array<T,S> a, static_array<T,S> b)
// {}
// inline bool
// operator>=(static_array<T,S> a, static_array<T,S> b)
// {}
// inline bool
// operator==(static_array<T,S> a, static_array<T,S> b)
// {}
// inline bool
// operator!=(static_array<T,S> a, static_array<T,S> b)
// { return !(a == b); }
////////////////////////////////////////////////////////////////////////////////
//element access
////////////////////////////////////////////////////////////////////////////////
//no bounds checking, accessing an index outside the array is undefined
//behaviour
T& operator[](size_t index){ return data_[index]; }
const T& operator[](size_t index) const { return data_[index]; }
////////////////////////////////////////////////////////////////////////////////
//operations
////////////////////////////////////////////////////////////////////////////////
//returns element at index
//throws if index is invalid
constexpr const T& at(size_t index) const
{
if(index < size())
return data_[index];
throw std::out_of_range ("Index out of range");
}
//access front and back elements
constexpr const T& front() const { return data_[0]; }
constexpr const T& back() const { return data_[size()-1]; }
//check if empty
constexpr bool empty() { return size() == 0; }
//get size and data
constexpr size_t size() const { return S; }
T* data(){ return data_; }
const T* data() const { return data_; }
private:
T data_[S] = {};
};