Class: CartesianIterator
- Inherits:
-
Object
- Object
- CartesianIterator
- Includes:
- Enumerable, GridSearch
- Defined in:
- lib/cartesian_iterator.rb,
lib/cartesian/grid_search.rb
Instance Method Summary collapse
- #dup ⇒ Object
- #each ⇒ 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!)
Methods included from GridSearch
Constructor Details
#initialize(foo, bar) ⇒ CartesianIterator
Returns a new instance of CartesianIterator.
3 4 5 6 7 8 |
# File 'lib/cartesian_iterator.rb', line 3 def initialize(foo, ) @lists = [] @tot_iter = 1 product!(foo) product!() end |
Instance Method Details
#dup ⇒ Object
10 11 12 |
# File 'lib/cartesian_iterator.rb', line 10 def dup Marshal.load(Marshal.dump(self)) end |
#each ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/cartesian_iterator.rb', line 48 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 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 |
#equal(other) ⇒ Object Also known as: ==
14 15 16 17 18 19 |
# File 'lib/cartesian_iterator.rb', line 14 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
43 44 45 46 |
# File 'lib/cartesian_iterator.rb', line 43 def left_product(other) (result = self.dup).left_product!(other) result end |
#left_product!(other) ⇒ Object
30 31 32 33 34 |
# File 'lib/cartesian_iterator.rb', line 30 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
36 37 38 39 |
# File 'lib/cartesian_iterator.rb', line 36 def product(other) (result = self.dup).product!(other) result end |
#product!(other) ⇒ Object Also known as: right_product!, x!
22 23 24 25 26 |
# File 'lib/cartesian_iterator.rb', line 22 def product!(other) @lists << other.to_a.dup @tot_iter *= @lists[-1].size self end |