combinatorics 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. data/.gemtest +0 -0
  2. data/.gitignore +8 -0
  3. data/Benchmarks.md +257 -26
  4. data/ChangeLog.md +12 -0
  5. data/LICENSE.txt +1 -2
  6. data/README.md +102 -32
  7. data/Rakefile +13 -2
  8. data/benchmarks/cartesian_product.rb +18 -0
  9. data/benchmarks/choose.rb +19 -0
  10. data/benchmarks/derange.rb +18 -0
  11. data/benchmarks/list_comprehension.rb +2 -5
  12. data/benchmarks/permute.rb +18 -0
  13. data/benchmarks/power_set.rb +18 -0
  14. data/combinatorics.gemspec +124 -7
  15. data/gemspec.yml +11 -6
  16. data/lib/combinatorics.rb +7 -0
  17. data/lib/combinatorics/cartesian_product.rb +3 -0
  18. data/lib/combinatorics/cartesian_product/cardinality.rb +45 -0
  19. data/lib/combinatorics/cartesian_product/extensions.rb +2 -0
  20. data/lib/combinatorics/cartesian_product/extensions/array.rb +7 -0
  21. data/lib/combinatorics/cartesian_product/extensions/set.rb +9 -0
  22. data/lib/combinatorics/cartesian_product/mixin.rb +57 -0
  23. data/lib/combinatorics/choose.rb +3 -0
  24. data/lib/combinatorics/choose/cardinality.rb +99 -0
  25. data/lib/combinatorics/choose/extensions.rb +2 -0
  26. data/lib/combinatorics/choose/extensions/array.rb +5 -0
  27. data/lib/combinatorics/choose/extensions/set.rb +6 -0
  28. data/lib/combinatorics/choose/mixin.rb +53 -0
  29. data/lib/combinatorics/derange.rb +3 -0
  30. data/lib/combinatorics/derange/cardinality.rb +23 -0
  31. data/lib/combinatorics/derange/extensions.rb +1 -0
  32. data/lib/combinatorics/derange/extensions/array.rb +5 -0
  33. data/lib/combinatorics/derange/mixin.rb +47 -0
  34. data/lib/combinatorics/enumerator.rb +2 -0
  35. data/lib/combinatorics/extensions/math.rb +177 -0
  36. data/lib/combinatorics/generator.rb +8 -1
  37. data/lib/combinatorics/permute.rb +3 -0
  38. data/lib/combinatorics/permute/cardinality.rb +98 -0
  39. data/lib/combinatorics/permute/extensions.rb +2 -0
  40. data/lib/combinatorics/permute/extensions/array.rb +7 -0
  41. data/lib/combinatorics/permute/extensions/set.rb +9 -0
  42. data/lib/combinatorics/permute/mixin.rb +48 -0
  43. data/lib/combinatorics/power_set.rb +1 -0
  44. data/lib/combinatorics/power_set/cardinality.rb +36 -0
  45. data/lib/combinatorics/power_set/mixin.rb +19 -22
  46. data/lib/combinatorics/version.rb +2 -2
  47. data/spec/cartesian_product/array_spec.rb +10 -0
  48. data/spec/cartesian_product/cardinality_spec.rb +64 -0
  49. data/spec/cartesian_product/mixin_examples.rb +98 -0
  50. data/spec/cartesian_product/set_spec.rb +10 -0
  51. data/spec/choose/array_spec.rb +9 -0
  52. data/spec/choose/cardinality_spec.rb +132 -0
  53. data/spec/choose/mixin_examples.rb +48 -0
  54. data/spec/choose/set_spec.rb +9 -0
  55. data/spec/derange/array_spec.rb +10 -0
  56. data/spec/derange/cardinality_spec.rb +14 -0
  57. data/spec/derange/mixin_examples.rb +52 -0
  58. data/spec/extensions/math_spec.rb +100 -0
  59. data/spec/extensions/range_spec.rb +1 -1
  60. data/spec/permute/array_spec.rb +10 -0
  61. data/spec/permute/cardinality_spec.rb +146 -0
  62. data/spec/permute/mixin_examples.rb +42 -0
  63. data/spec/permute/set_spec.rb +10 -0
  64. data/spec/power_set/array_spec.rb +3 -2
  65. data/spec/power_set/cardinality_spec.rb +32 -0
  66. data/spec/power_set/mixin_examples.rb +17 -8
  67. data/spec/power_set/set_spec.rb +3 -2
  68. data/spec/spec_helper.rb +5 -3
  69. metadata +114 -95
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ require 'combinatorics/permute'
4
+
5
+ shared_examples_for "Permute::Mixin" do
6
+ describe "#permute" do
7
+ it "should return [[]] for [].permute(0).to_a" do
8
+ set = subject[]
9
+ results = set.permute(0).to_a
10
+
11
+ results.should == [[]]
12
+ end
13
+
14
+ it "should return [[]] for [1].permute(0).to_a" do
15
+ set = subject[1]
16
+ results = set.permute(0).to_a
17
+
18
+ results.should == [[]]
19
+ end
20
+
21
+ it "should return [[1]] for [1].permute(1).to_a" do
22
+ set = subject[1]
23
+ results = set.permute(1).to_a
24
+
25
+ results.should == [[1]]
26
+ end
27
+
28
+ it "should return [[1], [2]] for [1, 2].permute(1).to_a" do
29
+ set = subject[1, 2]
30
+ results = set.permute(1).to_a
31
+
32
+ results.should =~ [[1], [2]]
33
+ end
34
+
35
+ it "should return [[1, 2]] for [1, 2].permute(2).to_a" do
36
+ set = subject[1, 2]
37
+ results = set.permute(2).to_a
38
+
39
+ results.should =~ [[1, 2], [2, 1]]
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+ require 'permute/mixin_examples'
3
+
4
+ require 'combinatorics/permute/extensions/set'
5
+
6
+ describe Set do
7
+ subject { Set }
8
+
9
+ it_should_behave_like "Permute::Mixin"
10
+ end
@@ -1,9 +1,10 @@
1
1
  require 'spec_helper'
2
- require 'combinatorics/power_set/extensions/array'
3
2
  require 'power_set/mixin_examples'
4
3
 
4
+ require 'combinatorics/power_set/extensions/array'
5
+
5
6
  describe Array do
6
7
  subject { Array }
7
8
 
8
- it_should_behave_like "Combinatorics::PowerSet::Mixin"
9
+ it_should_behave_like "PowerSet::Mixin"
9
10
  end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+ require 'combinatorics/power_set'
3
+
4
+ describe PowerSet do
5
+ subject { PowerSet }
6
+
7
+ describe "cardinality" do
8
+ it "should return 1 for cardinality(0)" do
9
+ subject.cardinality(0) == 1
10
+ end
11
+
12
+ it "should return 1 for cardinality(1)" do
13
+ subject.cardinality(1) == 1
14
+ end
15
+
16
+ it "should return 2 for cardinality(2)" do
17
+ subject.cardinality(2) == 2
18
+ end
19
+
20
+ it "should return 6 for cardinality(3)" do
21
+ subject.cardinality(3) == 6
22
+ end
23
+
24
+ it "should return 24 for cardinality(4)" do
25
+ subject.cardinality(4) == 24
26
+ end
27
+ end
28
+
29
+ it "should wrap cardinality with PowerSet.P" do
30
+ should respond_to(:P)
31
+ end
32
+ end
@@ -1,25 +1,34 @@
1
- shared_examples_for "Combinatorics::PowerSet::Mixin" do
1
+ require 'spec_helper'
2
+
3
+ require 'combinatorics/power_set'
4
+
5
+ shared_examples_for "PowerSet::Mixin" do
6
+ let(:empty_set) { Set[] }
7
+
2
8
  it "the powerset of an empty Set should only contain the empty Set" do
3
9
  set = subject[]
4
10
 
5
- set.powerset.should == [set]
11
+ set.powerset.to_a.should == [empty_set]
6
12
  end
7
13
 
8
14
  it "the powerset of a single Set should contain that Set" do
9
15
  set = subject[1]
10
16
 
11
- set.powerset.should == [subject[], set]
17
+ set.powerset.to_a.should == [empty_set, Set[*set]]
12
18
  end
13
19
 
14
20
  it "the powerset of a Set should all be subsets" do
15
- set = subject[1,2,3]
21
+ set = subject[1, 2, 3]
22
+ superset = Set[]
16
23
 
17
- set.powerset.each { |subset| (set & subset).should == subset }
24
+ set.powerset { |subset| superset += subset }
25
+
26
+ superset.should == Set[*set]
18
27
  end
19
28
 
20
- it "should alias cartesian_product to powerset" do
21
- set = subject[1,2,3]
29
+ it "should alias powerset to power_set" do
30
+ set = subject[1]
22
31
 
23
- set.cartesian_product.should == set.powerset
32
+ set.should respond_to(:power_set)
24
33
  end
25
34
  end
@@ -1,9 +1,10 @@
1
1
  require 'spec_helper'
2
- require 'combinatorics/power_set/extensions/set'
3
2
  require 'power_set/mixin_examples'
4
3
 
4
+ require 'combinatorics/power_set/extensions/set'
5
+
5
6
  describe Set do
6
7
  subject { Set }
7
8
 
8
- it_should_behave_like "Combinatorics::PowerSet::Mixin"
9
+ it_should_behave_like "PowerSet::Mixin"
9
10
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,6 @@
1
- require 'rubygems'
2
-
3
- gem 'rspec', '~> 2.0.0'
1
+ gem 'rspec', '~> 2.4'
4
2
  require 'rspec'
3
+
4
+ require 'combinatorics/version'
5
+
6
+ include Combinatorics
metadata CHANGED
@@ -1,95 +1,61 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: combinatorics
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 3
8
- - 1
9
- version: 0.3.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.1
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Postmodern
9
+ - Duper
13
10
  autorequire:
14
11
  bindir: bin
15
12
  cert_chain: []
16
-
17
- date: 2010-10-26 00:00:00 -07:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- name: ore
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
- - 1
31
- - 0
32
- version: 0.1.0
33
- type: :development
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
13
+ date: 2011-10-16 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
36
16
  name: ore-tasks
37
- prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
17
+ requirement: &12746560 !ruby/object:Gem::Requirement
39
18
  none: false
40
- requirements:
19
+ requirements:
41
20
  - - ~>
42
- - !ruby/object:Gem::Version
43
- segments:
44
- - 0
45
- - 1
46
- - 0
47
- version: 0.1.0
21
+ - !ruby/object:Gem::Version
22
+ version: '0.4'
48
23
  type: :development
49
- version_requirements: *id002
50
- - !ruby/object:Gem::Dependency
51
- name: rspec
52
24
  prerelease: false
53
- requirement: &id003 !ruby/object:Gem::Requirement
25
+ version_requirements: *12746560
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &12744980 !ruby/object:Gem::Requirement
54
29
  none: false
55
- requirements:
30
+ requirements:
56
31
  - - ~>
57
- - !ruby/object:Gem::Version
58
- segments:
59
- - 2
60
- - 0
61
- - 0
62
- version: 2.0.0
32
+ - !ruby/object:Gem::Version
33
+ version: '2.4'
63
34
  type: :development
64
- version_requirements: *id003
65
- - !ruby/object:Gem::Dependency
66
- name: yard
67
35
  prerelease: false
68
- requirement: &id004 !ruby/object:Gem::Requirement
36
+ version_requirements: *12744980
37
+ - !ruby/object:Gem::Dependency
38
+ name: yard
39
+ requirement: &12743140 !ruby/object:Gem::Requirement
69
40
  none: false
70
- requirements:
41
+ requirements:
71
42
  - - ~>
72
- - !ruby/object:Gem::Version
73
- segments:
74
- - 0
75
- - 6
76
- - 0
77
- version: 0.6.0
43
+ - !ruby/object:Gem::Version
44
+ version: '0.7'
78
45
  type: :development
79
- version_requirements: *id004
46
+ prerelease: false
47
+ version_requirements: *12743140
80
48
  description: A collection of modules and methods for performing Combinatoric calculations.
81
- email: postmodern.mod3@gmail.com
49
+ email:
50
+ - postmodern.mod3@gmail.com
51
+ - super@manson.vistech.net
82
52
  executables: []
83
-
84
53
  extensions: []
85
-
86
- extra_rdoc_files:
87
- - README.md
88
- - LICENSE.txt
89
- - Benchmarks.md
90
- - ChangeLog.md
91
- files:
54
+ extra_rdoc_files: []
55
+ files:
92
56
  - .document
57
+ - .gemtest
58
+ - .gitignore
93
59
  - .rspec
94
60
  - .yardopts
95
61
  - Benchmarks.md
@@ -97,68 +63,121 @@ files:
97
63
  - LICENSE.txt
98
64
  - README.md
99
65
  - Rakefile
66
+ - benchmarks/cartesian_product.rb
67
+ - benchmarks/choose.rb
68
+ - benchmarks/derange.rb
100
69
  - benchmarks/list_comprehension.rb
70
+ - benchmarks/permute.rb
71
+ - benchmarks/power_set.rb
101
72
  - combinatorics.gemspec
102
73
  - gemspec.yml
103
74
  - lib/combinatorics.rb
75
+ - lib/combinatorics/cartesian_product.rb
76
+ - lib/combinatorics/cartesian_product/cardinality.rb
77
+ - lib/combinatorics/cartesian_product/extensions.rb
78
+ - lib/combinatorics/cartesian_product/extensions/array.rb
79
+ - lib/combinatorics/cartesian_product/extensions/set.rb
80
+ - lib/combinatorics/cartesian_product/mixin.rb
81
+ - lib/combinatorics/choose.rb
82
+ - lib/combinatorics/choose/cardinality.rb
83
+ - lib/combinatorics/choose/extensions.rb
84
+ - lib/combinatorics/choose/extensions/array.rb
85
+ - lib/combinatorics/choose/extensions/set.rb
86
+ - lib/combinatorics/choose/mixin.rb
87
+ - lib/combinatorics/derange.rb
88
+ - lib/combinatorics/derange/cardinality.rb
89
+ - lib/combinatorics/derange/extensions.rb
90
+ - lib/combinatorics/derange/extensions/array.rb
91
+ - lib/combinatorics/derange/mixin.rb
104
92
  - lib/combinatorics/enumerator.rb
105
93
  - lib/combinatorics/extensions.rb
94
+ - lib/combinatorics/extensions/math.rb
106
95
  - lib/combinatorics/extensions/range.rb
107
96
  - lib/combinatorics/generator.rb
108
97
  - lib/combinatorics/list_comprehension.rb
98
+ - lib/combinatorics/permute.rb
99
+ - lib/combinatorics/permute/cardinality.rb
100
+ - lib/combinatorics/permute/extensions.rb
101
+ - lib/combinatorics/permute/extensions/array.rb
102
+ - lib/combinatorics/permute/extensions/set.rb
103
+ - lib/combinatorics/permute/mixin.rb
109
104
  - lib/combinatorics/power_set.rb
105
+ - lib/combinatorics/power_set/cardinality.rb
110
106
  - lib/combinatorics/power_set/extensions.rb
111
107
  - lib/combinatorics/power_set/extensions/array.rb
112
108
  - lib/combinatorics/power_set/extensions/set.rb
113
109
  - lib/combinatorics/power_set/mixin.rb
114
110
  - lib/combinatorics/version.rb
115
111
  - spec/.rspec
112
+ - spec/cartesian_product/array_spec.rb
113
+ - spec/cartesian_product/cardinality_spec.rb
114
+ - spec/cartesian_product/mixin_examples.rb
115
+ - spec/cartesian_product/set_spec.rb
116
+ - spec/choose/array_spec.rb
117
+ - spec/choose/cardinality_spec.rb
118
+ - spec/choose/mixin_examples.rb
119
+ - spec/choose/set_spec.rb
116
120
  - spec/combinatorics_spec.rb
121
+ - spec/derange/array_spec.rb
122
+ - spec/derange/cardinality_spec.rb
123
+ - spec/derange/mixin_examples.rb
117
124
  - spec/enumerator_spec.rb
125
+ - spec/extensions/math_spec.rb
118
126
  - spec/extensions/range_spec.rb
119
127
  - spec/generator_spec.rb
120
128
  - spec/list_comprehension_spec.rb
129
+ - spec/permute/array_spec.rb
130
+ - spec/permute/cardinality_spec.rb
131
+ - spec/permute/mixin_examples.rb
132
+ - spec/permute/set_spec.rb
121
133
  - spec/power_set/array_spec.rb
134
+ - spec/power_set/cardinality_spec.rb
122
135
  - spec/power_set/mixin_examples.rb
123
136
  - spec/power_set/set_spec.rb
124
137
  - spec/spec_helper.rb
125
- has_rdoc: yard
126
138
  homepage: http://github.com/postmodern/combinatorics
127
- licenses:
139
+ licenses:
128
140
  - MIT
129
141
  post_install_message:
130
142
  rdoc_options: []
131
-
132
- require_paths:
143
+ require_paths:
133
144
  - lib
134
- required_ruby_version: !ruby/object:Gem::Requirement
145
+ required_ruby_version: !ruby/object:Gem::Requirement
135
146
  none: false
136
- requirements:
137
- - - ">="
138
- - !ruby/object:Gem::Version
139
- segments:
140
- - 0
141
- version: "0"
142
- required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ! '>='
149
+ - !ruby/object:Gem::Version
150
+ version: 1.8.7
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
152
  none: false
144
- requirements:
145
- - - ">="
146
- - !ruby/object:Gem::Version
147
- segments:
148
- - 0
149
- version: "0"
153
+ requirements:
154
+ - - ! '>='
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
150
157
  requirements: []
151
-
152
158
  rubyforge_project:
153
- rubygems_version: 1.3.7
159
+ rubygems_version: 1.8.10
154
160
  signing_key:
155
161
  specification_version: 3
156
162
  summary: Bringing (more) Combinatorics to Ruby
157
- test_files:
158
- - spec/enumerator_spec.rb
163
+ test_files:
164
+ - spec/cartesian_product/array_spec.rb
165
+ - spec/cartesian_product/cardinality_spec.rb
166
+ - spec/cartesian_product/set_spec.rb
167
+ - spec/choose/array_spec.rb
168
+ - spec/choose/cardinality_spec.rb
169
+ - spec/choose/set_spec.rb
159
170
  - spec/combinatorics_spec.rb
160
- - spec/list_comprehension_spec.rb
161
- - spec/generator_spec.rb
171
+ - spec/derange/array_spec.rb
172
+ - spec/derange/cardinality_spec.rb
173
+ - spec/enumerator_spec.rb
174
+ - spec/extensions/math_spec.rb
162
175
  - spec/extensions/range_spec.rb
163
- - spec/power_set/set_spec.rb
176
+ - spec/generator_spec.rb
177
+ - spec/list_comprehension_spec.rb
178
+ - spec/permute/array_spec.rb
179
+ - spec/permute/cardinality_spec.rb
180
+ - spec/permute/set_spec.rb
164
181
  - spec/power_set/array_spec.rb
182
+ - spec/power_set/cardinality_spec.rb
183
+ - spec/power_set/set_spec.rb