feat(container/gtree): Add the function of popping left and right nodes of red-black tree - #4793
Open
LanceAdd wants to merge 5 commits into
Open
feat(container/gtree): Add the function of popping left and right nodes of red-black tree#4793LanceAdd wants to merge 5 commits into
LanceAdd wants to merge 5 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds atomic “pop” operations to GoFrame’s red-black tree containers (container/gtree), enabling callers to remove-and-return the current minimum/maximum element in a single concurrency-safe operation (aligning with garray.PopLeft/PopRight usage patterns).
Changes:
- Add
PopLeft/PopRighttoRedBlackKVTree[K,V], removing and returning min/max nodes under a write lock. - Add
PopLeft/PopRightto non-genericRedBlackTree, delegating to the generic implementation afterlazyInit().
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
container/gtree/gtree_k_v_redblacktree.go |
Implements PopLeft/PopRight on the generic red-black tree with write locking and internal removal. |
container/gtree/gtree_redblacktree.go |
Adds non-generic PopLeft/PopRight wrappers delegating to the generic tree after lazy initialization. |
Comments suppressed due to low confidence (1)
container/gtree/gtree_k_v_redblacktree.go:548
- PopRight already has the right-most node (including its Value), but calling doRemove(node.Key) does an extra tree.Get before removing. This adds an extra O(log n) lookup on every pop; you can remove the key directly here since the value is already known.
node := tree.tree.Right()
if node == nil {
return nil
}
tree.doRemove(node.Key)
return &RedBlackKVTreeNode[K, V]{
Key: node.Key,
Value: node.Value,
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
摘要
为
RedBlackKVTree(泛型)和RedBlackTree(非泛型)新增PopLeft和PopRight方法,支持原子地弹出最小和最大节点。动机
现有的
Left/Right方法只能查看极值节点,删除需要再调用Remove(node.Key)。这两步之间存在竞争窗口:该模式与
garray.PopLeft/garray.PopRight保持一致,使RedBlackKVTree可以作为并发安全的优先级队列使用,替代container/heap且具有更好的类型安全。实现
两个方法在内部持有写锁,并直接操作底层数据结构,避免调用公开的
Left/Right(会重复加锁导致死锁):变更文件
container/gtree/gtree_k_v_redblacktree.goPopLeft、PopRightcontainer/gtree/gtree_redblacktree.go示例