Method: CartesianIterator#each

Defined in:
lib/cartesian_iterator.rb

#eachObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cartesian_iterator.rb', line 54

def each
  return false if @tot_iter < 1

  elems = []
  for list in @lists
    elems << list.restart_and_raw_next
  end
  if RUBY_VERSION <= '1.9.1'; yield(*elems.map {|x| x }); else; yield(*elems); end # Yeah, v.map{|x|x} should be equal to v, but strangely it is NOT in Ruby versions prior to 1.9.2.

  last_list_index = @lists.size-1
  n = last_list_index
  loop do
    if !@lists[n].done?
      elems[n] = @lists[n].raw_next
      if RUBY_VERSION <= '1.9.1'; yield(*elems.map {|x| x }); else; yield(*elems); end # See previous comment.
      n = last_list_index
      next
    elsif n > 0
      elems[n] = @lists[n].restart_and_raw_next
      n -= 1
    else
      return true
    end
  end
end