class Bundler::Molinillo::DependencyGraph

A directed acyclic graph that is tuned to hold named dependencies

Constants

Edge

A directed edge of a {DependencyGraph} @attr [Vertex] origin The origin of the directed edge @attr [Vertex] destination The destination of the directed edge @attr [Array] requirements The requirements the directed edge represents

Attributes

edges[R]

@return [Set<Edge>] the edges of the dependency graph

root_vertices[R]

@return [{String => Vertex}] vertices that have no {Vertex#predecessors},

keyed by by {Vertex#name}
vertices[R]

@return [{String => Vertex}] the vertices of the dependency graph, keyed

by {Vertex#name}

Public Class Methods

new() click to toggle source
# File lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 49
def initialize
  @vertices = {}
  @edges = Set.new
  @root_vertices = {}
end
tsort(vertices) click to toggle source

Topologically sorts the given vertices. @param [Enumerable<Vertex>] vertices the vertices to be sorted, which must

all belong to the same graph.

@return [Array<Vertex>] The sorted vertices.

# File lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 27
def self.tsort(vertices)
  TSort.tsort(
    lambda { |b| vertices.each(&b) },
    lambda { |v, &b| (v.successors & vertices).each(&b) }
  )
end

Public Instance Methods

==(other) click to toggle source

@return [Boolean] whether the two dependency graphs are equal, determined

by a recursive traversal of each {#root_vertices} and its
{Vertex#successors}
# File lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 82
def ==(other)
  root_vertices == other.root_vertices
end
add_child_vertex(name, payload, parent_names, requirement) click to toggle source

@param [String] name @param [Object] payload @param [Array<String>] parent_names @param [Object] requirement the requirement that is requiring the child @return [void]

# File lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 91
def add_child_vertex(name, payload, parent_names, requirement)
  is_root = parent_names.include?(nil)
  parent_nodes = parent_names.compact.map { |n| vertex_named(n) }
  vertex = vertex_named(name) || if is_root
                                   add_root_vertex(name, payload)
                                 else
                                   add_vertex(name, payload)
                                 end
  vertex.payload ||= payload
  parent_nodes.each do |parent_node|
    add_edge(parent_node, vertex, requirement)
  end
  vertex
end
add_edge(origin, destination, requirement) click to toggle source

Adds a new {Edge} to the dependency graph @param [Vertex] origin @param [Vertex] destination @param [Object] requirement the requirement that this edge represents @return [Edge] the added edge

# File lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 151
def add_edge(origin, destination, requirement)
  if origin == destination || destination.path_to?(origin)
    raise CircularDependencyError.new([origin, destination])
  end
  Edge.new(origin, destination, [requirement]).tap { |e| edges << e }
end
add_root_vertex(name, payload) click to toggle source

@param [String] name @param [Object] payload @return [Vertex] the vertex that was added to `self`

# File lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 117
def add_root_vertex(name, payload)
  add_vertex(name, payload).tap { |v| root_vertices[name] = v }
end
add_vertex(name, payload) click to toggle source

@param [String] name @param [Object] payload @return [Vertex] the vertex that was added to `self`

# File lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 109
def add_vertex(name, payload)
  vertex = vertices[name] ||= Vertex.new(self, name, payload)
  vertex.tap { |v| v.payload = payload }
end
detach_vertex_named(name) click to toggle source

Detaches the {#vertex_named} `name` {Vertex} from the graph, recursively removing any non-root vertices that were orphaned in the process @param [String] name @return [void]

# File lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 125
def detach_vertex_named(name)
  vertex = vertex_named(name)
  return unless vertex
  successors = vertex.successors
  vertices.delete(name)
  edges.reject! { |e| e.origin == vertex || e.destination == vertex }
  successors.each { |v| detach_vertex_named(v.name) unless root_vertices[v.name] || v.predecessors.any? }
end
each() { |v| ... } click to toggle source

Enumerates through the vertices of the graph. @return [Array<Vertex>] The graph's vertices.

# File lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 11
def each
  vertices.values.each { |v| yield v }
end
Also aliased as: tsort_each_node
initialize_copy(other) click to toggle source

Initializes a copy of a {DependencyGraph}, ensuring that all {#vertices} have the correct {Vertex#graph} set

Calls superclass method
# File lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 57
def initialize_copy(other)
  super
  @vertices = other.vertices.reduce({}) do |vertices, (name, vertex)|
    vertices.tap do |hash|
      hash[name] = vertex.dup.tap { |v| v.graph = self }
    end
  end
  @root_vertices = Hash[@vertices.select { |n, _v| other.root_vertices[n] }]
  @edges = other.edges.map do |edge|
    Edge.new(
      vertex_named(edge.origin.name),
      vertex_named(edge.destination.name),
      edge.requirements.dup
    )
  end
end
inspect() click to toggle source

@return [String] a string suitable for debugging

# File lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 75
def inspect
  "#{self.class}:#{vertices.values.inspect}"
end
root_vertex_named(name) click to toggle source

@param [String] name @return [Vertex,nil] the root vertex with the given name

# File lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 142
def root_vertex_named(name)
  root_vertices[name]
end
tsort_each_child(vertex, &block) click to toggle source
# File lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 19
def tsort_each_child(vertex, &block)
  vertex.successors.each(&block)
end
tsort_each_node()
Alias for: each
vertex_named(name) click to toggle source

@param [String] name @return [Vertex,nil] the vertex with the given name

# File lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 136
def vertex_named(name)
  vertices[name]
end