Skip to content

Commit 5610c34

Browse files
committed
[WIP] Rename CacheLookupTable<T> to WeakLookupTable<Element>
1 parent 6fc9570 commit 5610c34

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

Sources/SwiftSyntax/CacheLookupTable.swift renamed to Sources/SwiftSyntax/WeakLookupTable.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===----------- CacheLookupTable.swift - Swift Syntax Library -----------===//
1+
//===----------- WeakLookupTable.swift - Swift Syntax Library -------------===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -9,7 +9,7 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12-
// This file provides lookup table for cached object.
12+
// This file provides lookup table with weak reference.
1313
//===----------------------------------------------------------------------===//
1414

1515
/// Protocol for self-identifiable object
@@ -35,10 +35,10 @@ private struct WeakReference<T: AnyObject>: ExpressibleByNilLiteral {
3535
/// References are stored in a hash table with simple open adressing. Because
3636
/// of weak reference, unlike normal open addressing, erased bucket are simply
3737
/// turned into 'nil'.
38-
class CacheLookupTable<T: Identifiable & AnyObject> {
38+
class WeakLookupTable<Element: Identifiable & AnyObject> {
3939

4040
/// Storage for the hash table.
41-
private var buckets: UnsafeMutablePointer<WeakReference<T>>
41+
private var buckets: UnsafeMutablePointer<WeakReference<Element>>
4242
private var bucketCount: Int
4343

4444
/// Estimated count of inserted values. This is greater than or equal to
@@ -47,7 +47,7 @@ class CacheLookupTable<T: Identifiable & AnyObject> {
4747
private var estimatedCount: Int
4848

4949
init(capacity: Int = 0) {
50-
bucketCount = CacheLookupTable<T>._bucketCount(for: capacity)
50+
bucketCount = WeakLookupTable<Element>._bucketCount(for: capacity)
5151
buckets = .allocate(capacity: bucketCount)
5252
buckets.initialize(repeating: nil, count: bucketCount)
5353
estimatedCount = 0
@@ -98,13 +98,13 @@ class CacheLookupTable<T: Identifiable & AnyObject> {
9898
}
9999

100100
@inline(__always)
101-
private func _idealBucket(for id: T.Identifier) -> Int {
101+
private func _idealBucket(for id: Element.Identifier) -> Int {
102102
return id.hashValue & _bucketMask
103103
}
104104

105105
/// Finds the bucket where the object with the specified id should be stored
106106
/// to.
107-
private func _findHole(_ id: T.Identifier) -> (pos: Int, found: Bool) {
107+
private func _findHole(_ id: Element.Identifier) -> (pos: Int, found: Bool) {
108108
var bucket = _idealBucket(for: id)
109109

110110
// Starting from the ideal bucket for the id, search an available bucket,
@@ -123,8 +123,8 @@ class CacheLookupTable<T: Identifiable & AnyObject> {
123123
/// Reserves enough space to store the specified number of elements. Returns
124124
/// true if resizing happened.
125125
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)
128128
if (bucketCount >= requiredBucketCount) {
129129
return false
130130
}
@@ -165,7 +165,7 @@ class CacheLookupTable<T: Identifiable & AnyObject> {
165165
/// Reserves enough space to store a single new object. Returns true if
166166
/// resizing happened.
167167
private func _ensurePlusOneCapacity() -> Bool {
168-
if bucketCount >= CacheLookupTable<T>
168+
if bucketCount >= WeakLookupTable<Element>
169169
._minimalBucketCount(for: estimatedCount &+ 1) {
170170
return false
171171
}
@@ -177,7 +177,7 @@ class CacheLookupTable<T: Identifiable & AnyObject> {
177177

178178
/// Inserts the given object into the table.
179179
@discardableResult
180-
func insert(_ obj: T) -> Bool {
180+
func insert(_ obj: Element) -> Bool {
181181
var (pos, found) = _findHole(obj.id)
182182
if found {
183183
return false
@@ -193,7 +193,7 @@ class CacheLookupTable<T: Identifiable & AnyObject> {
193193

194194
/// Get a object with specified id. Returns 'nil' if the object hasn't been
195195
/// insert()-ed or it's already been freed.
196-
public subscript(id: T.Identifier) -> T? {
196+
public subscript(id: Element.Identifier) -> Element? {
197197
// Since we don't fill the bucket when the object is freed (because we don't
198198
// know), we can't stop iteration at a hole. So in the worst case (i.e. if
199199
// the object doesn't exist in the table), full linear search is needed.

0 commit comments

Comments
 (0)