Class: Tangle::Vertex

Inherits:
Object
  • Object
show all
Includes:
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, vertex_id: object_id, **kwargs) ⇒ 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


20
21
22
23
24
25
26
27
28
29
# File 'lib/tangle/vertex.rb', line 20

def initialize(graph: nil,
               name: nil,
               vertex_id: object_id,
               **kwargs)
  @graph = graph
  @name = name
  @vertex_id = vertex_id

  initialize_mixins(**kwargs)
end

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph.



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

def graph
  @graph
end

#nameObject (readonly)

Returns the value of attribute name.



84
85
86
# File 'lib/tangle/vertex.rb', line 84

def name
  @name
end

#vertex_idObject (readonly)

Returns the value of attribute vertex_id.



85
86
87
# File 'lib/tangle/vertex.rb', line 85

def vertex_id
  @vertex_id
end

Instance Method Details

#!=(other) ⇒ Object

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



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

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

#==(other) ⇒ Object

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



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

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:



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

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

#clone_into(graph) ⇒ Object

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

clone_into(new_graph) => Vertex

Raises an ArgumentError if graph would remain the same.

Raises:

  • (ArgumentError)


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

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

  clone.with_graph(graph)
end

#edgesObject

Return all edges that touch this vertex



46
47
48
49
50
# File 'lib/tangle/vertex.rb', line 46

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)


72
73
74
# File 'lib/tangle/vertex.rb', line 72

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

#neighbours(included = edges) ⇒ Object

Return the set of adjacent vertices



54
55
56
# File 'lib/tangle/vertex.rb', line 54

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