Consistent indentation for class visibility labels
[folly.git] / folly / experimental / StringKeyedSet.h
1 /*
2  * Copyright 2017 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>
26 #include <folly/experimental/StringKeyedCommon.h>
27
28 namespace folly {
29
30 /**
31  * Wrapper class for set<string> that can
32  * perform lookup operations with StringPiece, not only string.
33  *
34  * It uses kind of hack: string pointed by StringPiece is copied when
35  * StringPiece is inserted into set
36  */
37 template <class Compare = std::less<StringPiece>,
38           class Alloc = std::allocator<StringPiece>>
39 class StringKeyedSetBase
40     : 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
60   explicit StringKeyedSetBase(
61     const key_compare& comp = key_compare(),
62     const allocator_type& alloc = allocator_type())
63       : Base(comp, alloc) {
64   }
65
66   explicit StringKeyedSetBase(const allocator_type& alloc)
67       : Base(alloc) {
68   }
69
70   template <class InputIterator>
71   StringKeyedSetBase(
72     InputIterator b, InputIterator e,
73     const key_compare& comp = key_compare(),
74     const allocator_type& alloc = allocator_type())
75       : Base(comp, alloc) {
76     for (; b != e; ++b) {
77       emplace(*b);
78     }
79   }
80
81   StringKeyedSetBase(const StringKeyedSetBase& rhs)
82       : StringKeyedSetBase(rhs, rhs.get_allocator()) {
83   }
84
85   StringKeyedSetBase(const StringKeyedSetBase& rhs,
86                      const allocator_type& a)
87       : StringKeyedSetBase(rhs.begin(), rhs.end(), rhs.key_comp(), a) {
88   }
89
90   StringKeyedSetBase(StringKeyedSetBase&& other) noexcept
91       : Base(std::move(other)) {
92     assert(other.empty());
93   }
94
95   StringKeyedSetBase(StringKeyedSetBase&& other,
96                      const allocator_type& alloc) noexcept
97       : Base(std::move(other), alloc) {
98     assert(other.empty());
99   }
100
101   StringKeyedSetBase(
102     std::initializer_list<value_type> il,
103     const key_compare& comp = key_compare(),
104     const allocator_type& alloc = allocator_type())
105       : StringKeyedSetBase(il.begin(), il.end(), comp, alloc) {
106   }
107
108   StringKeyedSetBase& operator=(const StringKeyedSetBase& other) {
109     if (this == &other) {
110       return *this;
111     }
112     return *this = StringKeyedSetBase(other);
113   }
114
115   StringKeyedSetBase& operator=(StringKeyedSetBase&& other) noexcept {
116     assert(this != &other);
117     clear();
118     Base::operator=(std::move(other));
119     assert(other.empty());
120     return *this;
121   }
122
123   using Base::empty;
124   using Base::size;
125   using Base::max_size;
126   using Base::begin;
127   using Base::end;
128   using Base::cbegin;
129   using Base::cend;
130   using Base::find;
131   using Base::count;
132   using Base::lower_bound;
133   using Base::upper_bound;
134
135   bool operator==(StringKeyedSetBase const& other) const {
136     Base const& lhs = *this;
137     Base const& rhs = static_cast<Base const&>(other);
138     return lhs == rhs;
139   }
140
141   template <class... Args>
142   std::pair<iterator, bool> emplace(Args&&... args) {
143     auto key = StringPiece(std::forward<Args>(args)...);
144     auto it = find(key);
145     if (it != end()) {
146       return {it, false};
147     }
148     return Base::emplace(stringPieceDup(key, get_allocator()));
149   }
150
151   std::pair<iterator, bool> insert(value_type val) {
152     auto it = find(val);
153     if (it != end()) {
154       return {it, false};
155     }
156     return Base::insert(stringPieceDup(val, get_allocator()));
157   }
158
159   iterator erase(const_iterator position) {
160     auto key = *position;
161     auto result = Base::erase(position);
162     stringPieceDel(key, get_allocator());
163     return result;
164   }
165
166   size_type erase(StringPiece key) {
167     auto it = find(key);
168     if (it == end()) {
169       return 0;
170     }
171     erase(it);
172     return 1;
173   }
174
175   void clear() noexcept {
176     for (auto it : *this) {
177       stringPieceDel(it, get_allocator());
178     }
179     Base::clear();
180   }
181
182   using Base::get_allocator;
183
184   void swap(StringKeyedSetBase& other) & {
185     return Base::swap(other);
186   }
187
188   ~StringKeyedSetBase() {
189     // Here we assume that set doesn't use keys in destructor
190     for (auto it : *this) {
191       stringPieceDel(it, get_allocator());
192     }
193   }
194 };
195
196 using StringKeyedSet = StringKeyedSetBase<>;
197
198 } // folly