14#include <lagrange/utils/assert.h>
18#include <initializer_list>
34template <
typename T,
size_t N>
38 std::array<T, N> m_array;
44 StackSet(std::initializer_list<T> init)
48 auto it = init.begin();
49 for (
size_t i = 0; i < m_size; ++i) {
50 m_array[i] = std::move(*it);
57 using iterator =
typename std::array<T, N>::iterator;
58 using const_iterator =
typename std::array<T, N>::const_iterator;
59 iterator begin() {
return m_array.begin(); }
60 iterator end() {
return m_array.begin() + m_size; }
61 const_iterator begin()
const {
return m_array.begin(); }
62 const_iterator end()
const {
return m_array.begin() + m_size; }
65 size_t size()
const {
return m_size; }
67 void clear() { m_size = 0; }
69 void resize(
const size_t i)
75 std::pair<iterator, bool> insert(
const T& v)
78 for (
size_t i = 0; i < m_size; ++i) {
79 if (m_array[i] == v) {
80 return {begin() + i,
false};
83 m_array[m_size++] = v;
84 return {begin() + m_size - 1,
true};
87 size_t erase(
const T& v)
99 bool contains(
const T& v)
const {
return find(v) != end(); }
101 const_iterator find(
const T& v)
const {
return std::find(begin(), end(), v); }
103 const T* data()
const {
return m_array.data(); }
105 const T& front()
const
108 return m_array.front();
111 const T& back()
const
114 return m_array.at(m_size - 1);
117 const T& at(
const size_t i)
const
120 return m_array.at(i);
123 const T& operator[](
const size_t i)
const
129 template <
typename U,
class UnaryOperation>
130 auto transformed(UnaryOperation op)
133 result.resize(size());
134 for (
size_t i = 0; i < size(); ++i) {
135 result[i] = op(at(i));
137 result.ensure_unique();
145 static_assert(D <= N,
"Invalid size");
146 return to_tuple_helper(std::make_index_sequence<D>());
150 template <
size_t... Indices>
151 auto to_tuple_helper(std::index_sequence<Indices...>)
153 return std::make_tuple(m_array[Indices]...);
158 std::sort(m_array.begin(), m_array.end());
159 auto it = std::unique(m_array.begin(), m_array.end());
160 m_size =
static_cast<size_t>(std::distance(m_array.begin(), it));
163 iterator find(
const T& v) {
return std::find(begin(), end(), v); }
165 T* data() {
return m_array.data(); }
170 return m_array.front();
176 return m_array.at(m_size - 1);
180template <
class T,
size_t N>
183 return (lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin()));
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
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
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Stack-allocated set with a maximum size.
Definition: StackSet.h:36