-
Notifications
You must be signed in to change notification settings - Fork 373
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
base: master
Are you sure you want to change the base?
Conversation
Decided to implement TraverseByOffset wtih similar reverse traverse feature. |
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 |
Also relevant: #450 |
#450 is merged. If someone is working on this PR, please, rebase first. Cc @harry-hov |
This one doesn't uses // 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 |
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.