Sha256: 8ba8f5e4a2cd6b3550583243a5b7a101f3a90bf0fd711032f03bd6022f686d7a

Contents?: true

Size: 1020 Bytes

Versions: 70

Compression:

Stored size: 1020 Bytes

Contents

# frozen_string_literal: true

require 'set'
require 'tsort'

module LegacyFacter
  module Core
    class DirectedGraph < Hash
      include TSort

      def acyclic?
        cycles.empty?
      end

      def cycles
        cycles = []
        each_strongly_connected_component do |component|
          cycles << component if component.size > 1
        end
        cycles
      end

      alias tsort_each_node each_key

      def tsort_each_child(node)
        fetch(node, []).each do |child|
          yield child
        end
      end

      def tsort
        missing = Set.new(values.flatten) - Set.new(keys)

        unless missing.empty?
          raise MissingVertex, "Cannot sort elements; cannot depend on missing elements #{missing.to_a}"
        end

        super
      rescue TSort::Cyclic
        raise CycleError, "Cannot sort elements; found the following cycles: #{cycles.inspect}"
      end

      class CycleError < StandardError; end
      class MissingVertex < StandardError; end
    end
  end
end

Version data entries

70 entries across 70 versions & 1 rubygems

Version Path
facter-4.5.2 lib/facter/custom_facts/core/directed_graph.rb
facter-4.5.1 lib/facter/custom_facts/core/directed_graph.rb
facter-4.5.0 lib/facter/custom_facts/core/directed_graph.rb
facter-4.4.3 lib/facter/custom_facts/core/directed_graph.rb
facter-4.4.2 lib/facter/custom_facts/core/directed_graph.rb
facter-4.4.1 lib/facter/custom_facts/core/directed_graph.rb
facter-4.4.0 lib/facter/custom_facts/core/directed_graph.rb
facter-4.3.1 lib/facter/custom_facts/core/directed_graph.rb
facter-4.3.0 lib/facter/custom_facts/core/directed_graph.rb
facter-4.2.14 lib/facter/custom_facts/core/directed_graph.rb
facter-4.2.13 lib/facter/custom_facts/core/directed_graph.rb
facter-4.2.12 lib/facter/custom_facts/core/directed_graph.rb
facter-4.2.11 lib/facter/custom_facts/core/directed_graph.rb
facter-4.2.10 lib/facter/custom_facts/core/directed_graph.rb
facter-4.2.9 lib/facter/custom_facts/core/directed_graph.rb
facter-4.2.8 lib/facter/custom_facts/core/directed_graph.rb
facter-4.2.7 lib/facter/custom_facts/core/directed_graph.rb
facter-4.2.6 lib/facter/custom_facts/core/directed_graph.rb
facter-4.2.5 lib/facter/custom_facts/core/directed_graph.rb
facter-4.2.4 lib/facter/custom_facts/core/directed_graph.rb