00001 #include "GLLogicError.h"
00002 #include "GLRuntimeError.h"
00003 #include "GLStorageVector.h"
00004
00011 GLStorageVector::GLStorageVector(unsigned expected_size):
00012 GLBaseOrganismStorage()
00013 {
00014
00015 m_storage.reserve(expected_size);
00016 }
00017
00018 GLStorageVector::~GLStorageVector()
00019 {
00020 emptyStorage();
00021 }
00022
00032 bool GLStorageVector::addOrganism(GLBaseOrganism* organism,
00033 GLConstants::TTwinsFlags twins)
00034 {
00035
00036
00037 if ((twins == GLConstants::TWINS_ALLOWED) ||
00038 (findOrganismIndex(organism) == -1))
00039 {
00040 m_storage.push_back(organism);
00041 return true;
00042 }
00043 return false;
00044 }
00045
00049 void GLStorageVector::emptyStorage()
00050 {
00051
00052 switch(getMemoryPolicy())
00053 {
00054 case GLConstants::FREE_ALL:
00055 deleteAllOrganisms();
00056 break;
00057 case GLConstants::FREE_UNLOCKED:
00058 deleteOrganisms();
00059 break;
00060 default:
00061 break;
00062 }
00063
00064 m_storage.clear();
00065 }
00066
00073 int GLStorageVector::findOrganismIndex(
00074 const GLBaseOrganism* organism,
00075 bool soft) const
00076 {
00077
00078 if (organism == NULL) return -1;
00079 for(int i = 0; i < getSize(); i++)
00080 {
00081 if (m_storage[i]->isEqual(organism, soft)) return i;
00082 }
00083 return -1;
00084 }
00085
00090 GLBaseOrganismStorage::iterator* GLStorageVector::getIterator()
00091 {
00092 return new iterator(m_storage);
00093 }
00094
00101 GLBaseOrganismStorage::const_iterator*
00102 GLStorageVector::getIteratorConst() const
00103 {
00104 return new const_iterator(m_storage);
00105 }
00106
00111 GLBaseOrganism* GLStorageVector::getOrganismPointer(unsigned i)
00112 {
00113 if ((i < 0) || (i >= m_storage.size()))
00114 {
00115 char errormsg[1000];
00116 sprintf(errormsg, "\nGLStorageVector::getOrganismPointer\n"
00117 "Request for the organism with index %u while "
00118 "only %u organisms are available\n",
00119 i, (unsigned)(m_storage.size()));
00120 throw GLRuntimeError(errormsg);
00121 }
00122 return m_storage[i];
00123 }
00124
00129 const GLBaseOrganism* GLStorageVector::getOrganismPointer(unsigned i) const
00130 {
00131 if ((i < 0) || (i >= m_storage.size()))
00132 {
00133 char errormsg[1000];
00134 sprintf(errormsg, "\nGLStorageVector::getOrganismPointer\n"
00135 "Request for the organism with index %i while "
00136 "only %i organisms are available\n",
00137 i, (unsigned)(m_storage.size()));
00138 throw GLRuntimeError(errormsg);
00139 }
00140 return m_storage[i];
00141 }
00142
00160 int GLStorageVector::getOrganismsVector(
00161 vector<GLBaseOrganism*>& container,
00162 GLConstants::TStorageFilling fill,
00163 int size,
00164 bool copy_clones)
00165 {
00166 if (fill == GLConstants::REPLACE) container.clear();
00167 int to_transfer = size;
00168 if ((size == -1) || (size >= getSize()))
00169 {
00170 to_transfer = getSize();
00171 }
00172
00173 int container_start = container.size();
00174 container.resize(container.size() + to_transfer);
00175
00176
00177 vector<GLBaseOrganism*>::iterator it_cont =
00178 container.begin() + container_start;
00179 vector<GLBaseOrganism*>::iterator it =
00180 m_storage.begin();
00181 for(int i = 0; i < to_transfer; i++)
00182 {
00183 *it_cont = copy_clones ?
00184 (*it)->makeClone(false) : *it;
00185 ++it_cont;
00186 ++it;
00187 }
00188 return to_transfer;
00189 }
00190
00206 int GLStorageVector::getOrganismsVector(
00207 vector<const GLBaseOrganism*>& container,
00208 GLConstants::TStorageFilling fill,
00209 int size,
00210 bool copy_clones) const
00211 {
00212 if (fill == GLConstants::REPLACE) container.clear();
00213 int to_transfer = size;
00214 if ((size == -1) || (size >= getSize()))
00215 {
00216 to_transfer = getSize();
00217 }
00218
00219 int container_start = container.size();
00220 container.resize(container.size() + to_transfer);
00221
00222
00223 vector<const GLBaseOrganism*>::iterator it_cont =
00224 container.begin() + container_start;
00225 vector<GLBaseOrganism*>::const_iterator it =
00226 m_storage.begin();
00227 for(int i = 0; i < to_transfer; i++)
00228 {
00229 *it_cont = copy_clones ?
00230 (*it)->makeClone(false) : *it;
00231 ++it_cont;
00232 ++it;
00233 }
00234 return to_transfer;
00235 }
00236
00243 void GLStorageVector::merge(
00244 const GLBaseOrganismStorage* storage,
00245 GLConstants::TTwinsFlags twins)
00246 {
00247
00248 const GLStorageVector* container =
00249 dynamic_cast<const GLStorageVector*>(storage);
00250 if (container == NULL)
00251 {
00252 char errormsg[1000];
00253 sprintf(errormsg, "\nGLStorageVector::merge\n"
00254 "Can be merged with another GLStorageVector or "
00255 "its children only");
00256 throw GLLogicError(errormsg);
00257 }
00258
00259 if (twins == GLConstants::TWINS_ALLOWED)
00260 {
00261 m_storage.insert(m_storage.end(), container->m_storage.begin(),
00262 container->m_storage.end());
00263 }
00264 else
00265 {
00266
00267 for(unsigned i = 0; i < container->m_storage.size(); i++)
00268 addOrganism(container->m_storage[i], twins);
00269 }
00270 }
00271
00278 int GLStorageVector::trimSize(unsigned int new_size)
00279 {
00280 if (m_storage.size() <= new_size) return m_storage.size();
00281
00282
00283
00284 if (getMemoryPolicy() != GLConstants::DO_NOT_FREE)
00285 {
00286 for(unsigned i = new_size; i < m_storage.size(); i++)
00287 {
00288
00289
00290
00291
00292
00293 if ((m_storage[i] != NULL) &&
00294 ((getMemoryPolicy() == GLConstants::FREE_ALL) ||
00295 ((getMemoryPolicy() == GLConstants::FREE_UNLOCKED) &&
00296 (!m_storage[i]->isLockedInMemory()))))
00297 {
00298 delete m_storage[i];
00299 }
00300 }
00301 }
00302 m_storage.resize(new_size);
00303 return new_size;
00304 }