Module: TournamentSystem::Algorithm::Util
Overview
This module provides utility functions for helping implement other algorithms.
Instance Method Summary collapse
-
#all_min_by(array) {|element| ... } ⇒ Array<element>
Collect all values in an array with a minimum value.
-
#padd_teams(teams) ⇒ Object
deprecated
Deprecated.
Please use #padd_teams_even instead.
-
#padd_teams_even(teams) ⇒ Array<team, nil>
Padd an array of teams to be even.
-
#padd_teams_pow2(teams) ⇒ Array<team, nil>
Padd an array of teams to the next power of 2.
-
#padded_teams_count(teams_count) ⇒ Object
deprecated
Deprecated.
Please use #padded_teams_even_count
-
#padded_teams_even_count(teams_count) ⇒ Integer
Padd the count of teams to be even.
-
#padded_teams_pow2_count(teams_count) ⇒ Integer
Padd the count of teams to be a power of 2.
Instance Method Details
#all_min_by(array) {|element| ... } ⇒ Array<element>
Collect all values in an array with a 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) = '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 unless Gem::Deprecate.skip padd_teams_even(teams) end |
#padd_teams_even(teams) ⇒ Array<team, nil>
Padd an array of teams to be even.
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.
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.
Please use #padded_teams_even_count
31 32 33 34 35 36 37 38 |
# File 'lib/tournament_system/algorithm/util.rb', line 31 def padded_teams_count(teams_count) = '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 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.
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.
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 |