struct Multiset(T)
- Multiset(T)
- Struct
- Value
- Object
Overview
Multiset implements a collection of unordered elements with duplicates. Also known as Bag.
Multiset uses Hash
as storage.
Examples
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? ms2 # => false
ms2.subset? ms1 # => true
Included Modules
- Enumerable(T)
- Iterable(T)
Defined in:
multiset.crConstant Summary
-
VERSION =
"0.3.0"
Constructors
-
.new(enumerable : Enumerable(T))
returns a new multiset with elements from the given
Enumerable
-
.new(initial_capacity = nil)
creates a new empty multiset
Instance Method Summary
-
#&(other : Multiset)
returns a new multiset built by performing multiset intersection with the given
Enumerable
- #&(other : Enumerable)
-
#*(sf)
scales the multiplicity of all elements and returns
self
-
#+(other)
adds all objects in the given
Enumerable
to a copy ofself
-
#-(other : Enumerable)
returns a new multiset with all elements in given
Enumerable
removed -
#<<(object : T)
increments multiplicity of the given
Object
and returnsself
-
#==(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 built by performing symmetric difference with the given
Enumerable
-
#|(other : Enumerable)
returns a new multiset built by performing mutiset union with the given
Enumerable
-
#add(object : T, count : Int32)
increments multiplicity of the given
Object
bycount
and returns self -
#add(object : T)
increments multiplicity of the given
Object
and returnsself
-
#clear
removes all elements and returns
self
-
#delete(object, count : Int32)
decrements multiplicity of the given
Object
bycount
and returnsself
-
#delete(object)
decrements multiplicity of the given
Object
and returnsself
-
#dup
returns a duplicate of
self
-
#each(&)
calls the given block for each element, yielding the element as a parameter.
-
#each
returns an iterator over each element
-
#empty?
returns
true
if the multiset has no elements -
#includes?(object)
returns
true
if givenObject
is an element in the multiset -
#inspect(io)
see
#to_s
-
#intersects?(other : Multiset)
returns
true
if the multiset has any element in common withother
-
#merge(elems : Multiset(T))
adds all elements from the given multiset and returns
self
-
#merge(elems)
adds
#each
element and returnsself
-
#multiplicity(object : T)
returns count of the given
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 given multiset -
#proper_superset?(other : Multiset)
returns
true
if the multiset is a proper superset of given multiset -
#size
returns the number of elements
-
#subset?(other : Multiset)
returns
true
if the multiset is a subset of given multiset -
#subtract(other : Multiset)
removes all elements in given
Enumerable
from multiset and returnsself
- #subtract(other : Enumerable)
-
#superset?(other : Multiset)
returns
true
if the multiset is a superset of given multiset -
#to_s(io)
returns a
String
representation of the multiset -
#uniq
returns an
Array
containing unique elements from the multiset
Constructor Detail
returns a new multiset with elements from the given Enumerable
Example
Multiset.new([1, 2, 3, 1]) # => Multiset{1, 1, 2, 3}
creates a new empty multiset
if initial_capacity
is given, it will determine the initial capacity of
the Hash
used internally
Example
ms = Multiset(Int32).new
ms.empty # => true
Instance Method Detail
returns a new multiset built by performing multiset intersection with the
given Enumerable
For each element, new multiplicity is minimum multiplicity in either multiset
Example
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 and returns self
Multiset{1, 2, 2} * 2 # => Multiset{1, 1, 2, 2, 2, 2}
adds all objects in the given Enumerable
to a copy of self
Example
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 given Enumerable
removed
Example
Multiset{1, 2, 3} - [1, 3] # => Multiset{2}
increments multiplicity of the given Object
and returns self
Example
ms = Multiset{1, 2, 3}
ms << 4 # => Multiset{1, 2, 3, 4}
returns a new multiset built by performing symmetric difference with the
given Enumerable
For each element, new multiplicity is absolute difference between multiplicity in either multiset.
Example
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 built by performing mutiset union with the given
Enumerable
For each element, new multiplicity is maximum multiplicity in either multiset.
Example
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 the given Object
by count
and returns self
Example
Multiset{4, 5}.add(6, 2) # => Multiset{1, 2, 6, 6}
increments multiplicity of the given Object
and returns self
Example
Multiset{1, 2, 3}.add(4) # => Multiset{1, 2, 3, 4}
decrements multiplicity of the given Object
by count
and returns self
decrements multiplicity of the given Object
and returns self
Example
Multiset{1, 2, 3}.delete(2) # => Multiset{1, 3}
Multiset{4, 4, 5}.delete(4) # => Multiset{4, 5}
calls the given block for each element, yielding the element as a parameter.
Returns self
returns true
if the multiset has no elements
Example
Multiset(Int32).new.empty? # => true
Multiset{1, 2, 3}.empty? # => false
returns true
if given Object
is an element in the multiset
Example
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 all elements from the given multiset and returns self
Example
ms = Multiset{3, 4, 5}
Multiset{1, 2, 3}.merge(ms) # => Multiset{1, 2, 3, 3, 4, 5}
adds #each
element and returns self
Example
ary = [3, 4, 5]
Multiset{1, 2, 3}.merge(ary) # => Multiset{1, 2, 3, 3, 4, 5}
returns count of the given Object
in the multiset
Example
ms = Multiset{1, 2, 2}
ms.multiplicity(1) # => 1
ms.multiplicity(2) # => 2
returns true
if the multiset is a proper subset of given multiset
Example
Mutiset{1, 2}.proper_subset? Multiset{1, 2, 3} # => true
Mutiset{1, 2}.proper_subset? Multiset{1, 1, 2} # => true
Mutiset{1, 2}.proper_subset? Multiset{1, 2} # => false
returns true
if the multiset is a proper superset of given multiset
Example
Mutiset{1, 2, 3}.proper_superset? Multiset{1, 2} # => true
Mutiset{1, 1, 2}.proper_superset? Multiset{1, 2} # => true
Mutiset{1, 2}.proper_superset? Multiset{1, 2} # => false
returns the number of elements
Example
Multiset{1, 2, 3}.size # => 3
Multiset{1, 1, 1, 2, 3}.size # => 5
returns true
if the multiset is a subset of given multiset
Example
Mutiset{1, 2}.subset? Multiset{1, 2, 3} # => true
Mutiset{1, 2}.subset? Multiset{1, 1, 2} # => true
Mutiset{1, 2}.subset? Multiset{1, 2} # => true
removes all elements in given Enumerable
from multiset and returns self
Example
Multiset{1, 2, 3}.subtract([1, 3]) # => Multiset{2}