30 #ifndef ALLOCATED_ARRAY_HXX
31 #define ALLOCATED_ARRAY_HXX
55 Buffer buffer{
nullptr};
61 :buffer{
new T[_size], _size} {
62 assert(
size() == 0 || buffer.data !=
nullptr);
66 :buffer{
new T[other.buffer.
size], other.buffer.
size} {
67 assert(
size() == 0 || buffer.data !=
nullptr);
68 assert(other.size() == 0 || other.buffer.data !=
nullptr);
70 std::copy_n(other.buffer.data, buffer.size, buffer.data);
74 :buffer(other.buffer) {
83 assert(
size() == 0 || buffer.data !=
nullptr);
84 assert(other.
size() == 0 || other.buffer.data !=
nullptr);
90 std::copy_n(other.buffer.data, other.buffer.
size, buffer.data);
95 std::swap(buffer, other.buffer);
100 return buffer.IsNull();
107 return buffer.IsEmpty();
113 constexpr size_type
size()
const {
118 return buffer.front();
121 const_reference_type
front()
const {
122 return buffer.front();
126 return buffer.back();
129 const_reference_type
back()
const {
130 return buffer.back();
139 return buffer.data[i];
148 return buffer.data[i];
152 return buffer.begin();
155 constexpr const_iterator
begin()
const {
156 return buffer.cbegin();
163 constexpr const_iterator
end()
const {
164 return buffer.cend();
171 if (_size == buffer.size)
174 delete[] buffer.data;
176 buffer.data =
new T[buffer.size];
178 assert(
size() == 0 || buffer.data !=
nullptr);
187 if (_size > buffer.size)
196 if (_size <= buffer.size)
199 T *new_data =
new T[_size];
200 assert(_size == 0 || new_data !=
nullptr);
202 std::move(buffer.data, buffer.data + preserve, new_data);
204 delete[] buffer.data;
205 buffer.data = new_data;
215 assert(_size <= buffer.size);
reference_type operator[](size_type i)
Returns one element.
void GrowDiscard(size_type _size)
Grows the array to the specified size, discarding old data.
Buffer::reference_type reference_type
void ResizeDiscard(size_type _size)
Resizes the array, discarding old data.
void GrowPreserve(size_type _size, size_type preserve)
Grows the array to the specified size, preserving the value of a range of elements, starting from the beginning.
const_reference_type front() const
Buffer::size_type size_type
A reference to a memory area that is writable.
AllocatedArray & operator=(const AllocatedArray &other)
void SetSize(size_type _size)
Declare that the buffer has the specified size.
AllocatedArray(size_type _size)
Buffer::const_iterator const_iterator
constexpr const_iterator begin() const
constexpr const_iterator end() const
constexpr AllocatedArray()=default
const T & const_reference_type
Buffer::const_reference_type const_reference_type
AllocatedArray(AllocatedArray &&other)
Buffer::iterator iterator
constexpr bool empty() const
Returns true if no memory was allocated so far.
const_pointer_type const_iterator
static constexpr WritableBuffer Null()
constexpr bool IsNull() const
AllocatedArray & operator=(AllocatedArray &&other)
const_reference_type back() const
AllocatedArray(const AllocatedArray &other)
constexpr size_type size() const
Returns the number of allocated elements.
An array allocated on the heap with a length determined at runtime.
const_reference_type operator[](size_type i) const
Returns one constant element.