set_theory 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -17,14 +17,52 @@ _For the whole library_
17
17
  _Or choose the parts you want_
18
18
 
19
19
  require 'set_theory/member_of'
20
- require 'set_theory/union'
21
- require 'set_theory/intersection'
22
20
  require 'set_theory/difference'
21
+ require 'set_theory/intersection'
22
+ require 'set_theory/power_set'
23
23
  require 'set_theory/symmetric_difference'
24
+ require 'set_theory/union'
24
25
 
25
26
  Usage
26
27
  -----
27
28
 
29
+ #### Membership (Object#member_of?)
30
+
31
+ Returns true if the receiving object is found within an array.
32
+
33
+ 1.member_of? [1, 2, 3] #=> true
34
+
35
+ #### Difference (Array#difference)
36
+
37
+ Returns an array with the objects of the receiving array that are not in the passed array.
38
+
39
+ [1, 2, 3].difference [2, 3, 4] #=> [1]
40
+ [2, 3, 4].difference [1, 2, 3] #=> [4]
41
+
42
+ #### Intersection (Array#intersection)
43
+
44
+ Returns an array containing only the objects that are members of both the receiving and passed array.
45
+
46
+ [1, 2, 3].intersection [2, 3, 4] #=> [2, 3]
47
+
48
+ #### Power set (Array#power_set)
49
+
50
+ Returns an array with all possible subsets of the receiving array.
51
+
52
+ [1, 2, 3].power_set #=> [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
53
+
54
+ #### Symmetric difference (Array#symmetric_difference)
55
+
56
+ Returns an array containing only the objects that are in one of the receiving or passed arrays, but not both.
57
+
58
+ [1, 2, 3].symmetric_difference [2, 3, 4] #=> [1, 4]
59
+
60
+ #### Union (Array#union)
61
+
62
+ Returns an array containing all members the receiving and passed arrays.
63
+
64
+ [1, 2, 3].union [2, 3, 4] #=> [1, 2, 3, 4]
65
+
28
66
  Development and Testing
29
67
  -----------------------
30
68
 
@@ -41,19 +79,23 @@ _Install dependencies_
41
79
 
42
80
  _Then run the specs_
43
81
 
44
- rake
82
+ bundle exec rake
45
83
 
46
84
  TODO
47
85
  ----
48
86
 
49
87
  * Possibly deal with type errors on parameter to member_of?
50
- * Add aliases for other set operations.
51
- * Add methods for missing set operations.
52
88
 
53
89
  Rationale
54
90
  ---------
55
91
  Yes, I know about [this patch](https://github.com/rails/rails/pull/265), but I prefer `Object#member_of?` and `!Object#member_of?`.
56
92
 
93
+ References
94
+ ----------
95
+ [http://en.wikipedia.org/wiki/Set_theory](http://en.wikipedia.org/wiki/Set_theory)
96
+
97
+ [http://rosettacode.org/wiki/Power_Set#Ruby](http://rosettacode.org/wiki/Power_Set#Ruby)
98
+
57
99
  Copyright
58
100
  ---------
59
101
  See LICENSE for details.
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # There are two methods described here for calculating the power set of
4
+ # a set.
5
+ #
6
+ # First is using a nice Ruby implementation from
7
+ # http://rosettacode.org/wiki/Power_Set#Ruby
8
+ #
9
+ # Second is using a binary count method.
10
+ #
11
+
12
+ require 'pp'
13
+ require 'benchmark'
14
+
15
+ array = [1,2,3,4,5,6,7,8,9,0,10,11,12,13,14,15,16,17,18,19]
16
+
17
+ puts "set size: #{array.size}"
18
+
19
+ inject = Benchmark.measure do
20
+
21
+ array.inject([[]]) { |ps,item|
22
+ ps + ps.map { |e| e + [item] }
23
+ }
24
+
25
+ end
26
+ puts "inject: #{inject}"
27
+
28
+ binary = Benchmark.measure do
29
+
30
+ out = []
31
+ (2**array.size).times do |i|
32
+ a = []
33
+ binary = i.to_s(2).rjust(array.size, '0')
34
+ binary.size.times do |j|
35
+ a << array[j] if binary[j] == '1'
36
+ end
37
+ out << a
38
+ end
39
+
40
+ end
41
+ puts "binary: #{binary}"
@@ -0,0 +1,8 @@
1
+ class Array
2
+ # Taken from http://rosettacode.org/wiki/Power_Set#Ruby
3
+ def power_set
4
+ self.inject([[]]) { |set,item|
5
+ set + set.map { |e| e + [item] }
6
+ }
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
1
  module SetTheory
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/set_theory.rb CHANGED
@@ -3,3 +3,4 @@ require "set_theory/union"
3
3
  require "set_theory/intersection"
4
4
  require "set_theory/difference"
5
5
  require "set_theory/symmetric_difference"
6
+ require "set_theory/power_set"
data/set_theory.gemspec CHANGED
@@ -9,11 +9,12 @@ Gem::Specification.new do |s|
9
9
  s.authors = ["Tim Preston"]
10
10
  s.email = ["tim@tp.id.au"]
11
11
  s.homepage = "http://github.com/tehpeh/set_theory"
12
- s.summary = %q{Extends Object with nice names for set operations.}
13
- s.description = %q{Extends Object with nice names for set operations.}
12
+ s.summary = %q{Extends Object and Array with nice names for set operations.}
13
+ s.description = %q{Extends Object and Array with nice names for set operations.\nSee the Set class (http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/index.html) for a library that doesn't pollute your Objects and Arrays.}
14
14
 
15
15
  s.rubyforge_project = "set_theory"
16
16
 
17
+ s.add_development_dependency "rake", "0.8.7"
17
18
  s.add_development_dependency "gem-release", "~> 0.0.16"
18
19
  s.add_development_dependency "rspec", "~> 2.6.0"
19
20
  s.add_development_dependency "guard", "~> 0.3.4"
@@ -0,0 +1,14 @@
1
+ require 'set_theory/power_set'
2
+
3
+ describe Array do
4
+ it 'responds to #power_set' do
5
+ Array.new.respond_to?(:power_set).should be
6
+ end
7
+
8
+ describe '#power_set' do
9
+ it 'calculates the power set of the set' do
10
+ ([1, 2, 3].power_set).should
11
+ eql [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]].sort
12
+ end
13
+ end
14
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: set_theory
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Tim Preston
@@ -10,65 +10,76 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-25 00:00:00 +10:00
13
+ date: 2011-05-30 00:00:00 +10:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
- name: gem-release
17
+ name: rake
18
18
  prerelease: false
19
19
  requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - "="
23
+ - !ruby/object:Gem::Version
24
+ version: 0.8.7
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: gem-release
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
20
31
  none: false
21
32
  requirements:
22
33
  - - ~>
23
34
  - !ruby/object:Gem::Version
24
35
  version: 0.0.16
25
36
  type: :development
26
- version_requirements: *id001
37
+ version_requirements: *id002
27
38
  - !ruby/object:Gem::Dependency
28
39
  name: rspec
29
40
  prerelease: false
30
- requirement: &id002 !ruby/object:Gem::Requirement
41
+ requirement: &id003 !ruby/object:Gem::Requirement
31
42
  none: false
32
43
  requirements:
33
44
  - - ~>
34
45
  - !ruby/object:Gem::Version
35
46
  version: 2.6.0
36
47
  type: :development
37
- version_requirements: *id002
48
+ version_requirements: *id003
38
49
  - !ruby/object:Gem::Dependency
39
50
  name: guard
40
51
  prerelease: false
41
- requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirement: &id004 !ruby/object:Gem::Requirement
42
53
  none: false
43
54
  requirements:
44
55
  - - ~>
45
56
  - !ruby/object:Gem::Version
46
57
  version: 0.3.4
47
58
  type: :development
48
- version_requirements: *id003
59
+ version_requirements: *id004
49
60
  - !ruby/object:Gem::Dependency
50
61
  name: guard-rspec
51
62
  prerelease: false
52
- requirement: &id004 !ruby/object:Gem::Requirement
63
+ requirement: &id005 !ruby/object:Gem::Requirement
53
64
  none: false
54
65
  requirements:
55
66
  - - ~>
56
67
  - !ruby/object:Gem::Version
57
68
  version: 0.3.1
58
69
  type: :development
59
- version_requirements: *id004
70
+ version_requirements: *id005
60
71
  - !ruby/object:Gem::Dependency
61
72
  name: rb-fsevent
62
73
  prerelease: false
63
- requirement: &id005 !ruby/object:Gem::Requirement
74
+ requirement: &id006 !ruby/object:Gem::Requirement
64
75
  none: false
65
76
  requirements:
66
77
  - - ~>
67
78
  - !ruby/object:Gem::Version
68
79
  version: 0.4.0
69
80
  type: :development
70
- version_requirements: *id005
71
- description: Extends Object with nice names for set operations.
81
+ version_requirements: *id006
82
+ description: Extends Object and Array with nice names for set operations.\nSee the Set class (http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/index.html) for a library that doesn't pollute your Objects and Arrays.
72
83
  email:
73
84
  - tim@tp.id.au
74
85
  executables: []
@@ -86,10 +97,12 @@ files:
86
97
  - LICENSE
87
98
  - README.md
88
99
  - Rakefile
100
+ - benchmark/power_set.rb
89
101
  - lib/set_theory.rb
90
102
  - lib/set_theory/difference.rb
91
103
  - lib/set_theory/intersection.rb
92
104
  - lib/set_theory/member_of.rb
105
+ - lib/set_theory/power_set.rb
93
106
  - lib/set_theory/symmetric_difference.rb
94
107
  - lib/set_theory/union.rb
95
108
  - lib/set_theory/version.rb
@@ -97,6 +110,7 @@ files:
97
110
  - spec/set_theory/difference_spec.rb
98
111
  - spec/set_theory/intersection_spec.rb
99
112
  - spec/set_theory/member_of_spec.rb
113
+ - spec/set_theory/power_set_spec.rb
100
114
  - spec/set_theory/symmetric_difference_spec.rb
101
115
  - spec/set_theory/union_spec.rb
102
116
  has_rdoc: true
@@ -126,10 +140,11 @@ rubyforge_project: set_theory
126
140
  rubygems_version: 1.5.2
127
141
  signing_key:
128
142
  specification_version: 3
129
- summary: Extends Object with nice names for set operations.
143
+ summary: Extends Object and Array with nice names for set operations.
130
144
  test_files:
131
145
  - spec/set_theory/difference_spec.rb
132
146
  - spec/set_theory/intersection_spec.rb
133
147
  - spec/set_theory/member_of_spec.rb
148
+ - spec/set_theory/power_set_spec.rb
134
149
  - spec/set_theory/symmetric_difference_spec.rb
135
150
  - spec/set_theory/union_spec.rb