Remove duplicates during bulk insertion.
[folly.git] / folly / sorted_vector_types.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
17 /*
18  * This header defines two classes that very nearly model
19  * AssociativeContainer (but not quite).  These implement set-like and
20  * map-like behavior on top of a sorted vector, instead of using
21  * rb-trees like std::set and std::map.
22  *
23  * This is potentially useful in cases where the number of elements in
24  * the set or map is small, or when you want to avoid using more
25  * memory than necessary and insertions/deletions are much more rare
26  * than lookups (these classes have O(N) insertions/deletions).
27  *
28  * In the interest of using these in conditions where the goal is to
29  * minimize memory usage, they support a GrowthPolicy parameter, which
30  * is a class defining a single function called increase_capacity,
31  * which will be called whenever we are about to insert something: you
32  * can then decide to call reserve() based on the current capacity()
33  * and size() of the passed in vector-esque Container type.  An
34  * example growth policy that grows one element at a time:
35  *
36  *    struct OneAtATimePolicy {
37  *      template<class Container>
38  *      void increase_capacity(Container& c) {
39  *        if (c.size() == c.capacity()) {
40  *          c.reserve(c.size() + 1);
41  *        }
42  *      }
43  *    };
44  *
45  *    typedef sorted_vector_set<int,
46  *                              std::less<int>,
47  *                              std::allocator<int>,
48  *                              OneAtATimePolicy>
49  *            OneAtATimeIntSet;
50  *
51  * Important differences from std::set and std::map:
52  *   - insert() and erase() invalidate iterators and references
53  *   - insert() and erase() are O(N)
54  *   - our iterators model RandomAccessIterator
55  *   - sorted_vector_map::value_type is pair<K,V>, not pair<const K,V>.
56  *     (This is basically because we want to store the value_type in
57  *     std::vector<>, which requires it to be Assignable.)
58  */
59
60 #pragma once
61
62 #include <algorithm>
63 #include <initializer_list>
64 #include <iterator>
65 #include <stdexcept>
66 #include <type_traits>
67 #include <utility>
68 #include <vector>
69
70 #include <boost/operators.hpp>
71 #include <folly/portability/BitsFunctexcept.h>
72
73 namespace folly {
74
75 //////////////////////////////////////////////////////////////////////
76
77 namespace detail {
78
79   // This wrapper goes around a GrowthPolicy and provides iterator
80   // preservation semantics, but only if the growth policy is not the
81   // default (i.e. nothing).
82   template<class Policy>
83   struct growth_policy_wrapper : private Policy {
84     template<class Container, class Iterator>
85     Iterator increase_capacity(Container& c, Iterator desired_insertion)
86     {
87       typedef typename Container::difference_type diff_t;
88       diff_t d = desired_insertion - c.begin();
89       Policy::increase_capacity(c);
90       return c.begin() + d;
91     }
92   };
93   template<>
94   struct growth_policy_wrapper<void> {
95     template<class Container, class Iterator>
96     Iterator increase_capacity(Container&, Iterator it) {
97       return it;
98     }
99   };
100
101   /*
102    * This helper returns the distance between two iterators if it is
103    * possible to figure it out without messing up the range
104    * (i.e. unless they are InputIterators).  Otherwise this returns
105    * -1.
106    */
107   template<class Iterator>
108   int distance_if_multipass(Iterator first, Iterator last) {
109     typedef typename std::iterator_traits<Iterator>::iterator_category categ;
110     if (std::is_same<categ,std::input_iterator_tag>::value)
111       return -1;
112     return std::distance(first, last);
113   }
114
115   template<class OurContainer, class Vector, class GrowthPolicy>
116   typename OurContainer::iterator
117   insert_with_hint(OurContainer& sorted,
118                    Vector& cont,
119                    typename OurContainer::iterator hint,
120                    typename OurContainer::value_type&& value,
121                    GrowthPolicy& po)
122   {
123     const typename OurContainer::value_compare& cmp(sorted.value_comp());
124     if (hint == cont.end() || cmp(value, *hint)) {
125       if (hint == cont.begin()) {
126         po.increase_capacity(cont, cont.begin());
127         return cont.insert(cont.begin(), std::move(value));
128       }
129       if (cmp(*(hint - 1), value)) {
130         hint = po.increase_capacity(cont, hint);
131         return cont.insert(hint, std::move(value));
132       }
133       return sorted.insert(std::move(value)).first;
134     }
135
136     if (cmp(*hint, value)) {
137       if (hint + 1 == cont.end() || cmp(value, *(hint + 1))) {
138         typename OurContainer::iterator it =
139           po.increase_capacity(cont, hint + 1);
140         return cont.insert(it, std::move(value));
141       }
142     }
143
144     // Value and *hint did not compare, so they are equal keys.
145     return hint;
146   }
147
148   template <class OurContainer, class Vector, class InputIterator>
149   void bulk_insert(
150       OurContainer& sorted,
151       Vector& cont,
152       InputIterator first,
153       InputIterator last) {
154     // prevent deref of middle where middle == cont.end()
155     if (first == last) {
156       return;
157     }
158
159     auto const& cmp(sorted.value_comp());
160
161     int const d = distance_if_multipass(first, last);
162     if (d != -1) {
163       cont.reserve(cont.size() + d);
164     }
165     auto const prev_size = cont.size();
166
167     std::copy(first, last, std::back_inserter(cont));
168     auto const middle = cont.begin() + prev_size;
169     if (!std::is_sorted(middle, cont.end(), cmp)) {
170       std::sort(middle, cont.end(), cmp);
171     }
172     if (middle != cont.begin() && cmp(*middle, *(middle - 1))) {
173       std::inplace_merge(cont.begin(), middle, cont.end(), cmp);
174       auto last = std::unique(
175           cont.begin(),
176           cont.end(),
177           [&](typename OurContainer::value_type const& a,
178               typename OurContainer::value_type const& b) {
179             return !cmp(a, b) && !cmp(b, a);
180           });
181       cont.erase(last, cont.end());
182     }
183   }
184 }
185
186 //////////////////////////////////////////////////////////////////////
187
188 /**
189  * A sorted_vector_set is a container similar to std::set<>, but
190  * implemented as as a sorted array with std::vector<>.
191  *
192  * @param class T               Data type to store
193  * @param class Compare         Comparison function that imposes a
194  *                              strict weak ordering over instances of T
195  * @param class Allocator       allocation policy
196  * @param class GrowthPolicy    policy object to control growth
197  *
198  * @author Aditya Agarwal <aditya@fb.com>
199  * @author Akhil Wable    <akhil@fb.com>
200  * @author Jordan DeLong  <delong.j@fb.com>
201  */
202 template<class T,
203          class Compare      = std::less<T>,
204          class Allocator    = std::allocator<T>,
205          class GrowthPolicy = void>
206 class sorted_vector_set
207   : boost::totally_ordered1<
208       sorted_vector_set<T,Compare,Allocator,GrowthPolicy>
209     , detail::growth_policy_wrapper<GrowthPolicy> >
210 {
211   typedef std::vector<T,Allocator> ContainerT;
212
213   detail::growth_policy_wrapper<GrowthPolicy>&
214   get_growth_policy() { return *this; }
215
216 public:
217   typedef T       value_type;
218   typedef T       key_type;
219   typedef Compare key_compare;
220   typedef Compare value_compare;
221
222   typedef typename ContainerT::pointer                pointer;
223   typedef typename ContainerT::reference              reference;
224   typedef typename ContainerT::const_reference        const_reference;
225   /*
226    * XXX: Our normal iterator ought to also be a constant iterator
227    * (cf. Defect Report 103 for std::set), but this is a bit more of a
228    * pain.
229    */
230   typedef typename ContainerT::iterator               iterator;
231   typedef typename ContainerT::const_iterator         const_iterator;
232   typedef typename ContainerT::difference_type        difference_type;
233   typedef typename ContainerT::size_type              size_type;
234   typedef typename ContainerT::reverse_iterator       reverse_iterator;
235   typedef typename ContainerT::const_reverse_iterator const_reverse_iterator;
236
237   explicit sorted_vector_set(const Compare& comp = Compare(),
238                              const Allocator& alloc = Allocator())
239     : m_(comp, alloc)
240   {}
241
242   template<class InputIterator>
243   explicit sorted_vector_set(
244       InputIterator first,
245       InputIterator last,
246       const Compare& comp = Compare(),
247       const Allocator& alloc = Allocator())
248     : m_(comp, alloc)
249   {
250     // This is linear if [first, last) is already sorted (and if we
251     // can figure out the distance between the two iterators).
252     insert(first, last);
253   }
254
255   /* implicit */ sorted_vector_set(
256       std::initializer_list<value_type> list,
257       const Compare& comp = Compare(),
258       const Allocator& alloc = Allocator())
259     : m_(comp, alloc)
260   {
261     insert(list.begin(), list.end());
262   }
263
264   // Construct a sorted_vector_set by stealing the storage of a prefilled
265   // container. The container need not be sorted already. This supports
266   // bulk construction of sorted_vector_set with zero allocations, not counting
267   // those performed by the caller. (The iterator range constructor performs at
268   // least one allocation).
269   //
270   // Note that `sorted_vector_set(const ContainerT& container)` is not provided,
271   // since the purpose of this constructor is to avoid an unnecessary copy.
272   explicit sorted_vector_set(
273       ContainerT&& container,
274       const Compare& comp = Compare())
275       : m_(comp, container.get_allocator()) {
276     std::sort(container.begin(), container.end(), value_comp());
277     m_.cont_.swap(container);
278   }
279
280   key_compare key_comp() const { return m_; }
281   value_compare value_comp() const { return m_; }
282
283   iterator begin()                      { return m_.cont_.begin();  }
284   iterator end()                        { return m_.cont_.end();    }
285   const_iterator cbegin() const         { return m_.cont_.cbegin(); }
286   const_iterator begin() const          { return m_.cont_.begin();  }
287   const_iterator cend() const           { return m_.cont_.cend();   }
288   const_iterator end() const            { return m_.cont_.end();    }
289   reverse_iterator rbegin()             { return m_.cont_.rbegin(); }
290   reverse_iterator rend()               { return m_.cont_.rend();   }
291   const_reverse_iterator rbegin() const { return m_.cont_.rbegin(); }
292   const_reverse_iterator rend() const   { return m_.cont_.rend();   }
293
294   void clear()                  { return m_.cont_.clear();    }
295   size_type size() const        { return m_.cont_.size();     }
296   size_type max_size() const    { return m_.cont_.max_size(); }
297   bool empty() const            { return m_.cont_.empty();    }
298   void reserve(size_type s)     { return m_.cont_.reserve(s); }
299   void shrink_to_fit()          { m_.cont_.shrink_to_fit();   }
300   size_type capacity() const    { return m_.cont_.capacity(); }
301
302   std::pair<iterator,bool> insert(const value_type& value) {
303     return insert(std::move(value_type(value)));
304   }
305
306   std::pair<iterator,bool> insert(value_type&& value) {
307     iterator it = lower_bound(value);
308     if (it == end() || value_comp()(value, *it)) {
309       it = get_growth_policy().increase_capacity(m_.cont_, it);
310       return std::make_pair(m_.cont_.insert(it, std::move(value)), true);
311     }
312     return std::make_pair(it, false);
313   }
314
315   iterator insert(iterator hint, const value_type& value) {
316     return insert(hint, std::move(value_type(value)));
317   }
318
319   iterator insert(iterator hint, value_type&& value) {
320     return detail::insert_with_hint(*this, m_.cont_, hint, std::move(value),
321       get_growth_policy());
322   }
323
324   template<class InputIterator>
325   void insert(InputIterator first, InputIterator last) {
326     detail::bulk_insert(*this, m_.cont_, first, last);
327   }
328
329   size_type erase(const key_type& key) {
330     iterator it = find(key);
331     if (it == end()) {
332       return 0;
333     }
334     m_.cont_.erase(it);
335     return 1;
336   }
337
338   void erase(iterator it) {
339     m_.cont_.erase(it);
340   }
341
342   void erase(iterator first, iterator last) {
343     m_.cont_.erase(first, last);
344   }
345
346   iterator find(const key_type& key) {
347     iterator it = lower_bound(key);
348     if (it == end() || !key_comp()(key, *it))
349       return it;
350     return end();
351   }
352
353   const_iterator find(const key_type& key) const {
354     const_iterator it = lower_bound(key);
355     if (it == end() || !key_comp()(key, *it))
356       return it;
357     return end();
358   }
359
360   size_type count(const key_type& key) const {
361     return find(key) == end() ? 0 : 1;
362   }
363
364   iterator lower_bound(const key_type& key) {
365     return std::lower_bound(begin(), end(), key, key_comp());
366   }
367
368   const_iterator lower_bound(const key_type& key) const {
369     return std::lower_bound(begin(), end(), key, key_comp());
370   }
371
372   iterator upper_bound(const key_type& key) {
373     return std::upper_bound(begin(), end(), key, key_comp());
374   }
375
376   const_iterator upper_bound(const key_type& key) const {
377     return std::upper_bound(begin(), end(), key, key_comp());
378   }
379
380   std::pair<iterator,iterator> equal_range(const key_type& key) {
381     return std::equal_range(begin(), end(), key, key_comp());
382   }
383
384   std::pair<const_iterator,const_iterator>
385   equal_range(const key_type& key) const {
386     return std::equal_range(begin(), end(), key, key_comp());
387   }
388
389   // Nothrow as long as swap() on the Compare type is nothrow.
390   void swap(sorted_vector_set& o) {
391     using std::swap;  // Allow ADL for swap(); fall back to std::swap().
392     Compare& a = m_;
393     Compare& b = o.m_;
394     swap(a, b);
395     m_.cont_.swap(o.m_.cont_);
396   }
397
398   bool operator==(const sorted_vector_set& other) const {
399     return other.m_.cont_ == m_.cont_;
400   }
401
402   bool operator<(const sorted_vector_set& other) const {
403     return m_.cont_ < other.m_.cont_;
404   }
405
406 private:
407   /*
408    * This structure derives from the comparison object in order to
409    * make use of the empty base class optimization if our comparison
410    * functor is an empty class (usual case).
411    *
412    * Wrapping up this member like this is better than deriving from
413    * the Compare object ourselves (there are some perverse edge cases
414    * involving virtual functions).
415    *
416    * More info:  http://www.cantrip.org/emptyopt.html
417    */
418   struct EBO : Compare {
419     explicit EBO(const Compare& c, const Allocator& alloc)
420       : Compare(c)
421       , cont_(alloc)
422     {}
423     ContainerT cont_;
424   } m_;
425 };
426
427 // Swap function that can be found using ADL.
428 template<class T, class C, class A, class G>
429 inline void swap(sorted_vector_set<T,C,A,G>& a,
430                  sorted_vector_set<T,C,A,G>& b) {
431   return a.swap(b);
432 }
433
434 //////////////////////////////////////////////////////////////////////
435
436 /**
437  * A sorted_vector_map is similar to a sorted_vector_set but stores
438  * <key,value> pairs instead of single elements.
439  *
440  * @param class Key           Key type
441  * @param class Value         Value type
442  * @param class Compare       Function that can compare key types and impose
443  *                            a strict weak ordering over them.
444  * @param class Allocator     allocation policy
445  * @param class GrowthPolicy  policy object to control growth
446  *
447  * @author Aditya Agarwal <aditya@fb.com>
448  * @author Akhil Wable    <akhil@fb.com>
449  * @author Jordan DeLong  <delong.j@fb.com>
450  */
451 template<class Key,
452          class Value,
453          class Compare        = std::less<Key>,
454          class Allocator      = std::allocator<std::pair<Key,Value> >,
455          class GrowthPolicy   = void>
456 class sorted_vector_map
457   : boost::totally_ordered1<
458       sorted_vector_map<Key,Value,Compare,Allocator,GrowthPolicy>
459     , detail::growth_policy_wrapper<GrowthPolicy> >
460 {
461   typedef std::vector<std::pair<Key,Value>,Allocator> ContainerT;
462
463   detail::growth_policy_wrapper<GrowthPolicy>&
464   get_growth_policy() { return *this; }
465
466 public:
467   typedef Key                                       key_type;
468   typedef Value                                     mapped_type;
469   typedef std::pair<key_type,mapped_type>           value_type;
470   typedef Compare                                   key_compare;
471
472   struct value_compare : private Compare {
473     bool operator()(const value_type& a, const value_type& b) const {
474       return Compare::operator()(a.first, b.first);
475     }
476
477   protected:
478     friend class sorted_vector_map;
479     explicit value_compare(const Compare& c) : Compare(c) {}
480   };
481
482   typedef typename ContainerT::pointer                pointer;
483   typedef typename ContainerT::reference              reference;
484   typedef typename ContainerT::const_reference        const_reference;
485   typedef typename ContainerT::iterator               iterator;
486   typedef typename ContainerT::const_iterator         const_iterator;
487   typedef typename ContainerT::difference_type        difference_type;
488   typedef typename ContainerT::size_type              size_type;
489   typedef typename ContainerT::reverse_iterator       reverse_iterator;
490   typedef typename ContainerT::const_reverse_iterator const_reverse_iterator;
491
492   explicit sorted_vector_map(const Compare& comp = Compare(),
493                              const Allocator& alloc = Allocator())
494     : m_(value_compare(comp), alloc)
495   {}
496
497   template<class InputIterator>
498   explicit sorted_vector_map(
499       InputIterator first,
500       InputIterator last,
501       const Compare& comp = Compare(),
502       const Allocator& alloc = Allocator())
503     : m_(value_compare(comp), alloc)
504   {
505     insert(first, last);
506   }
507
508   explicit sorted_vector_map(
509       std::initializer_list<value_type> list,
510       const Compare& comp = Compare(),
511       const Allocator& alloc = Allocator())
512     : m_(value_compare(comp), alloc)
513   {
514     insert(list.begin(), list.end());
515   }
516
517   // Construct a sorted_vector_map by stealing the storage of a prefilled
518   // container. The container need not be sorted already. This supports
519   // bulk construction of sorted_vector_map with zero allocations, not counting
520   // those performed by the caller. (The iterator range constructor performs at
521   // least one allocation).
522   //
523   // Note that `sorted_vector_map(const ContainerT& container)` is not provided,
524   // since the purpose of this constructor is to avoid an unnecessary copy.
525   explicit sorted_vector_map(
526       ContainerT&& container,
527       const Compare& comp = Compare())
528       : m_(value_compare(comp), container.get_allocator()) {
529     std::sort(container.begin(), container.end(), value_comp());
530     m_.cont_.swap(container);
531   }
532
533   key_compare key_comp() const { return m_; }
534   value_compare value_comp() const { return m_; }
535
536   iterator begin()                      { return m_.cont_.begin();  }
537   iterator end()                        { return m_.cont_.end();    }
538   const_iterator cbegin() const         { return m_.cont_.cbegin(); }
539   const_iterator begin() const          { return m_.cont_.begin();  }
540   const_iterator cend() const           { return m_.cont_.cend();   }
541   const_iterator end() const            { return m_.cont_.end();    }
542   reverse_iterator rbegin()             { return m_.cont_.rbegin(); }
543   reverse_iterator rend()               { return m_.cont_.rend();   }
544   const_reverse_iterator rbegin() const { return m_.cont_.rbegin(); }
545   const_reverse_iterator rend() const   { return m_.cont_.rend();   }
546
547   void clear()                  { return m_.cont_.clear();    }
548   size_type size() const        { return m_.cont_.size();     }
549   size_type max_size() const    { return m_.cont_.max_size(); }
550   bool empty() const            { return m_.cont_.empty();    }
551   void reserve(size_type s)     { return m_.cont_.reserve(s); }
552   void shrink_to_fit()          { m_.cont_.shrink_to_fit();   }
553   size_type capacity() const    { return m_.cont_.capacity(); }
554
555   std::pair<iterator,bool> insert(const value_type& value) {
556     return insert(std::move(value_type(value)));
557   }
558
559   std::pair<iterator,bool> insert(value_type&& value) {
560     iterator it = lower_bound(value.first);
561     if (it == end() || value_comp()(value, *it)) {
562       it = get_growth_policy().increase_capacity(m_.cont_, it);
563       return std::make_pair(m_.cont_.insert(it, std::move(value)), true);
564     }
565     return std::make_pair(it, false);
566   }
567
568   iterator insert(iterator hint, const value_type& value) {
569     return insert(hint, std::move(value_type(value)));
570   }
571
572   iterator insert(iterator hint, value_type&& value) {
573     return detail::insert_with_hint(*this, m_.cont_, hint, std::move(value),
574       get_growth_policy());
575   }
576
577   template<class InputIterator>
578   void insert(InputIterator first, InputIterator last) {
579     detail::bulk_insert(*this, m_.cont_, first, last);
580   }
581
582   size_type erase(const key_type& key) {
583     iterator it = find(key);
584     if (it == end()) {
585       return 0;
586     }
587     m_.cont_.erase(it);
588     return 1;
589   }
590
591   void erase(iterator it) {
592     m_.cont_.erase(it);
593   }
594
595   void erase(iterator first, iterator last) {
596     m_.cont_.erase(first, last);
597   }
598
599   iterator find(const key_type& key) {
600     iterator it = lower_bound(key);
601     if (it == end() || !key_comp()(key, it->first))
602       return it;
603     return end();
604   }
605
606   const_iterator find(const key_type& key) const {
607     const_iterator it = lower_bound(key);
608     if (it == end() || !key_comp()(key, it->first))
609       return it;
610     return end();
611   }
612
613   mapped_type& at(const key_type& key) {
614     iterator it = find(key);
615     if (it != end()) {
616       return it->second;
617     }
618     std::__throw_out_of_range("sorted_vector_map::at");
619   }
620
621   const mapped_type& at(const key_type& key) const {
622     const_iterator it = find(key);
623     if (it != end()) {
624       return it->second;
625     }
626     std::__throw_out_of_range("sorted_vector_map::at");
627   }
628
629   size_type count(const key_type& key) const {
630     return find(key) == end() ? 0 : 1;
631   }
632
633   iterator lower_bound(const key_type& key) {
634     auto c = key_comp();
635     auto f = [&](const value_type& a, const key_type& b) {
636       return c(a.first, b);
637     };
638     return std::lower_bound(begin(), end(), key, f);
639   }
640
641   const_iterator lower_bound(const key_type& key) const {
642     auto c = key_comp();
643     auto f = [&](const value_type& a, const key_type& b) {
644       return c(a.first, b);
645     };
646     return std::lower_bound(begin(), end(), key, f);
647   }
648
649   iterator upper_bound(const key_type& key) {
650     auto c = key_comp();
651     auto f = [&](const key_type& a, const value_type& b) {
652       return c(a, b.first);
653     };
654     return std::upper_bound(begin(), end(), key, f);
655   }
656
657   const_iterator upper_bound(const key_type& key) const {
658     auto c = key_comp();
659     auto f = [&](const key_type& a, const value_type& b) {
660       return c(a, b.first);
661     };
662     return std::upper_bound(begin(), end(), key, f);
663   }
664
665   std::pair<iterator,iterator> equal_range(const key_type& key) {
666     // Note: std::equal_range can't be passed a functor that takes
667     // argument types different from the iterator value_type, so we
668     // have to do this.
669     iterator low = lower_bound(key);
670     auto c = key_comp();
671     auto f = [&](const key_type& a, const value_type& b) {
672       return c(a, b.first);
673     };
674     iterator high = std::upper_bound(low, end(), key, f);
675     return std::make_pair(low, high);
676   }
677
678   std::pair<const_iterator,const_iterator>
679   equal_range(const key_type& key) const {
680     return const_cast<sorted_vector_map*>(this)->equal_range(key);
681   }
682
683   // Nothrow as long as swap() on the Compare type is nothrow.
684   void swap(sorted_vector_map& o) {
685     using std::swap; // Allow ADL for swap(); fall back to std::swap().
686     Compare& a = m_;
687     Compare& b = o.m_;
688     swap(a, b);
689     m_.cont_.swap(o.m_.cont_);
690   }
691
692   mapped_type& operator[](const key_type& key) {
693     iterator it = lower_bound(key);
694     if (it == end() || key_comp()(key, it->first)) {
695       return insert(it, value_type(key, mapped_type()))->second;
696     }
697     return it->second;
698   }
699
700   bool operator==(const sorted_vector_map& other) const {
701     return m_.cont_ == other.m_.cont_;
702   }
703
704   bool operator<(const sorted_vector_map& other) const {
705     return m_.cont_ < other.m_.cont_;
706   }
707
708 private:
709   // This is to get the empty base optimization; see the comment in
710   // sorted_vector_set.
711   struct EBO : value_compare {
712     explicit EBO(const value_compare& c, const Allocator& alloc)
713       : value_compare(c)
714       , cont_(alloc)
715     {}
716     ContainerT cont_;
717   } m_;
718 };
719
720 // Swap function that can be found using ADL.
721 template<class K, class V, class C, class A, class G>
722 inline void swap(sorted_vector_map<K,V,C,A,G>& a,
723                  sorted_vector_map<K,V,C,A,G>& b) {
724   return a.swap(b);
725 }
726
727 //////////////////////////////////////////////////////////////////////
728
729 }