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 bool empty() const { return m_size == 0; }
56
57 size_t size() const { return m_size; }
58
59 void clear() { m_size = 0; }
60
61 void resize(const size_t i)
62 {
63 la_runtime_assert(i <= m_array.size());
64 m_size = i;
65 }
66
67 void push_back(const T& v)
68 {
69 la_runtime_assert(m_size < m_array.size());
70 m_array[m_size++] = v;
71 }
72
73 template <class... Args>
74 void emplace_back(Args&&... args)
75 {
76 la_runtime_assert(m_size < m_array.size());
77 m_array[m_size++] = T(std::forward<Args>(args)...);
78 }
79
80 void pop_back()
81 {
82 la_runtime_assert(m_size > 0);
83 --m_size;
84 }
85
86 T* data() { return m_array.data(); }
87
88 const T* data() const { return m_array.data(); }
89
90 T& front()
91 {
92 la_runtime_assert(m_size > 0);
93 return m_array.front();
94 }
95
96 const T& front() const
97 {
98 la_runtime_assert(m_size > 0);
99 return m_array.front();
100 }
101
102 T& back()
103 {
104 la_runtime_assert(m_size > 0);
105 return m_array.at(m_size - 1);
106 }
107
108 const T& back() const
109 {
110 la_runtime_assert(m_size > 0);
111 return m_array.at(m_size - 1);
112 }
113
114 T& at(const size_t i)
115 {
116 la_runtime_assert(i < m_size);
117 return m_array.at(i);
118 }
119
120 const T& at(const size_t i) const
121 {
122 la_runtime_assert(i < m_size);
123 return m_array.at(i);
124 }
125
126 T& operator[](const size_t i)
127 {
128 la_runtime_assert(i < m_size);
129 return m_array[i];
130 }
131
132 const T& operator[](const size_t i) const
133 {
134 la_runtime_assert(i < m_size);
135 return m_array[i];
136 }
137
138 template <typename U, class UnaryOperation>
139 auto transformed(UnaryOperation op) {
140 StackVector<U, N> result;
141 result.resize(size());
142 for (size_t i = 0; i < size(); ++i) {
143 result[i] = op(at(i));
144 }
145 return result;
146 }
147
148 template <size_t D>
149 auto to_tuple() {
150 la_debug_assert(D == m_size);
151 static_assert(D <= N, "Invalid size");
152 return to_tuple_helper(std::make_index_sequence<D>());
153 }
154
155private:
156 template <size_t... Indices>
157 auto to_tuple_helper(std::index_sequence<Indices...>) {
158 return std::make_tuple(m_array[Indices]...);
159 }
160
161public:
162 using iterator = typename std::array<T, N>::iterator;
163 using const_iterator = typename std::array<T, N>::const_iterator;
164 iterator begin() { return m_array.begin(); }
165 iterator end() { return m_array.begin() + m_size; }
166 const_iterator begin() const { return m_array.begin(); }
167 const_iterator end() const { return m_array.begin() + m_size; }
168};
169
170template <class T, size_t N>
171bool operator==(const StackVector<T, N>& lhs, const StackVector<T, N>& rhs)
172{
173 return (lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin()));
174}
175
177
178} // 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