signed_multiset 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 09a3ae9f761ec896d77d62a7a691f9fe65a20f09
4
- data.tar.gz: 0f66bdd5eb798fece35bcd588bc90a50e1c22d81
3
+ metadata.gz: 5974015c8b24aea8ddb676594f8991c5ff7b989e
4
+ data.tar.gz: 6f8cbf71b0c72bddb0eb63ae3230337528df5fcd
5
5
  SHA512:
6
- metadata.gz: a30b92054d69d03ebfa1947fd8505f81ce57d7f1230095ec2ffff274910232a26596c8af4df9bfba87cacaab441e6704cc9b1fcde7477969b4411282628dc382
7
- data.tar.gz: 45684d6839b35ffe4c26073fd4d026064b2aa251ef8aee35eb07c1408ab1b4cfbf97d3e073dd2f4d098861a05247c64c4b8bbea4dfeeb3233ce5814ce3b3dd89
6
+ metadata.gz: b7976458e026b80c572ebe4ccde8fd4ac21ddb4b4dad0f42be3624c9b8d87895e74b47dc13d7379ff8fc31a14bfbe6d0abe67d7487c8d437f86c35d016c97c9d
7
+ data.tar.gz: b4eac8514917c458c8e559b93d4b54e12aae455675585c2ae1f75288b14e90d23bfb33a39e16c0732d6b1e83d451e2b115d00b7efc67498bf3d067904932b7d2
data/HISTORY.md ADDED
@@ -0,0 +1,4 @@
1
+ # 0.0.2
2
+ - New #delete method [@joshwlewis]
3
+ # 0.0.1
4
+ - Initial Release [@joshwlewis]
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # signed_multiset
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/signed_multiset.png)](http://badge.fury.io/rb/signed_multiset)
3
4
  [![Build Status](https://travis-ci.org/joshwlewis/signed_multiset.png?branch=master)](https://travis-ci.org/joshwlewis/signed_multiset)
4
5
  [![Code Climate](https://codeclimate.com/github/joshwlewis/signed_multiset.png)](https://codeclimate.com/github/joshwlewis/signed_multiset)
5
6
  [![Dependency Status](https://gemnasium.com/joshwlewis/signed_multiset.png)](https://gemnasium.com/joshwlewis/signed_multiset)
@@ -8,15 +9,15 @@ Signed Multiset is a Ruby implementation of a Multiset that allows negative memb
8
9
 
9
10
  You can think of it as a Multiset or Bag that allows for negative counts. It feels like a Ruby Hash or Array, but with some differences:
10
11
 
11
- - A key (any ruby object) can be added to or removed from the Multiset any number of times, and is referred to as it's multiplicity
12
+ - A key (any ruby object) can be added to or removed from the SignedMultiset any number of times. The number of times it is included in the multiset (positive or negative) is referred to as it's multiplicity.
12
13
 
13
- - A key can only be considered a member of the Signed Multiset if it's multiplicity is not 0. Setting it's multiplicity to 0 effectively removes that item from the SignedMultiset.
14
+ - A key can only be considered a member of the SignedMultiset if it's multiplicity is not 0. Setting it's multiplicity to 0 effectively removes that item from the SignedMultiset.
14
15
 
15
16
  - The size or length of a SignedMultiset is the number of unique keys with non-zero multiplicities.
16
17
 
17
18
  - The cardinality or sum of a SignedMultiset is the total sum of the multiplicities.
18
19
 
19
- For theory and proofs, refer to [Negative Membership](http://projecteuclid.org/DPubS/Repository/1.0/Disseminate?view=body&id=pdf_1&handle=euclid.ndjfl/1093635499) by Wayne D. Blizard.
20
+ For futher reading, refer to [Negative Membership](http://projecteuclid.org/DPubS/Repository/1.0/Disseminate?view=body&id=pdf_1&handle=euclid.ndjfl/1093635499) by Wayne D. Blizard.
20
21
 
21
22
  ## Installation
22
23
 
@@ -37,23 +38,44 @@ Or install it yourself as:
37
38
  ```ruby
38
39
  require 'signed_multiset'
39
40
 
40
- s = SignedMultiset.new(a: 1, b: 2, c: 3)
41
- #=> => <SignedMultiset a: 1, b: 2, c: 3>
41
+ set = SignedMultiset.new(a: 1, b: 2, c: 3)
42
+ # => <SignedMultiset a: 1, b: 2, c: 3>
42
43
 
43
- s[:b]
44
- #=> 2
44
+ set[:b]
45
+ # => 2
45
46
 
46
- s[:d] = -4
47
- #=> -4
47
+ set[:d] = -4
48
+ # => -4
48
49
 
49
- s << :d
50
- #=> <SignedMultiset a: 1, b: 2, c: 3, d: -3>
50
+ set << :d
51
+ # => <SignedMultiset a: 1, b: 2, c: 3, d: -3>
51
52
 
52
- s.increment(a, -1)
53
- #=> 0
53
+ set.increment(a, -1)
54
+ # => 0
54
55
 
55
- s[:a]
56
- #=> nil
56
+ set[:a]
57
+ # => nil
58
+
59
+ set
60
+ # => <SignedMultiset b: 2, c: 3, d: -3>
61
+
62
+ set.cardinality
63
+ # => 2
64
+
65
+ set.size
66
+ # => 3
67
+
68
+ other_set = SignedMultiset[:a, :c, :d, :d]
69
+ # => <SignedMultiset a: 1, c: 1, d: 2>
70
+
71
+ set + other_set
72
+ # => <SignedMultiset b: 2, c: 4, d: -1, a: 1>
73
+
74
+ set & other_set
75
+ # => <SignedMultiset c: 1, d: -3>
76
+
77
+ set | other_set
78
+ => <SignedMultiset b: 2, c: 3, d: 2, a: 1>
57
79
  ```
58
80
 
59
81
  ## Contributing
@@ -23,7 +23,7 @@ class SignedMultiset
23
23
  #
24
24
  # @return [Hash]
25
25
  def multiplicities
26
- entries.delete_if{|k,v| v == 0}
26
+ entries.reject{|k,v| v == 0}
27
27
  end
28
28
 
29
29
  # Iterate over the multiplicity collection.
@@ -53,7 +53,7 @@ class SignedMultiset
53
53
  # @return [Integer] The multiplicity for the key
54
54
  def []=(key, multiplicity)
55
55
  entries[key] = multiplicity
56
- multiplicities[key]
56
+ [key]
57
57
  end
58
58
 
59
59
  # Increment the multiplicity for a key.
@@ -64,6 +64,7 @@ class SignedMultiset
64
64
  def increment(key, value)
65
65
  entries[key] ||= 0
66
66
  entries[key] += value
67
+ [key]
67
68
  end
68
69
 
69
70
  # Increment multiplicity by 1 for a key. This method is chainable.
@@ -75,6 +76,14 @@ class SignedMultiset
75
76
  self
76
77
  end
77
78
 
79
+ # Remove key completely from the set, similar to [key]=0
80
+ #
81
+ # @param key [Object] The key to remove
82
+ # @return [Integer] The multiplicity of the item removed.
83
+ def delete(key)
84
+ entries.delete(key)
85
+ end
86
+
78
87
  # Creates a new instance of equal to current instance
79
88
  # @return [SignedMultiset]
80
89
  def dup
@@ -117,8 +126,7 @@ class SignedMultiset
117
126
  # @return (see #+)
118
127
  def &(other)
119
128
  (keys & other.keys).reduce(self.class.new) do |m, k|
120
- value = [self[k], other[k]].min
121
- m.increment(k, value); m
129
+ m.increment(k, [self[k], other[k]].min); m
122
130
  end
123
131
  end
124
132
 
@@ -179,11 +187,11 @@ class SignedMultiset
179
187
  end
180
188
 
181
189
  def to_s
182
- multiplicities.map{ |k,c| "#{k}: #{c}"}.join(', ')
190
+ multiplicities.map{ |k,m| "#{k}: #{m}"}.join(', ')
183
191
  end
184
192
 
185
193
  def inspect
186
- "<SignedMultiset #{to_s}>"
194
+ "<#{self.class} #{to_s}>"
187
195
  end
188
196
 
189
197
  private
@@ -1,3 +1,3 @@
1
1
  class SignedMultiset
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -65,7 +65,7 @@ describe SignedMultiset do
65
65
  end
66
66
  end
67
67
 
68
- describe "increment" do
68
+ describe "#increment" do
69
69
  it "should increment existing keys" do
70
70
  subject[:bar].must_equal(2)
71
71
  subject.increment(:bar, -4)
@@ -78,7 +78,7 @@ describe SignedMultiset do
78
78
  end
79
79
  end
80
80
 
81
- describe "<<" do
81
+ describe "#<<" do
82
82
  it "should increment existing keys" do
83
83
  subject[:bar].must_equal(2)
84
84
  subject << :bar
@@ -96,6 +96,14 @@ describe SignedMultiset do
96
96
  end
97
97
  end
98
98
 
99
+ describe "#delete" do
100
+ it "should remove the key" do
101
+ subject[:foo].must_equal(4)
102
+ subject.delete(:foo).must_equal(4)
103
+ subject[:foo].must_be_nil
104
+ end
105
+ end
106
+
99
107
  describe "#+" do
100
108
  let(:merged) { subject + other }
101
109
  it "should return a new set" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: signed_multiset
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Lewis
@@ -63,6 +63,7 @@ files:
63
63
  - .ruby-version
64
64
  - .travis.yml
65
65
  - Gemfile
66
+ - HISTORY.md
66
67
  - LICENSE.txt
67
68
  - README.md
68
69
  - Rakefile