Class: Dicey::DistributionCalculators::AutoSelector

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(calculators = AVAILABLE_CALCULATORS) ⇒ AutoSelector

Returns a new instance of AutoSelector.

Parameters:

  • calculators (Array<BaseCalculator>) (defaults to: AVAILABLE_CALCULATORS)

    calculators which this instance will consider



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.

Parameters:

Returns:



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.

Parameters:

Returns:



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