1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#pragma once
#include "glContainer.h"
#include <cassert>
#include <special_members.h>
#include <utility>
template<typename T> class InstanceVertices : protected glContainer<T> {
using base = glContainer<T>;
public:
class [[nodiscard]] InstanceProxy {
public:
InstanceProxy(InstanceVertices * iv, std::size_t idx) : instances {iv}, index {idx} { }
InstanceProxy(InstanceProxy && other) noexcept :
instances {std::exchange(other.instances, nullptr)}, index {other.index}
{
}
NO_COPY(InstanceProxy);
~InstanceProxy()
{
if (instances) {
instances->release(index);
}
}
InstanceProxy &
operator=(InstanceProxy && other) noexcept
{
if (instances) {
instances->release(index);
}
instances = std::exchange(other.instances, nullptr);
index = other.index;
return *this;
}
template<typename U>
InstanceProxy &
operator=(U && v)
{
instances->lookup(index) = std::forward<U>(v);
return *this;
}
// NOLINTNEXTLINE)hicpp-explicit-conversions
[[nodiscard]] operator T &()
{
return instances->lookup(index);
}
// NOLINTNEXTLINE)hicpp-explicit-conversions
[[nodiscard]] operator const T &() const
{
return instances->lookup(index);
}
[[nodiscard]] T *
get()
{
return &instances->lookup(index);
}
[[nodiscard]] const T *
get() const
{
return &instances->lookup(index);
}
[[nodiscard]] T *
operator->()
{
return get();
}
[[nodiscard]] const T *
operator->() const
{
return get();
}
[[nodiscard]] T &
operator*()
{
return instances->lookup(index);
}
[[nodiscard]] const T &
operator*() const
{
return instances->lookup(index);
}
private:
InstanceVertices<T> * instances;
std::size_t index;
};
template<typename... Params>
[[nodiscard]] InstanceProxy
acquire(Params &&... params)
{
if (!unused.empty()) {
auto idx = unused.back();
unused.pop_back();
index[idx] = base::size();
base::emplace_back(std::forward<Params>(params)...);
return InstanceProxy {this, idx};
}
index.emplace_back(base::size());
base::emplace_back(std::forward<Params>(params)...);
return InstanceProxy {this, index.size() - 1};
}
using base::bufferName;
[[nodiscard]] auto
size() const
{
base::unmap();
return base::size();
}
template<typename Pred>
glContainer<T>::iterator
partition(Pred pred)
{
return partition(base::begin(), base::end(), pred);
}
protected:
friend InstanceProxy;
void
release(const size_t pidx)
{
if (base::size() - 1 != index[pidx]) {
lookup(pidx) = std::move(base::back());
*std::find_if(index.begin(), index.end(), [this, old = base::size() - 1](const auto & i) {
return i == old && !std::binary_search(unused.begin(), unused.end(), &i - index.data());
}) = index[pidx];
}
base::pop_back();
if (pidx == index.size() - 1) {
index.pop_back();
}
else {
// Remember p.index is free index now, keeping it sorted
unused.insert(std::upper_bound(unused.begin(), unused.end(), pidx), pidx);
}
}
[[nodiscard]] T &
lookup(size_t pindex)
{
return base::data()[index[pindex]];
}
template<typename Pred>
glContainer<T>::iterator
partition(glContainer<T>::iterator first, glContainer<T>::iterator last, Pred pred)
{
while (first < last) {
first = std::find_if_not(first, last, pred);
last = --std::find_if(std::make_reverse_iterator(last), std::make_reverse_iterator(first), pred).base();
if (first < last) {
std::iter_swap(first, last);
std::iter_swap(std::find(index.begin(), index.end(), first - base::begin()),
std::find(index.begin(), index.end(), last - base::begin()));
}
}
return first;
}
// Index into buffer given to nth proxy
std::vector<size_t> index;
// List of free spaces in index
std::vector<size_t> unused;
};
|