-
Notifications
You must be signed in to change notification settings - Fork 1
/
iterator.cpp
170 lines (142 loc) · 6.12 KB
/
iterator.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <memory>
#include <iostream>
#include <cstdint>
#include <cstring>
template <typename Item>
class staticArray {
public:
// ------------- Iterators -------------- //
typedef int size_type;
class iterator
{
public:
/* C++17 comittee voted to depreciate inheriting from std::iterator
and recommandates to write the following typedefs instead */
typedef iterator self_type;
typedef Item value_type;
typedef Item& reference;
typedef Item* pointer;
typedef int difference_type;
typedef std::forward_iterator_tag iterator_category;
iterator(pointer p_ptr) : m_ptr(p_ptr) {}
virtual ~iterator() = default;
self_type operator++(int dummy) /* Postfix */ {
(void)dummy; self_type l_ret = *this; m_ptr++; return l_ret;
}
self_type operator++(void) /* Prefix */ { m_ptr++; return *this; }
reference operator* (void) { return *m_ptr; }
pointer operator->(void) { return m_ptr; }
bool operator==(const self_type& p_rhs ) { return m_ptr == p_rhs.m_ptr; }
bool operator!=(const self_type& p_rhs ) { return m_ptr != p_rhs.m_ptr; }
private:
pointer m_ptr;
};
class reverse_iterator
{
public:
/* C++17 comittee voted to depreciate inheriting from std::iterator
and recommandates to write the following typedefs instead */
typedef reverse_iterator self_type;
typedef Item value_type;
typedef Item& reference;
typedef Item* pointer;
typedef int difference_type;
typedef std::forward_iterator_tag iterator_category;
reverse_iterator(pointer p_ptr) : m_ptr(p_ptr) {}
virtual ~reverse_iterator() = default;
self_type operator++(int dummy) /* Postfix */ {
(void)dummy; self_type l_ret = *this; m_ptr--; return l_ret;
}
self_type operator++(void) /* Prefix */ { m_ptr--; return *this; }
reference operator* (void) { return *m_ptr; }
pointer operator->(void) { return m_ptr; }
bool operator==(const self_type& p_rhs) { return m_ptr == p_rhs.m_ptr; }
bool operator!=(const self_type& p_rhs) { return m_ptr != p_rhs.m_ptr; }
private:
pointer m_ptr;
};
class const_iterator
{
public:
/* C++17 comittee voted to depreciate inheriting from std::iterator
and recommandates to write the following typedefs instead */
typedef const_iterator self_type;
typedef Item value_type;
typedef Item& reference;
typedef Item* pointer;
typedef int difference_type;
typedef std::forward_iterator_tag iterator_category;
const_iterator(pointer p_ptr) : m_ptr(p_ptr) {}
virtual ~const_iterator() = default;
self_type operator++(int dummy) /* Postfix */ {
(void)dummy; self_type l_ret = *this; m_ptr++; return l_ret;
}
self_type operator++(void) /* Prefix */ { m_ptr++; return *this; }
const reference operator* (void) { return *m_ptr; }
pointer operator->(void) { return m_ptr; }
bool operator==(const self_type& p_rhs) { return m_ptr == p_rhs.m_ptr; }
bool operator!=(const self_type& p_rhs) { return m_ptr != p_rhs.m_ptr; }
private:
pointer m_ptr;
};
// ----- Constructors && destructor ----- //
staticArray(size_type p_capacity) : m_capa(p_capacity) {
ASSERT(m_capa >= 0, "staticArray cannot have a negative capacity!");
m_data = new Item[m_capa];
}
virtual ~staticArray() { clear(); }
// ----------- Public methods ----------- //
size_type size (void) const { return m_capa; }
void clear(void) {
if ( m_data ) {
delete[] m_data;
m_data = nullptr;
m_capa = 0;
}
}
Item& operator[](size_type p_index) {
ASSERT(p_index >= 0 && p_index < m_capa, "Out of range!");
return m_data[p_index];
}
const Item& operator[](size_type p_index) const {
ASSERT(p_index >= 0 && p_index < m_capa, "Out of range!");
return m_data[p_index];
}
iterator begin (void) { return iterator(m_data); }
iterator end (void) { return iterator(m_data + m_capa); }
reverse_iterator rbegin (void) { return reverse_iterator(m_data + m_capa - 1); }
reverse_iterator rend (void) { return reverse_iterator(m_data - 1); }
const_iterator cbegin (void) const { return const_iterator(m_data); }
const_iterator cend (void) const { return const_iterator(m_data + m_capa); }
protected:
void ASSERT(bool cond, const std::string& msg) {
if ( !(cond) ) {
std::cerr << "Assertion `" << cond << "` failed in " << __FILE__
<< " line " << __LINE__ << ": " << msg << std::endl;
std::terminate();
}
}
private:
size_type m_capa;
Item* m_data;
};
// ------------ Client code ------------ //
int main()
{
staticArray<int> myArray(100);
int i(0);
for ( auto it = myArray.begin();
it != myArray.end ();
it++ ) {
*it = i++;
}
for ( auto it = myArray.rbegin();
it != myArray.rend ();
it++ ) {
std::cout << *it << " ";
}
std::cout << std::endl;
std::cout << myArray[8] << std::endl;
// std::cout << myArray[102] << std::endl; /*!< Throws */
return 0;
}