Commit 77fd8250 authored by Ivan Donchevskiy's avatar Ivan Donchevskiy

Iterator for CycleStorage

parent 44ffcd23
......@@ -147,6 +147,57 @@ class TableBlockStorage
class CycleStorage
{
public:
class CycleStorageIterator
{
public:
typedef CycleStorageIterator Self;
CycleStorageIterator():
str(NULL), cs(NULL), current(0) {}
CycleStorageIterator(CycleStorage* cstor)
{
cs = cstor;
current = 0;
}
CycleStorageIterator(CycleStorage* cstor, int num)
{
cs = cstor;
if( num<0 || num>=cs->getSize() ) current = 0;
current = num;
}
void* operator *() const
{
return str;
}
Self& operator++();
Self operator++(int);
Self& operator--();
Self operator--(int);
inline bool operator==(const Self& other) const
{
if( memcmp(str, other.str, cs->getInfSize())==0 )
return true;
return false;
}
inline bool operator!=(const Self& other) const
{
if( memcmp(str, other.str, cs->getInfSize())==0 )
return false;
return true;
}
private:
void* str;
CycleStorage* cs;
int current;
};
typedef CycleStorageIterator iterator;
/*! Конструктор по умолчанию не открывает и не создает нового журнала */
CycleStorage();
......@@ -187,7 +238,7 @@ class CycleStorage
/*! Изменение размера журнала (количества записей в нем) */
bool setSize(int count);
inline int getByteSize() { return (size*full_size + sizeof(CycleStorageAttr)); }
inline int getSize(){ return size; }
inline int getInfSize(){ return inf_size; }
......@@ -195,6 +246,19 @@ class CycleStorage
bool checkAttr(int inf_sz, int inf_count, int seek);
inline int getHead(){ return head; }
inline int getTail(){ return tail; }
iterator begin()
{
return iterator(this);
}
iterator end()
{
return iterator(this,this->getTail());
}
protected:
FILE *file;
int inf_size;
......
......@@ -490,3 +490,41 @@ bool CycleStorage::setSize(int count)
return true;
}
/*! Некоторые операции для итератора */
CycleStorage::iterator& CycleStorage::iterator::operator++()
{
if( current>=cs->getSize() )
throw "Trying to perform operator ++ at the end of collection";
current++;
cs->readRow(current,str);
return *this;
}
CycleStorage::iterator CycleStorage::iterator::operator++(int)
{
Self temp = *this;
if(current>=cs->getSize())
throw "Trying to perform operator ++ at the end of collection";
current++;
cs->readRow(current,str);
return temp;
}
CycleStorage::iterator& CycleStorage::iterator::operator--()
{
if(current<=0)
throw "Trying to perform operator -- at the begining of collection";
current--;
cs->readRow(current,str);
return *this;
}
CycleStorage::iterator CycleStorage::iterator::operator--(int)
{
Self temp = *this;
if(current<=0)
throw "Trying to perform operator -- at the begining of collection";
current--;
cs->readRow(current,str);
return temp;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment