📄 circularbuffer.h
字号:
// file: $isip/class/dstr/CircularBuffer/CircularBuffer.h// version: $Id: CircularBuffer.h,v 1.49 2001/11/12 19:30:32 gao Exp $//// make sure definitions are only made once//#ifndef ISIP_CIRCULAR_BUFFER#define ISIP_CIRCULAR_BUFFER// isip include files//#ifndef ISIP_VECTOR#include <Vector.h>#endif#ifndef ISIP_FLOAT#include <Float.h>#endif#ifndef ISIP_CONSOLE#include <Console.h>#endif#ifndef ISIP_MEMORY_MANAGER#include <MemoryManager.h>#endif// CircularBuffer: this is a generic circular buffer of objects. this// class, unlike most other dstr classes, only supports the SYSTEM// memory management mode (see DstrBase for a more detailed explanation).// the class is implemented using read, write and// current index pointers, so users can easily keep track of the amount of// available data in the buffer.//template<class TObject>class CircularBuffer { //--------------------------------------------------------------------------- // // public constants // //---------------------------------------------------------------------------public: // define the class name // static const String CLASS_NAME; //---------------------------------------- // // i/o related constants // //---------------------------------------- static const String DEF_PARAM; static const String PARAM_VALUES; static const String PARAM_CAPACITY; static const String PARAM_READ; static const String PARAM_WRITE; static const String PARAM_CURR; //---------------------------------------- // // default values and arguments // //---------------------------------------- // default values // static const long DEF_BUF_CAPACITY = 8192; static const long DEF_READ_IDX = 0; static const long DEF_WRITE_IDX = -1; static const long DEF_CURR_IDX = 0; // default arguments to methods // static const long DEF_OFFSET = 0; static const long DEF_NUM_READ = 1; //---------------------------------------- // // error codes // //---------------------------------------- static const long ERR = 41600; static const long ERR_FULL = 41601; //--------------------------------------------------------------------------- // // protected data // //---------------------------------------------------------------------------protected: // declare a vector of data objects // Vector<TObject> v_d; // declare the position indices // Long read_d; Long write_d; Long curr_d; // declare the debugging parameters // static Integral::DEBUG debug_level_d; // define the memory manager // static MemoryManager mgr_d; //--------------------------------------------------------------------------- // // required public methods // //---------------------------------------------------------------------------public: // static methods: // diagnose method is moved outside the class header file and // defined in CircularBufferDiagnose.h in order to avoid issues // related to preprocessing of the diagnose code. // static const String& name(); // method: setDebug // static boolean setDebug(Integral::DEBUG debug_level) { debug_level_d = debug_level; return true; } // other debug methods // boolean debug(const unichar* message) const; // method: destructor // ~CircularBuffer() {} // default constructor // CircularBuffer(long capacity = DEF_BUF_CAPACITY); // method: copy constructor // CircularBuffer(const CircularBuffer<TObject>& copy_cbuf) { assign(copy_cbuf); } // assign methods // boolean assign(const CircularBuffer<TObject>& copy_cbuf); // method: operator= // CircularBuffer<TObject>& operator=(const CircularBuffer<TObject>& arg) { assign(arg); return *this; } // method: read // boolean read(Sof& sof, long tag) { return read(sof, tag, name()); } // method: write // boolean write(Sof& sof, long tag) const { return write(sof, tag, name()); } // other i/o methods: // sofSize determines the amount of disk storage needed to store this // object the read and write methods are for full object i/o. the // readData and writeData methods handle i/o when this object is a // component of another object // long sofSize() const; boolean read(Sof& sof, long tag, const String& name); boolean write(Sof& sof, long tag, const String& name) const; boolean readData(Sof& sof, const String& pname = DEF_PARAM, long size = SofParser::FULL_OBJECT, boolean param = true, boolean nested = true); boolean writeData(Sof& sof, const String& pname = DEF_PARAM) const; // equality methods // boolean eq(const CircularBuffer<TObject>& compare_cbuf) const; // method: new // static void* operator new(size_t size) { return mgr_d.get(); } // method: new[] // static void* operator new[](size_t size) { return mgr_d.getBlock(size); } // method: delete // static void operator delete(void* ptr) { mgr_d.release(ptr); } // method: delete[] // static void operator delete[](void* ptr) { mgr_d.releaseBlock(ptr); } // method: setGrowSize // static boolean setGrowSize(long grow_size) { return mgr_d.setGrow(grow_size); } // other memory management methods // boolean clear(Integral::CMODE cmode = Integral::DEF_CMODE); //--------------------------------------------------------------------------- // // class-specific public methods: // operator overload methods // //--------------------------------------------------------------------------- // operator overload methods // TObject& operator() (long offset); const TObject& operator() (long offset) const; //--------------------------------------------------------------------------- // // class-specific public methods: // get methods // //--------------------------------------------------------------------------- // get element methods // boolean getElement(TObject& out, long offset = DEF_OFFSET) const; boolean getElement(Vector<TObject>& out, long num_elem, long offset = DEF_OFFSET) const; // get number of element methods // long getNumElements() const; long getNumForward() const; long getNumBackward() const; //--------------------------------------------------------------------------- // // class-specific public methods: // class property methods // //--------------------------------------------------------------------------- // status checking methods // boolean isFull() const; boolean isEmpty() const; // method: getCapacity // long getCapacity() const { return v_d.getCapacity(); } // method: setCapacity // note that for a circular buffer, we don't distiguish between the // capacity and the length, since the class is designed to manage // how much of the capacity is in use. // boolean setCapacity(long capacity, boolean preserve_values = true) { v_d.setLength(capacity, preserve_values); v_d.setCapacity(capacity, preserve_values); return true; } //--------------------------------------------------------------------------- // // class-specific public methods: // data manipulation methods // //--------------------------------------------------------------------------- // data manipulation methods // boolean append(const TObject& in); boolean append(const Vector<TObject>& in); boolean append(const Vector<TObject>& in, long num_elem, long invec_offset = DEF_OFFSET); boolean prepend(const TObject& in); boolean prepend(const Vector<TObject>& in); boolean prepend(const Vector<TObject>& in, long num_elem, long invec_offset = DEF_OFFSET); //--------------------------------------------------------------------------- // // class-specific public methods: // positioning methods // //--------------------------------------------------------------------------- // method: resetCurr // boolean resetCurr() { curr_d = read_d; return true; } // other positioning methods // boolean seekCurr(long offset); boolean setRead(long num_read = DEF_NUM_READ); //--------------------------------------------------------------------------- // // private methods // //---------------------------------------------------------------------------private:};//-----------------------------------------------------------------------------//// we define non-integral constants at the end of class definition for// templates (for non-templates these are defined in the default constructor)// //-----------------------------------------------------------------------------// constants: required constants such as the class name//template <class TObject>const String CircularBuffer<TObject>::CLASS_NAME(L"CircularBuffer");template <class TObject>const String CircularBuffer<TObject>::DEF_PARAM(L"");template <class TObject>const String CircularBuffer<TObject>::PARAM_VALUES(L"values");template <class TObject>const String CircularBuffer<TObject>::PARAM_CAPACITY(L"capacity");template <class TObject>const String CircularBuffer<TObject>::PARAM_READ(L"read");template <class TObject>const String CircularBuffer<TObject>::PARAM_WRITE(L"write");template <class TObject>const String CircularBuffer<TObject>::PARAM_CURR(L"curr");// static instantiations: debug level and memory manager//template <class TObject>Integral::DEBUG CircularBuffer<TObject>::debug_level_d = Integral::NONE;template <class TObject>MemoryManager CircularBuffer<TObject>::mgr_d(sizeof(CircularBuffer<TObject>), CLASS_NAME);// below are all the methods for the CircularBuffer template class// //----------------------------------------------------------------------//// required static methods////----------------------------------------------------------------------// method: name//// arguments: none//// return: a static String& containing the class name//// this method returns the class name//template<class TObject>const String& CircularBuffer<TObject>::name() { // create the static name string for this class and return it // static String cname(CLASS_NAME); cname.clear(); cname.concat(CLASS_NAME); cname.concat(L"<"); cname.concat(TObject::name()); cname.concat(L">"); // return the name // return cname;}//----------------------------------------------------------------------//// required debug methods////----------------------------------------------------------------------// method: debug//// arguments:// const unichar* message: (input) information message//// return: a boolean value indicating status//// this method dumps the contents of an object to the console// template<class TObject>boolean CircularBuffer<TObject>::debug(const unichar* message_a) const { // local variables // String output; String value; String param; // output the debug level // value.assign(debug_level_d); output.debugStr(name(), message_a, L"debug_level_d", value); Console::put(output); // output the number of valid elements // long num_elem = getNumElements(); value.assign(num_elem); output.debugStr(name(), message_a, L"number of elements", value); Console::put(output); // output number of elements before and after // num_elem = getNumForward(); value.assign(num_elem); output.debugStr(name(), message_a, L"number forward", value); Console::put(output); num_elem = getNumBackward(); value.assign(num_elem); output.debugStr(name(), message_a, L"number backward", value); Console::put(output); num_elem = getCapacity(); value.assign(num_elem); output.debugStr(name(), message_a, L"capacity", value); Console::put(output); num_elem = getNumElements(); if (num_elem == 0) { return true; } // increment the indentation level in the console // Console::increaseIndention(); // find the start number for the loop // long j = read_d; long capacity = getCapacity(); if (write_d < read_d) { j -= capacity; } // loop all the valid elements // for (; j <= (long)write_d; j++) { // get the physical index // long i = j; if (i < 0) { i += capacity; } // dump the values out // param.assign(L"v_d["); value.assign(i); param.concat(value); param.concat(L"]"); String extras; // also output the position information // if (i == read_d) { extras.concat(L", "); extras.concat(L"read_d"); } if (i == curr_d) { extras.concat(L", "); extras.concat(L"curr_d"); } if (i == write_d) { extras.concat(L", "); extras.concat(L"write_d"); } param.concat(extras); output.debugStr(name(), message_a, param); Console::put(output); // increase indention // Console::increaseIndention(); // call the debug method of the element // v_d(i).debug(L""); Console::decreaseIndention(); } // decrement the indention level in the console // Console::decreaseIndention(); // exit gracefully // return true;}//------------------------------------------------------------------------//// required destructor/constructor(s)////-----------------------------------------------------------------------// method: default constructor//// arguments:// long capacity: (input) the capacity of the CircularBuffer//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -