Lagrange
StackVector.h
1/*
2 * Copyright 2022 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/utils/assert.h>
15
16#include <algorithm>
17#include <array>
18#include <initializer_list>
19
20namespace lagrange {
21
27
34template <typename T, size_t N>
36{
37private:
38 std::array<T, N> m_array;
39 size_t m_size = 0;
40
41public:
42 StackVector() = default;
43
44 StackVector(std::initializer_list<T> init)
45 : m_size(init.size())
46 {
47 la_runtime_assert(m_size <= N);
48 auto it = init.begin();
49 for (size_t i = 0; i < m_size; ++i) {
50 m_array[i] = std::move(*it);
51 ++it;
52 }
53 }
54
55 size_t size() const { return m_size; }
56
57 void clear() { m_size = 0; }
58
59 void resize(const size_t i)
60 {
61 la_runtime_assert(i <= m_array.size());
62 m_size = i;
63 }
64
65 void push_back(const T& v)
66 {
67 la_runtime_assert(m_size < m_array.size());
68 m_array[m_size++] = v;
69 }
70
71 template <class... Args>
72 void emplace_back(Args&&... args)
73 {
74 la_runtime_assert(m_size < m_array.size());
75 m_array[m_size++] = T(std::forward<Args>(args)...);
76 }
77
78 void pop_back()
79 {
80 la_runtime_assert(m_size > 0);
81 --m_size;
82 }
83
84 T* data() { return m_array.data(); }
85
86 const T* data() const { return m_array.data(); }
87
88 T& front()
89 {
90 la_runtime_assert(m_size > 0);
91 return m_array.front();
92 }
93
94 const T& front() const
95 {
96 la_runtime_assert(m_size > 0);
97 return m_array.front();
98 }
99
100 T& back()
101 {
102 la_runtime_assert(m_size > 0);
103 return m_array.at(m_size - 1);
104 }
105
106 const T& back() const
107 {
108 la_runtime_assert(m_size > 0);
109 return m_array.at(m_size - 1);
110 }
111
112 T& at(const size_t i)
113 {
114 la_runtime_assert(i < m_size);
115 return m_array.at(i);
116 }
117
118 const T& at(const size_t i) const
119 {
120 la_runtime_assert(i < m_size);
121 return m_array.at(i);
122 }
123
124 T& operator[](const size_t i)
125 {
126 la_runtime_assert(i < m_size);
127 return m_array[i];
128 }
129
130 const T& operator[](const size_t i) const
131 {
132 la_runtime_assert(i < m_size);
133 return m_array[i];
134 }
135
136 template <typename U, class UnaryOperation>
137 auto transformed(UnaryOperation op) {
138 StackVector<U, N> result;
139 result.resize(size());
140 for (size_t i = 0; i < size(); ++i) {
141 result[i] = op(at(i));
142 }
143 return result;
144 }
145
146 template <size_t D>
147 auto to_tuple() {
148 la_debug_assert(D == m_size);
149 static_assert(D <= N, "Invalid size");
150 return to_tuple_helper(std::make_index_sequence<D>());
151 }
152
153private:
154 template <size_t... Indices>
155 auto to_tuple_helper(std::index_sequence<Indices...>) {
156 return std::make_tuple(m_array[Indices]...);
157 }
158
159public:
160 using iterator = typename std::array<T, N>::iterator;
161 using const_iterator = typename std::array<T, N>::const_iterator;
162 iterator begin() { return m_array.begin(); }
163 iterator end() { return m_array.begin() + m_size; }
164 const_iterator begin() const { return m_array.begin(); }
165 const_iterator end() const { return m_array.begin() + m_size; }
166};
167
168template <class T, size_t N>
169bool operator==(const StackVector<T, N>& lhs, const StackVector<T, N>& rhs)
170{
171 return (lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin()));
172}
173
175
176} // namespace lagrange
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
#define la_debug_assert(...)
Debug assertion check.
Definition: assert.h:189
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Definition: project.cpp:27
Stack-allocated vector with a maximum size.
Definition: StackVector.h:36