Skip to content

Commit

Permalink
chore: fixup
Browse files Browse the repository at this point in the history
Signed-off-by: moul <[email protected]>
  • Loading branch information
moul committed Jan 15, 2025
1 parent 2e11d2e commit a84a227
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 6 deletions.
9 changes: 7 additions & 2 deletions examples/gno.land/p/demo/avl/pager/pager.gno
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ func (w *Wrapper) IterateByOffset(offset int, count int, cb func(index interface
return false
}

// Use the tree's IterateByOffset with the full count
// (positive for forward, negative for reverse)
// Use ReverseIterateByOffset for negative count
if count < 0 {
return w.tree.ReverseIterateByOffset(offset, -count, func(key string, value interface{}) bool {
return cb(key, value)
})
}

return w.tree.IterateByOffset(offset, count, func(key string, value interface{}) bool {
return cb(key, value)
})
Expand Down
93 changes: 89 additions & 4 deletions examples/gno.land/p/demo/avl/pager/z_filetest.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"gno.land/p/demo/avl"
"gno.land/p/demo/avl/pager"
avlpager "gno.land/p/demo/avl/pager"
"gno.land/p/demo/seqid"
"gno.land/p/demo/ufmt"
)
Expand All @@ -11,12 +11,12 @@ func main() {
// Create a new AVL tree and populate it with some key-value pairs.
var id seqid.ID
tree := avl.NewTree()
for i := 0; i < 42; i++ {
for i := 0; i < 41; i++ {
tree.Set(id.Next().String(), i)
}

// Create a new pager.
pager := pager.NewPager(tree, 7, false)
pager := avlpager.NewPager(tree, 7, false)

for pn := -1; pn < 8; pn++ {
page := pager.GetPage(pn)
Expand All @@ -28,6 +28,23 @@ func main() {
println(page.Picker())
println()
}

println("-------------")

// Create a new pager in reverse mode.
pager = avlpager.NewPager(tree, 7, true)

for pn := -1; pn < 8; pn++ {
page := pager.GetPage(pn)

println(ufmt.Sprintf("## Page %d of %d", page.PageNumber, page.TotalPages))
for idx, item := range page.Items {
println(ufmt.Sprintf("- idx=%d index=%s value=%d", idx, item.Index, item.Value))
}
println(page.Picker())
println()
}

}

// Output:
Expand Down Expand Up @@ -94,7 +111,75 @@ func main() {
// - idx=3 index=0000017 value=38
// - idx=4 index=0000018 value=39
// - idx=5 index=0000019 value=40
// - idx=6 index=000001a value=41
// [1](?page=1) | … | [4](?page=4) | [5](?page=5) | **6**
//
// ## Page 7 of 6
// [1](?page=1) | … | [5](?page=5) | [6](?page=6) | _7_
//
// -------------
// ## Page 0 of 6
// _0_ | [1](?page=1) | [2](?page=2) | … | [6](?page=6)
//
// ## Page 0 of 6
// _0_ | [1](?page=1) | [2](?page=2) | … | [6](?page=6)
//
// ## Page 1 of 6
// - idx=0 index=0000019 value=40
// - idx=1 index=0000018 value=39
// - idx=2 index=0000017 value=38
// - idx=3 index=0000016 value=37
// - idx=4 index=0000015 value=36
// - idx=5 index=0000014 value=35
// - idx=6 index=0000013 value=34
// **1** | [2](?page=2) | [3](?page=3) | … | [6](?page=6)
//
// ## Page 2 of 6
// - idx=0 index=0000012 value=33
// - idx=1 index=0000011 value=32
// - idx=2 index=0000010 value=31
// - idx=3 index=000000z value=30
// - idx=4 index=000000y value=29
// - idx=5 index=000000x value=28
// - idx=6 index=000000w value=27
// [1](?page=1) | **2** | [3](?page=3) | [4](?page=4) | … | [6](?page=6)
//
// ## Page 3 of 6
// - idx=0 index=000000v value=26
// - idx=1 index=000000t value=25
// - idx=2 index=000000s value=24
// - idx=3 index=000000r value=23
// - idx=4 index=000000q value=22
// - idx=5 index=000000p value=21
// - idx=6 index=000000n value=20
// [1](?page=1) | [2](?page=2) | **3** | [4](?page=4) | [5](?page=5) | [6](?page=6)
//
// ## Page 4 of 6
// - idx=0 index=000000m value=19
// - idx=1 index=000000k value=18
// - idx=2 index=000000j value=17
// - idx=3 index=000000h value=16
// - idx=4 index=000000g value=15
// - idx=5 index=000000f value=14
// - idx=6 index=000000e value=13
// [1](?page=1) | [2](?page=2) | [3](?page=3) | **4** | [5](?page=5) | [6](?page=6)
//
// ## Page 5 of 6
// - idx=0 index=000000d value=12
// - idx=1 index=000000c value=11
// - idx=2 index=000000b value=10
// - idx=3 index=000000a value=9
// - idx=4 index=0000009 value=8
// - idx=5 index=0000008 value=7
// - idx=6 index=0000007 value=6
// [1](?page=1) | … | [3](?page=3) | [4](?page=4) | **5** | [6](?page=6)
//
// ## Page 6 of 6
// - idx=0 index=0000006 value=5
// - idx=1 index=0000005 value=4
// - idx=2 index=0000004 value=3
// - idx=3 index=0000003 value=2
// - idx=4 index=0000002 value=1
// - idx=5 index=0000001 value=0
// [1](?page=1) | … | [4](?page=4) | [5](?page=5) | **6**
//
// ## Page 7 of 6
Expand Down

0 comments on commit a84a227

Please sign in to comment.