array.h
Go to the documentation of this file.
/*
* array.h
*
* Linear Array Container classes.
*
* Portable Tools 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): ______________________________________.
*
* $Revision: 25387 $
* $Author: rjongbloed $
* $Date: 2011-03-22 22:51:09 -0500 (Tue, 22 Mar 2011) $
*/
#ifndef PTLIB_ARRAY_H
#define PTLIB_ARRAY_H
#ifdef P_USE_PRAGMA
#pragma interface
#endif
#include <ptlib/contain.h>
// The abstract array class
class PAbstractArray : public PContainer
{
PCONTAINERINFO(PAbstractArray, PContainer);
public:
PAbstractArray(
PINDEX elementSizeInBytes,
PINDEX initialSize = 0
);
PAbstractArray(
PINDEX elementSizeInBytes,
const void *buffer,
PINDEX bufferSizeInElements,
PBoolean dynamicAllocation
);
virtual void PrintOn(
ostream &strm // Stream to print the object into.
) const;
virtual void ReadFrom(
istream &strm // Stream to read the objects contents from.
);
virtual Comparison Compare(
const PObject & obj
) const;
virtual PBoolean SetSize(
PINDEX newSize
);
void Attach(
const void *buffer,
PINDEX bufferSize
);
void * GetPointer(
PINDEX minSize = 1
);
PBoolean Concatenate(
const PAbstractArray & array
);
protected:
PBoolean InternalSetSize(PINDEX newSize, PBoolean force);
virtual void PrintElementOn(
ostream & stream,
PINDEX index
) const;
virtual void ReadElementFrom(
istream & stream,
PINDEX index
);
PAbstractArray(
PContainerReference & reference,
PINDEX elementSizeInBytes
);
PINDEX elementSize;
char * theArray;
PBoolean allocatedDynamically;
friend class PArrayObjects;
};
// An array of some base type
template <class T> class PBaseArray : public PAbstractArray
{
PCLASSINFO(PBaseArray, PAbstractArray);
public:
PBaseArray(
PINDEX initialSize = 0
) : PAbstractArray(sizeof(T), initialSize) { }
PBaseArray(
T const * buffer,
PINDEX length,
PBoolean dynamic = true
) : PAbstractArray(sizeof(T), buffer, length, dynamic) { }
virtual PObject * Clone() const
{
return PNEW PBaseArray<T>(*this, GetSize());
}
PBoolean SetAt(
PINDEX index,
T val
) {
return SetMinSize(index+1) && val==(((T *)theArray)[index] = val);
}
T GetAt(
PINDEX index
) const {
PASSERTINDEX(index);
return index < GetSize() ? ((T *)theArray)[index] : (T)0;
}
void Attach(
const T * buffer,
PINDEX bufferSize
) {
PAbstractArray::Attach(buffer, bufferSize);
}
T * GetPointer(
PINDEX minSize = 0
) {
return (T *)PAbstractArray::GetPointer(minSize);
}
T operator[](
PINDEX index
) const {
return GetAt(index);
}
T & operator[](
PINDEX index
) {
PASSERTINDEX(index);
PAssert(SetMinSize(index+1), POutOfMemory);
return ((T *)theArray)[index];
}
operator T const *() const {
return (T const *)theArray;
}
PBoolean Concatenate(
const PBaseArray & array
) {
return PAbstractArray::Concatenate(array);
}
protected:
virtual void PrintElementOn(
ostream & stream,
PINDEX index
) const {
stream << GetAt(index);
}
PBaseArray(PContainerReference & reference) : PAbstractArray(reference, sizeof(T)) { }
};
#define PBASEARRAY(cls, T) typedef PBaseArray<T> cls
#define PDECLARE_BASEARRAY(cls, T) \
PDECLARE_CLASS(cls, PBaseArray<T>) \
cls(PINDEX initialSize = 0) \
: PBaseArray<T>(initialSize) { } \
cls(PContainerReference & reference) \
: PBaseArray<T>(reference) { } \
cls(T const * buffer, PINDEX length, PBoolean dynamic = true) \
: PBaseArray<T>(buffer, length, dynamic) { } \
virtual PObject * Clone() const \
{ return PNEW cls(*this, GetSize()); } \
template <class T> class PScalarArray : public PBaseArray<T>
{
public:
PScalarArray(
PINDEX initialSize = 0
) : PBaseArray<T>(initialSize) { }
PScalarArray(
T const * buffer,
PINDEX length,
PBoolean dynamic = true
) : PBaseArray<T>(buffer, length, dynamic) { }
protected:
virtual void ReadElementFrom(
istream & stream,
PINDEX index
) {
T t;
stream >> t;
if (!stream.fail())
this->SetAt(index, t);
}
};
#define PSCALAR_ARRAY(cls, T) typedef PScalarArray<T> cls
#ifdef DOC_PLUS_PLUS
class PCharArray : public PBaseArray {
public:
PCharArray(
PINDEX initialSize = 0
);
PCharArray(
char const * buffer,
PINDEX length,
PBoolean dynamic = true
);
#else
PDECLARE_BASEARRAY(PCharArray, char);
#endif
public:
virtual void PrintOn(
ostream & strm
) const;
virtual void ReadFrom(
istream &strm // Stream to read the objects contents from.
);
};
#ifdef DOC_PLUS_PLUS
class PShortArray : public PBaseArray {
public:
PShortArray(
PINDEX initialSize = 0
);
PShortArray(
short const * buffer,
PINDEX length,
PBoolean dynamic = true
);
};
#else
PSCALAR_ARRAY(PShortArray, short);
#endif
#ifdef DOC_PLUS_PLUS
class PIntArray : public PBaseArray {
public:
PIntArray(
PINDEX initialSize = 0
);
PIntArray(
int const * buffer,
PINDEX length,
PBoolean dynamic = true
);
};
#else
PSCALAR_ARRAY(PIntArray, int);
#endif
#ifdef DOC_PLUS_PLUS
class PLongArray : public PBaseArray {
public:
PLongArray(
PINDEX initialSize = 0
);
PLongArray(
long const * buffer,
PINDEX length,
PBoolean dynamic = true
);
};
#else
PSCALAR_ARRAY(PLongArray, long);
#endif
#ifdef DOC_PLUS_PLUS
class PBYTEArray : public PBaseArray {
public:
PBYTEArray(
PINDEX initialSize = 0
);
PBYTEArray(
BYTE const * buffer,
PINDEX length,
PBoolean dynamic = true
);
};
#else
PDECLARE_BASEARRAY(PBYTEArray, BYTE);
#endif
public:
virtual void PrintOn(
ostream & strm
) const;
virtual void ReadFrom(
istream &strm
);
};
#ifdef DOC_PLUS_PLUS
class PWORDArray : public PBaseArray {
public:
PWORDArray(
PINDEX initialSize = 0
);
PWORDArray(
WORD const * buffer,
PINDEX length,
PBoolean dynamic = true
);
};
#else
PSCALAR_ARRAY(PWORDArray, WORD);
#endif
#ifdef DOC_PLUS_PLUS
class PUnsignedArray : public PBaseArray {
public:
PUnsignedArray(
PINDEX initialSize = 0
);
PUnsignedArray(
unsigned const * buffer,
PINDEX length,
PBoolean dynamic = true
);
};
#else
PSCALAR_ARRAY(PUnsignedArray, unsigned);
#endif
#ifdef DOC_PLUS_PLUS
class PDWORDArray : public PBaseArray {
public:
PDWORDArray(
PINDEX initialSize = 0
);
PDWORDArray(
DWORD const * buffer,
PINDEX length,
PBoolean dynamic = true
);
};
#else
PSCALAR_ARRAY(PDWORDArray, DWORD);
#endif
// Linear array of objects
class PArrayObjects : public PCollection
{
PCONTAINERINFO(PArrayObjects, PCollection);
public:
PINLINE PArrayObjects(
PINDEX initialSize = 0
);
virtual Comparison Compare(
const PObject & obj
) const;
virtual PINDEX GetSize() const;
virtual PBoolean SetSize(
PINDEX newSize
);
virtual PINDEX Append(
PObject * obj
);
virtual PINDEX Insert(
const PObject & before,
PObject * obj
);
virtual PINDEX InsertAt(
PINDEX index,
PObject * obj
);
virtual PBoolean Remove(
const PObject * obj
);
virtual PObject * RemoveAt(
PINDEX index
);
virtual PBoolean SetAt(
PINDEX index,
PObject * val
);
virtual PObject * GetAt(
PINDEX index
) const;
virtual PINDEX GetObjectsIndex(
const PObject * obj
) const;
virtual PINDEX GetValuesIndex(
const PObject & obj // Object to find equal of.
) const;
virtual void RemoveAll();
protected:
// The type below cannot be nested as DevStudio 2005 AUTOEXP.DAT doesn't like it
PBaseArray<PObject *> * theArray;
};
template <class T> class PArray : public PArrayObjects
{
PCLASSINFO(PArray, PArrayObjects);
public:
PArray(
PINDEX initialSize = 0
) : PArrayObjects(initialSize) { }
virtual PObject * Clone() const
{ return PNEW PArray(0, this); }
T & operator[](
PINDEX index
) const {
PObject * obj = GetAt(index);
PAssert(obj != NULL, PInvalidArrayElement);
return (T &)*obj;
}
protected:
PArray(int dummy, const PArray * c) : PArrayObjects(dummy, c) { }
};
#define PARRAY(cls, T) typedef PArray<T> cls
#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); } \
class PBitArray : public PBYTEArray
{
PCLASSINFO(PBitArray, PBYTEArray);
public:
PBitArray(
PINDEX initialSize = 0
);
PBitArray(
const void * buffer,
PINDEX length,
PBoolean dynamic = true
);
virtual PObject * Clone() const;
virtual PINDEX GetSize() const;
virtual PBoolean SetSize(
PINDEX newSize
);
PBoolean SetAt(
PINDEX index,
PBoolean val
);
PBoolean GetAt(
PINDEX index
) const;
void Attach(
const void * buffer,
PINDEX bufferSize
);
BYTE * GetPointer(
PINDEX minSize = 0
);
PBoolean operator[](
PINDEX index
) const { return GetAt(index); }
PBitArray & operator+=(
PINDEX index
) { SetAt(index, true); return *this; }
PBitArray & operator-=(
PINDEX index
) { SetAt(index, false); return *this; }
PBoolean Concatenate(
const PBitArray & array
);
};
#endif // PTLIB_ARRAY_H
// End Of File ///////////////////////////////////////////////////////////////
PArrayObjects::RemoveAt
virtual PObject * RemoveAt(PINDEX index)
Remove the object at the specified ordinal index from the collection.
PBYTEArray::PBYTEArray
PBYTEArray(PINDEX initialSize=0)
Construct a new dynamic array of unsigned chars.
PrintOn
virtual void PrintOn(ostream &strm) const
Print the array.
PArrayObjects
An array of objects.
Definition: array.h:813
PArrayObjects::SetAt
virtual PBoolean SetAt(PINDEX index, PObject *val)
Set the object at the specified ordinal position to the new value.
PArrayObjects::GetSize
virtual PINDEX GetSize() const
Get size of array.
PScalarArray::ReadElementFrom
virtual void ReadElementFrom(istream &stream, PINDEX index)
Definition: array.h:526
PCharArray
Array of characters.
Definition: array.h:551
PArray::operator[]
T & operator[](PINDEX index) const
Retrieve a reference to the object in the array.
Definition: array.h:1059
PArrayObjects::GetAt
virtual PObject * GetAt(PINDEX index) const
Get the object at the specified ordinal position.
PBitArray::operator+=
PBitArray & operator+=(PINDEX index)
Set a bit to the array.
Definition: array.h:1237
PBaseArray::operator[]
T & operator[](PINDEX index)
Get a reference to value from the array.
Definition: array.h:398
PBitArray::Attach
void Attach(const void *buffer, PINDEX bufferSize)
Attach a pointer to a static block to the bit array type.
PBaseArray
This template class maps the PAbstractArray to a specific element type.
Definition: array.h:274
PBaseArray::PBaseArray
PBaseArray(PINDEX initialSize=0)
Construct a new dynamic array of elements of the specified type.
Definition: array.h:285
PArrayObjects::RemoveAll
virtual void RemoveAll()
Remove all of the elements in the collection.
PBaseArray::Attach
void Attach(const T *buffer, PINDEX bufferSize)
Attach a pointer to a static block to the base array type.
Definition: array.h:344
PAbstractArray::GetPointer
void * GetPointer(PINDEX minSize=1)
Get a pointer to the internal array and assure that it is of at least the specified size...
PArrayObjects::PArrayObjects
PINLINE PArrayObjects(PINDEX initialSize=0)
Create a new array of objects.
PINLINE
#define PINLINE
Definition: object.h:127
PSCALAR_ARRAY
#define PSCALAR_ARRAY(cls, T)
Declare a dynamic array base type.
Definition: array.h:546
PBaseArray::SetAt
PBoolean SetAt(PINDEX index, T val)
Set the specific element in the array.
Definition: array.h:316
PObject::Comparison
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:1184
POutOfMemory
A new or malloc failed.
Definition: object.h:152
PBitArray::Concatenate
PBoolean Concatenate(const PBitArray &array)
Concatenate one array to the end of this array.
PBaseArray::operator[]
T operator[](PINDEX index) const
Get a value from the array.
Definition: array.h:382
PAbstractArray::Compare
virtual Comparison Compare(const PObject &obj) const
Get the relative rank of the two arrays.
PBitArray::SetSize
virtual PBoolean SetSize(PINDEX newSize)
Set the size of the array in bits.
PBitArray::operator[]
PBoolean operator[](PINDEX index) const
Get a value from the array.
Definition: array.h:1228
PAbstractArray::ReadElementFrom
virtual void ReadElementFrom(istream &stream, PINDEX index)
PScalarArray::PScalarArray
PScalarArray(PINDEX initialSize=0)
Construct a new dynamic array of elements of the specified type.
Definition: array.h:512
PDWORDArray
Array of unsigned long integers.
Definition: array.h:764
PLongArray::PLongArray
PLongArray(PINDEX initialSize=0)
Construct a new dynamic array of longs.
PBaseArray::PBaseArray
PBaseArray(T const *buffer, PINDEX length, PBoolean dynamic=true)
Construct a new dynamic array of elements of the specified type.
Definition: array.h:291
PArray::Clone
virtual PObject * Clone() const
Make a complete duplicate of the array.
Definition: array.h:1046
PWORDArray::PWORDArray
PWORDArray(PINDEX initialSize=0)
Construct a new dynamic array of unsigned shorts.
PCharArray::PCharArray
PCharArray(PINDEX initialSize=0)
Construct a new dynamic array of char.
PCharArray::PrintOn
virtual void PrintOn(ostream &strm) const
Print the array.
PAbstractArray
This class contains a variable length array of arbitrary memory blocks.
Definition: array.h:67
PBitArray::GetAt
PBoolean GetAt(PINDEX index) const
Get a bit from the array.
PUnsignedArray::PUnsignedArray
PUnsignedArray(PINDEX initialSize=0)
Construct a new dynamic array of unsigned ints.
PBoolean
BOOL PBoolean
Definition: object.h:102
PIntArray::PIntArray
PIntArray(PINDEX initialSize=0)
Construct a new dynamic array of ints.
PArrayObjects::theArray
PBaseArray< PObject * > * theArray
Definition: array.h:1013
PBYTEArray
Array of unsigned characters.
Definition: array.h:670
PWORDArray
Array of unsigned short integers.
Definition: array.h:710
contain.h
PAbstractArray::Attach
void Attach(const void *buffer, PINDEX bufferSize)
Attach a pointer to a static block to the base array type.
PAbstractArray::theArray
char * theArray
Pointer to the allocated block of memory.
Definition: array.h:245
PAbstractArray::PrintOn
virtual void PrintOn(ostream &strm) const
Output the contents of the object to the stream.
PAbstractArray::elementSize
PINDEX elementSize
Size of an element in bytes.
Definition: array.h:242
PArrayObjects::Append
virtual PINDEX Append(PObject *obj)
Append a new object to the collection.
PBitArray::SetAt
PBoolean SetAt(PINDEX index, PBoolean val)
Set the specific bit in the array.
PContainer::GetSize
virtual PINDEX GetSize() const
Get the current size of the container.
PBaseArray::PBaseArray
PBaseArray(PContainerReference &reference)
Definition: array.h:449
PArrayObjects::InsertAt
virtual PINDEX InsertAt(PINDEX index, PObject *obj)
Insert a new object at the specified ordinal index.
PAbstractArray::ReadFrom
virtual void ReadFrom(istream &strm)
Input the contents of the object from the stream.
PDECLARE_BASEARRAY
#define PDECLARE_BASEARRAY(cls, T)
Begin a declaration of an array of base types.
Definition: array.h:474
PBitArray::GetSize
virtual PINDEX GetSize() const
Get the current size of the container.
PContainerReference
Definition: contain.h:51
PBitArray
This class represents a dynamic bit array.
Definition: array.h:1114
PAbstractArray::PrintElementOn
virtual void PrintElementOn(ostream &stream, PINDEX index) const
PUnsignedArray
Array of unsigned integers.
Definition: array.h:737
PBaseArray::GetPointer
T * GetPointer(PINDEX minSize=0)
Get a pointer to the internal array and assure that it is of at least the specified size...
Definition: array.h:364
PShortArray::PShortArray
PShortArray(PINDEX initialSize=0)
Construct a new dynamic array of shorts.
PShortArray
Array of short integers.
Definition: array.h:589
PContainer
Abstract class to embody the base functionality of a container.
Definition: contain.h:104
PArrayObjects::Remove
virtual PBoolean Remove(const PObject *obj)
Remove the object from the collection.
PBitArray::Clone
virtual PObject * Clone() const
Clone the object.
PAbstractArray::InternalSetSize
PBoolean InternalSetSize(PINDEX newSize, PBoolean force)
PLongArray
Array of long integers.
Definition: array.h:643
PArrayObjects::Compare
virtual Comparison Compare(const PObject &obj) const
Get the relative rank of the two arrays.
PArray::PArray
PArray(int dummy, const PArray *c)
Definition: array.h:1069
PBitArray::operator-=
PBitArray & operator-=(PINDEX index)
Set a bit to the array.
Definition: array.h:1246
PBaseArray::Clone
virtual PObject * Clone() const
Clone the object.
Definition: array.h:302
PBitArray::GetPointer
BYTE * GetPointer(PINDEX minSize=0)
Get a pointer to the internal array and assure that it is of at least the specified size...
PBaseArray::GetAt
T GetAt(PINDEX index) const
Get a value from the array.
Definition: array.h:329
PAbstractArray::allocatedDynamically
PBoolean allocatedDynamically
Flag indicating the array was allocated on the heap.
Definition: array.h:248
PArrayObjects::Insert
virtual PINDEX Insert(const PObject &before, PObject *obj)
Insert a new object immediately before the specified object.
PContainer::SetMinSize
PBoolean SetMinSize(PINDEX minSize)
Set the minimum size of container.
PArray
This template class maps the PArrayObjects to a specific object type.
Definition: array.h:1024
PBaseArray::PrintElementOn
virtual void PrintElementOn(ostream &stream, PINDEX index) const
Definition: array.h:442
PAssert
#define PAssert(b, msg)
This macro is used to assert that a condition must be true.
Definition: object.h:192
PScalarArray
This template class maps the PAbstractArray to a specific element type.
Definition: array.h:502
PArray::PArray
PArray(PINDEX initialSize=0)
Create a new array of objects.
Definition: array.h:1036
PInvalidArrayElement
A NULL array element object was accessed.
Definition: object.h:156
PArrayObjects::SetSize
virtual PBoolean SetSize(PINDEX newSize)
Set the size of the array in objects.
PAbstractArray::SetSize
virtual PBoolean SetSize(PINDEX newSize)
Set the size of the array in elements.
PContainer::reference
PContainerReference * reference
Definition: contain.h:291
PIntArray
Array of integers.
Definition: array.h:616
PBaseArray::Concatenate
PBoolean Concatenate(const PBaseArray &array)
Concatenate one array to the end of this array.
Definition: array.h:434
PObject
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
PCollection
A collection is a container that collects together descendents of the PObject class.
Definition: contain.h:395
PArrayObjects::GetValuesIndex
virtual PINDEX GetValuesIndex(const PObject &obj) const
Search the collection for the specified value of the object.
PArrayObjects::GetObjectsIndex
virtual PINDEX GetObjectsIndex(const PObject *obj) const
Search the collection for the specific instance of the object.
PBitArray::PBitArray
PBitArray(PINDEX initialSize=0)
Construct a new dynamic array of bits.
PScalarArray::PScalarArray
PScalarArray(T const *buffer, PINDEX length, PBoolean dynamic=true)
Construct a new dynamic array of elements of the specified type.
Definition: array.h:518
PAbstractArray::Concatenate
PBoolean Concatenate(const PAbstractArray &array)
Concatenate one array to the end of this array.
PNEW
#define PNEW
Macro for overriding system default new operator.
Definition: object.h:890
ReadFrom
virtual void ReadFrom(istream &strm)
Read the array.
PDWORDArray::PDWORDArray
PDWORDArray(PINDEX initialSize=0)
Construct a new dynamic array of unsigned longs.
PCharArray::ReadFrom
virtual void ReadFrom(istream &strm)
Read the array.
PAbstractArray::PAbstractArray
PAbstractArray(PINDEX elementSizeInBytes, PINDEX initialSize=0)
Create a new dynamic array of initalSize elements of elementSizeInBytes bytes each.