Class: RGL::Edge::DirectedEdge
- Inherits:
-
Object
- Object
- RGL::Edge::DirectedEdge
- Defined in:
- lib/rgl/base.rb
Overview
An RGL::Edge is simply a directed pair (source -> target). Most library functions try do omit to instantiate edges. They instead use two vertex parameters for representing edges (see Graph#each_edge). If a client wants to store edges explicitly DirectedEdge or UnDirectedEdge instances are returned (i.e. Graph#edges).
Direct Known Subclasses
Instance Attribute Summary collapse
-
#source ⇒ Object
Returns the value of attribute source.
-
#target ⇒ Object
Returns the value of attribute target.
Class Method Summary collapse
-
.[](*a) ⇒ Object
Can be used to create an edge from a two element array.
Instance Method Summary collapse
-
#<=>(e) ⇒ Object
Sort support is dispatched to the <=> method of Array.
-
#[](index) ⇒ Object
Edges can be indexed.
-
#eql?(edge) ⇒ Boolean
(also: #==)
Two directed edges (u,v) and (x,y) are equal iff u == x and v == y.
- #hash ⇒ Object
-
#initialize(a, b) ⇒ DirectedEdge
constructor
Create a new DirectedEdge with source
a
and targetb
. -
#reverse ⇒ Edge
Returns (v,u) if self == (u,v).
-
#to_a ⇒ Array
Returns the array [source,target].
-
#to_s ⇒ String
(also: #inspect)
Returns string representation of the edge.
Constructor Details
#initialize(a, b) ⇒ DirectedEdge
Create a new DirectedEdge with source a
and target b
.
45 46 47 |
# File 'lib/rgl/base.rb', line 45 def initialize(a, b) @source, @target = a, b end |
Instance Attribute Details
#source ⇒ Object
Returns the value of attribute source.
36 37 38 |
# File 'lib/rgl/base.rb', line 36 def source @source end |
#target ⇒ Object
Returns the value of attribute target.
36 37 38 |
# File 'lib/rgl/base.rb', line 36 def target @target end |
Class Method Details
.[](*a) ⇒ Object
Can be used to create an edge from a two element array.
39 40 41 |
# File 'lib/rgl/base.rb', line 39 def self.[](*a) new(a[0], a[1]) end |
Instance Method Details
#<=>(e) ⇒ Object
Sort support is dispatched to the <=> method of Array
93 94 95 |
# File 'lib/rgl/base.rb', line 93 def <=> e self.to_a <=> e.to_a end |
#[](index) ⇒ Object
Edges can be indexed. edge.at(0) == edge.source, edge.at(n) == edge.target for all n>0. Edges can thus be used as a two element array.
70 71 72 |
# File 'lib/rgl/base.rb', line 70 def [](index) index.zero? ? source : target end |
#eql?(edge) ⇒ Boolean Also known as: ==
Two directed edges (u,v) and (x,y) are equal iff u == x and v == y. eql?
is needed when edges are inserted into a Set
. eql?
is aliased to ==.
51 52 53 |
# File 'lib/rgl/base.rb', line 51 def eql?(edge) (source == edge.source) && (target == edge.target) end |
#hash ⇒ Object
57 58 59 |
# File 'lib/rgl/base.rb', line 57 def hash source.hash ^ target.hash end |
#reverse ⇒ Edge
Returns (v,u) if self == (u,v).
63 64 65 |
# File 'lib/rgl/base.rb', line 63 def reverse self.class.new(target, source) end |
#to_a ⇒ Array
Returns the array [source,target].
87 88 89 |
# File 'lib/rgl/base.rb', line 87 def to_a [source, target] end |
#to_s ⇒ String Also known as: inspect
Returns string representation of the edge
78 79 80 |
# File 'lib/rgl/base.rb', line 78 def to_s "(#{source}-#{target})" end |