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)
6 * See http://www.boost.org/libs/multi_index for library home page.
9 #ifndef BOOST_MULTI_INDEX_DETAIL_INDEX_MATCHER_HPP
10 #define BOOST_MULTI_INDEX_DETAIL_INDEX_MATCHER_HPP
16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
18 #include <boost/noncopyable.hpp>
19 #include <boost/multi_index/detail/auto_space.hpp>
20 #include <boost/multi_index/detail/raw_ptr.hpp>
26 namespace multi_index{
30 /* index_matcher compares a sequence of elements against a
31 * base sequence, identifying those elements that belong to the
32 * longest subsequence which is ordered with respect to the base.
33 * For instance, if the base sequence is:
37 * and the compared sequence (not necesarilly the same length):
41 * the elements of the longest ordered subsequence are:
45 * The algorithm for obtaining such a subsequence is called
46 * Patience Sorting, described in ch. 1 of:
47 * Aldous, D., Diaconis, P.: "Longest increasing subsequences: from
48 * patience sorting to the Baik-Deift-Johansson Theorem", Bulletin
49 * of the American Mathematical Society, vol. 36, no 4, pp. 413-432,
51 * http://www.ams.org/bull/1999-36-04/S0273-0979-99-00796-X/
52 * S0273-0979-99-00796-X.pdf
54 * This implementation is not fully generic since it assumes that
55 * the sequences given are pointed to by index iterators (having a
59 namespace index_matcher{
61 /* The algorithm stores the nodes of the base sequence and a number
62 * of "piles" that are dynamically updated during the calculation
63 * stage. From a logical point of view, nodes form an independent
64 * sequence from piles. They are stored together so as to minimize
70 entry(void* node_,std::size_t pos_=0):node(node_),pos(pos_){}
82 const entry& x,const entry& y)const
84 return std::less<void*>()(x.node,y.node);
91 entry* pile_top_entry;
93 struct less_by_pile_top
96 const entry& x,const entry& y)const
98 return x.pile_top<y.pile_top;
103 /* common code operating on void *'s */
105 template<typename Allocator>
106 class algorithm_base:private noncopyable
109 algorithm_base(const Allocator& al,std::size_t size):
110 spc(al,size),size_(size),n_(0),sorted(false)
116 entries()[n_]=entry(node,n_);
120 void begin_algorithm()const
123 std::sort(entries(),entries()+size_,entry::less_by_node());
129 void add_node_to_algorithm(void* node)const
133 entries(),entries()+size_,
134 entry(node),entry::less_by_node()); /* localize entry */
136 std::size_t n=ent->pos; /* get its position */
141 entry* pile_ent= /* find the first available pile */
142 std::lower_bound( /* to stack the entry */
143 entries(),entries()+num_piles,
144 dummy,entry::less_by_pile_top());
146 pile_ent->pile_top=n; /* stack the entry */
147 pile_ent->pile_top_entry=ent;
149 /* if not the first pile, link entry to top of the preceding pile */
150 if(pile_ent>&entries()[0]){
151 ent->previous=(pile_ent-1)->pile_top_entry;
154 if(pile_ent==&entries()[num_piles]){ /* new pile? */
159 void finish_algorithm()const
162 /* Mark those elements which are in their correct position, i.e. those
163 * belonging to the longest increasing subsequence. These are those
164 * elements linked from the top of the last pile.
167 entry* ent=entries()[num_piles-1].pile_top_entry;
168 for(std::size_t n=num_piles;n--;){
175 bool is_ordered(void * node)const
177 return std::lower_bound(
178 entries(),entries()+size_,
179 entry(node),entry::less_by_node())->ordered;
183 entry* entries()const{return raw_ptr<entry*>(spc.data());}
185 auto_space<entry,Allocator> spc;
189 mutable std::size_t num_piles;
192 /* The algorithm has three phases:
193 * - Initialization, during which the nodes of the base sequence are added.
195 * - Results querying, through the is_ordered memfun.
198 template<typename Node,typename Allocator>
199 class algorithm:private algorithm_base<Allocator>
201 typedef algorithm_base<Allocator> super;
204 algorithm(const Allocator& al,std::size_t size):super(al,size){}
211 template<typename IndexIterator>
212 void execute(IndexIterator first,IndexIterator last)const
214 super::begin_algorithm();
216 for(IndexIterator it=first;it!=last;++it){
217 add_node_to_algorithm(get_node(it));
220 super::finish_algorithm();
223 bool is_ordered(Node* node)const
225 return super::is_ordered(node);
229 void add_node_to_algorithm(Node* node)const
231 super::add_node_to_algorithm(node);
234 template<typename IndexIterator>
235 static Node* get_node(IndexIterator it)
237 return static_cast<Node*>(it.get_node());
241 } /* namespace multi_index::detail::index_matcher */
243 } /* namespace multi_index::detail */
245 } /* namespace multi_index */
247 } /* namespace boost */