Class: Dicey::SumFrequencyCalculators::AutoSelector

Inherits:
Object
  • Object
show all
Defined in:
lib/dicey/sum_frequency_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.

[
  KroneckerSubstitution.new,
  MultinomialCoefficients.new,
  BruteForce.new,
].freeze

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



24
25
26
# File 'lib/dicey/sum_frequency_calculators/auto_selector.rb', line 24

def initialize(calculators = AVAILABLE_CALCULATORS)
  @calculators = calculators
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:



33
34
35
36
37
38
# File 'lib/dicey/sum_frequency_calculators/auto_selector.rb', line 33

def call(dice)
  compatible = @calculators.select { _1.valid_for?(dice) }
  return if compatible.empty?

  compatible.min_by { _1.heuristic_complexity(dice) }
end