Class: SignedMultiset

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

Constant Summary collapse

VERSION =
"0.2.1"

Instance Attribute Summary collapse

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.



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

#entriesObject (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.

Parameters:

Returns:



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.

Parameters:

Returns:



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.

Parameters:

Returns:



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.

Parameters:

  • key (Object)

    The key to increment the multiplicity of

Returns:

  • (self)


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

Parameters:

Returns:

  • (-1, 0, 1)


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.

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



45
46
47
# File 'lib/signed_multiset.rb', line 45

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



54
55
56
57
# File 'lib/signed_multiset.rb', line 54

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

#cardinalityInteger Also known as: sum

Get the cardinality (sum of multiplicities) for self.

Returns:

  • (Integer)


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

Parameters:

  • key (Object)

    The key to remove

Returns:

  • (Integer)

    The multiplicity of the item removed.



83
84
85
# File 'lib/signed_multiset.rb', line 83

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

#dupSignedMultiset

Creates a new instance of equal to current instance

Returns:



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.

Returns:

  • (Enumerator)


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.

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



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

#inspectObject



193
194
195
# File 'lib/signed_multiset.rb', line 193

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

#keysArray

Get the list of keys for self.

Returns:

  • (Array)


136
137
138
# File 'lib/signed_multiset.rb', line 136

def keys
  multiplicities.keys
end

#multiplicitiesHash

Get the non-zero key-multiplicity pairs.

Returns:

  • (Hash)


25
26
27
# File 'lib/signed_multiset.rb', line 25

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)


158
159
160
# File 'lib/signed_multiset.rb', line 158

def size
  keys.size
end

#to_aObject



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

def to_a
  multiplicities.to_a
end

#to_hashObject



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

def to_hash
  multiplicities.dup
end

#to_sObject



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

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

#valuesArray

Get the multiplicity values for self.

Returns:

  • (Array)


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.

Parameters:

Returns:



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