0
2
0
50
48
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2007 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 |
// Modified for use in LEMON. |
|
20 |
// We should really consider using Boost... |
|
19 |
// This file contains a modified version of the concept checking |
|
20 |
// utility from BOOST. |
|
21 |
// See the appropriate copyright notice below. |
|
21 | 22 |
|
22 |
// |
|
23 | 23 |
// (C) Copyright Jeremy Siek 2000. |
24 | 24 |
// Distributed under the Boost Software License, Version 1.0. (See |
25 | 25 |
// accompanying file LICENSE_1_0.txt or copy at |
26 | 26 |
// http://www.boost.org/LICENSE_1_0.txt) |
27 | 27 |
// |
28 | 28 |
// Revision History: |
29 | 29 |
// 05 May 2001: Workarounds for HP aCC from Thomas Matelich. (Jeremy Siek) |
30 | 30 |
// 02 April 2001: Removed limits header altogether. (Jeremy Siek) |
31 | 31 |
// 01 April 2001: Modified to use new <boost/limits.hpp> header. (JMaddock) |
32 | 32 |
// |
33 | 33 |
|
34 | 34 |
// See http://www.boost.org/libs/concept_check for documentation. |
35 | 35 |
|
36 | 36 |
#ifndef LEMON_BOOST_CONCEPT_CHECKS_HPP |
37 | 37 |
#define LEMON_BOOST_CONCEPT_CHECKS_HPP |
38 | 38 |
|
39 | 39 |
namespace lemon { |
40 | 40 |
|
41 | 41 |
/* |
42 | 42 |
"inline" is used for ignore_unused_variable_warning() |
43 | 43 |
and function_requires() to make sure there is no |
44 | 44 |
overtarget with g++. |
45 | 45 |
*/ |
46 | 46 |
|
47 | 47 |
template <class T> inline void ignore_unused_variable_warning(const T&) { } |
48 | 48 |
|
49 | 49 |
template <class Concept> |
50 | 50 |
inline void function_requires() |
51 | 51 |
{ |
52 | 52 |
#if !defined(NDEBUG) |
53 | 53 |
void (Concept::*x)() = & Concept::constraints; |
54 | 54 |
ignore_unused_variable_warning(x); |
55 | 55 |
#endif |
56 | 56 |
} |
57 | 57 |
|
58 | 58 |
template <typename Concept, typename Type> |
59 | 59 |
inline void checkConcept() { |
60 | 60 |
#if !defined(NDEBUG) |
61 | 61 |
typedef typename Concept::template Constraints<Type> ConceptCheck; |
62 | 62 |
void (ConceptCheck::*x)() = & ConceptCheck::constraints; |
63 | 63 |
ignore_unused_variable_warning(x); |
64 | 64 |
#endif |
65 | 65 |
} |
66 | 66 |
|
67 | 67 |
#define BOOST_CLASS_REQUIRE(type_var, ns, concept) \ |
68 | 68 |
typedef void (ns::concept <type_var>::* func##type_var##concept)(); \ |
69 | 69 |
template <func##type_var##concept Tp1_> \ |
70 | 70 |
struct concept_checking_##type_var##concept { }; \ |
... | ... |
@@ -202,97 +202,99 @@ |
202 | 202 |
StdMap& operator=(const StdMap&); |
203 | 203 |
|
204 | 204 |
public: |
205 | 205 |
|
206 | 206 |
///\e |
207 | 207 |
Reference operator[](const Key &k) { |
208 | 208 |
typename Map::iterator it = _map.lower_bound(k); |
209 | 209 |
if (it != _map.end() && !_map.key_comp()(k, it->first)) |
210 | 210 |
return it->second; |
211 | 211 |
else |
212 | 212 |
return _map.insert(it, std::make_pair(k, _value))->second; |
213 | 213 |
} |
214 | 214 |
|
215 | 215 |
/// \e |
216 | 216 |
ConstReference operator[](const Key &k) const { |
217 | 217 |
typename Map::const_iterator it = _map.find(k); |
218 | 218 |
if (it != _map.end()) |
219 | 219 |
return it->second; |
220 | 220 |
else |
221 | 221 |
return _value; |
222 | 222 |
} |
223 | 223 |
|
224 | 224 |
/// \e |
225 | 225 |
void set(const Key &k, const T &t) { |
226 | 226 |
typename Map::iterator it = _map.lower_bound(k); |
227 | 227 |
if (it != _map.end() && !_map.key_comp()(k, it->first)) |
228 | 228 |
it->second = t; |
229 | 229 |
else |
230 | 230 |
_map.insert(it, std::make_pair(k, t)); |
231 | 231 |
} |
232 | 232 |
|
233 | 233 |
/// \e |
234 | 234 |
void setAll(const T &t) { |
235 | 235 |
_value = t; |
236 | 236 |
_map.clear(); |
237 | 237 |
} |
238 | 238 |
|
239 | 239 |
template <typename T1, typename C1 = std::less<T1> > |
240 | 240 |
struct rebind { |
241 | 241 |
typedef StdMap<Key, T1, C1> other; |
242 | 242 |
}; |
243 | 243 |
}; |
244 | 244 |
|
245 | 245 |
/// \brief Map for storing values for the range \c [0..size-1] range keys |
246 | 246 |
/// |
247 | 247 |
/// The current map has the \c [0..size-1] keyset and the values |
248 | 248 |
/// are stored in a \c std::vector<T> container. It can be used with |
249 | 249 |
/// some data structures, for example \c UnionFind, \c BinHeap, when |
250 |
/// the used items are small integer numbers. |
|
250 |
/// the used items are small integer numbers. |
|
251 |
/// |
|
252 |
/// \todo Revise its name |
|
251 | 253 |
template <typename T> |
252 | 254 |
class IntegerMap { |
253 | 255 |
|
254 | 256 |
template <typename T1> |
255 | 257 |
friend class IntegerMap; |
256 | 258 |
|
257 | 259 |
public: |
258 | 260 |
|
259 | 261 |
typedef True ReferenceMapTag; |
260 | 262 |
///\e |
261 | 263 |
typedef int Key; |
262 | 264 |
///\e |
263 | 265 |
typedef T Value; |
264 | 266 |
///\e |
265 | 267 |
typedef T& Reference; |
266 | 268 |
///\e |
267 | 269 |
typedef const T& ConstReference; |
268 | 270 |
|
269 | 271 |
private: |
270 | 272 |
|
271 | 273 |
typedef std::vector<T> Vector; |
272 | 274 |
Vector _vector; |
273 | 275 |
|
274 | 276 |
public: |
275 | 277 |
|
276 | 278 |
/// Constructor with specified default value |
277 | 279 |
IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {} |
278 | 280 |
|
279 | 281 |
/// \brief Constructs the map from an appropriate std::vector. |
280 | 282 |
template <typename T1> |
281 | 283 |
IntegerMap(const std::vector<T1>& vector) |
282 | 284 |
: _vector(vector.begin(), vector.end()) {} |
283 | 285 |
|
284 | 286 |
/// \brief Constructs a map from an other IntegerMap. |
285 | 287 |
template <typename T1> |
286 | 288 |
IntegerMap(const IntegerMap<T1> &c) |
287 | 289 |
: _vector(c._vector.begin(), c._vector.end()) {} |
288 | 290 |
|
289 | 291 |
/// \brief Resize the container |
290 | 292 |
void resize(int size, const T& value = T()) { |
291 | 293 |
_vector.resize(size, value); |
292 | 294 |
} |
293 | 295 |
|
294 | 296 |
private: |
295 | 297 |
|
296 | 298 |
IntegerMap& operator=(const IntegerMap&); |
297 | 299 |
|
298 | 300 |
public: |
... | ... |
@@ -301,392 +303,396 @@ |
301 | 303 |
Reference operator[](Key k) { |
302 | 304 |
return _vector[k]; |
303 | 305 |
} |
304 | 306 |
|
305 | 307 |
/// \e |
306 | 308 |
ConstReference operator[](Key k) const { |
307 | 309 |
return _vector[k]; |
308 | 310 |
} |
309 | 311 |
|
310 | 312 |
/// \e |
311 | 313 |
void set(const Key &k, const T& t) { |
312 | 314 |
_vector[k] = t; |
313 | 315 |
} |
314 | 316 |
|
315 | 317 |
}; |
316 | 318 |
|
317 | 319 |
/// @} |
318 | 320 |
|
319 | 321 |
/// \addtogroup map_adaptors |
320 | 322 |
/// @{ |
321 | 323 |
|
322 | 324 |
/// \brief Identity mapping. |
323 | 325 |
/// |
324 | 326 |
/// This mapping gives back the given key as value without any |
325 | 327 |
/// modification. |
326 | 328 |
template <typename T> |
327 | 329 |
class IdentityMap : public MapBase<T, T> { |
328 | 330 |
public: |
329 | 331 |
typedef MapBase<T, T> Parent; |
330 | 332 |
typedef typename Parent::Key Key; |
331 | 333 |
typedef typename Parent::Value Value; |
332 | 334 |
|
333 | 335 |
/// \e |
334 | 336 |
const T& operator[](const T& t) const { |
335 | 337 |
return t; |
336 | 338 |
} |
337 | 339 |
}; |
338 | 340 |
|
339 | 341 |
///Returns an \c IdentityMap class |
340 | 342 |
|
341 | 343 |
///This function just returns an \c IdentityMap class. |
342 | 344 |
///\relates IdentityMap |
343 | 345 |
template<typename T> |
344 | 346 |
inline IdentityMap<T> identityMap() { |
345 | 347 |
return IdentityMap<T>(); |
346 | 348 |
} |
347 | 349 |
|
348 | 350 |
|
349 |
///Convert the \c Value of a map to another type. |
|
350 |
|
|
351 |
///\brief Convert the \c Value of a map to another type using |
|
352 |
///the default conversion. |
|
353 |
/// |
|
351 | 354 |
///This \c concepts::ReadMap "read only map" |
352 | 355 |
///converts the \c Value of a maps to type \c T. |
353 | 356 |
///Its \c Key is inherited from \c M. |
354 | 357 |
template <typename M, typename T> |
355 | 358 |
class ConvertMap : public MapBase<typename M::Key, T> { |
356 | 359 |
const M& m; |
357 | 360 |
public: |
358 | 361 |
typedef MapBase<typename M::Key, T> Parent; |
359 | 362 |
typedef typename Parent::Key Key; |
360 | 363 |
typedef typename Parent::Value Value; |
361 | 364 |
|
362 | 365 |
///Constructor |
363 | 366 |
|
364 | 367 |
///Constructor |
365 | 368 |
///\param _m is the underlying map |
366 | 369 |
ConvertMap(const M &_m) : m(_m) {}; |
367 | 370 |
|
368 | 371 |
/// \brief The subscript operator. |
369 | 372 |
/// |
370 | 373 |
/// The subscript operator. |
371 |
/// \param k The key |
|
372 |
/// \return The target of the arc |
|
373 | 374 |
Value operator[](const Key& k) const {return m[k];} |
374 | 375 |
}; |
375 | 376 |
|
376 | 377 |
///Returns an \c ConvertMap class |
377 | 378 |
|
378 | 379 |
///This function just returns an \c ConvertMap class. |
379 | 380 |
///\relates ConvertMap |
380 | 381 |
template<typename T, typename M> |
381 | 382 |
inline ConvertMap<M, T> convertMap(const M &m) { |
382 | 383 |
return ConvertMap<M, T>(m); |
383 | 384 |
} |
384 | 385 |
|
385 | 386 |
///Simple wrapping of the map |
386 | 387 |
|
387 | 388 |
///This \c concepts::ReadMap "read only map" returns the simple |
388 | 389 |
///wrapping of the given map. Sometimes the reference maps cannot be |
389 | 390 |
///combined with simple read maps. This map adaptor wraps the given |
390 | 391 |
///map to simple read map. |
392 |
/// |
|
393 |
/// \todo Revise the misleading name |
|
391 | 394 |
template<typename M> |
392 | 395 |
class SimpleMap : public MapBase<typename M::Key, typename M::Value> { |
393 | 396 |
const M& m; |
394 | 397 |
|
395 | 398 |
public: |
396 | 399 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
397 | 400 |
typedef typename Parent::Key Key; |
398 | 401 |
typedef typename Parent::Value Value; |
399 | 402 |
|
400 | 403 |
///Constructor |
401 | 404 |
SimpleMap(const M &_m) : m(_m) {}; |
402 | 405 |
///\e |
403 | 406 |
Value operator[](Key k) const {return m[k];} |
404 | 407 |
}; |
405 | 408 |
|
406 | 409 |
///Simple writeable wrapping of the map |
407 | 410 |
|
408 |
///This \c concepts:: |
|
411 |
///This \c concepts::WriteMap "write map" returns the simple |
|
409 | 412 |
///wrapping of the given map. Sometimes the reference maps cannot be |
410 | 413 |
///combined with simple read-write maps. This map adaptor wraps the |
411 | 414 |
///given map to simple read-write map. |
415 |
/// |
|
416 |
/// \todo Revise the misleading name |
|
412 | 417 |
template<typename M> |
413 | 418 |
class SimpleWriteMap : public MapBase<typename M::Key, typename M::Value> { |
414 | 419 |
M& m; |
415 | 420 |
|
416 | 421 |
public: |
417 | 422 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
418 | 423 |
typedef typename Parent::Key Key; |
419 | 424 |
typedef typename Parent::Value Value; |
420 | 425 |
|
421 | 426 |
///Constructor |
422 | 427 |
SimpleWriteMap(M &_m) : m(_m) {}; |
423 | 428 |
///\e |
424 | 429 |
Value operator[](Key k) const {return m[k];} |
425 | 430 |
///\e |
426 | 431 |
void set(Key k, const Value& c) { m.set(k, c); } |
427 | 432 |
}; |
428 | 433 |
|
429 | 434 |
///Sum of two maps |
430 | 435 |
|
431 | 436 |
///This \c concepts::ReadMap "read only map" returns the sum of the two |
432 | 437 |
///given maps. Its \c Key and \c Value will be inherited from \c M1. |
433 | 438 |
///The \c Key and \c Value of M2 must be convertible to those of \c M1. |
434 | 439 |
|
435 | 440 |
template<typename M1, typename M2> |
436 | 441 |
class AddMap : public MapBase<typename M1::Key, typename M1::Value> { |
437 | 442 |
const M1& m1; |
438 | 443 |
const M2& m2; |
439 | 444 |
|
440 | 445 |
public: |
441 | 446 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
442 | 447 |
typedef typename Parent::Key Key; |
443 | 448 |
typedef typename Parent::Value Value; |
444 | 449 |
|
445 | 450 |
///Constructor |
446 | 451 |
AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
447 | 452 |
///\e |
448 | 453 |
Value operator[](Key k) const {return m1[k]+m2[k];} |
449 | 454 |
}; |
450 | 455 |
|
451 | 456 |
///Returns an \c AddMap class |
452 | 457 |
|
453 | 458 |
///This function just returns an \c AddMap class. |
454 | 459 |
///\todo How to call these type of functions? |
455 | 460 |
/// |
456 | 461 |
///\relates AddMap |
457 | 462 |
template<typename M1, typename M2> |
458 | 463 |
inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) { |
459 | 464 |
return AddMap<M1, M2>(m1,m2); |
460 | 465 |
} |
461 | 466 |
|
462 | 467 |
///Shift a map with a constant. |
463 | 468 |
|
464 | 469 |
///This \c concepts::ReadMap "read only map" returns the sum of the |
465 | 470 |
///given map and a constant value. |
466 | 471 |
///Its \c Key and \c Value is inherited from \c M. |
467 | 472 |
/// |
468 | 473 |
///Actually, |
469 | 474 |
///\code |
470 | 475 |
/// ShiftMap<X> sh(x,v); |
471 | 476 |
///\endcode |
472 | 477 |
///is equivalent with |
473 | 478 |
///\code |
474 | 479 |
/// ConstMap<X::Key, X::Value> c_tmp(v); |
475 | 480 |
/// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v); |
476 | 481 |
///\endcode |
477 | 482 |
template<typename M, typename C = typename M::Value> |
478 | 483 |
class ShiftMap : public MapBase<typename M::Key, typename M::Value> { |
479 | 484 |
const M& m; |
480 | 485 |
C v; |
481 | 486 |
public: |
482 | 487 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
483 | 488 |
typedef typename Parent::Key Key; |
484 | 489 |
typedef typename Parent::Value Value; |
485 | 490 |
|
486 | 491 |
///Constructor |
487 | 492 |
|
488 | 493 |
///Constructor |
489 | 494 |
///\param _m is the undelying map |
490 | 495 |
///\param _v is the shift value |
491 | 496 |
ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {}; |
492 | 497 |
///\e |
493 | 498 |
Value operator[](Key k) const {return m[k] + v;} |
494 | 499 |
}; |
495 | 500 |
|
496 |
///Shift a map with a constant. |
|
501 |
///Shift a map with a constant. This map is also writable. |
|
497 | 502 |
|
498 | 503 |
///This \c concepts::ReadWriteMap "read-write map" returns the sum of the |
499 | 504 |
///given map and a constant value. It makes also possible to write the map. |
500 | 505 |
///Its \c Key and \c Value is inherited from \c M. |
501 | 506 |
/// |
502 | 507 |
///Actually, |
503 | 508 |
///\code |
504 | 509 |
/// ShiftMap<X> sh(x,v); |
505 | 510 |
///\endcode |
506 | 511 |
///is equivalent with |
507 | 512 |
///\code |
508 | 513 |
/// ConstMap<X::Key, X::Value> c_tmp(v); |
509 | 514 |
/// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v); |
510 | 515 |
///\endcode |
511 | 516 |
template<typename M, typename C = typename M::Value> |
512 | 517 |
class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> { |
513 | 518 |
M& m; |
514 | 519 |
C v; |
515 | 520 |
public: |
516 | 521 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
517 | 522 |
typedef typename Parent::Key Key; |
518 | 523 |
typedef typename Parent::Value Value; |
519 | 524 |
|
520 | 525 |
///Constructor |
521 | 526 |
|
522 | 527 |
///Constructor |
523 | 528 |
///\param _m is the undelying map |
524 | 529 |
///\param _v is the shift value |
525 | 530 |
ShiftWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {}; |
526 | 531 |
/// \e |
527 | 532 |
Value operator[](Key k) const {return m[k] + v;} |
528 | 533 |
/// \e |
529 | 534 |
void set(Key k, const Value& c) { m.set(k, c - v); } |
530 | 535 |
}; |
531 | 536 |
|
532 | 537 |
///Returns an \c ShiftMap class |
533 | 538 |
|
534 | 539 |
///This function just returns an \c ShiftMap class. |
535 | 540 |
///\relates ShiftMap |
536 | 541 |
template<typename M, typename C> |
537 | 542 |
inline ShiftMap<M, C> shiftMap(const M &m,const C &v) { |
538 | 543 |
return ShiftMap<M, C>(m,v); |
539 | 544 |
} |
540 | 545 |
|
541 | 546 |
template<typename M, typename C> |
542 | 547 |
inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) { |
543 | 548 |
return ShiftWriteMap<M, C>(m,v); |
544 | 549 |
} |
545 | 550 |
|
546 | 551 |
///Difference of two maps |
547 | 552 |
|
548 | 553 |
///This \c concepts::ReadMap "read only map" returns the difference |
549 | 554 |
///of the values of the two |
550 | 555 |
///given maps. Its \c Key and \c Value will be inherited from \c M1. |
551 | 556 |
///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
552 |
|
|
557 |
/// |
|
558 |
/// \todo Revise the misleading name |
|
553 | 559 |
template<typename M1, typename M2> |
554 | 560 |
class SubMap : public MapBase<typename M1::Key, typename M1::Value> { |
555 | 561 |
const M1& m1; |
556 | 562 |
const M2& m2; |
557 | 563 |
public: |
558 | 564 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
559 | 565 |
typedef typename Parent::Key Key; |
560 | 566 |
typedef typename Parent::Value Value; |
561 | 567 |
|
562 | 568 |
///Constructor |
563 | 569 |
SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
564 | 570 |
/// \e |
565 | 571 |
Value operator[](Key k) const {return m1[k]-m2[k];} |
566 | 572 |
}; |
567 | 573 |
|
568 | 574 |
///Returns a \c SubMap class |
569 | 575 |
|
570 | 576 |
///This function just returns a \c SubMap class. |
571 | 577 |
/// |
572 | 578 |
///\relates SubMap |
573 | 579 |
template<typename M1, typename M2> |
574 | 580 |
inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) { |
575 | 581 |
return SubMap<M1, M2>(m1, m2); |
576 | 582 |
} |
577 | 583 |
|
578 | 584 |
///Product of two maps |
579 | 585 |
|
580 | 586 |
///This \c concepts::ReadMap "read only map" returns the product of the |
581 | 587 |
///values of the two |
582 | 588 |
///given |
583 | 589 |
///maps. Its \c Key and \c Value will be inherited from \c M1. |
584 | 590 |
///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
585 | 591 |
|
586 | 592 |
template<typename M1, typename M2> |
587 | 593 |
class MulMap : public MapBase<typename M1::Key, typename M1::Value> { |
588 | 594 |
const M1& m1; |
589 | 595 |
const M2& m2; |
590 | 596 |
public: |
591 | 597 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
592 | 598 |
typedef typename Parent::Key Key; |
593 | 599 |
typedef typename Parent::Value Value; |
594 | 600 |
|
595 | 601 |
///Constructor |
596 | 602 |
MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
597 | 603 |
/// \e |
598 | 604 |
Value operator[](Key k) const {return m1[k]*m2[k];} |
599 | 605 |
}; |
600 | 606 |
|
601 | 607 |
///Returns a \c MulMap class |
602 | 608 |
|
603 | 609 |
///This function just returns a \c MulMap class. |
604 | 610 |
///\relates MulMap |
605 | 611 |
template<typename M1, typename M2> |
606 | 612 |
inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) { |
607 | 613 |
return MulMap<M1, M2>(m1,m2); |
608 | 614 |
} |
609 | 615 |
|
610 | 616 |
///Scales a maps with a constant. |
611 | 617 |
|
612 | 618 |
///This \c concepts::ReadMap "read only map" returns the value of the |
613 | 619 |
///given map multiplied from the left side with a constant value. |
614 | 620 |
///Its \c Key and \c Value is inherited from \c M. |
615 | 621 |
/// |
616 | 622 |
///Actually, |
617 | 623 |
///\code |
618 | 624 |
/// ScaleMap<X> sc(x,v); |
619 | 625 |
///\endcode |
620 | 626 |
///is equivalent with |
621 | 627 |
///\code |
622 | 628 |
/// ConstMap<X::Key, X::Value> c_tmp(v); |
623 | 629 |
/// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v); |
624 | 630 |
///\endcode |
625 | 631 |
template<typename M, typename C = typename M::Value> |
626 | 632 |
class ScaleMap : public MapBase<typename M::Key, typename M::Value> { |
627 | 633 |
const M& m; |
628 | 634 |
C v; |
629 | 635 |
public: |
630 | 636 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
631 | 637 |
typedef typename Parent::Key Key; |
632 | 638 |
typedef typename Parent::Value Value; |
633 | 639 |
|
634 | 640 |
///Constructor |
635 | 641 |
|
636 | 642 |
///Constructor |
637 | 643 |
///\param _m is the undelying map |
638 | 644 |
///\param _v is the scaling value |
639 | 645 |
ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {}; |
640 | 646 |
/// \e |
641 | 647 |
Value operator[](Key k) const {return v * m[k];} |
642 | 648 |
}; |
643 | 649 |
|
644 |
///Scales a maps with a constant. |
|
650 |
///Scales a maps with a constant (ReadWrite version). |
|
645 | 651 |
|
646 | 652 |
///This \c concepts::ReadWriteMap "read-write map" returns the value of the |
647 | 653 |
///given map multiplied from the left side with a constant value. It can |
648 | 654 |
///be used as write map also if the given multiplier is not zero. |
649 | 655 |
///Its \c Key and \c Value is inherited from \c M. |
650 | 656 |
template<typename M, typename C = typename M::Value> |
651 | 657 |
class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> { |
652 | 658 |
M& m; |
653 | 659 |
C v; |
654 | 660 |
public: |
655 | 661 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
656 | 662 |
typedef typename Parent::Key Key; |
657 | 663 |
typedef typename Parent::Value Value; |
658 | 664 |
|
659 | 665 |
///Constructor |
660 | 666 |
|
661 | 667 |
///Constructor |
662 | 668 |
///\param _m is the undelying map |
663 | 669 |
///\param _v is the scaling value |
664 | 670 |
ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {}; |
665 | 671 |
/// \e |
666 | 672 |
Value operator[](Key k) const {return v * m[k];} |
667 | 673 |
/// \e |
668 | 674 |
void set(Key k, const Value& c) { m.set(k, c / v);} |
669 | 675 |
}; |
670 | 676 |
|
671 | 677 |
///Returns an \c ScaleMap class |
672 | 678 |
|
673 | 679 |
///This function just returns an \c ScaleMap class. |
674 | 680 |
///\relates ScaleMap |
675 | 681 |
template<typename M, typename C> |
676 | 682 |
inline ScaleMap<M, C> scaleMap(const M &m,const C &v) { |
677 | 683 |
return ScaleMap<M, C>(m,v); |
678 | 684 |
} |
679 | 685 |
|
680 | 686 |
template<typename M, typename C> |
681 | 687 |
inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) { |
682 | 688 |
return ScaleWriteMap<M, C>(m,v); |
683 | 689 |
} |
684 | 690 |
|
685 | 691 |
///Quotient of two maps |
686 | 692 |
|
687 | 693 |
///This \c concepts::ReadMap "read only map" returns the quotient of the |
688 | 694 |
///values of the two |
689 | 695 |
///given maps. Its \c Key and \c Value will be inherited from \c M1. |
690 | 696 |
///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
691 | 697 |
|
692 | 698 |
template<typename M1, typename M2> |
... | ... |
@@ -813,155 +819,143 @@ |
813 | 819 |
///\code |
814 | 820 |
///addMap(m1,m2) |
815 | 821 |
///\endcode |
816 | 822 |
/// |
817 | 823 |
///This function is specialized for adaptable binary function |
818 | 824 |
///classes and c++ functions. |
819 | 825 |
/// |
820 | 826 |
///\relates CombineMap |
821 | 827 |
template<typename M1, typename M2, typename F, typename V> |
822 | 828 |
inline CombineMap<M1, M2, F, V> |
823 | 829 |
combineMap(const M1& m1,const M2& m2, const F& f) { |
824 | 830 |
return CombineMap<M1, M2, F, V>(m1,m2,f); |
825 | 831 |
} |
826 | 832 |
|
827 | 833 |
template<typename M1, typename M2, typename F> |
828 | 834 |
inline CombineMap<M1, M2, F, typename F::result_type> |
829 | 835 |
combineMap(const M1& m1, const M2& m2, const F& f) { |
830 | 836 |
return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f); |
831 | 837 |
} |
832 | 838 |
|
833 | 839 |
template<typename M1, typename M2, typename K1, typename K2, typename V> |
834 | 840 |
inline CombineMap<M1, M2, V (*)(K1, K2), V> |
835 | 841 |
combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) { |
836 | 842 |
return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f); |
837 | 843 |
} |
838 | 844 |
|
839 | 845 |
///Negative value of a map |
840 | 846 |
|
841 | 847 |
///This \c concepts::ReadMap "read only map" returns the negative |
842 | 848 |
///value of the |
843 | 849 |
///value returned by the |
844 | 850 |
///given map. Its \c Key and \c Value will be inherited from \c M. |
845 | 851 |
///The unary \c - operator must be defined for \c Value, of course. |
846 | 852 |
|
847 | 853 |
template<typename M> |
848 | 854 |
class NegMap : public MapBase<typename M::Key, typename M::Value> { |
849 | 855 |
const M& m; |
850 | 856 |
public: |
851 | 857 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
852 | 858 |
typedef typename Parent::Key Key; |
853 | 859 |
typedef typename Parent::Value Value; |
854 | 860 |
|
855 | 861 |
///Constructor |
856 | 862 |
NegMap(const M &_m) : m(_m) {}; |
857 | 863 |
/// \e |
858 | 864 |
Value operator[](Key k) const {return -m[k];} |
859 | 865 |
}; |
860 | 866 |
|
861 |
///Negative value of a map |
|
867 |
///Negative value of a map (ReadWrite version) |
|
862 | 868 |
|
863 | 869 |
///This \c concepts::ReadWriteMap "read-write map" returns the negative |
864 | 870 |
///value of the value returned by the |
865 | 871 |
///given map. Its \c Key and \c Value will be inherited from \c M. |
866 | 872 |
///The unary \c - operator must be defined for \c Value, of course. |
867 | 873 |
|
868 | 874 |
template<typename M> |
869 | 875 |
class NegWriteMap : public MapBase<typename M::Key, typename M::Value> { |
870 | 876 |
M& m; |
871 | 877 |
public: |
872 | 878 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
873 | 879 |
typedef typename Parent::Key Key; |
874 | 880 |
typedef typename Parent::Value Value; |
875 | 881 |
|
876 | 882 |
///Constructor |
877 | 883 |
NegWriteMap(M &_m) : m(_m) {}; |
878 | 884 |
/// \e |
879 | 885 |
Value operator[](Key k) const {return -m[k];} |
880 | 886 |
/// \e |
881 | 887 |
void set(Key k, const Value& v) { m.set(k, -v); } |
882 | 888 |
}; |
883 | 889 |
|
884 | 890 |
///Returns a \c NegMap class |
885 | 891 |
|
886 | 892 |
///This function just returns a \c NegMap class. |
887 | 893 |
///\relates NegMap |
888 | 894 |
template <typename M> |
889 | 895 |
inline NegMap<M> negMap(const M &m) { |
890 | 896 |
return NegMap<M>(m); |
891 | 897 |
} |
892 | 898 |
|
893 | 899 |
template <typename M> |
894 | 900 |
inline NegWriteMap<M> negMap(M &m) { |
895 | 901 |
return NegWriteMap<M>(m); |
896 | 902 |
} |
897 | 903 |
|
898 | 904 |
///Absolute value of a map |
899 | 905 |
|
900 | 906 |
///This \c concepts::ReadMap "read only map" returns the absolute value |
901 | 907 |
///of the |
902 | 908 |
///value returned by the |
903 | 909 |
///given map. Its \c Key and \c Value will be inherited |
904 | 910 |
///from <tt>M</tt>. <tt>Value</tt> |
905 | 911 |
///must be comparable to <tt>0</tt> and the unary <tt>-</tt> |
906 | 912 |
///operator must be defined for it, of course. |
907 | 913 |
/// |
908 |
///\bug We need a unified way to handle the situation below: |
|
909 |
///\code |
|
910 |
/// struct _UnConvertible {}; |
|
911 |
/// template<class A> inline A t_abs(A a) {return _UnConvertible();} |
|
912 |
/// template<> inline int t_abs<>(int n) {return abs(n);} |
|
913 |
/// template<> inline long int t_abs<>(long int n) {return labs(n);} |
|
914 |
/// template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);} |
|
915 |
/// template<> inline float t_abs<>(float n) {return fabsf(n);} |
|
916 |
/// template<> inline double t_abs<>(double n) {return fabs(n);} |
|
917 |
/// template<> inline long double t_abs<>(long double n) {return fabsl(n);} |
|
918 |
///\endcode |
|
919 |
|
|
920 | 914 |
|
921 | 915 |
template<typename M> |
922 | 916 |
class AbsMap : public MapBase<typename M::Key, typename M::Value> { |
923 | 917 |
const M& m; |
924 | 918 |
public: |
925 | 919 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
926 | 920 |
typedef typename Parent::Key Key; |
927 | 921 |
typedef typename Parent::Value Value; |
928 | 922 |
|
929 | 923 |
///Constructor |
930 | 924 |
AbsMap(const M &_m) : m(_m) {}; |
931 | 925 |
/// \e |
932 | 926 |
Value operator[](Key k) const { |
933 | 927 |
Value tmp = m[k]; |
934 | 928 |
return tmp >= 0 ? tmp : -tmp; |
935 | 929 |
} |
936 | 930 |
|
937 | 931 |
}; |
938 | 932 |
|
939 | 933 |
///Returns a \c AbsMap class |
940 | 934 |
|
941 | 935 |
///This function just returns a \c AbsMap class. |
942 | 936 |
///\relates AbsMap |
943 | 937 |
template<typename M> |
944 | 938 |
inline AbsMap<M> absMap(const M &m) { |
945 | 939 |
return AbsMap<M>(m); |
946 | 940 |
} |
947 | 941 |
|
948 | 942 |
///Converts an STL style functor to a map |
949 | 943 |
|
950 | 944 |
///This \c concepts::ReadMap "read only map" returns the value |
951 | 945 |
///of a |
952 | 946 |
///given map. |
953 | 947 |
/// |
954 | 948 |
///Template parameters \c K and \c V will become its |
955 | 949 |
///\c Key and \c Value. They must be given explicitely |
956 | 950 |
///because a functor does not provide such typedefs. |
957 | 951 |
/// |
958 | 952 |
///Parameter \c F is the type of the used functor. |
959 | 953 |
template<typename F, |
960 | 954 |
typename K = typename F::argument_type, |
961 | 955 |
typename V = typename F::result_type> |
962 | 956 |
class FunctorMap : public MapBase<K, V> { |
963 | 957 |
F f; |
964 | 958 |
public: |
965 | 959 |
typedef MapBase<K, V> Parent; |
966 | 960 |
typedef typename Parent::Key Key; |
967 | 961 |
typedef typename Parent::Value Value; |
... | ... |
@@ -996,511 +990,519 @@ |
996 | 990 |
return FunctorMap<V (*)(K), K, V>(f); |
997 | 991 |
} |
998 | 992 |
|
999 | 993 |
|
1000 | 994 |
///Converts a map to an STL style (unary) functor |
1001 | 995 |
|
1002 | 996 |
///This class Converts a map to an STL style (unary) functor. |
1003 | 997 |
///that is it provides an <tt>operator()</tt> to read its values. |
1004 | 998 |
/// |
1005 | 999 |
///For the sake of convenience it also works as |
1006 | 1000 |
///a ususal \c concepts::ReadMap "readable map", |
1007 | 1001 |
///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist. |
1008 | 1002 |
template <typename M> |
1009 | 1003 |
class MapFunctor : public MapBase<typename M::Key, typename M::Value> { |
1010 | 1004 |
const M& m; |
1011 | 1005 |
public: |
1012 | 1006 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
1013 | 1007 |
typedef typename Parent::Key Key; |
1014 | 1008 |
typedef typename Parent::Value Value; |
1015 | 1009 |
|
1016 | 1010 |
typedef typename M::Key argument_type; |
1017 | 1011 |
typedef typename M::Value result_type; |
1018 | 1012 |
|
1019 | 1013 |
///Constructor |
1020 | 1014 |
MapFunctor(const M &_m) : m(_m) {}; |
1021 | 1015 |
///\e |
1022 | 1016 |
Value operator()(Key k) const {return m[k];} |
1023 | 1017 |
///\e |
1024 | 1018 |
Value operator[](Key k) const {return m[k];} |
1025 | 1019 |
}; |
1026 | 1020 |
|
1027 | 1021 |
///Returns a \c MapFunctor class |
1028 | 1022 |
|
1029 | 1023 |
///This function just returns a \c MapFunctor class. |
1030 | 1024 |
///\relates MapFunctor |
1031 | 1025 |
template<typename M> |
1032 | 1026 |
inline MapFunctor<M> mapFunctor(const M &m) { |
1033 | 1027 |
return MapFunctor<M>(m); |
1034 | 1028 |
} |
1035 | 1029 |
|
1036 | 1030 |
///Applies all map setting operations to two maps |
1037 | 1031 |
|
1038 | 1032 |
///This map has two \c concepts::ReadMap "readable map" |
1039 | 1033 |
///parameters and each read request will be passed just to the |
1040 | 1034 |
///first map. This class is the just readable map type of the ForkWriteMap. |
1041 | 1035 |
/// |
1042 | 1036 |
///The \c Key and \c Value will be inherited from \c M1. |
1043 | 1037 |
///The \c Key and \c Value of M2 must be convertible from those of \c M1. |
1038 |
/// |
|
1039 |
/// \todo Why is it needed? |
|
1044 | 1040 |
template<typename M1, typename M2> |
1045 | 1041 |
class ForkMap : public MapBase<typename M1::Key, typename M1::Value> { |
1046 | 1042 |
const M1& m1; |
1047 | 1043 |
const M2& m2; |
1048 | 1044 |
public: |
1049 | 1045 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
1050 | 1046 |
typedef typename Parent::Key Key; |
1051 | 1047 |
typedef typename Parent::Value Value; |
1052 | 1048 |
|
1053 | 1049 |
///Constructor |
1054 | 1050 |
ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {}; |
1055 | 1051 |
/// \e |
1056 | 1052 |
Value operator[](Key k) const {return m1[k];} |
1057 | 1053 |
}; |
1058 | 1054 |
|
1059 | 1055 |
|
1060 | 1056 |
///Applies all map setting operations to two maps |
1061 | 1057 |
|
1062 | 1058 |
///This map has two \c concepts::WriteMap "writable map" |
1063 | 1059 |
///parameters and each write request will be passed to both of them. |
1064 | 1060 |
///If \c M1 is also \c concepts::ReadMap "readable", |
1065 | 1061 |
///then the read operations will return the |
1066 | 1062 |
///corresponding values of \c M1. |
1067 | 1063 |
/// |
1068 | 1064 |
///The \c Key and \c Value will be inherited from \c M1. |
1069 | 1065 |
///The \c Key and \c Value of M2 must be convertible from those of \c M1. |
1070 | 1066 |
template<typename M1, typename M2> |
1071 | 1067 |
class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> { |
1072 | 1068 |
M1& m1; |
1073 | 1069 |
M2& m2; |
1074 | 1070 |
public: |
1075 | 1071 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
1076 | 1072 |
typedef typename Parent::Key Key; |
1077 | 1073 |
typedef typename Parent::Value Value; |
1078 | 1074 |
|
1079 | 1075 |
///Constructor |
1080 | 1076 |
ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {}; |
1081 | 1077 |
///\e |
1082 | 1078 |
Value operator[](Key k) const {return m1[k];} |
1083 | 1079 |
///\e |
1084 | 1080 |
void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);} |
1085 | 1081 |
}; |
1086 | 1082 |
|
1087 | 1083 |
///Returns an \c ForkMap class |
1088 | 1084 |
|
1089 | 1085 |
///This function just returns an \c ForkMap class. |
1090 | 1086 |
/// |
1091 | 1087 |
///\relates ForkMap |
1092 | 1088 |
template <typename M1, typename M2> |
1093 | 1089 |
inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) { |
1094 | 1090 |
return ForkMap<M1, M2>(m1,m2); |
1095 | 1091 |
} |
1096 | 1092 |
|
1097 | 1093 |
template <typename M1, typename M2> |
1098 | 1094 |
inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) { |
1099 | 1095 |
return ForkWriteMap<M1, M2>(m1,m2); |
1100 | 1096 |
} |
1101 | 1097 |
|
1102 | 1098 |
|
1103 | 1099 |
|
1104 | 1100 |
/* ************* BOOL MAPS ******************* */ |
1105 | 1101 |
|
1106 | 1102 |
///Logical 'not' of a map |
1107 | 1103 |
|
1108 | 1104 |
///This bool \c concepts::ReadMap "read only map" returns the |
1109 | 1105 |
///logical negation of |
1110 | 1106 |
///value returned by the |
1111 | 1107 |
///given map. Its \c Key and will be inherited from \c M, |
1112 | 1108 |
///its Value is <tt>bool</tt>. |
1113 | 1109 |
template <typename M> |
1114 | 1110 |
class NotMap : public MapBase<typename M::Key, bool> { |
1115 | 1111 |
const M& m; |
1116 | 1112 |
public: |
1117 | 1113 |
typedef MapBase<typename M::Key, bool> Parent; |
1118 | 1114 |
typedef typename Parent::Key Key; |
1119 | 1115 |
typedef typename Parent::Value Value; |
1120 | 1116 |
|
1121 | 1117 |
/// Constructor |
1122 | 1118 |
NotMap(const M &_m) : m(_m) {}; |
1123 | 1119 |
///\e |
1124 | 1120 |
Value operator[](Key k) const {return !m[k];} |
1125 | 1121 |
}; |
1126 | 1122 |
|
1127 |
///Logical 'not' of a map |
|
1123 |
///Logical 'not' of a map (ReadWrie version) |
|
1128 | 1124 |
|
1129 | 1125 |
///This bool \c concepts::ReadWriteMap "read-write map" returns the |
1130 | 1126 |
///logical negation of value returned by the given map. When it is set, |
1131 | 1127 |
///the opposite value is set to the original map. |
1132 | 1128 |
///Its \c Key and will be inherited from \c M, |
1133 | 1129 |
///its Value is <tt>bool</tt>. |
1134 | 1130 |
template <typename M> |
1135 | 1131 |
class NotWriteMap : public MapBase<typename M::Key, bool> { |
1136 | 1132 |
M& m; |
1137 | 1133 |
public: |
1138 | 1134 |
typedef MapBase<typename M::Key, bool> Parent; |
1139 | 1135 |
typedef typename Parent::Key Key; |
1140 | 1136 |
typedef typename Parent::Value Value; |
1141 | 1137 |
|
1142 | 1138 |
/// Constructor |
1143 | 1139 |
NotWriteMap(M &_m) : m(_m) {}; |
1144 | 1140 |
///\e |
1145 | 1141 |
Value operator[](Key k) const {return !m[k];} |
1146 | 1142 |
///\e |
1147 | 1143 |
void set(Key k, bool v) { m.set(k, !v); } |
1148 | 1144 |
}; |
1149 | 1145 |
|
1150 | 1146 |
///Returns a \c NotMap class |
1151 | 1147 |
|
1152 | 1148 |
///This function just returns a \c NotMap class. |
1153 | 1149 |
///\relates NotMap |
1154 | 1150 |
template <typename M> |
1155 | 1151 |
inline NotMap<M> notMap(const M &m) { |
1156 | 1152 |
return NotMap<M>(m); |
1157 | 1153 |
} |
1158 | 1154 |
|
1159 | 1155 |
template <typename M> |
1160 | 1156 |
inline NotWriteMap<M> notMap(M &m) { |
1161 | 1157 |
return NotWriteMap<M>(m); |
1162 | 1158 |
} |
1163 | 1159 |
|
1164 | 1160 |
namespace _maps_bits { |
1165 | 1161 |
|
1166 | 1162 |
template <typename Value> |
1167 | 1163 |
struct Identity { |
1168 | 1164 |
typedef Value argument_type; |
1169 | 1165 |
typedef Value result_type; |
1170 | 1166 |
Value operator()(const Value& val) const { |
1171 | 1167 |
return val; |
1172 | 1168 |
} |
1173 | 1169 |
}; |
1174 | 1170 |
|
1175 | 1171 |
template <typename _Iterator, typename Enable = void> |
1176 | 1172 |
struct IteratorTraits { |
1177 | 1173 |
typedef typename std::iterator_traits<_Iterator>::value_type Value; |
1178 | 1174 |
}; |
1179 | 1175 |
|
1180 | 1176 |
template <typename _Iterator> |
1181 | 1177 |
struct IteratorTraits<_Iterator, |
1182 | 1178 |
typename exists<typename _Iterator::container_type>::type> |
1183 | 1179 |
{ |
1184 | 1180 |
typedef typename _Iterator::container_type::value_type Value; |
1185 | 1181 |
}; |
1186 | 1182 |
|
1187 | 1183 |
} |
1188 | 1184 |
|
1189 | 1185 |
|
1190 |
/// \brief Writable bool map for |
|
1186 |
/// \brief Writable bool map for logging each true assigned elements |
|
1191 | 1187 |
/// |
1192 |
/// Writable bool map |
|
1188 |
/// Writable bool map for logging each true assigned elements, i.e it |
|
1193 | 1189 |
/// copies all the keys set to true to the given iterator. |
1194 | 1190 |
/// |
1195 | 1191 |
/// \note The container of the iterator should contain space |
1196 | 1192 |
/// for each element. |
1197 | 1193 |
/// |
1198 |
/// The |
|
1194 |
/// The following example shows how you can write the edges found by the Prim |
|
1195 |
/// algorithm directly |
|
1199 | 1196 |
/// to the standard output. |
1200 | 1197 |
///\code |
1201 | 1198 |
/// typedef IdMap<Graph, Edge> EdgeIdMap; |
1202 | 1199 |
/// EdgeIdMap edgeId(graph); |
1203 | 1200 |
/// |
1204 | 1201 |
/// typedef MapFunctor<EdgeIdMap> EdgeIdFunctor; |
1205 | 1202 |
/// EdgeIdFunctor edgeIdFunctor(edgeId); |
1206 | 1203 |
/// |
1207 | 1204 |
/// StoreBoolMap<ostream_iterator<int>, EdgeIdFunctor> |
1208 | 1205 |
/// writerMap(ostream_iterator<int>(cout, " "), edgeIdFunctor); |
1209 | 1206 |
/// |
1210 | 1207 |
/// prim(graph, cost, writerMap); |
1211 | 1208 |
///\endcode |
1209 |
/// |
|
1210 |
///\todo Revise the name of this class and the relates ones. |
|
1212 | 1211 |
template <typename _Iterator, |
1213 | 1212 |
typename _Functor = |
1214 | 1213 |
_maps_bits::Identity<typename _maps_bits:: |
1215 | 1214 |
IteratorTraits<_Iterator>::Value> > |
1216 | 1215 |
class StoreBoolMap { |
1217 | 1216 |
public: |
1218 | 1217 |
typedef _Iterator Iterator; |
1219 | 1218 |
|
1220 | 1219 |
typedef typename _Functor::argument_type Key; |
1221 | 1220 |
typedef bool Value; |
1222 | 1221 |
|
1223 | 1222 |
typedef _Functor Functor; |
1224 | 1223 |
|
1225 | 1224 |
/// Constructor |
1226 | 1225 |
StoreBoolMap(Iterator it, const Functor& functor = Functor()) |
1227 | 1226 |
: _begin(it), _end(it), _functor(functor) {} |
1228 | 1227 |
|
1229 |
/// Gives back the given iterator set for the first |
|
1228 |
/// Gives back the given iterator set for the first key |
|
1230 | 1229 |
Iterator begin() const { |
1231 | 1230 |
return _begin; |
1232 | 1231 |
} |
1233 | 1232 |
|
1234 |
/// Gives back the |
|
1233 |
/// Gives back the the 'after the last' iterator |
|
1235 | 1234 |
Iterator end() const { |
1236 | 1235 |
return _end; |
1237 | 1236 |
} |
1238 | 1237 |
|
1239 | 1238 |
/// Setter function of the map |
1240 | 1239 |
void set(const Key& key, Value value) const { |
1241 | 1240 |
if (value) { |
1242 | 1241 |
*_end++ = _functor(key); |
1243 | 1242 |
} |
1244 | 1243 |
} |
1245 | 1244 |
|
1246 | 1245 |
private: |
1247 | 1246 |
Iterator _begin; |
1248 | 1247 |
mutable Iterator _end; |
1249 | 1248 |
Functor _functor; |
1250 | 1249 |
}; |
1251 | 1250 |
|
1252 |
/// \brief Writable bool map for store each true assigned elements in |
|
1253 |
/// a back insertable container. |
|
1251 |
/// \brief Writable bool map for logging each true assigned elements in |
|
1252 |
/// a back insertable container |
|
1254 | 1253 |
/// |
1255 |
/// Writable bool map for store each true assigned elements in a back |
|
1256 |
/// insertable container. It will push back all the keys set to true into |
|
1257 |
/// the container. It can be used to retrieve the items into a standard |
|
1258 |
/// container. The next example shows how can you store the undirected |
|
1259 |
/// |
|
1254 |
/// Writable bool map for logging each true assigned elements by pushing |
|
1255 |
/// back them into a back insertable container. |
|
1256 |
/// It can be used to retrieve the items into a standard |
|
1257 |
/// container. The next example shows how you can store the |
|
1258 |
/// edges found by the Prim algorithm in a vector. |
|
1260 | 1259 |
/// |
1261 | 1260 |
///\code |
1262 | 1261 |
/// vector<Edge> span_tree_edges; |
1263 | 1262 |
/// BackInserterBoolMap<vector<Edge> > inserter_map(span_tree_edges); |
1264 | 1263 |
/// prim(graph, cost, inserter_map); |
1265 | 1264 |
///\endcode |
1266 | 1265 |
template <typename Container, |
1267 | 1266 |
typename Functor = |
1268 | 1267 |
_maps_bits::Identity<typename Container::value_type> > |
1269 | 1268 |
class BackInserterBoolMap { |
1270 | 1269 |
public: |
1271 | 1270 |
typedef typename Container::value_type Key; |
1272 | 1271 |
typedef bool Value; |
1273 | 1272 |
|
1274 | 1273 |
/// Constructor |
1275 | 1274 |
BackInserterBoolMap(Container& _container, |
1276 | 1275 |
const Functor& _functor = Functor()) |
1277 | 1276 |
: container(_container), functor(_functor) {} |
1278 | 1277 |
|
1279 | 1278 |
/// Setter function of the map |
1280 | 1279 |
void set(const Key& key, Value value) { |
1281 | 1280 |
if (value) { |
1282 | 1281 |
container.push_back(functor(key)); |
1283 | 1282 |
} |
1284 | 1283 |
} |
1285 | 1284 |
|
1286 | 1285 |
private: |
1287 | 1286 |
Container& container; |
1288 | 1287 |
Functor functor; |
1289 | 1288 |
}; |
1290 | 1289 |
|
1291 |
/// \brief Writable bool map for |
|
1290 |
/// \brief Writable bool map for storing each true assignments in |
|
1292 | 1291 |
/// a front insertable container. |
1293 | 1292 |
/// |
1294 |
/// Writable bool map for |
|
1293 |
/// Writable bool map for storing each true assignment in a front |
|
1295 | 1294 |
/// insertable container. It will push front all the keys set to \c true into |
1296 | 1295 |
/// the container. For example see the BackInserterBoolMap. |
1297 | 1296 |
template <typename Container, |
1298 | 1297 |
typename Functor = |
1299 | 1298 |
_maps_bits::Identity<typename Container::value_type> > |
1300 | 1299 |
class FrontInserterBoolMap { |
1301 | 1300 |
public: |
1302 | 1301 |
typedef typename Container::value_type Key; |
1303 | 1302 |
typedef bool Value; |
1304 | 1303 |
|
1305 | 1304 |
/// Constructor |
1306 | 1305 |
FrontInserterBoolMap(Container& _container, |
1307 | 1306 |
const Functor& _functor = Functor()) |
1308 | 1307 |
: container(_container), functor(_functor) {} |
1309 | 1308 |
|
1310 | 1309 |
/// Setter function of the map |
1311 | 1310 |
void set(const Key& key, Value value) { |
1312 | 1311 |
if (value) { |
1313 | 1312 |
container.push_front(key); |
1314 | 1313 |
} |
1315 | 1314 |
} |
1316 | 1315 |
|
1317 | 1316 |
private: |
1318 | 1317 |
Container& container; |
1319 | 1318 |
Functor functor; |
1320 | 1319 |
}; |
1321 | 1320 |
|
1322 |
/// \brief Writable bool map for |
|
1321 |
/// \brief Writable bool map for storing each true assigned elements in |
|
1323 | 1322 |
/// an insertable container. |
1324 | 1323 |
/// |
1325 |
/// Writable bool map for |
|
1324 |
/// Writable bool map for storing each true assigned elements in an |
|
1326 | 1325 |
/// insertable container. It will insert all the keys set to \c true into |
1327 |
/// the container. |
|
1326 |
/// the container. |
|
1327 |
/// |
|
1328 |
/// For example, if you want to store the cut arcs of the strongly |
|
1328 | 1329 |
/// connected components in a set you can use the next code: |
1329 | 1330 |
/// |
1330 | 1331 |
///\code |
1331 | 1332 |
/// set<Arc> cut_arcs; |
1332 | 1333 |
/// InserterBoolMap<set<Arc> > inserter_map(cut_arcs); |
1333 | 1334 |
/// stronglyConnectedCutArcs(digraph, cost, inserter_map); |
1334 | 1335 |
///\endcode |
1335 | 1336 |
template <typename Container, |
1336 | 1337 |
typename Functor = |
1337 | 1338 |
_maps_bits::Identity<typename Container::value_type> > |
1338 | 1339 |
class InserterBoolMap { |
1339 | 1340 |
public: |
1340 | 1341 |
typedef typename Container::value_type Key; |
1341 | 1342 |
typedef bool Value; |
1342 | 1343 |
|
1343 | 1344 |
/// Constructor |
1344 | 1345 |
InserterBoolMap(Container& _container, typename Container::iterator _it, |
1345 | 1346 |
const Functor& _functor = Functor()) |
1346 | 1347 |
: container(_container), it(_it), functor(_functor) {} |
1347 | 1348 |
|
1348 | 1349 |
/// Constructor |
1349 | 1350 |
InserterBoolMap(Container& _container, const Functor& _functor = Functor()) |
1350 | 1351 |
: container(_container), it(_container.end()), functor(_functor) {} |
1351 | 1352 |
|
1352 | 1353 |
/// Setter function of the map |
1353 | 1354 |
void set(const Key& key, Value value) { |
1354 | 1355 |
if (value) { |
1355 | 1356 |
it = container.insert(it, key); |
1356 | 1357 |
++it; |
1357 | 1358 |
} |
1358 | 1359 |
} |
1359 | 1360 |
|
1360 | 1361 |
private: |
1361 | 1362 |
Container& container; |
1362 | 1363 |
typename Container::iterator it; |
1363 | 1364 |
Functor functor; |
1364 | 1365 |
}; |
1365 | 1366 |
|
1366 | 1367 |
/// \brief Fill the true set elements with a given value. |
1367 | 1368 |
/// |
1368 | 1369 |
/// Writable bool map to fill the elements set to \c true with a given value. |
1369 | 1370 |
/// The value can set |
1370 | 1371 |
/// the container. |
1371 | 1372 |
/// |
1372 |
/// The |
|
1373 |
/// The following code finds the connected components of a graph |
|
1373 | 1374 |
/// and stores it in the \c comp map: |
1374 | 1375 |
///\code |
1375 | 1376 |
/// typedef Graph::NodeMap<int> ComponentMap; |
1376 | 1377 |
/// ComponentMap comp(graph); |
1377 | 1378 |
/// typedef FillBoolMap<Graph::NodeMap<int> > ComponentFillerMap; |
1378 | 1379 |
/// ComponentFillerMap filler(comp, 0); |
1379 | 1380 |
/// |
1380 | 1381 |
/// Dfs<Graph>::DefProcessedMap<ComponentFillerMap>::Create dfs(graph); |
1381 | 1382 |
/// dfs.processedMap(filler); |
1382 | 1383 |
/// dfs.init(); |
1383 | 1384 |
/// for (NodeIt it(graph); it != INVALID; ++it) { |
1384 | 1385 |
/// if (!dfs.reached(it)) { |
1385 | 1386 |
/// dfs.addSource(it); |
1386 | 1387 |
/// dfs.start(); |
1387 | 1388 |
/// ++filler.fillValue(); |
1388 | 1389 |
/// } |
1389 | 1390 |
/// } |
1390 | 1391 |
///\endcode |
1391 | 1392 |
template <typename Map> |
1392 | 1393 |
class FillBoolMap { |
1393 | 1394 |
public: |
1394 | 1395 |
typedef typename Map::Key Key; |
1395 | 1396 |
typedef bool Value; |
1396 | 1397 |
|
1397 | 1398 |
/// Constructor |
1398 | 1399 |
FillBoolMap(Map& _map, const typename Map::Value& _fill) |
1399 | 1400 |
: map(_map), fill(_fill) {} |
1400 | 1401 |
|
1401 | 1402 |
/// Constructor |
1402 | 1403 |
FillBoolMap(Map& _map) |
1403 | 1404 |
: map(_map), fill() {} |
1404 | 1405 |
|
1405 | 1406 |
/// Gives back the current fill value |
1406 | 1407 |
const typename Map::Value& fillValue() const { |
1407 | 1408 |
return fill; |
1408 | 1409 |
} |
1409 | 1410 |
|
1410 | 1411 |
/// Gives back the current fill value |
1411 | 1412 |
typename Map::Value& fillValue() { |
1412 | 1413 |
return fill; |
1413 | 1414 |
} |
1414 | 1415 |
|
1415 | 1416 |
/// Sets the current fill value |
1416 | 1417 |
void fillValue(const typename Map::Value& _fill) { |
1417 | 1418 |
fill = _fill; |
1418 | 1419 |
} |
1419 | 1420 |
|
1420 |
/// |
|
1421 |
/// Set function of the map |
|
1421 | 1422 |
void set(const Key& key, Value value) { |
1422 | 1423 |
if (value) { |
1423 | 1424 |
map.set(key, fill); |
1424 | 1425 |
} |
1425 | 1426 |
} |
1426 | 1427 |
|
1427 | 1428 |
private: |
1428 | 1429 |
Map& map; |
1429 | 1430 |
typename Map::Value fill; |
1430 | 1431 |
}; |
1431 | 1432 |
|
1432 | 1433 |
|
1433 |
/// \brief Writable bool map which stores for each true assigned elements |
|
1434 |
/// the setting order number. |
|
1435 |
/// |
|
1434 |
/// \brief Writable bool map which stores the sequence number of |
|
1435 |
/// true assignments. |
|
1436 |
/// |
|
1436 | 1437 |
/// Writable bool map which stores for each true assigned elements |
1437 |
/// the |
|
1438 |
/// the sequence number of this setting. |
|
1439 |
/// It makes it easy to calculate the leaving |
|
1438 | 1440 |
/// order of the nodes in the \c Dfs algorithm. |
1439 | 1441 |
/// |
1440 | 1442 |
///\code |
1441 | 1443 |
/// typedef Digraph::NodeMap<int> OrderMap; |
1442 | 1444 |
/// OrderMap order(digraph); |
1443 | 1445 |
/// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap; |
1444 | 1446 |
/// OrderSetterMap setter(order); |
1445 | 1447 |
/// Dfs<Digraph>::DefProcessedMap<OrderSetterMap>::Create dfs(digraph); |
1446 | 1448 |
/// dfs.processedMap(setter); |
1447 | 1449 |
/// dfs.init(); |
1448 | 1450 |
/// for (NodeIt it(digraph); it != INVALID; ++it) { |
1449 | 1451 |
/// if (!dfs.reached(it)) { |
1450 | 1452 |
/// dfs.addSource(it); |
1451 | 1453 |
/// dfs.start(); |
1452 | 1454 |
/// } |
1453 | 1455 |
/// } |
1454 | 1456 |
///\endcode |
1455 | 1457 |
/// |
1456 |
/// The discovering order |
|
1458 |
/// The storing of the discovering order is more difficult because the |
|
1457 | 1459 |
/// ReachedMap should be readable in the dfs algorithm but the setting |
1458 |
/// order map is not readable. |
|
1460 |
/// order map is not readable. Thus we must use the fork map: |
|
1459 | 1461 |
/// |
1460 | 1462 |
///\code |
1461 | 1463 |
/// typedef Digraph::NodeMap<int> OrderMap; |
1462 | 1464 |
/// OrderMap order(digraph); |
1463 | 1465 |
/// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap; |
1464 | 1466 |
/// OrderSetterMap setter(order); |
1465 | 1467 |
/// typedef Digraph::NodeMap<bool> StoreMap; |
1466 | 1468 |
/// StoreMap store(digraph); |
1467 | 1469 |
/// |
1468 | 1470 |
/// typedef ForkWriteMap<StoreMap, OrderSetterMap> ReachedMap; |
1469 | 1471 |
/// ReachedMap reached(store, setter); |
1470 | 1472 |
/// |
1471 | 1473 |
/// Dfs<Digraph>::DefReachedMap<ReachedMap>::Create dfs(digraph); |
1472 | 1474 |
/// dfs.reachedMap(reached); |
1473 | 1475 |
/// dfs.init(); |
1474 | 1476 |
/// for (NodeIt it(digraph); it != INVALID; ++it) { |
1475 | 1477 |
/// if (!dfs.reached(it)) { |
1476 | 1478 |
/// dfs.addSource(it); |
1477 | 1479 |
/// dfs.start(); |
1478 | 1480 |
/// } |
1479 | 1481 |
/// } |
1480 | 1482 |
///\endcode |
1481 | 1483 |
template <typename Map> |
1482 | 1484 |
class SettingOrderBoolMap { |
1483 | 1485 |
public: |
1484 | 1486 |
typedef typename Map::Key Key; |
1485 | 1487 |
typedef bool Value; |
1486 | 1488 |
|
1487 | 1489 |
/// Constructor |
1488 | 1490 |
SettingOrderBoolMap(Map& _map) |
1489 | 1491 |
: map(_map), counter(0) {} |
1490 | 1492 |
|
1491 | 1493 |
/// Number of set operations. |
1492 | 1494 |
int num() const { |
1493 | 1495 |
return counter; |
1494 | 1496 |
} |
1495 | 1497 |
|
1496 | 1498 |
/// Setter function of the map |
1497 | 1499 |
void set(const Key& key, Value value) { |
1498 | 1500 |
if (value) { |
1499 | 1501 |
map.set(key, counter++); |
1500 | 1502 |
} |
1501 | 1503 |
} |
1502 | 1504 |
|
1503 | 1505 |
private: |
1504 | 1506 |
Map& map; |
1505 | 1507 |
int counter; |
1506 | 1508 |
}; |
0 comments (0 inline)