Cartesian 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cartesian.rb +81 -0
- data/tests/benchmark.rb +23 -0
- data/tests/tc_cartesian.rb +23 -0
- metadata +48 -0
data/lib/cartesian.rb
ADDED
@@ -0,0 +1,81 @@
|
|
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
|
+
module Cartesian
|
34
|
+
|
35
|
+
# Produces the cartesian product of self and other.
|
36
|
+
# The result is an array of pairs (i.e. two-element arrays).
|
37
|
+
#
|
38
|
+
# cartesian( [1,2], %w(A B) ) #=> [[1, "A"], [1, "B"], [2, "A"], [2, "B"]]
|
39
|
+
#
|
40
|
+
# or, if mixed in into Array,
|
41
|
+
#
|
42
|
+
# [1,2].cartesian %w(A B) #=> [[1, "A"], [1, "B"], [2, "A"], [2, "B"]]
|
43
|
+
#
|
44
|
+
def Cartesian.product(first, second)
|
45
|
+
result = []
|
46
|
+
first.each do |a|
|
47
|
+
second.each do |b|
|
48
|
+
result << [a, b]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
result
|
52
|
+
end
|
53
|
+
|
54
|
+
# Cartesian.product for mixin.
|
55
|
+
#
|
56
|
+
def cartesian(other)
|
57
|
+
Cartesian.product(self, other)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Behaves as product, except for the elements are joined.
|
61
|
+
#
|
62
|
+
# joined_cartesian( [1,2], %w(A B) ) #=> ["1A", "1B", "2A", "2B"]
|
63
|
+
#
|
64
|
+
# or, if mixed in into Array,
|
65
|
+
#
|
66
|
+
# [1,2].joined_cartesian %w(A B) #=> ["1A", "1B", "2A", "2B"]
|
67
|
+
#
|
68
|
+
def Cartesian.joined_product(first, second)
|
69
|
+
product(first, second).map {|pair| pair.join }
|
70
|
+
end
|
71
|
+
|
72
|
+
# Cartesian.joined_product for mixin.
|
73
|
+
#
|
74
|
+
def joined_cartesian(other)
|
75
|
+
Cartesian.joined_product(self, other)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class Array
|
80
|
+
include Cartesian
|
81
|
+
end
|
data/tests/benchmark.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require '../lib/cartesian'
|
2
|
+
require 'benchmark'
|
3
|
+
|
4
|
+
MULTIPLIER = 3
|
5
|
+
letras = ("a"*MULTIPLIER.."z"*MULTIPLIER).to_a
|
6
|
+
numeros = (0..10**1).to_a
|
7
|
+
|
8
|
+
Benchmark.bmbm do |x|
|
9
|
+
x.report("product") { Cartesian.product(letras, numeros) }
|
10
|
+
x.report("productZip") { Cartesian.productZip(letras, numeros) }
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
#~ def Cartesian.productZip(first, second)
|
16
|
+
#~ result = []
|
17
|
+
#~ first.each do |a|
|
18
|
+
#~ aaa = Array.new(second.size) { a }
|
19
|
+
#~ result += aaa.zip(second)
|
20
|
+
#~ end
|
21
|
+
#~ result
|
22
|
+
#~ end
|
23
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require '../lib/cartesian'
|
2
|
+
require 'benchmark'
|
3
|
+
|
4
|
+
MULTIPLIER = 3
|
5
|
+
letras = ("a"*MULTIPLIER.."z"*MULTIPLIER).to_a
|
6
|
+
numeros = (0..10**1).to_a
|
7
|
+
|
8
|
+
Benchmark.bmbm do |x|
|
9
|
+
x.report("product") { Cartesian.product(letras, numeros) }
|
10
|
+
x.report("productZip") { Cartesian.productZip(letras, numeros) }
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
#~ def Cartesian.productZip(first, second)
|
16
|
+
#~ result = []
|
17
|
+
#~ first.each do |a|
|
18
|
+
#~ aaa = Array.new(second.size) { a }
|
19
|
+
#~ result += aaa.zip(second)
|
20
|
+
#~ end
|
21
|
+
#~ result
|
22
|
+
#~ end
|
23
|
+
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.0
|
3
|
+
specification_version: 1
|
4
|
+
name: Cartesian
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.0
|
7
|
+
date: 2006-11-24 00:00:00 -03:00
|
8
|
+
summary: The Cartesian module provide methods for the calculation of the cartesian producted between two enumberable objects. It can also be easily mixed in into any enumberable class, i.e. any class with Enumerable module mixed in.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: adriano.mitre@gmail.com
|
12
|
+
homepage: http://rubyforge.org/projects/cartesian/
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire: cartesian.rb
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Adriano Brito Mitre
|
31
|
+
files:
|
32
|
+
- tests/benchmark.rb
|
33
|
+
- tests/tc_cartesian.rb
|
34
|
+
- lib/cartesian.rb
|
35
|
+
test_files:
|
36
|
+
- tests/tc_cartesian.rb
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
extra_rdoc_files: []
|
40
|
+
|
41
|
+
executables: []
|
42
|
+
|
43
|
+
extensions: []
|
44
|
+
|
45
|
+
requirements: []
|
46
|
+
|
47
|
+
dependencies: []
|
48
|
+
|