Class: Tangle::Edge

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Mixin::Initialize
Defined in:
lib/tangle/edge.rb

Overview

An edge in a graph, connecting two vertices

Direct Known Subclasses

Simple::Edge

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#graphObject (readonly)

Returns the value of attribute graph.



68
69
70
# File 'lib/tangle/edge.rb', line 68

def graph
  @graph
end

#nameObject (readonly)

Returns the value of attribute name.



67
68
69
# File 'lib/tangle/edge.rb', line 67

def name
  @name
end

#verticesObject (readonly)

Returns the value of attribute vertices.



69
70
71
# File 'lib/tangle/edge.rb', line 69

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.



45
46
47
48
49
50
51
52
53
54
# File 'lib/tangle/edge.rb', line 45

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?

Returns:

  • (Boolean)


60
61
62
# File 'lib/tangle/edge.rb', line 60

def eql?(other)
  @graph == other.graph && @vertices == other.vertices
end

#inspectObject



56
57
58
# File 'lib/tangle/edge.rb', line 56

def inspect
  "#<#{self.class}: #{@vertices}>"
end

#walk(from_vertex) ⇒ Object

Follow the edge from a vertex to the other end

walk(vertex) => Vertex

Raises:

  • (RuntimeError)


31
32
33
34
# File 'lib/tangle/edge.rb', line 31

def walk(from_vertex)
  raise RuntimeError unless @vertices.include?(from_vertex)
  @vertices.find { |other| other != from_vertex } || from_vertex
end