struct Multiset(T)

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

Defined in:

multiset.cr

Constant Summary

VERSION = {{ (`shards version /srv/crystaldoc.info/github-tcrouch-multiset.cr-v0.5.0/src`).chomp.stringify }}

Constructors

Instance Method Summary

Constructor Detail

def self.new(enumerable : Enumerable(T)) #

Creates a new multiset from the elements in enumerable.

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

[View source]
def self.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

[View source]

Instance Method Detail

def &(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}

[View source]
def &(other : Enumerable) #

[View source]
def *(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}

[View source]
def +(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}

[View source]
def -(other : Enumerable) #

Returns a new multiset with all elements in other removed.

Multiset{1, 2, 3} - [1, 3] # => Multiset{2}

[View source]
def <<(object : T) #

Alias for #add.

ms = Multiset{1, 2, 3}
ms << 4 # => Multiset{1, 2, 3, 4}

[View source]
def ==(other : Multiset) #

Returns true if both multisets contain the same elements.


[View source]
def ==(other : Set) #

Returns true if both sets contain the same elements.


[View source]
def ^(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}

[View source]
def |(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}

[View source]
def 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}

[View source]
def add(object : T) #

Increments multiplicity of object and returns self.

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

[View source]
def clear #

Removes all elements and returns self.

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

[View source]
def 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

[View source]
def compare_by_identity? : Bool #

Returns true if the multiset is comparing objects by object_id.

See #compare_by_identity.


[View source]
def 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}

[View source]
def 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}

[View source]
def dup #

Returns a new Multiset with the same elements.


[View source]
def each(&) #

Yields each element of the multiset, and returns self.


[View source]
def each #

Returns an iterator for each element of the multiset.


[View source]
def empty? #

Returns true if the multiset has no elements.

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

[View source]
def hash(hasher) #

See Object#hash(hasher)


[View source]
def 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

[View source]
def inspect(io : IO) : Nil #

Alias of #to_s.


[View source]
def intersects?(other : Multiset) #

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


[View source]
def 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}

[View source]
def 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}

[View source]
def multiplicity(object : T) #

Returns count of object in the multiset.

ms = Multiset{1, 2, 2}
ms.multiplicity(1) # => 1
ms.multiplicity(2) # => 2

[View source]
def multiplicity(object : U) forall U #

Returns 0.


[View source]
def proper_subset?(other : Multiset) #

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

DEPRECATED Use #proper_subset_of? instead.


[View source]
def 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

[View source]
def proper_superset?(other : Multiset) #

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

DEPRECATED Use #proper_superset_of? instead.


[View source]
def 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

[View source]
def size #

Returns the number of elements in the multiset.

Multiset{1, 2, 3}.size       # => 3
Multiset{1, 1, 1, 2, 3}.size # => 5

[View source]
def subset?(other : Multiset) #

Returns true if the multiset is a subset of other.

DEPRECATED Use #subset_of? instead.


[View source]
def 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

[View source]
def subtract(other : Multiset) #

Removes all elements in other and returns self.

ms = Multiset{1, 2, 3}
ms.subtract(Multiset{1, 3}) # => Multiset{2}

[View source]
def subtract(other : Enumerable) #

Removes all elements in other and returns self.

ms = Multiset{1, 2, 3}
ms.subtract([1, 3]) # => Multiset{2}

[View source]
def superset?(other : Multiset) #

Returns true if the multiset is a superset of other.

DEPRECATED Use #superset_of? instead.


[View source]
def 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

[View source]
def to_s(io : IO) : Nil #

Writes a string representation of the multiset to io.


[View source]
def uniq #

Returns an Array containing unique elements from the multiset.


[View source]