Class: Puppet::SimpleGraph::VertexWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/simple_graph.rb

Overview

An internal class for handling a vertex’s edges.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vertex) ⇒ VertexWrapper

Returns a new instance of VertexWrapper.



21
22
23
24
# File 'lib/puppet/simple_graph.rb', line 21

def initialize(vertex)
  @vertex = vertex
  @adjacencies = {:in => {}, :out => {}}
end

Instance Attribute Details

#inObject

Returns the value of attribute in.



12
13
14
# File 'lib/puppet/simple_graph.rb', line 12

def in
  @in
end

#outObject

Returns the value of attribute out.



12
13
14
# File 'lib/puppet/simple_graph.rb', line 12

def out
  @out
end

#vertexObject

Returns the value of attribute vertex.



12
13
14
# File 'lib/puppet/simple_graph.rb', line 12

def vertex
  @vertex
end

Instance Method Details

#add_edge(direction, edge) ⇒ Object

Add an edge to our list.



37
38
39
# File 'lib/puppet/simple_graph.rb', line 37

def add_edge(direction, edge)
  opposite_adjacencies(direction, edge) << edge
end

#adjacent(options) ⇒ Object

Find adjacent vertices or edges.



27
28
29
30
31
32
33
34
# File 'lib/puppet/simple_graph.rb', line 27

def adjacent(options)
  direction = options[:direction] || :out
  options[:type] ||= :vertices

  return send(direction.to_s + "_edges") if options[:type] == :edges

  @adjacencies[direction].keys.reject { |vertex| @adjacencies[direction][vertex].empty? }
end

#clearObject

Remove all references to everything.



15
16
17
18
19
# File 'lib/puppet/simple_graph.rb', line 15

def clear
  @adjacencies[:in].clear
  @adjacencies[:out].clear
  @vertex = nil
end

#edgesObject

Return all known edges.



42
43
44
# File 'lib/puppet/simple_graph.rb', line 42

def edges
  in_edges + out_edges
end

#has_edge?(direction, vertex) ⇒ Boolean

Test whether we share an edge with a given vertex.

Returns:

  • (Boolean)


47
48
49
# File 'lib/puppet/simple_graph.rb', line 47

def has_edge?(direction, vertex)
  return(vertex_adjacencies(direction, vertex).length > 0 ? true : false)
end

#inspectObject



83
84
85
# File 'lib/puppet/simple_graph.rb', line 83

def inspect
  { :@adjacencies => @adjacencies, :@vertex => @vertex.to_s }.inspect
end

#other_vertex(direction, edge) ⇒ Object

The other vertex in the edge.



65
66
67
68
69
70
71
# File 'lib/puppet/simple_graph.rb', line 65

def other_vertex(direction, edge)
  case direction
  when :in; edge.source
  else
    edge.target
  end
end

#remove_edge(direction, edge) ⇒ Object

Remove an edge from our list. Assumes that we’ve already checked that the edge is valid.



75
76
77
# File 'lib/puppet/simple_graph.rb', line 75

def remove_edge(direction, edge)
  opposite_adjacencies(direction, edge).delete(edge)
end

#to_sObject



79
80
81
# File 'lib/puppet/simple_graph.rb', line 79

def to_s
  vertex.to_s
end