ranked_index.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* Copyright 2003-2022 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. /* set operations */
  40. template<typename CompatibleKey>
  41. size_type count(const CompatibleKey& x)const
  42. {
  43. return count(x,this->comp_);
  44. }
  45. template<typename CompatibleKey,typename CompatibleCompare>
  46. size_type count(const CompatibleKey& x,const CompatibleCompare& comp)const
  47. {
  48. std::pair<size_type,size_type> p=this->equal_range_rank(x,comp);
  49. return p.second-p.first;
  50. }
  51. /* rank operations */
  52. iterator nth(size_type n)const
  53. {
  54. return this->make_iterator(index_node_type::from_impl(
  55. ranked_index_nth(n,this->header()->impl())));
  56. }
  57. size_type rank(iterator position)const
  58. {
  59. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  60. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  61. return ranked_index_rank(
  62. position.get_node()->impl(),this->header()->impl());
  63. }
  64. template<typename CompatibleKey>
  65. size_type find_rank(const CompatibleKey& x)const
  66. {
  67. return ranked_index_find_rank(
  68. this->root(),this->header(),this->key,x,this->comp_);
  69. }
  70. template<typename CompatibleKey,typename CompatibleCompare>
  71. size_type find_rank(
  72. const CompatibleKey& x,const CompatibleCompare& comp)const
  73. {
  74. return ranked_index_find_rank(
  75. this->root(),this->header(),this->key,x,comp);
  76. }
  77. template<typename CompatibleKey>
  78. size_type lower_bound_rank(const CompatibleKey& x)const
  79. {
  80. return ranked_index_lower_bound_rank(
  81. this->root(),this->header(),this->key,x,this->comp_);
  82. }
  83. template<typename CompatibleKey,typename CompatibleCompare>
  84. size_type lower_bound_rank(
  85. const CompatibleKey& x,const CompatibleCompare& comp)const
  86. {
  87. return ranked_index_lower_bound_rank(
  88. this->root(),this->header(),this->key,x,comp);
  89. }
  90. template<typename CompatibleKey>
  91. size_type upper_bound_rank(const CompatibleKey& x)const
  92. {
  93. return ranked_index_upper_bound_rank(
  94. this->root(),this->header(),this->key,x,this->comp_);
  95. }
  96. template<typename CompatibleKey,typename CompatibleCompare>
  97. size_type upper_bound_rank(
  98. const CompatibleKey& x,const CompatibleCompare& comp)const
  99. {
  100. return ranked_index_upper_bound_rank(
  101. this->root(),this->header(),this->key,x,comp);
  102. }
  103. template<typename CompatibleKey>
  104. std::pair<size_type,size_type> equal_range_rank(
  105. const CompatibleKey& x)const
  106. {
  107. return ranked_index_equal_range_rank(
  108. this->root(),this->header(),this->key,x,this->comp_);
  109. }
  110. template<typename CompatibleKey,typename CompatibleCompare>
  111. std::pair<size_type,size_type> equal_range_rank(
  112. const CompatibleKey& x,const CompatibleCompare& comp)const
  113. {
  114. return ranked_index_equal_range_rank(
  115. this->root(),this->header(),this->key,x,comp);
  116. }
  117. template<typename LowerBounder,typename UpperBounder>
  118. std::pair<size_type,size_type>
  119. range_rank(LowerBounder lower,UpperBounder upper)const
  120. {
  121. typedef typename mpl::if_<
  122. is_same<LowerBounder,unbounded_type>,
  123. BOOST_DEDUCED_TYPENAME mpl::if_<
  124. is_same<UpperBounder,unbounded_type>,
  125. both_unbounded_tag,
  126. lower_unbounded_tag
  127. >::type,
  128. BOOST_DEDUCED_TYPENAME mpl::if_<
  129. is_same<UpperBounder,unbounded_type>,
  130. upper_unbounded_tag,
  131. none_unbounded_tag
  132. >::type
  133. >::type dispatch;
  134. return range_rank(lower,upper,dispatch());
  135. }
  136. protected:
  137. ranked_index(const ranked_index& x):super(x){};
  138. ranked_index(const ranked_index& x,do_not_copy_elements_tag):
  139. super(x,do_not_copy_elements_tag()){};
  140. ranked_index(
  141. const ctor_args_list& args_list,const allocator_type& al):
  142. super(args_list,al){}
  143. private:
  144. template<typename LowerBounder,typename UpperBounder>
  145. std::pair<size_type,size_type>
  146. range_rank(LowerBounder lower,UpperBounder upper,none_unbounded_tag)const
  147. {
  148. index_node_type* y=this->header();
  149. index_node_type* z=this->root();
  150. if(!z)return std::pair<size_type,size_type>(0,0);
  151. size_type s=z->impl()->size;
  152. do{
  153. if(!lower(this->key(z->value()))){
  154. z=index_node_type::from_impl(z->right());
  155. }
  156. else if(!upper(this->key(z->value()))){
  157. y=z;
  158. s-=ranked_node_size(y->right())+1;
  159. z=index_node_type::from_impl(z->left());
  160. }
  161. else{
  162. return std::pair<size_type,size_type>(
  163. s-z->impl()->size+
  164. lower_range_rank(index_node_type::from_impl(z->left()),z,lower),
  165. s-ranked_node_size(z->right())+
  166. upper_range_rank(index_node_type::from_impl(z->right()),y,upper));
  167. }
  168. }while(z);
  169. return std::pair<size_type,size_type>(s,s);
  170. }
  171. template<typename LowerBounder,typename UpperBounder>
  172. std::pair<size_type,size_type>
  173. range_rank(LowerBounder,UpperBounder upper,lower_unbounded_tag)const
  174. {
  175. return std::pair<size_type,size_type>(
  176. 0,
  177. upper_range_rank(this->root(),this->header(),upper));
  178. }
  179. template<typename LowerBounder,typename UpperBounder>
  180. std::pair<size_type,size_type>
  181. range_rank(LowerBounder lower,UpperBounder,upper_unbounded_tag)const
  182. {
  183. return std::pair<size_type,size_type>(
  184. lower_range_rank(this->root(),this->header(),lower),
  185. this->size());
  186. }
  187. template<typename LowerBounder,typename UpperBounder>
  188. std::pair<size_type,size_type>
  189. range_rank(LowerBounder,UpperBounder,both_unbounded_tag)const
  190. {
  191. return std::pair<size_type,size_type>(0,this->size());
  192. }
  193. template<typename LowerBounder>
  194. size_type
  195. lower_range_rank(
  196. index_node_type* top,index_node_type* y,LowerBounder lower)const
  197. {
  198. if(!top)return 0;
  199. size_type s=top->impl()->size;
  200. do{
  201. if(lower(this->key(top->value()))){
  202. y=top;
  203. s-=ranked_node_size(y->right())+1;
  204. top=index_node_type::from_impl(top->left());
  205. }
  206. else top=index_node_type::from_impl(top->right());
  207. }while(top);
  208. return s;
  209. }
  210. template<typename UpperBounder>
  211. size_type
  212. upper_range_rank(
  213. index_node_type* top,index_node_type* y,UpperBounder upper)const
  214. {
  215. if(!top)return 0;
  216. size_type s=top->impl()->size;
  217. do{
  218. if(!upper(this->key(top->value()))){
  219. y=top;
  220. s-=ranked_node_size(y->right())+1;
  221. top=index_node_type::from_impl(top->left());
  222. }
  223. else top=index_node_type::from_impl(top->right());
  224. }while(top);
  225. return s;
  226. }
  227. };
  228. /* augmenting policy for ordered_index */
  229. struct rank_policy
  230. {
  231. template<typename OrderedIndexNodeImpl>
  232. struct augmented_node
  233. {
  234. typedef ranked_node<OrderedIndexNodeImpl> type;
  235. };
  236. template<typename OrderedIndexImpl>
  237. struct augmented_interface
  238. {
  239. typedef ranked_index<OrderedIndexImpl> type;
  240. };
  241. /* algorithmic stuff */
  242. template<typename Pointer>
  243. static void add(Pointer x,Pointer root)
  244. {
  245. x->size=1;
  246. while(x!=root){
  247. x=x->parent();
  248. ++(x->size);
  249. }
  250. }
  251. template<typename Pointer>
  252. static void remove(Pointer x,Pointer root)
  253. {
  254. while(x!=root){
  255. x=x->parent();
  256. --(x->size);
  257. }
  258. }
  259. template<typename Pointer>
  260. static void copy(Pointer x,Pointer y)
  261. {
  262. y->size=x->size;
  263. }
  264. template<typename Pointer>
  265. static void rotate_left(Pointer x,Pointer y) /* in: x==y->left() */
  266. {
  267. y->size=x->size;
  268. x->size=ranked_node_size(x->left())+ranked_node_size(x->right())+1;
  269. }
  270. template<typename Pointer>
  271. static void rotate_right(Pointer x,Pointer y) /* in: x==y->right() */
  272. {
  273. rotate_left(x,y);
  274. }
  275. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
  276. /* invariant stuff */
  277. template<typename Pointer>
  278. static bool invariant(Pointer x)
  279. {
  280. return x->size==ranked_node_size(x->left())+ranked_node_size(x->right())+1;
  281. }
  282. #endif
  283. };
  284. } /* namespace multi_index::detail */
  285. /* ranked_index specifiers */
  286. template<typename Arg1,typename Arg2,typename Arg3>
  287. struct ranked_unique
  288. {
  289. typedef typename detail::ordered_index_args<
  290. Arg1,Arg2,Arg3> index_args;
  291. typedef typename index_args::tag_list_type::type tag_list_type;
  292. typedef typename index_args::key_from_value_type key_from_value_type;
  293. typedef typename index_args::compare_type compare_type;
  294. template<typename Super>
  295. struct node_class
  296. {
  297. typedef detail::ordered_index_node<detail::rank_policy,Super> type;
  298. };
  299. template<typename SuperMeta>
  300. struct index_class
  301. {
  302. typedef detail::ordered_index<
  303. key_from_value_type,compare_type,
  304. SuperMeta,tag_list_type,detail::ordered_unique_tag,
  305. detail::rank_policy> type;
  306. };
  307. };
  308. template<typename Arg1,typename Arg2,typename Arg3>
  309. struct ranked_non_unique
  310. {
  311. typedef detail::ordered_index_args<
  312. Arg1,Arg2,Arg3> index_args;
  313. typedef typename index_args::tag_list_type::type tag_list_type;
  314. typedef typename index_args::key_from_value_type key_from_value_type;
  315. typedef typename index_args::compare_type compare_type;
  316. template<typename Super>
  317. struct node_class
  318. {
  319. typedef detail::ordered_index_node<detail::rank_policy,Super> type;
  320. };
  321. template<typename SuperMeta>
  322. struct index_class
  323. {
  324. typedef detail::ordered_index<
  325. key_from_value_type,compare_type,
  326. SuperMeta,tag_list_type,detail::ordered_non_unique_tag,
  327. detail::rank_policy> type;
  328. };
  329. };
  330. } /* namespace multi_index */
  331. } /* namespace boost */
  332. #endif