Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
node.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file node.cpp
1 /* This software is distributed under the GNU Lesser General Public License */
2 //==========================================================================
3 //
4 // node.cpp
5 //
6 //==========================================================================
7 // $Id: node.cpp,v 1.18 2001/11/07 13:58:10 pick Exp $
8 
9 #include <GTL/node_data.h>
10 #include <GTL/edge_data.h>
11 #include <GTL/graph.h>
12 #include <GTL/bfs.h>
13 
14 #include <cassert>
15 #include <iostream>
16 
17 #ifdef __GTL_MSVCC
18 # ifdef _DEBUG
19 # ifndef SEARCH_MEMORY_LEAKS_ENABLED
20 # error SEARCH NOT ENABLED
21 # endif
22 # define new DEBUG_NEW
23 # undef THIS_FILE
24  static char THIS_FILE[] = __FILE__;
25 # endif // _DEBUG
26 #endif // __GTL_MSVCC
27 
29 
31  data(0)
32 {
33 }
34 
35 GTL_EXTERN ostream& operator<< (ostream& os, const node& n) {
36  if (n != node()) {
37  return os << "[" << n.id() << "]";
38  } else {
39  return os << "[ UNDEF ]";
40  }
41 }
42 
44 {
45  return node::adj_nodes_iterator(*this, true);
46 }
47 
49 {
50  return node::adj_nodes_iterator(*this, false);
51 }
52 
54 {
55  return node::adj_edges_iterator(*this, true);
56 }
57 
59 {
60  return node::adj_edges_iterator(*this, false);
61 }
62 
64 {
65  return node::inout_edges_iterator(*this, true);
66 }
67 
69 {
70  return node::inout_edges_iterator(*this, false);
71 }
72 
74 {
75  return data->edges[0].begin();
76 }
77 
79 {
80  return data->edges[0].end();
81 }
82 
84 {
85  return data->edges[1].begin();
86 }
87 
89 {
90  return data->edges[1].end();
91 }
92 
93 int node::degree() const
94 {
95  return outdeg() + indeg();
96 }
97 
98 int node::outdeg() const
99 {
100  return data->edges[1].size();
101 }
102 
103 int node::indeg() const
104 {
105  return data->edges[0].size();
106 }
107 
108 int node::id() const
109 {
110  return data->id;
111 }
112 
113 bool node::is_directed() const
114 {
115  return data->owner->is_directed();
116 }
117 
119 {
120  return data->owner->is_undirected();
121 }
122 
123 const node& node::opposite(edge e) const
124 {
125  // not implemented for hypergraphs
126  assert(e.data);
127 
128  node& s = *(e.data->nodes[0].begin());
129  if (*this == s)
130  return *(e.data->nodes[1].begin());
131  else
132  return s;
133 }
134 
135 list<node> node::opposites(edge) const
136 {
137  // not implemented yet
138  return list<node>(); // to avoid compiler warnings
139 }
140 
141 bool node::is_hidden () const
142 {
143  return data->hidden;
144 }
145 
147 {
148  bfs b;
149  b.start_node(*this);
150  b.calc_level(true);
151  b.run(*data->owner);
152 
153  node last_node = *(--b.end());
154 
155  return b.level(last_node);
156 }
157 
159 {
160  return v1.data == v2.data;
161 }
162 
164 {
165  return v1.data != v2.data;
166 }
167 
169 {
170  return v1.data < v2.data;
171 }
172 
173 //--------------------------------------------------------------------------
174 // adj_edges_iterator
175 //--------------------------------------------------------------------------
176 
177 node::adj_edges_iterator::adj_edges_iterator()
178 {
179 }
180 
181 node::adj_edges_iterator::adj_edges_iterator(node n, bool start)
182 {
183  // iterators that are used everytime
184  last_edge[0] = n.out_edges_end();
185  last_edge[1] = n.in_edges_end();
186  directed = n.is_directed();
187  if (!directed)
188  {
189  begin_edge[0] = n.out_edges_begin();
190  begin_edge[1] = n.in_edges_begin();
191  }
192 
193  // set at start or end
194  if (start)
195  {
196  inout = 0;
197  akt_edge[0] = n.out_edges_begin();
198  if (!directed)
199  {
200  akt_edge[1] = n.in_edges_begin();
201  if (akt_edge[0] == last_edge[0])
202  inout = 1;
203  }
204  }
205  else
206  {
207  inout = directed ? 0 : 1;
208  akt_edge[0] = n.out_edges_end();
209  if (!directed)
210  akt_edge[1] = n.in_edges_end();
211  }
212 }
213 
216 {
217  return i.akt_edge[i.inout] == akt_edge[inout];
218 }
219 
222 {
223  return i.akt_edge[i.inout] != akt_edge[inout];
224 }
225 
226 node::adj_edges_iterator& node::adj_edges_iterator::operator++()
227 {
228  if (directed)
229  ++akt_edge[inout];
230  else
231  {
232  if (inout == 0)
233  {
234  ++akt_edge[0];
235  if (akt_edge[0] == last_edge[0])
236  ++inout;
237  }
238  else // inout == 1
239  {
240  if (akt_edge[1] == last_edge[1])
241  {
242  inout = 0;
243  akt_edge[0] = begin_edge[0];
244  akt_edge[1] = begin_edge[1];
245  if (begin_edge[0] == last_edge[0])
246  inout = 1;
247  }
248  else
249  ++akt_edge[inout];
250  }
251  }
252  return *this;
253 }
254 
255 node::adj_edges_iterator node::adj_edges_iterator::operator++(int)
256 {
258  operator++();
259  return tmp;
260 }
261 
262 node::adj_edges_iterator& node::adj_edges_iterator::operator--()
263 {
264  if (!directed && inout == 1 && akt_edge[1] == begin_edge[1])
265  inout = 0;
266  --akt_edge[inout];
267  return *this;
268 }
269 
270 node::adj_edges_iterator node::adj_edges_iterator::operator--(int)
271 {
273  operator--();
274  return tmp;
275 }
276 
278 {
279  return *akt_edge[inout];
280 }
281 
282 const edge* node::adj_edges_iterator::operator->() const
283 {
284  return akt_edge[inout].operator->();
285 }
286 
287 //--------------------------------------------------------------------------
288 // inout_edges_iterator
289 //--------------------------------------------------------------------------
290 
291 node::inout_edges_iterator::inout_edges_iterator()
292 {
293 }
294 
295 node::inout_edges_iterator::inout_edges_iterator(node n, bool start)
296 {
297  // iterators that are used everytime
298  last_edge = n.in_edges_end();
299  begin_edge = n.out_edges_begin();
300 
301  // set at start or end
302  if (start)
303  {
304  inout = 0;
305  akt_edge[0] = n.in_edges_begin();
306  akt_edge[1] = n.out_edges_begin();
307  if (akt_edge[0] == last_edge)
308  inout = 1;
309  }
310  else
311  {
312  inout = 1;
313  akt_edge[0] = n.in_edges_end();
314  akt_edge[1] = n.out_edges_end();
315  }
316 }
317 
320 {
321  return i.akt_edge[i.inout] == akt_edge[inout];
322 }
323 
326 {
327  return i.akt_edge[i.inout] != akt_edge[inout];
328 }
329 
330 node::inout_edges_iterator& node::inout_edges_iterator::operator++()
331 {
332  ++akt_edge[inout];
333  if ((akt_edge[inout] == last_edge) && (inout==0))
334  ++inout;
335  return *this;
336 }
337 
338 node::inout_edges_iterator node::inout_edges_iterator::operator++(int)
339 {
341  operator++();
342  return tmp;
343 }
344 
345 node::inout_edges_iterator& node::inout_edges_iterator::operator--()
346 {
347  if (inout == 1 && (akt_edge[1] == begin_edge))
348  inout = 0;
349  --akt_edge[inout];
350  return *this;
351 }
352 
353 node::inout_edges_iterator node::inout_edges_iterator::operator--(int)
354 {
356  operator--();
357  return tmp;
358 }
359 
361 {
362  return *akt_edge[inout];
363 }
364 
365 const edge* node::inout_edges_iterator::operator->() const
366 {
367  return akt_edge[inout].operator->();
368 }
369 
370 //--------------------------------------------------------------------------
371 // adj_nodes_iterator
372 //--------------------------------------------------------------------------
373 
374 node::adj_nodes_iterator::adj_nodes_iterator()
375 {
376 }
377 
378 node::adj_nodes_iterator::adj_nodes_iterator(const node& n, bool start)
379 {
380  int_node = n;
381  if (start)
382  akt_edge = n.adj_edges_begin();
383  else
384  akt_edge = n.adj_edges_end();
385 }
386 
389 {
390  return i.akt_edge == akt_edge;
391 }
392 
395 {
396  return i.akt_edge != akt_edge;
397 }
398 
399 node::adj_nodes_iterator& node::adj_nodes_iterator::operator++()
400 {
401  ++akt_edge;
402  return *this;
403 }
404 
405 node::adj_nodes_iterator node::adj_nodes_iterator::operator++(int)
406 {
408  operator++();
409  return tmp;
410 }
411 
412 node::adj_nodes_iterator& node::adj_nodes_iterator::operator--()
413 {
414  --akt_edge;
415  return *this;
416 }
417 
418 node::adj_nodes_iterator node::adj_nodes_iterator::operator--(int)
419 {
421  operator--();
422  return tmp;
423 }
424 
426 {
427  return int_node.opposite(*akt_edge);
428 }
429 
430 const node* node::adj_nodes_iterator::operator->() const
431 {
432  return &(int_node.opposite(*akt_edge));
433 }
434 
436 
437 //--------------------------------------------------------------------------
438 // end of file
439 //--------------------------------------------------------------------------