Module: TournamentSystem::Algorithm::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/tournament_system/algorithm/util.rb

Overview

This module provides utility functions for helping implement other algorithms.

Instance Method Summary collapse

Instance Method Details

#all_min_by(array) {|element| ... } ⇒ Array<element>

Collect all values in an array with a minimum value.

Parameters:

  • array (Array<element>)

Yield Parameters:

  • element

    an element of the array

Yield Returns:

  • (#<, #==)

    some value to find the minimum of

Returns:

  • (Array<element>)

    all elements with the minimum value



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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) ⇒ Object

Deprecated.

Please use #padd_teams_even instead.



9
10
11
12
13
14
15
16
# 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) ⇒ Array<team, nil>

Padd an array of teams to be even.

Parameters:

  • teams (Array<team>)

Returns:

  • (Array<team, nil>)


22
23
24
25
26
27
28
# 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) ⇒ Array<team, nil>

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

Parameters:

  • teams (Array<team>)

Returns:

  • (Array<team, nil>)


58
59
60
61
62
# 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) ⇒ Object

Deprecated.


31
32
33
34
35
36
37
38
# 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) ⇒ Integer

Padd the count of teams to be even.

Examples:

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

Parameters:

  • teams_count (Integer)

    the number of teams

Returns:

  • (Integer)


47
48
49
# 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) ⇒ Integer

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

Examples:

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

Parameters:

  • teams_count (Integer)

    the number of teams

Returns:

  • (Integer)


71
72
73
# File 'lib/tournament_system/algorithm/util.rb', line 71

def padded_teams_pow2_count(teams_count)
  2**Math.log2(teams_count).ceil
end