13 #pragma GCC diagnostic ignored "-Wstringop-overflow"
18 template <
typename T,
typename TAllocator = Mallocator,
typename TCounter = Size>
22 template <
typename TValue>
27 static constexpr uint64_t
max() {
34 static constexpr uint32_t
max() {
42 template <
typename T,
typename TAllocator = Mallocator,
typename TCounter = Size>
46 StorageType* data =
nullptr;
62 explicit Array(
const TCounter elementCnt,
const TCounter allocatedSize = maxValue) {
63 this->alloc(elementCnt, allocatedSize);
66 if (!std::is_trivially_default_constructible<T>::value) {
67 for (TCounter i = 0; i < actSize; ++i) {
68 new (data + i) StorageType();
77 Array(std::initializer_list<StorageType> list) {
78 actSize = list.size();
80 MemoryBlock block = TAllocator::allocate(maxSize *
sizeof(StorageType),
alignof(StorageType));
82 data = (StorageType*)block.
ptr;
84 for (
auto& l : list) {
85 new (data + i) StorageType(l);
99 if (!std::is_trivially_destructible<T>::value) {
100 for (TCounter i = 0; i < actSize; ++i) {
101 data[i].~StorageType();
105 MemoryBlock block(data, maxSize *
sizeof(StorageType));
106 TAllocator::deallocate(block);
124 const Array& rhs = other;
126 for (TCounter i = 0; i < rhs.
size(); ++i) {
135 template <typename U, typename = std::enable_if_t<std::is_lvalue_reference<T>::value, U>>
138 for (TCounter i = 0; i < other.size(); ++i) {
139 (*this)[i] = std::forward<U>(other[i]);
150 for (
const T& value : *
this) {
157 SPH_ASSERT(
unsigned(idx) <
unsigned(actSize), idx, actSize);
162 SPH_ASSERT(
unsigned(idx) <
unsigned(actSize), idx, actSize);
178 return data[actSize - 1];
183 return data[actSize - 1];
188 for (
auto& v : *
this) {
218 if (newSize <= maxSize) {
220 if (newSize >= actSize) {
222 if (!std::is_trivially_default_constructible<T>::value) {
223 for (TCounter i = actSize; i < newSize; ++i) {
224 new (data + i) StorageType();
228 if (!std::is_trivially_destructible<T>::value) {
230 for (TCounter i = newSize; i < actSize; ++i) {
231 data[i].~StorageType();
239 const TCounter actNewSize =
max(2 * maxSize, newSize);
240 Array newArray(0, actNewSize);
242 for (TCounter i = 0; i < actSize; ++i) {
243 new (newArray.data + i) StorageType(std::move(data[i]));
246 if (!std::is_trivially_default_constructible<T>::value) {
247 for (TCounter i = actSize; i < newSize; ++i) {
248 new (newArray.data + i) StorageType();
252 *
this = std::move(newArray);
266 const TCounter currentSize = actSize;
268 for (TCounter i = currentSize; i < actSize; ++i) {
281 if (newMaxSize > maxSize) {
282 const TCounter actNewSize =
max(2 * maxSize, newMaxSize);
286 newArray.alloc(0, actNewSize);
288 for (TCounter i = 0; i < actSize; ++i) {
289 new (newArray.data + i) StorageType(std::move(data[i]));
291 newArray.actSize = actSize;
293 *
this = std::move(newArray);
300 newArray.
pushAll(std::move(*
this));
301 *
this = std::move(newArray);
305 template <
typename U>
308 data[actSize - 1] = std::forward<U>(u);
311 template <
typename TIter>
312 void pushAll(
const TIter first,
const TIter last) {
313 for (TIter iter = first; iter != last; ++iter) {
324 reserve(actSize + other.size());
325 for (T& value : other) {
326 push(std::move(value));
331 template <
typename... TArgs>
335 StorageType* ptr =
new (data + actSize) StorageType(std::forward<TArgs>(args)...);
344 template <
typename U>
347 this->
resize(actSize + 1);
348 std::move_backward(this->
begin() + position, this->
end() - 1, this->
end());
349 data[
position] = std::forward<U>(value);
356 template <
typename TIterator>
357 void insert(
const TCounter
position,
const TIterator first,
const TIterator last) {
364 this->
resize(actSize + count);
365 std::move_backward(this->
begin() + position, this->
end() - count, this->
end());
367 for (TIterator iter = first; iter != last; ++iter) {
377 T value = data[actSize - 1];
385 for (TCounter i = idx; i < actSize - 1; ++i) {
386 data[i] = std::move(data[i + 1]);
402 for (
Size k = 0; k < idxs.
size() - 1; ++k) {
404 for (TCounter i = idxs[k]; i < idxs[k + 1] - 1; ++i) {
405 data[i - shift] = std::move(data[i + 1]);
410 for (TCounter i = idxs.
back(); i < actSize - 1; ++i) {
411 data[i - shift] = std::move(data[i + 1]);
418 template <
typename TIter>
424 const Size count = last - first;
427 for (TIter iter = first; iter !=
end() - count; ++iter) {
428 *iter = std::move(*(iter + count));
435 if (!std::is_trivially_destructible<T>::value) {
436 for (TCounter i = 0; i < actSize; ++i) {
437 data[i].~StorageType();
509 return view() == other.view();
514 return view() != other.view();
520 template <typename TStream, typename = std::enable_if_t<HasStreamOperator<T, TStream>::value>>
522 for (
const T& t : array) {
523 stream << t << std::endl;
529 void alloc(
const TCounter elementCnt,
const TCounter allocatedSize) {
530 actSize = elementCnt;
531 maxSize = allocatedSize;
532 if (allocatedSize == maxValue) {
536 MemoryBlock block = TAllocator::allocate(maxSize *
sizeof(StorageType),
alignof(StorageType));
538 data = (StorageType*)block.
ptr;
542 template <
typename T,
typename TAllocator,
typename TCounter>
557 template <
typename T,
typename TAllocator,
typename TCounter>
563 template <
typename T0,
typename... TArgs>
569 template <
typename T0,
typename... TArgs>
579 template <
typename T,
typename TCounter>
580 void swap(Sph::Array<T, TCounter>& ar1, Sph::Array<T, TCounter>& ar2) {
Allocators used by containers.
Simple non-owning view of a container.
Array< std::decay_t< T0 > > makeArray(T0 &&t0, TArgs &&... rest)
Creates an array from a list of parameters.
Array< T0 & > tieToArray(T0 &t0, TArgs &... rest)
Creates a l-value reference array from a list of l-value references.
INLINE CopyableArray< T, TAllocator, TCounter > copyable(const Array< T, TAllocator, TCounter > &array)
#define SPH_ASSERT(x,...)
uint32_t Size
Integral type used to index arrays (by default).
constexpr INLINE T max(const T &f1, const T &f2)
#define INLINE
Macros for conditional compilation based on selected compiler.
#define NAMESPACE_SPH_END
INLINE Float distance(const Vector &r, const Vector &axis)
Returns the distance of vector from given axis. The axis is assumed to be normalized.
Object providing safe access to continuous memory of data.
INLINE bool empty() const
INLINE TCounter size() const
Generic dynamically allocated resizable storage.
bool operator==(const Array &other) const noexcept
Comparison operator, comparings array element-by-element.
INLINE Iterator< const StorageType > cbegin() const noexcept
INLINE const T & front() const noexcept
INLINE const T & operator[](const TCounter idx) const noexcept
void resize(const TCounter newSize)
Resizes the array to new size.
void pushAll(Array &&other)
void reserve(const TCounter newMaxSize)
Allocates enough memory to store the given number of elements.
INLINE Iterator< StorageType > end() noexcept
void remove(const ArrayView< const TCounter > idxs)
Removes elements specified by indices from the array.
TAllocator & allocator()
Returns the interface to the allocator.
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
StorageType & emplaceBack(TArgs &&... args)
Constructs a new element at the end of the array in place, using the provided arguments.
Array(const TCounter elementCnt, const TCounter allocatedSize=maxValue)
Constructs array of given size.
void remove(const TCounter idx)
Removes an element with given index from the array.
Array & operator=(Array &&other)
Array(std::initializer_list< StorageType > list)
Constructs array from initialized list.
ArrayView< T, TCounter > view() noexcept
Explicit conversion to arrayview.
void fill(const T &t)
Sets all elements of the array to given value.
Array & operator=(Array< U > &&other)
For l-value references assign each value (does not actually move anything).
void insert(const TCounter position, U &&value)
Inserts a new element to given position in the array.
INLINE T & back() noexcept
Array & operator=(const CopyableArray< T, TAllocator, TCounter > &other)
Performs deep-copy of array elements, resizing array if needed.
void remove(TIter first, TIter last)
Removes all elements in given range.
INLINE T pop()
Removes the last element from the array and return its value.
ArrayView< const T, TCounter > view() const noexcept
Explicit conversion to arrayview, const version.
INLINE TCounter capacity() const noexcept
Array(Array &&other)
Move constructor from array of the same type.
void clear()
Removes all elements from the array, but does NOT release the memory.
INLINE const T & back() const noexcept
INLINE TCounter size() const noexcept
void insert(const TCounter position, const TIterator first, const TIterator last)
Inserts a range of values into the array, starting at given position.
INLINE T & front() noexcept
INLINE Iterator< const StorageType > begin() const noexcept
void swap(Array &other)
Swaps content of two arrays.
void shrink()
Reallocates the array, removing the unused elements to save memory.
INLINE T & operator[](const TCounter idx) noexcept
INLINE Iterator< const StorageType > end() const noexcept
const TAllocator & allocator() const
Returns the interface to the allocator.
INLINE bool empty() const noexcept
INLINE Iterator< const StorageType > cend() const noexcept
void resizeAndSet(const TCounter newSize, const T &value)
Resizes the array to new size and assigns a given value to all newly created elements.
INLINE Iterator< StorageType > begin() noexcept
void pushAll(const TIter first, const TIter last)
void pushAll(const Array &other)
friend TStream & operator<<(TStream &stream, const Array &array)
Prints content of array to stream.
bool operator!=(const Array &other) const noexcept
Inequality operator.
Array clone() const
Performs a deep copy of all elements of the array.
CopyableArray(const Array< T, TAllocator, TCounter > &array)
Simple (forward) iterator over continuous array of objects of type T.
Vector position(const Float a, const Float e, const Float u)
Computes the position on the elliptic trajectory.
Overload of std::swap for Sph::Array.
void swap(Sph::Array< T, TCounter > &ar1, Sph::Array< T, TCounter > &ar2)
Object with deleted copy constructor and copy operator.
static constexpr uint32_t max()
static constexpr uint64_t max()
Helper class, used to avoid including large header limits.h.