📄 array.h
字号:
/* * array.h * * Linear Array Container classes. * * Portable Windows Library * * Copyright (c) 1993-1998 Equivalence Pty. Ltd. * * The contents of this file are subject to the Mozilla Public License * Version 1.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. * * The Original Code is Portable Windows Library. * * The Initial Developer of the Original Code is Equivalence Pty. Ltd. * * Portions are Copyright (C) 1993 Free Software Foundation, Inc. * All Rights Reserved. * * Contributor(s): ______________________________________. * * $Log: array.h,v $ * Revision 1.19 1999/11/30 00:22:54 robertj * Updated documentation for doc++ * * Revision 1.18 1999/09/03 15:08:38 robertj * Fixed typo in ancestor class name * * Revision 1.17 1999/08/22 12:13:42 robertj * Fixed warning when using inlines on older GNU compiler * * Revision 1.16 1999/08/20 03:07:44 robertj * Fixed addded Concatenate function for non-template version. * * Revision 1.15 1999/08/18 01:45:12 robertj * Added concatenation function to "base type" arrays. * * Revision 1.14 1999/03/09 02:59:49 robertj * Changed comments to doc++ compatible documentation. * * Revision 1.13 1999/02/16 08:07:11 robertj * MSVC 6.0 compatibility changes. * * Revision 1.12 1998/09/23 06:20:16 robertj * Added open source copyright license. * * Revision 1.11 1998/08/21 05:23:57 robertj * Added hex dump capability to base array types. * Added ability to have base arrays of static memory blocks. * * Revision 1.10 1997/06/08 04:49:10 robertj * Fixed non-template class descendent order. * * Revision 1.9 1996/08/17 09:54:34 robertj * Optimised RemoveAll() for object arrays. * * Revision 1.8 1996/01/02 11:48:46 robertj * Removed requirement that PArray elements have parameterless constructor.. * * Revision 1.7 1995/10/14 14:52:33 robertj * Changed arrays to not break references. * * Revision 1.6 1995/06/17 11:12:18 robertj * Documentation update. * * Revision 1.5 1995/03/14 12:40:58 robertj * Updated documentation to use HTML codes. * * Revision 1.4 1995/02/22 10:50:26 robertj * Changes required for compiling release (optimised) version. * * Revision 1.3 1995/01/15 04:49:09 robertj * Fixed errors in template version. * * Revision 1.2 1994/12/21 11:52:46 robertj * Documentation and variable normalisation. * * Revision 1.1 1994/12/12 09:59:29 robertj * Initial revision * */#ifdef __GNUC__#pragma interface#endif///////////////////////////////////////////////////////////////////////////////// The abstract array class/**This class contains a variable length array of arbitrary memory blocks. These can be anything from individual bytes to large structures. Note that that does {\bf not} include class objects that require construction or destruction. Elements in this array will not execute the contructors or destructors of objects. An abstract array consists of a linear block of memory sufficient to hold #PContainer::GetSize()# elements of #elementSize# bytes each. The memory block itself will atuomatically be resized when required and freed when no more references to it are present. The PAbstractArray class would very rarely be descended from directly by the user. The #PBASEARRAY# macro would normally be used to create a class and any new classes descended from that. That will instantiate the template based on #PBaseArray# or directly declare and define a class (using inline functions) if templates are not being used. The #PBaseArray# class or #PBASEARRAY# macro will define the correctly typed operators for pointer access (#operator const T *#) and subscript access (#operator[]#). */class PAbstractArray : public PContainer{ PCONTAINERINFO(PAbstractArray, PContainer); public: /**@name Construction */ //@{ /**Create a new dynamic array of #initalSize# elements of #elementSizeInBytes# bytes each. The array memory is initialised to zeros. If the initial size is zero then no memory is allocated. Note that the internal pointer is set to NULL, not to a pointer to zero bytes of memory. This can be an important distinction when the pointer is obtained via an operator created in the #PBASEARRAY# macro. */ PAbstractArray( PINDEX elementSizeInBytes, /**Size of each element in the array. This must be > 0 or the constructor will assert. */ PINDEX initialSize = 0 /// Number of elements to allocate initially. ); /**Create a new dynamic array of #bufferSizeInElements# elements of #elementSizeInBytes# bytes each. The contents of the memory pointed to by buffer is then used to initialise the newly allocated array. If the initial size is zero then no memory is allocated. Note that the internal pointer is set to NULL, not to a pointer to zero bytes of memory. This can be an important distinction when the pointer is obtained via an operator created in the #PBASEARRAY# macro. If the #dynamicAllocation# parameter is FALSE then the pointer is used directly by the container. It 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. */ PAbstractArray( PINDEX elementSizeInBytes, /**Size of each element in the array. This must be > 0 or the constructor will assert. */ const void *buffer, /// Pointer to an array of elements. PINDEX bufferSizeInElements, /// Number of elements pointed to by buffer. BOOL dynamicAllocation /// Buffer is copied and dynamically allocated. ); //@} /**@name Overrides from class PObject */ //@{ /**Get the relative rank of the two arrays. The following algorithm is employed for the comparison:\begin{description} \item[EqualTo] if the two array memory blocks are identical in length and contents. \item[LessThan] if the array length is less than the #obj# parameters array length. \item[GreaterThan] if the array length is greater than the #obj# parameters array length.\end{description} If the array sizes are identical then the #memcmp()# function is used to rank the two arrays. @return comparison of the two objects, #EqualTo# for same, #LessThan# for #obj# logically less than the object and #GreaterThan# for #obj# logically greater than the object. */ virtual Comparison Compare( const PObject & obj /// Other PAbstractArray to compare against. ) const; //@} /**@name Overrides from class PContainer */ //@{ /**Set the size of the array in elements. A new array may be allocated to accomodate the new number of elements. 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 elements. ); //@} /**@name New functions for class */ //@{ /**Attach a pointer to a static block to the base 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 elements 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. */ void * GetPointer( PINDEX minSize = 1 /// Minimum size the array must be. ); /**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, or if the two arrays are of base elements of different sizes. @return TRUE if the memory allocation succeeded. */ BOOL Concatenate( const PAbstractArray & array /// Array to concatenate. ); //@} protected: void PrintNumbersOn(ostream & strm, PINDEX size, BOOL is_signed) const; virtual long GetNumberValueAt(PINDEX idx) const; /// Size of an element in bytes PINDEX elementSize; /// Pointer to the allocated block of memory. char * theArray; /// Flag indicating the array was allocated on the heap. BOOL allocatedDynamically; friend class PArrayObjects;};///////////////////////////////////////////////////////////////////////////////// An array of some base type#ifdef PHAS_TEMPLATES/**This template class maps the #PAbstractArray# to a specific element type. The functions in this class primarily do all the appropriate casting of types. Note that if templates are not used the #PBASEARRAY# macro will simulate the template instantiation. The following classes are instantiated automatically for the basic scalar types:\begin{itemize} \item #PCharArray# \item #PBYTEArray# \item #PShortArray# \item #PWORDArray# \item #PIntArray# \item #PUnsignedArray# \item #PLongArray# \item #PDWORDArray#\end{itemize} */template <class T> class PBaseArray : public PAbstractArray{ PCLASSINFO(PBaseArray, PAbstractArray); public: /**@name Construction */ //@{ /**Construct a new dynamic array of elements of the specified type. The array is initialised to all zero bytes. Note that this may not be logically equivalent to the zero value for the type, though this would be very rare. */ PBaseArray( PINDEX initialSize = 0 /// Initial number of elements in the array. ) : PAbstractArray(sizeof(T), initialSize) { } /**Construct a new dynamic array of elements of the specified type. */ PBaseArray( T const * buffer, /// Pointer to an array of the elements of type {\bf T}. PINDEX length, /// Number of elements pointed to by #buffer#. BOOL dynamic = TRUE /// Buffer is copied and dynamically allocated. ) : PAbstractArray(sizeof(T), buffer, length, dynamic) { } //@} /**@name Overrides from class PObject */ //@{ /** Clone the object. */ virtual PObject * Clone() const { return PNEW PBaseArray<T>(*this, GetSize()); } //@} /**@name Overrides from class PContainer */ //@{ /**Set the specific element 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. T val /// Value to set in the array. ) { return SetMinSize(index+1) && val==(((T *)theArray)[index] = val); } /**Get a value from the array. If the #index# is beyond the end of the allocated array then a zero value is returned. @return value at the array position. */ T GetAt( PINDEX index /// Position on the array to get value from. ) const { PASSERTINDEX(index); return index < GetSize() ? ((T *)theArray)[index] : (T)0; } /**Attach a pointer to a static block to the base 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 T * buffer, /// Pointer to an array of elements. PINDEX bufferSize /// Number of elements pointed to by buffer. ) { PAbstractArray::Attach(buffer, bufferSize); } /**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. */ T * GetPointer( PINDEX minSize = 0 /// Minimum size for returned buffer pointer. ) { return (T *)PAbstractArray::GetPointer(minSize); } //@} /**@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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -