MapOfTaggedObjects.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.2 $
00022 // $Date: 2003/02/14 23:02:10 $
00023 // $Source: /usr/local/cvs/OpenSees/SRC/tagged/storage/MapOfTaggedObjects.cpp,v $
00024                                                                         
00025                                                                         
00026 // File: ~/tagged/storage/MapOfTaggedObjects.C
00027 //
00028 // Written: fmk 
00029 // Created: 02/00
00030 // Revision: A
00031 //
00032 // Purpose: This file contains the implementation of the MapOfTaggedObjects
00033 // class.
00034 //
00035 // What: "@(#) MapOfTaggedObjects.C, revA"
00036 
00037 #include <TaggedObject.h>
00038 #include <MapOfTaggedObjects.h>
00039 
00040 #include <OPS_Globals.h>
00041 
00042 // some typedefs that will be useful
00043 typedef map<int, TaggedObject *> MAP_TAGGED;
00044 typedef MAP_TAGGED::value_type   MAP_TAGGED_TYPE;
00045 typedef MAP_TAGGED::iterator     MAP_TAGGED_ITERATOR;
00046 
00047 MapOfTaggedObjects::MapOfTaggedObjects()
00048 :myIter(*this)
00049 {
00050     // creates the iter with this as the argument
00051 }
00052 
00053 MapOfTaggedObjects::~MapOfTaggedObjects()
00054 {
00055     // does nothing
00056 }
00057 
00058 
00059 int
00060 MapOfTaggedObjects::setSize(int newSize)
00061 {
00062     // no setSize for map template .. can only check enough space available
00063     int maxSize = theMap.max_size();
00064     if (newSize > maxSize) {
00065       opserr << "MapOfTaggedObjects::setSize - failed as map stl has a max size of " << maxSize << "\n";
00066       return -1;
00067     } 
00068    
00069     return 0;
00070 }
00071 
00072 
00073 bool 
00074 MapOfTaggedObjects::addComponent(TaggedObject *newComponent)
00075 {
00076     MAP_TAGGED_ITERATOR theEle;
00077     int tag = newComponent->getTag();
00078 
00079     // check if the ele already in map, if not we add
00080     theEle = theMap.find(tag);
00081     if (theEle == theMap.end()) {
00082         theMap.insert(MAP_TAGGED_TYPE(tag,newComponent));
00083                       
00084         // check if sucessfully added 
00085         theEle = theMap.find(tag);
00086         if (theEle == theMap.end()) {
00087           opserr << "MapOfTaggedObjects::addComponent - map STL failed to add object with tag : " << 
00088             newComponent->getTag() << "\n";
00089           return false;
00090         }
00091     }
00092     
00093     // if ele already there map cannot add even if allowMultiple is true
00094     // as the map template does not allow multiple entries wih the same tag
00095     else {      
00096       opserr << "MapOfTaggedObjects::addComponent - not adding as one with similar tag exists, tag: " <<
00097         newComponent->getTag() << "\n";
00098       return false;
00099     }
00100     
00101     return true;  // o.k.
00102 }
00103 
00104 
00105 TaggedObject *
00106 MapOfTaggedObjects::removeComponent(int tag)
00107 {
00108     TaggedObject *removed =0;
00109     MAP_TAGGED_ITERATOR theEle;
00110 
00111     // return 0 if component does not exist, otherwise remove it
00112     theEle = theMap.find(tag);
00113     if (theEle == theMap.end()) // the object has not been added
00114         return 0;
00115     else { // the object exists so we remove it
00116         removed = (*theEle).second;
00117         int ok = theMap.erase(tag);
00118         if (ok != 1) { // ensure the map did remove the object
00119           opserr << "MapOfTaggedObjects::removeComponent - map STL failed to remove object with tag " << 
00120             tag << "\n";
00121           return 0;
00122         }
00123     }
00124 
00125     return removed;
00126 }
00127 
00128 
00129 int
00130 MapOfTaggedObjects::getNumComponents(void) const
00131 {
00132     return theMap.size();
00133 }
00134 
00135 
00136 TaggedObject *
00137 MapOfTaggedObjects::getComponentPtr(int tag)
00138 {
00139     TaggedObject *removed =0;
00140     MAP_TAGGED_ITERATOR theEle;
00141     
00142     // return 0 if component does not exist, otherwise remove it
00143     theEle = theMap.find(tag);
00144     if (theEle == theMap.end()) 
00145         return 0;
00146     else 
00147         removed = (*theEle).second;
00148     
00149     return removed;
00150 }
00151 
00152 
00153 TaggedObjectIter &
00154 MapOfTaggedObjects::getComponents()
00155 {
00156     myIter.reset();
00157     return myIter;
00158 }
00159 
00160 
00161 MapOfTaggedObjectsIter 
00162 MapOfTaggedObjects::getIter()
00163 {
00164     return MapOfTaggedObjectsIter(*this);
00165 }
00166 
00167 
00168 TaggedObjectStorage *
00169 MapOfTaggedObjects::getEmptyCopy(void)
00170 {
00171     MapOfTaggedObjects *theCopy = new MapOfTaggedObjects();
00172     
00173     if (theCopy == 0) {
00174       opserr << "MapOfTaggedObjects::getEmptyCopy-out of memory\n";
00175     }   
00176 
00177     return theCopy;
00178 }
00179 
00180 void
00181 MapOfTaggedObjects::clearAll(bool invokeDestructor)
00182 {
00183 
00184     // invoke the destructor on all the tagged objects stored
00185     if (invokeDestructor == true) {
00186         MAP_TAGGED_ITERATOR p = theMap.begin();
00187         while (p != theMap.end()) {
00188             delete (*p).second;
00189             p++;
00190         }
00191     }
00192 
00193     // now clear the map of all entries
00194     theMap.clear();
00195 }
00196 
00197 void
00198 MapOfTaggedObjects::Print(OPS_Stream &s, int flag)
00199 {
00200     // go through the array invoking Print on non-zero entries
00201     MAP_TAGGED_ITERATOR p = theMap.begin();
00202     while (p != theMap.end()) {
00203         ((*p).second)->Print(s, flag);
00204         p++;
00205     }
00206 }
00207 
00208 
00209 

Generated on Mon Oct 23 15:05:29 2006 for OpenSees by doxygen 1.5.0