Files
swift-collections/Sources/BasicContainers/UniqueSet/UniqueSet+Insertions.swift
2026-06-17 18:24:03 -07:00

187 lines
4.9 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
package mutating func _insert(
_ item: consuming Element
) -> RigidSet<Element>._InsertResult {
let r = _storage._find(item)
if let bucket = r.bucket {
return .init(bucket: bucket, remnant: item)
}
var hashValue = r.hashValue
if _ensureFreeCapacity(1), !_storage._table.isSmall {
hashValue = _storage._hashValue(for: item)
}
let bucket = _storage._insertNew(item, hashValue: hashValue)
return .init(bucket: bucket, remnant: nil)
}
/// Inserts the given element into the set unconditionally. If the set already
/// contained a member equal to `item`, then the new item replaces it.
///
/// - Parameter item: An element to insert into the set.
/// - Returns: An element equal to `item` if the set already contained such
/// a member, otherwise `nil`.
@inlinable
@discardableResult
public mutating func update(
with item: consuming Element
) -> Element? {
var r = self._insert(item)
guard let remnant = r.remnant.take() else { return nil }
return exchange(
&_storage._memberPtr(at: r.bucket).pointee,
with: remnant)
}
/// Inserts the given element in the set if it is not already present.
///
/// - Parameter item: An element to insert into the set.
/// - Returns:
@inlinable
@discardableResult
public mutating func insert(
_ item: consuming Element
) -> Element? {
var r = self._insert(item)
return r.remnant.take()
}
}
@available(SwiftStdlib 5.0, *)
extension UniqueSet where Element: ~Copyable {
@_alwaysEmitIntoClient
public mutating func insert<E: Error>(
maximumCount: Int,
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) -> Void {
_ensureFreeCapacity(maximumCount)
try _storage.insert(
maximumCount: maximumCount, initializingWith: initializer)
}
#if UnstableContainersPreview
@_alwaysEmitIntoClient
public mutating func insert<
E: Error,
P: Producer<Element, E> & ~Copyable & ~Escapable
>(
from producer: inout P
) throws(E)
where P.Element: ~Copyable
{
var done = false
while !done {
_ensureFreeCapacity(Swift.max(producer.underestimatedCount, 1))
try self.insert(maximumCount: self.freeCapacity) { target throws(E) in
while !target.isFull, !done {
done = try !producer.generate(into: &target)
}
}
}
}
#endif
#if UnstableContainersPreview
@_alwaysEmitIntoClient
public mutating func insert<
D: Drain<Element> & ~Copyable & ~Escapable
>(
from drain: inout D
) {
while true {
var span = drain.drainNext()
guard !span.isEmpty else { break }
while let next = span.popFirst() {
self.insert(next)
}
}
}
#endif
}
@available(SwiftStdlib 5.0, *)
extension UniqueSet /* where Element: Copyable */ {
@_alwaysEmitIntoClient
public mutating func insert(
copying items: borrowing Span<Element>
) {
_ensureFreeCapacity(items.count)
_storage.insert(copying: items)
}
#if UnstableContainersPreview
@_alwaysEmitIntoClient
package mutating func _insert<
S: Iterable_ & ~Copyable & ~Escapable
>(
copying items: borrowing S
) throws(S.Failure_)
where S.Element_ == Element {
_ensureFreeCapacity(items.underestimatedCount_)
var it = items.makeIterableIterator_()
while true {
let span = try it.nextSpan_()
guard !span.isEmpty else { break }
self.insert(copying: span)
}
}
#endif
#if UnstableContainersPreview
@_alwaysEmitIntoClient
@inline(__always)
public mutating func insert<
S: Iterable_ & ~Copyable & ~Escapable
>(
copying items: borrowing S
) throws(S.Failure_) where S.Element_ == Element {
try _insert(copying: items)
}
#endif
@_alwaysEmitIntoClient
@inline(__always)
public mutating func insert(copying items: some Sequence<Element>) {
_ensureFreeCapacity(items.underestimatedCount)
var it = items.makeIterator()
while let next = it.next() {
self.insert(next)
}
}
#if UnstableContainersPreview
@_alwaysEmitIntoClient
@inline(__always)
public mutating func insert<
S: Iterable_ & Sequence<Element>
>(
copying items: borrowing S
) throws(S.Failure_)
where S.Element_ == Element {
try _insert(copying: items)
}
#endif
}
#endif