ug4
Loading...
Searching...
No Matches
bucket_sorter.hpp
Go to the documentation of this file.
1// Felix Salfelder, 2016-2017, 2021
2//
3// This program is free software; you can redistribute it and/or modify it
4// under the terms of the GNU General Public License as published by the
5// Free Software Foundation; either version 3, or (at your option) any
6// later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program; if not, write to the Free Software
15// Foundation, 51 Franklin Street - Suite 500, Boston, MA 02110-1335, USA.
16//
17//
18// // contains code from boost/graph
19// //=======================================================================
20// // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
21// // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
22// //
23// // Distributed under the Boost Software License, Version 1.0. (See
24// // accompanying file LICENSE_1_0.txt or copy at
25// // http://www.boost.org/LICENSE_1_0.txt)
26// //=======================================================================
27#ifndef TREEDEC_BUCKET_SORTER_HPP
28#define TREEDEC_BUCKET_SORTER_HPP
29
30// avoid including the the other one
31#define BOOST_GRAPH_DETAIL_BUCKET_SORTER_HPP
32
33#include <vector>
34#include <cassert>
35#include "trace.h"
36
37// the __FreeBSD__ condition is a wild guess.
38#if defined(BOOST_CLANG) && (1 == BOOST_CLANG) && defined(__FreeBSD__)
39#define ITERATOR_CONSTRUCTOR_WORKAROUND
40#endif
41
42namespace boost {
43
44 template <class BucketType, class ValueType, class Bucket,
45 class ValueIndexMap>
47 public:
48 typedef BucketType bucket_type;
49 typedef ValueType value_type;
50 typedef Bucket Bucket_type;
51 typedef ValueIndexMap value_index_map;
52 typedef typename std::vector<value_type>::size_type size_type;
53
54 bucket_sorter(size_type _length, bucket_type _max_bucket,
55 const Bucket& _bucket = Bucket(),
56 const ValueIndexMap& _id = ValueIndexMap())
57 : next(_length+_max_bucket, invalid_value()),
58 prev(_length+_max_bucket, invalid_value()),
59 head(next.begin() + _length),
60 tail(prev.begin() + _length),
61 id_to_value(_length),
62 bucket(_bucket), id(_id) {
63 // trace2("created bs", _length, _max_bucket);
64 assert(head==next.begin()+_length);
65 assert(!next.size() || head[0]==invalid_value());
66
67
68 auto l = _length;
69 auto h = head;
70 for(; l < _length + _max_bucket; ++l){
71 *h = l;
72 ++h;
73 }
74 }
77
78 void remove(const value_type& x) {
79 size_type i = get(id, x);
80 assert(i<size());
81#if 0
82 std::cerr << "rm " << i << "\n";
83 std::cerr << "rm " << x << " " << i << " " << bucket[x] << "\n";
84 std::cerr << "rm " << head[bucket[x]] << "\n";
85 std::cerr << "rm " << prev[i] << " " << next[i] << "\n";
86#endif
87 auto next_node = next[i];
88 auto prev_node = prev[i];
89 assert(prev_node!=i);
90
91 prev[next_node] = prev_node;
92 next[prev_node] = next_node;
93
94 } // remove
95
96 void push_back(const value_type& x) {
97 assert(x<next.size());
98 id_to_value[get(id, x)] = x;
99 (*this)[bucket[x]].push_back(x);
100 }
101 void push_front(const value_type& x) {
102 // dont know how to do this right now, but i mean it.
103 // assert( !is_in_bucket(x) );
104 assert(x<next.size());
105 id_to_value[get(id, x)] = x;
106 (*this)[bucket[x]].push_front(x);
107 }
108 void push(const value_type& x) {
109 return push_front(x);
110 }
111
112 // update_back?
113 void update_front(const value_type& x) {
114 // dont know how to do this right now, but i mean it.
115 // assert( is_in_bucket(x) );
116 remove(x);
117 (*this)[bucket[x]].push(x);
118 }
119 void update(const value_type& x) {
120 return update_front(x);
121 }
122 void update_back(const value_type& x) { itested();
123 // dont know how to do this right now, but i mean it.
124 // assert( is_in_bucket(x) );
125 remove(x);
126 (*this)[bucket[x]].push_back(x);
127 }
128
129 // private:
130 // with KCC, the nested stack class is having access problems
131 // despite the friend decl.
133 return (std::numeric_limits<size_type>::max)();
134 }
135
136 typedef typename std::vector<size_type>::iterator Iter;
137 typedef typename std::vector<size_type>::const_iterator ConstIter;
138 typedef typename std::vector<value_type>::iterator IndexValueMap;
139 typedef typename std::vector<value_type>::const_iterator ConstIndexValueMap;
140
141 public:
142
143 template<class Iter_, class IndexValueMap_>
144 class stack_ {
145 public:
148 public:
150 // bug? feature?
152 public:
154 : s(s_), b(t) {}
156 : s(p.s), b(p.b) {}
158 : s(p.s), b(p.b) {untested();}
160 public:
162 assert(&s==&o.s); // how to compile time check?!
163 // or just fallback to pointer?
164 b = o.b;
165 return *this;
166 }
168 assert(&s==&o.s); // how to compile time check?!
169 // or just fallback to pointer?
170 b = o.b;
171 return *this;
172 }
174 assert(b<s.size());
175 return s.value[b];
176 }
178 assert(b!=invalid_value());
179 assert(b!=s.next[b]);
180 b = s.next[b];
181 return *this;
182 }
184 return o.b!=b;
185 }
187 { return o.b==b; }
188 private:
189 stack_ const& s;
191 };
192 public:
193 stack_(const stack_& p)
195 head(p.head),
196 next(p.next),
197 prev(p.prev),
198 tail(p.tail),
199 value(p.value),
200 id(p.id)
201 {untested();
202 }
203
205 bucket_id = p.bucket_id;
206 head = p.head;
207 next = p.next;
208 prev = p.prev;
209 tail = p.tail;
210 value = p.value;
211 id = p.id;
212 return *this;
213 }
214 // stack_(const stack_&&){untested();}
215 public:
216 stack_(bucket_type _bucket_id, Iter_ h, Iter_ n, Iter_ p, Iter_ t, IndexValueMap_ v,
217 const ValueIndexMap& _id)
218#ifdef ITERATOR_CONSTRUCTOR_WORKAROUND
219 : bucket_id(_bucket_id), head(), next(), prev(), value(v), id(_id) { itested();
220 head = h;
221 next = n;
222 prev = p;
223#else
224 : bucket_id(_bucket_id), head(h), next(n), prev(p), tail(t), value(v), id(_id) { // }
225#endif
226 }
227
228 // Avoid using default arg for ValueIndexMap so that the default
229 // constructor of the ValueIndexMap is not required if not used.
230 stack_(bucket_type _bucket_id, Iter_ h, Iter_ n, Iter_ p, Iter_ t, IndexValueMap_ v)
231#ifdef ITERATOR_CONSTRUCTOR_WORKAROUND
232 : bucket_id(_bucket_id), head(), next(), prev(), value(v) { untested();
233 head = h;
234 next = n;
235 prev = p;
236#else
237 : bucket_id(_bucket_id), head(h), next(n), prev(p), tail(t), value(v) { untested();
238#endif
239 }
240
241 void push_front(const value_type& x) {
242 const size_type new_head = get(id, x);
243 assert(new_head < size());
244 const size_type current = head[bucket_id];
245 if(new_head == current){ untested();
246// assert(false);
247 }
248 if ( current != invalid_value() ){
249 assert(current!=new_head);
250 prev[current] = new_head;
251 }else{ untested();
252 tail[bucket_id] = new_head;
253 }
254
255 prev[new_head] = bucket_id + (head - next);
256 next[new_head] = current;
257 head[bucket_id] = new_head;
258 }
259 void push_back(const value_type& x) {
260 const size_type new_tail = get(id, x);
261 assert(new_tail < size());
262 const size_type current = tail[bucket_id];
263
264 if(new_tail == current){ untested();
265// assert(false);
266 }
267 if ( current != invalid_value() ){
268 assert(current!=new_tail);
269 next[current] = new_tail;
270 }else{
271 head[bucket_id] = new_tail;
272 }
273
274 prev[new_tail] = current;
275 next[new_tail] = bucket_id + (head - next);
276 tail[bucket_id] = new_tail;
277 }
278 void push(const value_type& x) {
279 return push_front(x);
280 }
281 void pop() {
282 assert(!empty());
283 assert(bucket_id<size());
284 size_type current = head[bucket_id];
285 size_type next_node = next[current];
286 head[bucket_id] = next_node;
287 // prev[bucket_id] = bucket_id + (head - prev);
288 if ( next_node != invalid_value() ){
289// assert(next_node != prev[current]);
290 prev[next_node] = bucket_id + (head - next);
291 }else{ untested();
292 unreachable();
293 }
294 // assert(next_node != prev[current]);
295 }
296 value_type const& top() const { return value[ head[bucket_id] ]; }
297 value_type& top() { return value[ head[bucket_id] ]; }
298 value_type const& front() const { return value[ head[bucket_id] ]; }
300 bool empty() const {
301 return begin() == end();
302 }
303 public: // iterator access
305 return const_iterator(head[bucket_id], *this);
306 }
307 // BUG: template override in degree.hpp does not match (why?)
309 return const_iterator(head[bucket_id], *this);
310 }
312 return const_iterator(size()+bucket_id, *this);
313 }
314 public: // debug
315 size_t size()const{return head-next;}
316 private:
318 Iter_ head;
319 Iter_ next;
320 Iter_ prev;
321 Iter_ tail;
322 IndexValueMap_ value;
323 ValueIndexMap id;
324 }; // stack
325
328
330 assert(i < next.size());
331 return const_stack(i, head, next.begin(), prev.begin(), tail,
332 id_to_value.begin(), id);
333 }
335 assert(i < next.size());
336 return stack(i, head, next.begin(), prev.begin(), tail,
337 id_to_value.begin(), id);
338 }
339 // number of buckets.
340 size_t size() const{ return next.size() - (head-next.begin()); }
341 protected:
342 std::vector<size_type> next;
343 std::vector<size_type> prev;
344 typename std::vector<size_type>::iterator head;
345 typename std::vector<size_type>::iterator tail;
346 std::vector<value_type> id_to_value;
347 Bucket bucket;
348 ValueIndexMap id;
349 }; // stack_
350
351}
352
353#endif
354
355// vim:ts=8:sw=2:et
parameterString p
Definition bucket_sorter.hpp:149
bool operator==(const_iterator const &o)
Definition bucket_sorter.hpp:186
const_iterator(const const_iterator &p)
Definition bucket_sorter.hpp:155
const_iterator()
Definition bucket_sorter.hpp:151
bool operator!=(const_iterator const &o)
Definition bucket_sorter.hpp:183
const_iterator & operator++()
Definition bucket_sorter.hpp:177
size_type b
Definition bucket_sorter.hpp:190
value_type operator*() const
Definition bucket_sorter.hpp:173
const_iterator(const const_iterator &&p)
Definition bucket_sorter.hpp:157
stack_ const & s
Definition bucket_sorter.hpp:189
const_iterator & operator=(const const_iterator &&o)
Definition bucket_sorter.hpp:167
~const_iterator()
Definition bucket_sorter.hpp:159
const_iterator(size_type t, stack_ const &s_)
Definition bucket_sorter.hpp:153
const_iterator & operator=(const const_iterator &o)
Definition bucket_sorter.hpp:161
Definition bucket_sorter.hpp:144
const_iterator end() const
Definition bucket_sorter.hpp:311
IndexValueMap_ value
Definition bucket_sorter.hpp:322
const_iterator rbegin() const
Definition bucket_sorter.hpp:308
stack_(bucket_type _bucket_id, Iter_ h, Iter_ n, Iter_ p, Iter_ t, IndexValueMap_ v, const ValueIndexMap &_id)
Definition bucket_sorter.hpp:216
void push_back(const value_type &x)
Definition bucket_sorter.hpp:259
value_type const & front() const
Definition bucket_sorter.hpp:298
bool empty() const
Definition bucket_sorter.hpp:300
const_iterator begin() const
Definition bucket_sorter.hpp:304
bucket_type bucket_id
Definition bucket_sorter.hpp:317
value_type & front()
Definition bucket_sorter.hpp:299
value_type const & top() const
Definition bucket_sorter.hpp:296
void push(const value_type &x)
Definition bucket_sorter.hpp:278
size_t size() const
Definition bucket_sorter.hpp:315
stack_(const stack_ &p)
Definition bucket_sorter.hpp:193
ValueIndexMap id
Definition bucket_sorter.hpp:323
bucket_sorter base
Definition bucket_sorter.hpp:146
bucket_sorter::value_type value_type
Definition bucket_sorter.hpp:147
Iter_ next
Definition bucket_sorter.hpp:319
Iter_ head
Definition bucket_sorter.hpp:318
void pop()
Definition bucket_sorter.hpp:281
value_type & top()
Definition bucket_sorter.hpp:297
stack_ & operator=(const stack_ &p)
Definition bucket_sorter.hpp:204
Iter_ prev
Definition bucket_sorter.hpp:320
void push_front(const value_type &x)
Definition bucket_sorter.hpp:241
Iter_ tail
Definition bucket_sorter.hpp:321
stack_(bucket_type _bucket_id, Iter_ h, Iter_ n, Iter_ p, Iter_ t, IndexValueMap_ v)
Definition bucket_sorter.hpp:230
Definition bucket_sorter.hpp:46
std::vector< size_type >::const_iterator ConstIter
Definition bucket_sorter.hpp:137
void update_back(const value_type &x)
Definition bucket_sorter.hpp:122
bucket_sorter()
Definition bucket_sorter.hpp:75
stack_< Iter, IndexValueMap > stack
Definition bucket_sorter.hpp:326
BucketType bucket_type
Definition bucket_sorter.hpp:48
std::vector< value_type >::iterator IndexValueMap
Definition bucket_sorter.hpp:138
ValueType value_type
Definition bucket_sorter.hpp:49
std::vector< value_type >::const_iterator ConstIndexValueMap
Definition bucket_sorter.hpp:139
void push(const value_type &x)
Definition bucket_sorter.hpp:108
std::vector< size_type > prev
Definition bucket_sorter.hpp:343
std::vector< size_type > next
Definition bucket_sorter.hpp:342
bucket_sorter(size_type _length, bucket_type _max_bucket, const Bucket &_bucket=Bucket(), const ValueIndexMap &_id=ValueIndexMap())
Definition bucket_sorter.hpp:54
void push_back(const value_type &x)
Definition bucket_sorter.hpp:96
ValueIndexMap id
Definition bucket_sorter.hpp:348
std::vector< value_type >::size_type size_type
Definition bucket_sorter.hpp:52
const_stack operator[](const bucket_type &i) const
Definition bucket_sorter.hpp:329
size_t size() const
Definition bucket_sorter.hpp:340
void remove(const value_type &x)
Definition bucket_sorter.hpp:78
Bucket Bucket_type
Definition bucket_sorter.hpp:50
void push_front(const value_type &x)
Definition bucket_sorter.hpp:101
stack_< ConstIter, ConstIndexValueMap > const_stack
Definition bucket_sorter.hpp:327
std::vector< value_type > id_to_value
Definition bucket_sorter.hpp:346
stack operator[](const bucket_type &i)
Definition bucket_sorter.hpp:334
std::vector< size_type >::iterator Iter
Definition bucket_sorter.hpp:136
ValueIndexMap value_index_map
Definition bucket_sorter.hpp:51
static size_type invalid_value()
Definition bucket_sorter.hpp:132
void update(const value_type &x)
Definition bucket_sorter.hpp:119
std::vector< size_type >::iterator tail
Definition bucket_sorter.hpp:345
Bucket bucket
Definition bucket_sorter.hpp:347
void update_front(const value_type &x)
Definition bucket_sorter.hpp:113
std::vector< size_type >::iterator head
Definition bucket_sorter.hpp:344
#define untested()
Definition lua_table_handle.cpp:15
Definition boost_serialization_routines.h:49
SM_edge_weight_map< typename T::value_type, ug::BidirectionalMatrix< T > > get(edge_weight_t, ug::BidirectionalMatrix< T > const &g)
Definition bidirectional_boost.h:157
#define unreachable()
Definition trace.h:13
#define itested()
Definition trace.h:9