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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
#ifndef HEADER_LIST
#define HEADER_LIST
/*! \file List.hpp
* \brief Stores declaration and definition of templated class List
*/
#include <istream>
#include <ostream>
/*! \brief Templated class that stores an array of elements in dynamic memory
*
* \warning findIndex(), contain(), read(), write(), operator<<() and operator>>() require the type to have defined a couple of functions.
* \sa findIndex()
* \sa contain()
* \sa read()
* \sa write()
* \sa operator<<()
* \sa operator>>()
*/
template <typename T>
class List {
protected:
T* elements;
unsigned length;
unsigned count;
void resize();
void free();
void copyFrom(const List& other);
public:
//! Copies all elements in given array
List(const T* elements, unsigned elementsCount);
//! Adds an element
void add(const T& element);
//! Inserts element at given index
void insertAt(const T& element, unsigned index);
//! Removes element at index
bool removeAt(unsigned index);
//! Finds the index of element
unsigned findIndex(const T& element) const;
bool contain(const T& element) const;
//! Returns reference to element at index
T& operator[](unsigned index);
//! Returns constant reference to element at index
const T& operator[](unsigned index) const;
//! Appends elements from other list
List<T>& operator+=(const List<T> other);
//! Reads from stream
std::istream& read(std::istream& istr);
//! Writes to stream
std::ostream& write(std::ostream& ostr) const;
//! Returns the length
unsigned get_length() const;
//! Returns the count
unsigned get_count() const;
List();
List& operator=(const List& other);
List(const List& other);
~List();
List(List&& other);
List& operator=(List&& other);
};
//! Reads List from stream with >> operator
template <typename T>
std::istream& operator>>(std::istream& istr, List<T>& obj);
//! Writes List to stream with << operator
template <typename T>
std::ostream& operator<<(std::ostream& ostr, const List<T>& obj);
/* Private */
template <typename T>
void List<T>::resize() {
length = (length == 0) ? 8 : length << 1;
T* temp = new T[length];
for (int i = 0; i < count; i++)
temp[i] = elements[i];
delete[] elements;
elements = temp;
}
template <typename T>
void List<T>::free() {
delete[] elements;
}
template <typename T>
void List<T>::copyFrom(const List& other) {
elements = new T[other.length];
for (int i = 0; i < other.count; i++)
elements[i] = other.elements[i];
length = other.length;
count = other.count;
}
/* Public */
template <typename T>
List<T>::List(const T* elements, unsigned elementsCount) {
length = 8;
count = elementsCount;
this->elements = new T[length];
for (unsigned i = 0; i < count; i++)
this->elements[i] = elements[i];
}
/*!
* Resizes internal array if there is no space for additional elements.
*/
template <typename T>
void List<T>::add(const T& element) {
if (length == count) resize();
elements[count++] = element;
}
/*!
* If index is after the last element, the element is just added.
* Otherwise all elements after the index are shifted right and element is put in place.
*
* Resizes internal array if there is no space for additional elements.
*/
template <typename T>
void List<T>::insertAt(const T& element, unsigned index) {
if (index >= count) {
add(element);
return;
}
if (length == count) resize();
for (unsigned i = count; i > index; i--)
elements[i] = elements[i - 1];
elements[index] = element;
count++;
}
/*! \returns Wether element could be removed
*
* If index is after that of the last element, nothing is done and false is returned.
* Otherwise elements after index are shifted right and count is reduced.
*/
template <typename T>
bool List<T>::removeAt(unsigned index) {
if (index >= count) return false;
for (int i = index; i < count; i++)
elements[i] = elements[i + 1];
count--;
return true;
}
/*! \returns Index of element. If element isn't found, returns the count of element.
*
* \warning The function depends on the type having a function "compare" defined, which takes two elements and returns a number <0 if elem1 < elem2, >0 if elem1 > elem2, 0 if elem1 == elem2
* \note Searching is done linearly
*/
template <typename T>
unsigned List<T>::findIndex(const T& element) const {
unsigned ind = 0;
while (ind < count && elements[ind].compare(element) != 0)
ind++;
return ind;
}
/*! \returns Whether the element is contained in the current List
*
* \warning Function depends on findIndex(), which means the same "compare" function must be defined in the type
* \sa findIndex()
*/
template <typename T>
bool List<T>::contain(const T& element) const {
return findIndex(element) < count;
}
template <typename T>
T& List<T>::operator[](unsigned index) {
return elements[index];
}
template <typename T>
const T& List<T>::operator[](unsigned index) const {
return elements[index];
}
template <typename T>
List<T>& List<T>::operator+=(const List<T> other) {
for (unsigned i = 0; i < other.length; i++)
add(other[i]);
return *this;
}
/*! \param istr An input stream
*
* Directly reads bytes from stream (calls read() function).
* Any stored values are deleted and replaced with those from the stream.
*
* \warning The function depends on the type having a function "read" defined, which takes an std::istream& and writes it's data to it. Return type doesn't matter.
* \remark Doesn't alter the stream in any other way.
* \note Best used with binary ifstream
*/
template <typename T>
std::istream& List<T>::read(std::istream& istr) {
istr.read((char*)&length, sizeof(length));
istr.read((char*)&count, sizeof(count));
delete[] elements;
elements = new T[length];
for (int i = 0; i < count; i++)
elements[i].read(istr);
return istr;
}
/*! \param ostr An output stream
*
* Directly writes bytes to stream (calls write() function).
*
* \warning The function depends on the type having a function "write" defined, which takes an std::ostream& and writes it's data to it. Return type doesn't matter.
* \remark Doesn't alter the stream in any other way.
* \note Best used with binary ofstream
*/
template <typename T>
std::ostream& List<T>::write(std::ostream& ostr) const {
ostr.write((const char*)&length, sizeof(length));
ostr.write((const char*)&count, sizeof(count));
for (int i = 0; i < count; i++)
elements[i].write(ostr);
return ostr;
}
/*!
* Length is the size of the underlying array (allocated memory).
*/
template <typename T>
unsigned List<T>::get_length() const {
return length;
}
/*!
* Count is the amount of elements that are stored.
*/
template <typename T>
unsigned List<T>::get_count() const {
return count;
}
// Rule of 4
template <typename T>
List<T>::List() : List(nullptr, 0) {}
template <typename T>
List<T>& List<T>::operator=(const List& other) {
if (this != &other) {
free();
copyFrom(other);
}
return *this;
}
template <typename T>
List<T>::List(const List& other) {
copyFrom(other);
}
template <typename T>
List<T>::~List() {
free();
}
// Move semantics
template <typename T>
List<T>::List(List&& other) {
length = other.length;
count = other.count;
elements = other.elements;
other.elements = nullptr;
}
template <typename T>
List<T>& List<T>::operator=(List&& other) {
if (this != &other) {
free();
length = other.length;
count = other.count;
elements = other.elements;
other.elements = nullptr;
}
return *this;
}
/* Outside of class */
/*!
* Uses the stream's >> operator to read and parse the elements.
* The first item in the stream should be the count.
*
* \warning The function depends on the type having the operator >> defined, which takes an std::istream& and writes it's data to it. Return type doesn't matter.
* \note Best used with std::cin or text std::ifstream
*/
template <typename T>
std::istream& operator>>(std::istream& istr, List<T>& obj) {
List<T> newObj;
unsigned count;
istr >> count;
T temp;
for (int i = 0; i < count; i++) {
istr >> temp;
obj.add(temp);
}
return istr;
}
/*!
* Uses the stream's << operator to write the count and then all objects.
*
* \warning The function depends on the type having the operator << defined, which takes an std::ostream& and writes it's data to it. Return type doesn't matter.
* \note Best used with std::cout or text std::ofstream
*/
template <typename T>
std::ostream& operator<<(std::ostream& ostr, const List<T>& obj) {
ostr << obj.get_count() << std::endl;
for (int i = 0; i < obj.get_count(); i++)
ostr << obj[i];
return ostr;
}
#endif
|