class SetPartition::Array

Public Class Methods

new(array, partition_size=nil) click to toggle source
# File lib/set_partition/array.rb, line 3
def initialize array, partition_size=nil
  @array     = array
  @generator = if partition_size
                 FixedGenerator.new array.size, partition_size
               else
                 Generator.new array.size
               end
end

Public Instance Methods

current() click to toggle source
# File lib/set_partition/array.rb, line 40
def current
  reify @generator.current
end
end() click to toggle source
# File lib/set_partition/array.rb, line 16
def end
  reify @generator.end
end
next() click to toggle source
# File lib/set_partition/array.rb, line 20
def next
  reify @generator.next
end
partition_size() click to toggle source
# File lib/set_partition/array.rb, line 28
def partition_size
  @generator.size
end
prev() click to toggle source
# File lib/set_partition/array.rb, line 24
def prev
  reify @generator.prev
end
start() click to toggle source
# File lib/set_partition/array.rb, line 12
def start
  reify @generator.start
end
to_a() click to toggle source
# File lib/set_partition/array.rb, line 32
def to_a
  [reify(@generator.start)].tap do |array|
    while @generator.next
      array.push reify @generator.current
    end
  end
end

Private Instance Methods

reify(partition) click to toggle source
# File lib/set_partition/array.rb, line 45
def reify partition
  return unless partition

  [].tap do |array|
    partition.each_with_index do |partition_index, i|
      array[partition_index] ||= []
      array[partition_index].push @array[i]
    end
  end
end