2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2014 Attila Molnar <attilamolnar@hush.com>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 template <typename T, typename Comp>
31 class map_pair_compare : public Comp
34 typedef typename value_type::first_type key_type;
37 bool operator()(const value_type& x, const value_type& y) const
39 return Comp::operator()(x.first, y.first);
42 bool operator()(const value_type& x, const key_type& y) const
44 return Comp::operator()(x.first, y);
47 bool operator()(const key_type& x, const value_type& y) const
49 return Comp::operator()(x, y.first);
53 template <typename Val, typename Comp>
54 class map_value_compare : public std::binary_function<Val, Val, bool>
57 // Constructor should be private
59 bool operator()(const Val& x, const Val& y) const
62 return c(x.first, y.first);
66 template <typename T, typename Comp, typename Key = T, typename ElementComp = Comp>
70 typedef std::vector<T> storage_type;
74 typedef typename storage_type::iterator iterator;
75 typedef typename storage_type::const_iterator const_iterator;
76 typedef typename storage_type::reverse_iterator reverse_iterator;
77 typedef typename storage_type::const_reverse_iterator const_reverse_iterator;
79 typedef typename storage_type::size_type size_type;
80 typedef typename storage_type::difference_type difference_type;
84 typedef Comp key_compare;
85 typedef ElementComp value_compare;
89 flat_map_base(const flat_map_base& other)
94 size_type size() const { return vect.size(); }
95 bool empty() const { return vect.empty(); }
96 size_type capacity() const { return vect.capacity(); }
97 size_type max_size() const { return vect.max_size(); }
99 void clear() { vect.clear(); }
100 void reserve(size_type n) { vect.reserve(n); }
102 iterator begin() { return vect.begin(); }
103 iterator end() { return vect.end(); }
104 reverse_iterator rbegin() { return vect.rbegin(); }
105 reverse_iterator rend() { return vect.rend(); }
107 const_iterator begin() const { return vect.begin(); }
108 const_iterator end() const { return vect.end(); }
109 const_reverse_iterator rbegin() const { return vect.rbegin(); }
110 const_reverse_iterator rend() const { return vect.rend(); }
112 key_compare key_comp() const { return Comp(); }
114 iterator erase(iterator it) { return vect.erase(it); }
115 iterator erase(iterator first, iterator last) { return vect.erase(first, last); }
116 size_type erase(const key_type& x)
118 size_type n = vect.size();
119 std::pair<iterator, iterator> itpair = equal_range(x);
120 vect.erase(itpair.first, itpair.second);
121 return n - vect.size();
124 iterator find(const key_type& x)
127 iterator it = std::lower_bound(vect.begin(), vect.end(), x, c);
128 if ((it != vect.end()) && (!c(x, *it)))
133 const_iterator find(const key_type& x) const
135 // Same as above but this time we return a const_iterator
137 const_iterator it = std::lower_bound(vect.begin(), vect.end(), x, c);
138 if ((it != vect.end()) && (!c(x, *it)))
143 std::pair<iterator, iterator> equal_range(const key_type& x)
145 return std::equal_range(vect.begin(), vect.end(), x, value_compare());
148 std::pair<const_iterator, const_iterator> equal_range(const key_type& x) const
150 return std::equal_range(vect.begin(), vect.end(), x, value_compare());
153 iterator lower_bound(const key_type& x)
155 return std::lower_bound(vect.begin(), vect.end(), x, value_compare());
158 const_iterator lower_bound(const key_type& x) const
160 return std::lower_bound(vect.begin(), vect.end(), x, value_compare());
163 iterator upper_bound(const key_type& x)
165 return std::upper_bound(vect.begin(), vect.end(), x, value_compare());
168 const_iterator upper_bound(const key_type& x) const
170 return std::upper_bound(vect.begin(), vect.end(), x, value_compare());
173 size_type count(const key_type& x) const
175 std::pair<const_iterator, const_iterator> itpair = equal_range(x);
176 return std::distance(itpair.first, itpair.second);
180 std::pair<iterator, bool> insert_single(const value_type& x)
182 bool inserted = false;
185 iterator it = std::lower_bound(vect.begin(), vect.end(), x, c);
186 if ((it == vect.end()) || (c(x, *it)))
189 it = vect.insert(it, x);
191 return std::make_pair(it, inserted);
194 iterator insert_multi(const value_type& x)
196 iterator it = std::lower_bound(vect.begin(), vect.end(), x, value_compare());
197 return vect.insert(it, x);
201 } // namespace detail
203 template <typename T, typename Comp = std::less<T>, typename ElementComp = Comp>
204 class flat_set : public detail::flat_map_base<T, Comp, T, ElementComp>
206 typedef detail::flat_map_base<T, Comp, T, ElementComp> base_t;
209 typedef typename base_t::iterator iterator;
210 typedef typename base_t::value_type value_type;
214 template <typename InputIterator>
215 flat_set(InputIterator first, InputIterator last)
217 this->insert(first, last);
220 flat_set(const flat_set& other)
225 std::pair<iterator, bool> insert(const value_type& x)
227 return this->insert_single(x);
230 template <typename InputIterator>
231 void insert(InputIterator first, InputIterator last)
233 for (; first != last; ++first)
234 this->insert_single(*first);
237 void swap(flat_set& other)
239 base_t::vect.swap(other.vect);
243 template <typename T, typename Comp = std::less<T>, typename ElementComp = Comp>
244 class flat_multiset : public detail::flat_map_base<T, Comp, T, ElementComp>
246 typedef detail::flat_map_base<T, Comp, T, ElementComp> base_t;
249 typedef typename base_t::iterator iterator;
250 typedef typename base_t::value_type value_type;
254 template <typename InputIterator>
255 flat_multiset(InputIterator first, InputIterator last)
257 this->insert(first, last);
260 flat_multiset(const flat_multiset& other)
265 iterator insert(const value_type& x)
267 return this->insert_multi(x);
270 template <typename InputIterator>
271 void insert(InputIterator first, InputIterator last)
273 for (; first != last; ++first)
274 insert_multi(*first);
277 void swap(flat_multiset& other)
279 base_t::vect.swap(other.vect);
283 template <typename T, typename U, typename Comp = std::less<T>, typename ElementComp = Comp >
284 class flat_map : public detail::flat_map_base<std::pair<T, U>, Comp, T, detail::map_pair_compare<std::pair<T, U>, ElementComp> >
286 typedef detail::flat_map_base<std::pair<T, U>, Comp, T, detail::map_pair_compare<std::pair<T, U>, ElementComp> > base_t;
289 typedef typename base_t::iterator iterator;
290 typedef typename base_t::key_type key_type;
291 typedef typename base_t::value_type value_type;
292 typedef U mapped_type;
293 typedef typename base_t::value_compare value_compare;
297 template <typename InputIterator>
298 flat_map(InputIterator first, InputIterator last)
303 flat_map(const flat_map& other)
308 std::pair<iterator, bool> insert(const value_type& x)
310 return this->insert_single(x);
313 template <typename InputIterator>
314 void insert(InputIterator first, InputIterator last)
316 for (; first != last; ++first)
317 this->insert_single(*first);
320 void swap(flat_map& other)
322 base_t::vect.swap(other.vect);
325 mapped_type& operator[](const key_type& x)
327 return insert(std::make_pair(x, mapped_type())).first->second;
330 value_compare value_comp() const
332 return value_compare();
336 template <typename T, typename U, typename Comp = std::less<T>, typename ElementComp = Comp >
337 class flat_multimap : public detail::flat_map_base<std::pair<T, U>, Comp, T, detail::map_pair_compare<std::pair<T, U>, ElementComp> >
339 typedef detail::flat_map_base<std::pair<T, U>, Comp, T, detail::map_pair_compare<std::pair<T, U>, ElementComp> > base_t;
342 typedef typename base_t::iterator iterator;
343 typedef typename base_t::value_type value_type;
344 typedef U mapped_type;
345 typedef typename base_t::value_compare value_compare;
349 template <typename InputIterator>
350 flat_multimap(InputIterator first, InputIterator last)
352 this->insert(first, last);
355 flat_multimap(const flat_multimap& other)
360 iterator insert(const value_type& x)
362 return this->insert_multi(x);
365 template <typename InputIterator>
366 void insert(InputIterator first, InputIterator last)
368 for (; first != last; ++first)
369 this->insert_multi(*first);
372 void swap(flat_multimap& other)
374 base_t::vect.swap(other.vect);
377 value_compare value_comp() const
379 return value_compare();