📄 array.h
字号:
All objects are shifted down to fill the vacated position. Note if the index is beyond the size of the collection then the function will assert. @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. ); /**Set the object at the specified ordinal position to the new value. This will overwrite the existing entry. If the AllowDeleteObjects option is set then the old object is also deleted. @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. ); /**Get the object at the specified ordinal position. If the index was greater than the size of the collection then NULL is returned. @return pointer to object at the specified index. */ virtual PObject * GetAt( PINDEX index /// Index position in the collection of the object. ) const; /**Search the collection for the specific instance of the object. The object pointers are compared, not the values. A simple linear search from ordinal position zero is performed. @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. A simple linear search from ordinal position zero is performed. @return ordinal index position of the object, or P_MAX_INDEX. */ virtual PINDEX GetValuesIndex( const PObject & obj // Object to find equal of. ) const; /**Remove all of the elements in the collection. This operates by continually calling #RemoveAt()# until there are no objects left. The objects are removed from the last, at index #(GetSize()-1)# toward the first at index zero. */ virtual void RemoveAll(); //@} protected: PBASEARRAY(ObjPtrArray, PObject *); ObjPtrArray * theArray;};#ifdef PHAS_TEMPLATES/**This template class maps the PArrayObjects to a specific object type.The functions in this class primarily do all the appropriate casting of types.Note that if templates are not used the #PARRAY# macro willsimulate the template instantiation.*/template <class T> class PArray : public PArrayObjects{ PCLASSINFO(PArray, PArrayObjects); public: /**@name Construction */ //@{ /**Create a new array of objects. The array is initially set to the specified size with each entry having NULL as is pointer value. Note that by default, objects placed into the list will be deleted when removed or when all references to the list are destroyed. */ PArray( PINDEX initialSize = 0 /// Initial number of objects in the array. ) : PArrayObjects(initialSize) { } //@} /**@name Overrides from class PObject */ //@{ /** Make a complete duplicate of the array. Note that all objects in the array are also cloned, so this will make a complete copy of the array. */ virtual PObject * Clone() const { return PNEW PArray(0, this); } //@} /**@name New functions for class */ //@{ /**Retrieve a reference to the object in the array. If there was not an object at that ordinal position or the index was beyond the size of the array then the function asserts. @return reference to the object at #index# position. */ T & operator[]( PINDEX index /// Index position in the collection of the object. ) const { PObject * obj = GetAt(index); PAssert(obj != NULL, PInvalidArrayElement); return (T &)*obj; } //@} protected: PArray(int dummy, const PArray * c) : PArrayObjects(dummy, c) { }};/** Declare an array to a specific type of object.This macro is used to declare a descendent of PArrayObjects class,customised for a particular object type {\bf T}. This macro closes theclass declaration off so no additional members can be added.If the compilation is using templates then this macro produces a typedefof the #PArray# template class.See the #PBaseArray# class and #PDECLARE_ARRAY# macro for moreinformation.*/#define PARRAY(cls, T) typedef PArray<T> cls/** Begin declaration an array to a specific type of object.This macro is used to declare a descendent of PArrayObjects class,customised for a particular object type {\bf T}.If the compilation is using templates then this macro produces a descendentof the #PArray# template class. If templates are not being used thenthe macro defines a set of inline functions to do all casting of types. Theresultant classes have an identical set of functions in either case.See the #PBaseArray# and #PAbstractArray# classes for moreinformation.*/#define PDECLARE_ARRAY(cls, T) \ PARRAY(cls##_PTemplate, T); \ PDECLARE_CLASS(cls, cls##_PTemplate) \ protected: \ inline cls(int dummy, const cls * c) \ : cls##_PTemplate(dummy, c) { } \ public: \ inline cls(PINDEX initialSize = 0) \ : cls##_PTemplate(initialSize) { } \ virtual PObject * Clone() const \ { return PNEW cls(0, this); } \#else // PHAS_TEMPLATES#define PARRAY(cls, T) \ class cls : public PArrayObjects { \ PCLASSINFO(cls, PArrayObjects); \ protected: \ inline cls(int dummy, const cls * c) \ : PArrayObjects(dummy, c) { } \ public: \ inline cls(PINDEX initialSize = 0) \ : PArrayObjects(initialSize) { } \ virtual PObject * Clone() const \ { return PNEW cls(0, this); } \ inline T & operator[](PINDEX index) const\ { PObject * obj = GetAt(index); \ PAssert(obj != NULL, PInvalidArrayElement); \ /* want to do to this, but gcc 3.0 complains --> return *(T *)obj; } */ \ return (T &)*obj; } \ }#define PDECLARE_ARRAY(cls, T) \ PARRAY(cls##_PTemplate, T); \ PDECLARE_CLASS(cls, cls##_PTemplate) \ protected: \ inline cls(int dummy, const cls * c) \ : cls##_PTemplate(dummy, c) { } \ public: \ inline cls(PINDEX initialSize = 0) \ : cls##_PTemplate(initialSize) { } \ virtual PObject * Clone() const \ { return PNEW cls(0, this); } \#endif // PHAS_TEMPLATES/**This class represents a dynamic bit array. */class PBitArray : public PBYTEArray{ PCLASSINFO(PBitArray, PBYTEArray); public: /**@name Construction */ //@{ /**Construct a new dynamic array of bits. */ PBitArray( PINDEX initialSize = 0 /// Initial number of bits in the array. ); /**Construct a new dynamic array of elements of the specified type. */ PBitArray( const void * buffer, /// Pointer to an array of the elements of type {\bf T}. PINDEX length, /// Number of bits (not bytes!) pointed to by #buffer#. BOOL dynamic = TRUE /// Buffer is copied and dynamically allocated. ); //@} /**@name Overrides from class PObject */ //@{ /** Clone the object. */ virtual PObject * Clone() const; //@} /**@name Overrides from class PContainer */ //@{ /**Get the current size of the container. This represents the number of things the container contains. For some types of containers this will always return 1. @return number of objects in container. */ virtual PINDEX GetSize() const; /**Set the size of the array in bits. A new array may be allocated to accomodate the new number of bits. If the array increases in size then the new bytes are initialised to zero. If the array is made smaller then the data beyond the new size is lost. @return TRUE if the memory for the array was allocated successfully. */ virtual BOOL SetSize( PINDEX newSize /// New size of the array in bits, not bytes. ); /**Set the specific bit in the array. The array will automatically expand, if necessary, to fit the new element in. @return TRUE if new memory for the array was successfully allocated. */ BOOL SetAt( PINDEX index, /// Position in the array to set the new value. BOOL val /// Value to set in the array. ); /**Get a bit from the array. If the #index# is beyond the end of the allocated array then FALSE is returned. @return value at the array position. */ BOOL GetAt( PINDEX index /// Position on the array to get value from. ) const; /**Attach a pointer to a static block to the bit array type. The pointer is used directly and will not be copied to a dynamically allocated buffer. If the SetSize() function is used to change the size of the buffer, the object will be converted to a dynamic form with the contents of the static buffer copied to the allocated buffer. Any dynamically allocated buffer will be freed. */ void Attach( const void * buffer, /// Pointer to an array of elements. PINDEX bufferSize /// Number of bits (not bytes!) pointed to by buffer. ); /**Get a pointer to the internal array and assure that it is of at least the specified size. This is useful when the array contents are being set by some external or system function eg file read. It is unsafe to assume that the pointer is valid for very long after return from this function. The array may be resized or otherwise changed and the pointer returned invalidated. It should be used for simple calls to atomic functions, or very careful examination of the program logic must be performed. @return pointer to the array memory. */ BYTE * GetPointer( PINDEX minSize = 0 /// Minimum size in bits (not bytes!) for returned buffer pointer. ); //@} /**@name New functions for class */ //@{ /**Get a value from the array. If the #index# is beyond the end of the allocated array then a zero value is returned. This is functionally identical to the #PContainer::GetAt()# function. @return value at the array position. */ BOOL operator[]( PINDEX index /// Position on the array to get value from. ) const { return GetAt(index); } /**Set a bit to the array. This is functionally identical to the #PContainer::SetAt(index, TRUE)# function. */ PBitArray & operator+=( PINDEX index /// Position on the array to get value from. ) { SetAt(index, TRUE); return *this; } /**Set a bit to the array. This is functionally identical to the #PContainer::SetAt(index, TRUE)# function. */ PBitArray & operator-=( PINDEX index /// Position on the array to get value from. ) { SetAt(index, FALSE); return *this; } /**Concatenate one array to the end of this array. This function will allocate a new array large enough for the existing contents and the contents of the parameter. The paramters contents is then copied to the end of the existing array. Note this does nothing and returns FALSE if the target array is not dynamically allocated. @return TRUE if the memory allocation succeeded. */ BOOL Concatenate( const PBitArray & array /// Other array to concatenate ); //@}};// End Of File ///////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -