Class: LemonGraph::Digraph
- Inherits:
-
Object
- Object
- LemonGraph::Digraph
- Defined in:
- ext/lemongraph/digraph.cc,
lib/lemongraph/graphviz.rb,
ext/lemongraph/lemongraph.cc
Overview
Directed graph.
Defined Under Namespace
Instance Method Summary collapse
-
#add_arc(s, t) ⇒ Arc
Adds a new arc to the graph with source node s and target node t.
-
#add_node ⇒ Node
Adds a new node to the graph.
-
#arcs ⇒ Array<Arc>
Returns an array containing all the arcs of the graph.
-
#clear ⇒ self
Erases all nodes and arcs from the graph.
-
#each_arc ⇒ self, Enumerator
Calls the block, if given, once for each Arc in the graph.
-
#each_node ⇒ self, Enumerator
Calls the block, if given, once for each Node in the graph.
-
#erase(item) ⇒ self
Erases the node or arc from the graph.
-
#initialize_copy(orig) ⇒ self
Method called by dup and clone methods.
-
#nb_arcs ⇒ Integer
Number of arcs in the graph.
-
#nb_nodes ⇒ Integer
Number of nodes in the graph.
-
#nodes ⇒ Array<Node>
Returns an array containing all the nodes of the graph.
-
#to_dot(**h) ⇒ Graphviz
Returns a Graphviz object.
-
#valid?(item) ⇒ Boolean
Test if the given node/arc is valid, i.e.
Instance Method Details
#add_arc(s, t) ⇒ Arc
Adds a new arc to the graph with source node s and target node t.
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_node ⇒ Node
Adds a new node to the graph
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);
}
|
#arcs ⇒ Array<Arc>
Returns an array containing all the arcs of the graph.
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;
}
|
#clear ⇒ self
Erases all nodes and arcs from the graph
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_arc ⇒ Enumerator
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.
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_node ⇒ Enumerator
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.
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.
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.
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_arcs ⇒ Integer
Number of arcs in the graph.
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_nodes ⇒ Integer
Number of nodes in the graph.
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));
}
|
#nodes ⇒ Array<Node>
Returns an array containing all the nodes of the graph.
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.
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.
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;
}
|