00001 #include "GLBaseOrganism.h"
00002 #include "GLLogicError.h"
00003 #include "GLRuntimeError.h"
00004 #include "GLStorageSet.h"
00005
00006 GLStorageSet::GLStorageSet()
00007 {
00008 }
00009
00010 GLStorageSet::~GLStorageSet()
00011 {
00012 }
00013
00023 bool GLStorageSet::addOrganism(GLBaseOrganism* organism,
00024 GLConstants::TTwinsFlags twins)
00025 {
00026
00027
00028 return (m_storage.insert(organism)).second;
00029 }
00030
00034 void GLStorageSet::emptyStorage()
00035 {
00036
00037 switch(getMemoryPolicy())
00038 {
00039 case GLConstants::FREE_ALL:
00040 deleteAllOrganisms();
00041 break;
00042 case GLConstants::FREE_UNLOCKED:
00043 deleteOrganisms();
00044 break;
00045 default:
00046 break;
00047 }
00048
00049 m_storage.clear();
00050 }
00051
00060 const GLBaseOrganism* GLStorageSet::findOrganism(
00061 const GLBaseOrganism* organism,
00062 bool soft) const
00063 {
00064 TGLSetOfOrganisms::const_iterator it =
00065 m_storage.find(const_cast<GLBaseOrganism*>(organism));
00066 if (it == m_storage.end())
00067 {
00068 return NULL;
00069 }
00070 return *it;
00071 }
00076 GLBaseOrganismStorage::iterator* GLStorageSet::getIterator()
00077 {
00078 return new iterator(m_storage);
00079 }
00080
00087 GLBaseOrganismStorage::const_iterator*
00088 GLStorageSet::getIteratorConst() const
00089 {
00090 return new const_iterator(m_storage);
00091 }
00092
00093
00111 int GLStorageSet::getOrganismsVector(
00112 vector<GLBaseOrganism*>& container,
00113 GLConstants::TStorageFilling fill,
00114 int size,
00115 bool copy_clones)
00116 {
00117 if (fill == GLConstants::REPLACE) container.clear();
00118 int to_transfer = size;
00119 if ((size == -1) || (size >= getSize()))
00120 {
00121 to_transfer = getSize();
00122 }
00123 container.reserve(container.size() + to_transfer);
00124 TGLSetOfOrganisms::iterator it = m_storage.begin();
00125 for(int i = 0; i < to_transfer; i++)
00126 {
00127 container.push_back(copy_clones ?
00128 (*it)->makeClone(false) : *it);
00129 ++it;
00130 }
00131 return to_transfer;
00132 }
00133
00149 int GLStorageSet::getOrganismsVector(
00150 vector<const GLBaseOrganism*>& container,
00151 GLConstants::TStorageFilling fill,
00152 int size,
00153 bool copy_clones) const
00154 {
00155 if (fill == GLConstants::REPLACE) container.clear();
00156 int to_transfer = size;
00157 if ((size == -1) || (size >= getSize()))
00158 {
00159 to_transfer = getSize();
00160 }
00161
00162 int container_start = container.size();
00163 container.resize(container.size() + to_transfer);
00164
00165
00166 vector<const GLBaseOrganism*>::iterator it_cont =
00167 container.begin() + container_start;
00168 TGLSetOfOrganisms::const_iterator it =
00169 m_storage.begin();
00170 for(int i = 0; i < to_transfer; i++)
00171 {
00172 *it_cont = copy_clones ?
00173 (*it)->makeClone(false) : *it;
00174 ++it_cont;
00175 ++it;
00176 }
00177 return to_transfer;
00178 }
00179
00186 void GLStorageSet::merge(
00187 const GLBaseOrganismStorage* storage,
00188 GLConstants::TTwinsFlags twins)
00189 {
00190
00191 const GLStorageSet* container =
00192 dynamic_cast<const GLStorageSet*>(storage);
00193 if (container == NULL)
00194 {
00195 char errormsg[1000];
00196 sprintf(errormsg, "\nGLStorageSet::merge\n"
00197 "Can be merged with another GLStorageSet or "
00198 "its children only");
00199 throw GLLogicError(errormsg);
00200 }
00201
00202 m_storage.insert(container->m_storage.begin(),
00203 container->m_storage.end());
00204 }
00205
00212 int GLStorageSet::trimSize(unsigned int new_size)
00213 {
00214 throw GLRuntimeError("GLStorageSet::trimSize not implemented\n");
00215 if (m_storage.size() <= new_size) return m_storage.size();
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 return new_size;
00239 }