Skip to content

feat(container/gtree): add generic tree feature#4522

Merged
hailaz merged 23 commits intomasterfrom
feat/kv_tree
Nov 29, 2025
Merged

feat(container/gtree): add generic tree feature#4522
hailaz merged 23 commits intomasterfrom
feat/kv_tree

Conversation

@joy999
Copy link
Contributor

@joy999 joy999 commented Nov 24, 2025

add generic tree feature
improve gmap.TreeMap

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/gods from v1 to v2 (alpha) to support generics
  • Added new generic tree implementations: RedBlackKVTree[K, V], BKVTree[K, V], and AVLKVTree[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-generic TreeMap alias

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. The Flip method calls lazyInit() but then creates a new tree using tree.mu.IsSafe() which requires tree.AVLKVTree to be non-nil to access the embedded mu field.
// 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.

joy999 and others added 21 commits November 24, 2025 14:26
chore(ci): 更新ci-main-clean.sh脚本以注释掉MySQL清理逻辑
fix(ci): 注释掉polaris资源清理逻辑以避免潜在错误
feat(container/gmap): 更新 gmap_tree_map.go 文件,调整 import 格式并修正注释
@hailaz hailaz merged commit 777d7aa into master Nov 29, 2025
20 checks passed
@hailaz hailaz deleted the feat/kv_tree branch November 29, 2025 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants