class SignedMultiset

Constants

VERSION

Attributes

entries[R]

Public Class Methods

new(*args) click to toggle source

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

&(other) click to toggle source

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
+(other) click to toggle source

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
-(other) click to toggle source

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
<<(key) click to toggle source

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
<=>(other) click to toggle source

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
[](key) click to toggle source

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
[]=(key, multiplicity) click to toggle source

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
cardinality() click to toggle source

Get the cardinality (sum of multiplicities) for self.

@return [Integer]

# File lib/signed_multiset.rb, line 150
def cardinality
  values.inject(&:+)
end
Also aliased as: sum
delete(key) click to toggle source

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
dup() click to toggle source

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
each(*args) { |k,v| ... } click to toggle source

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
increment(key, value) click to toggle source

Increment the multiplicity for a key.

@param key (see []=) @param value [Integer] The desired increment value, positive or negative @return (see []=)

# File lib/signed_multiset.rb, line 64
def increment(key, value)
  entries[key] ||= 0
  entries[key] += value
  self[key]
end
inspect() click to toggle source
# File lib/signed_multiset.rb, line 193
def inspect
  "<#{self.class} #{to_s}>"
end
keys() click to toggle source

Get the list of keys for self.

@return [Array]

# File lib/signed_multiset.rb, line 136
def keys
  multiplicities.keys
end
length()
Alias for: size
multiplicities() click to toggle source

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
size() click to toggle source

Get the count of unique keys in the SignedMultiset.

@return [Integer]

# File lib/signed_multiset.rb, line 158
def size
  keys.size
end
Also aliased as: length
sum()
Alias for: cardinality
to_a() click to toggle source
# File lib/signed_multiset.rb, line 185
def to_a
  multiplicities.to_a
end
to_hash() click to toggle source
# File lib/signed_multiset.rb, line 181
def to_hash
  multiplicities.dup
end
to_s() click to toggle source
# File lib/signed_multiset.rb, line 189
def to_s
  multiplicities.map{ |k,m| "#{k}: #{m}"}.join(', ')
end
values() click to toggle source

Get the multiplicity values for self.

@return [Array]

# File lib/signed_multiset.rb, line 143
def values
  multiplicities.values
end
|(other) click to toggle source

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