class Bundler::PubGrub::PartialSolution

Attributes

assignments[R]
attempted_solutions[R]
decisions[R]

Public Class Methods

new() click to toggle source
# File bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb, line 8
def initialize
  reset!

  @attempted_solutions = 1
  @backtracking = false
end

Public Instance Methods

backtrack(previous_level) click to toggle source
# File bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb, line 62
def backtrack(previous_level)
  @backtracking = true

  new_assignments = assignments.select do |assignment|
    assignment.decision_level <= previous_level
  end

  new_decisions = Hash[decisions.first(previous_level)]

  reset!

  @decisions = new_decisions

  new_assignments.each do |assignment|
    add_assignment(assignment)
  end
end
decide(package, version) click to toggle source
# File bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb, line 53
def decide(package, version)
  @attempted_solutions += 1 if @backtracking
  @backtracking = false;

  decisions[package] = version
  assignment = Assignment.decision(package, version, decision_level, assignments.length)
  add_assignment(assignment)
end
decision_level() click to toggle source
# File bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb, line 15
def decision_level
  @decisions.length
end
derive(term, cause) click to toggle source
# File bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb, line 31
def derive(term, cause)
  add_assignment(Assignment.new(term, cause, decision_level, assignments.length))
end
relation(term) click to toggle source
# File bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb, line 19
def relation(term)
  package = term.package
  return :overlap if !@terms.key?(package)

  @relation_cache[package][term] ||=
    @terms[package].relation(term)
end
satisfier(term) click to toggle source
# File bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb, line 35
def satisfier(term)
  assignment =
    @assignments_by[term.package].bsearch do |assignment_by|
      @cumulative_assignments[assignment_by].satisfies?(term)
    end

  assignment || raise("#{term} unsatisfied")
end
satisfies?(term) click to toggle source
# File bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb, line 27
def satisfies?(term)
  relation(term) == :subset
end
unsatisfied() click to toggle source

A list of unsatisfied terms

# File bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb, line 45
def unsatisfied
  @required.keys.reject do |package|
    @decisions.key?(package)
  end.map do |package|
    @terms[package]
  end
end

Private Instance Methods

add_assignment(assignment) click to toggle source
# File bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb, line 101
def add_assignment(assignment)
  term = assignment.term
  package = term.package

  @assignments << assignment
  @assignments_by[package] << assignment

  @required[package] = true if term.positive?

  if @terms.key?(package)
    old_term = @terms[package]
    @terms[package] = old_term.intersect(term)
  else
    @terms[package] = term
  end
  @relation_cache[package].clear

  @cumulative_assignments[assignment] = @terms[package]
end
reset!() click to toggle source
# File bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb, line 82
def reset!
  # { Array<Assignment> }
  @assignments = []

  # { Package => Array<Assignment> }
  @assignments_by = Hash.new { |h,k| h[k] = [] }
  @cumulative_assignments = {}.compare_by_identity

  # { Package => Package::Version }
  @decisions = {}

  # { Package => Term }
  @terms = {}
  @relation_cache = Hash.new { |h,k| h[k] = {} }

  # { Package => Boolean }
  @required = {}
end