PageRenderTime 77ms CodeModel.GetById 15ms app.highlight 53ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/interprocess/containers/container/detail/flat_tree.hpp

http://hadesmem.googlecode.com/
C++ Header | 875 lines | 642 code | 128 blank | 105 comment | 50 complexity | f6900d2f5796d69122f1bc74c5f118a9 MD5 | raw file
  1////////////////////////////////////////////////////////////////////////////////
  2//
  3// (C) Copyright Ion Gaztanaga 2005-2009. Distributed under the Boost
  4// Software License, Version 1.0. (See accompanying file
  5// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6//
  7// See http://www.boost.org/libs/container for documentation.
  8//
  9////////////////////////////////////////////////////////////////////////////////
 10// The Loki Library
 11// Copyright (c) 2001 by Andrei Alexandrescu
 12// This code accompanies the book:
 13// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design 
 14//     Patterns Applied". Copyright (c) 2001. Addison-Wesley.
 15// Permission to use, copy, modify, distribute and sell this software for any 
 16//     purpose is hereby granted without fee, provided that the above copyright 
 17//     notice appear in all copies and that both that copyright notice and this 
 18//     permission notice appear in supporting documentation.
 19// The author or Addison-Welsey Longman make no representations about the 
 20//     suitability of this software for any purpose. It is provided "as is" 
 21//     without express or implied warranty.
 22///////////////////////////////////////////////////////////////////////////////
 23//
 24// Parts of this file come from AssocVector.h file from Loki library
 25//
 26////////////////////////////////////////////////////////////////////////////////
 27
 28#ifndef BOOST_CONTAINERS_FLAT_TREE_HPP
 29#define BOOST_CONTAINERS_FLAT_TREE_HPP
 30
 31#if (defined _MSC_VER) && (_MSC_VER >= 1200)
 32#  pragma once
 33#endif
 34
 35#include "config_begin.hpp"
 36#include INCLUDE_BOOST_CONTAINER_DETAIL_WORKAROUND_HPP
 37
 38#include INCLUDE_BOOST_CONTAINER_CONTAINER_FWD_HPP
 39
 40#include <algorithm>
 41#include <functional>
 42#include <utility>
 43
 44#include <boost/type_traits/has_trivial_destructor.hpp>
 45#include INCLUDE_BOOST_CONTAINER_MOVE_HPP
 46
 47#include INCLUDE_BOOST_CONTAINER_DETAIL_UTILITIES_HPP
 48#include INCLUDE_BOOST_CONTAINER_DETAIL_PAIR_HPP
 49#include INCLUDE_BOOST_CONTAINER_VECTOR_HPP
 50#include INCLUDE_BOOST_CONTAINER_DETAIL_VALUE_INIT_HPP
 51#include INCLUDE_BOOST_CONTAINER_DETAIL_DESTROYERS_HPP
 52
 53namespace boost {
 54
 55namespace container {
 56
 57namespace containers_detail {
 58
 59template<class Compare, class Value, class KeyOfValue>
 60class flat_tree_value_compare
 61   : private Compare
 62{
 63   typedef Value              first_argument_type;
 64   typedef Value              second_argument_type;
 65   typedef bool               return_type;
 66   public:     
 67   flat_tree_value_compare(const Compare &pred) 
 68      : Compare(pred)
 69   {}
 70
 71   bool operator()(const Value& lhs, const Value& rhs) const
 72   { 
 73      KeyOfValue key_extract;
 74      return Compare::operator()(key_extract(lhs), key_extract(rhs)); 
 75   }
 76
 77   const Compare &get_comp() const
 78      {  return *this;  }
 79   
 80   Compare &get_comp()
 81      {  return *this;  }
 82};
 83
 84template<class Pointer>
 85struct get_flat_tree_iterators
 86{
 87   typedef typename containers_detail::
 88      vector_iterator<Pointer>                        iterator;
 89   typedef typename containers_detail::
 90      vector_const_iterator<Pointer>                  const_iterator;
 91   typedef std::reverse_iterator<iterator>            reverse_iterator;
 92   typedef std::reverse_iterator<const_iterator>      const_reverse_iterator;
 93};
 94
 95template <class Key, class Value, class KeyOfValue, 
 96          class Compare, class Alloc>
 97class flat_tree
 98{
 99   typedef boost::container::vector<Value, Alloc>  vector_t;
100   typedef Alloc                                   allocator_t;
101
102   public:
103   typedef flat_tree_value_compare<Compare, Value, KeyOfValue> value_compare;
104
105 private:
106   struct Data 
107      //Inherit from value_compare to do EBO
108      : public value_compare
109   {
110      private:
111      BOOST_MOVE_MACRO_COPYABLE_AND_MOVABLE(Data)
112      public:
113      Data(const Data &d)
114         : value_compare(d), m_vect(d.m_vect)
115      {}
116      Data(const Compare &comp,
117           const vector_t &vect) 
118         : value_compare(comp), m_vect(vect){}
119
120      Data(const value_compare &comp,
121           const vector_t &vect) 
122         : value_compare(comp), m_vect(vect){}
123
124      Data(const Compare &comp,
125           const allocator_t &alloc) 
126         : value_compare(comp), m_vect(alloc){}
127
128      Data& operator=(BOOST_MOVE_MACRO_COPY_ASSIGN_REF(Data) d)
129      {
130         this->value_compare::operator=(d);
131         m_vect = d.m_vect;
132         return *this;
133      }
134
135      Data& operator=(BOOST_MOVE_MACRO_RV_REF(Data) d)
136      {
137         this->value_compare::operator=(BOOST_CONTAINER_MOVE_NAMESPACE::move(static_cast<value_compare &>(d)));
138         m_vect = BOOST_CONTAINER_MOVE_NAMESPACE::move(d.m_vect);
139         return *this;
140      }
141
142      vector_t m_vect;
143   };
144
145   Data m_data;
146   BOOST_MOVE_MACRO_COPYABLE_AND_MOVABLE(flat_tree)
147
148   public:
149
150   typedef typename vector_t::value_type              value_type;
151   typedef typename vector_t::pointer                 pointer;
152   typedef typename vector_t::const_pointer           const_pointer;
153   typedef typename vector_t::reference               reference;
154   typedef typename vector_t::const_reference         const_reference;
155   typedef Key                                        key_type;
156   typedef Compare                                    key_compare;
157   typedef typename vector_t::allocator_type          allocator_type;
158   typedef allocator_type                             stored_allocator_type;
159   typedef typename allocator_type::size_type         size_type;
160   typedef typename allocator_type::difference_type   difference_type;
161   typedef typename vector_t::iterator                iterator;
162   typedef typename vector_t::const_iterator          const_iterator;
163   typedef std::reverse_iterator<iterator>            reverse_iterator;
164   typedef std::reverse_iterator<const_iterator>      const_reverse_iterator;
165   
166
167   // allocation/deallocation
168   flat_tree(const Compare& comp     = Compare(), 
169             const allocator_type& a = allocator_type())
170      : m_data(comp, a)
171   { }
172
173   flat_tree(const flat_tree& x) 
174      :  m_data(x.m_data, x.m_data.m_vect)
175   { }
176
177   flat_tree(BOOST_MOVE_MACRO_RV_REF(flat_tree) x)
178      :  m_data(BOOST_CONTAINER_MOVE_NAMESPACE::move(x.m_data))
179   { }
180
181   template <class InputIterator>
182   flat_tree( ordered_range_t, InputIterator first, InputIterator last
183            , const Compare& comp     = Compare()
184            , const allocator_type& a = allocator_type())
185      : m_data(comp, a)
186   { this->m_data.m_vect.insert(this->m_data.m_vect.end(), first, last); }
187
188   ~flat_tree()
189   { }
190
191   flat_tree&  operator=(BOOST_MOVE_MACRO_COPY_ASSIGN_REF(flat_tree) x)
192   {  m_data = x.m_data;   return *this;  }
193
194   flat_tree&  operator=(BOOST_MOVE_MACRO_RV_REF(flat_tree) mx)
195   {  m_data = BOOST_CONTAINER_MOVE_NAMESPACE::move(mx.m_data); return *this;  }
196
197   public:    
198   // accessors:
199   Compare key_comp() const 
200   { return this->m_data.get_comp(); }
201
202   allocator_type get_allocator() const 
203   { return this->m_data.m_vect.get_allocator(); }
204
205   const stored_allocator_type &get_stored_allocator() const 
206   {  return this->m_data.m_vect.get_stored_allocator(); }
207
208   stored_allocator_type &get_stored_allocator()
209   {  return this->m_data.m_vect.get_stored_allocator(); }
210
211   iterator begin() 
212   { return this->m_data.m_vect.begin(); }
213
214   const_iterator begin() const 
215   { return this->cbegin(); }
216
217   const_iterator cbegin() const 
218   { return this->m_data.m_vect.begin(); }
219
220   iterator end() 
221   { return this->m_data.m_vect.end(); }
222
223   const_iterator end() const 
224   { return this->cend(); }
225
226   const_iterator cend() const 
227   { return this->m_data.m_vect.end(); }
228
229   reverse_iterator rbegin() 
230   { return reverse_iterator(this->end()); }
231
232   const_reverse_iterator rbegin() const 
233   {  return this->crbegin();  }
234
235   const_reverse_iterator crbegin() const 
236   {  return const_reverse_iterator(this->cend());  }
237
238   reverse_iterator rend() 
239   { return reverse_iterator(this->begin()); }
240
241   const_reverse_iterator rend() const 
242   { return this->crend(); } 
243
244   const_reverse_iterator crend() const 
245   { return const_reverse_iterator(this->cbegin()); } 
246
247   bool empty() const 
248   { return this->m_data.m_vect.empty(); }
249
250   size_type size() const 
251   { return this->m_data.m_vect.size(); }
252
253   size_type max_size() const 
254   { return this->m_data.m_vect.max_size(); }
255
256   void swap(flat_tree& other) 
257   {
258      value_compare& mycomp    = this->m_data;
259      value_compare& othercomp = other.m_data;
260      containers_detail::do_swap(mycomp, othercomp);
261      vector_t & myvect    = this->m_data.m_vect;
262      vector_t & othervect = other.m_data.m_vect;
263      myvect.swap(othervect);
264   }
265
266   public:
267   // insert/erase
268   std::pair<iterator,bool> insert_unique(const value_type& val)
269   {
270      insert_commit_data data;
271      std::pair<iterator,bool> ret = priv_insert_unique_prepare(val, data);
272      if(ret.second){
273         ret.first = priv_insert_commit(data, val);
274      }
275      return ret;
276   }
277
278   std::pair<iterator,bool> insert_unique(BOOST_MOVE_MACRO_RV_REF(value_type) val)
279   {
280      insert_commit_data data;
281      std::pair<iterator,bool> ret = priv_insert_unique_prepare(val, data);
282      if(ret.second){
283         ret.first = priv_insert_commit(data, BOOST_CONTAINER_MOVE_NAMESPACE::move(val));
284      }
285      return ret;
286   }
287
288
289   iterator insert_equal(const value_type& val)
290   {
291      iterator i = this->upper_bound(KeyOfValue()(val));
292      i = this->m_data.m_vect.insert(i, val);
293      return i;
294   }
295
296   iterator insert_equal(BOOST_MOVE_MACRO_RV_REF(value_type) mval)
297   {
298      iterator i = this->upper_bound(KeyOfValue()(mval));
299      i = this->m_data.m_vect.insert(i, BOOST_CONTAINER_MOVE_NAMESPACE::move(mval));
300      return i;
301   }
302
303   iterator insert_unique(const_iterator pos, const value_type& val)
304   {
305      insert_commit_data data;
306      std::pair<iterator,bool> ret = priv_insert_unique_prepare(pos, val, data);
307      if(ret.second){
308         ret.first = priv_insert_commit(data, val);
309      }
310      return ret.first;
311   }
312
313   iterator insert_unique(const_iterator pos, BOOST_MOVE_MACRO_RV_REF(value_type) mval)
314   {
315      insert_commit_data data;
316      std::pair<iterator,bool> ret = priv_insert_unique_prepare(pos, mval, data);
317      if(ret.second){
318         ret.first = priv_insert_commit(data, BOOST_CONTAINER_MOVE_NAMESPACE::move(mval));
319      }
320      return ret.first;
321   }
322
323   iterator insert_equal(const_iterator pos, const value_type& val)
324   {
325      insert_commit_data data;
326      priv_insert_equal_prepare(pos, val, data);
327      return priv_insert_commit(data, val);
328   }
329
330   iterator insert_equal(const_iterator pos, BOOST_MOVE_MACRO_RV_REF(value_type) mval)
331   {
332      insert_commit_data data;
333      priv_insert_equal_prepare(pos, mval, data);
334      return priv_insert_commit(data, BOOST_CONTAINER_MOVE_NAMESPACE::move(mval));
335   }
336
337   template <class InIt>
338   void insert_unique(InIt first, InIt last)
339   {
340      for ( ; first != last; ++first)
341         this->insert_unique(*first);
342   }
343
344   template <class InIt>
345   void insert_equal(InIt first, InIt last)
346   {
347      typedef typename 
348         std::iterator_traits<InIt>::iterator_category ItCat;
349      priv_insert_equal(first, last, ItCat());
350   }
351
352   #ifdef BOOST_CONTAINERS_PERFECT_FORWARDING
353
354   template <class... Args>
355   iterator emplace_unique(Args&&... args)
356   {
357      value_type && val = value_type(BOOST_CONTAINER_MOVE_NAMESPACE::forward<Args>(args)...);
358      insert_commit_data data;
359      std::pair<iterator,bool> ret =
360         priv_insert_unique_prepare(val, data);
361      if(ret.second){
362         ret.first = priv_insert_commit(data, BOOST_CONTAINER_MOVE_NAMESPACE::move(val));
363      }
364      return ret.first;
365   }
366
367   template <class... Args>
368   iterator emplace_hint_unique(const_iterator hint, Args&&... args)
369   {
370      value_type && val = value_type(BOOST_CONTAINER_MOVE_NAMESPACE::forward<Args>(args)...);
371      insert_commit_data data;
372      std::pair<iterator,bool> ret = priv_insert_unique_prepare(hint, val, data);
373      if(ret.second){
374         ret.first = priv_insert_commit(data, BOOST_CONTAINER_MOVE_NAMESPACE::move(val));
375      }
376      return ret.first;
377   }
378
379   template <class... Args>
380   iterator emplace_equal(Args&&... args)
381   {
382      value_type &&val = value_type(BOOST_CONTAINER_MOVE_NAMESPACE::forward<Args>(args)...);
383      iterator i = this->upper_bound(KeyOfValue()(val));
384      i = this->m_data.m_vect.insert(i, BOOST_CONTAINER_MOVE_NAMESPACE::move(val));
385      return i;
386   }
387
388   template <class... Args>
389   iterator emplace_hint_equal(const_iterator hint, Args&&... args)
390   {
391      value_type &&val = value_type(BOOST_CONTAINER_MOVE_NAMESPACE::forward<Args>(args)...);
392      insert_commit_data data;
393      priv_insert_equal_prepare(hint, val, data);
394      return priv_insert_commit(data, BOOST_CONTAINER_MOVE_NAMESPACE::move(val));
395   }
396
397   #else //#ifdef BOOST_CONTAINERS_PERFECT_FORWARDING
398
399   iterator emplace_unique()
400   {
401      containers_detail::value_init<value_type> vval;
402      value_type &val = vval.m_t;
403      insert_commit_data data;
404      std::pair<iterator,bool> ret =
405         priv_insert_unique_prepare(val, data);
406      if(ret.second){
407         ret.first = priv_insert_commit(data, BOOST_CONTAINER_MOVE_NAMESPACE::move(val));
408      }
409      return ret.first;
410   }
411
412   iterator emplace_hint_unique(const_iterator hint)
413   {
414      containers_detail::value_init<value_type> vval;
415      value_type &val = vval.m_t;
416      insert_commit_data data;
417      std::pair<iterator,bool> ret = priv_insert_unique_prepare(hint, val, data);
418      if(ret.second){
419         ret.first = priv_insert_commit(data, BOOST_CONTAINER_MOVE_NAMESPACE::move(val));
420      }
421      return ret.first;
422   }
423
424   iterator emplace_equal()
425   {
426      containers_detail::value_init<value_type> vval;
427      value_type &val = vval.m_t;
428      iterator i = this->upper_bound(KeyOfValue()(val));
429      i = this->m_data.m_vect.insert(i, BOOST_CONTAINER_MOVE_NAMESPACE::move(val));
430      return i;
431   }
432
433   iterator emplace_hint_equal(const_iterator hint)
434   {
435      containers_detail::value_init<value_type> vval;
436      value_type &val = vval.m_t;
437      insert_commit_data data;
438      priv_insert_equal_prepare(hint, val, data);
439      return priv_insert_commit(data, BOOST_CONTAINER_MOVE_NAMESPACE::move(val));
440   }
441
442   #define BOOST_PP_LOCAL_MACRO(n)                                                        \
443   template<BOOST_PP_ENUM_PARAMS(n, class P)>                                             \
444   iterator emplace_unique(BOOST_PP_ENUM(n, BOOST_CONTAINERS_PP_PARAM_LIST, _))         \
445   {                                                                                      \
446      value_type val(BOOST_PP_ENUM(n, BOOST_CONTAINERS_PP_PARAM_FORWARD, _));           \
447      insert_commit_data data;                                                            \
448      std::pair<iterator,bool> ret = priv_insert_unique_prepare(val, data);               \
449      if(ret.second){                                                                     \
450         ret.first = priv_insert_commit(data, BOOST_CONTAINER_MOVE_NAMESPACE::move(val)); \
451      }                                                                                   \
452      return ret.first;                                                                   \
453   }                                                                                      \
454                                                                                          \
455   template<BOOST_PP_ENUM_PARAMS(n, class P)>                                             \
456   iterator emplace_hint_unique(const_iterator hint,                                      \
457      BOOST_PP_ENUM(n, BOOST_CONTAINERS_PP_PARAM_LIST, _))                              \
458   {                                                                                      \
459      value_type val(BOOST_PP_ENUM(n, BOOST_CONTAINERS_PP_PARAM_FORWARD, _));           \
460      insert_commit_data data;                                                            \
461      std::pair<iterator,bool> ret = priv_insert_unique_prepare(hint, val, data);         \
462      if(ret.second){                                                                     \
463         ret.first = priv_insert_commit(data, BOOST_CONTAINER_MOVE_NAMESPACE::move(val));        \
464      }                                                                                   \
465      return ret.first;                                                                   \
466   }                                                                                      \
467                                                                                          \
468   template<BOOST_PP_ENUM_PARAMS(n, class P)>                                             \
469   iterator emplace_equal(BOOST_PP_ENUM(n, BOOST_CONTAINERS_PP_PARAM_LIST, _))          \
470   {                                                                                      \
471      value_type val(BOOST_PP_ENUM(n, BOOST_CONTAINERS_PP_PARAM_FORWARD, _));           \
472      iterator i = this->upper_bound(KeyOfValue()(val));                                  \
473      i = this->m_data.m_vect.insert(i, BOOST_CONTAINER_MOVE_NAMESPACE::move(val));       \
474      return i;                                                                           \
475   }                                                                                      \
476                                                                                          \
477   template<BOOST_PP_ENUM_PARAMS(n, class P)>                                             \
478   iterator emplace_hint_equal(const_iterator hint,                                       \
479      BOOST_PP_ENUM(n, BOOST_CONTAINERS_PP_PARAM_LIST, _))                              \
480   {                                                                                      \
481      value_type val(BOOST_PP_ENUM(n, BOOST_CONTAINERS_PP_PARAM_FORWARD, _));           \
482      insert_commit_data data;                                                            \
483      priv_insert_equal_prepare(hint, val, data);                                         \
484      return priv_insert_commit(data, BOOST_CONTAINER_MOVE_NAMESPACE::move(val));                \
485   }                                                                                      \
486   //!
487   #define BOOST_PP_LOCAL_LIMITS (1, BOOST_CONTAINERS_MAX_CONSTRUCTOR_PARAMETERS)
488   #include BOOST_PP_LOCAL_ITERATE()
489
490   #endif   //#ifdef BOOST_CONTAINERS_PERFECT_FORWARDING
491
492   iterator erase(const_iterator position)
493   {  return this->m_data.m_vect.erase(position);  }
494
495   size_type erase(const key_type& k)
496   {
497      std::pair<iterator,iterator > itp = this->equal_range(k);
498      size_type ret = static_cast<size_type>(itp.second-itp.first);
499      if (ret){
500         this->m_data.m_vect.erase(itp.first, itp.second);
501      }
502      return ret;
503   }
504
505   iterator erase(const_iterator first, const_iterator last)
506   {  return this->m_data.m_vect.erase(first, last);  }
507
508   void clear()
509   {  this->m_data.m_vect.clear();  }
510
511   //! <b>Effects</b>: Tries to deallocate the excess of memory created
512   //    with previous allocations. The size of the vector is unchanged
513   //!
514   //! <b>Throws</b>: If memory allocation throws, or T's copy constructor throws.
515   //!
516   //! <b>Complexity</b>: Linear to size().
517   void shrink_to_fit()
518   {  this->m_data.m_vect.shrink_to_fit();  }
519
520   // set operations:
521   iterator find(const key_type& k)
522   {
523      const Compare &key_comp = this->m_data.get_comp();
524      iterator i = this->lower_bound(k);
525
526      if (i != this->end() && key_comp(k, KeyOfValue()(*i))){  
527         i = this->end();  
528      }
529      return i;
530   }
531
532   const_iterator find(const key_type& k) const
533   {
534      const Compare &key_comp = this->m_data.get_comp();
535      const_iterator i = this->lower_bound(k);
536
537      if (i != this->end() && key_comp(k, KeyOfValue()(*i))){  
538         i = this->end();  
539      }
540      return i;
541   }
542
543   size_type count(const key_type& k) const
544   {
545      std::pair<const_iterator, const_iterator> p = this->equal_range(k);
546      size_type n = p.second - p.first;
547      return n;
548   }
549
550   iterator lower_bound(const key_type& k)
551   {  return this->priv_lower_bound(this->begin(), this->end(), k);  }
552
553   const_iterator lower_bound(const key_type& k) const
554   {  return this->priv_lower_bound(this->begin(), this->end(), k);  }
555
556   iterator upper_bound(const key_type& k)
557   {  return this->priv_upper_bound(this->begin(), this->end(), k);  }
558
559   const_iterator upper_bound(const key_type& k) const
560   {  return this->priv_upper_bound(this->begin(), this->end(), k);  }
561
562   std::pair<iterator,iterator> equal_range(const key_type& k)
563   {  return this->priv_equal_range(this->begin(), this->end(), k);  }
564
565   std::pair<const_iterator, const_iterator> equal_range(const key_type& k) const
566   {  return this->priv_equal_range(this->begin(), this->end(), k);  }
567
568   size_type capacity() const           
569   { return this->m_data.m_vect.capacity(); }
570
571   void reserve(size_type count)       
572   { this->m_data.m_vect.reserve(count);   }
573
574   private:
575   struct insert_commit_data
576   {
577      const_iterator position;
578   };
579
580   // insert/erase
581   void priv_insert_equal_prepare
582      (const_iterator pos, const value_type& val, insert_commit_data &data)
583   {
584      // N1780
585      //   To insert val at pos:
586      //   if pos == end || val <= *pos
587      //      if pos == begin || val >= *(pos-1)
588      //         insert val before pos
589      //      else
590      //         insert val before upper_bound(val)
591      //   else if pos+1 == end || val <= *(pos+1)
592      //      insert val after pos
593      //   else
594      //      insert val before lower_bound(val)
595      const value_compare &value_comp = this->m_data;
596
597      if(pos == this->cend() || !value_comp(*pos, val)){
598         if (pos == this->cbegin() || !value_comp(val, pos[-1])){
599            data.position = pos;
600         }
601         else{
602            data.position = 
603               this->priv_upper_bound(this->cbegin(), pos, KeyOfValue()(val));
604         }
605      }
606      //Works, but increases code complexity
607      //else if (++pos == this->end() || !value_comp(*pos, val)){
608      //   return this->m_data.m_vect.insert(pos, val);
609      //}
610      else{
611         data.position = 
612            this->priv_lower_bound(pos, this->cend(), KeyOfValue()(val));
613      }
614   }
615
616   std::pair<iterator,bool> priv_insert_unique_prepare
617      (const_iterator beg, const_iterator end, const value_type& val, insert_commit_data &commit_data)
618   {
619      const value_compare &value_comp  = this->m_data;
620      commit_data.position = this->priv_lower_bound(beg, end, KeyOfValue()(val));
621      return std::pair<iterator,bool>
622         ( *reinterpret_cast<iterator*>(&commit_data.position)
623         , commit_data.position == end || value_comp(val, *commit_data.position));
624   }
625
626   std::pair<iterator,bool> priv_insert_unique_prepare
627      (const value_type& val, insert_commit_data &commit_data)
628   {  return priv_insert_unique_prepare(this->begin(), this->end(), val, commit_data);   }
629
630   std::pair<iterator,bool> priv_insert_unique_prepare
631      (const_iterator pos, const value_type& val, insert_commit_data &commit_data)
632   {
633      //N1780. Props to Howard Hinnant!
634      //To insert val at pos:
635      //if pos == end || val <= *pos
636      //   if pos == begin || val >= *(pos-1)
637      //      insert val before pos
638      //   else
639      //      insert val before upper_bound(val)
640      //else if pos+1 == end || val <= *(pos+1)
641      //   insert val after pos
642      //else
643      //   insert val before lower_bound(val)
644      const value_compare &value_comp = this->m_data;
645
646      if(pos == this->cend() || value_comp(val, *pos)){
647         if(pos != this->cbegin() && !value_comp(val, pos[-1])){
648            if(value_comp(pos[-1], val)){
649               commit_data.position = pos;
650               return std::pair<iterator,bool>(*reinterpret_cast<iterator*>(&pos), true);
651            }
652            else{
653               return std::pair<iterator,bool>(*reinterpret_cast<iterator*>(&pos), false);
654            }
655         }
656         return this->priv_insert_unique_prepare(this->cbegin(), pos, val, commit_data);
657      }
658
659      // Works, but increases code complexity
660      //Next check
661      //else if (value_comp(*pos, val) && !value_comp(pos[1], val)){
662      //   if(value_comp(val, pos[1])){
663      //      commit_data.position = pos+1;
664      //      return std::pair<iterator,bool>(pos+1, true);
665      //   }
666      //   else{
667      //      return std::pair<iterator,bool>(pos+1, false);
668      //   }
669      //}
670      else{
671         //[... pos ... val ... ]
672         //The hint is before the insertion position, so insert it
673         //in the remaining range
674         return this->priv_insert_unique_prepare(pos, this->end(), val, commit_data);
675      }
676   }
677
678   template<class Convertible>
679   iterator priv_insert_commit
680      (insert_commit_data &commit_data, BOOST_MOVE_MACRO_FWD_REF(Convertible) convertible)
681   {
682      return this->m_data.m_vect.insert
683         ( commit_data.position
684         , BOOST_CONTAINER_MOVE_NAMESPACE::forward<Convertible>(convertible));
685   }
686
687   template <class RanIt>
688   RanIt priv_lower_bound(RanIt first, RanIt last,
689                          const key_type & key) const
690   {
691      const Compare &key_comp = this->m_data.get_comp();
692      KeyOfValue key_extract;
693      difference_type len = last - first, half;
694      RanIt middle;
695
696      while (len > 0) {
697         half = len >> 1;
698         middle = first;
699         middle += half;
700
701         if (key_comp(key_extract(*middle), key)) {
702            ++middle;
703            first = middle;
704            len = len - half - 1;
705         }
706         else
707            len = half;
708      }
709      return first;
710   }
711
712   template <class RanIt>
713   RanIt priv_upper_bound(RanIt first, RanIt last,
714                          const key_type & key) const
715   {
716      const Compare &key_comp = this->m_data.get_comp();
717      KeyOfValue key_extract;
718      difference_type len = last - first, half;
719      RanIt middle;
720
721      while (len > 0) {
722         half = len >> 1;
723         middle = first;
724         middle += half;
725
726         if (key_comp(key, key_extract(*middle))) {
727            len = half;
728         }
729         else{
730            first = ++middle;
731            len = len - half - 1;  
732         }
733      }
734      return first;
735   }
736
737   template <class RanIt>
738   std::pair<RanIt, RanIt>
739      priv_equal_range(RanIt first, RanIt last, const key_type& key) const
740   {
741      const Compare &key_comp = this->m_data.get_comp();
742      KeyOfValue key_extract;
743      difference_type len = last - first, half;
744      RanIt middle, left, right;
745
746      while (len > 0) {
747         half = len >> 1;
748         middle = first;
749         middle += half;
750
751         if (key_comp(key_extract(*middle), key)){
752            first = middle;
753            ++first;
754            len = len - half - 1;
755         }
756         else if (key_comp(key, key_extract(*middle))){
757            len = half;
758         }
759         else {
760            left = this->priv_lower_bound(first, middle, key);
761            first += len;
762            right = this->priv_upper_bound(++middle, first, key);
763            return std::pair<RanIt, RanIt>(left, right);
764         }
765      }
766      return std::pair<RanIt, RanIt>(first, first);
767   }
768
769   template <class FwdIt>
770   void priv_insert_equal(FwdIt first, FwdIt last, std::forward_iterator_tag)
771   {
772      size_type len = static_cast<size_type>(std::distance(first, last));
773      this->reserve(this->size()+len);
774      this->priv_insert_equal(first, last, std::input_iterator_tag());
775   }
776
777   template <class InIt>
778   void priv_insert_equal(InIt first, InIt last, std::input_iterator_tag)
779   {
780      for ( ; first != last; ++first)
781         this->insert_equal(*first);
782   }
783
784/*
785   template <class FwdIt>
786   void priv_insert_unique(FwdIt first, FwdIt last, std::forward_iterator_tag)
787   {
788      size_type len = static_cast<size_type>(std::distance(first, last));
789      this->reserve(this->size()+len);
790      priv_insert_unique(first, last, std::input_iterator_tag());
791   }
792
793   template <class InIt>
794   void priv_insert_unique(InIt first, InIt last, std::input_iterator_tag)
795   {
796      for ( ; first != last; ++first)
797         this->insert_unique(*first);
798   }
799*/
800};
801
802template <class Key, class Value, class KeyOfValue, 
803          class Compare, class Alloc>
804inline bool 
805operator==(const flat_tree<Key,Value,KeyOfValue,Compare,Alloc>& x, 
806           const flat_tree<Key,Value,KeyOfValue,Compare,Alloc>& y)
807{
808  return x.size() == y.size() &&
809         std::equal(x.begin(), x.end(), y.begin());
810}
811
812template <class Key, class Value, class KeyOfValue, 
813          class Compare, class Alloc>
814inline bool 
815operator<(const flat_tree<Key,Value,KeyOfValue,Compare,Alloc>& x, 
816          const flat_tree<Key,Value,KeyOfValue,Compare,Alloc>& y)
817{
818  return std::lexicographical_compare(x.begin(), x.end(), 
819                                      y.begin(), y.end());
820}
821
822template <class Key, class Value, class KeyOfValue, 
823          class Compare, class Alloc>
824inline bool 
825operator!=(const flat_tree<Key,Value,KeyOfValue,Compare,Alloc>& x, 
826           const flat_tree<Key,Value,KeyOfValue,Compare,Alloc>& y) 
827   {  return !(x == y); }
828
829template <class Key, class Value, class KeyOfValue, 
830          class Compare, class Alloc>
831inline bool 
832operator>(const flat_tree<Key,Value,KeyOfValue,Compare,Alloc>& x, 
833          const flat_tree<Key,Value,KeyOfValue,Compare,Alloc>& y) 
834   {  return y < x;  }
835
836template <class Key, class Value, class KeyOfValue, 
837          class Compare, class Alloc>
838inline bool 
839operator<=(const flat_tree<Key,Value,KeyOfValue,Compare,Alloc>& x, 
840           const flat_tree<Key,Value,KeyOfValue,Compare,Alloc>& y) 
841   {  return !(y < x);  }
842
843template <class Key, class Value, class KeyOfValue, 
844          class Compare, class Alloc>
845inline bool 
846operator>=(const flat_tree<Key,Value,KeyOfValue,Compare,Alloc>& x, 
847           const flat_tree<Key,Value,KeyOfValue,Compare,Alloc>& y) 
848   {  return !(x < y);  }
849
850
851template <class Key, class Value, class KeyOfValue, 
852          class Compare, class Alloc>
853inline void 
854swap(flat_tree<Key,Value,KeyOfValue,Compare,Alloc>& x, 
855     flat_tree<Key,Value,KeyOfValue,Compare,Alloc>& y)
856   {  x.swap(y);  }
857
858}  //namespace containers_detail {
859
860}  //namespace container {
861/*
862//!has_trivial_destructor_after_move<> == true_type
863//!specialization for optimizations
864template <class K, class V, class KOV, 
865class C, class A>
866struct has_trivial_destructor_after_move<boost::container::containers_detail::flat_tree<K, V, KOV, C, A> >
867{
868   static const bool value = has_trivial_destructor<A>::value && has_trivial_destructor<C>::value;
869};
870*/
871}  //namespace boost {
872
873#include INCLUDE_BOOST_CONTAINER_DETAIL_CONFIG_END_HPP
874
875#endif // BOOST_CONTAINERS_FLAT_TREE_HPP