📄 dict.h
字号:
Element * next;
Element * prev;
};
PDECLARE_BASEARRAY(Table, Element *)
#ifdef DOC_PLUS_PLUS
{
#endif
public:
virtual ~Table() { Destruct(); }
virtual void DestroyContents();
PINDEX AppendElement(PObject * key, PObject * data);
PObject * RemoveElement(const PObject & key);
BOOL SetLastElementAt(PINDEX index);
Element * GetElementAt(const PObject & key);
PINDEX GetElementsIndex(const PObject*obj,BOOL byVal,BOOL keys) const;
PINDEX lastIndex;
PINDEX lastBucket;
Element * lastElement;
BOOL deleteKeys;
friend class PHashTable;
friend class PAbstractSet;
};
friend class Table;
Table * hashTable;
};
//////////////////////////////////////////////////////////////////////////////
/** Abstract set of PObjects.
*/
class PAbstractSet : public PHashTable
{
PCONTAINERINFO(PAbstractSet, PHashTable);
public:
/**@name Construction */
//@{
/**Create a new, empty, set.
Note that by default, objects placed into the list will be deleted when
removed or when all references to the list are destroyed.
*/
PINLINE PAbstractSet();
//@}
/**@name Overrides from class PCollection */
//@{
/**Add a new object to the collection. If the objects value is already in
the set then the object is {\bf not} included. If the
#AllowDeleteObjects# option is set then the #obj# parameter
is also deleted.
@return
hash function value of the newly added object.
*/
virtual PINDEX Append(
PObject * obj ///< New object to place into the collection.
);
/**Add a new object to the collection. If the objects value is already in
the set then the object is {\bf not} included. If the
AllowDeleteObjects option is set then the #obj# parameter is
also deleted.
The object is always placed in the an ordinal position dependent on its
hash function. It is not placed at the specified position. The
#before# parameter is ignored.
@return
hash function value of the newly added object.
*/
virtual PINDEX Insert(
const PObject & before, ///< Object value to insert before.
PObject * obj ///< New object to place into the collection.
);
/**Add a new object to the collection. If the objects value is already in
the set then the object is {\bf not} included. If the
AllowDeleteObjects option is set then the #obj# parameter is
also deleted.
The object is always placed in the an ordinal position dependent on its
hash function. It is not placed at the specified position. The
#index# parameter is ignored.
@return
hash function value of the newly added object.
*/
virtual PINDEX InsertAt(
PINDEX index, ///< Index position in collection to place the object.
PObject * obj ///< New object to place into the collection.
);
/**Remove the object from the collection. If the AllowDeleteObjects option
is set then the object is also deleted.
Note that the comparison for searching for the object in collection is
made by pointer, not by value. Thus the parameter must point to the
same instance of the object that is in the collection.
@return
TRUE if the object was in the collection.
*/
virtual BOOL Remove(
const PObject * obj ///< Existing object to remove from the collection.
);
/**Remove an object at the specified index. 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 PObject * RemoveAt(
PINDEX index ///< Index position in collection to place the object.
);
/**This function is the same as PHashTable::AbstractGetKeyAt().
@return
Always NULL.
*/
virtual PObject * GetAt(
PINDEX index ///< Index position in the collection of the object.
) const;
/**Add a new object to the collection. If the objects value is already in
the set then the object is {\bf not} included. If the
AllowDeleteObjects option is set then the #obj# parameter is
also deleted.
The object is always placed in the an ordinal position dependent on its
hash function. It is not placed at the specified position. The
#index# parameter is ignored.
@return
TRUE if the object was successfully added.
*/
virtual BOOL SetAt(
PINDEX index, ///< Index position in collection to set.
PObject * val ///< New value to place into the collection.
);
/**Search the collection for the specific instance of the object. The
object pointers are compared, not the values. The hash table is used
to locate the entry.
Note that that will require value comparisons to be made to find the
equivalent entry and then a final check is made with the pointers to
see if they are the same instance.
@return
ordinal index position of the object, or P_MAX_INDEX.
*/
virtual PINDEX GetObjectsIndex(
const PObject * obj ///< Object to find.
) const;
/**Search the collection for the specified value of the object. 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
ordinal index position of the object, or P_MAX_INDEX.
*/
virtual PINDEX GetValuesIndex(
const PObject & obj ///< Object to find equal value.
) const;
//@}
};
#ifdef PHAS_TEMPLATES
/**This template class maps the PAbstractSet to a specific object type. The
functions in this class primarily do all the appropriate casting of types.
By default, objects placed into the set will {\bf not} be deleted when
removed or when all references to the set are destroyed. This is different
from the default on most collection classes.
Note that if templates are not used the #PDECLARE_SET# macro will
simulate the template instantiation.
*/
template <class T> class PSet : public PAbstractSet
{
PCLASSINFO(PSet, PAbstractSet);
public:
/**@name Construction */
//@{
/**Create a new, empty, dictionary. The parameter indicates whether to
delete objects that are removed from the set.
Note that by default, objects placed into the set will {\bf not} be
deleted when removed or when all references to the set are destroyed.
This is different from the default on most collection classes.
*/
inline PSet(BOOL initialDeleteObjects = FALSE)
: PAbstractSet() { AllowDeleteObjects(initialDeleteObjects); }
//@}
/**@name Overrides from class PObject */
//@{
/**Make a complete duplicate of the set. Note that all objects in the
array are also cloned, so this will make a complete copy of the set.
*/
virtual PObject * Clone() const
{ return PNEW PSet(0, this); }
//@}
/**@name New functions for class */
//@{
/**Include the specified object into the set. If the objects value is
already in the set then the object is {\bf not} included. If the
AllowDeleteObjects option is set then the #obj# parameter is
also deleted.
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.
*/
void Include(
const T * obj // New object to include in the set.
) { Append((PObject *)obj); }
/**Include the specified objects value into the set. If the objects value
is already in the set then the object is {\bf not} included.
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.
*/
PSet & operator+=(
const T & obj // New object to include in the set.
) { Append(obj.Clone()); return *this; }
/**Remove the object from the set. If the AllowDeleteObjects option is set
then the object is also deleted.
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.
*/
void Exclude(
const T * obj // New object to exclude in the set.
) { Remove(obj); }
/**Remove the objects value from the set. If the AllowDeleteObjects
option is set then the object is also deleted.
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.
*/
PSet & operator-=(
const T & obj // New object to exclude in the set.
) { RemoveAt(GetValuesIndex(obj)); return *this; }
/**Determine if the value of the object is contained in the set. 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 set.
*/
BOOL Contains(
const T & key ///< Key to look for in the set.
) const { return AbstractContains(key); }
/**Determine if the value of the object is contained in the set. 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 set.
*/
BOOL operator[](
const T & key ///< Key to look for in the set.
) const { return AbstractContains(key); }
/**Get the key in the set at the ordinal index position.
The ordinal position in the set 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.
*/
virtual const T & GetKeyAt(
PINDEX index ///< Index of value to get.
) const
{ return (const T &)AbstractGetKeyAt(index); }
//@}
protected:
PSet(int dummy, const PSet * c)
: PAbstractSet(dummy, c)
{ reference->deleteObjects = c->reference->deleteObjects; }
};
/**Declare set class.
This macro is used to declare a descendent of PAbstractSet class,
customised for a particular object type {\bf T}. 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 #PSet# template class.
See the #PSet# class and #PDECLARE_SET# macro for more
information.
*/
#define PSET(cls, T) typedef PSet<T> cls
/**Begin declaration of a set class.
This macro is used to declare a descendent of PAbstractSet class,
customised for a particular object type {\bf T}.
If the compilation is using templates then this macro produces a descendent
of the #PSet# 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 #PSet# and #PAbstractSet# classes for more information.
*/
#define PDECLARE_SET(cls, T, initDelObj) \
PSET(cls##_PTemplate, T); \
PDECLARE_CLASS(cls, cls##_PTemplate) \
protected: \
cls(int dummy, const cls * c) \
: cls##_PTemplate(dummy, c) { } \
public: \
cls(BOOL initialDeleteObjects = initDelObj) \
: cls##_PTemplate(initialDeleteObjects) { } \
virtual PObject * Clone() const \
{ return PNEW cls(0, this); } \
#else // PHAS_TEMPLATES
#define PSET(cls, K) \
class cls : public PAbstractSet { \
PCLASSINFO(cls, PAbstractSet); \
protected: \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -