proxygen
StringKeyedSet.h
Go to the documentation of this file.
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 
19 #pragma once
20 
21 #include <initializer_list>
22 #include <memory>
23 #include <set>
24 
25 #include <folly/Range.h>
27 
28 namespace folly {
29 
37 template <
38  class Compare = std::less<StringPiece>,
39  class Alloc = std::allocator<StringPiece>>
40 class StringKeyedSetBase : private std::set<StringPiece, Compare, Alloc> {
41  private:
42  using Base = std::set<StringPiece, Compare, Alloc>;
43 
44  public:
45  typedef typename Base::key_type key_type;
46  typedef typename Base::value_type value_type;
47  typedef typename Base::key_compare key_compare;
48  typedef typename Base::allocator_type allocator_type;
49  typedef typename Base::reference reference;
50  typedef typename Base::const_reference const_reference;
51  typedef typename Base::pointer pointer;
52  typedef typename Base::const_pointer const_pointer;
53  typedef typename Base::iterator iterator;
54  typedef typename Base::const_iterator const_iterator;
55  typedef typename Base::reverse_iterator reverse_iterator;
56  typedef typename Base::const_reverse_iterator const_reverse_iterator;
57  typedef typename Base::size_type size_type;
58  typedef typename Base::difference_type difference_type;
59 
61  const key_compare& comp = key_compare(),
62  const allocator_type& alloc = allocator_type())
63  : Base(comp, alloc) {}
64 
65  explicit StringKeyedSetBase(const allocator_type& alloc) : Base(alloc) {}
66 
67  template <class InputIterator>
69  InputIterator b,
70  InputIterator e,
71  const key_compare& comp = key_compare(),
72  const allocator_type& alloc = allocator_type())
73  : Base(comp, alloc) {
74  for (; b != e; ++b) {
75  emplace(*b);
76  }
77  }
78 
80  : StringKeyedSetBase(rhs, rhs.get_allocator()) {}
81 
82  StringKeyedSetBase(const StringKeyedSetBase& rhs, const allocator_type& a)
83  : StringKeyedSetBase(rhs.begin(), rhs.end(), rhs.key_comp(), a) {}
84 
86  : Base(std::move(other)) {
87  assert(other.empty());
88  }
89 
91  StringKeyedSetBase&& other,
92  const allocator_type& alloc) noexcept
93  : Base(std::move(other), alloc) {
94  assert(other.empty());
95  }
96 
98  std::initializer_list<value_type> il,
99  const key_compare& comp = key_compare(),
100  const allocator_type& alloc = allocator_type())
101  : StringKeyedSetBase(il.begin(), il.end(), comp, alloc) {}
102 
104  if (this == &other) {
105  return *this;
106  }
107  return *this = StringKeyedSetBase(other);
108  }
109 
111  assert(this != &other);
112  clear();
113  Base::operator=(std::move(other));
114  assert(other.empty());
115  return *this;
116  }
117 
118  using Base::begin;
119  using Base::cbegin;
120  using Base::cend;
121  using Base::count;
122  using Base::empty;
123  using Base::end;
124  using Base::find;
125  using Base::lower_bound;
126  using Base::max_size;
127  using Base::size;
128  using Base::upper_bound;
129 
130  bool operator==(StringKeyedSetBase const& other) const {
131  Base const& lhs = *this;
132  Base const& rhs = static_cast<Base const&>(other);
133  return lhs == rhs;
134  }
135 
136  template <class... Args>
137  std::pair<iterator, bool> emplace(Args&&... args) {
138  auto key = StringPiece(std::forward<Args>(args)...);
139  auto it = find(key);
140  if (it != end()) {
141  return {it, false};
142  }
143  return Base::emplace(stringPieceDup(key, get_allocator()));
144  }
145 
146  std::pair<iterator, bool> insert(value_type val) {
147  auto it = find(val);
148  if (it != end()) {
149  return {it, false};
150  }
151  return Base::insert(stringPieceDup(val, get_allocator()));
152  }
153 
154  iterator erase(const_iterator position) {
155  auto key = *position;
156  auto result = Base::erase(position);
157  stringPieceDel(key, get_allocator());
158  return result;
159  }
160 
161  size_type erase(StringPiece key) {
162  auto it = find(key);
163  if (it == end()) {
164  return 0;
165  }
166  erase(it);
167  return 1;
168  }
169 
170  void clear() noexcept {
171  for (auto it : *this) {
172  stringPieceDel(it, get_allocator());
173  }
174  Base::clear();
175  }
176 
177  using Base::get_allocator;
178 
179  void swap(StringKeyedSetBase& other) & {
180  return Base::swap(other);
181  }
182 
184  // Here we assume that set doesn't use keys in destructor
185  for (auto it : *this) {
186  stringPieceDel(it, get_allocator());
187  }
188  }
189 };
190 
192 
193 } // namespace folly
std::pair< iterator, bool > insert(value_type val)
char b
void stringPieceDel(StringPiece piece, const Alloc &alloc)
Base::key_compare key_compare
Base::size_type size_type
iterator erase(const_iterator position)
StringKeyedSetBase(const allocator_type &alloc)
Base::allocator_type allocator_type
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::set< StringPiece, Compare, Alloc > Base
STL namespace.
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
double val
Definition: String.cpp:273
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
Base::const_reference const_reference
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
size_type erase(StringPiece key)
requires E e noexcept(noexcept(s.error(std::move(e))))
StringKeyedSetBase & operator=(const StringKeyedSetBase &other)
StringKeyedSetBase(InputIterator b, InputIterator e, const key_compare &comp=key_compare(), const allocator_type &alloc=allocator_type())
FOLLY_PUSH_WARNING RHS rhs
Definition: Traits.h:649
bool operator==(StringKeyedSetBase const &other) const
StringKeyedSetBase(StringKeyedSetBase &&other) noexcept
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
Base::const_iterator const_iterator
constexpr auto empty(C const &c) -> decltype(c.empty())
Definition: Access.h:55
StringKeyedSetBase(const StringKeyedSetBase &rhs)
std::pair< iterator, bool > emplace(Args &&...args)
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
Base::const_reverse_iterator const_reverse_iterator
char a
StringKeyedSetBase(const StringKeyedSetBase &rhs, const allocator_type &a)
Base::difference_type difference_type
int * count
const
Definition: upload.py:398
Base::value_type value_type
StringKeyedSetBase(StringKeyedSetBase &&other, const allocator_type &alloc) noexcept
StringKeyedSetBase & operator=(StringKeyedSetBase &&other) noexcept
StringPiece stringPieceDup(StringPiece piece, const Alloc &alloc)
void swap(StringKeyedSetBase &other)&
void swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
Definition: F14TestUtil.h:414
StringKeyedSetBase(std::initializer_list< value_type > il, const key_compare &comp=key_compare(), const allocator_type &alloc=allocator_type())
Range< const char * > StringPiece
Iterator< typename Container::const_iterator > cbegin(const Container &c)
Definition: Padded.h:319
Base::const_pointer const_pointer
Base::reverse_iterator reverse_iterator
StringKeyedSetBase(const key_compare &comp=key_compare(), const allocator_type &alloc=allocator_type())
Iterator< typename Container::const_iterator > cend(const Container &c)
Definition: Padded.h:324
Base::reference reference