/*
Copyright (c) 2001                      RIPE NCC


All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of the author not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

/*
-------------------------------------------------------------------------------
Module Header
Filename          : DynamicArray.h
Author            : Rene Wilhelm
Date              : 20-AUG-2001
Description       : Template class for allocating and handling two and three dimensional
		    arrays of which the exact sizes are not known at compile time.
Language Version  : C++
Note		  : when array elements are complex objects, the assignment
		    operator will likely have to overloaded in the object's
		    class defition to properly copy values into the array
		    with SetElement function
TODO		  : exception handling for range errors in GetElement functions
OSs Tested        : Debian Linux 2.2
$Id: DynamicArray.h,v 1.1 2002/08/09 23:59:36 wilhelm Exp $
-------------------------------------------------------------------------------
*/

#ifndef DynamicArray_incluced
#define DynamicArray_incluced

#include <stdlib.h>
#include <iostream.h>

template<class T>
class DynamicArray {
private:
	int size;
	int sizeX;
	int sizeY;
	int sizeZ;
	T *data;
public:
	~DynamicArray();	
	DynamicArray(); 			
	void SetSize(int x, int y);
	void SetSize(int x, int y, int z);
	T& GetElement(int x, int y);
	T& GetElement(int x, int y, int z);
	void InitValue(const T& value);			  // init all elements
	void SetElement(int x, int y, const T& value);
	void SetElement(int x, int y, int z, const T& value);
};

template<class T> DynamicArray<T>::~DynamicArray()  {
	if (data != NULL) {
		delete [] data;
	}
};

template<class T> DynamicArray<T>::DynamicArray() {
	size  = 0;
	sizeX = 0;
	sizeY = 0; 
	sizeZ = 0; 
	data  = NULL;
};

template<class T> void DynamicArray<T>::SetSize(int x, int y) {
	if (data != NULL) {
		cerr << "DynamicArray: memory already allocated\n";
		return;
	}
	sizeX = x;
	sizeY = y;
	size  = sizeX * sizeY;
	data = new T[size];
};

template<class T> void DynamicArray<T>::SetSize(int x, int y, int z) {
	if (data != NULL) {
		cerr << "DynamicArray: memory already allocated\n";
		return;
	}
	sizeX = x;
	sizeY = y;
	sizeZ = z;
	size  = sizeX * sizeY * sizeZ;
	data = new T[size];
};

// GetElement functions return a reference to the element

template<class T> T& DynamicArray<T>::GetElement(int x, int y) {
	// range check x < sizeX , y < sizeY
	if (x < 0 || x > sizeX-1) {
		cerr << "DynamicArray: x index " << x << " out of range  [0-" << sizeX-1 <<"].\n";
	}
	if (y < 0 || y > sizeY-1) {
		cerr << "DynamicArray: y indey " << y << " out of range  [0-" << sizeY-1 <<"].\n";
	}
	int elementno = y * sizeX + x;
	return(data[elementno]);
};

template<class T> T& DynamicArray<T>::GetElement(int x, int y, int z) {
	// range check x < sizeX , y < sizeY, z < sizeZ
	if (x < 0 || x > sizeX-1) {
		cerr << "DynamicArray: x index " << x << " out of range  [0-" << sizeX-1 <<"].\n";
	}
	if (y < 0 || y > sizeY-1) {
		cerr << "DynamicArray: y indey " << y << " out of range  [0-" << sizeY-1 <<"].\n";
	}
	if (z < 0 || z > sizeZ-1) {
		cerr << "DynamicArray: z indey " << z << " out of range  [0-" << sizeZ-1 <<"].\n";
	}
	int elementno = z * sizeX * sizeY + y * sizeX + x;
	return(data[elementno]);
};

// InitValue and SetElement assign values to aray elements;
// special overloaded copy assign operator will be needed to properly
// handle complex objects with e.g. pointers among the data elements
// (default assignment copies pointer values, where one probably wants
//  the pointed to data items copied)

template<class T> void DynamicArray<T>::InitValue(const T& value) {
	// initialize all elements with particular value

	for (int i=0; i < size; i++) {
		data[i] = value;
	}
}

template<class T> void DynamicArray<T>::SetElement(int x, int y, const T& value) {

	// range check x < sizex , y < sizeY
	if (x < 0 || x > sizeX-1) {
		cerr << "DynamicArray: x index " << x << " out of range  [0-" << sizeX-1 <<"].\n";
		return;
	}
	if (y < 0 || y > sizeY-1) {
		cerr << "DynamicArray: y index " << y << " out of range  [0-" << sizeY-1 <<"].\n";
		return;
	}

	int elementno = y * sizeX + x;
	data[elementno] = value;
};

template<class T> void DynamicArray<T>::SetElement(int x, int y, int z, const T& value) {

	// range check x < sizex , y < sizeY, z < sizeZ
	if (x < 0 || x > sizeX-1) {
		cerr << "DynamicArray: x index " << x << " out of range  [0-" << sizeX-1 <<"].\n";
		return;
	}
	if (y < 0 || y > sizeY-1) {
		cerr << "DynamicArray: y index " << y << " out of range  [0-" << sizeY-1 <<"].\n";
		return;
	}
	if (z < 0 || z > sizeZ-1) {
		cerr << "DynamicArray: z index " << z << " out of range  [0-" << sizeZ-1 <<"].\n";
		return;
	}

	int elementno = z * sizeX * sizeY + y * sizeX + x;
	data[elementno] = value;
};

#endif
