module TournamentSystem::Algorithm::Util

This module provides utility functions for helping implement other algorithms.

Public Instance Methods

all_min_by(array) { |element| ... } click to toggle source

Collect all values in an array with a minimum value.

@param array [Array<element>] @yieldparam element an element of the array @yieldreturn [#<, #==] some value to find the minimum of @return [Array<element>] all elements with the minimum value

# File lib/tournament_system/algorithm/util.rb, line 83
def all_min_by(array)
  min_elements = []
  min_value = nil

  array.each do |element|
    value = yield element

    if !min_value || value < min_value
      min_elements = [element]
      min_value = value
    elsif value == min_value
      min_elements << element
    end
  end

  min_elements
end
padd_teams(teams) click to toggle source

@deprecated Please use {#padd_teams_even} instead.

# File lib/tournament_system/algorithm/util.rb, line 9
def padd_teams(teams)
  message = 'NOTE: padd_teams is now deprecated in favour of padd_teams_even. '\
            'It will be removed in the next major version.'\
            "Util.padd_teams called from #{Gem.location_of_caller.join(':')}"
  warn message unless Gem::Deprecate.skip

  padd_teams_even(teams)
end
padd_teams_even(teams) click to toggle source

Padd an array of teams to be even.

@param teams [Array<team>] @return [Array<team, nil>]

# File lib/tournament_system/algorithm/util.rb, line 22
def padd_teams_even(teams)
  if teams.length.odd?
    teams + [nil]
  else
    teams
  end
end
padd_teams_pow2(teams) click to toggle source

Padd an array of teams to the next power of 2.

@param teams [Array<team>] @return [Array<team, nil>]

# File lib/tournament_system/algorithm/util.rb, line 58
def padd_teams_pow2(teams)
  required = padded_teams_pow2_count(teams.length)

  Array.new(required) { |index| teams[index] }
end
padded_teams_count(teams_count) click to toggle source

@deprecated Please use {#padded_teams_even_count}

# File lib/tournament_system/algorithm/util.rb, line 31
def padded_teams_count(teams_count)
  message = 'Node: padded_teams_count is now deprecated in favour of padded_teams_even_count. '\
            'It will be removed in the next major version.'\
            "Util.padded_teams_count called from #{Gem.location_of_caller.join(':')}"
  warn message unless Gem::Deprecate.skip

  padded_teams_even_count(teams_count)
end
padded_teams_even_count(teams_count) click to toggle source

Padd the count of teams to be even.

@example

padded_teams_even_count(teams.length) == padd_teams_even(teams).length

@param teams_count [Integer] the number of teams @return [Integer]

# File lib/tournament_system/algorithm/util.rb, line 47
def padded_teams_even_count(teams_count)
  (teams_count / 2.0).ceil * 2
end
padded_teams_pow2_count(teams_count) click to toggle source

Padd the count of teams to be a power of 2.

@example

padded_teams_pow2_count(teams.length) == padd_teams_pow2(teams).length

@param teams_count [Integer] the number of teams @return [Integer]

# File lib/tournament_system/algorithm/util.rb, line 71
def padded_teams_pow2_count(teams_count)
  2**Math.log2(teams_count).ceil
end