Class: CartesianIterator
- Includes:
- GridSearch
- Defined in:
- lib/cartesian_iterator.rb
Instance Method Summary collapse
- #dup ⇒ Object
- #each {|elems| ... } ⇒ Object
- #equal(other) ⇒ Object (also: #==)
-
#initialize(foo, bar) ⇒ CartesianIterator
constructor
A new instance of CartesianIterator.
- #left_product(other) ⇒ Object
- #left_product!(other) ⇒ Object
- #product(other) ⇒ Object (also: #right_product, #x)
- #product!(other) ⇒ Object (also: #right_product!, #x!)
- #to_a ⇒ Object (also: #to_ary)
Methods included from GridSearch
Constructor Details
#initialize(foo, bar) ⇒ CartesianIterator
Returns a new instance of CartesianIterator.
7 8 9 10 11 12 |
# File 'lib/cartesian_iterator.rb', line 7 def initialize(foo, ) @lists = [] @tot_iter = 1 product!(foo) product!() end |
Instance Method Details
#dup ⇒ Object
14 15 16 |
# File 'lib/cartesian_iterator.rb', line 14 def dup Marshal.load(Marshal.dump(self)) end |
#each {|elems| ... } ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cartesian_iterator.rb', line 52 def each return false if @tot_iter < 1 elems = [] for list in @lists elems << list.restart_and_raw_next end yield *elems last_list_index = @lists.size-1 n = last_list_index loop do if elems[n] = @lists[n].raw_next yield *elems n = last_list_index next elsif n > 0 elems[n] = @lists[n].restart_and_raw_next n -= 1 else return true end end end |
#equal(other) ⇒ Object Also known as: ==
18 19 20 21 22 23 |
# File 'lib/cartesian_iterator.rb', line 18 def equal(other) self.instance_variables.each do |var_name| return false if self.instance_variable_get(var_name) != other.instance_variable_get(var_name) end true end |
#left_product(other) ⇒ Object
47 48 49 50 |
# File 'lib/cartesian_iterator.rb', line 47 def left_product(other) (result = self.dup).left_product!(other) result end |
#left_product!(other) ⇒ Object
34 35 36 37 38 |
# File 'lib/cartesian_iterator.rb', line 34 def left_product!(other) @lists.unshift other.to_a.dup @tot_iter *= @lists[-1].size self end |
#product(other) ⇒ Object Also known as: right_product, x
40 41 42 43 |
# File 'lib/cartesian_iterator.rb', line 40 def product(other) (result = self.dup).product!(other) result end |
#product!(other) ⇒ Object Also known as: right_product!, x!
26 27 28 29 30 |
# File 'lib/cartesian_iterator.rb', line 26 def product!(other) @lists << other.to_a.dup @tot_iter *= @lists[-1].size self end |
#to_a ⇒ Object Also known as: to_ary
77 78 79 80 81 |
# File 'lib/cartesian_iterator.rb', line 77 def to_a array = [] self.each {|*element| array << element } array end |