src/sparse-array.cpp

00001 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
00002 /*
00003  * frpuniverse
00004  * Copyright (C) Peter L. Berghold 2008 <Peter@Berghold.net>
00005  * 
00006  * frpuniverse is free software.
00007  * 
00008  * You may redistribute it and/or modify it under the terms of the
00009  * GNU General Public License, as published by the Free Software
00010  * Foundation; either version 2 of the License, or (at your option)
00011  * any later version.
00012  * 
00013  * frpuniverse is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with frpuniverse.  If not, write to:
00020  *      The Free Software Foundation, Inc.,
00021  *      51 Franklin Street, Fifth Floor
00022  *      Boston, MA  02110-1301, USA.
00023  */
00024 #ifdef DEBUG_ON
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #endif
00028 #include "sparse-array.hpp"
00029 
00030 sparseArray::sparseArray(){
00031         root = new sparseArrayNode(-1,-1);
00032 }
00033 
00034 
00035 sparseArrayNode* sparseArray::insertNewColumnHead(int c){
00036         sparseArrayNode* p = root;
00037         sparseArrayNode* n = new sparseArrayNode(-1,c);
00038 
00039         if ( root->getRight() == 0 ) {
00040                 root->setRight(n);
00041                 n->setRight(0);
00042                 n->setDown(0);
00043                 return n;
00044         }
00045         while(p->getRight()!=0){
00046                 if ( p->getRight()->getColumn() ==  c ) {
00047                         return p->getRight();
00048                 }
00049                 if ( p->getRight() -> getColumn() > c ) {
00050                         n->setRight(p->getRight());
00051                         p->setRight(n);
00052                         n->setDown(0);
00053                         return n;
00054                 }
00055                 p=p->getRight();
00056         }
00058         p->setRight(n);
00059         n->setRight(0);
00060         n->setDown(0);
00061         return n;
00062 }
00063 
00064                         
00065         
00066 
00067 
00068 //
00069 //  MMmmmm.. fun with pointers....
00070 //
00071 sparseArrayNode* sparseArray::findColumnHead(int c){
00072         sparseArrayNode* p = root;
00073 #ifdef DEBUG_ON
00074         printf("Entering sparseArray::findColumnHead([%d])\n",c);
00075 #endif
00076         while(p->getRight() != (sparseArrayNode*) 0 ) {
00077                 p = p->getRight();
00078 #ifdef DEBUG_ON
00079                 printf("%d ",p->getColumn());
00080 #endif
00081                 if ( p->getColumn() == c ) {
00082 #ifdef DEBUG_ON
00083                         printf("\nReturning from sparseArray::findColumnHead()\n");
00084 #endif
00085                         return p;
00086                 }
00087                 if ( p->getColumn() > c ) {
00088 #ifdef DEBUG_ON
00089                         printf("\nNeed new column header...\n");
00090 #endif
00091                         return this->insertNewColumnHead(c);
00092                 }
00093                 
00094         }
00095         
00096 #ifdef DEBUG_ON
00097         printf("\nFall through of loop... need new column heaader...\n");
00098 #endif
00099         return this->insertNewColumnHead(c);
00100 }
00101 
00102 
00103 void sparseArray::add(sparseArrayNode* p){
00104         int r=p->getRow();
00105         int c=p->getColumn();
00106         
00107         this->addAt(r,c,p);
00108         
00109 }
00110 
00111 void sparseArray::addAt(int row,int column,sparseArrayNode* node){
00112 #ifdef DEBUG_ON
00113         printf("Entering sparseArray::addAt([%d],[%d],<ptr>)\n",row,column);
00114 #endif
00115         
00116         sparseArrayNode* p = this->findColumnHead(column);
00117         node->setRow(row);
00118         node->setColumn(column);
00119         
00120         
00121         if ( p->getDown() == 0 ) {
00122 #ifdef DEBUG_ON
00123                 printf("\tInserting new node just below the column header\n");
00124 #endif
00125                 p->setDown(node);
00126 #ifdef DEBUG_ON
00127                 printf("Returning from sparseArray::addAt()\n");
00128 #endif
00129                 return;
00130         } else { 
00131                 while(p->getDown() != 0 ) {
00132 #ifdef DEBUG_ON
00133                         printf("\tThis is [%d,%d] and below that is [%d,%d] (Tryig to insert [%d,%d]\n",
00134 
00135                                    p->getRow(),p->getColumn(),
00136                                    p->getDown()->getRow(),p->getDown()->getColumn(),
00137                                    row,column
00138                                    );
00139 #endif
00140                         if ( p->getDown()->getRow() >= row ) {
00141 #ifdef DEBUG_ON
00142                                 printf("\t\tInserting here!\n");
00143 #endif
00144                                 node->setDown(p->getDown());
00145                                 p->setDown(p);
00146 #ifdef DEBUG_ON
00147                                 printf("returning.\n");
00148 #endif
00149                                 return;
00150                         }
00151 #ifdef DEBUG_ON
00152                         printf("\tGoing to the next node.\n");
00153 #endif
00154                         p=p->getDown();
00155                 }
00156                 p->setDown(node);
00157                 node->setDown(0);
00158         }
00159         
00160                                 
00161 }
00162 
00163 
00164 sparseArrayNode* sparseArray::findAt(int row,int column){
00165         sparseArrayNode *p = this->findColumnHead (column);
00166         
00167         while(p->getDown() != (sparseArrayNode*)0){
00168                 p = p->getDown();
00169                 if ( p->getRow() == row ) 
00170                         return p;
00171         }
00172         if ( p->getRow() != row )
00173                 return (sparseArrayNode*)0;
00174         else 
00175                 return p;
00176         
00177 }
00178 
00179 #ifdef DEBUG_ON
00180 void sparseArray::dump(){
00181         sparseArrayNode* r;
00182         sparseArrayNode* c;
00183         printf("Rotated dump of the sparse array\n");
00184         c=root;
00185         while(c->getRight()!=0){
00186                 c=c->getRight();
00187                 r=c;
00188                 printf("[%d,%d] ",r->getRow(),r->getColumn());
00189                 while(r->getDown()!=0){
00190                         r=r->getDown();
00191                         printf("[%d,%d] ",r->getRow(),r->getColumn());
00192                 }
00193                 printf ("\n");
00194         }
00195         printf ("\n");
00196 }
00197 
00198 #endif

Generated on Fri Mar 7 16:40:53 2008 for frpuniverse by  doxygen 1.4.7