-
Notifications
You must be signed in to change notification settings - Fork 399
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
feat(examples): add p/demo/avl/list #3324
Merged
Merged
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
1d25587
feat(examples): add p/demo/avl/list
moul 1745dd0
Merge branch 'master' into dev/moul/avllist
moul 5e2c655
chore: fixup
moul 6811a30
chore: fixup
moul b482ec1
chore: fixup
moul 7e2a012
chore: fixup
moul 8df76ab
Merge branch 'master' into dev/moul/avllist
moul 4d5a937
Merge branch 'master' into dev/moul/avllist
moul 14e1e38
Merge branch 'master' into dev/moul/avllist
moul 7ebca9d
Merge branch 'master' into dev/moul/avllist
thehowl c4afc1c
fix(p/demo/avl): correct order for IterateByOffset
thehowl f4bef70
Merge branch 'master' into dev/morgan/fix-avl-iterate
thehowl ced482b
reswap in pager
thehowl bd8817c
Merge remote-tracking branch 'thehowl/dev/morgan/fix-avl-iterate' int…
moul 14a4976
chore: fixup
moul b28a4e3
chore: fix an early-termination bug
moul 2d59e8d
Merge remote-tracking branch 'thehowl/dev/morgan/fix-avl-iterate' int…
moul cb21544
chore: fixup
moul 48ef6ad
Merge branch 'master' into dev/moul/avllist
moul 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module gno.land/p/demo/avl/list |
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,337 @@ | ||
// Package list implements a dynamic list data structure backed by an AVL tree. | ||
// It provides O(log n) operations for most list operations while maintaining | ||
// order stability. | ||
// | ||
// The list supports various operations including append, get, set, delete, | ||
// range queries, and iteration. It can store values of any type. | ||
// | ||
// Example usage: | ||
// | ||
// // Create a new list and add elements | ||
// var l list.List | ||
// l.Append(1, 2, 3) | ||
// | ||
// // Get and set elements | ||
// value := l.Get(1) // returns 2 | ||
// l.Set(1, 42) // updates index 1 to 42 | ||
// | ||
// // Delete elements | ||
// l.Delete(0) // removes first element | ||
// | ||
// // Iterate over elements | ||
// l.ForEach(func(index int, value interface{}) bool { | ||
// ufmt.Printf("index %d: %v\n", index, value) | ||
// return false // continue iteration | ||
// }) | ||
// // Output: | ||
// // index 0: 42 | ||
// // index 1: 3 | ||
// | ||
// // Create a list of specific size | ||
// l = list.Make(3, "default") // creates [default, default, default] | ||
// | ||
// // Create a list using a variable declaration | ||
// var l2 list.List | ||
// l2.Append(4, 5, 6) | ||
// println(l2.Len()) // Output: 3 | ||
package list | ||
|
||
import ( | ||
"gno.land/p/demo/avl" | ||
"gno.land/p/demo/seqid" | ||
) | ||
|
||
// List represents an ordered sequence of items backed by an AVL tree | ||
type List struct { | ||
tree avl.Tree | ||
idGen seqid.ID | ||
} | ||
|
||
// Len returns the number of elements in the list. | ||
// | ||
// Example: | ||
// | ||
// l := list.New() | ||
// l.Append(1, 2, 3) | ||
// println(l.Len()) // Output: 3 | ||
func (l *List) Len() int { | ||
return l.tree.Size() | ||
} | ||
|
||
// Append adds one or more values to the end of the list. | ||
// | ||
// Example: | ||
// | ||
// l := list.New() | ||
// l.Append(1) // adds single value | ||
// l.Append(2, 3, 4) // adds multiple values | ||
// println(l.Len()) // Output: 4 | ||
func (l *List) Append(values ...interface{}) { | ||
for _, v := range values { | ||
l.tree.Set(l.idGen.Next().String(), v) | ||
} | ||
} | ||
|
||
// Get returns the value at the specified index. | ||
// Returns nil if index is out of bounds. | ||
// | ||
// Example: | ||
// | ||
// l := list.New() | ||
// l.Append(1, 2, 3) | ||
// println(l.Get(1)) // Output: 2 | ||
// println(l.Get(-1)) // Output: nil | ||
// println(l.Get(999)) // Output: nil | ||
func (l *List) Get(index int) interface{} { | ||
if index < 0 || index >= l.tree.Size() { | ||
return nil | ||
} | ||
_, value := l.tree.GetByIndex(index) | ||
return value | ||
} | ||
|
||
// Set updates or appends a value at the specified index. | ||
// Returns true if the operation was successful, false otherwise. | ||
// For empty lists, only index 0 is valid (append case). | ||
// | ||
// Example: | ||
// | ||
// l := list.New() | ||
// l.Append(1, 2, 3) | ||
// | ||
// l.Set(1, 42) // updates existing index | ||
// println(l.Get(1)) // Output: 42 | ||
// | ||
// l.Set(3, 4) // appends at end | ||
// println(l.Get(3)) // Output: 4 | ||
// | ||
// l.Set(-1, 5) // invalid index | ||
// println(l.Len()) // Output: 4 (list unchanged) | ||
func (l *List) Set(index int, value interface{}) bool { | ||
size := l.tree.Size() | ||
|
||
// Handle empty list case - only allow index 0 | ||
if size == 0 { | ||
if index == 0 { | ||
l.Append(value) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
if index < 0 || index > size { | ||
return false | ||
} | ||
|
||
// If setting at the end (append case) | ||
if index == size { | ||
l.Append(value) | ||
return true | ||
} | ||
|
||
// Get the key at the specified index | ||
key, _ := l.tree.GetByIndex(index) | ||
if key == "" { | ||
return false | ||
} | ||
|
||
// Update the value at the existing key | ||
l.tree.Set(key, value) | ||
return true | ||
} | ||
|
||
// Delete removes the element at the specified index. | ||
// Returns the deleted value and true if successful, nil and false otherwise. | ||
// | ||
// Example: | ||
// | ||
// l := list.New() | ||
// l.Append(1, 2, 3) | ||
// | ||
// val, ok := l.Delete(1) | ||
// println(val, ok) // Output: 2 true | ||
// println(l.Len()) // Output: 2 | ||
// | ||
// val, ok = l.Delete(-1) | ||
// println(val, ok) // Output: nil false | ||
func (l *List) Delete(index int) (interface{}, bool) { | ||
size := l.tree.Size() | ||
// Always return nil, false for empty list | ||
if size == 0 { | ||
return nil, false | ||
} | ||
|
||
if index < 0 || index >= size { | ||
return nil, false | ||
} | ||
|
||
key, value := l.tree.GetByIndex(index) | ||
if key == "" { | ||
return nil, false | ||
} | ||
|
||
l.tree.Remove(key) | ||
return value, true | ||
} | ||
|
||
// Slice returns a slice of values from startIndex (inclusive) to endIndex (exclusive). | ||
// Returns nil if the range is invalid. | ||
// | ||
// Example: | ||
// | ||
// l := list.New() | ||
// l.Append(1, 2, 3, 4, 5) | ||
// | ||
// println(l.Slice(1, 4)) // Output: [2 3 4] | ||
// println(l.Slice(-1, 2)) // Output: [1 2] | ||
// println(l.Slice(3, 999)) // Output: [4 5] | ||
// println(l.Slice(3, 2)) // Output: nil | ||
func (l *List) Slice(startIndex, endIndex int) []interface{} { | ||
size := l.tree.Size() | ||
|
||
// Normalize bounds | ||
if startIndex < 0 { | ||
startIndex = 0 | ||
} | ||
if endIndex > size { | ||
endIndex = size | ||
} | ||
if startIndex >= endIndex { | ||
return nil | ||
} | ||
|
||
count := endIndex - startIndex | ||
result := make([]interface{}, count) | ||
|
||
// Fill the slice from end to start to maintain correct order | ||
i := count - 1 | ||
l.tree.IterateByOffset(size-endIndex, count, func(_ string, value interface{}) bool { | ||
result[i] = value | ||
i-- | ||
return false | ||
}) | ||
return result | ||
} | ||
|
||
// ForEach iterates through all elements in the list. | ||
// The callback function receives the index and value of each element. | ||
// Return true from the callback to stop iteration. | ||
// | ||
// Example: | ||
// | ||
// l := list.New() | ||
// l.Append(1, 2, 3) | ||
// | ||
// sum := 0 | ||
// l.ForEach(func(index int, value interface{}) bool { | ||
// sum += value.(int) | ||
// return false // continue iteration | ||
// }) | ||
// println(sum) // Output: 6 | ||
// | ||
// // Early stop | ||
// l.ForEach(func(index int, value interface{}) bool { | ||
// if value.(int) > 2 { | ||
// return true // stop iteration | ||
// } | ||
// return false | ||
// }) | ||
func (l *List) ForEach(fn func(index int, value interface{}) bool) { | ||
size := l.tree.Size() | ||
values := make([]interface{}, size) | ||
|
||
// First collect values in reverse order | ||
i := size - 1 | ||
l.tree.IterateByOffset(0, size, func(_ string, value interface{}) bool { | ||
values[i] = value | ||
i-- | ||
return false | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If reverse here is necessary, you can use ReverseIterateByOffset and call fn() directly |
||
|
||
// Then iterate through them in correct order | ||
for i := 0; i < size; i++ { | ||
if fn(i, values[i]) { | ||
return | ||
} | ||
} | ||
} | ||
|
||
// Clone creates a shallow copy of the list. | ||
// | ||
// Example: | ||
// | ||
// l := list.New() | ||
// l.Append(1, 2, 3) | ||
// | ||
// clone := l.Clone() | ||
// clone.Set(0, 42) | ||
// | ||
// println(l.Get(0)) // Output: 1 | ||
// println(clone.Get(0)) // Output: 42 | ||
func (l *List) Clone() *List { | ||
newList := &List{ | ||
tree: avl.Tree{}, // Create a new empty tree | ||
idGen: l.idGen, // Copy the current ID generator state | ||
} | ||
|
||
size := l.tree.Size() | ||
if size == 0 { | ||
return newList | ||
} | ||
|
||
// Collect values in correct order | ||
values := make([]interface{}, size) | ||
i := size - 1 | ||
l.tree.IterateByOffset(0, size, func(_ string, value interface{}) bool { | ||
values[i] = value | ||
i-- | ||
return false | ||
}) | ||
|
||
// Add values to new list in correct order | ||
for _, value := range values { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similarly here |
||
newList.Append(value) | ||
} | ||
|
||
return newList | ||
} | ||
|
||
// DeleteRange removes elements from startIndex (inclusive) to endIndex (exclusive). | ||
// Returns the number of elements deleted. | ||
// | ||
// Example: | ||
// | ||
// l := list.New() | ||
// l.Append(1, 2, 3, 4, 5) | ||
// | ||
// deleted := l.DeleteRange(1, 4) | ||
// println(deleted) // Output: 3 | ||
// println(l.Range(0, l.Len())) // Output: [1 5] | ||
func (l *List) DeleteRange(startIndex, endIndex int) int { | ||
size := l.tree.Size() | ||
|
||
// Normalize bounds | ||
if startIndex < 0 { | ||
startIndex = 0 | ||
} | ||
if endIndex > size { | ||
endIndex = size | ||
} | ||
if startIndex >= endIndex { | ||
return 0 | ||
} | ||
|
||
// Collect keys to delete | ||
keysToDelete := make([]string, 0, endIndex-startIndex) | ||
l.tree.IterateByOffset(size-endIndex, endIndex-startIndex, func(key string, _ interface{}) bool { | ||
keysToDelete = append(keysToDelete, key) | ||
return false | ||
}) | ||
|
||
// Delete collected keys | ||
for _, key := range keysToDelete { | ||
l.tree.Remove(key) | ||
} | ||
|
||
return len(keysToDelete) | ||
} |
Oops, something went wrong.
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.
Why is this reversed?