-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathHashing.swift
166 lines (148 loc) · 5.65 KB
/
Hashing.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements helpers for hashing collections.
//
import SwiftShims
/// The inverse of the default hash table load factor. Factored out so that it
/// can be used in multiple places in the implementation and stay consistent.
/// Should not be used outside `Dictionary` implementation.
@usableFromInline @_transparent
internal var _hashContainerDefaultMaxLoadFactorInverse: Double {
return 1.0 / 0.75
}
#if _runtime(_ObjC)
/// Call `[lhs isEqual: rhs]`.
///
/// This function is part of the runtime because `Bool` type is bridged to
/// `ObjCBool`, which is in Foundation overlay.
@_silgen_name("swift_stdlib_NSObject_isEqual")
internal func _stdlib_NSObject_isEqual(_ lhs: AnyObject, _ rhs: AnyObject) -> Bool
#endif
/// A temporary view of an array of AnyObject as an array of Unmanaged<AnyObject>
/// for fast iteration and transformation of the elements.
///
/// Accesses the underlying raw memory as Unmanaged<AnyObject> using untyped
/// memory accesses. The memory remains bound to managed AnyObjects.
@unsafe
internal struct _UnmanagedAnyObjectArray {
/// Underlying pointer.
internal var value: UnsafeMutableRawPointer
internal init(_ up: UnsafeMutablePointer<AnyObject>) {
unsafe self.value = UnsafeMutableRawPointer(up)
}
internal init?(_ up: UnsafeMutablePointer<AnyObject>?) {
guard let unwrapped = unsafe up else { return nil }
unsafe self.init(unwrapped)
}
internal subscript(i: Int) -> AnyObject {
get {
let unmanaged = unsafe value.load(
fromByteOffset: i * MemoryLayout<AnyObject>.stride,
as: Unmanaged<AnyObject>.self)
return unsafe unmanaged.takeUnretainedValue()
}
nonmutating set(newValue) {
let unmanaged = unsafe Unmanaged.passUnretained(newValue)
unsafe value.storeBytes(of: unmanaged,
toByteOffset: i * MemoryLayout<AnyObject>.stride,
as: Unmanaged<AnyObject>.self)
}
}
}
#if _runtime(_ObjC)
/// An NSEnumerator implementation returning zero elements. This is useful when
/// a concrete element type is not recoverable from the empty singleton.
// NOTE: older runtimes called this class _SwiftEmptyNSEnumerator. The two
// must coexist without conflicting ObjC class names, so it was
// renamed. The old name must not be used in the new runtime.
final internal class __SwiftEmptyNSEnumerator
: __SwiftNativeNSEnumerator, _NSEnumerator {
internal override required init() {
super.init()
_internalInvariant(_orphanedFoundationSubclassesReparented)
}
@objc
internal func nextObject() -> AnyObject? {
return nil
}
@objc(countByEnumeratingWithState:objects:count:)
internal func countByEnumerating(
with state: UnsafeMutablePointer<_SwiftNSFastEnumerationState>,
objects: UnsafeMutablePointer<AnyObject>,
count: Int
) -> Int {
// Even though we never do anything in here, we need to update the
// state so that callers know we actually ran.
var theState = unsafe state.pointee
if unsafe theState.state == 0 {
unsafe theState.state = 1 // Arbitrary non-zero value.
unsafe theState.itemsPtr = AutoreleasingUnsafeMutablePointer(objects)
unsafe theState.mutationsPtr = _fastEnumerationStorageMutationsPtr
}
unsafe state.pointee = theState
return 0
}
}
#endif
#if _runtime(_ObjC)
/// This is a minimal class holding a single tail-allocated flat buffer,
/// representing hash table storage for AnyObject elements. This is used to
/// store bridged elements in deferred bridging scenarios.
///
/// Using a dedicated class for this rather than a _BridgingBuffer makes it easy
/// to recognize these in heap dumps etc.
// NOTE: older runtimes called this class _BridgingHashBuffer.
// The two must coexist without a conflicting ObjC class name, so it
// was renamed. The old name must not be used in the new runtime.
@unsafe
internal final class __BridgingHashBuffer
: ManagedBuffer<__BridgingHashBuffer.Header, AnyObject> {
@unsafe
struct Header {
internal var owner: AnyObject
internal var hashTable: _HashTable
init(owner: AnyObject, hashTable: _HashTable) {
unsafe self.owner = owner
unsafe self.hashTable = unsafe hashTable
}
}
internal static func allocate(
owner: AnyObject,
hashTable: _HashTable
) -> __BridgingHashBuffer {
let buffer = unsafe self.create(minimumCapacity: hashTable.bucketCount) { _ in
unsafe Header(owner: owner, hashTable: hashTable)
}
return unsafe unsafeDowncast(buffer, to: __BridgingHashBuffer.self)
}
deinit {
for unsafe bucket in unsafe header.hashTable {
unsafe (firstElementAddress + bucket.offset).deinitialize(count: 1)
}
unsafe _fixLifetime(self)
}
internal subscript(bucket: _HashTable.Bucket) -> AnyObject {
@inline(__always) get {
unsafe _internalInvariant(header.hashTable.isOccupied(bucket))
defer { unsafe _fixLifetime(self) }
return unsafe firstElementAddress[bucket.offset]
}
}
@inline(__always)
internal func initialize(at bucket: _HashTable.Bucket, to object: AnyObject) {
unsafe _internalInvariant(header.hashTable.isOccupied(bucket))
unsafe (firstElementAddress + bucket.offset).initialize(to: object)
unsafe _fixLifetime(self)
}
}
#endif