📄 dict.h
字号:
destroyed. */ PDictionary() : PAbstractDictionary() { } //@} /**@name Overrides from class PObject */ //@{ /**Make a complete duplicate of the dictionary. Note that all objects in the array are also cloned, so this will make a complete copy of the dictionary. */ virtual PObject * Clone() const { return PNEW PDictionary(0, this); } //@} /**@name New functions for class */ //@{ /**Get the object contained in the dictionary at the #key# position. The hash table is used to locate the data quickly via the hash function provided by the #key#. The last key/data pair is remembered by the class so that subseqent access is very fast. @return reference to the object indexed by the key. */ D & operator[]( const K & key /// Key to look for in the dictionary. ) const { return (D &)GetRefAt(key); } /**Determine if the value of the object is contained in the hash table. The object values are compared, not the pointers. So the objects in the collection must correctly implement the #PObject::Compare()# function. The hash table is used to locate the entry. @return TRUE if the object value is in the dictionary. */ BOOL Contains( const K & key /// Key to look for in the dictionary. ) const { return AbstractContains(key); } /**Remove an object at the specified key. The returned pointer is then removed using the #SetAt()# function to set that key value to NULL. If the #AllowDeleteObjects# option is set then the object is also deleted. @return pointer to the object being removed, or NULL if it was deleted. */ virtual D * RemoveAt( const K & key /// Key for position in dictionary to get object. ) { D * obj = GetAt(key); AbstractSetAt(key, NULL); return obj; } /**Add a new object to the collection. If the objects value is already in the dictionary then the object is overrides the previous value. If the AllowDeleteObjects option is set then the old object is also deleted. The object is placed in the an ordinal position dependent on the keys hash function. Subsequent searches use the has function to speed access to the data item. @return TRUE if the object was successfully added. */ virtual BOOL SetAt( const K & key, // Key for position in dictionary to add object. D * obj // New object to put into the dictionary. ) { return AbstractSetAt(key, obj); } /**Get the object at the specified key position. If the key was not in the collection then NULL is returned. @return pointer to object at the specified key. */ virtual D * GetAt( const K & key // Key for position in dictionary to get object. ) const { return (D *)AbstractGetAt(key); } /**Get the key in the dictionary at the ordinal index position. The ordinal position in the dictionary is determined by the hash values of the keys and the order of insertion. The last key/data pair is remembered by the class so that subseqent access is very fast. @return reference to key at the index position. */ const K & GetKeyAt( PINDEX index /// Ordinal position in dictionary for key. ) const { return (const K &)AbstractGetKeyAt(index); } /**Get the data in the dictionary at the ordinal index position. The ordinal position in the dictionary is determined by the hash values of the keys and the order of insertion. The last key/data pair is remembered by the class so that subseqent access is very fast. @return reference to data at the index position. */ D & GetDataAt( PINDEX index /// Ordinal position in dictionary for data. ) const { return (D &)AbstractGetDataAt(index); } //@} protected: PDictionary(int dummy, const PDictionary * c) : PAbstractDictionary(dummy, c) { }};/**Declare a dictionary class. This macro is used to declare a descendent of PAbstractDictionary class, customised for a particular key type {\bf K} and data object type {\bf D}. This macro closes the class declaration off so no additional members can be added. If the compilation is using templates then this macro produces a typedef of the #PDictionary# template class. See the #PDictionary# class and #PDECLARE_DICTIONARY# macro for more information. */#define PDICTIONARY(cls, K, D) typedef PDictionary<K, D> cls/**Begin declaration of dictionary class. This macro is used to declare a descendent of PAbstractDictionary class, customised for a particular key type {\bf K} and data object type {\bf D}. If the compilation is using templates then this macro produces a descendent of the #PDictionary# template class. If templates are not being used then the macro defines a set of inline functions to do all casting of types. The resultant classes have an identical set of functions in either case. See the #PDictionary# and #PAbstractDictionary# classes for more information. */#define PDECLARE_DICTIONARY(cls, K, D) \ PDICTIONARY(cls##_PTemplate, K, D); \ PDECLARE_CLASS(cls, cls##_PTemplate) \ protected: \ cls(int dummy, const cls * c) \ : cls##_PTemplate(dummy, c) { } \ public: \ cls() \ : cls##_PTemplate() { } \ virtual PObject * Clone() const \ { return PNEW cls(0, this); } \/**This template class maps the #PAbstractDictionary# to a specific key type and a #POrdinalKey# data type. The functions in this class primarily do all the appropriate casting of types. Note that if templates are not used the #PDECLARE_ORDINAL_DICTIONARY# macro will simulate the template instantiation. */template <class K> class POrdinalDictionary : public PAbstractDictionary{ PCLASSINFO(POrdinalDictionary, PAbstractDictionary); public: /**@name Construction */ //@{ /**Create a new, empty, dictionary. Note that by default, objects placed into the dictionary will be deleted when removed or when all references to the dictionary are destroyed. */ POrdinalDictionary() : PAbstractDictionary() { } //@} /**@name Overrides from class PObject */ //@{ /**Make a complete duplicate of the dictionary. Note that all objects in the array are also cloned, so this will make a complete copy of the dictionary. */ virtual PObject * Clone() const { return PNEW POrdinalDictionary(0, this); } //@} /**@name New functions for class */ //@{ /**Get the object contained in the dictionary at the #key# position. The hash table is used to locate the data quickly via the hash function provided by the key. The last key/data pair is remembered by the class so that subseqent access is very fast. @return reference to the object indexed by the key. */ PINDEX operator[]( const K & key // Key to look for in the dictionary. ) const { return (POrdinalKey &)GetRefAt(key); } /**Determine if the value of the object is contained in the hash table. The object values are compared, not the pointers. So the objects in the collection must correctly implement the #PObject::Compare()# function. The hash table is used to locate the entry. @return TRUE if the object value is in the dictionary. */ BOOL Contains( const K & key /// Key to look for in the dictionary. ) const { return AbstractContains(key); } virtual POrdinalKey * GetAt( const K & key /// Key for position in dictionary to get object. ) const { return (POrdinalKey *)AbstractGetAt(key); } /* Get the object at the specified key position. If the key was not in the collection then NULL is returned. @return pointer to object at the specified key. */ /**Set the data at the specified ordinal index position in the dictionary. The ordinal position in the dictionary is determined by the hash values of the keys and the order of insertion. @return TRUE if the new object could be placed into the dictionary. */ virtual BOOL SetDataAt( PINDEX index, /// Ordinal index in the dictionary. PINDEX ordinal /// New ordinal value to put into the dictionary. ) { return PAbstractDictionary::SetDataAt(index, PNEW POrdinalKey(ordinal)); } /**Add a new object to the collection. If the objects value is already in the dictionary then the object is overrides the previous value. If the AllowDeleteObjects option is set then the old object is also deleted. The object is placed in the an ordinal position dependent on the keys hash function. Subsequent searches use the has function to speed access to the data item. @return TRUE if the object was successfully added. */ virtual BOOL SetAt( const K & key, /// Key for position in dictionary to add object. PINDEX ordinal /// New ordinal value to put into the dictionary. ) { return AbstractSetAt(key, PNEW POrdinalKey(ordinal)); } /**Remove an object at the specified key. The returned pointer is then removed using the #SetAt()# function to set that key value to NULL. If the #AllowDeleteObjects# option is set then the object is also deleted. @return pointer to the object being removed, or NULL if it was deleted. */ virtual PINDEX RemoveAt( const K & key /// Key for position in dictionary to get object. ) { PINDEX ord = *GetAt(key); AbstractSetAt(key, NULL); return ord; } /**Get the key in the dictionary at the ordinal index position. The ordinal position in the dictionary is determined by the hash values of the keys and the order of insertion. The last key/data pair is remembered by the class so that subseqent access is very fast. @return reference to key at the index position. */ const K & GetKeyAt( PINDEX index /// Ordinal position in dictionary for key. ) const { return (const K &)AbstractGetKeyAt(index); } /**Get the data in the dictionary at the ordinal index position. The ordinal position in the dictionary is determined by the hash values of the keys and the order of insertion. The last key/data pair is remembered by the class so that subseqent access is very fast. @return reference to data at the index position. */ PINDEX GetDataAt( PINDEX index /// Ordinal position in dictionary for data. ) const { return (POrdinalKey &)AbstractGetDataAt(index); } //@} protected: POrdinalDictionary(int dummy, const POrdinalDictionary * c) : PAbstractDictionary(dummy, c) { }};/**Declare an ordinal dictionary class. This macro is used to declare a descendent of PAbstractDictionary class, customised for a particular key type {\bf K} and data object type of #POrdinalKey#. This macro closes the class declaration off so no additional members can be added. If the compilation is using templates then this macro produces a typedef of the #POrdinalDictionary# template class. See the #POrdinalDictionary# class and #PDECLARE_ORDINAL_DICTIONARY# macro for more information. */#define PORDINAL_DICTIONARY(cls, K) typedef POrdinalDictionary<K> cls/**Begin declaration of an ordinal dictionary class. This macro is used to declare a descendent of PAbstractList class, customised for a particular key type {\bf K} and data object type of #POrdinalKey#. If the compilation is using templates then this macro produces a descendent of the #POrdinalDictionary# template class. If templates are not being used then the macro defines a set of inline functions to do all casting of types. The resultant classes have an identical set of functions in either case. See the #POrdinalDictionary# and #PAbstractDictionary# classes for more information. */#define PDECLARE_ORDINAL_DICTIONARY(cls, K) \ PORDINAL_DICTIONARY(cls##_PTemplate, K); \ PDECLARE_CLASS(cls, POrdinalDictionary<K>) \ protected: \ cls(int dummy, const cls * c) \ : cls##_PTemplate(dummy, c) { } \ public: \ cls() \ : cls##_PTemplate() { } \ virtual PObject * Clone() const \ { return PNEW cls(0, this); } \#else // PHAS_TEMPLATES#define PDICTIONARY(cls, K, D) \ class cls : public PAbstractDictionary { \ PCLASSINFO(cls, PAbstractDictionary); \ protected: \ inline cls(int dummy, const cls * c) \ : PAbstractDictionary(dummy, c) { } \ public: \ cls() \ : PAbstractDictionary() { } \ virtual PObject * Clone() const \ { return PNEW cls(0, this); } \ D & operator[](const K & key) const \ { return (D &)GetRefAt(key); } \ virtual BOOL Contains(const K & key) const \ { return AbstractContains(key); } \ virtual D * RemoveAt(const K & key) \ { D * obj = GetAt(key); AbstractSetAt(key, NULL); return obj; } \ virtual BOOL SetAt(const K & key, D * obj) \ { return AbstractSetAt(key, obj); } \ virtual D * GetAt(const K & key) const \ { return (D *)AbstractGetAt(key); } \ const K & GetKeyAt(PINDEX index) const \ { return (const K &)AbstractGetKeyAt(index); } \ D & GetDataAt(PINDEX index) const \ { return (D &)AbstractGetDataAt(index); } \ }#define PDECLARE_DICTIONARY(cls, K, D) \ PDICTIONARY(cls##_PTemplate, K, D); \ PDECLARE_CLASS(cls, cls##_PTemplate) \ protected: \ cls(int dummy, const cls * c) \ : cls##_PTemplate(dummy, c) { } \ public: \ cls() \ : cls##_PTemplate() { } \ virtual PObject * Clone() const \ { return PNEW cls(0, this); } \#define PORDINAL_DICTIONARY(cls, K) \ class cls : public PAbstractDictionary { \ PCLASSINFO(cls, PAbstractDictionary); \ protected: \ inline cls(int dummy, const cls * c) \ : PAbstractDictionary(dummy, c) { } \ public: \ inline cls() \ : PAbstractDictionary() { } \ inline virtual PObject * Clone() const \ { return PNEW cls(0, this); } \ inline PINDEX operator[](const K & key) const \ { return (POrdinalKey &)GetRefAt(key); } \ virtual BOOL Contains(const K & key) const \ { return AbstractContains(key); } \ inline virtual POrdinalKey * GetAt(const K & key) const \ { return (POrdinalKey *)AbstractGetAt(key); } \ inline virtual BOOL SetDataAt(PINDEX index, PINDEX ordinal) \ { return PAbstractDictionary::SetDataAt(index, PNEW POrdinalKey(ordinal)); } \ inline virtual BOOL SetAt(const K & key, PINDEX ordinal) \ { return AbstractSetAt(key, PNEW POrdinalKey(ordinal)); } \ inline virtual PINDEX RemoveAt(const K & key) \ { PINDEX ord = *GetAt(key); AbstractSetAt(key, NULL); return ord; } \ inline const K & GetKeyAt(PINDEX index) const \ { return (const K &)AbstractGetKeyAt(index); } \ inline PINDEX GetDataAt(PINDEX index) const \ { return (POrdinalKey &)AbstractGetDataAt(index); } \ }#define PDECLARE_ORDINAL_DICTIONARY(cls, K) \ PORDINAL_DICTIONARY(cls##_PTemplate, K); \ PDECLARE_CLASS(cls, cls##_PTemplate) \ protected: \ cls(int dummy, const cls * c) \ : cls##_PTemplate(dummy, c) { } \ public: \ cls() \ : cls##_PTemplate() { } \ virtual PObject * Clone() const \ { return PNEW cls(0, this); } \#endif // PHAS_TEMPLATES// End Of File ///////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -