class BracketGraph::Graph

Attributes

root[R]
seats[R]
starting_seats[R]

Public Class Methods

new(root_or_size) click to toggle source

Builds a new graph. The graph will be composed by a root seat and a match with two seats pointing to the root seat Each seat will then follows the same template (seat -> match -> 2 seats) until the generated last level seats (the starting seats) is equal to `size`.

@param size [Fixnum|Seat] The number of orphan seats to generate, or the root node @raise [ArgumentError] if size is not a power of 2

# File lib/bracket_graph/graph.rb, line 13
def initialize root_or_size
  if root_or_size.is_a? Seat
    @root = root_or_size
    update_references
  else
    raise ArgumentError, 'the given size is not a power of 2' if Math.log2(root_or_size) % 1 != 0
    build_tree root_or_size
  end
end

Public Instance Methods

[](position) click to toggle source
# File lib/bracket_graph/graph.rb, line 23
def [](position)
  seats.detect { |s| s.position == position }
end
as_json(*attrs) click to toggle source
# File lib/bracket_graph/graph.rb, line 45
def as_json *attrs
  @root.as_json *attrs
end
seed(teams, shuffle: false) click to toggle source

Fills the starting seats with the given `teams`

@param teams [Array] Teams to place as payload in the starting seats @param shuffle [true, false] Indicates if teams shoud be shuffled @raise [ArgumentError] if `teams.count` is greater then `#size`

# File lib/bracket_graph/graph.rb, line 37
def seed teams, shuffle: false
  raise ArgumentError, "Only a maximum of #{size} teams is allowed" if teams.size > size
  slots = TeamSeeder.new(teams, size, shuffle: shuffle).slots
  starting_seats.sort_by(&:position).each do |seat|
    seat.payload = slots.shift
  end
end
size() click to toggle source

Number of the starting seats

# File lib/bracket_graph/graph.rb, line 28
def size
  starting_seats.size
end

Private Instance Methods

build_tree(size) click to toggle source
# File lib/bracket_graph/graph.rb, line 51
def build_tree size
  build_tree! size
  update_references
end
build_tree!(size) click to toggle source
# File lib/bracket_graph/graph.rb, line 56
def build_tree! size
  @root = Seat.new size, round: Math.log2(size).to_i
  # Math.log2(size) indicates the graph depth
  Math.log2(size).to_i.times.inject [root] do |seats|
    seats.inject [] do |memo, seat|
      memo.concat create_children_of seat
    end
  end

end
create_children_of(seat) click to toggle source

Builds a match as a source of this seat @raise [NoMethodError] if a source match has already been set

# File lib/bracket_graph/graph.rb, line 79
def create_children_of seat
  raise NoMethodError, 'children already built' if seat.from.any?
  parent_position = seat.to ? seat.to.position : 0
  relative_position_halved = ((seat.position - parent_position) / 2).abs
  seat.from.concat [
    Seat.new(seat.position - relative_position_halved, to: seat),
    Seat.new(seat.position + relative_position_halved, to: seat)
  ]
end
update_references() click to toggle source
# File lib/bracket_graph/graph.rb, line 67
def update_references
  @seats = [root]
  @starting_seats = []
  nodes = [root]
  while nodes.any?
    @seats.concat nodes = nodes.map(&:from).flatten
  end
  @starting_seats = @seats.select { |s| s.from.empty? }
end