C++ Integer Array


//==========================================================================
// IntArray.h  --  One and Two Dimensional Integer Arrays
//
//    The 2-D array can be dynamically resized
//
// Revision History:
//      11/01/95    Skews Me 	Created
//      11/06/95    		Comments
//
// UNDONE: 3-D array
//==========================================================================
#ifndef INTARRAY_H
#define INTARRAY_H

#ifndef _INC_IOSTREAM
    #include <iostream.h>
#endif

typedef int BOOL;

//==========================================================================
//  Class OneDIntArray -- One Dimensional Array
//
//  This class encapsulates a one dimensional array of integer values.
//
//  Observe the inline overloaded operator[] function.
//
//  Note the omission of bounds checking.
//  The OneDIntArray class is designed to be a workhorse and leaves
//  parameter validation the responsibility of the caller (or client).
//  It is up to the programer to keep track that each allocation succeeds.
//
//  The overloaded operator void* () provides a means to test the success
//  of the array allocation.
//  Another method of testing is by calling the member function OutOfBounds
//  (specifying the intended size)
//==========================================================================

//==========================================================================
//  OneDIntArray
//
//  The array uses the overloaded friend function operator<<
//  to insert into an output stream the contents of the array.
//  The format is simply the number followed by a space (e.g. "7 7 7 7 ")).
//==========================================================================
class OneDIntArray
{
public:
    OneDIntArray (const int size = 0);
        // Allocate m_array and set m_size iff size > 0

    OneDIntArray (const int size, const int value);
        // Allocate m_array, set m_size, and initialize each element to value
        // Assumes size is greater than zero

    OneDIntArray (const OneDIntArray& arraySrc);
        // Copies arraySrc into this
        // Assumes arraySrc is valid

   ~OneDIntArray (void);
        // Deallocates m_array

    BOOL OutOfBounds (const int index) const
        { return ((index < 0) || (index >= m_size)); }
        // Verifies that index is within range

    operator void* () { return (m_array ? this : NULL); }
        // Implicit conversion to something testable

    int& operator[] (const int index)
        { return m_array [index]; }
        // Returns an l-value at the given index into m_array
        // Assumes m_array is valid and index is not OutOfBounds

    friend ostream& operator<< (ostream& output, OneDIntArray& array);
        // Insert into stream:
        //      m_array [0] ... m_array [m_size-1]
        //      (e.g. "7 7 7 7 ")
        //
        // Assumes valid array and output stream

protected:

    int*    m_array;    // Array of integer values
    int     m_size;     // Size of array
}; // OneDIntArray

//==========================================================================
//  TwoDIntArray
//==========================================================================
class TwoDIntArray
{
public:

    TwoDIntArray (const int nrows = 0, const int ncolumns = 0); 
   ~TwoDIntArray (void);

    BOOL Resize (const int cRows, const int cColumns);
        // Deletes the current array and allocates a new 2-D array.
        // Allocates one 1-D array of length cRows, and fills each row with a
        // an array of length cColumns.
        // Returns TRUE if successful, otherwise FALSE
                
    operator void* () { return (m_array ? this : NULL); }
        // Implicit conversion to something testable

    BOOL OutOfBounds (const int row, const int column);      
        // returns TRUE if [row, column] is not a valid index into array,
        // otherwise FALSE
                
    operator void* () { return (m_array ? this : NULL); }
        // Used to test validity of the (new) array

    OneDIntArray& operator[] (const int row) { return *array [row]; }
        // Returns reference to a 1-D array at row.
        // Used with OneDIntArray::operator[] if the form
        //  array[x][y] = value;

    int operator= (const int value);
        // Initializes the elements of the two-D array to the value
        // Returns the value

    friend ostream& operator<< (ostream& output, TwoDIntArray& array);

private:

    void Delete (void);

    OneDIntArray** m_array;
    int            m_cRows;    // number of rows
    int            m_cColumns; // number of columns
}; // TwoDIntArray

#endif // INTARRAY_H



//==========================================================================
//  IntArray.cpp -- One and Two Dimensional Integer Arrays
//
//  Revision History:
//      11/01/95   Skews Me  Created
//      11/05/95             Optimizations, assertions and invariants
//==========================================================================
#include <iostream.h>
#include "intarray.h"

//==========================================================================
//  OneDIntArray (const int size)
//
//  Allocates an uninitialized array of the given size
//==========================================================================
OneDIntArray::OneDIntArray (const int size)
{
    if (size > 0)
    {
        m_array = new int [m_size = size];
        // ASSERT (m_array != NULL);
    }
    else
    {
        m_array = NULL;
    }
}

//==========================================================================
//  OneDIntArray (const int size, const int value)
//
//  Allocates an array initialized to value of the given size
//
// OPTIMIZATION:
//      Uses m_size as index when initializing m_array
//==========================================================================
OneDIntArray::OneDIntArray (const int sizeNew, const int value)
{
    // ASSERT (size > 0);
    if ((m_array = new int [sizeNew]) != NULL)
    {
        for (m_size = 0; m_size < sizeNew; m_size++)
        {   // INV: m_size <= sizeNew
            //   && m_array [0...m_size] have been initialized

            m_array [m_size] = value;
        }
        // ASSERT (m_size == sizeNew);
    }
    // ASSERT (m_array != NULL);
} // OneDIntArray

//==========================================================================
// OneDIntArray -- Copies the given array
//
// OPTIMIZATION:
//      Uses m_size as index when initializing m_array
//==========================================================================
OneDIntArray::OneDIntArray (const OneDIntArray& arraySource)
{
    // ASSERT (arraySource && arraySource.m_size > 0);
    if ((m_array = new int [arraySource.m_size]) != NULL)
    {
        for (m_size = 0; m_size < arraySource.m_size; m_size++)
        {   // INV: m_size <= arraySource.m_size
            //   && m_array [0...m_size] have been set to arraySource's

            m_array [m_size] = arraySource.m_array [m_size];
        }
        // ASSERT (m_size == arraySource.size);
    }
    // ASSERT (m_array != NULL);
} // OneDIntArray

//==========================================================================
//  ~OneDIntArray
//
//  Destructor: Deallocate and reset
//==========================================================================
OneDIntArray::~OneDIntArray (void)
{
    if (m_array != NULL)
    {
        delete [] m_array;
        m_array = NULL;
        m_size  = 0;
    }
} // ~OneDIntArray

//==========================================================================
//  operator<<
//
//  Inserts into an output stream:
//      m_array [0] m_array [1] ... m_array [m_size-1]
//      (e.g. "7 7 7 7 ")
//
//  DEVNOTE UNDONE: Microsoft Visual C++ 1.50:
//      Use setw (doesn't work without some preprocessor directives defined)
//
//  Assumes valid array and output stream
//==========================================================================
ostream& operator<< (ostream& output, OneDIntArray& array)
{
    // ASSERT (array.m_array);
    // ASSERT (output);
    for (int index = 0;
         index < array.m_size && (output << array.m_array [index] << ' ');
         index++);
         // INV: index <= array.m_size
         //   && indexed space-deliminated array value has been output

    // ASSERT (output);
    return (output << endl);
} // operator<<

//==========================================================================
//  TwoDIntArray (const int nRows, const int nColumns)
//
//  If nRows and nColumns are positive, call Resize to allocate array
//==========================================================================
TwoDIntArray::TwoDIntArray : m_array (NULL) (const int cRows, const int cColumns)
{
    if ((cRows > 0) && (cColumns > 0))
    {
        Resize (cRows, cColumns);
    }
}

//==========================================================================
//  ~TwoDIntArray (void)
//==========================================================================
TwoDIntArray::~TwoDIntArray (void)
{
    Delete ();
}

//==========================================================================
//  Resize (int nRows, int nColumns)     
//
//  Allocates the 2-D array and initializes m_cRows & m_cColumns
//==========================================================================
BOOL TwoDIntArray::Resize (const int cRows, const int cColumns)
{
    if (m_array != NULL)
    {
        Delete ();
    }

    if ((m_array = new OneDIntArray* [cRows]) != NULL)
    {
         m_cRows = cRows;

         for (int row = 0, m_cColumns = 0; row < cRows; row++)
         {
             if ((m_array [row] = new OneDIntArray (cColumns)) != NULL)
             {
                 ++m_cColumns;
             }
             else
             {
                 // Error: Delete allocated arrays and break from allocation loop
                 for (int rowDel = 0; rowDel < row; rowDel++)
                 {
                     delete m_array [rowDel];
                 }
                 delete m_array;
                 m_array = NULL;
                 m_cRows = m_cColumns = 0;
                 return FALSE;
             }
        }
    }
    return ((m_cRows == cRows) && (m_cColumns == cColumns));
}

//==========================================================================
//  void Delete (void)
//==========================================================================
void TwoDIntArray::Delete (void)
{
    if (m_array != NULL)
    {
        for (int row = 0; row < m_cRows; row++)
        {
            delete m_array [row];
        }
        delete [] m_array;
        m_array = NULL;
    }
}

//==========================================================================
//  BOOL OutOfBounds (int row, int column)
//==========================================================================
BOOL TwoDIntArray::OutOfBounds (const int row, const int column)
{
    if ((row >= 0) && (row < m_cRows) && (column >= 0) && (column < m_cColumns))
    {
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

//==========================================================================
// int operator= (const int value)
//
// Set all elements of array to the value, return the value
//==========================================================================
int TwoDIntArray::operator= (const int value)
{
    for (int row = 0; row < m_cRows; row++)
    {
        for (int column = 0; column < m_cColumns; column++)
        {
            (*array [row]) [column] = value;
        }
    }
    return value;
}

//==========================================================================
//  operator<<
//==========================================================================
ostream& operator<< (ostream& output, TwoDIntArray& array)
{
    for (int row = 0; row < array.m_cRows && (output << array [row]); row++);
    return (output << endl);
}

See Also

Doubly-linked circular pointer list

More source code
misc keywords: c++ return array c++ array return c++