Class: Nanoc3::DirectedGraph

### Home Classes Methods

### In Files

- lib/nanoc3/base/directed_graph.rb

### Parent

Object

### Methods

- ::new
- #add_edge
- #delete_edges_to
- #direct_predecessors_of
- #direct_successors_of
- #edges
- #predecessors_of
- #remove_edge
- #successors_of

### Files

- ChangeLog
- LICENSE

### Class/Module Index

Quicksearch

- Nanoc3
- Nanoc3::ArrayExtensions
- Nanoc3::CLI
- Nanoc3::CLI::Base
- Nanoc3::CLI::Commands
- Nanoc3::CLI::Commands::Autocompile
- Nanoc3::CLI::Commands::Compile
- Nanoc3::CLI::Commands::CreateItem
- Nanoc3::CLI::Commands::CreateLayout
- Nanoc3::CLI::Commands::CreateSite
- Nanoc3::CLI::Commands::Debug
- Nanoc3::CLI::Commands::Help
- Nanoc3::CLI::Commands::Info
- Nanoc3::CLI::Commands::Update
- Nanoc3::CLI::Commands::View
- Nanoc3::CLI::Logger
- Nanoc3::CodeSnippet
- Nanoc3::Compiler
- Nanoc3::CompilerDSL
- Nanoc3::Context
- Nanoc3::DataSource
- Nanoc3::DataSources
- Nanoc3::DataSources::Delicious
- Nanoc3::DataSources::Filesystem
- Nanoc3::DataSources::FilesystemUnified
- Nanoc3::DataSources::FilesystemVerbose
- Nanoc3::DataSources::LastFM
- Nanoc3::DataSources::Twitter
- Nanoc3::DependencyTracker
- Nanoc3::DirectedGraph
- Nanoc3::Errors
- Nanoc3::Errors::CannotDetermineFilter
- Nanoc3::Errors::CannotLayoutBinaryItem
- Nanoc3::Errors::CannotUseBinaryFilter
- Nanoc3::Errors::CannotUseTextualFilter
- Nanoc3::Errors::DataNotYetAvailable
- Nanoc3::Errors::Generic
- Nanoc3::Errors::NoMatchingCompilationRuleFound
- Nanoc3::Errors::NoMatchingRoutingRuleFound
- Nanoc3::Errors::NoRulesFileFound
- Nanoc3::Errors::RecursiveCompilation
- Nanoc3::Errors::UnknownDataSource
- Nanoc3::Errors::UnknownFilter
- Nanoc3::Errors::UnknownLayout
- Nanoc3::Errors::UnmetDependency
- Nanoc3::Extra
- Nanoc3::Extra::AutoCompiler
- Nanoc3::Extra::CHiCk
- Nanoc3::Extra::CHiCk::CacheController
- Nanoc3::Extra::CHiCk::Client
- Nanoc3::Extra::CHiCk::RackClient
- Nanoc3::Extra::Deployers
- Nanoc3::Extra::Deployers::Rsync
- Nanoc3::Extra::FileProxy
- Nanoc3::Extra::TimeExtensions
- Nanoc3::Extra::VCS
- Nanoc3::Extra::VCSes
- Nanoc3::Extra::VCSes::Bazaar
- Nanoc3::Extra::VCSes::Dummy
- Nanoc3::Extra::VCSes::Git
- Nanoc3::Extra::VCSes::Mercurial
- Nanoc3::Extra::VCSes::Subversion
- Nanoc3::Extra::Validators
- Nanoc3::Extra::Validators::Links
- Nanoc3::Extra::Validators::Links::EachPairEnumerator
- Nanoc3::Extra::Validators::W3C
- Nanoc3::Filter
- Nanoc3::Filters
- Nanoc3::Filters::BlueCloth
- Nanoc3::Filters::CodeRay
- Nanoc3::Filters::ColorizeSyntax
- Nanoc3::Filters::ERB
- Nanoc3::Filters::Erubis
- Nanoc3::Filters::Haml
- Nanoc3::Filters::Kramdown
- Nanoc3::Filters::Less
- Nanoc3::Filters::Markaby
- Nanoc3::Filters::Maruku
- Nanoc3::Filters::RDiscount
- Nanoc3::Filters::RDoc
- Nanoc3::Filters::Rainpress
- Nanoc3::Filters::RedCloth
- Nanoc3::Filters::RelativizePaths
- Nanoc3::Filters::RubyPants
- Nanoc3::Filters::Sass
- Nanoc3::HashExtensions
- Nanoc3::Helpers
- Nanoc3::Helpers::Blogging
- Nanoc3::Helpers::Breadcrumbs
- Nanoc3::Helpers::Capturing
- Nanoc3::Helpers::Filtering
- Nanoc3::Helpers::HTMLEscape
- Nanoc3::Helpers::LinkTo
- Nanoc3::Helpers::Rendering
- Nanoc3::Helpers::Tagging
- Nanoc3::Helpers::Text
- Nanoc3::Helpers::XMLSitemap
- Nanoc3::Item
- Nanoc3::ItemRep
- Nanoc3::Layout
- Nanoc3::NotificationCenter
- Nanoc3::PluginRegistry
- Nanoc3::PluginRegistry::PluginMethods
- Nanoc3::Rule
- Nanoc3::RuleContext
- Nanoc3::Site
- Nanoc3::StringExtensions
- Nanoc3::Tasks
- Nanoc3::Tasks::Clean
- Array
- Enumerable
- Hash
- Object
- OrderedHash
- String
- Time

No matching classes.

# Nanoc3::DirectedGraph

Represents a directed graph. It is used by the dependency tracker for storing and querying dependencies between items.

@example Creating and using a directed graph

    # Create a graph with three vertices
    graph = DirectedGraph.new(%w( a b c d ))

    # Add edges
    graph.add_edge('a', 'b')
    graph.add_edge('b', 'c')
    graph.add_edge('c', 'd')

    # Get (direct) predecessors
    graph.direct_predecessors_of('d').sort
      # => %w( c )
    graph.predecessors_of('d').sort
      # => %w( a b c )

    # Modify edges
    graph.remove_edge('a', 'b')

    # Get (direct) predecessors again
    graph.direct_predecessors_of('d').sort
      # => %w( c )
    graph.predecessors_of('d').sort
      # => %w( b c )

### Attributes

vertices[R]

The list of vertices in this graph.

@return [Array]

### Public Class Methods

new(vertices) click to toggle source

Creates a new directed graph with the given vertices.

    # File lib/nanoc3/base/directed_graph.rb, line 41
    def initialize(vertices)
      @vertices = vertices

      @from_graph = {}
      @to_graph   = {}

      @vertice_indexes = {}
      vertices.each_with_index do |v, i|
        @vertice_indexes[v] = i
      end

      invalidate_caches
    end

### Public Instance Methods

add_edge(from, to) click to toggle source

Adds an edge from the first vertex to the second vertex.

@param from Vertex where the edge should start @param to Vertex where the edge should end

@return [void]

    # File lib/nanoc3/base/directed_graph.rb, line 61
    def add_edge(from, to)
      @from_graph[from] ||= Set.new
      @from_graph[from] << to

      @to_graph[to] ||= Set.new
      @to_graph[to]  << from

      invalidate_caches
    end

delete_edges_to(to) click to toggle source

Deletes all edges going to the given vertex.

@param to Vertex to which all edges should be removed

@return [void]

    # File lib/nanoc3/base/directed_graph.rb, line 93
    def delete_edges_to(to)
      @to_graph[to] ||= Set.new
      @to_graph[to].each do |from|
        @from_graph[from].delete(to)
      end
      @to_graph.delete(to)
    end

direct_predecessors_of(to) click to toggle source

Returns the direct predecessors of the given vertex, i.e. the vertices x where there is an edge from x to the given vertex y.

@param to The vertex of which the predecessors should be calculated

@return [Array] Direct predecessors of the given vertex

    # File lib/nanoc3/base/directed_graph.rb, line 107
    def direct_predecessors_of(to)
      @to_graph[to].to_a
    end

direct_successors_of(from) click to toggle source

Returns the direct successors of the given vertex, i.e. the vertices y where there is an edge from the given vertex x to y.

@param from The vertex of which the successors should be calculated

@return [Array] Direct successors of the given vertex

    # File lib/nanoc3/base/directed_graph.rb, line 117
    def direct_successors_of(from)
      @from_graph[from].to_a
    end

edges() click to toggle source

Returns an array of tuples representing the edges. The result of this method may take a while to compute and should be cached if possible.

@return [Array] The list of all edges in this graph.

    # File lib/nanoc3/base/directed_graph.rb, line 145
    def edges
      result = []
      @vertices.each_with_index do |v, i|
        direct_successors_of(v).map { |v2| @vertice_indexes[v2] }.each do |i2|
          result << [ i, i2 ]
        end
      end
      result
    end

predecessors_of(to) click to toggle source

Returns the predecessors of the given vertex, i.e. the vertices x for which there is a path from x to the given vertex y.

@param to The vertex of which the predecessors should be calculated

@return [Array] Predecessors of the given vertex

    # File lib/nanoc3/base/directed_graph.rb, line 127
    def predecessors_of(to)
      @predecessors[to] ||= recursively_find_vertices(to, :direct_predecessors_of)
    end

remove_edge(from, to) click to toggle source

Removes the edge from the first vertex to the second vertex. If the edge does not exist, nothing is done.

@param from Start vertex of the edge @param to End vertex of the edge

@return [void]

    # File lib/nanoc3/base/directed_graph.rb, line 78
    def remove_edge(from, to)
      @from_graph[from] ||= Set.new
      @from_graph[from].delete(to)

      @to_graph[to] ||= Set.new
      @to_graph[to].delete(from)

      invalidate_caches
    end

successors_of(from) click to toggle source

Returns the successors of the given vertex, i.e. the vertices y for which there is a path from the given vertex x to y.

@param from The vertex of which the successors should be calculated

@return [Array] Successors of the given vertex

    # File lib/nanoc3/base/directed_graph.rb, line 137
    def successors_of(from)
      @successors[from] ||= recursively_find_vertices(from, :direct_successors_of)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.