Class: Tangle::Vertex

Inherits:
SimpleDelegator
  • Object
show all
Includes:
PP::ObjectMixin, Mixin::Initialize
Defined in:
lib/tangle/vertex.rb

Overview

A named vertex in a graph

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph: nil, name: nil, delegate: nil, vertex_id: object_id) ⇒ Vertex

Create a new vertex

Vertex.new(…) => Vertex

Named arguments:

graph: a Graph or nil for an orphaned vertex
name: anything that's hashable and unique within the graph
delegate: delegate object for missing methods


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tangle/vertex.rb', line 22

def initialize(graph: nil,
               name: nil,
               delegate: nil,
               vertex_id: object_id)
  super(delegate) unless delegate.nil?

  @graph = graph
  @name = name
  @delegate = delegate
  @vertex_id = vertex_id

  initialize_mixins
end

Instance Attribute Details

#delegateObject (readonly)

Returns the value of attribute delegate.



95
96
97
# File 'lib/tangle/vertex.rb', line 95

def delegate
  @delegate
end

#graphObject (readonly)

Returns the value of attribute graph.



93
94
95
# File 'lib/tangle/vertex.rb', line 93

def graph
  @graph
end

#nameObject (readonly)

Returns the value of attribute name.



94
95
96
# File 'lib/tangle/vertex.rb', line 94

def name
  @name
end

#vertex_idObject (readonly)

Returns the value of attribute vertex_id.



96
97
98
# File 'lib/tangle/vertex.rb', line 96

def vertex_id
  @vertex_id
end

Instance Method Details

#!=(other) ⇒ Object

If two vertices have the same vertex_id, they have the same value



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

def !=(other)
  @vertex_id != other.vertex_id
end

#==(other) ⇒ Object

If two vertices have the same vertex_id, they have the same value



70
71
72
# File 'lib/tangle/vertex.rb', line 70

def ==(other)
  @vertex_id == other.vertex_id
end

#adjacent?(other) ⇒ Boolean

Two vertices are adjacent if there is an edge between them

Returns:

  • (Boolean)

Raises:



88
89
90
91
# File 'lib/tangle/vertex.rb', line 88

def adjacent?(other)
  raise GraphError unless @graph == other.graph
  edges.any? { |edge| edge.walk(self) == other }
end

#dup_into(graph) ⇒ Object

Duplicate a vertex in a new graph, keeping all other contained attributes End users should probably use Graph#subgrap instead.

dup_into(new_graph) => Vertex

Raises an ArgumentError if graph would remain the same.

Raises:

  • (ArgumentError)


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

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

  Vertex.new(
    graph:     graph,
    name:      @name,
    delegate:  @delegate,
    vertex_id: @vertex_id
  )
end

#edgesObject

Return all edges that touch this vertex



56
57
58
59
60
# File 'lib/tangle/vertex.rb', line 56

def edges
  return [] if @graph.nil?

  @graph.edges { |edge| edge.include? self }
end

#eql?(other) ⇒ Boolean

If two vertices have the same object_id, they are identical

Returns:

  • (Boolean)


82
83
84
# File 'lib/tangle/vertex.rb', line 82

def eql?(other)
  @object_id == other.object_id
end

#neighboursObject

Return the set of adjacent vertices



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

def neighbours
  Set.new(edges.map { |edge| edge.walk(self) })
end