-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from lorentey/PersistentCollections-updates
Persistent collections updates (part 4)
- Loading branch information
Showing
42 changed files
with
1,849 additions
and
1,573 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift Collections open source project | ||
// | ||
// Copyright (c) 2022 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// An abstract representation of a hash value. | ||
@usableFromInline | ||
@frozen | ||
internal struct _Hash { | ||
@usableFromInline | ||
internal var value: UInt | ||
|
||
@inlinable | ||
internal init<Key: Hashable>(_ key: Key) { | ||
let hashValue = key._rawHashValue(seed: 0) | ||
self.value = UInt(bitPattern: hashValue) | ||
} | ||
|
||
@inlinable | ||
internal init(_value: UInt) { | ||
self.value = _value | ||
} | ||
} | ||
|
||
extension _Hash: Equatable { | ||
@inlinable @inline(__always) | ||
internal static func ==(left: Self, right: Self) -> Bool { | ||
left.value == right.value | ||
} | ||
} | ||
|
||
extension _Hash: CustomStringConvertible { | ||
@usableFromInline | ||
internal var description: String { | ||
// Print hash values in radix 32 & reversed, so that the path in the hash | ||
// tree is readily visible. | ||
let p = String(value, radix: _Bitmap.capacity, uppercase: true) | ||
let c = _Level.limit | ||
let path = String(repeating: "0", count: Swift.max(0, c - p.count)) + p | ||
return String(path.reversed()) | ||
} | ||
} | ||
|
||
|
||
extension _Hash { | ||
@inlinable @inline(__always) | ||
internal static var bitWidth: Int { UInt.bitWidth } | ||
} | ||
|
||
extension _Hash { | ||
@inlinable | ||
internal subscript(_ level: _Level) -> _Bucket { | ||
get { | ||
assert(!level.isAtBottom) | ||
return _Bucket((value &>> level.shift) & _Bucket.bitMask) | ||
} | ||
set { | ||
let mask = _Bucket.bitMask &<< level.shift | ||
self.value &= ~mask | ||
self.value |= newValue.value &<< level.shift | ||
} | ||
} | ||
} | ||
|
||
extension _Hash { | ||
@inlinable | ||
internal func appending(_ bucket: _Bucket, at level: _Level) -> Self { | ||
assert(value >> level.shift == 0) | ||
var copy = self | ||
copy[level] = bucket | ||
return copy | ||
} | ||
|
||
@inlinable | ||
internal func isEqual(to other: _Hash, upTo level: _Level) -> Bool { | ||
if level.isAtRoot { return true } | ||
if level.isAtBottom { return self == other } | ||
let s = UInt(UInt.bitWidth) - level.shift | ||
let v1 = self.value &<< s | ||
let v2 = self.value &<< s | ||
return v1 == v2 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift Collections open source project | ||
// | ||
// Copyright (c) 2022 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import _CollectionsUtilities | ||
|
||
extension _Node { | ||
@usableFromInline | ||
internal func dump( | ||
firstPrefix: String = "", restPrefix: String = "", limit: Int = Int.max | ||
) { | ||
read { | ||
$0.dump( | ||
firstPrefix: firstPrefix, | ||
restPrefix: restPrefix, | ||
extra: "count: \(count), ", | ||
limit: limit) | ||
} | ||
} | ||
} | ||
|
||
extension _Node.Storage { | ||
@usableFromInline | ||
final internal func dump() { | ||
UnsafeHandle.read(self) { $0.dump() } | ||
} | ||
} | ||
|
||
extension _Node.UnsafeHandle { | ||
@usableFromInline | ||
internal func dump( | ||
firstPrefix: String = "", | ||
restPrefix: String = "", | ||
extra: String = "", | ||
limit: Int = Int.max | ||
) { | ||
print(""" | ||
\(firstPrefix)\(isCollisionNode ? "CollisionNode" : "Node")(\ | ||
at: \(_addressString(for: _header)), \ | ||
\(extra)\ | ||
byteCapacity: \(byteCapacity), \ | ||
freeBytes: \(bytesFree)) | ||
""") | ||
guard limit > 0 else { return } | ||
if isCollisionNode { | ||
let items = self._items | ||
for offset in items.indices { | ||
let item = items[offset] | ||
let hash = _Hash(item.key).description | ||
let itemStr = "hash: \(hash), key: \(item.key), value: \(item.value)" | ||
print("\(restPrefix) \(offset): \(itemStr)") | ||
} | ||
} else { | ||
var itemOffset = 0 | ||
var childOffset = 0 | ||
for b in 0 ..< UInt(_Bitmap.capacity) { | ||
let bucket = _Bucket(b) | ||
let bucketStr = "#\(String(b, radix: _Bitmap.capacity, uppercase: true))" | ||
if itemMap.contains(bucket) { | ||
let item = self[item: itemOffset] | ||
let hash = _Hash(item.key).description | ||
let itemStr = "hash: \(hash), key: \(item.key), value: \(item.value)" | ||
print("\(restPrefix) \(bucketStr) \(itemStr)") | ||
itemOffset += 1 | ||
} else if childMap.contains(bucket) { | ||
self[child: childOffset].dump( | ||
firstPrefix: "\(restPrefix) \(bucketStr) ", | ||
restPrefix: "\(restPrefix) ", | ||
limit: limit - 1) | ||
childOffset += 1 | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.