0
3
0
... | ... |
@@ -2743,1028 +2743,1315 @@ |
2743 | 2743 |
IterableIntMapNode(int _value) : value(_value) {} |
2744 | 2744 |
Item prev, next; |
2745 | 2745 |
int value; |
2746 | 2746 |
}; |
2747 | 2747 |
} |
2748 | 2748 |
|
2749 | 2749 |
/// \brief Dynamic iterable integer map. |
2750 | 2750 |
/// |
2751 | 2751 |
/// This class provides a special graph map type which can store an |
2752 | 2752 |
/// integer value for graph items (\c Node, \c Arc or \c Edge). |
2753 | 2753 |
/// For each non-negative value it is possible to iterate on the keys |
2754 | 2754 |
/// mapped to the value. |
2755 | 2755 |
/// |
2756 | 2756 |
/// This map is intended to be used with small integer values, for which |
2757 | 2757 |
/// it is efficient, and supports iteration only for non-negative values. |
2758 | 2758 |
/// If you need large values and/or iteration for negative integers, |
2759 | 2759 |
/// consider to use \ref IterableValueMap instead. |
2760 | 2760 |
/// |
2761 | 2761 |
/// This type is a reference map, so it can be modified with the |
2762 | 2762 |
/// subscript operator. |
2763 | 2763 |
/// |
2764 | 2764 |
/// \note The size of the data structure depends on the largest |
2765 | 2765 |
/// value in the map. |
2766 | 2766 |
/// |
2767 | 2767 |
/// \tparam GR The graph type. |
2768 | 2768 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
2769 | 2769 |
/// \c GR::Edge). |
2770 | 2770 |
/// |
2771 | 2771 |
/// \see IterableBoolMap, IterableValueMap |
2772 | 2772 |
/// \see CrossRefMap |
2773 | 2773 |
template <typename GR, typename K> |
2774 | 2774 |
class IterableIntMap |
2775 | 2775 |
: protected ItemSetTraits<GR, K>:: |
2776 | 2776 |
template Map<_maps_bits::IterableIntMapNode<K> >::Type { |
2777 | 2777 |
public: |
2778 | 2778 |
typedef typename ItemSetTraits<GR, K>:: |
2779 | 2779 |
template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent; |
2780 | 2780 |
|
2781 | 2781 |
/// The key type |
2782 | 2782 |
typedef K Key; |
2783 | 2783 |
/// The value type |
2784 | 2784 |
typedef int Value; |
2785 | 2785 |
/// The graph type |
2786 | 2786 |
typedef GR Graph; |
2787 | 2787 |
|
2788 | 2788 |
/// \brief Constructor of the map. |
2789 | 2789 |
/// |
2790 | 2790 |
/// Constructor of the map. It sets all values to -1. |
2791 | 2791 |
explicit IterableIntMap(const Graph& graph) |
2792 | 2792 |
: Parent(graph) {} |
2793 | 2793 |
|
2794 | 2794 |
/// \brief Constructor of the map with a given value. |
2795 | 2795 |
/// |
2796 | 2796 |
/// Constructor of the map with a given value. |
2797 | 2797 |
explicit IterableIntMap(const Graph& graph, int value) |
2798 | 2798 |
: Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) { |
2799 | 2799 |
if (value >= 0) { |
2800 | 2800 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) { |
2801 | 2801 |
lace(it); |
2802 | 2802 |
} |
2803 | 2803 |
} |
2804 | 2804 |
} |
2805 | 2805 |
|
2806 | 2806 |
private: |
2807 | 2807 |
|
2808 | 2808 |
void unlace(const Key& key) { |
2809 | 2809 |
typename Parent::Value& node = Parent::operator[](key); |
2810 | 2810 |
if (node.value < 0) return; |
2811 | 2811 |
if (node.prev != INVALID) { |
2812 | 2812 |
Parent::operator[](node.prev).next = node.next; |
2813 | 2813 |
} else { |
2814 | 2814 |
_first[node.value] = node.next; |
2815 | 2815 |
} |
2816 | 2816 |
if (node.next != INVALID) { |
2817 | 2817 |
Parent::operator[](node.next).prev = node.prev; |
2818 | 2818 |
} |
2819 | 2819 |
while (!_first.empty() && _first.back() == INVALID) { |
2820 | 2820 |
_first.pop_back(); |
2821 | 2821 |
} |
2822 | 2822 |
} |
2823 | 2823 |
|
2824 | 2824 |
void lace(const Key& key) { |
2825 | 2825 |
typename Parent::Value& node = Parent::operator[](key); |
2826 | 2826 |
if (node.value < 0) return; |
2827 | 2827 |
if (node.value >= int(_first.size())) { |
2828 | 2828 |
_first.resize(node.value + 1, INVALID); |
2829 | 2829 |
} |
2830 | 2830 |
node.prev = INVALID; |
2831 | 2831 |
node.next = _first[node.value]; |
2832 | 2832 |
if (node.next != INVALID) { |
2833 | 2833 |
Parent::operator[](node.next).prev = key; |
2834 | 2834 |
} |
2835 | 2835 |
_first[node.value] = key; |
2836 | 2836 |
} |
2837 | 2837 |
|
2838 | 2838 |
public: |
2839 | 2839 |
|
2840 | 2840 |
/// Indicates that the map is reference map. |
2841 | 2841 |
typedef True ReferenceMapTag; |
2842 | 2842 |
|
2843 | 2843 |
/// \brief Reference to the value of the map. |
2844 | 2844 |
/// |
2845 | 2845 |
/// This class is similar to the \c int type. It can |
2846 | 2846 |
/// be converted to \c int and it has the same operators. |
2847 | 2847 |
class Reference { |
2848 | 2848 |
friend class IterableIntMap; |
2849 | 2849 |
private: |
2850 | 2850 |
Reference(IterableIntMap& map, const Key& key) |
2851 | 2851 |
: _key(key), _map(map) {} |
2852 | 2852 |
public: |
2853 | 2853 |
|
2854 | 2854 |
Reference& operator=(const Reference& value) { |
2855 | 2855 |
_map.set(_key, static_cast<const int&>(value)); |
2856 | 2856 |
return *this; |
2857 | 2857 |
} |
2858 | 2858 |
|
2859 | 2859 |
operator const int&() const { |
2860 | 2860 |
return static_cast<const IterableIntMap&>(_map)[_key]; |
2861 | 2861 |
} |
2862 | 2862 |
|
2863 | 2863 |
Reference& operator=(int value) { |
2864 | 2864 |
_map.set(_key, value); |
2865 | 2865 |
return *this; |
2866 | 2866 |
} |
2867 | 2867 |
Reference& operator++() { |
2868 | 2868 |
_map.set(_key, _map[_key] + 1); |
2869 | 2869 |
return *this; |
2870 | 2870 |
} |
2871 | 2871 |
int operator++(int) { |
2872 | 2872 |
int value = _map[_key]; |
2873 | 2873 |
_map.set(_key, value + 1); |
2874 | 2874 |
return value; |
2875 | 2875 |
} |
2876 | 2876 |
Reference& operator--() { |
2877 | 2877 |
_map.set(_key, _map[_key] - 1); |
2878 | 2878 |
return *this; |
2879 | 2879 |
} |
2880 | 2880 |
int operator--(int) { |
2881 | 2881 |
int value = _map[_key]; |
2882 | 2882 |
_map.set(_key, value - 1); |
2883 | 2883 |
return value; |
2884 | 2884 |
} |
2885 | 2885 |
Reference& operator+=(int value) { |
2886 | 2886 |
_map.set(_key, _map[_key] + value); |
2887 | 2887 |
return *this; |
2888 | 2888 |
} |
2889 | 2889 |
Reference& operator-=(int value) { |
2890 | 2890 |
_map.set(_key, _map[_key] - value); |
2891 | 2891 |
return *this; |
2892 | 2892 |
} |
2893 | 2893 |
Reference& operator*=(int value) { |
2894 | 2894 |
_map.set(_key, _map[_key] * value); |
2895 | 2895 |
return *this; |
2896 | 2896 |
} |
2897 | 2897 |
Reference& operator/=(int value) { |
2898 | 2898 |
_map.set(_key, _map[_key] / value); |
2899 | 2899 |
return *this; |
2900 | 2900 |
} |
2901 | 2901 |
Reference& operator%=(int value) { |
2902 | 2902 |
_map.set(_key, _map[_key] % value); |
2903 | 2903 |
return *this; |
2904 | 2904 |
} |
2905 | 2905 |
Reference& operator&=(int value) { |
2906 | 2906 |
_map.set(_key, _map[_key] & value); |
2907 | 2907 |
return *this; |
2908 | 2908 |
} |
2909 | 2909 |
Reference& operator|=(int value) { |
2910 | 2910 |
_map.set(_key, _map[_key] | value); |
2911 | 2911 |
return *this; |
2912 | 2912 |
} |
2913 | 2913 |
Reference& operator^=(int value) { |
2914 | 2914 |
_map.set(_key, _map[_key] ^ value); |
2915 | 2915 |
return *this; |
2916 | 2916 |
} |
2917 | 2917 |
Reference& operator<<=(int value) { |
2918 | 2918 |
_map.set(_key, _map[_key] << value); |
2919 | 2919 |
return *this; |
2920 | 2920 |
} |
2921 | 2921 |
Reference& operator>>=(int value) { |
2922 | 2922 |
_map.set(_key, _map[_key] >> value); |
2923 | 2923 |
return *this; |
2924 | 2924 |
} |
2925 | 2925 |
|
2926 | 2926 |
private: |
2927 | 2927 |
Key _key; |
2928 | 2928 |
IterableIntMap& _map; |
2929 | 2929 |
}; |
2930 | 2930 |
|
2931 | 2931 |
/// The const reference type. |
2932 | 2932 |
typedef const Value& ConstReference; |
2933 | 2933 |
|
2934 | 2934 |
/// \brief Gives back the maximal value plus one. |
2935 | 2935 |
/// |
2936 | 2936 |
/// Gives back the maximal value plus one. |
2937 | 2937 |
int size() const { |
2938 | 2938 |
return _first.size(); |
2939 | 2939 |
} |
2940 | 2940 |
|
2941 | 2941 |
/// \brief Set operation of the map. |
2942 | 2942 |
/// |
2943 | 2943 |
/// Set operation of the map. |
2944 | 2944 |
void set(const Key& key, const Value& value) { |
2945 | 2945 |
unlace(key); |
2946 | 2946 |
Parent::operator[](key).value = value; |
2947 | 2947 |
lace(key); |
2948 | 2948 |
} |
2949 | 2949 |
|
2950 | 2950 |
/// \brief Const subscript operator of the map. |
2951 | 2951 |
/// |
2952 | 2952 |
/// Const subscript operator of the map. |
2953 | 2953 |
const Value& operator[](const Key& key) const { |
2954 | 2954 |
return Parent::operator[](key).value; |
2955 | 2955 |
} |
2956 | 2956 |
|
2957 | 2957 |
/// \brief Subscript operator of the map. |
2958 | 2958 |
/// |
2959 | 2959 |
/// Subscript operator of the map. |
2960 | 2960 |
Reference operator[](const Key& key) { |
2961 | 2961 |
return Reference(*this, key); |
2962 | 2962 |
} |
2963 | 2963 |
|
2964 | 2964 |
/// \brief Iterator for the keys with the same value. |
2965 | 2965 |
/// |
2966 | 2966 |
/// Iterator for the keys with the same value. It works |
2967 | 2967 |
/// like a graph item iterator, it can be converted to |
2968 | 2968 |
/// the item type of the map, incremented with \c ++ operator, and |
2969 | 2969 |
/// if the iterator leaves the last valid item, it will be equal to |
2970 | 2970 |
/// \c INVALID. |
2971 | 2971 |
class ItemIt : public Key { |
2972 | 2972 |
public: |
2973 | 2973 |
typedef Key Parent; |
2974 | 2974 |
|
2975 | 2975 |
/// \brief Invalid constructor \& conversion. |
2976 | 2976 |
/// |
2977 | 2977 |
/// This constructor initializes the iterator to be invalid. |
2978 | 2978 |
/// \sa Invalid for more details. |
2979 | 2979 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {} |
2980 | 2980 |
|
2981 | 2981 |
/// \brief Creates an iterator with a value. |
2982 | 2982 |
/// |
2983 | 2983 |
/// Creates an iterator with a value. It iterates on the |
2984 | 2984 |
/// keys mapped to the given value. |
2985 | 2985 |
/// \param map The IterableIntMap. |
2986 | 2986 |
/// \param value The value. |
2987 | 2987 |
ItemIt(const IterableIntMap& map, int value) : _map(&map) { |
2988 | 2988 |
if (value < 0 || value >= int(_map->_first.size())) { |
2989 | 2989 |
Parent::operator=(INVALID); |
2990 | 2990 |
} else { |
2991 | 2991 |
Parent::operator=(_map->_first[value]); |
2992 | 2992 |
} |
2993 | 2993 |
} |
2994 | 2994 |
|
2995 | 2995 |
/// \brief Increment operator. |
2996 | 2996 |
/// |
2997 | 2997 |
/// Increment operator. |
2998 | 2998 |
ItemIt& operator++() { |
2999 | 2999 |
Parent::operator=(_map->IterableIntMap::Parent:: |
3000 | 3000 |
operator[](static_cast<Parent&>(*this)).next); |
3001 | 3001 |
return *this; |
3002 | 3002 |
} |
3003 | 3003 |
|
3004 | 3004 |
private: |
3005 | 3005 |
const IterableIntMap* _map; |
3006 | 3006 |
}; |
3007 | 3007 |
|
3008 | 3008 |
protected: |
3009 | 3009 |
|
3010 | 3010 |
virtual void erase(const Key& key) { |
3011 | 3011 |
unlace(key); |
3012 | 3012 |
Parent::erase(key); |
3013 | 3013 |
} |
3014 | 3014 |
|
3015 | 3015 |
virtual void erase(const std::vector<Key>& keys) { |
3016 | 3016 |
for (int i = 0; i < int(keys.size()); ++i) { |
3017 | 3017 |
unlace(keys[i]); |
3018 | 3018 |
} |
3019 | 3019 |
Parent::erase(keys); |
3020 | 3020 |
} |
3021 | 3021 |
|
3022 | 3022 |
virtual void clear() { |
3023 | 3023 |
_first.clear(); |
3024 | 3024 |
Parent::clear(); |
3025 | 3025 |
} |
3026 | 3026 |
|
3027 | 3027 |
private: |
3028 | 3028 |
std::vector<Key> _first; |
3029 | 3029 |
}; |
3030 | 3030 |
|
3031 | 3031 |
namespace _maps_bits { |
3032 | 3032 |
template <typename Item, typename Value> |
3033 | 3033 |
struct IterableValueMapNode { |
3034 | 3034 |
IterableValueMapNode(Value _value = Value()) : value(_value) {} |
3035 | 3035 |
Item prev, next; |
3036 | 3036 |
Value value; |
3037 | 3037 |
}; |
3038 | 3038 |
} |
3039 | 3039 |
|
3040 | 3040 |
/// \brief Dynamic iterable map for comparable values. |
3041 | 3041 |
/// |
3042 | 3042 |
/// This class provides a special graph map type which can store a |
3043 | 3043 |
/// comparable value for graph items (\c Node, \c Arc or \c Edge). |
3044 | 3044 |
/// For each value it is possible to iterate on the keys mapped to |
3045 | 3045 |
/// the value (\c ItemIt), and the values of the map can be accessed |
3046 | 3046 |
/// with an STL compatible forward iterator (\c ValueIt). |
3047 | 3047 |
/// The map stores a linked list for each value, which contains |
3048 | 3048 |
/// the items mapped to the value, and the used values are stored |
3049 | 3049 |
/// in balanced binary tree (\c std::map). |
3050 | 3050 |
/// |
3051 | 3051 |
/// \ref IterableBoolMap and \ref IterableIntMap are similar classes |
3052 | 3052 |
/// specialized for \c bool and \c int values, respectively. |
3053 | 3053 |
/// |
3054 | 3054 |
/// This type is not reference map, so it cannot be modified with |
3055 | 3055 |
/// the subscript operator. |
3056 | 3056 |
/// |
3057 | 3057 |
/// \tparam GR The graph type. |
3058 | 3058 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
3059 | 3059 |
/// \c GR::Edge). |
3060 | 3060 |
/// \tparam V The value type of the map. It can be any comparable |
3061 | 3061 |
/// value type. |
3062 | 3062 |
/// |
3063 | 3063 |
/// \see IterableBoolMap, IterableIntMap |
3064 | 3064 |
/// \see CrossRefMap |
3065 | 3065 |
template <typename GR, typename K, typename V> |
3066 | 3066 |
class IterableValueMap |
3067 | 3067 |
: protected ItemSetTraits<GR, K>:: |
3068 | 3068 |
template Map<_maps_bits::IterableValueMapNode<K, V> >::Type { |
3069 | 3069 |
public: |
3070 | 3070 |
typedef typename ItemSetTraits<GR, K>:: |
3071 | 3071 |
template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent; |
3072 | 3072 |
|
3073 | 3073 |
/// The key type |
3074 | 3074 |
typedef K Key; |
3075 | 3075 |
/// The value type |
3076 | 3076 |
typedef V Value; |
3077 | 3077 |
/// The graph type |
3078 | 3078 |
typedef GR Graph; |
3079 | 3079 |
|
3080 | 3080 |
public: |
3081 | 3081 |
|
3082 | 3082 |
/// \brief Constructor of the map with a given value. |
3083 | 3083 |
/// |
3084 | 3084 |
/// Constructor of the map with a given value. |
3085 | 3085 |
explicit IterableValueMap(const Graph& graph, |
3086 | 3086 |
const Value& value = Value()) |
3087 | 3087 |
: Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) { |
3088 | 3088 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) { |
3089 | 3089 |
lace(it); |
3090 | 3090 |
} |
3091 | 3091 |
} |
3092 | 3092 |
|
3093 | 3093 |
protected: |
3094 | 3094 |
|
3095 | 3095 |
void unlace(const Key& key) { |
3096 | 3096 |
typename Parent::Value& node = Parent::operator[](key); |
3097 | 3097 |
if (node.prev != INVALID) { |
3098 | 3098 |
Parent::operator[](node.prev).next = node.next; |
3099 | 3099 |
} else { |
3100 | 3100 |
if (node.next != INVALID) { |
3101 | 3101 |
_first[node.value] = node.next; |
3102 | 3102 |
} else { |
3103 | 3103 |
_first.erase(node.value); |
3104 | 3104 |
} |
3105 | 3105 |
} |
3106 | 3106 |
if (node.next != INVALID) { |
3107 | 3107 |
Parent::operator[](node.next).prev = node.prev; |
3108 | 3108 |
} |
3109 | 3109 |
} |
3110 | 3110 |
|
3111 | 3111 |
void lace(const Key& key) { |
3112 | 3112 |
typename Parent::Value& node = Parent::operator[](key); |
3113 | 3113 |
typename std::map<Value, Key>::iterator it = _first.find(node.value); |
3114 | 3114 |
if (it == _first.end()) { |
3115 | 3115 |
node.prev = node.next = INVALID; |
3116 | 3116 |
_first.insert(std::make_pair(node.value, key)); |
3117 | 3117 |
} else { |
3118 | 3118 |
node.prev = INVALID; |
3119 | 3119 |
node.next = it->second; |
3120 | 3120 |
if (node.next != INVALID) { |
3121 | 3121 |
Parent::operator[](node.next).prev = key; |
3122 | 3122 |
} |
3123 | 3123 |
it->second = key; |
3124 | 3124 |
} |
3125 | 3125 |
} |
3126 | 3126 |
|
3127 | 3127 |
public: |
3128 | 3128 |
|
3129 | 3129 |
/// \brief Forward iterator for values. |
3130 | 3130 |
/// |
3131 | 3131 |
/// This iterator is an STL compatible forward |
3132 | 3132 |
/// iterator on the values of the map. The values can |
3133 | 3133 |
/// be accessed in the <tt>[beginValue, endValue)</tt> range. |
3134 | 3134 |
class ValueIt |
3135 | 3135 |
: public std::iterator<std::forward_iterator_tag, Value> { |
3136 | 3136 |
friend class IterableValueMap; |
3137 | 3137 |
private: |
3138 | 3138 |
ValueIt(typename std::map<Value, Key>::const_iterator _it) |
3139 | 3139 |
: it(_it) {} |
3140 | 3140 |
public: |
3141 | 3141 |
|
3142 | 3142 |
/// Constructor |
3143 | 3143 |
ValueIt() {} |
3144 | 3144 |
|
3145 | 3145 |
/// \e |
3146 | 3146 |
ValueIt& operator++() { ++it; return *this; } |
3147 | 3147 |
/// \e |
3148 | 3148 |
ValueIt operator++(int) { |
3149 | 3149 |
ValueIt tmp(*this); |
3150 | 3150 |
operator++(); |
3151 | 3151 |
return tmp; |
3152 | 3152 |
} |
3153 | 3153 |
|
3154 | 3154 |
/// \e |
3155 | 3155 |
const Value& operator*() const { return it->first; } |
3156 | 3156 |
/// \e |
3157 | 3157 |
const Value* operator->() const { return &(it->first); } |
3158 | 3158 |
|
3159 | 3159 |
/// \e |
3160 | 3160 |
bool operator==(ValueIt jt) const { return it == jt.it; } |
3161 | 3161 |
/// \e |
3162 | 3162 |
bool operator!=(ValueIt jt) const { return it != jt.it; } |
3163 | 3163 |
|
3164 | 3164 |
private: |
3165 | 3165 |
typename std::map<Value, Key>::const_iterator it; |
3166 | 3166 |
}; |
3167 | 3167 |
|
3168 | 3168 |
/// \brief Returns an iterator to the first value. |
3169 | 3169 |
/// |
3170 | 3170 |
/// Returns an STL compatible iterator to the |
3171 | 3171 |
/// first value of the map. The values of the |
3172 | 3172 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt> |
3173 | 3173 |
/// range. |
3174 | 3174 |
ValueIt beginValue() const { |
3175 | 3175 |
return ValueIt(_first.begin()); |
3176 | 3176 |
} |
3177 | 3177 |
|
3178 | 3178 |
/// \brief Returns an iterator after the last value. |
3179 | 3179 |
/// |
3180 | 3180 |
/// Returns an STL compatible iterator after the |
3181 | 3181 |
/// last value of the map. The values of the |
3182 | 3182 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt> |
3183 | 3183 |
/// range. |
3184 | 3184 |
ValueIt endValue() const { |
3185 | 3185 |
return ValueIt(_first.end()); |
3186 | 3186 |
} |
3187 | 3187 |
|
3188 | 3188 |
/// \brief Set operation of the map. |
3189 | 3189 |
/// |
3190 | 3190 |
/// Set operation of the map. |
3191 | 3191 |
void set(const Key& key, const Value& value) { |
3192 | 3192 |
unlace(key); |
3193 | 3193 |
Parent::operator[](key).value = value; |
3194 | 3194 |
lace(key); |
3195 | 3195 |
} |
3196 | 3196 |
|
3197 | 3197 |
/// \brief Const subscript operator of the map. |
3198 | 3198 |
/// |
3199 | 3199 |
/// Const subscript operator of the map. |
3200 | 3200 |
const Value& operator[](const Key& key) const { |
3201 | 3201 |
return Parent::operator[](key).value; |
3202 | 3202 |
} |
3203 | 3203 |
|
3204 | 3204 |
/// \brief Iterator for the keys with the same value. |
3205 | 3205 |
/// |
3206 | 3206 |
/// Iterator for the keys with the same value. It works |
3207 | 3207 |
/// like a graph item iterator, it can be converted to |
3208 | 3208 |
/// the item type of the map, incremented with \c ++ operator, and |
3209 | 3209 |
/// if the iterator leaves the last valid item, it will be equal to |
3210 | 3210 |
/// \c INVALID. |
3211 | 3211 |
class ItemIt : public Key { |
3212 | 3212 |
public: |
3213 | 3213 |
typedef Key Parent; |
3214 | 3214 |
|
3215 | 3215 |
/// \brief Invalid constructor \& conversion. |
3216 | 3216 |
/// |
3217 | 3217 |
/// This constructor initializes the iterator to be invalid. |
3218 | 3218 |
/// \sa Invalid for more details. |
3219 | 3219 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {} |
3220 | 3220 |
|
3221 | 3221 |
/// \brief Creates an iterator with a value. |
3222 | 3222 |
/// |
3223 | 3223 |
/// Creates an iterator with a value. It iterates on the |
3224 | 3224 |
/// keys which have the given value. |
3225 | 3225 |
/// \param map The IterableValueMap |
3226 | 3226 |
/// \param value The value |
3227 | 3227 |
ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) { |
3228 | 3228 |
typename std::map<Value, Key>::const_iterator it = |
3229 | 3229 |
map._first.find(value); |
3230 | 3230 |
if (it == map._first.end()) { |
3231 | 3231 |
Parent::operator=(INVALID); |
3232 | 3232 |
} else { |
3233 | 3233 |
Parent::operator=(it->second); |
3234 | 3234 |
} |
3235 | 3235 |
} |
3236 | 3236 |
|
3237 | 3237 |
/// \brief Increment operator. |
3238 | 3238 |
/// |
3239 | 3239 |
/// Increment Operator. |
3240 | 3240 |
ItemIt& operator++() { |
3241 | 3241 |
Parent::operator=(_map->IterableValueMap::Parent:: |
3242 | 3242 |
operator[](static_cast<Parent&>(*this)).next); |
3243 | 3243 |
return *this; |
3244 | 3244 |
} |
3245 | 3245 |
|
3246 | 3246 |
|
3247 | 3247 |
private: |
3248 | 3248 |
const IterableValueMap* _map; |
3249 | 3249 |
}; |
3250 | 3250 |
|
3251 | 3251 |
protected: |
3252 | 3252 |
|
3253 | 3253 |
virtual void add(const Key& key) { |
3254 | 3254 |
Parent::add(key); |
3255 | 3255 |
unlace(key); |
3256 | 3256 |
} |
3257 | 3257 |
|
3258 | 3258 |
virtual void add(const std::vector<Key>& keys) { |
3259 | 3259 |
Parent::add(keys); |
3260 | 3260 |
for (int i = 0; i < int(keys.size()); ++i) { |
3261 | 3261 |
lace(keys[i]); |
3262 | 3262 |
} |
3263 | 3263 |
} |
3264 | 3264 |
|
3265 | 3265 |
virtual void erase(const Key& key) { |
3266 | 3266 |
unlace(key); |
3267 | 3267 |
Parent::erase(key); |
3268 | 3268 |
} |
3269 | 3269 |
|
3270 | 3270 |
virtual void erase(const std::vector<Key>& keys) { |
3271 | 3271 |
for (int i = 0; i < int(keys.size()); ++i) { |
3272 | 3272 |
unlace(keys[i]); |
3273 | 3273 |
} |
3274 | 3274 |
Parent::erase(keys); |
3275 | 3275 |
} |
3276 | 3276 |
|
3277 | 3277 |
virtual void build() { |
3278 | 3278 |
Parent::build(); |
3279 | 3279 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) { |
3280 | 3280 |
lace(it); |
3281 | 3281 |
} |
3282 | 3282 |
} |
3283 | 3283 |
|
3284 | 3284 |
virtual void clear() { |
3285 | 3285 |
_first.clear(); |
3286 | 3286 |
Parent::clear(); |
3287 | 3287 |
} |
3288 | 3288 |
|
3289 | 3289 |
private: |
3290 | 3290 |
std::map<Value, Key> _first; |
3291 | 3291 |
}; |
3292 | 3292 |
|
3293 | 3293 |
/// \brief Map of the source nodes of arcs in a digraph. |
3294 | 3294 |
/// |
3295 | 3295 |
/// SourceMap provides access for the source node of each arc in a digraph, |
3296 | 3296 |
/// which is returned by the \c source() function of the digraph. |
3297 | 3297 |
/// \tparam GR The digraph type. |
3298 | 3298 |
/// \see TargetMap |
3299 | 3299 |
template <typename GR> |
3300 | 3300 |
class SourceMap { |
3301 | 3301 |
public: |
3302 | 3302 |
|
3303 | 3303 |
/// The key type (the \c Arc type of the digraph). |
3304 | 3304 |
typedef typename GR::Arc Key; |
3305 | 3305 |
/// The value type (the \c Node type of the digraph). |
3306 | 3306 |
typedef typename GR::Node Value; |
3307 | 3307 |
|
3308 | 3308 |
/// \brief Constructor |
3309 | 3309 |
/// |
3310 | 3310 |
/// Constructor. |
3311 | 3311 |
/// \param digraph The digraph that the map belongs to. |
3312 | 3312 |
explicit SourceMap(const GR& digraph) : _graph(digraph) {} |
3313 | 3313 |
|
3314 | 3314 |
/// \brief Returns the source node of the given arc. |
3315 | 3315 |
/// |
3316 | 3316 |
/// Returns the source node of the given arc. |
3317 | 3317 |
Value operator[](const Key& arc) const { |
3318 | 3318 |
return _graph.source(arc); |
3319 | 3319 |
} |
3320 | 3320 |
|
3321 | 3321 |
private: |
3322 | 3322 |
const GR& _graph; |
3323 | 3323 |
}; |
3324 | 3324 |
|
3325 | 3325 |
/// \brief Returns a \c SourceMap class. |
3326 | 3326 |
/// |
3327 | 3327 |
/// This function just returns an \c SourceMap class. |
3328 | 3328 |
/// \relates SourceMap |
3329 | 3329 |
template <typename GR> |
3330 | 3330 |
inline SourceMap<GR> sourceMap(const GR& graph) { |
3331 | 3331 |
return SourceMap<GR>(graph); |
3332 | 3332 |
} |
3333 | 3333 |
|
3334 | 3334 |
/// \brief Map of the target nodes of arcs in a digraph. |
3335 | 3335 |
/// |
3336 | 3336 |
/// TargetMap provides access for the target node of each arc in a digraph, |
3337 | 3337 |
/// which is returned by the \c target() function of the digraph. |
3338 | 3338 |
/// \tparam GR The digraph type. |
3339 | 3339 |
/// \see SourceMap |
3340 | 3340 |
template <typename GR> |
3341 | 3341 |
class TargetMap { |
3342 | 3342 |
public: |
3343 | 3343 |
|
3344 | 3344 |
/// The key type (the \c Arc type of the digraph). |
3345 | 3345 |
typedef typename GR::Arc Key; |
3346 | 3346 |
/// The value type (the \c Node type of the digraph). |
3347 | 3347 |
typedef typename GR::Node Value; |
3348 | 3348 |
|
3349 | 3349 |
/// \brief Constructor |
3350 | 3350 |
/// |
3351 | 3351 |
/// Constructor. |
3352 | 3352 |
/// \param digraph The digraph that the map belongs to. |
3353 | 3353 |
explicit TargetMap(const GR& digraph) : _graph(digraph) {} |
3354 | 3354 |
|
3355 | 3355 |
/// \brief Returns the target node of the given arc. |
3356 | 3356 |
/// |
3357 | 3357 |
/// Returns the target node of the given arc. |
3358 | 3358 |
Value operator[](const Key& e) const { |
3359 | 3359 |
return _graph.target(e); |
3360 | 3360 |
} |
3361 | 3361 |
|
3362 | 3362 |
private: |
3363 | 3363 |
const GR& _graph; |
3364 | 3364 |
}; |
3365 | 3365 |
|
3366 | 3366 |
/// \brief Returns a \c TargetMap class. |
3367 | 3367 |
/// |
3368 | 3368 |
/// This function just returns a \c TargetMap class. |
3369 | 3369 |
/// \relates TargetMap |
3370 | 3370 |
template <typename GR> |
3371 | 3371 |
inline TargetMap<GR> targetMap(const GR& graph) { |
3372 | 3372 |
return TargetMap<GR>(graph); |
3373 | 3373 |
} |
3374 | 3374 |
|
3375 | 3375 |
/// \brief Map of the "forward" directed arc view of edges in a graph. |
3376 | 3376 |
/// |
3377 | 3377 |
/// ForwardMap provides access for the "forward" directed arc view of |
3378 | 3378 |
/// each edge in a graph, which is returned by the \c direct() function |
3379 | 3379 |
/// of the graph with \c true parameter. |
3380 | 3380 |
/// \tparam GR The graph type. |
3381 | 3381 |
/// \see BackwardMap |
3382 | 3382 |
template <typename GR> |
3383 | 3383 |
class ForwardMap { |
3384 | 3384 |
public: |
3385 | 3385 |
|
3386 | 3386 |
/// The key type (the \c Edge type of the digraph). |
3387 | 3387 |
typedef typename GR::Edge Key; |
3388 | 3388 |
/// The value type (the \c Arc type of the digraph). |
3389 | 3389 |
typedef typename GR::Arc Value; |
3390 | 3390 |
|
3391 | 3391 |
/// \brief Constructor |
3392 | 3392 |
/// |
3393 | 3393 |
/// Constructor. |
3394 | 3394 |
/// \param graph The graph that the map belongs to. |
3395 | 3395 |
explicit ForwardMap(const GR& graph) : _graph(graph) {} |
3396 | 3396 |
|
3397 | 3397 |
/// \brief Returns the "forward" directed arc view of the given edge. |
3398 | 3398 |
/// |
3399 | 3399 |
/// Returns the "forward" directed arc view of the given edge. |
3400 | 3400 |
Value operator[](const Key& key) const { |
3401 | 3401 |
return _graph.direct(key, true); |
3402 | 3402 |
} |
3403 | 3403 |
|
3404 | 3404 |
private: |
3405 | 3405 |
const GR& _graph; |
3406 | 3406 |
}; |
3407 | 3407 |
|
3408 | 3408 |
/// \brief Returns a \c ForwardMap class. |
3409 | 3409 |
/// |
3410 | 3410 |
/// This function just returns an \c ForwardMap class. |
3411 | 3411 |
/// \relates ForwardMap |
3412 | 3412 |
template <typename GR> |
3413 | 3413 |
inline ForwardMap<GR> forwardMap(const GR& graph) { |
3414 | 3414 |
return ForwardMap<GR>(graph); |
3415 | 3415 |
} |
3416 | 3416 |
|
3417 | 3417 |
/// \brief Map of the "backward" directed arc view of edges in a graph. |
3418 | 3418 |
/// |
3419 | 3419 |
/// BackwardMap provides access for the "backward" directed arc view of |
3420 | 3420 |
/// each edge in a graph, which is returned by the \c direct() function |
3421 | 3421 |
/// of the graph with \c false parameter. |
3422 | 3422 |
/// \tparam GR The graph type. |
3423 | 3423 |
/// \see ForwardMap |
3424 | 3424 |
template <typename GR> |
3425 | 3425 |
class BackwardMap { |
3426 | 3426 |
public: |
3427 | 3427 |
|
3428 | 3428 |
/// The key type (the \c Edge type of the digraph). |
3429 | 3429 |
typedef typename GR::Edge Key; |
3430 | 3430 |
/// The value type (the \c Arc type of the digraph). |
3431 | 3431 |
typedef typename GR::Arc Value; |
3432 | 3432 |
|
3433 | 3433 |
/// \brief Constructor |
3434 | 3434 |
/// |
3435 | 3435 |
/// Constructor. |
3436 | 3436 |
/// \param graph The graph that the map belongs to. |
3437 | 3437 |
explicit BackwardMap(const GR& graph) : _graph(graph) {} |
3438 | 3438 |
|
3439 | 3439 |
/// \brief Returns the "backward" directed arc view of the given edge. |
3440 | 3440 |
/// |
3441 | 3441 |
/// Returns the "backward" directed arc view of the given edge. |
3442 | 3442 |
Value operator[](const Key& key) const { |
3443 | 3443 |
return _graph.direct(key, false); |
3444 | 3444 |
} |
3445 | 3445 |
|
3446 | 3446 |
private: |
3447 | 3447 |
const GR& _graph; |
3448 | 3448 |
}; |
3449 | 3449 |
|
3450 | 3450 |
/// \brief Returns a \c BackwardMap class |
3451 | 3451 |
|
3452 | 3452 |
/// This function just returns a \c BackwardMap class. |
3453 | 3453 |
/// \relates BackwardMap |
3454 | 3454 |
template <typename GR> |
3455 | 3455 |
inline BackwardMap<GR> backwardMap(const GR& graph) { |
3456 | 3456 |
return BackwardMap<GR>(graph); |
3457 | 3457 |
} |
3458 | 3458 |
|
3459 | 3459 |
/// \brief Map of the in-degrees of nodes in a digraph. |
3460 | 3460 |
/// |
3461 | 3461 |
/// This map returns the in-degree of a node. Once it is constructed, |
3462 | 3462 |
/// the degrees are stored in a standard \c NodeMap, so each query is done |
3463 | 3463 |
/// in constant time. On the other hand, the values are updated automatically |
3464 | 3464 |
/// whenever the digraph changes. |
3465 | 3465 |
/// |
3466 | 3466 |
/// \warning Besides \c addNode() and \c addArc(), a digraph structure |
3467 | 3467 |
/// may provide alternative ways to modify the digraph. |
3468 | 3468 |
/// The correct behavior of InDegMap is not guarantied if these additional |
3469 | 3469 |
/// features are used. For example, the functions |
3470 | 3470 |
/// \ref ListDigraph::changeSource() "changeSource()", |
3471 | 3471 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
3472 | 3472 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
3473 | 3473 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
3474 | 3474 |
/// |
3475 | 3475 |
/// \sa OutDegMap |
3476 | 3476 |
template <typename GR> |
3477 | 3477 |
class InDegMap |
3478 | 3478 |
: protected ItemSetTraits<GR, typename GR::Arc> |
3479 | 3479 |
::ItemNotifier::ObserverBase { |
3480 | 3480 |
|
3481 | 3481 |
public: |
3482 | 3482 |
|
3483 | 3483 |
/// The graph type of InDegMap |
3484 | 3484 |
typedef GR Graph; |
3485 | 3485 |
typedef GR Digraph; |
3486 | 3486 |
/// The key type |
3487 | 3487 |
typedef typename Digraph::Node Key; |
3488 | 3488 |
/// The value type |
3489 | 3489 |
typedef int Value; |
3490 | 3490 |
|
3491 | 3491 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
3492 | 3492 |
::ItemNotifier::ObserverBase Parent; |
3493 | 3493 |
|
3494 | 3494 |
private: |
3495 | 3495 |
|
3496 | 3496 |
class AutoNodeMap |
3497 | 3497 |
: public ItemSetTraits<Digraph, Key>::template Map<int>::Type { |
3498 | 3498 |
public: |
3499 | 3499 |
|
3500 | 3500 |
typedef typename ItemSetTraits<Digraph, Key>:: |
3501 | 3501 |
template Map<int>::Type Parent; |
3502 | 3502 |
|
3503 | 3503 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {} |
3504 | 3504 |
|
3505 | 3505 |
virtual void add(const Key& key) { |
3506 | 3506 |
Parent::add(key); |
3507 | 3507 |
Parent::set(key, 0); |
3508 | 3508 |
} |
3509 | 3509 |
|
3510 | 3510 |
virtual void add(const std::vector<Key>& keys) { |
3511 | 3511 |
Parent::add(keys); |
3512 | 3512 |
for (int i = 0; i < int(keys.size()); ++i) { |
3513 | 3513 |
Parent::set(keys[i], 0); |
3514 | 3514 |
} |
3515 | 3515 |
} |
3516 | 3516 |
|
3517 | 3517 |
virtual void build() { |
3518 | 3518 |
Parent::build(); |
3519 | 3519 |
Key it; |
3520 | 3520 |
typename Parent::Notifier* nf = Parent::notifier(); |
3521 | 3521 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
3522 | 3522 |
Parent::set(it, 0); |
3523 | 3523 |
} |
3524 | 3524 |
} |
3525 | 3525 |
}; |
3526 | 3526 |
|
3527 | 3527 |
public: |
3528 | 3528 |
|
3529 | 3529 |
/// \brief Constructor. |
3530 | 3530 |
/// |
3531 | 3531 |
/// Constructor for creating an in-degree map. |
3532 | 3532 |
explicit InDegMap(const Digraph& graph) |
3533 | 3533 |
: _digraph(graph), _deg(graph) { |
3534 | 3534 |
Parent::attach(_digraph.notifier(typename Digraph::Arc())); |
3535 | 3535 |
|
3536 | 3536 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
3537 | 3537 |
_deg[it] = countInArcs(_digraph, it); |
3538 | 3538 |
} |
3539 | 3539 |
} |
3540 | 3540 |
|
3541 | 3541 |
/// \brief Gives back the in-degree of a Node. |
3542 | 3542 |
/// |
3543 | 3543 |
/// Gives back the in-degree of a Node. |
3544 | 3544 |
int operator[](const Key& key) const { |
3545 | 3545 |
return _deg[key]; |
3546 | 3546 |
} |
3547 | 3547 |
|
3548 | 3548 |
protected: |
3549 | 3549 |
|
3550 | 3550 |
typedef typename Digraph::Arc Arc; |
3551 | 3551 |
|
3552 | 3552 |
virtual void add(const Arc& arc) { |
3553 | 3553 |
++_deg[_digraph.target(arc)]; |
3554 | 3554 |
} |
3555 | 3555 |
|
3556 | 3556 |
virtual void add(const std::vector<Arc>& arcs) { |
3557 | 3557 |
for (int i = 0; i < int(arcs.size()); ++i) { |
3558 | 3558 |
++_deg[_digraph.target(arcs[i])]; |
3559 | 3559 |
} |
3560 | 3560 |
} |
3561 | 3561 |
|
3562 | 3562 |
virtual void erase(const Arc& arc) { |
3563 | 3563 |
--_deg[_digraph.target(arc)]; |
3564 | 3564 |
} |
3565 | 3565 |
|
3566 | 3566 |
virtual void erase(const std::vector<Arc>& arcs) { |
3567 | 3567 |
for (int i = 0; i < int(arcs.size()); ++i) { |
3568 | 3568 |
--_deg[_digraph.target(arcs[i])]; |
3569 | 3569 |
} |
3570 | 3570 |
} |
3571 | 3571 |
|
3572 | 3572 |
virtual void build() { |
3573 | 3573 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
3574 | 3574 |
_deg[it] = countInArcs(_digraph, it); |
3575 | 3575 |
} |
3576 | 3576 |
} |
3577 | 3577 |
|
3578 | 3578 |
virtual void clear() { |
3579 | 3579 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
3580 | 3580 |
_deg[it] = 0; |
3581 | 3581 |
} |
3582 | 3582 |
} |
3583 | 3583 |
private: |
3584 | 3584 |
|
3585 | 3585 |
const Digraph& _digraph; |
3586 | 3586 |
AutoNodeMap _deg; |
3587 | 3587 |
}; |
3588 | 3588 |
|
3589 | 3589 |
/// \brief Map of the out-degrees of nodes in a digraph. |
3590 | 3590 |
/// |
3591 | 3591 |
/// This map returns the out-degree of a node. Once it is constructed, |
3592 | 3592 |
/// the degrees are stored in a standard \c NodeMap, so each query is done |
3593 | 3593 |
/// in constant time. On the other hand, the values are updated automatically |
3594 | 3594 |
/// whenever the digraph changes. |
3595 | 3595 |
/// |
3596 | 3596 |
/// \warning Besides \c addNode() and \c addArc(), a digraph structure |
3597 | 3597 |
/// may provide alternative ways to modify the digraph. |
3598 | 3598 |
/// The correct behavior of OutDegMap is not guarantied if these additional |
3599 | 3599 |
/// features are used. For example, the functions |
3600 | 3600 |
/// \ref ListDigraph::changeSource() "changeSource()", |
3601 | 3601 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
3602 | 3602 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
3603 | 3603 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
3604 | 3604 |
/// |
3605 | 3605 |
/// \sa InDegMap |
3606 | 3606 |
template <typename GR> |
3607 | 3607 |
class OutDegMap |
3608 | 3608 |
: protected ItemSetTraits<GR, typename GR::Arc> |
3609 | 3609 |
::ItemNotifier::ObserverBase { |
3610 | 3610 |
|
3611 | 3611 |
public: |
3612 | 3612 |
|
3613 | 3613 |
/// The graph type of OutDegMap |
3614 | 3614 |
typedef GR Graph; |
3615 | 3615 |
typedef GR Digraph; |
3616 | 3616 |
/// The key type |
3617 | 3617 |
typedef typename Digraph::Node Key; |
3618 | 3618 |
/// The value type |
3619 | 3619 |
typedef int Value; |
3620 | 3620 |
|
3621 | 3621 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
3622 | 3622 |
::ItemNotifier::ObserverBase Parent; |
3623 | 3623 |
|
3624 | 3624 |
private: |
3625 | 3625 |
|
3626 | 3626 |
class AutoNodeMap |
3627 | 3627 |
: public ItemSetTraits<Digraph, Key>::template Map<int>::Type { |
3628 | 3628 |
public: |
3629 | 3629 |
|
3630 | 3630 |
typedef typename ItemSetTraits<Digraph, Key>:: |
3631 | 3631 |
template Map<int>::Type Parent; |
3632 | 3632 |
|
3633 | 3633 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {} |
3634 | 3634 |
|
3635 | 3635 |
virtual void add(const Key& key) { |
3636 | 3636 |
Parent::add(key); |
3637 | 3637 |
Parent::set(key, 0); |
3638 | 3638 |
} |
3639 | 3639 |
virtual void add(const std::vector<Key>& keys) { |
3640 | 3640 |
Parent::add(keys); |
3641 | 3641 |
for (int i = 0; i < int(keys.size()); ++i) { |
3642 | 3642 |
Parent::set(keys[i], 0); |
3643 | 3643 |
} |
3644 | 3644 |
} |
3645 | 3645 |
virtual void build() { |
3646 | 3646 |
Parent::build(); |
3647 | 3647 |
Key it; |
3648 | 3648 |
typename Parent::Notifier* nf = Parent::notifier(); |
3649 | 3649 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
3650 | 3650 |
Parent::set(it, 0); |
3651 | 3651 |
} |
3652 | 3652 |
} |
3653 | 3653 |
}; |
3654 | 3654 |
|
3655 | 3655 |
public: |
3656 | 3656 |
|
3657 | 3657 |
/// \brief Constructor. |
3658 | 3658 |
/// |
3659 | 3659 |
/// Constructor for creating an out-degree map. |
3660 | 3660 |
explicit OutDegMap(const Digraph& graph) |
3661 | 3661 |
: _digraph(graph), _deg(graph) { |
3662 | 3662 |
Parent::attach(_digraph.notifier(typename Digraph::Arc())); |
3663 | 3663 |
|
3664 | 3664 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
3665 | 3665 |
_deg[it] = countOutArcs(_digraph, it); |
3666 | 3666 |
} |
3667 | 3667 |
} |
3668 | 3668 |
|
3669 | 3669 |
/// \brief Gives back the out-degree of a Node. |
3670 | 3670 |
/// |
3671 | 3671 |
/// Gives back the out-degree of a Node. |
3672 | 3672 |
int operator[](const Key& key) const { |
3673 | 3673 |
return _deg[key]; |
3674 | 3674 |
} |
3675 | 3675 |
|
3676 | 3676 |
protected: |
3677 | 3677 |
|
3678 | 3678 |
typedef typename Digraph::Arc Arc; |
3679 | 3679 |
|
3680 | 3680 |
virtual void add(const Arc& arc) { |
3681 | 3681 |
++_deg[_digraph.source(arc)]; |
3682 | 3682 |
} |
3683 | 3683 |
|
3684 | 3684 |
virtual void add(const std::vector<Arc>& arcs) { |
3685 | 3685 |
for (int i = 0; i < int(arcs.size()); ++i) { |
3686 | 3686 |
++_deg[_digraph.source(arcs[i])]; |
3687 | 3687 |
} |
3688 | 3688 |
} |
3689 | 3689 |
|
3690 | 3690 |
virtual void erase(const Arc& arc) { |
3691 | 3691 |
--_deg[_digraph.source(arc)]; |
3692 | 3692 |
} |
3693 | 3693 |
|
3694 | 3694 |
virtual void erase(const std::vector<Arc>& arcs) { |
3695 | 3695 |
for (int i = 0; i < int(arcs.size()); ++i) { |
3696 | 3696 |
--_deg[_digraph.source(arcs[i])]; |
3697 | 3697 |
} |
3698 | 3698 |
} |
3699 | 3699 |
|
3700 | 3700 |
virtual void build() { |
3701 | 3701 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
3702 | 3702 |
_deg[it] = countOutArcs(_digraph, it); |
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 |
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 <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); |
133 | 133 |
s = bf_test.predNode(t); |
134 | 134 |
b = bf_test.reached(t); |
135 | 135 |
pp = bf_test.path(t); |
136 | 136 |
pp = bf_test.negativeCycle(); |
137 | 137 |
} |
138 | 138 |
} |
139 | 139 |
|
140 | 140 |
void checkBellmanFordFunctionCompile() |
141 | 141 |
{ |
142 | 142 |
typedef int Value; |
143 | 143 |
typedef concepts::Digraph Digraph; |
144 | 144 |
typedef Digraph::Arc Arc; |
145 | 145 |
typedef Digraph::Node Node; |
146 | 146 |
typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap; |
147 | 147 |
|
148 | 148 |
Digraph g; |
149 | 149 |
bool b; |
150 | 150 |
bellmanFord(g,LengthMap()).run(Node()); |
151 | 151 |
b = bellmanFord(g,LengthMap()).run(Node(),Node()); |
152 | 152 |
bellmanFord(g,LengthMap()) |
153 | 153 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
154 | 154 |
.distMap(concepts::ReadWriteMap<Node,Value>()) |
155 | 155 |
.run(Node()); |
156 | 156 |
b=bellmanFord(g,LengthMap()) |
157 | 157 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
158 | 158 |
.distMap(concepts::ReadWriteMap<Node,Value>()) |
159 | 159 |
.path(concepts::Path<Digraph>()) |
160 | 160 |
.dist(Value()) |
161 | 161 |
.run(Node(),Node()); |
162 | 162 |
} |
163 | 163 |
|
164 | 164 |
|
165 | 165 |
template <typename Digraph, typename Value> |
166 | 166 |
void checkBellmanFord() { |
167 | 167 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
168 | 168 |
typedef typename Digraph::template ArcMap<Value> LengthMap; |
169 | 169 |
|
170 | 170 |
Digraph gr; |
171 | 171 |
Node s, t; |
172 | 172 |
LengthMap length(gr); |
173 | 173 |
|
174 | 174 |
std::istringstream input(test_lgf); |
175 | 175 |
digraphReader(gr, input). |
176 | 176 |
arcMap("length", length). |
177 | 177 |
node("source", s). |
178 | 178 |
node("target", t). |
179 | 179 |
run(); |
180 | 180 |
|
181 | 181 |
BellmanFord<Digraph, LengthMap> |
182 | 182 |
bf(gr, length); |
183 | 183 |
bf.run(s); |
184 | 184 |
Path<Digraph> p = bf.path(t); |
185 | 185 |
|
186 | 186 |
check(bf.reached(t) && bf.dist(t) == -1, "Bellman-Ford found a wrong path."); |
187 | 187 |
check(p.length() == 3, "path() found a wrong path."); |
188 | 188 |
check(checkPath(gr, p), "path() found a wrong path."); |
189 | 189 |
check(pathSource(gr, p) == s, "path() found a wrong path."); |
190 | 190 |
check(pathTarget(gr, p) == t, "path() found a wrong path."); |
191 | 191 |
|
192 | 192 |
ListPath<Digraph> path; |
193 | 193 |
Value dist; |
194 | 194 |
bool reached = bellmanFord(gr,length).path(path).dist(dist).run(s,t); |
195 | 195 |
|
196 | 196 |
check(reached && dist == -1, "Bellman-Ford found a wrong path."); |
197 | 197 |
check(path.length() == 3, "path() found a wrong path."); |
198 | 198 |
check(checkPath(gr, path), "path() found a wrong path."); |
199 | 199 |
check(pathSource(gr, path) == s, "path() found a wrong path."); |
200 | 200 |
check(pathTarget(gr, path) == t, "path() found a wrong path."); |
201 | 201 |
|
202 | 202 |
for(ArcIt e(gr); e!=INVALID; ++e) { |
203 | 203 |
Node u=gr.source(e); |
204 | 204 |
Node v=gr.target(e); |
205 | 205 |
check(!bf.reached(u) || (bf.dist(v) - bf.dist(u) <= length[e]), |
206 | 206 |
"Wrong output. dist(target)-dist(source)-arc_length=" << |
207 | 207 |
bf.dist(v) - bf.dist(u) - length[e]); |
208 | 208 |
} |
209 | 209 |
|
210 | 210 |
for(NodeIt v(gr); v!=INVALID; ++v) { |
211 | 211 |
if (bf.reached(v)) { |
212 | 212 |
check(v==s || bf.predArc(v)!=INVALID, "Wrong tree."); |
213 | 213 |
if (bf.predArc(v)!=INVALID ) { |
214 | 214 |
Arc e=bf.predArc(v); |
215 | 215 |
Node u=gr.source(e); |
216 | 216 |
check(u==bf.predNode(v),"Wrong tree."); |
217 | 217 |
check(bf.dist(v) - bf.dist(u) == length[e], |
218 | 218 |
"Wrong distance! Difference: " << |
219 | 219 |
bf.dist(v) - bf.dist(u) - length[e]); |
220 | 220 |
} |
221 | 221 |
} |
222 | 222 |
} |
223 | 223 |
} |
224 | 224 |
|
225 | 225 |
void checkBellmanFordNegativeCycle() { |
226 | 226 |
DIGRAPH_TYPEDEFS(SmartDigraph); |
227 | 227 |
|
228 | 228 |
SmartDigraph gr; |
229 | 229 |
IntArcMap length(gr); |
230 | 230 |
|
231 | 231 |
Node n1 = gr.addNode(); |
232 | 232 |
Node n2 = gr.addNode(); |
233 | 233 |
Node n3 = gr.addNode(); |
234 | 234 |
Node n4 = gr.addNode(); |
235 | 235 |
|
236 | 236 |
Arc a1 = gr.addArc(n1, n2); |
237 | 237 |
Arc a2 = gr.addArc(n2, n2); |
238 | 238 |
|
239 | 239 |
length[a1] = 2; |
240 | 240 |
length[a2] = -1; |
241 | 241 |
|
242 | 242 |
{ |
243 | 243 |
BellmanFord<SmartDigraph, IntArcMap> bf(gr, length); |
244 | 244 |
bf.run(n1); |
245 | 245 |
StaticPath<SmartDigraph> p = bf.negativeCycle(); |
246 | 246 |
check(p.length() == 1 && p.front() == p.back() && p.front() == a2, |
247 | 247 |
"Wrong negative cycle."); |
248 | 248 |
} |
249 | 249 |
|
250 | 250 |
length[a2] = 0; |
251 | 251 |
|
252 | 252 |
{ |
253 | 253 |
BellmanFord<SmartDigraph, IntArcMap> bf(gr, length); |
254 | 254 |
bf.run(n1); |
255 | 255 |
check(bf.negativeCycle().empty(), |
256 | 256 |
"Negative cycle should not be found."); |
257 | 257 |
} |
258 | 258 |
|
259 | 259 |
length[gr.addArc(n1, n3)] = 5; |
260 | 260 |
length[gr.addArc(n4, n3)] = 1; |
261 | 261 |
length[gr.addArc(n2, n4)] = 2; |
262 | 262 |
length[gr.addArc(n3, n2)] = -4; |
263 | 263 |
|
264 | 264 |
{ |
265 | 265 |
BellmanFord<SmartDigraph, IntArcMap> bf(gr, length); |
266 | 266 |
bf.init(); |
267 | 267 |
bf.addSource(n1); |
268 | 268 |
for (int i = 0; i < 4; ++i) { |
269 | 269 |
check(bf.negativeCycle().empty(), |
270 | 270 |
"Negative cycle should not be found."); |
271 | 271 |
bf.processNextRound(); |
272 | 272 |
} |
273 | 273 |
StaticPath<SmartDigraph> p = bf.negativeCycle(); |
274 | 274 |
check(p.length() == 3, "Wrong negative cycle."); |
275 | 275 |
check(length[p.nth(0)] + length[p.nth(1)] + length[p.nth(2)] == -1, |
276 | 276 |
"Wrong negative cycle."); |
277 | 277 |
} |
278 | 278 |
} |
279 | 279 |
|
280 | 280 |
int main() { |
281 | 281 |
checkBellmanFord<ListDigraph, int>(); |
282 | 282 |
checkBellmanFord<SmartDigraph, double>(); |
283 | 283 |
checkBellmanFordNegativeCycle(); |
284 | 284 |
return 0; |
285 | 285 |
} |
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 |
|
41 |
int _x; |
|
41 | 42 |
public: |
42 |
C(int |
|
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; |
137 | 153 |
RangeMap<B> map2(10); |
138 | 154 |
RangeMap<B> map3(10,B()); |
139 | 155 |
RangeMap<B> map4 = map1; |
140 | 156 |
RangeMap<B> map5 = rangeMap<B>(); |
141 | 157 |
RangeMap<B> map6 = rangeMap<B>(10); |
142 | 158 |
RangeMap<B> map7 = rangeMap(10,B()); |
143 | 159 |
|
144 | 160 |
checkConcept< ReferenceMap<int, double, double&, const double&>, |
145 | 161 |
RangeMap<double> >(); |
146 | 162 |
std::vector<double> v(10, 0); |
147 | 163 |
v[5] = 100; |
148 | 164 |
RangeMap<double> map8(v); |
149 | 165 |
RangeMap<double> map9 = rangeMap(v); |
150 | 166 |
check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100, |
151 | 167 |
"Something is wrong with RangeMap"); |
152 | 168 |
} |
153 | 169 |
|
154 | 170 |
// SparseMap |
155 | 171 |
{ |
156 | 172 |
checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >(); |
157 | 173 |
SparseMap<A,B> map1; |
158 | 174 |
SparseMap<A,B> map2 = B(); |
159 | 175 |
SparseMap<A,B> map3 = sparseMap<A,B>(); |
160 | 176 |
SparseMap<A,B> map4 = sparseMap<A>(B()); |
161 | 177 |
|
162 | 178 |
checkConcept< ReferenceMap<double, int, int&, const int&>, |
163 | 179 |
SparseMap<double, int> >(); |
164 | 180 |
std::map<double, int> m; |
165 | 181 |
SparseMap<double, int> map5(m); |
166 | 182 |
SparseMap<double, int> map6(m,10); |
167 | 183 |
SparseMap<double, int> map7 = sparseMap(m); |
168 | 184 |
SparseMap<double, int> map8 = sparseMap(m,10); |
169 | 185 |
|
170 | 186 |
check(map5[1.0] == 0 && map5[3.14] == 0 && |
171 | 187 |
map6[1.0] == 10 && map6[3.14] == 10, |
172 | 188 |
"Something is wrong with SparseMap"); |
173 | 189 |
map5[1.0] = map6[3.14] = 100; |
174 | 190 |
check(map5[1.0] == 100 && map5[3.14] == 0 && |
175 | 191 |
map6[1.0] == 10 && map6[3.14] == 100, |
176 | 192 |
"Something is wrong with SparseMap"); |
177 | 193 |
} |
178 | 194 |
|
179 | 195 |
// ComposeMap |
180 | 196 |
{ |
181 | 197 |
typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap; |
182 | 198 |
checkConcept<ReadMap<B,double>, CompMap>(); |
183 | 199 |
CompMap map1 = CompMap(DoubleMap(),ReadMap<B,A>()); |
184 | 200 |
CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>()); |
185 | 201 |
|
186 | 202 |
SparseMap<double, bool> m1(false); m1[3.14] = true; |
187 | 203 |
RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14; |
188 | 204 |
check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1], |
189 | 205 |
"Something is wrong with ComposeMap") |
190 | 206 |
} |
191 | 207 |
|
192 | 208 |
// CombineMap |
193 | 209 |
{ |
194 | 210 |
typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap; |
195 | 211 |
checkConcept<ReadMap<A,double>, CombMap>(); |
196 | 212 |
CombMap map1 = CombMap(DoubleMap(), DoubleMap()); |
197 | 213 |
CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>()); |
198 | 214 |
|
199 | 215 |
check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3, |
200 | 216 |
"Something is wrong with CombineMap"); |
201 | 217 |
} |
202 | 218 |
|
203 | 219 |
// FunctorToMap, MapToFunctor |
204 | 220 |
{ |
205 | 221 |
checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >(); |
206 | 222 |
checkConcept<ReadMap<A,B>, FunctorToMap<F> >(); |
207 | 223 |
FunctorToMap<F> map1; |
208 | 224 |
FunctorToMap<F> map2 = FunctorToMap<F>(F()); |
209 | 225 |
B b = functorToMap(F())[A()]; |
210 | 226 |
|
211 | 227 |
checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >(); |
212 | 228 |
MapToFunctor<ReadMap<A,B> > map = MapToFunctor<ReadMap<A,B> >(ReadMap<A,B>()); |
213 | 229 |
|
214 | 230 |
check(functorToMap(&func)[A()] == 3, |
215 | 231 |
"Something is wrong with FunctorToMap"); |
216 | 232 |
check(mapToFunctor(constMap<A,int>(2))(A()) == 2, |
217 | 233 |
"Something is wrong with MapToFunctor"); |
218 | 234 |
check(mapToFunctor(functorToMap(&func))(A()) == 3 && |
219 | 235 |
mapToFunctor(functorToMap(&func))[A()] == 3, |
220 | 236 |
"Something is wrong with FunctorToMap or MapToFunctor"); |
221 | 237 |
check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2, |
222 | 238 |
"Something is wrong with FunctorToMap or MapToFunctor"); |
223 | 239 |
} |
224 | 240 |
|
225 | 241 |
// ConvertMap |
226 | 242 |
{ |
227 | 243 |
checkConcept<ReadMap<double,double>, |
228 | 244 |
ConvertMap<ReadMap<double, int>, double> >(); |
229 | 245 |
ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true)); |
230 | 246 |
ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false)); |
231 | 247 |
} |
232 | 248 |
|
233 | 249 |
// ForkMap |
234 | 250 |
{ |
235 | 251 |
checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >(); |
236 | 252 |
|
237 | 253 |
typedef RangeMap<double> RM; |
238 | 254 |
typedef SparseMap<int, double> SM; |
239 | 255 |
RM m1(10, -1); |
240 | 256 |
SM m2(-1); |
241 | 257 |
checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >(); |
242 | 258 |
checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >(); |
243 | 259 |
ForkMap<RM, SM> map1(m1,m2); |
244 | 260 |
ForkMap<SM, RM> map2 = forkMap(m2,m1); |
245 | 261 |
map2.set(5, 10); |
246 | 262 |
check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 && |
247 | 263 |
m2[5] == 10 && map2[1] == -1 && map2[5] == 10, |
248 | 264 |
"Something is wrong with ForkMap"); |
249 | 265 |
} |
250 | 266 |
|
251 | 267 |
// Arithmetic maps: |
252 | 268 |
// - AddMap, SubMap, MulMap, DivMap |
253 | 269 |
// - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap |
254 | 270 |
// - NegMap, NegWriteMap, AbsMap |
255 | 271 |
{ |
256 | 272 |
checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >(); |
257 | 273 |
checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >(); |
258 | 274 |
checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >(); |
259 | 275 |
checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >(); |
260 | 276 |
|
261 | 277 |
ConstMap<int, double> c1(1.0), c2(3.14); |
262 | 278 |
IdentityMap<int> im; |
263 | 279 |
ConvertMap<IdentityMap<int>, double> id(im); |
264 | 280 |
check(addMap(c1,id)[0] == 1.0 && addMap(c1,id)[10] == 11.0, |
265 | 281 |
"Something is wrong with AddMap"); |
266 | 282 |
check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0, |
267 | 283 |
"Something is wrong with SubMap"); |
268 | 284 |
check(mulMap(id,c2)[0] == 0 && mulMap(id,c2)[2] == 6.28, |
269 | 285 |
"Something is wrong with MulMap"); |
270 | 286 |
check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2] == 1.57, |
271 | 287 |
"Something is wrong with DivMap"); |
272 | 288 |
|
273 | 289 |
checkConcept<DoubleMap, ShiftMap<DoubleMap> >(); |
274 | 290 |
checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >(); |
275 | 291 |
checkConcept<DoubleMap, ScaleMap<DoubleMap> >(); |
276 | 292 |
checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >(); |
277 | 293 |
checkConcept<DoubleMap, NegMap<DoubleMap> >(); |
278 | 294 |
checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >(); |
279 | 295 |
checkConcept<DoubleMap, AbsMap<DoubleMap> >(); |
280 | 296 |
|
281 | 297 |
check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0, |
282 | 298 |
"Something is wrong with ShiftMap"); |
283 | 299 |
check(shiftWriteMap(id, 2.0)[1] == 3.0 && |
284 | 300 |
shiftWriteMap(id, 2.0)[10] == 12.0, |
285 | 301 |
"Something is wrong with ShiftWriteMap"); |
286 | 302 |
check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0, |
287 | 303 |
"Something is wrong with ScaleMap"); |
288 | 304 |
check(scaleWriteMap(id, 2.0)[1] == 2.0 && |
289 | 305 |
scaleWriteMap(id, 2.0)[10] == 20.0, |
290 | 306 |
"Something is wrong with ScaleWriteMap"); |
291 | 307 |
check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0, |
292 | 308 |
"Something is wrong with NegMap"); |
293 | 309 |
check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0, |
294 | 310 |
"Something is wrong with NegWriteMap"); |
295 | 311 |
check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0, |
296 | 312 |
"Something is wrong with AbsMap"); |
297 | 313 |
} |
298 | 314 |
|
299 | 315 |
// Logical maps: |
300 | 316 |
// - TrueMap, FalseMap |
301 | 317 |
// - AndMap, OrMap |
302 | 318 |
// - NotMap, NotWriteMap |
303 | 319 |
// - EqualMap, LessMap |
304 | 320 |
{ |
305 | 321 |
checkConcept<BoolMap, TrueMap<A> >(); |
306 | 322 |
checkConcept<BoolMap, FalseMap<A> >(); |
307 | 323 |
checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >(); |
308 | 324 |
checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >(); |
309 | 325 |
checkConcept<BoolMap, NotMap<BoolMap> >(); |
310 | 326 |
checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >(); |
311 | 327 |
checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >(); |
312 | 328 |
checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >(); |
313 | 329 |
|
314 | 330 |
TrueMap<int> tm; |
315 | 331 |
FalseMap<int> fm; |
316 | 332 |
RangeMap<bool> rm(2); |
317 | 333 |
rm[0] = true; rm[1] = false; |
318 | 334 |
check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] && |
319 | 335 |
!andMap(fm,rm)[0] && !andMap(fm,rm)[1], |
320 | 336 |
"Something is wrong with AndMap"); |
321 | 337 |
check(orMap(tm,rm)[0] && orMap(tm,rm)[1] && |
322 | 338 |
orMap(fm,rm)[0] && !orMap(fm,rm)[1], |
323 | 339 |
"Something is wrong with OrMap"); |
324 | 340 |
check(!notMap(rm)[0] && notMap(rm)[1], |
325 | 341 |
"Something is wrong with NotMap"); |
326 | 342 |
check(!notWriteMap(rm)[0] && notWriteMap(rm)[1], |
327 | 343 |
"Something is wrong with NotWriteMap"); |
328 | 344 |
|
329 | 345 |
ConstMap<int, double> cm(2.0); |
330 | 346 |
IdentityMap<int> im; |
331 | 347 |
ConvertMap<IdentityMap<int>, double> id(im); |
332 | 348 |
check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3], |
333 | 349 |
"Something is wrong with LessMap"); |
334 | 350 |
check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3], |
335 | 351 |
"Something is wrong with EqualMap"); |
336 | 352 |
} |
337 | 353 |
|
338 | 354 |
// LoggerBoolMap |
339 | 355 |
{ |
340 | 356 |
typedef std::vector<int> vec; |
341 | 357 |
checkConcept<WriteMap<int, bool>, LoggerBoolMap<vec::iterator> >(); |
342 | 358 |
checkConcept<WriteMap<int, bool>, |
343 | 359 |
LoggerBoolMap<std::back_insert_iterator<vec> > >(); |
344 | 360 |
|
345 | 361 |
vec v1; |
346 | 362 |
vec v2(10); |
347 | 363 |
LoggerBoolMap<std::back_insert_iterator<vec> > |
348 | 364 |
map1(std::back_inserter(v1)); |
349 | 365 |
LoggerBoolMap<vec::iterator> map2(v2.begin()); |
350 | 366 |
map1.set(10, false); |
351 | 367 |
map1.set(20, true); map2.set(20, true); |
352 | 368 |
map1.set(30, false); map2.set(40, false); |
353 | 369 |
map1.set(50, true); map2.set(50, true); |
354 | 370 |
map1.set(60, true); map2.set(60, true); |
355 | 371 |
check(v1.size() == 3 && v2.size() == 10 && |
356 | 372 |
v1[0]==20 && v1[1]==50 && v1[2]==60 && |
357 | 373 |
v2[0]==20 && v2[1]==50 && v2[2]==60, |
358 | 374 |
"Something is wrong with LoggerBoolMap"); |
359 | 375 |
|
360 | 376 |
int i = 0; |
361 | 377 |
for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin(); |
362 | 378 |
it != map2.end(); ++it ) |
363 | 379 |
check(v1[i++] == *it, "Something is wrong with LoggerBoolMap"); |
364 | 380 |
|
365 | 381 |
typedef ListDigraph Graph; |
366 | 382 |
DIGRAPH_TYPEDEFS(Graph); |
367 | 383 |
Graph gr; |
368 | 384 |
|
369 | 385 |
Node n0 = gr.addNode(); |
370 | 386 |
Node n1 = gr.addNode(); |
371 | 387 |
Node n2 = gr.addNode(); |
372 | 388 |
Node n3 = gr.addNode(); |
373 | 389 |
|
374 | 390 |
gr.addArc(n3, n0); |
375 | 391 |
gr.addArc(n3, n2); |
376 | 392 |
gr.addArc(n0, n2); |
377 | 393 |
gr.addArc(n2, n1); |
378 | 394 |
gr.addArc(n0, n1); |
379 | 395 |
|
380 | 396 |
{ |
381 | 397 |
std::vector<Node> v; |
382 | 398 |
dfs(gr).processedMap(loggerBoolMap(std::back_inserter(v))).run(); |
383 | 399 |
|
384 | 400 |
check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3, |
385 | 401 |
"Something is wrong with LoggerBoolMap"); |
386 | 402 |
} |
387 | 403 |
{ |
388 | 404 |
std::vector<Node> v(countNodes(gr)); |
389 | 405 |
dfs(gr).processedMap(loggerBoolMap(v.begin())).run(); |
390 | 406 |
|
391 | 407 |
check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3, |
392 | 408 |
"Something is wrong with LoggerBoolMap"); |
393 | 409 |
} |
394 | 410 |
} |
395 | 411 |
|
396 | 412 |
// IdMap, RangeIdMap |
397 | 413 |
{ |
398 | 414 |
typedef ListDigraph Graph; |
399 | 415 |
DIGRAPH_TYPEDEFS(Graph); |
400 | 416 |
|
401 | 417 |
checkConcept<ReadMap<Node, int>, IdMap<Graph, Node> >(); |
402 | 418 |
checkConcept<ReadMap<Arc, int>, IdMap<Graph, Arc> >(); |
403 | 419 |
checkConcept<ReadMap<Node, int>, RangeIdMap<Graph, Node> >(); |
404 | 420 |
checkConcept<ReadMap<Arc, int>, RangeIdMap<Graph, Arc> >(); |
405 | 421 |
|
406 | 422 |
Graph gr; |
407 | 423 |
IdMap<Graph, Node> nmap(gr); |
408 | 424 |
IdMap<Graph, Arc> amap(gr); |
409 | 425 |
RangeIdMap<Graph, Node> nrmap(gr); |
410 | 426 |
RangeIdMap<Graph, Arc> armap(gr); |
411 | 427 |
|
412 | 428 |
Node n0 = gr.addNode(); |
413 | 429 |
Node n1 = gr.addNode(); |
414 | 430 |
Node n2 = gr.addNode(); |
415 | 431 |
|
416 | 432 |
Arc a0 = gr.addArc(n0, n1); |
417 | 433 |
Arc a1 = gr.addArc(n0, n2); |
418 | 434 |
Arc a2 = gr.addArc(n2, n1); |
419 | 435 |
Arc a3 = gr.addArc(n2, n0); |
420 | 436 |
|
421 | 437 |
check(nmap[n0] == gr.id(n0) && nmap(gr.id(n0)) == n0, "Wrong IdMap"); |
422 | 438 |
check(nmap[n1] == gr.id(n1) && nmap(gr.id(n1)) == n1, "Wrong IdMap"); |
423 | 439 |
check(nmap[n2] == gr.id(n2) && nmap(gr.id(n2)) == n2, "Wrong IdMap"); |
424 | 440 |
|
425 | 441 |
check(amap[a0] == gr.id(a0) && amap(gr.id(a0)) == a0, "Wrong IdMap"); |
426 | 442 |
check(amap[a1] == gr.id(a1) && amap(gr.id(a1)) == a1, "Wrong IdMap"); |
427 | 443 |
check(amap[a2] == gr.id(a2) && amap(gr.id(a2)) == a2, "Wrong IdMap"); |
428 | 444 |
check(amap[a3] == gr.id(a3) && amap(gr.id(a3)) == a3, "Wrong IdMap"); |
429 | 445 |
|
430 | 446 |
check(nmap.inverse()[gr.id(n0)] == n0, "Wrong IdMap::InverseMap"); |
431 | 447 |
check(amap.inverse()[gr.id(a0)] == a0, "Wrong IdMap::InverseMap"); |
432 | 448 |
|
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 |
|
|
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(); |
564 | 581 |
check(*it++ == 'A' && *it++ == 'A' && *it++ == 'B' && |
565 | 582 |
it == map.endValue(), "Wrong value iterator"); |
566 | 583 |
|
567 | 584 |
map.set(n0, 'C'); |
568 | 585 |
|
569 | 586 |
check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A', |
570 | 587 |
"Wrong CrossRefMap"); |
571 | 588 |
check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap"); |
572 | 589 |
check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap"); |
573 | 590 |
check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap"); |
574 | 591 |
check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1, |
575 | 592 |
"Wrong CrossRefMap::count()"); |
576 | 593 |
|
577 | 594 |
it = map.beginValue(); |
578 | 595 |
check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' && |
579 | 596 |
it == map.endValue(), "Wrong value iterator"); |
580 | 597 |
} |
581 | 598 |
|
582 | 599 |
// CrossRefMap |
583 | 600 |
{ |
584 | 601 |
typedef SmartDigraph Graph; |
585 | 602 |
DIGRAPH_TYPEDEFS(Graph); |
586 | 603 |
|
587 | 604 |
checkConcept<ReadWriteMap<Node, int>, |
588 | 605 |
CrossRefMap<Graph, Node, int> >(); |
589 | 606 |
|
590 | 607 |
Graph gr; |
591 | 608 |
typedef CrossRefMap<Graph, Node, char> CRMap; |
592 | 609 |
typedef CRMap::ValueIterator ValueIt; |
593 | 610 |
CRMap map(gr); |
594 | 611 |
|
595 | 612 |
Node n0 = gr.addNode(); |
596 | 613 |
Node n1 = gr.addNode(); |
597 | 614 |
Node n2 = gr.addNode(); |
598 | 615 |
|
599 | 616 |
map.set(n0, 'A'); |
600 | 617 |
map.set(n1, 'B'); |
601 | 618 |
map.set(n2, 'C'); |
602 | 619 |
map.set(n2, 'A'); |
603 | 620 |
map.set(n0, 'C'); |
604 | 621 |
|
605 | 622 |
check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A', |
606 | 623 |
"Wrong CrossRefMap"); |
607 | 624 |
check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap"); |
608 | 625 |
check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap"); |
609 | 626 |
check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap"); |
610 | 627 |
|
611 | 628 |
ValueIt it = map.beginValue(); |
612 | 629 |
check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' && |
613 | 630 |
it == map.endValue(), "Wrong value iterator"); |
614 | 631 |
} |
615 | 632 |
|
616 | 633 |
// Iterable bool map |
617 | 634 |
{ |
618 | 635 |
typedef SmartGraph Graph; |
619 | 636 |
typedef SmartGraph::Node Item; |
620 | 637 |
|
621 | 638 |
typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm; |
622 | 639 |
checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>(); |
623 | 640 |
|
624 | 641 |
const int num = 10; |
625 | 642 |
Graph g; |
626 | 643 |
std::vector<Item> items; |
627 | 644 |
for (int i = 0; i < num; ++i) { |
628 | 645 |
items.push_back(g.addNode()); |
629 | 646 |
} |
630 | 647 |
|
631 | 648 |
Ibm map1(g, true); |
632 | 649 |
int n = 0; |
633 | 650 |
for (Ibm::TrueIt it(map1); it != INVALID; ++it) { |
634 | 651 |
check(map1[static_cast<Item>(it)], "Wrong TrueIt"); |
635 | 652 |
++n; |
636 | 653 |
} |
637 | 654 |
check(n == num, "Wrong number"); |
638 | 655 |
|
639 | 656 |
n = 0; |
640 | 657 |
for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) { |
641 | 658 |
check(map1[static_cast<Item>(it)], "Wrong ItemIt for true"); |
642 | 659 |
++n; |
643 | 660 |
} |
644 | 661 |
check(n == num, "Wrong number"); |
645 | 662 |
check(Ibm::FalseIt(map1) == INVALID, "Wrong FalseIt"); |
646 | 663 |
check(Ibm::ItemIt(map1, false) == INVALID, "Wrong ItemIt for false"); |
647 | 664 |
|
648 | 665 |
map1[items[5]] = true; |
649 | 666 |
|
650 | 667 |
n = 0; |
651 | 668 |
for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) { |
652 | 669 |
check(map1[static_cast<Item>(it)], "Wrong ItemIt for true"); |
653 | 670 |
++n; |
654 | 671 |
} |
655 | 672 |
check(n == num, "Wrong number"); |
656 | 673 |
|
657 | 674 |
map1[items[num / 2]] = false; |
658 | 675 |
check(map1[items[num / 2]] == false, "Wrong map value"); |
659 | 676 |
|
660 | 677 |
n = 0; |
661 | 678 |
for (Ibm::TrueIt it(map1); it != INVALID; ++it) { |
662 | 679 |
check(map1[static_cast<Item>(it)], "Wrong TrueIt for true"); |
663 | 680 |
++n; |
664 | 681 |
} |
665 | 682 |
check(n == num - 1, "Wrong number"); |
666 | 683 |
|
667 | 684 |
n = 0; |
668 | 685 |
for (Ibm::FalseIt it(map1); it != INVALID; ++it) { |
669 | 686 |
check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true"); |
670 | 687 |
++n; |
671 | 688 |
} |
672 | 689 |
check(n == 1, "Wrong number"); |
673 | 690 |
|
674 | 691 |
map1[items[0]] = false; |
675 | 692 |
check(map1[items[0]] == false, "Wrong map value"); |
676 | 693 |
|
677 | 694 |
map1[items[num - 1]] = false; |
678 | 695 |
check(map1[items[num - 1]] == false, "Wrong map value"); |
679 | 696 |
|
680 | 697 |
n = 0; |
681 | 698 |
for (Ibm::TrueIt it(map1); it != INVALID; ++it) { |
682 | 699 |
check(map1[static_cast<Item>(it)], "Wrong TrueIt for true"); |
683 | 700 |
++n; |
684 | 701 |
} |
685 | 702 |
check(n == num - 3, "Wrong number"); |
686 | 703 |
check(map1.trueNum() == num - 3, "Wrong number"); |
687 | 704 |
|
688 | 705 |
n = 0; |
689 | 706 |
for (Ibm::FalseIt it(map1); it != INVALID; ++it) { |
690 | 707 |
check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true"); |
691 | 708 |
++n; |
692 | 709 |
} |
693 | 710 |
check(n == 3, "Wrong number"); |
694 | 711 |
check(map1.falseNum() == 3, "Wrong number"); |
695 | 712 |
} |
696 | 713 |
|
697 | 714 |
// Iterable int map |
698 | 715 |
{ |
699 | 716 |
typedef SmartGraph Graph; |
700 | 717 |
typedef SmartGraph::Node Item; |
701 | 718 |
typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim; |
702 | 719 |
|
703 | 720 |
checkConcept<ReferenceMap<Item, int, int&, const int&>, Iim>(); |
704 | 721 |
|
705 | 722 |
const int num = 10; |
706 | 723 |
Graph g; |
707 | 724 |
std::vector<Item> items; |
708 | 725 |
for (int i = 0; i < num; ++i) { |
709 | 726 |
items.push_back(g.addNode()); |
710 | 727 |
} |
711 | 728 |
|
712 | 729 |
Iim map1(g); |
713 | 730 |
check(map1.size() == 0, "Wrong size"); |
714 | 731 |
|
715 | 732 |
for (int i = 0; i < num; ++i) { |
716 | 733 |
map1[items[i]] = i; |
717 | 734 |
} |
718 | 735 |
check(map1.size() == num, "Wrong size"); |
719 | 736 |
|
720 | 737 |
for (int i = 0; i < num; ++i) { |
721 | 738 |
Iim::ItemIt it(map1, i); |
722 | 739 |
check(static_cast<Item>(it) == items[i], "Wrong value"); |
723 | 740 |
++it; |
724 | 741 |
check(static_cast<Item>(it) == INVALID, "Wrong value"); |
725 | 742 |
} |
726 | 743 |
|
727 | 744 |
for (int i = 0; i < num; ++i) { |
728 | 745 |
map1[items[i]] = i % 2; |
729 | 746 |
} |
730 | 747 |
check(map1.size() == 2, "Wrong size"); |
731 | 748 |
|
732 | 749 |
int n = 0; |
733 | 750 |
for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) { |
734 | 751 |
check(map1[static_cast<Item>(it)] == 0, "Wrong value"); |
735 | 752 |
++n; |
736 | 753 |
} |
737 | 754 |
check(n == (num + 1) / 2, "Wrong number"); |
738 | 755 |
|
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)