Class: SignedMultiset
- Inherits:
-
Object
- Object
- SignedMultiset
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/signed_multiset.rb
Constant Summary collapse
- VERSION =
"0.2.0"
Instance Method Summary collapse
-
#&(other) ⇒ SignedMultiset
Combine self with another SignedMultiset via intersection to create a merged instance.
-
#+(other) ⇒ SignedMultiset
Combine self with another SignedMultiset via addition to create a merged instance.
-
#-(other) ⇒ SignedMultiset
Combine self with another SignedMultiset via subtraction to create a merged instance.
-
#<<(key) ⇒ self
Increment multiplicity by 1 for a key.
-
#<=>(other) ⇒ -1, ...
Compare self with another SignedMultiset.
-
#[](key) ⇒ Integer?
Get the multiplicity for a key.
-
#[]=(key, multiplicity) ⇒ Integer
Set the multiplicity for a key.
-
#cardinality ⇒ Integer
(also: #sum)
Get the cardinality (sum of multiplicities) for self.
-
#delete(key) ⇒ Integer
Remove key completely from the set, similar to [key]=0.
-
#dup ⇒ SignedMultiset
Creates a new instance of equal to current instance.
-
#each(*args, &block) ⇒ Enumerator
Iterate over the multiplicity collection.
-
#increment(key, value) ⇒ Integer
Increment the multiplicity for a key.
-
#initialize(*args) ⇒ SignedMultiset
constructor
Create a new SignedMultiset instance.
- #inspect ⇒ Object
-
#keys ⇒ Array
Get the list of keys for self.
-
#multiplicities ⇒ Hash
Get the non-zero key-multiplicity pairs.
-
#size ⇒ Integer
(also: #length)
Get the count of unique keys in the SignedMultiset.
- #to_a ⇒ Object
- #to_hash ⇒ Object
- #to_s ⇒ Object
-
#values ⇒ Array
Get the multiplicity values for self.
-
#|(other) ⇒ SignedMultiset
Combine self with another SignedMultiset via union to create a merged instance.
Constructor Details
#initialize(*args) ⇒ SignedMultiset
Create a new SignedMultiset instance.
10 11 12 13 14 15 16 17 |
# File 'lib/signed_multiset.rb', line 10 def initialize(*args) obj = args.count == 1 ? args.first : args if obj.respond_to?(:each) obj.each { |k, v| increment(k, v || 1) } else self << obj end end |
Instance Method Details
#&(other) ⇒ SignedMultiset
Combine self with another SignedMultiset via intersection to create a merged instance.
124 125 126 127 128 |
# File 'lib/signed_multiset.rb', line 124 def &(other) (keys & other.keys).reduce(self.class.new) do |m, k| m.increment(k, [self[k], other[k]].min); m end end |
#+(other) ⇒ SignedMultiset
Combine self with another SignedMultiset via addition to create a merged instance.
94 95 96 97 98 |
# File 'lib/signed_multiset.rb', line 94 def +(other) other.multiplicities.reduce(self.dup) do |m, (k, v)| m.increment(k,v); m end end |
#-(other) ⇒ SignedMultiset
Combine self with another SignedMultiset via subtraction to create a merged instance.
104 105 106 107 108 |
# File 'lib/signed_multiset.rb', line 104 def -(other) other.multiplicities.reduce(self.dup) do |m, (k, v)| m.increment(k,-v); m end end |
#<<(key) ⇒ self
Increment multiplicity by 1 for a key. This method is chainable.
71 72 73 74 |
# File 'lib/signed_multiset.rb', line 71 def <<(key) increment(key, 1) self end |
#<=>(other) ⇒ -1, ...
Compare self with another SignedMultiset
164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/signed_multiset.rb', line 164 def <=>(other) if [:multiplicities, :cardinality, :size].all? { |m| other.respond_to?(m) } if multiplicities == other.multiplicities 0 elsif cardinality != other.cardinality cardinality <=> other.cardinality elsif size != other.size size <=> other.size else multiplicities <=> other.multiplicities end end end |
#[](key) ⇒ Integer?
Get the multiplicity for a key.
42 43 44 |
# File 'lib/signed_multiset.rb', line 42 def [](key) multiplicities[key] end |
#[]=(key, multiplicity) ⇒ Integer
Set the multiplicity for a key.
51 52 53 54 |
# File 'lib/signed_multiset.rb', line 51 def []=(key, multiplicity) entries[key] = multiplicity self[key] end |
#cardinality ⇒ Integer Also known as: sum
Get the cardinality (sum of multiplicities) for self.
147 148 149 |
# File 'lib/signed_multiset.rb', line 147 def cardinality values.inject(&:+) end |
#delete(key) ⇒ Integer
Remove key completely from the set, similar to [key]=0
80 81 82 |
# File 'lib/signed_multiset.rb', line 80 def delete(key) entries.delete(key) end |
#dup ⇒ SignedMultiset
Creates a new instance of equal to current instance
86 87 88 |
# File 'lib/signed_multiset.rb', line 86 def dup self.class.new(multiplicities) end |
#each(*args, &block) ⇒ Enumerator
Iterate over the multiplicity collection.
29 30 31 32 33 34 35 |
# File 'lib/signed_multiset.rb', line 29 def each(*args, &block) if block_given? multiplicities.each { |k,v| yield(k,v) } else multiplicities.each(args) end end |
#increment(key, value) ⇒ Integer
Increment the multiplicity for a key.
61 62 63 64 65 |
# File 'lib/signed_multiset.rb', line 61 def increment(key, value) entries[key] ||= 0 entries[key] += value self[key] end |
#inspect ⇒ Object
190 191 192 |
# File 'lib/signed_multiset.rb', line 190 def inspect "<#{self.class} #{to_s}>" end |
#keys ⇒ Array
Get the list of keys for self.
133 134 135 |
# File 'lib/signed_multiset.rb', line 133 def keys multiplicities.keys end |
#multiplicities ⇒ Hash
Get the non-zero key-multiplicity pairs.
22 23 24 |
# File 'lib/signed_multiset.rb', line 22 def multiplicities entries.reject{|k,v| v == 0} end |
#size ⇒ Integer Also known as: length
Get the count of unique keys in the SignedMultiset.
155 156 157 |
# File 'lib/signed_multiset.rb', line 155 def size keys.size end |
#to_a ⇒ Object
182 183 184 |
# File 'lib/signed_multiset.rb', line 182 def to_a multiplicities.to_a end |
#to_hash ⇒ Object
178 179 180 |
# File 'lib/signed_multiset.rb', line 178 def to_hash multiplicities.dup end |
#to_s ⇒ Object
186 187 188 |
# File 'lib/signed_multiset.rb', line 186 def to_s multiplicities.map{ |k,m| "#{k}: #{m}"}.join(', ') end |
#values ⇒ Array
Get the multiplicity values for self.
140 141 142 |
# File 'lib/signed_multiset.rb', line 140 def values multiplicities.values end |
#|(other) ⇒ SignedMultiset
Combine self with another SignedMultiset via union to create a merged instance.
114 115 116 117 118 |
# File 'lib/signed_multiset.rb', line 114 def |(other) (keys | other.keys).reduce(self.class.new) do |m, k| m.increment(k, [self[k] || 0, other[k] || 0].max); m end end |