00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "ID.h"
00037 #include <stdlib.h>
00038
00039
00040 int ID::ID_NOT_VALID_ENTRY = 0;
00041
00042
00043
00044
00045 ID::ID()
00046 :sz(0), data(0), arraySize(0)
00047 {
00048
00049 }
00050
00051
00052
00053
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
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
00076 for (int i=0; i<size; i++)
00077 data[i] = 0;
00078 }
00079
00080
00081
00082
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
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
00115 for (int i=0; i<arraySize; i++)
00116 data[i] = 0;
00117 }
00118
00119
00120
00121
00122
00123
00124 ID::ID(const ID &other)
00125 {
00126 sz = other.sz;
00127 arraySize = other.arraySize;
00128 data = 0;
00129
00130
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
00139 for (int i=0; i<sz; i++)
00140 data[i] = other.data[i];
00141 }
00142
00143
00144
00145
00146
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
00166 for (int i=0; i<sz; i++)
00167 if (data[i] == value)
00168 return i;
00169
00170
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
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
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
00202 if (x < sz)
00203 return data[x];
00204
00205
00206
00207
00208
00209
00210
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
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
00228 for (int i=0; i<sz; i++)
00229 newData[i] = data[i];
00230
00231 for (int j=sz; j<arraySize; j++)
00232 newData[j] = 0;
00233
00234 sz = x+1;
00235
00236 free((void *)data);
00237 data = newData;
00238 arraySize = newArraySize;
00239
00240 return newData[x];
00241 }
00242 else {
00243
00244 g3ErrorHandler->warning("ID::[]): ran out of memory with arraySize %d\n",
00245 arraySize);
00246 return ID_NOT_VALID_ENTRY;
00247 }
00248 }
00249
00250
00251 return ID_NOT_VALID_ENTRY;
00252 }
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 ID &
00263 ID::operator=(const ID &V)
00264 {
00265
00266 if (this != &V) {
00267
00268
00269
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
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
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
00300
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
00312
00313
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