ders::cycl_buf Class Reference

Cyclic buffer. More...

#include <cycl_buf.hpp>

List of all members.

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)


Detailed Description

Cyclic buffer.

Definition at line 26 of file cycl_buf.hpp.


Constructor & Destructor Documentation

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]

Definition at line 36 of file cycl_buf.hpp.

00036 { operator delete(buf); }


Member Function Documentation

int ders::cycl_buf::size (  )  const [inline]

Definition at line 38 of file cycl_buf.hpp.

00038 { return cnt; }

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  ) 

Definition at line 67 of file cycl_buf.cpp.

00068 {
00069  if (cnt+cb.cnt>sz) reserve(cnt+cb.cnt);
00070 
00071  if (cb.fst+cb.cnt<=cb.sz) put(cb.buf+cb.fst, cb.cnt);
00072  else {
00073       put(cb.buf+cb.fst, cb.sz-cb.fst);
00074       put(cb.buf, cb.fst+cb.cnt-cb.sz);
00075  }
00076 }

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  ) 

Definition at line 97 of file cycl_buf.cpp.

00098 {
00099  if (cnt+cb.cnt>sz) reserve(cnt+cb.cnt);
00100 
00101  if (cb.fst+cb.cnt<=cb.sz) put_front(cb.buf+cb.fst, cb.cnt);
00102  else {
00103       put_front(cb.buf, cb.fst+cb.cnt-cb.sz);
00104       put_front(cb.buf+cb.fst, cb.sz-cb.fst);
00105  }
00106 }

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 }


The documentation for this class was generated from the following files:

Generated on Tue Dec 8 11:35:32 2009 for derslib by  doxygen 1.5.5