class SignedMultiset
Constants
- VERSION
Attributes
Public Class Methods
Create a new SignedMultiset
instance.
@param object [Enumerable, nil] An array of keys, or key-muliplicity pairs.
# 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
Public Instance Methods
Combine self with another SignedMultiset
via intersection to create a merged instance.
@param other (see +
) @return (see +
)
# 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
Combine self with another SignedMultiset
via addition to create a merged instance.
@param other [SignedMultiset] @return [SignedMultiset]
# 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
Combine self with another SignedMultiset
via subtraction to create a merged instance.
@param other (see +
) @return (see +
)
# 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
Increment multiplicity by 1 for a key. This method is chainable.
@param key [Object] The key to increment the multiplicity of @return [self]
# File lib/signed_multiset.rb, line 74 def <<(key) increment(key, 1) self end
Compare self with another SignedMultiset
@param other (see +
) @return [-1,0,1]
# 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
Get the multiplicity for a key.
@param key [Object] The key to get the multiplicity of @return [Integer, nil] The multiplicity for the key, or nil if the key is
not present, or has a zero multiplicity
# File lib/signed_multiset.rb, line 45 def [](key) multiplicities[key] end
Set the multiplicity for a key.
@param key [Object] The key to set the multiplicity of @param multiplicity [Integer] The desired multiplicity @return [Integer] The multiplicity for the key
# File lib/signed_multiset.rb, line 54 def []=(key, multiplicity) entries[key] = multiplicity self[key] end
Get the cardinality (sum of multiplicities) for self.
@return [Integer]
# File lib/signed_multiset.rb, line 150 def cardinality values.inject(&:+) end
Remove key completely from the set, similar to [key]=0
@param key [Object] The key to remove @return [Integer] The multiplicity of the item removed.
# File lib/signed_multiset.rb, line 83 def delete(key) entries.delete(key) end
Creates a new instance of equal to current instance @return [SignedMultiset]
# File lib/signed_multiset.rb, line 89 def dup self.class.new(multiplicities) end
Iterate over the multiplicity collection.
@return [Enumerator]
# 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
# File lib/signed_multiset.rb, line 193 def inspect "<#{self.class} #{to_s}>" end
Get the list of keys for self.
@return [Array]
# File lib/signed_multiset.rb, line 136 def keys multiplicities.keys end
Get the non-zero key-multiplicity pairs.
@return [Hash]
# File lib/signed_multiset.rb, line 25 def multiplicities entries.reject{|k,v| v == 0} end
Get the count of unique keys in the SignedMultiset
.
@return [Integer]
# File lib/signed_multiset.rb, line 158 def size keys.size end
# File lib/signed_multiset.rb, line 185 def to_a multiplicities.to_a end
# File lib/signed_multiset.rb, line 181 def to_hash multiplicities.dup end
# File lib/signed_multiset.rb, line 189 def to_s multiplicities.map{ |k,m| "#{k}: #{m}"}.join(', ') end
Get the multiplicity values for self.
@return [Array]
# File lib/signed_multiset.rb, line 143 def values multiplicities.values end
Combine self with another SignedMultiset
via union to create a merged instance.
@param other (see +
) @return (see +
)
# 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