src/sparse-vector.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 
00025 #include <malloc.h>
00026 #include <string.h>
00027 #include <stdio.h>
00028 
00029 #include "sparse-vector.hpp"
00030 
00031 
00032 void sparseVector::addAt(int ix,sparseVectorNode* n){
00033         sparseVectorNode* p=root;
00034         
00035         n -> setIndex(ix); // Just to make sure for sanity's sake later. 
00036         
00037 
00038         if ( p->getNext() == (sparseVectorNode*) 0 ) { // Root node points to zero
00039                 p->setNext(n);
00040                 n->setNext((sparseVectorNode* ) 0 );
00041         } else { 
00042                 while ( p->getNext() != (sparseVectorNode*)0){
00043                         p = p->getNext();
00044                         if ( (p->getNext() != (sparseVectorNode*)0) &&
00045                                  (p->getNext()->getIndex() > n->getIndex()) ){
00046                                 n->setNext(p->getNext());
00047                                 p->setNext(n);
00048                                 return;
00049                         }
00050                 }
00051                 
00052                 p->setNext(n);
00053                 n->setNext((sparseVectorNode*)0);
00054                 return;
00055         }
00056         
00057 }
00058 
00065 sparseVectorNode* sparseVector::findAt(int ix){
00066         sparseVectorNode* p = root;
00067         
00068         if ( root->getNext() == (sparseVectorNode*) 0 ) 
00069                 return (sparseVectorNode* ) 0;
00070         while(p->getNext() != (sparseVectorNode*)0){
00071                 p = p->getNext();
00072                 if ( p->getIndex() == ix ) 
00073                         return p;
00074         }
00075         return (p->getIndex() == ix ? p : (sparseVectorNode*) 0 );
00076         
00077 }
00078 
00079 #define XMLSTART "<sparseVector"
00080 #define XMLEND "</sparseVector>"
00081 
00082 static char* addXML(char* s,sparseVectorNode* p){
00083         char *t = p->dumpAsXML();
00084         int  len_s = strlen(s);
00085         int  len_t = strlen(t);
00086         char *u = (char *) malloc(len_s+ len_t+ 2);
00087         u[0]='\0';
00088         strcpy(u,s);
00089         strcat(u,t);
00090         free(s);
00091         s=u;
00092         free(t);
00093         return u;
00094 }
00095 
00096 char* sparseVector::dumpAsXML() {
00097         // Get ready folks.. this will be ugly...
00098         char * s;
00099         sparseVectorNode* p;
00100         
00101         s=(char*) malloc(129);
00102         s[0]='\0';
00103         
00104         sprintf(s,"%s\n",XMLSTART);
00105         if ( root->getNext() == (sparseVectorNode*)0){
00106                 strcat(s,XMLEND);
00107                 return s;
00108         } else {
00109                 p=root->getNext();
00110                 s = addXML(s,p);
00111                 while(p->getNext() != (sparseVectorNode*)0){
00112                         p=p->getNext();
00113                         s=addXML(s,p);
00114                 }
00115         }
00116         strcat(s,XMLEND);
00117         
00118         return s;
00119 }
00120 
00121                 
00122                 
00123                 

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