Class: Dicey::DistributionCalculators::AutoSelector
- Inherits:
-
Object
- Object
- Dicey::DistributionCalculators::AutoSelector
- Defined in:
- lib/dicey/distribution_calculators/auto_selector.rb
Overview
Tool to automatically select a calculator for a given set of dice.
Calculator is guaranteed to be compatible, with a strong chance of being the most performant.
Constant Summary collapse
- AVAILABLE_CALCULATORS =
Calculators to consider when selecting a match.
[ Trivial.new, Binomial.new, PolynomialConvolution.new, MultinomialCoefficients.new, Iterative.new, ].freeze
- INSTANCE =
Instance to be used through call.
new.freeze
Class Method Summary collapse
-
.call(dice) ⇒ BaseCalculator?
Determine best (or adequate) calculator for a given set of dice based on heuristics from the list of available calculators.
Instance Method Summary collapse
-
#call(dice) ⇒ BaseCalculator?
Determine best (or adequate) calculator for a given set of dice based on heuristics from the list of available calculators.
-
#initialize(calculators = AVAILABLE_CALCULATORS) ⇒ AutoSelector
constructor
A new instance of AutoSelector.
Constructor Details
#initialize(calculators = AVAILABLE_CALCULATORS) ⇒ AutoSelector
Returns a new instance of AutoSelector.
52 53 54 |
# File 'lib/dicey/distribution_calculators/auto_selector.rb', line 52 def initialize(calculators = AVAILABLE_CALCULATORS) @calculators = calculators end |
Class Method Details
.call(dice) ⇒ BaseCalculator?
Determine best (or adequate) calculator for a given set of dice based on heuristics from the list of available calculators.
Uses shared INSTANCE for calls.
46 47 48 |
# File 'lib/dicey/distribution_calculators/auto_selector.rb', line 46 def self.call(dice) INSTANCE.call(dice) end |
Instance Method Details
#call(dice) ⇒ BaseCalculator?
Determine best (or adequate) calculator for a given set of dice based on heuristics from the list of available calculators.
65 66 67 68 69 70 |
# File 'lib/dicey/distribution_calculators/auto_selector.rb', line 65 def call(dice) compatible = @calculators.select { _1.valid_for?(dice) } return if compatible.empty? compatible.min_by { _1.heuristic_complexity(dice) } end |