Method: BracketGraph::Graph#initialize

Defined in:
lib/bracket_graph/graph.rb

#initialize(root_or_size) ⇒ Graph

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`.

Parameters:

  • size (Fixnum|Seat)

    The number of orphan seats to generate, or the root node

Raises:

  • (ArgumentError)

    if size is not a power of 2



13
14
15
16
17
18
19
20
21
# 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