Multimap
public struct Multimap<Key: Hashable, Value: Equatable>
A Multimap is a special kind of dictionary in which each key may be associated with multiple values. This implementation allows duplicate key-value pairs.
Comforms to Sequence
, ExpressibleByDictionaryLiteral
,
Equatable
and CustomStringConvertible
-
Returns an array containing the values associated with the specified key. An empty array is returned if the key does not exist in the multimap.
Declaration
Swift
public subscript(key: Key) -> [Value]
-
Constructs an empty multimap.
Declaration
Swift
public init()
-
Constructs a multimap from a dictionary.
Declaration
Swift
public init(_ elements: Dictionary<Key, Value>)
-
Number of key-value pairs stored in the multimap.
Declaration
Swift
public fileprivate(set) var count = 0
-
Number of distinct keys stored in the multimap.
Declaration
Swift
public var keyCount: Int
-
Returns
true
if and only ifcount == 0
.Declaration
Swift
public var isEmpty: Bool
-
A sequence containing the multimap’s unique keys.
Declaration
Swift
public var keys: AnySequence<Key>
-
A sequence containing the multimap’s values.
Declaration
Swift
public var values: AnySequence<Value>
-
Returns
true
if the multimap contains at least one key-value pair with the given key.Declaration
Swift
public mutating func containsKey(_ key: Key) -> Bool
-
Returns
true
if the multimap contains at least one key-value pair with the given key and value.Declaration
Swift
public func containsValue(_ value: Value, forKey key: Key) -> Bool
-
Inserts a key-value pair into the multimap.
Declaration
Swift
public mutating func insertValue(_ value: Value, forKey key: Key)
-
Inserts multiple key-value pairs into the multimap.
Declaration
Swift
public mutating func insertValues<S:Sequence>(_ values: S, forKey key: Key) where S.Iterator.Element == Value
Parameters
values
A sequence of values to associate with the given key
-
Replaces all the values associated with a given key. If the key does not exist in the multimap, the values are inserted.
Declaration
Swift
public mutating func replaceValues<S:Sequence>(_ values: S, forKey key: Key) where S.Iterator.Element == Value
Parameters
values
A sequence of values to associate with the given key
-
Removes a single key-value pair with the given key and value from the multimap, if it exists.
Declaration
Swift
public mutating func removeValue(_ value: Value, forKey key: Key) -> Value?
Return Value
The removed value, or nil if no matching pair is found.
-
Removes all values associated with the given key.
Declaration
Swift
public mutating func removeValuesForKey(_ key: Key)
-
Removes all the elements from the multimap, and by default clears the underlying storage buffer.
Declaration
Swift
public mutating func removeAll(keepingCapacity keep: Bool = true)
-
Provides for-in loop functionality.
Declaration
Swift
public func makeIterator() -> AnyIterator<(Key,Value)>
Return Value
A generator over the elements.
-
Constructs a multiset using a dictionary literal. Unlike a set, multiple copies of an element are inserted.
Declaration
Swift
public init(dictionaryLiteral elements: (Key, Value)...)
-
A string containing a suitable textual representation of the multimap.
Declaration
Swift
public var description: String
-
Returns
true
if and only if the multimaps contain the same key-value pairs.Declaration
Swift
public static func ==<Key, Value>(lhs: Multimap<Key, Value>, rhs: Multimap<Key, Value>) -> Bool