Other Structures
The following structures are available globally.
-
A resizable, value-semantic buffer of homogenous elements of statically-unknown type.
See moreDeclaration
Swift
public struct AnyArrayBuffer<Dispatch> where Dispatch : AnyObject
-
A wrapper for a value of any type, with efficient access and inline storage of bounded size for small values.
Existential types such as
Any
provide the storage characteristics ofAnyValue
, but many operations on existentials optimize poorly, soAnyValue
may be needed to achieve efficient access.Using
enums
and no existential, it is possible to build such storage for:- types that are trivially copiable (such as Int), or
- collections of elements with the inline storage being (roughly) a multiple of the element
size.
You might consider using the
enum
approach where applicable.
Declaration
Swift
public struct AnyValue
extension AnyValue: CustomStringConvertible, CustomDebugStringConvertible
-
A value-semantic collection of
See moreStorage.Element
with unbounded growth.Declaration
Swift
public struct ArrayBuffer<Element>
extension ArrayBuffer: RandomAccessCollection, MutableCollection
-
Bounded-sized, reference-semantic, contiguous storage of
See moreElement
s.Declaration
Swift
public struct ArrayStorage<Element>
extension ArrayStorage : RandomAccessCollection, MutableCollection
-
A collection that is all the elements of one collection followed by all the elements of a second collection.
See moreDeclaration
Swift
public struct Concatenation<First: Collection, Second: Collection>: Collection where First.Element == Second.Element
extension Concatenation: BidirectionalCollection where First: BidirectionalCollection, Second: BidirectionalCollection
extension Concatenation: RandomAccessCollection where First: RandomAccessCollection, Second: RandomAccessCollection
extension Concatenation: MutableCollection where First: MutableCollection, Second: MutableCollection
-
A replacement for
See moreVoid
that can conform to protocols.Declaration
Swift
public struct Empty : DefaultInitializable
extension Empty: CustomStringConvertible
extension Empty: Hashable, Comparable
-
A fixed sized collection of 0 elements.
See moreDeclaration
Swift
public struct Array0<T> : FixedSizeArray
extension Array0 : Equatable where T : Equatable
extension Array0 : Hashable where T : Hashable
extension Array0 : Comparable where Element : Comparable
-
A fixed sized random access collection one element longer than
See moreTail
, supporting efficient creation of instances of related types via single-element insertion and deletion.Declaration
Swift
public struct ArrayN<Tail> : FixedSizeArray where Tail : FixedSizeArray
extension ArrayN : Equatable where Element : Equatable, Tail : Equatable
extension ArrayN : Hashable where Element : Hashable, Tail : Hashable
extension ArrayN : Comparable where Element : Comparable, Tail : Comparable
-
A comparable tuple containing a comparable priority and an arbitrary payload.
See moreDeclaration
Swift
public struct PriorityQueueElement<Priority, Payload> where Priority : Comparable
extension PriorityQueueElement: Equatable, Comparable
-
Adapts a collection indexed by
Int
s into aPriorityQueueIndexer
andIndexProtocol
.See also
ArrayPriorityQueueIndexer
.Declaration
Swift
public struct CollectionPriorityQueueIndexer< Key: IdIndexable, Table: RandomAccessCollection & MutableCollection, Value >: PriorityQueueIndexer, IndexProtocol where Table.Index == Int, Table.Element == Value?
extension CollectionPriorityQueueIndexer: DefaultInitializable where Table: DefaultInitializable
-
A zero-sized type that does no indexing, and can be used when re-prioritization within a
See morePriorityQueue
is not needed.Declaration
Swift
public struct NonIndexingPriorityQueueIndexer<Key, Value> : PriorityQueueIndexer, DefaultInitializable
-
A collection of
Priority
s andPayload
s that allows for efficient retrieval of the smallest priority and its associated payload, and insertion of payloads at arbitrary priorities.This is a min-priority queue, where
a
has “higher priority” thanb
ifa < b
.See also
SimplePriorityQueue
.Declaration
Swift
public struct GenericPriorityQueue< Priority: Comparable, Payload, Heap: RandomAccessCollection & RangeReplaceableCollection & MutableCollection, ElementLocations: PriorityQueueIndexer >: Queue where Heap.Element == PriorityQueueElement<Priority, Payload>, ElementLocations.Key == Payload, ElementLocations.Value == Heap.Index
extension GenericPriorityQueue: RandomAccessCollection
extension GenericPriorityQueue: CustomStringConvertible
extension GenericPriorityQueue: DefaultInitializable where Heap: DefaultInitializable, ElementLocations: DefaultInitializable
-
A collection of
Priority
s andPayload
s that allows for efficient retrieval of the smallest priority and its associated payload, and insertion of payloads at arbitrary priorities.This is a max-priority queue, where
a
has “higher priority” thanb
ifa < b
.See also
SimpleMaxPriorityQueue
.Declaration
Swift
public struct GenericMaxPriorityQueue< Priority: Comparable, Payload, Heap: RandomAccessCollection & RangeReplaceableCollection & MutableCollection, ElementLocations: PriorityQueueIndexer >: Queue where Heap.Element == PriorityQueueElement<Priority, Payload>, ElementLocations.Key == Payload, ElementLocations.Value == Heap.Index
extension GenericMaxPriorityQueue: RandomAccessCollection
extension GenericMaxPriorityQueue: CustomStringConvertible
extension GenericMaxPriorityQueue: DefaultInitializable where Heap: DefaultInitializable, ElementLocations: DefaultInitializable
-
A nominal version of the tuple type that is the
See moreElement
ofSwift.Dictionary
.Declaration
Swift
public struct KeyValuePair<Key, Value>
extension KeyValuePair: Equatable where Key: Equatable, Value: Equatable
extension KeyValuePair: Comparable where Key: Comparable, Value: Comparable
extension KeyValuePair: Hashable where Key: Hashable, Value: Hashable
extension KeyValuePair : Decodable where Key : Decodable, Value : Decodable
extension KeyValuePair : Encodable where Key : Encodable, Value : Encodable
-
A Dictionary with a deterministic sequence traversal order determined by the order in which keys are added.
See moreDeclaration
Swift
public struct InsertionOrderedDictionary<Key, Value> where Key : Hashable
extension InsertionOrderedDictionary : RandomAccessCollection
extension InsertionOrderedDictionary : Sequence
extension InsertionOrderedDictionary : CustomReflectable
extension InsertionOrderedDictionary : CustomStringConvertible, CustomDebugStringConvertible
extension InsertionOrderedDictionary : Hashable where Value : Hashable
extension InsertionOrderedDictionary : Equatable where Value : Equatable
extension InsertionOrderedDictionary : Decodable where Key : Decodable, Value : Decodable
extension InsertionOrderedDictionary : Encodable where Key : Encodable, Value : Encodable
-
A Dictionary with a nominal
See moreElement
type, that can conform to things.Declaration
Swift
public struct NominalElementDictionary<Key, Value> where Key : Hashable
extension NominalElementDictionary : Collection
extension NominalElementDictionary : Sequence
extension NominalElementDictionary : CustomReflectable
extension NominalElementDictionary : CustomStringConvertible, CustomDebugStringConvertible
extension NominalElementDictionary : Hashable where Value : Hashable
extension NominalElementDictionary : Equatable where Value : Equatable
extension NominalElementDictionary : Decodable where Key : Decodable, Value : Decodable
extension NominalElementDictionary : Encodable where Key : Encodable, Value : Encodable
-
Fast pseudorandom number generator using permuted congruential generators.
It combines two underlying 32-bit output PCG-XSH-RS random number generators to balance speed with
See moreRandomNumberGenerator
‘s requirement of 64-bit output.Declaration
Swift
public struct PCGRandomNumberGenerator : RandomNumberGenerator
-
A Sequence whose elements are projections, through a lens of type
BaseElementPart
, of the elements of someBase
sequence.
See moreProjections
is analogous toLazyMapSequence
but doesn’t store a closure or aKeyPath
, and whenBase
conforms toMutableCollection
, so doesProjections<Base, L>
, as long asL.Focus
is-aWritableKeyPath
.Declaration
Swift
-
An algebraic product type whose first element is of type
See moreHead
and whose remaining elements can be stored inTail
.Declaration
Swift
-
A type whose full identity is known to the type system.
Generic function/method APIs often accept a
T.Type
parameter, whereT
is a generic parameter, to drive deduction, for example:extension UnsafeRawPointer { func assumingMemoryBound<T>(to _: T.Type) -> UnsafePointer<T> { ... } }
which is then used as follows:
someRawPointer.assumingMemoryBound(to: Foo.self)
The problem with such interfaces is that you can easily end up driving type deduction with a value that doesn’t match the deduced type:
let foo: Any.Type = Foo.self assert(foo == Foo.self) someRawPointer.assumingMemoryBound(to: foo) // not what you might expect
Despite the fact that the function argument is equal to
Foo.self
, the deduced type ofT
isAny
. It isn’t always obvious at the use site of such an API that the value of the function argument is irrelevant and only its type matters. To avoid this problem, we can instead useType<T>
in the function signature:extension UnsafeRawPointer { func assumingMemoryBound<T>(to _: Type<T>) -> UnsafePointer<T> { ... } }
which is then used as follows:
See moresomeRawPointer.assumingMemoryBound(to: Type<Foo>())
-
A nominal wrapper around
Any.Type
that conforms to useful protocols.Note that the
See moreComparable
conformance will not have a stable ordering across program invocations. If reproducible ordering is important, you can sacrifice performance for stability by usingString(reflecting: self.value)
as a sort key.Declaration
Swift