... | ... |
@@ -2999,513 +2999,513 @@ |
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 |
lace(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); |
... | ... |
@@ -388,613 +388,616 @@ |
388 | 388 |
Node n2 = gr.addNode(); |
389 | 389 |
Node n3 = gr.addNode(); |
390 | 390 |
|
391 | 391 |
gr.addArc(n3, n0); |
392 | 392 |
gr.addArc(n3, n2); |
393 | 393 |
gr.addArc(n0, n2); |
394 | 394 |
gr.addArc(n2, n1); |
395 | 395 |
gr.addArc(n0, n1); |
396 | 396 |
|
397 | 397 |
{ |
398 | 398 |
std::vector<Node> v; |
399 | 399 |
dfs(gr).processedMap(loggerBoolMap(std::back_inserter(v))).run(); |
400 | 400 |
|
401 | 401 |
check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3, |
402 | 402 |
"Something is wrong with LoggerBoolMap"); |
403 | 403 |
} |
404 | 404 |
{ |
405 | 405 |
std::vector<Node> v(countNodes(gr)); |
406 | 406 |
dfs(gr).processedMap(loggerBoolMap(v.begin())).run(); |
407 | 407 |
|
408 | 408 |
check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3, |
409 | 409 |
"Something is wrong with LoggerBoolMap"); |
410 | 410 |
} |
411 | 411 |
} |
412 | 412 |
|
413 | 413 |
// IdMap, RangeIdMap |
414 | 414 |
{ |
415 | 415 |
typedef ListDigraph Graph; |
416 | 416 |
DIGRAPH_TYPEDEFS(Graph); |
417 | 417 |
|
418 | 418 |
checkConcept<ReadMap<Node, int>, IdMap<Graph, Node> >(); |
419 | 419 |
checkConcept<ReadMap<Arc, int>, IdMap<Graph, Arc> >(); |
420 | 420 |
checkConcept<ReadMap<Node, int>, RangeIdMap<Graph, Node> >(); |
421 | 421 |
checkConcept<ReadMap<Arc, int>, RangeIdMap<Graph, Arc> >(); |
422 | 422 |
|
423 | 423 |
Graph gr; |
424 | 424 |
IdMap<Graph, Node> nmap(gr); |
425 | 425 |
IdMap<Graph, Arc> amap(gr); |
426 | 426 |
RangeIdMap<Graph, Node> nrmap(gr); |
427 | 427 |
RangeIdMap<Graph, Arc> armap(gr); |
428 | 428 |
|
429 | 429 |
Node n0 = gr.addNode(); |
430 | 430 |
Node n1 = gr.addNode(); |
431 | 431 |
Node n2 = gr.addNode(); |
432 | 432 |
|
433 | 433 |
Arc a0 = gr.addArc(n0, n1); |
434 | 434 |
Arc a1 = gr.addArc(n0, n2); |
435 | 435 |
Arc a2 = gr.addArc(n2, n1); |
436 | 436 |
Arc a3 = gr.addArc(n2, n0); |
437 | 437 |
|
438 | 438 |
check(nmap[n0] == gr.id(n0) && nmap(gr.id(n0)) == n0, "Wrong IdMap"); |
439 | 439 |
check(nmap[n1] == gr.id(n1) && nmap(gr.id(n1)) == n1, "Wrong IdMap"); |
440 | 440 |
check(nmap[n2] == gr.id(n2) && nmap(gr.id(n2)) == n2, "Wrong IdMap"); |
441 | 441 |
|
442 | 442 |
check(amap[a0] == gr.id(a0) && amap(gr.id(a0)) == a0, "Wrong IdMap"); |
443 | 443 |
check(amap[a1] == gr.id(a1) && amap(gr.id(a1)) == a1, "Wrong IdMap"); |
444 | 444 |
check(amap[a2] == gr.id(a2) && amap(gr.id(a2)) == a2, "Wrong IdMap"); |
445 | 445 |
check(amap[a3] == gr.id(a3) && amap(gr.id(a3)) == a3, "Wrong IdMap"); |
446 | 446 |
|
447 | 447 |
check(nmap.inverse()[gr.id(n0)] == n0, "Wrong IdMap::InverseMap"); |
448 | 448 |
check(amap.inverse()[gr.id(a0)] == a0, "Wrong IdMap::InverseMap"); |
449 | 449 |
|
450 | 450 |
check(nrmap.size() == 3 && armap.size() == 4, |
451 | 451 |
"Wrong RangeIdMap::size()"); |
452 | 452 |
|
453 | 453 |
check(nrmap[n0] == 0 && nrmap(0) == n0, "Wrong RangeIdMap"); |
454 | 454 |
check(nrmap[n1] == 1 && nrmap(1) == n1, "Wrong RangeIdMap"); |
455 | 455 |
check(nrmap[n2] == 2 && nrmap(2) == n2, "Wrong RangeIdMap"); |
456 | 456 |
|
457 | 457 |
check(armap[a0] == 0 && armap(0) == a0, "Wrong RangeIdMap"); |
458 | 458 |
check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap"); |
459 | 459 |
check(armap[a2] == 2 && armap(2) == a2, "Wrong RangeIdMap"); |
460 | 460 |
check(armap[a3] == 3 && armap(3) == a3, "Wrong RangeIdMap"); |
461 | 461 |
|
462 | 462 |
check(nrmap.inverse()[0] == n0, "Wrong RangeIdMap::InverseMap"); |
463 | 463 |
check(armap.inverse()[0] == a0, "Wrong RangeIdMap::InverseMap"); |
464 | 464 |
|
465 | 465 |
gr.erase(n1); |
466 | 466 |
|
467 | 467 |
if (nrmap[n0] == 1) nrmap.swap(n0, n2); |
468 | 468 |
nrmap.swap(n2, n0); |
469 | 469 |
if (armap[a1] == 1) armap.swap(a1, a3); |
470 | 470 |
armap.swap(a3, a1); |
471 | 471 |
|
472 | 472 |
check(nrmap.size() == 2 && armap.size() == 2, |
473 | 473 |
"Wrong RangeIdMap::size()"); |
474 | 474 |
|
475 | 475 |
check(nrmap[n0] == 1 && nrmap(1) == n0, "Wrong RangeIdMap"); |
476 | 476 |
check(nrmap[n2] == 0 && nrmap(0) == n2, "Wrong RangeIdMap"); |
477 | 477 |
|
478 | 478 |
check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap"); |
479 | 479 |
check(armap[a3] == 0 && armap(0) == a3, "Wrong RangeIdMap"); |
480 | 480 |
|
481 | 481 |
check(nrmap.inverse()[0] == n2, "Wrong RangeIdMap::InverseMap"); |
482 | 482 |
check(armap.inverse()[0] == a3, "Wrong RangeIdMap::InverseMap"); |
483 | 483 |
} |
484 | 484 |
|
485 | 485 |
// SourceMap, TargetMap, ForwardMap, BackwardMap, InDegMap, OutDegMap |
486 | 486 |
{ |
487 | 487 |
typedef ListGraph Graph; |
488 | 488 |
GRAPH_TYPEDEFS(Graph); |
489 | 489 |
|
490 | 490 |
checkConcept<ReadMap<Arc, Node>, SourceMap<Graph> >(); |
491 | 491 |
checkConcept<ReadMap<Arc, Node>, TargetMap<Graph> >(); |
492 | 492 |
checkConcept<ReadMap<Edge, Arc>, ForwardMap<Graph> >(); |
493 | 493 |
checkConcept<ReadMap<Edge, Arc>, BackwardMap<Graph> >(); |
494 | 494 |
checkConcept<ReadMap<Node, int>, InDegMap<Graph> >(); |
495 | 495 |
checkConcept<ReadMap<Node, int>, OutDegMap<Graph> >(); |
496 | 496 |
|
497 | 497 |
Graph gr; |
498 | 498 |
Node n0 = gr.addNode(); |
499 | 499 |
Node n1 = gr.addNode(); |
500 | 500 |
Node n2 = gr.addNode(); |
501 | 501 |
|
502 | 502 |
gr.addEdge(n0,n1); |
503 | 503 |
gr.addEdge(n1,n2); |
504 | 504 |
gr.addEdge(n0,n2); |
505 | 505 |
gr.addEdge(n2,n1); |
506 | 506 |
gr.addEdge(n1,n2); |
507 | 507 |
gr.addEdge(n0,n1); |
508 | 508 |
|
509 | 509 |
for (EdgeIt e(gr); e != INVALID; ++e) { |
510 | 510 |
check(forwardMap(gr)[e] == gr.direct(e, true), "Wrong ForwardMap"); |
511 | 511 |
check(backwardMap(gr)[e] == gr.direct(e, false), "Wrong BackwardMap"); |
512 | 512 |
} |
513 | 513 |
|
514 | 514 |
check(mapCompare(gr, |
515 | 515 |
sourceMap(orienter(gr, constMap<Edge, bool>(true))), |
516 | 516 |
targetMap(orienter(gr, constMap<Edge, bool>(false)))), |
517 | 517 |
"Wrong SourceMap or TargetMap"); |
518 | 518 |
|
519 | 519 |
typedef Orienter<Graph, const ConstMap<Edge, bool> > Digraph; |
520 | 520 |
Digraph dgr(gr, constMap<Edge, bool>(true)); |
521 | 521 |
OutDegMap<Digraph> odm(dgr); |
522 | 522 |
InDegMap<Digraph> idm(dgr); |
523 | 523 |
|
524 | 524 |
check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 1, "Wrong OutDegMap"); |
525 | 525 |
check(idm[n0] == 0 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap"); |
526 | 526 |
|
527 | 527 |
gr.addEdge(n2, n0); |
528 | 528 |
|
529 | 529 |
check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 2, "Wrong OutDegMap"); |
530 | 530 |
check(idm[n0] == 1 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap"); |
531 | 531 |
} |
532 | 532 |
|
533 | 533 |
// CrossRefMap |
534 | 534 |
{ |
535 | 535 |
typedef ListDigraph Graph; |
536 | 536 |
DIGRAPH_TYPEDEFS(Graph); |
537 | 537 |
|
538 | 538 |
checkConcept<ReadWriteMap<Node, int>, |
539 | 539 |
CrossRefMap<Graph, Node, int> >(); |
540 | 540 |
checkConcept<ReadWriteMap<Node, bool>, |
541 | 541 |
CrossRefMap<Graph, Node, bool> >(); |
542 | 542 |
checkConcept<ReadWriteMap<Node, double>, |
543 | 543 |
CrossRefMap<Graph, Node, double> >(); |
544 | 544 |
|
545 | 545 |
Graph gr; |
546 | 546 |
typedef CrossRefMap<Graph, Node, char> CRMap; |
547 | 547 |
CRMap map(gr); |
548 | 548 |
|
549 | 549 |
Node n0 = gr.addNode(); |
550 | 550 |
Node n1 = gr.addNode(); |
551 | 551 |
Node n2 = gr.addNode(); |
552 | 552 |
|
553 | 553 |
map.set(n0, 'A'); |
554 | 554 |
map.set(n1, 'B'); |
555 | 555 |
map.set(n2, 'C'); |
556 | 556 |
|
557 | 557 |
check(map[n0] == 'A' && map('A') == n0 && map.inverse()['A'] == n0, |
558 | 558 |
"Wrong CrossRefMap"); |
559 | 559 |
check(map[n1] == 'B' && map('B') == n1 && map.inverse()['B'] == n1, |
560 | 560 |
"Wrong CrossRefMap"); |
561 | 561 |
check(map[n2] == 'C' && map('C') == n2 && map.inverse()['C'] == n2, |
562 | 562 |
"Wrong CrossRefMap"); |
563 | 563 |
check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1, |
564 | 564 |
"Wrong CrossRefMap::count()"); |
565 | 565 |
|
566 | 566 |
CRMap::ValueIt it = map.beginValue(); |
567 | 567 |
check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' && |
568 | 568 |
it == map.endValue(), "Wrong value iterator"); |
569 | 569 |
|
570 | 570 |
map.set(n2, 'A'); |
571 | 571 |
|
572 | 572 |
check(map[n0] == 'A' && map[n1] == 'B' && map[n2] == 'A', |
573 | 573 |
"Wrong CrossRefMap"); |
574 | 574 |
check(map('A') == n0 && map.inverse()['A'] == n0, "Wrong CrossRefMap"); |
575 | 575 |
check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap"); |
576 | 576 |
check(map('C') == INVALID && map.inverse()['C'] == INVALID, |
577 | 577 |
"Wrong CrossRefMap"); |
578 | 578 |
check(map.count('A') == 2 && map.count('B') == 1 && map.count('C') == 0, |
579 | 579 |
"Wrong CrossRefMap::count()"); |
580 | 580 |
|
581 | 581 |
it = map.beginValue(); |
582 | 582 |
check(*it++ == 'A' && *it++ == 'A' && *it++ == 'B' && |
583 | 583 |
it == map.endValue(), "Wrong value iterator"); |
584 | 584 |
|
585 | 585 |
map.set(n0, 'C'); |
586 | 586 |
|
587 | 587 |
check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A', |
588 | 588 |
"Wrong CrossRefMap"); |
589 | 589 |
check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap"); |
590 | 590 |
check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap"); |
591 | 591 |
check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap"); |
592 | 592 |
check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1, |
593 | 593 |
"Wrong CrossRefMap::count()"); |
594 | 594 |
|
595 | 595 |
it = map.beginValue(); |
596 | 596 |
check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' && |
597 | 597 |
it == map.endValue(), "Wrong value iterator"); |
598 | 598 |
} |
599 | 599 |
|
600 | 600 |
// CrossRefMap |
601 | 601 |
{ |
602 | 602 |
typedef SmartDigraph Graph; |
603 | 603 |
DIGRAPH_TYPEDEFS(Graph); |
604 | 604 |
|
605 | 605 |
checkConcept<ReadWriteMap<Node, int>, |
606 | 606 |
CrossRefMap<Graph, Node, int> >(); |
607 | 607 |
|
608 | 608 |
Graph gr; |
609 | 609 |
typedef CrossRefMap<Graph, Node, char> CRMap; |
610 | 610 |
typedef CRMap::ValueIterator ValueIt; |
611 | 611 |
CRMap map(gr); |
612 | 612 |
|
613 | 613 |
Node n0 = gr.addNode(); |
614 | 614 |
Node n1 = gr.addNode(); |
615 | 615 |
Node n2 = gr.addNode(); |
616 | 616 |
|
617 | 617 |
map.set(n0, 'A'); |
618 | 618 |
map.set(n1, 'B'); |
619 | 619 |
map.set(n2, 'C'); |
620 | 620 |
map.set(n2, 'A'); |
621 | 621 |
map.set(n0, 'C'); |
622 | 622 |
|
623 | 623 |
check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A', |
624 | 624 |
"Wrong CrossRefMap"); |
625 | 625 |
check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap"); |
626 | 626 |
check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap"); |
627 | 627 |
check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap"); |
628 | 628 |
|
629 | 629 |
ValueIt it = map.beginValue(); |
630 | 630 |
check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' && |
631 | 631 |
it == map.endValue(), "Wrong value iterator"); |
632 | 632 |
} |
633 | 633 |
|
634 | 634 |
// Iterable bool map |
635 | 635 |
{ |
636 | 636 |
typedef SmartGraph Graph; |
637 | 637 |
typedef SmartGraph::Node Item; |
638 | 638 |
|
639 | 639 |
typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm; |
640 | 640 |
checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>(); |
641 | 641 |
|
642 | 642 |
const int num = 10; |
643 | 643 |
Graph g; |
644 |
Ibm map0(g, true); |
|
644 | 645 |
std::vector<Item> items; |
645 | 646 |
for (int i = 0; i < num; ++i) { |
646 | 647 |
items.push_back(g.addNode()); |
647 | 648 |
} |
648 | 649 |
|
649 | 650 |
Ibm map1(g, true); |
650 | 651 |
int n = 0; |
651 | 652 |
for (Ibm::TrueIt it(map1); it != INVALID; ++it) { |
652 | 653 |
check(map1[static_cast<Item>(it)], "Wrong TrueIt"); |
653 | 654 |
++n; |
654 | 655 |
} |
655 | 656 |
check(n == num, "Wrong number"); |
656 | 657 |
|
657 | 658 |
n = 0; |
658 | 659 |
for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) { |
659 | 660 |
check(map1[static_cast<Item>(it)], "Wrong ItemIt for true"); |
660 | 661 |
++n; |
661 | 662 |
} |
662 | 663 |
check(n == num, "Wrong number"); |
663 | 664 |
check(Ibm::FalseIt(map1) == INVALID, "Wrong FalseIt"); |
664 | 665 |
check(Ibm::ItemIt(map1, false) == INVALID, "Wrong ItemIt for false"); |
665 | 666 |
|
666 | 667 |
map1[items[5]] = true; |
667 | 668 |
|
668 | 669 |
n = 0; |
669 | 670 |
for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) { |
670 | 671 |
check(map1[static_cast<Item>(it)], "Wrong ItemIt for true"); |
671 | 672 |
++n; |
672 | 673 |
} |
673 | 674 |
check(n == num, "Wrong number"); |
674 | 675 |
|
675 | 676 |
map1[items[num / 2]] = false; |
676 | 677 |
check(map1[items[num / 2]] == false, "Wrong map value"); |
677 | 678 |
|
678 | 679 |
n = 0; |
679 | 680 |
for (Ibm::TrueIt it(map1); it != INVALID; ++it) { |
680 | 681 |
check(map1[static_cast<Item>(it)], "Wrong TrueIt for true"); |
681 | 682 |
++n; |
682 | 683 |
} |
683 | 684 |
check(n == num - 1, "Wrong number"); |
684 | 685 |
|
685 | 686 |
n = 0; |
686 | 687 |
for (Ibm::FalseIt it(map1); it != INVALID; ++it) { |
687 | 688 |
check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true"); |
688 | 689 |
++n; |
689 | 690 |
} |
690 | 691 |
check(n == 1, "Wrong number"); |
691 | 692 |
|
692 | 693 |
map1[items[0]] = false; |
693 | 694 |
check(map1[items[0]] == false, "Wrong map value"); |
694 | 695 |
|
695 | 696 |
map1[items[num - 1]] = false; |
696 | 697 |
check(map1[items[num - 1]] == false, "Wrong map value"); |
697 | 698 |
|
698 | 699 |
n = 0; |
699 | 700 |
for (Ibm::TrueIt it(map1); it != INVALID; ++it) { |
700 | 701 |
check(map1[static_cast<Item>(it)], "Wrong TrueIt for true"); |
701 | 702 |
++n; |
702 | 703 |
} |
703 | 704 |
check(n == num - 3, "Wrong number"); |
704 | 705 |
check(map1.trueNum() == num - 3, "Wrong number"); |
705 | 706 |
|
706 | 707 |
n = 0; |
707 | 708 |
for (Ibm::FalseIt it(map1); it != INVALID; ++it) { |
708 | 709 |
check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true"); |
709 | 710 |
++n; |
710 | 711 |
} |
711 | 712 |
check(n == 3, "Wrong number"); |
712 | 713 |
check(map1.falseNum() == 3, "Wrong number"); |
713 | 714 |
} |
714 | 715 |
|
715 | 716 |
// Iterable int map |
716 | 717 |
{ |
717 | 718 |
typedef SmartGraph Graph; |
718 | 719 |
typedef SmartGraph::Node Item; |
719 | 720 |
typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim; |
720 | 721 |
|
721 | 722 |
checkConcept<ReferenceMap<Item, int, int&, const int&>, Iim>(); |
722 | 723 |
|
723 | 724 |
const int num = 10; |
724 | 725 |
Graph g; |
726 |
Iim map0(g, 0); |
|
725 | 727 |
std::vector<Item> items; |
726 | 728 |
for (int i = 0; i < num; ++i) { |
727 | 729 |
items.push_back(g.addNode()); |
728 | 730 |
} |
729 | 731 |
|
730 | 732 |
Iim map1(g); |
731 | 733 |
check(map1.size() == 0, "Wrong size"); |
732 | 734 |
|
733 | 735 |
for (int i = 0; i < num; ++i) { |
734 | 736 |
map1[items[i]] = i; |
735 | 737 |
} |
736 | 738 |
check(map1.size() == num, "Wrong size"); |
737 | 739 |
|
738 | 740 |
for (int i = 0; i < num; ++i) { |
739 | 741 |
Iim::ItemIt it(map1, i); |
740 | 742 |
check(static_cast<Item>(it) == items[i], "Wrong value"); |
741 | 743 |
++it; |
742 | 744 |
check(static_cast<Item>(it) == INVALID, "Wrong value"); |
743 | 745 |
} |
744 | 746 |
|
745 | 747 |
for (int i = 0; i < num; ++i) { |
746 | 748 |
map1[items[i]] = i % 2; |
747 | 749 |
} |
748 | 750 |
check(map1.size() == 2, "Wrong size"); |
749 | 751 |
|
750 | 752 |
int n = 0; |
751 | 753 |
for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) { |
752 | 754 |
check(map1[static_cast<Item>(it)] == 0, "Wrong value"); |
753 | 755 |
++n; |
754 | 756 |
} |
755 | 757 |
check(n == (num + 1) / 2, "Wrong number"); |
756 | 758 |
|
757 | 759 |
for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) { |
758 | 760 |
check(map1[static_cast<Item>(it)] == 1, "Wrong value"); |
759 | 761 |
++n; |
760 | 762 |
} |
761 | 763 |
check(n == num, "Wrong number"); |
762 | 764 |
|
763 | 765 |
} |
764 | 766 |
|
765 | 767 |
// Iterable value map |
766 | 768 |
{ |
767 | 769 |
typedef SmartGraph Graph; |
768 | 770 |
typedef SmartGraph::Node Item; |
769 | 771 |
typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm; |
770 | 772 |
|
771 | 773 |
checkConcept<ReadWriteMap<Item, double>, Ivm>(); |
772 | 774 |
|
773 | 775 |
const int num = 10; |
774 | 776 |
Graph g; |
777 |
Ivm map0(g, 0.0); |
|
775 | 778 |
std::vector<Item> items; |
776 | 779 |
for (int i = 0; i < num; ++i) { |
777 | 780 |
items.push_back(g.addNode()); |
778 | 781 |
} |
779 | 782 |
|
780 | 783 |
Ivm map1(g, 0.0); |
781 | 784 |
check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size"); |
782 | 785 |
check(*map1.beginValue() == 0.0, "Wrong value"); |
783 | 786 |
|
784 | 787 |
for (int i = 0; i < num; ++i) { |
785 | 788 |
map1.set(items[i], static_cast<double>(i)); |
786 | 789 |
} |
787 | 790 |
check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size"); |
788 | 791 |
|
789 | 792 |
for (int i = 0; i < num; ++i) { |
790 | 793 |
Ivm::ItemIt it(map1, static_cast<double>(i)); |
791 | 794 |
check(static_cast<Item>(it) == items[i], "Wrong value"); |
792 | 795 |
++it; |
793 | 796 |
check(static_cast<Item>(it) == INVALID, "Wrong value"); |
794 | 797 |
} |
795 | 798 |
|
796 | 799 |
for (Ivm::ValueIt vit = map1.beginValue(); |
797 | 800 |
vit != map1.endValue(); ++vit) { |
798 | 801 |
check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit, |
799 | 802 |
"Wrong ValueIt"); |
800 | 803 |
} |
801 | 804 |
|
802 | 805 |
for (int i = 0; i < num; ++i) { |
803 | 806 |
map1.set(items[i], static_cast<double>(i % 2)); |
804 | 807 |
} |
805 | 808 |
check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size"); |
806 | 809 |
|
807 | 810 |
int n = 0; |
808 | 811 |
for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) { |
809 | 812 |
check(map1[static_cast<Item>(it)] == 0.0, "Wrong value"); |
810 | 813 |
++n; |
811 | 814 |
} |
812 | 815 |
check(n == (num + 1) / 2, "Wrong number"); |
813 | 816 |
|
814 | 817 |
for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) { |
815 | 818 |
check(map1[static_cast<Item>(it)] == 1.0, "Wrong value"); |
816 | 819 |
++n; |
817 | 820 |
} |
818 | 821 |
check(n == num, "Wrong number"); |
819 | 822 |
|
820 | 823 |
} |
821 | 824 |
|
822 | 825 |
// Graph map utilities: |
823 | 826 |
// mapMin(), mapMax(), mapMinValue(), mapMaxValue() |
824 | 827 |
// mapFind(), mapFindIf(), mapCount(), mapCountIf() |
825 | 828 |
// mapCopy(), mapCompare(), mapFill() |
826 | 829 |
{ |
827 | 830 |
DIGRAPH_TYPEDEFS(SmartDigraph); |
828 | 831 |
|
829 | 832 |
SmartDigraph g; |
830 | 833 |
Node n1 = g.addNode(); |
831 | 834 |
Node n2 = g.addNode(); |
832 | 835 |
Node n3 = g.addNode(); |
833 | 836 |
|
834 | 837 |
SmartDigraph::NodeMap<int> map1(g); |
835 | 838 |
SmartDigraph::ArcMap<char> map2(g); |
836 | 839 |
ConstMap<Node, A> cmap1 = A(); |
837 | 840 |
ConstMap<Arc, C> cmap2 = C(0); |
838 | 841 |
|
839 | 842 |
map1[n1] = 10; |
840 | 843 |
map1[n2] = 5; |
841 | 844 |
map1[n3] = 12; |
842 | 845 |
|
843 | 846 |
// mapMin(), mapMax(), mapMinValue(), mapMaxValue() |
844 | 847 |
check(mapMin(g, map1) == n2, "Wrong mapMin()"); |
845 | 848 |
check(mapMax(g, map1) == n3, "Wrong mapMax()"); |
846 | 849 |
check(mapMin(g, map1, std::greater<int>()) == n3, "Wrong mapMin()"); |
847 | 850 |
check(mapMax(g, map1, std::greater<int>()) == n2, "Wrong mapMax()"); |
848 | 851 |
check(mapMinValue(g, map1) == 5, "Wrong mapMinValue()"); |
849 | 852 |
check(mapMaxValue(g, map1) == 12, "Wrong mapMaxValue()"); |
850 | 853 |
|
851 | 854 |
check(mapMin(g, map2) == INVALID, "Wrong mapMin()"); |
852 | 855 |
check(mapMax(g, map2) == INVALID, "Wrong mapMax()"); |
853 | 856 |
|
854 | 857 |
check(mapMin(g, cmap1) != INVALID, "Wrong mapMin()"); |
855 | 858 |
check(mapMax(g, cmap2) == INVALID, "Wrong mapMax()"); |
856 | 859 |
|
857 | 860 |
Arc a1 = g.addArc(n1, n2); |
858 | 861 |
Arc a2 = g.addArc(n1, n3); |
859 | 862 |
Arc a3 = g.addArc(n2, n3); |
860 | 863 |
Arc a4 = g.addArc(n3, n1); |
861 | 864 |
|
862 | 865 |
map2[a1] = 'b'; |
863 | 866 |
map2[a2] = 'a'; |
864 | 867 |
map2[a3] = 'b'; |
865 | 868 |
map2[a4] = 'c'; |
866 | 869 |
|
867 | 870 |
// mapMin(), mapMax(), mapMinValue(), mapMaxValue() |
868 | 871 |
check(mapMin(g, map2) == a2, "Wrong mapMin()"); |
869 | 872 |
check(mapMax(g, map2) == a4, "Wrong mapMax()"); |
870 | 873 |
check(mapMin(g, map2, std::greater<int>()) == a4, "Wrong mapMin()"); |
871 | 874 |
check(mapMax(g, map2, std::greater<int>()) == a2, "Wrong mapMax()"); |
872 | 875 |
check(mapMinValue(g, map2, std::greater<int>()) == 'c', |
873 | 876 |
"Wrong mapMinValue()"); |
874 | 877 |
check(mapMaxValue(g, map2, std::greater<int>()) == 'a', |
875 | 878 |
"Wrong mapMaxValue()"); |
876 | 879 |
|
877 | 880 |
check(mapMin(g, cmap1) != INVALID, "Wrong mapMin()"); |
878 | 881 |
check(mapMax(g, cmap2) != INVALID, "Wrong mapMax()"); |
879 | 882 |
check(mapMaxValue(g, cmap2) == C(0), "Wrong mapMaxValue()"); |
880 | 883 |
|
881 | 884 |
check(mapMin(g, composeMap(functorToMap(&createC), map2)) == a2, |
882 | 885 |
"Wrong mapMin()"); |
883 | 886 |
check(mapMax(g, composeMap(functorToMap(&createC), map2)) == a4, |
884 | 887 |
"Wrong mapMax()"); |
885 | 888 |
check(mapMinValue(g, composeMap(functorToMap(&createC), map2)) == C('a'), |
886 | 889 |
"Wrong mapMinValue()"); |
887 | 890 |
check(mapMaxValue(g, composeMap(functorToMap(&createC), map2)) == C('c'), |
888 | 891 |
"Wrong mapMaxValue()"); |
889 | 892 |
|
890 | 893 |
// mapFind(), mapFindIf() |
891 | 894 |
check(mapFind(g, map1, 5) == n2, "Wrong mapFind()"); |
892 | 895 |
check(mapFind(g, map1, 6) == INVALID, "Wrong mapFind()"); |
893 | 896 |
check(mapFind(g, map2, 'a') == a2, "Wrong mapFind()"); |
894 | 897 |
check(mapFind(g, map2, 'e') == INVALID, "Wrong mapFind()"); |
895 | 898 |
check(mapFind(g, cmap2, C(0)) == ArcIt(g), "Wrong mapFind()"); |
896 | 899 |
check(mapFind(g, cmap2, C(1)) == INVALID, "Wrong mapFind()"); |
897 | 900 |
|
898 | 901 |
check(mapFindIf(g, map1, Less<int>(7)) == n2, |
899 | 902 |
"Wrong mapFindIf()"); |
900 | 903 |
check(mapFindIf(g, map1, Less<int>(5)) == INVALID, |
901 | 904 |
"Wrong mapFindIf()"); |
902 | 905 |
check(mapFindIf(g, map2, Less<char>('d')) == ArcIt(g), |
903 | 906 |
"Wrong mapFindIf()"); |
904 | 907 |
check(mapFindIf(g, map2, Less<char>('a')) == INVALID, |
905 | 908 |
"Wrong mapFindIf()"); |
906 | 909 |
|
907 | 910 |
// mapCount(), mapCountIf() |
908 | 911 |
check(mapCount(g, map1, 5) == 1, "Wrong mapCount()"); |
909 | 912 |
check(mapCount(g, map1, 6) == 0, "Wrong mapCount()"); |
910 | 913 |
check(mapCount(g, map2, 'a') == 1, "Wrong mapCount()"); |
911 | 914 |
check(mapCount(g, map2, 'b') == 2, "Wrong mapCount()"); |
912 | 915 |
check(mapCount(g, map2, 'e') == 0, "Wrong mapCount()"); |
913 | 916 |
check(mapCount(g, cmap2, C(0)) == 4, "Wrong mapCount()"); |
914 | 917 |
check(mapCount(g, cmap2, C(1)) == 0, "Wrong mapCount()"); |
915 | 918 |
|
916 | 919 |
check(mapCountIf(g, map1, Less<int>(11)) == 2, |
917 | 920 |
"Wrong mapCountIf()"); |
918 | 921 |
check(mapCountIf(g, map1, Less<int>(13)) == 3, |
919 | 922 |
"Wrong mapCountIf()"); |
920 | 923 |
check(mapCountIf(g, map1, Less<int>(5)) == 0, |
921 | 924 |
"Wrong mapCountIf()"); |
922 | 925 |
check(mapCountIf(g, map2, Less<char>('d')) == 4, |
923 | 926 |
"Wrong mapCountIf()"); |
924 | 927 |
check(mapCountIf(g, map2, Less<char>('c')) == 3, |
925 | 928 |
"Wrong mapCountIf()"); |
926 | 929 |
check(mapCountIf(g, map2, Less<char>('a')) == 0, |
927 | 930 |
"Wrong mapCountIf()"); |
928 | 931 |
|
929 | 932 |
// MapIt, ConstMapIt |
930 | 933 |
/* |
931 | 934 |
These tests can be used after applying bugfix #330 |
932 | 935 |
typedef SmartDigraph::NodeMap<int>::MapIt MapIt; |
933 | 936 |
typedef SmartDigraph::NodeMap<int>::ConstMapIt ConstMapIt; |
934 | 937 |
check(*std::min_element(MapIt(map1), MapIt(INVALID)) == 5, |
935 | 938 |
"Wrong NodeMap<>::MapIt"); |
936 | 939 |
check(*std::max_element(ConstMapIt(map1), ConstMapIt(INVALID)) == 12, |
937 | 940 |
"Wrong NodeMap<>::MapIt"); |
938 | 941 |
|
939 | 942 |
int sum = 0; |
940 | 943 |
std::for_each(MapIt(map1), MapIt(INVALID), Sum<int>(sum)); |
941 | 944 |
check(sum == 27, "Wrong NodeMap<>::MapIt"); |
942 | 945 |
std::for_each(ConstMapIt(map1), ConstMapIt(INVALID), Sum<int>(sum)); |
943 | 946 |
check(sum == 54, "Wrong NodeMap<>::ConstMapIt"); |
944 | 947 |
*/ |
945 | 948 |
|
946 | 949 |
// mapCopy(), mapCompare(), mapFill() |
947 | 950 |
check(mapCompare(g, map1, map1), "Wrong mapCompare()"); |
948 | 951 |
check(mapCompare(g, cmap2, cmap2), "Wrong mapCompare()"); |
949 | 952 |
check(mapCompare(g, map1, shiftMap(map1, 0)), "Wrong mapCompare()"); |
950 | 953 |
check(mapCompare(g, map2, scaleMap(map2, 1)), "Wrong mapCompare()"); |
951 | 954 |
check(!mapCompare(g, map1, shiftMap(map1, 1)), "Wrong mapCompare()"); |
952 | 955 |
|
953 | 956 |
SmartDigraph::NodeMap<int> map3(g, 0); |
954 | 957 |
SmartDigraph::ArcMap<char> map4(g, 'a'); |
955 | 958 |
|
956 | 959 |
check(!mapCompare(g, map1, map3), "Wrong mapCompare()"); |
957 | 960 |
check(!mapCompare(g, map2, map4), "Wrong mapCompare()"); |
958 | 961 |
|
959 | 962 |
mapCopy(g, map1, map3); |
960 | 963 |
mapCopy(g, map2, map4); |
961 | 964 |
|
962 | 965 |
check(mapCompare(g, map1, map3), "Wrong mapCompare() or mapCopy()"); |
963 | 966 |
check(mapCompare(g, map2, map4), "Wrong mapCompare() or mapCopy()"); |
964 | 967 |
|
965 | 968 |
Undirector<SmartDigraph> ug(g); |
966 | 969 |
Undirector<SmartDigraph>::EdgeMap<char> umap1(ug, 'x'); |
967 | 970 |
Undirector<SmartDigraph>::ArcMap<double> umap2(ug, 3.14); |
968 | 971 |
|
969 | 972 |
check(!mapCompare(g, map2, umap1), "Wrong mapCompare() or mapCopy()"); |
970 | 973 |
check(!mapCompare(g, umap1, map2), "Wrong mapCompare() or mapCopy()"); |
971 | 974 |
check(!mapCompare(ug, map2, umap1), "Wrong mapCompare() or mapCopy()"); |
972 | 975 |
check(!mapCompare(ug, umap1, map2), "Wrong mapCompare() or mapCopy()"); |
973 | 976 |
|
974 | 977 |
mapCopy(g, map2, umap1); |
975 | 978 |
|
976 | 979 |
check(mapCompare(g, map2, umap1), "Wrong mapCompare() or mapCopy()"); |
977 | 980 |
check(mapCompare(g, umap1, map2), "Wrong mapCompare() or mapCopy()"); |
978 | 981 |
check(mapCompare(ug, map2, umap1), "Wrong mapCompare() or mapCopy()"); |
979 | 982 |
check(mapCompare(ug, umap1, map2), "Wrong mapCompare() or mapCopy()"); |
980 | 983 |
|
981 | 984 |
mapCopy(g, map2, umap1); |
982 | 985 |
mapCopy(g, umap1, map2); |
983 | 986 |
mapCopy(ug, map2, umap1); |
984 | 987 |
mapCopy(ug, umap1, map2); |
985 | 988 |
|
986 | 989 |
check(!mapCompare(ug, umap1, umap2), "Wrong mapCompare() or mapCopy()"); |
987 | 990 |
mapCopy(ug, umap1, umap2); |
988 | 991 |
check(mapCompare(ug, umap1, umap2), "Wrong mapCompare() or mapCopy()"); |
989 | 992 |
|
990 | 993 |
check(!mapCompare(g, map1, constMap<Node>(2)), "Wrong mapCompare()"); |
991 | 994 |
mapFill(g, map1, 2); |
992 | 995 |
check(mapCompare(g, constMap<Node>(2), map1), "Wrong mapFill()"); |
993 | 996 |
|
994 | 997 |
check(!mapCompare(g, map2, constMap<Arc>('z')), "Wrong mapCompare()"); |
995 | 998 |
mapCopy(g, constMap<Arc>('z'), map2); |
996 | 999 |
check(mapCompare(g, constMap<Arc>('z'), map2), "Wrong mapCopy()"); |
997 | 1000 |
} |
998 | 1001 |
|
999 | 1002 |
return 0; |
1000 | 1003 |
} |
0 comments (0 inline)