diff --git a/examples/gno.land/p/demo/avl/pager/pager.gno b/examples/gno.land/p/demo/avl/pager/pager.gno index 900320cb96c..8519f362f58 100644 --- a/examples/gno.land/p/demo/avl/pager/pager.gno +++ b/examples/gno.land/p/demo/avl/pager/pager.gno @@ -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) }) diff --git a/examples/gno.land/p/demo/avl/pager/z_filetest.gno b/examples/gno.land/p/demo/avl/pager/z_filetest.gno index 0d3aacf56c1..9189eedc211 100644 --- a/examples/gno.land/p/demo/avl/pager/z_filetest.gno +++ b/examples/gno.land/p/demo/avl/pager/z_filetest.gno @@ -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" ) @@ -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) @@ -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: @@ -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