Other Structures

The following structures are available globally.

  • A resizable, value-semantic buffer of homogenous elements of statically-unknown type.

    See more

    Declaration

    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 of AnyValue, but many operations on existentials optimize poorly, so AnyValue 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.
    See more

    Declaration

    Swift

    public struct AnyValue
    extension AnyValue: CustomStringConvertible, CustomDebugStringConvertible
  • A value-semantic collection of Storage.Element with unbounded growth.

    See more

    Declaration

    Swift

    public struct ArrayBuffer<Element>
    extension ArrayBuffer: RandomAccessCollection, MutableCollection
  • Bounded-sized, reference-semantic, contiguous storage of Elements.

    See more

    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 more

    Declaration

    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 Void that can conform to protocols.

    See more

    Declaration

    Swift

    public struct Empty : DefaultInitializable
    extension Empty: CustomStringConvertible
    extension Empty: Hashable, Comparable
  • A fixed sized collection of 0 elements.

    See more

    Declaration

    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 Tail, supporting efficient creation of instances of related types via single-element insertion and deletion.

    See more

    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

Priority Queue

Max priority queue.

  • A collection of Prioritys and Payloads 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” than b if a < b.

    See more

    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 Element of Swift.Dictionary.

    See more

    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 more

    Declaration

    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 Element type, that can conform to things.

    See more

    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 RandomNumberGenerator‘s requirement of 64-bit output.

    See more

    Declaration

    Swift

    public struct PCGRandomNumberGenerator : RandomNumberGenerator
  • A Sequence whose elements are projections, through a lens of type BaseElementPart, of the elements of some Base sequence.

    Projections is analogous to LazyMapSequence but doesn’t store a closure or a KeyPath, and when Base conforms to MutableCollection, so does Projections<Base, L>, as long as L.Focus is-a WritableKeyPath.

    See more

    Declaration

    Swift

  • An algebraic product type whose first element is of type Head and whose remaining elements can be stored in Tail.

    See more

    Declaration

    Swift

  • A type whose full identity is known to the type system.

    Generic function/method APIs often accept a T.Type parameter, where T 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 of T is Any. 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 use Type<T> in the function signature:

    extension UnsafeRawPointer {
      func assumingMemoryBound<T>(to _: Type<T>) -> UnsafePointer<T> { ... }
    }
    

    which is then used as follows:

    someRawPointer.assumingMemoryBound(to: Type<Foo>())
    
    See more
  • A nominal wrapper around Any.Type that conforms to useful protocols.

    Note that the Comparable conformance will not have a stable ordering across program invocations. If reproducible ordering is important, you can sacrifice performance for stability by using String(reflecting: self.value) as a sort key.

    See more

    Declaration

    Swift