Bimap
public struct Bimap<Key: Hashable, Value: Hashable>
A Bimap is a special kind of dictionary that allows bidirectional lookup between keys and values.
All keys and values must be unique.
It allows to get, set, or delete a key-value pairs using
subscript notation: bimap[value: aValue] = aKey
or bimap[key: aKey] = aValue
Conforms to Sequence
, Collection
, ExpressibleByDictionaryLiteral
,
Equatable
, Hashable
and CustomStringConvertible
.
-
Constructs an empty bimap.
Declaration
Swift
public init()
-
Constructs a bimap with at least the given number of elements worth of storage. The actual capacity will be the smallest power of 2 that’s >=
minimumCapacity
.Declaration
Swift
public init(minimumCapacity: Int)
-
Constructs a bimap from a dictionary.
Declaration
Swift
public init(_ elements: Dictionary<Key, Value>)
-
Constructs a bimap from a sequence of key-value pairs.
Declaration
Swift
public init<S:Sequence>(_ elements: S) where S.Iterator.Element == (Key, Value)
-
Number of key-value pairs stored in the bimap.
Declaration
Swift
public var count: Int
-
Returns
true
if and only ifcount == 0
.Declaration
Swift
public var isEmpty: Bool
-
A collection containing all the bimap’s keys.
Declaration
Swift
public var keys: AnyCollection<Key>
-
A collection containing all the bimap’s values.
Declaration
Swift
public var values: AnyCollection<Value>
-
Inserts or updates a value for a given key and returns the previous value for that key if one existed, or
nil
if a previous value did not exist. Subscript access is preferred.Declaration
Swift
public mutating func updateValue(_ value: Value, forKey key: Key) -> Value?
-
Removes the key-value pair for the given key and returns its value, or
nil
if a value for that key did not previously exist. Subscript access is preferred.Declaration
Swift
public mutating func removeValueForKey(_ key: Key) -> Value?
-
Removes the key-value pair for the given value and returns its key, or
nil
if a key for that value did not previously exist. Subscript access is preferred.Declaration
Swift
public mutating func removeKeyForValue(_ value: Value) -> Key?
-
Removes all the elements from the bimap, and by default clears the underlying storage buffer.
Declaration
Swift
public mutating func removeAll(keepCapacity keep: Bool = true)
-
Provides for-in loop functionality.
Declaration
Swift
public func makeIterator() -> DictionaryIterator<Key, Value>
Return Value
A generator over the elements.
-
The position of the first element in a non-empty bimap.
Identical to
endIndex
in an empty bimapComplexity: amortized O(1)
Declaration
Swift
public var startIndex: DictionaryIndex<Key, Value>
-
The collection’s
past the end
position.endIndex
is not a valid argument tosubscript
, and is always reachable fromstartIndex
by zero or more applications ofsuccessor()
.Complexity: amortized O(1)
Declaration
Swift
public var endIndex: DictionaryIndex<Key, Value>
-
Returns the position immediately after the given index.
Returns
The index value immediately afteri
.Declaration
Swift
public func index(after i: DictionaryIndex<Key, Value>) -> DictionaryIndex<Key, Value>
Parameters
i
A valid index of the collection.
i
must be less thanendIndex
.Return Value
The index value immediately after
i
.
-
Constructs a bimap using a dictionary literal.
Declaration
Swift
public init(dictionaryLiteral elements: (Key, Value)...)
-
A string containing a suitable textual representation of the bimap.
Declaration
Swift
public var description: String
-
The hash value.
x == y
impliesx.hashValue == y.hashValue
Declaration
Swift
public var hashValue: Int