Cartesian 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +29 -0
- data/Manifest.txt +6 -0
- data/PostInstall.txt +9 -0
- data/README.txt +37 -0
- data/Rakefile +13 -0
- data/lib/cartesian.rb +0 -161
- metadata +83 -36
- data/lib/cartesian_iterator.rb +0 -109
- data/lib/extensions.rb +0 -12
- data/lib/grid_search.rb +0 -39
- data/lib/recursive.rb +0 -8
- data/tests/benchmark.rb +0 -26
- data/tests/coverage/-home-adriano-software-ruby-Cartesian-lib-cartesian_iterator_rb.html +0 -308
- data/tests/coverage/-home-adriano-software-ruby-Cartesian-lib-cartesian_rb.html +0 -384
- data/tests/coverage/-home-adriano-software-ruby-Cartesian-lib-extensions_rb.html +0 -214
- data/tests/coverage/-home-adriano-software-ruby-Cartesian-lib-grid_search_rb.html +0 -242
- data/tests/coverage/index.html +0 -141
- data/tests/tc_cartesian.rb +0 -83
- data/tests/tc_cartesian_iterator.rb +0 -34
- data/tests/tc_extensions.rb +0 -12
- data/tests/tc_grid_search.rb +0 -15
data/History.txt
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
=== 0.4.2 / 2011-01-12
|
2
|
+
|
3
|
+
* gem renamed to cartesian in all lowercase
|
4
|
+
* last version under the name Cartesian, includes a dependecy for the new gem
|
5
|
+
|
6
|
+
=== 0.4.1 04-11-2008 03:18
|
7
|
+
|
8
|
+
* 1 major enhancement:
|
9
|
+
* mainly bugfixes and some refactoring. new features: left_product and non-destructive #product methods
|
10
|
+
|
11
|
+
=== 0.3.0 29-10-2007 05:49
|
12
|
+
|
13
|
+
[undocumented changes]
|
14
|
+
|
15
|
+
=== 0.2.3 13-01-2007 20:50
|
16
|
+
|
17
|
+
* 1 minor enhancement:
|
18
|
+
* power! (aliased as "**") method added
|
19
|
+
|
20
|
+
=== 0.2.1 13-01-2007 01:44
|
21
|
+
|
22
|
+
* 1 major enhancement:
|
23
|
+
* Method ".x(enum)" added, which has constant memory requirements, in contrast with the exponential memory usage of the conventional approach.
|
24
|
+
|
25
|
+
=== 0.1.0 24-11-2006 05:35
|
26
|
+
|
27
|
+
* 1 major enhancement:
|
28
|
+
* first public release
|
29
|
+
|
data/Manifest.txt
ADDED
data/PostInstall.txt
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
|
2
|
+
WARNING: The Cartesian gem has been renamed and it is no
|
3
|
+
longer supported or developed under its original name.
|
4
|
+
|
5
|
+
The new gem, whose name is cartesian in all lowercase,
|
6
|
+
was included as a dependency of this last version of the
|
7
|
+
old gem for your convenience, but from now on please
|
8
|
+
refer to the gem by its new ortography.
|
9
|
+
|
data/README.txt
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
= Cartesian
|
2
|
+
|
3
|
+
* http://adrianomitre.github.com/cartesian/
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
The Cartesian gem has become obsolete due to renaming.
|
8
|
+
Please refer to the new gem: cartesian (in all lowercase).
|
9
|
+
|
10
|
+
The new gem was included as a dependency of this last version
|
11
|
+
of this old gem, but from now on please refer to the gem by
|
12
|
+
its new ortography.
|
13
|
+
|
14
|
+
== LICENSE:
|
15
|
+
|
16
|
+
(The MIT License)
|
17
|
+
|
18
|
+
Copyright (c) 2006-2011 Adriano Mitre
|
19
|
+
|
20
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
21
|
+
a copy of this software and associated documentation files (the
|
22
|
+
'Software'), to deal in the Software without restriction, including
|
23
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
24
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
25
|
+
permit persons to whom the Software is furnished to do so, subject to
|
26
|
+
the following conditions:
|
27
|
+
|
28
|
+
The above copyright notice and this permission notice shall be
|
29
|
+
included in all copies or substantial portions of the Software.
|
30
|
+
|
31
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
32
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
33
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
34
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
35
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
36
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
37
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
|
6
|
+
Hoe.spec 'Cartesian' do
|
7
|
+
developer('Adriano Mitre', 'adriano.mitre@gmail.com')
|
8
|
+
self.version = '0.4.2'
|
9
|
+
self.post_install_message = File.read('PostInstall.txt')
|
10
|
+
self.extra_deps = [['cartesian', '>= 0.5.0']]
|
11
|
+
end
|
12
|
+
|
13
|
+
# vim: syntax=ruby
|
data/lib/cartesian.rb
CHANGED
@@ -1,161 +0,0 @@
|
|
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
|
metadata
CHANGED
@@ -1,68 +1,115 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: Cartesian
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 4
|
8
|
+
- 2
|
9
|
+
version: 0.4.2
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
|
-
- Adriano
|
8
|
-
autorequire:
|
12
|
+
- Adriano Mitre
|
13
|
+
autorequire:
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2011-01-12 00:00:00 -02:00
|
13
18
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
16
|
-
|
17
|
-
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: cartesian
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
- 5
|
31
|
+
- 0
|
32
|
+
version: 0.5.0
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: hoe
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 2
|
45
|
+
- 8
|
46
|
+
- 0
|
47
|
+
version: 2.8.0
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
description: |-
|
51
|
+
The Cartesian gem has become obsolete due to renaming.
|
52
|
+
Please refer to the new gem: cartesian (in all lowercase).
|
53
|
+
|
54
|
+
The new gem was included as a dependency of this last version
|
55
|
+
of this old gem, but from now on please refer to the gem by
|
56
|
+
its new ortography.
|
57
|
+
email:
|
58
|
+
- adriano.mitre@gmail.com
|
18
59
|
executables: []
|
19
60
|
|
20
61
|
extensions: []
|
21
62
|
|
22
|
-
extra_rdoc_files:
|
23
|
-
|
63
|
+
extra_rdoc_files:
|
64
|
+
- History.txt
|
65
|
+
- Manifest.txt
|
66
|
+
- PostInstall.txt
|
67
|
+
- README.txt
|
24
68
|
files:
|
25
|
-
-
|
26
|
-
-
|
27
|
-
-
|
28
|
-
-
|
29
|
-
-
|
30
|
-
- tests/coverage/-home-adriano-software-ruby-Cartesian-lib-cartesian_rb.html
|
31
|
-
- tests/coverage/-home-adriano-software-ruby-Cartesian-lib-grid_search_rb.html
|
32
|
-
- tests/coverage/index.html
|
33
|
-
- tests/coverage/-home-adriano-software-ruby-Cartesian-lib-cartesian_iterator_rb.html
|
34
|
-
- tests/coverage/-home-adriano-software-ruby-Cartesian-lib-extensions_rb.html
|
35
|
-
- tests/tc_cartesian_iterator.rb
|
36
|
-
- lib/recursive.rb
|
37
|
-
- lib/cartesian_iterator.rb
|
69
|
+
- History.txt
|
70
|
+
- Manifest.txt
|
71
|
+
- PostInstall.txt
|
72
|
+
- README.txt
|
73
|
+
- Rakefile
|
38
74
|
- lib/cartesian.rb
|
39
|
-
- lib/grid_search.rb
|
40
|
-
- lib/extensions.rb
|
41
75
|
has_rdoc: true
|
42
|
-
homepage: http://
|
43
|
-
|
44
|
-
rdoc_options: []
|
76
|
+
homepage: http://adrianomitre.github.com/cartesian/
|
77
|
+
licenses: []
|
45
78
|
|
79
|
+
post_install_message: "\n\
|
80
|
+
WARNING: The Cartesian gem has been renamed and it is no\n\
|
81
|
+
longer supported or developed under its original name.\n\n\
|
82
|
+
The new gem, whose name is cartesian in all lowercase,\n\
|
83
|
+
was included as a dependency of this last version of the\n\
|
84
|
+
old gem for your convenience, but from now on please\n\
|
85
|
+
refer to the gem by its new ortography.\n\n"
|
86
|
+
rdoc_options:
|
87
|
+
- --main
|
88
|
+
- README.txt
|
46
89
|
require_paths:
|
47
90
|
- lib
|
48
91
|
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
49
93
|
requirements:
|
50
94
|
- - ">="
|
51
95
|
- !ruby/object:Gem::Version
|
96
|
+
segments:
|
97
|
+
- 0
|
52
98
|
version: "0"
|
53
|
-
version:
|
54
99
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
55
101
|
requirements:
|
56
102
|
- - ">="
|
57
103
|
- !ruby/object:Gem::Version
|
104
|
+
segments:
|
105
|
+
- 0
|
58
106
|
version: "0"
|
59
|
-
version:
|
60
107
|
requirements: []
|
61
108
|
|
62
|
-
rubyforge_project:
|
63
|
-
rubygems_version: 1.
|
109
|
+
rubyforge_project: cartesian
|
110
|
+
rubygems_version: 1.3.7
|
64
111
|
signing_key:
|
65
|
-
specification_version:
|
66
|
-
summary: The Cartesian
|
67
|
-
test_files:
|
68
|
-
|
112
|
+
specification_version: 3
|
113
|
+
summary: The Cartesian gem has become obsolete due to renaming
|
114
|
+
test_files: []
|
115
|
+
|
data/lib/cartesian_iterator.rb
DELETED
@@ -1,109 +0,0 @@
|
|
1
|
-
require 'grid_search'
|
2
|
-
|
3
|
-
class CartesianIterator
|
4
|
-
|
5
|
-
include GridSearch
|
6
|
-
|
7
|
-
def initialize(foo, bar)
|
8
|
-
@lists = []
|
9
|
-
@tot_iter = 1
|
10
|
-
product!(foo)
|
11
|
-
product!(bar)
|
12
|
-
end
|
13
|
-
|
14
|
-
def dup
|
15
|
-
Marshal.load(Marshal.dump(self))
|
16
|
-
end
|
17
|
-
|
18
|
-
def equal(other)
|
19
|
-
self.instance_variables.each do |var_name|
|
20
|
-
return false if self.instance_variable_get(var_name) != other.instance_variable_get(var_name)
|
21
|
-
end
|
22
|
-
true
|
23
|
-
end
|
24
|
-
alias == equal
|
25
|
-
|
26
|
-
def product!(other)
|
27
|
-
@lists << other.to_a.dup
|
28
|
-
@tot_iter *= @lists[-1].size
|
29
|
-
self
|
30
|
-
end
|
31
|
-
alias right_product! product!
|
32
|
-
alias x! product!
|
33
|
-
|
34
|
-
def left_product!(other)
|
35
|
-
@lists.unshift other.to_a.dup
|
36
|
-
@tot_iter *= @lists[-1].size
|
37
|
-
self
|
38
|
-
end
|
39
|
-
|
40
|
-
def product(other)
|
41
|
-
(result = self.dup).product!(other)
|
42
|
-
result
|
43
|
-
end
|
44
|
-
alias right_product product
|
45
|
-
alias x product
|
46
|
-
|
47
|
-
def left_product(other)
|
48
|
-
(result = self.dup).left_product!(other)
|
49
|
-
result
|
50
|
-
end
|
51
|
-
|
52
|
-
def each
|
53
|
-
return false if @tot_iter < 1
|
54
|
-
|
55
|
-
elems = []
|
56
|
-
for list in @lists
|
57
|
-
elems << list.restart_and_raw_next
|
58
|
-
end
|
59
|
-
yield *elems
|
60
|
-
|
61
|
-
last_list_index = @lists.size-1
|
62
|
-
n = last_list_index
|
63
|
-
loop do
|
64
|
-
if elems[n] = @lists[n].raw_next
|
65
|
-
yield *elems
|
66
|
-
n = last_list_index
|
67
|
-
next
|
68
|
-
elsif n > 0
|
69
|
-
elems[n] = @lists[n].restart_and_raw_next
|
70
|
-
n -= 1
|
71
|
-
else
|
72
|
-
return true
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def to_a
|
78
|
-
array = []
|
79
|
-
self.each {|*element| array << element }
|
80
|
-
array
|
81
|
-
end
|
82
|
-
alias to_ary to_a
|
83
|
-
|
84
|
-
end
|
85
|
-
|
86
|
-
module Iterable
|
87
|
-
def restart
|
88
|
-
@next_index = -1
|
89
|
-
true
|
90
|
-
end
|
91
|
-
alias start restart
|
92
|
-
|
93
|
-
def next
|
94
|
-
@next_index or restart
|
95
|
-
raw_next
|
96
|
-
end
|
97
|
-
|
98
|
-
def raw_next
|
99
|
-
self[@next_index += 1]
|
100
|
-
end
|
101
|
-
|
102
|
-
def restart_and_raw_next
|
103
|
-
self[@next_index = 0]
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
class Array
|
108
|
-
include Iterable
|
109
|
-
end
|