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

Directed::Edge, Simple::Edge

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vertex1, vertex2 = vertex1, graph: nil, **kwargs) ⇒ 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.



19
20
21
22
23
24
25
26
# File 'lib/tangle/edge.rb', line 19

def initialize(vertex1, vertex2 = vertex1, graph: nil, **kwargs)
  with_graph(graph)
  with_vertices(vertex1, vertex2)

  initialize_mixins(**kwargs)

  validate_edge
end

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph.



77
78
79
# File 'lib/tangle/edge.rb', line 77

def graph
  @graph
end

#nameObject (readonly)

Returns the value of attribute name.



76
77
78
# File 'lib/tangle/edge.rb', line 76

def name
  @name
end

#verticesObject (readonly)

Returns the value of attribute vertices.



78
79
80
# File 'lib/tangle/edge.rb', line 78

def vertices
  @vertices
end

Instance Method Details

#clone_into(graph) ⇒ Object

Clone 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.

clone_into(graph) => Edge or nil

Raises an ArgumentError if graph would remain the same.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tangle/edge.rb', line 53

def clone_into(graph)
  raise ArgumentError if graph == @graph

  vertices = @vertices.map do |vertex|
    graph.get_vertex(vertex.vertex_id)
  end

  clone.with_graph(graph).with_vertices(*vertices)
rescue KeyError
  nil
end

#eql?(other) ⇒ Boolean Also known as: ==, ===, equal?

Returns:

  • (Boolean)


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

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

#inspectObject



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

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

#other(for_vertex) ⇒ Object

Return the other vertex for a vertex of this edge

Raises:

  • (RuntimeError)


38
39
40
41
42
# File 'lib/tangle/edge.rb', line 38

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



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

def walk(from_vertex, selector: :other)
  send(selector, from_vertex)
end