annotate DEPENDENCIES/generic/include/boost/multi_index/detail/index_matcher.hpp @ 125:34e428693f5d vext

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