index_matcher.hpp 6.0 KB

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