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
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#pragma once
/** \file
* \ingroup bli
*
* A `blender::VectorAdaptor` is a container with a fixed maximum size and does not own the
* underlying memory. When an adaptor is constructed, you have to provide it with an uninitialized
* array that will be filled when elements are added to the vector. The vector adaptor is not able
* to grow. Therefore, it is undefined behavior to add more elements than fit into the provided
* buffer.
*/
#include "BLI_span.hh"
namespace blender {
template<typename T> class VectorAdaptor {
private:
T *begin_;
T *end_;
T *capacity_end_;
public:
VectorAdaptor() : begin_(nullptr), end_(nullptr), capacity_end_(nullptr)
{
}
VectorAdaptor(T *data, int64_t capacity, int64_t size = 0)
: begin_(data), end_(data + size), capacity_end_(data + capacity)
{
}
VectorAdaptor(MutableSpan<T> span) : VectorAdaptor(span.data(), span.size(), 0)
{
}
void append(const T &value)
{
BLI_assert(end_ < capacity_end_);
new (end_) T(value);
end_++;
}
void append(T &&value)
{
BLI_assert(end_ < capacity_end_);
new (end_) T(std::move(value));
end_++;
}
void append_n_times(const T &value, int64_t n)
{
BLI_assert(end_ + n <= capacity_end_);
uninitialized_fill_n(end_, n, value);
end_ += n;
}
void extend(Span<T> values)
{
BLI_assert(end_ + values.size() <= capacity_end_);
uninitialized_copy_n(values.data(), values.size(), end_);
end_ += values.size();
}
int64_t capacity() const
{
return capacity_end_ - begin_;
}
int64_t size() const
{
return end_ - begin_;
}
bool is_empty() const
{
return begin_ == end_;
}
bool is_full() const
{
return end_ == capacity_end_;
}
};
} // namespace blender
|