Skip to content

Commit

Permalink
don't return a bool from IndexInParent
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoreilly committed Dec 26, 2023
1 parent 809322d commit 09088c3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
6 changes: 3 additions & 3 deletions ki.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ type Ki interface {
// one-parent, no-cycles structure -- see SetParent.
Parent() Ki

// IndexInParent returns our index within our parent object -- caches the
// IndexInParent returns our index within our parent object. It caches the
// last value and uses that for an optimized search so subsequent calls
// are typically quite fast. Returns false if we don't have a parent.
IndexInParent() (int, bool)
// are typically quite fast. Returns -1 if we don't have a parent.
IndexInParent() int

// ParentLevel finds a given potential parent node recursively up the
// hierarchy, returning level above current node that the parent was
Expand Down
15 changes: 8 additions & 7 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,19 @@ func (n *Node) Parent() Ki {
return n.Par
}

// IndexInParent returns our index within our parent object -- caches the
// IndexInParent returns our index within our parent object. It caches the
// last value and uses that for an optimized search so subsequent calls
// are typically quite fast. Returns false if we don't have a parent.
func (n *Node) IndexInParent() (int, bool) {
// are typically quite fast. Returns -1 if we don't have a parent.
func (n *Node) IndexInParent() int {
if n.Par == nil {
return -1, false
return -1
}
idx, ok := n.Par.Children().IndexOf(n.This(), n.index) // very fast if index is close..
if ok {
n.index = idx
if !ok {
return -1
}
return idx, ok
n.index = idx
return idx
}

// ParentLevel finds a given potential parent node recursively up the
Expand Down
12 changes: 5 additions & 7 deletions walki.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func Prev(nd Ki) Ki {
if nd.Parent() == nil {
return nil
}
myidx, ok := nd.IndexInParent()
if ok && myidx > 0 {
myidx := nd.IndexInParent()
if myidx > 0 {
nn := nd.Parent().Child(myidx - 1)
return LastChild(nn)
}
Expand All @@ -61,11 +61,9 @@ func NextSibling(nd Ki) Ki {
if nd.Parent() == nil {
return nil
}
myidx, ok := nd.IndexInParent()
if ok {
if myidx < nd.Parent().NumChildren()-1 {
return nd.Parent().Child(myidx + 1)
}
myidx := nd.IndexInParent()
if myidx >= 0 && myidx < nd.Parent().NumChildren()-1 {
return nd.Parent().Child(myidx + 1)
}
return NextSibling(nd.Parent())
}

0 comments on commit 09088c3

Please sign in to comment.