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
00037 #include <TaggedObject.h>
00038 #include <ArrayOfTaggedObjects.h>
00039
00040 #include <OPS_Globals.h>
00041
00042 ArrayOfTaggedObjects::ArrayOfTaggedObjects(int size)
00043 :numComponents(0),sizeComponentArray(0), positionLastEntry(0),
00044 positionLastNoFitEntry(0),fitFlag(true), theComponents(0),
00045 myIter(*this)
00046 {
00047
00048 theComponents = new TaggedObject *[size];
00049
00050
00051 if (theComponents == 0) {
00052 opserr << "ArrayOfTaggedObjects::ArrayOfTaggedObjects - failed to allocate an array of size " << size << endln;
00053 } else
00054 sizeComponentArray = size;
00055
00056
00057 for (int i=0; i<sizeComponentArray; i++)
00058 theComponents[i] = 0;
00059 }
00060
00061
00062 ArrayOfTaggedObjects::~ArrayOfTaggedObjects()
00063 {
00064 if (theComponents != 0)
00065 delete [] theComponents;
00066 }
00067
00068
00069
00070 int
00071 ArrayOfTaggedObjects::setSize(int newSize)
00072 {
00073
00074 if (newSize < 0 && newSize > sizeComponentArray) {
00075 opserr << "ArrayOfTaggedObjects::setSize - invalid size " << newSize << endln;
00076 return -1;
00077 }
00078
00079 if (newSize < 2) newSize = 2;
00080
00081
00082
00083
00084 TaggedObject **newArray = new TaggedObject *[newSize];
00085
00086 if (newArray == 0) {
00087
00088 opserr << "ArrayOfTaggedObjects::setSize - failed to allocate an array of size " << newSize << endln;
00089 return -2;
00090 }
00091
00092
00093
00094
00095
00096
00097
00098 for (int i=0; i<newSize; i++)
00099 newArray[i] = 0;
00100
00101
00102 TaggedObject **oldArray = theComponents;
00103 int oldArrayLastEntry = positionLastEntry;
00104
00105
00106 theComponents = newArray;
00107 sizeComponentArray = newSize;
00108
00109 int error = 0;
00110 if (fitFlag == true && positionLastEntry <= newSize) {
00111
00112
00113
00114 theComponents = newArray;
00115 sizeComponentArray = newSize;
00116 for (int i=0; i<=positionLastEntry; i++)
00117 theComponents[i] = oldArray[i];
00118 } else {
00119
00120
00121
00122 numComponents = 0;
00123 sizeComponentArray = newSize;
00124 positionLastEntry = 0;
00125 positionLastNoFitEntry = 0;
00126 fitFlag = true;
00127
00128
00129
00130 for (int j=0; j<=oldArrayLastEntry; j++)
00131 if (oldArray[j] != 0)
00132 if (this->addComponent(oldArray[j]) == false) {
00133
00134 opserr << "SERIOUS ERROR: ArrayOfTaggedObjects::setSize() - we have lost a component with tag: " <<
00135 oldArray[j]->getTag() << endln;
00136 error = -3;
00137 }
00138 }
00139
00140
00141 if (oldArray != 0)
00142 delete [] oldArray;
00143
00144 return error;
00145 }
00146
00147
00148 bool
00149 ArrayOfTaggedObjects::addComponent(TaggedObject *newComponent)
00150 {
00151
00152
00153 TaggedObject *other = this->getComponentPtr(newComponent->getTag());
00154 if (other != 0) {
00155 opserr << "WARNING ArrayOfTaggedObjects::addComponent() - component" <<
00156 " with tag already exists, not adding component with tag: " <<
00157 newComponent->getTag() << endln;
00158 return false;
00159 }
00160
00161
00162
00163 if (numComponents == sizeComponentArray)
00164 if (this->setSize(2*numComponents) < 0) {
00165 opserr << "ArrayOfTaggedObjects::addComponent()- failed to enlarge the array with size" <<
00166 2*numComponents << endln;
00167 return false;
00168 }
00169
00170
00171
00172
00173 int newComponentTag = newComponent->getTag();
00174
00175 if ((newComponentTag >= 0) && (newComponentTag < sizeComponentArray)) {
00176 if (theComponents[newComponentTag] == 0) {
00177 theComponents[newComponentTag] = newComponent;
00178 numComponents ++;
00179 if (newComponentTag > positionLastEntry)
00180 positionLastEntry = newComponentTag;
00181 return true;
00182 }
00183 }
00184
00185
00186 while (theComponents[positionLastNoFitEntry] != 0 &&
00187 positionLastNoFitEntry < sizeComponentArray )
00188 positionLastNoFitEntry++;
00189
00190
00191 if (positionLastNoFitEntry == sizeComponentArray) {
00192 opserr << "ArrayOfTaggedObjects::addComponent() - could not - find a vacant spot after enlarging!!\n";
00193 return false;
00194 }
00195
00196 theComponents[positionLastNoFitEntry] = newComponent;
00197 numComponents++;
00198 if (positionLastNoFitEntry > positionLastEntry)
00199 positionLastEntry = positionLastNoFitEntry;
00200 fitFlag = false;
00201
00202 return true;
00203 }
00204
00205 TaggedObject *
00206 ArrayOfTaggedObjects::removeComponent(int tag)
00207 {
00208 TaggedObject *removed;
00209
00210
00211 if ((tag >= 0) && (tag < sizeComponentArray))
00212
00213
00214 if (fitFlag == true) {
00215 removed = theComponents[tag];
00216 theComponents[tag] = 0;
00217
00218
00219
00220 if (removed != 0) {
00221 numComponents--;
00222
00223 if (positionLastEntry == tag) {
00224 for (int i = positionLastEntry; i>=0; i--)
00225 if (theComponents[i] != 0) {
00226 positionLastEntry = i;
00227 i = -1;
00228 }
00229 }
00230 }
00231 return removed;
00232 } else
00233 if (theComponents[tag] != 0)
00234 if ((theComponents[tag]->getTag()) == tag) {
00235 removed = theComponents[tag];
00236 theComponents[tag] = 0;
00237 if (positionLastEntry == tag) {
00238 for (int i = positionLastEntry; i>=0; i--)
00239 if (theComponents[i] != 0) {
00240 positionLastEntry = i;
00241 i = -1;
00242 }
00243 }
00244 positionLastNoFitEntry = 0;
00245 numComponents--;
00246 return removed;
00247 }
00248
00249
00250
00251 for (int i=0; i<=positionLastEntry; i++)
00252 if (theComponents[i] != 0)
00253 if (theComponents[i]->getTag() == tag) {
00254
00255
00256
00257 removed = theComponents[i];
00258 theComponents[i] = 0;
00259 if (positionLastEntry == i) {
00260 for (int j = positionLastEntry; j>=0; j--)
00261 if (theComponents[j] != 0) {
00262 positionLastEntry = j;
00263 j = -1;
00264 }
00265 }
00266 positionLastNoFitEntry = 0;
00267 numComponents--;
00268 return removed;
00269 }
00270
00271
00272 return 0;
00273 }
00274
00275 int
00276 ArrayOfTaggedObjects::getNumComponents(void) const
00277 {
00278 return numComponents;
00279 }
00280
00281 TaggedObject *
00282 ArrayOfTaggedObjects::getComponentPtr(int tag)
00283 {
00284
00285
00286 if ((tag >= 0) && (tag < sizeComponentArray))
00287 if (fitFlag == true)
00288 return theComponents[tag];
00289 else {
00290 if (theComponents[tag] != 0)
00291 if ((theComponents[tag]->getTag()) == tag)
00292 return theComponents[tag];
00293 }
00294
00295
00296
00297 for (int i=0; i<=positionLastEntry; i++)
00298 if (theComponents[i] != 0)
00299 if (theComponents[i]->getTag() == tag)
00300 return theComponents[i];
00301
00302
00303 return 0;
00304 }
00305
00306
00307 TaggedObjectIter &
00308 ArrayOfTaggedObjects::getComponents()
00309 {
00310
00311
00312 myIter.reset();
00313 return myIter;
00314 }
00315
00316
00317 ArrayOfTaggedObjectsIter
00318 ArrayOfTaggedObjects::getIter()
00319 {
00320
00321
00322
00323 return ArrayOfTaggedObjectsIter(*this);
00324 }
00325
00326
00327 TaggedObjectStorage *
00328 ArrayOfTaggedObjects::getEmptyCopy(void)
00329 {
00330 ArrayOfTaggedObjects *theCopy = new ArrayOfTaggedObjects(sizeComponentArray);
00331
00332 if (theCopy == 0) {
00333 opserr << "ArrayOfTaggedObjects::getEmptyCopy - out of memory\n";
00334 }
00335
00336 return theCopy;
00337 }
00338
00339 void
00340 ArrayOfTaggedObjects::clearAll(bool invokeDestructors)
00341 {
00342 if (invokeDestructors == true) {
00343
00344
00345 for (int i=0; i<=positionLastEntry; i++) {
00346 if (theComponents[i] != 0) {
00347 delete theComponents[i];
00348 theComponents[i] = 0;
00349 }
00350 }
00351 } else {
00352
00353 for (int i=0; i<=positionLastEntry; i++) {
00354 if (theComponents[i] != 0) {
00355 theComponents[i] = 0;
00356 }
00357 }
00358 }
00359
00360 positionLastEntry = 0;
00361 positionLastNoFitEntry = 0;
00362 fitFlag = true;
00363 numComponents = 0;
00364 }
00365
00366
00367
00368
00369
00370
00371
00372 void
00373 ArrayOfTaggedObjects::Print(OPS_Stream &s, int flag)
00374 {
00375
00376 for (int i=0; i<=positionLastEntry; i++)
00377 if (theComponents[i] != 0)
00378 theComponents[i]->Print(s, flag);
00379 }
00380
00381
00382