Class: MOSAIK::Graph::Vertex

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mosaik/graph/vertex.rb

Overview

Vertex in the graph

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, attributes = {}) ⇒ Vertex

Returns a new instance of Vertex.



23
24
25
26
27
# File 'lib/mosaik/graph/vertex.rb', line 23

def initialize(id, attributes = {})
  @id = id
  @edges = T.let(Hash.new { |h, k| h[k] = [] }, T::Hash[String, T::Array[Edge]])
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



20
21
22
# File 'lib/mosaik/graph/vertex.rb', line 20

def attributes
  @attributes
end

#edgesObject (readonly)

Returns the value of attribute edges.



17
18
19
# File 'lib/mosaik/graph/vertex.rb', line 17

def edges
  @edges
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/mosaik/graph/vertex.rb', line 14

def id
  @id
end

Instance Method Details

#add_edge(to, attributes = {}) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/mosaik/graph/vertex.rb', line 30

def add_edge(to, attributes = {})
  edge = Edge.new(attributes)

  T.must(edges[to]) << edge

  edge
end

#find_edge(to, attributes = {}) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/mosaik/graph/vertex.rb', line 44

def find_edge(to, attributes = {})
  if attributes.empty?
    T.must(edges[to]).first
  else
    T.must(edges[to]).find { |edge| attributes.all? { |k, v| edge.attributes[k] == v } }
  end
end

#find_edges(to) ⇒ Object



39
40
41
# File 'lib/mosaik/graph/vertex.rb', line 39

def find_edges(to)
  T.must(edges[to])
end

#find_or_add_edge(to, attributes = {}) ⇒ Object



53
54
55
# File 'lib/mosaik/graph/vertex.rb', line 53

def find_or_add_edge(to, attributes = {})
  find_edge(to, attributes) || add_edge(to, attributes)
end

#inspectObject



67
68
69
# File 'lib/mosaik/graph/vertex.rb', line 67

def inspect
  "#<#{self.class.name} id=#{id} attributes=#{attributes.map { |k, v| "#{k}: #{v}" }.join(', ')} edges=#{edges.size}>"
end

#remove_edge(id, edge = nil) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/mosaik/graph/vertex.rb', line 58

def remove_edge(id, edge = nil)
  if edge.nil?
    edges.delete(id)
  else
    T.must(edges[id]).delete(edge)
  end
end