📄 doublelinkedlist.h
字号:
// file: $isip/class/dstr/DoubleLinkedList/DoubleLinkedList.h// version: $Id: DoubleLinkedList.h,v 1.89 2001/11/16 14:10:28 gao Exp $//// make sure definitions are made only once//#ifndef ISIP_DOUBLE_LINKED_LIST#define ISIP_DOUBLE_LINKED_LIST// include files//#include <typeinfo>// isip include files//#ifndef ISIP_DSTR_BASE#include <DstrBase.h>#endif#ifndef ISIP_DOUBLE_LINKED_NODE#include <DoubleLinkedNode.h>#endif#ifndef ISIP_VECTOR_LONG#include <VectorLong.h>#endif#ifndef ISIP_STRING#include <String.h>#endif#ifndef ISIP_CHAR#include <Char.h>#endif#ifndef ISIP_LONG#include <Long.h>#endif#ifndef ISIP_CONSOLE#include <Console.h>#endif#ifndef ISIP_STACK#include <Stack.h>#endif// forward class definitions//template<class TObject> class Stack;template<class TObject> class DoubleLinkedListDiagnose;// DoubleLinkedList: a generic doubly-linked list template class. the// user may choose whether or not the DoubleLinkedList is used in// USER-allocated or SYSTEM-allocated mode. in SYSTEM-allocated mode,// the DoubleLinkedList class is responsible for managing the memory// for the objects it contains. this implies that a copy is made each// time an object is inserted to the list. in USER-allocation mode the// user is responsible for allocation and deletion of the objects that// are stored in the DoubleLinkedList. in this mode, pointers to the// external objects are stored, i.e. a copy is not made. the mode// chosen is dependent on the user's need to trade-off speed versus// complexity of object management.//template<class TObject>class DoubleLinkedList : public DstrBase { //--------------------------------------------------------------------------- // // public constants // //---------------------------------------------------------------------------public: // define the class name // static const String CLASS_NAME; //---------------------------------------- // // i/o related constants // //---------------------------------------- static const String DEF_PARAM; //---------------------------------------- // // default values and arguments // //---------------------------------------- // default values // // default arguments to methods // //---------------------------------------- // // error codes // //---------------------------------------- static const long ERR = 40400; //--------------------------------------------------------------------------- // // protected data // //---------------------------------------------------------------------------protected: // define the NODE object // typedef DoubleLinkedNode<TObject> NODE; // pointers to the first node, last node and current node // NODE* first_d; NODE* last_d; NODE* curr_d; // a pointer to a node the user marks // NODE* mark_d; // the number of nodes in the list // Long length_d; // the allocation mode // ALLOCATION alloc_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 the DoubleLinkedListDiagnose.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 // ~DoubleLinkedList() { clear(Integral::RESET); } // default constructor // DoubleLinkedList(ALLOCATION alloc = DEF_ALLOCATION); // copy constructor // DoubleLinkedList(const DoubleLinkedList<TObject>& copy_list); // assign methods // boolean assign(const DoubleLinkedList<TObject>& copy_list); // method: operator= // DoubleLinkedList<TObject>& operator=(const DoubleLinkedList<TObject>& arg) { assign(arg); return *this; } // equality methods // boolean eq(const DoubleLinkedList<TObject>& compare_list) const; // i/o methods: // long sofSize() 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 // 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 = false); 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: // overload operators and extensions to required methods // //--------------------------------------------------------------------------- // overloaded operators // TObject& operator() (long index); const TObject& operator() (long index) const; // method: ne // boolean ne(const DoubleLinkedList<TObject>& compare_list) const { return (!eq(compare_list)); } //--------------------------------------------------------------------------- // // class-specific public methods: // positioning methods // //--------------------------------------------------------------------------- // method: gotoFirst // boolean gotoFirst() { return ((curr_d = first_d) != (NODE*)NULL); } // method: gotoLast // boolean gotoLast() { return ((curr_d = last_d) != (NODE*)NULL); } // other positioning methods: // these methods move to the specified node and return a boolean indicating // whether or not the node is valid // boolean gotoNext(); boolean gotoPrev(); boolean gotoMark(); // method: gotoPosition // boolean gotoPosition(long number) { NODE* node = getNode(number); if (node == (NODE*)NULL) { return false; } curr_d = node; return true; } // method to find the index of the current element // long getPosition() const; //--------------------------------------------------------------------------- // // class-specific public methods: // marking methods // //--------------------------------------------------------------------------- // method: markIsSet // boolean markIsSet() const { return (mark_d != (NODE*)NULL); } // method: isMarkedElement // boolean isMarkedElement() const { return (markIsSet() && (curr_d == mark_d)); } // method: clearMark // boolean clearMark() { mark_d = (NODE*)NULL; return true; } // other marked node methods: // methods to check, set and clear the marked node // boolean setMark(); //--------------------------------------------------------------------------- // // class-specific public methods: // access methods // //--------------------------------------------------------------------------- // method: getFirst // TObject* getFirst() { if (first_d != (NODE*)NULL) { return first_d->getItem(); } return (TObject*)NULL; } // method: getFirst // const TObject* getFirst() const { if (first_d != (NODE*)NULL) { return first_d->getItem(); } return (TObject*)NULL; } // method: getLast // TObject* getLast() { if (last_d != (NODE*)NULL) { return last_d->getItem(); } return (TObject*)NULL; } // method: getLast // const TObject* getLast() const { if (last_d != (NODE*)NULL) { return last_d->getItem(); } return (TObject*)NULL; } // method: getNext // TObject* getNext() { if ((curr_d != (NODE*)NULL) && (curr_d->getNext() != (NODE*)NULL)) { return curr_d->getNext()->getItem(); } return (TObject*)NULL; } // method: getNext // const TObject* getNext() const { if ((curr_d != (NODE*)NULL) && (curr_d->getNext() != (NODE*)NULL)) { return curr_d->getNext()->getItem(); } return (TObject*)NULL; } // method: getPrev // TObject* getPrev() { if ((curr_d != (NODE*)NULL) && (curr_d->getPrev() != (NODE*)NULL)) { return curr_d->getPrev()->getItem(); } return (TObject*)NULL; } // method: getPrev // const TObject* getPrev() const { if ((curr_d != (NODE*)NULL) && (curr_d->getPrev() != (NODE*)NULL)) { return curr_d->getPrev()->getItem(); } return (TObject*)NULL; } // method: getMark // TObject* getMark() { if (mark_d != (NODE*)NULL) { return mark_d->getItem(); } return (TObject*)NULL; } // method: getMark // const TObject* getMark() const { if (mark_d != (NODE*)NULL) { return mark_d->getItem(); } return (TObject*)NULL; } // method: getCurr // TObject* getCurr() { if (curr_d != (NODE*)NULL) { return curr_d->getItem(); } return (TObject*)NULL; } // method: getCurr // const TObject* getCurr() const { if (curr_d != (NODE*)NULL) { return curr_d->getItem(); } return (TObject*)NULL; } //--------------------------------------------------------------------------- // // class-specific public methods: // insert and remove methods // //--------------------------------------------------------------------------- // insert and remove methods: // the current pointer is moved to the node inserted. // boolean insert(TObject* item); boolean insert(DoubleLinkedList<TObject>& ilist); boolean remove(TObject*& item); boolean remove(); boolean insertFirst(TObject* item); boolean insertFirst(DoubleLinkedList<TObject>& ilist); boolean removeFirst(TObject*& item); boolean removeFirst(); boolean insertLast(TObject* item); boolean insertLast(DoubleLinkedList<TObject>& ilist); boolean removeLast(TObject*& item); boolean removeLast(); //--------------------------------------------------------------------------- // // class-specific public methods: // property methods // //--------------------------------------------------------------------------- // method: isEmpty // boolean isEmpty() const { return ((long)length_d == 0); } // method: isFirst // boolean isFirst() const { return (!isEmpty() && (curr_d == first_d)); } // method: isLast // boolean isLast() const { return (!isEmpty() && (curr_d == last_d)); } // method: length // long length() const { return length_d; } // item containment methods: // methods to determine if the specified object is contained in the // list. the find method leaves the list positioned at the first // found element, the contains method leaves the list unchanged. // boolean find(const TObject* item); boolean contains(const TObject* item) const; //--------------------------------------------------------------------------- // // class-specific public methods: // ordering methods // //--------------------------------------------------------------------------- // sort and reverse methods: // boolean sort(Integral::ORDER sort_order = Integral::ASCENDING, SORT_ALGO = DEF_SORT_ALGO); boolean reverse(); // swap methods: // swap two nodes in the list given the node indices // boolean swap(long i, long j); //--------------------------------------------------------------------------- // // class-specific public methods: // apply methods // //--------------------------------------------------------------------------- // apply methods: // apply an external function to each element in the list methods // boolean apply(boolean (TObject::*method)()); boolean apply(boolean (TObject::*method)(), DoubleLinkedList<TObject>& arg); //--------------------------------------------------------------------------- // // class-specific public methods: // allocation mode methods // //--------------------------------------------------------------------------- // method: getAllocationMode // ALLOCATION getAllocationMode() const { return alloc_d; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -