1
- //===----------- CacheLookupTable .swift - Swift Syntax Library -----------===//
1
+ //===----------- WeakLookupTable .swift - Swift Syntax Library -- -----------===//
2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
9
9
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
10
//
11
11
//===----------------------------------------------------------------------===//
12
- // This file provides lookup table for cached object .
12
+ // This file provides lookup table with weak reference .
13
13
//===----------------------------------------------------------------------===//
14
14
15
15
/// Protocol for self-identifiable object
@@ -35,10 +35,10 @@ private struct WeakReference<T: AnyObject>: ExpressibleByNilLiteral {
35
35
/// References are stored in a hash table with simple open adressing. Because
36
36
/// of weak reference, unlike normal open addressing, erased bucket are simply
37
37
/// turned into 'nil'.
38
- class CacheLookupTable < T : Identifiable & AnyObject > {
38
+ class WeakLookupTable < Element : Identifiable & AnyObject > {
39
39
40
40
/// Storage for the hash table.
41
- private var buckets : UnsafeMutablePointer < WeakReference < T > >
41
+ private var buckets : UnsafeMutablePointer < WeakReference < Element > >
42
42
private var bucketCount : Int
43
43
44
44
/// Estimated count of inserted values. This is greater than or equal to
@@ -47,7 +47,7 @@ class CacheLookupTable<T: Identifiable & AnyObject> {
47
47
private var estimatedCount : Int
48
48
49
49
init ( capacity: Int = 0 ) {
50
- bucketCount = CacheLookupTable < T > . _bucketCount( for: capacity)
50
+ bucketCount = WeakLookupTable < Element > . _bucketCount( for: capacity)
51
51
buckets = . allocate( capacity: bucketCount)
52
52
buckets. initialize ( repeating: nil , count: bucketCount)
53
53
estimatedCount = 0
@@ -98,13 +98,13 @@ class CacheLookupTable<T: Identifiable & AnyObject> {
98
98
}
99
99
100
100
@inline ( __always)
101
- private func _idealBucket( for id: T . Identifier ) -> Int {
101
+ private func _idealBucket( for id: Element . Identifier ) -> Int {
102
102
return id. hashValue & _bucketMask
103
103
}
104
104
105
105
/// Finds the bucket where the object with the specified id should be stored
106
106
/// to.
107
- private func _findHole( _ id: T . Identifier ) -> ( pos: Int , found: Bool ) {
107
+ private func _findHole( _ id: Element . Identifier ) -> ( pos: Int , found: Bool ) {
108
108
var bucket = _idealBucket ( for: id)
109
109
110
110
// Starting from the ideal bucket for the id, search an available bucket,
@@ -123,8 +123,8 @@ class CacheLookupTable<T: Identifiable & AnyObject> {
123
123
/// Reserves enough space to store the specified number of elements. Returns
124
124
/// true if resizing happened.
125
125
func reserveCapacity( _ requiredCapacity: Int ) -> Bool {
126
- let requiredBucketCount =
127
- CacheLookupTable < T > . _bucketCount( for: requiredCapacity, from: bucketCount)
126
+ let requiredBucketCount = WeakLookupTable < Element >
127
+ . _bucketCount( for: requiredCapacity, from: bucketCount)
128
128
if ( bucketCount >= requiredBucketCount) {
129
129
return false
130
130
}
@@ -165,7 +165,7 @@ class CacheLookupTable<T: Identifiable & AnyObject> {
165
165
/// Reserves enough space to store a single new object. Returns true if
166
166
/// resizing happened.
167
167
private func _ensurePlusOneCapacity( ) -> Bool {
168
- if bucketCount >= CacheLookupTable < T >
168
+ if bucketCount >= WeakLookupTable < Element >
169
169
. _minimalBucketCount ( for: estimatedCount &+ 1 ) {
170
170
return false
171
171
}
@@ -177,7 +177,7 @@ class CacheLookupTable<T: Identifiable & AnyObject> {
177
177
178
178
/// Inserts the given object into the table.
179
179
@discardableResult
180
- func insert( _ obj: T ) -> Bool {
180
+ func insert( _ obj: Element ) -> Bool {
181
181
var ( pos, found) = _findHole ( obj. id)
182
182
if found {
183
183
return false
@@ -193,7 +193,7 @@ class CacheLookupTable<T: Identifiable & AnyObject> {
193
193
194
194
/// Get a object with specified id. Returns 'nil' if the object hasn't been
195
195
/// insert()-ed or it's already been freed.
196
- public subscript( id: T . Identifier ) -> T ? {
196
+ public subscript( id: Element . Identifier ) -> Element ? {
197
197
// Since we don't fill the bucket when the object is freed (because we don't
198
198
// know), we can't stop iteration at a hole. So in the worst case (i.e. if
199
199
// the object doesn't exist in the table), full linear search is needed.
0 commit comments