class SetPartition::Generator

Attributes

current[R]

Public Class Methods

new(length) click to toggle source
# File lib/set_partition/generator.rb, line 5
def initialize length
  @length = length
  initialize_first
  initialize_last
  start
end

Public Instance Methods

end() click to toggle source
# File lib/set_partition/generator.rb, line 18
def end
  @current = @last.dup
  @maximum = @last.dup
end
length() click to toggle source
# File lib/set_partition/generator.rb, line 36
def length
  partition_length
end
next() click to toggle source
# File lib/set_partition/generator.rb, line 23
def next
  return if @current == @last
  next_partition
  @current
end
prev() click to toggle source
# File lib/set_partition/generator.rb, line 29
def prev
  return if @current == @first
  previous_partition
  @current
end
Also aliased as: previous
previous()
Alias for: prev
rewind()
Alias for: start
start() click to toggle source
# File lib/set_partition/generator.rb, line 12
def start
  @current = @first.dup
  @maximum = @first.dup
end
Also aliased as: rewind

Private Instance Methods

initialize_first() click to toggle source
# File lib/set_partition/generator.rb, line 41
def initialize_first
  @first = [0] * @length
end
initialize_last() click to toggle source
# File lib/set_partition/generator.rb, line 45
def initialize_last
  @last = 0.upto(@length - 1).to_a
end
next_partition() click to toggle source
# File lib/set_partition/generator.rb, line 49
def next_partition
  (@length - 1).downto(1) do |i|
    if @current[i] <= @maximum[i - 1]
      @current[i] = @current[i] + 1
      @maximum[i] = [@maximum[i], @current[i]].max

      (i + 1).upto(@length - 1) do |j|
        @current[j] = @current[0]
        @maximum[j] = @maximum[i]
      end
      return
    end
  end
end
partition_length() click to toggle source
# File lib/set_partition/generator.rb, line 78
def partition_length
  @maximum[@length - 1] - @maximum[0] + 1
end
previous_partition() click to toggle source
# File lib/set_partition/generator.rb, line 64
def previous_partition
  (@length - 1).downto(1) do |i|
    if @current[i] > @current[0]
      @current[i] = @current[i] - 1
      @maximum[i] = @maximum[i] - 1

      (i + 1).upto(@length - 1) do |j|
        @current[j] = @maximum[j] = @maximum[i] + j - i
      end
      return
    end
  end
end