ARRAY.H

//////////////////////////////////////////////////////////////////////////////// 
// array.h
//
// Copyright (C) 1987-1997 By Microsoft Corp. All rights reserved.
// Copyright (C) 1997 Metawise Computing, Inc. All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////


#ifndef _ARRAY_H_
#define _ARRAY_H_

////////////////////////////////////////////////////////////////////////////////
// Array definition similar to CArray in MFC.
// We are only implementing CPtrArray here.

class CPtrArray
{
public:
// Constructor/Destructor
CPtrArray (UINT nInitSize = 0);
~CPtrArray ();

// Attributes
int GetSize () const;
int GetUpperBound () const;
void SetSize (int nNewSize, int nGrowBy = -1);
void* GetBuffer () const;// return internal buffer (array of ptrs)

// Operations
void FreeExtra ();
void RemoveAll ();

// Accessing Elements (nIndex is zero-based)
void* GetAt (int nIndex) const;
void SetAt (int nIndex, void* pNewElem);
void*& ElementAt (int nIndex);

// Potentially Growing the Array
void SetAtGrow (int nIndex, void* pNewElem);
int Add (void* pNewElem);
int Append (const CPtrArray& src);
void Copy (const CPtrArray& src);

// Insert/Removal
void RemoveAt (int nIndex, int nCount = 1);

// Overloaded Operator Helpers
void* operator [] (int nIndex) const;
void*& operator [] (int nIndex);

protected:
// Implementations
void** m_pData;// buffer pointer
int m_nSize;// #elem in array
int m_nMaxSize;// max allocated
int m_nGrowBy;// grow amount
};


////////////////////////////////////////////////////////////////////////////////
// Inline implementations of CPtrArray

#ifndef _DEBUG

inline int CPtrArray::GetSize () const
{
return m_nSize;
}

inline int CPtrArray::GetUpperBound () const
{
return m_nSize - 1;
}

inline void CPtrArray::RemoveAll ()
{
SetSize (0);
}

inline void* CPtrArray::GetAt (int nIndex) const
{
assert(0 <= nIndex && nIndex < m_nSize);
return m_pData [nIndex];
}

inline void CPtrArray::SetAt (int nIndex, void* pNewElem)
{
assert(0 <= nIndex && nIndex < m_nSize);
m_pData[nIndex] = pNewElem;
}

inline int CPtrArray::Add (void* pNewElem)
{
int nIndex = m_nSize;

SetAtGrow (nIndex, pNewElem);
return nIndex;
}

inline void*& CPtrArray::ElementAt (int nIndex)
{
assert(0 <= nIndex && nIndex < m_nSize);
return m_pData [nIndex];
}

inline void* CPtrArray::operator [] (int nIndex) const
{
return GetAt (nIndex);
}

inline void*& CPtrArray::operator [] (int nIndex)
{
assert(0 <= nIndex && nIndex < m_nSize);
return m_pData [nIndex];
}

inline void* CPtrArray::GetBuffer () const
{
return (void*) m_pData;
}

#endif // _DEBUG


#ifndef min
#define min(X, Y) (((X) <= (Y)) ? (X) : (Y))
#endif

#ifndef max
#define max(X, Y) (((X) >= (Y)) ? (X) : (Y))
#endif

#endif // _ARRAY_H_