Skip to content
16 changes: 13 additions & 3 deletions examples/gno.land/p/nt/avl/node.gno
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,15 @@ func (node *Node) Get(key string) (index int, value any, exists bool) {
// GetByIndex retrieves the key-value pair of the node at the given index
// in the subtree rooted at the node.
func (node *Node) GetByIndex(index int) (key string, value any) {
if index < 0 {
panic("GetByIndex: negative index not allowed")
}

if node.height == 0 {
if index == 0 {
return node.key, node.value
} else {
if index != 0 {
panic("GetByIndex asked for invalid index")
Comment on lines +116 to 117
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here
nitpick: I think it is better you can have this change reverted to avoid reviewing uneccessary changes

Copy link
Contributor Author

@Davphla Davphla Nov 27, 2025

Choose a reason for hiding this comment

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

This condition is not return first, therefore it is hard to read and should be change (As I've done in #4889). If not done in this PR, it should be done in another.

}
return node.key, node.value
} else {
// TODO: could improve this by storing the sizes
leftNode := node.getLeftNode()
Expand Down Expand Up @@ -267,6 +270,13 @@ func (node *Node) rotateLeft() *Node {
func (node *Node) calcHeightAndSize() {
node.height = maxInt8(node.getLeftNode().height, node.getRightNode().height) + 1
node.size = node.getLeftNode().size + node.getRightNode().size

if node.height < 0 {
panic("height overflow: tree height exceeds int8 max")
}
if node.size < 0 {
panic("size overflow: node size exceeds int max")
}
}

// calcBalance calculates the balance factor of the node.
Expand Down
4 changes: 4 additions & 0 deletions examples/gno.land/p/nt/avl/pager/pager.gno
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (p *Pager) GetPage(pageNumber int) *Page {
}

func (p *Pager) GetPageWithSize(pageNumber, pageSize int) *Page {
if pageSize <= 0 {
panic("GetPageWithSize: invalid page size")
}

totalItems := p.Tree.Size()
totalPages := int(math.Ceil(float64(totalItems) / float64(pageSize)))

Expand Down
Loading