完成IndexedList的迭代器,并测试完成
This commit is contained in:
parent
4104f1cb75
commit
f150c70460
@ -40,6 +40,23 @@ namespace hgl
|
|||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const DataArray<T> &GetRawData()const{return data_array;}
|
||||||
|
const DataArray<I> &GetRawIndex()const{return data_index;}
|
||||||
|
|
||||||
|
T &operator[](int32 index)
|
||||||
|
{
|
||||||
|
if ( index<0||index>=data_index.GetCount() )
|
||||||
|
return data_array[0];
|
||||||
|
return data_array[data_index[index]];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T &operator[](int32 index)const
|
||||||
|
{
|
||||||
|
if ( index<0||index>=data_index.GetCount() )
|
||||||
|
return data_array[0];
|
||||||
|
return data_array[data_index[index]];
|
||||||
|
}
|
||||||
|
|
||||||
public: // 迭代器
|
public: // 迭代器
|
||||||
|
|
||||||
class Iterator
|
class Iterator
|
||||||
@ -56,11 +73,11 @@ namespace hgl
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Iterator(const IndexedList<T, I>* lst, int32 idx):list(lst),current_index(idx){}
|
Iterator(IndexedList<T, I>* lst, int32 idx):list(lst),current_index(idx){}
|
||||||
|
|
||||||
T& operator*() const
|
T& operator*()
|
||||||
{
|
{
|
||||||
return list->data_array[list->data_index[current_index]];
|
return (*list)[current_index];
|
||||||
}
|
}
|
||||||
|
|
||||||
Iterator &operator++(){current_index++;return *this;}
|
Iterator &operator++(){current_index++;return *this;}
|
||||||
@ -80,20 +97,53 @@ namespace hgl
|
|||||||
}
|
}
|
||||||
};//class Iterator
|
};//class Iterator
|
||||||
|
|
||||||
Iterator begin() const
|
Iterator begin (){return Iterator(this,0);}
|
||||||
|
Iterator end (){return Iterator(this,data_index.GetCount());}
|
||||||
|
Iterator last (){return Iterator(this,data_index.GetCount()-1);}
|
||||||
|
|
||||||
|
public: // 只读迭代器
|
||||||
|
|
||||||
|
class ConstIterator
|
||||||
{
|
{
|
||||||
return Iterator(this,0);
|
private:
|
||||||
|
|
||||||
|
const IndexedList<T,I> *list;
|
||||||
|
int32 current_index;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
using pointer = T*;
|
||||||
|
using reference = T&;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
ConstIterator(const IndexedList<T, I>* lst, int32 idx):list(lst),current_index(idx){}
|
||||||
|
|
||||||
|
const T& operator*()const
|
||||||
|
{
|
||||||
|
return (*list)[current_index];
|
||||||
}
|
}
|
||||||
|
|
||||||
Iterator end() const
|
ConstIterator &operator++(){current_index++;return *this;}
|
||||||
|
ConstIterator operator++(int){Iterator tmp=*this;++(*this);return tmp;}
|
||||||
|
|
||||||
|
ConstIterator &operator--(){current_index--;return *this;}
|
||||||
|
ConstIterator operator--(int){Iterator tmp=*this;--(*this);return tmp;}
|
||||||
|
|
||||||
|
bool operator==(const ConstIterator &other) const
|
||||||
{
|
{
|
||||||
return Iterator(this,data_index.GetCount());
|
return current_index==other.current_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
Iterator last() const
|
bool operator!=(const ConstIterator &other) const
|
||||||
{
|
{
|
||||||
return Iterator(this,data_index.GetCount()-1);
|
return current_index!=other.current_index;
|
||||||
}
|
}
|
||||||
|
};//class ConstIterator
|
||||||
|
|
||||||
|
ConstIterator begin ()const{return ConstIterator(this,0);}
|
||||||
|
ConstIterator end ()const{return ConstIterator(this,data_index.GetCount());}
|
||||||
|
ConstIterator last ()const{return ConstIterator(this,data_index.GetCount()-1);}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user