Class: SignedMultiset

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/signed_multiset.rb

Constant Summary collapse

VERSION =
"0.2.0"

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ SignedMultiset

Create a new SignedMultiset instance.

Parameters:

  • object (Enumerable, nil)

    An array of keys, or key-muliplicity pairs.



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.

Parameters:

Returns:



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.

Parameters:

Returns:



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.

Parameters:

Returns:



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.

Parameters:

  • key (Object)

    The key to increment the multiplicity of

Returns:

  • (self)


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

Parameters:

Returns:

  • (-1, 0, 1)


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.

Parameters:

  • key (Object)

    The key to get the multiplicity of

Returns:

  • (Integer, nil)

    The multiplicity for the key, or nil if the key is not present, or has a zero multiplicity



42
43
44
# File 'lib/signed_multiset.rb', line 42

def [](key)
  multiplicities[key]
end

#[]=(key, multiplicity) ⇒ Integer

Set the multiplicity for a key.

Parameters:

  • key (Object)

    The key to set the multiplicity of

  • multiplicity (Integer)

    The desired multiplicity

Returns:

  • (Integer)

    The multiplicity for the key



51
52
53
54
# File 'lib/signed_multiset.rb', line 51

def []=(key, multiplicity)
  entries[key] = multiplicity
  self[key]
end

#cardinalityInteger Also known as: sum

Get the cardinality (sum of multiplicities) for self.

Returns:

  • (Integer)


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

Parameters:

  • key (Object)

    The key to remove

Returns:

  • (Integer)

    The multiplicity of the item removed.



80
81
82
# File 'lib/signed_multiset.rb', line 80

def delete(key)
  entries.delete(key)
end

#dupSignedMultiset

Creates a new instance of equal to current instance

Returns:



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.

Returns:

  • (Enumerator)


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.

Parameters:

  • value (Integer)

    The desired increment value, positive or negative

  • key (Object)

    The key to set the multiplicity of

Returns:

  • (Integer)

    The multiplicity for the 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

#inspectObject



190
191
192
# File 'lib/signed_multiset.rb', line 190

def inspect
  "<#{self.class} #{to_s}>"
end

#keysArray

Get the list of keys for self.

Returns:

  • (Array)


133
134
135
# File 'lib/signed_multiset.rb', line 133

def keys
  multiplicities.keys
end

#multiplicitiesHash

Get the non-zero key-multiplicity pairs.

Returns:

  • (Hash)


22
23
24
# File 'lib/signed_multiset.rb', line 22

def multiplicities
  entries.reject{|k,v| v == 0}
end

#sizeInteger Also known as: length

Get the count of unique keys in the SignedMultiset.

Returns:

  • (Integer)


155
156
157
# File 'lib/signed_multiset.rb', line 155

def size
  keys.size
end

#to_aObject



182
183
184
# File 'lib/signed_multiset.rb', line 182

def to_a
  multiplicities.to_a
end

#to_hashObject



178
179
180
# File 'lib/signed_multiset.rb', line 178

def to_hash
  multiplicities.dup
end

#to_sObject



186
187
188
# File 'lib/signed_multiset.rb', line 186

def to_s
  multiplicities.map{ |k,m| "#{k}: #{m}"}.join(', ')
end

#valuesArray

Get the multiplicity values for self.

Returns:

  • (Array)


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.

Parameters:

Returns:



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