-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharray.h
More file actions
66 lines (52 loc) · 1.29 KB
/
array.h
File metadata and controls
66 lines (52 loc) · 1.29 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
#ifndef A1_ARRAY_H
#define A1_ARRAY_H
#include <array>
#include <cstddef>
#include <stdexcept>
template <typename T>
class Array
{
public:
class Iterator
{
public:
T &operator*() { return *m_item; }
bool operator!=(const Iterator &other)
{
return m_item != other.m_item;
}
Iterator &operator++()
{
++m_item;
return *this;
}
private:
explicit Iterator(T *item) : m_item{item} {}
T *m_item;
friend class Array;
};
template <std::size_t Size>
Array(T (&data)[Size]) : m_data{data}, m_size{Size}
{
}
template <std::size_t Size>
Array(std::array<T, Size> &array) : m_data{array.data()}, m_size{Size}
{
}
Array(T *data, std::size_t size) : m_data{data}, m_size{size} {}
T &operator[](std::size_t index) { return m_data[index]; }
T &at(std::size_t index)
{
if (index >= m_size)
throw std::out_of_range{"index out of range"};
return m_data[index];
}
Iterator begin() { return Iterator{m_data}; }
Iterator end() { return Iterator{m_data + m_size}; }
T *data() const { return m_data; }
std::size_t size() const { return m_size; }
private:
T *m_data;
std::size_t m_size;
};
#endif