Files
swift-collections/Sources/BasicContainers/UniqueSet/UniqueSet+Initializers.swift
T
Nate Cook a99a015934 Disambiguate ContainersPreview and stdlib
Adds trailing underscores to the BorrowingSequence protocols and
requirements, in order to allow both parallel conformances and
the ability to evolve the requirements in this package.
2026-03-27 07:41:32 -05:00

111 lines
2.7 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
//
//===----------------------------------------------------------------------===//
#if !COLLECTIONS_SINGLE_MODULE
import ContainersPreview
#endif
#if compiler(>=6.4) && COLLECTIONS_UNSTABLE_HASHED_CONTAINERS
@available(SwiftStdlib 5.0, *)
extension UniqueSet where Element: ~Copyable {
@inlinable
public init() {
self.init(_storage: .init())
}
@inlinable
public init(minimumCapacity: Int) {
precondition(minimumCapacity >= 0, "Capacity must be nonnegative")
let table = _HTable(minimumCapacity: minimumCapacity)
self.init(_storage: RigidSet(_table: table))
}
@inlinable
public init(consuming set: consuming RigidSet<Element>) {
self.init(_storage: set)
}
}
@available(SwiftStdlib 5.0, *)
extension UniqueSet where Element: ~Copyable {
@_alwaysEmitIntoClient
public init<E: Error>(
minimumCapacity: Int,
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) {
self.init(minimumCapacity: minimumCapacity)
try self.insert(
maximumCount: minimumCapacity,
initializingWith: initializer)
}
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
@_alwaysEmitIntoClient
public init<
E: Error,
P: Producer<Element, E> & ~Copyable & ~Escapable
>(
minimumCapacity: Int? = nil, from producer: inout P
) throws(E)
where P.Element: ~Copyable
{
let c = producer.underestimatedCount
if let minimumCapacity {
self.init(minimumCapacity: Swift.min(minimumCapacity, c))
} else {
self.init(minimumCapacity: c)
}
try self.insert(from: &producer)
}
#endif
}
@available(SwiftStdlib 5.0, *)
extension UniqueSet where Element: Copyable {
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
@available(SwiftStdlib 6.4, *)
@_alwaysEmitIntoClient
public init<
S: BorrowingSequence_<Element> & ~Copyable & ~Escapable
>(
copying items: borrowing S
) {
self.init()
self.insert(copying: items)
}
#endif
@_alwaysEmitIntoClient
public init(copying items: some Sequence<Element>) {
self.init()
self.insert(copying: items)
}
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
@available(SwiftStdlib 6.4, *)
@_alwaysEmitIntoClient
public init<
S: BorrowingSequence_<Element> & Sequence<Element>
>(
copying items: borrowing S
) {
self.init()
self._insert(copying: items)
}
#endif
}
#endif