-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add p/demo/avlhelpers. See the PR.
Signed-off-by: Jeff Thompson <[email protected]>
- Loading branch information
Showing
3 changed files
with
129 additions
and
0 deletions.
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,32 @@ | ||
package avlhelpers | ||
|
||
import ( | ||
"gno.land/p/demo/avl" | ||
) | ||
|
||
// Get a list of keys starting from the given prefix. Limit the | ||
// number of results to maxResults. | ||
func ListKeysByPrefix(tree avl.Tree, prefix string, maxResults int) []string { | ||
end := "" | ||
n := len(prefix) | ||
// To make the end of the search, increment the final character ASCII by one. | ||
for n > 0 { | ||
if ascii := int(prefix[n-1]); ascii < 0xff { | ||
end = prefix[0:n-1] + string(ascii+1) | ||
break | ||
} | ||
|
||
// The last character is 0xff. Try the previous character. | ||
n-- | ||
} | ||
|
||
result := []string{} | ||
tree.Iterate(prefix, end, func(key string, value interface{}) bool { | ||
result = append(result, key) | ||
if len(result) >= maxResults { | ||
return true | ||
} | ||
return false | ||
}) | ||
return result | ||
} |
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,3 @@ | ||
module gno.land/p/demo/avlhelpers | ||
|
||
require gno.land/p/demo/avl v0.0.0-latest |
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,94 @@ | ||
// PKGPATH: gno.land/r/test | ||
package test | ||
|
||
import ( | ||
"encoding/hex" | ||
|
||
"gno.land/p/demo/avl" | ||
"gno.land/p/demo/avlhelpers" | ||
"gno.land/p/demo/ufmt" | ||
) | ||
|
||
func init() { | ||
} | ||
|
||
func main() { | ||
tree := avl.Tree{} | ||
|
||
{ | ||
// Empty tree. | ||
matches := avlhelpers.ListKeysByPrefix(tree, "", 10) | ||
println(ufmt.Sprintf("# matches: %d", len(matches))) | ||
} | ||
|
||
tree.Set("alice", "") | ||
tree.Set("andy", "") | ||
tree.Set("bob", "") | ||
|
||
{ | ||
// Match only alice. | ||
matches := avlhelpers.ListKeysByPrefix(tree, "al", 10) | ||
println(ufmt.Sprintf("# matches: %d", len(matches))) | ||
println("match: " + matches[0]) | ||
} | ||
|
||
{ | ||
// Match alice and andy. | ||
matches := avlhelpers.ListKeysByPrefix(tree, "a", 10) | ||
println(ufmt.Sprintf("# matches: %d", len(matches))) | ||
println("match: " + matches[0]) | ||
println("match: " + matches[1]) | ||
} | ||
|
||
{ | ||
// Match alice and andy limited to 1. | ||
matches := avlhelpers.ListKeysByPrefix(tree, "a", 1) | ||
println(ufmt.Sprintf("# matches: %d", len(matches))) | ||
println("match: " + matches[0]) | ||
} | ||
|
||
tree = avl.Tree{} | ||
tree.Set("a\xff", "") | ||
tree.Set("a\xff\xff", "") | ||
tree.Set("b", "") | ||
tree.Set("\xff\xff\x00", "") | ||
|
||
{ | ||
// Match only "a\xff\xff". | ||
matches := avlhelpers.ListKeysByPrefix(tree, "a\xff\xff", 10) | ||
println(ufmt.Sprintf("# matches: %d", len(matches))) | ||
println(ufmt.Sprintf("match: %s", hex.EncodeToString([]byte(matches[0])))) | ||
} | ||
|
||
{ | ||
// Match "a\xff" and "a\xff\xff". | ||
matches := avlhelpers.ListKeysByPrefix(tree, "a\xff", 10) | ||
println(ufmt.Sprintf("# matches: %d", len(matches))) | ||
println(ufmt.Sprintf("match: %s", hex.EncodeToString([]byte(matches[0])))) | ||
println(ufmt.Sprintf("match: %s", hex.EncodeToString([]byte(matches[1])))) | ||
} | ||
|
||
{ | ||
// Edge case: Match only "\xff\xff\x00". | ||
matches := avlhelpers.ListKeysByPrefix(tree, "\xff\xff", 10) | ||
println(ufmt.Sprintf("# matches: %d", len(matches))) | ||
println(ufmt.Sprintf("match: %s", hex.EncodeToString([]byte(matches[0])))) | ||
} | ||
} | ||
|
||
// Output: | ||
// # matches: 0 | ||
// # matches: 1 | ||
// match: alice | ||
// # matches: 2 | ||
// match: alice | ||
// match: andy | ||
// # matches: 1 | ||
// match: alice | ||
// # matches: 1 | ||
// match: 61ffff | ||
// # matches: 2 | ||
// match: 61ff | ||
// match: 61ffff | ||
// # matches: 1 | ||
// match: ffff00 |