Itasca C++ Interface
Toggle main menu visibility
Loading...
Searching...
No Matches
flatarray.h
1
#pragma once
2
#include <tuple>
3
4
template
<
typename
TupleT,
typename
Fn>
5
void
for_each_tuple(TupleT&& tp, Fn&& fn) {
6
std::apply(
7
[&fn](
auto
&& ...args)
8
{
9
(fn(std::forward<
decltype
(args)>(args)), ...);
10
}, std::forward<TupleT>(tp)
11
);
12
}
13
14
template
<
class
T>
class
FlatArrayVec :
public
std::vector<T> {
15
public
:
16
typedef
uint64 size_type;
17
FlatArrayVec() { }
18
FlatArrayVec(size_type s,
double
=1.3) : defaultStorage_(s) { this->reserve(defaultStorage_); }
19
FlatArrayVec(
const
FlatArrayVec<T> &f) { operator=(f); }
20
FlatArrayVec(std::initializer_list<T> l) {
21
for
(
auto
it=l.begin();it!=l.end();++it)
22
push_back(*it);
23
}
24
using
std::vector<T>::reserve;
25
using
std::vector<T>::assign;
26
const
FlatArrayVec<T> &operator=(
const
FlatArrayVec<T> &f) {
27
defaultStorage_ = f.defaultStorage_;
28
this->clear();
29
reserve(f.capacity());
30
if
constexpr
(std::is_copy_assignable<T>::value)
31
assign(f.begin(),f.end());
32
else
33
throw
Exception
(
"Internal Error: Attempting to copy a vector who's members are not copyable (like an owned pointer)"
);
34
return
*
this
;
35
}
36
const
size_type &allocated()
const
{
return
this->capacity(); }
37
size_type removeReplaceLast(size_type i) {
38
if
(i>=this->size())
39
throw
Exception
(
"RemoveReplaceLast failed"
);
40
std::swap(this->at(i),this->back());
41
this->pop_back();
42
return
this->size();
43
}
44
size_type removeReplaceLastClip(size_type i) {
45
size_type ret = removeReplaceLast(i);
46
this->shrink_to_fit();
47
return
ret;
48
}
49
50
bool
remove(size_type i) {
51
if
(i>=this->size())
52
throw
Exception
(
"Remove failed"
);
53
this->erase(i);
54
return
true
;
55
}
56
size_type removeAll(
const
T &t) {
57
size_type ret = 0;
58
for
(size_type i=0;i<this->size();) {
59
if
(t==this->at(i)) {
60
remove(i);
61
++ret;
62
}
else
63
++i;
64
}
65
return
ret;
66
}
67
void
reset() {
68
this->clear();
69
if
(defaultStorage_ != this->capacity()) {
70
this->shrink_to_fit();
71
this->reserve(defaultStorage_);
72
}
73
}
74
void
clip() { this->shrink_to_fit(); }
75
const
T & operator[] (
const
int
nIndex)
const
{
76
try
{
77
#ifdef _DEBUG
78
if
(nIndex >= (
int
)this->size() or nIndex < 0)
79
throw
Exception
(
"Size check failed in FlatArrayVec"
);
80
#endif
81
return
std::vector<T>::operator[](nIndex);
82
}
catch
(...) {
83
throw
;
84
}
85
}
86
87
T & operator[] (
const
int
nIndex) {
88
try
{
89
#ifdef _DEBUG
90
if
(nIndex >= (
int
)this->size() or nIndex < 0)
91
throw
Exception
(
"Size check failed in FlatArrayVec"
);
92
#endif
93
return
std::vector<T>::operator[](nIndex);
94
}
catch
(...) {
95
throw
;
96
}
97
}
98
private
:
99
size_type defaultStorage_ = 1000;
100
};
101
// EoF
Exception
Base exception class for all Itasca code.
Definition
baseexception.h:10
common
base
src
flatarray.h
Generated by
1.18.0