Skip to content
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

WIP: r/demo/boards pagination #447

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

WIP: r/demo/boards pagination #447

wants to merge 2 commits into from

Conversation

jaekwon
Copy link
Contributor

@jaekwon jaekwon commented Jan 2, 2023

Investigating approach to use for pagination.
The current draft form works for next-pagination but doesn't really help skip forward or go backward.
So I'll be replacing "last" with offset, and make sure avl iteration can support offsets.

@jaekwon
Copy link
Contributor Author

jaekwon commented Jan 2, 2023

Decided to implement TraverseByOffset wtih similar reverse traverse feature.
Then this would be better suited for use for pagination in /r/boards.

@harry-hov
Copy link
Contributor

I just took a look at TraverseByOffset. The naive way I can think of is:

// TraverseByOffset traverses all nodes, including inner nodes.
// A limit of math.MaxInt means no limit.
// XXX Actually implement this method signature by modifying the implementation below.
// XXX Then, make tests.
func (tree *Tree) TraverseByOffset(offset, limit int, ascending bool, leavesOnly bool, cb func(*Tree) bool) bool {
	count := 0
	start := offset
	end := offset + limit
	stop := false

	return tree.TraverseInRange("", "", ascending, leavesOnly, func(t *Tree) bool {
		count += 1
		if count <= start {
			return false
		}
		if count <= end {
			stop = cb(t)
			if stop {
				return stop
			}
		} else if count == end+1 {
			return true
		}
		return stop
	})
}

I'm still trying to figure out an efficient way to skip offset

@jaekwon
Copy link
Contributor Author

jaekwon commented Jan 6, 2023

Also relevant: #450

@moul
Copy link
Member

moul commented Jan 11, 2023

#450 is merged.

If someone is working on this PR, please, rebase first.

Cc @harry-hov

@harry-hov
Copy link
Contributor

This one doesn't uses TraverseInRange()

// TraverseByOffset traverses all nodes, including inner nodes.
// A limit of math.MaxInt means no limit.
// XXX Actually implement this method signature by modifying the implementation below.
// XXX Then, make tests.
func (node *Node) TraverseByOffset(offset, limit int, ascending bool, leavesOnly bool, cb func(*Node) bool) bool {
	return traverseByOffset(node, &offset, &limit, ascending, leavesOnly, cb)
}

func traverseByOffset(node *Node, offset, limit *int, ascending, leavesOnly bool, cb func(*Node) bool) bool {
	if node == nil {
		return false
	}

	var firstPass, secondPass *Node
	if ascending {
		firstPass = node.getLeftNode()
		secondPass = node.getRightNode()
	} else {
		firstPass = node.getRightNode()
		secondPass = node.getLeftNode()
	}

	if stop := traverseByOffset(firstPass, offset, limit, ascending, leavesOnly, cb); stop {
		return stop
	}
	if (!node.IsLeaf() && !leavesOnly) || node.IsLeaf() {
		if *offset > 0 {
			*offset -= 1
			return false
		}
		if *limit == 0 {
			return true
		}

		*limit -= 1
		if stop := cb(node); stop {
			return stop
		}
	}
	if stop := traverseByOffset(secondPass, offset, limit, ascending, leavesOnly, cb); stop {
		return stop
	}

	return false
}

I'm not sure if we can do this without a helper function.


PS: I also thought about writing an iterative approach. But needs a stack implementation for that. Maybe we can utilize this if needed #456

@moul moul marked this pull request as draft January 19, 2023 18:22
@moul moul added 🗺️good first issue🗺️ Ideal for newcomer contributors help wanted labels Jan 19, 2023
@moul moul added this to the 🌟 main.gno.land (wanted) milestone Sep 8, 2023
@moul moul mentioned this pull request Jul 14, 2024
@Kouteki Kouteki added help wanted Want to contribute? We recommend these issues. and removed help wanted labels Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🗺️good first issue🗺️ Ideal for newcomer contributors help wanted Want to contribute? We recommend these issues.
Projects
Status: Todo
Status: 🌟 Wanted for Launch
Status: No status
Status: Backlog
Development

Successfully merging this pull request may close these issues.

4 participants