Files
Tony Parker c883a1729a Add support for embedded Swift mode
Remove calls to `debugPrint` (which does not exist), and ifdef out support for `Codable` and `Reflection`.
2025-07-17 16:36:56 -07:00

49 lines
1.8 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 - 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 !$Embedded
extension OrderedSet: Encodable where Element: Encodable {
/// Encodes the elements of this ordered set into the given encoder.
///
/// - Parameter encoder: The encoder to write data to.
@inlinable
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(_elements)
}
}
extension OrderedSet: Decodable where Element: Decodable {
/// Creates a new ordered set by decoding from the given decoder.
///
/// This initializer throws an error if reading from the decoder fails, or
/// if the decoded contents contain duplicate values.
///
/// - Parameter decoder: The decoder to read data from.
@inlinable
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let elements = try container.decode(ContiguousArray<Element>.self)
let (table, end) = _HashTable.create(untilFirstDuplicateIn: elements)
guard end == elements.endIndex else {
let context = DecodingError.Context(
codingPath: container.codingPath,
debugDescription: "Decoded elements aren't unique (first duplicate at offset \(end))")
throw DecodingError.dataCorrupted(context)
}
self.init(
_uniqueElements: elements,
elements.count > _HashTable.maximumUnhashedCount ? table : nil)
}
}
#endif