2016-04-12 21:30:44 +02:00
|
|
|
/*
|
2019-12-09 09:35:50 +01:00
|
|
|
* Copyright 2010-2019 Max Kellermann <max.kellermann@gmail.com>
|
2016-04-12 21:30:44 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ALLOCATED_ARRAY_HXX
|
|
|
|
#define ALLOCATED_ARRAY_HXX
|
|
|
|
|
2019-12-09 09:42:07 +01:00
|
|
|
#include "ConstBuffer.hxx"
|
2016-04-12 21:30:44 +02:00
|
|
|
#include "WritableBuffer.hxx"
|
|
|
|
#include "Compiler.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An array allocated on the heap with a length determined at runtime.
|
|
|
|
*/
|
|
|
|
template<class T>
|
|
|
|
class AllocatedArray {
|
|
|
|
typedef WritableBuffer<T> Buffer;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef typename Buffer::size_type size_type;
|
|
|
|
typedef typename Buffer::reference_type reference_type;
|
|
|
|
typedef typename Buffer::const_reference_type const_reference_type;
|
|
|
|
typedef typename Buffer::iterator iterator;
|
|
|
|
typedef typename Buffer::const_iterator const_iterator;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Buffer buffer{nullptr};
|
|
|
|
|
|
|
|
public:
|
|
|
|
constexpr AllocatedArray() = default;
|
|
|
|
|
2018-12-28 17:17:28 +01:00
|
|
|
explicit AllocatedArray(size_type _size) noexcept
|
2019-12-09 09:40:57 +01:00
|
|
|
:buffer{new T[_size], _size} {}
|
2016-04-12 21:30:44 +02:00
|
|
|
|
2019-12-09 09:39:36 +01:00
|
|
|
explicit AllocatedArray(const AllocatedArray &other) noexcept {
|
2016-04-12 21:30:44 +02:00
|
|
|
assert(other.size() == 0 || other.buffer.data != nullptr);
|
|
|
|
|
2019-12-09 09:39:36 +01:00
|
|
|
if (other == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
buffer = {new T[other.buffer.size], other.buffer.size};
|
2016-04-12 21:30:44 +02:00
|
|
|
std::copy_n(other.buffer.data, buffer.size, buffer.data);
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:17:28 +01:00
|
|
|
AllocatedArray(AllocatedArray &&other) noexcept
|
2019-12-09 09:35:50 +01:00
|
|
|
:buffer(std::exchange(other.buffer, nullptr)) {}
|
2016-04-12 21:30:44 +02:00
|
|
|
|
2018-12-28 17:17:28 +01:00
|
|
|
~AllocatedArray() noexcept {
|
2016-04-12 21:30:44 +02:00
|
|
|
delete[] buffer.data;
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:17:28 +01:00
|
|
|
AllocatedArray &operator=(const AllocatedArray &other) noexcept {
|
2016-04-12 21:30:44 +02:00
|
|
|
assert(size() == 0 || buffer.data != nullptr);
|
|
|
|
assert(other.size() == 0 || other.buffer.data != nullptr);
|
|
|
|
|
|
|
|
if (&other == this)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
ResizeDiscard(other.size());
|
|
|
|
std::copy_n(other.buffer.data, other.buffer.size, buffer.data);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:17:28 +01:00
|
|
|
AllocatedArray &operator=(AllocatedArray &&other) noexcept {
|
2018-12-28 17:18:41 +01:00
|
|
|
using std::swap;
|
|
|
|
swap(buffer, other.buffer);
|
2016-04-12 21:30:44 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:17:28 +01:00
|
|
|
constexpr bool IsNull() const noexcept {
|
2016-04-12 21:30:44 +02:00
|
|
|
return buffer.IsNull();
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:17:28 +01:00
|
|
|
constexpr bool operator==(std::nullptr_t) const noexcept {
|
2017-09-21 20:34:36 +02:00
|
|
|
return buffer == nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:17:28 +01:00
|
|
|
constexpr bool operator!=(std::nullptr_t) const noexcept {
|
2017-09-21 20:34:36 +02:00
|
|
|
return buffer != nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-12 21:30:44 +02:00
|
|
|
/**
|
|
|
|
* Returns true if no memory was allocated so far.
|
|
|
|
*/
|
2018-12-28 17:17:28 +01:00
|
|
|
constexpr bool empty() const noexcept {
|
2017-11-10 19:24:33 +01:00
|
|
|
return buffer.empty();
|
2016-04-12 21:30:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of allocated elements.
|
|
|
|
*/
|
2018-12-28 17:17:28 +01:00
|
|
|
constexpr size_type size() const noexcept {
|
2016-04-12 21:30:44 +02:00
|
|
|
return buffer.size;
|
|
|
|
}
|
|
|
|
|
2019-12-09 09:40:18 +01:00
|
|
|
/**
|
|
|
|
* Returns the number of allocated elements.
|
|
|
|
*/
|
|
|
|
constexpr size_type capacity() const noexcept {
|
|
|
|
return buffer.size;
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:17:28 +01:00
|
|
|
reference_type front() noexcept {
|
2016-04-12 21:30:44 +02:00
|
|
|
return buffer.front();
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:17:28 +01:00
|
|
|
const_reference_type front() const noexcept {
|
2016-04-12 21:30:44 +02:00
|
|
|
return buffer.front();
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:17:28 +01:00
|
|
|
reference_type back() noexcept {
|
2016-04-12 21:30:44 +02:00
|
|
|
return buffer.back();
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:17:28 +01:00
|
|
|
const_reference_type back() const noexcept {
|
2016-04-12 21:30:44 +02:00
|
|
|
return buffer.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns one element. No bounds checking.
|
|
|
|
*/
|
2018-12-28 17:17:28 +01:00
|
|
|
reference_type operator[](size_type i) noexcept {
|
2019-12-09 09:42:07 +01:00
|
|
|
return buffer[i];
|
2016-04-12 21:30:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns one constant element. No bounds checking.
|
|
|
|
*/
|
2018-12-28 17:17:28 +01:00
|
|
|
const_reference_type operator[](size_type i) const noexcept {
|
2019-12-09 09:42:07 +01:00
|
|
|
return buffer[i];
|
2016-04-12 21:30:44 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 17:17:28 +01:00
|
|
|
iterator begin() noexcept {
|
2016-04-12 21:30:44 +02:00
|
|
|
return buffer.begin();
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:17:28 +01:00
|
|
|
constexpr const_iterator begin() const noexcept {
|
2016-04-12 21:30:44 +02:00
|
|
|
return buffer.cbegin();
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:17:28 +01:00
|
|
|
iterator end() noexcept {
|
2016-04-12 21:30:44 +02:00
|
|
|
return buffer.end();
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:17:28 +01:00
|
|
|
constexpr const_iterator end() const noexcept {
|
2016-04-12 21:30:44 +02:00
|
|
|
return buffer.cend();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resizes the array, discarding old data.
|
|
|
|
*/
|
2018-12-28 17:17:28 +01:00
|
|
|
void ResizeDiscard(size_type _size) noexcept {
|
2016-04-12 21:30:44 +02:00
|
|
|
if (_size == buffer.size)
|
|
|
|
return;
|
|
|
|
|
|
|
|
delete[] buffer.data;
|
|
|
|
buffer.size = _size;
|
|
|
|
buffer.data = new T[buffer.size];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grows the array to the specified size, discarding old data.
|
|
|
|
* Similar to ResizeDiscard(), but will never shrink the array to
|
|
|
|
* avoid expensive heap operations.
|
|
|
|
*/
|
2018-12-28 17:17:28 +01:00
|
|
|
void GrowDiscard(size_type _size) noexcept {
|
2016-04-12 21:30:44 +02:00
|
|
|
if (_size > buffer.size)
|
|
|
|
ResizeDiscard(_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grows the array to the specified size, preserving the value of a
|
|
|
|
* range of elements, starting from the beginning.
|
|
|
|
*/
|
2018-12-28 17:17:28 +01:00
|
|
|
void GrowPreserve(size_type _size, size_type preserve) noexcept {
|
2016-04-12 21:30:44 +02:00
|
|
|
if (_size <= buffer.size)
|
|
|
|
return;
|
|
|
|
|
|
|
|
T *new_data = new T[_size];
|
|
|
|
|
|
|
|
std::move(buffer.data, buffer.data + preserve, new_data);
|
|
|
|
|
|
|
|
delete[] buffer.data;
|
|
|
|
buffer.data = new_data;
|
|
|
|
buffer.size = _size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Declare that the buffer has the specified size. Must not be
|
|
|
|
* larger than the current size. Excess elements are not used (but
|
|
|
|
* they are still allocated).
|
|
|
|
*/
|
2018-12-28 17:17:28 +01:00
|
|
|
void SetSize(size_type _size) noexcept {
|
2016-04-12 21:30:44 +02:00
|
|
|
assert(_size <= buffer.size);
|
|
|
|
|
|
|
|
buffer.size = _size;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|