-
Notifications
You must be signed in to change notification settings - Fork 305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Persistent collections updates (part 7) #182
Merged
lorentey
merged 11 commits into
apple:release/1.1
from
lorentey:PersistentCollections-updates
Sep 23, 2022
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e19ba8e
[Utilities] Adjust Swift 5.7 version check
lorentey c355812
[PersistentDictionary] Small cleanups
lorentey 04b134e
[PersistentCollections] Fix memory management for the hash tree iterator
lorentey 958fd47
[PersistentSet] Start drafting the new type
lorentey 24619df
Update Sources/PersistentCollections/Node/_Node+isDisjoint.swift
lorentey f2539a4
Update Sources/PersistentCollections/Node/_Stack.swift
lorentey f79fd63
[PersistentCollections] _Node.isDisjoint: Add new fast path
lorentey de13650
[PersistentCollections] _Node.isEqual: Don’t allow customizing key co…
lorentey 94d979c
[Benchmarks] Fix header name in #include
lorentey 1184191
Fix dependency declarations in package manifest
lorentey 59858a7
[BitSet.Counted] Fix invariant checking (d’oh)
lorentey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
129 changes: 129 additions & 0 deletions
129
Sources/PersistentCollections/Node/_HashTreeIterator.swift
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,129 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@usableFromInline | ||
@frozen | ||
internal struct _HashTreeIterator { | ||
@usableFromInline | ||
internal struct _Opaque { | ||
internal var ancestorSlots: _AncestorSlots | ||
internal var ancestorNodes: _Stack<_UnmanagedNode> | ||
internal var level: _Level | ||
internal var isAtEnd: Bool | ||
|
||
@usableFromInline | ||
@_effects(releasenone) | ||
internal init(_ root: _UnmanagedNode) { | ||
self.ancestorSlots = .empty | ||
self.ancestorNodes = _Stack(filledWith: root) | ||
self.level = .top | ||
self.isAtEnd = false | ||
} | ||
} | ||
|
||
@usableFromInline | ||
internal let root: _RawStorage | ||
|
||
@usableFromInline | ||
internal var node: _UnmanagedNode | ||
|
||
@usableFromInline | ||
internal var slot: _Slot | ||
|
||
@usableFromInline | ||
internal var endSlot: _Slot | ||
|
||
@usableFromInline | ||
internal var _o: _Opaque | ||
|
||
@usableFromInline | ||
@_effects(releasenone) | ||
internal init(root: __shared _RawNode) { | ||
self.root = root.storage | ||
self.node = root.unmanaged | ||
self.slot = .zero | ||
self.endSlot = node.itemsEndSlot | ||
self._o = _Opaque(self.node) | ||
|
||
if node.hasItems { return } | ||
if node.hasChildren { | ||
_descendToLeftmostItem(ofChildAtSlot: .zero) | ||
} else { | ||
self._o.isAtEnd = true | ||
} | ||
} | ||
} | ||
|
||
extension _HashTreeIterator: IteratorProtocol { | ||
@inlinable | ||
internal mutating func next() -> (node: _UnmanagedNode, slot: _Slot)? { | ||
guard slot < endSlot else { | ||
return _next() | ||
} | ||
defer { slot = slot.next() } | ||
return (node, slot) | ||
} | ||
|
||
@usableFromInline | ||
@_effects(releasenone) | ||
internal mutating func _next() -> (node: _UnmanagedNode, slot: _Slot)? { | ||
if _o.isAtEnd { return nil } | ||
if node.hasChildren { | ||
_descendToLeftmostItem(ofChildAtSlot: .zero) | ||
slot = slot.next() | ||
return (node, .zero) | ||
} | ||
while !_o.level.isAtRoot { | ||
let nextChild = _ascend().next() | ||
if nextChild < node.childrenEndSlot { | ||
_descendToLeftmostItem(ofChildAtSlot: nextChild) | ||
slot = slot.next() | ||
return (node, .zero) | ||
} | ||
} | ||
// At end | ||
endSlot = node.itemsEndSlot | ||
slot = endSlot | ||
_o.isAtEnd = true | ||
return nil | ||
} | ||
} | ||
|
||
extension _HashTreeIterator { | ||
internal mutating func _descend(toChildSlot childSlot: _Slot) { | ||
assert(childSlot < node.childrenEndSlot) | ||
_o.ancestorSlots[_o.level] = childSlot | ||
_o.ancestorNodes.push(node) | ||
_o.level = _o.level.descend() | ||
node = node.unmanagedChild(at: childSlot) | ||
slot = .zero | ||
endSlot = node.itemsEndSlot | ||
} | ||
|
||
internal mutating func _ascend() -> _Slot { | ||
assert(!_o.level.isAtRoot) | ||
node = _o.ancestorNodes.pop() | ||
_o.level = _o.level.ascend() | ||
let childSlot = _o.ancestorSlots[_o.level] | ||
_o.ancestorSlots.clear(_o.level) | ||
return childSlot | ||
} | ||
|
||
internal mutating func _descendToLeftmostItem( | ||
ofChildAtSlot childSlot: _Slot | ||
) { | ||
_descend(toChildSlot: childSlot) | ||
while endSlot == .zero { | ||
assert(node.hasChildren) | ||
_descend(toChildSlot: .zero) | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍