Multiset

public struct Multiset<T: Hashable>

A Multiset (sometimes called a bag) is a special kind of set in which members are allowed to appear more than once. It’s possible to convert a multiset to a set: let set = Set(multiset)

Conforms to Sequence, ExpressibleByArrayLiteral and Hashable.

  • Constructs an empty multiset.

    Declaration

    Swift

    public init()
  • Constructs a multiset from a sequence, such as an array.

    Declaration

    Swift

    public init<S: Sequence>(_ elements: S) where S.Iterator.Element == T
  • Number of elements stored in the multiset, including multiple copies.

    Declaration

    Swift

    public fileprivate(set) var count = 0
  • Returns true if and only if count == 0.

    Declaration

    Swift

    public var isEmpty: Bool
  • Number of distinct elements stored in the multiset.

    Declaration

    Swift

    public var distinctCount: Int
  • A sequence containing the multiset’s distinct elements.

    Declaration

    Swift

    public var distinctElements: AnySequence<T>
  • Returns true if the multiset contains the given element.

    Declaration

    Swift

    public func contains(_ element: T) -> Bool
  • Returns the number of occurrences of an element in the multiset.

    Declaration

    Swift

    public func count(_ element: T) -> Int
  • Inserts a single occurrence of an element into the multiset.

    Declaration

    Swift

    public mutating func insert(_ element: T)
  • Inserts a number of occurrences of an element into the multiset.

    Declaration

    Swift

    public mutating func insert(_ element: T, occurrences: Int)
  • Removes a single occurrence of an element from the multiset, if present.

    Declaration

    Swift

    public mutating func remove(_ element: T)
  • Removes a number of occurrences of an element from the multiset. If the multiset contains fewer than this number of occurrences to begin with, all occurrences will be removed.

    Declaration

    Swift

    public mutating func remove(_ element: T, occurrences: Int)
  • Removes all occurrences of an element from the multiset, if present.

    Declaration

    Swift

    public mutating func removeAllOf(_ element: T)
  • Removes all the elements from the multiset, and by default clears the underlying storage buffer.

    Declaration

    Swift

    public mutating func removeAll(keepingCapacity keep: Bool = false)
  • Provides for-in loop functionality. Generates multiple occurrences per element.

    Declaration

    Swift

    public func makeIterator() -> AnyIterator<T>

    Return Value

    A generator over the elements.

  • A string containing a suitable textual representation of the multiset.

    Declaration

    Swift

    public var description: String
  • Constructs a multiset using an array literal. Unlike a set, multiple copies of an element are inserted.

    Declaration

    Swift

    public init(arrayLiteral elements: T...)
  • The hash value. x == y implies x.hashValue == y.hashValue

    Declaration

    Swift

    public var hashValue: Int