From: <si...@us...> - 2012-03-30 02:59:15
|
Revision: 4534 http://simupop.svn.sourceforge.net/simupop/?rev=4534&view=rev Author: simupop Date: 2012-03-30 02:59:08 +0000 (Fri, 30 Mar 2012) Log Message: ----------- Add function insert_back and use it to simplify Population.removeIndividuals and Population.removeSubPops Modified Paths: -------------- trunk/src/mutant_vector.h trunk/src/population.cpp Modified: trunk/src/mutant_vector.h =================================================================== --- trunk/src/mutant_vector.h 2012-03-30 01:24:46 UTC (rev 4533) +++ trunk/src/mutant_vector.h 2012-03-30 02:59:08 UTC (rev 4534) @@ -238,7 +238,7 @@ while ((filled_ > 0) && (index_data_[filled_ - 1] >= size)) { --filled_; } - }else { + } else { index_data_.resize(capacity_); value_data_.resize(capacity_); filled_ = 0; @@ -366,7 +366,6 @@ } - // Assignment inline vectorm & operator =(const vectorm & v) { @@ -416,6 +415,28 @@ } + class const_val_iterator; + + // insert, added by Bo + inline void insert_back(const const_val_iterator & beg, const const_val_iterator end, int shift = 0) + { + BOOST_UBLAS_CHECK(filled_ == 0 || index_data_ [filled_ - 1] < i, external_logic()); + if (filled_ + (end - beg) >= capacity_) + reserve(2 * std::max(capacity_, filled_ + (end - beg)), true); + BOOST_UBLAS_CHECK(filled_ < capacity_, internal_logic()); + const_val_iterator ptr = beg; + BOOST_UBLAS_CHECK(beg == end || beg != (*this)().end(), bad_index()); + for (; ptr != end; ++ptr) { + if (*ptr != 0) { + index_data_ [filled_] = ptr.index() + shift; + value_data_ [filled_] = *ptr; + ++filled_; + } + } + storage_invariants(); + } + + inline void pop_back() { BOOST_UBLAS_CHECK(filled_ > 0, external_logic()); @@ -510,6 +531,13 @@ } + // added by Bo to calcualte number of elements between things + inline size_type operator -(const const_val_iterator & it) const + { + return it_ - it.it_; + } + + // Assignment inline const_val_iterator & operator =(const const_val_iterator & it) { @@ -537,6 +565,12 @@ } + inline const_val_iterator val_begin(size_type idx) const + { + return find(idx); + } + + inline const_val_iterator val_end() const { return find(size_); @@ -615,6 +649,12 @@ friend class const_val_iterator; }; + inline val_iterator val_begin(size_type idx) + { + return find(idx); + } + + inline val_iterator val_begin() { return find(0); @@ -1065,20 +1105,21 @@ return &((*this)()); } + /* - void deleted() - { - (*this)().erase_element(m_index); - } - */ + void deleted() + { + (*this)().erase_element(m_index); + } + */ void assignIfDiffer(const_reference value) { if (value != this->value()) (*this)()[m_index] = value; } - + friend class const_iterator; operator const_iterator() const Modified: trunk/src/population.cpp =================================================================== --- trunk/src/population.cpp 2012-03-30 01:24:46 UTC (rev 4533) +++ trunk/src/population.cpp 2012-03-30 02:59:08 UTC (rev 4534) @@ -777,6 +777,7 @@ GenoIterator b = genoBegin(false); GenoIterator e = genoEnd(false); IndexArray idx(b.getIndexIterator(), e.getIndexIterator()); + cerr << "IDX: " << idx << " (size " << idx.size() << ")" << endl; ValueArray val(b.getValueIterator(), e.getValueIterator()); vectoru x; @@ -784,6 +785,8 @@ x.push_back(int(val[i])); cerr << "VAL: " << x << " (size " << x.size() << ")" << endl; } + + #endif @@ -1203,12 +1206,13 @@ size_t infoStep = infoSize(); RawIndIterator oldInd = m_inds.begin(); RawIndIterator newInd = m_inds.begin(); - GenoIterator oldPtr = m_genotype.begin(); #ifdef MUTANTALLELE - IndexArray new_index; - ValueArray new_value; + size_t oldPtr = 0; + vectorm new_genotype; + new_genotype.resize(m_genotype.size()); size_t lagging = 0; #else + GenoIterator oldPtr = m_genotype.begin(); GenoIterator newPtr = m_genotype.begin(); #endif #ifdef LINEAGE @@ -1259,16 +1263,7 @@ copy(oldInfoPtr, oldInfoPtr + infoStep, newInfoPtr); LINEAGE_EXPR(copy(oldLineagePtr, oldLineagePtr + step, newLineagePtr)); } - GenoIterator b(oldPtr + 0); - GenoIterator e(oldPtr + step); - IndexArray arr(b.getIndexIterator(), e.getIndexIterator()); - if (lagging > 0) - for (size_t k = 0; k < arr.size(); ++k) - arr[k] -= lagging; - if (arr.size() > 0) { - new_index.insert(new_index.end(), arr.begin(), arr.end()); - new_value.insert(new_value.end(), b.getValueIterator(), e.getValueIterator()); - } + new_genotype.insert_back(m_genotype.val_begin(oldPtr), m_genotype.val_begin(oldPtr + step), -lagging); #else if (oldInd != newInd) { *newInd = *oldInd; @@ -1307,17 +1302,8 @@ copy(oldInfoPtr, oldInfoPtr + infoStep * spSize, newInfoPtr); LINEAGE_EXPR(copy(oldLineagePtr, oldLineagePtr + step * spSize, newLineagePtr)); } - // genotype has to be called in any case - GenoIterator b(oldPtr + 0); - GenoIterator e(oldPtr + step * spSize); - IndexArray arr(b.getIndexIterator(), e.getIndexIterator()); - if (lagging > 0) - for (size_t k = 0; k < arr.size(); ++k) - arr[k] -= lagging; - if (arr.size() > 0) { - new_index.insert(new_index.end(), arr.begin(), arr.end()); - new_value.insert(new_value.end(), b.getValueIterator(), e.getValueIterator()); - } + new_genotype.insert_back(m_genotype.val_begin(oldPtr), m_genotype.val_begin(oldPtr + step * spSize), + -lagging); #else if (oldPtr != newPtr) { copy(oldPtr, oldPtr + step * spSize, newPtr); @@ -1339,8 +1325,8 @@ // m_inds.erase(newInd, m_inds.end()); #ifdef MUTANTALLELE - vectorm new_geno(m_genotype.size() - lagging, new_index, new_value); - m_genotype.swap(new_geno); + new_genotype.resize(m_genotype.size() - lagging, true); + m_genotype.swap(new_genotype); #else m_genotype.erase(newPtr, m_genotype.end()); #endif @@ -1375,12 +1361,13 @@ RawIndIterator newInd = m_inds.begin(); InfoIterator oldInfoPtr = m_info.begin(); InfoIterator newInfoPtr = m_info.begin(); - GenoIterator oldPtr = m_genotype.begin(); #ifdef MUTANTALLELE - IndexArray new_index; - ValueArray new_value; + size_t oldPtr = 0; + vectorm new_genotype; + new_genotype.resize(m_genotype.size()); size_t lagging = 0; #else + GenoIterator oldPtr = m_genotype.begin(); GenoIterator newPtr = m_genotype.begin(); #endif @@ -1402,16 +1389,7 @@ copy(oldInfoPtr, oldInfoPtr + infoStep, newInfoPtr); LINEAGE_EXPR(copy(oldLineagePtr, oldLineagePtr + step, newLineagePtr)); } - GenoIterator b(oldPtr + 0); - GenoIterator e(oldPtr + step); - IndexArray arr(b.getIndexIterator(), e.getIndexIterator()); - if (lagging > 0) - for (size_t k = 0; k < arr.size(); ++k) - arr[k] -= lagging; - if (arr.size() > 0) { - new_index.insert(new_index.end(), arr.begin(), arr.end()); - new_value.insert(new_value.end(), b.getValueIterator(), e.getValueIterator()); - } + new_genotype.insert_back(m_genotype.val_begin(oldPtr), m_genotype.val_begin(oldPtr + step), -lagging); #else if (oldInd != newInd) { *newInd = *oldInd; @@ -1441,8 +1419,8 @@ // m_inds.erase(newInd, m_inds.end()); #ifdef MUTANTALLELE - vectorm new_geno(m_genotype.size() - lagging, new_index, new_value); - m_genotype.swap(new_geno); + new_genotype.resize(m_genotype.size() - lagging, true); + m_genotype.swap(new_genotype); #else m_genotype.erase(newPtr, m_genotype.end()); #endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |