Update of /cvsroot/pclasses/pclasses2/include/pclasses
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16104/include/pclasses
Modified Files:
CircularQueue.h
Log Message:
- Added methods to CircularQueue for efficient buffering/unbuffering of arrays
Index: CircularQueue.h
===================================================================
RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/CircularQueue.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- CircularQueue.h 3 Jun 2005 09:50:13 -0000 1.2
+++ CircularQueue.h 3 Jul 2005 23:45:03 -0000 1.3
@@ -23,6 +23,7 @@
#include <pclasses/BasicTypes.h>
#include <pclasses/Algorithm.h>
+#include <iostream>
namespace P {
@@ -114,17 +115,41 @@
inline bool full() const throw()
{ return ((_in + 1) % _size == _out); }
+ inline size_t size() const throw()
+ {
+ if(_in >= _out)
+ return (_in - _out);
+ return (_size - _out) + _in;
+ }
+
void push(const Type& val)
{
if(!full())
{
- copy_construct(_buffer + (_in % _size), &val, 1);
+ //copy_construct(_buffer + (_in % _size), &val, 1);
+ copy_construct(_buffer + _in, &val, 1);
_in = (_in + 1) % _size;
return;
}
throw OverrunError("Queue overrun", P_SOURCEINFO);
}
+ void push(const Type* buffer, size_t count)
+ {
+ size_t cap = capacity();
+ if(cap - size() >= count)
+ {
+ //size_t withoutWrap = cap - _in;
+ size_t withoutWrap = _size - _in;
+ copy_construct(_buffer + _in, buffer, min(withoutWrap,count));
+ if(count > withoutWrap)
+ copy_construct(_buffer, buffer + withoutWrap, count - withoutWrap);
+ _in = (_in + count) % _size;
+ return;
+ }
+ throw OverrunError("Queue overrun", P_SOURCEINFO);
+ }
+
//! Remove next item from queue
/*!
Removes the next item from queue.
@@ -142,6 +167,35 @@
throw UnderrunError("Queue underrun", P_SOURCEINFO);
}
+ void pop(size_t count) throw(UnderrunError)
+ {
+ if(size() >= count)
+ {
+ //size_t withoutWrap = capacity() - _out;
+ size_t withoutWrap = _size - _out;
+ destruct(_buffer + _out, min(withoutWrap,count));
+ if(count > withoutWrap)
+ destruct(_buffer, count - withoutWrap);
+ _out = (_out + count) % _size;
+ return;
+ }
+ throw UnderrunError("Queue underrun", P_SOURCEINFO);
+ }
+
+ void get(Type* buffer, size_t count) throw(UnderrunError)
+ {
+ if(size() >= count)
+ {
+ //size_t withoutWrap = capacity() - _out;
+ size_t withoutWrap = _size - _out;
+ copy(buffer, _buffer + _out, min(withoutWrap,count));
+ if(count > withoutWrap)
+ copy(buffer + withoutWrap, _buffer, count - withoutWrap);
+ return;
+ }
+ throw UnderrunError("Queue underrun", P_SOURCEINFO);
+ }
+
//! Returns a reference to the front item
/*!
Returns a reference to the front item in the queue, which is the next item
|