flat_map.hpp 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2012. 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. #ifndef BOOST_CONTAINER_FLAT_MAP_HPP
  11. #define BOOST_CONTAINER_FLAT_MAP_HPP
  12. #if defined(_MSC_VER)
  13. # pragma once
  14. #endif
  15. #include <boost/container/detail/config_begin.hpp>
  16. #include <boost/container/detail/workaround.hpp>
  17. #include <boost/container/container_fwd.hpp>
  18. #include <utility>
  19. #include <functional>
  20. #include <memory>
  21. #include <boost/container/detail/flat_tree.hpp>
  22. #include <boost/type_traits/has_trivial_destructor.hpp>
  23. #include <boost/container/detail/mpl.hpp>
  24. #include <boost/container/allocator_traits.hpp>
  25. #include <boost/container/throw_exception.hpp>
  26. #include <boost/move/utility.hpp>
  27. #include <boost/move/detail/move_helpers.hpp>
  28. #include <boost/detail/no_exceptions_support.hpp>
  29. namespace boost {
  30. namespace container {
  31. /// @cond
  32. // Forward declarations of operators == and <, needed for friend declarations.
  33. template <class Key, class T, class Compare, class Allocator>
  34. class flat_map;
  35. template <class Key, class T, class Compare, class Allocator>
  36. inline bool operator==(const flat_map<Key,T,Compare,Allocator>& x,
  37. const flat_map<Key,T,Compare,Allocator>& y);
  38. template <class Key, class T, class Compare, class Allocator>
  39. inline bool operator<(const flat_map<Key,T,Compare,Allocator>& x,
  40. const flat_map<Key,T,Compare,Allocator>& y);
  41. namespace container_detail{
  42. template<class D, class S>
  43. static D &force(const S &s)
  44. { return *const_cast<D*>((reinterpret_cast<const D*>(&s))); }
  45. template<class D, class S>
  46. static D force_copy(S s)
  47. {
  48. D *vp = reinterpret_cast<D *>(&s);
  49. return D(*vp);
  50. }
  51. } //namespace container_detail{
  52. /// @endcond
  53. //! A flat_map is a kind of associative container that supports unique keys (contains at
  54. //! most one of each key value) and provides for fast retrieval of values of another
  55. //! type T based on the keys. The flat_map class supports random-access iterators.
  56. //!
  57. //! A flat_map satisfies all of the requirements of a container and of a reversible
  58. //! container and of an associative container. A flat_map also provides
  59. //! most operations described for unique keys. For a
  60. //! flat_map<Key,T> the key_type is Key and the value_type is std::pair<Key,T>
  61. //! (unlike std::map<Key, T> which value_type is std::pair<<b>const</b> Key, T>).
  62. //!
  63. //! Compare is the ordering function for Keys (e.g. <i>std::less<Key></i>).
  64. //!
  65. //! Allocator is the allocator to allocate the value_types
  66. //! (e.g. <i>allocator< std::pair<Key, T> ></i>).
  67. //!
  68. //! flat_map is similar to std::map but it's implemented like an ordered vector.
  69. //! This means that inserting a new element into a flat_map invalidates
  70. //! previous iterators and references
  71. //!
  72. //! Erasing an element invalidates iterators and references
  73. //! pointing to elements that come after (their keys are bigger) the erased element.
  74. //!
  75. //! This container provides random-access iterators.
  76. #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
  77. template <class Key, class T, class Compare = std::less<Key>, class Allocator = std::allocator< std::pair< Key, T> > >
  78. #else
  79. template <class Key, class T, class Compare, class Allocator>
  80. #endif
  81. class flat_map
  82. {
  83. /// @cond
  84. private:
  85. BOOST_COPYABLE_AND_MOVABLE(flat_map)
  86. //This is the tree that we should store if pair was movable
  87. typedef container_detail::flat_tree<Key,
  88. std::pair<Key, T>,
  89. container_detail::select1st< std::pair<Key, T> >,
  90. Compare,
  91. Allocator> tree_t;
  92. //This is the real tree stored here. It's based on a movable pair
  93. typedef container_detail::flat_tree<Key,
  94. container_detail::pair<Key, T>,
  95. container_detail::select1st<container_detail::pair<Key, T> >,
  96. Compare,
  97. typename allocator_traits<Allocator>::template portable_rebind_alloc
  98. <container_detail::pair<Key, T> >::type> impl_tree_t;
  99. impl_tree_t m_flat_tree; // flat tree representing flat_map
  100. typedef typename impl_tree_t::value_type impl_value_type;
  101. typedef typename impl_tree_t::const_iterator impl_const_iterator;
  102. typedef typename impl_tree_t::allocator_type impl_allocator_type;
  103. typedef container_detail::flat_tree_value_compare
  104. < Compare
  105. , container_detail::select1st< std::pair<Key, T> >
  106. , std::pair<Key, T> > value_compare_impl;
  107. typedef typename container_detail::get_flat_tree_iterators
  108. <typename allocator_traits<Allocator>::pointer>::iterator iterator_impl;
  109. typedef typename container_detail::get_flat_tree_iterators
  110. <typename allocator_traits<Allocator>::pointer>::const_iterator const_iterator_impl;
  111. typedef typename container_detail::get_flat_tree_iterators
  112. <typename allocator_traits<Allocator>::pointer>::reverse_iterator reverse_iterator_impl;
  113. typedef typename container_detail::get_flat_tree_iterators
  114. <typename allocator_traits<Allocator>::pointer>::const_reverse_iterator const_reverse_iterator_impl;
  115. /// @endcond
  116. public:
  117. //////////////////////////////////////////////
  118. //
  119. // types
  120. //
  121. //////////////////////////////////////////////
  122. typedef Key key_type;
  123. typedef T mapped_type;
  124. typedef std::pair<Key, T> value_type;
  125. typedef typename boost::container::allocator_traits<Allocator>::pointer pointer;
  126. typedef typename boost::container::allocator_traits<Allocator>::const_pointer const_pointer;
  127. typedef typename boost::container::allocator_traits<Allocator>::reference reference;
  128. typedef typename boost::container::allocator_traits<Allocator>::const_reference const_reference;
  129. typedef typename boost::container::allocator_traits<Allocator>::size_type size_type;
  130. typedef typename boost::container::allocator_traits<Allocator>::difference_type difference_type;
  131. typedef Allocator allocator_type;
  132. typedef BOOST_CONTAINER_IMPDEF(Allocator) stored_allocator_type;
  133. typedef BOOST_CONTAINER_IMPDEF(value_compare_impl) value_compare;
  134. typedef Compare key_compare;
  135. typedef BOOST_CONTAINER_IMPDEF(iterator_impl) iterator;
  136. typedef BOOST_CONTAINER_IMPDEF(const_iterator_impl) const_iterator;
  137. typedef BOOST_CONTAINER_IMPDEF(reverse_iterator_impl) reverse_iterator;
  138. typedef BOOST_CONTAINER_IMPDEF(const_reverse_iterator_impl) const_reverse_iterator;
  139. typedef BOOST_CONTAINER_IMPDEF(impl_value_type) movable_value_type;
  140. public:
  141. //////////////////////////////////////////////
  142. //
  143. // construct/copy/destroy
  144. //
  145. //////////////////////////////////////////////
  146. //! <b>Effects</b>: Default constructs an empty flat_map.
  147. //!
  148. //! <b>Complexity</b>: Constant.
  149. flat_map()
  150. : m_flat_tree() {}
  151. //! <b>Effects</b>: Constructs an empty flat_map using the specified
  152. //! comparison object and allocator.
  153. //!
  154. //! <b>Complexity</b>: Constant.
  155. explicit flat_map(const Compare& comp, const allocator_type& a = allocator_type())
  156. : m_flat_tree(comp, container_detail::force<impl_allocator_type>(a))
  157. {}
  158. //! <b>Effects</b>: Constructs an empty flat_map using the specified allocator.
  159. //!
  160. //! <b>Complexity</b>: Constant.
  161. explicit flat_map(const allocator_type& a)
  162. : m_flat_tree(container_detail::force<impl_allocator_type>(a))
  163. {}
  164. //! <b>Effects</b>: Constructs an empty flat_map using the specified comparison object and
  165. //! allocator, and inserts elements from the range [first ,last ).
  166. //!
  167. //! <b>Complexity</b>: Linear in N if the range [first ,last ) is already sorted using
  168. //! comp and otherwise N logN, where N is last - first.
  169. template <class InputIterator>
  170. flat_map(InputIterator first, InputIterator last, const Compare& comp = Compare(),
  171. const allocator_type& a = allocator_type())
  172. : m_flat_tree(true, first, last, comp, container_detail::force<impl_allocator_type>(a))
  173. {}
  174. //! <b>Effects</b>: Constructs an empty flat_map using the specified comparison object and
  175. //! allocator, and inserts elements from the ordered unique range [first ,last). This function
  176. //! is more efficient than the normal range creation for ordered ranges.
  177. //!
  178. //! <b>Requires</b>: [first ,last) must be ordered according to the predicate and must be
  179. //! unique values.
  180. //!
  181. //! <b>Complexity</b>: Linear in N.
  182. //!
  183. //! <b>Note</b>: Non-standard extension.
  184. template <class InputIterator>
  185. flat_map( ordered_unique_range_t, InputIterator first, InputIterator last
  186. , const Compare& comp = Compare(), const allocator_type& a = allocator_type())
  187. : m_flat_tree(ordered_range, first, last, comp, a)
  188. {}
  189. //! <b>Effects</b>: Copy constructs a flat_map.
  190. //!
  191. //! <b>Complexity</b>: Linear in x.size().
  192. flat_map(const flat_map& x)
  193. : m_flat_tree(x.m_flat_tree) {}
  194. //! <b>Effects</b>: Move constructs a flat_map.
  195. //! Constructs *this using x's resources.
  196. //!
  197. //! <b>Complexity</b>: Constant.
  198. //!
  199. //! <b>Postcondition</b>: x is emptied.
  200. flat_map(BOOST_RV_REF(flat_map) x)
  201. : m_flat_tree(boost::move(x.m_flat_tree))
  202. {}
  203. //! <b>Effects</b>: Copy constructs a flat_map using the specified allocator.
  204. //!
  205. //! <b>Complexity</b>: Linear in x.size().
  206. flat_map(const flat_map& x, const allocator_type &a)
  207. : m_flat_tree(x.m_flat_tree, a)
  208. {}
  209. //! <b>Effects</b>: Move constructs a flat_map using the specified allocator.
  210. //! Constructs *this using x's resources.
  211. //!
  212. //! <b>Complexity</b>: Constant if x.get_allocator() == a, linear otherwise.
  213. flat_map(BOOST_RV_REF(flat_map) x, const allocator_type &a)
  214. : m_flat_tree(boost::move(x.m_flat_tree), a)
  215. {}
  216. //! <b>Effects</b>: Makes *this a copy of x.
  217. //!
  218. //! <b>Complexity</b>: Linear in x.size().
  219. flat_map& operator=(BOOST_COPY_ASSIGN_REF(flat_map) x)
  220. { m_flat_tree = x.m_flat_tree; return *this; }
  221. //! <b>Effects</b>: Move constructs a flat_map.
  222. //! Constructs *this using x's resources.
  223. //!
  224. //! <b>Complexity</b>: Construct.
  225. //!
  226. //! <b>Postcondition</b>: x is emptied.
  227. flat_map& operator=(BOOST_RV_REF(flat_map) mx)
  228. { m_flat_tree = boost::move(mx.m_flat_tree); return *this; }
  229. //! <b>Effects</b>: Returns a copy of the Allocator that
  230. //! was passed to the object's constructor.
  231. //!
  232. //! <b>Complexity</b>: Constant.
  233. allocator_type get_allocator() const BOOST_CONTAINER_NOEXCEPT
  234. { return container_detail::force_copy<allocator_type>(m_flat_tree.get_allocator()); }
  235. //! <b>Effects</b>: Returns a reference to the internal allocator.
  236. //!
  237. //! <b>Throws</b>: Nothing
  238. //!
  239. //! <b>Complexity</b>: Constant.
  240. //!
  241. //! <b>Note</b>: Non-standard extension.
  242. stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT
  243. { return container_detail::force<stored_allocator_type>(m_flat_tree.get_stored_allocator()); }
  244. //! <b>Effects</b>: Returns a reference to the internal allocator.
  245. //!
  246. //! <b>Throws</b>: Nothing
  247. //!
  248. //! <b>Complexity</b>: Constant.
  249. //!
  250. //! <b>Note</b>: Non-standard extension.
  251. const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT
  252. { return container_detail::force<stored_allocator_type>(m_flat_tree.get_stored_allocator()); }
  253. //////////////////////////////////////////////
  254. //
  255. // iterators
  256. //
  257. //////////////////////////////////////////////
  258. //! <b>Effects</b>: Returns an iterator to the first element contained in the container.
  259. //!
  260. //! <b>Throws</b>: Nothing.
  261. //!
  262. //! <b>Complexity</b>: Constant.
  263. iterator begin() BOOST_CONTAINER_NOEXCEPT
  264. { return container_detail::force_copy<iterator>(m_flat_tree.begin()); }
  265. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the container.
  266. //!
  267. //! <b>Throws</b>: Nothing.
  268. //!
  269. //! <b>Complexity</b>: Constant.
  270. const_iterator begin() const BOOST_CONTAINER_NOEXCEPT
  271. { return container_detail::force_copy<const_iterator>(m_flat_tree.begin()); }
  272. //! <b>Effects</b>: Returns an iterator to the end of the container.
  273. //!
  274. //! <b>Throws</b>: Nothing.
  275. //!
  276. //! <b>Complexity</b>: Constant.
  277. iterator end() BOOST_CONTAINER_NOEXCEPT
  278. { return container_detail::force_copy<iterator>(m_flat_tree.end()); }
  279. //! <b>Effects</b>: Returns a const_iterator to the end of the container.
  280. //!
  281. //! <b>Throws</b>: Nothing.
  282. //!
  283. //! <b>Complexity</b>: Constant.
  284. const_iterator end() const BOOST_CONTAINER_NOEXCEPT
  285. { return container_detail::force_copy<const_iterator>(m_flat_tree.end()); }
  286. //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
  287. //! of the reversed container.
  288. //!
  289. //! <b>Throws</b>: Nothing.
  290. //!
  291. //! <b>Complexity</b>: Constant.
  292. reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT
  293. { return container_detail::force_copy<reverse_iterator>(m_flat_tree.rbegin()); }
  294. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  295. //! of the reversed container.
  296. //!
  297. //! <b>Throws</b>: Nothing.
  298. //!
  299. //! <b>Complexity</b>: Constant.
  300. const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT
  301. { return container_detail::force_copy<const_reverse_iterator>(m_flat_tree.rbegin()); }
  302. //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
  303. //! of the reversed container.
  304. //!
  305. //! <b>Throws</b>: Nothing.
  306. //!
  307. //! <b>Complexity</b>: Constant.
  308. reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT
  309. { return container_detail::force_copy<reverse_iterator>(m_flat_tree.rend()); }
  310. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  311. //! of the reversed container.
  312. //!
  313. //! <b>Throws</b>: Nothing.
  314. //!
  315. //! <b>Complexity</b>: Constant.
  316. const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT
  317. { return container_detail::force_copy<const_reverse_iterator>(m_flat_tree.rend()); }
  318. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the container.
  319. //!
  320. //! <b>Throws</b>: Nothing.
  321. //!
  322. //! <b>Complexity</b>: Constant.
  323. const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT
  324. { return container_detail::force_copy<const_iterator>(m_flat_tree.cbegin()); }
  325. //! <b>Effects</b>: Returns a const_iterator to the end of the container.
  326. //!
  327. //! <b>Throws</b>: Nothing.
  328. //!
  329. //! <b>Complexity</b>: Constant.
  330. const_iterator cend() const BOOST_CONTAINER_NOEXCEPT
  331. { return container_detail::force_copy<const_iterator>(m_flat_tree.cend()); }
  332. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  333. //! of the reversed container.
  334. //!
  335. //! <b>Throws</b>: Nothing.
  336. //!
  337. //! <b>Complexity</b>: Constant.
  338. const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT
  339. { return container_detail::force_copy<const_reverse_iterator>(m_flat_tree.crbegin()); }
  340. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  341. //! of the reversed container.
  342. //!
  343. //! <b>Throws</b>: Nothing.
  344. //!
  345. //! <b>Complexity</b>: Constant.
  346. const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT
  347. { return container_detail::force_copy<const_reverse_iterator>(m_flat_tree.crend()); }
  348. //////////////////////////////////////////////
  349. //
  350. // capacity
  351. //
  352. //////////////////////////////////////////////
  353. //! <b>Effects</b>: Returns true if the container contains no elements.
  354. //!
  355. //! <b>Throws</b>: Nothing.
  356. //!
  357. //! <b>Complexity</b>: Constant.
  358. bool empty() const BOOST_CONTAINER_NOEXCEPT
  359. { return m_flat_tree.empty(); }
  360. //! <b>Effects</b>: Returns the number of the elements contained in the container.
  361. //!
  362. //! <b>Throws</b>: Nothing.
  363. //!
  364. //! <b>Complexity</b>: Constant.
  365. size_type size() const BOOST_CONTAINER_NOEXCEPT
  366. { return m_flat_tree.size(); }
  367. //! <b>Effects</b>: Returns the largest possible size of the container.
  368. //!
  369. //! <b>Throws</b>: Nothing.
  370. //!
  371. //! <b>Complexity</b>: Constant.
  372. size_type max_size() const BOOST_CONTAINER_NOEXCEPT
  373. { return m_flat_tree.max_size(); }
  374. //! <b>Effects</b>: Number of elements for which memory has been allocated.
  375. //! capacity() is always greater than or equal to size().
  376. //!
  377. //! <b>Throws</b>: Nothing.
  378. //!
  379. //! <b>Complexity</b>: Constant.
  380. size_type capacity() const BOOST_CONTAINER_NOEXCEPT
  381. { return m_flat_tree.capacity(); }
  382. //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
  383. //! effect. Otherwise, it is a request for allocation of additional memory.
  384. //! If the request is successful, then capacity() is greater than or equal to
  385. //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
  386. //!
  387. //! <b>Throws</b>: If memory allocation allocation throws or T's copy constructor throws.
  388. //!
  389. //! <b>Note</b>: If capacity() is less than "cnt", iterators and references to
  390. //! to values might be invalidated.
  391. void reserve(size_type cnt)
  392. { m_flat_tree.reserve(cnt); }
  393. //! <b>Effects</b>: Tries to deallocate the excess of memory created
  394. // with previous allocations. The size of the vector is unchanged
  395. //!
  396. //! <b>Throws</b>: If memory allocation throws, or T's copy constructor throws.
  397. //!
  398. //! <b>Complexity</b>: Linear to size().
  399. void shrink_to_fit()
  400. { m_flat_tree.shrink_to_fit(); }
  401. //////////////////////////////////////////////
  402. //
  403. // element access
  404. //
  405. //////////////////////////////////////////////
  406. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  407. //! Effects: If there is no key equivalent to x in the flat_map, inserts
  408. //! value_type(x, T()) into the flat_map.
  409. //!
  410. //! Returns: Allocator reference to the mapped_type corresponding to x in *this.
  411. //!
  412. //! Complexity: Logarithmic.
  413. mapped_type &operator[](const key_type& k);
  414. //! Effects: If there is no key equivalent to x in the flat_map, inserts
  415. //! value_type(move(x), T()) into the flat_map (the key is move-constructed)
  416. //!
  417. //! Returns: Allocator reference to the mapped_type corresponding to x in *this.
  418. //!
  419. //! Complexity: Logarithmic.
  420. mapped_type &operator[](key_type &&k) ;
  421. #else
  422. BOOST_MOVE_CONVERSION_AWARE_CATCH( operator[] , key_type, mapped_type&, this->priv_subscript)
  423. #endif
  424. //! Returns: Allocator reference to the element whose key is equivalent to x.
  425. //!
  426. //! Throws: An exception object of type out_of_range if no such element is present.
  427. //!
  428. //! Complexity: logarithmic.
  429. T& at(const key_type& k)
  430. {
  431. iterator i = this->find(k);
  432. if(i == this->end()){
  433. throw_out_of_range("flat_map::at key not found");
  434. }
  435. return i->second;
  436. }
  437. //! Returns: Allocator reference to the element whose key is equivalent to x.
  438. //!
  439. //! Throws: An exception object of type out_of_range if no such element is present.
  440. //!
  441. //! Complexity: logarithmic.
  442. const T& at(const key_type& k) const
  443. {
  444. const_iterator i = this->find(k);
  445. if(i == this->end()){
  446. throw_out_of_range("flat_map::at key not found");
  447. }
  448. return i->second;
  449. }
  450. //////////////////////////////////////////////
  451. //
  452. // modifiers
  453. //
  454. //////////////////////////////////////////////
  455. #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  456. //! <b>Effects</b>: Inserts an object x of type T constructed with
  457. //! std::forward<Args>(args)... if and only if there is no element in the container
  458. //! with key equivalent to the key of x.
  459. //!
  460. //! <b>Returns</b>: The bool component of the returned pair is true if and only
  461. //! if the insertion takes place, and the iterator component of the pair
  462. //! points to the element with key equivalent to the key of x.
  463. //!
  464. //! <b>Complexity</b>: Logarithmic search time plus linear insertion
  465. //! to the elements with bigger keys than x.
  466. //!
  467. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  468. template <class... Args>
  469. std::pair<iterator,bool> emplace(Args&&... args)
  470. { return container_detail::force_copy< std::pair<iterator, bool> >(m_flat_tree.emplace_unique(boost::forward<Args>(args)...)); }
  471. //! <b>Effects</b>: Inserts an object of type T constructed with
  472. //! std::forward<Args>(args)... in the container if and only if there is
  473. //! no element in the container with key equivalent to the key of x.
  474. //! p is a hint pointing to where the insert should start to search.
  475. //!
  476. //! <b>Returns</b>: An iterator pointing to the element with key equivalent
  477. //! to the key of x.
  478. //!
  479. //! <b>Complexity</b>: Logarithmic search time (constant if x is inserted
  480. //! right before p) plus insertion linear to the elements with bigger keys than x.
  481. //!
  482. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  483. template <class... Args>
  484. iterator emplace_hint(const_iterator hint, Args&&... args)
  485. {
  486. return container_detail::force_copy<iterator>
  487. (m_flat_tree.emplace_hint_unique( container_detail::force_copy<impl_const_iterator>(hint)
  488. , boost::forward<Args>(args)...));
  489. }
  490. #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING
  491. #define BOOST_PP_LOCAL_MACRO(n) \
  492. BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \
  493. std::pair<iterator,bool> emplace(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
  494. { return container_detail::force_copy< std::pair<iterator, bool> > \
  495. (m_flat_tree.emplace_unique(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); } \
  496. \
  497. BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \
  498. iterator emplace_hint(const_iterator hint \
  499. BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
  500. { return container_detail::force_copy<iterator>(m_flat_tree.emplace_hint_unique \
  501. (container_detail::force_copy<impl_const_iterator>(hint) \
  502. BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); } \
  503. //!
  504. #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS)
  505. #include BOOST_PP_LOCAL_ITERATE()
  506. #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING
  507. //! <b>Effects</b>: Inserts x if and only if there is no element in the container
  508. //! with key equivalent to the key of x.
  509. //!
  510. //! <b>Returns</b>: The bool component of the returned pair is true if and only
  511. //! if the insertion takes place, and the iterator component of the pair
  512. //! points to the element with key equivalent to the key of x.
  513. //!
  514. //! <b>Complexity</b>: Logarithmic search time plus linear insertion
  515. //! to the elements with bigger keys than x.
  516. //!
  517. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  518. std::pair<iterator,bool> insert(const value_type& x)
  519. { return container_detail::force_copy<std::pair<iterator,bool> >(
  520. m_flat_tree.insert_unique(container_detail::force<impl_value_type>(x))); }
  521. //! <b>Effects</b>: Inserts a new value_type move constructed from the pair if and
  522. //! only if there is no element in the container with key equivalent to the key of x.
  523. //!
  524. //! <b>Returns</b>: The bool component of the returned pair is true if and only
  525. //! if the insertion takes place, and the iterator component of the pair
  526. //! points to the element with key equivalent to the key of x.
  527. //!
  528. //! <b>Complexity</b>: Logarithmic search time plus linear insertion
  529. //! to the elements with bigger keys than x.
  530. //!
  531. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  532. std::pair<iterator,bool> insert(BOOST_RV_REF(value_type) x)
  533. { return container_detail::force_copy<std::pair<iterator,bool> >(
  534. m_flat_tree.insert_unique(boost::move(container_detail::force<impl_value_type>(x)))); }
  535. //! <b>Effects</b>: Inserts a new value_type move constructed from the pair if and
  536. //! only if there is no element in the container with key equivalent to the key of x.
  537. //!
  538. //! <b>Returns</b>: The bool component of the returned pair is true if and only
  539. //! if the insertion takes place, and the iterator component of the pair
  540. //! points to the element with key equivalent to the key of x.
  541. //!
  542. //! <b>Complexity</b>: Logarithmic search time plus linear insertion
  543. //! to the elements with bigger keys than x.
  544. //!
  545. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  546. std::pair<iterator,bool> insert(BOOST_RV_REF(movable_value_type) x)
  547. {
  548. return container_detail::force_copy<std::pair<iterator,bool> >
  549. (m_flat_tree.insert_unique(boost::move(x)));
  550. }
  551. //! <b>Effects</b>: Inserts a copy of x in the container if and only if there is
  552. //! no element in the container with key equivalent to the key of x.
  553. //! p is a hint pointing to where the insert should start to search.
  554. //!
  555. //! <b>Returns</b>: An iterator pointing to the element with key equivalent
  556. //! to the key of x.
  557. //!
  558. //! <b>Complexity</b>: Logarithmic search time (constant if x is inserted
  559. //! right before p) plus insertion linear to the elements with bigger keys than x.
  560. //!
  561. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  562. iterator insert(const_iterator position, const value_type& x)
  563. {
  564. return container_detail::force_copy<iterator>(
  565. m_flat_tree.insert_unique( container_detail::force_copy<impl_const_iterator>(position)
  566. , container_detail::force<impl_value_type>(x)));
  567. }
  568. //! <b>Effects</b>: Inserts an element move constructed from x in the container.
  569. //! p is a hint pointing to where the insert should start to search.
  570. //!
  571. //! <b>Returns</b>: An iterator pointing to the element with key equivalent to the key of x.
  572. //!
  573. //! <b>Complexity</b>: Logarithmic search time (constant if x is inserted
  574. //! right before p) plus insertion linear to the elements with bigger keys than x.
  575. //!
  576. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  577. iterator insert(const_iterator position, BOOST_RV_REF(value_type) x)
  578. {
  579. return container_detail::force_copy<iterator>
  580. (m_flat_tree.insert_unique( container_detail::force_copy<impl_const_iterator>(position)
  581. , boost::move(container_detail::force<impl_value_type>(x))));
  582. }
  583. //! <b>Effects</b>: Inserts an element move constructed from x in the container.
  584. //! p is a hint pointing to where the insert should start to search.
  585. //!
  586. //! <b>Returns</b>: An iterator pointing to the element with key equivalent to the key of x.
  587. //!
  588. //! <b>Complexity</b>: Logarithmic search time (constant if x is inserted
  589. //! right before p) plus insertion linear to the elements with bigger keys than x.
  590. //!
  591. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  592. iterator insert(const_iterator position, BOOST_RV_REF(movable_value_type) x)
  593. {
  594. return container_detail::force_copy<iterator>(
  595. m_flat_tree.insert_unique(container_detail::force_copy<impl_const_iterator>(position), boost::move(x)));
  596. }
  597. //! <b>Requires</b>: first, last are not iterators into *this.
  598. //!
  599. //! <b>Effects</b>: inserts each element from the range [first,last) if and only
  600. //! if there is no element with key equivalent to the key of that element.
  601. //!
  602. //! <b>Complexity</b>: At most N log(size()+N) (N is the distance from first to last)
  603. //! search time plus N*size() insertion time.
  604. //!
  605. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  606. template <class InputIterator>
  607. void insert(InputIterator first, InputIterator last)
  608. { m_flat_tree.insert_unique(first, last); }
  609. //! <b>Requires</b>: first, last are not iterators into *this.
  610. //!
  611. //! <b>Requires</b>: [first ,last) must be ordered according to the predicate and must be
  612. //! unique values.
  613. //!
  614. //! <b>Effects</b>: inserts each element from the range [first,last) if and only
  615. //! if there is no element with key equivalent to the key of that element. This
  616. //! function is more efficient than the normal range creation for ordered ranges.
  617. //!
  618. //! <b>Complexity</b>: At most N log(size()+N) (N is the distance from first to last)
  619. //! search time plus N*size() insertion time.
  620. //!
  621. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  622. //!
  623. //! <b>Note</b>: Non-standard extension.
  624. template <class InputIterator>
  625. void insert(ordered_unique_range_t, InputIterator first, InputIterator last)
  626. { m_flat_tree.insert_unique(ordered_unique_range, first, last); }
  627. //! <b>Effects</b>: Erases the element pointed to by position.
  628. //!
  629. //! <b>Returns</b>: Returns an iterator pointing to the element immediately
  630. //! following q prior to the element being erased. If no such element exists,
  631. //! returns end().
  632. //!
  633. //! <b>Complexity</b>: Linear to the elements with keys bigger than position
  634. //!
  635. //! <b>Note</b>: Invalidates elements with keys
  636. //! not less than the erased element.
  637. iterator erase(const_iterator position)
  638. {
  639. return container_detail::force_copy<iterator>
  640. (m_flat_tree.erase(container_detail::force_copy<impl_const_iterator>(position)));
  641. }
  642. //! <b>Effects</b>: Erases all elements in the container with key equivalent to x.
  643. //!
  644. //! <b>Returns</b>: Returns the number of erased elements.
  645. //!
  646. //! <b>Complexity</b>: Logarithmic search time plus erasure time
  647. //! linear to the elements with bigger keys.
  648. size_type erase(const key_type& x)
  649. { return m_flat_tree.erase(x); }
  650. //! <b>Effects</b>: Erases all the elements in the range [first, last).
  651. //!
  652. //! <b>Returns</b>: Returns last.
  653. //!
  654. //! <b>Complexity</b>: size()*N where N is the distance from first to last.
  655. //!
  656. //! <b>Complexity</b>: Logarithmic search time plus erasure time
  657. //! linear to the elements with bigger keys.
  658. iterator erase(const_iterator first, const_iterator last)
  659. {
  660. return container_detail::force_copy<iterator>(
  661. m_flat_tree.erase( container_detail::force_copy<impl_const_iterator>(first)
  662. , container_detail::force_copy<impl_const_iterator>(last)));
  663. }
  664. //! <b>Effects</b>: Swaps the contents of *this and x.
  665. //!
  666. //! <b>Throws</b>: Nothing.
  667. //!
  668. //! <b>Complexity</b>: Constant.
  669. void swap(flat_map& x)
  670. { m_flat_tree.swap(x.m_flat_tree); }
  671. //! <b>Effects</b>: erase(a.begin(),a.end()).
  672. //!
  673. //! <b>Postcondition</b>: size() == 0.
  674. //!
  675. //! <b>Complexity</b>: linear in size().
  676. void clear() BOOST_CONTAINER_NOEXCEPT
  677. { m_flat_tree.clear(); }
  678. //////////////////////////////////////////////
  679. //
  680. // observers
  681. //
  682. //////////////////////////////////////////////
  683. //! <b>Effects</b>: Returns the comparison object out
  684. //! of which a was constructed.
  685. //!
  686. //! <b>Complexity</b>: Constant.
  687. key_compare key_comp() const
  688. { return container_detail::force_copy<key_compare>(m_flat_tree.key_comp()); }
  689. //! <b>Effects</b>: Returns an object of value_compare constructed out
  690. //! of the comparison object.
  691. //!
  692. //! <b>Complexity</b>: Constant.
  693. value_compare value_comp() const
  694. { return value_compare(container_detail::force_copy<key_compare>(m_flat_tree.key_comp())); }
  695. //////////////////////////////////////////////
  696. //
  697. // map operations
  698. //
  699. //////////////////////////////////////////////
  700. //! <b>Returns</b>: An iterator pointing to an element with the key
  701. //! equivalent to x, or end() if such an element is not found.
  702. //!
  703. //! <b>Complexity</b>: Logarithmic.
  704. iterator find(const key_type& x)
  705. { return container_detail::force_copy<iterator>(m_flat_tree.find(x)); }
  706. //! <b>Returns</b>: Allocator const_iterator pointing to an element with the key
  707. //! equivalent to x, or end() if such an element is not found.
  708. //!
  709. //! <b>Complexity</b>: Logarithmic.s
  710. const_iterator find(const key_type& x) const
  711. { return container_detail::force_copy<const_iterator>(m_flat_tree.find(x)); }
  712. //! <b>Returns</b>: The number of elements with key equivalent to x.
  713. //!
  714. //! <b>Complexity</b>: log(size())+count(k)
  715. size_type count(const key_type& x) const
  716. { return static_cast<size_type>(m_flat_tree.find(x) != m_flat_tree.end()); }
  717. //! <b>Returns</b>: An iterator pointing to the first element with key not less
  718. //! than k, or a.end() if such an element is not found.
  719. //!
  720. //! <b>Complexity</b>: Logarithmic
  721. iterator lower_bound(const key_type& x)
  722. { return container_detail::force_copy<iterator>(m_flat_tree.lower_bound(x)); }
  723. //! <b>Returns</b>: Allocator const iterator pointing to the first element with key not
  724. //! less than k, or a.end() if such an element is not found.
  725. //!
  726. //! <b>Complexity</b>: Logarithmic
  727. const_iterator lower_bound(const key_type& x) const
  728. { return container_detail::force_copy<const_iterator>(m_flat_tree.lower_bound(x)); }
  729. //! <b>Returns</b>: An iterator pointing to the first element with key not less
  730. //! than x, or end() if such an element is not found.
  731. //!
  732. //! <b>Complexity</b>: Logarithmic
  733. iterator upper_bound(const key_type& x)
  734. { return container_detail::force_copy<iterator>(m_flat_tree.upper_bound(x)); }
  735. //! <b>Returns</b>: Allocator const iterator pointing to the first element with key not
  736. //! less than x, or end() if such an element is not found.
  737. //!
  738. //! <b>Complexity</b>: Logarithmic
  739. const_iterator upper_bound(const key_type& x) const
  740. { return container_detail::force_copy<const_iterator>(m_flat_tree.upper_bound(x)); }
  741. //! <b>Effects</b>: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)).
  742. //!
  743. //! <b>Complexity</b>: Logarithmic
  744. std::pair<iterator,iterator> equal_range(const key_type& x)
  745. { return container_detail::force_copy<std::pair<iterator,iterator> >(m_flat_tree.equal_range(x)); }
  746. //! <b>Effects</b>: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)).
  747. //!
  748. //! <b>Complexity</b>: Logarithmic
  749. std::pair<const_iterator,const_iterator> equal_range(const key_type& x) const
  750. { return container_detail::force_copy<std::pair<const_iterator,const_iterator> >(m_flat_tree.equal_range(x)); }
  751. /// @cond
  752. template <class K1, class T1, class C1, class A1>
  753. friend bool operator== (const flat_map<K1, T1, C1, A1>&,
  754. const flat_map<K1, T1, C1, A1>&);
  755. template <class K1, class T1, class C1, class A1>
  756. friend bool operator< (const flat_map<K1, T1, C1, A1>&,
  757. const flat_map<K1, T1, C1, A1>&);
  758. private:
  759. mapped_type &priv_subscript(const key_type& k)
  760. {
  761. iterator i = lower_bound(k);
  762. // i->first is greater than or equivalent to k.
  763. if (i == end() || key_comp()(k, (*i).first)){
  764. container_detail::value_init<mapped_type> m;
  765. i = insert(i, impl_value_type(k, ::boost::move(m.m_t)));
  766. }
  767. return (*i).second;
  768. }
  769. mapped_type &priv_subscript(BOOST_RV_REF(key_type) mk)
  770. {
  771. key_type &k = mk;
  772. iterator i = lower_bound(k);
  773. // i->first is greater than or equivalent to k.
  774. if (i == end() || key_comp()(k, (*i).first)){
  775. container_detail::value_init<mapped_type> m;
  776. i = insert(i, impl_value_type(boost::move(k), ::boost::move(m.m_t)));
  777. }
  778. return (*i).second;
  779. }
  780. /// @endcond
  781. };
  782. template <class Key, class T, class Compare, class Allocator>
  783. inline bool operator==(const flat_map<Key,T,Compare,Allocator>& x,
  784. const flat_map<Key,T,Compare,Allocator>& y)
  785. { return x.m_flat_tree == y.m_flat_tree; }
  786. template <class Key, class T, class Compare, class Allocator>
  787. inline bool operator<(const flat_map<Key,T,Compare,Allocator>& x,
  788. const flat_map<Key,T,Compare,Allocator>& y)
  789. { return x.m_flat_tree < y.m_flat_tree; }
  790. template <class Key, class T, class Compare, class Allocator>
  791. inline bool operator!=(const flat_map<Key,T,Compare,Allocator>& x,
  792. const flat_map<Key,T,Compare,Allocator>& y)
  793. { return !(x == y); }
  794. template <class Key, class T, class Compare, class Allocator>
  795. inline bool operator>(const flat_map<Key,T,Compare,Allocator>& x,
  796. const flat_map<Key,T,Compare,Allocator>& y)
  797. { return y < x; }
  798. template <class Key, class T, class Compare, class Allocator>
  799. inline bool operator<=(const flat_map<Key,T,Compare,Allocator>& x,
  800. const flat_map<Key,T,Compare,Allocator>& y)
  801. { return !(y < x); }
  802. template <class Key, class T, class Compare, class Allocator>
  803. inline bool operator>=(const flat_map<Key,T,Compare,Allocator>& x,
  804. const flat_map<Key,T,Compare,Allocator>& y)
  805. { return !(x < y); }
  806. template <class Key, class T, class Compare, class Allocator>
  807. inline void swap(flat_map<Key,T,Compare,Allocator>& x,
  808. flat_map<Key,T,Compare,Allocator>& y)
  809. { x.swap(y); }
  810. /// @cond
  811. } //namespace container {
  812. //!has_trivial_destructor_after_move<> == true_type
  813. //!specialization for optimizations
  814. template <class K, class T, class C, class Allocator>
  815. struct has_trivial_destructor_after_move<boost::container::flat_map<K, T, C, Allocator> >
  816. {
  817. static const bool value = has_trivial_destructor_after_move<Allocator>::value && has_trivial_destructor_after_move<C>::value;
  818. };
  819. namespace container {
  820. // Forward declaration of operators < and ==, needed for friend declaration.
  821. template <class Key, class T, class Compare, class Allocator>
  822. class flat_multimap;
  823. template <class Key, class T, class Compare, class Allocator>
  824. inline bool operator==(const flat_multimap<Key,T,Compare,Allocator>& x,
  825. const flat_multimap<Key,T,Compare,Allocator>& y);
  826. template <class Key, class T, class Compare, class Allocator>
  827. inline bool operator<(const flat_multimap<Key,T,Compare,Allocator>& x,
  828. const flat_multimap<Key,T,Compare,Allocator>& y);
  829. /// @endcond
  830. //! A flat_multimap is a kind of associative container that supports equivalent keys
  831. //! (possibly containing multiple copies of the same key value) and provides for
  832. //! fast retrieval of values of another type T based on the keys. The flat_multimap
  833. //! class supports random-access iterators.
  834. //!
  835. //! A flat_multimap satisfies all of the requirements of a container and of a reversible
  836. //! container and of an associative container. For a
  837. //! flat_multimap<Key,T> the key_type is Key and the value_type is std::pair<Key,T>
  838. //! (unlike std::multimap<Key, T> which value_type is std::pair<<b>const</b> Key, T>).
  839. //!
  840. //! Compare is the ordering function for Keys (e.g. <i>std::less<Key></i>).
  841. //!
  842. //! Allocator is the allocator to allocate the value_types
  843. //! (e.g. <i>allocator< std::pair<Key, T> ></i>).
  844. //!
  845. //! flat_multimap is similar to std::multimap but it's implemented like an ordered vector.
  846. //! This means that inserting a new element into a flat_map invalidates
  847. //! previous iterators and references
  848. //!
  849. //! Erasing an element invalidates iterators and references
  850. //! pointing to elements that come after (their keys are bigger) the erased element.
  851. //!
  852. //! This container provides random-access iterators.
  853. #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
  854. template <class Key, class T, class Compare = std::less<Key>, class Allocator = std::allocator< std::pair< Key, T> > >
  855. #else
  856. template <class Key, class T, class Compare, class Allocator>
  857. #endif
  858. class flat_multimap
  859. {
  860. /// @cond
  861. private:
  862. BOOST_COPYABLE_AND_MOVABLE(flat_multimap)
  863. typedef container_detail::flat_tree<Key,
  864. std::pair<Key, T>,
  865. container_detail::select1st< std::pair<Key, T> >,
  866. Compare,
  867. Allocator> tree_t;
  868. //This is the real tree stored here. It's based on a movable pair
  869. typedef container_detail::flat_tree<Key,
  870. container_detail::pair<Key, T>,
  871. container_detail::select1st<container_detail::pair<Key, T> >,
  872. Compare,
  873. typename allocator_traits<Allocator>::template portable_rebind_alloc
  874. <container_detail::pair<Key, T> >::type> impl_tree_t;
  875. impl_tree_t m_flat_tree; // flat tree representing flat_map
  876. typedef typename impl_tree_t::value_type impl_value_type;
  877. typedef typename impl_tree_t::const_iterator impl_const_iterator;
  878. typedef typename impl_tree_t::allocator_type impl_allocator_type;
  879. typedef container_detail::flat_tree_value_compare
  880. < Compare
  881. , container_detail::select1st< std::pair<Key, T> >
  882. , std::pair<Key, T> > value_compare_impl;
  883. typedef typename container_detail::get_flat_tree_iterators
  884. <typename allocator_traits<Allocator>::pointer>::iterator iterator_impl;
  885. typedef typename container_detail::get_flat_tree_iterators
  886. <typename allocator_traits<Allocator>::pointer>::const_iterator const_iterator_impl;
  887. typedef typename container_detail::get_flat_tree_iterators
  888. <typename allocator_traits<Allocator>::pointer>::reverse_iterator reverse_iterator_impl;
  889. typedef typename container_detail::get_flat_tree_iterators
  890. <typename allocator_traits<Allocator>::pointer>::const_reverse_iterator const_reverse_iterator_impl;
  891. /// @endcond
  892. public:
  893. //////////////////////////////////////////////
  894. //
  895. // types
  896. //
  897. //////////////////////////////////////////////
  898. typedef Key key_type;
  899. typedef T mapped_type;
  900. typedef std::pair<Key, T> value_type;
  901. typedef typename boost::container::allocator_traits<Allocator>::pointer pointer;
  902. typedef typename boost::container::allocator_traits<Allocator>::const_pointer const_pointer;
  903. typedef typename boost::container::allocator_traits<Allocator>::reference reference;
  904. typedef typename boost::container::allocator_traits<Allocator>::const_reference const_reference;
  905. typedef typename boost::container::allocator_traits<Allocator>::size_type size_type;
  906. typedef typename boost::container::allocator_traits<Allocator>::difference_type difference_type;
  907. typedef Allocator allocator_type;
  908. typedef BOOST_CONTAINER_IMPDEF(Allocator) stored_allocator_type;
  909. typedef BOOST_CONTAINER_IMPDEF(value_compare_impl) value_compare;
  910. typedef Compare key_compare;
  911. typedef BOOST_CONTAINER_IMPDEF(iterator_impl) iterator;
  912. typedef BOOST_CONTAINER_IMPDEF(const_iterator_impl) const_iterator;
  913. typedef BOOST_CONTAINER_IMPDEF(reverse_iterator_impl) reverse_iterator;
  914. typedef BOOST_CONTAINER_IMPDEF(const_reverse_iterator_impl) const_reverse_iterator;
  915. typedef BOOST_CONTAINER_IMPDEF(impl_value_type) movable_value_type;
  916. //////////////////////////////////////////////
  917. //
  918. // construct/copy/destroy
  919. //
  920. //////////////////////////////////////////////
  921. //! <b>Effects</b>: Default constructs an empty flat_map.
  922. //!
  923. //! <b>Complexity</b>: Constant.
  924. flat_multimap()
  925. : m_flat_tree() {}
  926. //! <b>Effects</b>: Constructs an empty flat_multimap using the specified comparison
  927. //! object and allocator.
  928. //!
  929. //! <b>Complexity</b>: Constant.
  930. explicit flat_multimap(const Compare& comp,
  931. const allocator_type& a = allocator_type())
  932. : m_flat_tree(comp, container_detail::force<impl_allocator_type>(a))
  933. {}
  934. //! <b>Effects</b>: Constructs an empty flat_multimap using the specified allocator.
  935. //!
  936. //! <b>Complexity</b>: Constant.
  937. explicit flat_multimap(const allocator_type& a)
  938. : m_flat_tree(container_detail::force<impl_allocator_type>(a))
  939. {}
  940. //! <b>Effects</b>: Constructs an empty flat_multimap using the specified comparison object
  941. //! and allocator, and inserts elements from the range [first ,last ).
  942. //!
  943. //! <b>Complexity</b>: Linear in N if the range [first ,last ) is already sorted using
  944. //! comp and otherwise N logN, where N is last - first.
  945. template <class InputIterator>
  946. flat_multimap(InputIterator first, InputIterator last,
  947. const Compare& comp = Compare(),
  948. const allocator_type& a = allocator_type())
  949. : m_flat_tree(false, first, last, comp, container_detail::force<impl_allocator_type>(a))
  950. {}
  951. //! <b>Effects</b>: Constructs an empty flat_multimap using the specified comparison object and
  952. //! allocator, and inserts elements from the ordered range [first ,last). This function
  953. //! is more efficient than the normal range creation for ordered ranges.
  954. //!
  955. //! <b>Requires</b>: [first ,last) must be ordered according to the predicate.
  956. //!
  957. //! <b>Complexity</b>: Linear in N.
  958. //!
  959. //! <b>Note</b>: Non-standard extension.
  960. template <class InputIterator>
  961. flat_multimap(ordered_range_t, InputIterator first, InputIterator last,
  962. const Compare& comp = Compare(),
  963. const allocator_type& a = allocator_type())
  964. : m_flat_tree(ordered_range, first, last, comp, a)
  965. {}
  966. //! <b>Effects</b>: Copy constructs a flat_multimap.
  967. //!
  968. //! <b>Complexity</b>: Linear in x.size().
  969. flat_multimap(const flat_multimap& x)
  970. : m_flat_tree(x.m_flat_tree) { }
  971. //! <b>Effects</b>: Move constructs a flat_multimap. Constructs *this using x's resources.
  972. //!
  973. //! <b>Complexity</b>: Constant.
  974. //!
  975. //! <b>Postcondition</b>: x is emptied.
  976. flat_multimap(BOOST_RV_REF(flat_multimap) x)
  977. : m_flat_tree(boost::move(x.m_flat_tree))
  978. {}
  979. //! <b>Effects</b>: Copy constructs a flat_multimap using the specified allocator.
  980. //!
  981. //! <b>Complexity</b>: Linear in x.size().
  982. flat_multimap(const flat_multimap& x, const allocator_type &a)
  983. : m_flat_tree(x.m_flat_tree, a)
  984. {}
  985. //! <b>Effects</b>: Move constructs a flat_multimap using the specified allocator.
  986. //! Constructs *this using x's resources.
  987. //!
  988. //! <b>Complexity</b>: Constant if a == x.get_allocator(), linear otherwise.
  989. flat_multimap(BOOST_RV_REF(flat_multimap) x, const allocator_type &a)
  990. : m_flat_tree(boost::move(x.m_flat_tree), a)
  991. { }
  992. //! <b>Effects</b>: Makes *this a copy of x.
  993. //!
  994. //! <b>Complexity</b>: Linear in x.size().
  995. flat_multimap& operator=(BOOST_COPY_ASSIGN_REF(flat_multimap) x)
  996. { m_flat_tree = x.m_flat_tree; return *this; }
  997. //! <b>Effects</b>: this->swap(x.get()).
  998. //!
  999. //! <b>Complexity</b>: Constant.
  1000. flat_multimap& operator=(BOOST_RV_REF(flat_multimap) mx)
  1001. { m_flat_tree = boost::move(mx.m_flat_tree); return *this; }
  1002. //! <b>Effects</b>: Returns a copy of the Allocator that
  1003. //! was passed to the object's constructor.
  1004. //!
  1005. //! <b>Complexity</b>: Constant.
  1006. allocator_type get_allocator() const BOOST_CONTAINER_NOEXCEPT
  1007. { return container_detail::force_copy<allocator_type>(m_flat_tree.get_allocator()); }
  1008. //! <b>Effects</b>: Returns a reference to the internal allocator.
  1009. //!
  1010. //! <b>Throws</b>: Nothing
  1011. //!
  1012. //! <b>Complexity</b>: Constant.
  1013. //!
  1014. //! <b>Note</b>: Non-standard extension.
  1015. stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT
  1016. { return container_detail::force<stored_allocator_type>(m_flat_tree.get_stored_allocator()); }
  1017. //! <b>Effects</b>: Returns a reference to the internal allocator.
  1018. //!
  1019. //! <b>Throws</b>: Nothing
  1020. //!
  1021. //! <b>Complexity</b>: Constant.
  1022. //!
  1023. //! <b>Note</b>: Non-standard extension.
  1024. const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT
  1025. { return container_detail::force<stored_allocator_type>(m_flat_tree.get_stored_allocator()); }
  1026. //////////////////////////////////////////////
  1027. //
  1028. // iterators
  1029. //
  1030. //////////////////////////////////////////////
  1031. //! <b>Effects</b>: Returns an iterator to the first element contained in the container.
  1032. //!
  1033. //! <b>Throws</b>: Nothing.
  1034. //!
  1035. //! <b>Complexity</b>: Constant.
  1036. iterator begin() BOOST_CONTAINER_NOEXCEPT
  1037. { return container_detail::force_copy<iterator>(m_flat_tree.begin()); }
  1038. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the container.
  1039. //!
  1040. //! <b>Throws</b>: Nothing.
  1041. //!
  1042. //! <b>Complexity</b>: Constant.
  1043. const_iterator begin() const BOOST_CONTAINER_NOEXCEPT
  1044. { return container_detail::force_copy<const_iterator>(m_flat_tree.begin()); }
  1045. //! <b>Effects</b>: Returns an iterator to the end of the container.
  1046. //!
  1047. //! <b>Throws</b>: Nothing.
  1048. //!
  1049. //! <b>Complexity</b>: Constant.
  1050. iterator end() BOOST_CONTAINER_NOEXCEPT
  1051. { return container_detail::force_copy<iterator>(m_flat_tree.end()); }
  1052. //! <b>Effects</b>: Returns a const_iterator to the end of the container.
  1053. //!
  1054. //! <b>Throws</b>: Nothing.
  1055. //!
  1056. //! <b>Complexity</b>: Constant.
  1057. const_iterator end() const BOOST_CONTAINER_NOEXCEPT
  1058. { return container_detail::force_copy<const_iterator>(m_flat_tree.end()); }
  1059. //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
  1060. //! of the reversed container.
  1061. //!
  1062. //! <b>Throws</b>: Nothing.
  1063. //!
  1064. //! <b>Complexity</b>: Constant.
  1065. reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT
  1066. { return container_detail::force_copy<reverse_iterator>(m_flat_tree.rbegin()); }
  1067. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  1068. //! of the reversed container.
  1069. //!
  1070. //! <b>Throws</b>: Nothing.
  1071. //!
  1072. //! <b>Complexity</b>: Constant.
  1073. const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT
  1074. { return container_detail::force_copy<const_reverse_iterator>(m_flat_tree.rbegin()); }
  1075. //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
  1076. //! of the reversed container.
  1077. //!
  1078. //! <b>Throws</b>: Nothing.
  1079. //!
  1080. //! <b>Complexity</b>: Constant.
  1081. reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT
  1082. { return container_detail::force_copy<reverse_iterator>(m_flat_tree.rend()); }
  1083. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  1084. //! of the reversed container.
  1085. //!
  1086. //! <b>Throws</b>: Nothing.
  1087. //!
  1088. //! <b>Complexity</b>: Constant.
  1089. const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT
  1090. { return container_detail::force_copy<const_reverse_iterator>(m_flat_tree.rend()); }
  1091. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the container.
  1092. //!
  1093. //! <b>Throws</b>: Nothing.
  1094. //!
  1095. //! <b>Complexity</b>: Constant.
  1096. const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT
  1097. { return container_detail::force_copy<const_iterator>(m_flat_tree.cbegin()); }
  1098. //! <b>Effects</b>: Returns a const_iterator to the end of the container.
  1099. //!
  1100. //! <b>Throws</b>: Nothing.
  1101. //!
  1102. //! <b>Complexity</b>: Constant.
  1103. const_iterator cend() const BOOST_CONTAINER_NOEXCEPT
  1104. { return container_detail::force_copy<const_iterator>(m_flat_tree.cend()); }
  1105. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  1106. //! of the reversed container.
  1107. //!
  1108. //! <b>Throws</b>: Nothing.
  1109. //!
  1110. //! <b>Complexity</b>: Constant.
  1111. const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT
  1112. { return container_detail::force_copy<const_reverse_iterator>(m_flat_tree.crbegin()); }
  1113. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  1114. //! of the reversed container.
  1115. //!
  1116. //! <b>Throws</b>: Nothing.
  1117. //!
  1118. //! <b>Complexity</b>: Constant.
  1119. const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT
  1120. { return container_detail::force_copy<const_reverse_iterator>(m_flat_tree.crend()); }
  1121. //////////////////////////////////////////////
  1122. //
  1123. // capacity
  1124. //
  1125. //////////////////////////////////////////////
  1126. //! <b>Effects</b>: Returns true if the container contains no elements.
  1127. //!
  1128. //! <b>Throws</b>: Nothing.
  1129. //!
  1130. //! <b>Complexity</b>: Constant.
  1131. bool empty() const BOOST_CONTAINER_NOEXCEPT
  1132. { return m_flat_tree.empty(); }
  1133. //! <b>Effects</b>: Returns the number of the elements contained in the container.
  1134. //!
  1135. //! <b>Throws</b>: Nothing.
  1136. //!
  1137. //! <b>Complexity</b>: Constant.
  1138. size_type size() const BOOST_CONTAINER_NOEXCEPT
  1139. { return m_flat_tree.size(); }
  1140. //! <b>Effects</b>: Returns the largest possible size of the container.
  1141. //!
  1142. //! <b>Throws</b>: Nothing.
  1143. //!
  1144. //! <b>Complexity</b>: Constant.
  1145. size_type max_size() const BOOST_CONTAINER_NOEXCEPT
  1146. { return m_flat_tree.max_size(); }
  1147. //! <b>Effects</b>: Number of elements for which memory has been allocated.
  1148. //! capacity() is always greater than or equal to size().
  1149. //!
  1150. //! <b>Throws</b>: Nothing.
  1151. //!
  1152. //! <b>Complexity</b>: Constant.
  1153. size_type capacity() const BOOST_CONTAINER_NOEXCEPT
  1154. { return m_flat_tree.capacity(); }
  1155. //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
  1156. //! effect. Otherwise, it is a request for allocation of additional memory.
  1157. //! If the request is successful, then capacity() is greater than or equal to
  1158. //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
  1159. //!
  1160. //! <b>Throws</b>: If memory allocation allocation throws or T's copy constructor throws.
  1161. //!
  1162. //! <b>Note</b>: If capacity() is less than "cnt", iterators and references to
  1163. //! to values might be invalidated.
  1164. void reserve(size_type cnt)
  1165. { m_flat_tree.reserve(cnt); }
  1166. //! <b>Effects</b>: Tries to deallocate the excess of memory created
  1167. // with previous allocations. The size of the vector is unchanged
  1168. //!
  1169. //! <b>Throws</b>: If memory allocation throws, or T's copy constructor throws.
  1170. //!
  1171. //! <b>Complexity</b>: Linear to size().
  1172. void shrink_to_fit()
  1173. { m_flat_tree.shrink_to_fit(); }
  1174. //////////////////////////////////////////////
  1175. //
  1176. // modifiers
  1177. //
  1178. //////////////////////////////////////////////
  1179. #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1180. //! <b>Effects</b>: Inserts an object of type T constructed with
  1181. //! std::forward<Args>(args)... and returns the iterator pointing to the
  1182. //! newly inserted element.
  1183. //!
  1184. //! <b>Complexity</b>: Logarithmic search time plus linear insertion
  1185. //! to the elements with bigger keys than x.
  1186. //!
  1187. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  1188. template <class... Args>
  1189. iterator emplace(Args&&... args)
  1190. { return container_detail::force_copy<iterator>(m_flat_tree.emplace_equal(boost::forward<Args>(args)...)); }
  1191. //! <b>Effects</b>: Inserts an object of type T constructed with
  1192. //! std::forward<Args>(args)... in the container.
  1193. //! p is a hint pointing to where the insert should start to search.
  1194. //!
  1195. //! <b>Returns</b>: An iterator pointing to the element with key equivalent
  1196. //! to the key of x.
  1197. //!
  1198. //! <b>Complexity</b>: Logarithmic search time (constant time if the value
  1199. //! is to be inserted before p) plus linear insertion
  1200. //! to the elements with bigger keys than x.
  1201. //!
  1202. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  1203. template <class... Args>
  1204. iterator emplace_hint(const_iterator hint, Args&&... args)
  1205. {
  1206. return container_detail::force_copy<iterator>(m_flat_tree.emplace_hint_equal
  1207. (container_detail::force_copy<impl_const_iterator>(hint), boost::forward<Args>(args)...));
  1208. }
  1209. #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING
  1210. #define BOOST_PP_LOCAL_MACRO(n) \
  1211. BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \
  1212. iterator emplace(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
  1213. { return container_detail::force_copy<iterator>(m_flat_tree.emplace_equal \
  1214. (BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); } \
  1215. \
  1216. BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \
  1217. iterator emplace_hint(const_iterator hint \
  1218. BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
  1219. { return container_detail::force_copy<iterator>(m_flat_tree.emplace_hint_equal \
  1220. (container_detail::force_copy<impl_const_iterator>(hint) \
  1221. BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); } \
  1222. //!
  1223. #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS)
  1224. #include BOOST_PP_LOCAL_ITERATE()
  1225. #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING
  1226. //! <b>Effects</b>: Inserts x and returns the iterator pointing to the
  1227. //! newly inserted element.
  1228. //!
  1229. //! <b>Complexity</b>: Logarithmic search time plus linear insertion
  1230. //! to the elements with bigger keys than x.
  1231. //!
  1232. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  1233. iterator insert(const value_type& x)
  1234. {
  1235. return container_detail::force_copy<iterator>(
  1236. m_flat_tree.insert_equal(container_detail::force<impl_value_type>(x)));
  1237. }
  1238. //! <b>Effects</b>: Inserts a new value move-constructed from x and returns
  1239. //! the iterator pointing to the newly inserted element.
  1240. //!
  1241. //! <b>Complexity</b>: Logarithmic search time plus linear insertion
  1242. //! to the elements with bigger keys than x.
  1243. //!
  1244. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  1245. iterator insert(BOOST_RV_REF(value_type) x)
  1246. { return container_detail::force_copy<iterator>(m_flat_tree.insert_equal(boost::move(x))); }
  1247. //! <b>Effects</b>: Inserts a new value move-constructed from x and returns
  1248. //! the iterator pointing to the newly inserted element.
  1249. //!
  1250. //! <b>Complexity</b>: Logarithmic search time plus linear insertion
  1251. //! to the elements with bigger keys than x.
  1252. //!
  1253. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  1254. iterator insert(BOOST_RV_REF(impl_value_type) x)
  1255. { return container_detail::force_copy<iterator>(m_flat_tree.insert_equal(boost::move(x))); }
  1256. //! <b>Effects</b>: Inserts a copy of x in the container.
  1257. //! p is a hint pointing to where the insert should start to search.
  1258. //!
  1259. //! <b>Returns</b>: An iterator pointing to the element with key equivalent
  1260. //! to the key of x.
  1261. //!
  1262. //! <b>Complexity</b>: Logarithmic search time (constant time if the value
  1263. //! is to be inserted before p) plus linear insertion
  1264. //! to the elements with bigger keys than x.
  1265. //!
  1266. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  1267. iterator insert(const_iterator position, const value_type& x)
  1268. {
  1269. return container_detail::force_copy<iterator>
  1270. (m_flat_tree.insert_equal( container_detail::force_copy<impl_const_iterator>(position)
  1271. , container_detail::force<impl_value_type>(x)));
  1272. }
  1273. //! <b>Effects</b>: Inserts a value move constructed from x in the container.
  1274. //! p is a hint pointing to where the insert should start to search.
  1275. //!
  1276. //! <b>Returns</b>: An iterator pointing to the element with key equivalent
  1277. //! to the key of x.
  1278. //!
  1279. //! <b>Complexity</b>: Logarithmic search time (constant time if the value
  1280. //! is to be inserted before p) plus linear insertion
  1281. //! to the elements with bigger keys than x.
  1282. //!
  1283. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  1284. iterator insert(const_iterator position, BOOST_RV_REF(value_type) x)
  1285. {
  1286. return container_detail::force_copy<iterator>
  1287. (m_flat_tree.insert_equal(container_detail::force_copy<impl_const_iterator>(position)
  1288. , boost::move(x)));
  1289. }
  1290. //! <b>Effects</b>: Inserts a value move constructed from x in the container.
  1291. //! p is a hint pointing to where the insert should start to search.
  1292. //!
  1293. //! <b>Returns</b>: An iterator pointing to the element with key equivalent
  1294. //! to the key of x.
  1295. //!
  1296. //! <b>Complexity</b>: Logarithmic search time (constant time if the value
  1297. //! is to be inserted before p) plus linear insertion
  1298. //! to the elements with bigger keys than x.
  1299. //!
  1300. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  1301. iterator insert(const_iterator position, BOOST_RV_REF(impl_value_type) x)
  1302. {
  1303. return container_detail::force_copy<iterator>(
  1304. m_flat_tree.insert_equal(container_detail::force_copy<impl_const_iterator>(position), boost::move(x)));
  1305. }
  1306. //! <b>Requires</b>: first, last are not iterators into *this.
  1307. //!
  1308. //! <b>Effects</b>: inserts each element from the range [first,last) .
  1309. //!
  1310. //! <b>Complexity</b>: At most N log(size()+N) (N is the distance from first to last)
  1311. //! search time plus N*size() insertion time.
  1312. //!
  1313. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  1314. template <class InputIterator>
  1315. void insert(InputIterator first, InputIterator last)
  1316. { m_flat_tree.insert_equal(first, last); }
  1317. //! <b>Requires</b>: first, last are not iterators into *this.
  1318. //!
  1319. //! <b>Requires</b>: [first ,last) must be ordered according to the predicate.
  1320. //!
  1321. //! <b>Effects</b>: inserts each element from the range [first,last) if and only
  1322. //! if there is no element with key equivalent to the key of that element. This
  1323. //! function is more efficient than the normal range creation for ordered ranges.
  1324. //!
  1325. //! <b>Complexity</b>: At most N log(size()+N) (N is the distance from first to last)
  1326. //! search time plus N*size() insertion time.
  1327. //!
  1328. //! <b>Note</b>: If an element is inserted it might invalidate elements.
  1329. //!
  1330. //! <b>Note</b>: Non-standard extension.
  1331. template <class InputIterator>
  1332. void insert(ordered_range_t, InputIterator first, InputIterator last)
  1333. { m_flat_tree.insert_equal(ordered_range, first, last); }
  1334. //! <b>Effects</b>: Erases the element pointed to by position.
  1335. //!
  1336. //! <b>Returns</b>: Returns an iterator pointing to the element immediately
  1337. //! following q prior to the element being erased. If no such element exists,
  1338. //! returns end().
  1339. //!
  1340. //! <b>Complexity</b>: Linear to the elements with keys bigger than position
  1341. //!
  1342. //! <b>Note</b>: Invalidates elements with keys
  1343. //! not less than the erased element.
  1344. iterator erase(const_iterator position)
  1345. {
  1346. return container_detail::force_copy<iterator>(
  1347. m_flat_tree.erase(container_detail::force_copy<impl_const_iterator>(position)));
  1348. }
  1349. //! <b>Effects</b>: Erases all elements in the container with key equivalent to x.
  1350. //!
  1351. //! <b>Returns</b>: Returns the number of erased elements.
  1352. //!
  1353. //! <b>Complexity</b>: Logarithmic search time plus erasure time
  1354. //! linear to the elements with bigger keys.
  1355. size_type erase(const key_type& x)
  1356. { return m_flat_tree.erase(x); }
  1357. //! <b>Effects</b>: Erases all the elements in the range [first, last).
  1358. //!
  1359. //! <b>Returns</b>: Returns last.
  1360. //!
  1361. //! <b>Complexity</b>: size()*N where N is the distance from first to last.
  1362. //!
  1363. //! <b>Complexity</b>: Logarithmic search time plus erasure time
  1364. //! linear to the elements with bigger keys.
  1365. iterator erase(const_iterator first, const_iterator last)
  1366. {
  1367. return container_detail::force_copy<iterator>
  1368. (m_flat_tree.erase( container_detail::force_copy<impl_const_iterator>(first)
  1369. , container_detail::force_copy<impl_const_iterator>(last)));
  1370. }
  1371. //! <b>Effects</b>: Swaps the contents of *this and x.
  1372. //!
  1373. //! <b>Throws</b>: Nothing.
  1374. //!
  1375. //! <b>Complexity</b>: Constant.
  1376. void swap(flat_multimap& x)
  1377. { m_flat_tree.swap(x.m_flat_tree); }
  1378. //! <b>Effects</b>: erase(a.begin(),a.end()).
  1379. //!
  1380. //! <b>Postcondition</b>: size() == 0.
  1381. //!
  1382. //! <b>Complexity</b>: linear in size().
  1383. void clear() BOOST_CONTAINER_NOEXCEPT
  1384. { m_flat_tree.clear(); }
  1385. //////////////////////////////////////////////
  1386. //
  1387. // observers
  1388. //
  1389. //////////////////////////////////////////////
  1390. //! <b>Effects</b>: Returns the comparison object out
  1391. //! of which a was constructed.
  1392. //!
  1393. //! <b>Complexity</b>: Constant.
  1394. key_compare key_comp() const
  1395. { return container_detail::force_copy<key_compare>(m_flat_tree.key_comp()); }
  1396. //! <b>Effects</b>: Returns an object of value_compare constructed out
  1397. //! of the comparison object.
  1398. //!
  1399. //! <b>Complexity</b>: Constant.
  1400. value_compare value_comp() const
  1401. { return value_compare(container_detail::force_copy<key_compare>(m_flat_tree.key_comp())); }
  1402. //////////////////////////////////////////////
  1403. //
  1404. // map operations
  1405. //
  1406. //////////////////////////////////////////////
  1407. //! <b>Returns</b>: An iterator pointing to an element with the key
  1408. //! equivalent to x, or end() if such an element is not found.
  1409. //!
  1410. //! <b>Complexity</b>: Logarithmic.
  1411. iterator find(const key_type& x)
  1412. { return container_detail::force_copy<iterator>(m_flat_tree.find(x)); }
  1413. //! <b>Returns</b>: An const_iterator pointing to an element with the key
  1414. //! equivalent to x, or end() if such an element is not found.
  1415. //!
  1416. //! <b>Complexity</b>: Logarithmic.
  1417. const_iterator find(const key_type& x) const
  1418. { return container_detail::force_copy<const_iterator>(m_flat_tree.find(x)); }
  1419. //! <b>Returns</b>: The number of elements with key equivalent to x.
  1420. //!
  1421. //! <b>Complexity</b>: log(size())+count(k)
  1422. size_type count(const key_type& x) const
  1423. { return m_flat_tree.count(x); }
  1424. //! <b>Returns</b>: An iterator pointing to the first element with key not less
  1425. //! than k, or a.end() if such an element is not found.
  1426. //!
  1427. //! <b>Complexity</b>: Logarithmic
  1428. iterator lower_bound(const key_type& x)
  1429. { return container_detail::force_copy<iterator>(m_flat_tree.lower_bound(x)); }
  1430. //! <b>Returns</b>: Allocator const iterator pointing to the first element with key
  1431. //! not less than k, or a.end() if such an element is not found.
  1432. //!
  1433. //! <b>Complexity</b>: Logarithmic
  1434. const_iterator lower_bound(const key_type& x) const
  1435. { return container_detail::force_copy<const_iterator>(m_flat_tree.lower_bound(x)); }
  1436. //! <b>Returns</b>: An iterator pointing to the first element with key not less
  1437. //! than x, or end() if such an element is not found.
  1438. //!
  1439. //! <b>Complexity</b>: Logarithmic
  1440. iterator upper_bound(const key_type& x)
  1441. {return container_detail::force_copy<iterator>(m_flat_tree.upper_bound(x)); }
  1442. //! <b>Returns</b>: Allocator const iterator pointing to the first element with key
  1443. //! not less than x, or end() if such an element is not found.
  1444. //!
  1445. //! <b>Complexity</b>: Logarithmic
  1446. const_iterator upper_bound(const key_type& x) const
  1447. { return container_detail::force_copy<const_iterator>(m_flat_tree.upper_bound(x)); }
  1448. //! <b>Effects</b>: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)).
  1449. //!
  1450. //! <b>Complexity</b>: Logarithmic
  1451. std::pair<iterator,iterator> equal_range(const key_type& x)
  1452. { return container_detail::force_copy<std::pair<iterator,iterator> >(m_flat_tree.equal_range(x)); }
  1453. //! <b>Effects</b>: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)).
  1454. //!
  1455. //! <b>Complexity</b>: Logarithmic
  1456. std::pair<const_iterator,const_iterator> equal_range(const key_type& x) const
  1457. { return container_detail::force_copy<std::pair<const_iterator,const_iterator> >(m_flat_tree.equal_range(x)); }
  1458. /// @cond
  1459. template <class K1, class T1, class C1, class A1>
  1460. friend bool operator== (const flat_multimap<K1, T1, C1, A1>& x,
  1461. const flat_multimap<K1, T1, C1, A1>& y);
  1462. template <class K1, class T1, class C1, class A1>
  1463. friend bool operator< (const flat_multimap<K1, T1, C1, A1>& x,
  1464. const flat_multimap<K1, T1, C1, A1>& y);
  1465. /// @endcond
  1466. };
  1467. template <class Key, class T, class Compare, class Allocator>
  1468. inline bool operator==(const flat_multimap<Key,T,Compare,Allocator>& x,
  1469. const flat_multimap<Key,T,Compare,Allocator>& y)
  1470. { return x.m_flat_tree == y.m_flat_tree; }
  1471. template <class Key, class T, class Compare, class Allocator>
  1472. inline bool operator<(const flat_multimap<Key,T,Compare,Allocator>& x,
  1473. const flat_multimap<Key,T,Compare,Allocator>& y)
  1474. { return x.m_flat_tree < y.m_flat_tree; }
  1475. template <class Key, class T, class Compare, class Allocator>
  1476. inline bool operator!=(const flat_multimap<Key,T,Compare,Allocator>& x,
  1477. const flat_multimap<Key,T,Compare,Allocator>& y)
  1478. { return !(x == y); }
  1479. template <class Key, class T, class Compare, class Allocator>
  1480. inline bool operator>(const flat_multimap<Key,T,Compare,Allocator>& x,
  1481. const flat_multimap<Key,T,Compare,Allocator>& y)
  1482. { return y < x; }
  1483. template <class Key, class T, class Compare, class Allocator>
  1484. inline bool operator<=(const flat_multimap<Key,T,Compare,Allocator>& x,
  1485. const flat_multimap<Key,T,Compare,Allocator>& y)
  1486. { return !(y < x); }
  1487. template <class Key, class T, class Compare, class Allocator>
  1488. inline bool operator>=(const flat_multimap<Key,T,Compare,Allocator>& x,
  1489. const flat_multimap<Key,T,Compare,Allocator>& y)
  1490. { return !(x < y); }
  1491. template <class Key, class T, class Compare, class Allocator>
  1492. inline void swap(flat_multimap<Key,T,Compare,Allocator>& x, flat_multimap<Key,T,Compare,Allocator>& y)
  1493. { x.swap(y); }
  1494. }}
  1495. /// @cond
  1496. namespace boost {
  1497. //!has_trivial_destructor_after_move<> == true_type
  1498. //!specialization for optimizations
  1499. template <class K, class T, class C, class Allocator>
  1500. struct has_trivial_destructor_after_move< boost::container::flat_multimap<K, T, C, Allocator> >
  1501. {
  1502. static const bool value = has_trivial_destructor_after_move<Allocator>::value && has_trivial_destructor_after_move<C>::value;
  1503. };
  1504. } //namespace boost {
  1505. /// @endcond
  1506. #include <boost/container/detail/config_end.hpp>
  1507. #endif /* BOOST_CONTAINER_FLAT_MAP_HPP */