Multiset(T)
Inherits Iterable < Enumerable < Struct < Value < Object
A Multiset (or bag) is a collection of unordered elements that is
similar to a set, but allows duplicate values.
Multiset uses Hash as storage.
ms1 = Multiset.new [1, 2]
ms2 = Multiset{2, 1}
ms1 == ms2 # => true
ms1.add(2) # => Multiset{1, 2, 2}
ms1.merge([2, 6]) # => Multiset{1, 2, 2, 2, 6}
ms1.multiplicity(2) # => 3
ms1.subset_of? ms2 # => false
ms2.subset_of? ms1 # => true
Constants
Constructors
Creates a new multiset from the elements in enumerable.
Multiset.new([1, 2, 3, 1]) # => Multiset{1, 1, 2, 3}
Create a new empty Multiset.
If an initial_capacity is given, it will set the initial capacity
of the internal Hash.
ms = Multiset(Int32).new
ms.empty # => true
Instance methods
Returns a new multiset by performing multiset intersection with other.
For each element, new multiplicity is minimum multiplicity in either multiset
ms1 = Multiset{1, 1, 1, 2, 2, 3, 4, 5}
ms2 = Multiset{1, 1, 3, 3, 6}
ms3 = Multiset{'a', 1, 1}
ms1 & ms2 # => Multiset{1, 1, 3}
ms1 & ms3 # => Multiset{1, 1}
Scales the multiplicity of all elements by sf and returns self.
Multiset{1, 2, 2} * 2 # => Multiset{1, 1, 2, 2, 2, 2}
Returns a new multiset containing the elements from both self and
other.
Multiset{1, 2, 3} + Multiset{3, 4, 5} # => Multiset{1, 2, 3, 3, 4, 5}
Multiset{1, 2, 3} + [3, 4, 5] # => Multiset{1, 2, 3, 3, 4, 5}
Returns a new multiset with all elements in other removed.
Multiset{1, 2, 3} - [1, 3] # => Multiset{2}
Returns a new multiset by performing symmetric difference with other.
For each element, new multiplicity is absolute difference between multiplicity in either multiset.
ms1 = Multiset{1, 1, 1, 2, 2, 3, 4, 5}
ms2 = Multiset{1, 1, 3, 3, 6}
ms3 = Multiset{'a', 1, 1}
ms1 ^ ms2 # => Multiset{1, 2, 2, 3, 4, 5, 6}
ms1 ^ ms3 # => Multiset{'a', 1, 2, 2, 3, 4, 5}
Returns a new multiset by performing mutiset union with other.
For each element, new multiplicity is maximum multiplicity in either multiset.
ms1 = Multiset{1, 1, 1, 2, 2, 3, 4, 5}
ms2 = Multiset{1, 1, 3, 3, 6}
ms3 = Multiset{'a', 1, 1}
ms1 | ms2 # => Multiset{1, 1, 1, 3, 3, 6, 2, 2, 4, 5}
ms1 | ms3 # => Multiset{'a', 1, 1, 1, 2, 2, 3, 4, 5}
Increments multiplicity of object by count and returns self.
ms = Multiset{1, 2, 3}
ms.add(4, 2) # => Multiset{1, 2, 3, 4, 4}
Increments multiplicity of object and returns self.
ms = Multiset{1, 2, 3}
ms.add(4) # => Multiset{1, 2, 3, 4}
Removes all elements and returns self.
ms = Multiset{1, 2, 2}
ms.clear
ms.empty? # => true
Makes this multiset compare objects using their object_id.
ms = Multiset{"string"}
ms.includes?("str" + "ing") # => true
ms.compare_by_identity
ms.includes?("str" + "ing") # => false
Returns true if the multiset is comparing objects by object_id.
See compare_by_identity.
Decrements multiplicity of object by count and returns self.
Multiset{1, 2, 3}.delete(2, 1) # => Multiset{1, 3}
Multiset{4, 4, 5}.delete(4, 2) # => Multiset{5}
Decrements multiplicity of object and returns self.
Multiset{1, 2, 3}.delete(2) # => Multiset{1, 3}
Multiset{4, 4, 5}.delete(4) # => Multiset{4, 5}
Returns true if the multiset has no elements.
Multiset(Int32).new.empty? # => true
Multiset{1, 2, 3}.empty? # => false
Returns true if object is an element in the multiset.
Multiset{1, 2, 3}.includes?(3) # => true
Multiset{1, 2, 3}.includes?(4) # => false
Multiset{1, 2, 3}.includes?('a') # => false
Returns true if the multiset has any element in common with other.
Adds each element of other and returns self.
ms = Multiset{3, 4, 5}
Multiset{1, 2, 3}.merge(ms) # => Multiset{1, 2, 3, 3, 4, 5}
Adds each element of elems and returns self.
ms = Multiset{1, 2, 3}
ms.merge([3, 4, 5]) # => Multiset{1, 2, 3, 3, 4, 5}
Returns count of object in the multiset.
ms = Multiset{1, 2, 2}
ms.multiplicity(1) # => 1
ms.multiplicity(2) # => 2
Returns true if the multiset is a proper subset of other.
Mutiset{1, 2}.proper_subset_of? Multiset{1, 2, 3} # => true
Mutiset{1, 2}.proper_subset_of? Multiset{1, 1, 2} # => true
Mutiset{1, 2}.proper_subset_of? Multiset{1, 2} # => false
Returns true if the multiset is a proper superset of other.
Returns true if the multiset is a proper superset of other.
Mutiset{1, 2, 3}.proper_superset_of? Multiset{1, 2} # => true
Mutiset{1, 1, 2}.proper_superset_of? Multiset{1, 2} # => true
Mutiset{1, 2}.proper_superset_of? Multiset{1, 2} # => false
Returns the number of elements in the multiset.
Multiset{1, 2, 3}.size # => 3
Multiset{1, 1, 1, 2, 3}.size # => 5
Returns true if the multiset is a subset of other.
Mutiset{1, 2}.subset_of? Multiset{1, 2, 3} # => true
Mutiset{1, 2}.subset_of? Multiset{1, 1, 2} # => true
Mutiset{1, 2}.subset_of? Multiset{1, 2} # => true
Removes all elements in other and returns self.
ms = Multiset{1, 2, 3}
ms.subtract(Multiset{1, 3}) # => Multiset{2}
Removes all elements in other and returns self.
ms = Multiset{1, 2, 3}
ms.subtract([1, 3]) # => Multiset{2}
Returns true if the multiset is a superset of other.
Mutiset{1, 2, 3}.superset_of? Multiset{1, 2} # => true
Mutiset{1, 1, 2}.superset_of? Multiset{1, 2} # => true
Mutiset{1, 2}.superset_of? Multiset{1, 2} # => true