Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

ID.cpp

Go to the documentation of this file.
00001 /* ****************************************************************** **
00002 **    OpenSees - Open System for Earthquake Engineering Simulation    **
00003 **          Pacific Earthquake Engineering Research Center            **
00004 **                                                                    **
00005 **                                                                    **
00006 ** (C) Copyright 1999, The Regents of the University of California    **
00007 ** All Rights Reserved.                                               **
00008 **                                                                    **
00009 ** Commercial use of this program without express permission of the   **
00010 ** University of California, Berkeley, is strictly prohibited.  See   **
00011 ** file 'COPYRIGHT'  in main directory for information on usage and   **
00012 ** redistribution,  and for a DISCLAIMER OF ALL WARRANTIES.           **
00013 **                                                                    **
00014 ** Developed by:                                                      **
00015 **   Frank McKenna (fmckenna@ce.berkeley.edu)                         **
00016 **   Gregory L. Fenves (fenves@ce.berkeley.edu)                       **
00017 **   Filip C. Filippou (filippou@ce.berkeley.edu)                     **
00018 **                                                                    **
00019 ** ****************************************************************** */
00020                                                                         
00021 // $Revision: 1.1.1.1 $
00022 // $Date: 2000/09/15 08:23:22 $
00023 // $Source: /usr/local/cvs/OpenSees/SRC/matrix/ID.cpp,v $
00024                                                                         
00025                                                                         
00026 // File: ~/matrix/ID.C
00027 //
00028 // Written: fmk 
00029 // Created: 11/96
00030 // Revision: A
00031 //
00032 // Description: This file contains the class implementation for ID.
00033 //
00034 // What: "@(#) ID.C, revA"
00035 
00036 #include "ID.h"
00037 #include <stdlib.h>
00038 
00039 
00040 int ID::ID_NOT_VALID_ENTRY = 0;
00041 
00042 // ID():
00043 // Standard constructor, sets size = 0;
00044 
00045 ID::ID()
00046 :sz(0), data(0), arraySize(0)
00047 {
00048 
00049 }
00050 
00051 
00052 // ID(int size):
00053 // Constructor used to allocate a ID of size size.
00054 
00055 ID::ID(int size)
00056   :sz(size), data(0), arraySize(size)
00057 {
00058 
00059 #ifdef _G3DEBUG
00060   if (sz <= 0) {
00061     g3ErrorHandler->warning("ID::ID(int) - size %d specified <= 0\n",size);
00062     sz = 1;
00063     arraySize = 1;
00064   }
00065 #endif    
00066 
00067   // create the space for the data & check space was available
00068   data = (int *)malloc(size*sizeof(int));
00069   if (data == 0) {
00070     g3ErrorHandler->fatal("ID::ID(int): ran out of memory with size %d\n",
00071      size);
00072     exit(-1);
00073   }
00074 
00075   // zero the data
00076   for (int i=0; i<size; i++)
00077     data[i] = 0;
00078 }
00079 
00080 
00081 // ID(int size):
00082 // Constructor used to allocate a ID of size size.
00083 
00084 ID::ID(int size, int arraySz)
00085   :sz(size), data(0), arraySize(arraySz)
00086 {
00087 #ifdef _G3DEBUG
00088   if (sz < 0) {
00089     g3ErrorHandler->warning("ID::ID(size, arraySize) - size %d specified < 0\n",size);
00090     sz = 0;
00091   }
00092   if (arraySz <= 0) {
00093     g3ErrorHandler->warning("ID::ID(size, arraySize) - arraySize %d specified < 0\n",arraySz);
00094     if (sz != 0) 
00095       arraySz = sz;
00096     else
00097       arraySz = 1;
00098   }
00099   if (arraySz < sz) {
00100     g3ErrorHandler->warning("ID::ID(size, arraySize) - arraySize %d specified <  size\n",
00101        arraySz, size);
00102     arraySz = sz;
00103   }
00104 #endif    
00105 
00106   // create the space
00107   data = (int *)malloc(arraySize*sizeof(int));
00108   if (data == 0) {
00109     g3ErrorHandler->fatal("ID::ID(int, int): ran out of memory with arraySize %d\n",
00110      arraySize);
00111     exit(-1);
00112   }
00113 
00114   // zero the data
00115   for (int i=0; i<arraySize; i++)
00116     data[i] = 0;
00117 }
00118 
00119 
00120 
00121 // ID(const ID&):
00122 // Constructor to init a ID from another.
00123 
00124 ID::ID(const ID &other)
00125 {
00126     sz = other.sz;
00127     arraySize = other.arraySize;
00128     data = 0;
00129 
00130   // create the space
00131   data = (int *)malloc(arraySize*sizeof(int));
00132   if (data == 0) {
00133     g3ErrorHandler->fatal("ID::ID(ID): ran out of memory with arraySize %d\n",
00134      arraySize);
00135     exit(-1);
00136   }
00137   
00138   // copy the data 
00139   for (int i=0; i<sz; i++)
00140     data[i] = other.data[i];
00141 } 
00142 
00143 
00144 
00145 // ~ID():
00146 //  destructor, deletes the [] data
00147 
00148 ID::~ID()
00149 {
00150   if (data != 0) 
00151     free((void *)data);
00152 }
00153 
00154 
00155 void
00156 ID::Zero(void)
00157 {
00158   for (int i=0; i<sz; i++)
00159     data[i] =0;
00160 }
00161 
00162 int
00163 ID::getLocation(int value) const
00164 {
00165   // search through ID for the value
00166   for (int i=0; i<sz; i++)
00167     if (data[i] == value)
00168       return i;
00169 
00170   // if we get here the value is not in the array
00171   return -1;
00172 }
00173 
00174 int
00175 ID::removeValue(int value)
00176 {
00177   int place = -1;
00178   for (int i=0; i<sz; i++)
00179     if (data[i] == value) {
00180       place = i;
00181       // copy the rest of the components down one in ID
00182       for (int j=i; j<sz-1; j++)
00183  data[j] = data[j+1];  
00184       sz--;
00185     }
00186   return place;
00187 }    
00188 
00189 
00190 int &
00191 ID::operator[](int x) 
00192 {
00193 #ifdef _G3DEBUG
00194   // check if it is inside range [0,sz-1]
00195   if (x < 0) {
00196     g3ErrorHandler->warning("ID::[] - location x %d < 0\n",x);
00197     return ID_NOT_VALID_ENTRY;
00198   }
00199 #endif
00200 
00201   // see if quick return
00202   if (x < sz)
00203     return data[x];
00204     
00205   /*
00206    * otherwise we have to enlarge the order of the ID
00207    */
00208     
00209   // see if we can just enlarge the array
00210   // without having to go get more space
00211 
00212   if (x < arraySize) {
00213     for (int i=sz; i<x; i++)
00214       data[i] = 0;
00215     sz = x+1;
00216     return data[x];
00217   }
00218 
00219   // otherwise we go get more space
00220   if (x >= arraySize) {
00221     int newArraySize = arraySize * 2;
00222     if (newArraySize < x) 
00223       newArraySize = x;
00224     int *newData = (int *)malloc(newArraySize*sizeof(int));    
00225     
00226     if (newData != 0) {
00227       // copy the old
00228       for (int i=0; i<sz; i++)
00229  newData[i] = data[i];
00230       // zero the new
00231       for (int j=sz; j<arraySize; j++)
00232  newData[j] = 0;
00233       
00234       sz = x+1;
00235       // release the memory held by the old
00236       free((void *)data);     
00237       data = newData;
00238       arraySize = newArraySize;
00239       
00240       return newData[x];
00241     }
00242     else {
00243       // we could not allocate more mem .. leave the current size
00244       g3ErrorHandler->warning("ID::[]): ran out of memory with arraySize %d\n",
00245          arraySize);
00246       return ID_NOT_VALID_ENTRY;
00247     }
00248   }
00249   
00250   // we should never get here, but some compilers need this line
00251   return ID_NOT_VALID_ENTRY; 
00252 }
00253     
00254 
00255 
00256 // ID &operator=(const ID  &V):
00257 // the assignment operator, This is assigned to be a copy of V. if sizes
00258 // are not compatable this.data [] is deleted. The data pointers will not
00259 // point to the same area in mem after the assignment.
00260 //
00261 
00262 ID &
00263 ID::operator=(const ID &V) 
00264 {
00265     // first check we are not trying v = v
00266     if (this != &V) {
00267  
00268  // check size compatability, if different delete
00269  // old and make room for new.
00270  if (sz != V.sz) {
00271      if (arraySize < V.sz) {
00272   arraySize = V.sz;
00273   if (data != 0)
00274       free((void *)data);
00275   sz = V.sz;
00276   data = (int *)malloc(arraySize*sizeof(int));  
00277   // check we got the memory requested
00278   if (data == 0) {
00279       cerr << "WARNING ID::=(ID) - ran out of memory ";
00280       cerr << "for new array of size" << arraySize << endl;
00281       sz = 0;
00282       arraySize = 0;
00283   }
00284      }
00285  }
00286  
00287  // copy the data
00288  for (int i=0; i<sz; i++)
00289      data[i] = V(i);
00290     }
00291 
00292     return *this;
00293 }
00294 
00295 
00296 
00297 
00298 
00299 // friend ostream &operator<<(ostream &s, const ID &V)
00300 // A function is defined to allow user to print the IDs using ostreams.
00301 
00302 ostream &operator<<(ostream &s, const ID &V)
00303 {
00304     for (int i=0; i<V.Size(); i++) 
00305     {
00306  s << V(i) << " ";
00307     }
00308     return s << "\n";
00309 }
00310 
00311 // friend istream &operator>>(istream &s, ID &V)
00312 // A function is defined to allow user to input the data into a ID which has already
00313 // been constructed with data, i.e. ID(int) or ID(const ID &) constructors.
00314 
00315 istream &operator>>(istream &s, ID &V)
00316 {
00317     for (int i=0; i<V.Size(); i++) 
00318  s >> V(i);
00319 
00320     return s;
00321 }
00322 
00323 
00324 
00325 
00326 
Copyright Contact Us