gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Add missing tags and indicators
0 4 0
default
4 files changed with 31 insertions and 1 deletions:
↑ Collapse diff ↑
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_BITS_TRAITS_H
20 20
#define LEMON_BITS_TRAITS_H
21 21

	
22 22
//\file
23 23
//\brief Traits for graphs and maps
24 24
//
25 25

	
26 26
#include <lemon/bits/enable_if.h>
27 27

	
28 28
namespace lemon {
29 29

	
30 30
  struct InvalidType {};
31 31

	
32 32
  template <typename _Graph, typename _Item>
33 33
  class ItemSetTraits {};
34 34

	
35 35

	
36 36
  template <typename Graph, typename Enable = void>
37 37
  struct NodeNotifierIndicator {
38 38
    typedef InvalidType Type;
39 39
  };
40 40
  template <typename Graph>
41 41
  struct NodeNotifierIndicator<
42 42
    Graph,
43 43
    typename enable_if<typename Graph::NodeNotifier::Notifier, void>::type
44 44
  > {
45 45
    typedef typename Graph::NodeNotifier Type;
46 46
  };
47 47

	
48 48
  template <typename _Graph>
49 49
  class ItemSetTraits<_Graph, typename _Graph::Node> {
50 50
  public:
51 51

	
52 52
    typedef _Graph Graph;
53 53

	
54 54
    typedef typename Graph::Node Item;
55 55
    typedef typename Graph::NodeIt ItemIt;
56 56

	
57 57
    typedef typename NodeNotifierIndicator<Graph>::Type ItemNotifier;
58 58

	
59 59
    template <typename _Value>
60 60
    class Map : public Graph::template NodeMap<_Value> {
61 61
    public:
62 62
      typedef typename Graph::template NodeMap<_Value> Parent;
63 63
      typedef typename Graph::template NodeMap<_Value> Type;
64 64
      typedef typename Parent::Value Value;
65 65

	
66 66
      Map(const Graph& _digraph) : Parent(_digraph) {}
67 67
      Map(const Graph& _digraph, const Value& _value)
68 68
        : Parent(_digraph, _value) {}
69 69

	
70 70
     };
71 71

	
72 72
  };
73 73

	
74 74
  template <typename Graph, typename Enable = void>
75 75
  struct ArcNotifierIndicator {
76 76
    typedef InvalidType Type;
77 77
  };
78 78
  template <typename Graph>
79 79
  struct ArcNotifierIndicator<
80 80
    Graph,
81 81
    typename enable_if<typename Graph::ArcNotifier::Notifier, void>::type
82 82
  > {
83 83
    typedef typename Graph::ArcNotifier Type;
84 84
  };
85 85

	
86 86
  template <typename _Graph>
87 87
  class ItemSetTraits<_Graph, typename _Graph::Arc> {
88 88
  public:
89 89

	
90 90
    typedef _Graph Graph;
91 91

	
92 92
    typedef typename Graph::Arc Item;
93 93
    typedef typename Graph::ArcIt ItemIt;
94 94

	
95 95
    typedef typename ArcNotifierIndicator<Graph>::Type ItemNotifier;
96 96

	
97 97
    template <typename _Value>
98 98
    class Map : public Graph::template ArcMap<_Value> {
99 99
    public:
100 100
      typedef typename Graph::template ArcMap<_Value> Parent;
101 101
      typedef typename Graph::template ArcMap<_Value> Type;
102 102
      typedef typename Parent::Value Value;
103 103

	
104 104
      Map(const Graph& _digraph) : Parent(_digraph) {}
105 105
      Map(const Graph& _digraph, const Value& _value)
106 106
        : Parent(_digraph, _value) {}
107 107
    };
108 108

	
109 109
  };
110 110

	
111 111
  template <typename Graph, typename Enable = void>
112 112
  struct EdgeNotifierIndicator {
113 113
    typedef InvalidType Type;
114 114
  };
115 115
  template <typename Graph>
116 116
  struct EdgeNotifierIndicator<
117 117
    Graph,
118 118
    typename enable_if<typename Graph::EdgeNotifier::Notifier, void>::type
119 119
  > {
120 120
    typedef typename Graph::EdgeNotifier Type;
121 121
  };
122 122

	
123 123
  template <typename _Graph>
124 124
  class ItemSetTraits<_Graph, typename _Graph::Edge> {
125 125
  public:
126 126

	
127 127
    typedef _Graph Graph;
128 128

	
129 129
    typedef typename Graph::Edge Item;
130 130
    typedef typename Graph::EdgeIt ItemIt;
131 131

	
132 132
    typedef typename EdgeNotifierIndicator<Graph>::Type ItemNotifier;
133 133

	
134 134
    template <typename _Value>
135 135
    class Map : public Graph::template EdgeMap<_Value> {
136 136
    public:
137 137
      typedef typename Graph::template EdgeMap<_Value> Parent;
138 138
      typedef typename Graph::template EdgeMap<_Value> Type;
139 139
      typedef typename Parent::Value Value;
140 140

	
141 141
      Map(const Graph& _digraph) : Parent(_digraph) {}
142 142
      Map(const Graph& _digraph, const Value& _value)
143 143
        : Parent(_digraph, _value) {}
144 144
    };
145 145

	
146 146
  };
147 147

	
148 148
  template <typename Map, typename Enable = void>
149 149
  struct MapTraits {
150 150
    typedef False ReferenceMapTag;
151 151

	
152 152
    typedef typename Map::Key Key;
153 153
    typedef typename Map::Value Value;
154 154

	
155 155
    typedef Value ConstReturnValue;
156 156
    typedef Value ReturnValue;
157 157
  };
158 158

	
159 159
  template <typename Map>
160 160
  struct MapTraits<
161 161
    Map, typename enable_if<typename Map::ReferenceMapTag, void>::type >
162 162
  {
163 163
    typedef True ReferenceMapTag;
164 164

	
165 165
    typedef typename Map::Key Key;
166 166
    typedef typename Map::Value Value;
167 167

	
168 168
    typedef typename Map::ConstReference ConstReturnValue;
169 169
    typedef typename Map::Reference ReturnValue;
170 170

	
171 171
    typedef typename Map::ConstReference ConstReference;
172 172
    typedef typename Map::Reference Reference;
173 173
 };
174 174

	
175 175
  template <typename MatrixMap, typename Enable = void>
176 176
  struct MatrixMapTraits {
177 177
    typedef False ReferenceMapTag;
178 178

	
179 179
    typedef typename MatrixMap::FirstKey FirstKey;
180 180
    typedef typename MatrixMap::SecondKey SecondKey;
181 181
    typedef typename MatrixMap::Value Value;
182 182

	
183 183
    typedef Value ConstReturnValue;
184 184
    typedef Value ReturnValue;
185 185
  };
186 186

	
187 187
  template <typename MatrixMap>
188 188
  struct MatrixMapTraits<
189 189
    MatrixMap, typename enable_if<typename MatrixMap::ReferenceMapTag,
190 190
                                  void>::type >
191 191
  {
192 192
    typedef True ReferenceMapTag;
193 193

	
194 194
    typedef typename MatrixMap::FirstKey FirstKey;
195 195
    typedef typename MatrixMap::SecondKey SecondKey;
196 196
    typedef typename MatrixMap::Value Value;
197 197

	
198 198
    typedef typename MatrixMap::ConstReference ConstReturnValue;
199 199
    typedef typename MatrixMap::Reference ReturnValue;
200 200

	
201 201
    typedef typename MatrixMap::ConstReference ConstReference;
202 202
    typedef typename MatrixMap::Reference Reference;
203 203
 };
204 204

	
205 205
  // Indicators for the tags
206 206

	
207 207
  template <typename Graph, typename Enable = void>
208 208
  struct NodeNumTagIndicator {
209 209
    static const bool value = false;
210 210
  };
211 211

	
212 212
  template <typename Graph>
213 213
  struct NodeNumTagIndicator<
214 214
    Graph,
215 215
    typename enable_if<typename Graph::NodeNumTag, void>::type
216 216
  > {
217 217
    static const bool value = true;
218 218
  };
219 219

	
220 220
  template <typename Graph, typename Enable = void>
221
  struct ArcNumTagIndicator {
222
    static const bool value = false;
223
  };
224

	
225
  template <typename Graph>
226
  struct ArcNumTagIndicator<
227
    Graph,
228
    typename enable_if<typename Graph::ArcNumTag, void>::type
229
  > {
230
    static const bool value = true;
231
  };
232

	
233
  template <typename Graph, typename Enable = void>
221 234
  struct EdgeNumTagIndicator {
222 235
    static const bool value = false;
223 236
  };
224 237

	
225 238
  template <typename Graph>
226 239
  struct EdgeNumTagIndicator<
227 240
    Graph,
228 241
    typename enable_if<typename Graph::EdgeNumTag, void>::type
229 242
  > {
230 243
    static const bool value = true;
231 244
  };
232 245

	
233 246
  template <typename Graph, typename Enable = void>
247
  struct FindArcTagIndicator {
248
    static const bool value = false;
249
  };
250

	
251
  template <typename Graph>
252
  struct FindArcTagIndicator<
253
    Graph,
254
    typename enable_if<typename Graph::FindArcTag, void>::type
255
  > {
256
    static const bool value = true;
257
  };
258

	
259
  template <typename Graph, typename Enable = void>
234 260
  struct FindEdgeTagIndicator {
235 261
    static const bool value = false;
236 262
  };
237 263

	
238 264
  template <typename Graph>
239 265
  struct FindEdgeTagIndicator<
240 266
    Graph,
241 267
    typename enable_if<typename Graph::FindEdgeTag, void>::type
242 268
  > {
243 269
    static const bool value = true;
244 270
  };
245 271

	
246 272
  template <typename Graph, typename Enable = void>
247 273
  struct UndirectedTagIndicator {
248 274
    static const bool value = false;
249 275
  };
250 276

	
251 277
  template <typename Graph>
252 278
  struct UndirectedTagIndicator<
253 279
    Graph,
254 280
    typename enable_if<typename Graph::UndirectedTag, void>::type
255 281
  > {
256 282
    static const bool value = true;
257 283
  };
258 284

	
259 285
  template <typename Graph, typename Enable = void>
260 286
  struct BuildTagIndicator {
261 287
    static const bool value = false;
262 288
  };
263 289

	
264 290
  template <typename Graph>
265 291
  struct BuildTagIndicator<
266 292
    Graph,
267 293
    typename enable_if<typename Graph::BuildTag, void>::type
268 294
  > {
269 295
    static const bool value = true;
270 296
  };
271 297

	
272 298
}
273 299

	
274 300
#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_FULL_GRAPH_H
20 20
#define LEMON_FULL_GRAPH_H
21 21

	
22 22
#include <lemon/core.h>
23 23
#include <lemon/bits/graph_extender.h>
24 24

	
25 25
///\ingroup graphs
26 26
///\file
27 27
///\brief FullGraph and FullDigraph classes.
28 28

	
29 29
namespace lemon {
30 30

	
31 31
  class FullDigraphBase {
32 32
  public:
33 33

	
34 34
    typedef FullDigraphBase Graph;
35 35

	
36 36
    class Node;
37 37
    class Arc;
38 38

	
39 39
  protected:
40 40

	
41 41
    int _node_num;
42 42
    int _arc_num;
43 43

	
44 44
    FullDigraphBase() {}
45 45

	
46 46
    void construct(int n) { _node_num = n; _arc_num = n * n; }
47 47

	
48 48
  public:
49 49

	
50 50
    typedef True NodeNumTag;
51 51
    typedef True ArcNumTag;
52 52

	
53 53
    Node operator()(int ix) const { return Node(ix); }
54 54
    int index(const Node& node) const { return node._id; }
55 55

	
56 56
    Arc arc(const Node& s, const Node& t) const {
57 57
      return Arc(s._id * _node_num + t._id);
58 58
    }
59 59

	
60 60
    int nodeNum() const { return _node_num; }
61 61
    int arcNum() const { return _arc_num; }
62 62

	
63 63
    int maxNodeId() const { return _node_num - 1; }
64 64
    int maxArcId() const { return _arc_num - 1; }
65 65

	
66 66
    Node source(Arc arc) const { return arc._id / _node_num; }
67 67
    Node target(Arc arc) const { return arc._id % _node_num; }
68 68

	
69 69
    static int id(Node node) { return node._id; }
70 70
    static int id(Arc arc) { return arc._id; }
71 71

	
72 72
    static Node nodeFromId(int id) { return Node(id);}
73 73
    static Arc arcFromId(int id) { return Arc(id);}
74 74

	
75 75
    typedef True FindArcTag;
76 76

	
77 77
    Arc findArc(Node s, Node t, Arc prev = INVALID) const {
78 78
      return prev == INVALID ? arc(s, t) : INVALID;
79 79
    }
80 80

	
81 81
    class Node {
82 82
      friend class FullDigraphBase;
83 83

	
84 84
    protected:
85 85
      int _id;
86 86
      Node(int id) : _id(id) {}
87 87
    public:
88 88
      Node() {}
89 89
      Node (Invalid) : _id(-1) {}
90 90
      bool operator==(const Node node) const {return _id == node._id;}
91 91
      bool operator!=(const Node node) const {return _id != node._id;}
92 92
      bool operator<(const Node node) const {return _id < node._id;}
93 93
    };
94 94

	
95 95
    class Arc {
96 96
      friend class FullDigraphBase;
97 97

	
98 98
    protected:
99 99
      int _id;  // _node_num * source + target;
100 100

	
101 101
      Arc(int id) : _id(id) {}
102 102

	
103 103
    public:
104 104
      Arc() { }
105 105
      Arc (Invalid) { _id = -1; }
106 106
      bool operator==(const Arc arc) const {return _id == arc._id;}
107 107
      bool operator!=(const Arc arc) const {return _id != arc._id;}
108 108
      bool operator<(const Arc arc) const {return _id < arc._id;}
109 109
    };
110 110

	
111 111
    void first(Node& node) const {
112 112
      node._id = _node_num - 1;
113 113
    }
114 114

	
115 115
    static void next(Node& node) {
116 116
      --node._id;
117 117
    }
118 118

	
119 119
    void first(Arc& arc) const {
120 120
      arc._id = _arc_num - 1;
121 121
    }
122 122

	
123 123
    static void next(Arc& arc) {
124 124
      --arc._id;
125 125
    }
126 126

	
127 127
    void firstOut(Arc& arc, const Node& node) const {
128 128
      arc._id = (node._id + 1) * _node_num - 1;
129 129
    }
130 130

	
131 131
    void nextOut(Arc& arc) const {
132 132
      if (arc._id % _node_num == 0) arc._id = 0;
133 133
      --arc._id;
134 134
    }
135 135

	
136 136
    void firstIn(Arc& arc, const Node& node) const {
137 137
      arc._id = _arc_num + node._id - _node_num;
138 138
    }
139 139

	
140 140
    void nextIn(Arc& arc) const {
141 141
      arc._id -= _node_num;
142 142
      if (arc._id < 0) arc._id = -1;
143 143
    }
144 144

	
145 145
  };
146 146

	
147 147
  typedef DigraphExtender<FullDigraphBase> ExtendedFullDigraphBase;
148 148

	
149 149
  /// \ingroup graphs
150 150
  ///
151 151
  /// \brief A full digraph class.
152 152
  ///
153 153
  /// This is a simple and fast directed full graph implementation.
154 154
  /// From each node go arcs to each node (including the source node),
155 155
  /// therefore the number of the arcs in the digraph is the square of
156 156
  /// the node number. This digraph type is completely static, so you
157 157
  /// can neither add nor delete either arcs or nodes, and it needs
158 158
  /// constant space in memory.
159 159
  ///
160 160
  /// This class conforms to the \ref concepts::Digraph "Digraph" concept
161 161
  /// and it also has an important extra feature that its maps are
162 162
  /// real \ref concepts::ReferenceMap "reference map"s.
163 163
  ///
164 164
  /// The \c FullDigraph and \c FullGraph classes are very similar,
165 165
  /// but there are two differences. While this class conforms only
166 166
  /// to the \ref concepts::Digraph "Digraph" concept, the \c FullGraph
167 167
  /// class conforms to the \ref concepts::Graph "Graph" concept,
168 168
  /// moreover \c FullGraph does not contain a loop arc for each
169 169
  /// node as \c FullDigraph does.
170 170
  ///
171 171
  /// \sa FullGraph
172 172
  class FullDigraph : public ExtendedFullDigraphBase {
173 173
  public:
174 174

	
175 175
    typedef ExtendedFullDigraphBase Parent;
176 176

	
177 177
    /// \brief Constructor
178 178
    FullDigraph() { construct(0); }
179 179

	
180 180
    /// \brief Constructor
181 181
    ///
182 182
    /// Constructor.
183 183
    /// \param n The number of the nodes.
184 184
    FullDigraph(int n) { construct(n); }
185 185

	
186 186
    /// \brief Resizes the digraph
187 187
    ///
188 188
    /// Resizes the digraph. The function will fully destroy and
189 189
    /// rebuild the digraph. This cause that the maps of the digraph will
190 190
    /// reallocated automatically and the previous values will be lost.
191 191
    void resize(int n) {
192 192
      Parent::notifier(Arc()).clear();
193 193
      Parent::notifier(Node()).clear();
194 194
      construct(n);
195 195
      Parent::notifier(Node()).build();
196 196
      Parent::notifier(Arc()).build();
197 197
    }
198 198

	
199 199
    /// \brief Returns the node with the given index.
200 200
    ///
201 201
    /// Returns the node with the given index. Since it is a static
202 202
    /// digraph its nodes can be indexed with integers from the range
203 203
    /// <tt>[0..nodeNum()-1]</tt>.
204 204
    /// \sa index()
205 205
    Node operator()(int ix) const { return Parent::operator()(ix); }
206 206

	
207 207
    /// \brief Returns the index of the given node.
208 208
    ///
209 209
    /// Returns the index of the given node. Since it is a static
210 210
    /// digraph its nodes can be indexed with integers from the range
211 211
    /// <tt>[0..nodeNum()-1]</tt>.
212 212
    /// \sa operator()
213 213
    int index(const Node& node) const { return Parent::index(node); }
214 214

	
215 215
    /// \brief Returns the arc connecting the given nodes.
216 216
    ///
217 217
    /// Returns the arc connecting the given nodes.
218 218
    Arc arc(const Node& u, const Node& v) const {
219 219
      return Parent::arc(u, v);
220 220
    }
221 221

	
222 222
    /// \brief Number of nodes.
223 223
    int nodeNum() const { return Parent::nodeNum(); }
224 224
    /// \brief Number of arcs.
225 225
    int arcNum() const { return Parent::arcNum(); }
226 226
  };
227 227

	
228 228

	
229 229
  class FullGraphBase {
230 230
    int _node_num;
231 231
    int _edge_num;
232 232
  public:
233 233

	
234 234
    typedef FullGraphBase Graph;
235 235

	
236 236
    class Node;
237 237
    class Arc;
238 238
    class Edge;
239 239

	
240 240
  protected:
241 241

	
242 242
    FullGraphBase() {}
243 243

	
244 244
    void construct(int n) { _node_num = n; _edge_num = n * (n - 1) / 2; }
245 245

	
246 246
    int _uid(int e) const {
247 247
      int u = e / _node_num;
248 248
      int v = e % _node_num;
249 249
      return u < v ? u : _node_num - 2 - u;
250 250
    }
251 251

	
252 252
    int _vid(int e) const {
253 253
      int u = e / _node_num;
254 254
      int v = e % _node_num;
255 255
      return u < v ? v : _node_num - 1 - v;
256 256
    }
257 257

	
258 258
    void _uvid(int e, int& u, int& v) const {
259 259
      u = e / _node_num;
260 260
      v = e % _node_num;
261 261
      if  (u >= v) {
262 262
        u = _node_num - 2 - u;
263 263
        v = _node_num - 1 - v;
264 264
      }
265 265
    }
266 266

	
267 267
    void _stid(int a, int& s, int& t) const {
268 268
      if ((a & 1) == 1) {
269 269
        _uvid(a >> 1, s, t);
270 270
      } else {
271 271
        _uvid(a >> 1, t, s);
272 272
      }
273 273
    }
274 274

	
275 275
    int _eid(int u, int v) const {
276 276
      if (u < (_node_num - 1) / 2) {
277 277
        return u * _node_num + v;
278 278
      } else {
279 279
        return (_node_num - 1 - u) * _node_num - v - 1;
280 280
      }
281 281
    }
282 282

	
283 283
  public:
284 284

	
285 285
    Node operator()(int ix) const { return Node(ix); }
286 286
    int index(const Node& node) const { return node._id; }
287 287

	
288 288
    Edge edge(const Node& u, const Node& v) const {
289 289
      if (u._id < v._id) {
290 290
        return Edge(_eid(u._id, v._id));
291 291
      } else if (u._id != v._id) {
292 292
        return Edge(_eid(v._id, u._id));
293 293
      } else {
294 294
        return INVALID;
295 295
      }
296 296
    }
297 297

	
298 298
    Arc arc(const Node& s, const Node& t) const {
299 299
      if (s._id < t._id) {
300 300
        return Arc((_eid(s._id, t._id) << 1) | 1);
301 301
      } else if (s._id != t._id) {
302 302
        return Arc(_eid(t._id, s._id) << 1);
303 303
      } else {
304 304
        return INVALID;
305 305
      }
306 306
    }
307 307

	
308 308
    typedef True NodeNumTag;
309
    typedef True ArcNumTag;
309 310
    typedef True EdgeNumTag;
310 311

	
311 312
    int nodeNum() const { return _node_num; }
312 313
    int arcNum() const { return 2 * _edge_num; }
313 314
    int edgeNum() const { return _edge_num; }
314 315

	
315 316
    static int id(Node node) { return node._id; }
316 317
    static int id(Arc arc) { return arc._id; }
317 318
    static int id(Edge edge) { return edge._id; }
318 319

	
319 320
    int maxNodeId() const { return _node_num-1; }
320 321
    int maxArcId() const { return 2 * _edge_num-1; }
321 322
    int maxEdgeId() const { return _edge_num-1; }
322 323

	
323 324
    static Node nodeFromId(int id) { return Node(id);}
324 325
    static Arc arcFromId(int id) { return Arc(id);}
325 326
    static Edge edgeFromId(int id) { return Edge(id);}
326 327

	
327 328
    Node u(Edge edge) const {
328 329
      return Node(_uid(edge._id));
329 330
    }
330 331

	
331 332
    Node v(Edge edge) const {
332 333
      return Node(_vid(edge._id));
333 334
    }
334 335

	
335 336
    Node source(Arc arc) const {
336 337
      return Node((arc._id & 1) == 1 ?
337 338
                  _uid(arc._id >> 1) : _vid(arc._id >> 1));
338 339
    }
339 340

	
340 341
    Node target(Arc arc) const {
341 342
      return Node((arc._id & 1) == 1 ?
342 343
                  _vid(arc._id >> 1) : _uid(arc._id >> 1));
343 344
    }
344 345

	
345 346
    typedef True FindEdgeTag;
347
    typedef True FindArcTag;
346 348

	
347 349
    Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
348 350
      return prev != INVALID ? INVALID : edge(u, v);
349 351
    }
350 352

	
351 353
    Arc findArc(Node s, Node t, Arc prev = INVALID) const {
352 354
      return prev != INVALID ? INVALID : arc(s, t);
353 355
    }
354 356

	
355 357
    class Node {
356 358
      friend class FullGraphBase;
357 359

	
358 360
    protected:
359 361
      int _id;
360 362
      Node(int id) : _id(id) {}
361 363
    public:
362 364
      Node() {}
363 365
      Node (Invalid) { _id = -1; }
364 366
      bool operator==(const Node node) const {return _id == node._id;}
365 367
      bool operator!=(const Node node) const {return _id != node._id;}
366 368
      bool operator<(const Node node) const {return _id < node._id;}
367 369
    };
368 370

	
369 371
    class Edge {
370 372
      friend class FullGraphBase;
371 373
      friend class Arc;
372 374

	
373 375
    protected:
374 376
      int _id;
375 377

	
376 378
      Edge(int id) : _id(id) {}
377 379

	
378 380
    public:
379 381
      Edge() { }
380 382
      Edge (Invalid) { _id = -1; }
381 383

	
382 384
      bool operator==(const Edge edge) const {return _id == edge._id;}
383 385
      bool operator!=(const Edge edge) const {return _id != edge._id;}
384 386
      bool operator<(const Edge edge) const {return _id < edge._id;}
385 387
    };
386 388

	
387 389
    class Arc {
388 390
      friend class FullGraphBase;
389 391

	
390 392
    protected:
391 393
      int _id;
392 394

	
393 395
      Arc(int id) : _id(id) {}
394 396

	
395 397
    public:
396 398
      Arc() { }
397 399
      Arc (Invalid) { _id = -1; }
398 400

	
399 401
      operator Edge() const { return Edge(_id != -1 ? (_id >> 1) : -1); }
400 402

	
401 403
      bool operator==(const Arc arc) const {return _id == arc._id;}
402 404
      bool operator!=(const Arc arc) const {return _id != arc._id;}
403 405
      bool operator<(const Arc arc) const {return _id < arc._id;}
404 406
    };
405 407

	
406 408
    static bool direction(Arc arc) {
407 409
      return (arc._id & 1) == 1;
408 410
    }
409 411

	
410 412
    static Arc direct(Edge edge, bool dir) {
411 413
      return Arc((edge._id << 1) | (dir ? 1 : 0));
412 414
    }
413 415

	
414 416
    void first(Node& node) const {
415 417
      node._id = _node_num - 1;
416 418
    }
417 419

	
418 420
    static void next(Node& node) {
419 421
      --node._id;
420 422
    }
421 423

	
422 424
    void first(Arc& arc) const {
423 425
      arc._id = (_edge_num << 1) - 1;
424 426
    }
425 427

	
426 428
    static void next(Arc& arc) {
427 429
      --arc._id;
428 430
    }
429 431

	
430 432
    void first(Edge& edge) const {
431 433
      edge._id = _edge_num - 1;
432 434
    }
433 435

	
434 436
    static void next(Edge& edge) {
435 437
      --edge._id;
436 438
    }
437 439

	
438 440
    void firstOut(Arc& arc, const Node& node) const {
439 441
      int s = node._id, t = _node_num - 1;
440 442
      if (s < t) {
441 443
        arc._id = (_eid(s, t) << 1) | 1;
442 444
      } else {
443 445
        --t;
444 446
        arc._id = (t != -1 ? (_eid(t, s) << 1) : -1);
445 447
      }
446 448
    }
447 449

	
448 450
    void nextOut(Arc& arc) const {
449 451
      int s, t;
450 452
      _stid(arc._id, s, t);
451 453
      --t;
452 454
      if (s < t) {
453 455
        arc._id = (_eid(s, t) << 1) | 1;
454 456
      } else {
455 457
        if (s == t) --t;
456 458
        arc._id = (t != -1 ? (_eid(t, s) << 1) : -1);
457 459
      }
458 460
    }
459 461

	
460 462
    void firstIn(Arc& arc, const Node& node) const {
461 463
      int s = _node_num - 1, t = node._id;
462 464
      if (s > t) {
463 465
        arc._id = (_eid(t, s) << 1);
464 466
      } else {
465 467
        --s;
466 468
        arc._id = (s != -1 ? (_eid(s, t) << 1) | 1 : -1);
467 469
      }
468 470
    }
469 471

	
470 472
    void nextIn(Arc& arc) const {
471 473
      int s, t;
472 474
      _stid(arc._id, s, t);
473 475
      --s;
474 476
      if (s > t) {
475 477
        arc._id = (_eid(t, s) << 1);
476 478
      } else {
477 479
        if (s == t) --s;
478 480
        arc._id = (s != -1 ? (_eid(s, t) << 1) | 1 : -1);
479 481
      }
480 482
    }
481 483

	
482 484
    void firstInc(Edge& edge, bool& dir, const Node& node) const {
483 485
      int u = node._id, v = _node_num - 1;
484 486
      if (u < v) {
485 487
        edge._id = _eid(u, v);
486 488
        dir = true;
487 489
      } else {
488 490
        --v;
489 491
        edge._id = (v != -1 ? _eid(v, u) : -1);
490 492
        dir = false;
491 493
      }
492 494
    }
493 495

	
494 496
    void nextInc(Edge& edge, bool& dir) const {
495 497
      int u, v;
496 498
      if (dir) {
497 499
        _uvid(edge._id, u, v);
498 500
        --v;
499 501
        if (u < v) {
500 502
          edge._id = _eid(u, v);
501 503
        } else {
502 504
          --v;
503 505
          edge._id = (v != -1 ? _eid(v, u) : -1);
504 506
          dir = false;
505 507
        }
506 508
      } else {
507 509
        _uvid(edge._id, v, u);
508 510
        --v;
509 511
        edge._id = (v != -1 ? _eid(v, u) : -1);
510 512
      }
511 513
    }
512 514

	
513 515
  };
514 516

	
515 517
  typedef GraphExtender<FullGraphBase> ExtendedFullGraphBase;
516 518

	
517 519
  /// \ingroup graphs
518 520
  ///
519 521
  /// \brief An undirected full graph class.
520 522
  ///
521 523
  /// This is a simple and fast undirected full graph
522 524
  /// implementation. From each node go edge to each other node,
523 525
  /// therefore the number of edges in the graph is \f$n(n-1)/2\f$.
524 526
  /// This graph type is completely static, so you can neither
525 527
  /// add nor delete either edges or nodes, and it needs constant
526 528
  /// space in memory.
527 529
  ///
528 530
  /// This class conforms to the \ref concepts::Graph "Graph" concept
529 531
  /// and it also has an important extra feature that its maps are
530 532
  /// real \ref concepts::ReferenceMap "reference map"s.
531 533
  ///
532 534
  /// The \c FullGraph and \c FullDigraph classes are very similar,
533 535
  /// but there are two differences. While the \c FullDigraph class
534 536
  /// conforms only to the \ref concepts::Digraph "Digraph" concept,
535 537
  /// this class conforms to the \ref concepts::Graph "Graph" concept,
536 538
  /// moreover \c FullGraph does not contain a loop arc for each
537 539
  /// node as \c FullDigraph does.
538 540
  ///
539 541
  /// \sa FullDigraph
540 542
  class FullGraph : public ExtendedFullGraphBase {
541 543
  public:
542 544

	
543 545
    typedef ExtendedFullGraphBase Parent;
544 546

	
545 547
    /// \brief Constructor
546 548
    FullGraph() { construct(0); }
547 549

	
548 550
    /// \brief Constructor
549 551
    ///
550 552
    /// Constructor.
551 553
    /// \param n The number of the nodes.
552 554
    FullGraph(int n) { construct(n); }
553 555

	
554 556
    /// \brief Resizes the graph
555 557
    ///
556 558
    /// Resizes the graph. The function will fully destroy and
557 559
    /// rebuild the graph. This cause that the maps of the graph will
558 560
    /// reallocated automatically and the previous values will be lost.
559 561
    void resize(int n) {
560 562
      Parent::notifier(Arc()).clear();
561 563
      Parent::notifier(Edge()).clear();
562 564
      Parent::notifier(Node()).clear();
563 565
      construct(n);
564 566
      Parent::notifier(Node()).build();
565 567
      Parent::notifier(Edge()).build();
566 568
      Parent::notifier(Arc()).build();
567 569
    }
568 570

	
569 571
    /// \brief Returns the node with the given index.
570 572
    ///
571 573
    /// Returns the node with the given index. Since it is a static
572 574
    /// graph its nodes can be indexed with integers from the range
573 575
    /// <tt>[0..nodeNum()-1]</tt>.
574 576
    /// \sa index()
575 577
    Node operator()(int ix) const { return Parent::operator()(ix); }
576 578

	
577 579
    /// \brief Returns the index of the given node.
578 580
    ///
579 581
    /// Returns the index of the given node. Since it is a static
580 582
    /// graph its nodes can be indexed with integers from the range
581 583
    /// <tt>[0..nodeNum()-1]</tt>.
582 584
    /// \sa operator()
583 585
    int index(const Node& node) const { return Parent::index(node); }
584 586

	
585 587
    /// \brief Returns the arc connecting the given nodes.
586 588
    ///
587 589
    /// Returns the arc connecting the given nodes.
588 590
    Arc arc(const Node& s, const Node& t) const {
589 591
      return Parent::arc(s, t);
590 592
    }
591 593

	
592 594
    /// \brief Returns the edge connects the given nodes.
593 595
    ///
594 596
    /// Returns the edge connects the given nodes.
595 597
    Edge edge(const Node& u, const Node& v) const {
596 598
      return Parent::edge(u, v);
597 599
    }
598 600

	
599 601
    /// \brief Number of nodes.
600 602
    int nodeNum() const { return Parent::nodeNum(); }
601 603
    /// \brief Number of arcs.
602 604
    int arcNum() const { return Parent::arcNum(); }
603 605
    /// \brief Number of edges.
604 606
    int edgeNum() const { return Parent::edgeNum(); }
605 607

	
606 608
  };
607 609

	
608 610

	
609 611
} //namespace lemon
610 612

	
611 613

	
612 614
#endif //LEMON_FULL_GRAPH_H
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 GRID_GRAPH_H
20 20
#define GRID_GRAPH_H
21 21

	
22 22
#include <lemon/core.h>
23 23
#include <lemon/bits/graph_extender.h>
24 24
#include <lemon/dim2.h>
25 25
#include <lemon/assert.h>
26 26

	
27 27
///\ingroup graphs
28 28
///\file
29 29
///\brief GridGraph class.
30 30

	
31 31
namespace lemon {
32 32

	
33 33
  class GridGraphBase {
34 34

	
35 35
  public:
36 36

	
37 37
    typedef GridGraphBase Graph;
38 38

	
39 39
    class Node;
40 40
    class Edge;
41 41
    class Arc;
42 42

	
43 43
  public:
44 44

	
45 45
    GridGraphBase() {}
46 46

	
47 47
  protected:
48 48

	
49 49
    void construct(int width, int height) {
50 50
       _width = width; _height = height;
51 51
      _node_num = width * height;
52 52
      _edge_num = 2 * _node_num - width - height;
53 53
      _edge_limit = _node_num - _width;
54 54
    }
55 55

	
56 56
  public:
57 57

	
58 58
    Node operator()(int i, int j) const {
59 59
      LEMON_DEBUG(0 <= i && i < _width &&
60 60
                  0 <= j  && j < _height, "Index out of range");
61 61
      return Node(i + j * _width);
62 62
    }
63 63

	
64 64
    int col(Node n) const {
65 65
      return n._id % _width;
66 66
    }
67 67

	
68 68
    int row(Node n) const {
69 69
      return n._id / _width;
70 70
    }
71 71

	
72 72
    dim2::Point<int> pos(Node n) const {
73 73
      return dim2::Point<int>(col(n), row(n));
74 74
    }
75 75

	
76 76
    int width() const {
77 77
      return _width;
78 78
    }
79 79

	
80 80
    int height() const {
81 81
      return _height;
82 82
    }
83 83

	
84 84
    typedef True NodeNumTag;
85
    typedef True EdgeNumTag;
85 86
    typedef True ArcNumTag;
86 87

	
87 88
    int nodeNum() const { return _node_num; }
88 89
    int edgeNum() const { return _edge_num; }
89 90
    int arcNum() const { return 2 * _edge_num; }
90 91

	
91 92
    Node u(Edge edge) const {
92 93
      if (edge._id < _edge_limit) {
93 94
        return edge._id;
94 95
      } else {
95 96
        return (edge._id - _edge_limit) % (_width - 1) +
96 97
          (edge._id - _edge_limit) / (_width - 1) * _width;
97 98
      }
98 99
    }
99 100

	
100 101
    Node v(Edge edge) const {
101 102
      if (edge._id < _edge_limit) {
102 103
        return edge._id + _width;
103 104
      } else {
104 105
        return (edge._id - _edge_limit) % (_width - 1) +
105 106
          (edge._id - _edge_limit) / (_width - 1) * _width + 1;
106 107
      }
107 108
    }
108 109

	
109 110
    Node source(Arc arc) const {
110 111
      return (arc._id & 1) == 1 ? u(arc) : v(arc);
111 112
    }
112 113

	
113 114
    Node target(Arc arc) const {
114 115
      return (arc._id & 1) == 1 ? v(arc) : u(arc);
115 116
    }
116 117

	
117 118
    static int id(Node node) { return node._id; }
118 119
    static int id(Edge edge) { return edge._id; }
119 120
    static int id(Arc arc) { return arc._id; }
120 121

	
121 122
    int maxNodeId() const { return _node_num - 1; }
122 123
    int maxEdgeId() const { return _edge_num - 1; }
123 124
    int maxArcId() const { return 2 * _edge_num - 1; }
124 125

	
125 126
    static Node nodeFromId(int id) { return Node(id);}
126 127
    static Edge edgeFromId(int id) { return Edge(id);}
127 128
    static Arc arcFromId(int id) { return Arc(id);}
128 129

	
129 130
    typedef True FindEdgeTag;
131
    typedef True FindArcTag;
130 132

	
131 133
    Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
132 134
      if (prev != INVALID) return INVALID;
133 135
      if (v._id > u._id) {
134 136
        if (v._id - u._id == _width)
135 137
          return Edge(u._id);
136 138
        if (v._id - u._id == 1 && u._id % _width < _width - 1) {
137 139
          return Edge(u._id / _width * (_width - 1) +
138 140
                      u._id % _width + _edge_limit);
139 141
        }
140 142
      } else {
141 143
        if (u._id - v._id == _width)
142 144
          return Edge(v._id);
143 145
        if (u._id - v._id == 1 && v._id % _width < _width - 1) {
144 146
          return Edge(v._id / _width * (_width - 1) +
145 147
                      v._id % _width + _edge_limit);
146 148
        }
147 149
      }
148 150
      return INVALID;
149 151
    }
150 152

	
151 153
    Arc findArc(Node u, Node v, Arc prev = INVALID) const {
152 154
      if (prev != INVALID) return INVALID;
153 155
      if (v._id > u._id) {
154 156
        if (v._id - u._id == _width)
155 157
          return Arc((u._id << 1) | 1);
156 158
        if (v._id - u._id == 1 && u._id % _width < _width - 1) {
157 159
          return Arc(((u._id / _width * (_width - 1) +
158 160
                       u._id % _width + _edge_limit) << 1) | 1);
159 161
        }
160 162
      } else {
161 163
        if (u._id - v._id == _width)
162 164
          return Arc(v._id << 1);
163 165
        if (u._id - v._id == 1 && v._id % _width < _width - 1) {
164 166
          return Arc((v._id / _width * (_width - 1) +
165 167
                       v._id % _width + _edge_limit) << 1);
166 168
        }
167 169
      }
168 170
      return INVALID;
169 171
    }
170 172

	
171 173
    class Node {
172 174
      friend class GridGraphBase;
173 175

	
174 176
    protected:
175 177
      int _id;
176 178
      Node(int id) : _id(id) {}
177 179
    public:
178 180
      Node() {}
179 181
      Node (Invalid) : _id(-1) {}
180 182
      bool operator==(const Node node) const {return _id == node._id;}
181 183
      bool operator!=(const Node node) const {return _id != node._id;}
182 184
      bool operator<(const Node node) const {return _id < node._id;}
183 185
    };
184 186

	
185 187
    class Edge {
186 188
      friend class GridGraphBase;
187 189
      friend class Arc;
188 190

	
189 191
    protected:
190 192
      int _id;
191 193

	
192 194
      Edge(int id) : _id(id) {}
193 195

	
194 196
    public:
195 197
      Edge() {}
196 198
      Edge (Invalid) : _id(-1) {}
197 199
      bool operator==(const Edge edge) const {return _id == edge._id;}
198 200
      bool operator!=(const Edge edge) const {return _id != edge._id;}
199 201
      bool operator<(const Edge edge) const {return _id < edge._id;}
200 202
    };
201 203

	
202 204
    class Arc {
203 205
      friend class GridGraphBase;
204 206

	
205 207
    protected:
206 208
      int _id;
207 209

	
208 210
      Arc(int id) : _id(id) {}
209 211

	
210 212
    public:
211 213
      Arc() {}
212 214
      Arc (Invalid) : _id(-1) {}
213 215
      operator Edge() const { return _id != -1 ? Edge(_id >> 1) : INVALID; }
214 216
      bool operator==(const Arc arc) const {return _id == arc._id;}
215 217
      bool operator!=(const Arc arc) const {return _id != arc._id;}
216 218
      bool operator<(const Arc arc) const {return _id < arc._id;}
217 219
    };
218 220

	
219 221
    static bool direction(Arc arc) {
220 222
      return (arc._id & 1) == 1;
221 223
    }
222 224

	
223 225
    static Arc direct(Edge edge, bool dir) {
224 226
      return Arc((edge._id << 1) | (dir ? 1 : 0));
225 227
    }
226 228

	
227 229
    void first(Node& node) const {
228 230
      node._id = _node_num - 1;
229 231
    }
230 232

	
231 233
    static void next(Node& node) {
232 234
      --node._id;
233 235
    }
234 236

	
235 237
    void first(Edge& edge) const {
236 238
      edge._id = _edge_num - 1;
237 239
    }
238 240

	
239 241
    static void next(Edge& edge) {
240 242
      --edge._id;
241 243
    }
242 244

	
243 245
    void first(Arc& arc) const {
244 246
      arc._id = 2 * _edge_num - 1;
245 247
    }
246 248

	
247 249
    static void next(Arc& arc) {
248 250
      --arc._id;
249 251
    }
250 252

	
251 253
    void firstOut(Arc& arc, const Node& node) const {
252 254
      if (node._id % _width < _width - 1) {
253 255
        arc._id = (_edge_limit + node._id % _width +
254 256
                   (node._id / _width) * (_width - 1)) << 1 | 1;
255 257
        return;
256 258
      }
257 259
      if (node._id < _node_num - _width) {
258 260
        arc._id = node._id << 1 | 1;
259 261
        return;
260 262
      }
261 263
      if (node._id % _width > 0) {
262 264
        arc._id = (_edge_limit + node._id % _width +
263 265
                   (node._id / _width) * (_width - 1) - 1) << 1;
264 266
        return;
265 267
      }
266 268
      if (node._id >= _width) {
267 269
        arc._id = (node._id - _width) << 1;
268 270
        return;
269 271
      }
270 272
      arc._id = -1;
271 273
    }
272 274

	
273 275
    void nextOut(Arc& arc) const {
274 276
      int nid = arc._id >> 1;
275 277
      if ((arc._id & 1) == 1) {
276 278
        if (nid >= _edge_limit) {
277 279
          nid = (nid - _edge_limit) % (_width - 1) +
278 280
            (nid - _edge_limit) / (_width - 1) * _width;
279 281
          if (nid < _node_num - _width) {
280 282
            arc._id = nid << 1 | 1;
281 283
            return;
282 284
          }
283 285
        }
284 286
        if (nid % _width > 0) {
285 287
          arc._id = (_edge_limit + nid % _width +
286 288
                     (nid / _width) * (_width - 1) - 1) << 1;
287 289
          return;
288 290
        }
289 291
        if (nid >= _width) {
290 292
          arc._id = (nid - _width) << 1;
291 293
          return;
292 294
        }
293 295
      } else {
294 296
        if (nid >= _edge_limit) {
295 297
          nid = (nid - _edge_limit) % (_width - 1) +
296 298
            (nid - _edge_limit) / (_width - 1) * _width + 1;
297 299
          if (nid >= _width) {
298 300
            arc._id = (nid - _width) << 1;
299 301
            return;
300 302
          }
301 303
        }
302 304
      }
303 305
      arc._id = -1;
304 306
    }
305 307

	
306 308
    void firstIn(Arc& arc, const Node& node) const {
307 309
      if (node._id % _width < _width - 1) {
308 310
        arc._id = (_edge_limit + node._id % _width +
309 311
                   (node._id / _width) * (_width - 1)) << 1;
310 312
        return;
311 313
      }
312 314
      if (node._id < _node_num - _width) {
313 315
        arc._id = node._id << 1;
314 316
        return;
315 317
      }
316 318
      if (node._id % _width > 0) {
317 319
        arc._id = (_edge_limit + node._id % _width +
318 320
                   (node._id / _width) * (_width - 1) - 1) << 1 | 1;
319 321
        return;
320 322
      }
321 323
      if (node._id >= _width) {
322 324
        arc._id = (node._id - _width) << 1 | 1;
323 325
        return;
324 326
      }
325 327
      arc._id = -1;
326 328
    }
327 329

	
328 330
    void nextIn(Arc& arc) const {
329 331
      int nid = arc._id >> 1;
330 332
      if ((arc._id & 1) == 0) {
331 333
        if (nid >= _edge_limit) {
332 334
          nid = (nid - _edge_limit) % (_width - 1) +
333 335
            (nid - _edge_limit) / (_width - 1) * _width;
334 336
          if (nid < _node_num - _width) {
335 337
            arc._id = nid << 1;
336 338
            return;
337 339
          }
338 340
        }
339 341
        if (nid % _width > 0) {
340 342
          arc._id = (_edge_limit + nid % _width +
341 343
                     (nid / _width) * (_width - 1) - 1) << 1 | 1;
342 344
          return;
343 345
        }
344 346
        if (nid >= _width) {
345 347
          arc._id = (nid - _width) << 1 | 1;
346 348
          return;
347 349
        }
348 350
      } else {
349 351
        if (nid >= _edge_limit) {
350 352
          nid = (nid - _edge_limit) % (_width - 1) +
351 353
            (nid - _edge_limit) / (_width - 1) * _width + 1;
352 354
          if (nid >= _width) {
353 355
            arc._id = (nid - _width) << 1 | 1;
354 356
            return;
355 357
          }
356 358
        }
357 359
      }
358 360
      arc._id = -1;
359 361
    }
360 362

	
361 363
    void firstInc(Edge& edge, bool& dir, const Node& node) const {
362 364
      if (node._id % _width < _width - 1) {
363 365
        edge._id = _edge_limit + node._id % _width +
364 366
          (node._id / _width) * (_width - 1);
365 367
        dir = true;
366 368
        return;
367 369
      }
368 370
      if (node._id < _node_num - _width) {
369 371
        edge._id = node._id;
370 372
        dir = true;
371 373
        return;
372 374
      }
373 375
      if (node._id % _width > 0) {
374 376
        edge._id = _edge_limit + node._id % _width +
375 377
          (node._id / _width) * (_width - 1) - 1;
376 378
        dir = false;
377 379
        return;
378 380
      }
379 381
      if (node._id >= _width) {
380 382
        edge._id = node._id - _width;
381 383
        dir = false;
382 384
        return;
383 385
      }
384 386
      edge._id = -1;
385 387
      dir = true;
386 388
    }
387 389

	
388 390
    void nextInc(Edge& edge, bool& dir) const {
389 391
      int nid = edge._id;
390 392
      if (dir) {
391 393
        if (nid >= _edge_limit) {
392 394
          nid = (nid - _edge_limit) % (_width - 1) +
393 395
            (nid - _edge_limit) / (_width - 1) * _width;
394 396
          if (nid < _node_num - _width) {
395 397
            edge._id = nid;
396 398
            return;
397 399
          }
398 400
        }
399 401
        if (nid % _width > 0) {
400 402
          edge._id = _edge_limit + nid % _width +
401 403
            (nid / _width) * (_width - 1) - 1;
402 404
          dir = false;
403 405
          return;
404 406
        }
405 407
        if (nid >= _width) {
406 408
          edge._id = nid - _width;
407 409
          dir = false;
408 410
          return;
409 411
        }
410 412
      } else {
411 413
        if (nid >= _edge_limit) {
412 414
          nid = (nid - _edge_limit) % (_width - 1) +
413 415
            (nid - _edge_limit) / (_width - 1) * _width + 1;
414 416
          if (nid >= _width) {
415 417
            edge._id = nid - _width;
416 418
            return;
417 419
          }
418 420
        }
419 421
      }
420 422
      edge._id = -1;
421 423
      dir = true;
422 424
    }
423 425

	
424 426
    Arc right(Node n) const {
425 427
      if (n._id % _width < _width - 1) {
426 428
        return Arc(((_edge_limit + n._id % _width +
427 429
                    (n._id / _width) * (_width - 1)) << 1) | 1);
428 430
      } else {
429 431
        return INVALID;
430 432
      }
431 433
    }
432 434

	
433 435
    Arc left(Node n) const {
434 436
      if (n._id % _width > 0) {
435 437
        return Arc((_edge_limit + n._id % _width +
436 438
                     (n._id / _width) * (_width - 1) - 1) << 1);
437 439
      } else {
438 440
        return INVALID;
439 441
      }
440 442
    }
441 443

	
442 444
    Arc up(Node n) const {
443 445
      if (n._id < _edge_limit) {
444 446
        return Arc((n._id << 1) | 1);
445 447
      } else {
446 448
        return INVALID;
447 449
      }
448 450
    }
449 451

	
450 452
    Arc down(Node n) const {
451 453
      if (n._id >= _width) {
452 454
        return Arc((n._id - _width) << 1);
453 455
      } else {
454 456
        return INVALID;
455 457
      }
456 458
    }
457 459

	
458 460
  private:
459 461
    int _width, _height;
460 462
    int _node_num, _edge_num;
461 463
    int _edge_limit;
462 464
  };
463 465

	
464 466

	
465 467
  typedef GraphExtender<GridGraphBase> ExtendedGridGraphBase;
466 468

	
467 469
  /// \ingroup graphs
468 470
  ///
469 471
  /// \brief Grid graph class
470 472
  ///
471 473
  /// This class implements a special graph type. The nodes of the
472 474
  /// graph can be indexed by two integer \c (i,j) value where \c i is
473 475
  /// in the \c [0..width()-1] range and j is in the \c
474 476
  /// [0..height()-1] range.  Two nodes are connected in the graph if
475 477
  /// the indexes differ exactly on one position and exactly one is
476 478
  /// the difference. The nodes of the graph can be indexed by position
477 479
  /// with the \c operator()() function. The positions of the nodes can be
478 480
  /// get with \c pos(), \c col() and \c row() members. The outgoing
479 481
  /// arcs can be retrieved with the \c right(), \c up(), \c left()
480 482
  /// and \c down() functions, where the bottom-left corner is the
481 483
  /// origin.
482 484
  ///
483 485
  /// \image html grid_graph.png
484 486
  /// \image latex grid_graph.eps "Grid graph" width=\textwidth
485 487
  ///
486 488
  /// A short example about the basic usage:
487 489
  ///\code
488 490
  /// GridGraph graph(rows, cols);
489 491
  /// GridGraph::NodeMap<int> val(graph);
490 492
  /// for (int i = 0; i < graph.width(); ++i) {
491 493
  ///   for (int j = 0; j < graph.height(); ++j) {
492 494
  ///     val[graph(i, j)] = i + j;
493 495
  ///   }
494 496
  /// }
495 497
  ///\endcode
496 498
  ///
497 499
  /// This graph type is fully conform to the \ref concepts::Graph
498 500
  /// "Graph" concept, and it also has an important extra feature
499 501
  /// that its maps are real \ref concepts::ReferenceMap
500 502
  /// "reference map"s.
501 503
  class GridGraph : public ExtendedGridGraphBase {
502 504
  public:
503 505

	
504 506
    typedef ExtendedGridGraphBase Parent;
505 507

	
506 508
    /// \brief Map to get the indices of the nodes as dim2::Point<int>.
507 509
    ///
508 510
    /// Map to get the indices of the nodes as dim2::Point<int>.
509 511
    class IndexMap {
510 512
    public:
511 513
      /// \brief The key type of the map
512 514
      typedef GridGraph::Node Key;
513 515
      /// \brief The value type of the map
514 516
      typedef dim2::Point<int> Value;
515 517

	
516 518
      /// \brief Constructor
517 519
      ///
518 520
      /// Constructor
519 521
      IndexMap(const GridGraph& graph) : _graph(graph) {}
520 522

	
521 523
      /// \brief The subscript operator
522 524
      ///
523 525
      /// The subscript operator.
524 526
      Value operator[](Key key) const {
525 527
        return _graph.pos(key);
526 528
      }
527 529

	
528 530
    private:
529 531
      const GridGraph& _graph;
530 532
    };
531 533

	
532 534
    /// \brief Map to get the column of the nodes.
533 535
    ///
534 536
    /// Map to get the column of the nodes.
535 537
    class ColMap {
536 538
    public:
537 539
      /// \brief The key type of the map
538 540
      typedef GridGraph::Node Key;
539 541
      /// \brief The value type of the map
540 542
      typedef int Value;
541 543

	
542 544
      /// \brief Constructor
543 545
      ///
544 546
      /// Constructor
545 547
      ColMap(const GridGraph& graph) : _graph(graph) {}
546 548

	
547 549
      /// \brief The subscript operator
548 550
      ///
549 551
      /// The subscript operator.
550 552
      Value operator[](Key key) const {
551 553
        return _graph.col(key);
552 554
      }
553 555

	
554 556
    private:
555 557
      const GridGraph& _graph;
556 558
    };
557 559

	
558 560
    /// \brief Map to get the row of the nodes.
559 561
    ///
560 562
    /// Map to get the row of the nodes.
561 563
    class RowMap {
562 564
    public:
563 565
      /// \brief The key type of the map
564 566
      typedef GridGraph::Node Key;
565 567
      /// \brief The value type of the map
566 568
      typedef int Value;
567 569

	
568 570
      /// \brief Constructor
569 571
      ///
570 572
      /// Constructor
571 573
      RowMap(const GridGraph& graph) : _graph(graph) {}
572 574

	
573 575
      /// \brief The subscript operator
574 576
      ///
575 577
      /// The subscript operator.
576 578
      Value operator[](Key key) const {
577 579
        return _graph.row(key);
578 580
      }
579 581

	
580 582
    private:
581 583
      const GridGraph& _graph;
582 584
    };
583 585

	
584 586
    /// \brief Constructor
585 587
    ///
586 588
    /// Construct a grid graph with given size.
587 589
    GridGraph(int width, int height) { construct(width, height); }
588 590

	
589 591
    /// \brief Resize the graph
590 592
    ///
591 593
    /// Resize the graph. The function will fully destroy and rebuild
592 594
    /// the graph.  This cause that the maps of the graph will
593 595
    /// reallocated automatically and the previous values will be
594 596
    /// lost.
595 597
    void resize(int width, int height) {
596 598
      Parent::notifier(Arc()).clear();
597 599
      Parent::notifier(Edge()).clear();
598 600
      Parent::notifier(Node()).clear();
599 601
      construct(width, height);
600 602
      Parent::notifier(Node()).build();
601 603
      Parent::notifier(Edge()).build();
602 604
      Parent::notifier(Arc()).build();
603 605
    }
604 606

	
605 607
    /// \brief The node on the given position.
606 608
    ///
607 609
    /// Gives back the node on the given position.
608 610
    Node operator()(int i, int j) const {
609 611
      return Parent::operator()(i, j);
610 612
    }
611 613

	
612 614
    /// \brief Gives back the column index of the node.
613 615
    ///
614 616
    /// Gives back the column index of the node.
615 617
    int col(Node n) const {
616 618
      return Parent::col(n);
617 619
    }
618 620

	
619 621
    /// \brief Gives back the row index of the node.
620 622
    ///
621 623
    /// Gives back the row index of the node.
622 624
    int row(Node n) const {
623 625
      return Parent::row(n);
624 626
    }
625 627

	
626 628
    /// \brief Gives back the position of the node.
627 629
    ///
628 630
    /// Gives back the position of the node, ie. the <tt>(col,row)</tt> pair.
629 631
    dim2::Point<int> pos(Node n) const {
630 632
      return Parent::pos(n);
631 633
    }
632 634

	
633 635
    /// \brief Gives back the number of the columns.
634 636
    ///
635 637
    /// Gives back the number of the columns.
636 638
    int width() const {
637 639
      return Parent::width();
638 640
    }
639 641

	
640 642
    /// \brief Gives back the number of the rows.
641 643
    ///
642 644
    /// Gives back the number of the rows.
643 645
    int height() const {
644 646
      return Parent::height();
645 647
    }
646 648

	
647 649
    /// \brief Gives back the arc goes right from the node.
648 650
    ///
649 651
    /// Gives back the arc goes right from the node. If there is not
650 652
    /// outgoing arc then it gives back INVALID.
651 653
    Arc right(Node n) const {
652 654
      return Parent::right(n);
653 655
    }
654 656

	
655 657
    /// \brief Gives back the arc goes left from the node.
656 658
    ///
657 659
    /// Gives back the arc goes left from the node. If there is not
658 660
    /// outgoing arc then it gives back INVALID.
659 661
    Arc left(Node n) const {
660 662
      return Parent::left(n);
661 663
    }
662 664

	
663 665
    /// \brief Gives back the arc goes up from the node.
664 666
    ///
665 667
    /// Gives back the arc goes up from the node. If there is not
666 668
    /// outgoing arc then it gives back INVALID.
667 669
    Arc up(Node n) const {
668 670
      return Parent::up(n);
669 671
    }
670 672

	
671 673
    /// \brief Gives back the arc goes down from the node.
672 674
    ///
673 675
    /// Gives back the arc goes down from the node. If there is not
674 676
    /// outgoing arc then it gives back INVALID.
675 677
    Arc down(Node n) const {
676 678
      return Parent::down(n);
677 679
    }
678 680

	
679 681
    /// \brief Index map of the grid graph
680 682
    ///
681 683
    /// Just returns an IndexMap for the grid graph.
682 684
    IndexMap indexMap() const {
683 685
      return IndexMap(*this);
684 686
    }
685 687

	
686 688
    /// \brief Row map of the grid graph
687 689
    ///
688 690
    /// Just returns a RowMap for the grid graph.
689 691
    RowMap rowMap() const {
690 692
      return RowMap(*this);
691 693
    }
692 694

	
693 695
    /// \brief Column map of the grid graph
694 696
    ///
695 697
    /// Just returns a ColMap for the grid graph.
696 698
    ColMap colMap() const {
697 699
      return ColMap(*this);
698 700
    }
699 701

	
700 702
  };
701 703

	
702 704
}
703 705
#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_SMART_GRAPH_H
20 20
#define LEMON_SMART_GRAPH_H
21 21

	
22 22
///\ingroup graphs
23 23
///\file
24 24
///\brief SmartDigraph and SmartGraph classes.
25 25

	
26 26
#include <vector>
27 27

	
28 28
#include <lemon/core.h>
29 29
#include <lemon/error.h>
30 30
#include <lemon/bits/graph_extender.h>
31 31

	
32 32
namespace lemon {
33 33

	
34 34
  class SmartDigraph;
35 35
  ///Base of SmartDigraph
36 36

	
37 37
  ///Base of SmartDigraph
38 38
  ///
39 39
  class SmartDigraphBase {
40 40
  protected:
41 41

	
42 42
    struct NodeT
43 43
    {
44 44
      int first_in, first_out;
45 45
      NodeT() {}
46 46
    };
47 47
    struct ArcT
48 48
    {
49 49
      int target, source, next_in, next_out;
50 50
      ArcT() {}
51 51
    };
52 52

	
53 53
    std::vector<NodeT> nodes;
54 54
    std::vector<ArcT> arcs;
55 55

	
56 56
  public:
57 57

	
58 58
    typedef SmartDigraphBase Graph;
59 59

	
60 60
    class Node;
61 61
    class Arc;
62 62

	
63 63
  public:
64 64

	
65 65
    SmartDigraphBase() : nodes(), arcs() { }
66 66
    SmartDigraphBase(const SmartDigraphBase &_g)
67 67
      : nodes(_g.nodes), arcs(_g.arcs) { }
68 68

	
69 69
    typedef True NodeNumTag;
70
    typedef True EdgeNumTag;
70
    typedef True ArcNumTag;
71 71

	
72 72
    int nodeNum() const { return nodes.size(); }
73 73
    int arcNum() const { return arcs.size(); }
74 74

	
75 75
    int maxNodeId() const { return nodes.size()-1; }
76 76
    int maxArcId() const { return arcs.size()-1; }
77 77

	
78 78
    Node addNode() {
79 79
      int n = nodes.size();
80 80
      nodes.push_back(NodeT());
81 81
      nodes[n].first_in = -1;
82 82
      nodes[n].first_out = -1;
83 83
      return Node(n);
84 84
    }
85 85

	
86 86
    Arc addArc(Node u, Node v) {
87 87
      int n = arcs.size();
88 88
      arcs.push_back(ArcT());
89 89
      arcs[n].source = u._id;
90 90
      arcs[n].target = v._id;
91 91
      arcs[n].next_out = nodes[u._id].first_out;
92 92
      arcs[n].next_in = nodes[v._id].first_in;
93 93
      nodes[u._id].first_out = nodes[v._id].first_in = n;
94 94

	
95 95
      return Arc(n);
96 96
    }
97 97

	
98 98
    void clear() {
99 99
      arcs.clear();
100 100
      nodes.clear();
101 101
    }
102 102

	
103 103
    Node source(Arc a) const { return Node(arcs[a._id].source); }
104 104
    Node target(Arc a) const { return Node(arcs[a._id].target); }
105 105

	
106 106
    static int id(Node v) { return v._id; }
107 107
    static int id(Arc a) { return a._id; }
108 108

	
109 109
    static Node nodeFromId(int id) { return Node(id);}
110 110
    static Arc arcFromId(int id) { return Arc(id);}
111 111

	
112 112
    bool valid(Node n) const {
113 113
      return n._id >= 0 && n._id < static_cast<int>(nodes.size());
114 114
    }
115 115
    bool valid(Arc a) const {
116 116
      return a._id >= 0 && a._id < static_cast<int>(arcs.size());
117 117
    }
118 118

	
119 119
    class Node {
120 120
      friend class SmartDigraphBase;
121 121
      friend class SmartDigraph;
122 122

	
123 123
    protected:
124 124
      int _id;
125 125
      explicit Node(int id) : _id(id) {}
126 126
    public:
127 127
      Node() {}
128 128
      Node (Invalid) : _id(-1) {}
129 129
      bool operator==(const Node i) const {return _id == i._id;}
130 130
      bool operator!=(const Node i) const {return _id != i._id;}
131 131
      bool operator<(const Node i) const {return _id < i._id;}
132 132
    };
133 133

	
134 134

	
135 135
    class Arc {
136 136
      friend class SmartDigraphBase;
137 137
      friend class SmartDigraph;
138 138

	
139 139
    protected:
140 140
      int _id;
141 141
      explicit Arc(int id) : _id(id) {}
142 142
    public:
143 143
      Arc() { }
144 144
      Arc (Invalid) : _id(-1) {}
145 145
      bool operator==(const Arc i) const {return _id == i._id;}
146 146
      bool operator!=(const Arc i) const {return _id != i._id;}
147 147
      bool operator<(const Arc i) const {return _id < i._id;}
148 148
    };
149 149

	
150 150
    void first(Node& node) const {
151 151
      node._id = nodes.size() - 1;
152 152
    }
153 153

	
154 154
    static void next(Node& node) {
155 155
      --node._id;
156 156
    }
157 157

	
158 158
    void first(Arc& arc) const {
159 159
      arc._id = arcs.size() - 1;
160 160
    }
161 161

	
162 162
    static void next(Arc& arc) {
163 163
      --arc._id;
164 164
    }
165 165

	
166 166
    void firstOut(Arc& arc, const Node& node) const {
167 167
      arc._id = nodes[node._id].first_out;
168 168
    }
169 169

	
170 170
    void nextOut(Arc& arc) const {
171 171
      arc._id = arcs[arc._id].next_out;
172 172
    }
173 173

	
174 174
    void firstIn(Arc& arc, const Node& node) const {
175 175
      arc._id = nodes[node._id].first_in;
176 176
    }
177 177

	
178 178
    void nextIn(Arc& arc) const {
179 179
      arc._id = arcs[arc._id].next_in;
180 180
    }
181 181

	
182 182
  };
183 183

	
184 184
  typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase;
185 185

	
186 186
  ///\ingroup graphs
187 187
  ///
188 188
  ///\brief A smart directed graph class.
189 189
  ///
190 190
  ///This is a simple and fast digraph implementation.
191 191
  ///It is also quite memory efficient, but at the price
192 192
  ///that <b> it does support only limited (only stack-like)
193 193
  ///node and arc deletions</b>.
194 194
  ///It conforms to the \ref concepts::Digraph "Digraph concept" with
195 195
  ///an important extra feature that its maps are real \ref
196 196
  ///concepts::ReferenceMap "reference map"s.
197 197
  ///
198 198
  ///\sa concepts::Digraph.
199 199
  class SmartDigraph : public ExtendedSmartDigraphBase {
200 200
  public:
201 201

	
202 202
    typedef ExtendedSmartDigraphBase Parent;
203 203

	
204 204
  private:
205 205

	
206 206
    ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
207 207

	
208 208
    ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
209 209
    ///
210 210
    SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {};
211 211
    ///\brief Assignment of SmartDigraph to another one is \e not allowed.
212 212
    ///Use DigraphCopy() instead.
213 213

	
214 214
    ///Assignment of SmartDigraph to another one is \e not allowed.
215 215
    ///Use DigraphCopy() instead.
216 216
    void operator=(const SmartDigraph &) {}
217 217

	
218 218
  public:
219 219

	
220 220
    /// Constructor
221 221

	
222 222
    /// Constructor.
223 223
    ///
224 224
    SmartDigraph() {};
225 225

	
226 226
    ///Add a new node to the digraph.
227 227

	
228 228
    /// \return the new node.
229 229
    ///
230 230
    Node addNode() { return Parent::addNode(); }
231 231

	
232 232
    ///Add a new arc to the digraph.
233 233

	
234 234
    ///Add a new arc to the digraph with source node \c s
235 235
    ///and target node \c t.
236 236
    ///\return the new arc.
237 237
    Arc addArc(const Node& s, const Node& t) {
238 238
      return Parent::addArc(s, t);
239 239
    }
240 240

	
241 241
    /// \brief Using this it is possible to avoid the superfluous memory
242 242
    /// allocation.
243 243

	
244 244
    /// Using this it is possible to avoid the superfluous memory
245 245
    /// allocation: if you know that the digraph you want to build will
246 246
    /// be very large (e.g. it will contain millions of nodes and/or arcs)
247 247
    /// then it is worth reserving space for this amount before starting
248 248
    /// to build the digraph.
249 249
    /// \sa reserveArc
250 250
    void reserveNode(int n) { nodes.reserve(n); };
251 251

	
252 252
    /// \brief Using this it is possible to avoid the superfluous memory
253 253
    /// allocation.
254 254

	
255 255
    /// Using this it is possible to avoid the superfluous memory
256 256
    /// allocation: if you know that the digraph you want to build will
257 257
    /// be very large (e.g. it will contain millions of nodes and/or arcs)
258 258
    /// then it is worth reserving space for this amount before starting
259 259
    /// to build the digraph.
260 260
    /// \sa reserveNode
261 261
    void reserveArc(int m) { arcs.reserve(m); };
262 262

	
263 263
    /// \brief Node validity check
264 264
    ///
265 265
    /// This function gives back true if the given node is valid,
266 266
    /// ie. it is a real node of the graph.
267 267
    ///
268 268
    /// \warning A removed node (using Snapshot) could become valid again
269 269
    /// when new nodes are added to the graph.
270 270
    bool valid(Node n) const { return Parent::valid(n); }
271 271

	
272 272
    /// \brief Arc validity check
273 273
    ///
274 274
    /// This function gives back true if the given arc is valid,
275 275
    /// ie. it is a real arc of the graph.
276 276
    ///
277 277
    /// \warning A removed arc (using Snapshot) could become valid again
278 278
    /// when new arcs are added to the graph.
279 279
    bool valid(Arc a) const { return Parent::valid(a); }
280 280

	
281 281
    ///Clear the digraph.
282 282

	
283 283
    ///Erase all the nodes and arcs from the digraph.
284 284
    ///
285 285
    void clear() {
286 286
      Parent::clear();
287 287
    }
288 288

	
289 289
    ///Split a node.
290 290

	
291 291
    ///This function splits a node. First a new node is added to the digraph,
292 292
    ///then the source of each outgoing arc of \c n is moved to this new node.
293 293
    ///If \c connect is \c true (this is the default value), then a new arc
294 294
    ///from \c n to the newly created node is also added.
295 295
    ///\return The newly created node.
296 296
    ///
297 297
    ///\note The <tt>Arc</tt>s
298 298
    ///referencing a moved arc remain
299 299
    ///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s
300 300
    ///may be invalidated.
301 301
    ///\warning This functionality cannot be used together with the Snapshot
302 302
    ///feature.
303 303
    Node split(Node n, bool connect = true)
304 304
    {
305 305
      Node b = addNode();
306 306
      nodes[b._id].first_out=nodes[n._id].first_out;
307 307
      nodes[n._id].first_out=-1;
308 308
      for(int i=nodes[b._id].first_out;i!=-1;i++) arcs[i].source=b._id;
309 309
      if(connect) addArc(n,b);
310 310
      return b;
311 311
    }
312 312

	
313 313
  public:
314 314

	
315 315
    class Snapshot;
316 316

	
317 317
  protected:
318 318

	
319 319
    void restoreSnapshot(const Snapshot &s)
320 320
    {
321 321
      while(s.arc_num<arcs.size()) {
322 322
        Arc arc = arcFromId(arcs.size()-1);
323 323
        Parent::notifier(Arc()).erase(arc);
324 324
        nodes[arcs.back().source].first_out=arcs.back().next_out;
325 325
        nodes[arcs.back().target].first_in=arcs.back().next_in;
326 326
        arcs.pop_back();
327 327
      }
328 328
      while(s.node_num<nodes.size()) {
329 329
        Node node = nodeFromId(nodes.size()-1);
330 330
        Parent::notifier(Node()).erase(node);
331 331
        nodes.pop_back();
332 332
      }
333 333
    }
334 334

	
335 335
  public:
336 336

	
337 337
    ///Class to make a snapshot of the digraph and to restrore to it later.
338 338

	
339 339
    ///Class to make a snapshot of the digraph and to restrore to it later.
340 340
    ///
341 341
    ///The newly added nodes and arcs can be removed using the
342 342
    ///restore() function.
343 343
    ///\note After you restore a state, you cannot restore
344 344
    ///a later state, in other word you cannot add again the arcs deleted
345 345
    ///by restore() using another one Snapshot instance.
346 346
    ///
347 347
    ///\warning If you do not use correctly the snapshot that can cause
348 348
    ///either broken program, invalid state of the digraph, valid but
349 349
    ///not the restored digraph or no change. Because the runtime performance
350 350
    ///the validity of the snapshot is not stored.
351 351
    class Snapshot
352 352
    {
353 353
      SmartDigraph *_graph;
354 354
    protected:
355 355
      friend class SmartDigraph;
356 356
      unsigned int node_num;
357 357
      unsigned int arc_num;
358 358
    public:
359 359
      ///Default constructor.
360 360

	
361 361
      ///Default constructor.
362 362
      ///To actually make a snapshot you must call save().
363 363
      ///
364 364
      Snapshot() : _graph(0) {}
365 365
      ///Constructor that immediately makes a snapshot
366 366

	
367 367
      ///This constructor immediately makes a snapshot of the digraph.
368 368
      ///\param graph The digraph we make a snapshot of.
369 369
      Snapshot(SmartDigraph &graph) : _graph(&graph) {
370 370
        node_num=_graph->nodes.size();
371 371
        arc_num=_graph->arcs.size();
372 372
      }
373 373

	
374 374
      ///Make a snapshot.
375 375

	
376 376
      ///Make a snapshot of the digraph.
377 377
      ///
378 378
      ///This function can be called more than once. In case of a repeated
379 379
      ///call, the previous snapshot gets lost.
380 380
      ///\param graph The digraph we make the snapshot of.
381 381
      void save(SmartDigraph &graph)
382 382
      {
383 383
        _graph=&graph;
384 384
        node_num=_graph->nodes.size();
385 385
        arc_num=_graph->arcs.size();
386 386
      }
387 387

	
388 388
      ///Undo the changes until a snapshot.
389 389

	
390 390
      ///Undo the changes until a snapshot created by save().
391 391
      ///
392 392
      ///\note After you restored a state, you cannot restore
393 393
      ///a later state, in other word you cannot add again the arcs deleted
394 394
      ///by restore().
395 395
      void restore()
396 396
      {
397 397
        _graph->restoreSnapshot(*this);
398 398
      }
399 399
    };
400 400
  };
401 401

	
402 402

	
403 403
  class SmartGraphBase {
404 404

	
405 405
  protected:
406 406

	
407 407
    struct NodeT {
408 408
      int first_out;
409 409
    };
410 410

	
411 411
    struct ArcT {
412 412
      int target;
413 413
      int next_out;
414 414
    };
415 415

	
416 416
    std::vector<NodeT> nodes;
417 417
    std::vector<ArcT> arcs;
418 418

	
419 419
    int first_free_arc;
420 420

	
421 421
  public:
422 422

	
423 423
    typedef SmartGraphBase Digraph;
424 424

	
425 425
    class Node;
426 426
    class Arc;
427 427
    class Edge;
428 428

	
429 429
    class Node {
430 430
      friend class SmartGraphBase;
431 431
    protected:
432 432

	
433 433
      int _id;
434 434
      explicit Node(int id) { _id = id;}
435 435

	
436 436
    public:
437 437
      Node() {}
438 438
      Node (Invalid) { _id = -1; }
439 439
      bool operator==(const Node& node) const {return _id == node._id;}
440 440
      bool operator!=(const Node& node) const {return _id != node._id;}
441 441
      bool operator<(const Node& node) const {return _id < node._id;}
442 442
    };
443 443

	
444 444
    class Edge {
445 445
      friend class SmartGraphBase;
446 446
    protected:
447 447

	
448 448
      int _id;
449 449
      explicit Edge(int id) { _id = id;}
450 450

	
451 451
    public:
452 452
      Edge() {}
453 453
      Edge (Invalid) { _id = -1; }
454 454
      bool operator==(const Edge& arc) const {return _id == arc._id;}
455 455
      bool operator!=(const Edge& arc) const {return _id != arc._id;}
456 456
      bool operator<(const Edge& arc) const {return _id < arc._id;}
457 457
    };
458 458

	
459 459
    class Arc {
460 460
      friend class SmartGraphBase;
461 461
    protected:
462 462

	
463 463
      int _id;
464 464
      explicit Arc(int id) { _id = id;}
465 465

	
466 466
    public:
467 467
      operator Edge() const {
468 468
        return _id != -1 ? edgeFromId(_id / 2) : INVALID;
469 469
      }
470 470

	
471 471
      Arc() {}
472 472
      Arc (Invalid) { _id = -1; }
473 473
      bool operator==(const Arc& arc) const {return _id == arc._id;}
474 474
      bool operator!=(const Arc& arc) const {return _id != arc._id;}
475 475
      bool operator<(const Arc& arc) const {return _id < arc._id;}
476 476
    };
477 477

	
478 478

	
479 479

	
480 480
    SmartGraphBase()
481 481
      : nodes(), arcs() {}
482 482

	
483 483

	
484 484
    int maxNodeId() const { return nodes.size()-1; }
485 485
    int maxEdgeId() const { return arcs.size() / 2 - 1; }
486 486
    int maxArcId() const { return arcs.size()-1; }
487 487

	
488 488
    Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); }
489 489
    Node target(Arc e) const { return Node(arcs[e._id].target); }
490 490

	
491 491
    Node u(Edge e) const { return Node(arcs[2 * e._id].target); }
492 492
    Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
493 493

	
494 494
    static bool direction(Arc e) {
495 495
      return (e._id & 1) == 1;
496 496
    }
497 497

	
498 498
    static Arc direct(Edge e, bool d) {
499 499
      return Arc(e._id * 2 + (d ? 1 : 0));
500 500
    }
501 501

	
502 502
    void first(Node& node) const {
503 503
      node._id = nodes.size() - 1;
504 504
    }
505 505

	
506 506
    void next(Node& node) const {
507 507
      --node._id;
508 508
    }
509 509

	
510 510
    void first(Arc& arc) const {
511 511
      arc._id = arcs.size() - 1;
512 512
    }
513 513

	
514 514
    void next(Arc& arc) const {
515 515
      --arc._id;
516 516
    }
517 517

	
518 518
    void first(Edge& arc) const {
519 519
      arc._id = arcs.size() / 2 - 1;
520 520
    }
521 521

	
522 522
    void next(Edge& arc) const {
523 523
      --arc._id;
524 524
    }
525 525

	
526 526
    void firstOut(Arc &arc, const Node& v) const {
527 527
      arc._id = nodes[v._id].first_out;
528 528
    }
529 529
    void nextOut(Arc &arc) const {
530 530
      arc._id = arcs[arc._id].next_out;
531 531
    }
532 532

	
533 533
    void firstIn(Arc &arc, const Node& v) const {
534 534
      arc._id = ((nodes[v._id].first_out) ^ 1);
535 535
      if (arc._id == -2) arc._id = -1;
536 536
    }
537 537
    void nextIn(Arc &arc) const {
538 538
      arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1);
539 539
      if (arc._id == -2) arc._id = -1;
540 540
    }
541 541

	
542 542
    void firstInc(Edge &arc, bool& d, const Node& v) const {
543 543
      int de = nodes[v._id].first_out;
544 544
      if (de != -1) {
545 545
        arc._id = de / 2;
546 546
        d = ((de & 1) == 1);
547 547
      } else {
548 548
        arc._id = -1;
549 549
        d = true;
550 550
      }
551 551
    }
552 552
    void nextInc(Edge &arc, bool& d) const {
553 553
      int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out);
554 554
      if (de != -1) {
555 555
        arc._id = de / 2;
556 556
        d = ((de & 1) == 1);
557 557
      } else {
558 558
        arc._id = -1;
559 559
        d = true;
560 560
      }
561 561
    }
562 562

	
563 563
    static int id(Node v) { return v._id; }
564 564
    static int id(Arc e) { return e._id; }
565 565
    static int id(Edge e) { return e._id; }
566 566

	
567 567
    static Node nodeFromId(int id) { return Node(id);}
568 568
    static Arc arcFromId(int id) { return Arc(id);}
569 569
    static Edge edgeFromId(int id) { return Edge(id);}
570 570

	
571 571
    bool valid(Node n) const {
572 572
      return n._id >= 0 && n._id < static_cast<int>(nodes.size());
573 573
    }
574 574
    bool valid(Arc a) const {
575 575
      return a._id >= 0 && a._id < static_cast<int>(arcs.size());
576 576
    }
577 577
    bool valid(Edge e) const {
578 578
      return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size());
579 579
    }
580 580

	
581 581
    Node addNode() {
582 582
      int n = nodes.size();
583 583
      nodes.push_back(NodeT());
584 584
      nodes[n].first_out = -1;
585 585

	
586 586
      return Node(n);
587 587
    }
588 588

	
589 589
    Edge addEdge(Node u, Node v) {
590 590
      int n = arcs.size();
591 591
      arcs.push_back(ArcT());
592 592
      arcs.push_back(ArcT());
593 593

	
594 594
      arcs[n].target = u._id;
595 595
      arcs[n | 1].target = v._id;
596 596

	
597 597
      arcs[n].next_out = nodes[v._id].first_out;
598 598
      nodes[v._id].first_out = n;
599 599

	
600 600
      arcs[n | 1].next_out = nodes[u._id].first_out;
601 601
      nodes[u._id].first_out = (n | 1);
602 602

	
603 603
      return Edge(n / 2);
604 604
    }
605 605

	
606 606
    void clear() {
607 607
      arcs.clear();
608 608
      nodes.clear();
609 609
    }
610 610

	
611 611
  };
612 612

	
613 613
  typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
614 614

	
615 615
  /// \ingroup graphs
616 616
  ///
617 617
  /// \brief A smart undirected graph class.
618 618
  ///
619 619
  /// This is a simple and fast graph implementation.
620 620
  /// It is also quite memory efficient, but at the price
621 621
  /// that <b> it does support only limited (only stack-like)
622 622
  /// node and arc deletions</b>.
623 623
  /// Except from this it conforms to
624 624
  /// the \ref concepts::Graph "Graph concept".
625 625
  ///
626 626
  /// It also has an
627 627
  /// important extra feature that
628 628
  /// its maps are real \ref concepts::ReferenceMap "reference map"s.
629 629
  ///
630 630
  /// \sa concepts::Graph.
631 631
  ///
632 632
  class SmartGraph : public ExtendedSmartGraphBase {
633 633
  private:
634 634

	
635 635
    ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
636 636

	
637 637
    ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
638 638
    ///
639 639
    SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
640 640

	
641 641
    ///\brief Assignment of SmartGraph to another one is \e not allowed.
642 642
    ///Use GraphCopy() instead.
643 643

	
644 644
    ///Assignment of SmartGraph to another one is \e not allowed.
645 645
    ///Use GraphCopy() instead.
646 646
    void operator=(const SmartGraph &) {}
647 647

	
648 648
  public:
649 649

	
650 650
    typedef ExtendedSmartGraphBase Parent;
651 651

	
652 652
    /// Constructor
653 653

	
654 654
    /// Constructor.
655 655
    ///
656 656
    SmartGraph() {}
657 657

	
658 658
    ///Add a new node to the graph.
659 659

	
660 660
    /// \return the new node.
661 661
    ///
662 662
    Node addNode() { return Parent::addNode(); }
663 663

	
664 664
    ///Add a new edge to the graph.
665 665

	
666 666
    ///Add a new edge to the graph with node \c s
667 667
    ///and \c t.
668 668
    ///\return the new edge.
669 669
    Edge addEdge(const Node& s, const Node& t) {
670 670
      return Parent::addEdge(s, t);
671 671
    }
672 672

	
673 673
    /// \brief Node validity check
674 674
    ///
675 675
    /// This function gives back true if the given node is valid,
676 676
    /// ie. it is a real node of the graph.
677 677
    ///
678 678
    /// \warning A removed node (using Snapshot) could become valid again
679 679
    /// when new nodes are added to the graph.
680 680
    bool valid(Node n) const { return Parent::valid(n); }
681 681

	
682 682
    /// \brief Arc validity check
683 683
    ///
684 684
    /// This function gives back true if the given arc is valid,
685 685
    /// ie. it is a real arc of the graph.
686 686
    ///
687 687
    /// \warning A removed arc (using Snapshot) could become valid again
688 688
    /// when new edges are added to the graph.
689 689
    bool valid(Arc a) const { return Parent::valid(a); }
690 690

	
691 691
    /// \brief Edge validity check
692 692
    ///
693 693
    /// This function gives back true if the given edge is valid,
694 694
    /// ie. it is a real edge of the graph.
695 695
    ///
696 696
    /// \warning A removed edge (using Snapshot) could become valid again
697 697
    /// when new edges are added to the graph.
698 698
    bool valid(Edge e) const { return Parent::valid(e); }
699 699

	
700 700
    ///Clear the graph.
701 701

	
702 702
    ///Erase all the nodes and edges from the graph.
703 703
    ///
704 704
    void clear() {
705 705
      Parent::clear();
706 706
    }
707 707

	
708 708
  public:
709 709

	
710 710
    class Snapshot;
711 711

	
712 712
  protected:
713 713

	
714 714
    void saveSnapshot(Snapshot &s)
715 715
    {
716 716
      s._graph = this;
717 717
      s.node_num = nodes.size();
718 718
      s.arc_num = arcs.size();
719 719
    }
720 720

	
721 721
    void restoreSnapshot(const Snapshot &s)
722 722
    {
723 723
      while(s.arc_num<arcs.size()) {
724 724
        int n=arcs.size()-1;
725 725
        Edge arc=edgeFromId(n/2);
726 726
        Parent::notifier(Edge()).erase(arc);
727 727
        std::vector<Arc> dir;
728 728
        dir.push_back(arcFromId(n));
729 729
        dir.push_back(arcFromId(n-1));
730 730
        Parent::notifier(Arc()).erase(dir);
731 731
        nodes[arcs[n].target].first_out=arcs[n].next_out;
732 732
        nodes[arcs[n-1].target].first_out=arcs[n-1].next_out;
733 733
        arcs.pop_back();
734 734
        arcs.pop_back();
735 735
      }
736 736
      while(s.node_num<nodes.size()) {
737 737
        int n=nodes.size()-1;
738 738
        Node node = nodeFromId(n);
739 739
        Parent::notifier(Node()).erase(node);
740 740
        nodes.pop_back();
741 741
      }
742 742
    }
743 743

	
744 744
  public:
745 745

	
746 746
    ///Class to make a snapshot of the digraph and to restrore to it later.
747 747

	
748 748
    ///Class to make a snapshot of the digraph and to restrore to it later.
749 749
    ///
750 750
    ///The newly added nodes and arcs can be removed using the
751 751
    ///restore() function.
752 752
    ///
753 753
    ///\note After you restore a state, you cannot restore
754 754
    ///a later state, in other word you cannot add again the arcs deleted
755 755
    ///by restore() using another one Snapshot instance.
756 756
    ///
757 757
    ///\warning If you do not use correctly the snapshot that can cause
758 758
    ///either broken program, invalid state of the digraph, valid but
759 759
    ///not the restored digraph or no change. Because the runtime performance
760 760
    ///the validity of the snapshot is not stored.
761 761
    class Snapshot
762 762
    {
763 763
      SmartGraph *_graph;
764 764
    protected:
765 765
      friend class SmartGraph;
766 766
      unsigned int node_num;
767 767
      unsigned int arc_num;
768 768
    public:
769 769
      ///Default constructor.
770 770

	
771 771
      ///Default constructor.
772 772
      ///To actually make a snapshot you must call save().
773 773
      ///
774 774
      Snapshot() : _graph(0) {}
775 775
      ///Constructor that immediately makes a snapshot
776 776

	
777 777
      ///This constructor immediately makes a snapshot of the digraph.
778 778
      ///\param graph The digraph we make a snapshot of.
779 779
      Snapshot(SmartGraph &graph) {
780 780
        graph.saveSnapshot(*this);
781 781
      }
782 782

	
783 783
      ///Make a snapshot.
784 784

	
785 785
      ///Make a snapshot of the graph.
786 786
      ///
787 787
      ///This function can be called more than once. In case of a repeated
788 788
      ///call, the previous snapshot gets lost.
789 789
      ///\param graph The digraph we make the snapshot of.
790 790
      void save(SmartGraph &graph)
791 791
      {
792 792
        graph.saveSnapshot(*this);
793 793
      }
794 794

	
795 795
      ///Undo the changes until a snapshot.
796 796

	
797 797
      ///Undo the changes until a snapshot created by save().
798 798
      ///
799 799
      ///\note After you restored a state, you cannot restore
800 800
      ///a later state, in other word you cannot add again the arcs deleted
801 801
      ///by restore().
802 802
      void restore()
803 803
      {
804 804
        _graph->restoreSnapshot(*this);
805 805
      }
806 806
    };
807 807
  };
808 808

	
809 809
} //namespace lemon
810 810

	
811 811

	
812 812
#endif //LEMON_SMART_GRAPH_H
0 comments (0 inline)