[GRASS-SVN] r36532 - grass/trunk/lib/vector/diglib

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Mar 30 12:14:51 EDT 2009


Author: mmetz
Date: 2009-03-30 12:14:50 -0400 (Mon, 30 Mar 2009)
New Revision: 36532

Modified:
   grass/trunk/lib/vector/diglib/rbtree.c
Log:
easier interface for Red Black Tree

Modified: grass/trunk/lib/vector/diglib/rbtree.c
===================================================================
--- grass/trunk/lib/vector/diglib/rbtree.c	2009-03-30 15:06:44 UTC (rev 36531)
+++ grass/trunk/lib/vector/diglib/rbtree.c	2009-03-30 16:14:50 UTC (rev 36532)
@@ -265,17 +265,13 @@
  */
 int rbtree_init_trav(struct RB_TRAV *trav, struct RB_TREE *tree)
 {
-    int i;
-
     assert(trav && tree);
 
     trav->tree = tree;
     trav->curr_node = tree->root;
     trav->first = 1;
+    trav->top = 0;
 
-    for (i = 0; i < RBTREE_MAX_HEIGHT; i++)
-	trav->up[i] = NULL;
-
     return 0;
 }
 
@@ -290,19 +286,19 @@
     
     if (trav->curr_node == NULL) {
 	if (trav->first)
-	    G_warning("RB tree: empty tree");
+	    G_debug(1, "RB tree: empty tree");
 	else
-	    G_warning("RB tree: finished traversing");
+	    G_debug(1, "RB tree: finished traversing");
 
 	return NULL;
     }
 	
-    if (trav->first) {
+    if (!trav->first)
+	return rbtree_next(trav);
+    else {
 	trav->first = 0;
 	return rbtree_first(trav);
     }
-    else
-	return rbtree_next(trav);
 }
 
 /* find start point to traverse the tree in ascending order
@@ -310,7 +306,7 @@
  * magnitudes faster than traversing the whole tree
  * may return first item that's smaller or first item that's larger
  * struct RB_TRAV *trav needs to be initialized first
- * returns pointer to data, NULL on error
+ * returns pointer to data, NULL when finished
  */
 void *rbtree_traverse_start(struct RB_TRAV *trav, const void *data)
 {
@@ -318,16 +314,20 @@
 
     assert(trav && data);
 
-    if (trav->first == 0) {
-	G_debug(1, "RB tree: trav must be initialised first");
-	return NULL;
-    }
-
     if (trav->curr_node == NULL) {
-	G_warning("RB tree: empty tree");
+	if (trav->first)
+	    G_warning("RB tree: empty tree");
+	else
+	    G_warning("RB tree: finished traversing");
+
 	return NULL;
     }
 	
+    if (!trav->first)
+	return rbtree_next(trav);
+
+    /* else first time, get start node */
+
     trav->first = 0;
     trav->top = 0;
 
@@ -365,8 +365,6 @@
  */
 void *rbtree_first(struct RB_TRAV *trav)
 {
-    trav->top = 0;
-
     /* get smallest item */
     while (trav->curr_node->link[0] != NULL) {
 	trav->up[trav->top++] = trav->curr_node;



More information about the grass-commit mailing list