safe_btree_set.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2013 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // The safe_btree_set<> is like btree_set<> except that it removes the caveat
  16. // about insertion and deletion invalidating existing iterators at a small cost
  17. // in making iterators larger and slower.
  18. //
  19. // Revalidation occurs whenever an iterator is accessed. References
  20. // and pointers returned by safe_btree_map<> iterators are not stable,
  21. // they are potentially invalidated by any non-const method on the set.
  22. //
  23. // BEGIN INCORRECT EXAMPLE
  24. // for (auto i = safe_set->begin(); i != safe_set->end(); ++i) {
  25. // const T &value = *i; // DO NOT DO THIS
  26. // [code that modifies safe_set and uses value];
  27. // }
  28. // END INCORRECT EXAMPLE
  29. #ifndef UTIL_BTREE_SAFE_BTREE_SET_H__
  30. #define UTIL_BTREE_SAFE_BTREE_SET_H__
  31. #include <functional>
  32. #include <memory>
  33. #include "btree_container.h"
  34. #include "btree_set.h"
  35. #include "safe_btree.h"
  36. namespace btree {
  37. // The safe_btree_set class is needed mainly for its constructors.
  38. template <typename Key,
  39. typename Compare = std::less<Key>,
  40. typename Alloc = std::allocator<Key>,
  41. int TargetNodeSize = 256>
  42. class safe_btree_set : public btree_unique_container<
  43. safe_btree<btree_set_params<Key, Compare, Alloc, TargetNodeSize> > > {
  44. typedef safe_btree_set<Key, Compare, Alloc, TargetNodeSize> self_type;
  45. typedef btree_set_params<Key, Compare, Alloc, TargetNodeSize> params_type;
  46. typedef safe_btree<params_type> btree_type;
  47. typedef btree_unique_container<btree_type> super_type;
  48. public:
  49. typedef typename btree_type::key_compare key_compare;
  50. typedef typename btree_type::allocator_type allocator_type;
  51. public:
  52. // Default constructor.
  53. safe_btree_set(const key_compare &comp = key_compare(),
  54. const allocator_type &alloc = allocator_type())
  55. : super_type(comp, alloc) {
  56. }
  57. // Copy constructor.
  58. safe_btree_set(const self_type &x)
  59. : super_type(x) {
  60. }
  61. // Range constructor.
  62. template <class InputIterator>
  63. safe_btree_set(InputIterator b, InputIterator e,
  64. const key_compare &comp = key_compare(),
  65. const allocator_type &alloc = allocator_type())
  66. : super_type(b, e, comp, alloc) {
  67. }
  68. };
  69. template <typename K, typename C, typename A, int N>
  70. inline void swap(safe_btree_set<K, C, A, N> &x,
  71. safe_btree_set<K, C, A, N> &y) {
  72. x.swap(y);
  73. }
  74. } // namespace btree
  75. #endif // UTIL_BTREE_SAFE_BTREE_SET_H__