30 void copyFrom(
const List& other);
34 List(
const T* elements,
unsigned elementsCount);
36 void add(
const T& element);
38 void insertAt(
const T& element,
unsigned index);
53 std::istream&
read(std::istream& istr);
55 std::ostream&
write(std::ostream& ostr)
const;
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];
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;
109 count = elementsCount;
110 this->elements =
new T[length];
111 for (
unsigned i = 0; i < count; i++)
112 this->elements[i] = elements[i];
120 if (length == count) resize();
122 elements[count++] = element;
133 if (index >= count) {
137 if (length == count) resize();
139 for (
unsigned i = count; i > index; i--)
140 elements[i] = elements[i - 1];
142 elements[index] = element;
153 if (index >= count)
return false;
155 for (
int i = index; i < count; i++)
156 elements[i] = elements[i + 1];
169 while (ind < count && elements[ind].compare(element) != 0)
181 return findIndex(element) < count;
186 return elements[index];
191 return elements[index];
196 for (
unsigned i = 0; i < other.length; i++)
212 istr.read((
char*)&length,
sizeof(length));
213 istr.read((
char*)&count,
sizeof(count));
216 elements =
new T[length];
218 for (
int i = 0; i < count; i++)
219 elements[i].
read(istr);
234 ostr.write((
const char*)&length,
sizeof(length));
235 ostr.write((
const char*)&count,
sizeof(count));
237 for (
int i = 0; i < count; i++)
238 elements[i].write(ostr);
266 if (
this != &other) {
287 length = other.length;
289 elements = other.elements;
290 other.elements =
nullptr;
295 if (
this != &other) {
297 length = other.length;
299 elements = other.elements;
300 other.elements =
nullptr;
321 for (
int i = 0; i < count; i++) {
338 for (
int i = 0; i < obj.
get_count(); i++)
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
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