Class: Tangle::Edge
- Inherits:
-
Object
- Object
- Tangle::Edge
- Extended by:
- Forwardable
- Includes:
- Mixin::Initialize
- Defined in:
- lib/tangle/edge.rb
Overview
An edge in a graph, connecting two vertices
Direct Known Subclasses
Instance Attribute Summary collapse
-
#graph ⇒ Object
readonly
Returns the value of attribute graph.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#vertices ⇒ Object
readonly
Returns the value of attribute vertices.
Instance Method Summary collapse
-
#dup_into(graph) ⇒ Object
Duplicate an edge into another graph, replacing original vertices with their already prepared duplicates in the other graph.
- #eql?(other) ⇒ Boolean (also: #==, #===, #equal?)
-
#initialize(vertex1, vertex2 = vertex1, graph: nil) ⇒ Edge
constructor
Create a new edge between vertices.
- #inspect ⇒ Object
-
#other(for_vertex) ⇒ Object
Return the other vertex for a vertex of this edge.
-
#walk(from_vertex, selector: :other) ⇒ Object
Follow the edge from a vertex to the other end.
Constructor Details
#initialize(vertex1, vertex2 = vertex1, graph: nil) ⇒ Edge
Create a new edge between vertices
Edge.new(vtx1) => Edge (loop) Edge.new(vtx1, vtx2) => Edge
End users should probably use Graph#add_edge instead.
18 19 20 21 22 23 24 25 |
# File 'lib/tangle/edge.rb', line 18 def initialize(vertex1, vertex2 = vertex1, graph: nil) @vertices ||= Set[vertex1, vertex2] @graph = graph initialize_mixins validate_edge end |
Instance Attribute Details
#graph ⇒ Object (readonly)
Returns the value of attribute graph.
75 76 77 |
# File 'lib/tangle/edge.rb', line 75 def graph @graph end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
74 75 76 |
# File 'lib/tangle/edge.rb', line 74 def name @name end |
#vertices ⇒ Object (readonly)
Returns the value of attribute vertices.
76 77 78 |
# File 'lib/tangle/edge.rb', line 76 def vertices @vertices end |
Instance Method Details
#dup_into(graph) ⇒ Object
Duplicate an edge into another graph, replacing original vertices with their already prepared duplicates in the other graph. Returns nil if any of the vertices does not exist in the other graph. End users should probably use Graph#subgraph instead.
dup_into(graph) => Edge or nil
Raises an ArgumentError if graph would remain the same.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/tangle/edge.rb', line 52 def dup_into(graph) raise ArgumentError if graph == @graph vertices = @vertices.map do |vertex| graph.get_vertex(vertex.vertex_id) end self.class.new(*vertices, graph: graph) rescue KeyError nil end |
#eql?(other) ⇒ Boolean Also known as: ==, ===, equal?
67 68 69 |
# File 'lib/tangle/edge.rb', line 67 def eql?(other) @graph == other.graph && @vertices == other.vertices end |
#inspect ⇒ Object
63 64 65 |
# File 'lib/tangle/edge.rb', line 63 def inspect "#<#{self.class}: #{@vertices}>" end |
#other(for_vertex) ⇒ Object
Return the other vertex for a vertex of this edge
37 38 39 40 41 |
# File 'lib/tangle/edge.rb', line 37 def other(for_vertex) raise RuntimeError unless @vertices.include?(for_vertex) @vertices.find { |other| other != for_vertex } || for_vertex end |
#walk(from_vertex, selector: :other) ⇒ Object
Follow the edge from a vertex to the other end
walk(vertex) => Vertex
31 32 33 |
# File 'lib/tangle/edge.rb', line 31 def walk(from_vertex, selector: :other) send(selector, from_vertex) end |