Wildmeshing Toolkit
Loading...
Searching...
No Matches
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
>
7
#include <
wmtk/simplex/Simplex.hpp
>
8
#include <
wmtk/utils/Rational.hpp
>
9
10
namespace
wmtk::utils
{
11
12
template
<
typename
T, u
int
64_t ArraySize>
13
T&
DynamicArray<T, ArraySize>::operator[]
(
const
uint64_t index)
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, u
int
64_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, u
int
64_t ArraySize>
35
void
DynamicArray<T, ArraySize>::emplace_back
(
const
T& val)
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, u
int
64_t ArraySize>
56
uint64_t
DynamicArray<T, ArraySize>::size
()
const
57
{
58
return
m_end_index;
59
}
60
61
template
<
typename
T, u
int
64_t ArraySize>
62
uint64_t
DynamicArray<T, ArraySize>::capacity
()
const
63
{
64
if
(m_use_vector) {
65
return
m_vector.
capacity
();
66
}
67
return
ArraySize;
68
}
69
70
template
<
typename
T, u
int
64_t ArraySize>
71
inline
constexpr
uint64_t
DynamicArray<T, ArraySize>::array_size
()
72
{
73
return
ArraySize;
74
}
75
76
template
<
typename
T, u
int
64_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, u
int
64_t ArraySize>
91
bool
DynamicArray<T, ArraySize>::uses_vector
()
const
92
{
93
return
m_use_vector;
94
}
95
96
template
<
typename
T, u
int
64_t ArraySize>
97
void
DynamicArray<T, ArraySize>::switch_to_vector
()
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, u
int
64_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, u
int
64_t ArraySize>
112
typename
DynamicArray<T, ArraySize>::Iterator
DynamicArray<T, ArraySize>::Iterator::operator++
()
113
{
114
++m_index;
115
return
*
this
;
116
}
117
118
template
<
typename
T, u
int
64_t ArraySize>
119
bool
DynamicArray<T, ArraySize>::Iterator::operator!=
(
const
Iterator
& other)
const
120
{
121
return
m_index != other.
m_index
;
122
}
123
124
template
<
typename
T, u
int
64_t ArraySize>
125
T
DynamicArray<T, ArraySize>::Iterator::operator*
()
126
{
127
return
(*m_container)[m_index];
128
}
129
130
}
// namespace wmtk::utils
DynamicArray.hpp
Logger.hpp
Rational.hpp
Simplex.hpp
Tuple.hpp
wmtk::utils::DynamicArray::Iterator
Definition
DynamicArray.hpp:14
wmtk::utils::DynamicArray::Iterator::m_index
uint64_t m_index
Definition
DynamicArray.hpp:23
wmtk::utils::DynamicArray::Iterator::operator++
Iterator operator++()
Definition
DynamicArray.hxx:112
wmtk::utils::DynamicArray::Iterator::operator!=
bool operator!=(const Iterator &other) const
Definition
DynamicArray.hxx:119
wmtk::utils::DynamicArray::Iterator::operator*
T operator*()
Definition
DynamicArray.hxx:125
wmtk::utils::DynamicArray
Definition
DynamicArray.hpp:11
wmtk::utils::DynamicArray::reserve
void reserve(const uint64_t new_capacity)
Definition
DynamicArray.hxx:77
wmtk::utils::DynamicArray::emplace_back
void emplace_back(const T &val)
Definition
DynamicArray.hxx:35
wmtk::utils::DynamicArray::capacity
uint64_t capacity() const
Definition
DynamicArray.hxx:62
wmtk::utils
Definition
array_to_map.hpp:3
wmtk::logger
spdlog::logger & logger()
Retrieves the current logger.
Definition
Logger.cpp:58
src
wmtk
utils
DynamicArray.hxx
Generated by
1.9.8