mvalngbase.cpp

Go to the documentation of this file.
00001 #include "mvalngbase.h"
00002 #include "string.h"
00003 #include "stdlib.h"
00004 
00005 //
00006 //******************************************************************************
00007 //                    MVALNGBASE.CPP
00008 //******************************************************************************
00009 //
00010 //
00011 //********************************************************************************
00012 //
00013 //            Chris Anderson (C) UCLA 1995
00014 //
00015 //
00016 //********************************************************************************
00017 //
00018 //
00019 //********************************************************************************
00020 //                    CONSTRUCTORS
00021 //********************************************************************************
00022 //
00023 
00024 #define longNullValue 0
00025 
00026 MVAlongBase::MVAlongBase()
00027 {
00028      dataPointer = newLong(0);
00029      size        = 0;
00030 }
00031 
00032 MVAlongBase::MVAlongBase( const MVAlongBase& A)
00033 {
00034     if(A.size == 0)
00035     {dataPointer = newLong(0); size = 0;}
00036     else
00037     {
00038     dataPointer = newLong(A.size);
00039     size        = A.size;
00040     long* Aptr;
00041     long* ptr;
00042     for(Aptr = A.dataPointer, ptr = dataPointer;
00043         Aptr < A.dataPointer + A.size; Aptr++, ptr++)
00044         *ptr = *Aptr;
00045     }
00046 } 
00047 
00048 
00049 MVAlongBase::MVAlongBase(long inSize)
00050 {
00051     dataPointer = newLong(inSize);
00052     size        = inSize;
00053 }
00054 MVAlongBase::MVAlongBase(long* data, long inSize)
00055 {
00056     dataPointer = newLong(inSize);
00057     size        = inSize;
00058     long* ptr;
00059     long* dptr;
00060     for(ptr = dataPointer, dptr = data;dptr < data + inSize;
00061         ptr++, dptr++)
00062         *ptr = *dptr;
00063 }
00064 //
00065 //********************************************************************************
00066 //                    DESTRUCTOR
00067 //********************************************************************************
00068 //
00069 MVAlongBase::~MVAlongBase()
00070 {
00071      deleteLong();
00072      size  = 0;
00073 }
00074 
00075 //
00076 //********************************************************************************
00077 //                 INPUT/OUTPUT
00078 //********************************************************************************
00079 //
00080 ostream&  operator <<(ostream& out_stream, const MVAlongBase& A)
00081 {
00082     for(long i = 0; i < A.size; i++)
00083     out_stream  << A.dataPointer[i] << '\n';
00084     return(out_stream);
00085 }
00086 
00087 istream&  operator >>(istream& in, MVAlongBase& A)
00088 {
00089    MVAlongBase::inputLong(in, A);
00090    return(in);
00091 }
00092 //
00093 //********************************************************************************
00094 //                    ASSIGNMENT
00095 //********************************************************************************
00096 //
00097 MVAlongBase&  MVAlongBase::operator =( const MVAlongBase& A)
00098 {
00099     if(dataPointer == 0)
00100     { dataPointer = newLong(A.size); size  = A.size;}
00101 //
00102     if(size != A.size)
00103     {MVAlongBase::sizeError(size, A.size);}
00104 //  
00105     long* Aptr; long* ptr;
00106     for(Aptr = A.dataPointer, ptr = dataPointer;
00107         Aptr < A.dataPointer + A.size; Aptr++, ptr++)
00108         *ptr = *Aptr;
00109     return *this;
00110 }
00111 //
00112 //********************************************************************************
00113 //                    MEMBER_FUNCTIONS
00114 //********************************************************************************
00115 //
00116 //
00117 //********************************************************************************
00118 //      Initalize Functions
00119 //********************************************************************************
00120 //
00121 void MVAlongBase::initialize()
00122 {
00123     deleteLong();
00124     dataPointer = newLong(0);
00125     size        = 0;
00126 }
00127 
00128 void MVAlongBase::initialize( const MVAlongBase& A)
00129 {
00130     deleteLong();
00131     if(A.size == 0)
00132     {
00133      dataPointer = newLong(0);
00134      size        = 0;
00135     }
00136     else
00137     {
00138     dataPointer = newLong(A.size);
00139     size        = A.size;
00140     long* Aptr;
00141     long* ptr;
00142     for(Aptr = A.dataPointer, ptr = dataPointer;
00143         Aptr < A.dataPointer + A.size; Aptr++, ptr++)
00144         *ptr = *Aptr;
00145     }
00146 }
00147 
00148 void  MVAlongBase::initialize(long inSize)
00149 {
00150     deleteLong();
00151     dataPointer = newLong(inSize);
00152     size        = inSize;
00153 }
00154 
00155 void  MVAlongBase::initialize(long* data, long inSize)
00156 {
00157     deleteLong();
00158     dataPointer = newLong(inSize);
00159     size        = inSize;
00160     long* ptr;
00161     long* dptr;
00162     for(ptr = dataPointer, dptr = data;dptr < data + inSize;
00163         ptr++, dptr++)
00164         *ptr = *dptr;
00165 }
00166 
00167 void  MVAlongBase::copyToArray(long* A) const
00168 {
00169     long* ptr;
00170     long* Aptr;
00171     for(ptr = dataPointer,Aptr = A; ptr < dataPointer + size;
00172         ptr++, Aptr++)
00173     *Aptr = *ptr;
00174 }
00175 //
00176 //********************************************************************************
00177 //      Resizing
00178 //********************************************************************************
00179 //
00180 void  MVAlongBase::resizeTo(long n)
00181 {
00182     MVAlongBase Temp;
00183     if( n > 0)
00184     {
00185         Temp.initialize(*this);
00186         this->initialize(n);
00187 
00188         long* Tptr;
00189         long* ptr;
00190 
00191         if(Temp.getSize() < n)
00192         {
00193         for(ptr  = dataPointer, Tptr = Temp.getDataPointer();
00194             Tptr < Temp.getDataPointer() + Temp.getSize();
00195                            Tptr++, ptr++)
00196         *ptr = *Tptr;
00197         }
00198         else
00199         {
00200         for(ptr  = dataPointer, Tptr = Temp.getDataPointer();
00201             ptr <  dataPointer + n;
00202                            Tptr++, ptr++)
00203         *ptr = *Tptr;
00204         }
00205     }
00206     else if(n == 0)
00207     {
00208     deleteLong();
00209     dataPointer = newLong(0);
00210     size        = 0;
00211     }
00212     else
00213     {MVAlongBase::reSizeError(n);}
00214 };
00215 //
00216 //********************************************************************************
00217 //      Logical Equality and Inequality
00218 //********************************************************************************
00219 //
00220 int  MVAlongBase::operator == (const MVAlongBase& A) const
00221 {
00222     if(size != A.size) return 0;
00223     if(size == 0     ) return 1; 
00224  
00225     for(long i = 0; i < size ; i++)
00226     if(dataPointer[i] != A.dataPointer[i]) return 0;
00227 
00228     return 1;
00229 }
00230 
00231 int  MVAlongBase::operator != (const MVAlongBase& A) const
00232 {
00233     if(size != A.size) return 1;
00234     if(size == 0     ) return 0; 
00235  
00236     for(long i = 0; i < size ; i++)
00237     if(dataPointer[i] != A.dataPointer[i]) return 1; 
00238     
00239     return 0;
00240 }
00241 //
00242 //********************************************************************************
00243 //                     Input Utility
00244 //********************************************************************************
00245 //
00246 void MVAlongBase::inputLong(istream& in, MVAlongBase& A)
00247 {
00248 //
00249 //  This routine extracts the N values of the
00250 //  "next" sequence of long values in the input stream
00251 //  copies them into the MVAlongBase array A.
00252 //
00253 //  By "next" sequence we mean the set of numbers contained
00254 //  in the stream which are separated by spaces or new-line
00255 //  characters. Any initial character data in the stream is
00256 //  skipped and the end of the sequence is triggered by
00257 //  encountering character data or end of file (EOF).
00258 //
00259 //  If A has a prescribed non-zero size then this routine
00260 //  places the N values into the first N locations of A;
00261 //  the remainder of A's values are untouched.
00262 //
00263 //  If the size of A is less than N an error message is generated.
00264 //
00265 //  If A is of zero size (a null) array, then this routine
00266 //  resizes A to N and copies the data into A.
00267 //
00268 //
00269     if(in.eof() != 0) return;
00270     long i;
00271     char in_char;
00272     char in_token[128];
00273 
00274     int   long_hit; int   char_hit;
00275     long  long_count;
00276 
00277     long file_position;
00278 
00279     file_position = in.tellg();
00280     long_count   = 0;
00281 
00282     in_char = in.get();
00283     while((in.eof() == 0)&&((in_char == '\n')||(in_char == ' ')))
00284     {in_char = in.get();}
00285 
00286     long_hit = 0;
00287     char_hit  = 0;
00288 
00289     while((in.eof() == 0)&&(char_hit == 0))
00290     {
00291     //
00292     //  Digit Processing
00293     //
00294     if((48 <= int(in_char)&&(int(in_char) <= 57)))
00295     {
00296       long_hit = 1;
00297       while((in.eof() == 0)&&((in_char != '\n')&& (in_char != ' ')))
00298       {in_char = in.get();}
00299       long_count++;
00300     }
00301     else
00302     //
00303     // Character Processing
00304     //
00305     {
00306       if(long_hit == 1) {in.putback(in_char); char_hit = 1;}
00307       while((char_hit == 0)&&(in.eof() == 0)&&((in_char != '\n') && (in_char != ' ')))
00308       {
00309       if(in_char == '\\') { in_char = in.get();}
00310       in_char = in.get();
00311       }
00312     }
00313 
00314     while((in.eof() == 0)&&((in_char == '\n')||(in_char ==  ' ')))
00315     {in_char = in.get();}
00316 
00317     }
00318 //
00319 //  Read in the data
00320 //
00321     if(A.getSize() == 0)
00322     {A.resizeTo(long_count);}
00323     else
00324     {
00325     if(A.getSize() < long_count)
00326     {MVAlongBase::inputError( A.getSize(), long_count );}
00327     }
00328 
00329     in.clear();
00330     in.seekg(file_position);
00331 
00332     in_char = in.get();
00333     while((in.eof() == 0)&&((in_char == '\n')||(in_char == ' ')))
00334     {in_char = in.get();}
00335 
00336     long_hit   = 0;
00337     long_count = 0;
00338     while(in.eof() == 0)
00339     {
00340     //
00341     //  Digit Processing
00342     //
00343     if((48 <= int(in_char)&&(int(in_char) <= 57)))
00344     {
00345       long_hit = 1;
00346       i         = 0;
00347       while((in.eof() == 0)&&((in_char != '\n')
00348                             && (in_char != ' ')))
00349       {in_token[i] = in_char; in_char = in.get(); i++;}
00350       in_token[i] = '\0';
00351       if(strchr(in_token,'.'))
00352       {
00353       A[long_count] = long(strtod(in_token,NULL));
00354       }
00355       else
00356       {
00357       A[long_count] = long(strtol(in_token,NULL,10));
00358       }
00359       long_count++;
00360     }
00361     else
00362     //
00363     // Character Processing
00364     //
00365     {
00366     //
00367     // Return if have a long_hit followed by a character hit
00368     //
00369       if(long_hit == 1) {in.putback(in_char); return;}
00370       while((in.eof() == 0)&&((in_char != '\n') && (in_char != ' ')))
00371       {
00372       if(in_char == '\\') { in_char = in.get();}
00373       in_char = in.get();
00374       }
00375     }
00376 
00377     while((in.eof() == 0)&&((in_char == '\n')||(in_char ==  ' ')))
00378     {in_char = in.get();}
00379 
00380     }
00381 }
00382 //
00383 //********************************************************************************
00384 //                     newLong (memory allocation)
00385 //********************************************************************************
00386 //
00387 long* MVAlongBase::newLong(long newSize)
00388 {
00389      long* dataP;
00390      if(newSize > 0)
00391      {
00392         dataP = new long[newSize];
00393         long* ptr;
00394         for(ptr = dataP; ptr < dataP + newSize;  ptr++) *ptr = longNullValue;
00395         return dataP;
00396      }
00397      else
00398      {dataP = 0; return dataP;}
00399 }
00400 void MVAlongBase::deleteLong()
00401 {
00402      if(dataPointer != 0) delete [] dataPointer;
00403      dataPointer = 0;
00404 }
00405 
00406 //
00407 //********************************************************************************
00408 //                     Error Handling
00409 //********************************************************************************
00410 //
00411 void   MVAlongBase::inputError(long ArraySize, long RequiredSize)
00412 {
00413     cerr << "MVAlongBase Array of Insufficient Size For >> Operation"
00414          << endl  << "Array    Size : " << ArraySize
00415          << endl  << "Required Size : " << RequiredSize << endl;
00416          exit(1);
00417 }
00418 void   MVAlongBase::indexError(long Index)
00419 {
00420     cerr << "Index out of range in MVAlongBase.\n";
00421     cerr << "Offending Index Value = " << Index << endl;
00422     exit(1);
00423 };
00424 void   MVAlongBase::sizeError(long Lsize, long Rsize)
00425 {
00426     cerr << "Error : Assignment of unequal sized MVAlongBase \n";
00427     cerr << "Left Operand : " << Lsize   << endl;
00428     cerr << "Rght Operand : " << Rsize << endl;
00429     exit(1);
00430 };
00431 void   MVAlongBase::reSizeError(long size)
00432 {
00433     cerr << "Error : invalid size for resizing of MVAlongBase \n";
00434     cerr << "Offending Size  : " << size   << endl;
00435     exit(1);
00436 };
00437 //
00438 //********************************************************************************
00439 //                     CPP File End
00440 //********************************************************************************
00441 //
00442 
00443   

Generated on Wed Aug 6 12:58:49 2003 for Open-Sessame Framework by doxygen1.3