00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef PTLIB_DICT_H
00036 #define PTLIB_DICT_H
00037
00038 #ifdef P_USE_PRAGMA
00039 #pragma interface
00040 #endif
00041
00042 #include <ptlib/array.h>
00043
00045
00046
00050 class POrdinalKey : public PObject
00051 {
00052 PCLASSINFO(POrdinalKey, PObject);
00053
00054 public:
00059 PINLINE POrdinalKey(
00060 PINDEX newKey = 0
00061 );
00062
00065 PINLINE POrdinalKey & operator=(PINDEX);
00067
00070
00071 virtual PObject * Clone() const;
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 virtual Comparison Compare(const PObject & obj) const;
00083
00090 virtual PINDEX HashFunction() const;
00091
00098 virtual void PrintOn(ostream & strm) const;
00100
00105 PINLINE operator PINDEX() const;
00106
00109 PINLINE PINDEX operator++();
00110
00113 PINLINE PINDEX operator++(int);
00114
00117 PINLINE PINDEX operator--();
00118
00121 PINLINE PINDEX operator--(int);
00122
00125 PINLINE POrdinalKey & operator+=(PINDEX);
00126
00129 PINLINE POrdinalKey & operator-=(PINDEX );
00131
00132 private:
00133 PINDEX theKey;
00134 };
00135
00136
00138
00139
00140 struct PHashTableElement
00141 {
00142 PObject * key;
00143 PObject * data;
00144 PHashTableElement * next;
00145 PHashTableElement * prev;
00146
00147 PDECLARE_POOL_ALLOCATOR();
00148 };
00149
00150 PDECLARE_BASEARRAY(PHashTableInfo, PHashTableElement *)
00151 #ifdef DOC_PLUS_PLUS
00152 {
00153 #endif
00154 public:
00155 virtual ~PHashTableInfo() { Destruct(); }
00156 virtual void DestroyContents();
00157
00158 PINDEX AppendElement(PObject * key, PObject * data);
00159 PObject * RemoveElement(const PObject & key);
00160 PBoolean SetLastElementAt(PINDEX index, PHashTableElement * & lastElement);
00161 PHashTableElement * GetElementAt(const PObject & key);
00162 PINDEX GetElementsIndex(const PObject*obj,PBoolean byVal,PBoolean keys) const;
00163
00164 PBoolean deleteKeys;
00165
00166 typedef PHashTableElement Element;
00167 friend class PHashTable;
00168 friend class PAbstractSet;
00169 };
00170
00171
00182 class PHashTable : public PCollection
00183 {
00184 PCONTAINERINFO(PHashTable, PCollection);
00185
00186 public:
00189
00190 PHashTable();
00192
00204 virtual Comparison Compare(
00205 const PObject & obj
00206 ) const;
00208
00209
00219 virtual PBoolean SetSize(
00220 PINDEX newSize
00221 );
00223
00224
00235 PINLINE PBoolean AbstractContains(
00236 const PObject & key
00237 ) const;
00238
00253 virtual const PObject & AbstractGetKeyAt(
00254 PINDEX index
00255 ) const;
00256
00271 virtual PObject & AbstractGetDataAt(
00272 PINDEX index
00273 ) const;
00275
00276
00277 typedef PHashTableElement Element;
00278 typedef PHashTableInfo Table;
00279 PHashTableInfo * hashTable;
00280 };
00281
00282
00284
00287 class PAbstractSet : public PHashTable
00288 {
00289 PCONTAINERINFO(PAbstractSet, PHashTable);
00290 public:
00298 PINLINE PAbstractSet();
00300
00311 virtual PINDEX Append(
00312 PObject * obj
00313 );
00314
00327 virtual PINDEX Insert(
00328 const PObject & before,
00329 PObject * obj
00330 );
00331
00344 virtual PINDEX InsertAt(
00345 PINDEX index,
00346 PObject * obj
00347 );
00348
00359 virtual PBoolean Remove(
00360 const PObject * obj
00361 );
00362
00369 virtual PObject * RemoveAt(
00370 PINDEX index
00371 );
00372
00378 virtual PObject * GetAt(
00379 PINDEX index
00380 ) const;
00381
00394 virtual PBoolean SetAt(
00395 PINDEX index,
00396 PObject * val
00397 );
00398
00410 virtual PINDEX GetObjectsIndex(
00411 const PObject * obj
00412 ) const;
00413
00422 virtual PINDEX GetValuesIndex(
00423 const PObject & obj
00424 ) const;
00426 };
00427
00428
00439 template <class T> class PSet : public PAbstractSet
00440 {
00441 PCLASSINFO(PSet, PAbstractSet);
00442
00443 public:
00453 inline PSet(PBoolean initialDeleteObjects = false)
00454 : PAbstractSet() { AllowDeleteObjects(initialDeleteObjects); }
00456
00462 virtual PObject * Clone() const
00463 { return PNEW PSet(0, this); }
00465
00477 void Include(
00478 const T * obj
00479 ) { Append((PObject *)obj); }
00480
00488 PSet & operator+=(
00489 const T & obj
00490 ) { Append(obj.Clone()); return *this; }
00491
00499 void Exclude(
00500 const T * obj
00501 ) { Remove(obj); }
00502
00510 PSet & operator-=(
00511 const T & obj
00512 ) { RemoveAt(GetValuesIndex(obj)); return *this; }
00513
00522 PBoolean Contains(
00523 const T & key
00524 ) const { return AbstractContains(key); }
00525
00534 PBoolean operator[](
00535 const T & key
00536 ) const { return AbstractContains(key); }
00537
00549 virtual const T & GetKeyAt(
00550 PINDEX index
00551 ) const
00552 { return (const T &)AbstractGetKeyAt(index); }
00554
00555
00556 protected:
00557 PSet(int dummy, const PSet * c)
00558 : PAbstractSet(dummy, c)
00559 { reference->deleteObjects = c->reference->deleteObjects; }
00560 };
00561
00562
00574 #define PSET(cls, T) typedef PSet<T> cls
00575
00576
00588 #define PDECLARE_SET(cls, T, initDelObj) \
00589 PSET(cls##_PTemplate, T); \
00590 PDECLARE_CLASS(cls, cls##_PTemplate) \
00591 protected: \
00592 cls(int dummy, const cls * c) \
00593 : cls##_PTemplate(dummy, c) { } \
00594 public: \
00595 cls(PBoolean initialDeleteObjects = initDelObj) \
00596 : cls##_PTemplate(initialDeleteObjects) { } \
00597 virtual PObject * Clone() const \
00598 { return PNEW cls(0, this); } \
00599
00600
00601
00602 PSET(POrdinalSet, POrdinalKey);
00603
00604
00606
00609 class PAbstractDictionary : public PHashTable
00610 {
00611 PCLASSINFO(PAbstractDictionary, PHashTable);
00612 public:
00620 PINLINE PAbstractDictionary();
00622
00631 virtual void PrintOn(
00632 ostream &strm
00633 ) const;
00635
00646 virtual PINDEX Insert(
00647 const PObject & key,
00648 PObject * obj
00649 );
00650
00657 virtual PINDEX InsertAt(
00658 PINDEX index,
00659 PObject * obj
00660 );
00661
00671 virtual PObject * RemoveAt(
00672 PINDEX index
00673 );
00674
00683 virtual PBoolean SetAt(
00684 PINDEX index,
00685 PObject * val
00686 );
00687
00694 virtual PObject * GetAt(
00695 PINDEX index
00696 ) const;
00697
00709 virtual PINDEX GetObjectsIndex(
00710 const PObject * obj
00711 ) const;
00712
00721 virtual PINDEX GetValuesIndex(
00722 const PObject & obj
00723 ) const;
00725
00726
00737 virtual PBoolean SetDataAt(
00738 PINDEX index,
00739 PObject * obj
00740 );
00741
00753 virtual PBoolean AbstractSetAt(
00754 const PObject & key,
00755 PObject * obj
00756 );
00757
00767 virtual PObject & GetRefAt(
00768 const PObject & key
00769 ) const;
00770
00777 virtual PObject * AbstractGetAt(
00778 const PObject & key
00779 ) const;
00781
00782 protected:
00783 PINLINE PAbstractDictionary(int dummy, const PAbstractDictionary * c);
00784
00785 private:
00791 virtual PINDEX Append(
00792 PObject * obj
00793 );
00794
00805 virtual PBoolean Remove(
00806 const PObject * obj
00807 );
00808
00809 };
00810
00811
00819 template <class K, class D> class PDictionary : public PAbstractDictionary
00820 {
00821 PCLASSINFO(PDictionary, PAbstractDictionary);
00822
00823 public:
00832 PDictionary()
00833 : PAbstractDictionary() { }
00835
00842 virtual PObject * Clone() const
00843 { return PNEW PDictionary(0, this); }
00845
00858 D & operator[](
00859 const K & key
00860 ) const
00861 { return (D &)GetRefAt(key); }
00862
00871 PBoolean Contains(
00872 const K & key
00873 ) const { return AbstractContains(key); }
00874
00886 virtual D * RemoveAt(
00887 const K & key
00888 ) {
00889 D * obj = GetAt(key); AbstractSetAt(key, NULL);
00890 return reference->deleteObjects ? (obj ? (D *)-1 : NULL) : obj;
00891 }
00892
00904 virtual PBoolean SetAt(
00905 const K & key,
00906 D * obj
00907 ) { return AbstractSetAt(key, obj); }
00908
00915 virtual D * GetAt(
00916 const K & key
00917 ) const { return (D *)AbstractGetAt(key); }
00918
00930 const K & GetKeyAt(
00931 PINDEX index
00932 ) const
00933 { return (const K &)AbstractGetKeyAt(index); }
00934
00946 D & GetDataAt(
00947 PINDEX index
00948 ) const
00949 { return (D &)AbstractGetDataAt(index); }
00951
00952 typedef std::pair<K, D *> value_type;
00953
00954 protected:
00955 PDictionary(int dummy, const PDictionary * c)
00956 : PAbstractDictionary(dummy, c) { }
00957 };
00958
00959
00972 #define PDICTIONARY(cls, K, D) typedef PDictionary<K, D> cls
00973
00974
00987 #define PDECLARE_DICTIONARY(cls, K, D) \
00988 PDICTIONARY(cls##_PTemplate, K, D); \
00989 PDECLARE_CLASS(cls, cls##_PTemplate) \
00990 protected: \
00991 cls(int dummy, const cls * c) \
00992 : cls##_PTemplate(dummy, c) { } \
00993 public: \
00994 cls() \
00995 : cls##_PTemplate() { } \
00996 virtual PObject * Clone() const \
00997 { return PNEW cls(0, this); } \
00998
00999
01007 template <class K> class POrdinalDictionary : public PAbstractDictionary
01008 {
01009 PCLASSINFO(POrdinalDictionary, PAbstractDictionary);
01010
01011 public:
01020 POrdinalDictionary()
01021 : PAbstractDictionary() { }
01023
01030 virtual PObject * Clone() const
01031 { return PNEW POrdinalDictionary(0, this); }
01033
01046 PINDEX operator[](
01047 const K & key
01048 ) const
01049 { return (POrdinalKey &)GetRefAt(key); }
01050
01059 PBoolean Contains(
01060 const K & key
01061 ) const { return AbstractContains(key); }
01062
01063 virtual POrdinalKey * GetAt(
01064 const K & key
01065 ) const { return (POrdinalKey *)AbstractGetAt(key); }
01066
01067
01068
01069
01070
01071
01072
01081 virtual PBoolean SetDataAt(
01082 PINDEX index,
01083 PINDEX ordinal
01084 ) { return PAbstractDictionary::SetDataAt(index, PNEW POrdinalKey(ordinal)); }
01085
01097 virtual PBoolean SetAt(
01098 const K & key,
01099 PINDEX ordinal
01100 ) { return AbstractSetAt(key, PNEW POrdinalKey(ordinal)); }
01101
01110 virtual PINDEX RemoveAt(
01111 const K & key
01112 ) { PINDEX ord = *GetAt(key); AbstractSetAt(key, NULL); return ord; }
01113
01125 const K & GetKeyAt(
01126 PINDEX index
01127 ) const
01128 { return (const K &)AbstractGetKeyAt(index); }
01129
01141 PINDEX GetDataAt(
01142 PINDEX index
01143 ) const
01144 { return (POrdinalKey &)AbstractGetDataAt(index); }
01146
01147 protected:
01148 POrdinalDictionary(int dummy, const POrdinalDictionary * c)
01149 : PAbstractDictionary(dummy, c) { }
01150 };
01151
01152
01165 #define PORDINAL_DICTIONARY(cls, K) typedef POrdinalDictionary<K> cls
01166
01167
01182 #define PDECLARE_ORDINAL_DICTIONARY(cls, K) \
01183 PORDINAL_DICTIONARY(cls##_PTemplate, K); \
01184 PDECLARE_CLASS(cls, POrdinalDictionary<K>) \
01185 protected: \
01186 cls(int dummy, const cls * c) \
01187 : cls##_PTemplate(dummy, c) { } \
01188 public: \
01189 cls() \
01190 : cls##_PTemplate() { } \
01191 virtual PObject * Clone() const \
01192 { return PNEW cls(0, this); } \
01193
01194
01195 #endif // PTLIB_DICT_H
01196
01197