-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNBKDoubleWidth+Words+Pointers.swift
72 lines (64 loc) · 2.85 KB
/
NBKDoubleWidth+Words+Pointers.swift
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
//=----------------------------------------------------------------------------=
// This source file is part of the Numberick open source project.
//
// Copyright (c) 2023 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=
import NBKCoreKit
//*============================================================================*
// MARK: * NBK x Double Width x Words x Pointers
//*============================================================================*
extension NBKDoubleWidth {
//=------------------------------------------------------------------------=
// MARK: Details x Contiguous UInt Collection
//=------------------------------------------------------------------------=
/// Grants unsafe access to the collection's contiguous storage.
///
/// The elements of the contiguous storage appear in the order of the collection.
///
@inlinable public func withContiguousStorage<T>(
_ body: (UnsafeBufferPointer<UInt>) throws -> T) rethrows -> T {
var base = self; return try base.withUnsafeMutableData(as: UInt.self) { data in
#if _endian(big)
data.reverse()
#endif
return try body(UnsafeBufferPointer(data))
}
}
/// Grants unsafe access to the collection's contiguous storage.
///
/// The elements of the contiguous storage appear in the order of the collection.
///
/// - Note: This member is required by `Swift.Sequence`.
///
@inlinable public func withContiguousStorageIfAvailable<T>(
_ body: (UnsafeBufferPointer<UInt>) throws -> T) rethrows -> T? {
try self.withContiguousStorage(body)
}
/// Grants unsafe access to the collection's contiguous mutable storage.
///
/// The elements of the contiguous mutable storage appear in the order of the collection.
///
@inlinable public mutating func withContiguousMutableStorage<T>(
_ body: (inout UnsafeMutableBufferPointer<UInt>) throws -> T) rethrows -> T {
return try self.withUnsafeMutableData(as: UInt.self) { data in
#if _endian(big)
do { data.reverse() }
defer { data.reverse() }
#endif
return try body(&data)
}
}
/// Grants unsafe access to the collection's contiguous mutable storage.
///
/// The elements of the contiguous mutable storage appear in the order of the collection.
///
/// - Note: This member is required by `Swift.MutableCollection`.
///
@inlinable public mutating func withContiguousMutableStorageIfAvailable<T>(
_ body: (inout UnsafeMutableBufferPointer<UInt>) throws -> T) rethrows -> T? {
try self.withContiguousMutableStorage(body)
}
}