/Src/Dependencies/Boost/boost/multi_index/detail/index_matcher.hpp

http://hadesmem.googlecode.com/ · C++ Header · 248 lines · 148 code · 46 blank · 54 comment · 6 complexity · 2036f888f63d34a2ae69afb283f77121 MD5 · raw file

  1. /* Copyright 2003-2008 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_DETAIL_INDEX_MATCHER_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_INDEX_MATCHER_HPP
  10. #if defined(_MSC_VER)&&(_MSC_VER>=1200)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  14. #include <algorithm>
  15. #include <boost/noncopyable.hpp>
  16. #include <boost/multi_index/detail/auto_space.hpp>
  17. #include <cstddef>
  18. #include <functional>
  19. namespace boost{
  20. namespace multi_index{
  21. namespace detail{
  22. /* index_matcher compares a sequence of elements against a
  23. * base sequence, identifying those elements that belong to the
  24. * longest subsequence which is ordered with respect to the base.
  25. * For instance, if the base sequence is:
  26. *
  27. * 0 1 2 3 4 5 6 7 8 9
  28. *
  29. * and the compared sequence (not necesarilly the same length):
  30. *
  31. * 1 4 2 3 0 7 8 9
  32. *
  33. * the elements of the longest ordered subsequence are:
  34. *
  35. * 1 2 3 7 8 9
  36. *
  37. * The algorithm for obtaining such a subsequence is called
  38. * Patience Sorting, described in ch. 1 of:
  39. * Aldous, D., Diaconis, P.: "Longest increasing subsequences: from
  40. * patience sorting to the Baik-Deift-Johansson Theorem", Bulletin
  41. * of the American Mathematical Society, vol. 36, no 4, pp. 413-432,
  42. * July 1999.
  43. * http://www.ams.org/bull/1999-36-04/S0273-0979-99-00796-X/
  44. * S0273-0979-99-00796-X.pdf
  45. *
  46. * This implementation is not fully generic since it assumes that
  47. * the sequences given are pointed to by index iterators (having a
  48. * get_node() memfun.)
  49. */
  50. namespace index_matcher{
  51. /* The algorithm stores the nodes of the base sequence and a number
  52. * of "piles" that are dynamically updated during the calculation
  53. * stage. From a logical point of view, nodes form an independent
  54. * sequence from piles. They are stored together so as to minimize
  55. * allocated memory.
  56. */
  57. struct entry
  58. {
  59. entry(void* node_,std::size_t pos_=0):node(node_),pos(pos_){}
  60. /* node stuff */
  61. void* node;
  62. std::size_t pos;
  63. entry* previous;
  64. bool ordered;
  65. struct less_by_node
  66. {
  67. bool operator()(
  68. const entry& x,const entry& y)const
  69. {
  70. return std::less<void*>()(x.node,y.node);
  71. }
  72. };
  73. /* pile stuff */
  74. std::size_t pile_top;
  75. entry* pile_top_entry;
  76. struct less_by_pile_top
  77. {
  78. bool operator()(
  79. const entry& x,const entry& y)const
  80. {
  81. return x.pile_top<y.pile_top;
  82. }
  83. };
  84. };
  85. /* common code operating on void *'s */
  86. template<typename Allocator>
  87. class algorithm_base:private noncopyable
  88. {
  89. protected:
  90. algorithm_base(const Allocator& al,std::size_t size):
  91. spc(al,size),size_(size),n(0),sorted(false)
  92. {
  93. }
  94. void add(void* node)
  95. {
  96. entries()[n]=entry(node,n);
  97. ++n;
  98. }
  99. void begin_algorithm()const
  100. {
  101. if(!sorted){
  102. std::sort(entries(),entries()+size_,entry::less_by_node());
  103. sorted=true;
  104. }
  105. num_piles=0;
  106. }
  107. void add_node_to_algorithm(void* node)const
  108. {
  109. entry* ent=
  110. std::lower_bound(
  111. entries(),entries()+size_,
  112. entry(node),entry::less_by_node()); /* localize entry */
  113. ent->ordered=false;
  114. std::size_t n=ent->pos; /* get its position */
  115. entry dummy(0);
  116. dummy.pile_top=n;
  117. entry* pile_ent= /* find the first available pile */
  118. std::lower_bound( /* to stack the entry */
  119. entries(),entries()+num_piles,
  120. dummy,entry::less_by_pile_top());
  121. pile_ent->pile_top=n; /* stack the entry */
  122. pile_ent->pile_top_entry=ent;
  123. /* if not the first pile, link entry to top of the preceding pile */
  124. if(pile_ent>&entries()[0]){
  125. ent->previous=(pile_ent-1)->pile_top_entry;
  126. }
  127. if(pile_ent==&entries()[num_piles]){ /* new pile? */
  128. ++num_piles;
  129. }
  130. }
  131. void finish_algorithm()const
  132. {
  133. if(num_piles>0){
  134. /* Mark those elements which are in their correct position, i.e. those
  135. * belonging to the longest increasing subsequence. These are those
  136. * elements linked from the top of the last pile.
  137. */
  138. entry* ent=entries()[num_piles-1].pile_top_entry;
  139. for(std::size_t n=num_piles;n--;){
  140. ent->ordered=true;
  141. ent=ent->previous;
  142. }
  143. }
  144. }
  145. bool is_ordered(void * node)const
  146. {
  147. return std::lower_bound(
  148. entries(),entries()+size_,
  149. entry(node),entry::less_by_node())->ordered;
  150. }
  151. private:
  152. entry* entries()const{return &*spc.data();}
  153. auto_space<entry,Allocator> spc;
  154. std::size_t size_;
  155. std::size_t n;
  156. mutable bool sorted;
  157. mutable std::size_t num_piles;
  158. };
  159. /* The algorithm has three phases:
  160. * - Initialization, during which the nodes of the base sequence are added.
  161. * - Execution.
  162. * - Results querying, through the is_ordered memfun.
  163. */
  164. template<typename Node,typename Allocator>
  165. class algorithm:private algorithm_base<Allocator>
  166. {
  167. typedef algorithm_base<Allocator> super;
  168. public:
  169. algorithm(const Allocator& al,std::size_t size):super(al,size){}
  170. void add(Node* node)
  171. {
  172. super::add(node);
  173. }
  174. template<typename IndexIterator>
  175. void execute(IndexIterator first,IndexIterator last)const
  176. {
  177. super::begin_algorithm();
  178. for(IndexIterator it=first;it!=last;++it){
  179. add_node_to_algorithm(get_node(it));
  180. }
  181. super::finish_algorithm();
  182. }
  183. bool is_ordered(Node* node)const
  184. {
  185. return super::is_ordered(node);
  186. }
  187. private:
  188. void add_node_to_algorithm(Node* node)const
  189. {
  190. super::add_node_to_algorithm(node);
  191. }
  192. template<typename IndexIterator>
  193. static Node* get_node(IndexIterator it)
  194. {
  195. return static_cast<Node*>(it.get_node());
  196. }
  197. };
  198. } /* namespace multi_index::detail::index_matcher */
  199. } /* namespace multi_index::detail */
  200. } /* namespace multi_index */
  201. } /* namespace boost */
  202. #endif