Class: LemonGraph::Digraph::Node

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

Overview

Node of a LemonGraph::Digraph.

Instance Attribute Summary

Attributes inherited from GraphItem

#graph, #id

Instance Method Summary collapse

Methods inherited from GraphItem

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

Instance Method Details

#arc_from(source) ⇒ Arc

Creates an Arc from source source to target self.

Parameters:

  • source (Node)

    source node of the arc

Returns:



252
253
254
255
256
# File 'ext/lemongraph/digraph_node.cc', line 252

VALUE lemongraph_dinode_arc_from(VALUE self, VALUE source)
{
  VALUE rbg = rb_iv_get(self, "@graph");
  return lemongraph_digraph_add_arc(rbg, source, self);
}

#arc_to(target) ⇒ Arc

Creates an Arc from source self to target target.

Parameters:

  • target (Node)

    target node of the arc

Returns:



241
242
243
244
245
# File 'ext/lemongraph/digraph_node.cc', line 241

VALUE lemongraph_dinode_arc_to(VALUE self, VALUE target)
{
  VALUE rbg = rb_iv_get(self, "@graph");
  return lemongraph_digraph_add_arc(rbg, self, target);
}

#contract_in(other, remove_new_loops = true) ⇒ Node

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

Parameters:

  • other (Node)

    the node in which to contract self

  • remove_new_loops (Boolean) (defaults to: true)

    whether or not to remove newly created loops

Returns:

  • (Node)

    other is returned, self is erased



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'ext/lemongraph/digraph_node.cc', line 103

VALUE lemongraph_dinode_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::ListDigraph &g = lemongraph_digraph_rb2ref(rbg);
  lemongraph_digraph_raise_if_not_valid_node(g, rbg, self);
  lemongraph_digraph_raise_if_not_valid_node(g, rbg, other);
  g.contract(lemongraph_rbdinode2dinode(other), lemongraph_rbdinode2dinode(self), remove_new_loops);
  return other;
}

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

Calls the block, if given, once for each incoming Arc to 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:

    Returns:

    • (self)
  • #each_inarcEnumerator

    Returns:

    • (Enumerator)

Returns:

  • (self, Enumerator)


166
167
168
169
170
171
172
173
174
175
# File 'ext/lemongraph/digraph_node.cc', line 166

VALUE lemongraph_dinode_each_inarc(VALUE self)
{
  RETURN_SIZED_ENUMERATOR(self, 0, 0, lemongraph_dinode_inarcenum_nbinarcs);
  VALUE rbg = rb_iv_get(self, "@graph");
  lemon::ListDigraph& g = lemongraph_digraph_rb2ref(rbg);
  lemon::ListDigraph::Node n = lemongraph_rbdinode2dinode(self);
  for (lemon::ListDigraph::InArcIt a(g, n); a != lemon::INVALID; ++a)
    rb_yield(lemongraph_make_diarc(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:

    Returns:

    • (self)
  • #each_outarcEnumerator

    Returns:

    • (Enumerator)

Returns:

  • (self, Enumerator)


225
226
227
228
229
230
231
232
233
234
# File 'ext/lemongraph/digraph_node.cc', line 225

VALUE lemongraph_dinode_each_outarc(VALUE self)
{
  RETURN_SIZED_ENUMERATOR(self, 0, 0, lemongraph_dinode_outarcenum_nboutarcs);
  VALUE rbg = rb_iv_get(self, "@graph");
  lemon::ListDigraph& g = lemongraph_digraph_rb2ref(rbg);
  lemon::ListDigraph::Node n = lemongraph_rbdinode2dinode(self);
  for (lemon::ListDigraph::OutArcIt a(g, n); a != lemon::INVALID; ++a)
    rb_yield(lemongraph_make_diarc(a, rbg));
  return self;
}

#eraseself

Erases the node from the graph it belongs to.

Returns:

  • (self)


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

VALUE lemongraph_dinode_erase(VALUE self)
{
  lemongraph_digraph_rb2ref(rb_iv_get(self, "@graph")).erase(lemongraph_rbdinode2dinode(self));
  return self;
}

#inarcsArray<Arc>

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

Returns:



135
136
137
138
139
140
141
142
143
144
# File 'ext/lemongraph/digraph_node.cc', line 135

VALUE lemongraph_dinode_inarcs(VALUE self)
{
  VALUE rbg = rb_iv_get(self, "@graph");
  lemon::ListDigraph& g = lemongraph_digraph_rb2ref(rbg);
  lemon::ListDigraph::Node n = lemongraph_rbdinode2dinode(self);
  VALUE ar = rb_ary_new_capa(lemon::countInArcs(g, n));
  for (lemon::ListDigraph::InArcIt a(g, n); a != lemon::INVALID; ++a)
    rb_ary_push(ar, lemongraph_make_diarc(a, rbg));
  return ar;
}

#nb_inarcsInteger

Number of incoming arcs to the node.

Returns:

  • (Integer)


123
124
125
126
127
128
129
# File 'ext/lemongraph/digraph_node.cc', line 123

VALUE lemongraph_dinode_nb_inarcs(VALUE self)
{
  VALUE rbg = rb_iv_get(self, "@graph");
  lemon::ListDigraph& g = lemongraph_digraph_rb2ref(rbg);
  lemon::ListDigraph::Node n = lemongraph_rbdinode2dinode(self);
  return INT2NUM(lemon::countInArcs(g, n));
}

#nb_outarcsInteger

Number of outgoing arcs from the node.

Returns:

  • (Integer)


182
183
184
185
186
187
188
# File 'ext/lemongraph/digraph_node.cc', line 182

VALUE lemongraph_dinode_nb_outarcs(VALUE self)
{
  VALUE rbg = rb_iv_get(self, "@graph");
  lemon::ListDigraph& g = lemongraph_digraph_rb2ref(rbg);
  lemon::ListDigraph::Node n = lemongraph_rbdinode2dinode(self);
  return INT2NUM(lemon::countOutArcs(g, n));
}

#opposite(arc) ⇒ Node

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

Parameters:

Returns:



264
265
266
267
268
269
270
271
272
273
274
275
# File 'ext/lemongraph/digraph_node.cc', line 264

VALUE lemongraph_dinode_opposite(VALUE self, VALUE arc)
{
  VALUE rbg = rb_iv_get(self, "@graph");
  lemon::ListDigraph& g = lemongraph_digraph_rb2ref(rbg);
  if (rb_class_of(arc) != c_DiArc) {
    rb_raise(rb_eTypeError, "expecting a %" PRIsVALUE ", not a %s",
        rb_class_name(c_DiArc), rb_obj_classname(arc));
  }
  if (rb_iv_get(arc, "@graph") != rbg)
    rb_raise(rb_eRuntimeError, "node and arc are not from the same graph");
  return lemongraph_make_dinode(g.oppositeNode(lemongraph_rbdinode2dinode(self), lemongraph_rbdiarc2diarc(arc)), rbg);
}

#outarcsArray<Arc>

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

Returns:



194
195
196
197
198
199
200
201
202
203
# File 'ext/lemongraph/digraph_node.cc', line 194

VALUE lemongraph_dinode_outarcs(VALUE self)
{
  VALUE rbg = rb_iv_get(self, "@graph");
  lemon::ListDigraph& g = lemongraph_digraph_rb2ref(rbg);
  lemon::ListDigraph::Node n = lemongraph_rbdinode2dinode(self);
  VALUE ar = rb_ary_new_capa(lemon::countOutArcs(g, n));
  for (lemon::ListDigraph::OutArcIt a(g, n); a != lemon::INVALID; ++a)
    rb_ary_push(ar, lemongraph_make_diarc(a, rbg));
  return ar;
}

#split(connect = true) ⇒ Node

Splits the node. First, a new node is added to the digraph, then the source of each outgoing arc of self is moved to this new node. If the parameter connect is true (this is the default), then a new arc from self to the newly created node is also added.

Parameters:

  • connect (Boolean) (defaults to: true)

    whether to connect self to the new node

Returns:

  • (Node)

    the newly created node.



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

VALUE lemongraph_dinode_split(int argc, VALUE *argv, VALUE self)
{
  bool connect = true;
  if (argc == 1)
    connect = (argv[0] != Qnil && argv[0] != Qfalse);
  else if (argc > 1)
    rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 0..1)", argc);
  VALUE rbgraph = rb_iv_get(self, "@graph");
  lemon::ListDigraph& g = lemongraph_digraph_rb2ref(rbgraph);
  lemon::ListDigraph::Node n = g.split(lemongraph_rbdinode2dinode(self), connect);
  return lemongraph_make_dinode(n, rbgraph);
}

#valid?Boolean

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

Returns:

  • (Boolean)


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

VALUE lemongraph_dinode_is_valid(VALUE self)
{
  return lemongraph_digraph_rb2ref(rb_iv_get(self, "@graph")).valid(lemongraph_rbdinode2dinode(self)) ? Qtrue : Qfalse;
}