marci@642
|
1 |
// -*- c++ -*-
|
marci@642
|
2 |
#ifndef HUGO_SAGE_GRAPH_H
|
marci@642
|
3 |
#define HUGO_SAGE_GRAPH_H
|
marci@642
|
4 |
|
marci@642
|
5 |
#include <iostream>
|
marci@642
|
6 |
#include <vector>
|
marci@642
|
7 |
|
marci@642
|
8 |
#include <hugo/invalid.h>
|
marci@642
|
9 |
|
marci@642
|
10 |
namespace hugo {
|
marci@642
|
11 |
|
alpar@774
|
12 |
// template <typename It>
|
alpar@774
|
13 |
// int count(It it) {
|
alpar@774
|
14 |
// int i=0;
|
alpar@774
|
15 |
// for( ; it.valid(); ++it) { ++i; }
|
alpar@774
|
16 |
// return i;
|
alpar@774
|
17 |
// }
|
marci@642
|
18 |
|
marci@642
|
19 |
class SageGraph {
|
marci@642
|
20 |
struct node_item;
|
marci@642
|
21 |
struct edge_item;
|
marci@642
|
22 |
public:
|
marci@642
|
23 |
class Node;
|
marci@642
|
24 |
class NodeIt;
|
marci@642
|
25 |
class Edge;
|
marci@642
|
26 |
class EdgeIt;
|
marci@642
|
27 |
class OutEdgeIt;
|
marci@642
|
28 |
class InEdgeIt;
|
marci@642
|
29 |
class SymEdgeIt;
|
marci@642
|
30 |
template <typename T> class NodeMap;
|
marci@642
|
31 |
template <typename T> class EdgeMap;
|
marci@642
|
32 |
// private:
|
marci@642
|
33 |
template <typename T> friend class NodeMap;
|
marci@642
|
34 |
template <typename T> friend class EdgeMap;
|
marci@642
|
35 |
|
marci@642
|
36 |
template <typename T>
|
marci@642
|
37 |
class NodeMap {
|
marci@642
|
38 |
const SageGraph& G;
|
marci@642
|
39 |
std::vector<T> container;
|
marci@642
|
40 |
public:
|
marci@642
|
41 |
typedef T ValueType;
|
marci@642
|
42 |
typedef Node KeyType;
|
marci@642
|
43 |
NodeMap(const SageGraph& _G) : G(_G), container(G.node_id) { }
|
marci@642
|
44 |
NodeMap(const SageGraph& _G, T a) :
|
marci@642
|
45 |
G(_G), container(G.node_id, a) { }
|
marci@642
|
46 |
void set(Node n, T a) { container[/*G.id(n)*/n.node->id]=a; }
|
marci@642
|
47 |
// T get(Node n) const { return container[/*G.id(n)*/n.node->id]; }
|
marci@642
|
48 |
typename std::vector<T>::reference operator[](Node n) {
|
marci@642
|
49 |
return container[/*G.id(n)*/n.node->id]; }
|
marci@642
|
50 |
typename std::vector<T>::const_reference operator[](Node n) const {
|
marci@642
|
51 |
return container[/*G.id(n)*/n.node->id];
|
marci@642
|
52 |
}
|
marci@642
|
53 |
void update() { container.resize(G.node_id); }
|
marci@642
|
54 |
void update(T a) { container.resize(G.node_id, a); }
|
marci@642
|
55 |
};
|
marci@642
|
56 |
|
marci@642
|
57 |
template <typename T>
|
marci@642
|
58 |
class EdgeMap {
|
marci@642
|
59 |
const SageGraph& G;
|
marci@642
|
60 |
std::vector<T> container;
|
marci@642
|
61 |
public:
|
marci@642
|
62 |
typedef T ValueType;
|
marci@642
|
63 |
typedef Edge KeyType;
|
marci@642
|
64 |
EdgeMap(const SageGraph& _G) : G(_G), container(G.edge_id) { }
|
marci@642
|
65 |
EdgeMap(const SageGraph& _G, T a) :
|
marci@642
|
66 |
G(_G), container(G.edge_id, a) { }
|
marci@642
|
67 |
void set(Edge e, T a) { container[/*G.id(e)*/e.edge->id]=a; }
|
marci@642
|
68 |
// T get(Edge e) const { return container[/*G.id(e)*/e.edge->id]; }
|
marci@642
|
69 |
typename std::vector<T>::reference operator[](Edge e) {
|
marci@642
|
70 |
return container[/*G.id(e)*/e.edge->id]; }
|
marci@642
|
71 |
typename std::vector<T>::const_reference operator[](Edge e) const {
|
marci@642
|
72 |
return container[/*G.id(e)*/e.edge->id];
|
marci@642
|
73 |
}
|
marci@642
|
74 |
void update() { container.resize(G.edge_id); }
|
marci@642
|
75 |
void update(T a) { container.resize(G.edge_id, a); }
|
marci@642
|
76 |
};
|
marci@642
|
77 |
|
marci@642
|
78 |
private:
|
marci@642
|
79 |
int node_id;
|
marci@642
|
80 |
int edge_id;
|
marci@642
|
81 |
int _node_num;
|
marci@642
|
82 |
int _edge_num;
|
marci@642
|
83 |
|
marci@642
|
84 |
node_item* _first_node;
|
marci@642
|
85 |
node_item* _last_node;
|
marci@642
|
86 |
|
marci@642
|
87 |
struct node_item {
|
marci@642
|
88 |
int id;
|
marci@642
|
89 |
edge_item* _first_out_edge;
|
marci@642
|
90 |
edge_item* _last_out_edge;
|
marci@642
|
91 |
edge_item* _first_in_edge;
|
marci@642
|
92 |
edge_item* _last_in_edge;
|
marci@642
|
93 |
node_item* _next_node;
|
marci@642
|
94 |
node_item* _prev_node;
|
marci@642
|
95 |
};
|
marci@642
|
96 |
|
marci@642
|
97 |
struct edge_item {
|
marci@642
|
98 |
int id;
|
marci@642
|
99 |
node_item* _tail;
|
marci@642
|
100 |
node_item* _head;
|
marci@642
|
101 |
edge_item* _next_out;
|
marci@642
|
102 |
edge_item* _prev_out;
|
marci@642
|
103 |
edge_item* _next_in;
|
marci@642
|
104 |
edge_item* _prev_in;
|
marci@642
|
105 |
};
|
marci@642
|
106 |
|
marci@642
|
107 |
node_item* _add_node() {
|
marci@642
|
108 |
node_item* p=new node_item;
|
marci@642
|
109 |
p->id=node_id++;
|
marci@642
|
110 |
p->_first_out_edge=0;
|
marci@642
|
111 |
p->_last_out_edge=0;
|
marci@642
|
112 |
p->_first_in_edge=0;
|
marci@642
|
113 |
p->_last_in_edge=0;
|
marci@642
|
114 |
p->_prev_node=_last_node;
|
marci@642
|
115 |
p->_next_node=0;
|
marci@642
|
116 |
if (_last_node) _last_node->_next_node=p;
|
marci@642
|
117 |
_last_node=p;
|
marci@642
|
118 |
if (!_first_node) _first_node=p;
|
marci@642
|
119 |
|
marci@642
|
120 |
++_node_num;
|
marci@642
|
121 |
return p;
|
marci@642
|
122 |
}
|
marci@642
|
123 |
|
marci@642
|
124 |
edge_item* _add_edge(node_item* _tail, node_item* _head) {
|
marci@642
|
125 |
edge_item* e=new edge_item;
|
marci@642
|
126 |
e->id=edge_id++;
|
marci@642
|
127 |
e->_tail=_tail;
|
marci@642
|
128 |
e->_head=_head;
|
marci@642
|
129 |
|
marci@642
|
130 |
e->_prev_out=_tail->_last_out_edge;
|
marci@642
|
131 |
if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
|
marci@642
|
132 |
_tail->_last_out_edge=e;
|
marci@642
|
133 |
if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
|
marci@642
|
134 |
e->_next_out=0;
|
marci@642
|
135 |
|
marci@642
|
136 |
e->_prev_in=_head->_last_in_edge;
|
marci@642
|
137 |
if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
|
marci@642
|
138 |
_head->_last_in_edge=e;
|
marci@642
|
139 |
if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
|
marci@642
|
140 |
e->_next_in=0;
|
marci@642
|
141 |
|
marci@642
|
142 |
++_edge_num;
|
marci@642
|
143 |
return e;
|
marci@642
|
144 |
}
|
marci@642
|
145 |
|
marci@642
|
146 |
//deletes a node which has no out edge and no in edge
|
marci@642
|
147 |
void _delete_node(node_item* v) {
|
marci@642
|
148 |
if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else
|
marci@642
|
149 |
_last_node=v->_prev_node;
|
marci@642
|
150 |
if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else
|
marci@642
|
151 |
_first_node=v->_next_node;
|
marci@642
|
152 |
|
marci@642
|
153 |
delete v;
|
marci@642
|
154 |
--_node_num;
|
marci@642
|
155 |
}
|
marci@642
|
156 |
|
marci@642
|
157 |
void _delete_edge(edge_item* e) {
|
marci@642
|
158 |
if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
|
marci@642
|
159 |
(e->_tail)->_last_out_edge=e->_prev_out;
|
marci@642
|
160 |
if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
|
marci@642
|
161 |
(e->_tail)->_first_out_edge=e->_next_out;
|
marci@642
|
162 |
if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
|
marci@642
|
163 |
(e->_head)->_last_in_edge=e->_prev_in;
|
marci@642
|
164 |
if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
|
marci@642
|
165 |
(e->_head)->_first_in_edge=e->_next_in;
|
marci@642
|
166 |
|
marci@642
|
167 |
delete e;
|
marci@642
|
168 |
--_edge_num;
|
marci@642
|
169 |
}
|
marci@642
|
170 |
|
marci@642
|
171 |
void _set_tail(edge_item* e, node_item* _tail) {
|
marci@642
|
172 |
if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
|
marci@642
|
173 |
(e->_tail)->_last_out_edge=e->_prev_out;
|
marci@642
|
174 |
if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
|
marci@642
|
175 |
(e->_tail)->_first_out_edge=e->_next_out;
|
marci@642
|
176 |
|
marci@642
|
177 |
e->_tail=_tail;
|
marci@642
|
178 |
|
marci@642
|
179 |
e->_prev_out=_tail->_last_out_edge;
|
marci@642
|
180 |
if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
|
marci@642
|
181 |
_tail->_last_out_edge=e;
|
marci@642
|
182 |
if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
|
marci@642
|
183 |
e->_next_out=0;
|
marci@642
|
184 |
}
|
marci@642
|
185 |
|
marci@642
|
186 |
void _set_head(edge_item* e, node_item* _head) {
|
marci@642
|
187 |
if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
|
marci@642
|
188 |
(e->_head)->_last_in_edge=e->_prev_in;
|
marci@642
|
189 |
if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
|
marci@642
|
190 |
(e->_head)->_first_in_edge=e->_next_in;
|
marci@642
|
191 |
|
marci@642
|
192 |
e->_head=_head;
|
marci@642
|
193 |
|
marci@642
|
194 |
e->_prev_in=_head->_last_in_edge;
|
marci@642
|
195 |
if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
|
marci@642
|
196 |
_head->_last_in_edge=e;
|
marci@642
|
197 |
if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
|
marci@642
|
198 |
e->_next_in=0;
|
marci@642
|
199 |
}
|
marci@642
|
200 |
|
marci@642
|
201 |
public:
|
marci@642
|
202 |
|
marci@642
|
203 |
/* default constructor */
|
marci@642
|
204 |
|
marci@642
|
205 |
SageGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { }
|
marci@642
|
206 |
|
marci@642
|
207 |
~SageGraph() {
|
marci@642
|
208 |
NodeIt n;
|
marci@642
|
209 |
while (this->valid(first(n))) erase(n);
|
marci@642
|
210 |
//while (first<NodeIt>().valid()) erase(first<NodeIt>());
|
marci@642
|
211 |
}
|
marci@642
|
212 |
|
marci@642
|
213 |
int nodeNum() const { return _node_num; }
|
marci@642
|
214 |
int edgeNum() const { return _edge_num; }
|
marci@642
|
215 |
|
marci@642
|
216 |
/* functions to construct iterators from the graph, or from each other */
|
marci@642
|
217 |
|
marci@642
|
218 |
//NodeIt firstNode() const { return NodeIt(*this); }
|
marci@642
|
219 |
//EdgeIt firstEdge() const { return EdgeIt(*this); }
|
marci@642
|
220 |
|
marci@642
|
221 |
//OutEdgeIt firstOutEdge(const Node v) const { return OutEdgeIt(v); }
|
marci@642
|
222 |
//InEdgeIt firstInEdge(const Node v) const { return InEdgeIt(v); }
|
marci@642
|
223 |
//SymEdgeIt firstSymEdge(const Node v) const { return SymEdgeIt(v); }
|
marci@642
|
224 |
Node tail(Edge e) const { return e.tailNode(); }
|
marci@642
|
225 |
Node head(Edge e) const { return e.headNode(); }
|
marci@642
|
226 |
|
marci@642
|
227 |
Node aNode(const OutEdgeIt& e) const { return e.aNode(); }
|
marci@642
|
228 |
Node aNode(const InEdgeIt& e) const { return e.aNode(); }
|
marci@642
|
229 |
Node aNode(const SymEdgeIt& e) const { return e.aNode(); }
|
marci@642
|
230 |
|
marci@642
|
231 |
Node bNode(const OutEdgeIt& e) const { return e.bNode(); }
|
marci@642
|
232 |
Node bNode(const InEdgeIt& e) const { return e.bNode(); }
|
marci@642
|
233 |
Node bNode(const SymEdgeIt& e) const { return e.bNode(); }
|
marci@642
|
234 |
|
marci@642
|
235 |
//Node invalid_node() { return Node(); }
|
marci@642
|
236 |
//Edge invalid_edge() { return Edge(); }
|
marci@642
|
237 |
//OutEdgeIt invalid_out_edge() { return OutEdgeIt(); }
|
marci@642
|
238 |
//InEdgeIt invalid_in_edge() { return InEdgeIt(); }
|
marci@642
|
239 |
//SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); }
|
marci@642
|
240 |
|
marci@642
|
241 |
/* same methods in other style */
|
marci@642
|
242 |
/* for experimental purpose */
|
marci@642
|
243 |
|
marci@642
|
244 |
NodeIt& first(NodeIt& v) const {
|
marci@642
|
245 |
v=NodeIt(*this); return v; }
|
marci@642
|
246 |
EdgeIt& first(EdgeIt& e) const {
|
marci@642
|
247 |
e=EdgeIt(*this); return e; }
|
marci@642
|
248 |
OutEdgeIt& first(OutEdgeIt& e, Node v) const {
|
marci@642
|
249 |
e=OutEdgeIt(*this, v); return e; }
|
marci@642
|
250 |
InEdgeIt& first(InEdgeIt& e, Node v) const {
|
marci@642
|
251 |
e=InEdgeIt(*this, v); return e; }
|
marci@642
|
252 |
SymEdgeIt& first(SymEdgeIt& e, Node v) const {
|
marci@642
|
253 |
e=SymEdgeIt(*this, v); return e; }
|
marci@642
|
254 |
//void getTail(Node& n, const Edge& e) const { n=tail(e); }
|
marci@642
|
255 |
//void getHead(Node& n, const Edge& e) const { n=head(e); }
|
marci@642
|
256 |
|
marci@642
|
257 |
//void getANode(Node& n, const OutEdgeIt& e) const { n=e.aNode(); }
|
marci@642
|
258 |
//void getANode(Node& n, const InEdgeIt& e) const { n=e.aNode(); }
|
marci@642
|
259 |
//void getANode(Node& n, const SymEdgeIt& e) const { n=e.aNode(); }
|
marci@642
|
260 |
//void getBNode(Node& n, const OutEdgeIt& e) const { n=e.bNode(); }
|
marci@642
|
261 |
//void getBNode(Node& n, const InEdgeIt& e) const { n=e.bNode(); }
|
marci@642
|
262 |
//void getBNode(Node& n, const SymEdgeIt& e) const { n=e.bNode(); }
|
marci@642
|
263 |
//void get_invalid(Node& n) { n=Node(); }
|
marci@642
|
264 |
//void get_invalid(Edge& e) { e=Edge(); }
|
marci@642
|
265 |
//void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); }
|
marci@642
|
266 |
//void get_invalid(InEdgeIt& e) { e=InEdgeIt(); }
|
marci@642
|
267 |
//void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); }
|
marci@642
|
268 |
|
marci@642
|
269 |
// template< typename It >
|
marci@642
|
270 |
// It first() const {
|
marci@642
|
271 |
// It e;
|
marci@642
|
272 |
// first(e);
|
marci@642
|
273 |
// return e;
|
marci@642
|
274 |
// }
|
marci@642
|
275 |
|
marci@642
|
276 |
// template< typename It >
|
marci@642
|
277 |
// It first(Node v) const {
|
marci@642
|
278 |
// It e;
|
marci@642
|
279 |
// first(e, v);
|
marci@642
|
280 |
// return e;
|
marci@642
|
281 |
// }
|
marci@642
|
282 |
|
marci@642
|
283 |
bool valid(Node n) const { return n.valid(); }
|
marci@642
|
284 |
bool valid(Edge e) const { return e.valid(); }
|
marci@642
|
285 |
|
marci@642
|
286 |
// template <typename It> It getNext(It it) const {
|
marci@642
|
287 |
// It tmp(it); next(tmp); return tmp; }
|
marci@642
|
288 |
// NodeIt& next(NodeIt& it) const { return ++it; }
|
marci@642
|
289 |
// EdgeIt& next(EdgeIt& it) const { return ++it; }
|
marci@642
|
290 |
// OutEdgeIt& next(OutEdgeIt& it) const { return ++it; }
|
marci@642
|
291 |
// InEdgeIt& next(InEdgeIt& it) const { return ++it; }
|
marci@642
|
292 |
// SymEdgeIt& next(SymEdgeIt& it) const { return ++it; }
|
marci@642
|
293 |
// template <typename It> It& next(It& it) const { return ++it; }
|
marci@642
|
294 |
template <typename It> It& next(It& it) const { ++it; return it; }
|
marci@642
|
295 |
|
marci@642
|
296 |
|
marci@642
|
297 |
/* for getting id's of graph objects */
|
marci@642
|
298 |
/* these are important for the implementation of property vectors */
|
marci@642
|
299 |
|
marci@642
|
300 |
int id(Node v) const { return v.node->id; }
|
marci@642
|
301 |
int id(Edge e) const { return e.edge->id; }
|
marci@642
|
302 |
|
marci@642
|
303 |
/* adding nodes and edges */
|
marci@642
|
304 |
|
marci@642
|
305 |
Node addNode() { return Node(_add_node()); }
|
marci@642
|
306 |
Edge addEdge(Node u, Node v) {
|
marci@642
|
307 |
return Edge(_add_edge(u.node, v.node));
|
marci@642
|
308 |
}
|
marci@642
|
309 |
|
marci@642
|
310 |
void erase(Node i) {
|
marci@642
|
311 |
{
|
marci@642
|
312 |
OutEdgeIt e;
|
marci@642
|
313 |
while (this->valid(first(e, i))) erase(e);
|
marci@642
|
314 |
}
|
marci@642
|
315 |
{
|
marci@642
|
316 |
InEdgeIt e;
|
marci@642
|
317 |
while (this->valid(first(e, i))) erase(e);
|
marci@642
|
318 |
}
|
marci@642
|
319 |
//while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i));
|
marci@642
|
320 |
//while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i));
|
marci@642
|
321 |
_delete_node(i.node);
|
marci@642
|
322 |
}
|
marci@642
|
323 |
|
marci@642
|
324 |
void erase(Edge e) { _delete_edge(e.edge); }
|
marci@642
|
325 |
|
marci@642
|
326 |
void clear() {
|
marci@642
|
327 |
NodeIt e;
|
marci@642
|
328 |
while (this->valid(first(e))) erase(e);
|
marci@642
|
329 |
//while (first<NodeIt>().valid()) erase(first<NodeIt>());
|
marci@642
|
330 |
}
|
marci@642
|
331 |
|
marci@642
|
332 |
void setTail(Edge e, Node tail) {
|
marci@642
|
333 |
_set_tail(e.edge, tail.node);
|
marci@642
|
334 |
}
|
marci@642
|
335 |
|
marci@642
|
336 |
void setHead(Edge e, Node head) {
|
marci@642
|
337 |
_set_head(e.edge, head.node);
|
marci@642
|
338 |
}
|
marci@642
|
339 |
|
marci@642
|
340 |
/* stream operations, for testing purpose */
|
marci@642
|
341 |
|
marci@642
|
342 |
// friend std::ostream& operator<<(std::ostream& os, const Node& i) {
|
marci@642
|
343 |
// if (i.valid())
|
marci@642
|
344 |
// os << i.node->id;
|
marci@642
|
345 |
// else
|
marci@642
|
346 |
// os << "invalid";
|
marci@642
|
347 |
// return os;
|
marci@642
|
348 |
// }
|
marci@642
|
349 |
// friend std::ostream& operator<<(std::ostream& os, const Edge& i) {
|
marci@642
|
350 |
// if (i.valid())
|
marci@642
|
351 |
// os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")";
|
marci@642
|
352 |
// else
|
marci@642
|
353 |
// os << "invalid";
|
marci@642
|
354 |
// return os;
|
marci@642
|
355 |
// }
|
marci@642
|
356 |
|
marci@642
|
357 |
class Node {
|
marci@642
|
358 |
friend class SageGraph;
|
marci@642
|
359 |
template <typename T> friend class NodeMap;
|
marci@642
|
360 |
|
marci@642
|
361 |
friend class Edge;
|
marci@642
|
362 |
friend class OutEdgeIt;
|
marci@642
|
363 |
friend class InEdgeIt;
|
marci@642
|
364 |
friend class SymEdgeIt;
|
marci@642
|
365 |
//public: //FIXME: It is required by op= of NodeIt
|
marci@642
|
366 |
protected:
|
marci@642
|
367 |
node_item* node;
|
marci@642
|
368 |
protected:
|
marci@642
|
369 |
friend int SageGraph::id(Node v) const;
|
marci@642
|
370 |
public:
|
marci@642
|
371 |
Node() /*: node(0)*/ { }
|
marci@642
|
372 |
Node(const Invalid&) : node(0) { }
|
marci@642
|
373 |
protected:
|
marci@642
|
374 |
Node(node_item* _node) : node(_node) { }
|
marci@642
|
375 |
bool valid() const { return (node); }
|
marci@642
|
376 |
public:
|
marci@642
|
377 |
//void makeInvalid() { node=0; }
|
marci@642
|
378 |
friend bool operator==(Node u, Node v) { return v.node==u.node; }
|
marci@642
|
379 |
friend bool operator!=(Node u, Node v) { return v.node!=u.node; }
|
marci@642
|
380 |
friend std::ostream& operator<<(std::ostream& os, const Node& i);
|
marci@642
|
381 |
};
|
marci@642
|
382 |
|
marci@642
|
383 |
class NodeIt : public Node {
|
marci@642
|
384 |
friend class SageGraph;
|
marci@642
|
385 |
//protected:
|
marci@642
|
386 |
public: //for everybody but marci
|
marci@642
|
387 |
NodeIt(const SageGraph& G) : Node(G._first_node) { }
|
alpar@774
|
388 |
NodeIt(const SageGraph& G, const Node& n) : Node(n) { }
|
marci@642
|
389 |
public:
|
marci@642
|
390 |
NodeIt() : Node() { }
|
marci@642
|
391 |
NodeIt(const Invalid& i) : Node(i) { }
|
marci@642
|
392 |
protected:
|
marci@642
|
393 |
NodeIt(node_item* v) : Node(v) { }
|
alpar@774
|
394 |
public:
|
marci@642
|
395 |
NodeIt& operator++() { node=node->_next_node; return *this; }
|
marci@642
|
396 |
//FIXME::
|
marci@642
|
397 |
// NodeIt& operator=(const Node& e)
|
marci@642
|
398 |
// { node=e.node; return *this; }
|
marci@642
|
399 |
};
|
marci@642
|
400 |
|
marci@642
|
401 |
class Edge {
|
marci@642
|
402 |
friend class SageGraph;
|
marci@642
|
403 |
template <typename T> friend class EdgeMap;
|
marci@642
|
404 |
|
marci@642
|
405 |
friend class Node;
|
marci@642
|
406 |
friend class NodeIt;
|
marci@642
|
407 |
protected:
|
marci@642
|
408 |
edge_item* edge;
|
marci@642
|
409 |
friend int SageGraph::id(Edge e) const;
|
marci@642
|
410 |
public:
|
marci@642
|
411 |
Edge() /*: edge(0)*/ { }
|
marci@642
|
412 |
Edge(const Invalid&) : edge(0) { }
|
marci@642
|
413 |
//Edge() { }
|
marci@642
|
414 |
protected:
|
marci@642
|
415 |
Edge(edge_item* _edge) : edge(_edge) { }
|
marci@642
|
416 |
bool valid() const { return (edge); }
|
marci@642
|
417 |
public:
|
marci@642
|
418 |
//void makeInvalid() { edge=0; }
|
marci@642
|
419 |
friend bool operator==(Edge u, Edge v) { return v.edge==u.edge; }
|
marci@642
|
420 |
friend bool operator!=(Edge u, Edge v) { return v.edge!=u.edge; }
|
marci@642
|
421 |
protected:
|
marci@642
|
422 |
Node tailNode() const { return Node(edge->_tail); }
|
marci@642
|
423 |
Node headNode() const { return Node(edge->_head); }
|
marci@642
|
424 |
public:
|
marci@642
|
425 |
friend std::ostream& operator<<(std::ostream& os, const Edge& i);
|
marci@642
|
426 |
};
|
marci@642
|
427 |
|
marci@642
|
428 |
class EdgeIt : public Edge {
|
marci@642
|
429 |
friend class SageGraph;
|
alpar@774
|
430 |
public:
|
alpar@774
|
431 |
EdgeIt() : Edge() { }
|
alpar@774
|
432 |
EdgeIt(const Invalid& i) : Edge(i) { }
|
marci@642
|
433 |
EdgeIt(const SageGraph& G) {
|
marci@642
|
434 |
node_item* v=G._first_node;
|
marci@642
|
435 |
if (v) edge=v->_first_out_edge; else edge=0;
|
marci@642
|
436 |
while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
|
marci@642
|
437 |
}
|
alpar@774
|
438 |
EdgeIt(const SageGraph& G, const Edge& e) : Edge(e) { }
|
alpar@774
|
439 |
// protected:
|
alpar@774
|
440 |
// EdgeIt(edge_item* _e) : Edge(_e) { }
|
marci@642
|
441 |
public:
|
marci@642
|
442 |
EdgeIt& operator++() {
|
marci@642
|
443 |
node_item* v=edge->_tail;
|
marci@642
|
444 |
edge=edge->_next_out;
|
marci@642
|
445 |
while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
|
marci@642
|
446 |
return *this;
|
marci@642
|
447 |
}
|
marci@642
|
448 |
};
|
marci@642
|
449 |
|
marci@642
|
450 |
class OutEdgeIt : public Edge {
|
marci@642
|
451 |
friend class SageGraph;
|
marci@642
|
452 |
public:
|
alpar@774
|
453 |
OutEdgeIt() : Edge() { }
|
marci@642
|
454 |
OutEdgeIt(const Invalid& i) : Edge(i) { }
|
alpar@774
|
455 |
OutEdgeIt(const SageGraph&, Node _v) : Edge(_v.node->_first_out_edge) { }
|
alpar@774
|
456 |
OutEdgeIt(const SageGraph&, const Edge& e) : Edge(e) { }
|
marci@642
|
457 |
OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
|
marci@642
|
458 |
protected:
|
marci@642
|
459 |
Node aNode() const { return Node(edge->_tail); }
|
marci@642
|
460 |
Node bNode() const { return Node(edge->_head); }
|
marci@642
|
461 |
};
|
marci@642
|
462 |
|
marci@642
|
463 |
class InEdgeIt : public Edge {
|
marci@642
|
464 |
friend class SageGraph;
|
marci@642
|
465 |
public:
|
alpar@774
|
466 |
InEdgeIt() : Edge() { }
|
alpar@774
|
467 |
InEdgeIt(Invalid i) : Edge(i) { }
|
alpar@774
|
468 |
InEdgeIt(const SageGraph&, Node _v) : Edge(_v.node->_first_in_edge) { }
|
alpar@774
|
469 |
InEdgeIt(const SageGraph&, const Edge& e) : Edge(e) { }
|
marci@642
|
470 |
InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
|
marci@642
|
471 |
protected:
|
marci@642
|
472 |
Node aNode() const { return Node(edge->_head); }
|
marci@642
|
473 |
Node bNode() const { return Node(edge->_tail); }
|
marci@642
|
474 |
};
|
marci@642
|
475 |
|
marci@642
|
476 |
class SymEdgeIt : public Edge {
|
marci@642
|
477 |
friend class SageGraph;
|
marci@642
|
478 |
bool out_or_in; //1 iff out, 0 iff in
|
marci@642
|
479 |
//node_item* v;
|
marci@642
|
480 |
//protected:
|
marci@642
|
481 |
protected: //for alpar
|
marci@642
|
482 |
SymEdgeIt(const Node& _v) /*: v(_v.node)*/ {
|
marci@642
|
483 |
out_or_in=1;
|
marci@642
|
484 |
edge=_v.node->_first_out_edge;
|
marci@642
|
485 |
if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
|
marci@642
|
486 |
}
|
marci@642
|
487 |
public:
|
marci@642
|
488 |
SymEdgeIt() : Edge() /*, v(0)*/ { }
|
marci@642
|
489 |
SymEdgeIt(const Invalid& i) : Edge(i) { }
|
marci@642
|
490 |
SymEdgeIt(const SageGraph&, Node _v) /*: v(_v.node)*/ {
|
marci@642
|
491 |
out_or_in=1;
|
marci@642
|
492 |
edge=_v.node->_first_out_edge;
|
marci@642
|
493 |
if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
|
marci@642
|
494 |
}
|
marci@642
|
495 |
protected:
|
marci@642
|
496 |
SymEdgeIt& operator++() {
|
marci@642
|
497 |
if (out_or_in) {
|
marci@642
|
498 |
node_item* v=edge->_tail;
|
marci@642
|
499 |
edge=edge->_next_out;
|
marci@642
|
500 |
if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
|
marci@642
|
501 |
} else {
|
marci@642
|
502 |
edge=edge->_next_in;
|
marci@642
|
503 |
}
|
marci@642
|
504 |
return *this;
|
marci@642
|
505 |
}
|
marci@642
|
506 |
protected:
|
marci@642
|
507 |
Node aNode() const {
|
marci@642
|
508 |
return (out_or_in) ? Node(edge->_tail) : Node(edge->_head); }
|
marci@642
|
509 |
Node bNode() const {
|
marci@642
|
510 |
return (out_or_in) ? Node(edge->_head) : Node(edge->_tail); }
|
marci@642
|
511 |
};
|
marci@642
|
512 |
};
|
marci@642
|
513 |
|
marci@642
|
514 |
inline
|
marci@642
|
515 |
std::ostream& operator<<(std::ostream& os, const SageGraph::Node& i) {
|
marci@642
|
516 |
if (i.valid())
|
marci@642
|
517 |
os << i.node->id;
|
marci@642
|
518 |
else
|
marci@642
|
519 |
os << "invalid";
|
marci@642
|
520 |
return os;
|
marci@642
|
521 |
}
|
marci@642
|
522 |
|
marci@642
|
523 |
inline
|
marci@642
|
524 |
std::ostream& operator<<(std::ostream& os, const SageGraph::Edge& i) {
|
marci@642
|
525 |
if (i.valid())
|
marci@642
|
526 |
os << "(" << i.tailNode() << "--" << i.edge->id << "->"
|
marci@642
|
527 |
<< i.headNode() << ")";
|
marci@642
|
528 |
else
|
marci@642
|
529 |
os << "invalid";
|
marci@642
|
530 |
return os;
|
marci@642
|
531 |
}
|
marci@642
|
532 |
|
marci@642
|
533 |
class UndirSageGraph : public SageGraph {
|
marci@642
|
534 |
public:
|
marci@642
|
535 |
typedef SymEdgeIt OutEdgeIt;
|
marci@642
|
536 |
typedef SymEdgeIt InEdgeIt;
|
marci@642
|
537 |
};
|
marci@642
|
538 |
|
marci@642
|
539 |
} //namespace hugo
|
marci@642
|
540 |
|
marci@642
|
541 |
#endif //HUGO_SAGE_GRAPH_H
|