10#include <initializer_list>
25template <
typename T, std::
size_t MaxSize = 8,
typename NonReboundT = T>
26struct SmallBufferAllocator
28 alignas(
alignof(T))
char m_small_buffer[MaxSize *
sizeof(T)];
29 std::allocator<T> m_alloc{};
30 bool m_small_buffer_used =
false;
35 using propagate_on_container_move_assignment = std::false_type;
36 using propagate_on_container_swap = std::false_type;
37 using is_always_equal = std::false_type;
42 typedef SmallBufferAllocator<U, MaxSize, NonReboundT> other;
45 constexpr SmallBufferAllocator() noexcept = default;
47 constexpr SmallBufferAllocator(const SmallBufferAllocator<U, MaxSize, NonReboundT>&) noexcept
51 constexpr SmallBufferAllocator(
const SmallBufferAllocator& other) noexcept
52 : m_small_buffer_used(other.m_small_buffer_used)
54 constexpr SmallBufferAllocator& operator=(
const SmallBufferAllocator& other)
noexcept
56 m_small_buffer_used = other.m_small_buffer_used;
59 constexpr SmallBufferAllocator(SmallBufferAllocator&&) noexcept {}
60 constexpr SmallBufferAllocator& operator=(
const SmallBufferAllocator&&) noexcept
65 constexpr T* allocate(
const std::size_t n)
68 if constexpr (std::is_same<T, NonReboundT>::value) {
70 m_small_buffer_used =
true;
72 return reinterpret_cast<T*
>(&m_small_buffer);
75 m_small_buffer_used =
false;
77 return m_alloc.allocate(n);
79 constexpr void deallocate(
void* p,
const std::size_t n)
82 if (&m_small_buffer != p) m_alloc.deallocate(
static_cast<T*
>(p), n);
83 m_small_buffer_used =
false;
90 const SmallBufferAllocator& lhs,
91 const SmallBufferAllocator& rhs)
93 return !lhs.m_small_buffer_used && !rhs.m_small_buffer_used;
96 const SmallBufferAllocator& lhs,
97 const SmallBufferAllocator& rhs)
122template <
typename T, std::
size_t N = 8>
123class SmallVector :
public std::vector<T, SmallBufferAllocator<T, N>>
126 using vectorT = std::vector<T, SmallBufferAllocator<T, N>>;
130 constexpr SmallVector()
noexcept { vectorT::reserve(N); }
135 if (other.size() <= N) vectorT::reserve(N);
136 vectorT::operator=(std::move(other));
139 std::is_nothrow_move_constructible<T>::value)
141 if (other.size() <= N) vectorT::reserve(N);
142 vectorT::operator=(std::move(other));
149 vectorT::resize(count);
154 vectorT::assign(count, value);
156 template <
class InputIt>
160 vectorT::insert(vectorT::begin(), first, last);
165 vectorT::insert(vectorT::begin(), init);
170 swap(
static_cast<vectorT&
>(a),
static_cast<vectorT&
>(b));
Hybrid vector that uses the stack upto a maximum size, and the heap beyond that.
Definition: SmallVector.h:124
constexpr void swap(function_ref< R(Args...)> &lhs, function_ref< R(Args...)> &rhs) noexcept
Swaps the referred callables of lhs and rhs.
Definition: function_ref.h:114
bool operator==(const shared_ptr< T > &sp1, const shared_ptr< U > &sp2)
Operator == overloading.
Definition: shared_ptr.h:344
bool operator!=(const shared_ptr< T > &sp1, const shared_ptr< U > &sp2)
Operator != overloading.
Definition: shared_ptr.h:363
Main namespace for Lagrange.
Definition: AABBIGL.h:30