struct Multiset(T)
- Multiset(T)
- Struct
- Value
- Object
Overview
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
Included Modules
- Enumerable(T)
- Iterable(T)
Defined in:
multiset.crConstant Summary
-
VERSION =
{{ (`shards version /srv/crystaldoc.info/github-tcrouch-multiset.cr-v0.5.0/src`).chomp.stringify }}
Constructors
-
.new(enumerable : Enumerable(T))
Creates a new multiset from the elements in enumerable.
-
.new(initial_capacity = nil)
Create a new empty
Multiset
.
Instance Method Summary
-
#&(other : Multiset)
Returns a new multiset by performing multiset intersection with other.
- #&(other : Enumerable)
-
#*(sf : Int32)
Scales the multiplicity of all elements by sf and returns
self
. -
#+(other)
Returns a new multiset containing the elements from both
self
and other. -
#-(other : Enumerable)
Returns a new multiset with all elements in other removed.
-
#<<(object : T)
Alias for
#add
. -
#==(other : Multiset)
Returns
true
if both multisets contain the same elements. -
#==(other : Set)
Returns
true
if both sets contain the same elements. -
#^(other : Enumerable)
Returns a new multiset by performing symmetric difference with other.
-
#|(other : Enumerable)
Returns a new multiset by performing mutiset union with other.
-
#add(object : T, count : Int32)
Increments multiplicity of object by count and returns
self
. -
#add(object : T)
Increments multiplicity of object and returns
self
. -
#clear
Removes all elements and returns
self
. -
#compare_by_identity
Makes this multiset compare objects using their
object_id
. -
#compare_by_identity? : Bool
Returns
true
if the multiset is comparing objects byobject_id
. -
#delete(object, count : Int32)
Decrements multiplicity of object by count and returns
self
. -
#delete(object)
Decrements multiplicity of object and returns
self
. -
#dup
Returns a new
Multiset
with the same elements. -
#each(&)
Yields each element of the multiset, and returns
self
. -
#each
Returns an iterator for each element of the multiset.
-
#empty?
Returns
true
if the multiset has no elements. -
#hash(hasher)
See
Object#hash(hasher)
-
#includes?(object)
Returns
true
if object is an element in the multiset. -
#inspect(io : IO) : Nil
Alias of
#to_s
. -
#intersects?(other : Multiset)
Returns
true
if the multiset has any element in common with other. -
#merge(other : Multiset(T))
Adds each element of other and returns
self
. -
#merge(elems)
Adds each element of elems and returns
self
. -
#multiplicity(object : T)
Returns count of object in the multiset.
-
#multiplicity(object : U) forall U
Returns
0
. -
#proper_subset?(other : Multiset)
Returns
true
if the multiset is a proper subset of other.DEPRECATED Use #proper_subset_of? instead.
-
#proper_subset_of?(other : Multiset) : Bool
Returns
true
if the multiset is a proper subset of other. -
#proper_superset?(other : Multiset)
Returns
true
if the multiset is a proper superset of other.DEPRECATED Use #proper_superset_of? instead.
-
#proper_superset_of?(other : Multiset) : Bool
Returns
true
if the multiset is a proper superset of other. -
#size
Returns the number of elements in the multiset.
-
#subset?(other : Multiset)
Returns
true
if the multiset is a subset of other.DEPRECATED Use #subset_of? instead.
-
#subset_of?(other : Multiset) : Bool
Returns
true
if the multiset is a subset of other. -
#subtract(other : Multiset)
Removes all elements in other and returns
self
. -
#subtract(other : Enumerable)
Removes all elements in other and returns
self
. -
#superset?(other : Multiset)
Returns
true
if the multiset is a superset of other.DEPRECATED Use #superset_of? instead.
-
#superset_of?(other : Multiset) : Bool
Returns
true
if the multiset is a superset of other. -
#to_s(io : IO) : Nil
Writes a string representation of the multiset to io.
-
#uniq
Returns an
Array
containing unique elements from the multiset.
Constructor Detail
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 Method Detail
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.
DEPRECATED Use #proper_subset_of? instead.
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.
DEPRECATED Use #proper_superset_of? instead.
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.
DEPRECATED Use #subset_of? instead.
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.
DEPRECATED Use #superset_of? instead.
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