Wildmeshing Toolkit
Loading...
Searching...
No Matches
DynamicArray.hxx
Go to the documentation of this file.
1#include "DynamicArray.hpp"
2
3#include <assert.h>
5
6#include <wmtk/Tuple.hpp>
9
10namespace wmtk::utils {
11
12template <typename T, uint64_t ArraySize>
14{
15 assert(index < m_end_index);
16 if (m_use_vector) {
17 return m_vector[index];
18 } else {
19 return m_array[index];
20 }
21}
22
23template <typename T, uint64_t ArraySize>
24const T& DynamicArray<T, ArraySize>::operator[](const uint64_t index) const
25{
26 assert(index < m_end_index);
27 if (m_use_vector) {
28 return m_vector[index];
29 } else {
30 return m_array[index];
31 }
34template <typename T, uint64_t ArraySize>
36{
37 if (m_use_vector) {
38 m_vector.emplace_back(val);
39 m_end_index++;
40 return;
41 }
43 if (m_end_index < ArraySize) {
44 m_array[m_end_index++] = val;
45 return;
46 }
47
48 // switch from array to vector
49 switch_to_vector();
51 m_vector.emplace_back(val);
52 ++m_end_index;
53}
54
55template <typename T, uint64_t ArraySize>
57{
58 return m_end_index;
59}
60
61template <typename T, uint64_t ArraySize>
63{
64 if (m_use_vector) {
65 return m_vector.capacity();
66 }
67 return ArraySize;
68}
69
70template <typename T, uint64_t ArraySize>
71inline constexpr uint64_t DynamicArray<T, ArraySize>::array_size()
72{
73 return ArraySize;
74}
75
76template <typename T, uint64_t ArraySize>
77void DynamicArray<T, ArraySize>::reserve(const uint64_t new_capacity)
78{
79 if (new_capacity < ArraySize) {
80 return;
81 }
82
83 if (!m_use_vector) {
84 switch_to_vector();
85 }
86
87 m_vector.reserve(new_capacity);
88}
89
90template <typename T, uint64_t ArraySize>
92{
93 return m_use_vector;
94}
95
96template <typename T, uint64_t ArraySize>
98{
99 logger().debug("Switching from array to vector.");
100 m_use_vector = true;
101 m_vector.reserve(ArraySize * 2);
102 std::copy(m_array.begin(), m_array.end(), std::back_inserter(m_vector));
103}
104
105template <typename T, uint64_t ArraySize>
107 : m_container(container)
108 , m_index(index)
109{}
110
111template <typename T, uint64_t ArraySize>
117
118template <typename T, uint64_t ArraySize>
120{
121 return m_index != other.m_index;
122}
123
124template <typename T, uint64_t ArraySize>
126{
127 return (*m_container)[m_index];
128}
129
130} // namespace wmtk::utils
bool operator!=(const Iterator &other) const
void reserve(const uint64_t new_capacity)
void emplace_back(const T &val)
uint64_t capacity() const
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:58