struct

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

VERSION = {{ (`shards version /tmp/tmp.oaFMOa/src/src`).chomp.stringify }}

Constructors

new(enumerable : Enumerable(T))

Creates a new multiset from the elements in enumerable.

Multiset.new([1, 2, 3, 1]) # => Multiset{1, 1, 2, 3}
Source
new(initial_capacity = nil)

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
Source

Instance methods

&(other : Multiset)

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}
Source
&(other : Enumerable)
Source
*(sf : Int32)

Scales the multiplicity of all elements by sf and returns self.

Multiset{1, 2, 2} * 2 # => Multiset{1, 1, 2, 2, 2, 2}
Source
+(other)

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}
Source
-(other : Enumerable)

Returns a new multiset with all elements in other removed.

Multiset{1, 2, 3} - [1, 3] # => Multiset{2}
Source
<<(object : T)

Alias for add.

ms = Multiset{1, 2, 3}
ms << 4 # => Multiset{1, 2, 3, 4}
Source
==(other : Multiset)

Returns true if both multisets contain the same elements.

Source
==(other : Set)

Returns true if both sets contain the same elements.

Source
^(other : Enumerable)

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}
Source
|(other : Enumerable)

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}
Source
add(object : T, count : Int32)

Increments multiplicity of object by count and returns self.

ms = Multiset{1, 2, 3}
ms.add(4, 2) # => Multiset{1, 2, 3, 4, 4}
Source
add(object : T)

Increments multiplicity of object and returns self.

ms = Multiset{1, 2, 3}
ms.add(4) # => Multiset{1, 2, 3, 4}
Source
clear

Removes all elements and returns self.

ms = Multiset{1, 2, 2}
ms.clear
ms.empty? # => true
Source
compare_by_identity

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
Source
compare_by_identity?

Returns true if the multiset is comparing objects by object_id.

See compare_by_identity.

Source
delete(object, count : Int32)

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}
Source
delete(object)

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}
Source
dup

Returns a new Multiset with the same elements.

Source
each

Yields each element of the multiset, and returns self.

Source
each

Returns an iterator for each element of the multiset.

Source
empty?

Returns true if the multiset has no elements.

Multiset(Int32).new.empty? # => true
Multiset{1, 2, 3}.empty?   # => false
Source
hash(hasher)

See Object#hash(hasher)

Source
includes?(object)

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
Source
inspect(io : IO) : Nil

Alias of #to_s.

Source
intersects?(other : Multiset)

Returns true if the multiset has any element in common with other.

Source
merge(other : Multiset(T))

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}
Source
merge(elems)

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}
Source
multiplicity(object : T)

Returns count of object in the multiset.

ms = Multiset{1, 2, 2}
ms.multiplicity(1) # => 1
ms.multiplicity(2) # => 2
Source
multiplicity(object : U) forall U

Returns 0.

Source
proper_subset?(other : Multiset)

Returns true if the multiset is a proper subset of other.

Source
proper_subset_of?(other : Multiset) : Bool

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
Source
proper_superset?(other : Multiset)

Returns true if the multiset is a proper superset of other.

Source
proper_superset_of?(other : Multiset) : Bool

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
Source
size

Returns the number of elements in the multiset.

Multiset{1, 2, 3}.size       # => 3
Multiset{1, 1, 1, 2, 3}.size # => 5
Source
subset?(other : Multiset)

Returns true if the multiset is a subset of other.

Source
subset_of?(other : Multiset) : Bool

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
Source
subtract(other : Multiset)

Removes all elements in other and returns self.

ms = Multiset{1, 2, 3}
ms.subtract(Multiset{1, 3}) # => Multiset{2}
Source
subtract(other : Enumerable)

Removes all elements in other and returns self.

ms = Multiset{1, 2, 3}
ms.subtract([1, 3]) # => Multiset{2}
Source
superset?(other : Multiset)

Returns true if the multiset is a superset of other.

Source
superset_of?(other : Multiset) : Bool

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
Source
to_s(io : IO) : Nil

Writes a string representation of the multiset to io.

Source
uniq

Returns an Array containing unique elements from the multiset.

Source