#ifndef ARRAY_H #define ARRAY_H #include namespace ecci { const size_t initialCapacity = 10; const size_t increaseFactor = 10; template class Array { private: size_t count; size_t capacity; DataType* elements; public: explicit Array(size_t capacity = initialCapacity); Array(const Array& other); Array(Array&& other); ~Array(); Array& operator=(const Array& other); Array& operator=(Array&& other); inline size_t getCount() const { return count; } inline DataType& operator[](size_t index) { return this->elements[index]; } inline const DataType& operator[](size_t index) const { return this->elements[index]; } void append(const DataType& value); void sort(); private: void increaseCapacity(); }; } // namespace ecci #include namespace ecci { template Array::Array( size_t capacity ) : count{ 0 } , capacity{ capacity ? capacity : initialCapacity } , elements{ new DataType[this->capacity]() } { } template Array::Array(const Array& other) : count{ other.count } , capacity{ other.capacity } , elements{ new DataType[this->capacity]() } { for ( size_t index = 0; index < this->count; ++index ) this->elements[index] = other.elements[index]; } template Array::Array(Array&& other) : count{ other.count } , capacity{ other.capacity } , elements{ other.elements } { other.count = other.capacity = 0; other.elements = nullptr; } template Array::~Array() { delete [] this->elements; } template Array& Array::operator=(const Array& other) { if ( this != &other ) { if ( this->capacity != other.capacity ) { delete [] this->elements; this->capacity = other.capacity; this->elements = new DataType[ this->capacity ](); } this->count = other.count; for ( size_t index = 0; index < this->count; ++index ) this->elements[index] = other.elements[index]; } return *this; } template Array& Array::operator=(Array&& other) { if ( this != &other ) { delete [] this->elements; this->count = other.count; this->capacity = other.capacity; this->elements = other.elements; other.count = other.capacity = 0; other.elements = nullptr; } return *this; } template void Array::append(const DataType& value) { if ( this->count == this->capacity ) increaseCapacity(); this->elements[this->count++] = value; } template void Array::increaseCapacity() { this->capacity *= increaseFactor; DataType* newElements = new DataType[this->capacity]; // if ( newElements == nullptr ) // throw Exception("could not increase array"); for ( size_t index = 0; index < this->count; ++index ) newElements[index] = this->elements[index]; delete [] this->elements; this->elements = newElements; } template void Array::sort() { std::sort( this->elements, this->elements + this->count ); } } // namespace ecci #endif // ARRAY_H