ord_index_ops.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* Copyright 2003-2014 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. * The internal implementation of red-black trees is based on that of SGI STL
  9. * stl_tree.h file:
  10. *
  11. * Copyright (c) 1996,1997
  12. * Silicon Graphics Computer Systems, Inc.
  13. *
  14. * Permission to use, copy, modify, distribute and sell this software
  15. * and its documentation for any purpose is hereby granted without fee,
  16. * provided that the above copyright notice appear in all copies and
  17. * that both that copyright notice and this permission notice appear
  18. * in supporting documentation. Silicon Graphics makes no
  19. * representations about the suitability of this software for any
  20. * purpose. It is provided "as is" without express or implied warranty.
  21. *
  22. *
  23. * Copyright (c) 1994
  24. * Hewlett-Packard Company
  25. *
  26. * Permission to use, copy, modify, distribute and sell this software
  27. * and its documentation for any purpose is hereby granted without fee,
  28. * provided that the above copyright notice appear in all copies and
  29. * that both that copyright notice and this permission notice appear
  30. * in supporting documentation. Hewlett-Packard Company makes no
  31. * representations about the suitability of this software for any
  32. * purpose. It is provided "as is" without express or implied warranty.
  33. *
  34. */
  35. #ifndef BOOST_MULTI_INDEX_DETAIL_ORD_INDEX_OPS_HPP
  36. #define BOOST_MULTI_INDEX_DETAIL_ORD_INDEX_OPS_HPP
  37. #if defined(_MSC_VER)
  38. #pragma once
  39. #endif
  40. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  41. #include <boost/mpl/and.hpp>
  42. #include <boost/multi_index/detail/promotes_arg.hpp>
  43. #include <utility>
  44. namespace boost{
  45. namespace multi_index{
  46. namespace detail{
  47. /* Common code for index memfuns having templatized and
  48. * non-templatized versions.
  49. * Implementation note: When CompatibleKey is consistently promoted to
  50. * KeyFromValue::result_type for comparison, the promotion is made once in
  51. * advance to increase efficiency.
  52. */
  53. template<
  54. typename Node,typename KeyFromValue,
  55. typename CompatibleKey,typename CompatibleCompare
  56. >
  57. inline Node* ordered_index_find(
  58. Node* top,Node* y,const KeyFromValue& key,const CompatibleKey& x,
  59. const CompatibleCompare& comp)
  60. {
  61. typedef typename KeyFromValue::result_type key_type;
  62. return ordered_index_find(
  63. top,y,key,x,comp,
  64. mpl::and_<
  65. promotes_1st_arg<CompatibleCompare,CompatibleKey,key_type>,
  66. promotes_2nd_arg<CompatibleCompare,key_type,CompatibleKey> >());
  67. }
  68. template<
  69. typename Node,typename KeyFromValue,
  70. typename CompatibleCompare
  71. >
  72. inline Node* ordered_index_find(
  73. Node* top,Node* y,const KeyFromValue& key,
  74. const BOOST_DEDUCED_TYPENAME KeyFromValue::result_type& x,
  75. const CompatibleCompare& comp,mpl::true_)
  76. {
  77. return ordered_index_find(top,y,key,x,comp,mpl::false_());
  78. }
  79. template<
  80. typename Node,typename KeyFromValue,
  81. typename CompatibleKey,typename CompatibleCompare
  82. >
  83. inline Node* ordered_index_find(
  84. Node* top,Node* y,const KeyFromValue& key,const CompatibleKey& x,
  85. const CompatibleCompare& comp,mpl::false_)
  86. {
  87. Node* y0=y;
  88. while (top){
  89. if(!comp(key(top->value()),x)){
  90. y=top;
  91. top=Node::from_impl(top->left());
  92. }
  93. else top=Node::from_impl(top->right());
  94. }
  95. return (y==y0||comp(x,key(y->value())))?y0:y;
  96. }
  97. template<
  98. typename Node,typename KeyFromValue,
  99. typename CompatibleKey,typename CompatibleCompare
  100. >
  101. inline Node* ordered_index_lower_bound(
  102. Node* top,Node* y,const KeyFromValue& key,const CompatibleKey& x,
  103. const CompatibleCompare& comp)
  104. {
  105. typedef typename KeyFromValue::result_type key_type;
  106. return ordered_index_lower_bound(
  107. top,y,key,x,comp,
  108. promotes_2nd_arg<CompatibleCompare,key_type,CompatibleKey>());
  109. }
  110. template<
  111. typename Node,typename KeyFromValue,
  112. typename CompatibleCompare
  113. >
  114. inline Node* ordered_index_lower_bound(
  115. Node* top,Node* y,const KeyFromValue& key,
  116. const BOOST_DEDUCED_TYPENAME KeyFromValue::result_type& x,
  117. const CompatibleCompare& comp,mpl::true_)
  118. {
  119. return ordered_index_lower_bound(top,y,key,x,comp,mpl::false_());
  120. }
  121. template<
  122. typename Node,typename KeyFromValue,
  123. typename CompatibleKey,typename CompatibleCompare
  124. >
  125. inline Node* ordered_index_lower_bound(
  126. Node* top,Node* y,const KeyFromValue& key,const CompatibleKey& x,
  127. const CompatibleCompare& comp,mpl::false_)
  128. {
  129. while(top){
  130. if(!comp(key(top->value()),x)){
  131. y=top;
  132. top=Node::from_impl(top->left());
  133. }
  134. else top=Node::from_impl(top->right());
  135. }
  136. return y;
  137. }
  138. template<
  139. typename Node,typename KeyFromValue,
  140. typename CompatibleKey,typename CompatibleCompare
  141. >
  142. inline Node* ordered_index_upper_bound(
  143. Node* top,Node* y,const KeyFromValue& key,const CompatibleKey& x,
  144. const CompatibleCompare& comp)
  145. {
  146. typedef typename KeyFromValue::result_type key_type;
  147. return ordered_index_upper_bound(
  148. top,y,key,x,comp,
  149. promotes_1st_arg<CompatibleCompare,CompatibleKey,key_type>());
  150. }
  151. template<
  152. typename Node,typename KeyFromValue,
  153. typename CompatibleCompare
  154. >
  155. inline Node* ordered_index_upper_bound(
  156. Node* top,Node* y,const KeyFromValue& key,
  157. const BOOST_DEDUCED_TYPENAME KeyFromValue::result_type& x,
  158. const CompatibleCompare& comp,mpl::true_)
  159. {
  160. return ordered_index_upper_bound(top,y,key,x,comp,mpl::false_());
  161. }
  162. template<
  163. typename Node,typename KeyFromValue,
  164. typename CompatibleKey,typename CompatibleCompare
  165. >
  166. inline Node* ordered_index_upper_bound(
  167. Node* top,Node* y,const KeyFromValue& key,const CompatibleKey& x,
  168. const CompatibleCompare& comp,mpl::false_)
  169. {
  170. while(top){
  171. if(comp(x,key(top->value()))){
  172. y=top;
  173. top=Node::from_impl(top->left());
  174. }
  175. else top=Node::from_impl(top->right());
  176. }
  177. return y;
  178. }
  179. template<
  180. typename Node,typename KeyFromValue,
  181. typename CompatibleKey,typename CompatibleCompare
  182. >
  183. inline std::pair<Node*,Node*> ordered_index_equal_range(
  184. Node* top,Node* y,const KeyFromValue& key,const CompatibleKey& x,
  185. const CompatibleCompare& comp)
  186. {
  187. typedef typename KeyFromValue::result_type key_type;
  188. return ordered_index_equal_range(
  189. top,y,key,x,comp,
  190. mpl::and_<
  191. promotes_1st_arg<CompatibleCompare,CompatibleKey,key_type>,
  192. promotes_2nd_arg<CompatibleCompare,key_type,CompatibleKey> >());
  193. }
  194. template<
  195. typename Node,typename KeyFromValue,
  196. typename CompatibleCompare
  197. >
  198. inline std::pair<Node*,Node*> ordered_index_equal_range(
  199. Node* top,Node* y,const KeyFromValue& key,
  200. const BOOST_DEDUCED_TYPENAME KeyFromValue::result_type& x,
  201. const CompatibleCompare& comp,mpl::true_)
  202. {
  203. return ordered_index_equal_range(top,y,key,x,comp,mpl::false_());
  204. }
  205. template<
  206. typename Node,typename KeyFromValue,
  207. typename CompatibleKey,typename CompatibleCompare
  208. >
  209. inline std::pair<Node*,Node*> ordered_index_equal_range(
  210. Node* top,Node* y,const KeyFromValue& key,const CompatibleKey& x,
  211. const CompatibleCompare& comp,mpl::false_)
  212. {
  213. while(top){
  214. if(comp(key(top->value()),x)){
  215. top=Node::from_impl(top->right());
  216. }
  217. else if(comp(x,key(top->value()))){
  218. y=top;
  219. top=Node::from_impl(top->left());
  220. }
  221. else{
  222. return std::pair<Node*,Node*>(
  223. ordered_index_lower_bound(
  224. Node::from_impl(top->left()),top,key,x,comp,mpl::false_()),
  225. ordered_index_upper_bound(
  226. Node::from_impl(top->right()),y,key,x,comp,mpl::false_()));
  227. }
  228. }
  229. return std::pair<Node*,Node*>(y,y);
  230. }
  231. } /* namespace multi_index::detail */
  232. } /* namespace multi_index */
  233. } /* namespace boost */
  234. #endif