1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 - 2024 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
//
//===----------------------------------------------------------------------===//
#if !COLLECTIONS_SINGLE_MODULE
import InternalCollectionsUtilities
#endif
extension TreeSet {
/// Creates an empty set.
///
/// This initializer is equivalent to initializing with an empty array
/// literal.
///
/// - Complexity: O(1)
@inlinable
public init() {
self.init(_new: ._emptyNode())
}
/// Creates a new set from a finite sequence of items.
///
/// - Parameter items: The elements to use as members of the new set.
/// The sequence is allowed to contain duplicate elements, but only
/// the first duplicate instance is preserved in the result.
///
/// - Complexity: This operation is expected to perform O(*n*)
/// hashing and equality comparisons on average (where *n*
/// is the number of elements in the sequence), provided that
/// `Element` properly implements hashing.
@inlinable
public init(_ items: __owned some Sequence<Element>) {
if let items = _specialize(items, for: Self.self) {
self = items
return
}
self.init()
for item in items {
self._insert(item)
}
}
/// Creates a new set from a an existing set. This is functionally the same as
/// copying the value of `items` into a new variable.
///
/// - Parameter items: The elements to use as members of the new set.
///
/// - Complexity: O(1)
@inlinable
public init(_ items: __owned Self) {
self = items
}
/// Creates a new persistent set from the keys view of an existing persistent
/// dictionary.
///
/// - Parameter items: The elements to use as members of the new set.
///
/// - Complexity: O(*items.count*)
@inlinable
public init<Value>(
_ item: __owned TreeDictionary<Element, Value>.Keys
) {
self.init(_new: item._base._root.mapValues { _ in () })
}
}
|