Files
swift-collections/Sources/BasicContainers/UniqueSet/UniqueSet+Initializers.swift
T
Franz Busch 3c373b900c Use the defines from traits directly
Package traits already pass defines with their trait name when building. Instead of adding our own conditional defines based on the traits we can use the trait provided defines directly. I observed that various tooling behaves better with the trait provided defines.
2026-05-06 07:41:02 -07:00

109 lines
2.6 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) && UnstableHashedContainers
@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 UnstableContainersPreview
@_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 UnstableContainersPreview
@_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 UnstableContainersPreview
@_alwaysEmitIntoClient
public init<
S: BorrowingSequence_<Element> & Sequence<Element>
>(
copying items: borrowing S
) {
self.init()
self._insert(copying: items)
}
#endif
}
#endif