/**
 * \file DynArray.h
 * \date 02.03.2002
 *
 * Template for the DynArray class
 */

#ifndef SUPPORT_DYNARRAY_H
#define SUPPORT_DYNARRAY_H

#include <assert.h>

/**
 * \class DynArray
 * \author Daniel Novomesky
 * \author Marek Lehuta
 * \brief Dynamic array encapsulation class
 */
template<class T,int maxvelkost>
class DynArray
{
public:
// Construction and destruction
	DynArray()
	{
		m_size = 0;
		for(int i=0;i<maxvelkost;i++)
			m_array[i] = 0;
	}

	~DynArray()
	{
		RemoveAll();
	}

// Operations
	int GetSize() const
	{
		return m_size;
	}

	int GetUpperBound( ) const
	{
		return GetSize() - 1;
	}

	T& operator[](int nIndex)
	{
		assert(nIndex < m_size && nIndex >=0);
		assert(m_array[nIndex]);

		return *(m_array[nIndex]);
	}

	T operator[]( int nIndex ) const
	{
		assert(nIndex < m_size && nIndex >=0);
		assert(m_array[nIndex]);

		return *(m_array[nIndex]);
	}

	int Add(const T& newElement)
	{
		assert(m_size < maxvelkost);
		m_array[m_size] = new T(newElement);

		m_size++;
		return m_size - 1;
	}

	void RemoveAt(int nIndex)
	{
		assert(nIndex < m_size && nIndex >=0);
		delete m_array[nIndex];
		for(int i= nIndex + 1; i<m_size;i++)
		{
			m_array[i-1] = m_array[i];
		}
		m_size--;
	}

	void RemoveAll()
	{
		for(int i=0;i<m_size;i++)
		{
			delete m_array[i];
			m_array[i] = 0;
		}
		m_size = 0;
	}

private:
	T* m_array[maxvelkost];
	int m_size;

};

///////////////////////////////////////////////////////////////////////////////

#endif	// __DYN_ARRAY__
