forked from JGRennison/cpp-btree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
safe_btree_set.h
100 lines (85 loc) · 3.15 KB
/
safe_btree_set.h
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
// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// The safe_btree_set<> is like btree_set<> except that it removes the caveat
// about insertion and deletion invalidating existing iterators at a small cost
// in making iterators larger and slower.
//
// Revalidation occurs whenever an iterator is accessed. References
// and pointers returned by safe_btree_map<> iterators are not stable,
// they are potentially invalidated by any non-const method on the set.
//
// BEGIN INCORRECT EXAMPLE
// for (auto i = safe_set->begin(); i != safe_set->end(); ++i) {
// const T &value = *i; // DO NOT DO THIS
// [code that modifies safe_set and uses value];
// }
// END INCORRECT EXAMPLE
#ifndef UTIL_BTREE_SAFE_BTREE_SET_H__
#define UTIL_BTREE_SAFE_BTREE_SET_H__
#include <functional>
#include <memory>
#include "btree_container.h"
#include "btree_set.h"
#include "safe_btree.h"
namespace btree {
// The safe_btree_set class is needed mainly for its constructors.
template <typename Key,
typename Compare = std::less<Key>,
typename Alloc = std::allocator<Key>,
int TargetNodeSize = 256>
class safe_btree_set : public btree_unique_container<
safe_btree<btree_set_params<Key, Compare, Alloc, TargetNodeSize> > > {
typedef safe_btree_set<Key, Compare, Alloc, TargetNodeSize> self_type;
typedef btree_set_params<Key, Compare, Alloc, TargetNodeSize> params_type;
typedef safe_btree<params_type> btree_type;
typedef btree_unique_container<btree_type> super_type;
public:
typedef typename btree_type::key_compare key_compare;
typedef typename btree_type::allocator_type allocator_type;
public:
// Default constructor.
safe_btree_set(const key_compare &comp = key_compare(),
const allocator_type &alloc = allocator_type())
: super_type(comp, alloc) {
}
// Copy constructor.
safe_btree_set(const self_type &x)
: super_type(x) {
}
// Move constructor.
safe_btree_set(self_type &&x) noexcept
: super_type() {
this->swap(x);
}
// Copy/move assignment
self_type& operator=(self_type x) noexcept {
this->swap(x);
return *this;
}
// Range constructor.
template <class InputIterator>
safe_btree_set(InputIterator b, InputIterator e,
const key_compare &comp = key_compare(),
const allocator_type &alloc = allocator_type())
: super_type(b, e, comp, alloc) {
}
};
template <typename K, typename C, typename A, int N>
inline void swap(safe_btree_set<K, C, A, N> &x,
safe_btree_set<K, C, A, N> &y) {
x.swap(y);
}
} // namespace btree
#endif // UTIL_BTREE_SAFE_BTREE_SET_H__