Skip to content

Commit ab6b153

Browse files
committed
Simplify ExpressionID-to-keypath code
1 parent 28a7e0d commit ab6b153

File tree

1 file changed

+51
-56
lines changed

1 file changed

+51
-56
lines changed

Sources/Testing/SourceAttribution/ExpressionID.swift

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public struct __ExpressionID: Sendable {
2929

3030
/// An enumeration that attempts to efficiently store the key path elements
3131
/// corresponding to an expression ID.
32+
///
33+
/// Instances of this type can be used to produce keys and key paths for an
34+
/// instance of `Graph` whose key type is `UInt32`.
3235
fileprivate enum Elements: Sendable {
3336
/// This ID does not use any words.
3437
///
@@ -56,79 +59,71 @@ extension __ExpressionID.Elements: Equatable, Hashable {}
5659

5760
// MARK: - Collection
5861

59-
extension __ExpressionID {
60-
/// A type representing the elements in a key path produced from the unique
61-
/// identifier of an expression.
62-
///
63-
/// Instances of this type can be used to produce keys and key paths for an
64-
/// instance of `Graph` whose key type is `UInt32`.
65-
private struct _KeyPathForGraph: Collection {
66-
/// Underlying storage for the collection.
67-
var elements: __ExpressionID.Elements
68-
69-
var count: Int {
70-
switch elements {
71-
case .none:
72-
0
73-
case let .packed(word):
74-
word.nonzeroBitCount
75-
case let .keyPath(keyPath):
76-
keyPath.count
77-
}
62+
extension __ExpressionID.Elements: Collection {
63+
var count: Int {
64+
switch self {
65+
case .none:
66+
0
67+
case let .packed(word):
68+
word.nonzeroBitCount
69+
case let .keyPath(keyPath):
70+
keyPath.count
7871
}
72+
}
7973

80-
var startIndex: Int {
81-
switch elements {
82-
case .none, .keyPath:
83-
0
84-
case let .packed(word):
85-
word.trailingZeroBitCount
86-
}
74+
var startIndex: Int {
75+
switch self {
76+
case .none, .keyPath:
77+
0
78+
case let .packed(word):
79+
word.trailingZeroBitCount
8780
}
81+
}
8882

89-
var endIndex: Int {
90-
switch elements {
91-
case .none:
92-
0
93-
case .packed:
94-
UInt64.bitWidth
95-
case let .keyPath(keyPath):
96-
keyPath.count
97-
}
83+
var endIndex: Int {
84+
switch self {
85+
case .none:
86+
0
87+
case .packed:
88+
UInt64.bitWidth
89+
case let .keyPath(keyPath):
90+
keyPath.count
9891
}
92+
}
9993

100-
func index(after i: Int) -> Int {
101-
let uncheckedNextIndex = i + 1
102-
switch elements {
103-
case .none, .keyPath:
104-
return uncheckedNextIndex
105-
case let .packed(word):
106-
// Mask off the low bits including the one at `i`. The trailing zero
107-
// count of the resulting value equals the next actual bit index.
108-
let maskedWord = word & (~0 << uncheckedNextIndex)
109-
return maskedWord.trailingZeroBitCount
110-
}
94+
func index(after i: Int) -> Int {
95+
let uncheckedNextIndex = i + 1
96+
switch self {
97+
case .none, .keyPath:
98+
return uncheckedNextIndex
99+
case let .packed(word):
100+
// Mask off the low bits including the one at `i`. The trailing zero
101+
// count of the resulting value equals the next actual bit index.
102+
let maskedWord = word & (~0 << uncheckedNextIndex)
103+
return maskedWord.trailingZeroBitCount
111104
}
105+
}
112106

113-
subscript(position: Int) -> UInt32 {
114-
switch elements {
115-
case .none:
116-
swt_unreachable()
117-
case .packed:
118-
UInt32(position)
119-
case let .keyPath(keyPath):
120-
keyPath[position]
121-
}
107+
subscript(position: Int) -> UInt32 {
108+
switch self {
109+
case .none:
110+
swt_unreachable()
111+
case .packed:
112+
UInt32(position)
113+
case let .keyPath(keyPath):
114+
keyPath[position]
122115
}
123116
}
117+
}
124118

119+
extension __ExpressionID {
125120
/// A representation of this instance suitable for use as a key path in an
126121
/// instance of `Graph` where the key type is `UInt32`.
127122
///
128123
/// The values in this collection, being swift-syntax node IDs, are never more
129124
/// than 32 bits wide.
130125
var keyPathRepresentation: some Collection<UInt32> {
131-
_KeyPathForGraph(elements: elements)
126+
elements
132127
}
133128
}
134129

0 commit comments

Comments
 (0)