📄 vector.h
字号:
// file: $isip/class/dstr/Vector/Vector.h// version: $Id: Vector.h,v 1.97 2002/08/18 02:07:47 gao Exp $//// make sure definitions are made only once//#ifndef ISIP_VECTOR#define ISIP_VECTOR// isip include files//#ifndef ISIP_DSTR_BASE#include <DstrBase.h>#endif#ifndef ISIP_DOUBLE_LINKED_LIST#include <DoubleLinkedList.h>#endif#ifndef ISIP_LONG#include <Long.h>#endif#ifndef ISIP_STRING#include <String.h>#endif#ifndef ISIP_CHAR#include <Char.h>#endif#ifndef ISIP_CONSOLE#include <Console.h>#endif// forward class definitions//template<class TObject> class VectorDiagnose;// Vector: a generic vector template class. it is simply a container// class to hold an array of objects, such as Char, String, etc.//template<class TObject>class Vector : public DstrBase { //--------------------------------------------------------------------------- // // public constants // //---------------------------------------------------------------------------public: // define the class name // static const String CLASS_NAME; //---------------------------------------- // // i/o related constants // //---------------------------------------- static const String DEF_PARAM; static const String BLOCK_START_STR; // L"{\n"; static const String BLOCK_DELIM_STR; // L"}, {\n"; static const String BLOCK_END_STR; // L"}"; static const String BLOCK_TERM_STR; // L";\n"; // define how the skip table will be written in binary mode // static const long SKIP_TABLE_GROUP = Sof::SKIP_TABLE_GROUP; static const long SKIP_TABLE_SKIP = 100; static const long SKIP_TABLE_LENGTH = SKIP_TABLE_GROUP * sizeof(int32); // i/o data buffering lengths // static const long TEXT_WRITE_SIZE = 512; //---------------------------------------- // // default values and arguments // //---------------------------------------- // default values // static const long DEF_LENGTH = 0; static const long DEF_END_POS = -1; // default arguments to methods // //---------------------------------------- // // error codes // //---------------------------------------- static const long ERR = 40900; static const long ERR_WMODE = 40901; //--------------------------------------------------------------------------- // // protected data // //---------------------------------------------------------------------------protected: // vector elements // TObject* v_d; // number of elements of this vector // Long length_d; // the maximum number of elements // Long capacity_d; // 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 VectorDiagnose.h in order to avoid issues // related to preprocessing of the diagnose code. // static const String& name(); // method: setDebug // static boolean setDebug(Integral::DEBUG level) { debug_level_d = level; return true; } // other debug methods // boolean debug(const unichar* message) const; // destructor/constructor(s) // ~Vector(); Vector(long length = DEF_LENGTH); Vector(const Vector<TObject>& copy_vector); // assign methods // boolean assign(const Vector<TObject>& copy_vector); // method: operator= // Vector<TObject>& operator=(const Vector<TObject>& arg) { assign(arg); return *this; } // equality methods // boolean eq(const Vector<TObject>& compare_vector) const; // 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 for 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; // 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: // extensions to required methods // //--------------------------------------------------------------------------- // assign methods: // boolean assign(TObject& arg); // method: ne // boolean ne(const Vector<TObject>& arg) const { return (!eq(arg)); } // partial read methods: // these methods must work together to complete the read // boolean readStart(Sof& sof, const String& pname = DEF_PARAM, long size = SofParser::FULL_OBJECT, boolean param = true, boolean nested = true); long readPartialData(Sof& sof, long start_pos, long num_elem, const String& pname = DEF_PARAM, long size = SofParser::FULL_OBJECT, boolean param = true, boolean nested = false); // method: readTerminate // boolean readTerminate(Sof& sof) { return sof.stopPartialRead(); } // partial write methods: // these methods must work together to complete the write // boolean writeStart(Sof& sof, const String& pname = DEF_PARAM) const; long writePartialData(Sof& sof, long start_pos, long num_elem) const; boolean writeTerminate(Sof& sof, const String& pname = DEF_PARAM) const; //--------------------------------------------------------------------------- // // class-specific public methods: // item access methods // //-------------------------------------------------------------------------- // method: operator() // TObject& operator()(long index) { // do range checking in this construct so it will be optimized out // ISIP_FULL_CHECK(if ((index < 0) || (index >= (long)length_d)) { Error::handle(name(), L"operator()", Error::BOUNDS, __FILE__, __LINE__); static TObject ptr; return ptr; }) return v_d[index]; } // method: operator() // const TObject& operator()(long index) const { // do range checking in this construct so it will be optimized out // ISIP_FULL_CHECK(if ((index < 0) || (index >= (long)length_d)) { Error::handle(name(), L"operator()", Error::BOUNDS, __FILE__, __LINE__); static TObject ptr; return ptr; }) return v_d[index]; } //--------------------------------------------------------------------------- // // class-specific public methods: // size-related methods // //-------------------------------------------------------------------------- // method: length // long length() const { return (long)length_d; } // method: getCapacity // long getCapacity() const { return (long)capacity_d; } // method to get vector size // long getVectorSize(long offset_a, Sof& sof_a); long getVectorSize(Sof& sof, long tag, const String& name = name(), const String& pname = DEF_PARAM, long size = SofParser::FULL_OBJECT, boolean param = true, boolean nested = true); // resize methods // boolean setLength(long length, boolean preserve_values = true); boolean setCapacity(long capacity, boolean preserve_values = true); //--------------------------------------------------------------------------- // // class-specific public methods: // data manipulation methods // //-------------------------------------------------------------------------- // positioning methods // boolean move(const Vector<TObject>& source_vector, long num_elements, long source_offset, long dest_offset); boolean shift(const Vector<TObject>& source_vector, long delta); boolean shift(long delta); // concatenation methods // boolean concat(const Vector<TObject>& v2); boolean concat(const Vector<TObject>& v1, const Vector<TObject>& v2); boolean concat(const TObject& obj); // range operation methods // boolean deleteRange(const Vector<TObject>& arg, long offset, long num_elements); boolean deleteRange(long offset, long num_elements); boolean setRange(long offset, long num_elements, const TObject& value); // sort methods // boolean sort(Integral::ORDER sort_order = Integral::ASCENDING, SORT_ALGO = DEF_SORT_ALGO); //--------------------------------------------------------------------------- // // class-specific public methods: // item location and containment methods // //-------------------------------------------------------------------------- // element location methods // long first(const TObject& value, long start_pos = Integral::NO_POS) const; long last(const TObject& value, long end_pos = Integral::NO_POS) const; // method: contains // boolean contains(long& index, const TObject* value) const { index = Integral::NO_POS; return ((value != (TObject*)NULL) && ((index = first(*value)) != Integral::NO_POS)); } // method: contains // boolean contains(const TObject* value) const { long index; return contains(index, value); } //--------------------------------------------------------------------------- // // class-specific public methods: // apply methods // //-------------------------------------------------------------------------- // method: apply // boolean apply(boolean (TObject::*method)(), Vector<TObject>& arg) { assign(arg); return apply(method); } // other apply methods: // methods to apply an external function to each element in the vector // boolean apply(boolean (TObject::*method)()); //--------------------------------------------------------------------------- // // private methods // //---------------------------------------------------------------------------private: // sort methods // boolean randQuickSort(Integral::ORDER sort_order); boolean insertionSort(Integral::ORDER sort_order); boolean clearBinPos(int32* bin_pos) const; // friend class // template <class TObject_diagnose> friend class VectorDiagnose; }; //-----------------------------------------------------------------------------//// 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 Vector<TObject>::CLASS_NAME(L"Vector");// constants: required constants for i/o methods//template <class TObject>const String Vector<TObject>::DEF_PARAM(L"values");template <class TObject>const String Vector<TObject>::BLOCK_START_STR(L"{\n");template <class TObject>const String Vector<TObject>::BLOCK_DELIM_STR(L"}, {\n");template <class TObject>const String Vector<TObject>::BLOCK_END_STR(L"}");template <class TObject>const String Vector<TObject>::BLOCK_TERM_STR(L";\n");// static instantiations: debug level and memory manager//template <class TObject>Integral::DEBUG Vector<TObject>::debug_level_d = Integral::NONE;template <class TObject>MemoryManager Vector<TObject>::mgr_d(sizeof(Vector<TObject>), CLASS_NAME);// below are all the methods for the Vector 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& Vector<TObject>::name() { // create the static name string for this class and return it // static String cname; 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 Vector<TObject>::debug(const unichar* message_a) const { // declare temporary strings to hold class data // String output; String value; String param;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -