C0 code coverage information
Generated on Tue Nov 04 04:48:28 -0200 2008 with
rcov 0.8.1.2
Code reported as executed by Ruby looks like
this... and this: this line is also marked as
covered. Lines considered as run by rcov, but
not reported by Ruby, look like this, and
this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not
executed.
1 #
2 # The CartesianProduct
module provide methods for the calculation 3 # of the cartesian producted between two enumberable
objects. 4 #
5 # It can also be
easily mixed in into any enumberable class, 6 # i.e. any class with Enumerable
module mixed in. 7 #
Notice that the names of the methods for mixin are different. 8 # 9 # Module: 10 # Cartesian::product(foo, bar) 11 # 12 # Mixin: 13 # foo.cartesian( bar )
14 # 15 # The module is automatically
mixed in Array class.
16 # 17 # == Author
18 # Adriano MITRE
<adriano.mitre@gmail.com> 19 #
20 # == Example 21 #
22 # as module
23 # require
'cartesian' 24 # foo =
[1, 2] 25 # bar =
["a", "b"] 26 # Cartesian::product(foo, bar) #=> [[1,
"a"], [1, "b"], [2, "a"], [2, "b"]]
27 # as mixin
28 # require
'cartesian' 29 # foo =
[1, 2] 30 # bar =
["a", "b"] 31 # foo.cartesian(bar) #=> [[1, "a"], [1,
"b"], [2, "a"], [2, "b"]] 32 # 33 34 require 'cartesian_iterator' 35 36 module Cartesian 37
38 # Produces the cartesian product of self and other. 39 # The result is an array of pairs
(i.e. two-element arrays). 40 #
41 # Cartesian::product( [1,2], %w(A B) ) #=> [[1, "A"], [1,
"B"], [2, "A"], [2, "B"]] 42 # 43 # or, if mixed in into Array,
44 # 45 # [1,2].cartesian %w(A B) #=>
[[1, "A"], [1, "B"], [2, "A"], [2,
"B"]] 46 #
47 def
Cartesian.product(first, second) 48 first.x(second).to_a 49 end 50
51 # Behaves as product, except for the elements are joined. 52 # 53 # Cartesian::joined_cartesian(
[1,2], %w(A B) ) #=> ["1A", "1B", "2A",
"2B"] 54 #
55 # or, if mixed in
into Array, 56 #
57 #
[1,2].joined_cartesian %w(A B) #=> ["1A", "1B",
"2A", "2B"] 58 #
59 def Cartesian.joined_product(first, second) 60 product(first, second).map {|pair|
pair.join } 61 end
62 63 # Cartesian.joined_product for
mixin. 64 #
65 def
joined_cartesian(other)
66 Cartesian.joined_product(self, other) 67 end 68
69 # Convenient way of iterating over the elements. 70 # Preferable when the cartesian
product array 71 # is
not needed, for the consumption of memory 72 # is fixed and very small, in contrast with the
73 # exponential memory
requirements of the 74
# conventional approach. 75 #
76 # for row, col in (1..10).x(1..30) 77 # Matrix[row, col] = row**2 + col**3 78 # end 79 # 80 # Of course, calls can be chained
as in 81 # 82 # for x, y, z in
(1..10).x(1..10).x(1..10) 83 # # ... do something ... 84 # end 85 # 86 #-- 87 # for letter, number in %w{a b
c}.x(1..3) 88 # ... do
something ... 89 # end
90 #++ 91 # 92 # Beware that both +self+ and
+other+ must implement
93 # +to_a+, i.e., be convertible to array. 94 # 95 def x(other) 96 case other 97 when CartesianIterator 98 other.left_product(self)
99 else 100 CartesianIterator.new(self, other)
101 end 102 end 103 alias cartesian x 104 alias right_product x 105 106 def left_product(other) 107 case other 108 when CartesianIterator
109
other.right_product(self) 110 else 111 CartesianIterator.new(other, self) 112 end 113 end 114 115 # Concise way of iterating
multi-dimensionally 116
# over the same array or range. 117 # 118 # For instance, 119 # 120 # for x,y,z in [0,1]**3 121 # puts [x, y, z].join(',')
122 # end 123 # 124 # produces the following output
125 # 126 # 0,0,0 127 # 0,0,1 128 # 0,1,0 129 # 0,1,1 130 # 1,0,0 131 # 1,0,1 132 # 1,1,0 133 # 1,1,1 134 # 135 # It also works with Range
objects. 136 #
137 def **(fixnum)
138 if fixnum < 0
139 raise ArgumentError,
"negative power" 140 elsif fixnum == 0 141 return [] 142 elsif fixnum == 1 143 return self 144 else 145 iter = CartesianIterator.new(self, self) 146 (fixnum-2).times do 147 iter.product!(self) 148 end 149 iter 150 end 151 end 152 alias :power! :** 153 end 154 155 class Array 156 include Cartesian 157 end 158 159 class Range 160 include Cartesian 161 end
Generated using the rcov
code coverage analysis tool for Ruby version 0.8.1.2.