Sha256: 13ca03cb6005928d5f0e1f4d41fa5d4c14d24b21b1d0a8dfaeac93d3d2723804

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

# = combination.rb: Class for handling variable state combinations
# Copyright (C) 2005-2007  Carl Youngblood mailto:carl@youngbloods.org
#
# Takes an array of arrays and iterates over all combinations of sub-elements.
# For example:
# 
#  c = Sbn::Combination.new([[1, 2], [6, 7, 8]])
#  c.each {|comb| p comb }
#
# Will produce:
#
#  [1, 6]
#  [1, 7]
#  [1, 8]
#  [2, 6]
#  [2, 7]
#  [2, 8]

module Sbn
  class Combination # :nodoc:
    include Enumerable
  
    def initialize(arr)
      @arr = arr
      @current = Array.new(arr.size, 0)
    end
  
    def each
      iterations = @arr.inject(1) {|product, element| product * element.size } - 1
      yield current
      iterations.times { yield self.next_combination }
    end
  
    def <=>(other)
      @current <=> other.current
    end

    def first
      @current.fill 0
    end
  
    def last
      @current.size.times {|i| @current[i] = @arr[i].size - 1 }
    end
  
    def current
      returnval = []
      @current.size.times {|i| returnval[i] = @arr[i][@current[i]] }
      returnval
    end 
  
    def next_combination
      i = @current.size - 1
      @current.reverse.each do |e|
        if e == @arr[i].size - 1
          @current[i] = 0
        else
          @current[i] += 1
          break
        end
        i -= 1
      end
      current
    end
  
    def prev_combination
      i = @current.size - 1
      @current.reverse.each do |e|
        if e == 0
          @current[i] = @arr[i].size - 1
        else
          @current[i] -= 1
          break
        end
        i -= 1
      end
      current
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sbn-0.9.1 lib/sbn/combination.rb