[Kstars-devel] branches/kstars/summer/kdeedu/kstars/kstars/skycomponents

Akarsh Simha akarshsimha at gmail.com
Sun Jun 8 21:32:40 CEST 2008


SVN commit 818557 by asimha:

+ General code quality improvements. Thanks to James for letting me
know of some of these coding conventions.

+ StarBlockCache::deleteBlocks() and StarBlockCache::useBlock() must
also free the memory alloted for StarBlock::star

CCMAIL: kstars-devel at kde.org



 M  +70 -59    starblockcache.cpp  
 M  +1 -1      starblockcache.h  


--- branches/kstars/summer/kdeedu/kstars/kstars/skycomponents/starblockcache.cpp #818556:818557
@@ -22,45 +22,45 @@
     first = NULL;
     last = NULL;
     nstarblocks = 0;
-    useID = -1;
+    useID = 0;
 }
 
-StarBlockCache::StarBlockCache(int nblocks) {
+StarBlockCache::StarBlockCache( int nblocks ) {
     first = NULL;
     last = NULL;
     nstarblocks = 0;
-    useID = -1;
-    addNewBlocks(nblocks);
+    useID = 0;
+    addNewBlocks( nblocks );
 }
 
 StarBlockCache::~StarBlockCache() {
-    deleteBlocks(nstarblocks);
+    deleteBlocks( nstarblocks );
 }
 
-int StarBlockCache::addNewBlocks(int nblocks) {
+int StarBlockCache::addNewBlocks( int nblocks ) {
     int i = 0;
 
-    for(i = 0; i <  nblocks; ++i) {
+    for( i = 0; i <  nblocks; ++i ) {
         if( last == NULL ) {
-            first = last = (StarBlock *) malloc(sizeof(StarBlock));
-            if(first == NULL)
+            first = last = (StarBlock *) malloc( sizeof( StarBlock ) );
+            if( first == NULL )
                 return 0;
-            first -> prev = NULL;
+            first->prev = NULL;
         }
         else {
-            last -> next = (StarBlock *) malloc(sizeof(StarBlock));
-            if(last -> next == NULL)
+            last->next = (StarBlock *) malloc( sizeof( StarBlock ) );
+            if( last->next == NULL )
                 return i;
-            last -> next -> prev = last;
-            last = last -> next;
+            last->next->prev = last;
+            last = last->next;
         }
 
-        last -> next = NULL;
-        last -> useID = -1;
-        last -> star = NULL;
-        last -> parent = NULL;
-        last -> nstars = 0;
-        last -> brightMag = last -> faintMag = -5.0;
+        last->next = NULL;
+        last->useID = 0;
+        last->star = NULL;
+        last->parent = NULL;
+        last->nstars = 0;
+        last->brightMag = last->faintMag = -5.0;
     }
 
     nstarblocks += i;
@@ -69,17 +69,21 @@
 
 StarBlock *StarBlockCache::getBlock() {
 
-    if(last -> useID == -1 || last -> useID != useID) {
-        if(last -> parent) {
-            last -> parent -> removeAll(last);
-            last -> parent = NULL;
+    if( last->useID == 0 || last->useID != useID ) {
+        if( last->parent ) {
+            last->parent->removeAll( last );
+            last->parent = NULL;
         }
-        useBlock(last);
+        if( last->star ) {
+            free( star );
+            last->star = NULL;
+        }
+        useBlock( last );
         return first;
     }
     else
-        if(addNewBlocks(1)) {
-            useBlock(last);
+        if( addNewBlocks( 1 ) ) {
+            useBlock( last );
             return first;
         }
         else
@@ -87,87 +91,94 @@
 
 }
 
-bool StarBlockCache::useBlock(StarBlock *block) {
+bool StarBlockCache::useBlock( StarBlock *block ) {
 
-    if(!block)
+    if( !block )
         return false;
 
-    if(!first)
+    if( !first )
         return false;
 
-    if(!block -> prev) { // Block is already in the front
-        block -> useID = useID;
+    if( !block->prev ) { // Block is already in the front
+        block->useID = useID;
         return true;
     }
 
-    block -> prev -> next = block -> next;
-    if( block -> next )
-        block -> next -> prev = block -> prev;
-    first -> prev = block;
-    block -> next = first;
+    block->prev->next = block->next;
+    if( block->next )
+        block->next->prev = block->prev;
+    first->prev = block;
+    block->next = first;
     first = block;
 
-    block -> useID = useID;
+    block->useID = useID;
 
     return true;
 }
 
 
-bool StarBlockCache::groupMove(StarBlock *start, const int nblocks) {
+bool StarBlockCache::groupMove( StarBlock *start, const int nblocks ) {
 
     StarBlock *end;
 
     // Check for trivial cases
-    if(!start || nblocks < 0)
+    if( !start || nblocks < 0 )
         return false;
 
-    if(nblocks == 0)
+    if( nblocks == 0 )
         return true;
 
-    if(!first)
+    if( !first )
         return false;
 
     // Check for premature end
     end = start;
-    for(int i = 1; i < nblocks; ++i, end = end -> next) {
-        if(end  == NULL)
+    for( int i = 1; i < nblocks; ++i ) {
+        if( end  == NULL )
             return false;
+        end = end->next;
     }
-    if(end == NULL)
+    if( end == NULL )
         return false;
 
     // Update useIDs
     end = start;
-    for(int i = 1; i < nblocks; ++i, end = end -> next)
-        end -> useID = useID;
-    end -> useID = useID;
+    for( int i = 1; i < nblocks; ++i ) {
+        end->useID = useID;
+        end = end->next;
+    }
+    end->useID = useID;
 
     // Check if we are already in the front
-    if(!start -> prev)
+    if( !start->prev )
         return true;
 
-    start -> prev -> next = end -> next;
-    end -> next -> prev = start -> prev;
+    start->prev->next = end->next;
+    end->next->prev = start->prev;
 
-    first -> prev = end;
-    end -> next = first;
-    start -> prev = NULL;
+    first->prev = end;
+    end->next = first;
+    start->prev = NULL;
     first = start;
 }
 
-int StarBlockCache::deleteBlocks(int nblocks) {
+int StarBlockCache::deleteBlocks( int nblocks ) {
     int i;
     StarBlock *temp;
 
     i = 0;
-    while(last != NULL || i == nblocks) {
-        temp = last -> prev;
+    while( last != NULL || i == nblocks ) {
+        temp = last->prev;
+        if( last->star ) {
+            free( last->star );
+            last->star = NULL;
+        }
         free(last);
         last = temp;
         i++;
     }   
-    if(last)
-        last -> next = NULL;
+    if( last )
+        last->next = NULL;
     else
         first = NULL;
 
--- branches/kstars/summer/kdeedu/kstars/kstars/skycomponents/starblockcache.h #818556:818557
@@ -103,7 +103,7 @@
      */
     int deleteBlocks(int nblocks);
 
-    long useID;                // A number identifying the current 'use' cycle
+    quint32 useID;             // A number identifying the current 'use' cycle
 
  private:
 


More information about the Kstars-devel mailing list