123456789_123456789_123456789_123456789_123456789_

Class: Bundler::PubGrub::PartialSolution

Relationships & Source Files
Inherits: Object
Defined in: lib/bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newPartialSolution

[ GitHub ]

  
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb', line 8

def initialize
  reset!

  @attempted_solutions = 1
  @backtracking = false
end

Instance Attribute Details

#assignments (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb', line 5

attr_reader :assignments, :decisions

#attempted_solutions (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb', line 6

attr_reader :attempted_solutions

#decisions (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb', line 5

attr_reader :assignments, :decisions

Instance Method Details

#add_assignment(assignment) (private)

[ GitHub ]

  
# File 'lib/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

#backtrack(previous_level)

[ GitHub ]

  
# File 'lib/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)

[ GitHub ]

  
# File 'lib/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

[ GitHub ]

  
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb', line 15

def decision_level
  @decisions.length
end

#derive(term, cause)

[ GitHub ]

  
# File 'lib/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)

[ GitHub ]

  
# File 'lib/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

#reset! (private)

[ GitHub ]

  
# File 'lib/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

#satisfier(term)

[ GitHub ]

  
# File 'lib/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) ⇒ Boolean

[ GitHub ]

  
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/partial_solution.rb', line 27

def satisfies?(term)
  relation(term) == :subset
end

#unsatisfied

A list of unsatisfied terms

[ GitHub ]

  
# File 'lib/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