|
@@ -42,7 +42,7 @@ int RTreeSearchF(struct RTree *t, struct RTree_Rect *r,
|
|
|
int hitCount = 0, notfound, currlevel;
|
|
|
int i;
|
|
|
int top = 0;
|
|
|
- struct fstack *s = t->fs;
|
|
|
+ struct nstack *s = t->ns;
|
|
|
|
|
|
/* stack size of t->rootlevel + 1 is enough because of depth first search */
|
|
|
/* only one node per level on stack at any given time */
|
|
@@ -51,7 +51,6 @@ int RTreeSearchF(struct RTree *t, struct RTree_Rect *r,
|
|
|
currlevel = t->rootlevel;
|
|
|
s[top].pos = t->rootpos;
|
|
|
s[top].sn = RTreeGetNode(s[top].pos, currlevel, t);
|
|
|
- /* RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t); */
|
|
|
s[top].branch_id = i = 0;
|
|
|
|
|
|
while (top >= 0) {
|
|
@@ -66,7 +65,6 @@ int RTreeSearchF(struct RTree *t, struct RTree_Rect *r,
|
|
|
/* add next node to stack */
|
|
|
s[top].pos = n->branch[i].child.pos;
|
|
|
s[top].sn = RTreeGetNode(s[top].pos, currlevel, t);
|
|
|
- /* RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t); */
|
|
|
s[top].branch_id = 0;
|
|
|
notfound = 0;
|
|
|
break;
|
|
@@ -119,28 +117,16 @@ static int RTreeInsertRect2F(struct RTree_Rect *r, union RTree_Child child, int
|
|
|
int top = 0, down = 0;
|
|
|
int result;
|
|
|
struct RTree_Branch *b = &(t->tmpb2);
|
|
|
- struct fstack *s = t->fs;
|
|
|
+ struct nstack *s = t->ns;
|
|
|
|
|
|
- static struct RTree_Node nn;
|
|
|
- static struct RTree_Rect nr;
|
|
|
- static int rect_init = 0;
|
|
|
-
|
|
|
- if (!rect_init) {
|
|
|
- for (i = 0; i < MAXCARD; i++) {
|
|
|
- nn.branch[i].rect.boundary = RTreeAllocBoundary(t);
|
|
|
- }
|
|
|
- nr.boundary = RTreeAllocBoundary(t);
|
|
|
- rect_init = t->ndims_alloc;
|
|
|
- }
|
|
|
+ struct RTree_Rect *nr = &(t->orect);
|
|
|
|
|
|
- n2 = &nn;
|
|
|
- /* n2 = newnode; */
|
|
|
+ n2 = newnode;
|
|
|
|
|
|
/* add root node position to stack */
|
|
|
currlevel = t->rootlevel;
|
|
|
s[top].pos = t->rootpos;
|
|
|
s[top].sn = RTreeGetNode(s[top].pos, currlevel, t);
|
|
|
- /* RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t); */
|
|
|
|
|
|
/* go down to level of insertion */
|
|
|
while (s[top].sn->level > level) {
|
|
@@ -151,10 +137,7 @@ static int RTreeInsertRect2F(struct RTree_Rect *r, union RTree_Child child, int
|
|
|
/* add next node to stack */
|
|
|
s[top].pos = n->branch[i].child.pos;
|
|
|
s[top].sn = RTreeGetNode(s[top].pos, currlevel, t);
|
|
|
- /* RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t); */
|
|
|
- assert(s[top].sn->level == currlevel);
|
|
|
}
|
|
|
- assert(s[top].sn->level == level);
|
|
|
|
|
|
/* Have reached level for insertion. Add rect, split if necessary */
|
|
|
RTreeCopyRect(&(b->rect), r, t);
|
|
@@ -166,7 +149,6 @@ static int RTreeInsertRect2F(struct RTree_Rect *r, union RTree_Child child, int
|
|
|
cover = &(s[top - 1].sn->branch[s[top - 1].branch_id].rect);
|
|
|
result = RTreeAddBranch(b, s[top].sn, &n2, ee, cover, overflow, t);
|
|
|
/* update node */
|
|
|
- /* RTreePutNode(&(s[top].sn), s[top].pos, t); */
|
|
|
RTreeNodeChanged(s[top].sn, s[top].pos, t);
|
|
|
/* write out new node if node was split */
|
|
|
if (result == 1) {
|
|
@@ -180,24 +162,16 @@ static int RTreeInsertRect2F(struct RTree_Rect *r, union RTree_Child child, int
|
|
|
down = top--;
|
|
|
i = s[top].branch_id;
|
|
|
if (result == 0) { /* branch was added */
|
|
|
- RTreeCombineRect(&(s[top].sn->branch[i].rect), r, &nr, t);
|
|
|
- /* rewrite rect */
|
|
|
- /*
|
|
|
- if (!RTreeCompareRect(&nr, &(s[top].sn.branch[i].rect), t)) {
|
|
|
- RTreeUpdateRect(&nr, &(s[top].sn), s[top].pos, i, t);
|
|
|
- }
|
|
|
- */
|
|
|
if (RTreeExpandRect(&(s[top].sn->branch[i].rect), r, t)) {
|
|
|
RTreeNodeChanged(s[top].sn, s[top].pos, t);
|
|
|
}
|
|
|
}
|
|
|
else if (result == 2) { /* branches were removed */
|
|
|
/* get node cover of previous node */
|
|
|
- RTreeNodeCover(s[down].sn, &nr, t);
|
|
|
+ RTreeNodeCover(s[down].sn, nr, t);
|
|
|
/* rewrite rect */
|
|
|
- if (!RTreeCompareRect(&nr, &(s[top].sn->branch[i].rect), t)) {
|
|
|
- /* RTreeUpdateRect(&nr, &(s[top].sn), s[top].pos, i, t); */
|
|
|
- RTreeCopyRect(&(s[top].sn->branch[i].rect), &nr, t);
|
|
|
+ if (!RTreeCompareRect(nr, &(s[top].sn->branch[i].rect), t)) {
|
|
|
+ RTreeCopyRect(&(s[top].sn->branch[i].rect), nr, t);
|
|
|
RTreeNodeChanged(s[top].sn, s[top].pos, t);
|
|
|
}
|
|
|
}
|
|
@@ -216,7 +190,6 @@ static int RTreeInsertRect2F(struct RTree_Rect *r, union RTree_Child child, int
|
|
|
RTreeAddBranch(b, s[top].sn, &n2, ee, cover, overflow, t);
|
|
|
|
|
|
/* update node */
|
|
|
- /* RTreePutNode(&(s[top].sn), s[top].pos, t); */
|
|
|
RTreeNodeChanged(s[top].sn, s[top].pos, t);
|
|
|
|
|
|
/* write out new node if node was split */
|
|
@@ -228,9 +201,6 @@ static int RTreeInsertRect2F(struct RTree_Rect *r, union RTree_Child child, int
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /* copy node */
|
|
|
- RTreeCopyNode(newnode, n2, t);
|
|
|
-
|
|
|
return result;
|
|
|
}
|
|
|
|
|
@@ -247,48 +217,42 @@ int RTreeInsertRectF(struct RTree_Rect *r, union RTree_Child child, int level,
|
|
|
{
|
|
|
struct RTree_ListBranch *reInsertList = NULL;
|
|
|
struct RTree_ListBranch *e;
|
|
|
- int i, result;
|
|
|
+ int result;
|
|
|
char overflow[MAXLEVEL];
|
|
|
struct RTree_Branch *b = &(t->tmpb1);
|
|
|
off_t newnode_pos = -1;
|
|
|
|
|
|
struct RTree_Node *oldroot;
|
|
|
- static struct RTree_Node newroot, newnode;
|
|
|
- static int rect_init = 0;
|
|
|
+ static struct RTree_Node *newroot = NULL, *newnode = NULL;
|
|
|
|
|
|
- if (!rect_init) {
|
|
|
- for (i = 0; i < MAXCARD; i++) {
|
|
|
- /* oldroot.branch[i].rect.boundary = RTreeAllocBoundary(t); */
|
|
|
- newroot.branch[i].rect.boundary = RTreeAllocBoundary(t);
|
|
|
- newnode.branch[i].rect.boundary = RTreeAllocBoundary(t);
|
|
|
- }
|
|
|
- rect_init = t->ndims_alloc;
|
|
|
+ if (!newroot) {
|
|
|
+ newroot = RTreeAllocNode(t, 1);
|
|
|
+ newnode = RTreeAllocNode(t, 1);
|
|
|
}
|
|
|
|
|
|
/* R*-tree forced reinsertion: for each level only once */
|
|
|
memset(overflow, t->overflow, MAXLEVEL);
|
|
|
|
|
|
- result = RTreeInsertRect2F(r, child, level, &newnode, &newnode_pos,
|
|
|
+ result = RTreeInsertRect2F(r, child, level, newnode, &newnode_pos,
|
|
|
t, &reInsertList, overflow);
|
|
|
|
|
|
if (result == 1) { /* root split */
|
|
|
oldroot = RTreeGetNode(t->rootpos, t->rootlevel, t);
|
|
|
- /* RTreeGetNode(&oldroot, t->rootpos, t->rootlevel, t); */
|
|
|
/* grow a new root, & tree taller */
|
|
|
t->rootlevel++;
|
|
|
- RTreeInitNode(t, &newroot, NODETYPE(t->rootlevel, t->fd));
|
|
|
- newroot.level = t->rootlevel;
|
|
|
+ RTreeInitNode(t, newroot, NODETYPE(t->rootlevel, t->fd));
|
|
|
+ newroot->level = t->rootlevel;
|
|
|
/* branch for old root */
|
|
|
RTreeNodeCover(oldroot, &(b->rect), t);
|
|
|
b->child.pos = t->rootpos;
|
|
|
- RTreeAddBranch(b, &newroot, NULL, NULL, NULL, NULL, t);
|
|
|
+ RTreeAddBranch(b, newroot, NULL, NULL, NULL, NULL, t);
|
|
|
/* branch for new node created by RTreeInsertRect2() */
|
|
|
- RTreeNodeCover(&newnode, &(b->rect), t);
|
|
|
+ RTreeNodeCover(newnode, &(b->rect), t);
|
|
|
b->child.pos = newnode_pos; /* offset to new node as returned by RTreeInsertRect2F() */
|
|
|
- RTreeAddBranch(b, &newroot, NULL, NULL, NULL, NULL, t);
|
|
|
+ RTreeAddBranch(b, newroot, NULL, NULL, NULL, NULL, t);
|
|
|
/* write new root node */
|
|
|
t->rootpos = RTreeGetNodePos(t);
|
|
|
- RTreeWriteNode(&newroot, t);
|
|
|
+ RTreeWriteNode(newroot, t);
|
|
|
t->n_nodes++;
|
|
|
|
|
|
return result;
|
|
@@ -304,27 +268,26 @@ int RTreeInsertRectF(struct RTree_Rect *r, union RTree_Child child, int level,
|
|
|
RTreeFreeListBranch(e);
|
|
|
/* reinsert branches */
|
|
|
result =
|
|
|
- RTreeInsertRect2F(&(b->rect), b->child, level, &newnode, &newnode_pos, t,
|
|
|
+ RTreeInsertRect2F(&(b->rect), b->child, level, newnode, &newnode_pos, t,
|
|
|
&reInsertList, overflow);
|
|
|
|
|
|
if (result == 1) { /* root split */
|
|
|
oldroot = RTreeGetNode(t->rootpos, t->rootlevel, t);
|
|
|
- /* RTreeGetNode(&oldroot, t->rootpos, t->rootlevel, t); */
|
|
|
/* grow a new root, & tree taller */
|
|
|
t->rootlevel++;
|
|
|
- RTreeInitNode(t, &newroot, NODETYPE(t->rootlevel, t->fd));
|
|
|
- newroot.level = t->rootlevel;
|
|
|
+ RTreeInitNode(t, newroot, NODETYPE(t->rootlevel, t->fd));
|
|
|
+ newroot->level = t->rootlevel;
|
|
|
/* branch for old root */
|
|
|
RTreeNodeCover(oldroot, &(b->rect), t);
|
|
|
b->child.pos = t->rootpos;
|
|
|
- RTreeAddBranch(b, &newroot, NULL, NULL, NULL, NULL, t);
|
|
|
+ RTreeAddBranch(b, newroot, NULL, NULL, NULL, NULL, t);
|
|
|
/* branch for new node created by RTreeInsertRect2() */
|
|
|
- RTreeNodeCover(&newnode, &(b->rect), t);
|
|
|
+ RTreeNodeCover(newnode, &(b->rect), t);
|
|
|
b->child.pos = newnode_pos;
|
|
|
- RTreeAddBranch(b, &newroot, NULL, NULL, NULL, NULL, t);
|
|
|
+ RTreeAddBranch(b, newroot, NULL, NULL, NULL, NULL, t);
|
|
|
/* write new root node */
|
|
|
t->rootpos = RTreeGetNodePos(t);
|
|
|
- RTreeWriteNode(&newroot, t);
|
|
|
+ RTreeWriteNode(newroot, t);
|
|
|
t->n_nodes++;
|
|
|
}
|
|
|
}
|
|
@@ -347,23 +310,14 @@ RTreeDeleteRect2F(struct RTree_Rect *r, union RTree_Child child, struct RTree *t
|
|
|
struct RTree_Node *n;
|
|
|
int top = 0, down = 0;
|
|
|
int minfill;
|
|
|
- struct fstack *s = t->fs;
|
|
|
+ struct nstack *s = t->ns;
|
|
|
|
|
|
- static struct RTree_Rect nr;
|
|
|
- static int rect_init = 0;
|
|
|
-
|
|
|
- assert(ee);
|
|
|
-
|
|
|
- if (!rect_init) {
|
|
|
- nr.boundary = RTreeAllocBoundary(t);
|
|
|
- rect_init = 1;
|
|
|
- }
|
|
|
+ struct RTree_Rect *nr = &(t->orect);
|
|
|
|
|
|
/* add root node position to stack */
|
|
|
currlevel = t->rootlevel;
|
|
|
s[top].pos = t->rootpos;
|
|
|
s[top].sn = RTreeGetNode(s[top].pos, currlevel, t);
|
|
|
- /* RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t); */
|
|
|
s[top].branch_id = 0;
|
|
|
|
|
|
while (notfound && top >= 0) {
|
|
@@ -378,7 +332,6 @@ RTreeDeleteRect2F(struct RTree_Rect *r, union RTree_Child child, struct RTree *t
|
|
|
/* add next node to stack */
|
|
|
s[top].pos = n->branch[i].child.pos;
|
|
|
s[top].sn = RTreeGetNode(s[top].pos, currlevel, t);
|
|
|
- /* RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t); */
|
|
|
s[top].branch_id = 0;
|
|
|
|
|
|
notfound = 0;
|
|
@@ -398,7 +351,6 @@ RTreeDeleteRect2F(struct RTree_Rect *r, union RTree_Child child, struct RTree *t
|
|
|
if (s[top].sn->branch[i].child.id &&
|
|
|
s[top].sn->branch[i].child.id == child.id) { /* found item */
|
|
|
RTreeDisconnectBranch(s[top].sn, i, t);
|
|
|
- /* RTreePutNode(&(s[top].sn), s[top].pos, t); */
|
|
|
RTreeNodeChanged(s[top].sn, s[top].pos, t);
|
|
|
t->n_leafs--;
|
|
|
notfound = 0;
|
|
@@ -419,22 +371,19 @@ RTreeDeleteRect2F(struct RTree_Rect *r, union RTree_Child child, struct RTree *t
|
|
|
down = top;
|
|
|
top--;
|
|
|
i = s[top].branch_id - 1;
|
|
|
- assert(s[down].sn->level == s[top].sn->level - 1);
|
|
|
|
|
|
minfill = (s[down].sn->level ? t->min_node_fill : t->min_leaf_fill);
|
|
|
if (s[down].sn->count >= minfill) {
|
|
|
/* just update node cover */
|
|
|
- RTreeNodeCover(s[down].sn, &nr, t);
|
|
|
+ RTreeNodeCover(s[down].sn, nr, t);
|
|
|
/* rewrite rect */
|
|
|
- if (!RTreeCompareRect(&nr, &(s[top].sn->branch[i].rect), t)) {
|
|
|
- /* RTreeUpdateRect(&nr, &(s[top].sn), s[top].pos, i, t); */
|
|
|
- RTreeCopyRect(&(s[top].sn->branch[i].rect), &nr, t);
|
|
|
+ if (!RTreeCompareRect(nr, &(s[top].sn->branch[i].rect), t)) {
|
|
|
+ RTreeCopyRect(&(s[top].sn->branch[i].rect), nr, t);
|
|
|
RTreeNodeChanged(s[top].sn, s[top].pos, t);
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
/* not enough entries in child, eliminate child node */
|
|
|
- assert(s[top].sn->branch[i].child.pos == s[down].pos);
|
|
|
n = RTreeAllocNode(t, s[down].sn->level);
|
|
|
/* copy node */
|
|
|
RTreeCopyNode(n, s[down].sn, t);
|
|
@@ -442,7 +391,6 @@ RTreeDeleteRect2F(struct RTree_Rect *r, union RTree_Child child, struct RTree *t
|
|
|
RTreeReInsertNode(n, ee);
|
|
|
RTreeDisconnectBranch(s[top].sn, i, t);
|
|
|
|
|
|
- /* RTreePutNode(&(s[top].sn), s[top].pos, t); */
|
|
|
RTreeNodeChanged(s[top].sn, s[top].pos, t);
|
|
|
}
|
|
|
}
|
|
@@ -464,16 +412,6 @@ int RTreeDeleteRectF(struct RTree_Rect *r, union RTree_Child child, struct RTree
|
|
|
struct RTree_Node *n;
|
|
|
struct RTree_ListNode *e, *reInsertList = NULL;
|
|
|
|
|
|
- static struct RTree_Node rn;
|
|
|
- static int rect_init = 0;
|
|
|
-
|
|
|
- if (!rect_init) {
|
|
|
- for (i = 0; i < MAXCARD; i++) {
|
|
|
- rn.branch[i].rect.boundary = RTreeAllocBoundary(t);
|
|
|
- }
|
|
|
- rect_init = 1;
|
|
|
- }
|
|
|
-
|
|
|
if (!RTreeDeleteRect2F(r, child, t, &reInsertList)) {
|
|
|
/* found and deleted a data item */
|
|
|
|
|
@@ -505,10 +443,6 @@ int RTreeDeleteRectF(struct RTree_Rect *r, union RTree_Child child, struct RTree
|
|
|
|
|
|
/* check for redundant root (not leaf, 1 child) and eliminate */
|
|
|
n = RTreeGetNode(t->rootpos, t->rootlevel, t);
|
|
|
- /*
|
|
|
- RTreeGetNode(&rn, t->rootpos, t->rootlevel, t);
|
|
|
- n = &rn;
|
|
|
- */
|
|
|
|
|
|
if (n->count == 1 && n->level > 0) {
|
|
|
for (i = 0; i < t->nodecard; i++) {
|