gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge
0 3 0
merge default
1 file changed with 494 insertions and 12 deletions:
↑ Collapse diff ↑
Ignore white space 128 line context
... ...
@@ -3703,68 +3703,355 @@
3703 3703
      }
3704 3704
    }
3705 3705

	
3706 3706
    virtual void clear() {
3707 3707
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
3708 3708
        _deg[it] = 0;
3709 3709
      }
3710 3710
    }
3711 3711
  private:
3712 3712

	
3713 3713
    const Digraph& _digraph;
3714 3714
    AutoNodeMap _deg;
3715 3715
  };
3716 3716

	
3717 3717
  /// \brief Potential difference map
3718 3718
  ///
3719 3719
  /// PotentialDifferenceMap returns the difference between the potentials of
3720 3720
  /// the source and target nodes of each arc in a digraph, i.e. it returns
3721 3721
  /// \code
3722 3722
  ///   potential[gr.target(arc)] - potential[gr.source(arc)].
3723 3723
  /// \endcode
3724 3724
  /// \tparam GR The digraph type.
3725 3725
  /// \tparam POT A node map storing the potentials.
3726 3726
  template <typename GR, typename POT>
3727 3727
  class PotentialDifferenceMap {
3728 3728
  public:
3729 3729
    /// Key type
3730 3730
    typedef typename GR::Arc Key;
3731 3731
    /// Value type
3732 3732
    typedef typename POT::Value Value;
3733 3733

	
3734 3734
    /// \brief Constructor
3735 3735
    ///
3736 3736
    /// Contructor of the map.
3737 3737
    explicit PotentialDifferenceMap(const GR& gr,
3738 3738
                                    const POT& potential)
3739 3739
      : _digraph(gr), _potential(potential) {}
3740 3740

	
3741 3741
    /// \brief Returns the potential difference for the given arc.
3742 3742
    ///
3743 3743
    /// Returns the potential difference for the given arc, i.e.
3744 3744
    /// \code
3745 3745
    ///   potential[gr.target(arc)] - potential[gr.source(arc)].
3746 3746
    /// \endcode
3747 3747
    Value operator[](const Key& arc) const {
3748 3748
      return _potential[_digraph.target(arc)] -
3749 3749
        _potential[_digraph.source(arc)];
3750 3750
    }
3751 3751

	
3752 3752
  private:
3753 3753
    const GR& _digraph;
3754 3754
    const POT& _potential;
3755 3755
  };
3756 3756

	
3757 3757
  /// \brief Returns a PotentialDifferenceMap.
3758 3758
  ///
3759 3759
  /// This function just returns a PotentialDifferenceMap.
3760 3760
  /// \relates PotentialDifferenceMap
3761 3761
  template <typename GR, typename POT>
3762 3762
  PotentialDifferenceMap<GR, POT>
3763 3763
  potentialDifferenceMap(const GR& gr, const POT& potential) {
3764 3764
    return PotentialDifferenceMap<GR, POT>(gr, potential);
3765 3765
  }
3766 3766

	
3767

	
3768
  /// \brief Copy the values of a graph map to another map.
3769
  ///
3770
  /// This function copies the values of a graph map to another graph map.
3771
  /// \c To::Key must be equal or convertible to \c From::Key and
3772
  /// \c From::Value must be equal or convertible to \c To::Value.
3773
  ///
3774
  /// For example, an edge map of \c int value type can be copied to
3775
  /// an arc map of \c double value type in an undirected graph, but
3776
  /// an arc map cannot be copied to an edge map.
3777
  /// Note that even a \ref ConstMap can be copied to a standard graph map,
3778
  /// but \ref mapFill() can also be used for this purpose.
3779
  ///
3780
  /// \param gr The graph for which the maps are defined.
3781
  /// \param from The map from which the values have to be copied.
3782
  /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
3783
  /// \param to The map to which the values have to be copied.
3784
  /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
3785
  template <typename GR, typename From, typename To>
3786
  void mapCopy(const GR& gr, const From& from, To& to) {
3787
    typedef typename To::Key Item;
3788
    typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
3789
    
3790
    for (ItemIt it(gr); it != INVALID; ++it) {
3791
      to.set(it, from[it]);
3792
    }
3793
  }
3794

	
3795
  /// \brief Compare two graph maps.
3796
  ///
3797
  /// This function compares the values of two graph maps. It returns 
3798
  /// \c true if the maps assign the same value for all items in the graph.
3799
  /// The \c Key type of the maps (\c Node, \c Arc or \c Edge) must be equal
3800
  /// and their \c Value types must be comparable using \c %operator==().
3801
  ///
3802
  /// \param gr The graph for which the maps are defined.
3803
  /// \param map1 The first map.
3804
  /// \param map2 The second map.
3805
  template <typename GR, typename Map1, typename Map2>
3806
  bool mapCompare(const GR& gr, const Map1& map1, const Map2& map2) {
3807
    typedef typename Map2::Key Item;
3808
    typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
3809
    
3810
    for (ItemIt it(gr); it != INVALID; ++it) {
3811
      if (!(map1[it] == map2[it])) return false;
3812
    }
3813
    return true;
3814
  }
3815

	
3816
  /// \brief Return an item having minimum value of a graph map.
3817
  ///
3818
  /// This function returns an item (\c Node, \c Arc or \c Edge) having
3819
  /// minimum value of the given graph map.
3820
  /// If the item set is empty, it returns \c INVALID.
3821
  ///
3822
  /// \param gr The graph for which the map is defined.
3823
  /// \param map The graph map.
3824
  template <typename GR, typename Map>
3825
  typename Map::Key mapMin(const GR& gr, const Map& map) {
3826
    return mapMin(gr, map, std::less<typename Map::Value>());
3827
  }
3828

	
3829
  /// \brief Return an item having minimum value of a graph map.
3830
  ///
3831
  /// This function returns an item (\c Node, \c Arc or \c Edge) having
3832
  /// minimum value of the given graph map.
3833
  /// If the item set is empty, it returns \c INVALID.
3834
  ///
3835
  /// \param gr The graph for which the map is defined.
3836
  /// \param map The graph map.
3837
  /// \param comp Comparison function object.
3838
  template <typename GR, typename Map, typename Comp>
3839
  typename Map::Key mapMin(const GR& gr, const Map& map, const Comp& comp) {
3840
    typedef typename Map::Key Item;
3841
    typedef typename Map::Value Value;
3842
    typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
3843

	
3844
    ItemIt min_item(gr);
3845
    if (min_item == INVALID) return INVALID;
3846
    Value min = map[min_item];
3847
    for (ItemIt it(gr); it != INVALID; ++it) {
3848
      if (comp(map[it], min)) {
3849
        min = map[it];
3850
        min_item = it;
3851
      }
3852
    }
3853
    return min_item;
3854
  }
3855

	
3856
  /// \brief Return an item having maximum value of a graph map.
3857
  ///
3858
  /// This function returns an item (\c Node, \c Arc or \c Edge) having
3859
  /// maximum value of the given graph map.
3860
  /// If the item set is empty, it returns \c INVALID.
3861
  ///
3862
  /// \param gr The graph for which the map is defined.
3863
  /// \param map The graph map.
3864
  template <typename GR, typename Map>
3865
  typename Map::Key mapMax(const GR& gr, const Map& map) {
3866
    return mapMax(gr, map, std::less<typename Map::Value>());
3867
  }
3868

	
3869
  /// \brief Return an item having maximum value of a graph map.
3870
  ///
3871
  /// This function returns an item (\c Node, \c Arc or \c Edge) having
3872
  /// maximum value of the given graph map.
3873
  /// If the item set is empty, it returns \c INVALID.
3874
  ///
3875
  /// \param gr The graph for which the map is defined.
3876
  /// \param map The graph map.
3877
  /// \param comp Comparison function object.
3878
  template <typename GR, typename Map, typename Comp>
3879
  typename Map::Key mapMax(const GR& gr, const Map& map, const Comp& comp) {
3880
    typedef typename Map::Key Item;
3881
    typedef typename Map::Value Value;
3882
    typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
3883

	
3884
    ItemIt max_item(gr);
3885
    if (max_item == INVALID) return INVALID;
3886
    Value max = map[max_item];
3887
    for (ItemIt it(gr); it != INVALID; ++it) {
3888
      if (comp(max, map[it])) {
3889
        max = map[it];
3890
        max_item = it;
3891
      }
3892
    }
3893
    return max_item;
3894
  }
3895

	
3896
  /// \brief Return the minimum value of a graph map.
3897
  ///
3898
  /// This function returns the minimum value of the given graph map.
3899
  /// The corresponding item set of the graph must not be empty.
3900
  ///
3901
  /// \param gr The graph for which the map is defined.
3902
  /// \param map The graph map.
3903
  template <typename GR, typename Map>
3904
  typename Map::Value mapMinValue(const GR& gr, const Map& map) {
3905
    return map[mapMin(gr, map, std::less<typename Map::Value>())];
3906
  }
3907

	
3908
  /// \brief Return the minimum value of a graph map.
3909
  ///
3910
  /// This function returns the minimum value of the given graph map.
3911
  /// The corresponding item set of the graph must not be empty.
3912
  ///
3913
  /// \param gr The graph for which the map is defined.
3914
  /// \param map The graph map.
3915
  /// \param comp Comparison function object.
3916
  template <typename GR, typename Map, typename Comp>
3917
  typename Map::Value
3918
  mapMinValue(const GR& gr, const Map& map, const Comp& comp) {
3919
    return map[mapMin(gr, map, comp)];
3920
  }
3921

	
3922
  /// \brief Return the maximum value of a graph map.
3923
  ///
3924
  /// This function returns the maximum value of the given graph map.
3925
  /// The corresponding item set of the graph must not be empty.
3926
  ///
3927
  /// \param gr The graph for which the map is defined.
3928
  /// \param map The graph map.
3929
  template <typename GR, typename Map>
3930
  typename Map::Value mapMaxValue(const GR& gr, const Map& map) {
3931
    return map[mapMax(gr, map, std::less<typename Map::Value>())];
3932
  }
3933

	
3934
  /// \brief Return the maximum value of a graph map.
3935
  ///
3936
  /// This function returns the maximum value of the given graph map.
3937
  /// The corresponding item set of the graph must not be empty.
3938
  ///
3939
  /// \param gr The graph for which the map is defined.
3940
  /// \param map The graph map.
3941
  /// \param comp Comparison function object.
3942
  template <typename GR, typename Map, typename Comp>
3943
  typename Map::Value
3944
  mapMaxValue(const GR& gr, const Map& map, const Comp& comp) {
3945
    return map[mapMax(gr, map, comp)];
3946
  }
3947

	
3948
  /// \brief Return an item having a specified value in a graph map.
3949
  ///
3950
  /// This function returns an item (\c Node, \c Arc or \c Edge) having
3951
  /// the specified assigned value in the given graph map.
3952
  /// If no such item exists, it returns \c INVALID.
3953
  ///
3954
  /// \param gr The graph for which the map is defined.
3955
  /// \param map The graph map.
3956
  /// \param val The value that have to be found.
3957
  template <typename GR, typename Map>
3958
  typename Map::Key
3959
  mapFind(const GR& gr, const Map& map, const typename Map::Value& val) {
3960
    typedef typename Map::Key Item;
3961
    typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
3962

	
3963
    for (ItemIt it(gr); it != INVALID; ++it) {
3964
      if (map[it] == val) return it;
3965
    }
3966
    return INVALID;
3967
  }
3968

	
3969
  /// \brief Return an item having value for which a certain predicate is
3970
  /// true in a graph map.
3971
  ///
3972
  /// This function returns an item (\c Node, \c Arc or \c Edge) having
3973
  /// such assigned value for which the specified predicate is true
3974
  /// in the given graph map.
3975
  /// If no such item exists, it returns \c INVALID.
3976
  ///
3977
  /// \param gr The graph for which the map is defined.
3978
  /// \param map The graph map.
3979
  /// \param pred The predicate function object.
3980
  template <typename GR, typename Map, typename Pred>
3981
  typename Map::Key
3982
  mapFindIf(const GR& gr, const Map& map, const Pred& pred) {
3983
    typedef typename Map::Key Item;
3984
    typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
3985

	
3986
    for (ItemIt it(gr); it != INVALID; ++it) {
3987
      if (pred(map[it])) return it;
3988
    }
3989
    return INVALID;
3990
  }
3991

	
3992
  /// \brief Return the number of items having a specified value in a
3993
  /// graph map.
3994
  ///
3995
  /// This function returns the number of items (\c Node, \c Arc or \c Edge)
3996
  /// having the specified assigned value in the given graph map.
3997
  ///
3998
  /// \param gr The graph for which the map is defined.
3999
  /// \param map The graph map.
4000
  /// \param val The value that have to be counted.
4001
  template <typename GR, typename Map>
4002
  int mapCount(const GR& gr, const Map& map, const typename Map::Value& val) {
4003
    typedef typename Map::Key Item;
4004
    typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
4005

	
4006
    int cnt = 0;
4007
    for (ItemIt it(gr); it != INVALID; ++it) {
4008
      if (map[it] == val) ++cnt;
4009
    }
4010
    return cnt;
4011
  }
4012

	
4013
  /// \brief Return the number of items having values for which a certain
4014
  /// predicate is true in a graph map.
4015
  ///
4016
  /// This function returns the number of items (\c Node, \c Arc or \c Edge)
4017
  /// having such assigned values for which the specified predicate is true
4018
  /// in the given graph map.
4019
  ///
4020
  /// \param gr The graph for which the map is defined.
4021
  /// \param map The graph map.
4022
  /// \param pred The predicate function object.
4023
  template <typename GR, typename Map, typename Pred>
4024
  int mapCountIf(const GR& gr, const Map& map, const Pred& pred) {
4025
    typedef typename Map::Key Item;
4026
    typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
4027

	
4028
    int cnt = 0;
4029
    for (ItemIt it(gr); it != INVALID; ++it) {
4030
      if (pred(map[it])) ++cnt;
4031
    }
4032
    return cnt;
4033
  }
4034

	
4035
  /// \brief Fill a graph map with a certain value.
4036
  ///
4037
  /// This function sets the specified value for all items (\c Node,
4038
  /// \c Arc or \c Edge) in the given graph map.
4039
  ///
4040
  /// \param gr The graph for which the map is defined.
4041
  /// \param map The graph map. It must conform to the
4042
  /// \ref concepts::WriteMap "WriteMap" concept.
4043
  /// \param val The value.
4044
  template <typename GR, typename Map>
4045
  void mapFill(const GR& gr, Map& map, const typename Map::Value& val) {
4046
    typedef typename Map::Key Item;
4047
    typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
4048

	
4049
    for (ItemIt it(gr); it != INVALID; ++it) {
4050
      map.set(it, val);
4051
    }
4052
  }
4053

	
3767 4054
  /// @}
3768 4055
}
3769 4056

	
3770 4057
#endif // LEMON_MAPS_H
Ignore white space 128 line context
... ...
@@ -4,129 +4,129 @@
4 4
 *
5 5
 * Copyright (C) 2003-2009
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 19
#include <lemon/concepts/digraph.h>
20 20
#include <lemon/smart_graph.h>
21 21
#include <lemon/list_graph.h>
22 22
#include <lemon/lgf_reader.h>
23 23
#include <lemon/bellman_ford.h>
24 24
#include <lemon/path.h>
25 25

	
26 26
#include "graph_test.h"
27 27
#include "test_tools.h"
28 28

	
29 29
using namespace lemon;
30 30

	
31 31
char test_lgf[] =
32 32
  "@nodes\n"
33 33
  "label\n"
34 34
  "0\n"
35 35
  "1\n"
36 36
  "2\n"
37 37
  "3\n"
38 38
  "4\n"
39 39
  "@arcs\n"
40 40
  "    length\n"
41 41
  "0 1 3\n"
42 42
  "1 2 -3\n"
43 43
  "1 2 -5\n"
44 44
  "1 3 -2\n"
45 45
  "0 2 -1\n"
46 46
  "1 2 -4\n"
47 47
  "0 3 2\n"
48 48
  "4 2 -5\n"
49 49
  "2 3 1\n"
50 50
  "@attributes\n"
51 51
  "source 0\n"
52 52
  "target 3\n";
53 53

	
54 54

	
55 55
void checkBellmanFordCompile()
56 56
{
57 57
  typedef int Value;
58 58
  typedef concepts::Digraph Digraph;
59 59
  typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap;
60 60
  typedef BellmanFord<Digraph, LengthMap> BF;
61 61
  typedef Digraph::Node Node;
62 62
  typedef Digraph::Arc Arc;
63 63

	
64 64
  Digraph gr;
65 65
  Node s, t, n;
66 66
  Arc e;
67 67
  Value l;
68
  int k;
68
  int k=3;
69 69
  bool b;
70 70
  BF::DistMap d(gr);
71 71
  BF::PredMap p(gr);
72 72
  LengthMap length;
73 73
  concepts::Path<Digraph> pp;
74 74

	
75 75
  {
76 76
    BF bf_test(gr,length);
77 77
    const BF& const_bf_test = bf_test;
78 78

	
79 79
    bf_test.run(s);
80 80
    bf_test.run(s,k);
81 81

	
82 82
    bf_test.init();
83 83
    bf_test.addSource(s);
84 84
    bf_test.addSource(s, 1);
85 85
    b = bf_test.processNextRound();
86 86
    b = bf_test.processNextWeakRound();
87 87

	
88 88
    bf_test.start();
89 89
    bf_test.checkedStart();
90 90
    bf_test.limitedStart(k);
91 91

	
92 92
    l  = const_bf_test.dist(t);
93 93
    e  = const_bf_test.predArc(t);
94 94
    s  = const_bf_test.predNode(t);
95 95
    b  = const_bf_test.reached(t);
96 96
    d  = const_bf_test.distMap();
97 97
    p  = const_bf_test.predMap();
98 98
    pp = const_bf_test.path(t);
99 99
    pp = const_bf_test.negativeCycle();
100 100
    
101 101
    for (BF::ActiveIt it(const_bf_test); it != INVALID; ++it) {}
102 102
  }
103 103
  {
104 104
    BF::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
105 105
      ::SetDistMap<concepts::ReadWriteMap<Node,Value> >
106 106
      ::SetOperationTraits<BellmanFordDefaultOperationTraits<Value> >
107 107
      ::Create bf_test(gr,length);
108 108

	
109 109
    LengthMap length_map;
110 110
    concepts::ReadWriteMap<Node,Arc> pred_map;
111 111
    concepts::ReadWriteMap<Node,Value> dist_map;
112 112
    
113 113
    bf_test
114 114
      .lengthMap(length_map)
115 115
      .predMap(pred_map)
116 116
      .distMap(dist_map);
117 117

	
118 118
    bf_test.run(s);
119 119
    bf_test.run(s,k);
120 120

	
121 121
    bf_test.init();
122 122
    bf_test.addSource(s);
123 123
    bf_test.addSource(s, 1);
124 124
    b = bf_test.processNextRound();
125 125
    b = bf_test.processNextWeakRound();
126 126

	
127 127
    bf_test.start();
128 128
    bf_test.checkedStart();
129 129
    bf_test.limitedStart(k);
130 130

	
131 131
    l  = bf_test.dist(t);
132 132
    e  = bf_test.predArc(t);
Ignore white space 128 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2009
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 19
#include <deque>
20 20
#include <set>
21 21

	
22 22
#include <lemon/concept_check.h>
23 23
#include <lemon/concepts/maps.h>
24 24
#include <lemon/maps.h>
25 25
#include <lemon/list_graph.h>
26 26
#include <lemon/smart_graph.h>
27 27
#include <lemon/adaptors.h>
28 28
#include <lemon/dfs.h>
29
#include <algorithm>
29 30

	
30 31
#include "test_tools.h"
31 32

	
32 33
using namespace lemon;
33 34
using namespace lemon::concepts;
34 35

	
35 36
struct A {};
36 37
inline bool operator<(A, A) { return true; }
37 38
struct B {};
38 39

	
39 40
class C {
40
  int x;
41
  int _x;
41 42
public:
42
  C(int _x) : x(_x) {}
43
  C(int x) : _x(x) {}
44
  int get() const { return _x; }
45
};
46
inline bool operator<(C c1, C c2) { return c1.get() < c2.get(); }
47
inline bool operator==(C c1, C c2) { return c1.get() == c2.get(); }
48

	
49
C createC(int x) { return C(x); }
50

	
51
template <typename T>
52
class Less {
53
  T _t;
54
public:
55
  Less(T t): _t(t) {}
56
  bool operator()(const T& t) const { return t < _t; }
43 57
};
44 58

	
45 59
class F {
46 60
public:
47 61
  typedef A argument_type;
48 62
  typedef B result_type;
49 63

	
50 64
  B operator()(const A&) const { return B(); }
51 65
private:
52 66
  F& operator=(const F&);
53 67
};
54 68

	
55 69
int func(A) { return 3; }
56 70

	
57 71
int binc(int a, B) { return a+1; }
58 72

	
73
template <typename T>
74
class Sum {
75
  T& _sum;
76
public:
77
  Sum(T& sum) : _sum(sum) {}
78
  void operator()(const T& t) { _sum += t; }
79
};
80

	
59 81
typedef ReadMap<A, double> DoubleMap;
60 82
typedef ReadWriteMap<A, double> DoubleWriteMap;
61 83
typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
62 84

	
63 85
typedef ReadMap<A, bool> BoolMap;
64 86
typedef ReadWriteMap<A, bool> BoolWriteMap;
65 87
typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
66 88

	
67
template<typename Map1, typename Map2, typename ItemIt>
68
void compareMap(const Map1& map1, const Map2& map2, ItemIt it) {
69
  for (; it != INVALID; ++it)
70
    check(map1[it] == map2[it], "The maps are not equal");
71
}
72

	
73 89
int main()
74 90
{
75 91
  // Map concepts
76 92
  checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
77 93
  checkConcept<ReadMap<A,C>, ReadMap<A,C> >();
78 94
  checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
79 95
  checkConcept<WriteMap<A,C>, WriteMap<A,C> >();
80 96
  checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
81 97
  checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >();
82 98
  checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
83 99
  checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >();
84 100

	
85 101
  // NullMap
86 102
  {
87 103
    checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
88 104
    NullMap<A,B> map1;
89 105
    NullMap<A,B> map2 = map1;
90 106
    map1 = nullMap<A,B>();
91 107
  }
92 108

	
93 109
  // ConstMap
94 110
  {
95 111
    checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
96 112
    checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >();
97 113
    ConstMap<A,B> map1;
98 114
    ConstMap<A,B> map2 = B();
99 115
    ConstMap<A,B> map3 = map1;
100 116
    map1 = constMap<A>(B());
101 117
    map1 = constMap<A,B>();
102 118
    map1.setAll(B());
103 119
    ConstMap<A,C> map4(C(1));
104 120
    ConstMap<A,C> map5 = map4;
105 121
    map4 = constMap<A>(C(2));
106 122
    map4.setAll(C(3));
107 123

	
108 124
    checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
109 125
    check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
110 126

	
111 127
    checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
112 128
    ConstMap<A,Const<int,10> > map6;
113 129
    ConstMap<A,Const<int,10> > map7 = map6;
114 130
    map6 = constMap<A,int,10>();
115 131
    map7 = constMap<A,Const<int,10> >();
116 132
    check(map6[A()] == 10 && map7[A()] == 10,
117 133
          "Something is wrong with ConstMap");
118 134
  }
119 135

	
120 136
  // IdentityMap
121 137
  {
122 138
    checkConcept<ReadMap<A,A>, IdentityMap<A> >();
123 139
    IdentityMap<A> map1;
124 140
    IdentityMap<A> map2 = map1;
125 141
    map1 = identityMap<A>();
126 142

	
127 143
    checkConcept<ReadMap<double,double>, IdentityMap<double> >();
128 144
    check(identityMap<double>()[1.0] == 1.0 &&
129 145
          identityMap<double>()[3.14] == 3.14,
130 146
          "Something is wrong with IdentityMap");
131 147
  }
132 148

	
133 149
  // RangeMap
134 150
  {
135 151
    checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
136 152
    RangeMap<B> map1;
... ...
@@ -433,131 +449,132 @@
433 449
    check(nrmap.size() == 3 && armap.size() == 4,
434 450
          "Wrong RangeIdMap::size()");
435 451

	
436 452
    check(nrmap[n0] == 0 && nrmap(0) == n0, "Wrong RangeIdMap");
437 453
    check(nrmap[n1] == 1 && nrmap(1) == n1, "Wrong RangeIdMap");
438 454
    check(nrmap[n2] == 2 && nrmap(2) == n2, "Wrong RangeIdMap");
439 455
    
440 456
    check(armap[a0] == 0 && armap(0) == a0, "Wrong RangeIdMap");
441 457
    check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
442 458
    check(armap[a2] == 2 && armap(2) == a2, "Wrong RangeIdMap");
443 459
    check(armap[a3] == 3 && armap(3) == a3, "Wrong RangeIdMap");
444 460

	
445 461
    check(nrmap.inverse()[0] == n0, "Wrong RangeIdMap::InverseMap");
446 462
    check(armap.inverse()[0] == a0, "Wrong RangeIdMap::InverseMap");
447 463
    
448 464
    gr.erase(n1);
449 465
    
450 466
    if (nrmap[n0] == 1) nrmap.swap(n0, n2);
451 467
    nrmap.swap(n2, n0);
452 468
    if (armap[a1] == 1) armap.swap(a1, a3);
453 469
    armap.swap(a3, a1);
454 470
    
455 471
    check(nrmap.size() == 2 && armap.size() == 2,
456 472
          "Wrong RangeIdMap::size()");
457 473

	
458 474
    check(nrmap[n0] == 1 && nrmap(1) == n0, "Wrong RangeIdMap");
459 475
    check(nrmap[n2] == 0 && nrmap(0) == n2, "Wrong RangeIdMap");
460 476
    
461 477
    check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
462 478
    check(armap[a3] == 0 && armap(0) == a3, "Wrong RangeIdMap");
463 479

	
464 480
    check(nrmap.inverse()[0] == n2, "Wrong RangeIdMap::InverseMap");
465 481
    check(armap.inverse()[0] == a3, "Wrong RangeIdMap::InverseMap");
466 482
  }
467 483
  
468 484
  // SourceMap, TargetMap, ForwardMap, BackwardMap, InDegMap, OutDegMap
469 485
  {
470 486
    typedef ListGraph Graph;
471 487
    GRAPH_TYPEDEFS(Graph);
472 488
    
473 489
    checkConcept<ReadMap<Arc, Node>, SourceMap<Graph> >();
474 490
    checkConcept<ReadMap<Arc, Node>, TargetMap<Graph> >();
475 491
    checkConcept<ReadMap<Edge, Arc>, ForwardMap<Graph> >();
476 492
    checkConcept<ReadMap<Edge, Arc>, BackwardMap<Graph> >();
477 493
    checkConcept<ReadMap<Node, int>, InDegMap<Graph> >();
478 494
    checkConcept<ReadMap<Node, int>, OutDegMap<Graph> >();
479 495

	
480 496
    Graph gr;
481 497
    Node n0 = gr.addNode();
482 498
    Node n1 = gr.addNode();
483 499
    Node n2 = gr.addNode();
484 500
    
485 501
    gr.addEdge(n0,n1);
486 502
    gr.addEdge(n1,n2);
487 503
    gr.addEdge(n0,n2);
488 504
    gr.addEdge(n2,n1);
489 505
    gr.addEdge(n1,n2);
490 506
    gr.addEdge(n0,n1);
491 507
    
492 508
    for (EdgeIt e(gr); e != INVALID; ++e) {
493 509
      check(forwardMap(gr)[e] == gr.direct(e, true), "Wrong ForwardMap");
494 510
      check(backwardMap(gr)[e] == gr.direct(e, false), "Wrong BackwardMap");
495 511
    }
496 512
    
497
    compareMap(sourceMap(orienter(gr, constMap<Edge, bool>(true))),
498
               targetMap(orienter(gr, constMap<Edge, bool>(false))),
499
               EdgeIt(gr));
513
    check(mapCompare(gr,
514
          sourceMap(orienter(gr, constMap<Edge, bool>(true))),
515
          targetMap(orienter(gr, constMap<Edge, bool>(false)))),
516
          "Wrong SourceMap or TargetMap");
500 517

	
501 518
    typedef Orienter<Graph, const ConstMap<Edge, bool> > Digraph;
502 519
    Digraph dgr(gr, constMap<Edge, bool>(true));
503 520
    OutDegMap<Digraph> odm(dgr);
504 521
    InDegMap<Digraph> idm(dgr);
505 522
    
506 523
    check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 1, "Wrong OutDegMap");
507 524
    check(idm[n0] == 0 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap");
508 525
   
509 526
    gr.addEdge(n2, n0);
510 527

	
511 528
    check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 2, "Wrong OutDegMap");
512 529
    check(idm[n0] == 1 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap");
513 530
  }
514 531
  
515 532
  // CrossRefMap
516 533
  {
517 534
    typedef ListDigraph Graph;
518 535
    DIGRAPH_TYPEDEFS(Graph);
519 536

	
520 537
    checkConcept<ReadWriteMap<Node, int>,
521 538
                 CrossRefMap<Graph, Node, int> >();
522 539
    checkConcept<ReadWriteMap<Node, bool>,
523 540
                 CrossRefMap<Graph, Node, bool> >();
524 541
    checkConcept<ReadWriteMap<Node, double>,
525 542
                 CrossRefMap<Graph, Node, double> >();
526 543
    
527 544
    Graph gr;
528 545
    typedef CrossRefMap<Graph, Node, char> CRMap;
529 546
    CRMap map(gr);
530 547
    
531 548
    Node n0 = gr.addNode();
532 549
    Node n1 = gr.addNode();
533 550
    Node n2 = gr.addNode();
534 551
    
535 552
    map.set(n0, 'A');
536 553
    map.set(n1, 'B');
537 554
    map.set(n2, 'C');
538 555
    
539 556
    check(map[n0] == 'A' && map('A') == n0 && map.inverse()['A'] == n0,
540 557
          "Wrong CrossRefMap");
541 558
    check(map[n1] == 'B' && map('B') == n1 && map.inverse()['B'] == n1,
542 559
          "Wrong CrossRefMap");
543 560
    check(map[n2] == 'C' && map('C') == n2 && map.inverse()['C'] == n2,
544 561
          "Wrong CrossRefMap");
545 562
    check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
546 563
          "Wrong CrossRefMap::count()");
547 564
    
548 565
    CRMap::ValueIt it = map.beginValue();
549 566
    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
550 567
          it == map.endValue(), "Wrong value iterator");
551 568
    
552 569
    map.set(n2, 'A');
553 570

	
554 571
    check(map[n0] == 'A' && map[n1] == 'B' && map[n2] == 'A',
555 572
          "Wrong CrossRefMap");
556 573
    check(map('A') == n0 && map.inverse()['A'] == n0, "Wrong CrossRefMap");
557 574
    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
558 575
    check(map('C') == INVALID && map.inverse()['C'] == INVALID,
559 576
          "Wrong CrossRefMap");
560 577
    check(map.count('A') == 2 && map.count('B') == 1 && map.count('C') == 0,
561 578
          "Wrong CrossRefMap::count()");
562 579

	
563 580
    it = map.beginValue();
... ...
@@ -739,66 +756,244 @@
739 756
    for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) {
740 757
      check(map1[static_cast<Item>(it)] == 1, "Wrong value");
741 758
      ++n;
742 759
    }
743 760
    check(n == num, "Wrong number");
744 761

	
745 762
  }
746 763

	
747 764
  // Iterable value map
748 765
  {
749 766
    typedef SmartGraph Graph;
750 767
    typedef SmartGraph::Node Item;
751 768
    typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm;
752 769

	
753 770
    checkConcept<ReadWriteMap<Item, double>, Ivm>();
754 771

	
755 772
    const int num = 10;
756 773
    Graph g;
757 774
    std::vector<Item> items;
758 775
    for (int i = 0; i < num; ++i) {
759 776
      items.push_back(g.addNode());
760 777
    }
761 778

	
762 779
    Ivm map1(g, 0.0);
763 780
    check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size");
764 781
    check(*map1.beginValue() == 0.0, "Wrong value");
765 782

	
766 783
    for (int i = 0; i < num; ++i) {
767 784
      map1.set(items[i], static_cast<double>(i));
768 785
    }
769 786
    check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size");
770 787

	
771 788
    for (int i = 0; i < num; ++i) {
772 789
      Ivm::ItemIt it(map1, static_cast<double>(i));
773 790
      check(static_cast<Item>(it) == items[i], "Wrong value");
774 791
      ++it;
775 792
      check(static_cast<Item>(it) == INVALID, "Wrong value");
776 793
    }
777 794

	
778 795
    for (Ivm::ValueIt vit = map1.beginValue();
779 796
         vit != map1.endValue(); ++vit) {
780 797
      check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit,
781 798
            "Wrong ValueIt");
782 799
    }
783 800

	
784 801
    for (int i = 0; i < num; ++i) {
785 802
      map1.set(items[i], static_cast<double>(i % 2));
786 803
    }
787 804
    check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size");
788 805

	
789 806
    int n = 0;
790 807
    for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
791 808
      check(map1[static_cast<Item>(it)] == 0.0, "Wrong value");
792 809
      ++n;
793 810
    }
794 811
    check(n == (num + 1) / 2, "Wrong number");
795 812

	
796 813
    for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
797 814
      check(map1[static_cast<Item>(it)] == 1.0, "Wrong value");
798 815
      ++n;
799 816
    }
800 817
    check(n == num, "Wrong number");
801 818

	
802 819
  }
820
  
821
  // Graph map utilities:
822
  // mapMin(), mapMax(), mapMinValue(), mapMaxValue()
823
  // mapFind(), mapFindIf(), mapCount(), mapCountIf()
824
  // mapCopy(), mapCompare(), mapFill()
825
  {
826
    DIGRAPH_TYPEDEFS(SmartDigraph);
827

	
828
    SmartDigraph g;
829
    Node n1 = g.addNode();
830
    Node n2 = g.addNode();
831
    Node n3 = g.addNode();
832
    
833
    SmartDigraph::NodeMap<int> map1(g);
834
    SmartDigraph::ArcMap<char> map2(g);
835
    ConstMap<Node, A> cmap1 = A();
836
    ConstMap<Arc, C> cmap2 = C(0);
837
    
838
    map1[n1] = 10;
839
    map1[n2] = 5;
840
    map1[n3] = 12;
841
    
842
    // mapMin(), mapMax(), mapMinValue(), mapMaxValue()
843
    check(mapMin(g, map1) == n2, "Wrong mapMin()");
844
    check(mapMax(g, map1) == n3, "Wrong mapMax()");
845
    check(mapMin(g, map1, std::greater<int>()) == n3, "Wrong mapMin()");
846
    check(mapMax(g, map1, std::greater<int>()) == n2, "Wrong mapMax()");
847
    check(mapMinValue(g, map1) == 5, "Wrong mapMinValue()");
848
    check(mapMaxValue(g, map1) == 12, "Wrong mapMaxValue()");
849

	
850
    check(mapMin(g, map2) == INVALID, "Wrong mapMin()");
851
    check(mapMax(g, map2) == INVALID, "Wrong mapMax()");
852

	
853
    check(mapMin(g, cmap1) != INVALID, "Wrong mapMin()");
854
    check(mapMax(g, cmap2) == INVALID, "Wrong mapMax()");
855

	
856
    Arc a1 = g.addArc(n1, n2);
857
    Arc a2 = g.addArc(n1, n3);
858
    Arc a3 = g.addArc(n2, n3);
859
    Arc a4 = g.addArc(n3, n1);
860
    
861
    map2[a1] = 'b';
862
    map2[a2] = 'a';
863
    map2[a3] = 'b';
864
    map2[a4] = 'c';
865

	
866
    // mapMin(), mapMax(), mapMinValue(), mapMaxValue()
867
    check(mapMin(g, map2) == a2, "Wrong mapMin()");
868
    check(mapMax(g, map2) == a4, "Wrong mapMax()");
869
    check(mapMin(g, map2, std::greater<int>()) == a4, "Wrong mapMin()");
870
    check(mapMax(g, map2, std::greater<int>()) == a2, "Wrong mapMax()");
871
    check(mapMinValue(g, map2, std::greater<int>()) == 'c',
872
          "Wrong mapMinValue()");
873
    check(mapMaxValue(g, map2, std::greater<int>()) == 'a',
874
          "Wrong mapMaxValue()");
875

	
876
    check(mapMin(g, cmap1) != INVALID, "Wrong mapMin()");
877
    check(mapMax(g, cmap2) != INVALID, "Wrong mapMax()");
878
    check(mapMaxValue(g, cmap2) == C(0), "Wrong mapMaxValue()");
879

	
880
    check(mapMin(g, composeMap(functorToMap(&createC), map2)) == a2,
881
          "Wrong mapMin()");
882
    check(mapMax(g, composeMap(functorToMap(&createC), map2)) == a4,
883
          "Wrong mapMax()");
884
    check(mapMinValue(g, composeMap(functorToMap(&createC), map2)) == C('a'),
885
          "Wrong mapMinValue()");
886
    check(mapMaxValue(g, composeMap(functorToMap(&createC), map2)) == C('c'),
887
          "Wrong mapMaxValue()");
888

	
889
    // mapFind(), mapFindIf()
890
    check(mapFind(g, map1, 5) == n2, "Wrong mapFind()");
891
    check(mapFind(g, map1, 6) == INVALID, "Wrong mapFind()");
892
    check(mapFind(g, map2, 'a') == a2, "Wrong mapFind()");
893
    check(mapFind(g, map2, 'e') == INVALID, "Wrong mapFind()");
894
    check(mapFind(g, cmap2, C(0)) == ArcIt(g), "Wrong mapFind()");
895
    check(mapFind(g, cmap2, C(1)) == INVALID, "Wrong mapFind()");
896

	
897
    check(mapFindIf(g, map1, Less<int>(7)) == n2,
898
          "Wrong mapFindIf()");
899
    check(mapFindIf(g, map1, Less<int>(5)) == INVALID,
900
          "Wrong mapFindIf()");
901
    check(mapFindIf(g, map2, Less<char>('d')) == ArcIt(g),
902
          "Wrong mapFindIf()");
903
    check(mapFindIf(g, map2, Less<char>('a')) == INVALID,
904
          "Wrong mapFindIf()");
905

	
906
    // mapCount(), mapCountIf()
907
    check(mapCount(g, map1, 5) == 1, "Wrong mapCount()");
908
    check(mapCount(g, map1, 6) == 0, "Wrong mapCount()");
909
    check(mapCount(g, map2, 'a') == 1, "Wrong mapCount()");
910
    check(mapCount(g, map2, 'b') == 2, "Wrong mapCount()");
911
    check(mapCount(g, map2, 'e') == 0, "Wrong mapCount()");
912
    check(mapCount(g, cmap2, C(0)) == 4, "Wrong mapCount()");
913
    check(mapCount(g, cmap2, C(1)) == 0, "Wrong mapCount()");
914

	
915
    check(mapCountIf(g, map1, Less<int>(11)) == 2,
916
          "Wrong mapCountIf()");
917
    check(mapCountIf(g, map1, Less<int>(13)) == 3,
918
          "Wrong mapCountIf()");
919
    check(mapCountIf(g, map1, Less<int>(5)) == 0,
920
          "Wrong mapCountIf()");
921
    check(mapCountIf(g, map2, Less<char>('d')) == 4,
922
          "Wrong mapCountIf()");
923
    check(mapCountIf(g, map2, Less<char>('c')) == 3,
924
          "Wrong mapCountIf()");
925
    check(mapCountIf(g, map2, Less<char>('a')) == 0,
926
          "Wrong mapCountIf()");
927
     
928
    // MapIt, ConstMapIt
929
/*
930
These tests can be used after applying bugfix #330
931
    typedef SmartDigraph::NodeMap<int>::MapIt MapIt;
932
    typedef SmartDigraph::NodeMap<int>::ConstMapIt ConstMapIt;
933
    check(*std::min_element(MapIt(map1), MapIt(INVALID)) == 5,
934
          "Wrong NodeMap<>::MapIt");
935
    check(*std::max_element(ConstMapIt(map1), ConstMapIt(INVALID)) == 12,
936
          "Wrong NodeMap<>::MapIt");
937
    
938
    int sum = 0;
939
    std::for_each(MapIt(map1), MapIt(INVALID), Sum<int>(sum));
940
    check(sum == 27, "Wrong NodeMap<>::MapIt");
941
    std::for_each(ConstMapIt(map1), ConstMapIt(INVALID), Sum<int>(sum));
942
    check(sum == 54, "Wrong NodeMap<>::ConstMapIt");
943
*/
944

	
945
    // mapCopy(), mapCompare(), mapFill()
946
    check(mapCompare(g, map1, map1), "Wrong mapCompare()");
947
    check(mapCompare(g, cmap2, cmap2), "Wrong mapCompare()");
948
    check(mapCompare(g, map1, shiftMap(map1, 0)), "Wrong mapCompare()");
949
    check(mapCompare(g, map2, scaleMap(map2, 1)), "Wrong mapCompare()");
950
    check(!mapCompare(g, map1, shiftMap(map1, 1)), "Wrong mapCompare()");
951

	
952
    SmartDigraph::NodeMap<int> map3(g, 0);
953
    SmartDigraph::ArcMap<char> map4(g, 'a');
954
    
955
    check(!mapCompare(g, map1, map3), "Wrong mapCompare()");
956
    check(!mapCompare(g, map2, map4), "Wrong mapCompare()");    
957
    
958
    mapCopy(g, map1, map3);
959
    mapCopy(g, map2, map4);
960

	
961
    check(mapCompare(g, map1, map3), "Wrong mapCompare() or mapCopy()");
962
    check(mapCompare(g, map2, map4), "Wrong mapCompare() or mapCopy()");    
963
    
964
    Undirector<SmartDigraph> ug(g);
965
    Undirector<SmartDigraph>::EdgeMap<char> umap1(ug, 'x');
966
    Undirector<SmartDigraph>::ArcMap<double> umap2(ug, 3.14);
967
    
968
    check(!mapCompare(g, map2, umap1), "Wrong mapCompare() or mapCopy()");
969
    check(!mapCompare(g, umap1, map2), "Wrong mapCompare() or mapCopy()");
970
    check(!mapCompare(ug, map2, umap1), "Wrong mapCompare() or mapCopy()");
971
    check(!mapCompare(ug, umap1, map2), "Wrong mapCompare() or mapCopy()");
972
    
973
    mapCopy(g, map2, umap1);
974

	
975
    check(mapCompare(g, map2, umap1), "Wrong mapCompare() or mapCopy()");
976
    check(mapCompare(g, umap1, map2), "Wrong mapCompare() or mapCopy()");
977
    check(mapCompare(ug, map2, umap1), "Wrong mapCompare() or mapCopy()");
978
    check(mapCompare(ug, umap1, map2), "Wrong mapCompare() or mapCopy()");
979
    
980
    mapCopy(g, map2, umap1);
981
    mapCopy(g, umap1, map2);
982
    mapCopy(ug, map2, umap1);
983
    mapCopy(ug, umap1, map2);
984
    
985
    check(!mapCompare(ug, umap1, umap2), "Wrong mapCompare() or mapCopy()");
986
    mapCopy(ug, umap1, umap2);
987
    check(mapCompare(ug, umap1, umap2), "Wrong mapCompare() or mapCopy()");
988
    
989
    check(!mapCompare(g, map1, constMap<Node>(2)), "Wrong mapCompare()");
990
    mapFill(g, map1, 2);
991
    check(mapCompare(g, constMap<Node>(2), map1), "Wrong mapFill()");
992

	
993
    check(!mapCompare(g, map2, constMap<Arc>('z')), "Wrong mapCompare()");
994
    mapCopy(g, constMap<Arc>('z'), map2);
995
    check(mapCompare(g, constMap<Arc>('z'), map2), "Wrong mapCopy()");
996
  }
997
  
803 998
  return 0;
804 999
}
0 comments (0 inline)