Skip to content

Commit

Permalink
Consolidated #padd_teams methods into Algorithm::Util and deprecated …
Browse files Browse the repository at this point in the history
…the other usages.
  • Loading branch information
BenjaminSchaaf committed Jun 3, 2018
1 parent f0668b5 commit 3f5bc58
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 35 deletions.
13 changes: 6 additions & 7 deletions lib/tournament_system/algorithm/single_bracket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ def guess_round(teams_count, matches_count)
round.to_i
end

# Padd an array of teams to the next highest power of 2.
#
# @param teams [Array<team>]
# @return [Array<team, nil>]
# @deprecated Please use {Util.padd_teams_pow2} instead.
def padd_teams(teams)
required = max_teams(total_rounds(teams.length))
message = 'NOTE: padd_teams is now deprecated in favour of Util.padd_teams_even.'\
'It will be removed in the next major version'\
"SingleBracket.padd_teams called from #{Gem.location_of_caller.join(':')}"
warn message unless Gem::Deprecate.skip

# Insert the padding at the bottom to give top teams byes first
Array.new(required) { |index| teams[index] }
Util.padd_teams_pow2(teams)
end

# Seed teams for a single bracket tournament.
Expand Down
26 changes: 25 additions & 1 deletion lib/tournament_system/algorithm/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,42 @@ module Algorithm
module Util
extend self

# @deprecated Please use {#padd_teams_even} instead.
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 an array of teams to be even.
#
# @param teams [Array<team>]
# @return [Array<team, nil>]
def padd_teams(teams)
def padd_teams_even(teams)
if teams.length.odd?
teams + [nil]
else
teams
end
end

# pow2 is not uncommunicative
# :reek:UncommunicativeMethodName

# Padd an array of teams to the next power of 2.
#
# @param teams [Array<team>]
# @return [Array<team, nil>]
def padd_teams_pow2(teams)
# Get the next power of 2
required = 2**Math.log2(teams.length).ceil

Array.new(required) { |index| teams[index] }
end

# Padd the count of teams to be even.
#
# @example
Expand Down
2 changes: 1 addition & 1 deletion lib/tournament_system/round_robin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module RoundRobin
def generate(driver, options = {})
round = options[:round] || guess_round(driver)

teams = Algorithm::Util.padd_teams(driver.seeded_teams)
teams = Algorithm::Util.padd_teams_even(driver.seeded_teams)

matches = Algorithm::RoundRobin.round_robin_pairing(teams, round)

Expand Down
2 changes: 1 addition & 1 deletion lib/tournament_system/single_elimination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def generate(driver, _options = {})
round = guess_round(driver)

teams = if driver.matches.empty?
padded = Algorithm::SingleBracket.padd_teams driver.seeded_teams
padded = Algorithm::Util.padd_teams_pow2 driver.seeded_teams
Algorithm::SingleBracket.seed padded
else
last_matches = previous_round_matches driver, round
Expand Down
16 changes: 0 additions & 16 deletions spec/tournament/algorithm/single_bracket_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,6 @@ def gen_teams(num)
end
end

describe '#padd_teams' do
it 'works' do
expect(described_class.padd_teams(gen_teams(2))).to eq([1, 2])
expect(described_class.padd_teams(gen_teams(3))).to eq([1, 2, 3, nil])
expect(described_class.padd_teams(gen_teams(4))).to eq([1, 2, 3, 4])
expect(described_class.padd_teams(gen_teams(5)))
.to eq([1, 2, 3, 4, 5, nil, nil, nil])
expect(described_class.padd_teams(gen_teams(6)))
.to eq([1, 2, 3, 4, 5, 6, nil, nil])
expect(described_class.padd_teams(gen_teams(7)))
.to eq([1, 2, 3, 4, 5, 6, 7, nil])
expect(described_class.padd_teams(gen_teams(8)))
.to eq([1, 2, 3, 4, 5, 6, 7, 8])
end
end

describe '#seed' do
it 'works for powers of 2' do
expect(described_class.seed(gen_teams(2))).to eq([1, 2])
Expand Down
34 changes: 25 additions & 9 deletions spec/tournament/algorithm/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,45 @@ def gen_teams(num)
(1..num).to_a.freeze
end

describe '#padd_teams' do
describe '#padd_teams_even' do
it 'works for even teams' do
expect(described_class.padd_teams(gen_teams(2))).to eq((1..2).to_a)
expect(described_class.padd_teams(gen_teams(4))).to eq((1..4).to_a)
expect(described_class.padd_teams(gen_teams(6))).to eq((1..6).to_a)
expect(described_class.padd_teams(gen_teams(8))).to eq((1..8).to_a)
expect(described_class.padd_teams_even(gen_teams(2))).to eq((1..2).to_a)
expect(described_class.padd_teams_even(gen_teams(4))).to eq((1..4).to_a)
expect(described_class.padd_teams_even(gen_teams(6))).to eq((1..6).to_a)
expect(described_class.padd_teams_even(gen_teams(8))).to eq((1..8).to_a)
end

it 'works for odd teams' do
expect(described_class.padd_teams(gen_teams(3)))
expect(described_class.padd_teams_even(gen_teams(3)))
.to eq((1..3).to_a + [nil])

expect(described_class.padd_teams(gen_teams(5)))
expect(described_class.padd_teams_even(gen_teams(5)))
.to eq((1..5).to_a + [nil])

expect(described_class.padd_teams(gen_teams(7)))
expect(described_class.padd_teams_even(gen_teams(7)))
.to eq((1..7).to_a + [nil])

expect(described_class.padd_teams(gen_teams(9)))
expect(described_class.padd_teams_even(gen_teams(9)))
.to eq((1..9).to_a + [nil])
end
end

describe '#padd_teams_pow2' do
it 'works' do
expect(described_class.padd_teams_pow2(gen_teams(2))).to eq([1, 2])
expect(described_class.padd_teams_pow2(gen_teams(3))).to eq([1, 2, 3, nil])
expect(described_class.padd_teams_pow2(gen_teams(4))).to eq([1, 2, 3, 4])
expect(described_class.padd_teams_pow2(gen_teams(5)))
.to eq([1, 2, 3, 4, 5, nil, nil, nil])
expect(described_class.padd_teams_pow2(gen_teams(6)))
.to eq([1, 2, 3, 4, 5, 6, nil, nil])
expect(described_class.padd_teams_pow2(gen_teams(7)))
.to eq([1, 2, 3, 4, 5, 6, 7, nil])
expect(described_class.padd_teams_pow2(gen_teams(8)))
.to eq([1, 2, 3, 4, 5, 6, 7, 8])
end
end

describe '#padded_teams_count' do
it 'works for even numbers' do
expect(described_class.padded_teams_count(2)).to eq(2)
Expand Down

0 comments on commit 3f5bc58

Please sign in to comment.