#ifndef CT_CLOUDINDEXITERATORT_H #define CT_CLOUDINDEXITERATORT_H #include #include "ct_global/ct_context.h" #include "ct_cloud/abstract/ct_abstractcloudt.h" #include "ct_cloudindex/registered/abstract/ct_abstractcloudindexregistered.h" template class CT_AbstractCloudIndexT; template class CT_CloudIndexIteratorT { public: typedef CT_CloudIndexIteratorT self_type; CT_CloudIndexIteratorT(const CT_AbstractCloudIndexT *index) { m_index = (CT_AbstractCloudIndexT*)index; m_startPos = 0; m_endPos = (m_index == NULL) ? 0 : m_index->size(); toFront(); } CT_CloudIndexIteratorT(QSharedPointer cir) { if(cir.data() == NULL) m_index = NULL; else m_index = dynamic_cast*>(cir->abstractCloudIndex()); m_startPos = 0; m_endPos = (m_index == NULL) ? 0 : m_index->size(); toFront(); } inline size_t size() const { return m_index->size(); } inline void toFront() { m_currentPos = m_startPos; m_atFront = true; } inline bool hasNext() const { if(m_startPos == m_endPos) { return false; } if(m_atFront) { return m_currentPos < m_endPos; } else { return m_currentPos < (m_endPos-1); } } inline CT_CloudIndexIteratorT& next() { if(m_atFront || (m_startPos == m_endPos)) { m_currentPos = m_startPos; m_atFront = false; } else { ++m_currentPos; } return *this; } inline void toBack() { m_currentPos = m_endPos; m_atFront = false; } inline bool hasPrevious() const { return m_currentPos > m_startPos; } inline CT_CloudIndexIteratorT& previous() { if(m_currentPos > m_startPos) { --m_currentPos; } return *this; } inline CT_CloudIndexIteratorT& jump(size_t n) { if(m_startPos == m_endPos) return *this; // if at front if(m_atFront) { if(n != 0) { m_currentPos = m_startPos+(n-1); // go to start+n-1 m_atFront = false; } // if over the end if(m_currentPos > (m_endPos-1)) m_currentPos = (m_endPos-1); // go to end // else if we jump over the end } else if((m_currentPos+n) >= (m_endPos-1)) { m_currentPos = (m_endPos-1); // go to end m_atFront = false; } else { m_currentPos += n; m_atFront = false; } return *this; } inline size_t cIndex() { return (*m_index)[m_currentPos]; } const T& cT() const { return operator*(); } T& cT() { return operator*(); } const T& operator*() const { return PS_REPOSITORY->globalCloud()->constTAt(cIndex()); } const T* operator->() const { return &(operator*()); } T& operator*() { return PS_REPOSITORY->globalCloud()->tAt(cIndex()); } T* operator->() { return &(operator*()); } private: CT_AbstractCloudIndexT *m_index; size_t m_startPos; size_t m_currentPos; size_t m_endPos; bool m_atFront; }; #endif // CT_CLOUDINDEXITERATORT_H