1 /* Copyright 2003-2005 Joaquín M López Muñoz.
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
12 #if defined(_MSC_VER)&&(_MSC_VER>=1200)
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>
25 namespace multi_index{
29 /* index_matcher compares a sequence of elements against a
30 * base sequence, identifying those elements that belong to the
31 * longest subsequence which is ordered with respect to the base.
32 * For instance, if the base sequence is:
36 * and the compared sequence (not necesarilly the same length):
40 * the elements of the longest ordered subsequence are:
44 * The algorithm for obtaining such a subsequence is called
45 * Patience Sorting, described in ch. 1 of:
46 * Aldous, D., Diaconis, P.: "Longest increasing subsequences: from
47 * patience sorting to the Baik-Deift-Johansson Theorem", Bulletin
48 * of the American Mathematical Society, vol. 36, no 4, pp. 413-432,
50 * http://www.ams.org/bull/1999-36-04/S0273-0979-99-00796-X/
51 * S0273-0979-99-00796-X.pdf
53 * This implementation is not fully generic since it assumes that
54 * the sequences given are pointed to by index iterators (having a
58 namespace index_matcher{
60 /* The algorithm stores the nodes of the base sequence and a number
61 * of "piles" that are dynamically updated during the calculation
62 * stage. From a logical point of view, nodes form an independent
63 * sequence from piles. They are stored together so as to minimize
69 entry(void* node_,std::size_t pos_=0):node(node_),pos(pos_){}
81 const entry& x,const entry& y)const
83 return std::less<void*>()(x.node,y.node);
90 entry* pile_top_entry;
92 struct less_by_pile_top
95 const entry& x,const entry& y)const
97 return x.pile_top<y.pile_top;
102 /* common code operating on void *'s */
104 template<typename Allocator>
105 class algorithm_base:private noncopyable
108 algorithm_base(const Allocator& al,std::size_t size):
109 spc(al,size),size_(size),n(0),sorted(false)
115 entries()[n]=entry(node,n);
119 void begin_algorithm()const
122 std::sort(entries(),entries()+size_,entry::less_by_node());
128 void add_node_to_algorithm(void* node)const
132 entries(),entries()+size_,
133 entry(node),entry::less_by_node()); /* localize entry */
135 std::size_t n=ent->pos; /* get its position */
140 entry* pile_ent= /* find the first available pile */
141 std::lower_bound( /* to stack the entry */
142 entries(),entries()+num_piles,
143 dummy,entry::less_by_pile_top());
145 pile_ent->pile_top=n; /* stack the entry */
146 pile_ent->pile_top_entry=ent;
148 /* if not the first pile, link entry to top of the preceding pile */
149 if(pile_ent>&entries()[0]){
150 ent->previous=(pile_ent-1)->pile_top_entry;
153 if(pile_ent==&entries()[num_piles]){ /* new pile? */
158 void finish_algorithm()const
161 /* Mark those elements which are in their correct position, i.e. those
162 * belonging to the longest increasing subsequence. These are those
163 * elements linked from the top of the last pile.
166 entry* ent=entries()[num_piles-1].pile_top_entry;
167 for(std::size_t n=num_piles;n--;){
174 bool is_ordered(void * node)const
176 return std::lower_bound(
177 entries(),entries()+size_,
178 entry(node),entry::less_by_node())->ordered;
182 entry* entries()const{return spc.data();}
184 auto_space<entry,Allocator> spc;
188 mutable std::size_t num_piles;
191 /* The algorithm has three phases:
192 * - Initialization, during which the nodes of the base sequence are added.
194 * - Results querying, through the is_ordered memfun.
197 template<typename Node,typename Allocator>
198 class algorithm:private algorithm_base<Allocator>
200 typedef algorithm_base<Allocator> super;
203 algorithm(const Allocator& al,std::size_t size):super(al,size){}
210 template<typename IndexIterator>
211 void execute(IndexIterator first,IndexIterator last)const
213 super::begin_algorithm();
215 for(IndexIterator it=first;it!=last;++it){
216 add_node_to_algorithm(get_node(it));
219 super::finish_algorithm();
222 bool is_ordered(Node* node)const
224 return super::is_ordered(node);
228 void add_node_to_algorithm(Node* node)const
230 super::add_node_to_algorithm(node);
233 template<typename IndexIterator>
234 static Node* get_node(IndexIterator it)
236 return static_cast<Node*>(it.get_node());
240 } /* namespace multi_index::detail::index_matcher */
242 } /* namespace multi_index::detail */
244 } /* namespace multi_index */
246 } /* namespace boost */