ranked_index.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* Copyright 2003-2020 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/multi_index for library home page.
  7. */
  8. #ifndef BOOST_MULTI_INDEX_RANKED_INDEX_HPP
  9. #define BOOST_MULTI_INDEX_RANKED_INDEX_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  14. #include <boost/multi_index/detail/ord_index_impl.hpp>
  15. #include <boost/multi_index/detail/rnk_index_ops.hpp>
  16. #include <boost/multi_index/ranked_index_fwd.hpp>
  17. namespace boost{
  18. namespace multi_index{
  19. namespace detail{
  20. /* ranked_index augments a given ordered index to provide rank operations */
  21. template<typename OrderedIndexNodeImpl>
  22. struct ranked_node:OrderedIndexNodeImpl
  23. {
  24. typedef typename OrderedIndexNodeImpl::size_type size_type;
  25. size_type size;
  26. };
  27. template<typename OrderedIndexImpl>
  28. class ranked_index:public OrderedIndexImpl
  29. {
  30. typedef OrderedIndexImpl super;
  31. protected:
  32. typedef typename super::index_node_type index_node_type;
  33. typedef typename super::node_impl_pointer node_impl_pointer;
  34. public:
  35. typedef typename super::ctor_args_list ctor_args_list;
  36. typedef typename super::allocator_type allocator_type;
  37. typedef typename super::iterator iterator;
  38. typedef typename super::size_type size_type;
  39. /* rank operations */
  40. iterator nth(size_type n)const
  41. {
  42. return this->make_iterator(index_node_type::from_impl(
  43. ranked_index_nth(n,this->header()->impl())));
  44. }
  45. size_type rank(iterator position)const
  46. {
  47. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  48. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  49. return ranked_index_rank(
  50. position.get_node()->impl(),this->header()->impl());
  51. }
  52. template<typename CompatibleKey>
  53. size_type find_rank(const CompatibleKey& x)const
  54. {
  55. return ranked_index_find_rank(
  56. this->root(),this->header(),this->key,x,this->comp_);
  57. }
  58. template<typename CompatibleKey,typename CompatibleCompare>
  59. size_type find_rank(
  60. const CompatibleKey& x,const CompatibleCompare& comp)const
  61. {
  62. return ranked_index_find_rank(
  63. this->root(),this->header(),this->key,x,comp);
  64. }
  65. template<typename CompatibleKey>
  66. size_type lower_bound_rank(const CompatibleKey& x)const
  67. {
  68. return ranked_index_lower_bound_rank(
  69. this->root(),this->header(),this->key,x,this->comp_);
  70. }
  71. template<typename CompatibleKey,typename CompatibleCompare>
  72. size_type lower_bound_rank(
  73. const CompatibleKey& x,const CompatibleCompare& comp)const
  74. {
  75. return ranked_index_lower_bound_rank(
  76. this->root(),this->header(),this->key,x,comp);
  77. }
  78. template<typename CompatibleKey>
  79. size_type upper_bound_rank(const CompatibleKey& x)const
  80. {
  81. return ranked_index_upper_bound_rank(
  82. this->root(),this->header(),this->key,x,this->comp_);
  83. }
  84. template<typename CompatibleKey,typename CompatibleCompare>
  85. size_type upper_bound_rank(
  86. const CompatibleKey& x,const CompatibleCompare& comp)const
  87. {
  88. return ranked_index_upper_bound_rank(
  89. this->root(),this->header(),this->key,x,comp);
  90. }
  91. template<typename CompatibleKey>
  92. std::pair<size_type,size_type> equal_range_rank(
  93. const CompatibleKey& x)const
  94. {
  95. return ranked_index_equal_range_rank(
  96. this->root(),this->header(),this->key,x,this->comp_);
  97. }
  98. template<typename CompatibleKey,typename CompatibleCompare>
  99. std::pair<size_type,size_type> equal_range_rank(
  100. const CompatibleKey& x,const CompatibleCompare& comp)const
  101. {
  102. return ranked_index_equal_range_rank(
  103. this->root(),this->header(),this->key,x,comp);
  104. }
  105. template<typename LowerBounder,typename UpperBounder>
  106. std::pair<size_type,size_type>
  107. range_rank(LowerBounder lower,UpperBounder upper)const
  108. {
  109. typedef typename mpl::if_<
  110. is_same<LowerBounder,unbounded_type>,
  111. BOOST_DEDUCED_TYPENAME mpl::if_<
  112. is_same<UpperBounder,unbounded_type>,
  113. both_unbounded_tag,
  114. lower_unbounded_tag
  115. >::type,
  116. BOOST_DEDUCED_TYPENAME mpl::if_<
  117. is_same<UpperBounder,unbounded_type>,
  118. upper_unbounded_tag,
  119. none_unbounded_tag
  120. >::type
  121. >::type dispatch;
  122. return range_rank(lower,upper,dispatch());
  123. }
  124. protected:
  125. ranked_index(const ranked_index& x):super(x){};
  126. ranked_index(const ranked_index& x,do_not_copy_elements_tag):
  127. super(x,do_not_copy_elements_tag()){};
  128. ranked_index(
  129. const ctor_args_list& args_list,const allocator_type& al):
  130. super(args_list,al){}
  131. private:
  132. template<typename LowerBounder,typename UpperBounder>
  133. std::pair<size_type,size_type>
  134. range_rank(LowerBounder lower,UpperBounder upper,none_unbounded_tag)const
  135. {
  136. index_node_type* y=this->header();
  137. index_node_type* z=this->root();
  138. if(!z)return std::pair<size_type,size_type>(0,0);
  139. size_type s=z->impl()->size;
  140. do{
  141. if(!lower(this->key(z->value()))){
  142. z=index_node_type::from_impl(z->right());
  143. }
  144. else if(!upper(this->key(z->value()))){
  145. y=z;
  146. s-=ranked_node_size(y->right())+1;
  147. z=index_node_type::from_impl(z->left());
  148. }
  149. else{
  150. return std::pair<size_type,size_type>(
  151. s-z->impl()->size+
  152. lower_range_rank(index_node_type::from_impl(z->left()),z,lower),
  153. s-ranked_node_size(z->right())+
  154. upper_range_rank(index_node_type::from_impl(z->right()),y,upper));
  155. }
  156. }while(z);
  157. return std::pair<size_type,size_type>(s,s);
  158. }
  159. template<typename LowerBounder,typename UpperBounder>
  160. std::pair<size_type,size_type>
  161. range_rank(LowerBounder,UpperBounder upper,lower_unbounded_tag)const
  162. {
  163. return std::pair<size_type,size_type>(
  164. 0,
  165. upper_range_rank(this->root(),this->header(),upper));
  166. }
  167. template<typename LowerBounder,typename UpperBounder>
  168. std::pair<size_type,size_type>
  169. range_rank(LowerBounder lower,UpperBounder,upper_unbounded_tag)const
  170. {
  171. return std::pair<size_type,size_type>(
  172. lower_range_rank(this->root(),this->header(),lower),
  173. this->size());
  174. }
  175. template<typename LowerBounder,typename UpperBounder>
  176. std::pair<size_type,size_type>
  177. range_rank(LowerBounder,UpperBounder,both_unbounded_tag)const
  178. {
  179. return std::pair<size_type,size_type>(0,this->size());
  180. }
  181. template<typename LowerBounder>
  182. size_type
  183. lower_range_rank(
  184. index_node_type* top,index_node_type* y,LowerBounder lower)const
  185. {
  186. if(!top)return 0;
  187. size_type s=top->impl()->size;
  188. do{
  189. if(lower(this->key(top->value()))){
  190. y=top;
  191. s-=ranked_node_size(y->right())+1;
  192. top=index_node_type::from_impl(top->left());
  193. }
  194. else top=index_node_type::from_impl(top->right());
  195. }while(top);
  196. return s;
  197. }
  198. template<typename UpperBounder>
  199. size_type
  200. upper_range_rank(
  201. index_node_type* top,index_node_type* y,UpperBounder upper)const
  202. {
  203. if(!top)return 0;
  204. size_type s=top->impl()->size;
  205. do{
  206. if(!upper(this->key(top->value()))){
  207. y=top;
  208. s-=ranked_node_size(y->right())+1;
  209. top=index_node_type::from_impl(top->left());
  210. }
  211. else top=index_node_type::from_impl(top->right());
  212. }while(top);
  213. return s;
  214. }
  215. };
  216. /* augmenting policy for ordered_index */
  217. struct rank_policy
  218. {
  219. template<typename OrderedIndexNodeImpl>
  220. struct augmented_node
  221. {
  222. typedef ranked_node<OrderedIndexNodeImpl> type;
  223. };
  224. template<typename OrderedIndexImpl>
  225. struct augmented_interface
  226. {
  227. typedef ranked_index<OrderedIndexImpl> type;
  228. };
  229. /* algorithmic stuff */
  230. template<typename Pointer>
  231. static void add(Pointer x,Pointer root)
  232. {
  233. x->size=1;
  234. while(x!=root){
  235. x=x->parent();
  236. ++(x->size);
  237. }
  238. }
  239. template<typename Pointer>
  240. static void remove(Pointer x,Pointer root)
  241. {
  242. while(x!=root){
  243. x=x->parent();
  244. --(x->size);
  245. }
  246. }
  247. template<typename Pointer>
  248. static void copy(Pointer x,Pointer y)
  249. {
  250. y->size=x->size;
  251. }
  252. template<typename Pointer>
  253. static void rotate_left(Pointer x,Pointer y) /* in: x==y->left() */
  254. {
  255. y->size=x->size;
  256. x->size=ranked_node_size(x->left())+ranked_node_size(x->right())+1;
  257. }
  258. template<typename Pointer>
  259. static void rotate_right(Pointer x,Pointer y) /* in: x==y->right() */
  260. {
  261. rotate_left(x,y);
  262. }
  263. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
  264. /* invariant stuff */
  265. template<typename Pointer>
  266. static bool invariant(Pointer x)
  267. {
  268. return x->size==ranked_node_size(x->left())+ranked_node_size(x->right())+1;
  269. }
  270. #endif
  271. };
  272. } /* namespace multi_index::detail */
  273. /* ranked_index specifiers */
  274. template<typename Arg1,typename Arg2,typename Arg3>
  275. struct ranked_unique
  276. {
  277. typedef typename detail::ordered_index_args<
  278. Arg1,Arg2,Arg3> index_args;
  279. typedef typename index_args::tag_list_type::type tag_list_type;
  280. typedef typename index_args::key_from_value_type key_from_value_type;
  281. typedef typename index_args::compare_type compare_type;
  282. template<typename Super>
  283. struct node_class
  284. {
  285. typedef detail::ordered_index_node<detail::rank_policy,Super> type;
  286. };
  287. template<typename SuperMeta>
  288. struct index_class
  289. {
  290. typedef detail::ordered_index<
  291. key_from_value_type,compare_type,
  292. SuperMeta,tag_list_type,detail::ordered_unique_tag,
  293. detail::rank_policy> type;
  294. };
  295. };
  296. template<typename Arg1,typename Arg2,typename Arg3>
  297. struct ranked_non_unique
  298. {
  299. typedef detail::ordered_index_args<
  300. Arg1,Arg2,Arg3> index_args;
  301. typedef typename index_args::tag_list_type::type tag_list_type;
  302. typedef typename index_args::key_from_value_type key_from_value_type;
  303. typedef typename index_args::compare_type compare_type;
  304. template<typename Super>
  305. struct node_class
  306. {
  307. typedef detail::ordered_index_node<detail::rank_policy,Super> type;
  308. };
  309. template<typename SuperMeta>
  310. struct index_class
  311. {
  312. typedef detail::ordered_index<
  313. key_from_value_type,compare_type,
  314. SuperMeta,tag_list_type,detail::ordered_non_unique_tag,
  315. detail::rank_policy> type;
  316. };
  317. };
  318. } /* namespace multi_index */
  319. } /* namespace boost */
  320. #endif