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