Sha256: ca92e9de439be27a437d37729aaa1048fae53c1ec11da1e216431b80720b1b53

Contents?: true

Size: 1.5 KB

Versions: 5

Compression:

Stored size: 1.5 KB

Contents

module Solve
  class Solver
    # @author Andrew Garson <andrew.garson@gmail.com>
    # @author Jamie Winsor <jamie@vialstudios.com>
    class VariableTable
      attr_reader :rows

      def initialize
        @rows = Array.new
      end

      # @param [String] artifact
      # @param [String] source
      def add(artifact, source)
        row = rows.detect { |row| row.artifact == artifact }
        if row.nil?
          @rows << VariableRow.new(artifact, source)
        else
          row.add_source(source)
        end
      end

      def first_unbound
        @rows.detect { |row| row.bound? == false }
      end

      # @param [String] artifact
      def find_artifact(artifact)
        @rows.detect { |row| row.artifact == artifact }
      end

      # @param [String] source
      def remove_all_with_only_this_source!(source)
        with_only_this_source, others = @rows.partition { |row| row.sources == [source] }
        @rows = others
        with_only_this_source
      end

      # @param [String] source
      def all_from_source(source)
        @rows.select { |row| row.sources.include?(source) }
      end

      # @param [String] artifact
      def before(artifact)
        artifact_index = @rows.index { |row| row.artifact == artifact }
        (artifact_index == 0) ? nil : @rows[artifact_index - 1]
      end

      # @param [String] artifact
      def all_after(artifact)
        artifact_index = @rows.index { |row| row.artifact == artifact }
        @rows[(artifact_index+1)..-1]
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
solve-0.4.2 lib/solve/solver/variable_table.rb
solve-0.4.1 lib/solve/solver/variable_table.rb
solve-0.4.0 lib/solve/solver/variable_table.rb
solve-0.4.0.rc1 lib/solve/solver/variable_table.rb
solve-0.3.1 lib/solve/solver/variable_table.rb