Class: SignedMultiset
- Inherits:
-
Object
- Object
- SignedMultiset
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/signed_multiset.rb
Constant Summary collapse
- VERSION =
"0.2.1"
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
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.
12 13 14 15 16 17 18 19 20 |
# File 'lib/signed_multiset.rb', line 12 def initialize(*args) @entries = {} 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 Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
7 8 9 |
# File 'lib/signed_multiset.rb', line 7 def entries @entries end |
Instance Method Details
#&(other) ⇒ SignedMultiset
Combine self with another SignedMultiset via intersection to create a merged instance.
127 128 129 130 131 |
# File 'lib/signed_multiset.rb', line 127 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.
97 98 99 100 101 |
# File 'lib/signed_multiset.rb', line 97 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.
107 108 109 110 111 |
# File 'lib/signed_multiset.rb', line 107 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.
74 75 76 77 |
# File 'lib/signed_multiset.rb', line 74 def <<(key) increment(key, 1) self end |
#<=>(other) ⇒ -1, ...
Compare self with another SignedMultiset
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/signed_multiset.rb', line 167 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.
45 46 47 |
# File 'lib/signed_multiset.rb', line 45 def [](key) multiplicities[key] end |
#[]=(key, multiplicity) ⇒ Integer
Set the multiplicity for a key.
54 55 56 57 |
# File 'lib/signed_multiset.rb', line 54 def []=(key, multiplicity) entries[key] = multiplicity self[key] end |
#cardinality ⇒ Integer Also known as: sum
Get the cardinality (sum of multiplicities) for self.
150 151 152 |
# File 'lib/signed_multiset.rb', line 150 def cardinality values.inject(&:+) end |
#delete(key) ⇒ Integer
Remove key completely from the set, similar to [key]=0
83 84 85 |
# File 'lib/signed_multiset.rb', line 83 def delete(key) entries.delete(key) end |
#dup ⇒ SignedMultiset
Creates a new instance of equal to current instance
89 90 91 |
# File 'lib/signed_multiset.rb', line 89 def dup self.class.new(multiplicities) end |
#each(*args, &block) ⇒ Enumerator
Iterate over the multiplicity collection.
32 33 34 35 36 37 38 |
# File 'lib/signed_multiset.rb', line 32 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.
64 65 66 67 68 |
# File 'lib/signed_multiset.rb', line 64 def increment(key, value) entries[key] ||= 0 entries[key] += value self[key] end |
#inspect ⇒ Object
193 194 195 |
# File 'lib/signed_multiset.rb', line 193 def inspect "<#{self.class} #{to_s}>" end |
#keys ⇒ Array
Get the list of keys for self.
136 137 138 |
# File 'lib/signed_multiset.rb', line 136 def keys multiplicities.keys end |
#multiplicities ⇒ Hash
Get the non-zero key-multiplicity pairs.
25 26 27 |
# File 'lib/signed_multiset.rb', line 25 def multiplicities entries.reject{|k,v| v == 0} end |
#size ⇒ Integer Also known as: length
Get the count of unique keys in the SignedMultiset.
158 159 160 |
# File 'lib/signed_multiset.rb', line 158 def size keys.size end |
#to_a ⇒ Object
185 186 187 |
# File 'lib/signed_multiset.rb', line 185 def to_a multiplicities.to_a end |
#to_hash ⇒ Object
181 182 183 |
# File 'lib/signed_multiset.rb', line 181 def to_hash multiplicities.dup end |
#to_s ⇒ Object
189 190 191 |
# File 'lib/signed_multiset.rb', line 189 def to_s multiplicities.map{ |k,m| "#{k}: #{m}"}.join(', ') end |
#values ⇒ Array
Get the multiplicity values for self.
143 144 145 |
# File 'lib/signed_multiset.rb', line 143 def values multiplicities.values end |
#|(other) ⇒ SignedMultiset
Combine self with another SignedMultiset via union to create a merged instance.
117 118 119 120 121 |
# File 'lib/signed_multiset.rb', line 117 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 |