StringKeyedSet.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright 2015-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. // Copyright 2013-present Facebook. All Rights Reserved.
  17. // @author: Pavlo Kushnir (pavlo)
  18. #pragma once
  19. #include <initializer_list>
  20. #include <memory>
  21. #include <set>
  22. #include <folly/Range.h>
  23. #include <folly/experimental/StringKeyedCommon.h>
  24. namespace folly {
  25. /**
  26. * Wrapper class for set<string> that can
  27. * perform lookup operations with StringPiece, not only string.
  28. *
  29. * It uses kind of hack: string pointed by StringPiece is copied when
  30. * StringPiece is inserted into set
  31. */
  32. template <
  33. class Compare = std::less<StringPiece>,
  34. class Alloc = std::allocator<StringPiece>>
  35. class StringKeyedSetBase : private std::set<StringPiece, Compare, Alloc> {
  36. private:
  37. using Base = std::set<StringPiece, Compare, Alloc>;
  38. public:
  39. typedef typename Base::key_type key_type;
  40. typedef typename Base::value_type value_type;
  41. typedef typename Base::key_compare key_compare;
  42. typedef typename Base::allocator_type allocator_type;
  43. typedef typename Base::reference reference;
  44. typedef typename Base::const_reference const_reference;
  45. typedef typename Base::pointer pointer;
  46. typedef typename Base::const_pointer const_pointer;
  47. typedef typename Base::iterator iterator;
  48. typedef typename Base::const_iterator const_iterator;
  49. typedef typename Base::reverse_iterator reverse_iterator;
  50. typedef typename Base::const_reverse_iterator const_reverse_iterator;
  51. typedef typename Base::size_type size_type;
  52. typedef typename Base::difference_type difference_type;
  53. explicit StringKeyedSetBase(
  54. const key_compare& comp = key_compare(),
  55. const allocator_type& alloc = allocator_type())
  56. : Base(comp, alloc) {}
  57. explicit StringKeyedSetBase(const allocator_type& alloc) : Base(alloc) {}
  58. template <class InputIterator>
  59. StringKeyedSetBase(
  60. InputIterator b,
  61. InputIterator e,
  62. const key_compare& comp = key_compare(),
  63. const allocator_type& alloc = allocator_type())
  64. : Base(comp, alloc) {
  65. for (; b != e; ++b) {
  66. emplace(*b);
  67. }
  68. }
  69. StringKeyedSetBase(const StringKeyedSetBase& rhs)
  70. : StringKeyedSetBase(rhs, rhs.get_allocator()) {}
  71. StringKeyedSetBase(const StringKeyedSetBase& rhs, const allocator_type& a)
  72. : StringKeyedSetBase(rhs.begin(), rhs.end(), rhs.key_comp(), a) {}
  73. StringKeyedSetBase(StringKeyedSetBase&& other) noexcept
  74. : Base(std::move(other)) {
  75. assert(other.empty());
  76. }
  77. StringKeyedSetBase(
  78. StringKeyedSetBase&& other,
  79. const allocator_type& alloc) noexcept
  80. : Base(std::move(other), alloc) {
  81. assert(other.empty());
  82. }
  83. StringKeyedSetBase(
  84. std::initializer_list<value_type> il,
  85. const key_compare& comp = key_compare(),
  86. const allocator_type& alloc = allocator_type())
  87. : StringKeyedSetBase(il.begin(), il.end(), comp, alloc) {}
  88. StringKeyedSetBase& operator=(const StringKeyedSetBase& other) {
  89. if (this == &other) {
  90. return *this;
  91. }
  92. return *this = StringKeyedSetBase(other);
  93. }
  94. StringKeyedSetBase& operator=(StringKeyedSetBase&& other) noexcept {
  95. assert(this != &other);
  96. clear();
  97. Base::operator=(std::move(other));
  98. assert(other.empty());
  99. return *this;
  100. }
  101. using Base::begin;
  102. using Base::cbegin;
  103. using Base::cend;
  104. using Base::count;
  105. using Base::empty;
  106. using Base::end;
  107. using Base::find;
  108. using Base::lower_bound;
  109. using Base::max_size;
  110. using Base::size;
  111. using Base::upper_bound;
  112. bool operator==(StringKeyedSetBase const& other) const {
  113. Base const& lhs = *this;
  114. Base const& rhs = static_cast<Base const&>(other);
  115. return lhs == rhs;
  116. }
  117. template <class... Args>
  118. std::pair<iterator, bool> emplace(Args&&... args) {
  119. auto key = StringPiece(std::forward<Args>(args)...);
  120. auto it = find(key);
  121. if (it != end()) {
  122. return {it, false};
  123. }
  124. return Base::emplace(stringPieceDup(key, get_allocator()));
  125. }
  126. std::pair<iterator, bool> insert(value_type val) {
  127. auto it = find(val);
  128. if (it != end()) {
  129. return {it, false};
  130. }
  131. return Base::insert(stringPieceDup(val, get_allocator()));
  132. }
  133. iterator erase(const_iterator position) {
  134. auto key = *position;
  135. auto result = Base::erase(position);
  136. stringPieceDel(key, get_allocator());
  137. return result;
  138. }
  139. size_type erase(StringPiece key) {
  140. auto it = find(key);
  141. if (it == end()) {
  142. return 0;
  143. }
  144. erase(it);
  145. return 1;
  146. }
  147. void clear() noexcept {
  148. for (auto it : *this) {
  149. stringPieceDel(it, get_allocator());
  150. }
  151. Base::clear();
  152. }
  153. using Base::get_allocator;
  154. void swap(StringKeyedSetBase& other) & {
  155. return Base::swap(other);
  156. }
  157. ~StringKeyedSetBase() {
  158. // Here we assume that set doesn't use keys in destructor
  159. for (auto it : *this) {
  160. stringPieceDel(it, get_allocator());
  161. }
  162. }
  163. };
  164. using StringKeyedSet = StringKeyedSetBase<>;
  165. } // namespace folly