Skip to content

Commit

Permalink
Rework comments to reduce confusion about "leaf" entries
Browse files Browse the repository at this point in the history
  • Loading branch information
peterstace committed Jun 14, 2023
1 parent cca5380 commit a98c3fc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
19 changes: 10 additions & 9 deletions rtree/rtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ import (
)

const (
minChildren = 2
maxChildren = 4
minEntries = 2
maxEntries = 4
)

// node is a node in an R-Tree. nodes can either be leaf nodes holding entries
// for terminal items, or intermediate nodes holding entries for more nodes.
// node is a node in an R-Tree, holding user record IDs and/or links to deeper
// nodes in the tree.
type node struct {
entries [maxChildren]entry
entries [maxEntries]entry
numEntries int
}

// entry is an entry under a node, leading either to terminal items, or more nodes.
// entry is an entry contained inside a node. An entry can either hold a user
// record ID, or point to a deeper node in the tree (but not both). Because 0
// is a valid record ID, the child pointer should be used to distinguish
// between the two types of entries.
type entry struct {
box Box

// For leaf nodes, recordID is populated. For non-leaf nodes, child is populated.
box Box
child *node
recordID int
}
Expand Down
9 changes: 3 additions & 6 deletions rtree/rtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,6 @@ func checkInvariants(t *testing.T, rt *RTree, boxes []Box) {
if e.child == nil {
minLeafLevel = min(minLeafLevel, level)
maxLeafLevel = max(maxLeafLevel, level)
if e.child != nil {
t.Fatalf("leaf node has child (entry %d)", i)
}
if _, ok := unfound[e.recordID]; !ok {
t.Fatal("record ID found in tree but wasn't in unfound map")
}
Expand All @@ -162,12 +159,12 @@ func checkInvariants(t *testing.T, rt *RTree, boxes []Box) {
}
for i := current.numEntries; i < len(current.entries); i++ {
e := current.entries[i]
if e.box != (Box{}) || e.child != nil || e.recordID != 0 {
if e != (entry{}) {
t.Fatal("entry past numEntries is not the zero value")
}
}
if current.numEntries > maxChildren ||
(current != rt.root && current.numEntries < minChildren) {
if current.numEntries > maxEntries ||
(current != rt.root && current.numEntries < minEntries) {
t.Fatalf("%p: unexpected number of entries", current)
}
}
Expand Down

0 comments on commit a98c3fc

Please sign in to comment.