Class: LemonGraph::Graph::Node

Inherits:
LemonGraph::GraphItem show all
Defined in:
ext/lemongraph/graph_node.cc,
ext/lemongraph/lemongraph.cc

Overview

Node of a LemonGraph::Graph.

Instance Attribute Summary

Attributes inherited from LemonGraph::GraphItem

#graph, #id

Instance Method Summary collapse

Methods inherited from LemonGraph::GraphItem

#!=, #<=>, #==

Instance Method Details

#contract_in(other, remove_new_loops = true) ⇒ Node

Contracts self into the given node. self is removed, but instead of deleting its incident edges, they are joined to node other. If remove_new_loops is true (this is the default), then the newly created loops are removed.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'ext/lemongraph/graph_node.cc', line 80

VALUE lemongraph_unnode_contract(int argc, VALUE *argv, VALUE self)
{
  if (argc < 1 || argc > 2)
    rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 1..2)", argc);
  bool remove_new_loops = true;
  VALUE other = argv[0];
  if (argc == 2)
    remove_new_loops = (argv[1] != Qnil && argv[1] != Qfalse);
  VALUE rbg = rb_iv_get(self, "@graph");
  lemon::ListGraph &g = lemongraph_graph_rb2ref(rbg);
  lemongraph_graph_raise_if_not_valid_node(g, rbg, self);
  lemongraph_graph_raise_if_not_valid_node(g, rbg, other);
  g.contract(lemongraph_rbunnode2unnode(other), lemongraph_rbunnode2unnode(self), remove_new_loops);
  return other;
}

#each_edge {|edge| ... } ⇒ self #each_edgeEnumerator

Calls the block, if given, once for each edges incident to the node. The edges are passed as parameters to the block.

Returns self, or, if no block is given, an Enumerator is returned.

Overloads:

  • #each_edge {|edge| ... } ⇒ self

    Yield Parameters:



143
144
145
146
147
148
149
150
151
152
# File 'ext/lemongraph/graph_node.cc', line 143

VALUE lemongraph_unnode_each_edge(VALUE self)
{
  RETURN_SIZED_ENUMERATOR(self, 0, 0, lemongraph_unnode_edgeenum_nbedges);
  VALUE rbg = rb_iv_get(self, "@graph");
  lemon::ListGraph& g = lemongraph_graph_rb2ref(rbg);
  lemon::ListGraph::Node n = lemongraph_rbunnode2unnode(self);
  for (lemon::ListGraph::IncEdgeIt e(g, n); e != lemon::INVALID; ++e)
    rb_yield(lemongraph_make_unedge(e, rbg));
  return self;
}

#each_inarc {|arc| ... } ⇒ self #each_inarcEnumerator

Calls the block, if given, once for each incoming Arc of the node. The arcs are passed as parameters to the block.

Returns self, or, if no block is given, an Enumerator is returned.

Overloads:

  • #each_inarc {|arc| ... } ⇒ self

    Yield Parameters:



201
202
203
204
205
206
207
208
209
210
# File 'ext/lemongraph/graph_node.cc', line 201

VALUE lemongraph_unnode_each_inarc(VALUE self)
{
  RETURN_SIZED_ENUMERATOR(self, 0, 0, lemongraph_unnode_inarcenum_nbinarcs);
  VALUE rbg = rb_iv_get(self, "@graph");
  lemon::ListGraph& g = lemongraph_graph_rb2ref(rbg);
  lemon::ListGraph::Node n = lemongraph_rbunnode2unnode(self);
  for (lemon::ListGraph::InArcIt a(g, n); a != lemon::INVALID; ++a)
    rb_yield(lemongraph_make_unarc(a, rbg));
  return self;
}

#each_outarc {|arc| ... } ⇒ self #each_outarcEnumerator

Calls the block, if given, once for each outgoing Arc from the node. The arcs are passed as parameters to the block.

Returns self, or, if no block is given, an Enumerator is returned.

Overloads:

  • #each_outarc {|arc| ... } ⇒ self

    Yield Parameters:



259
260
261
262
263
264
265
266
267
268
# File 'ext/lemongraph/graph_node.cc', line 259

VALUE lemongraph_unnode_each_outarc(VALUE self)
{
  RETURN_SIZED_ENUMERATOR(self, 0, 0, lemongraph_unnode_outarcenum_nboutarcs);
  VALUE rbg = rb_iv_get(self, "@graph");
  lemon::ListGraph& g = lemongraph_graph_rb2ref(rbg);
  lemon::ListGraph::Node n = lemongraph_rbunnode2unnode(self);
  for (lemon::ListGraph::OutArcIt a(g, n); a != lemon::INVALID; ++a)
    rb_yield(lemongraph_make_unarc(a, rbg));
  return self;
}

#edge_from(u) ⇒ Edge

Creates an Edge from node u to self.



286
287
288
289
290
# File 'ext/lemongraph/graph_node.cc', line 286

VALUE lemongraph_unnode_edge_from(VALUE self, VALUE u)
{
  VALUE rbg = rb_iv_get(self, "@graph");
  return lemongraph_graph_add_edge(rbg, u, self);
}

#edge_to(v) ⇒ Edge

Creates an Edge from self to node v.



275
276
277
278
279
# File 'ext/lemongraph/graph_node.cc', line 275

VALUE lemongraph_unnode_edge_to(VALUE self, VALUE v)
{
  VALUE rbg = rb_iv_get(self, "@graph");
  return lemongraph_graph_add_edge(rbg, self, v);
}

#edgesArray<Edge>

Returns an array containing all the edges incident to the node.



112
113
114
115
116
117
118
119
120
121
# File 'ext/lemongraph/graph_node.cc', line 112

VALUE lemongraph_unnode_edges(VALUE self)
{
  VALUE rbg = rb_iv_get(self, "@graph");
  lemon::ListGraph& g = lemongraph_graph_rb2ref(rbg);
  lemon::ListGraph::Node n = lemongraph_rbunnode2unnode(self);
  VALUE ar = rb_ary_new_capa(lemon::countIncEdges(g, n));
  for (lemon::ListGraph::IncEdgeIt e(g, n); e != lemon::INVALID; ++e)
    rb_ary_push(ar, lemongraph_make_unedge(e, rbg));
  return ar;
}

#eraseself

Erases the node from the graph it belongs to.



65
66
67
68
69
# File 'ext/lemongraph/graph_node.cc', line 65

VALUE lemongraph_unnode_erase(VALUE self)
{
  lemongraph_graph_rb2ref(rb_iv_get(self, "@graph")).erase(lemongraph_rbunnode2unnode(self));
  return self;
}

#inarcsArray<Arc>

Returns an array containing all the incoming arcs to the node.



170
171
172
173
174
175
176
177
178
179
# File 'ext/lemongraph/graph_node.cc', line 170

VALUE lemongraph_unnode_inarcs(VALUE self)
{
  VALUE rbg = rb_iv_get(self, "@graph");
  lemon::ListGraph& g = lemongraph_graph_rb2ref(rbg);
  lemon::ListGraph::Node n = lemongraph_rbunnode2unnode(self);
  VALUE ar = rb_ary_new_capa(lemon::countInArcs(g, n));
  for (lemon::ListGraph::InArcIt a(g, n); a != lemon::INVALID; ++a)
    rb_ary_push(ar, lemongraph_make_unarc(a, rbg));
  return ar;
}

#nb_edgesInteger

Number of edges incident to the node.



100
101
102
103
104
105
106
# File 'ext/lemongraph/graph_node.cc', line 100

VALUE lemongraph_unnode_nb_edges(VALUE self)
{
  VALUE rbg = rb_iv_get(self, "@graph");
  lemon::ListGraph& g = lemongraph_graph_rb2ref(rbg);
  lemon::ListGraph::Node n = lemongraph_rbunnode2unnode(self);
  return INT2NUM(lemon::countIncEdges(g, n));
}

#nb_inarcsInteger

Number of incoming arcs to the node.



158
159
160
161
162
163
164
# File 'ext/lemongraph/graph_node.cc', line 158

VALUE lemongraph_unnode_nb_inarcs(VALUE self)
{
  VALUE rbg = rb_iv_get(self, "@graph");
  lemon::ListGraph& g = lemongraph_graph_rb2ref(rbg);
  lemon::ListGraph::Node n = lemongraph_rbunnode2unnode(self);
  return INT2NUM(lemon::countInArcs(g, n));
}

#nb_outarcsInteger

Number of outgoing arcs from the node.



216
217
218
219
220
221
222
# File 'ext/lemongraph/graph_node.cc', line 216

VALUE lemongraph_unnode_nb_outarcs(VALUE self)
{
  VALUE rbg = rb_iv_get(self, "@graph");
  lemon::ListGraph& g = lemongraph_graph_rb2ref(rbg);
  lemon::ListGraph::Node n = lemongraph_rbunnode2unnode(self);
  return INT2NUM(lemon::countOutArcs(g, n));
}

#opposite(edge) ⇒ Node

Returns the node opposite to self on edge. Returns nil if edge is not incident to self.



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'ext/lemongraph/graph_node.cc', line 298

VALUE lemongraph_unnode_opposite(VALUE self, VALUE edge)
{
  VALUE r;
  VALUE rbg = rb_iv_get(self, "@graph");
  lemon::ListGraph& g = lemongraph_graph_rb2ref(rbg);
  VALUE klass = rb_class_of(edge);
  if (klass == c_UnEdge) {
    if (rb_iv_get(edge, "@graph") != rbg)
      rb_raise(rb_eRuntimeError, "node and edge are not from the same graph");
    r = lemongraph_make_unnode(g.oppositeNode(lemongraph_rbunnode2unnode(self), lemongraph_rbunedge2unedge(edge)), rbg);
  }
  else if (klass == c_UnArc) {
    if (rb_iv_get(edge, "@graph") != rbg)
      rb_raise(rb_eRuntimeError, "node and arc are not from the same graph");
    r = lemongraph_make_unnode(g.oppositeNode(lemongraph_rbunnode2unnode(self), lemongraph_rbunarc2unarc(edge)), rbg);
  }
  else {
    rb_raise(rb_eTypeError, "expecting a %" PRIsVALUE " or a %" PRIsVALUE ", not a %s",
        rb_class_name(c_UnEdge), rb_class_name(c_UnArc), rb_obj_classname(edge));
  }
  return r;
}

#outarcsArray<Arc>

Returns an array containing all the outgoing arcs from the node.



228
229
230
231
232
233
234
235
236
237
# File 'ext/lemongraph/graph_node.cc', line 228

VALUE lemongraph_unnode_outarcs(VALUE self)
{
  VALUE rbg = rb_iv_get(self, "@graph");
  lemon::ListGraph& g = lemongraph_graph_rb2ref(rbg);
  lemon::ListGraph::Node n = lemongraph_rbunnode2unnode(self);
  VALUE ar = rb_ary_new_capa(lemon::countOutArcs(g, n));
  for (lemon::ListGraph::OutArcIt a(g, n); a != lemon::INVALID; ++a)
    rb_ary_push(ar, lemongraph_make_unarc(a, rbg));
  return ar;
}

#valid?Boolean

Test if the node is valid, i.e. it is a real node of the graph it belongs to.



56
57
58
59
# File 'ext/lemongraph/graph_node.cc', line 56

VALUE lemongraph_unnode_is_valid(VALUE self)
{
  return lemongraph_graph_rb2ref(rb_iv_get(self, "@graph")).valid(lemongraph_rbunnode2unnode(self)) ? Qtrue : Qfalse;
}