-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyArray.h
More file actions
90 lines (72 loc) · 1.89 KB
/
MyArray.h
File metadata and controls
90 lines (72 loc) · 1.89 KB
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
80
81
82
83
84
85
86
87
88
89
#pragma once
#include <cstddef> /// std::size_t
#include <cassert> /// assert()
#include <iostream>
/*
* C++ std::array
* https://en.cppreference.com/w/cpp/container/array
*/
template<typename T, std::size_t _size>
class MyArray {
private:
T data_[_size];
public:
using type = T;
using iterator = T*;
using const_iterator = const T*;
using pointer = T*;
/// important that this is a reference. Otherwise there would be a performance hit if we were to store large objects, they would be copied every time they are accessed.
T& operator[](std::size_t index) {
assert(index < _size);
return data_[index];
}
[[nodiscard]] constexpr std::size_t size() const noexcept {
return _size;
}
[[nodiscard]] constexpr bool empty() const noexcept
{
return size() == 0;
}
T* data() {
return data_;
}
const T* data() const {
return data_;
}
T& front() {
data_[0];
}
T& back() {
data_[_size - 1];
}
constexpr const_iterator begin() const noexcept
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
return data();
}
constexpr iterator begin() noexcept
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
return data();
}
constexpr const_iterator end() const noexcept
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
return data() + _size;
}
constexpr iterator end() noexcept
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
return data() + _size;
}
constexpr const_iterator cbegin() const noexcept
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
return const_iterator(data());
}
constexpr const_iterator cend() const noexcept
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
return const_iterator(data() + _size);
}
};