[GRASS-SVN] r54107 - in grass/trunk/lib/vector: diglib rtree

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Nov 30 02:34:13 PST 2012


Author: mmetz
Date: 2012-11-30 02:34:12 -0800 (Fri, 30 Nov 2012)
New Revision: 54107

Modified:
   grass/trunk/lib/vector/diglib/spindex_rw.c
   grass/trunk/lib/vector/rtree/index.c
   grass/trunk/lib/vector/rtree/index.h
   grass/trunk/lib/vector/rtree/indexf.c
   grass/trunk/lib/vector/rtree/indexm.c
   grass/trunk/lib/vector/rtree/io.c
   grass/trunk/lib/vector/rtree/node.c
   grass/trunk/lib/vector/rtree/rtree.h
Log:
huhabla: rtree dynamic memory allocation

Modified: grass/trunk/lib/vector/diglib/spindex_rw.c
===================================================================
--- grass/trunk/lib/vector/diglib/spindex_rw.c	2012-11-30 10:07:39 UTC (rev 54106)
+++ grass/trunk/lib/vector/diglib/spindex_rw.c	2012-11-30 10:34:12 UTC (rev 54107)
@@ -615,12 +615,10 @@
 int rtree_dump_node_file(FILE * fp, off_t pos, int with_z, struct RTree *t)
 {
     int i;
-    static struct RTree_Node n;
-    static int node_init = 0;
+    static struct RTree_Node *n = NULL;
     
-    if (!node_init) {
-	for (i = 0; i < MAXCARD; i++)
-	    n.branch[i].rect.boundary = RTreeAllocBoundary(t);
+    if (!n) {
+	n = RTreeAllocNode(t, 1);
     }
 
     /* recursive nearly-but-a-bit-messy depth-first pre-order traversal
@@ -628,21 +626,21 @@
     /* TODO: change to non-recursive depth-first post-order traversal */
     /* left for comparison with GRASS6.x */
 
-    RTreeReadNode(&n, pos, t);
-    fprintf(fp, "Node level=%d  count=%d\n", n.level, n.count);
+    RTreeReadNode(n, pos, t);
+    fprintf(fp, "Node level=%d  count=%d\n", n->level, n->count);
 
-    if (n.level > 0)
+    if (n->level > 0)
 	for (i = 0; i < NODECARD; i++) {
-	    if (n.branch[i].child.pos >= 0) {
+	    if (n->branch[i].child.pos >= 0) {
 		fprintf(fp, "  Branch %d", i);
-		rtree_dump_branch_file(fp, &n.branch[i], with_z, n.level, t);
+		rtree_dump_branch_file(fp, &(n->branch[i]), with_z, n->level, t);
 	    }
 	}
     else
 	for (i = 0; i < LEAFCARD; i++) {
-	    if (n.branch[i].child.id) {
+	    if (n->branch[i].child.id) {
 		fprintf(fp, "  Branch %d", i);
-		rtree_dump_branch_file(fp, &n.branch[i], with_z, n.level, t);
+		rtree_dump_branch_file(fp, &(n->branch[i]), with_z, n->level, t);
 	    }
 	}
 
@@ -691,7 +689,7 @@
     int sidx_nodesize, sidx_leafsize;
     struct RTree_Node *n;
     int i, j, writeout, maxcard;
-    struct spidxpstack s[MAXLEVEL];
+    struct spidxpstack *s = G_malloc(MAXLEVEL * sizeof(struct spidxstack));
     int top = 0;
 
     /* should be foolproof */
@@ -768,6 +766,8 @@
 	    }
 	}
     }
+    
+    G_free(s);
 
     return nextfreepos;
 }
@@ -793,21 +793,19 @@
     int sidx_nodesize, sidx_leafsize;
     struct RTree_Node *n;
     int i, j, writeout, maxcard;
-    static struct spidxstack s[MAXLEVEL];
-    static int stack_init = 0;
+    static struct spidxstack *s = NULL;
     int top = 0;
     
-    if (!stack_init) {
+    if (!s) {
+	s = G_malloc(MAXLEVEL * sizeof(struct spidxstack));
 	for (i = 0; i < MAXLEVEL; i++) {
+	    s[i].sn.branch = G_malloc(MAXCARD * sizeof(struct RTree_Branch));
 	    for (j = 0; j < MAXCARD; j++) {
 		s[i].sn.branch[j].rect.boundary = G_malloc(6 * sizeof(RectReal));
 	    }
 	}
-	stack_init = 1;
     }
 
-
-
     /* write pending changes to file */
     RTreeFlushBuffer(t);
 
@@ -915,17 +913,17 @@
     struct RTree_Node *newnode = NULL;
     int i, j, loadnode, maxcard;
     struct spidxstack *last;
-    static struct spidxstack s[MAXLEVEL];
-    static int stack_init = 0;
+    static struct spidxstack *s = NULL;
     int top = 0;
 
-    if (!stack_init) {
+    if (!s) {
+	s = G_malloc(MAXLEVEL * sizeof(struct spidxstack));
 	for (i = 0; i < MAXLEVEL; i++) {
+	    s[i].sn.branch = G_malloc(MAXCARD * sizeof(struct RTree_Branch));
 	    for (j = 0; j < MAXCARD; j++) {
 		s[i].sn.branch[j].rect.boundary = G_malloc(6 * sizeof(RectReal));
 	    }
 	}
-	stack_init = 1;
     }
 
     /* stack size of t->rootlevel + 1 would be enough because of
@@ -1041,17 +1039,17 @@
     off_t newnode_pos = -1;
     int i, j, loadnode, maxcard;
     struct spidxstack *last;
-    static struct spidxstack s[MAXLEVEL];
-    static int stack_init = 0;
+    static struct spidxstack *s = NULL;
     int top = 0;
 
-    if (!stack_init) {
+    if (!s) {
+	s = G_malloc(MAXLEVEL * sizeof(struct spidxstack));
 	for (i = 0; i < MAXLEVEL; i++) {
+	    s[i].sn.branch = G_malloc(MAXCARD * sizeof(struct RTree_Branch));
 	    for (j = 0; j < MAXCARD; j++) {
 		s[i].sn.branch[j].rect.boundary = G_malloc(6 * sizeof(RectReal));
 	    }
 	}
-	stack_init = 1;
     }
 	
     /* stack size of t->rootlevel + 1 would be enough because of
@@ -1343,10 +1341,12 @@
     int which, i = 0;
 
     /* check mru first */
+    /* t->used[level][i] */
     while (t->nb[level][t->used[level][i]].pos != nodepos &&
-           t->nb[level][t->used[level][i]].pos >= 0 &&
-	   i < NODE_BUFFER_SIZE - 1)
+	   t->nb[level][t->used[level][i]].pos >= 0 &&
+	   i < NODE_BUFFER_SIZE - 1) {
 	i++;
+    }
 
     which = t->used[level][i];
 
@@ -1355,6 +1355,8 @@
 	t->nb[level][which].pos = nodepos;
     }
     assert(t->nb[level][which].n.level == level);
+
+
     /* make it mru */
     if (i) { /* t->used[level][0] != which */
 #if 0

Modified: grass/trunk/lib/vector/rtree/index.c
===================================================================
--- grass/trunk/lib/vector/rtree/index.c	2012-11-30 10:07:39 UTC (rev 54106)
+++ grass/trunk/lib/vector/rtree/index.c	2012-11-30 10:34:12 UTC (rev 54107)
@@ -110,19 +110,19 @@
 	    new_rtree->nb[i] = new_rtree->nb[i - 1] + NODE_BUFFER_SIZE;
 	}
 
+	new_rtree->used = malloc(MAXLEVEL * sizeof(int *));
+	new_rtree->used[0] = malloc(MAXLEVEL * NODE_BUFFER_SIZE * sizeof(int));
 	for (i = 0; i < MAXLEVEL; i++) {
-
-	    /*
-	    for (j = 0; j < MAXCARD; j++) {
-		new_rtree->fs[i].sn.branch[j].rect.boundary = RTreeAllocBoundary(new_rtree);
-	    }
-	    */
+	    if (i)
+		new_rtree->used[i] = new_rtree->used[i - 1] + NODE_BUFFER_SIZE;
 	    for (j = 0; j < NODE_BUFFER_SIZE; j++) {
 		new_rtree->nb[i][j].dirty = 0;
 		new_rtree->nb[i][j].pos = -1;
 		/* usage order */
 		new_rtree->used[i][j] = j;
 
+		new_rtree->nb[i][j].n.branch = malloc(MAXCARD * sizeof(struct RTree_Branch));
+
 		/* alloc memory for rectangles */
 		for (k = 0; k < MAXCARD; k++) {
 		    new_rtree->nb[i][j].n.branch[k].rect.boundary = RTreeAllocBoundary(new_rtree);
@@ -166,12 +166,16 @@
     new_rtree->n_leafs = 0;
 
     /* initialize temp variables */
+    new_rtree->ns = malloc(MAXLEVEL * sizeof(struct nstack));
+    
     new_rtree->p.cover[0].boundary = RTreeAllocBoundary(new_rtree);
     new_rtree->p.cover[1].boundary = RTreeAllocBoundary(new_rtree);
     
     new_rtree->tmpb1.rect.boundary = RTreeAllocBoundary(new_rtree);
     new_rtree->tmpb2.rect.boundary = RTreeAllocBoundary(new_rtree);
     new_rtree->c.rect.boundary = RTreeAllocBoundary(new_rtree);
+
+    new_rtree->BranchBuf = malloc((MAXCARD + 1) * sizeof(struct RTree_Branch));
     for (i = 0; i <= MAXCARD; i++) {
 	new_rtree->BranchBuf[i].rect.boundary = RTreeAllocBoundary(new_rtree);
     }
@@ -226,11 +230,15 @@
 	    free(t->free_nodes.pos);
 	free(t->nb[0]);
 	free(t->nb);
+	free(t->used[0]);
+	free(t->used);
     }
     else if (t->root)
 	RTreeDestroyNode(t->root, t->root->level ? t->nodecard : t->leafcard);
 
     /* free temp variables */
+    free(t->ns);
+    
     RTreeFreeBoundary(&(t->p.cover[0]));
     RTreeFreeBoundary(&(t->p.cover[1]));
     
@@ -240,6 +248,7 @@
     for (i = 0; i <= MAXCARD; i++) {
         RTreeFreeBoundary(&(t->BranchBuf[i].rect));
     }
+    free(t->BranchBuf);
     RTreeFreeBoundary(&(t->rect_0));
     RTreeFreeBoundary(&(t->rect_1));
     RTreeFreeBoundary(&(t->upperrect));

Modified: grass/trunk/lib/vector/rtree/index.h
===================================================================
--- grass/trunk/lib/vector/rtree/index.h	2012-11-30 10:07:39 UTC (rev 54106)
+++ grass/trunk/lib/vector/rtree/index.h	2012-11-30 10:34:12 UTC (rev 54107)
@@ -111,9 +111,7 @@
 /* io.c */
 struct RTree_Node *RTreeGetNode(off_t, int, struct RTree *);
 void RTreeNodeChanged(struct RTree_Node *, off_t , struct RTree *);
-void RTreePutNode(struct RTree_Node *, off_t, struct RTree *);
 size_t RTreeRewriteNode(struct RTree_Node *, off_t, struct RTree *);
-void RTreeUpdateRect(struct RTree_Rect *, struct RTree_Node *, off_t, int, struct RTree *);
 void RTreeAddNodePos(off_t, int, struct RTree *);
 
 #endif /* _INDEX_ */

Modified: grass/trunk/lib/vector/rtree/indexf.c
===================================================================
--- grass/trunk/lib/vector/rtree/indexf.c	2012-11-30 10:07:39 UTC (rev 54106)
+++ grass/trunk/lib/vector/rtree/indexf.c	2012-11-30 10:34:12 UTC (rev 54107)
@@ -42,7 +42,7 @@
     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 @@
     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 @@
 		    /* 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 @@
     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 @@
 	/* 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 @@
 	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 @@
 	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 @@
 		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 @@
 	}
     }
 
-    /* copy node */
-    RTreeCopyNode(newnode, n2, t);
-
     return result;
 }
 
@@ -247,48 +217,42 @@
 {
     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 @@
 	    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 @@
     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 @@
 		    /* 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 @@
 		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 @@
 	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 @@
 	    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 @@
     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 @@
 
 	/* 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++) {

Modified: grass/trunk/lib/vector/rtree/indexm.c
===================================================================
--- grass/trunk/lib/vector/rtree/indexm.c	2012-11-30 10:07:39 UTC (rev 54106)
+++ grass/trunk/lib/vector/rtree/indexm.c	2012-11-30 10:34:12 UTC (rev 54107)
@@ -39,7 +39,7 @@
     int hitCount = 0, notfound;
     int i;
     int top = 0;
-    struct mstack *s = t->ms;
+    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 */
@@ -111,7 +111,7 @@
     int top = 0, down = 0;
     int result;
     struct RTree_Branch *b = &(t->tmpb2);
-    struct mstack *s = t->ms;
+    struct nstack *s = t->ns;
 
     /* add root node to stack */
     s[top].sn = t->root;
@@ -270,7 +270,7 @@
     struct RTree_Node *n;
     int top = 0, down = 0;
     int minfill;
-    struct mstack *s = t->ms;
+    struct nstack *s = t->ns;
 
     assert(ee);
 

Modified: grass/trunk/lib/vector/rtree/io.c
===================================================================
--- grass/trunk/lib/vector/rtree/io.c	2012-11-30 10:07:39 UTC (rev 54106)
+++ grass/trunk/lib/vector/rtree/io.c	2012-11-30 10:34:12 UTC (rev 54107)
@@ -24,7 +24,7 @@
 #include <grass/config.h>
 #include "index.h"
 
-#define USAGE_SWAP
+/* #define USAGE_SWAP */
 
 /* add new free node position for recycling */
 void RTreeAddNodePos(off_t pos, int level, struct RTree *t)
@@ -50,7 +50,6 @@
     /* is it possible that this node is not in the buffer? */
     assert(i < NODE_BUFFER_SIZE);
     which = t->used[level][i];
-    assert(t->nb[level][which].n.level == level);
     t->nb[level][which].pos = -1;
     t->nb[level][which].dirty = 0;
     
@@ -123,8 +122,6 @@
     if (t->nb[level][which].pos != nodepos) {
 	/* rewrite node in buffer */
 	if (t->nb[level][which].dirty) {
-	    assert(t->nb[level][which].pos >= 0);
-	    assert(t->nb[level][which].n.level == level);
 	    RTreeRewriteNode(&(t->nb[level][which].n),
 	                     t->nb[level][which].pos, t);
 	    t->nb[level][which].dirty = 0;
@@ -132,7 +129,6 @@
 	RTreeReadNode(&(t->nb[level][which].n), nodepos, t);
 	t->nb[level][which].pos = nodepos;
     }
-    assert(t->nb[level][which].n.level == level);
     /* make it mru */
     if (i) { /* t->used[level][0] != which */
 #ifdef USAGE_SWAP
@@ -202,7 +198,6 @@
     /* as it is used, it should always be mru */
     assert(i == 0);
     which = t->used[n->level][i];
-    assert(t->nb[n->level][which].n.level == n->level);
 
     t->nb[n->level][which].dirty = 1;
 
@@ -221,84 +216,6 @@
     }
 }
 
-/* update node in buffer */
-void RTreePutNode(struct RTree_Node *n, off_t nodepos, struct RTree *t)
-{
-    int which, i = 0;
-
-    /* check mru first */
-    while (t->nb[n->level][t->used[n->level][i]].pos != nodepos &&
-           i < NODE_BUFFER_SIZE)
-	i++;
-
-    assert(i < NODE_BUFFER_SIZE);
-    /* as it is used, it should always be mru */
-    assert(i == 0);
-    which = t->used[n->level][i];
-
-    assert(t->nb[n->level][which].n.level == n->level);
-    /* copy node */
-    RTreeCopyNode(&(t->nb[n->level][which].n), n, t);
-    t->nb[n->level][which].dirty = 1;
-
-    /* make it mru */
-    if (i) { /* t->used[level][0] != which */
-#ifdef USAGE_SWAP
-	t->used[n->level][i] = t->used[n->level][0];
-	t->used[n->level][0] = which;
-#else
-	while (i) {
-	    t->used[n->level][i] = t->used[n->level][i - 1];
-	    i--;
-	}
-	t->used[n->level][0] = which;
-#endif
-    }
-}
-
-/* update rectangle */
-void RTreeUpdateRect(struct RTree_Rect *r, struct RTree_Node *n,
-                     off_t nodepos, int b, struct RTree *t)
-{
-    int i, j, k, which;
-
-    /* check mru first */
-    i = 0;
-    while (t->nb[n->level][t->used[n->level][i]].pos != nodepos &&
-           i < NODE_BUFFER_SIZE)
-	i++;
-
-    assert(i < NODE_BUFFER_SIZE);
-    /* as it is used, it should always be mru */
-    assert(i == 0);
-    which = t->used[n->level][i];
-
-    assert(t->nb[n->level][which].n.level == n->level);
-    for (j = 0; j < t->ndims_alloc; j++) {
-	t->nb[n->level][which].n.branch[b].rect.boundary[j] =
-	                  n->branch[b].rect.boundary[j] = r->boundary[j];
-	k = j + t->ndims_alloc;
-	t->nb[n->level][which].n.branch[b].rect.boundary[k] =
-	                  n->branch[b].rect.boundary[k] = r->boundary[k];
-    }
-
-    t->nb[n->level][which].dirty = 1;
-
-    /* make it mru */
-    if (i) { /* t->used[level][0] != which */
-#ifdef USAGE_SWAP
-	t->used[n->level][i] = t->used[n->level][0];
-	t->used[n->level][0] = which;
-#else
-	while (i) {
-	    t->used[n->level][i] = t->used[n->level][i - 1];
-	    i--;
-	}
-	t->used[n->level][0] = which;
-#endif
-    }
-}
-
 /* flush pending changes to file */
 void RTreeFlushBuffer(struct RTree *t)
 {

Modified: grass/trunk/lib/vector/rtree/node.c
===================================================================
--- grass/trunk/lib/vector/rtree/node.c	2012-11-30 10:07:39 UTC (rev 54106)
+++ grass/trunk/lib/vector/rtree/node.c	2012-11-30 10:34:12 UTC (rev 54107)
@@ -81,6 +81,8 @@
 
     n->count = 0;
     n->level = level;
+    
+    n->branch = malloc(MAXCARD * sizeof(struct RTree_Branch));
 
     for (i = 0; i < MAXCARD; i++) {
 	n->branch[i].rect.boundary = RTreeAllocBoundary(t);
@@ -99,6 +101,7 @@
     for (i = 0; i < MAXCARD; i++)
 	RTreeFreeBoundary(&(n->branch[i].rect));
 
+    free(n->branch);
     free(n);
 }
 

Modified: grass/trunk/lib/vector/rtree/rtree.h
===================================================================
--- grass/trunk/lib/vector/rtree/rtree.h	2012-11-30 10:07:39 UTC (rev 54106)
+++ grass/trunk/lib/vector/rtree/rtree.h	2012-11-30 10:34:12 UTC (rev 54107)
@@ -52,7 +52,7 @@
 #define MAXLEVEL 20        /* 8^MAXLEVEL items are guaranteed to fit into the tree */
 
 /* number of nodes buffered per level */
-#define NODE_BUFFER_SIZE 8
+#define NODE_BUFFER_SIZE 32
 
 struct RTree_Rect
 {
@@ -78,7 +78,7 @@
 {
     int count;          /* number of branches */
     int level;		/* 0 is leaf, others positive */
-    struct RTree_Branch branch[MAXCARD];
+    struct RTree_Branch *branch;
 };
 
 /*
@@ -99,20 +99,13 @@
 typedef int rt_valid_child_fn(union RTree_Child *);
 
 /* temp vars for each tree */
-/* stacks used for non-recursive insertion/deletion */
-/* stack for file-based index */
-struct fstack
+/* node stack used for non-recursive insertion/deletion */
+struct nstack
 {
     struct RTree_Node *sn;	/* stack node */
     int branch_id;		/* branch number to follow down */
     off_t pos;			/* file position of stack node */
 };
-/* stack for memory-based index */
-struct mstack
-{
-    struct RTree_Node *sn;	/* stack node pointer */
-    int branch_id;		/* branch number to follow down */
-};
 
 /* node buffer for file-based index */
 struct NodeBuffer
@@ -171,7 +164,7 @@
     /* usage order of buffered nodes per level
      * used[level][0] = most recently used
      * used[level][NODE_BUFFER_SIZE - 1] = least recently used */
-    int used[MAXLEVEL][NODE_BUFFER_SIZE];
+    int **used;
 
     /* insert, delete, search */
     rt_insert_fn *insert_rect;
@@ -183,13 +176,12 @@
 
     /* internal variables, specific for each tree,
      * allocated with tree initialization */
-    /* stacks for tree traversal */
-    struct mstack ms[MAXLEVEL];
-    struct fstack fs[MAXLEVEL];
+    /* node stack for tree traversal */
+    struct nstack *ns;
     
     /* variables for splitting / forced reinsertion */
     struct RTree_PartitionVars p;
-    struct RTree_Branch BranchBuf[MAXCARD + 1];
+    struct RTree_Branch *BranchBuf;
 
     struct RTree_Branch tmpb1, tmpb2, c;
     int BranchCount;



More information about the grass-commit mailing list