Skip to content

Commit 032a518

Browse files
committed
feature: 限制树高度为 4294967295 从而优化内存布局
1 parent eae8f06 commit 032a518

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

rtree.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ type Rtree struct {
2929
MinChildren int
3030
MaxChildren int
3131
root *node
32-
size int // 每次调用 Insert 都会加 1,调用 DeleteWithComparator 时会减 1
33-
height int
32+
size int // 每次调用 Insert 都会加 1,调用 DeleteWithComparator 时会减 1
33+
height uint32 // 不应超过 4294967295 层。层数太深,失去了使用树的意义
3434

3535
// deleted is a temporary buffer to avoid memory allocations in Delete.
3636
// It is just an optimization and not part of the data structure.
@@ -92,7 +92,7 @@ func (tree *Rtree) String() string {
9292
// Depth returns the maximum depth of tree.
9393
//
9494
// 返回树的最大深度。
95-
func (tree *Rtree) Depth() int {
95+
func (tree *Rtree) Depth() uint32 {
9696
return tree.height
9797
}
9898

@@ -168,16 +168,16 @@ func (tree *Rtree) bulkLoad(objs []Spatial) {
168168
// sort all entries by first dimension
169169
sortByDim(0, entries)
170170

171-
tree.height = int(h)
171+
tree.height = uint32(h)
172172
tree.size = n
173-
tree.root = tree.omt(int(h), int(S), entries, int(s))
173+
tree.root = tree.omt(uint32(h), int(S), entries, int(s))
174174
}
175175

176176
// omt is the recursive part of the Overlap Minimizing Top-loading bulk-
177177
// load approach. Returns the root node of a subtree.
178178
//
179179
// 重叠最小化顶部加载批量加载方法的递归部分。返回一棵子树的根节点。
180-
func (tree *Rtree) omt(level, nSlices int, objs []entry, m int) *node {
180+
func (tree *Rtree) omt(level uint32, nSlices int, objs []entry, m int) *node {
181181
// if number of objects is less than or equal than max children per leaf,
182182
// we need to create a leaf node
183183
if len(objs) <= m {
@@ -221,7 +221,7 @@ func (tree *Rtree) omt(level, nSlices int, objs []entry, m int) *node {
221221
// create sub trees
222222
walkPartitions(vertSize, objs, func(vert []entry) {
223223
// sort vertical slice by a different dimension on every level
224-
sortByDim((tree.height-level+1)%tree.Dim, vert)
224+
sortByDim(int((tree.height-level+1))%tree.Dim, vert)
225225

226226
// split slice into groups of size k
227227
walkPartitions(k, vert, func(part []entry) {
@@ -241,7 +241,7 @@ func (tree *Rtree) omt(level, nSlices int, objs []entry, m int) *node {
241241
type node struct {
242242
parent *node
243243
entries []entry
244-
level int // node depth in the Rtree
244+
level uint32 // node depth in the Rtree 节点所处深度,不应超过 4294967295 层。层数太深,失去了使用树的意义
245245
leaf bool
246246
}
247247

@@ -310,7 +310,7 @@ func (tree *Rtree) Insert(obj Spatial) {
310310
// insert adds the specified entry to the tree at the specified level.
311311
//
312312
// “插入”(insert)操作会将指定的条目添加到树中指定的层级上。
313-
func (tree *Rtree) insert(e entry, level int) {
313+
func (tree *Rtree) insert(e entry, level uint32) {
314314
leaf := tree.chooseNode(tree.root, e, level)
315315
leaf.entries = append(leaf.entries, e)
316316

@@ -344,7 +344,7 @@ func (tree *Rtree) insert(e entry, level int) {
344344
// chooseNode finds the node at the specified level to which e should be added.
345345
//
346346
// “选择节点”(chooseNode)操作会在指定层级上查找元素 e 应添加到哪个节点。
347-
func (tree *Rtree) chooseNode(n *node, e entry, level int) *node {
347+
func (tree *Rtree) chooseNode(n *node, e entry, level uint32) *node {
348348
if n.leaf || n.level == level { // 如果n是叶子节点或者n的层级等于指定的层级,则应添加到该节点 n 上
349349
return n
350350
}
@@ -907,7 +907,7 @@ func (tree *Rtree) nearestNeighbor(p Point, n *node, d float64, nearest Spatial,
907907
func (tree *Rtree) NearestNeighbors(k int, p Point, needTrace bool, filters ...Filter) []Spatial {
908908
// preallocate the buffers for sortings the branches. At each level of the
909909
// tree, we slide the buffer by the number of entries in the node.
910-
maxBufSize := tree.MaxChildren * tree.Depth()
910+
maxBufSize := tree.MaxChildren * int(tree.Depth())
911911
branches := make([]entry, maxBufSize)
912912
branchDists := make([]float64, maxBufSize)
913913

rtree_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func items(n *node) chan Spatial {
118118
return ch
119119
}
120120

121-
func validate(n *node, height, max int) error {
121+
func validate(n *node, height uint32, max int) error {
122122
if n.level != height {
123123
return fmt.Errorf("level %d != height %d", n.level, height)
124124
}

0 commit comments

Comments
 (0)