Wildmeshing Toolkit
DynamicArray.hxx
Go to the documentation of this file.
1 #include "DynamicArray.hpp"
2 
3 #include <assert.h>
4 #include <wmtk/utils/Logger.hpp>
5 
6 #include <wmtk/Tuple.hpp>
9 
10 namespace wmtk::utils {
11 
12 template <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 
23 template <typename T, uint64_t ArraySize>
24 const 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  }
32 }
33 
34 template <typename T, uint64_t ArraySize>
36 {
37  if (m_use_vector) {
38  m_vector.emplace_back(val);
39  m_end_index++;
40  return;
41  }
42 
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();
50 
51  m_vector.emplace_back(val);
52  ++m_end_index;
53 }
54 
55 template <typename T, uint64_t ArraySize>
57 {
58  return m_end_index;
59 }
60 
61 template <typename T, uint64_t ArraySize>
63 {
64  if (m_use_vector) {
65  return m_vector.capacity();
66  }
67  return ArraySize;
68 }
69 
70 template <typename T, uint64_t ArraySize>
71 inline constexpr uint64_t DynamicArray<T, ArraySize>::array_size()
72 {
73  return ArraySize;
74 }
75 
76 template <typename T, uint64_t ArraySize>
77 void 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 
90 template <typename T, uint64_t ArraySize>
92 {
93  return m_use_vector;
94 }
95 
96 template <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 
105 template <typename T, uint64_t ArraySize>
106 DynamicArray<T, ArraySize>::Iterator::Iterator(const DynamicArray* container, const uint64_t index)
107  : m_container(container)
108  , m_index(index)
109 {}
110 
111 template <typename T, uint64_t ArraySize>
113 {
114  ++m_index;
115  return *this;
116 }
117 
118 template <typename T, uint64_t ArraySize>
120 {
121  return m_index != other.m_index;
122 }
123 
124 template <typename T, uint64_t ArraySize>
126 {
127  return (*m_container)[m_index];
128 }
129 
130 } // namespace wmtk::utils
bool operator!=(const Iterator &other) const
Iterator(const DynamicArray *container, const uint64_t index=0)
void reserve(const uint64_t new_capacity)
void emplace_back(const T &val)
T & operator[](const uint64_t index)
constexpr static uint64_t array_size()
Return the size of the static array.
uint64_t capacity() const
spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:58