#ifndef _ZLIST_HPP_ #define _ZLIST_HPP_ #include #include template struct zlist { std::array*buf; uint32_t bpos; uint32_t epos; uint32_t len; zlist() :buf(nullptr) ,bpos(0) ,epos(0) ,len(1) { buf=new std::array(); len = 1; while(len < SIZE) len <<= 1; } ~zlist() { delete buf; } void clear() { bpos=epos; } int index(uint32_t p)const { int l = p&(len-1); return l - len + SIZE; //return p&(SIZE-1); } void push(const T&o) { if(size()>=SIZE) skip(1); buf->at(index(epos++))=o; } void rpush(const T&o) { if(size()>=SIZE) rskip(1); buf->at(index(--bpos))=o; } T& grow() { if(size()>=SIZE) skip(1); ++epos; return rat(0); } T& rgrow() { if(size()>=SIZE) rskip(1); --bpos; return at(0); } template int find_if(const Pred&p)const { for(int i=0,len=size();i int find_last_if(const Pred&p)const { for(int i=0,len=size();i void skip_if(const Pred&p) { while(!empty()) { if(!p(at(0))) break; skip(1); } } template void for_each(const Pred&p) { for(unsigned i=bpos;iat(index(i))); } void skip(int count) { assert(count<=(int)size()); bpos+=count; } void rskip(int count) { assert(count<=(int)size()); epos-=count; } bool empty()const { return epos==bpos; } int size()const { return epos-bpos; } T&at(int i) { assert(i<(int)size()); return buf->at(index(bpos+i)); } const T&at(int i) const { assert(i<(int)size()); return buf->at(index(bpos+i)); } T&rat(int i) { assert(i<(int)size()); return buf->at(index(epos-i-1)); } const T&rat(int i) const { assert(i<(int)size()); return buf->at(index(epos-i-1)); } T&operator[](int i) { return at(i); } const T&operator[](int i) const { return at(i); } T&operator()(int i) { return rat(i); } const T&operator()(int i) const { return rat(i); } }; #endif