From 09088c303a14e2b23e8ac0410ec19e285248de97 Mon Sep 17 00:00:00 2001 From: Kai O'Reilly Date: Mon, 25 Dec 2023 21:52:00 -0800 Subject: [PATCH] don't return a bool from IndexInParent --- ki.go | 6 +++--- node.go | 15 ++++++++------- walki.go | 12 +++++------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/ki.go b/ki.go index 6aaf0fa..121d93a 100644 --- a/ki.go +++ b/ki.go @@ -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 diff --git a/node.go b/node.go index 96a5d9d..d9c6f9d 100644 --- a/node.go +++ b/node.go @@ -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 diff --git a/walki.go b/walki.go index 8acc44c..cb4714f 100644 --- a/walki.go +++ b/walki.go @@ -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) } @@ -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()) }