sgtree.hpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-2013
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. //
  13. // The option that yields to non-floating point 1/sqrt(2) alpha is taken
  14. // from the scapegoat tree implementation of the PSPP library.
  15. //
  16. /////////////////////////////////////////////////////////////////////////////
  17. #ifndef BOOST_INTRUSIVE_SGTREE_HPP
  18. #define BOOST_INTRUSIVE_SGTREE_HPP
  19. #include <boost/intrusive/detail/config_begin.hpp>
  20. #include <algorithm>
  21. #include <cstddef>
  22. #include <functional>
  23. #include <iterator>
  24. #include <utility>
  25. #include <cmath>
  26. #include <cstddef>
  27. #include <boost/intrusive/detail/assert.hpp>
  28. #include <boost/static_assert.hpp>
  29. #include <boost/intrusive/intrusive_fwd.hpp>
  30. #include <boost/intrusive/bs_set_hook.hpp>
  31. #include <boost/intrusive/bstree.hpp>
  32. #include <boost/intrusive/detail/tree_node.hpp>
  33. #include <boost/intrusive/detail/ebo_functor_holder.hpp>
  34. #include <boost/intrusive/pointer_traits.hpp>
  35. #include <boost/intrusive/detail/mpl.hpp>
  36. #include <boost/intrusive/detail/utilities.hpp>
  37. #include <boost/intrusive/options.hpp>
  38. #include <boost/intrusive/sgtree_algorithms.hpp>
  39. #include <boost/intrusive/link_mode.hpp>
  40. #include <boost/move/move.hpp>
  41. namespace boost {
  42. namespace intrusive {
  43. /// @cond
  44. namespace detail{
  45. //! Returns floor(log2(n)/log2(sqrt(2))) -> floor(2*log2(n))
  46. //! Undefined if N is 0.
  47. //!
  48. //! This function does not use float point operations.
  49. inline std::size_t calculate_h_sqrt2 (std::size_t n)
  50. {
  51. std::size_t f_log2 = detail::floor_log2(n);
  52. return (2*f_log2) + (n >= detail::sqrt2_pow_2xplus1 (f_log2));
  53. }
  54. struct h_alpha_sqrt2_t
  55. {
  56. h_alpha_sqrt2_t(void){}
  57. std::size_t operator()(std::size_t n) const
  58. { return calculate_h_sqrt2(n); }
  59. };
  60. struct alpha_0_75_by_max_size_t
  61. {
  62. alpha_0_75_by_max_size_t(void){}
  63. std::size_t operator()(std::size_t max_tree_size) const
  64. {
  65. const std::size_t max_tree_size_limit = ((~std::size_t(0))/std::size_t(3));
  66. return max_tree_size > max_tree_size_limit ? max_tree_size/4*3 : max_tree_size*3/4;
  67. }
  68. };
  69. struct h_alpha_t
  70. {
  71. explicit h_alpha_t(float inv_minus_logalpha)
  72. : inv_minus_logalpha_(inv_minus_logalpha)
  73. {}
  74. std::size_t operator()(std::size_t n) const
  75. {
  76. //Returns floor(log2(1/alpha(n))) ->
  77. // floor(log2(n)/log(1/alpha)) ->
  78. // floor(log2(n)/(-log2(alpha)))
  79. //return static_cast<std::size_t>(std::log(float(n))*inv_minus_logalpha_);
  80. return static_cast<std::size_t>(detail::fast_log2(float(n))*inv_minus_logalpha_);
  81. }
  82. private:
  83. //Since the function will be repeatedly called
  84. //precalculate constant data to avoid repeated
  85. //calls to log and division.
  86. //This will store 1/(-std::log2(alpha_))
  87. float inv_minus_logalpha_;
  88. };
  89. struct alpha_by_max_size_t
  90. {
  91. explicit alpha_by_max_size_t(float alpha)
  92. : alpha_(alpha)
  93. {}
  94. float operator()(std::size_t max_tree_size) const
  95. { return float(max_tree_size)*alpha_; }
  96. private:
  97. float alpha_;
  98. };
  99. template<bool Activate, class SizeType>
  100. struct alpha_holder
  101. {
  102. typedef boost::intrusive::detail::h_alpha_t h_alpha_t;
  103. typedef boost::intrusive::detail::alpha_by_max_size_t multiply_by_alpha_t;
  104. alpha_holder() : max_tree_size_(0)
  105. { set_alpha(0.7f); }
  106. float get_alpha() const
  107. { return alpha_; }
  108. void set_alpha(float alpha)
  109. {
  110. alpha_ = alpha;
  111. inv_minus_logalpha_ = 1/(-detail::fast_log2(alpha));
  112. }
  113. h_alpha_t get_h_alpha_t() const
  114. { return h_alpha_t(/*alpha_, */inv_minus_logalpha_); }
  115. multiply_by_alpha_t get_multiply_by_alpha_t() const
  116. { return multiply_by_alpha_t(alpha_); }
  117. protected:
  118. float alpha_;
  119. float inv_minus_logalpha_;
  120. SizeType max_tree_size_;
  121. };
  122. template<class SizeType>
  123. struct alpha_holder<false, SizeType>
  124. {
  125. //This specialization uses alpha = 1/sqrt(2)
  126. //without using floating point operations
  127. //Downside: alpha CAN't be changed.
  128. typedef boost::intrusive::detail::h_alpha_sqrt2_t h_alpha_t;
  129. typedef boost::intrusive::detail::alpha_0_75_by_max_size_t multiply_by_alpha_t;
  130. float get_alpha() const
  131. { return 0.70710677f; }
  132. void set_alpha(float)
  133. { //alpha CAN't be changed.
  134. BOOST_INTRUSIVE_INVARIANT_ASSERT(0);
  135. }
  136. h_alpha_t get_h_alpha_t() const
  137. { return h_alpha_t(); }
  138. multiply_by_alpha_t get_multiply_by_alpha_t() const
  139. { return multiply_by_alpha_t(); }
  140. SizeType max_tree_size_;
  141. };
  142. } //namespace detail{
  143. struct sgtree_defaults
  144. {
  145. typedef detail::default_bstree_hook proto_value_traits;
  146. static const bool constant_time_size = true;
  147. typedef std::size_t size_type;
  148. typedef void compare;
  149. static const bool floating_point = true;
  150. };
  151. /// @endcond
  152. //! The class template sgtree is an intrusive scapegoat tree container, that
  153. //! is used to construct intrusive sg_set and sg_multiset containers.
  154. //! The no-throw guarantee holds only, if the value_compare object
  155. //! doesn't throw.
  156. //!
  157. //! The template parameter \c T is the type to be managed by the container.
  158. //! The user can specify additional options and if no options are provided
  159. //! default options are used.
  160. //!
  161. //! The container supports the following options:
  162. //! \c base_hook<>/member_hook<>/value_traits<>,
  163. //! \c floating_point<>, \c size_type<> and
  164. //! \c compare<>.
  165. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  166. template<class T, class ...Options>
  167. #else
  168. template<class ValueTraits, class VoidOrKeyComp, class SizeType, bool FloatingPoint>
  169. #endif
  170. class sgtree_impl
  171. /// @cond
  172. : public bstree_impl<ValueTraits, VoidOrKeyComp, SizeType, true, SgTreeAlgorithms>
  173. , public detail::alpha_holder<FloatingPoint, SizeType>
  174. /// @endcond
  175. {
  176. public:
  177. typedef ValueTraits value_traits;
  178. /// @cond
  179. typedef bstree_impl< ValueTraits, VoidOrKeyComp, SizeType
  180. , true, SgTreeAlgorithms> tree_type;
  181. typedef tree_type implementation_defined;
  182. typedef typename tree_type::real_value_traits real_value_traits;
  183. /// @endcond
  184. typedef typename implementation_defined::pointer pointer;
  185. typedef typename implementation_defined::const_pointer const_pointer;
  186. typedef typename implementation_defined::value_type value_type;
  187. typedef typename implementation_defined::key_type key_type;
  188. typedef typename implementation_defined::reference reference;
  189. typedef typename implementation_defined::const_reference const_reference;
  190. typedef typename implementation_defined::difference_type difference_type;
  191. typedef typename implementation_defined::size_type size_type;
  192. typedef typename implementation_defined::value_compare value_compare;
  193. typedef typename implementation_defined::key_compare key_compare;
  194. typedef typename implementation_defined::iterator iterator;
  195. typedef typename implementation_defined::const_iterator const_iterator;
  196. typedef typename implementation_defined::reverse_iterator reverse_iterator;
  197. typedef typename implementation_defined::const_reverse_iterator const_reverse_iterator;
  198. typedef typename implementation_defined::node_traits node_traits;
  199. typedef typename implementation_defined::node node;
  200. typedef typename implementation_defined::node_ptr node_ptr;
  201. typedef typename implementation_defined::const_node_ptr const_node_ptr;
  202. typedef BOOST_INTRUSIVE_IMPDEF(sgtree_algorithms<node_traits>) node_algorithms;
  203. static const bool constant_time_size = implementation_defined::constant_time_size;
  204. static const bool floating_point = FloatingPoint;
  205. static const bool stateful_value_traits = implementation_defined::stateful_value_traits;
  206. /// @cond
  207. private:
  208. //noncopyable
  209. typedef detail::alpha_holder<FloatingPoint, SizeType> alpha_traits;
  210. typedef typename alpha_traits::h_alpha_t h_alpha_t;
  211. typedef typename alpha_traits::multiply_by_alpha_t multiply_by_alpha_t;
  212. BOOST_MOVABLE_BUT_NOT_COPYABLE(sgtree_impl)
  213. BOOST_STATIC_ASSERT(((int)real_value_traits::link_mode != (int)auto_unlink));
  214. enum { safemode_or_autounlink =
  215. (int)real_value_traits::link_mode == (int)auto_unlink ||
  216. (int)real_value_traits::link_mode == (int)safe_link };
  217. /// @endcond
  218. public:
  219. typedef BOOST_INTRUSIVE_IMPDEF(typename node_algorithms::insert_commit_data) insert_commit_data;
  220. //! @copydoc ::boost::intrusive::bstree::bstree(const value_compare &,const value_traits &)
  221. explicit sgtree_impl( const value_compare &cmp = value_compare()
  222. , const value_traits &v_traits = value_traits())
  223. : tree_type(cmp, v_traits)
  224. {}
  225. //! @copydoc ::boost::intrusive::bstree::bstree(bool,Iterator,Iterator,const value_compare &,const value_traits &)
  226. template<class Iterator>
  227. sgtree_impl( bool unique, Iterator b, Iterator e
  228. , const value_compare &cmp = value_compare()
  229. , const value_traits &v_traits = value_traits())
  230. : tree_type(cmp, v_traits)
  231. {
  232. if(unique)
  233. this->insert_unique(b, e);
  234. else
  235. this->insert_equal(b, e);
  236. }
  237. //! @copydoc ::boost::intrusive::bstree::bstree(bstree &&)
  238. sgtree_impl(BOOST_RV_REF(sgtree_impl) x)
  239. : tree_type(::boost::move(static_cast<tree_type&>(x))), alpha_traits(x.get_alpha_traits())
  240. { std::swap(this->get_alpha_traits(), x.get_alpha_traits()); }
  241. //! @copydoc ::boost::intrusive::bstree::operator=(bstree &&)
  242. sgtree_impl& operator=(BOOST_RV_REF(sgtree_impl) x)
  243. {
  244. this->get_alpha_traits() = x.get_alpha_traits();
  245. return static_cast<sgtree_impl&>(tree_type::operator=(::boost::move(static_cast<tree_type&>(x))));
  246. }
  247. /// @cond
  248. private:
  249. const alpha_traits &get_alpha_traits() const
  250. { return *this; }
  251. alpha_traits &get_alpha_traits()
  252. { return *this; }
  253. h_alpha_t get_h_alpha_func() const
  254. { return this->get_alpha_traits().get_h_alpha_t(); }
  255. multiply_by_alpha_t get_alpha_by_max_size_func() const
  256. { return this->get_alpha_traits().get_multiply_by_alpha_t(); }
  257. /// @endcond
  258. public:
  259. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  260. //! @copydoc ::boost::intrusive::bstree::~bstree()
  261. ~sgtree_impl();
  262. //! @copydoc ::boost::intrusive::bstree::begin()
  263. iterator begin();
  264. //! @copydoc ::boost::intrusive::bstree::begin()const
  265. const_iterator begin() const;
  266. //! @copydoc ::boost::intrusive::bstree::cbegin()const
  267. const_iterator cbegin() const;
  268. //! @copydoc ::boost::intrusive::bstree::end()
  269. iterator end();
  270. //! @copydoc ::boost::intrusive::bstree::end()const
  271. const_iterator end() const;
  272. //! @copydoc ::boost::intrusive::bstree::cend()const
  273. const_iterator cend() const;
  274. //! @copydoc ::boost::intrusive::bstree::rbegin()
  275. reverse_iterator rbegin();
  276. //! @copydoc ::boost::intrusive::bstree::rbegin()const
  277. const_reverse_iterator rbegin() const;
  278. //! @copydoc ::boost::intrusive::bstree::crbegin()const
  279. const_reverse_iterator crbegin() const;
  280. //! @copydoc ::boost::intrusive::bstree::rend()
  281. reverse_iterator rend();
  282. //! @copydoc ::boost::intrusive::bstree::rend()const
  283. const_reverse_iterator rend() const;
  284. //! @copydoc ::boost::intrusive::bstree::crend()const
  285. const_reverse_iterator crend() const;
  286. //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(iterator)
  287. static sgtree_impl &container_from_end_iterator(iterator end_iterator);
  288. //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(const_iterator)
  289. static const sgtree_impl &container_from_end_iterator(const_iterator end_iterator);
  290. //! @copydoc ::boost::intrusive::bstree::container_from_iterator(iterator)
  291. static sgtree_impl &container_from_iterator(iterator it);
  292. //! @copydoc ::boost::intrusive::bstree::container_from_iterator(const_iterator)
  293. static const sgtree_impl &container_from_iterator(const_iterator it);
  294. //! @copydoc ::boost::intrusive::bstree::key_comp()const
  295. key_compare key_comp() const;
  296. //! @copydoc ::boost::intrusive::bstree::value_comp()const
  297. value_compare value_comp() const;
  298. //! @copydoc ::boost::intrusive::bstree::empty()const
  299. bool empty() const;
  300. //! @copydoc ::boost::intrusive::bstree::size()const
  301. size_type size() const;
  302. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  303. //! @copydoc ::boost::intrusive::bstree::swap
  304. void swap(sgtree_impl& other)
  305. {
  306. //This can throw
  307. using std::swap;
  308. this->tree_type::swap(static_cast<tree_type&>(other));
  309. swap(this->get_alpha_traits(), other.get_alpha_traits());
  310. }
  311. //! @copydoc ::boost::intrusive::bstree::clone_from
  312. //! Additional notes: it also copies the alpha factor from the source container.
  313. template <class Cloner, class Disposer>
  314. void clone_from(const sgtree_impl &src, Cloner cloner, Disposer disposer)
  315. {
  316. this->tree_type::clone_from(src, cloner, disposer);
  317. this->get_alpha_traits() = src.get_alpha_traits();
  318. }
  319. //! @copydoc ::boost::intrusive::bstree::insert_equal(reference)
  320. iterator insert_equal(reference value)
  321. {
  322. detail::key_nodeptr_comp<value_compare, real_value_traits>
  323. key_node_comp(this->value_comp(), &this->get_real_value_traits());
  324. node_ptr to_insert(this->get_real_value_traits().to_node_ptr(value));
  325. if(safemode_or_autounlink)
  326. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
  327. std::size_t max_tree_size = (std::size_t)this->max_tree_size_;
  328. node_ptr p = node_algorithms::insert_equal_upper_bound
  329. (this->tree_type::header_ptr(), to_insert, key_node_comp
  330. , (size_type)this->size(), this->get_h_alpha_func(), max_tree_size);
  331. this->tree_type::sz_traits().increment();
  332. this->max_tree_size_ = (size_type)max_tree_size;
  333. return iterator(p, this->real_value_traits_ptr());
  334. }
  335. //! @copydoc ::boost::intrusive::bstree::insert_equal(const_iterator,reference)
  336. iterator insert_equal(const_iterator hint, reference value)
  337. {
  338. detail::key_nodeptr_comp<value_compare, real_value_traits>
  339. key_node_comp(this->value_comp(), &this->get_real_value_traits());
  340. node_ptr to_insert(this->get_real_value_traits().to_node_ptr(value));
  341. if(safemode_or_autounlink)
  342. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
  343. std::size_t max_tree_size = (std::size_t)this->max_tree_size_;
  344. node_ptr p = node_algorithms::insert_equal
  345. (this->tree_type::header_ptr(), hint.pointed_node(), to_insert, key_node_comp
  346. , (std::size_t)this->size(), this->get_h_alpha_func(), max_tree_size);
  347. this->tree_type::sz_traits().increment();
  348. this->max_tree_size_ = (size_type)max_tree_size;
  349. return iterator(p, this->real_value_traits_ptr());
  350. }
  351. //! @copydoc ::boost::intrusive::bstree::insert_equal(Iterator,Iterator)
  352. template<class Iterator>
  353. void insert_equal(Iterator b, Iterator e)
  354. {
  355. iterator iend(this->end());
  356. for (; b != e; ++b)
  357. this->insert_equal(iend, *b);
  358. }
  359. //! @copydoc ::boost::intrusive::bstree::insert_unique(reference)
  360. std::pair<iterator, bool> insert_unique(reference value)
  361. {
  362. insert_commit_data commit_data;
  363. std::pair<iterator, bool> ret = insert_unique_check(value, this->value_comp(), commit_data);
  364. if(!ret.second)
  365. return ret;
  366. return std::pair<iterator, bool> (insert_unique_commit(value, commit_data), true);
  367. }
  368. //! @copydoc ::boost::intrusive::bstree::insert_unique(const_iterator,reference)
  369. iterator insert_unique(const_iterator hint, reference value)
  370. {
  371. insert_commit_data commit_data;
  372. std::pair<iterator, bool> ret = insert_unique_check(hint, value, this->value_comp(), commit_data);
  373. if(!ret.second)
  374. return ret.first;
  375. return insert_unique_commit(value, commit_data);
  376. }
  377. //! @copydoc ::boost::intrusive::bstree::insert_unique_check(const KeyType&,KeyValueCompare,insert_commit_data&)
  378. template<class KeyType, class KeyValueCompare>
  379. std::pair<iterator, bool> insert_unique_check
  380. (const KeyType &key, KeyValueCompare key_value_comp, insert_commit_data &commit_data)
  381. {
  382. detail::key_nodeptr_comp<KeyValueCompare, real_value_traits>
  383. comp(key_value_comp, &this->get_real_value_traits());
  384. std::pair<node_ptr, bool> ret =
  385. (node_algorithms::insert_unique_check
  386. (this->tree_type::header_ptr(), key, comp, commit_data));
  387. return std::pair<iterator, bool>(iterator(ret.first, this->real_value_traits_ptr()), ret.second);
  388. }
  389. //! @copydoc ::boost::intrusive::bstree::insert_unique_check(const_iterator,const KeyType&,KeyValueCompare,insert_commit_data&)
  390. template<class KeyType, class KeyValueCompare>
  391. std::pair<iterator, bool> insert_unique_check
  392. (const_iterator hint, const KeyType &key
  393. ,KeyValueCompare key_value_comp, insert_commit_data &commit_data)
  394. {
  395. detail::key_nodeptr_comp<KeyValueCompare, real_value_traits>
  396. comp(key_value_comp, &this->get_real_value_traits());
  397. std::pair<node_ptr, bool> ret =
  398. (node_algorithms::insert_unique_check
  399. (this->tree_type::header_ptr(), hint.pointed_node(), key, comp, commit_data));
  400. return std::pair<iterator, bool>(iterator(ret.first, this->real_value_traits_ptr()), ret.second);
  401. }
  402. //! @copydoc ::boost::intrusive::bstree::insert_unique_commit
  403. iterator insert_unique_commit(reference value, const insert_commit_data &commit_data)
  404. {
  405. node_ptr to_insert(this->get_real_value_traits().to_node_ptr(value));
  406. if(safemode_or_autounlink)
  407. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
  408. std::size_t max_tree_size = (std::size_t)this->max_tree_size_;
  409. node_algorithms::insert_unique_commit
  410. ( this->tree_type::header_ptr(), to_insert, commit_data
  411. , (std::size_t)this->size(), this->get_h_alpha_func(), max_tree_size);
  412. this->tree_type::sz_traits().increment();
  413. this->max_tree_size_ = (size_type)max_tree_size;
  414. return iterator(to_insert, this->real_value_traits_ptr());
  415. }
  416. //! @copydoc ::boost::intrusive::bstree::insert_unique(Iterator,Iterator)
  417. template<class Iterator>
  418. void insert_unique(Iterator b, Iterator e)
  419. {
  420. if(this->empty()){
  421. iterator iend(this->end());
  422. for (; b != e; ++b)
  423. this->insert_unique(iend, *b);
  424. }
  425. else{
  426. for (; b != e; ++b)
  427. this->insert_unique(*b);
  428. }
  429. }
  430. //! @copydoc ::boost::intrusive::bstree::insert_before
  431. iterator insert_before(const_iterator pos, reference value)
  432. {
  433. node_ptr to_insert(this->get_real_value_traits().to_node_ptr(value));
  434. if(safemode_or_autounlink)
  435. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
  436. std::size_t max_tree_size = (std::size_t)this->max_tree_size_;
  437. node_ptr p = node_algorithms::insert_before
  438. ( this->tree_type::header_ptr(), pos.pointed_node(), to_insert
  439. , (size_type)this->size(), this->get_h_alpha_func(), max_tree_size);
  440. this->tree_type::sz_traits().increment();
  441. this->max_tree_size_ = (size_type)max_tree_size;
  442. return iterator(p, this->real_value_traits_ptr());
  443. }
  444. //! @copydoc ::boost::intrusive::bstree::push_back
  445. void push_back(reference value)
  446. {
  447. node_ptr to_insert(this->get_real_value_traits().to_node_ptr(value));
  448. if(safemode_or_autounlink)
  449. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
  450. std::size_t max_tree_size = (std::size_t)this->max_tree_size_;
  451. node_algorithms::push_back
  452. ( this->tree_type::header_ptr(), to_insert
  453. , (size_type)this->size(), this->get_h_alpha_func(), max_tree_size);
  454. this->tree_type::sz_traits().increment();
  455. this->max_tree_size_ = (size_type)max_tree_size;
  456. }
  457. //! @copydoc ::boost::intrusive::bstree::push_front
  458. void push_front(reference value)
  459. {
  460. node_ptr to_insert(this->get_real_value_traits().to_node_ptr(value));
  461. if(safemode_or_autounlink)
  462. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
  463. std::size_t max_tree_size = (std::size_t)this->max_tree_size_;
  464. node_algorithms::push_front
  465. ( this->tree_type::header_ptr(), to_insert
  466. , (size_type)this->size(), this->get_h_alpha_func(), max_tree_size);
  467. this->tree_type::sz_traits().increment();
  468. this->max_tree_size_ = (size_type)max_tree_size;
  469. }
  470. //! @copydoc ::boost::intrusive::bstree::erase(const_iterator)
  471. iterator erase(const_iterator i)
  472. {
  473. const_iterator ret(i);
  474. ++ret;
  475. node_ptr to_erase(i.pointed_node());
  476. if(safemode_or_autounlink)
  477. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!node_algorithms::unique(to_erase));
  478. std::size_t max_tree_size = this->max_tree_size_;
  479. node_algorithms::erase
  480. ( this->tree_type::header_ptr(), to_erase, (std::size_t)this->size()
  481. , max_tree_size, this->get_alpha_by_max_size_func());
  482. this->max_tree_size_ = (size_type)max_tree_size;
  483. this->tree_type::sz_traits().decrement();
  484. if(safemode_or_autounlink)
  485. node_algorithms::init(to_erase);
  486. return ret.unconst();
  487. }
  488. //! @copydoc ::boost::intrusive::bstree::erase(const_iterator,const_iterator)
  489. iterator erase(const_iterator b, const_iterator e)
  490. { size_type n; return private_erase(b, e, n); }
  491. //! @copydoc ::boost::intrusive::bstree::erase(const_reference)
  492. size_type erase(const_reference value)
  493. { return this->erase(value, this->value_comp()); }
  494. //! @copydoc ::boost::intrusive::bstree::erase(const KeyType&,KeyValueCompare)
  495. template<class KeyType, class KeyValueCompare>
  496. size_type erase(const KeyType& key, KeyValueCompare comp
  497. /// @cond
  498. , typename detail::enable_if_c<!detail::is_convertible<KeyValueCompare, const_iterator>::value >::type * = 0
  499. /// @endcond
  500. )
  501. {
  502. std::pair<iterator,iterator> p = this->equal_range(key, comp);
  503. size_type n;
  504. private_erase(p.first, p.second, n);
  505. return n;
  506. }
  507. //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_iterator,Disposer)
  508. template<class Disposer>
  509. iterator erase_and_dispose(const_iterator i, Disposer disposer)
  510. {
  511. node_ptr to_erase(i.pointed_node());
  512. iterator ret(this->erase(i));
  513. disposer(this->get_real_value_traits().to_value_ptr(to_erase));
  514. return ret;
  515. }
  516. #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  517. template<class Disposer>
  518. iterator erase_and_dispose(iterator i, Disposer disposer)
  519. { return this->erase_and_dispose(const_iterator(i), disposer); }
  520. #endif
  521. //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_iterator,const_iterator,Disposer)
  522. template<class Disposer>
  523. iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer)
  524. { size_type n; return private_erase(b, e, n, disposer); }
  525. //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_reference, Disposer)
  526. template<class Disposer>
  527. size_type erase_and_dispose(const_reference value, Disposer disposer)
  528. {
  529. std::pair<iterator,iterator> p = this->equal_range(value);
  530. size_type n;
  531. private_erase(p.first, p.second, n, disposer);
  532. return n;
  533. }
  534. //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const KeyType&,KeyValueCompare,Disposer)
  535. template<class KeyType, class KeyValueCompare, class Disposer>
  536. size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer
  537. /// @cond
  538. , typename detail::enable_if_c<!detail::is_convertible<KeyValueCompare, const_iterator>::value >::type * = 0
  539. /// @endcond
  540. )
  541. {
  542. std::pair<iterator,iterator> p = this->equal_range(key, comp);
  543. size_type n;
  544. private_erase(p.first, p.second, n, disposer);
  545. return n;
  546. }
  547. //! @copydoc ::boost::intrusive::bstree::clear
  548. void clear()
  549. {
  550. tree_type::clear();
  551. this->max_tree_size_ = 0;
  552. }
  553. //! @copydoc ::boost::intrusive::bstree::clear_and_dispose
  554. template<class Disposer>
  555. void clear_and_dispose(Disposer disposer)
  556. {
  557. tree_type::clear_and_dispose(disposer);
  558. this->max_tree_size_ = 0;
  559. }
  560. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  561. //! @copydoc ::boost::intrusive::bstree::count(const_reference)const
  562. size_type count(const_reference value) const;
  563. //! @copydoc ::boost::intrusive::bstree::count(const KeyType&,KeyValueCompare)const
  564. template<class KeyType, class KeyValueCompare>
  565. size_type count(const KeyType& key, KeyValueCompare comp) const;
  566. //! @copydoc ::boost::intrusive::bstree::lower_bound(const_reference)
  567. iterator lower_bound(const_reference value);
  568. //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyValueCompare)
  569. template<class KeyType, class KeyValueCompare>
  570. iterator lower_bound(const KeyType& key, KeyValueCompare comp);
  571. //! @copydoc ::boost::intrusive::bstree::lower_bound(const_reference)const
  572. const_iterator lower_bound(const_reference value) const;
  573. //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyValueCompare)const
  574. template<class KeyType, class KeyValueCompare>
  575. const_iterator lower_bound(const KeyType& key, KeyValueCompare comp) const;
  576. //! @copydoc ::boost::intrusive::bstree::upper_bound(const_reference)
  577. iterator upper_bound(const_reference value);
  578. //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyValueCompare)
  579. template<class KeyType, class KeyValueCompare>
  580. iterator upper_bound(const KeyType& key, KeyValueCompare comp);
  581. //! @copydoc ::boost::intrusive::bstree::upper_bound(const_reference)const
  582. const_iterator upper_bound(const_reference value) const;
  583. //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyValueCompare)const
  584. template<class KeyType, class KeyValueCompare>
  585. const_iterator upper_bound(const KeyType& key, KeyValueCompare comp) const;
  586. //! @copydoc ::boost::intrusive::bstree::find(const_reference)
  587. iterator find(const_reference value);
  588. //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyValueCompare)
  589. template<class KeyType, class KeyValueCompare>
  590. iterator find(const KeyType& key, KeyValueCompare comp);
  591. //! @copydoc ::boost::intrusive::bstree::find(const_reference)const
  592. const_iterator find(const_reference value) const;
  593. //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyValueCompare)const
  594. template<class KeyType, class KeyValueCompare>
  595. const_iterator find(const KeyType& key, KeyValueCompare comp) const;
  596. //! @copydoc ::boost::intrusive::bstree::equal_range(const_reference)
  597. std::pair<iterator,iterator> equal_range(const_reference value);
  598. //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyValueCompare)
  599. template<class KeyType, class KeyValueCompare>
  600. std::pair<iterator,iterator> equal_range(const KeyType& key, KeyValueCompare comp);
  601. //! @copydoc ::boost::intrusive::bstree::equal_range(const_reference)const
  602. std::pair<const_iterator, const_iterator>
  603. equal_range(const_reference value) const;
  604. //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyValueCompare)const
  605. template<class KeyType, class KeyValueCompare>
  606. std::pair<const_iterator, const_iterator>
  607. equal_range(const KeyType& key, KeyValueCompare comp) const;
  608. //! @copydoc ::boost::intrusive::bstree::bounded_range(const_reference,const_reference,bool,bool)
  609. std::pair<iterator,iterator> bounded_range
  610. (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed);
  611. //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool)
  612. template<class KeyType, class KeyValueCompare>
  613. std::pair<iterator,iterator> bounded_range
  614. (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed);
  615. //! @copydoc ::boost::intrusive::bstree::bounded_range(const_reference,const_reference,bool,bool)const
  616. std::pair<const_iterator, const_iterator>
  617. bounded_range(const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const;
  618. //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool)const
  619. template<class KeyType, class KeyValueCompare>
  620. std::pair<const_iterator, const_iterator> bounded_range
  621. (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const;
  622. //! @copydoc ::boost::intrusive::bstree::s_iterator_to(reference)
  623. static iterator s_iterator_to(reference value);
  624. //! @copydoc ::boost::intrusive::bstree::s_iterator_to(const_reference)
  625. static const_iterator s_iterator_to(const_reference value);
  626. //! @copydoc ::boost::intrusive::bstree::iterator_to(reference)
  627. iterator iterator_to(reference value);
  628. //! @copydoc ::boost::intrusive::bstree::iterator_to(const_reference)const
  629. const_iterator iterator_to(const_reference value) const;
  630. //! @copydoc ::boost::intrusive::bstree::init_node(reference)
  631. static void init_node(reference value);
  632. //! @copydoc ::boost::intrusive::bstree::unlink_leftmost_without_rebalance
  633. pointer unlink_leftmost_without_rebalance();
  634. //! @copydoc ::boost::intrusive::bstree::replace_node
  635. void replace_node(iterator replace_this, reference with_this);
  636. //! @copydoc ::boost::intrusive::bstree::remove_node
  637. void remove_node(reference value);
  638. //! @copydoc ::boost::intrusive::bstree::rebalance
  639. void rebalance();
  640. //! @copydoc ::boost::intrusive::bstree::rebalance_subtree
  641. iterator rebalance_subtree(iterator root);
  642. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  643. //! <b>Returns</b>: The balance factor (alpha) used in this tree
  644. //!
  645. //! <b>Throws</b>: Nothing.
  646. //!
  647. //! <b>Complexity</b>: Constant.
  648. float balance_factor() const
  649. { return this->get_alpha_traits().get_alpha(); }
  650. //! <b>Requires</b>: new_alpha must be a value between 0.5 and 1.0
  651. //!
  652. //! <b>Effects</b>: Establishes a new balance factor (alpha) and rebalances
  653. //! the tree if the new balance factor is stricter (less) than the old factor.
  654. //!
  655. //! <b>Throws</b>: Nothing.
  656. //!
  657. //! <b>Complexity</b>: Linear to the elements in the subtree.
  658. void balance_factor(float new_alpha)
  659. {
  660. BOOST_INTRUSIVE_INVARIANT_ASSERT((new_alpha > 0.5f && new_alpha < 1.0f));
  661. if(new_alpha < 0.5f && new_alpha >= 1.0f) return;
  662. //The alpha factor CAN't be changed if the fixed, floating operation-less
  663. //1/sqrt(2) alpha factor option is activated
  664. BOOST_STATIC_ASSERT((floating_point));
  665. float old_alpha = this->get_alpha_traits().get_alpha();
  666. this->get_alpha_traits().set_alpha(new_alpha);
  667. if(new_alpha < old_alpha){
  668. this->max_tree_size_ = this->size();
  669. this->rebalance();
  670. }
  671. }
  672. /// @cond
  673. private:
  674. template<class Disposer>
  675. iterator private_erase(const_iterator b, const_iterator e, size_type &n, Disposer disposer)
  676. {
  677. for(n = 0; b != e; ++n)
  678. this->erase_and_dispose(b++, disposer);
  679. return b.unconst();
  680. }
  681. iterator private_erase(const_iterator b, const_iterator e, size_type &n)
  682. {
  683. for(n = 0; b != e; ++n)
  684. this->erase(b++);
  685. return b.unconst();
  686. }
  687. /// @endcond
  688. };
  689. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  690. template<class T, class ...Options>
  691. bool operator< (const sgtree_impl<T, Options...> &x, const sgtree_impl<T, Options...> &y);
  692. template<class T, class ...Options>
  693. bool operator==(const sgtree_impl<T, Options...> &x, const sgtree_impl<T, Options...> &y);
  694. template<class T, class ...Options>
  695. bool operator!= (const sgtree_impl<T, Options...> &x, const sgtree_impl<T, Options...> &y);
  696. template<class T, class ...Options>
  697. bool operator>(const sgtree_impl<T, Options...> &x, const sgtree_impl<T, Options...> &y);
  698. template<class T, class ...Options>
  699. bool operator<=(const sgtree_impl<T, Options...> &x, const sgtree_impl<T, Options...> &y);
  700. template<class T, class ...Options>
  701. bool operator>=(const sgtree_impl<T, Options...> &x, const sgtree_impl<T, Options...> &y);
  702. template<class T, class ...Options>
  703. void swap(sgtree_impl<T, Options...> &x, sgtree_impl<T, Options...> &y);
  704. #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  705. //! Helper metafunction to define a \c sgtree that yields to the same type when the
  706. //! same options (either explicitly or implicitly) are used.
  707. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  708. template<class T, class ...Options>
  709. #else
  710. template<class T, class O1 = void, class O2 = void
  711. , class O3 = void, class O4 = void>
  712. #endif
  713. struct make_sgtree
  714. {
  715. /// @cond
  716. typedef typename pack_options
  717. < sgtree_defaults,
  718. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  719. O1, O2, O3, O4
  720. #else
  721. Options...
  722. #endif
  723. >::type packed_options;
  724. typedef typename detail::get_value_traits
  725. <T, typename packed_options::proto_value_traits>::type value_traits;
  726. typedef sgtree_impl
  727. < value_traits
  728. , typename packed_options::compare
  729. , typename packed_options::size_type
  730. , packed_options::floating_point
  731. > implementation_defined;
  732. /// @endcond
  733. typedef implementation_defined type;
  734. };
  735. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  736. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  737. template<class T, class O1, class O2, class O3, class O4>
  738. #else
  739. template<class T, class ...Options>
  740. #endif
  741. class sgtree
  742. : public make_sgtree<T,
  743. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  744. O1, O2, O3, O4
  745. #else
  746. Options...
  747. #endif
  748. >::type
  749. {
  750. typedef typename make_sgtree
  751. <T,
  752. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  753. O1, O2, O3, O4
  754. #else
  755. Options...
  756. #endif
  757. >::type Base;
  758. BOOST_MOVABLE_BUT_NOT_COPYABLE(sgtree)
  759. public:
  760. typedef typename Base::value_compare value_compare;
  761. typedef typename Base::value_traits value_traits;
  762. typedef typename Base::real_value_traits real_value_traits;
  763. typedef typename Base::iterator iterator;
  764. typedef typename Base::const_iterator const_iterator;
  765. typedef typename Base::reverse_iterator reverse_iterator;
  766. typedef typename Base::const_reverse_iterator const_reverse_iterator;
  767. //Assert if passed value traits are compatible with the type
  768. BOOST_STATIC_ASSERT((detail::is_same<typename real_value_traits::value_type, T>::value));
  769. explicit sgtree( const value_compare &cmp = value_compare()
  770. , const value_traits &v_traits = value_traits())
  771. : Base(cmp, v_traits)
  772. {}
  773. template<class Iterator>
  774. sgtree( bool unique, Iterator b, Iterator e
  775. , const value_compare &cmp = value_compare()
  776. , const value_traits &v_traits = value_traits())
  777. : Base(unique, b, e, cmp, v_traits)
  778. {}
  779. sgtree(BOOST_RV_REF(sgtree) x)
  780. : Base(::boost::move(static_cast<Base&>(x)))
  781. {}
  782. sgtree& operator=(BOOST_RV_REF(sgtree) x)
  783. { return static_cast<sgtree &>(this->Base::operator=(::boost::move(static_cast<Base&>(x)))); }
  784. static sgtree &container_from_end_iterator(iterator end_iterator)
  785. { return static_cast<sgtree &>(Base::container_from_end_iterator(end_iterator)); }
  786. static const sgtree &container_from_end_iterator(const_iterator end_iterator)
  787. { return static_cast<const sgtree &>(Base::container_from_end_iterator(end_iterator)); }
  788. static sgtree &container_from_iterator(iterator it)
  789. { return static_cast<sgtree &>(Base::container_from_iterator(it)); }
  790. static const sgtree &container_from_iterator(const_iterator it)
  791. { return static_cast<const sgtree &>(Base::container_from_iterator(it)); }
  792. };
  793. #endif
  794. } //namespace intrusive
  795. } //namespace boost
  796. #include <boost/intrusive/detail/config_end.hpp>
  797. #endif //BOOST_INTRUSIVE_SGTREE_HPP