[GRASS-SVN] r39648 - grass/trunk/lib/segment

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Oct 30 09:25:02 EDT 2009


Author: mmetz
Date: 2009-10-30 09:25:01 -0400 (Fri, 30 Oct 2009)
New Revision: 39648

Modified:
   grass/trunk/lib/segment/address.c
   grass/trunk/lib/segment/pagein.c
   grass/trunk/lib/segment/put.c
   grass/trunk/lib/segment/rbtree.c
   grass/trunk/lib/segment/release.c
   grass/trunk/lib/segment/seek.c
   grass/trunk/lib/segment/setup.c
Log:
format and clean up code

Modified: grass/trunk/lib/segment/address.c
===================================================================
--- grass/trunk/lib/segment/address.c	2009-10-30 12:17:45 UTC (rev 39647)
+++ grass/trunk/lib/segment/address.c	2009-10-30 13:25:01 UTC (rev 39648)
@@ -14,41 +14,47 @@
 
 #include <grass/segment.h>
 
-int segment_address_fast(const SEGMENT * SEG, int row, int col, int *n, int *index)
+int segment_address_fast(const SEGMENT * SEG, int row, int col, int *n,
+			 int *index)
 {
     if (row) {
-        int seg_r = row >> SEG->srowbits;
-        int seg_c = col >> SEG->scolbits;
-     
-        *n = seg_r * SEG->spr + seg_c;
-        *index = ((row - (seg_r << SEG->srowbits)) << SEG->scolbits) + col - (seg_c << SEG->scolbits);
+	int seg_r = row >> SEG->srowbits;
+	int seg_c = col >> SEG->scolbits;
+
+	*n = seg_r * SEG->spr + seg_c;
+	*index =
+	    ((row - (seg_r << SEG->srowbits)) << SEG->scolbits) + col -
+	    (seg_c << SEG->scolbits);
     }
     /* for simple arrays */
     else {
-        *n = col >> SEG->scolbits;
-        *index = col - ((*n) << SEG->scolbits);
+	*n = col >> SEG->scolbits;
+	*index = col - ((*n) << SEG->scolbits);
     }
     if (SEG->slow_seek == 0)
-        *index = *index << SEG->lenbits;
+	*index = *index << SEG->lenbits;
     else
-        *index *= SEG->len;
+	*index *= SEG->len;
 
     return 0;
 }
 
-int segment_address_slow(const SEGMENT * SEG, int row, int col, int *n, int *index)
+int segment_address_slow(const SEGMENT * SEG, int row, int col, int *n,
+			 int *index)
 {
     if (row) {
-        int seg_r = row / SEG->srows;
-        int seg_c = col / SEG->scols;
+	int seg_r = row / SEG->srows;
+	int seg_c = col / SEG->scols;
 
-        *n = seg_r * SEG->spr + seg_c;
-        *index = (row - seg_r * SEG->srows) * SEG->scols + col - seg_c * SEG->scols;
+	*n = seg_r * SEG->spr + seg_c;
+	*index =
+	    (row - seg_r * SEG->srows) * SEG->scols + col -
+	    seg_c * SEG->scols;
     }
     /* for simple arrays */
     else {
-        *n = col / SEG->scols;
-        *index = col - *n * SEG->scols;
+	*n = col / SEG->scols;
+	*index = col - *n * SEG->scols;
     }
     *index *= SEG->len;
 
@@ -56,8 +62,7 @@
 }
 
 static int (*segment_adrs[2]) () = {
-    segment_address_fast, segment_address_slow
-};
+segment_address_fast, segment_address_slow};
 
 /**
  * \fn int segment_address (SEGMENT *SEG, int row, int col, int *n, int *index)
@@ -75,12 +80,12 @@
 int segment_address(const SEGMENT * SEG, int row, int col, int *n, int *index)
 {
     /* old code
-    *n = row / SEG->srows * SEG->spr + col / SEG->scols;
-    *index = (row % SEG->srows * SEG->scols + col % SEG->scols) * SEG->len;
-    */
+     *n = row / SEG->srows * SEG->spr + col / SEG->scols;
+     *index = (row % SEG->srows * SEG->scols + col % SEG->scols) * SEG->len;
+     */
 
     /* this function is called at least once every time data are accessed in SEG
      * avoid very slow modulus and divisions, modulus was the main time killer */
 
-    return (*segment_adrs[SEG->slow_adrs])(SEG, row, col, n, index);
+    return (*segment_adrs[SEG->slow_adrs]) (SEG, row, col, n, index);
 }

Modified: grass/trunk/lib/segment/pagein.c
===================================================================
--- grass/trunk/lib/segment/pagein.c	2009-10-30 12:17:45 UTC (rev 39647)
+++ grass/trunk/lib/segment/pagein.c	2009-10-30 13:25:01 UTC (rev 39648)
@@ -43,7 +43,7 @@
     /* is n the current segment? */
     if (n == SEG->scb[SEG->cur].n)
 	return SEG->cur;
-	
+
     /* search the in memory segments */
     seg_search.i = 0;
     seg_search.n = n;
@@ -63,34 +63,37 @@
 	    /* make it youngest */
 	    SEG->youngest = SEG->scb[cur].age;
 	}
-	
+
 	return SEG->cur = cur;
     }
-    
+
     /* find a slot to use to hold segment */
-    if (SEG->nfreeslots) {  /* any free slots left ? */
-	cur = SEG->freeslot[--SEG->nfreeslots];
-    }
-    else {	/* find oldest segment */
+    if (!SEG->nfreeslots) {
+	/* use oldest segment */
 	SEG->oldest = SEG->oldest->younger;
 	cur = SEG->oldest->cur;
 	SEG->oldest->cur = -1;
-	SEG->scb[cur].age = NULL;
-    }
 
-    /* if slot is used, write it out, if dirty */
-    if (SEG->scb[cur].n >= 0 && SEG->scb[cur].dirty) {
-	if (segment_pageout(SEG, cur) < 0)
-	    return -1;
+	/* unload segment (remove from search tree) */
+	if (SEG->scb[cur].n >= 0) {
+	    seg_search.n = SEG->scb[cur].n;
+	    if (rbtree_remove(SEG->loaded, &seg_search) == 0)
+		G_fatal_error("could not remove segment");
+	    seg_search.n = n;
+
+	    /* write it out if dirty */
+	    if (SEG->scb[cur].dirty) {
+		if (segment_pageout(SEG, cur) < 0)
+		    return -1;
+	    }
+	}
     }
-	
-    if (SEG->scb[cur].n >= 0) {
-	seg_search.n = SEG->scb[cur].n;
-	if (rbtree_remove(SEG->loaded, &seg_search) == 0)
-	    G_fatal_error("could not remove segment");
-	seg_search.n = n;
+    else {
+	/* free slots left */
+	cur = SEG->freeslot[--SEG->nfreeslots];
     }
 
+
     /* read in the segment */
     SEG->scb[cur].n = n;
     SEG->scb[cur].dirty = 0;
@@ -113,9 +116,6 @@
 	return -1;
     }
 
-    if (cur < 0 || n < 0)
-	G_fatal_error("segment not loaded");
-
     /* remember loaded segment */
     seg_search.i = cur;
     if (rbtree_insert(SEG->loaded, &seg_search) == 0)
@@ -125,6 +125,6 @@
     SEG->youngest = SEG->youngest->younger;
     SEG->scb[cur].age = SEG->youngest;
     SEG->youngest->cur = cur;
-    
+
     return SEG->cur = cur;
 }

Modified: grass/trunk/lib/segment/put.c
===================================================================
--- grass/trunk/lib/segment/put.c	2009-10-30 12:17:45 UTC (rev 39647)
+++ grass/trunk/lib/segment/put.c	2009-10-30 13:25:01 UTC (rev 39648)
@@ -47,8 +47,8 @@
 
     segment_address(SEG, row, col, &n, &index);
     if ((i = segment_pagein(SEG, n)) < 0) {
-        G_warning("segment lib: put: pagein failed");
-        return -1;
+	G_warning("segment lib: put: pagein failed");
+	return -1;
     }
 
     SEG->scb[i].dirty = 1;

Modified: grass/trunk/lib/segment/rbtree.c
===================================================================
--- grass/trunk/lib/segment/rbtree.c	2009-10-30 12:17:45 UTC (rev 39647)
+++ grass/trunk/lib/segment/rbtree.c	2009-10-30 13:25:01 UTC (rev 39648)
@@ -47,7 +47,7 @@
 /* create new tree and initialize
  * returns pointer to new tree, NULL for memory allocation error
  */
-struct RB_TREE *rbtree_create(rb_compare_fn *compare, size_t rb_datasize)
+struct RB_TREE *rbtree_create(rb_compare_fn * compare, size_t rb_datasize)
 {
     struct RB_TREE *tree = G_malloc(sizeof(*tree));
 
@@ -64,7 +64,7 @@
     tree->root = NULL;
 
     return tree;
-} 
+}
 
 /* add an item to a tree
  * non-recursive top-down insertion
@@ -74,7 +74,7 @@
 int rbtree_insert(struct RB_TREE *tree, void *data)
 {
     assert(tree && data);
-    
+
     if (tree->root == NULL) {
 	/* create a new root node for tree */
 	tree->root = rbtree_make_node(tree->datasize, data);
@@ -82,10 +82,12 @@
 	    return 0;
     }
     else {
-	struct RB_NODE head = {0}; /* False tree root */
+	struct RB_NODE head = { 0 };	/* False tree root */
 
-	struct RB_NODE *g, *t;     /* Grandparent & parent */
-	struct RB_NODE *p, *q;     /* Iterator & parent */
+	struct RB_NODE *g, *t;	/* Grandparent & parent */
+
+	struct RB_NODE *p, *q;	/* Iterator & parent */
+
 	int dir = 0, last = 0;
 
 	/* Set up helpers */
@@ -94,7 +96,7 @@
 	q = t->link[1] = tree->root;
 
 	/* Search down the tree */
-	for ( ; ; ) {
+	for (;;) {
 	    if (q == NULL) {
 		/* Insert new node at the bottom */
 		p->link[dir] = q = rbtree_make_node(tree->datasize, data);
@@ -154,15 +156,15 @@
  */
 int rbtree_remove(struct RB_TREE *tree, const void *data)
 {
-    struct RB_NODE head = {0}; /* False tree root */
-    struct RB_NODE *q, *p, *g; /* Helpers */
-    struct RB_NODE *f = NULL;  /* Found item */
+    struct RB_NODE head = { 0 };	/* False tree root */
+    struct RB_NODE *q, *p, *g;	/* Helpers */
+    struct RB_NODE *f = NULL;	/* Found item */
     int dir = 1, removed = 0;
 
     assert(tree && data);
 
     if (tree->root == NULL) {
-	return 0; /* empty tree, nothing to remove */
+	return 0;		/* empty tree, nothing to remove */
     }
 
     /* Set up helpers */
@@ -193,8 +195,7 @@
 		struct RB_NODE *s = p->link[!last];
 
 		if (s != NULL) {
-		    if (!is_red(s->link[!last]) &&
-		        !is_red(s->link[last])) {
+		    if (!is_red(s->link[!last]) && !is_red(s->link[last])) {
 			/* Color flip */
 			p->red = 0;
 			s->red = 1;
@@ -232,7 +233,7 @@
 
     /* Update root and make it black */
     tree->root = head.link[1];
-    if ( tree->root != NULL)
+    if (tree->root != NULL)
 	tree->root->red = 0;
 
     return removed;
@@ -251,7 +252,7 @@
     while (curr_node != NULL) {
 	cmp = tree->rb_compare(curr_node->data, data);
 	if (cmp == 0)
-	    return curr_node->data;   /* found */
+	    return curr_node->data;	/* found */
 	else {
 	    curr_node = curr_node->link[cmp < 0];
 	}
@@ -283,7 +284,7 @@
 void *rbtree_traverse(struct RB_TRAV *trav)
 {
     assert(trav);
-    
+
     if (trav->curr_node == NULL) {
 	if (trav->first)
 	    G_debug(1, "RB tree: empty tree");
@@ -292,7 +293,7 @@
 
 	return NULL;
     }
-	
+
     if (!trav->first)
 	return rbtree_next(trav);
     else {
@@ -322,7 +323,7 @@
 
 	return NULL;
     }
-	
+
     if (!trav->first)
 	return rbtree_next(trav);
 
@@ -343,13 +344,13 @@
 	     * largest item is smaller than search template */
 	    if (trav->curr_node->link[dir] == NULL)
 		return trav->curr_node->data;
-		
+
 	    trav->up[trav->top++] = trav->curr_node;
 	    trav->curr_node = trav->curr_node->link[dir];
 	}
     }
 
-    return NULL; /* should not happen */
+    return NULL;		/* should not happen */
 }
 
 /* two functions needed to fully traverse the tree: initialize and continue
@@ -371,7 +372,7 @@
 	trav->curr_node = trav->curr_node->link[0];
     }
 
-    return trav->curr_node->data; /* return smallest item */
+    return trav->curr_node->data;	/* return smallest item */
 }
 
 /* continue traversing the tree in ascending order
@@ -393,6 +394,7 @@
     else {
 	/* at smallest item in this branch, go back up */
 	struct RB_NODE *last;
+
 	do {
 	    if (trav->top == 0) {
 		trav->curr_node = NULL;
@@ -406,12 +408,13 @@
     if (trav->curr_node != NULL) {
 	return trav->curr_node->data;
     }
-    else	
-	return NULL; /* finished traversing */
+    else
+	return NULL;		/* finished traversing */
 }
 
 /* destroy the tree */
-void rbtree_destroy(struct RB_TREE *tree) {
+void rbtree_destroy(struct RB_TREE *tree)
+{
     rbtree_destroy2(tree->root);
     G_free(tree);
 }
@@ -430,7 +433,7 @@
 int rbtree_debug(struct RB_TREE *tree, struct RB_NODE *root)
 {
     int lh, rh;
- 
+
     if (root == NULL)
 	return 1;
     else {
@@ -452,7 +455,7 @@
 	if (ln) {
 	    lcmp = tree->rb_compare(ln->data, root->data);
 	}
-	
+
 	if (rn) {
 	    rcmp = tree->rb_compare(rn->data, root->data);
 	}
@@ -460,8 +463,8 @@
 	/* Invalid binary search tree:
 	 * left node >= parent or right node <= parent */
 	if ((ln != NULL && lcmp > -1)
-	 || (rn != NULL && rcmp < 1)) {
-	    G_warning("Red Black Tree debugging: Binary tree violation" );
+	    || (rn != NULL && rcmp < 1)) {
+	    G_warning("Red Black Tree debugging: Binary tree violation");
 	    return 0;
 	}
 
@@ -496,9 +499,9 @@
     new_node->data = G_malloc(datasize);
     if (new_node->data == NULL)
 	G_fatal_error("RB Search Tree: Out of memory!");
-	
+
     memcpy(new_node->data, data, datasize);
-    new_node->red = 1;            /* 1 is red, 0 is black */
+    new_node->red = 1;		/* 1 is red, 0 is black */
     new_node->link[0] = NULL;
     new_node->link[1] = NULL;
 
@@ -527,7 +530,7 @@
 
     return newroot;
 }
- 
+
 /* double rotation */
 struct RB_NODE *rbtree_double(struct RB_NODE *root, int dir)
 {

Modified: grass/trunk/lib/segment/release.c
===================================================================
--- grass/trunk/lib/segment/release.c	2009-10-30 12:17:45 UTC (rev 39647)
+++ grass/trunk/lib/segment/release.c	2009-10-30 13:25:01 UTC (rev 39648)
@@ -14,6 +14,7 @@
 
 #include <stdlib.h>
 #include <grass/segment.h>
+#include "rbtree.h"
 
 
 /**
@@ -43,6 +44,10 @@
 	free(SEG->scb[i].buf);
     free(SEG->scb);
 
+    free(SEG->freeslot);
+    free(SEG->agequeue);
+    rbtree_destroy(SEG->loaded);
+
     SEG->open = 0;
 
     return 1;

Modified: grass/trunk/lib/segment/seek.c
===================================================================
--- grass/trunk/lib/segment/seek.c	2009-10-30 12:17:45 UTC (rev 39647)
+++ grass/trunk/lib/segment/seek.c	2009-10-30 13:25:01 UTC (rev 39648)
@@ -61,8 +61,7 @@
 }
 
 static int (*segment_seek_mode[2]) () = {
-    segment_seek_fast, segment_seek_slow
-};
+segment_seek_fast, segment_seek_slow};
 
 int segment_seek(const SEGMENT * SEG, int n, int index)
 {

Modified: grass/trunk/lib/segment/setup.c
===================================================================
--- grass/trunk/lib/segment/setup.c	2009-10-30 12:17:45 UTC (rev 39647)
+++ grass/trunk/lib/segment/setup.c	2009-10-30 13:25:01 UTC (rev 39648)
@@ -17,7 +17,6 @@
 #include <math.h>
 #include <grass/gis.h>
 #include <grass/segment.h>
-
 #include "rbtree.h"
 
 
@@ -81,7 +80,8 @@
     /* adjust number of open segments if larger than number of total segments */
     if (SEG->nseg > SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows)) {
 	G_warning("segment: reducing number of open segments from %d to %d",
-		   SEG->nseg, SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows));
+		  SEG->nseg,
+		  SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows));
 	SEG->nseg = SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows);
     }
 
@@ -93,12 +93,13 @@
     if ((SEG->freeslot = (int *)G_malloc(SEG->nseg * sizeof(int))) == NULL)
 	return -2;
 
-    if ((SEG->agequeue = (struct aq *)G_malloc((SEG->nseg + 1) * sizeof(struct aq))) == NULL)
+    if ((SEG->agequeue =
+	 (struct aq *)G_malloc((SEG->nseg + 1) * sizeof(struct aq))) == NULL)
 	return -2;
 
     SEG->srowscols = SEG->srows * SEG->scols;
     SEG->size = SEG->srowscols * SEG->len;
-    
+
     for (i = 0; i < SEG->nseg; i++) {
 	if ((SEG->scb[i].buf = G_malloc(SEG->size)) == NULL)
 	    return -2;
@@ -117,16 +118,16 @@
 	    SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
 	}
     }
-    
+
     SEG->agequeue[SEG->nseg].cur = -1;
     SEG->agequeue[SEG->nseg].younger = &(SEG->agequeue[SEG->nseg - 1]);
     SEG->agequeue[SEG->nseg].older = &(SEG->agequeue[0]);
     SEG->youngest = SEG->oldest = &(SEG->agequeue[SEG->nseg]);
-    
+
     SEG->nfreeslots = SEG->nseg;
     SEG->cur = 0;
     SEG->open = 1;
-    
+
     SEG->loaded = rbtree_create(segment_compare, sizeof(SEGID));
 
     return 1;
@@ -135,14 +136,14 @@
 int segment_compare(const void *sega, const void *segb)
 {
     SEGID *a, *b;
-    
-    a = (SEGID *)sega;
-    b = (SEGID *)segb;
-    
+
+    a = (SEGID *) sega;
+    b = (SEGID *) segb;
+
     if (a->n > b->n)
 	return 1;
     else if (a->n < b->n)
 	return -1;
-    
+
     return 0;
 }



More information about the grass-commit mailing list