#ifndef CT_CLOUDINDEXSTDLISTT_HPP #define CT_CLOUDINDEXSTDLISTT_HPP #include "ct_cloudindex/ct_cloudindexstdlistt.h" #include template CT_CloudIndexStdListT::CT_CloudIndexStdListT(const size_t &size) : CT_AbstractModifiableCloudIndexT() { this->internalSetSortType(CT_AbstractCloudIndex::SortedInAscendingOrder); this->internalSetUnregisteredWhenIsEmpty(false); m_collection = QSharedPointer< std::list >(new std::list(size)); } template void CT_CloudIndexStdListT::setSortType(CT_AbstractCloudIndex::SortType type) { if(this->sortType() != type) { this->internalSetSortType(type); if(this->sortType() == CT_AbstractCloudIndex::SortedInAscendingOrder) m_collection->sort(); } } template size_t CT_CloudIndexStdListT::size() const { return m_collection->size(); } template size_t CT_CloudIndexStdListT::memoryUsed() const { return size() * sizeof(T); } template size_t CT_CloudIndexStdListT::indexAt(const size_t &i) const { typename std::list::const_iterator it = m_collection->begin(); std::advance(it, i); return *it; } template const ct_index_type& CT_CloudIndexStdListT::constIndexAt(const size_t &i) const { typename std::list::iterator it = m_collection->begin(); std::advance(it, i); return *it; } template size_t CT_CloudIndexStdListT::operator[](const size_t &i) const { typename std::list::const_iterator it = m_collection->begin(); std::advance(it, i); return *it; } template void CT_CloudIndexStdListT::indexAt(const size_t &i, size_t &index) const { typename std::list::const_iterator it = m_collection->begin(); std::advance(it, i); index = *it; } template size_t CT_CloudIndexStdListT::first() const { return m_collection->front(); } template size_t CT_CloudIndexStdListT::last() const { return m_collection->back(); } template bool CT_CloudIndexStdListT::contains(const size_t &index) const { if(this->sortType() == CT_AbstractCloudIndex::SortedInAscendingOrder) return std::binary_search(m_collection->begin(), m_collection->end(), index); return (std::find(m_collection->begin(), m_collection->end(), index) != m_collection->end()); } template size_t CT_CloudIndexStdListT::indexOf(const size_t &index) const { if(this->sortType() == CT_AbstractCloudIndex::SortedInAscendingOrder) { typename std::list::iterator first = m_collection->begin(); typename std::list::iterator last = m_collection->end(); first = std::lower_bound(first, last, index); if(first!=last && !(index<(*first))) return *first; } else { typename std::list::const_iterator it = std::find(m_collection->begin(), m_collection->end(), index); if(it != m_collection->end()) return *it; } return size(); } template size_t CT_CloudIndexStdListT::lowerBound(const size_t &value) const { typename std::list::iterator it; typename std::list::iterator itEnd = m_collection->end(); if(this->sortType() == CT_AbstractCloudIndex::SortedInAscendingOrder) it = std::lower_bound(m_collection->begin(), m_collection->end(), value); else it = std::find_if(m_collection->begin(), m_collection->end(), std::bind2nd(std::greater_equal(), value)); return size() - std::distance(it, itEnd); } template size_t CT_CloudIndexStdListT::upperBound(const size_t &value) const { typename std::list::iterator it; typename std::list::iterator itEnd = m_collection->end(); if(this->sortType() == CT_AbstractCloudIndex::SortedInAscendingOrder) it = std::upper_bound(m_collection->begin(), m_collection->end(), value); else std::find_if(m_collection->begin(), m_collection->end(), std::bind2nd(std::greater(), value)); return size() - std::distance(it, itEnd); } template void CT_CloudIndexStdListT::addIndex(const size_t &newIndex) { if(this->sortType() == CT_AbstractCloudIndex::SortedInAscendingOrder) m_collection->insert(std::lower_bound(m_collection->begin(), m_collection->end(), newIndex), newIndex); else m_collection->push_back(newIndex); } template void CT_CloudIndexStdListT::removeIndex(const size_t &index) { if(this->sortType() == CT_AbstractCloudIndex::SortedInAscendingOrder) { typename std::list::iterator first = m_collection->begin(); typename std::list::iterator last = m_collection->end(); first = std::lower_bound(first, last, index); if(first!=last && !(index<(*first))) m_collection->erase(first); } else { m_collection->erase(std::find(m_collection->begin(), m_collection->end(), index)); } } template void CT_CloudIndexStdListT::replaceIndex(const size_t &i, const ct_index_type &newIndex, const bool &verifyRespectSort) { typename std::list::iterator it = m_collection->begin(); std::advance(it, i); (*it) = newIndex; if(verifyRespectSort && (this->sortType() == CT_CloudIndexStdListT::SortedInAscendingOrder)) { bool ok = false; if(i>0) { typename std::list::const_iterator itP = it; --itP; if((*itP) <= newIndex) ok = true; } if(ok) { typename std::list::const_iterator itN = it; ++itN; if(itN != m_collection->end()) { if((*itN) >= newIndex) ok = true; else ok = false; } } if(!ok) this->setSortType(CT_CloudIndexStdListT::NotSorted); } } template void CT_CloudIndexStdListT::push_front(const size_t &newIndex) { CT_AbstractCloudIndex::SortType t = this->sortType(); if(t != CT_AbstractCloudIndex::NotSorted) { if(size() > 0) { if(newIndex > first()) t = CT_AbstractCloudIndex::NotSorted; } } m_collection->insert(m_collection->begin(), newIndex); } template void CT_CloudIndexStdListT::fill() { typename std::list::iterator it = m_collection->begin(); typename std::list::iterator end = m_collection->end(); ct_index_type i = 0; while(it != end) { (*it) = i; ++it; ++i; } } template void CT_CloudIndexStdListT::clear() { internalClear(); } template void CT_CloudIndexStdListT::erase(const size_t &beginIndex, const size_t &sizes) { typename std::list::iterator b = m_collection->begin(); std::advance(b, beginIndex); typename std::list::iterator e = m_collection->begin(); std::advance(e, beginIndex+sizes); m_collection->erase(b, e); } template void CT_CloudIndexStdListT::resize(const size_t &newSize) { m_collection->resize(newSize, 0); } template void CT_CloudIndexStdListT::removeIfOrShiftIf(typename CT_CloudIndexStdListT::FindIfFunction findIf, typename CT_CloudIndexStdListT::RemoveIfFunction removeIf, typename CT_CloudIndexStdListT::ShiftIfFunction shiftIf, const size_t &shiftValue, const bool &negativeShift, void *context) { typename std::list::iterator first = listFindIf(findIf, context); typename std::list::iterator last = m_collection->end(); if(first != last) { typename std::list::iterator i = first; size_t nI; while(i != last) { nI = *i; if(!(*removeIf)(context, nI)) { if((*shiftIf)(context, nI)) { if(negativeShift) *i -= shiftValue; else *i += shiftValue; } *first = *i; ++first; } ++i; } } if(first != last) m_collection->erase(first); } template void CT_CloudIndexStdListT::shiftAll(const size_t &offset, const bool &negativeOffset) { this->internalShiftAll(offset, negativeOffset); } template void CT_CloudIndexStdListT::eraseBetweenAndShiftRest(const size_t &eraseBeginPos, const size_t &eraseSize, const size_t &offset, const bool &negativeOffset) { erase(eraseBeginPos, eraseSize); typename std::list::iterator b = m_collection->begin(); typename std::list::iterator e = m_collection->end(); std::advance(b, eraseBeginPos); if(negativeOffset) { while(b != e) { (*b) = (*b) - offset; ++b; } } else { while(b != e) { (*b) = (*b) + offset; ++b; } } } template CT_SharedPointer< std::vector > CT_CloudIndexStdListT::toStdVectorInt() const { CT_SharedPointer< std::vector > indices(new std::vector(size())); typename std::list::iterator f = m_collection->begin(); typename std::list::iterator l = m_collection->end(); size_t i = 0; while(f != l) { (*indices.get())[i] = *f; ++f; ++i; } return indices; } template CT_AbstractCloud* CT_CloudIndexStdListT::copy() const { CT_CloudIndexStdListT *index = new CT_CloudIndexStdListT(size()); std::copy(m_collection->begin(), m_collection->end(), index->m_collection->begin()); index->setSortType(this->sortType()); return index; } template template typename std::list::iterator CT_CloudIndexStdListT::listFindIf(typename CT_CloudIndexStdListT::FindIfFunction findIf, void *context) const { typename std::list::iterator first = m_collection->begin(); typename std::list::iterator last = m_collection->end(); size_t tmp; while (first!=last) { tmp = *first; if ((*findIf)(context, tmp)) return first; ++first; } return last; } template std::list< ct_index_type >* CT_CloudIndexStdListT::internalData() const { return m_collection.data(); } template void CT_CloudIndexStdListT::internalShiftAll(const size_t &offset, const bool &negativeOffset) { typename std::list::iterator first = m_collection->begin(); typename std::list::iterator last = m_collection->end(); if(negativeOffset) { while(first != last) { *first -= offset; ++first; } } else { while(first != last) { *first += offset; ++first; } } } template void CT_CloudIndexStdListT::internalClear() { m_collection->clear(); } #endif // CT_CLOUDINDEXSTDLISTT_HPP