feat(container/gtree): add generic tree feature#4522
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds generic tree features to the GoFrame container package, introducing type-safe generic variants of existing tree data structures (RedBlackTree, BTree, AVLTree) while maintaining backward compatibility through wrapper types.
Key Changes:
- Upgraded
github.com/emirpasic/godsfrom v1 to v2 (alpha) to support generics - Added new generic tree implementations:
RedBlackKVTree[K, V],BKVTree[K, V], andAVLKVTree[K, V] - Refactored existing non-generic trees (
RedBlackTree,BTree,AVLTree) as wrappers around generic variants - Added generic
TreeKVMap[K, V]to gmap package alongside non-genericTreeMapalias
Reviewed changes
Copilot reviewed 10 out of 11 changed files in this pull request and generated 11 comments.
Show a summary per file
| File | Description |
|---|---|
| go.mod / go.sum | Upgraded emirpasic/gods dependency from v1.18.1 to v2.0.0-alpha for generic support |
| container/gtree/gtree_k_v_redblacktree.go | New generic red-black tree implementation with type-safe K/V parameters |
| container/gtree/gtree_k_v_btree.go | New generic B-tree implementation with type-safe K/V parameters |
| container/gtree/gtree_k_v_avltree.go | New generic AVL tree implementation with type-safe K/V parameters |
| container/gtree/gtree_redblacktree.go | Refactored as wrapper with lazy initialization around RedBlackKVTree[any, any] |
| container/gtree/gtree_btree.go | Refactored as wrapper around BKVTree[any, any] |
| container/gtree/gtree_avltree.go | Refactored as wrapper with lazy initialization around AVLKVTree[any, any] |
| container/gtree/gtree.go | Added generic version of iteratorFromGetIndex helper function |
| container/gmap/gmap_tree_map.go | Updated to alias TreeKVMap[any, any] instead of gtree.RedBlackTree |
| container/gmap/gmap_tree_k_v_map.go | New generic tree map implementation wrapping RedBlackKVTree |
Comments suppressed due to low confidence (2)
container/gtree/gtree_avltree.go:236
- Inconsistent lazy initialization: Methods Iterator and IteratorFrom on lines 230-236 are missing
lazyInit()calls, while most other methods in this file have them. This could lead to nil pointer dereference if these methods are called on an uninitialized tree.
// Iterator is alias of IteratorAsc.
//
// Also see IteratorAsc.
func (tree *AVLTree) Iterator(f func(key, value any) bool) {
tree.IteratorAsc(f)
}
container/gtree/gtree_avltree.go:320
- Missing lazy initialization before accessing
tree.mu.IsSafe()at line 320. TheFlipmethod callslazyInit()but then creates a new tree usingtree.mu.IsSafe()which requirestree.AVLKVTreeto be non-nil to access the embeddedmufield.
// Flip exchanges key-value of the tree to value-key.
// Note that you should guarantee the value is the same type as key,
// or else the comparator would panic.
//
// If the type of value is different with key, you pass the new `comparator`.
func (tree *AVLTree) Flip(comparator ...func(v1, v2 any) int) {
var t = new(AVLTree)
if len(comparator) > 0 {
t = NewAVLTree(comparator[0], tree.mu.IsSafe())
} else {
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
chore(ci): 更新ci-main-clean.sh脚本以注释掉MySQL清理逻辑
fix(ci): 注释掉polaris资源清理逻辑以避免潜在错误
feat(container/gmap): 更新 gmap_tree_map.go 文件,调整 import 格式并修正注释
hailaz
approved these changes
Nov 29, 2025
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.
add generic tree feature
improve gmap.TreeMap