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: 20385 $
00030  * $Author: rjongbloed $
00031  * $Date: 2008-06-04 10:40:38 +0000 (Wed, 04 Jun 2008) $
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 = NULL; }
00053     PListElement * head;
00054     PListElement * tail;
00055 };
00056 
00077 class PAbstractList : public PCollection
00078 {
00079   PCONTAINERINFO(PAbstractList, PCollection);
00080 
00081   public:
00089     PINLINE PAbstractList();
00091 
00092   // Overrides from class PObject
00120     virtual Comparison Compare(const PObject & obj) const;
00121 
00131     virtual PBoolean SetSize(
00132       PINDEX newSize  
00133     );
00135 
00144     virtual PINDEX Append(
00145       PObject * obj   
00146     );
00147 
00160     virtual PINDEX Insert(
00161       const PObject & before,   
00162       PObject * obj             
00163     );
00164 
00172     virtual PINDEX InsertAt(
00173       PINDEX index,   
00174       PObject * obj   
00175     );
00176 
00183     virtual PBoolean Remove(
00184       const PObject * obj   
00185     );
00186 
00196     virtual PObject * RemoveAt(
00197       PINDEX index   
00198     );
00199 
00211     virtual PBoolean SetAt(
00212       PINDEX index,   
00213       PObject * val   
00214     );
00215     
00226     virtual PBoolean ReplaceAt(
00227       PINDEX index,   
00228       PObject * val   
00229     );
00230 
00241     virtual PObject * GetAt(
00242       PINDEX index  
00243     ) const;
00244 
00252     virtual PINDEX GetObjectsIndex(
00253       const PObject * obj  
00254     ) const;
00255 
00264     virtual PINDEX GetValuesIndex(
00265       const PObject & obj  
00266     ) const;
00268 
00269 
00270   protected:
00281     PINLINE PObject & GetReferenceAt(
00282       PINDEX index  
00283     ) const;
00284 
00294     PBoolean SetCurrent(
00295       PINDEX index,           
00296       PListElement * & lastElement 
00297     ) const;
00298 
00299     PObject * RemoveElement(PListElement * element);
00300 
00301     // The types below cannot be nested as DevStudio 2005 AUTOEXP.DAT doesn't like it
00302     typedef PListElement Element;
00303     PListInfo * info;
00304 };
00305 
00306 
00313 template <class T> class PList : public PAbstractList
00314 {
00315   PCLASSINFO(PList, PAbstractList);
00316 
00317   public:
00325     PList()
00326       : PAbstractList() { }
00328 
00334     virtual PObject * Clone() const
00335       { return PNEW PList(0, this); }
00337 
00340     class iterator_base : public std::iterator<std::bidirectional_iterator_tag, T> {
00341     protected:
00342       iterator_base(PListElement * e) : element(e) { }
00343       PListElement * element;
00344 
00345       void Next() { element = PAssertNULL(element)->next; }
00346       void Prev() { element = PAssertNULL(element)->prev; }
00347       T * Ptr() const { return  (T *)PAssertNULL(element)->data; }
00348 
00349     public:
00350       bool operator==(const iterator_base & it) const { return element == it.element; }
00351       bool operator!=(const iterator_base & it) const { return element != it.element; }
00352     };
00353 
00354     class iterator : public iterator_base {
00355     public:
00356       iterator(PListElement * e = NULL) : iterator_base(e) { }
00357 
00358       iterator operator++()    {                      iterator_base::Next(); return *this; }
00359       iterator operator--()    {                      iterator_base::Prev(); return *this; }
00360       iterator operator++(int) { iterator it = *this; iterator_base::Next(); return it;    }
00361       iterator operator--(int) { iterator it = *this; iterator_base::Prev(); return it;    }
00362 
00363       T * operator->() const { return  iterator_base::Ptr(); }
00364       T & operator* () const { return *iterator_base::Ptr(); }
00365     };
00366 
00367     iterator begin()  { return info->head; }
00368     iterator end()    { return iterator(); }
00369     iterator rbegin() { return info->tail; }
00370     iterator rend()   { return iterator(); }
00371 
00372 
00373     class const_iterator : public iterator_base {
00374     public:
00375       const_iterator(PListElement * e = NULL) : iterator_base(e) { }
00376 
00377       const_iterator operator++()    {                            iterator_base::Next(); return *this; }
00378       const_iterator operator--()    {                            iterator_base::Prev(); return *this; }
00379       const_iterator operator++(int) { const_iterator it = *this; iterator_base::Next(); return it;    }
00380       const_iterator operator--(int) { const_iterator it = *this; iterator_base::Prev(); return it;    }
00381 
00382       const T * operator->() const { return  iterator_base::Ptr(); }
00383       const T & operator* () const { return *iterator_base::Ptr(); }
00384     };
00385 
00386     const_iterator begin()  const { return info->head; }
00387     const_iterator end()    const { return const_iterator(); }
00388     const_iterator rbegin() const { return info->tail; }
00389     const_iterator rend()   const { return iterator(); }
00390 
00391     T & front() const { return *(T *)PAssertNULL(info->head)->data; }
00392     T & back() const { return *(T *)PAssertNULL(info->tail)->data; }
00393     void erase(const iterator & it) { Remove(&*it); }
00394     void erase(const const_iterator & it) { Remove(&*it); }
00395 
00396     typedef T value_type;
00398 
00412     T & operator[](PINDEX index) const
00413       { return (T &)GetReferenceAt(index); }
00415 
00416   protected:
00417     PList(int dummy, const PList * c)
00418       : PAbstractList(dummy, c) { }
00419 };
00420 
00421 
00433 #define PLIST(cls, T) typedef PList<T> cls
00434 
00446 #define PDECLARE_LIST(cls, T) \
00447   PLIST(cls##_PTemplate, T); \
00448   PDECLARE_CLASS(cls, PList<T>) \
00449   protected: \
00450     cls(int dummy, const cls * c) \
00451       : PList<T>(dummy, c) { } \
00452   public: \
00453     cls() \
00454       : PList<T>() { } \
00455     virtual PObject * Clone() const \
00456       { return PNEW cls(0, this); } \
00457 
00458 
00471 template <class T> class PQueue : public PAbstractList
00472 {
00473   PCLASSINFO(PQueue, PAbstractList);
00474 
00475   public:
00484     PQueue()
00485       : PAbstractList() { DisallowDeleteObjects(); }
00487 
00493     virtual PObject * Clone() const
00494       { return PNEW PQueue(0, this); }
00496 
00502     virtual void Enqueue(
00503       T * obj   
00504     ) { PAbstractList::Append(obj); }
00510     virtual T * Dequeue()
00511       { if (GetSize() == 0) return NULL; else return (T *)PAbstractList::RemoveAt(0);}
00513 
00514   protected:
00515     PQueue(int dummy, const PQueue * c)
00516       : PAbstractList(dummy, c)
00517       { reference->deleteObjects = c->reference->deleteObjects; }
00518 };
00519 
00520 
00533 #define PQUEUE(cls, T) typedef PQueue<T> cls
00534 
00535 
00548 #define PDECLARE_QUEUE(cls, T) \
00549   PQUEUE(cls##_PTemplate, T); \
00550   PDECLARE_CLASS(cls, cls##_PTemplate) \
00551   protected: \
00552     cls(int dummy, const cls * c) \
00553       : cls##_PTemplate(dummy, c) { } \
00554   public: \
00555     cls() \
00556       : cls##_PTemplate() { } \
00557     virtual PObject * Clone() const \
00558       { return PNEW cls(0, this); } \
00559 
00560 
00573 template <class T> class PStack : public PAbstractList
00574 {
00575   PCLASSINFO(PStack, PAbstractList);
00576 
00577   public:
00586     PStack()
00587       : PAbstractList() { DisallowDeleteObjects(); }
00589 
00595     virtual PObject * Clone() const
00596       { return PNEW PStack(0, this); }
00598 
00605     virtual void Push(
00606       T * obj    
00607     ) { PAbstractList::InsertAt(0, obj); }
00608 
00614     virtual T * Pop()
00615       { return (T *)PAbstractList::RemoveAt(0); }
00616 
00623     virtual T & Top()
00624       { PAssert(GetSize() > 0, PStackEmpty); return *(T *)GetAt(0); }
00626 
00627   protected:
00628     PStack(int dummy, const PStack * c)
00629       : PAbstractList(dummy, c)
00630       { reference->deleteObjects = c->reference->deleteObjects; }
00631 };
00632 
00633 
00646 #define PSTACK(cls, T) typedef PStack<T> cls
00647 
00648 
00661 #define PDECLARE_STACK(cls, T) \
00662   PSTACK(cls##_PTemplate, T); \
00663   PDECLARE_CLASS(cls, cls##_PTemplate) \
00664   protected: \
00665     cls(int dummy, const cls * c) \
00666       : cls##_PTemplate(dummy, c) { } \
00667   public: \
00668     cls() \
00669       : cls##_PTemplate() { } \
00670     virtual PObject * Clone() const \
00671       { return PNEW cls(0, this); } \
00672 
00673 
00675 // Sorted List of PObjects
00676 
00677 struct PSortedListElement
00678 {
00679   PSortedListElement * parent;
00680   PSortedListElement * left;
00681   PSortedListElement * right;
00682   PObject            * data;
00683   PINDEX               subTreeSize;
00684   enum { Red, Black }  colour;
00685 };
00686 
00687 struct PSortedListInfo
00688 {
00689   PSortedListInfo();
00690 
00691   PSortedListElement * root;
00692   //PSortedListElement * lastElement;
00693   //PINDEX               lastIndex;
00694   PSortedListElement   nil;
00695 
00696   PSortedListElement * Successor(const PSortedListElement * node) const;
00697   PSortedListElement * Predecessor(const PSortedListElement * node) const;
00698   PSortedListElement * OrderSelect(PSortedListElement * node, PINDEX index) const;
00699 
00700   typedef PSortedListElement Element;
00701 };
00702 
00729 class PAbstractSortedList : public PCollection
00730 {
00731   PCONTAINERINFO(PAbstractSortedList, PCollection);
00732 
00733   public:
00741     PAbstractSortedList();
00743 
00773     virtual Comparison Compare(const PObject & obj) const;
00775 
00785     virtual PBoolean SetSize(
00786       PINDEX newSize  // New size for the sorted list, this is ignored.
00787     );
00789 
00798     virtual PINDEX Append(
00799       PObject * obj   // New object to place into the collection.
00800     );
00801 
00811     virtual PINDEX Insert(
00812       const PObject & before,   // Object value to insert before.
00813       PObject * obj             // New object to place into the collection.
00814     );
00815 
00825     virtual PINDEX InsertAt(
00826       PINDEX index,   // Index position in collection to place the object.
00827       PObject * obj   // New object to place into the collection.
00828     );
00829 
00840     virtual PBoolean Remove(
00841       const PObject * obj   // Existing object to remove from the collection.
00842     );
00843 
00853     virtual PObject * RemoveAt(
00854       PINDEX index   // Index position in collection to place the object.
00855     );
00856 
00863     virtual void RemoveAll();
00864 
00871     virtual PBoolean SetAt(
00872       PINDEX index,   // Index position in collection to set.
00873       PObject * val   // New value to place into the collection.
00874     );
00875 
00882     virtual PObject * GetAt(
00883       PINDEX index  // Index position in the collection of the object.
00884     ) const;
00885 
00897     virtual PINDEX GetObjectsIndex(
00898       const PObject * obj
00899     ) const;
00900     virtual PINDEX GetObjectsIndex(
00901       const PObject * obj,
00902       PSortedListElement * & lastElement
00903     ) const;
00904 
00913     virtual PINDEX GetValuesIndex(
00914       const PObject & obj
00915     ) const;
00917 
00918     // The type below cannot be nested as DevStudio 2005 AUTOEXP.DAT doesn't like it
00919     typedef PSortedListElement Element;
00920 
00921   protected:
00922     
00923     // New functions for class
00924     void RemoveElement(Element * node);
00925     void LeftRotate(Element * node);
00926     void RightRotate(Element * node);
00927     void DeleteSubTrees(Element * node, PBoolean deleteObject);
00928     PINDEX ValueSelect(const Element * node, const PObject & obj, const Element ** lastElement) const;
00929 
00930     // The type below cannot be nested as DevStudio 2005 AUTOEXP.DAT doesn't like it
00931     PSortedListInfo * info;
00932 };
00933 
00934 
00942 template <class T> class PSortedList : public PAbstractSortedList
00943 {
00944   PCLASSINFO(PSortedList, PAbstractSortedList);
00945 
00946   public:
00954     PSortedList()
00955       : PAbstractSortedList() { }
00957 
00963     virtual PObject * Clone() const
00964       { return PNEW PSortedList(0, this); }
00966 
00979     T & operator[](PINDEX index) const
00980       { return *(T *)GetAt(index); }
00982 
00983   protected:
00984     PSortedList(int dummy, const PSortedList * c)
00985       : PAbstractSortedList(dummy, c) { }
00986 };
00987 
00988 
01000 #define PSORTED_LIST(cls, T) typedef PSortedList<T> cls
01001 
01002 
01015 #define PDECLARE_SORTED_LIST(cls, T) \
01016   PSORTED_LIST(cls##_PTemplate, T); \
01017   PDECLARE_CLASS(cls, PSortedList<T>) \
01018   protected: \
01019     cls(int dummy, const cls * c) \
01020       : PSortedList<T>(dummy, c) { } \
01021   public: \
01022     cls() \
01023       : PSortedList<T>() { } \
01024     virtual PObject * Clone() const \
01025       { return PNEW cls(0, this); } \
01026 
01027 
01028 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Sep 15 01:21:35 2008 for PTLib by  doxygen 1.5.1