flatten 20260225
This commit is contained in:
137
tjp/core/containers/InPlaceArray.hpp
Executable file
137
tjp/core/containers/InPlaceArray.hpp
Executable file
@@ -0,0 +1,137 @@
|
||||
// License: Modified MIT (NON-AI)
|
||||
// See the LICENSE file in the root directory for license information.
|
||||
// Copyright 2025 Timothy Prepscius
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "InPlaceArray.h"
|
||||
#include "Array.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <array>
|
||||
|
||||
#include "../types/Types.h"
|
||||
|
||||
namespace tjp {
|
||||
namespace core {
|
||||
|
||||
template<typename T, size_t N>
|
||||
class InPlaceArray
|
||||
{
|
||||
public:
|
||||
static constexpr size_t MaxLength = N;
|
||||
|
||||
private:
|
||||
using Array = std::array<T, N>;
|
||||
using Block = char;
|
||||
using Size = u32;
|
||||
|
||||
union Static {
|
||||
Block block;
|
||||
Array array;
|
||||
|
||||
Static() : block(0) {}
|
||||
~Static () {}
|
||||
} ;
|
||||
|
||||
Static v;
|
||||
Size length = 0;
|
||||
|
||||
typedef std::allocator<T> Allocator;
|
||||
Allocator allocator;
|
||||
|
||||
public:
|
||||
using value_type = typename Array::value_type;
|
||||
using iterator = typename Array::iterator;
|
||||
using const_iterator = typename Array::const_iterator;
|
||||
using pointer = typename Array::pointer;
|
||||
using reference = typename Array::reference;
|
||||
using const_reference = typename Array::const_reference;
|
||||
|
||||
inline auto data() { return v.array.data(); }
|
||||
inline auto data() const { return v.array.data(); }
|
||||
|
||||
inline auto begin() { return v.array.begin(); }
|
||||
inline auto end () { return v.array.begin() + length; }
|
||||
|
||||
inline auto begin() const { return v.array.begin(); }
|
||||
inline auto end () const { return v.array.begin() + length; }
|
||||
|
||||
inline auto rbegin() { return v.array.rbegin() + (v.array.size() - length); }
|
||||
inline auto rend () { return v.array.rend(); }
|
||||
|
||||
inline auto rbegin() const { return v.array.rbegin() + (v.array.size() - length); }
|
||||
inline auto rend () const { return v.array.rend(); }
|
||||
|
||||
inline const_reference front () const { return v.array.front(); }
|
||||
inline const_reference back () const { return *(v.array.data() + (length - 1)); }
|
||||
inline reference front () { return v.array.front(); }
|
||||
inline reference back () { return *(v.array.data() + (length - 1)); }
|
||||
|
||||
private:
|
||||
template<typename ... Args>
|
||||
void construct(T *t, Args&& ...args)
|
||||
{
|
||||
std::allocator_traits<Allocator>::construct(allocator, t, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
void destruct(T *t)
|
||||
{
|
||||
std::allocator_traits<Allocator>::destroy(allocator, t);
|
||||
}
|
||||
|
||||
protected:
|
||||
void shorten_block(size_t length_);
|
||||
void shorten_items(size_t length_);
|
||||
void shorten(size_t length_);
|
||||
void append_block(const T *begin, const T *end);
|
||||
void append_items(const T *begin, const T *end);
|
||||
void emplace_items(T *begin, T *end);
|
||||
|
||||
public:
|
||||
InPlaceArray ();
|
||||
InPlaceArray(size_t length_, T t=T());
|
||||
|
||||
// InPlaceArray(InPlaceArray &&rhs);
|
||||
|
||||
// template<size_t N2>
|
||||
InPlaceArray(const InPlaceArray<T, N> &rhs);
|
||||
InPlaceArray(const std::initializer_list<T> &rhs);
|
||||
InPlaceArray(const T *begin, const T *end);
|
||||
InPlaceArray(const T *begin, size_t size);
|
||||
|
||||
// template<size_t N2>
|
||||
InPlaceArray &operator =(const InPlaceArray<T, N> &rhs);
|
||||
InPlaceArray &operator =(const Array &rhs);
|
||||
~InPlaceArray ();
|
||||
|
||||
void resize(size_t length_, T t=T());
|
||||
void append(const T *begin, const T *end);
|
||||
void emplace(T *begin, T *end);
|
||||
|
||||
void push_back_();
|
||||
void push_back (const T &element);
|
||||
void push_back (T &&element);
|
||||
void pop_back ();
|
||||
|
||||
template<typename ... Args>
|
||||
T &emplace_back (Args&& ...args);
|
||||
|
||||
const T &at(size_t i) const;
|
||||
T &at(size_t i);
|
||||
|
||||
const T &operator[] (size_t i) const;
|
||||
T &operator[] (size_t i);
|
||||
|
||||
size_t size () const;
|
||||
void clear ();
|
||||
|
||||
bool empty () const;
|
||||
} ;
|
||||
|
||||
} // namespace utilities
|
||||
} // namespace
|
||||
|
||||
#include "InPlaceArray.inl"
|
||||
#include "InPlaceArray+iterator.inl"
|
||||
Reference in New Issue
Block a user