#include <cycl_buf.hpp>
Public Member Functions | |
cycl_buf (int rsrv) | |
~cycl_buf () | |
int | size () const |
void | reserve (int n) |
void | put (const void *ptr, int n) |
void | put (const cycl_buf &cb) |
void | put_front (const void *ptr, int n) |
void | put_front (const cycl_buf &cb) |
void | get (void *ptr, int n) |
Definition at line 26 of file cycl_buf.hpp.
ders::cycl_buf::cycl_buf | ( | int | rsrv | ) |
Definition at line 21 of file cycl_buf.cpp.
00022 { 00023 assert(rsrv>0); 00024 sz=rsrv; 00025 buf=static_cast<char*>(operator new(sz)); 00026 fst=0; 00027 cnt=0; 00028 }
ders::cycl_buf::~cycl_buf | ( | ) | [inline] |
int ders::cycl_buf::size | ( | ) | const [inline] |
void ders::cycl_buf::reserve | ( | int | n | ) |
Definition at line 30 of file cycl_buf.cpp.
00031 { 00032 assert(n>=0); 00033 int nsz=cnt+n; 00034 if (nsz<=sz) return; 00035 00036 if (nsz<sz*2) nsz=sz*2; 00037 char* nbuf=static_cast<char*>(operator new(nsz)); 00038 00039 int n1=sz-fst; 00040 if (cnt<=n1) memcpy(nbuf, buf+fst, cnt); 00041 else { 00042 memcpy(nbuf, buf+fst, n1); 00043 memcpy(nbuf+n1, buf, cnt-n1); 00044 } 00045 00046 operator delete(buf); 00047 buf=nbuf; 00048 sz=nsz; 00049 fst=0; 00050 }
void ders::cycl_buf::put | ( | const void * | ptr, | |
int | n | |||
) |
Definition at line 52 of file cycl_buf.cpp.
00053 { 00054 assert(n>=0); 00055 if (cnt+n>sz) reserve(cnt+n); 00056 00057 int pos=(fst+cnt)%sz, n1=sz-pos; 00058 if (n<=n1) memcpy(buf+pos, ptr, n); 00059 else { 00060 memcpy(buf+pos, ptr, n1); 00061 memcpy(buf, static_cast<const char*>(ptr)+n1, n-n1); 00062 } 00063 00064 cnt+=n; 00065 }
void ders::cycl_buf::put | ( | const cycl_buf & | cb | ) |
void ders::cycl_buf::put_front | ( | const void * | ptr, | |
int | n | |||
) |
Definition at line 78 of file cycl_buf.cpp.
00079 { 00080 assert(n>=0); 00081 if (cnt+n>sz) reserve(cnt+n); 00082 00083 int nfst=fst-n; 00084 if (nfst>=0) { 00085 fst=nfst; 00086 memcpy(buf+fst, ptr, n); 00087 } 00088 else { 00089 fst=sz+nfst; 00090 memcpy(buf+fst, ptr, -nfst); 00091 memcpy(buf, static_cast<const char*>(ptr)-nfst, n+nfst); 00092 } 00093 00094 cnt+=n; 00095 }
void ders::cycl_buf::put_front | ( | const cycl_buf & | cb | ) |
void ders::cycl_buf::get | ( | void * | ptr, | |
int | n | |||
) |
Definition at line 108 of file cycl_buf.cpp.
00109 { 00110 assert(n<=cnt && n>=0); 00111 00112 int n1=sz-fst; 00113 if (n<=n1) memcpy(ptr, buf+fst, n); 00114 else { 00115 memcpy(ptr, buf+fst, n1); 00116 memcpy(static_cast<char*>(ptr)+n1, buf, n-n1); 00117 } 00118 00119 fst=(fst+n)%sz; 00120 cnt-=n; 00121 }