FMI Ticket System
| Kamen Mladenov | FMI Computer Science Year 1 Group 6 | Project 1 | Project Topic 4 |
List.hpp
Go to the documentation of this file.
1#ifndef HEADER_LIST
2#define HEADER_LIST
3
8#include <istream>
9#include <ostream>
10
21template <typename T>
22class List {
23protected:
24 T* elements;
25 unsigned length;
26 unsigned count;
27
28 void resize();
29 void free();
30 void copyFrom(const List& other);
31
32public:
34 List(const T* elements, unsigned elementsCount);
36 void add(const T& element);
38 void insertAt(const T& element, unsigned index);
40 bool removeAt(unsigned index);
42 unsigned findIndex(const T& element) const;
43 bool contain(const T& element) const;
45 T& operator[](unsigned index);
47 const T& operator[](unsigned index) const;
48
50 List<T>& operator+=(const List<T> other);
51
53 std::istream& read(std::istream& istr);
55 std::ostream& write(std::ostream& ostr) const;
56
58 unsigned get_length() const;
60 unsigned get_count() const;
61
62 List();
63 List& operator=(const List& other);
64 List(const List& other);
65 ~List();
66
67 List(List&& other);
68 List& operator=(List&& other);
69};
70
72template <typename T>
73std::istream& operator>>(std::istream& istr, List<T>& obj);
75template <typename T>
76std::ostream& operator<<(std::ostream& ostr, const List<T>& obj);
77
78/* Private */
79
80template <typename T>
81void List<T>::resize() {
82 length = (length == 0) ? 8 : length << 1;
83 T* temp = new T[length];
84 for (int i = 0; i < count; i++)
85 temp[i] = elements[i];
86 delete[] elements;
87 elements = temp;
88}
89
90template <typename T>
91void List<T>::free() {
92 delete[] elements;
93}
94
95template <typename T>
96void List<T>::copyFrom(const List& other) {
97 elements = new T[other.length];
98 for (int i = 0; i < other.count; i++)
99 elements[i] = other.elements[i];
100 length = other.length;
101 count = other.count;
102}
103
104/* Public */
105
106template <typename T>
107List<T>::List(const T* elements, unsigned elementsCount) {
108 length = 8;
109 count = elementsCount;
110 this->elements = new T[length];
111 for (unsigned i = 0; i < count; i++)
112 this->elements[i] = elements[i];
113}
114
118template <typename T>
119void List<T>::add(const T& element) {
120 if (length == count) resize();
121
122 elements[count++] = element;
123}
124
131template <typename T>
132void List<T>::insertAt(const T& element, unsigned index) {
133 if (index >= count) {
134 add(element);
135 return;
136 }
137 if (length == count) resize();
138
139 for (unsigned i = count; i > index; i--)
140 elements[i] = elements[i - 1];
141
142 elements[index] = element;
143 count++;
144}
145
151template <typename T>
152bool List<T>::removeAt(unsigned index) {
153 if (index >= count) return false;
154
155 for (int i = index; i < count; i++)
156 elements[i] = elements[i + 1];
157 count--;
158 return true;
159}
160
166template <typename T>
167unsigned List<T>::findIndex(const T& element) const {
168 unsigned ind = 0;
169 while (ind < count && elements[ind].compare(element) != 0)
170 ind++;
171 return ind;
172}
173
179template <typename T>
180bool List<T>::contain(const T& element) const {
181 return findIndex(element) < count;
182}
183
184template <typename T>
185T& List<T>::operator[](unsigned index) {
186 return elements[index];
187}
188
189template <typename T>
190const T& List<T>::operator[](unsigned index) const {
191 return elements[index];
192}
193
194template <typename T>
196 for (unsigned i = 0; i < other.length; i++)
197 add(other[i]);
198 return *this;
199}
200
210template <typename T>
211std::istream& List<T>::read(std::istream& istr) {
212 istr.read((char*)&length, sizeof(length));
213 istr.read((char*)&count, sizeof(count));
214
215 delete[] elements;
216 elements = new T[length];
217
218 for (int i = 0; i < count; i++)
219 elements[i].read(istr);
220
221 return istr;
222}
223
232template <typename T>
233std::ostream& List<T>::write(std::ostream& ostr) const {
234 ostr.write((const char*)&length, sizeof(length));
235 ostr.write((const char*)&count, sizeof(count));
236
237 for (int i = 0; i < count; i++)
238 elements[i].write(ostr);
239
240 return ostr;
241}
242
246template <typename T>
247unsigned List<T>::get_length() const {
248 return length;
249}
250
254template <typename T>
255unsigned List<T>::get_count() const {
256 return count;
257}
258
259// Rule of 4
260
261template <typename T>
262List<T>::List() : List(nullptr, 0) {}
263
264template <typename T>
265List<T>& List<T>::operator=(const List& other) {
266 if (this != &other) {
267 free();
268 copyFrom(other);
269 }
270 return *this;
271}
272
273template <typename T>
274List<T>::List(const List& other) {
275 copyFrom(other);
276}
277
278template <typename T>
280 free();
281}
282
283// Move semantics
284
285template <typename T>
286List<T>::List(List&& other) {
287 length = other.length;
288 count = other.count;
289 elements = other.elements;
290 other.elements = nullptr;
291}
292
293template <typename T>
295 if (this != &other) {
296 free();
297 length = other.length;
298 count = other.count;
299 elements = other.elements;
300 other.elements = nullptr;
301 }
302 return *this;
303}
304
305/* Outside of class */
306
314template <typename T>
315std::istream& operator>>(std::istream& istr, List<T>& obj) {
316 List<T> newObj;
317 unsigned count;
318 istr >> count;
319
320 T temp;
321 for (int i = 0; i < count; i++) {
322 istr >> temp;
323 obj.add(temp);
324 }
325
326 return istr;
327}
328
335template <typename T>
336std::ostream& operator<<(std::ostream& ostr, const List<T>& obj) {
337 ostr << obj.get_count() << std::endl;
338 for (int i = 0; i < obj.get_count(); i++)
339 ostr << obj[i];
340
341 return ostr;
342}
343
344
345#endif
std::istream & operator>>(std::istream &istr, List< T > &obj)
Reads List from stream with >> operator.
Definition: List.hpp:315
std::ostream & operator<<(std::ostream &ostr, const List< T > &obj)
Writes List to stream with << operator.
Definition: List.hpp:336
void read(T *storage)
Reads user input and stores it.
Definition: Toolbox.hpp:58
Templated class that stores an array of elements in dynamic memory.
Definition: List.hpp:22
bool contain(const T &element) const
Definition: List.hpp:180
T & operator[](unsigned index)
Returns reference to element at index.
Definition: List.hpp:185
unsigned findIndex(const T &element) const
Finds the index of element.
Definition: List.hpp:167
unsigned get_count() const
Returns the count.
Definition: List.hpp:255
std::istream & read(std::istream &istr)
Reads from stream.
Definition: List.hpp:211
const T & operator[](unsigned index) const
Returns constant reference to element at index.
Definition: List.hpp:190
std::ostream & write(std::ostream &ostr) const
Writes to stream.
Definition: List.hpp:233
List(const T *elements, unsigned elementsCount)
Copies all elements in given array.
Definition: List.hpp:107
unsigned get_length() const
Returns the length.
Definition: List.hpp:247
List< T > & operator+=(const List< T > other)
Appends elements from other list.
Definition: List.hpp:195
void add(const T &element)
Adds an element.
Definition: List.hpp:119
bool removeAt(unsigned index)
Removes element at index.
Definition: List.hpp:152
void insertAt(const T &element, unsigned index)
Inserts element at given index.
Definition: List.hpp:132