-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstatic-array.hpp
More file actions
230 lines (195 loc) · 6.1 KB
/
static-array.hpp
File metadata and controls
230 lines (195 loc) · 6.1 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#ifndef _SCTL_STATIC_ARRAY_HPP_
#define _SCTL_STATIC_ARRAY_HPP_
#include <initializer_list> // for initializer_list
#include "sctl/common.hpp" // for Long, sctl
namespace sctl {
#ifdef SCTL_MEMDEBUG
/**
* A fixed-size array class with additional functionalities for memory debugging.
* This class provides a wrapper around a fixed-size array with custom iterators and
* various operators to facilitate element access and manipulation. It is used when
* `SCTL_MEMDEBUG` is defined. In a regular build, `StaticArray` is an alias for a
* C-style fixed-size array.
*
* @tparam ValueType The type of elements stored in the array.
* @tparam DIM The number of elements in the array.
*/
template <class ValueType, Long DIM> class StaticArray {
typedef Long difference_type;
public:
/**
* Default constructor.
*/
StaticArray() = default;
/**
* Copy constructor.
*
* @param other The `StaticArray` to copy from.
*/
StaticArray(const StaticArray&) = default;
/**
* Copy assignment operator.
*
* @param other The `StaticArray` to copy from.
* @return A reference to the updated `StaticArray`.
*/
StaticArray& operator=(const StaticArray&) = default;
/**
* Constructor with initializer list.
*
* @param arr_ An initializer list to initialize the array.
*/
explicit StaticArray(std::initializer_list<ValueType> arr_);
/**
* Default destructor.
*/
~StaticArray() = default;
/**
* Dereference operator (const).
*
* @return A const reference to the element at the current position.
*/
const ValueType& operator*() const;
/**
* Dereference operator.
*
* @return A reference to the element at the current position.
*/
ValueType& operator*();
/**
* Member access operator (const).
*
* @return A const pointer to the element at the current position.
*/
const ValueType* operator->() const;
/**
* Member access operator.
*
* @return A pointer to the element at the current position.
*/
ValueType* operator->();
/**
* Subscript operator (const).
*
* @param off The offset from the beginning of the array.
* @return A const reference to the element at the specified offset.
*/
const ValueType& operator[](difference_type off) const;
/**
* Subscript operator.
*
* @param off The offset from the beginning of the array.
* @return A reference to the element at the specified offset.
*/
ValueType& operator[](difference_type off);
/**
* Conversion to constant iterator.
*
* @return A constant iterator pointing to the beginning of the array.
*/
operator ConstIterator<ValueType>() const;
/**
* Conversion to iterator.
*
* @return An iterator pointing to the beginning of the array.
*/
operator Iterator<ValueType>();
/**
* Addition operator (const).
*
* @param i The offset to add.
* @return A constant iterator pointing to the new position.
*/
ConstIterator<ValueType> operator+(difference_type i) const;
/**
* Addition operator.
*
* @param i The offset to add.
* @return An iterator pointing to the new position.
*/
Iterator<ValueType> operator+(difference_type i);
/**
* Addition operator for constant iterator.
*
* @param i The offset to add.
* @param right The `StaticArray` to operate on.
* @return A constant iterator pointing to the new position.
*/
template <class T, Long d> friend ConstIterator<T> operator+(typename StaticArray<T,d>::difference_type i, const StaticArray<T,d>& right);
/**
* Addition operator for iterator.
*
* @param i The offset to add.
* @param right The `StaticArray` to operate on.
* @return An iterator pointing to the new position.
*/
template <class T, Long d> friend Iterator<T> operator+(typename StaticArray<T,d>::difference_type i, StaticArray<T,d>& right);
/**
* Subtraction operator (const).
*
* @param i The offset to subtract.
* @return A constant iterator pointing to the new position.
*/
ConstIterator<ValueType> operator-(difference_type i) const;
/**
* Subtraction operator.
*
* @param i The offset to subtract.
* @return An iterator pointing to the new position.
*/
Iterator<ValueType> operator-(difference_type i);
/**
* Difference operator.
*
* @param I The constant iterator to subtract.
* @return The difference between the current position and the iterator.
*/
difference_type operator-(const ConstIterator<ValueType>& I) const;
/**
* Equality comparison operator.
*
* @param I The constant iterator to compare with.
* @return `true` if the iterators are equal, `false` otherwise.
*/
bool operator==(const ConstIterator<ValueType>& I) const;
/**
* Inequality comparison operator.
*
* @param I The constant iterator to compare with.
* @return `true` if the iterators are not equal, `false` otherwise.
*/
bool operator!=(const ConstIterator<ValueType>& I) const;
/**
* Less-than comparison operator.
*
* @param I The constant iterator to compare with.
* @return `true` if the current iterator is less than the given iterator, `false` otherwise.
*/
bool operator< (const ConstIterator<ValueType>& I) const;
/**
* Less-than-or-equal comparison operator.
*
* @param I The constant iterator to compare with.
* @return `true` if the current iterator is less than or equal to the given iterator, `false` otherwise.
*/
bool operator<=(const ConstIterator<ValueType>& I) const;
/**
* Greater-than comparison operator.
*
* @param I The constant iterator to compare with.
* @return `true` if the current iterator is greater than the given iterator, `false` otherwise.
*/
bool operator> (const ConstIterator<ValueType>& I) const;
/**
* Greater-than-or-equal comparison operator.
*
* @param I The constant iterator to compare with.
* @return `true` if the current iterator is greater than or equal to the given iterator, `false` otherwise.
*/
bool operator>=(const ConstIterator<ValueType>& I) const;
private:
ValueType arr_[DIM]; ///< The array of elements.
};
#endif
} // end namespace sctl
#endif // _SCTL_STATIC_ARRAY_HPP_