gravatar
deba@inf.elte.hu
deba@inf.elte.hu
New implementation of GRAPH_TYPEDEFS
0 3 0
default
3 files changed with 119 insertions and 14 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5 5
 * Copyright (C) 2003-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_GRAPH_UTILS_H
20 20
#define LEMON_GRAPH_UTILS_H
21 21

	
22 22
#include <iterator>
23 23
#include <vector>
24 24
#include <map>
25 25
#include <cmath>
26 26
#include <algorithm>
27 27

	
28 28
#include <lemon/bits/invalid.h>
29 29
#include <lemon/bits/utility.h>
30 30
#include <lemon/maps.h>
31 31
#include <lemon/bits/traits.h>
32 32

	
33 33
#include <lemon/bits/alteration_notifier.h>
34 34
#include <lemon/bits/default_map.h>
35 35

	
36 36
///\ingroup gutils
37 37
///\file
38 38
///\brief Graph utilities.
39 39

	
40 40
namespace lemon {
41 41

	
42 42
  /// \addtogroup gutils
43 43
  /// @{
44 44

	
45
  namespace _graph_utils_bits {
46
    template <typename Graph>
47
    struct Node { typedef typename Graph::Node type; };
48

	
49
    template <typename Graph>
50
    struct NodeIt { typedef typename Graph::NodeIt type; };
51

	
52
    template <typename Graph>
53
    struct Arc { typedef typename Graph::Arc type; };
54

	
55
    template <typename Graph>
56
    struct ArcIt { typedef typename Graph::ArcIt type; };
57

	
58
    template <typename Graph>
59
    struct Edge { typedef typename Graph::Edge type; };
60

	
61
    template <typename Graph>
62
    struct EdgeIt { typedef typename Graph::EdgeIt type; };
63

	
64
    template <typename Graph>
65
    struct OutArcIt { typedef typename Graph::OutArcIt type; };
66

	
67
    template <typename Graph>
68
    struct InArcIt { typedef typename Graph::InArcIt type; };
69

	
70
    template <typename Graph>
71
    struct IncEdgeIt { typedef typename Graph::IncEdgeIt type; };
72

	
73
    template <typename Graph>
74
    struct BoolNodeMap { 
75
      typedef typename Graph::template NodeMap<bool> type; 
76
    };
77

	
78
    template <typename Graph>
79
    struct IntNodeMap { 
80
      typedef typename Graph::template NodeMap<int> type; 
81
    };
82

	
83
    template <typename Graph>
84
    struct DoubleNodeMap { 
85
      typedef typename Graph::template NodeMap<double> type; 
86
    };
87

	
88
    template <typename Graph>
89
    struct BoolArcMap { 
90
      typedef typename Graph::template ArcMap<bool> type; 
91
    };
92

	
93
    template <typename Graph>
94
    struct IntArcMap { 
95
      typedef typename Graph::template ArcMap<int> type; 
96
    };
97

	
98
    template <typename Graph>
99
    struct DoubleArcMap { 
100
      typedef typename Graph::template ArcMap<double> type; 
101
    };
102

	
103
    template <typename Graph>
104
    struct BoolEdgeMap { 
105
      typedef typename Graph::template EdgeMap<bool> type; 
106
    };
107

	
108
    template <typename Graph>
109
    struct IntEdgeMap { 
110
      typedef typename Graph::template EdgeMap<int> type; 
111
    };
112

	
113
    template <typename Graph>
114
    struct DoubleEdgeMap { 
115
      typedef typename Graph::template EdgeMap<double> type; 
116
    };
117

	
118
    
119
  }
120

	
45 121
  ///Creates convenience typedefs for the digraph types and iterators
46 122

	
47 123
  ///This \c \#define creates convenience typedefs for the following types
48 124
  ///of \c Digraph: \c Node,  \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
49 125
  ///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, 
50 126
  ///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. 
51 127
#define DIGRAPH_TYPEDEFS(Digraph)					\
52
  typedef Digraph::Node Node;						\
53
  typedef Digraph::NodeIt NodeIt;					\
54
  typedef Digraph::Arc Arc;						\
55
  typedef Digraph::ArcIt ArcIt;						\
56
  typedef Digraph::InArcIt InArcIt;					\
57
  typedef Digraph::OutArcIt OutArcIt
128
  typedef typename ::lemon::_graph_utils_bits::				\
129
  Node<Digraph>::type Node;						\
130
  typedef typename ::lemon::_graph_utils_bits::				\
131
  NodeIt<Digraph>::type	NodeIt;						\
132
  typedef typename ::lemon::_graph_utils_bits::				\
133
  Arc<Digraph>::type Arc;						\
134
  typedef typename ::lemon::_graph_utils_bits::				\
135
  ArcIt<Digraph>::type ArcIt;						\
136
  typedef typename ::lemon::_graph_utils_bits::				\
137
  OutArcIt<Digraph>::type OutArcIt;					\
138
  typedef typename ::lemon::_graph_utils_bits::				\
139
  InArcIt<Digraph>::type InArcIt;					\
140
  typedef typename ::lemon::_graph_utils_bits::				\
141
  BoolNodeMap<Digraph>::type BoolNodeMap;				\
142
  typedef typename ::lemon::_graph_utils_bits::				\
143
  IntNodeMap<Digraph>::type IntNodeMap;					\
144
  typedef typename ::lemon::_graph_utils_bits::				\
145
  DoubleNodeMap<Digraph>::type DoubleNodeMap;				\
146
  typedef typename ::lemon::_graph_utils_bits::				\
147
  BoolArcMap<Digraph>::type BoolArcMap;					\
148
  typedef typename ::lemon::_graph_utils_bits::				\
149
  IntArcMap<Digraph>::type IntArcMap;					\
150
  typedef typename ::lemon::_graph_utils_bits::				\
151
  DoubleArcMap<Digraph>::type DoubleArcMap
152

	
58 153

	
59 154
  ///Creates convenience typedefs for the graph types and iterators
60 155

	
61 156
  ///This \c \#define creates the same convenience typedefs as defined
62 157
  ///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
63 158
  ///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
64 159
  ///\c DoubleEdgeMap.
65 160
#define GRAPH_TYPEDEFS(Graph)						\
66 161
  DIGRAPH_TYPEDEFS(Graph);						\
67
  typedef Graph::Edge Edge;						\
68
  typedef Graph::EdgeIt EdgeIt;						\
69
  typedef Graph::IncEdgeIt IncEdgeIt
162
  typedef typename ::lemon::_graph_utils_bits::				\
163
  Edge<Graph>::type Edge;						\
164
  typedef typename ::lemon::_graph_utils_bits::				\
165
  EdgeIt<Graph>::type EdgeIt;						\
166
  typedef typename ::lemon::_graph_utils_bits::				\
167
  IncEdgeIt<Graph>::type IncEdgeIt					\
168
  typedef typename ::lemon::_graph_utils_bits::				\
169
  BoolEdgeMap<Graph>::type BoolEdgeMap;					\
170
  typedef typename ::lemon::_graph_utils_bits::				\
171
  IntEdgeMap<Graph>::type IntEdgeMap;					\
172
  typedef typename ::lemon::_graph_utils_bits::				\
173
  DoubleEdgeMap<Graph>::type DoubleEdgeMap
174

	
70 175

	
71 176
  /// \brief Function to count the items in the graph.
72 177
  ///
73 178
  /// This function counts the items (nodes, arcs etc) in the graph.
74 179
  /// The complexity of the function is O(n) because
75 180
  /// it iterates on all of the items.
76 181
  template <typename Graph, typename Item>
77 182
  inline int countItems(const Graph& g) {
78 183
    typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
79 184
    int num = 0;
80 185
    for (ItemIt it(g); it != INVALID; ++it) {
81 186
      ++num;
82 187
    }
83 188
    return num;
84 189
  }
85 190

	
86 191
  // Node counting:
87 192

	
88 193
  namespace _graph_utils_bits {
89 194
    
90 195
    template <typename Graph, typename Enable = void>
91 196
    struct CountNodesSelector {
92 197
      static int count(const Graph &g) {
93 198
        return countItems<Graph, typename Graph::Node>(g);
94 199
      }
95 200
    };
96 201

	
97 202
    template <typename Graph>
98 203
    struct CountNodesSelector<
99 204
      Graph, typename 
100 205
      enable_if<typename Graph::NodeNumTag, void>::type> 
101 206
    {
102 207
      static int count(const Graph &g) {
103 208
        return g.nodeNum();
104 209
      }
105 210
    };    
106 211
  }
107 212

	
108 213
  /// \brief Function to count the nodes in the graph.
109 214
  ///
110 215
  /// This function counts the nodes in the graph.
111 216
  /// The complexity of the function is O(n) but for some
112 217
  /// graph structures it is specialized to run in O(1).
113 218
  ///
114 219
  /// If the graph contains a \e nodeNum() member function and a 
115 220
  /// \e NodeNumTag tag then this function calls directly the member
116 221
  /// function to query the cardinality of the node set.
117 222
  template <typename Graph>
118 223
  inline int countNodes(const Graph& g) {
119 224
    return _graph_utils_bits::CountNodesSelector<Graph>::count(g);
120 225
  }
121 226

	
122 227
  // Arc counting:
123 228

	
124 229
  namespace _graph_utils_bits {
125 230
    
126 231
    template <typename Graph, typename Enable = void>
127 232
    struct CountArcsSelector {
128 233
      static int count(const Graph &g) {
129 234
        return countItems<Graph, typename Graph::Arc>(g);
130 235
      }
131 236
    };
132 237

	
133 238
    template <typename Graph>
134 239
    struct CountArcsSelector<
135 240
      Graph, 
136 241
      typename enable_if<typename Graph::ArcNumTag, void>::type> 
137 242
    {
138 243
      static int count(const Graph &g) {
139 244
        return g.arcNum();
140 245
      }
141 246
    };    
142 247
  }
143 248

	
144 249
  /// \brief Function to count the arcs in the graph.
145 250
  ///
146 251
  /// This function counts the arcs in the graph.
147 252
  /// The complexity of the function is O(e) but for some
148 253
  /// graph structures it is specialized to run in O(1).
149 254
  ///
150 255
  /// If the graph contains a \e arcNum() member function and a 
151 256
  /// \e EdgeNumTag tag then this function calls directly the member
152 257
  /// function to query the cardinality of the arc set.
153 258
  template <typename Graph>
154 259
  inline int countArcs(const Graph& g) {
155 260
    return _graph_utils_bits::CountArcsSelector<Graph>::count(g);
156 261
  }
157 262

	
158 263
  // Edge counting:
159 264
  namespace _graph_utils_bits {
160 265
    
161 266
    template <typename Graph, typename Enable = void>
162 267
    struct CountEdgesSelector {
163 268
      static int count(const Graph &g) {
164 269
        return countItems<Graph, typename Graph::Edge>(g);
165 270
      }
166 271
    };
167 272

	
168 273
    template <typename Graph>
169 274
    struct CountEdgesSelector<
170 275
      Graph, 
171 276
      typename enable_if<typename Graph::EdgeNumTag, void>::type> 
172 277
    {
173 278
      static int count(const Graph &g) {
174 279
        return g.edgeNum();
175 280
      }
176 281
    };    
177 282
  }
178 283

	
179 284
  /// \brief Function to count the edges in the graph.
180 285
  ///
181 286
  /// This function counts the edges in the graph.
182 287
  /// The complexity of the function is O(m) but for some
183 288
  /// graph structures it is specialized to run in O(1).
184 289
  ///
185 290
  /// If the graph contains a \e edgeNum() member function and a 
186 291
  /// \e EdgeNumTag tag then this function calls directly the member
187 292
  /// function to query the cardinality of the edge set.
188 293
  template <typename Graph>
189 294
  inline int countEdges(const Graph& g) {
190 295
    return _graph_utils_bits::CountEdgesSelector<Graph>::count(g);
191 296

	
192 297
  }
193 298

	
194 299

	
195 300
  template <typename Graph, typename DegIt>
196 301
  inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
197 302
    int num = 0;
198 303
    for (DegIt it(_g, _n); it != INVALID; ++it) {
199 304
      ++num;
200 305
    }
201 306
    return num;
202 307
  }
203 308

	
204 309
  /// \brief Function to count the number of the out-arcs from node \c n.
205 310
  ///
206 311
  /// This function counts the number of the out-arcs from node \c n
207 312
  /// in the graph.  
208 313
  template <typename Graph>
209 314
  inline int countOutArcs(const Graph& _g,  const typename Graph::Node& _n) {
210 315
    return countNodeDegree<Graph, typename Graph::OutArcIt>(_g, _n);
211 316
  }
212 317

	
213 318
  /// \brief Function to count the number of the in-arcs to node \c n.
214 319
  ///
215 320
  /// This function counts the number of the in-arcs to node \c n
216 321
  /// in the graph.  
217 322
  template <typename Graph>
218 323
  inline int countInArcs(const Graph& _g,  const typename Graph::Node& _n) {
219 324
    return countNodeDegree<Graph, typename Graph::InArcIt>(_g, _n);
220 325
  }
221 326

	
222 327
  /// \brief Function to count the number of the inc-edges to node \c n.
223 328
  ///
224 329
  /// This function counts the number of the inc-edges to node \c n
225 330
  /// in the graph.  
226 331
  template <typename Graph>
227 332
  inline int countIncEdges(const Graph& _g,  const typename Graph::Node& _n) {
228 333
    return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
229 334
  }
230 335

	
231 336
  namespace _graph_utils_bits {
232 337
    
233 338
    template <typename Graph, typename Enable = void>
234 339
    struct FindArcSelector {
235 340
      typedef typename Graph::Node Node;
236 341
      typedef typename Graph::Arc Arc;
237 342
      static Arc find(const Graph &g, Node u, Node v, Arc e) {
238 343
        if (e == INVALID) {
239 344
          g.firstOut(e, u);
240 345
        } else {
241 346
          g.nextOut(e);
242 347
        }
243 348
        while (e != INVALID && g.target(e) != v) {
244 349
          g.nextOut(e);
245 350
        }
246 351
        return e;
247 352
      }
248 353
    };
249 354

	
250 355
    template <typename Graph>
251 356
    struct FindArcSelector<
252 357
      Graph, 
253 358
      typename enable_if<typename Graph::FindEdgeTag, void>::type> 
254 359
    {
255 360
      typedef typename Graph::Node Node;
256 361
      typedef typename Graph::Arc Arc;
257 362
      static Arc find(const Graph &g, Node u, Node v, Arc prev) {
258 363
        return g.findArc(u, v, prev);
259 364
      }
260 365
    };    
261 366
  }
... ...
@@ -1867,385 +1972,385 @@
1867 1972
    
1868 1973
    typedef typename Digraph::Arc Arc;
1869 1974

	
1870 1975
    virtual void add(const Arc& arc) {
1871 1976
      ++_deg[_digraph.target(arc)];
1872 1977
    }
1873 1978

	
1874 1979
    virtual void add(const std::vector<Arc>& arcs) {
1875 1980
      for (int i = 0; i < int(arcs.size()); ++i) {
1876 1981
        ++_deg[_digraph.target(arcs[i])];
1877 1982
      }
1878 1983
    }
1879 1984

	
1880 1985
    virtual void erase(const Arc& arc) {
1881 1986
      --_deg[_digraph.target(arc)];
1882 1987
    }
1883 1988

	
1884 1989
    virtual void erase(const std::vector<Arc>& arcs) {
1885 1990
      for (int i = 0; i < int(arcs.size()); ++i) {
1886 1991
        --_deg[_digraph.target(arcs[i])];
1887 1992
      }
1888 1993
    }
1889 1994

	
1890 1995
    virtual void build() {
1891 1996
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
1892 1997
	_deg[it] = countInArcs(_digraph, it);
1893 1998
      }      
1894 1999
    }
1895 2000

	
1896 2001
    virtual void clear() {
1897 2002
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
1898 2003
	_deg[it] = 0;
1899 2004
      }
1900 2005
    }
1901 2006
  private:
1902 2007
    
1903 2008
    const Digraph& _digraph;
1904 2009
    AutoNodeMap _deg;
1905 2010
  };
1906 2011

	
1907 2012
  /// \brief Map of the node out-degrees.
1908 2013
  ///
1909 2014
  /// This map returns the out-degree of a node. Once it is constructed,
1910 2015
  /// the degrees are stored in a standard NodeMap, so each query is done
1911 2016
  /// in constant time. On the other hand, the values are updated automatically
1912 2017
  /// whenever the digraph changes.
1913 2018
  ///
1914 2019
  /// \warning Besides addNode() and addArc(), a digraph structure may provide
1915 2020
  /// alternative ways to modify the digraph. The correct behavior of OutDegMap
1916 2021
  /// is not guarantied if these additional features are used. For example
1917 2022
  /// the functions \ref ListDigraph::changeSource() "changeSource()",
1918 2023
  /// \ref ListDigraph::changeTarget() "changeTarget()" and
1919 2024
  /// \ref ListDigraph::reverseArc() "reverseArc()"
1920 2025
  /// of \ref ListDigraph will \e not update the degree values correctly.
1921 2026
  ///
1922 2027
  /// \sa InDegMap
1923 2028

	
1924 2029
  template <typename _Digraph>
1925 2030
  class OutDegMap  
1926 2031
    : protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
1927 2032
      ::ItemNotifier::ObserverBase {
1928 2033

	
1929 2034
  public:
1930 2035
    
1931 2036
    typedef _Digraph Digraph;
1932 2037
    typedef int Value;
1933 2038
    typedef typename Digraph::Node Key;
1934 2039

	
1935 2040
    typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
1936 2041
    ::ItemNotifier::ObserverBase Parent;
1937 2042

	
1938 2043
  private:
1939 2044

	
1940 2045
    class AutoNodeMap : public DefaultMap<Digraph, Key, int> {
1941 2046
    public:
1942 2047

	
1943 2048
      typedef DefaultMap<Digraph, Key, int> Parent;
1944 2049

	
1945 2050
      AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
1946 2051
      
1947 2052
      virtual void add(const Key& key) {
1948 2053
	Parent::add(key);
1949 2054
	Parent::set(key, 0);
1950 2055
      }
1951 2056
      virtual void add(const std::vector<Key>& keys) {
1952 2057
	Parent::add(keys);
1953 2058
	for (int i = 0; i < int(keys.size()); ++i) {
1954 2059
	  Parent::set(keys[i], 0);
1955 2060
	}
1956 2061
      }
1957 2062
      virtual void build() {
1958 2063
	Parent::build();
1959 2064
	Key it;
1960 2065
	typename Parent::Notifier* nf = Parent::notifier();
1961 2066
	for (nf->first(it); it != INVALID; nf->next(it)) {
1962 2067
	  Parent::set(it, 0);
1963 2068
	}
1964 2069
      }
1965 2070
    };
1966 2071

	
1967 2072
  public:
1968 2073

	
1969 2074
    /// \brief Constructor.
1970 2075
    ///
1971 2076
    /// Constructor for creating out-degree map.
1972 2077
    explicit OutDegMap(const Digraph& digraph) 
1973 2078
      : _digraph(digraph), _deg(digraph) {
1974 2079
      Parent::attach(_digraph.notifier(typename Digraph::Arc()));
1975 2080
      
1976 2081
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
1977 2082
	_deg[it] = countOutArcs(_digraph, it);
1978 2083
      }
1979 2084
    }
1980 2085

	
1981 2086
    /// Gives back the out-degree of a Node.
1982 2087
    int operator[](const Key& key) const {
1983 2088
      return _deg[key];
1984 2089
    }
1985 2090

	
1986 2091
  protected:
1987 2092
    
1988 2093
    typedef typename Digraph::Arc Arc;
1989 2094

	
1990 2095
    virtual void add(const Arc& arc) {
1991 2096
      ++_deg[_digraph.source(arc)];
1992 2097
    }
1993 2098

	
1994 2099
    virtual void add(const std::vector<Arc>& arcs) {
1995 2100
      for (int i = 0; i < int(arcs.size()); ++i) {
1996 2101
        ++_deg[_digraph.source(arcs[i])];
1997 2102
      }
1998 2103
    }
1999 2104

	
2000 2105
    virtual void erase(const Arc& arc) {
2001 2106
      --_deg[_digraph.source(arc)];
2002 2107
    }
2003 2108

	
2004 2109
    virtual void erase(const std::vector<Arc>& arcs) {
2005 2110
      for (int i = 0; i < int(arcs.size()); ++i) {
2006 2111
        --_deg[_digraph.source(arcs[i])];
2007 2112
      }
2008 2113
    }
2009 2114

	
2010 2115
    virtual void build() {
2011 2116
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2012 2117
	_deg[it] = countOutArcs(_digraph, it);
2013 2118
      }      
2014 2119
    }
2015 2120

	
2016 2121
    virtual void clear() {
2017 2122
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2018 2123
	_deg[it] = 0;
2019 2124
      }
2020 2125
    }
2021 2126
  private:
2022 2127
    
2023 2128
    const Digraph& _digraph;
2024 2129
    AutoNodeMap _deg;
2025 2130
  };
2026 2131

	
2027 2132

	
2028 2133
  ///Dynamic arc look up between given endpoints.
2029 2134
  
2030 2135
  ///\ingroup gutils
2031 2136
  ///Using this class, you can find an arc in a digraph from a given
2032 2137
  ///source to a given target in amortized time <em>O(log d)</em>,
2033 2138
  ///where <em>d</em> is the out-degree of the source node.
2034 2139
  ///
2035 2140
  ///It is possible to find \e all parallel arcs between two nodes with
2036 2141
  ///the \c findFirst() and \c findNext() members.
2037 2142
  ///
2038 2143
  ///See the \ref ArcLookUp and \ref AllArcLookUp classes if your
2039 2144
  ///digraph is not changed so frequently.
2040 2145
  ///
2041 2146
  ///This class uses a self-adjusting binary search tree, Sleator's
2042 2147
  ///and Tarjan's Splay tree for guarantee the logarithmic amortized
2043 2148
  ///time bound for arc lookups. This class also guarantees the
2044 2149
  ///optimal time bound in a constant factor for any distribution of
2045 2150
  ///queries.
2046 2151
  ///
2047 2152
  ///\param G The type of the underlying digraph.  
2048 2153
  ///
2049 2154
  ///\sa ArcLookUp  
2050 2155
  ///\sa AllArcLookUp  
2051 2156
  template<class G>
2052 2157
  class DynArcLookUp 
2053 2158
    : protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase
2054 2159
  {
2055 2160
  public:
2056 2161
    typedef typename ItemSetTraits<G, typename G::Arc>
2057 2162
    ::ItemNotifier::ObserverBase Parent;
2058 2163

	
2059
    DIGRAPH_TYPEDEFS(typename G);
2164
    DIGRAPH_TYPEDEFS(G);
2060 2165
    typedef G Digraph;
2061 2166

	
2062 2167
  protected:
2063 2168

	
2064 2169
    class AutoNodeMap : public DefaultMap<G, Node, Arc> {
2065 2170
    public:
2066 2171

	
2067 2172
      typedef DefaultMap<G, Node, Arc> Parent;
2068 2173

	
2069 2174
      AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {}
2070 2175
      
2071 2176
      virtual void add(const Node& node) {
2072 2177
	Parent::add(node);
2073 2178
	Parent::set(node, INVALID);
2074 2179
      }
2075 2180

	
2076 2181
      virtual void add(const std::vector<Node>& nodes) {
2077 2182
	Parent::add(nodes);
2078 2183
	for (int i = 0; i < int(nodes.size()); ++i) {
2079 2184
	  Parent::set(nodes[i], INVALID);
2080 2185
	}
2081 2186
      }
2082 2187

	
2083 2188
      virtual void build() {
2084 2189
	Parent::build();
2085 2190
	Node it;
2086 2191
	typename Parent::Notifier* nf = Parent::notifier();
2087 2192
	for (nf->first(it); it != INVALID; nf->next(it)) {
2088 2193
	  Parent::set(it, INVALID);
2089 2194
	}
2090 2195
      }
2091 2196
    };
2092 2197

	
2093 2198
    const Digraph &_g;
2094 2199
    AutoNodeMap _head;
2095 2200
    typename Digraph::template ArcMap<Arc> _parent;
2096 2201
    typename Digraph::template ArcMap<Arc> _left;
2097 2202
    typename Digraph::template ArcMap<Arc> _right;
2098 2203
    
2099 2204
    class ArcLess {
2100 2205
      const Digraph &g;
2101 2206
    public:
2102 2207
      ArcLess(const Digraph &_g) : g(_g) {}
2103 2208
      bool operator()(Arc a,Arc b) const 
2104 2209
      {
2105 2210
	return g.target(a)<g.target(b);
2106 2211
      }
2107 2212
    };
2108 2213
    
2109 2214
  public:
2110 2215
    
2111 2216
    ///Constructor
2112 2217

	
2113 2218
    ///Constructor.
2114 2219
    ///
2115 2220
    ///It builds up the search database.
2116 2221
    DynArcLookUp(const Digraph &g) 
2117 2222
      : _g(g),_head(g),_parent(g),_left(g),_right(g) 
2118 2223
    { 
2119 2224
      Parent::attach(_g.notifier(typename Digraph::Arc()));
2120 2225
      refresh(); 
2121 2226
    }
2122 2227
    
2123 2228
  protected:
2124 2229

	
2125 2230
    virtual void add(const Arc& arc) {
2126 2231
      insert(arc);
2127 2232
    }
2128 2233

	
2129 2234
    virtual void add(const std::vector<Arc>& arcs) {
2130 2235
      for (int i = 0; i < int(arcs.size()); ++i) {
2131 2236
	insert(arcs[i]);
2132 2237
      }
2133 2238
    }
2134 2239

	
2135 2240
    virtual void erase(const Arc& arc) {
2136 2241
      remove(arc);
2137 2242
    }
2138 2243

	
2139 2244
    virtual void erase(const std::vector<Arc>& arcs) {
2140 2245
      for (int i = 0; i < int(arcs.size()); ++i) {
2141 2246
	remove(arcs[i]);
2142 2247
      }     
2143 2248
    }
2144 2249

	
2145 2250
    virtual void build() {
2146 2251
      refresh();
2147 2252
    }
2148 2253

	
2149 2254
    virtual void clear() {
2150 2255
      for(NodeIt n(_g);n!=INVALID;++n) {
2151 2256
	_head.set(n, INVALID);
2152 2257
      }
2153 2258
    }
2154 2259

	
2155 2260
    void insert(Arc arc) {
2156 2261
      Node s = _g.source(arc);
2157 2262
      Node t = _g.target(arc);
2158 2263
      _left.set(arc, INVALID);
2159 2264
      _right.set(arc, INVALID);
2160 2265
      
2161 2266
      Arc e = _head[s];
2162 2267
      if (e == INVALID) {
2163 2268
	_head.set(s, arc);
2164 2269
	_parent.set(arc, INVALID);
2165 2270
	return;
2166 2271
      }
2167 2272
      while (true) {
2168 2273
	if (t < _g.target(e)) {
2169 2274
	  if (_left[e] == INVALID) {
2170 2275
	    _left.set(e, arc);
2171 2276
	    _parent.set(arc, e);
2172 2277
	    splay(arc);
2173 2278
	    return;
2174 2279
	  } else {
2175 2280
	    e = _left[e];
2176 2281
	  }
2177 2282
	} else {
2178 2283
	  if (_right[e] == INVALID) {
2179 2284
	    _right.set(e, arc);
2180 2285
	    _parent.set(arc, e);
2181 2286
	    splay(arc);
2182 2287
	    return;
2183 2288
	  } else {
2184 2289
	    e = _right[e];
2185 2290
	  }
2186 2291
	}
2187 2292
      }
2188 2293
    }
2189 2294

	
2190 2295
    void remove(Arc arc) {
2191 2296
      if (_left[arc] == INVALID) {
2192 2297
	if (_right[arc] != INVALID) {
2193 2298
	  _parent.set(_right[arc], _parent[arc]);
2194 2299
	}
2195 2300
	if (_parent[arc] != INVALID) {
2196 2301
	  if (_left[_parent[arc]] == arc) {
2197 2302
	    _left.set(_parent[arc], _right[arc]);
2198 2303
	  } else {
2199 2304
	    _right.set(_parent[arc], _right[arc]);
2200 2305
	  }
2201 2306
	} else {
2202 2307
	  _head.set(_g.source(arc), _right[arc]);
2203 2308
	}
2204 2309
      } else if (_right[arc] == INVALID) {
2205 2310
	_parent.set(_left[arc], _parent[arc]);
2206 2311
	if (_parent[arc] != INVALID) {
2207 2312
	  if (_left[_parent[arc]] == arc) {
2208 2313
	    _left.set(_parent[arc], _left[arc]);
2209 2314
	  } else {
2210 2315
	    _right.set(_parent[arc], _left[arc]);
2211 2316
	  }
2212 2317
	} else {
2213 2318
	  _head.set(_g.source(arc), _left[arc]);
2214 2319
	}
2215 2320
      } else {
2216 2321
	Arc e = _left[arc];
2217 2322
	if (_right[e] != INVALID) {
2218 2323
	  e = _right[e];	  
2219 2324
	  while (_right[e] != INVALID) {
2220 2325
	    e = _right[e];
2221 2326
	  }
2222 2327
	  Arc s = _parent[e];
2223 2328
	  _right.set(_parent[e], _left[e]);
2224 2329
	  if (_left[e] != INVALID) {
2225 2330
	    _parent.set(_left[e], _parent[e]);
2226 2331
	  }
2227 2332
	  
2228 2333
	  _left.set(e, _left[arc]);
2229 2334
	  _parent.set(_left[arc], e);
2230 2335
	  _right.set(e, _right[arc]);
2231 2336
	  _parent.set(_right[arc], e);
2232 2337

	
2233 2338
	  _parent.set(e, _parent[arc]);
2234 2339
	  if (_parent[arc] != INVALID) {
2235 2340
	    if (_left[_parent[arc]] == arc) {
2236 2341
	      _left.set(_parent[arc], e);
2237 2342
	    } else {
2238 2343
	      _right.set(_parent[arc], e);
2239 2344
	    }
2240 2345
	  }
2241 2346
	  splay(s);
2242 2347
	} else {
2243 2348
	  _right.set(e, _right[arc]);
2244 2349
	  _parent.set(_right[arc], e);
2245 2350

	
2246 2351
	  if (_parent[arc] != INVALID) {
2247 2352
	    if (_left[_parent[arc]] == arc) {
2248 2353
	      _left.set(_parent[arc], e);
2249 2354
	    } else {
2250 2355
	      _right.set(_parent[arc], e);
2251 2356
	    }
... ...
@@ -2304,412 +2409,412 @@
2304 2409
	  _left.set(_parent[v], v);
2305 2410
	}
2306 2411
      }
2307 2412
      if (_left[w] != INVALID){
2308 2413
	_parent.set(_left[w], w);
2309 2414
      }
2310 2415
    }
2311 2416

	
2312 2417
    void zag(Arc v) {        
2313 2418
      Arc w = _parent[v];
2314 2419
      _parent.set(v, _parent[w]);
2315 2420
      _parent.set(w, v);
2316 2421
      _right.set(w, _left[v]);
2317 2422
      _left.set(v, w);
2318 2423
      if (_parent[v] != INVALID){
2319 2424
	if (_left[_parent[v]] == w) {
2320 2425
	  _left.set(_parent[v], v);
2321 2426
	} else {
2322 2427
	  _right.set(_parent[v], v);
2323 2428
	}
2324 2429
      }
2325 2430
      if (_right[w] != INVALID){
2326 2431
	_parent.set(_right[w], w);
2327 2432
      }
2328 2433
    }
2329 2434

	
2330 2435
    void splay(Arc v) {
2331 2436
      while (_parent[v] != INVALID) {
2332 2437
	if (v == _left[_parent[v]]) {
2333 2438
	  if (_parent[_parent[v]] == INVALID) {
2334 2439
	    zig(v);
2335 2440
	  } else {
2336 2441
	    if (_parent[v] == _left[_parent[_parent[v]]]) {
2337 2442
	      zig(_parent[v]);
2338 2443
	      zig(v);
2339 2444
	    } else {
2340 2445
	      zig(v);
2341 2446
	      zag(v);
2342 2447
	    }
2343 2448
	  }
2344 2449
	} else {
2345 2450
	  if (_parent[_parent[v]] == INVALID) {
2346 2451
	    zag(v);
2347 2452
	  } else {
2348 2453
	    if (_parent[v] == _left[_parent[_parent[v]]]) {
2349 2454
	      zag(v);
2350 2455
	      zig(v);
2351 2456
	    } else {
2352 2457
	      zag(_parent[v]);
2353 2458
	      zag(v);
2354 2459
	    }
2355 2460
	  }
2356 2461
	}
2357 2462
      }
2358 2463
      _head[_g.source(v)] = v;
2359 2464
    }
2360 2465

	
2361 2466

	
2362 2467
  public:
2363 2468
    
2364 2469
    ///Find an arc between two nodes.
2365 2470
    
2366 2471
    ///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where
2367 2472
    /// <em>d</em> is the number of outgoing arcs of \c s.
2368 2473
    ///\param s The source node
2369 2474
    ///\param t The target node
2370 2475
    ///\return An arc from \c s to \c t if there exists,
2371 2476
    ///\ref INVALID otherwise.
2372 2477
    Arc operator()(Node s, Node t) const
2373 2478
    {
2374 2479
      Arc a = _head[s];
2375 2480
      while (true) {
2376 2481
	if (_g.target(a) == t) {
2377 2482
	  const_cast<DynArcLookUp&>(*this).splay(a);
2378 2483
	  return a;
2379 2484
	} else if (t < _g.target(a)) {
2380 2485
	  if (_left[a] == INVALID) {
2381 2486
	    const_cast<DynArcLookUp&>(*this).splay(a);
2382 2487
	    return INVALID;
2383 2488
	  } else {
2384 2489
	    a = _left[a];
2385 2490
	  }
2386 2491
	} else  {
2387 2492
	  if (_right[a] == INVALID) {
2388 2493
	    const_cast<DynArcLookUp&>(*this).splay(a);
2389 2494
	    return INVALID;
2390 2495
	  } else {
2391 2496
	    a = _right[a];
2392 2497
	  }
2393 2498
	}
2394 2499
      }
2395 2500
    }
2396 2501

	
2397 2502
    ///Find the first arc between two nodes.
2398 2503
    
2399 2504
    ///Find the first arc between two nodes in time
2400 2505
    /// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
2401 2506
    /// outgoing arcs of \c s.  
2402 2507
    ///\param s The source node 
2403 2508
    ///\param t The target node
2404 2509
    ///\return An arc from \c s to \c t if there exists, \ref INVALID
2405 2510
    /// otherwise.
2406 2511
    Arc findFirst(Node s, Node t) const
2407 2512
    {
2408 2513
      Arc a = _head[s];
2409 2514
      Arc r = INVALID;
2410 2515
      while (true) {
2411 2516
	if (_g.target(a) < t) {
2412 2517
	  if (_right[a] == INVALID) {
2413 2518
	    const_cast<DynArcLookUp&>(*this).splay(a);
2414 2519
	    return r;
2415 2520
	  } else {
2416 2521
	    a = _right[a];
2417 2522
	  }
2418 2523
	} else {
2419 2524
	  if (_g.target(a) == t) {
2420 2525
	    r = a;
2421 2526
	  }
2422 2527
	  if (_left[a] == INVALID) {
2423 2528
	    const_cast<DynArcLookUp&>(*this).splay(a);
2424 2529
	    return r;
2425 2530
	  } else {
2426 2531
	    a = _left[a];
2427 2532
	  }
2428 2533
	}
2429 2534
      }
2430 2535
    }
2431 2536

	
2432 2537
    ///Find the next arc between two nodes.
2433 2538
    
2434 2539
    ///Find the next arc between two nodes in time
2435 2540
    /// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
2436 2541
    /// outgoing arcs of \c s.  
2437 2542
    ///\param s The source node 
2438 2543
    ///\param t The target node
2439 2544
    ///\return An arc from \c s to \c t if there exists, \ref INVALID
2440 2545
    /// otherwise.
2441 2546

	
2442 2547
    ///\note If \c e is not the result of the previous \c findFirst()
2443 2548
    ///operation then the amorized time bound can not be guaranteed.
2444 2549
#ifdef DOXYGEN
2445 2550
    Arc findNext(Node s, Node t, Arc a) const
2446 2551
#else
2447 2552
    Arc findNext(Node, Node t, Arc a) const
2448 2553
#endif
2449 2554
    {
2450 2555
      if (_right[a] != INVALID) {
2451 2556
	a = _right[a];
2452 2557
	while (_left[a] != INVALID) {
2453 2558
	  a = _left[a];
2454 2559
	}
2455 2560
	const_cast<DynArcLookUp&>(*this).splay(a);
2456 2561
      } else {
2457 2562
	while (_parent[a] != INVALID && _right[_parent[a]] ==  a) {
2458 2563
	  a = _parent[a];
2459 2564
	}
2460 2565
	if (_parent[a] == INVALID) {
2461 2566
	  return INVALID;
2462 2567
	} else {
2463 2568
	  a = _parent[a];
2464 2569
	  const_cast<DynArcLookUp&>(*this).splay(a);
2465 2570
	}
2466 2571
      }
2467 2572
      if (_g.target(a) == t) return a;
2468 2573
      else return INVALID;    
2469 2574
    }
2470 2575

	
2471 2576
  };
2472 2577

	
2473 2578
  ///Fast arc look up between given endpoints.
2474 2579
  
2475 2580
  ///\ingroup gutils
2476 2581
  ///Using this class, you can find an arc in a digraph from a given
2477 2582
  ///source to a given target in time <em>O(log d)</em>,
2478 2583
  ///where <em>d</em> is the out-degree of the source node.
2479 2584
  ///
2480 2585
  ///It is not possible to find \e all parallel arcs between two nodes.
2481 2586
  ///Use \ref AllArcLookUp for this purpose.
2482 2587
  ///
2483 2588
  ///\warning This class is static, so you should refresh() (or at least
2484 2589
  ///refresh(Node)) this data structure
2485 2590
  ///whenever the digraph changes. This is a time consuming (superlinearly
2486 2591
  ///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs).
2487 2592
  ///
2488 2593
  ///\param G The type of the underlying digraph.
2489 2594
  ///
2490 2595
  ///\sa DynArcLookUp
2491 2596
  ///\sa AllArcLookUp  
2492 2597
  template<class G>
2493 2598
  class ArcLookUp 
2494 2599
  {
2495 2600
  public:
2496
    DIGRAPH_TYPEDEFS(typename G);
2601
    DIGRAPH_TYPEDEFS(G);
2497 2602
    typedef G Digraph;
2498 2603

	
2499 2604
  protected:
2500 2605
    const Digraph &_g;
2501 2606
    typename Digraph::template NodeMap<Arc> _head;
2502 2607
    typename Digraph::template ArcMap<Arc> _left;
2503 2608
    typename Digraph::template ArcMap<Arc> _right;
2504 2609
    
2505 2610
    class ArcLess {
2506 2611
      const Digraph &g;
2507 2612
    public:
2508 2613
      ArcLess(const Digraph &_g) : g(_g) {}
2509 2614
      bool operator()(Arc a,Arc b) const 
2510 2615
      {
2511 2616
	return g.target(a)<g.target(b);
2512 2617
      }
2513 2618
    };
2514 2619
    
2515 2620
  public:
2516 2621
    
2517 2622
    ///Constructor
2518 2623

	
2519 2624
    ///Constructor.
2520 2625
    ///
2521 2626
    ///It builds up the search database, which remains valid until the digraph
2522 2627
    ///changes.
2523 2628
    ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
2524 2629
    
2525 2630
  private:
2526 2631
    Arc refreshRec(std::vector<Arc> &v,int a,int b) 
2527 2632
    {
2528 2633
      int m=(a+b)/2;
2529 2634
      Arc me=v[m];
2530 2635
      _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
2531 2636
      _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
2532 2637
      return me;
2533 2638
    }
2534 2639
  public:
2535 2640
    ///Refresh the data structure at a node.
2536 2641

	
2537 2642
    ///Build up the search database of node \c n.
2538 2643
    ///
2539 2644
    ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
2540 2645
    ///the number of the outgoing arcs of \c n.
2541 2646
    void refresh(Node n) 
2542 2647
    {
2543 2648
      std::vector<Arc> v;
2544 2649
      for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
2545 2650
      if(v.size()) {
2546 2651
	std::sort(v.begin(),v.end(),ArcLess(_g));
2547 2652
	_head[n]=refreshRec(v,0,v.size()-1);
2548 2653
      }
2549 2654
      else _head[n]=INVALID;
2550 2655
    }
2551 2656
    ///Refresh the full data structure.
2552 2657

	
2553 2658
    ///Build up the full search database. In fact, it simply calls
2554 2659
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
2555 2660
    ///
2556 2661
    ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
2557 2662
    ///the number of the arcs of \c n and <em>D</em> is the maximum
2558 2663
    ///out-degree of the digraph.
2559 2664

	
2560 2665
    void refresh() 
2561 2666
    {
2562 2667
      for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
2563 2668
    }
2564 2669
    
2565 2670
    ///Find an arc between two nodes.
2566 2671
    
2567 2672
    ///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where
2568 2673
    /// <em>d</em> is the number of outgoing arcs of \c s.
2569 2674
    ///\param s The source node
2570 2675
    ///\param t The target node
2571 2676
    ///\return An arc from \c s to \c t if there exists,
2572 2677
    ///\ref INVALID otherwise.
2573 2678
    ///
2574 2679
    ///\warning If you change the digraph, refresh() must be called before using
2575 2680
    ///this operator. If you change the outgoing arcs of
2576 2681
    ///a single node \c n, then
2577 2682
    ///\ref refresh(Node) "refresh(n)" is enough.
2578 2683
    ///
2579 2684
    Arc operator()(Node s, Node t) const
2580 2685
    {
2581 2686
      Arc e;
2582 2687
      for(e=_head[s];
2583 2688
	  e!=INVALID&&_g.target(e)!=t;
2584 2689
	  e = t < _g.target(e)?_left[e]:_right[e]) ;
2585 2690
      return e;
2586 2691
    }
2587 2692

	
2588 2693
  };
2589 2694

	
2590 2695
  ///Fast look up of all arcs between given endpoints.
2591 2696
  
2592 2697
  ///\ingroup gutils
2593 2698
  ///This class is the same as \ref ArcLookUp, with the addition
2594 2699
  ///that it makes it possible to find all arcs between given endpoints.
2595 2700
  ///
2596 2701
  ///\warning This class is static, so you should refresh() (or at least
2597 2702
  ///refresh(Node)) this data structure
2598 2703
  ///whenever the digraph changes. This is a time consuming (superlinearly
2599 2704
  ///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs).
2600 2705
  ///
2601 2706
  ///\param G The type of the underlying digraph.
2602 2707
  ///
2603 2708
  ///\sa DynArcLookUp
2604 2709
  ///\sa ArcLookUp  
2605 2710
  template<class G>
2606 2711
  class AllArcLookUp : public ArcLookUp<G>
2607 2712
  {
2608 2713
    using ArcLookUp<G>::_g;
2609 2714
    using ArcLookUp<G>::_right;
2610 2715
    using ArcLookUp<G>::_left;
2611 2716
    using ArcLookUp<G>::_head;
2612 2717

	
2613
    DIGRAPH_TYPEDEFS(typename G);
2718
    DIGRAPH_TYPEDEFS(G);
2614 2719
    typedef G Digraph;
2615 2720
    
2616 2721
    typename Digraph::template ArcMap<Arc> _next;
2617 2722
    
2618 2723
    Arc refreshNext(Arc head,Arc next=INVALID)
2619 2724
    {
2620 2725
      if(head==INVALID) return next;
2621 2726
      else {
2622 2727
	next=refreshNext(_right[head],next);
2623 2728
// 	_next[head]=next;
2624 2729
	_next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
2625 2730
	  ? next : INVALID;
2626 2731
	return refreshNext(_left[head],head);
2627 2732
      }
2628 2733
    }
2629 2734
    
2630 2735
    void refreshNext()
2631 2736
    {
2632 2737
      for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
2633 2738
    }
2634 2739
    
2635 2740
  public:
2636 2741
    ///Constructor
2637 2742

	
2638 2743
    ///Constructor.
2639 2744
    ///
2640 2745
    ///It builds up the search database, which remains valid until the digraph
2641 2746
    ///changes.
2642 2747
    AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();}
2643 2748

	
2644 2749
    ///Refresh the data structure at a node.
2645 2750

	
2646 2751
    ///Build up the search database of node \c n.
2647 2752
    ///
2648 2753
    ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
2649 2754
    ///the number of the outgoing arcs of \c n.
2650 2755
    
2651 2756
    void refresh(Node n) 
2652 2757
    {
2653 2758
      ArcLookUp<G>::refresh(n);
2654 2759
      refreshNext(_head[n]);
2655 2760
    }
2656 2761
    
2657 2762
    ///Refresh the full data structure.
2658 2763

	
2659 2764
    ///Build up the full search database. In fact, it simply calls
2660 2765
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
2661 2766
    ///
2662 2767
    ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
2663 2768
    ///the number of the arcs of \c n and <em>D</em> is the maximum
2664 2769
    ///out-degree of the digraph.
2665 2770

	
2666 2771
    void refresh() 
2667 2772
    {
2668 2773
      for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
2669 2774
    }
2670 2775
    
2671 2776
    ///Find an arc between two nodes.
2672 2777
    
2673 2778
    ///Find an arc between two nodes.
2674 2779
    ///\param s The source node
2675 2780
    ///\param t The target node
2676 2781
    ///\param prev The previous arc between \c s and \c t. It it is INVALID or
2677 2782
    ///not given, the operator finds the first appropriate arc.
2678 2783
    ///\return An arc from \c s to \c t after \c prev or
2679 2784
    ///\ref INVALID if there is no more.
2680 2785
    ///
2681 2786
    ///For example, you can count the number of arcs from \c u to \c v in the
2682 2787
    ///following way.
2683 2788
    ///\code
2684 2789
    ///AllArcLookUp<ListDigraph> ae(g);
2685 2790
    ///...
2686 2791
    ///int n=0;
2687 2792
    ///for(Arc e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++;
2688 2793
    ///\endcode
2689 2794
    ///
2690 2795
    ///Finding the first arc take <em>O(</em>log<em>d)</em> time, where
2691 2796
    /// <em>d</em> is the number of outgoing arcs of \c s. Then, the
2692 2797
    ///consecutive arcs are found in constant time.
2693 2798
    ///
2694 2799
    ///\warning If you change the digraph, refresh() must be called before using
2695 2800
    ///this operator. If you change the outgoing arcs of
2696 2801
    ///a single node \c n, then
2697 2802
    ///\ref refresh(Node) "refresh(n)" is enough.
2698 2803
    ///
2699 2804
#ifdef DOXYGEN
2700 2805
    Arc operator()(Node s, Node t, Arc prev=INVALID) const {}
2701 2806
#else
2702 2807
    using ArcLookUp<G>::operator() ;
2703 2808
    Arc operator()(Node s, Node t, Arc prev) const
2704 2809
    {
2705 2810
      return prev==INVALID?(*this)(s,t):_next[prev];
2706 2811
    }
2707 2812
#endif
2708 2813
      
2709 2814
  };
2710 2815

	
2711 2816
  /// @}
2712 2817

	
2713 2818
} //END OF NAMESPACE LEMON
2714 2819

	
2715 2820
#endif
Ignore white space 6 line context
... ...
@@ -113,385 +113,385 @@
113 113
    public:
114 114
      typedef _Value Value;
115 115
      typedef _Converter Converter;
116 116

	
117 117
    private:
118 118
      Value& _value;
119 119
      Converter _converter;
120 120

	
121 121
    public:
122 122
      ValueStorage(Value& value, const Converter& converter = Converter())
123 123
 	: _value(value), _converter(converter) {}
124 124

	
125 125
      virtual void set(const std::string& value) {
126 126
	_value = _converter(value);
127 127
      }
128 128
    };
129 129

	
130 130
    template <typename Value>
131 131
    struct MapLookUpConverter {
132 132
      const std::map<std::string, Value>& _map;
133 133

	
134 134
      MapLookUpConverter(const std::map<std::string, Value>& map)
135 135
        : _map(map) {}
136 136

	
137 137
      Value operator()(const std::string& str) {
138 138
        typename std::map<std::string, Value>::const_iterator it =
139 139
          _map.find(str);
140 140
        if (it == _map.end()) {
141 141
          std::ostringstream msg;
142 142
          msg << "Item not found: " << str;
143 143
          throw DataFormatError(msg.str().c_str());
144 144
        }
145 145
        return it->second;
146 146
      }
147 147
    };
148 148

	
149 149
    bool isWhiteSpace(char c) {
150 150
      return c == ' ' || c == '\t' || c == '\v' || 
151 151
        c == '\n' || c == '\r' || c == '\f'; 
152 152
    }
153 153
    
154 154
    bool isOct(char c) {
155 155
      return '0' <= c && c <='7'; 
156 156
    }
157 157
    
158 158
    int valueOct(char c) {
159 159
      LEMON_ASSERT(isOct(c), "The character is not octal.");
160 160
      return c - '0';
161 161
    }
162 162

	
163 163
    bool isHex(char c) {
164 164
      return ('0' <= c && c <= '9') || 
165 165
	('a' <= c && c <= 'z') || 
166 166
	('A' <= c && c <= 'Z'); 
167 167
    }
168 168
    
169 169
    int valueHex(char c) {
170 170
      LEMON_ASSERT(isHex(c), "The character is not hexadecimal.");
171 171
      if ('0' <= c && c <= '9') return c - '0';
172 172
      if ('a' <= c && c <= 'z') return c - 'a' + 10;
173 173
      return c - 'A' + 10;
174 174
    }
175 175

	
176 176
    bool isIdentifierFirstChar(char c) {
177 177
      return ('a' <= c && c <= 'z') ||
178 178
	('A' <= c && c <= 'Z') || c == '_';
179 179
    }
180 180

	
181 181
    bool isIdentifierChar(char c) {
182 182
      return isIdentifierFirstChar(c) ||
183 183
	('0' <= c && c <= '9');
184 184
    }
185 185

	
186 186
    char readEscape(std::istream& is) {
187 187
      char c;
188 188
      if (!is.get(c))
189 189
	throw DataFormatError("Escape format error");
190 190

	
191 191
      switch (c) {
192 192
      case '\\':
193 193
	return '\\';
194 194
      case '\"':
195 195
	return '\"';
196 196
      case '\'':
197 197
	return '\'';
198 198
      case '\?':
199 199
	return '\?';
200 200
      case 'a':
201 201
	return '\a';
202 202
      case 'b':
203 203
	return '\b';
204 204
      case 'f':
205 205
	return '\f';
206 206
      case 'n':
207 207
	return '\n';
208 208
      case 'r':
209 209
	return '\r';
210 210
      case 't':
211 211
	return '\t';
212 212
      case 'v':
213 213
	return '\v';
214 214
      case 'x':
215 215
	{
216 216
	  int code;
217 217
	  if (!is.get(c) || !isHex(c)) 
218 218
	    throw DataFormatError("Escape format error");
219 219
	  else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
220 220
	  else code = code * 16 + valueHex(c);
221 221
	  return code;
222 222
	}
223 223
      default:
224 224
	{
225 225
	  int code;
226 226
	  if (!isOct(c)) 
227 227
	    throw DataFormatError("Escape format error");
228 228
	  else if (code = valueOct(c), !is.get(c) || !isOct(c)) 
229 229
	    is.putback(c);
230 230
	  else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c)) 
231 231
	    is.putback(c);
232 232
	  else code = code * 8 + valueOct(c);
233 233
	  return code;
234 234
	}	      
235 235
      } 
236 236
    }
237 237
    
238 238
    std::istream& readToken(std::istream& is, std::string& str) {
239 239
      std::ostringstream os;
240 240

	
241 241
      char c;
242 242
      is >> std::ws;
243 243
      
244 244
      if (!is.get(c)) 
245 245
	return is;
246 246

	
247 247
      if (c == '\"') {
248 248
	while (is.get(c) && c != '\"') {
249 249
	  if (c == '\\') 
250 250
	    c = readEscape(is);
251 251
	  os << c;
252 252
	}
253 253
	if (!is) 
254 254
	  throw DataFormatError("Quoted format error");
255 255
      } else {
256 256
	is.putback(c);
257 257
	while (is.get(c) && !isWhiteSpace(c)) {
258 258
	  if (c == '\\') 
259 259
	    c = readEscape(is);
260 260
	  os << c;
261 261
	}
262 262
	if (!is) {
263 263
	  is.clear();
264 264
	} else {
265 265
	  is.putback(c);
266 266
	}
267 267
      }
268 268
      str = os.str();
269 269
      return is;
270 270
    }
271 271

	
272 272
    std::istream& readIdentifier(std::istream& is, std::string& str) {
273 273
      std::ostringstream os;
274 274

	
275 275
      char c;
276 276
      is >> std::ws;
277 277
      
278 278
      if (!is.get(c))
279 279
	return is;
280 280

	
281 281
      if (!isIdentifierFirstChar(c))
282 282
	throw DataFormatError("Wrong char in identifier");
283 283
      
284 284
      os << c;
285 285
      
286 286
      while (is.get(c) && !isWhiteSpace(c)) {
287 287
	if (!isIdentifierChar(c)) 
288 288
	  throw DataFormatError("Wrong char in identifier");	  
289 289
	os << c;
290 290
      }
291 291
      if (!is) is.clear();
292 292
     
293 293
      str = os.str();
294 294
      return is;
295 295
    }
296 296
    
297 297
  }
298 298
  
299 299
  /// \e
300 300
  template <typename _Digraph>
301 301
  class DigraphReader {
302 302
  public:
303 303

	
304 304
    typedef _Digraph Digraph;
305
    DIGRAPH_TYPEDEFS(typename Digraph);
305
    DIGRAPH_TYPEDEFS(Digraph);
306 306
    
307 307
  private:
308 308

	
309 309

	
310 310
    std::istream* _is;
311 311
    bool local_is;
312 312

	
313 313
    Digraph& _digraph;
314 314

	
315 315
    std::string _nodes_caption;
316 316
    std::string _arcs_caption;
317 317
    std::string _attributes_caption;
318 318

	
319 319
    typedef std::map<std::string, Node> NodeIndex;
320 320
    NodeIndex _node_index;
321 321
    typedef std::map<std::string, Arc> ArcIndex;
322 322
    ArcIndex _arc_index;
323 323
    
324 324
    typedef std::vector<std::pair<std::string, 
325 325
      _reader_bits::MapStorageBase<Node>*> > NodeMaps;    
326 326
    NodeMaps _node_maps; 
327 327

	
328 328
    typedef std::vector<std::pair<std::string,
329 329
      _reader_bits::MapStorageBase<Arc>*> >ArcMaps;
330 330
    ArcMaps _arc_maps;
331 331

	
332 332
    typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> 
333 333
      Attributes;
334 334
    Attributes _attributes;
335 335

	
336 336
    bool _use_nodes;
337 337
    bool _use_arcs;
338 338

	
339 339
    int line_num;
340 340
    std::istringstream line;
341 341

	
342 342
  public:
343 343

	
344 344
    /// \e
345 345
    DigraphReader(std::istream& is, Digraph& digraph) 
346 346
      : _is(&is), local_is(false), _digraph(digraph),
347 347
	_use_nodes(false), _use_arcs(false) {}
348 348

	
349 349
    /// \e
350 350
    DigraphReader(const std::string& fn, Digraph& digraph) 
351 351
      : _is(new std::ifstream(fn.c_str())), local_is(true), _digraph(digraph),
352 352
    	_use_nodes(false), _use_arcs(false) {}
353 353

	
354 354

	
355 355
    /// \e
356 356
    DigraphReader(const char* fn, Digraph& digraph) 
357 357
      : _is(new std::ifstream(fn)), local_is(true), _digraph(digraph),
358 358
    	_use_nodes(false), _use_arcs(false) {}
359 359

	
360 360
    /// \e
361 361
    DigraphReader(DigraphReader& other) 
362 362
      : _is(other._is), local_is(other.local_is), _digraph(other._digraph),
363 363
	_use_nodes(other._use_nodes), _use_arcs(other._use_arcs) {
364 364

	
365 365
      other.is = 0;
366 366
      other.local_is = false;
367 367
      
368 368
      _node_index.swap(other._node_index);
369 369
      _arc_index.swap(other._arc_index);
370 370

	
371 371
      _node_maps.swap(other._node_maps);
372 372
      _arc_maps.swap(other._arc_maps);
373 373
      _attributes.swap(other._attributes);
374 374

	
375 375
      _nodes_caption = other._nodes_caption;
376 376
      _arcs_caption = other._arcs_caption;
377 377
      _attributes_caption = other._attributes_caption;
378 378
    }
379 379

	
380 380
    /// \e
381 381
    ~DigraphReader() {
382 382
      for (typename NodeMaps::iterator it = _node_maps.begin(); 
383 383
	   it != _node_maps.end(); ++it) {
384 384
	delete it->second;
385 385
      }
386 386

	
387 387
      for (typename ArcMaps::iterator it = _arc_maps.begin(); 
388 388
	   it != _arc_maps.end(); ++it) {
389 389
	delete it->second;
390 390
      }
391 391

	
392 392
      for (typename Attributes::iterator it = _attributes.begin(); 
393 393
	   it != _attributes.end(); ++it) {
394 394
	delete it->second;
395 395
      }
396 396

	
397 397
      if (local_is) {
398 398
	delete _is;
399 399
      }
400 400

	
401 401
    }
402 402

	
403 403
  private:
404 404
    
405 405
    DigraphReader& operator=(const DigraphReader&);
406 406

	
407 407
  public:
408 408

	
409 409
    /// \e
410 410
    template <typename Map>
411 411
    DigraphReader& nodeMap(const std::string& caption, Map& map) {
412 412
      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
413 413
      _reader_bits::MapStorageBase<Node>* storage = 
414 414
	new _reader_bits::MapStorage<Node, Map>(map);
415 415
      _node_maps.push_back(std::make_pair(caption, storage));
416 416
      return *this;
417 417
    }
418 418

	
419 419
    /// \e
420 420
    template <typename Map, typename Converter>
421 421
    DigraphReader& nodeMap(const std::string& caption, Map& map, 
422 422
			   const Converter& converter = Converter()) {
423 423
      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
424 424
      _reader_bits::MapStorageBase<Node>* storage = 
425 425
	new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
426 426
      _node_maps.push_back(std::make_pair(caption, storage));
427 427
      return *this;
428 428
    }
429 429

	
430 430
    /// \e
431 431
    template <typename Map>
432 432
    DigraphReader& arcMap(const std::string& caption, Map& map) {
433 433
      checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
434 434
      _reader_bits::MapStorageBase<Arc>* storage = 
435 435
	new _reader_bits::MapStorage<Arc, Map>(map);
436 436
      _arc_maps.push_back(std::make_pair(caption, storage));
437 437
      return *this;
438 438
    }
439 439

	
440 440
    /// \e
441 441
    template <typename Map, typename Converter>
442 442
    DigraphReader& arcMap(const std::string& caption, Map& map, 
443 443
			  const Converter& converter = Converter()) {
444 444
      checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
445 445
      _reader_bits::MapStorageBase<Arc>* storage = 
446 446
	new _reader_bits::MapStorage<Arc, Map, Converter>(map, converter);
447 447
      _arc_maps.push_back(std::make_pair(caption, storage));
448 448
      return *this;
449 449
    }
450 450

	
451 451
    /// \e
452 452
    template <typename Value>
453 453
    DigraphReader& attribute(const std::string& caption, Value& value) {
454 454
      _reader_bits::ValueStorageBase* storage = 
455 455
	new _reader_bits::ValueStorage<Value>(value);
456 456
      _attributes.insert(std::make_pair(caption, storage));
457 457
      return *this;
458 458
    }
459 459

	
460 460
    /// \e
461 461
    template <typename Value, typename Converter>
462 462
    DigraphReader& attribute(const std::string& caption, Value& value, 
463 463
			     const Converter& converter = Converter()) {
464 464
      _reader_bits::ValueStorageBase* storage = 
465 465
	new _reader_bits::ValueStorage<Value, Converter>(value, converter);
466 466
      _attributes.insert(std::make_pair(caption, storage));
467 467
      return *this;
468 468
    }
469 469

	
470 470
    /// \e
471 471
    DigraphReader& node(const std::string& caption, Node& node) {
472 472
      typedef _reader_bits::MapLookUpConverter<Node> Converter;
473 473
      Converter converter(_node_index);
474 474
      _reader_bits::ValueStorageBase* storage = 
475 475
	new _reader_bits::ValueStorage<Node, Converter>(node, converter);
476 476
      _attributes.insert(std::make_pair(caption, storage));
477 477
      return *this;
478 478
    }
479 479

	
480 480
    /// \e
481 481
    DigraphReader& arc(const std::string& caption, Arc& arc) {
482 482
      typedef _reader_bits::MapLookUpConverter<Arc> Converter;
483 483
      Converter converter(_arc_index);
484 484
      _reader_bits::ValueStorageBase* storage = 
485 485
	new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
486 486
      _attributes.insert(std::make_pair(caption, storage));
487 487
      return *this;
488 488
    }
489 489

	
490 490
    /// \e
491 491
    DigraphReader& nodes(const std::string& caption) {
492 492
      _nodes_caption = caption;
493 493
      return *this;
494 494
    }
495 495

	
496 496
    /// \e
497 497
    DigraphReader& arcs(const std::string& caption) {
Ignore white space 384 line context
... ...
@@ -48,385 +48,385 @@
48 48
	return os.str();
49 49
      }
50 50
    };
51 51

	
52 52
    template <typename T>
53 53
    bool operator<(const T&, const T&) {
54 54
      throw DataFormatError("Label map is not comparable");
55 55
    }
56 56

	
57 57
    template <typename _Map>
58 58
    class MapLess {
59 59
    public:
60 60
      typedef _Map Map;
61 61
      typedef typename Map::Key Item;
62 62

	
63 63
    private:
64 64
      const Map& _map;
65 65
      
66 66
    public:
67 67
      MapLess(const Map& map) : _map(map) {}
68 68

	
69 69
      bool operator()(const Item& left, const Item& right) {
70 70
	return _map[left] < _map[right];
71 71
      }
72 72
    };
73 73

	
74 74
    template <typename _Item>    
75 75
    class MapStorageBase {
76 76
    public:
77 77
      typedef _Item Item;
78 78

	
79 79
    public:
80 80
      MapStorageBase() {}
81 81
      virtual ~MapStorageBase() {}
82 82

	
83 83
      virtual std::string get(const Item& item) = 0;
84 84
      virtual void sort(std::vector<Item>&) = 0;
85 85
    };
86 86

	
87 87
    template <typename _Item, typename _Map, 
88 88
	      typename _Converter = DefaultConverter<typename _Map::Value> >
89 89
    class MapStorage : public MapStorageBase<_Item> {
90 90
    public:
91 91
      typedef _Map Map;
92 92
      typedef _Converter Converter;
93 93
      typedef _Item Item;
94 94
      
95 95
    private:
96 96
      const Map& _map;
97 97
      Converter _converter;
98 98

	
99 99
    public:
100 100
      MapStorage(const Map& map, const Converter& converter = Converter()) 
101 101
	: _map(map), _converter(converter) {}
102 102
      virtual ~MapStorage() {}
103 103

	
104 104
      virtual std::string get(const Item& item) {
105 105
	return _converter(_map[item]);
106 106
      }
107 107
      virtual void sort(std::vector<Item>& items) {
108 108
	MapLess<Map> less(_map);
109 109
	std::sort(items.begin(), items.end(), less);
110 110
      }
111 111
    };
112 112

	
113 113
    class ValueStorageBase {
114 114
    public:
115 115
      ValueStorageBase() {}
116 116
      virtual ~ValueStorageBase() {}
117 117

	
118 118
      virtual std::string get() = 0;      
119 119
    };
120 120

	
121 121
    template <typename _Value, typename _Converter = DefaultConverter<_Value> >
122 122
    class ValueStorage : public ValueStorageBase {
123 123
    public:
124 124
      typedef _Value Value;
125 125
      typedef _Converter Converter;
126 126

	
127 127
    private:
128 128
      const Value& _value;
129 129
      Converter _converter;
130 130

	
131 131
    public:
132 132
      ValueStorage(const Value& value, const Converter& converter = Converter())
133 133
 	: _value(value), _converter(converter) {}
134 134

	
135 135
      virtual std::string get() {
136 136
	return _converter(_value);
137 137
      }
138 138
    };
139 139

	
140 140
    template <typename Value>
141 141
    struct MapLookUpConverter {
142 142
      const std::map<Value, std::string>& _map;
143 143
      
144 144
      MapLookUpConverter(const std::map<Value, std::string>& map) 
145 145
	: _map(map) {}
146 146
      
147 147
      std::string operator()(const Value& str) {
148 148
	typename std::map<Value, std::string>::const_iterator it = 
149 149
	  _map.find(str);
150 150
	if (it == _map.end()) {
151 151
	  throw DataFormatError("Item not found");
152 152
	}
153 153
	return it->second;
154 154
      }
155 155
    };
156 156

	
157 157
    bool isWhiteSpace(char c) {
158 158
      return c == ' ' || c == '\t' || c == '\v' || 
159 159
        c == '\n' || c == '\r' || c == '\f'; 
160 160
    }
161 161

	
162 162
    bool isEscaped(char c) {
163 163
      return c == '\\' || c == '\"' || c == '\'' || 
164 164
	c == '\a' || c == '\b';
165 165
    }
166 166

	
167 167
    static void writeEscape(std::ostream& os, char c) {
168 168
      switch (c) {
169 169
      case '\\':
170 170
	os << "\\\\";
171 171
	return;
172 172
      case '\"':
173 173
	os << "\\\"";
174 174
	return;
175 175
      case '\a':
176 176
	os << "\\a";
177 177
	return;
178 178
      case '\b':
179 179
	os << "\\b";
180 180
	return;
181 181
      case '\f':
182 182
	os << "\\f";
183 183
	return;
184 184
      case '\r':
185 185
	os << "\\r";
186 186
	return;
187 187
      case '\n':
188 188
	os << "\\n";
189 189
	return;
190 190
      case '\t':
191 191
	os << "\\t";
192 192
	return;
193 193
      case '\v':
194 194
	os << "\\v";
195 195
	return;
196 196
      default:
197 197
	if (c < 0x20) {
198 198
	  os << '\\' << std::oct << static_cast<int>(c);
199 199
	} else {
200 200
	  os << c;
201 201
	}
202 202
	return;
203 203
      }     
204 204
    }
205 205

	
206 206
    bool requireEscape(const std::string& str) {
207 207
      std::istringstream is(str);
208 208
      char c;
209 209
      while (is.get(c)) {
210 210
	if (isWhiteSpace(c) || isEscaped(c)) {
211 211
	  return true;
212 212
	}
213 213
      }
214 214
      return false;
215 215
    }
216 216
    
217 217
    std::ostream& writeToken(std::ostream& os, const std::string& str) {
218 218

	
219 219
      if (requireEscape(str)) {
220 220
	os << '\"';
221 221
	for (std::string::const_iterator it = str.begin(); 
222 222
	     it != str.end(); ++it) {
223 223
	  writeEscape(os, *it);
224 224
	}	
225 225
	os << '\"';
226 226
      } else {
227 227
	os << str;
228 228
      }
229 229
      return os;
230 230
    }
231 231

	
232 232
  }
233 233
  
234 234
  /// \e
235 235
  template <typename _Digraph>
236 236
  class DigraphWriter {
237 237
  public:
238 238

	
239 239
    typedef _Digraph Digraph;
240
    DIGRAPH_TYPEDEFS(typename Digraph);
240
    DIGRAPH_TYPEDEFS(Digraph);
241 241
    
242 242
  private:
243 243

	
244 244

	
245 245
    std::ostream* _os;
246 246
    bool local_os;
247 247

	
248 248
    Digraph& _digraph;
249 249

	
250 250
    std::string _nodes_caption;
251 251
    std::string _arcs_caption;
252 252
    std::string _attributes_caption;
253 253
    
254 254
    typedef std::map<Node, std::string> NodeIndex;
255 255
    NodeIndex _node_index;
256 256
    typedef std::map<Arc, std::string> ArcIndex;
257 257
    ArcIndex _arc_index;
258 258

	
259 259
    typedef std::vector<std::pair<std::string, 
260 260
      _writer_bits::MapStorageBase<Node>* > > NodeMaps;    
261 261
    NodeMaps _node_maps; 
262 262

	
263 263
    typedef std::vector<std::pair<std::string, 
264 264
      _writer_bits::MapStorageBase<Arc>* > >ArcMaps;
265 265
    ArcMaps _arc_maps;
266 266

	
267 267
    typedef std::vector<std::pair<std::string, 
268 268
      _writer_bits::ValueStorageBase*> > Attributes;
269 269
    Attributes _attributes;
270 270

	
271 271
    bool _skip_nodes;
272 272
    bool _skip_arcs;
273 273

	
274 274
  public:
275 275

	
276 276
    /// \e
277 277
    DigraphWriter(std::ostream& is, Digraph& digraph) 
278 278
      : _os(&is), local_os(false), _digraph(digraph),
279 279
	_skip_nodes(false), _skip_arcs(false) {}
280 280

	
281 281
    /// \e
282 282
    DigraphWriter(const std::string& fn, Digraph& digraph) 
283 283
      : _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph),
284 284
	_skip_nodes(false), _skip_arcs(false) {}
285 285

	
286 286
    /// \e
287 287
    DigraphWriter(const char* fn, Digraph& digraph) 
288 288
      : _os(new std::ofstream(fn)), local_os(true), _digraph(digraph),
289 289
	_skip_nodes(false), _skip_arcs(false) {}
290 290

	
291 291
    DigraphWriter(DigraphWriter& other) 
292 292
      : _os(other._os), local_os(other.local_os), _digraph(other._digraph),
293 293
	_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
294 294

	
295 295
      other.is = 0;
296 296
      other.local_os = false;
297 297

	
298 298
      _node_index.swap(other._node_index);
299 299
      _arc_index.swap(other._arc_index);
300 300

	
301 301
      _node_maps.swap(other._node_maps);
302 302
      _arc_maps.swap(other._arc_maps);
303 303
      _attributes.swap(other._attributes);
304 304

	
305 305
      _nodes_caption = other._nodes_caption;
306 306
      _arcs_caption = other._arcs_caption;
307 307
      _attributes_caption = other._attributes_caption;
308 308
    }
309 309

	
310 310
    /// \e
311 311
    ~DigraphWriter() {
312 312
      for (typename NodeMaps::iterator it = _node_maps.begin(); 
313 313
	   it != _node_maps.end(); ++it) {
314 314
	delete it->second;
315 315
      }
316 316

	
317 317
      for (typename ArcMaps::iterator it = _arc_maps.begin(); 
318 318
	   it != _arc_maps.end(); ++it) {
319 319
	delete it->second;
320 320
      }
321 321

	
322 322
      for (typename Attributes::iterator it = _attributes.begin(); 
323 323
	   it != _attributes.end(); ++it) {
324 324
	delete it->second;
325 325
      }
326 326

	
327 327
      if (local_os) {
328 328
	delete _os;
329 329
      }
330 330
    }
331 331

	
332 332
  private:
333 333
    
334 334
    DigraphWriter& operator=(const DigraphWriter&);
335 335

	
336 336
  public:
337 337

	
338 338
    /// \e
339 339
    template <typename Map>
340 340
    DigraphWriter& nodeMap(const std::string& caption, const Map& map) {
341 341
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
342 342
      _writer_bits::MapStorageBase<Node>* storage = 
343 343
	new _writer_bits::MapStorage<Node, Map>(map);
344 344
      _node_maps.push_back(std::make_pair(caption, storage));
345 345
      return *this;
346 346
    }
347 347

	
348 348
    /// \e
349 349
    template <typename Map, typename Converter>
350 350
    DigraphWriter& nodeMap(const std::string& caption, const Map& map, 
351 351
			   const Converter& converter = Converter()) {
352 352
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
353 353
      _writer_bits::MapStorageBase<Node>* storage = 
354 354
	new _writer_bits::MapStorage<Node, Map, Converter>(map, converter);
355 355
      _node_maps.push_back(std::make_pair(caption, storage));
356 356
      return *this;
357 357
    }
358 358

	
359 359
    /// \e
360 360
    template <typename Map>
361 361
    DigraphWriter& arcMap(const std::string& caption, const Map& map) {
362 362
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
363 363
      _writer_bits::MapStorageBase<Arc>* storage = 
364 364
	new _writer_bits::MapStorage<Arc, Map>(map);
365 365
      _arc_maps.push_back(std::make_pair(caption, storage));
366 366
      return *this;
367 367
    }
368 368

	
369 369
    /// \e
370 370
    template <typename Map, typename Converter>
371 371
    DigraphWriter& arcMap(const std::string& caption, const Map& map, 
372 372
			  const Converter& converter = Converter()) {
373 373
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
374 374
      _writer_bits::MapStorageBase<Arc>* storage = 
375 375
	new _writer_bits::MapStorage<Arc, Map, Converter>(map, converter);
376 376
      _arc_maps.push_back(std::make_pair(caption, storage));
377 377
      return *this;
378 378
    }
379 379

	
380 380
    /// \e
381 381
    template <typename Value>
382 382
    DigraphWriter& attribute(const std::string& caption, const Value& value) {
383 383
      _writer_bits::ValueStorageBase* storage = 
384 384
	new _writer_bits::ValueStorage<Value>(value);
385 385
      _attributes.push_back(std::make_pair(caption, storage));
386 386
      return *this;
387 387
    }
388 388

	
389 389
    /// \e
390 390
    template <typename Value, typename Converter>
391 391
    DigraphWriter& attribute(const std::string& caption, const Value& value, 
392 392
			     const Converter& converter = Converter()) {
393 393
      _writer_bits::ValueStorageBase* storage = 
394 394
	new _writer_bits::ValueStorage<Value, Converter>(value, converter);
395 395
      _attributes.push_back(std::make_pair(caption, storage));
396 396
      return *this;
397 397
    }
398 398

	
399 399
    /// \e
400 400
    DigraphWriter& node(const std::string& caption, const Node& node) {
401 401
      typedef _writer_bits::MapLookUpConverter<Node> Converter;
402 402
      Converter converter(_node_index);
403 403
      _writer_bits::ValueStorageBase* storage = 
404 404
	new _writer_bits::ValueStorage<Node, Converter>(node, converter);
405 405
      _attributes.push_back(std::make_pair(caption, storage));
406 406
      return *this;
407 407
    }
408 408

	
409 409
    /// \e
410 410
    DigraphWriter& arc(const std::string& caption, const Arc& arc) {
411 411
      typedef _writer_bits::MapLookUpConverter<Arc> Converter;
412 412
      Converter converter(_arc_index);
413 413
      _writer_bits::ValueStorageBase* storage = 
414 414
	new _writer_bits::ValueStorage<Arc, Converter>(arc, converter);
415 415
      _attributes.push_back(std::make_pair(caption, storage));
416 416
      return *this;
417 417
    }
418 418

	
419 419
    /// \e
420 420
    DigraphWriter& nodes(const std::string& caption) {
421 421
      _nodes_caption = caption;
422 422
      return *this;
423 423
    }
424 424

	
425 425
    /// \e
426 426
    DigraphWriter& arcs(const std::string& caption) {
427 427
      _arcs_caption = caption;
428 428
      return *this;
429 429
    }
430 430

	
431 431
    /// \e
432 432
    DigraphWriter& attributes(const std::string& caption) {
0 comments (0 inline)