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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
#include <memory>
#include <vector>
namespace vespalib {
/** metrics-internal utility class */
template <typename T>
class StableStore
{
using MyClass = StableStore<T>;
template<typename U>
friend void swap(StableStore<U> &a, StableStore<U> &b);
public:
using UP = std::unique_ptr<MyClass>;
StableStore();
~StableStore() {}
void add(T t) {
size_t sz = _mine.size();
if (sz == _mine.capacity()) {
UP next(new MyClass(_size, std::move(_more), std::move(_mine)));;
_mine.clear();
_mine.reserve(sz << 1);
_more = std::move(next);
}
_mine.push_back(t);
++_size;
}
template<typename FUNC>
void for_each(FUNC &&func) const {
std::vector<const MyClass *> vv;
dffill(vv);
for (const MyClass *p : vv) {
for (const T& elem : p->_mine) {
func(elem);
}
}
}
size_t size() const { return _size; }
private:
void dffill(std::vector<const MyClass *> &vv) const {
if (_more) { _more->dffill(vv); }
vv.push_back(this);
}
StableStore(size_t sz, UP &&more, std::vector<T> &&mine);
size_t _size;
UP _more;
std::vector<T> _mine;
};
template<typename T>
StableStore<T>::StableStore()
: _size(0),
_more(),
_mine()
{
_mine.reserve(3);
}
template<typename T>
StableStore<T>::StableStore(size_t sz, UP &&more, std::vector<T> &&mine)
: _size(sz),
_more(std::move(more)),
_mine(std::move(mine))
{}
template <typename T>
void swap(StableStore<T> &a,
StableStore<T> &b)
{
using std::swap;
swap(a._size, b._size);
swap(a._mine, b._mine);
swap(a._more, b._more);
}
} // namespace vespalib
|