Class: Tangle::Edge

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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.



17
18
19
20
21
22
# File 'lib/tangle/edge.rb', line 17

def initialize(vertex1, vertex2 = vertex1, graph: nil)
  @vertices = Set[vertex1, vertex2]
  @graph = graph

  validate_edge
end

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph.



65
66
67
# File 'lib/tangle/edge.rb', line 65

def graph
  @graph
end

#nameObject (readonly)

Returns the value of attribute name.



64
65
66
# File 'lib/tangle/edge.rb', line 64

def name
  @name
end

#verticesObject (readonly)

Returns the value of attribute vertices.



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

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.



42
43
44
45
46
47
48
49
50
51
# File 'lib/tangle/edge.rb', line 42

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)


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

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

#inspectObject



53
54
55
# File 'lib/tangle/edge.rb', line 53

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)


28
29
30
31
# File 'lib/tangle/edge.rb', line 28

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