Class: LemonGraph::Digraph

Inherits:
Object
  • Object
show all
Defined in:
ext/lemongraph/digraph.cc,
lib/lemongraph/graphviz.rb,
ext/lemongraph/lemongraph.cc

Overview

Directed graph.

Defined Under Namespace

Classes: Arc, Node

Instance Method Summary collapse

Instance Method Details

#add_arc(s, t) ⇒ Arc

Adds a new arc to the graph with source node s and target node t.

Parameters:

  • s (Node)

    source node of the arc

  • t (Node)

    target node of the arc

Returns:

  • (Arc)

    the newly created arc



92
93
94
95
96
97
98
99
100
101
# File 'ext/lemongraph/digraph.cc', line 92

VALUE lemongraph_digraph_add_arc(VALUE self, VALUE s, VALUE t)
{
	lemon::ListDigraph &g = lemongraph_digraph_rb2ref(self);
	lemongraph_digraph_raise_if_not_valid_node(g, self, s);
	lemongraph_digraph_raise_if_not_valid_node(g, self, t);
	lemon::ListDigraph::Arc a = g.addArc(
			lemongraph_rbdinode2dinode(s),
			lemongraph_rbdinode2dinode(t));
	return lemongraph_make_diarc(a, self);
}

#add_nodeNode

Adds a new node to the graph

Returns:

  • (Node)

    the newly created node



67
68
69
70
71
# File 'ext/lemongraph/digraph.cc', line 67

VALUE lemongraph_digraph_add_node(VALUE self)
{
	lemon::ListDigraph::Node n = lemongraph_digraph_rb2ref(self).addNode();
	return lemongraph_make_dinode(n, self);
}

#arcsArray<Arc>

Returns an array containing all the arcs of the graph.

Returns:



231
232
233
234
235
236
237
238
# File 'ext/lemongraph/digraph.cc', line 231

VALUE lemongraph_digraph_arcs(VALUE self)
{
	lemon::ListDigraph& g = lemongraph_digraph_rb2ref(self);
	VALUE ar = rb_ary_new_capa(lemon::countArcs(g));
	for (lemon::ListDigraph::ArcIt a(g); a != lemon::INVALID; ++a)
		rb_ary_push(ar, lemongraph_make_diarc(a, self));
	return ar;
}

#clearself

Erases all nodes and arcs from the graph

Returns:

  • (self)


159
160
161
162
163
# File 'ext/lemongraph/digraph.cc', line 159

VALUE lemongraph_digraph_clear(VALUE self)
{
	lemongraph_digraph_rb2ref(self).clear();
	return self;
}

#each_arc {|arc| ... } ⇒ self #each_arcEnumerator

Calls the block, if given, once for each Arc in the graph. The arcs are passed as parameters to the block.

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

Overloads:

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

    Yield Parameters:

    Returns:

    • (self)
  • #each_arcEnumerator

    Returns:

    • (Enumerator)

Returns:

  • (self, Enumerator)


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

VALUE lemongraph_digraph_each_arc(VALUE self)
{
	RETURN_SIZED_ENUMERATOR(self, 0, 0, lemongraph_digraph_arcenum_nbarcs);
	lemon::ListDigraph& g = lemongraph_digraph_rb2ref(self);
	for (lemon::ListDigraph::ArcIt a(g); a != lemon::INVALID; ++a)
		rb_yield(lemongraph_make_diarc(a, self));
	return self;
}

#each_node {|node| ... } ⇒ self #each_nodeEnumerator

Calls the block, if given, once for each Node in the graph. The nodes are passed as parameters to the block.

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

Overloads:

  • #each_node {|node| ... } ⇒ self

    Yield Parameters:

    Returns:

    • (self)
  • #each_nodeEnumerator

    Returns:

    • (Enumerator)

Returns:

  • (self, Enumerator)


208
209
210
211
212
213
214
215
# File 'ext/lemongraph/digraph.cc', line 208

VALUE lemongraph_digraph_each_node(VALUE self)
{
	RETURN_SIZED_ENUMERATOR(self, 0, 0, lemongraph_digraph_nodeenum_nbnodes);
	lemon::ListDigraph& g = lemongraph_digraph_rb2ref(self);
	for (lemon::ListDigraph::NodeIt n(g); n != lemon::INVALID; ++n)
		rb_yield(lemongraph_make_dinode(n, self));
	return self;
}

#erase(node) ⇒ self #erase(arc) ⇒ self

Erases the node or arc from the graph.

Overloads:

  • #erase(node) ⇒ self

    Erases the given node along with its outgoing and incoming arcs from the graph.

    Parameters:

    • node (Node)

      the node to erase

  • #erase(arc) ⇒ self

    Erases the given arc from the graph.

    Parameters:

    • arc (Arc)

      the arc to erase

Returns:

  • (self)


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'ext/lemongraph/digraph.cc', line 136

VALUE lemongraph_digraph_erase_nodearc(VALUE self, VALUE item)
{
	VALUE klass = rb_class_of(item);
	if (klass == c_DiNode) {
		if (rb_iv_get(item, "@graph") != self)
			rb_raise(rb_eRuntimeError, "this node does not belong to the graph");
		lemongraph_digraph_rb2ref(self).erase(lemongraph_rbdinode2dinode(item));
	}
	else if (klass == c_DiArc) {
		if (rb_iv_get(item, "@graph") != self)
			rb_raise(rb_eRuntimeError, "this arc does not belong to the graph");
		lemongraph_digraph_rb2ref(self).erase(lemongraph_rbdiarc2diarc(item));
	}
	else
		rb_raise(rb_eTypeError, "expecting a %" PRIsVALUE " or a %" PRIsVALUE ", not a %s",
				rb_class_name(c_DiNode), rb_class_name(c_DiArc), rb_obj_classname(item));
	return self;
}

#initialize_copy(orig) ⇒ self

Method called by dup and clone methods.

Returns:

  • (self)


273
274
275
276
277
278
279
280
# File 'ext/lemongraph/digraph.cc', line 273

VALUE lemongraph_digraph_initialize_copy(VALUE self, VALUE orig)
{
	rb_call_super(1, &orig);
	lemon::ListDigraph& from = lemongraph_digraph_rb2ref(orig);
	lemon::ListDigraph& to =   lemongraph_digraph_rb2ref(self);
	lemon::digraphCopy(from, to).run();
	return self;
}

#nb_arcsInteger

Number of arcs in the graph.

Returns:

  • (Integer)


221
222
223
224
225
# File 'ext/lemongraph/digraph.cc', line 221

VALUE lemongraph_digraph_nb_arcs(VALUE self)
{
	lemon::ListDigraph& g = lemongraph_digraph_rb2ref(self);
	return INT2NUM(lemon::countArcs(g));
}

#nb_nodesInteger

Number of nodes in the graph.

Returns:

  • (Integer)


169
170
171
172
173
# File 'ext/lemongraph/digraph.cc', line 169

VALUE lemongraph_digraph_nb_nodes(VALUE self)
{
	lemon::ListDigraph& g = lemongraph_digraph_rb2ref(self);
	return INT2NUM(lemon::countNodes(g));
}

#nodesArray<Node>

Returns an array containing all the nodes of the graph.

Returns:



179
180
181
182
183
184
185
186
# File 'ext/lemongraph/digraph.cc', line 179

VALUE lemongraph_digraph_nodes(VALUE self)
{
	lemon::ListDigraph& g = lemongraph_digraph_rb2ref(self);
	VALUE a = rb_ary_new_capa(lemon::countNodes(g));
	for (lemon::ListDigraph::NodeIt n(g); n != lemon::INVALID; ++n)
		rb_ary_push(a, lemongraph_make_dinode(n, self));
	return a;
}

#to_dot(**h) ⇒ Graphviz

Returns a Graphviz object. See Graphviz#initialize for optional keyword arguments.

Returns:



224
225
226
# File 'lib/lemongraph/graphviz.rb', line 224

def to_dot(**h)
  Graphviz.new(self, **h)
end

#valid?(item) ⇒ Boolean

Test if the given node/arc is valid, i.e. it is a real node/arc of the graph.

Parameters:

Returns:

  • (Boolean)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'ext/lemongraph/digraph.cc', line 108

VALUE lemongraph_digraph_item_is_valid(VALUE self, VALUE item)
{
	bool r = false;
	VALUE klass = rb_class_of(item);
	if (klass == c_DiNode) {
		if (rb_iv_get(item, "@graph") == self)
			r = lemongraph_digraph_rb2ref(self).valid(lemongraph_rbdinode2dinode(item));
	}
	else if (klass == c_DiArc) {
		if (rb_iv_get(item, "@graph") == self)
			r = lemongraph_digraph_rb2ref(self).valid(lemongraph_rbdiarc2diarc(item));
	}
	else
		rb_raise(rb_eTypeError, "expecting a %" PRIsVALUE " or a %" PRIsVALUE ", not a %s",
				rb_class_name(c_DiNode), rb_class_name(c_DiArc), rb_obj_classname(item));
	return r ? Qtrue : Qfalse;
}