gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Include /lemon/config.h globally (#230)
0 2 0
default
2 files changed with 2 insertions and 0 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
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
#ifndef LEMON_BITS_DEFAULT_MAP_H
20 20
#define LEMON_BITS_DEFAULT_MAP_H
21 21

	
22
#include <lemon/config.h>
22 23
#include <lemon/bits/array_map.h>
23 24
#include <lemon/bits/vector_map.h>
24 25
//#include <lemon/bits/debug_map.h>
25 26

	
26 27
//\ingroup graphbits
27 28
//\file
28 29
//\brief Graph maps that construct and destruct their elements dynamically.
29 30

	
30 31
namespace lemon {
31 32

	
32 33

	
33 34
  //#ifndef LEMON_USE_DEBUG_MAP
34 35

	
35 36
  template <typename _Graph, typename _Item, typename _Value>
36 37
  struct DefaultMapSelector {
37 38
    typedef ArrayMap<_Graph, _Item, _Value> Map;
38 39
  };
39 40

	
40 41
  // bool
41 42
  template <typename _Graph, typename _Item>
42 43
  struct DefaultMapSelector<_Graph, _Item, bool> {
43 44
    typedef VectorMap<_Graph, _Item, bool> Map;
44 45
  };
45 46

	
46 47
  // char
47 48
  template <typename _Graph, typename _Item>
48 49
  struct DefaultMapSelector<_Graph, _Item, char> {
49 50
    typedef VectorMap<_Graph, _Item, char> Map;
50 51
  };
51 52

	
52 53
  template <typename _Graph, typename _Item>
53 54
  struct DefaultMapSelector<_Graph, _Item, signed char> {
54 55
    typedef VectorMap<_Graph, _Item, signed char> Map;
55 56
  };
56 57

	
57 58
  template <typename _Graph, typename _Item>
58 59
  struct DefaultMapSelector<_Graph, _Item, unsigned char> {
59 60
    typedef VectorMap<_Graph, _Item, unsigned char> Map;
60 61
  };
61 62

	
62 63

	
63 64
  // int
64 65
  template <typename _Graph, typename _Item>
65 66
  struct DefaultMapSelector<_Graph, _Item, signed int> {
66 67
    typedef VectorMap<_Graph, _Item, signed int> Map;
67 68
  };
68 69

	
69 70
  template <typename _Graph, typename _Item>
70 71
  struct DefaultMapSelector<_Graph, _Item, unsigned int> {
71 72
    typedef VectorMap<_Graph, _Item, unsigned int> Map;
72 73
  };
73 74

	
74 75

	
75 76
  // short
76 77
  template <typename _Graph, typename _Item>
77 78
  struct DefaultMapSelector<_Graph, _Item, signed short> {
78 79
    typedef VectorMap<_Graph, _Item, signed short> Map;
79 80
  };
80 81

	
81 82
  template <typename _Graph, typename _Item>
82 83
  struct DefaultMapSelector<_Graph, _Item, unsigned short> {
83 84
    typedef VectorMap<_Graph, _Item, unsigned short> Map;
84 85
  };
85 86

	
86 87

	
87 88
  // long
88 89
  template <typename _Graph, typename _Item>
89 90
  struct DefaultMapSelector<_Graph, _Item, signed long> {
90 91
    typedef VectorMap<_Graph, _Item, signed long> Map;
91 92
  };
92 93

	
93 94
  template <typename _Graph, typename _Item>
94 95
  struct DefaultMapSelector<_Graph, _Item, unsigned long> {
95 96
    typedef VectorMap<_Graph, _Item, unsigned long> Map;
96 97
  };
97 98

	
98 99

	
99 100
#if defined HAVE_LONG_LONG
100 101

	
101 102
  // long long
102 103
  template <typename _Graph, typename _Item>
103 104
  struct DefaultMapSelector<_Graph, _Item, signed long long> {
104 105
    typedef VectorMap<_Graph, _Item, signed long long> Map;
105 106
  };
106 107

	
107 108
  template <typename _Graph, typename _Item>
108 109
  struct DefaultMapSelector<_Graph, _Item, unsigned long long> {
109 110
    typedef VectorMap<_Graph, _Item, unsigned long long> Map;
110 111
  };
111 112

	
112 113
#endif
113 114

	
114 115

	
115 116
  // float
116 117
  template <typename _Graph, typename _Item>
117 118
  struct DefaultMapSelector<_Graph, _Item, float> {
118 119
    typedef VectorMap<_Graph, _Item, float> Map;
119 120
  };
120 121

	
121 122

	
122 123
  // double
123 124
  template <typename _Graph, typename _Item>
124 125
  struct DefaultMapSelector<_Graph, _Item, double> {
125 126
    typedef VectorMap<_Graph, _Item,  double> Map;
126 127
  };
127 128

	
128 129

	
129 130
  // long double
130 131
  template <typename _Graph, typename _Item>
131 132
  struct DefaultMapSelector<_Graph, _Item, long double> {
132 133
    typedef VectorMap<_Graph, _Item, long double> Map;
133 134
  };
134 135

	
135 136

	
136 137
  // pointer
137 138
  template <typename _Graph, typename _Item, typename _Ptr>
138 139
  struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
139 140
    typedef VectorMap<_Graph, _Item, _Ptr*> Map;
140 141
  };
141 142

	
142 143
// #else
143 144

	
144 145
//   template <typename _Graph, typename _Item, typename _Value>
145 146
//   struct DefaultMapSelector {
146 147
//     typedef DebugMap<_Graph, _Item, _Value> Map;
147 148
//   };
148 149

	
149 150
// #endif
150 151

	
151 152
  // DefaultMap class
152 153
  template <typename _Graph, typename _Item, typename _Value>
153 154
  class DefaultMap
154 155
    : public DefaultMapSelector<_Graph, _Item, _Value>::Map {
155 156
  public:
156 157
    typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent;
157 158
    typedef DefaultMap<_Graph, _Item, _Value> Map;
158 159

	
159 160
    typedef typename Parent::Graph Graph;
160 161
    typedef typename Parent::Value Value;
161 162

	
162 163
    explicit DefaultMap(const Graph& graph) : Parent(graph) {}
163 164
    DefaultMap(const Graph& graph, const Value& value)
164 165
      : Parent(graph, value) {}
165 166

	
166 167
    DefaultMap& operator=(const DefaultMap& cmap) {
167 168
      return operator=<DefaultMap>(cmap);
168 169
    }
169 170

	
170 171
    template <typename CMap>
171 172
    DefaultMap& operator=(const CMap& cmap) {
172 173
      Parent::operator=(cmap);
173 174
      return *this;
174 175
    }
175 176

	
176 177
  };
177 178

	
178 179
}
179 180

	
180 181
#endif
Ignore white space 4096 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
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
#ifndef LEMON_CORE_H
20 20
#define LEMON_CORE_H
21 21

	
22 22
#include <vector>
23 23
#include <algorithm>
24 24

	
25
#include <lemon/core.h>
25 26
#include <lemon/bits/enable_if.h>
26 27
#include <lemon/bits/traits.h>
27 28
#include <lemon/assert.h>
28 29

	
29 30
///\file
30 31
///\brief LEMON core utilities.
31 32
///
32 33
///This header file contains core utilities for LEMON.
33 34
///It is automatically included by all graph types, therefore it usually
34 35
///do not have to be included directly.
35 36

	
36 37
namespace lemon {
37 38

	
38 39
  /// \brief Dummy type to make it easier to create invalid iterators.
39 40
  ///
40 41
  /// Dummy type to make it easier to create invalid iterators.
41 42
  /// See \ref INVALID for the usage.
42 43
  struct Invalid {
43 44
  public:
44 45
    bool operator==(Invalid) { return true;  }
45 46
    bool operator!=(Invalid) { return false; }
46 47
    bool operator< (Invalid) { return false; }
47 48
  };
48 49

	
49 50
  /// \brief Invalid iterators.
50 51
  ///
51 52
  /// \ref Invalid is a global type that converts to each iterator
52 53
  /// in such a way that the value of the target iterator will be invalid.
53 54
#ifdef LEMON_ONLY_TEMPLATES
54 55
  const Invalid INVALID = Invalid();
55 56
#else
56 57
  extern const Invalid INVALID;
57 58
#endif
58 59

	
59 60
  /// \addtogroup gutils
60 61
  /// @{
61 62

	
62 63
  ///Create convenience typedefs for the digraph types and iterators
63 64

	
64 65
  ///This \c \#define creates convenient type definitions for the following
65 66
  ///types of \c Digraph: \c Node,  \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
66 67
  ///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
67 68
  ///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap.
68 69
  ///
69 70
  ///\note If the graph type is a dependent type, ie. the graph type depend
70 71
  ///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS()
71 72
  ///macro.
72 73
#define DIGRAPH_TYPEDEFS(Digraph)                                       \
73 74
  typedef Digraph::Node Node;                                           \
74 75
  typedef Digraph::NodeIt NodeIt;                                       \
75 76
  typedef Digraph::Arc Arc;                                             \
76 77
  typedef Digraph::ArcIt ArcIt;                                         \
77 78
  typedef Digraph::InArcIt InArcIt;                                     \
78 79
  typedef Digraph::OutArcIt OutArcIt;                                   \
79 80
  typedef Digraph::NodeMap<bool> BoolNodeMap;                           \
80 81
  typedef Digraph::NodeMap<int> IntNodeMap;                             \
81 82
  typedef Digraph::NodeMap<double> DoubleNodeMap;                       \
82 83
  typedef Digraph::ArcMap<bool> BoolArcMap;                             \
83 84
  typedef Digraph::ArcMap<int> IntArcMap;                               \
84 85
  typedef Digraph::ArcMap<double> DoubleArcMap
85 86

	
86 87
  ///Create convenience typedefs for the digraph types and iterators
87 88

	
88 89
  ///\see DIGRAPH_TYPEDEFS
89 90
  ///
90 91
  ///\note Use this macro, if the graph type is a dependent type,
91 92
  ///ie. the graph type depend on a template parameter.
92 93
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph)                              \
93 94
  typedef typename Digraph::Node Node;                                  \
94 95
  typedef typename Digraph::NodeIt NodeIt;                              \
95 96
  typedef typename Digraph::Arc Arc;                                    \
96 97
  typedef typename Digraph::ArcIt ArcIt;                                \
97 98
  typedef typename Digraph::InArcIt InArcIt;                            \
98 99
  typedef typename Digraph::OutArcIt OutArcIt;                          \
99 100
  typedef typename Digraph::template NodeMap<bool> BoolNodeMap;         \
100 101
  typedef typename Digraph::template NodeMap<int> IntNodeMap;           \
101 102
  typedef typename Digraph::template NodeMap<double> DoubleNodeMap;     \
102 103
  typedef typename Digraph::template ArcMap<bool> BoolArcMap;           \
103 104
  typedef typename Digraph::template ArcMap<int> IntArcMap;             \
104 105
  typedef typename Digraph::template ArcMap<double> DoubleArcMap
105 106

	
106 107
  ///Create convenience typedefs for the graph types and iterators
107 108

	
108 109
  ///This \c \#define creates the same convenient type definitions as defined
109 110
  ///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
110 111
  ///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
111 112
  ///\c DoubleEdgeMap.
112 113
  ///
113 114
  ///\note If the graph type is a dependent type, ie. the graph type depend
114 115
  ///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS()
115 116
  ///macro.
116 117
#define GRAPH_TYPEDEFS(Graph)                                           \
117 118
  DIGRAPH_TYPEDEFS(Graph);                                              \
118 119
  typedef Graph::Edge Edge;                                             \
119 120
  typedef Graph::EdgeIt EdgeIt;                                         \
120 121
  typedef Graph::IncEdgeIt IncEdgeIt;                                   \
121 122
  typedef Graph::EdgeMap<bool> BoolEdgeMap;                             \
122 123
  typedef Graph::EdgeMap<int> IntEdgeMap;                               \
123 124
  typedef Graph::EdgeMap<double> DoubleEdgeMap
124 125

	
125 126
  ///Create convenience typedefs for the graph types and iterators
126 127

	
127 128
  ///\see GRAPH_TYPEDEFS
128 129
  ///
129 130
  ///\note Use this macro, if the graph type is a dependent type,
130 131
  ///ie. the graph type depend on a template parameter.
131 132
#define TEMPLATE_GRAPH_TYPEDEFS(Graph)                                  \
132 133
  TEMPLATE_DIGRAPH_TYPEDEFS(Graph);                                     \
133 134
  typedef typename Graph::Edge Edge;                                    \
134 135
  typedef typename Graph::EdgeIt EdgeIt;                                \
135 136
  typedef typename Graph::IncEdgeIt IncEdgeIt;                          \
136 137
  typedef typename Graph::template EdgeMap<bool> BoolEdgeMap;           \
137 138
  typedef typename Graph::template EdgeMap<int> IntEdgeMap;             \
138 139
  typedef typename Graph::template EdgeMap<double> DoubleEdgeMap
139 140

	
140 141
  /// \brief Function to count the items in a graph.
141 142
  ///
142 143
  /// This function counts the items (nodes, arcs etc.) in a graph.
143 144
  /// The complexity of the function is linear because
144 145
  /// it iterates on all of the items.
145 146
  template <typename Graph, typename Item>
146 147
  inline int countItems(const Graph& g) {
147 148
    typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
148 149
    int num = 0;
149 150
    for (ItemIt it(g); it != INVALID; ++it) {
150 151
      ++num;
151 152
    }
152 153
    return num;
153 154
  }
154 155

	
155 156
  // Node counting:
156 157

	
157 158
  namespace _core_bits {
158 159

	
159 160
    template <typename Graph, typename Enable = void>
160 161
    struct CountNodesSelector {
161 162
      static int count(const Graph &g) {
162 163
        return countItems<Graph, typename Graph::Node>(g);
163 164
      }
164 165
    };
165 166

	
166 167
    template <typename Graph>
167 168
    struct CountNodesSelector<
168 169
      Graph, typename
169 170
      enable_if<typename Graph::NodeNumTag, void>::type>
170 171
    {
171 172
      static int count(const Graph &g) {
172 173
        return g.nodeNum();
173 174
      }
174 175
    };
175 176
  }
176 177

	
177 178
  /// \brief Function to count the nodes in the graph.
178 179
  ///
179 180
  /// This function counts the nodes in the graph.
180 181
  /// The complexity of the function is <em>O</em>(<em>n</em>), but for some
181 182
  /// graph structures it is specialized to run in <em>O</em>(1).
182 183
  ///
183 184
  /// \note If the graph contains a \c nodeNum() member function and a
184 185
  /// \c NodeNumTag tag then this function calls directly the member
185 186
  /// function to query the cardinality of the node set.
186 187
  template <typename Graph>
187 188
  inline int countNodes(const Graph& g) {
188 189
    return _core_bits::CountNodesSelector<Graph>::count(g);
189 190
  }
190 191

	
191 192
  // Arc counting:
192 193

	
193 194
  namespace _core_bits {
194 195

	
195 196
    template <typename Graph, typename Enable = void>
196 197
    struct CountArcsSelector {
197 198
      static int count(const Graph &g) {
198 199
        return countItems<Graph, typename Graph::Arc>(g);
199 200
      }
200 201
    };
201 202

	
202 203
    template <typename Graph>
203 204
    struct CountArcsSelector<
204 205
      Graph,
205 206
      typename enable_if<typename Graph::ArcNumTag, void>::type>
206 207
    {
207 208
      static int count(const Graph &g) {
208 209
        return g.arcNum();
209 210
      }
210 211
    };
211 212
  }
212 213

	
213 214
  /// \brief Function to count the arcs in the graph.
214 215
  ///
215 216
  /// This function counts the arcs in the graph.
216 217
  /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
217 218
  /// graph structures it is specialized to run in <em>O</em>(1).
218 219
  ///
219 220
  /// \note If the graph contains a \c arcNum() member function and a
220 221
  /// \c ArcNumTag tag then this function calls directly the member
221 222
  /// function to query the cardinality of the arc set.
222 223
  template <typename Graph>
223 224
  inline int countArcs(const Graph& g) {
224 225
    return _core_bits::CountArcsSelector<Graph>::count(g);
225 226
  }
226 227

	
227 228
  // Edge counting:
228 229

	
229 230
  namespace _core_bits {
230 231

	
231 232
    template <typename Graph, typename Enable = void>
232 233
    struct CountEdgesSelector {
233 234
      static int count(const Graph &g) {
234 235
        return countItems<Graph, typename Graph::Edge>(g);
235 236
      }
236 237
    };
237 238

	
238 239
    template <typename Graph>
239 240
    struct CountEdgesSelector<
240 241
      Graph,
241 242
      typename enable_if<typename Graph::EdgeNumTag, void>::type>
242 243
    {
243 244
      static int count(const Graph &g) {
244 245
        return g.edgeNum();
245 246
      }
246 247
    };
247 248
  }
248 249

	
249 250
  /// \brief Function to count the edges in the graph.
250 251
  ///
251 252
  /// This function counts the edges in the graph.
252 253
  /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
253 254
  /// graph structures it is specialized to run in <em>O</em>(1).
254 255
  ///
255 256
  /// \note If the graph contains a \c edgeNum() member function and a
256 257
  /// \c EdgeNumTag tag then this function calls directly the member
257 258
  /// function to query the cardinality of the edge set.
258 259
  template <typename Graph>
259 260
  inline int countEdges(const Graph& g) {
260 261
    return _core_bits::CountEdgesSelector<Graph>::count(g);
261 262

	
262 263
  }
263 264

	
264 265

	
265 266
  template <typename Graph, typename DegIt>
266 267
  inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
267 268
    int num = 0;
268 269
    for (DegIt it(_g, _n); it != INVALID; ++it) {
269 270
      ++num;
270 271
    }
271 272
    return num;
272 273
  }
273 274

	
274 275
  /// \brief Function to count the number of the out-arcs from node \c n.
275 276
  ///
276 277
  /// This function counts the number of the out-arcs from node \c n
277 278
  /// in the graph \c g.
278 279
  template <typename Graph>
279 280
  inline int countOutArcs(const Graph& g,  const typename Graph::Node& n) {
280 281
    return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n);
281 282
  }
282 283

	
283 284
  /// \brief Function to count the number of the in-arcs to node \c n.
284 285
  ///
285 286
  /// This function counts the number of the in-arcs to node \c n
286 287
  /// in the graph \c g.
287 288
  template <typename Graph>
288 289
  inline int countInArcs(const Graph& g,  const typename Graph::Node& n) {
289 290
    return countNodeDegree<Graph, typename Graph::InArcIt>(g, n);
290 291
  }
291 292

	
292 293
  /// \brief Function to count the number of the inc-edges to node \c n.
293 294
  ///
294 295
  /// This function counts the number of the inc-edges to node \c n
295 296
  /// in the undirected graph \c g.
296 297
  template <typename Graph>
297 298
  inline int countIncEdges(const Graph& g,  const typename Graph::Node& n) {
298 299
    return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n);
299 300
  }
300 301

	
301 302
  namespace _core_bits {
302 303

	
303 304
    template <typename Digraph, typename Item, typename RefMap>
304 305
    class MapCopyBase {
305 306
    public:
306 307
      virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
307 308

	
308 309
      virtual ~MapCopyBase() {}
309 310
    };
310 311

	
311 312
    template <typename Digraph, typename Item, typename RefMap,
312 313
              typename FromMap, typename ToMap>
313 314
    class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
314 315
    public:
315 316

	
316 317
      MapCopy(const FromMap& map, ToMap& tmap)
317 318
        : _map(map), _tmap(tmap) {}
318 319

	
319 320
      virtual void copy(const Digraph& digraph, const RefMap& refMap) {
320 321
        typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
321 322
        for (ItemIt it(digraph); it != INVALID; ++it) {
322 323
          _tmap.set(refMap[it], _map[it]);
323 324
        }
324 325
      }
325 326

	
326 327
    private:
327 328
      const FromMap& _map;
328 329
      ToMap& _tmap;
329 330
    };
330 331

	
331 332
    template <typename Digraph, typename Item, typename RefMap, typename It>
332 333
    class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
333 334
    public:
334 335

	
335 336
      ItemCopy(const Item& item, It& it) : _item(item), _it(it) {}
336 337

	
337 338
      virtual void copy(const Digraph&, const RefMap& refMap) {
338 339
        _it = refMap[_item];
339 340
      }
340 341

	
341 342
    private:
342 343
      Item _item;
343 344
      It& _it;
344 345
    };
345 346

	
346 347
    template <typename Digraph, typename Item, typename RefMap, typename Ref>
347 348
    class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
348 349
    public:
349 350

	
350 351
      RefCopy(Ref& map) : _map(map) {}
351 352

	
352 353
      virtual void copy(const Digraph& digraph, const RefMap& refMap) {
353 354
        typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
354 355
        for (ItemIt it(digraph); it != INVALID; ++it) {
355 356
          _map.set(it, refMap[it]);
356 357
        }
357 358
      }
358 359

	
359 360
    private:
360 361
      Ref& _map;
361 362
    };
362 363

	
363 364
    template <typename Digraph, typename Item, typename RefMap,
364 365
              typename CrossRef>
365 366
    class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
366 367
    public:
367 368

	
368 369
      CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
369 370

	
370 371
      virtual void copy(const Digraph& digraph, const RefMap& refMap) {
371 372
        typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
372 373
        for (ItemIt it(digraph); it != INVALID; ++it) {
373 374
          _cmap.set(refMap[it], it);
374 375
        }
375 376
      }
376 377

	
377 378
    private:
378 379
      CrossRef& _cmap;
379 380
    };
380 381

	
381 382
    template <typename Digraph, typename Enable = void>
382 383
    struct DigraphCopySelector {
383 384
      template <typename From, typename NodeRefMap, typename ArcRefMap>
384 385
      static void copy(const From& from, Digraph &to,
385 386
                       NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
386 387
        for (typename From::NodeIt it(from); it != INVALID; ++it) {
387 388
          nodeRefMap[it] = to.addNode();
388 389
        }
389 390
        for (typename From::ArcIt it(from); it != INVALID; ++it) {
390 391
          arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
391 392
                                    nodeRefMap[from.target(it)]);
392 393
        }
393 394
      }
394 395
    };
395 396

	
396 397
    template <typename Digraph>
397 398
    struct DigraphCopySelector<
398 399
      Digraph,
399 400
      typename enable_if<typename Digraph::BuildTag, void>::type>
400 401
    {
401 402
      template <typename From, typename NodeRefMap, typename ArcRefMap>
402 403
      static void copy(const From& from, Digraph &to,
403 404
                       NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
404 405
        to.build(from, nodeRefMap, arcRefMap);
405 406
      }
406 407
    };
407 408

	
408 409
    template <typename Graph, typename Enable = void>
409 410
    struct GraphCopySelector {
410 411
      template <typename From, typename NodeRefMap, typename EdgeRefMap>
411 412
      static void copy(const From& from, Graph &to,
412 413
                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
413 414
        for (typename From::NodeIt it(from); it != INVALID; ++it) {
414 415
          nodeRefMap[it] = to.addNode();
415 416
        }
416 417
        for (typename From::EdgeIt it(from); it != INVALID; ++it) {
417 418
          edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)],
418 419
                                      nodeRefMap[from.v(it)]);
419 420
        }
420 421
      }
421 422
    };
422 423

	
423 424
    template <typename Graph>
424 425
    struct GraphCopySelector<
425 426
      Graph,
426 427
      typename enable_if<typename Graph::BuildTag, void>::type>
427 428
    {
428 429
      template <typename From, typename NodeRefMap, typename EdgeRefMap>
429 430
      static void copy(const From& from, Graph &to,
430 431
                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
431 432
        to.build(from, nodeRefMap, edgeRefMap);
432 433
      }
433 434
    };
434 435

	
435 436
  }
436 437

	
437 438
  /// \brief Class to copy a digraph.
438 439
  ///
439 440
  /// Class to copy a digraph to another digraph (duplicate a digraph). The
440 441
  /// simplest way of using it is through the \c digraphCopy() function.
441 442
  ///
442 443
  /// This class not only make a copy of a digraph, but it can create
443 444
  /// references and cross references between the nodes and arcs of
444 445
  /// the two digraphs, and it can copy maps to use with the newly created
445 446
  /// digraph.
446 447
  ///
447 448
  /// To make a copy from a digraph, first an instance of DigraphCopy
448 449
  /// should be created, then the data belongs to the digraph should
449 450
  /// assigned to copy. In the end, the \c run() member should be
450 451
  /// called.
451 452
  ///
452 453
  /// The next code copies a digraph with several data:
453 454
  ///\code
454 455
  ///  DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
455 456
  ///  // Create references for the nodes
456 457
  ///  OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
457 458
  ///  cg.nodeRef(nr);
458 459
  ///  // Create cross references (inverse) for the arcs
459 460
  ///  NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph);
460 461
  ///  cg.arcCrossRef(acr);
461 462
  ///  // Copy an arc map
462 463
  ///  OrigGraph::ArcMap<double> oamap(orig_graph);
463 464
  ///  NewGraph::ArcMap<double> namap(new_graph);
464 465
  ///  cg.arcMap(oamap, namap);
465 466
  ///  // Copy a node
466 467
  ///  OrigGraph::Node on;
467 468
  ///  NewGraph::Node nn;
468 469
  ///  cg.node(on, nn);
469 470
  ///  // Execute copying
470 471
  ///  cg.run();
471 472
  ///\endcode
472 473
  template <typename From, typename To>
473 474
  class DigraphCopy {
474 475
  private:
475 476

	
476 477
    typedef typename From::Node Node;
477 478
    typedef typename From::NodeIt NodeIt;
478 479
    typedef typename From::Arc Arc;
479 480
    typedef typename From::ArcIt ArcIt;
480 481

	
481 482
    typedef typename To::Node TNode;
482 483
    typedef typename To::Arc TArc;
483 484

	
484 485
    typedef typename From::template NodeMap<TNode> NodeRefMap;
485 486
    typedef typename From::template ArcMap<TArc> ArcRefMap;
486 487

	
487 488
  public:
488 489

	
489 490
    /// \brief Constructor of DigraphCopy.
490 491
    ///
491 492
    /// Constructor of DigraphCopy for copying the content of the
492 493
    /// \c from digraph into the \c to digraph.
493 494
    DigraphCopy(const From& from, To& to)
494 495
      : _from(from), _to(to) {}
495 496

	
496 497
    /// \brief Destructor of DigraphCopy
497 498
    ///
498 499
    /// Destructor of DigraphCopy.
499 500
    ~DigraphCopy() {
500 501
      for (int i = 0; i < int(_node_maps.size()); ++i) {
501 502
        delete _node_maps[i];
502 503
      }
503 504
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
504 505
        delete _arc_maps[i];
505 506
      }
506 507

	
507 508
    }
508 509

	
509 510
    /// \brief Copy the node references into the given map.
510 511
    ///
511 512
    /// This function copies the node references into the given map.
512 513
    /// The parameter should be a map, whose key type is the Node type of
513 514
    /// the source digraph, while the value type is the Node type of the
514 515
    /// destination digraph.
515 516
    template <typename NodeRef>
516 517
    DigraphCopy& nodeRef(NodeRef& map) {
517 518
      _node_maps.push_back(new _core_bits::RefCopy<From, Node,
518 519
                           NodeRefMap, NodeRef>(map));
519 520
      return *this;
520 521
    }
521 522

	
522 523
    /// \brief Copy the node cross references into the given map.
523 524
    ///
524 525
    /// This function copies the node cross references (reverse references)
525 526
    /// into the given map. The parameter should be a map, whose key type
526 527
    /// is the Node type of the destination digraph, while the value type is
527 528
    /// the Node type of the source digraph.
528 529
    template <typename NodeCrossRef>
529 530
    DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
530 531
      _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
531 532
                           NodeRefMap, NodeCrossRef>(map));
532 533
      return *this;
533 534
    }
534 535

	
535 536
    /// \brief Make a copy of the given node map.
536 537
    ///
537 538
    /// This function makes a copy of the given node map for the newly
538 539
    /// created digraph.
539 540
    /// The key type of the new map \c tmap should be the Node type of the
540 541
    /// destination digraph, and the key type of the original map \c map
541 542
    /// should be the Node type of the source digraph.
542 543
    template <typename FromMap, typename ToMap>
543 544
    DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
544 545
      _node_maps.push_back(new _core_bits::MapCopy<From, Node,
545 546
                           NodeRefMap, FromMap, ToMap>(map, tmap));
546 547
      return *this;
547 548
    }
548 549

	
549 550
    /// \brief Make a copy of the given node.
550 551
    ///
551 552
    /// This function makes a copy of the given node.
552 553
    DigraphCopy& node(const Node& node, TNode& tnode) {
553 554
      _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
554 555
                           NodeRefMap, TNode>(node, tnode));
555 556
      return *this;
556 557
    }
557 558

	
558 559
    /// \brief Copy the arc references into the given map.
559 560
    ///
560 561
    /// This function copies the arc references into the given map.
561 562
    /// The parameter should be a map, whose key type is the Arc type of
562 563
    /// the source digraph, while the value type is the Arc type of the
563 564
    /// destination digraph.
564 565
    template <typename ArcRef>
565 566
    DigraphCopy& arcRef(ArcRef& map) {
566 567
      _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
567 568
                          ArcRefMap, ArcRef>(map));
568 569
      return *this;
569 570
    }
570 571

	
571 572
    /// \brief Copy the arc cross references into the given map.
572 573
    ///
573 574
    /// This function copies the arc cross references (reverse references)
574 575
    /// into the given map. The parameter should be a map, whose key type
575 576
    /// is the Arc type of the destination digraph, while the value type is
576 577
    /// the Arc type of the source digraph.
577 578
    template <typename ArcCrossRef>
578 579
    DigraphCopy& arcCrossRef(ArcCrossRef& map) {
579 580
      _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
580 581
                          ArcRefMap, ArcCrossRef>(map));
581 582
      return *this;
582 583
    }
583 584

	
584 585
    /// \brief Make a copy of the given arc map.
585 586
    ///
586 587
    /// This function makes a copy of the given arc map for the newly
587 588
    /// created digraph.
588 589
    /// The key type of the new map \c tmap should be the Arc type of the
589 590
    /// destination digraph, and the key type of the original map \c map
590 591
    /// should be the Arc type of the source digraph.
591 592
    template <typename FromMap, typename ToMap>
592 593
    DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
593 594
      _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
594 595
                          ArcRefMap, FromMap, ToMap>(map, tmap));
595 596
      return *this;
596 597
    }
597 598

	
598 599
    /// \brief Make a copy of the given arc.
599 600
    ///
600 601
    /// This function makes a copy of the given arc.
601 602
    DigraphCopy& arc(const Arc& arc, TArc& tarc) {
602 603
      _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
603 604
                          ArcRefMap, TArc>(arc, tarc));
604 605
      return *this;
605 606
    }
606 607

	
607 608
    /// \brief Execute copying.
608 609
    ///
609 610
    /// This function executes the copying of the digraph along with the
610 611
    /// copying of the assigned data.
611 612
    void run() {
612 613
      NodeRefMap nodeRefMap(_from);
613 614
      ArcRefMap arcRefMap(_from);
614 615
      _core_bits::DigraphCopySelector<To>::
615 616
        copy(_from, _to, nodeRefMap, arcRefMap);
616 617
      for (int i = 0; i < int(_node_maps.size()); ++i) {
617 618
        _node_maps[i]->copy(_from, nodeRefMap);
618 619
      }
619 620
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
620 621
        _arc_maps[i]->copy(_from, arcRefMap);
621 622
      }
622 623
    }
623 624

	
624 625
  protected:
625 626

	
626 627
    const From& _from;
627 628
    To& _to;
628 629

	
629 630
    std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
630 631
      _node_maps;
631 632

	
632 633
    std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
633 634
      _arc_maps;
634 635

	
635 636
  };
636 637

	
637 638
  /// \brief Copy a digraph to another digraph.
638 639
  ///
639 640
  /// This function copies a digraph to another digraph.
640 641
  /// The complete usage of it is detailed in the DigraphCopy class, but
641 642
  /// a short example shows a basic work:
642 643
  ///\code
643 644
  /// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run();
644 645
  ///\endcode
645 646
  ///
646 647
  /// After the copy the \c nr map will contain the mapping from the
647 648
  /// nodes of the \c from digraph to the nodes of the \c to digraph and
648 649
  /// \c acr will contain the mapping from the arcs of the \c to digraph
649 650
  /// to the arcs of the \c from digraph.
650 651
  ///
651 652
  /// \see DigraphCopy
652 653
  template <typename From, typename To>
653 654
  DigraphCopy<From, To> digraphCopy(const From& from, To& to) {
654 655
    return DigraphCopy<From, To>(from, to);
655 656
  }
656 657

	
657 658
  /// \brief Class to copy a graph.
658 659
  ///
659 660
  /// Class to copy a graph to another graph (duplicate a graph). The
660 661
  /// simplest way of using it is through the \c graphCopy() function.
661 662
  ///
662 663
  /// This class not only make a copy of a graph, but it can create
663 664
  /// references and cross references between the nodes, edges and arcs of
664 665
  /// the two graphs, and it can copy maps for using with the newly created
665 666
  /// graph.
666 667
  ///
667 668
  /// To make a copy from a graph, first an instance of GraphCopy
668 669
  /// should be created, then the data belongs to the graph should
669 670
  /// assigned to copy. In the end, the \c run() member should be
670 671
  /// called.
671 672
  ///
672 673
  /// The next code copies a graph with several data:
673 674
  ///\code
674 675
  ///  GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
675 676
  ///  // Create references for the nodes
676 677
  ///  OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
677 678
  ///  cg.nodeRef(nr);
678 679
  ///  // Create cross references (inverse) for the edges
679 680
  ///  NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph);
680 681
  ///  cg.edgeCrossRef(ecr);
681 682
  ///  // Copy an edge map
682 683
  ///  OrigGraph::EdgeMap<double> oemap(orig_graph);
683 684
  ///  NewGraph::EdgeMap<double> nemap(new_graph);
684 685
  ///  cg.edgeMap(oemap, nemap);
685 686
  ///  // Copy a node
686 687
  ///  OrigGraph::Node on;
687 688
  ///  NewGraph::Node nn;
688 689
  ///  cg.node(on, nn);
689 690
  ///  // Execute copying
690 691
  ///  cg.run();
691 692
  ///\endcode
692 693
  template <typename From, typename To>
693 694
  class GraphCopy {
694 695
  private:
695 696

	
696 697
    typedef typename From::Node Node;
697 698
    typedef typename From::NodeIt NodeIt;
698 699
    typedef typename From::Arc Arc;
699 700
    typedef typename From::ArcIt ArcIt;
700 701
    typedef typename From::Edge Edge;
701 702
    typedef typename From::EdgeIt EdgeIt;
702 703

	
703 704
    typedef typename To::Node TNode;
704 705
    typedef typename To::Arc TArc;
705 706
    typedef typename To::Edge TEdge;
706 707

	
707 708
    typedef typename From::template NodeMap<TNode> NodeRefMap;
708 709
    typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
709 710

	
710 711
    struct ArcRefMap {
711 712
      ArcRefMap(const From& from, const To& to,
712 713
                const EdgeRefMap& edge_ref, const NodeRefMap& node_ref)
713 714
        : _from(from), _to(to),
714 715
          _edge_ref(edge_ref), _node_ref(node_ref) {}
715 716

	
716 717
      typedef typename From::Arc Key;
717 718
      typedef typename To::Arc Value;
718 719

	
719 720
      Value operator[](const Key& key) const {
720 721
        bool forward = _from.u(key) != _from.v(key) ?
721 722
          _node_ref[_from.source(key)] ==
722 723
          _to.source(_to.direct(_edge_ref[key], true)) :
723 724
          _from.direction(key);
724 725
        return _to.direct(_edge_ref[key], forward);
725 726
      }
726 727

	
727 728
      const From& _from;
728 729
      const To& _to;
729 730
      const EdgeRefMap& _edge_ref;
730 731
      const NodeRefMap& _node_ref;
731 732
    };
732 733

	
733 734
  public:
734 735

	
735 736
    /// \brief Constructor of GraphCopy.
736 737
    ///
737 738
    /// Constructor of GraphCopy for copying the content of the
738 739
    /// \c from graph into the \c to graph.
739 740
    GraphCopy(const From& from, To& to)
740 741
      : _from(from), _to(to) {}
741 742

	
742 743
    /// \brief Destructor of GraphCopy
743 744
    ///
744 745
    /// Destructor of GraphCopy.
745 746
    ~GraphCopy() {
746 747
      for (int i = 0; i < int(_node_maps.size()); ++i) {
747 748
        delete _node_maps[i];
748 749
      }
749 750
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
750 751
        delete _arc_maps[i];
751 752
      }
752 753
      for (int i = 0; i < int(_edge_maps.size()); ++i) {
753 754
        delete _edge_maps[i];
754 755
      }
755 756
    }
756 757

	
757 758
    /// \brief Copy the node references into the given map.
758 759
    ///
759 760
    /// This function copies the node references into the given map.
760 761
    /// The parameter should be a map, whose key type is the Node type of
761 762
    /// the source graph, while the value type is the Node type of the
762 763
    /// destination graph.
763 764
    template <typename NodeRef>
764 765
    GraphCopy& nodeRef(NodeRef& map) {
765 766
      _node_maps.push_back(new _core_bits::RefCopy<From, Node,
766 767
                           NodeRefMap, NodeRef>(map));
767 768
      return *this;
768 769
    }
769 770

	
770 771
    /// \brief Copy the node cross references into the given map.
771 772
    ///
772 773
    /// This function copies the node cross references (reverse references)
773 774
    /// into the given map. The parameter should be a map, whose key type
774 775
    /// is the Node type of the destination graph, while the value type is
775 776
    /// the Node type of the source graph.
776 777
    template <typename NodeCrossRef>
777 778
    GraphCopy& nodeCrossRef(NodeCrossRef& map) {
778 779
      _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
779 780
                           NodeRefMap, NodeCrossRef>(map));
780 781
      return *this;
781 782
    }
782 783

	
783 784
    /// \brief Make a copy of the given node map.
784 785
    ///
785 786
    /// This function makes a copy of the given node map for the newly
786 787
    /// created graph.
787 788
    /// The key type of the new map \c tmap should be the Node type of the
788 789
    /// destination graph, and the key type of the original map \c map
789 790
    /// should be the Node type of the source graph.
790 791
    template <typename FromMap, typename ToMap>
791 792
    GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
792 793
      _node_maps.push_back(new _core_bits::MapCopy<From, Node,
793 794
                           NodeRefMap, FromMap, ToMap>(map, tmap));
794 795
      return *this;
795 796
    }
796 797

	
797 798
    /// \brief Make a copy of the given node.
798 799
    ///
799 800
    /// This function makes a copy of the given node.
800 801
    GraphCopy& node(const Node& node, TNode& tnode) {
801 802
      _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
802 803
                           NodeRefMap, TNode>(node, tnode));
803 804
      return *this;
804 805
    }
805 806

	
806 807
    /// \brief Copy the arc references into the given map.
807 808
    ///
808 809
    /// This function copies the arc references into the given map.
809 810
    /// The parameter should be a map, whose key type is the Arc type of
810 811
    /// the source graph, while the value type is the Arc type of the
811 812
    /// destination graph.
812 813
    template <typename ArcRef>
813 814
    GraphCopy& arcRef(ArcRef& map) {
814 815
      _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
815 816
                          ArcRefMap, ArcRef>(map));
816 817
      return *this;
817 818
    }
818 819

	
819 820
    /// \brief Copy the arc cross references into the given map.
820 821
    ///
821 822
    /// This function copies the arc cross references (reverse references)
822 823
    /// into the given map. The parameter should be a map, whose key type
823 824
    /// is the Arc type of the destination graph, while the value type is
824 825
    /// the Arc type of the source graph.
825 826
    template <typename ArcCrossRef>
826 827
    GraphCopy& arcCrossRef(ArcCrossRef& map) {
827 828
      _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
828 829
                          ArcRefMap, ArcCrossRef>(map));
829 830
      return *this;
830 831
    }
831 832

	
832 833
    /// \brief Make a copy of the given arc map.
833 834
    ///
834 835
    /// This function makes a copy of the given arc map for the newly
835 836
    /// created graph.
836 837
    /// The key type of the new map \c tmap should be the Arc type of the
837 838
    /// destination graph, and the key type of the original map \c map
838 839
    /// should be the Arc type of the source graph.
839 840
    template <typename FromMap, typename ToMap>
840 841
    GraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
841 842
      _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
842 843
                          ArcRefMap, FromMap, ToMap>(map, tmap));
843 844
      return *this;
844 845
    }
845 846

	
846 847
    /// \brief Make a copy of the given arc.
847 848
    ///
848 849
    /// This function makes a copy of the given arc.
849 850
    GraphCopy& arc(const Arc& arc, TArc& tarc) {
850 851
      _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
851 852
                          ArcRefMap, TArc>(arc, tarc));
852 853
      return *this;
853 854
    }
854 855

	
855 856
    /// \brief Copy the edge references into the given map.
856 857
    ///
857 858
    /// This function copies the edge references into the given map.
858 859
    /// The parameter should be a map, whose key type is the Edge type of
859 860
    /// the source graph, while the value type is the Edge type of the
860 861
    /// destination graph.
861 862
    template <typename EdgeRef>
862 863
    GraphCopy& edgeRef(EdgeRef& map) {
863 864
      _edge_maps.push_back(new _core_bits::RefCopy<From, Edge,
864 865
                           EdgeRefMap, EdgeRef>(map));
865 866
      return *this;
866 867
    }
867 868

	
868 869
    /// \brief Copy the edge cross references into the given map.
869 870
    ///
870 871
    /// This function copies the edge cross references (reverse references)
871 872
    /// into the given map. The parameter should be a map, whose key type
872 873
    /// is the Edge type of the destination graph, while the value type is
873 874
    /// the Edge type of the source graph.
874 875
    template <typename EdgeCrossRef>
875 876
    GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
876 877
      _edge_maps.push_back(new _core_bits::CrossRefCopy<From,
877 878
                           Edge, EdgeRefMap, EdgeCrossRef>(map));
878 879
      return *this;
879 880
    }
880 881

	
881 882
    /// \brief Make a copy of the given edge map.
882 883
    ///
883 884
    /// This function makes a copy of the given edge map for the newly
884 885
    /// created graph.
885 886
    /// The key type of the new map \c tmap should be the Edge type of the
886 887
    /// destination graph, and the key type of the original map \c map
887 888
    /// should be the Edge type of the source graph.
888 889
    template <typename FromMap, typename ToMap>
889 890
    GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
890 891
      _edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
891 892
                           EdgeRefMap, FromMap, ToMap>(map, tmap));
892 893
      return *this;
893 894
    }
894 895

	
895 896
    /// \brief Make a copy of the given edge.
896 897
    ///
897 898
    /// This function makes a copy of the given edge.
898 899
    GraphCopy& edge(const Edge& edge, TEdge& tedge) {
899 900
      _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
900 901
                           EdgeRefMap, TEdge>(edge, tedge));
901 902
      return *this;
902 903
    }
903 904

	
904 905
    /// \brief Execute copying.
905 906
    ///
906 907
    /// This function executes the copying of the graph along with the
907 908
    /// copying of the assigned data.
908 909
    void run() {
909 910
      NodeRefMap nodeRefMap(_from);
910 911
      EdgeRefMap edgeRefMap(_from);
911 912
      ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap);
912 913
      _core_bits::GraphCopySelector<To>::
913 914
        copy(_from, _to, nodeRefMap, edgeRefMap);
914 915
      for (int i = 0; i < int(_node_maps.size()); ++i) {
915 916
        _node_maps[i]->copy(_from, nodeRefMap);
916 917
      }
917 918
      for (int i = 0; i < int(_edge_maps.size()); ++i) {
918 919
        _edge_maps[i]->copy(_from, edgeRefMap);
919 920
      }
920 921
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
921 922
        _arc_maps[i]->copy(_from, arcRefMap);
922 923
      }
923 924
    }
924 925

	
925 926
  private:
926 927

	
927 928
    const From& _from;
928 929
    To& _to;
929 930

	
930 931
    std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
931 932
      _node_maps;
932 933

	
933 934
    std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
934 935
      _arc_maps;
935 936

	
936 937
    std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
937 938
      _edge_maps;
938 939

	
939 940
  };
940 941

	
941 942
  /// \brief Copy a graph to another graph.
942 943
  ///
943 944
  /// This function copies a graph to another graph.
944 945
  /// The complete usage of it is detailed in the GraphCopy class,
945 946
  /// but a short example shows a basic work:
946 947
  ///\code
947 948
  /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
948 949
  ///\endcode
949 950
  ///
950 951
  /// After the copy the \c nr map will contain the mapping from the
951 952
  /// nodes of the \c from graph to the nodes of the \c to graph and
952 953
  /// \c ecr will contain the mapping from the edges of the \c to graph
953 954
  /// to the edges of the \c from graph.
954 955
  ///
955 956
  /// \see GraphCopy
956 957
  template <typename From, typename To>
957 958
  GraphCopy<From, To>
958 959
  graphCopy(const From& from, To& to) {
959 960
    return GraphCopy<From, To>(from, to);
960 961
  }
961 962

	
962 963
  namespace _core_bits {
963 964

	
964 965
    template <typename Graph, typename Enable = void>
965 966
    struct FindArcSelector {
966 967
      typedef typename Graph::Node Node;
967 968
      typedef typename Graph::Arc Arc;
968 969
      static Arc find(const Graph &g, Node u, Node v, Arc e) {
969 970
        if (e == INVALID) {
970 971
          g.firstOut(e, u);
971 972
        } else {
972 973
          g.nextOut(e);
973 974
        }
974 975
        while (e != INVALID && g.target(e) != v) {
975 976
          g.nextOut(e);
976 977
        }
977 978
        return e;
978 979
      }
979 980
    };
980 981

	
981 982
    template <typename Graph>
982 983
    struct FindArcSelector<
983 984
      Graph,
984 985
      typename enable_if<typename Graph::FindArcTag, void>::type>
985 986
    {
986 987
      typedef typename Graph::Node Node;
987 988
      typedef typename Graph::Arc Arc;
988 989
      static Arc find(const Graph &g, Node u, Node v, Arc prev) {
989 990
        return g.findArc(u, v, prev);
990 991
      }
991 992
    };
992 993
  }
993 994

	
994 995
  /// \brief Find an arc between two nodes of a digraph.
995 996
  ///
996 997
  /// This function finds an arc from node \c u to node \c v in the
997 998
  /// digraph \c g.
998 999
  ///
999 1000
  /// If \c prev is \ref INVALID (this is the default value), then
1000 1001
  /// it finds the first arc from \c u to \c v. Otherwise it looks for
1001 1002
  /// the next arc from \c u to \c v after \c prev.
1002 1003
  /// \return The found arc or \ref INVALID if there is no such an arc.
1003 1004
  ///
1004 1005
  /// Thus you can iterate through each arc from \c u to \c v as it follows.
1005 1006
  ///\code
1006 1007
  /// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) {
1007 1008
  ///   ...
1008 1009
  /// }
1009 1010
  ///\endcode
1010 1011
  ///
1011 1012
  /// \note \ref ConArcIt provides iterator interface for the same
1012 1013
  /// functionality.
1013 1014
  ///
1014 1015
  ///\sa ConArcIt
1015 1016
  ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1016 1017
  template <typename Graph>
1017 1018
  inline typename Graph::Arc
1018 1019
  findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1019 1020
          typename Graph::Arc prev = INVALID) {
1020 1021
    return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev);
1021 1022
  }
1022 1023

	
1023 1024
  /// \brief Iterator for iterating on parallel arcs connecting the same nodes.
1024 1025
  ///
1025 1026
  /// Iterator for iterating on parallel arcs connecting the same nodes. It is
1026 1027
  /// a higher level interface for the \ref findArc() function. You can
1027 1028
  /// use it the following way:
1028 1029
  ///\code
1029 1030
  /// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
1030 1031
  ///   ...
1031 1032
  /// }
1032 1033
  ///\endcode
1033 1034
  ///
1034 1035
  ///\sa findArc()
1035 1036
  ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1036 1037
  template <typename _Graph>
1037 1038
  class ConArcIt : public _Graph::Arc {
1038 1039
  public:
1039 1040

	
1040 1041
    typedef _Graph Graph;
1041 1042
    typedef typename Graph::Arc Parent;
1042 1043

	
1043 1044
    typedef typename Graph::Arc Arc;
1044 1045
    typedef typename Graph::Node Node;
1045 1046

	
1046 1047
    /// \brief Constructor.
1047 1048
    ///
1048 1049
    /// Construct a new ConArcIt iterating on the arcs that
1049 1050
    /// connects nodes \c u and \c v.
1050 1051
    ConArcIt(const Graph& g, Node u, Node v) : _graph(g) {
1051 1052
      Parent::operator=(findArc(_graph, u, v));
1052 1053
    }
1053 1054

	
1054 1055
    /// \brief Constructor.
1055 1056
    ///
1056 1057
    /// Construct a new ConArcIt that continues the iterating from arc \c a.
1057 1058
    ConArcIt(const Graph& g, Arc a) : Parent(a), _graph(g) {}
1058 1059

	
1059 1060
    /// \brief Increment operator.
1060 1061
    ///
1061 1062
    /// It increments the iterator and gives back the next arc.
1062 1063
    ConArcIt& operator++() {
1063 1064
      Parent::operator=(findArc(_graph, _graph.source(*this),
1064 1065
                                _graph.target(*this), *this));
1065 1066
      return *this;
1066 1067
    }
1067 1068
  private:
1068 1069
    const Graph& _graph;
1069 1070
  };
1070 1071

	
1071 1072
  namespace _core_bits {
1072 1073

	
1073 1074
    template <typename Graph, typename Enable = void>
1074 1075
    struct FindEdgeSelector {
1075 1076
      typedef typename Graph::Node Node;
1076 1077
      typedef typename Graph::Edge Edge;
1077 1078
      static Edge find(const Graph &g, Node u, Node v, Edge e) {
1078 1079
        bool b;
1079 1080
        if (u != v) {
1080 1081
          if (e == INVALID) {
1081 1082
            g.firstInc(e, b, u);
1082 1083
          } else {
1083 1084
            b = g.u(e) == u;
1084 1085
            g.nextInc(e, b);
1085 1086
          }
1086 1087
          while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) {
1087 1088
            g.nextInc(e, b);
1088 1089
          }
1089 1090
        } else {
1090 1091
          if (e == INVALID) {
1091 1092
            g.firstInc(e, b, u);
1092 1093
          } else {
1093 1094
            b = true;
1094 1095
            g.nextInc(e, b);
1095 1096
          }
1096 1097
          while (e != INVALID && (!b || g.v(e) != v)) {
1097 1098
            g.nextInc(e, b);
1098 1099
          }
1099 1100
        }
1100 1101
        return e;
1101 1102
      }
1102 1103
    };
1103 1104

	
1104 1105
    template <typename Graph>
1105 1106
    struct FindEdgeSelector<
1106 1107
      Graph,
1107 1108
      typename enable_if<typename Graph::FindEdgeTag, void>::type>
1108 1109
    {
1109 1110
      typedef typename Graph::Node Node;
1110 1111
      typedef typename Graph::Edge Edge;
1111 1112
      static Edge find(const Graph &g, Node u, Node v, Edge prev) {
1112 1113
        return g.findEdge(u, v, prev);
1113 1114
      }
1114 1115
    };
1115 1116
  }
1116 1117

	
1117 1118
  /// \brief Find an edge between two nodes of a graph.
1118 1119
  ///
1119 1120
  /// This function finds an edge from node \c u to node \c v in graph \c g.
1120 1121
  /// If node \c u and node \c v is equal then each loop edge
1121 1122
  /// will be enumerated once.
1122 1123
  ///
1123 1124
  /// If \c prev is \ref INVALID (this is the default value), then
1124 1125
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
1125 1126
  /// the next edge from \c u to \c v after \c prev.
1126 1127
  /// \return The found edge or \ref INVALID if there is no such an edge.
1127 1128
  ///
1128 1129
  /// Thus you can iterate through each edge between \c u and \c v
1129 1130
  /// as it follows.
1130 1131
  ///\code
1131 1132
  /// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) {
1132 1133
  ///   ...
1133 1134
  /// }
1134 1135
  ///\endcode
1135 1136
  ///
1136 1137
  /// \note \ref ConEdgeIt provides iterator interface for the same
1137 1138
  /// functionality.
1138 1139
  ///
1139 1140
  ///\sa ConEdgeIt
1140 1141
  template <typename Graph>
1141 1142
  inline typename Graph::Edge
1142 1143
  findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1143 1144
            typename Graph::Edge p = INVALID) {
1144 1145
    return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p);
1145 1146
  }
1146 1147

	
1147 1148
  /// \brief Iterator for iterating on parallel edges connecting the same nodes.
1148 1149
  ///
1149 1150
  /// Iterator for iterating on parallel edges connecting the same nodes.
1150 1151
  /// It is a higher level interface for the findEdge() function. You can
1151 1152
  /// use it the following way:
1152 1153
  ///\code
1153 1154
  /// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) {
1154 1155
  ///   ...
1155 1156
  /// }
1156 1157
  ///\endcode
1157 1158
  ///
1158 1159
  ///\sa findEdge()
1159 1160
  template <typename _Graph>
1160 1161
  class ConEdgeIt : public _Graph::Edge {
1161 1162
  public:
1162 1163

	
1163 1164
    typedef _Graph Graph;
1164 1165
    typedef typename Graph::Edge Parent;
1165 1166

	
1166 1167
    typedef typename Graph::Edge Edge;
1167 1168
    typedef typename Graph::Node Node;
1168 1169

	
1169 1170
    /// \brief Constructor.
1170 1171
    ///
1171 1172
    /// Construct a new ConEdgeIt iterating on the edges that
1172 1173
    /// connects nodes \c u and \c v.
1173 1174
    ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g), _u(u), _v(v) {
1174 1175
      Parent::operator=(findEdge(_graph, _u, _v));
1175 1176
    }
1176 1177

	
1177 1178
    /// \brief Constructor.
1178 1179
    ///
1179 1180
    /// Construct a new ConEdgeIt that continues iterating from edge \c e.
1180 1181
    ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {}
1181 1182

	
1182 1183
    /// \brief Increment operator.
1183 1184
    ///
1184 1185
    /// It increments the iterator and gives back the next edge.
1185 1186
    ConEdgeIt& operator++() {
1186 1187
      Parent::operator=(findEdge(_graph, _u, _v, *this));
1187 1188
      return *this;
1188 1189
    }
1189 1190
  private:
1190 1191
    const Graph& _graph;
1191 1192
    Node _u, _v;
1192 1193
  };
1193 1194

	
1194 1195

	
1195 1196
  ///Dynamic arc look-up between given endpoints.
1196 1197

	
1197 1198
  ///Using this class, you can find an arc in a digraph from a given
1198 1199
  ///source to a given target in amortized time <em>O</em>(log<em>d</em>),
1199 1200
  ///where <em>d</em> is the out-degree of the source node.
1200 1201
  ///
1201 1202
  ///It is possible to find \e all parallel arcs between two nodes with
1202 1203
  ///the \c operator() member.
1203 1204
  ///
1204 1205
  ///This is a dynamic data structure. Consider to use \ref ArcLookUp or
1205 1206
  ///\ref AllArcLookUp if your digraph is not changed so frequently.
1206 1207
  ///
1207 1208
  ///This class uses a self-adjusting binary search tree, the Splay tree
1208 1209
  ///of Sleator and Tarjan to guarantee the logarithmic amortized
1209 1210
  ///time bound for arc look-ups. This class also guarantees the
1210 1211
  ///optimal time bound in a constant factor for any distribution of
1211 1212
  ///queries.
1212 1213
  ///
1213 1214
  ///\tparam G The type of the underlying digraph.
1214 1215
  ///
1215 1216
  ///\sa ArcLookUp
1216 1217
  ///\sa AllArcLookUp
1217 1218
  template<class G>
1218 1219
  class DynArcLookUp
1219 1220
    : protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase
1220 1221
  {
1221 1222
  public:
1222 1223
    typedef typename ItemSetTraits<G, typename G::Arc>
1223 1224
    ::ItemNotifier::ObserverBase Parent;
1224 1225

	
1225 1226
    TEMPLATE_DIGRAPH_TYPEDEFS(G);
1226 1227
    typedef G Digraph;
1227 1228

	
1228 1229
  protected:
1229 1230

	
1230 1231
    class AutoNodeMap : public ItemSetTraits<G, Node>::template Map<Arc>::Type {
1231 1232
    public:
1232 1233

	
1233 1234
      typedef typename ItemSetTraits<G, Node>::template Map<Arc>::Type Parent;
1234 1235

	
1235 1236
      AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {}
1236 1237

	
1237 1238
      virtual void add(const Node& node) {
1238 1239
        Parent::add(node);
1239 1240
        Parent::set(node, INVALID);
1240 1241
      }
1241 1242

	
1242 1243
      virtual void add(const std::vector<Node>& nodes) {
1243 1244
        Parent::add(nodes);
1244 1245
        for (int i = 0; i < int(nodes.size()); ++i) {
1245 1246
          Parent::set(nodes[i], INVALID);
1246 1247
        }
1247 1248
      }
1248 1249

	
1249 1250
      virtual void build() {
1250 1251
        Parent::build();
1251 1252
        Node it;
1252 1253
        typename Parent::Notifier* nf = Parent::notifier();
1253 1254
        for (nf->first(it); it != INVALID; nf->next(it)) {
1254 1255
          Parent::set(it, INVALID);
1255 1256
        }
1256 1257
      }
1257 1258
    };
1258 1259

	
1259 1260
    const Digraph &_g;
1260 1261
    AutoNodeMap _head;
1261 1262
    typename Digraph::template ArcMap<Arc> _parent;
1262 1263
    typename Digraph::template ArcMap<Arc> _left;
1263 1264
    typename Digraph::template ArcMap<Arc> _right;
1264 1265

	
1265 1266
    class ArcLess {
1266 1267
      const Digraph &g;
1267 1268
    public:
1268 1269
      ArcLess(const Digraph &_g) : g(_g) {}
1269 1270
      bool operator()(Arc a,Arc b) const
1270 1271
      {
1271 1272
        return g.target(a)<g.target(b);
1272 1273
      }
1273 1274
    };
1274 1275

	
1275 1276
  public:
1276 1277

	
1277 1278
    ///Constructor
1278 1279

	
1279 1280
    ///Constructor.
1280 1281
    ///
1281 1282
    ///It builds up the search database.
1282 1283
    DynArcLookUp(const Digraph &g)
1283 1284
      : _g(g),_head(g),_parent(g),_left(g),_right(g)
1284 1285
    {
1285 1286
      Parent::attach(_g.notifier(typename Digraph::Arc()));
1286 1287
      refresh();
1287 1288
    }
1288 1289

	
1289 1290
  protected:
1290 1291

	
1291 1292
    virtual void add(const Arc& arc) {
1292 1293
      insert(arc);
1293 1294
    }
1294 1295

	
1295 1296
    virtual void add(const std::vector<Arc>& arcs) {
1296 1297
      for (int i = 0; i < int(arcs.size()); ++i) {
1297 1298
        insert(arcs[i]);
1298 1299
      }
1299 1300
    }
1300 1301

	
1301 1302
    virtual void erase(const Arc& arc) {
1302 1303
      remove(arc);
1303 1304
    }
1304 1305

	
1305 1306
    virtual void erase(const std::vector<Arc>& arcs) {
1306 1307
      for (int i = 0; i < int(arcs.size()); ++i) {
1307 1308
        remove(arcs[i]);
1308 1309
      }
1309 1310
    }
1310 1311

	
1311 1312
    virtual void build() {
1312 1313
      refresh();
1313 1314
    }
1314 1315

	
1315 1316
    virtual void clear() {
1316 1317
      for(NodeIt n(_g);n!=INVALID;++n) {
1317 1318
        _head.set(n, INVALID);
1318 1319
      }
1319 1320
    }
1320 1321

	
1321 1322
    void insert(Arc arc) {
1322 1323
      Node s = _g.source(arc);
1323 1324
      Node t = _g.target(arc);
1324 1325
      _left.set(arc, INVALID);
1325 1326
      _right.set(arc, INVALID);
1326 1327

	
1327 1328
      Arc e = _head[s];
1328 1329
      if (e == INVALID) {
1329 1330
        _head.set(s, arc);
1330 1331
        _parent.set(arc, INVALID);
1331 1332
        return;
1332 1333
      }
1333 1334
      while (true) {
1334 1335
        if (t < _g.target(e)) {
1335 1336
          if (_left[e] == INVALID) {
1336 1337
            _left.set(e, arc);
1337 1338
            _parent.set(arc, e);
1338 1339
            splay(arc);
1339 1340
            return;
1340 1341
          } else {
1341 1342
            e = _left[e];
1342 1343
          }
1343 1344
        } else {
1344 1345
          if (_right[e] == INVALID) {
1345 1346
            _right.set(e, arc);
1346 1347
            _parent.set(arc, e);
1347 1348
            splay(arc);
1348 1349
            return;
1349 1350
          } else {
1350 1351
            e = _right[e];
1351 1352
          }
1352 1353
        }
1353 1354
      }
1354 1355
    }
1355 1356

	
1356 1357
    void remove(Arc arc) {
1357 1358
      if (_left[arc] == INVALID) {
1358 1359
        if (_right[arc] != INVALID) {
1359 1360
          _parent.set(_right[arc], _parent[arc]);
1360 1361
        }
1361 1362
        if (_parent[arc] != INVALID) {
1362 1363
          if (_left[_parent[arc]] == arc) {
1363 1364
            _left.set(_parent[arc], _right[arc]);
1364 1365
          } else {
1365 1366
            _right.set(_parent[arc], _right[arc]);
1366 1367
          }
1367 1368
        } else {
1368 1369
          _head.set(_g.source(arc), _right[arc]);
1369 1370
        }
1370 1371
      } else if (_right[arc] == INVALID) {
1371 1372
        _parent.set(_left[arc], _parent[arc]);
1372 1373
        if (_parent[arc] != INVALID) {
1373 1374
          if (_left[_parent[arc]] == arc) {
1374 1375
            _left.set(_parent[arc], _left[arc]);
1375 1376
          } else {
1376 1377
            _right.set(_parent[arc], _left[arc]);
1377 1378
          }
1378 1379
        } else {
1379 1380
          _head.set(_g.source(arc), _left[arc]);
1380 1381
        }
1381 1382
      } else {
1382 1383
        Arc e = _left[arc];
1383 1384
        if (_right[e] != INVALID) {
1384 1385
          e = _right[e];
1385 1386
          while (_right[e] != INVALID) {
1386 1387
            e = _right[e];
1387 1388
          }
1388 1389
          Arc s = _parent[e];
1389 1390
          _right.set(_parent[e], _left[e]);
1390 1391
          if (_left[e] != INVALID) {
1391 1392
            _parent.set(_left[e], _parent[e]);
1392 1393
          }
1393 1394

	
1394 1395
          _left.set(e, _left[arc]);
1395 1396
          _parent.set(_left[arc], e);
1396 1397
          _right.set(e, _right[arc]);
1397 1398
          _parent.set(_right[arc], e);
1398 1399

	
1399 1400
          _parent.set(e, _parent[arc]);
1400 1401
          if (_parent[arc] != INVALID) {
1401 1402
            if (_left[_parent[arc]] == arc) {
1402 1403
              _left.set(_parent[arc], e);
1403 1404
            } else {
1404 1405
              _right.set(_parent[arc], e);
1405 1406
            }
1406 1407
          }
1407 1408
          splay(s);
1408 1409
        } else {
1409 1410
          _right.set(e, _right[arc]);
1410 1411
          _parent.set(_right[arc], e);
1411 1412
          _parent.set(e, _parent[arc]);
1412 1413

	
1413 1414
          if (_parent[arc] != INVALID) {
1414 1415
            if (_left[_parent[arc]] == arc) {
1415 1416
              _left.set(_parent[arc], e);
1416 1417
            } else {
1417 1418
              _right.set(_parent[arc], e);
1418 1419
            }
1419 1420
          } else {
1420 1421
            _head.set(_g.source(arc), e);
1421 1422
          }
1422 1423
        }
1423 1424
      }
1424 1425
    }
1425 1426

	
1426 1427
    Arc refreshRec(std::vector<Arc> &v,int a,int b)
1427 1428
    {
1428 1429
      int m=(a+b)/2;
1429 1430
      Arc me=v[m];
1430 1431
      if (a < m) {
1431 1432
        Arc left = refreshRec(v,a,m-1);
1432 1433
        _left.set(me, left);
1433 1434
        _parent.set(left, me);
1434 1435
      } else {
1435 1436
        _left.set(me, INVALID);
1436 1437
      }
1437 1438
      if (m < b) {
1438 1439
        Arc right = refreshRec(v,m+1,b);
1439 1440
        _right.set(me, right);
1440 1441
        _parent.set(right, me);
1441 1442
      } else {
1442 1443
        _right.set(me, INVALID);
1443 1444
      }
1444 1445
      return me;
1445 1446
    }
1446 1447

	
1447 1448
    void refresh() {
1448 1449
      for(NodeIt n(_g);n!=INVALID;++n) {
1449 1450
        std::vector<Arc> v;
1450 1451
        for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a);
1451 1452
        if (!v.empty()) {
1452 1453
          std::sort(v.begin(),v.end(),ArcLess(_g));
1453 1454
          Arc head = refreshRec(v,0,v.size()-1);
1454 1455
          _head.set(n, head);
1455 1456
          _parent.set(head, INVALID);
1456 1457
        }
1457 1458
        else _head.set(n, INVALID);
1458 1459
      }
1459 1460
    }
1460 1461

	
1461 1462
    void zig(Arc v) {
1462 1463
      Arc w = _parent[v];
1463 1464
      _parent.set(v, _parent[w]);
1464 1465
      _parent.set(w, v);
1465 1466
      _left.set(w, _right[v]);
1466 1467
      _right.set(v, w);
1467 1468
      if (_parent[v] != INVALID) {
1468 1469
        if (_right[_parent[v]] == w) {
1469 1470
          _right.set(_parent[v], v);
1470 1471
        } else {
1471 1472
          _left.set(_parent[v], v);
1472 1473
        }
1473 1474
      }
1474 1475
      if (_left[w] != INVALID){
1475 1476
        _parent.set(_left[w], w);
1476 1477
      }
1477 1478
    }
1478 1479

	
1479 1480
    void zag(Arc v) {
1480 1481
      Arc w = _parent[v];
1481 1482
      _parent.set(v, _parent[w]);
1482 1483
      _parent.set(w, v);
1483 1484
      _right.set(w, _left[v]);
1484 1485
      _left.set(v, w);
1485 1486
      if (_parent[v] != INVALID){
1486 1487
        if (_left[_parent[v]] == w) {
1487 1488
          _left.set(_parent[v], v);
1488 1489
        } else {
1489 1490
          _right.set(_parent[v], v);
1490 1491
        }
1491 1492
      }
1492 1493
      if (_right[w] != INVALID){
1493 1494
        _parent.set(_right[w], w);
1494 1495
      }
1495 1496
    }
1496 1497

	
1497 1498
    void splay(Arc v) {
1498 1499
      while (_parent[v] != INVALID) {
1499 1500
        if (v == _left[_parent[v]]) {
1500 1501
          if (_parent[_parent[v]] == INVALID) {
1501 1502
            zig(v);
1502 1503
          } else {
1503 1504
            if (_parent[v] == _left[_parent[_parent[v]]]) {
1504 1505
              zig(_parent[v]);
1505 1506
              zig(v);
1506 1507
            } else {
1507 1508
              zig(v);
1508 1509
              zag(v);
1509 1510
            }
1510 1511
          }
1511 1512
        } else {
1512 1513
          if (_parent[_parent[v]] == INVALID) {
1513 1514
            zag(v);
1514 1515
          } else {
1515 1516
            if (_parent[v] == _left[_parent[_parent[v]]]) {
1516 1517
              zag(v);
1517 1518
              zig(v);
1518 1519
            } else {
1519 1520
              zag(_parent[v]);
1520 1521
              zag(v);
1521 1522
            }
1522 1523
          }
1523 1524
        }
1524 1525
      }
1525 1526
      _head[_g.source(v)] = v;
1526 1527
    }
1527 1528

	
1528 1529

	
1529 1530
  public:
1530 1531

	
1531 1532
    ///Find an arc between two nodes.
1532 1533

	
1533 1534
    ///Find an arc between two nodes.
1534 1535
    ///\param s The source node.
1535 1536
    ///\param t The target node.
1536 1537
    ///\param p The previous arc between \c s and \c t. It it is INVALID or
1537 1538
    ///not given, the operator finds the first appropriate arc.
1538 1539
    ///\return An arc from \c s to \c t after \c p or
1539 1540
    ///\ref INVALID if there is no more.
1540 1541
    ///
1541 1542
    ///For example, you can count the number of arcs from \c u to \c v in the
1542 1543
    ///following way.
1543 1544
    ///\code
1544 1545
    ///DynArcLookUp<ListDigraph> ae(g);
1545 1546
    ///...
1546 1547
    ///int n = 0;
1547 1548
    ///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++;
1548 1549
    ///\endcode
1549 1550
    ///
1550 1551
    ///Finding the arcs take at most <em>O</em>(log<em>d</em>)
1551 1552
    ///amortized time, specifically, the time complexity of the lookups
1552 1553
    ///is equal to the optimal search tree implementation for the
1553 1554
    ///current query distribution in a constant factor.
1554 1555
    ///
1555 1556
    ///\note This is a dynamic data structure, therefore the data
1556 1557
    ///structure is updated after each graph alteration. Thus although
1557 1558
    ///this data structure is theoretically faster than \ref ArcLookUp
1558 1559
    ///and \ref AllArcLookUp, it often provides worse performance than
1559 1560
    ///them.
1560 1561
    Arc operator()(Node s, Node t, Arc p = INVALID) const  {
1561 1562
      if (p == INVALID) {
1562 1563
        Arc a = _head[s];
1563 1564
        if (a == INVALID) return INVALID;
1564 1565
        Arc r = INVALID;
1565 1566
        while (true) {
1566 1567
          if (_g.target(a) < t) {
1567 1568
            if (_right[a] == INVALID) {
1568 1569
              const_cast<DynArcLookUp&>(*this).splay(a);
1569 1570
              return r;
1570 1571
            } else {
1571 1572
              a = _right[a];
1572 1573
            }
1573 1574
          } else {
1574 1575
            if (_g.target(a) == t) {
1575 1576
              r = a;
1576 1577
            }
1577 1578
            if (_left[a] == INVALID) {
1578 1579
              const_cast<DynArcLookUp&>(*this).splay(a);
1579 1580
              return r;
1580 1581
            } else {
1581 1582
              a = _left[a];
1582 1583
            }
1583 1584
          }
1584 1585
        }
1585 1586
      } else {
1586 1587
        Arc a = p;
1587 1588
        if (_right[a] != INVALID) {
1588 1589
          a = _right[a];
1589 1590
          while (_left[a] != INVALID) {
1590 1591
            a = _left[a];
1591 1592
          }
1592 1593
          const_cast<DynArcLookUp&>(*this).splay(a);
1593 1594
        } else {
1594 1595
          while (_parent[a] != INVALID && _right[_parent[a]] ==  a) {
1595 1596
            a = _parent[a];
1596 1597
          }
1597 1598
          if (_parent[a] == INVALID) {
1598 1599
            return INVALID;
1599 1600
          } else {
1600 1601
            a = _parent[a];
1601 1602
            const_cast<DynArcLookUp&>(*this).splay(a);
1602 1603
          }
1603 1604
        }
1604 1605
        if (_g.target(a) == t) return a;
1605 1606
        else return INVALID;
1606 1607
      }
1607 1608
    }
1608 1609

	
1609 1610
  };
1610 1611

	
1611 1612
  ///Fast arc look-up between given endpoints.
1612 1613

	
1613 1614
  ///Using this class, you can find an arc in a digraph from a given
1614 1615
  ///source to a given target in time <em>O</em>(log<em>d</em>),
1615 1616
  ///where <em>d</em> is the out-degree of the source node.
1616 1617
  ///
1617 1618
  ///It is not possible to find \e all parallel arcs between two nodes.
1618 1619
  ///Use \ref AllArcLookUp for this purpose.
1619 1620
  ///
1620 1621
  ///\warning This class is static, so you should call refresh() (or at
1621 1622
  ///least refresh(Node)) to refresh this data structure whenever the
1622 1623
  ///digraph changes. This is a time consuming (superlinearly proportional
1623 1624
  ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
1624 1625
  ///
1625 1626
  ///\tparam G The type of the underlying digraph.
1626 1627
  ///
1627 1628
  ///\sa DynArcLookUp
1628 1629
  ///\sa AllArcLookUp
1629 1630
  template<class G>
1630 1631
  class ArcLookUp
1631 1632
  {
1632 1633
  public:
1633 1634
    TEMPLATE_DIGRAPH_TYPEDEFS(G);
1634 1635
    typedef G Digraph;
1635 1636

	
1636 1637
  protected:
1637 1638
    const Digraph &_g;
1638 1639
    typename Digraph::template NodeMap<Arc> _head;
1639 1640
    typename Digraph::template ArcMap<Arc> _left;
1640 1641
    typename Digraph::template ArcMap<Arc> _right;
1641 1642

	
1642 1643
    class ArcLess {
1643 1644
      const Digraph &g;
1644 1645
    public:
1645 1646
      ArcLess(const Digraph &_g) : g(_g) {}
1646 1647
      bool operator()(Arc a,Arc b) const
1647 1648
      {
1648 1649
        return g.target(a)<g.target(b);
1649 1650
      }
1650 1651
    };
1651 1652

	
1652 1653
  public:
1653 1654

	
1654 1655
    ///Constructor
1655 1656

	
1656 1657
    ///Constructor.
1657 1658
    ///
1658 1659
    ///It builds up the search database, which remains valid until the digraph
1659 1660
    ///changes.
1660 1661
    ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
1661 1662

	
1662 1663
  private:
1663 1664
    Arc refreshRec(std::vector<Arc> &v,int a,int b)
1664 1665
    {
1665 1666
      int m=(a+b)/2;
1666 1667
      Arc me=v[m];
1667 1668
      _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
1668 1669
      _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
1669 1670
      return me;
1670 1671
    }
1671 1672
  public:
1672 1673
    ///Refresh the search data structure at a node.
1673 1674

	
1674 1675
    ///Build up the search database of node \c n.
1675 1676
    ///
1676 1677
    ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em>
1677 1678
    ///is the number of the outgoing arcs of \c n.
1678 1679
    void refresh(Node n)
1679 1680
    {
1680 1681
      std::vector<Arc> v;
1681 1682
      for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
1682 1683
      if(v.size()) {
1683 1684
        std::sort(v.begin(),v.end(),ArcLess(_g));
1684 1685
        _head[n]=refreshRec(v,0,v.size()-1);
1685 1686
      }
1686 1687
      else _head[n]=INVALID;
1687 1688
    }
1688 1689
    ///Refresh the full data structure.
1689 1690

	
1690 1691
    ///Build up the full search database. In fact, it simply calls
1691 1692
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
1692 1693
    ///
1693 1694
    ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
1694 1695
    ///the number of the arcs in the digraph and <em>D</em> is the maximum
1695 1696
    ///out-degree of the digraph.
1696 1697
    void refresh()
1697 1698
    {
1698 1699
      for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
1699 1700
    }
1700 1701

	
1701 1702
    ///Find an arc between two nodes.
1702 1703

	
1703 1704
    ///Find an arc between two nodes in time <em>O</em>(log<em>d</em>),
1704 1705
    ///where <em>d</em> is the number of outgoing arcs of \c s.
1705 1706
    ///\param s The source node.
1706 1707
    ///\param t The target node.
1707 1708
    ///\return An arc from \c s to \c t if there exists,
1708 1709
    ///\ref INVALID otherwise.
1709 1710
    ///
1710 1711
    ///\warning If you change the digraph, refresh() must be called before using
1711 1712
    ///this operator. If you change the outgoing arcs of
1712 1713
    ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
1713 1714
    Arc operator()(Node s, Node t) const
1714 1715
    {
1715 1716
      Arc e;
1716 1717
      for(e=_head[s];
1717 1718
          e!=INVALID&&_g.target(e)!=t;
1718 1719
          e = t < _g.target(e)?_left[e]:_right[e]) ;
1719 1720
      return e;
1720 1721
    }
1721 1722

	
1722 1723
  };
1723 1724

	
1724 1725
  ///Fast look-up of all arcs between given endpoints.
1725 1726

	
1726 1727
  ///This class is the same as \ref ArcLookUp, with the addition
1727 1728
  ///that it makes it possible to find all parallel arcs between given
1728 1729
  ///endpoints.
1729 1730
  ///
1730 1731
  ///\warning This class is static, so you should call refresh() (or at
1731 1732
  ///least refresh(Node)) to refresh this data structure whenever the
1732 1733
  ///digraph changes. This is a time consuming (superlinearly proportional
1733 1734
  ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
1734 1735
  ///
1735 1736
  ///\tparam G The type of the underlying digraph.
1736 1737
  ///
1737 1738
  ///\sa DynArcLookUp
1738 1739
  ///\sa ArcLookUp
1739 1740
  template<class G>
1740 1741
  class AllArcLookUp : public ArcLookUp<G>
1741 1742
  {
1742 1743
    using ArcLookUp<G>::_g;
1743 1744
    using ArcLookUp<G>::_right;
1744 1745
    using ArcLookUp<G>::_left;
1745 1746
    using ArcLookUp<G>::_head;
1746 1747

	
1747 1748
    TEMPLATE_DIGRAPH_TYPEDEFS(G);
1748 1749
    typedef G Digraph;
1749 1750

	
1750 1751
    typename Digraph::template ArcMap<Arc> _next;
1751 1752

	
1752 1753
    Arc refreshNext(Arc head,Arc next=INVALID)
1753 1754
    {
1754 1755
      if(head==INVALID) return next;
1755 1756
      else {
1756 1757
        next=refreshNext(_right[head],next);
1757 1758
        _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
1758 1759
          ? next : INVALID;
1759 1760
        return refreshNext(_left[head],head);
1760 1761
      }
1761 1762
    }
1762 1763

	
1763 1764
    void refreshNext()
1764 1765
    {
1765 1766
      for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
1766 1767
    }
1767 1768

	
1768 1769
  public:
1769 1770
    ///Constructor
1770 1771

	
1771 1772
    ///Constructor.
1772 1773
    ///
1773 1774
    ///It builds up the search database, which remains valid until the digraph
1774 1775
    ///changes.
1775 1776
    AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();}
1776 1777

	
1777 1778
    ///Refresh the data structure at a node.
1778 1779

	
1779 1780
    ///Build up the search database of node \c n.
1780 1781
    ///
1781 1782
    ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is
1782 1783
    ///the number of the outgoing arcs of \c n.
1783 1784
    void refresh(Node n)
1784 1785
    {
1785 1786
      ArcLookUp<G>::refresh(n);
1786 1787
      refreshNext(_head[n]);
1787 1788
    }
1788 1789

	
1789 1790
    ///Refresh the full data structure.
1790 1791

	
1791 1792
    ///Build up the full search database. In fact, it simply calls
1792 1793
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
1793 1794
    ///
1794 1795
    ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
1795 1796
    ///the number of the arcs in the digraph and <em>D</em> is the maximum
1796 1797
    ///out-degree of the digraph.
1797 1798
    void refresh()
1798 1799
    {
1799 1800
      for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
1800 1801
    }
1801 1802

	
1802 1803
    ///Find an arc between two nodes.
1803 1804

	
1804 1805
    ///Find an arc between two nodes.
1805 1806
    ///\param s The source node.
1806 1807
    ///\param t The target node.
1807 1808
    ///\param prev The previous arc between \c s and \c t. It it is INVALID or
1808 1809
    ///not given, the operator finds the first appropriate arc.
1809 1810
    ///\return An arc from \c s to \c t after \c prev or
1810 1811
    ///\ref INVALID if there is no more.
1811 1812
    ///
1812 1813
    ///For example, you can count the number of arcs from \c u to \c v in the
1813 1814
    ///following way.
1814 1815
    ///\code
1815 1816
    ///AllArcLookUp<ListDigraph> ae(g);
1816 1817
    ///...
1817 1818
    ///int n = 0;
1818 1819
    ///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++;
1819 1820
    ///\endcode
1820 1821
    ///
1821 1822
    ///Finding the first arc take <em>O</em>(log<em>d</em>) time,
1822 1823
    ///where <em>d</em> is the number of outgoing arcs of \c s. Then the
1823 1824
    ///consecutive arcs are found in constant time.
1824 1825
    ///
1825 1826
    ///\warning If you change the digraph, refresh() must be called before using
1826 1827
    ///this operator. If you change the outgoing arcs of
1827 1828
    ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
1828 1829
    ///
1829 1830
#ifdef DOXYGEN
1830 1831
    Arc operator()(Node s, Node t, Arc prev=INVALID) const {}
1831 1832
#else
1832 1833
    using ArcLookUp<G>::operator() ;
1833 1834
    Arc operator()(Node s, Node t, Arc prev) const
1834 1835
    {
1835 1836
      return prev==INVALID?(*this)(s,t):_next[prev];
1836 1837
    }
1837 1838
#endif
1838 1839

	
1839 1840
  };
1840 1841

	
1841 1842
  /// @}
1842 1843

	
1843 1844
} //namespace lemon
1844 1845

	
1845 1846
#endif
0 comments (0 inline)