lists.h

Go to the documentation of this file.
00001 /*
00002  * lists.h
00003  *
00004  * List Container Classes
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
00025  * All Rights Reserved.
00026  *
00027  * Contributor(s): ______________________________________.
00028  *
00029  * $Revision: 19008 $
00030  * $Author: rjongbloed $
00031  * $Date: 2007-11-29 09:17:41 +0000 (Thu, 29 Nov 2007) $
00032  */
00033 
00034 #ifdef P_USE_PRAGMA
00035 #pragma interface
00036 #endif
00037 
00038 
00040 // PList container class
00041 
00042 struct PListElement
00043 {
00044     PListElement(PObject * theData);
00045     PListElement * prev;
00046     PListElement * next;
00047     PObject * data;
00048 };
00049 
00050 struct PListInfo
00051 {
00052     PListInfo() { head = tail = lastElement = NULL; }
00053     PListElement * head;
00054     PListElement * tail;
00055     PListElement * lastElement;
00056     PINDEX    lastIndex;
00057 };
00058 
00079 class PAbstractList : public PCollection
00080 {
00081   PCONTAINERINFO(PAbstractList, PCollection);
00082 
00083   public:
00091     PINLINE PAbstractList();
00093 
00094   // Overrides from class PObject
00122     virtual Comparison Compare(const PObject & obj) const;
00123 
00133     virtual PBoolean SetSize(
00134       PINDEX newSize  
00135     );
00137 
00146     virtual PINDEX Append(
00147       PObject * obj   
00148     );
00149 
00162     virtual PINDEX Insert(
00163       const PObject & before,   
00164       PObject * obj             
00165     );
00166 
00174     virtual PINDEX InsertAt(
00175       PINDEX index,   
00176       PObject * obj   
00177     );
00178 
00185     virtual PBoolean Remove(
00186       const PObject * obj   
00187     );
00188 
00198     virtual PObject * RemoveAt(
00199       PINDEX index   
00200     );
00201 
00213     virtual PBoolean SetAt(
00214       PINDEX index,   
00215       PObject * val   
00216     );
00217     
00228     virtual PBoolean ReplaceAt(
00229       PINDEX index,   
00230       PObject * val   
00231     );
00232 
00243     virtual PObject * GetAt(
00244       PINDEX index  
00245     ) const;
00246 
00254     virtual PINDEX GetObjectsIndex(
00255       const PObject * obj  
00256     ) const;
00257 
00266     virtual PINDEX GetValuesIndex(
00267       const PObject & obj  
00268     ) const;
00270 
00271 
00272   protected:
00283     PINLINE PObject & GetReferenceAt(
00284       PINDEX index  
00285     ) const;
00286 
00296     PBoolean SetCurrent(
00297       PINDEX index  
00298     ) const;
00299 
00300     // The types below cannot be nested as DevStudio 2005 AUTOEXP.DAT doesn't like it
00301     typedef PListElement Element;
00302     PListInfo * info;
00303 };
00304 
00305 
00306 #ifdef PHAS_TEMPLATES
00307 
00314 template <class T> class PList : public PAbstractList
00315 {
00316   PCLASSINFO(PList, PAbstractList);
00317 
00318   public:
00326     PList()
00327       : PAbstractList() { }
00329 
00335     virtual PObject * Clone() const
00336       { return PNEW PList(0, this); }
00338 
00352     T & operator[](PINDEX index) const
00353       { return (T &)GetReferenceAt(index); }
00355 
00356   protected:
00357     PList(int dummy, const PList * c)
00358       : PAbstractList(dummy, c) { }
00359 };
00360 
00361 
00373 #define PLIST(cls, T) typedef PList<T> cls
00374 
00386 #define PDECLARE_LIST(cls, T) \
00387   PLIST(cls##_PTemplate, T); \
00388   PDECLARE_CLASS(cls, PList<T>) \
00389   protected: \
00390     cls(int dummy, const cls * c) \
00391       : PList<T>(dummy, c) { } \
00392   public: \
00393     cls() \
00394       : PList<T>() { } \
00395     virtual PObject * Clone() const \
00396       { return PNEW cls(0, this); } \
00397 
00398 
00411 template <class T> class PQueue : public PAbstractList
00412 {
00413   PCLASSINFO(PQueue, PAbstractList);
00414 
00415   public:
00424     PQueue()
00425       : PAbstractList() { DisallowDeleteObjects(); }
00427 
00433     virtual PObject * Clone() const
00434       { return PNEW PQueue(0, this); }
00436 
00442     virtual void Enqueue(
00443       T * obj   
00444     ) { PAbstractList::Append(obj); }
00450     virtual T * Dequeue()
00451       { if (GetSize() == 0) return NULL; else return (T *)PAbstractList::RemoveAt(0);}
00453 
00454   protected:
00455     PQueue(int dummy, const PQueue * c)
00456       : PAbstractList(dummy, c)
00457       { reference->deleteObjects = c->reference->deleteObjects; }
00458 };
00459 
00460 
00473 #define PQUEUE(cls, T) typedef PQueue<T> cls
00474 
00475 
00488 #define PDECLARE_QUEUE(cls, T) \
00489   PQUEUE(cls##_PTemplate, T); \
00490   PDECLARE_CLASS(cls, cls##_PTemplate) \
00491   protected: \
00492     cls(int dummy, const cls * c) \
00493       : cls##_PTemplate(dummy, c) { } \
00494   public: \
00495     cls() \
00496       : cls##_PTemplate() { } \
00497     virtual PObject * Clone() const \
00498       { return PNEW cls(0, this); } \
00499 
00500 
00513 template <class T> class PStack : public PAbstractList
00514 {
00515   PCLASSINFO(PStack, PAbstractList);
00516 
00517   public:
00526     PStack()
00527       : PAbstractList() { DisallowDeleteObjects(); }
00529 
00535     virtual PObject * Clone() const
00536       { return PNEW PStack(0, this); }
00538 
00545     virtual void Push(
00546       T * obj    
00547     ) { PAbstractList::InsertAt(0, obj); }
00548 
00554     virtual T * Pop()
00555       { return (T *)PAbstractList::RemoveAt(0); }
00556 
00563     virtual T & Top()
00564       { PAssert(GetSize() > 0, PStackEmpty); return *(T *)GetAt(0); }
00566 
00567   protected:
00568     PStack(int dummy, const PStack * c)
00569       : PAbstractList(dummy, c)
00570       { reference->deleteObjects = c->reference->deleteObjects; }
00571 };
00572 
00573 
00586 #define PSTACK(cls, T) typedef PStack<T> cls
00587 
00588 
00601 #define PDECLARE_STACK(cls, T) \
00602   PSTACK(cls##_PTemplate, T); \
00603   PDECLARE_CLASS(cls, cls##_PTemplate) \
00604   protected: \
00605     cls(int dummy, const cls * c) \
00606       : cls##_PTemplate(dummy, c) { } \
00607   public: \
00608     cls() \
00609       : cls##_PTemplate() { } \
00610     virtual PObject * Clone() const \
00611       { return PNEW cls(0, this); } \
00612 
00613 
00614 #else // PHAS_TEMPLATES
00615 
00616 
00617 #define PLIST(cls, T) \
00618   class cls : public PAbstractList { \
00619   PCLASSINFO(cls, PAbstractList); \
00620   protected: \
00621     inline cls(int dummy, const cls * c) \
00622       : PAbstractList(dummy, c) { } \
00623   public: \
00624     inline cls() \
00625       : PAbstractList() { } \
00626     virtual PObject * Clone() const \
00627       { return PNEW cls(0, this); } \
00628     inline T & operator[](PINDEX index) const \
00629       { return (T &)GetReferenceAt(index); } \
00630   }
00631 
00632 #define PDECLARE_LIST(cls, T) \
00633   PLIST(cls##_PTemplate, T); \
00634   PDECLARE_CLASS(cls, cls##_PTemplate) \
00635   protected: \
00636     cls(int dummy, const cls * c) \
00637       : cls##_PTemplate(dummy, c) { } \
00638   public: \
00639     cls() \
00640       : cls##_PTemplate() { } \
00641     virtual PObject * Clone() const \
00642       { return PNEW cls(0, this); } \
00643 
00644 
00645 #define PQUEUE(cls, T) \
00646   class cls : public PAbstractList { \
00647   PCLASSINFO(cls, PAbstractList); \
00648   protected: \
00649     inline cls(int dummy, const cls * c) \
00650       : PAbstractList(dummy, c) \
00651       { reference->deleteObjects = c->reference->deleteObjects; } \
00652   public: \
00653     inline cls() \
00654       : PAbstractList() { DisallowDeleteObjects(); } \
00655     virtual PObject * Clone() const \
00656       { return PNEW cls(0, this); } \
00657     virtual void Enqueue(T * t) \
00658       { PAbstractList::Append(t); } \
00659     virtual T * Dequeue() \
00660       { if (GetSize() == 0) return NULL; else return (T *)PAbstractList::RemoveAt(0);} \
00661   }
00662 
00663 #define PDECLARE_QUEUE(cls, T) \
00664   PQUEUE(cls##_PTemplate, T); \
00665   PDECLARE_CLASS(cls, cls##_PTemplate) \
00666   protected: \
00667     cls(int dummy, const cls * c) \
00668       : cls##_PTemplate(dummy, c) { } \
00669   public: \
00670     cls() \
00671       : cls##_PTemplate() { } \
00672     virtual PObject * Clone() const \
00673       { return PNEW cls(0, this); } \
00674 
00675 #define PSTACK(cls, T) \
00676   class cls : public PAbstractList { \
00677   PCLASSINFO(cls, PAbstractList); \
00678   protected: \
00679     inline cls(int dummy, const cls * c) \
00680       : PAbstractList(dummy, c) \
00681       { reference->deleteObjects = c->reference->deleteObjects; } \
00682   public: \
00683     inline cls() \
00684       : PAbstractList() { DisallowDeleteObjects(); } \
00685     virtual PObject * Clone() const \
00686       { return PNEW cls(0, this); } \
00687     virtual void Push(T * t) \
00688       { PAbstractList::InsertAt(0, t); } \
00689     virtual T * Pop() \
00690       { PAssert(GetSize() > 0, PStackEmpty); return (T *)PAbstractList::RemoveAt(0); } \
00691     virtual T & Top() \
00692       { PAssert(GetSize() > 0, PStackEmpty); return *(T *)GetAt(0); } \
00693   }
00694 
00695 #define PDECLARE_STACK(cls, T) \
00696   PSTACK(cls##_PTemplate, T); \
00697   PDECLARE_CLASS(cls, cls##_PTemplate) \
00698   protected: \
00699     cls(int dummy, const cls * c) \
00700       : cls##_PTemplate(dummy, c) { } \
00701   public: \
00702     cls() \
00703       : cls##_PTemplate() { } \
00704     virtual PObject * Clone() const \
00705       { return PNEW cls(0, this); } \
00706 
00707 
00708 #endif // PHAS_TEMPLATES
00709 
00710 
00712 // Sorted List of PObjects
00713 
00714 struct PSortedListElement
00715 {
00716   PSortedListElement * parent;
00717   PSortedListElement * left;
00718   PSortedListElement * right;
00719   PObject            * data;
00720   PINDEX               subTreeSize;
00721   enum { Red, Black }  colour;
00722 };
00723 
00724 struct PSortedListInfo
00725 {
00726   PSortedListInfo();
00727 
00728   PSortedListElement * root;
00729   PSortedListElement * lastElement;
00730   PINDEX               lastIndex;
00731   PSortedListElement   nil;
00732 
00733   PSortedListElement * Successor(const PSortedListElement * node) const;
00734   PSortedListElement * Predecessor(const PSortedListElement * node) const;
00735   PSortedListElement * OrderSelect(PSortedListElement * node, PINDEX index) const;
00736 
00737   typedef PSortedListElement Element;
00738 };
00739 
00766 class PAbstractSortedList : public PCollection
00767 {
00768   PCONTAINERINFO(PAbstractSortedList, PCollection);
00769 
00770   public:
00778     PAbstractSortedList();
00780 
00810     virtual Comparison Compare(const PObject & obj) const;
00812 
00822     virtual PBoolean SetSize(
00823       PINDEX newSize  // New size for the sorted list, this is ignored.
00824     );
00826 
00835     virtual PINDEX Append(
00836       PObject * obj   // New object to place into the collection.
00837     );
00838 
00848     virtual PINDEX Insert(
00849       const PObject & before,   // Object value to insert before.
00850       PObject * obj             // New object to place into the collection.
00851     );
00852 
00862     virtual PINDEX InsertAt(
00863       PINDEX index,   // Index position in collection to place the object.
00864       PObject * obj   // New object to place into the collection.
00865     );
00866 
00877     virtual PBoolean Remove(
00878       const PObject * obj   // Existing object to remove from the collection.
00879     );
00880 
00890     virtual PObject * RemoveAt(
00891       PINDEX index   // Index position in collection to place the object.
00892     );
00893 
00900     virtual void RemoveAll();
00901 
00908     virtual PBoolean SetAt(
00909       PINDEX index,   // Index position in collection to set.
00910       PObject * val   // New value to place into the collection.
00911     );
00912 
00919     virtual PObject * GetAt(
00920       PINDEX index  // Index position in the collection of the object.
00921     ) const;
00922 
00934     virtual PINDEX GetObjectsIndex(
00935       const PObject * obj
00936     ) const;
00937 
00946     virtual PINDEX GetValuesIndex(
00947       const PObject & obj
00948     ) const;
00950 
00951     // The type below cannot be nested as DevStudio 2005 AUTOEXP.DAT doesn't like it
00952     typedef PSortedListElement Element;
00953 
00954   protected:
00955     
00956     // New functions for class
00957     void RemoveElement(Element * node);
00958     void LeftRotate(Element * node);
00959     void RightRotate(Element * node);
00960     void DeleteSubTrees(Element * node, PBoolean deleteObject);
00961     PINDEX ValueSelect(const Element * node, const PObject & obj, const Element ** lastElement) const;
00962 
00963     // The type below cannot be nested as DevStudio 2005 AUTOEXP.DAT doesn't like it
00964     PSortedListInfo * info;
00965 };
00966 
00967 
00968 #ifdef PHAS_TEMPLATES
00969 
00977 template <class T> class PSortedList : public PAbstractSortedList
00978 {
00979   PCLASSINFO(PSortedList, PAbstractSortedList);
00980 
00981   public:
00989     PSortedList()
00990       : PAbstractSortedList() { }
00992 
00998     virtual PObject * Clone() const
00999       { return PNEW PSortedList(0, this); }
01001 
01014     T & operator[](PINDEX index) const
01015       { return *(T *)GetAt(index); }
01017 
01018   protected:
01019     PSortedList(int dummy, const PSortedList * c)
01020       : PAbstractSortedList(dummy, c) { }
01021 };
01022 
01023 
01035 #define PSORTED_LIST(cls, T) typedef PSortedList<T> cls
01036 
01037 
01050 #define PDECLARE_SORTED_LIST(cls, T) \
01051   PSORTED_LIST(cls##_PTemplate, T); \
01052   PDECLARE_CLASS(cls, PSortedList<T>) \
01053   protected: \
01054     cls(int dummy, const cls * c) \
01055       : PSortedList<T>(dummy, c) { } \
01056   public: \
01057     cls() \
01058       : PSortedList<T>() { } \
01059     virtual PObject * Clone() const \
01060       { return PNEW cls(0, this); } \
01061 
01062 
01063 #else // PHAS_TEMPLATES
01064 
01065 
01066 #define PSORTED_LIST(cls, T) \
01067   class cls : public PAbstractSortedList { \
01068   PCLASSINFO(cls, PAbstractSortedList); \
01069   protected: \
01070     inline cls(int dummy, const cls * c) \
01071       : PAbstractSortedList(dummy, c) { } \
01072   public: \
01073     inline cls() \
01074       : PAbstractSortedList() { } \
01075     virtual PObject * Clone() const \
01076       { return PNEW cls(0, this); } \
01077     inline T & operator[](PINDEX index) const \
01078       { return *(T *)GetAt(index); } \
01079   }
01080 
01081 #define PDECLARE_SORTED_LIST(cls, T) \
01082   PSORTED_LIST(cls##_PTemplate, T); \
01083   PDECLARE_CLASS(cls, cls##_PTemplate) \
01084   protected: \
01085     cls(int dummy, const cls * c) \
01086       : cls##_PTemplate(dummy, c) { } \
01087   public: \
01088     cls() \
01089       : cls##_PTemplate() { } \
01090     virtual PObject * Clone() const \
01091       { return PNEW cls(0, this); } \
01092 
01093 
01094 #endif  // PHAS_TEMPLATES
01095 
01096 
01097 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Dec 10 11:18:57 2007 for PTLib by  doxygen 1.5.1