首页 > 代码库 > 两维的动态数组的C++封装

两维的动态数组的C++封装

问题描述:

1.包装一个可扩展的动态的两维数组,支持泛型;

2.把存取的细节都包装起来,留一些简单的接口供外部使用;

3.稍作扩展可代替STL vector 使用;


程序代码:

#ifndef _TWODIM_ARRAY_H_
#define _TWODIM_ARRAY_H_

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

/*
* encapsulate two dimension array 
*
*/
template<class T, size_t ROW, size_t COL >
class TwoDimArray
{
public:
	TwoDimArray():m_storage(0), m_row(ROW), m_col(COL),
		          m_curRow(0), m_totalCount(0)

	{

	}

	/*
	*Copy constructor
	*
	*/
	template<class V, size_t row, size_t col >
	TwoDimArray( const TwoDimArray<V, row, col>& rhs ):m_storage(0), m_row(ROW), m_col(COL),
													   m_curRow(0), m_totalCount(0)
	{
		for( size_t i = 0; i < rhs.Size(); i++ )
		{
			Store( rhs[i] );
		}
	}

	/*
	* Destructor
	*
	*/
	~TwoDimArray()
	{
		Clear();
	}

	/*
	* Assignment operator overload
	*
	*/
	template<class V, size_t row, size_t col >
	TwoDimArray& operator = ( const TwoDimArray<V, row, col>& rhs )
	{
		if( this != &rhs )
		{
			Clear();

			for( size_t i = 0; i < rhs.m_totalCount; i++ )
			{
				Store( rhs[i] );
			}
		}

		return *this;
	}

	/*
	* Operator [] overload
	*
	*/
	T& operator [] ( int idx )
	{
		return m_storage[idx / m_col ][idx % m_col];
	}

	/*
	*operator [] overload
	*
	*/
	const T& operator [] ( int idx ) const 
	{
		return m_storage[idx / m_col ][idx % m_col];
	}

	/*
	* Clear all object
	*
	*/
	void Clear()
	{
		for( int i = 1; i < m_row; i++ )
		{
			delete [] m_storage[i];
		}

		delete [] m_storage;

		m_col = 0;
		m_row = 0;
		m_curRow = 0;
	}

	/*
	*
	*
	*/
	size_t Size() const
	{
		return m_totalCount;
	}

	/*
	*
	*
	*/
	void Reset()
	{
		m_totalCount = 0;
	}

	
	/*
	*
	*
	*/
	T& At( size_t idx )
	{
		return m_storage[idx / m_col ][idx % m_col];
	}

	/*
	*
	*
	*/
	const T& At( size_t idx ) const 
	{
		return m_storage[idx / m_col ][idx % m_col]
	}

	/*
	*
	*
	*/
	T& Cur( size_t idx )
	{
		return (*this)[idx];
	}

	/*
	*
	*
	*/
	T& Prev( size_t idx )
	{
		assert( idx < m_totalCount && idx > 0 );
		return (*this)[idx - 1];
	}

	/*
	*
	*
	*/
	T& Next( size_t idx )
	{
		assert( idx < m_totalCount - 1 );
		return (*this)[idx + 1];
	}

	/*
	*
	*
	*/
	const T& Cur( size_t idx) const 
	{
		return (*this)[idx];
	}

	/*
	*
	*
	*/
	const T& Prev( size_t idx ) const 
	{
		assert( idx < m_totalCount && idx > 0 );
		return (*this)[idx - 1];
	}

	/*
	*
	*
	*/
	const T& Next( size_t idx ) const 
	{
		assert( idx < m_totalCount - 1 );
		return (*this)[idx + 1];
	}

	/*
	* Store object to array
	*
	*/
	void Store( const T& data )
	{
		*Store() = data;
		m_totalCount++;
	}

private:
	/*
	*
	*
	*/
	T* Store()
	{
		if( !m_totalCount )
		{
			m_storage = new T*[m_row];
			memset( m_storage, 0x00, sizeof(T*)*m_row );

			m_storage[m_curRow] = new T[m_col];
			memset( m_storage[m_curRow], 0x00, sizeof(T) * m_col );

			return &m_storage[m_curRow][0];
		}

		size_t curRow = m_totalCount / m_col;
		if( curRow > m_curRow )
		{
			m_curRow++;
			m_storage[m_curRow] = new T[m_col];
			memset( m_storage[m_curRow], 0x00, sizeof(T) * m_col );

			return &m_storage[m_curRow][0];
		}
		else
		{
			return &m_storage[curRow][m_totalCount % m_col];
		}
	}

private:
	T**    m_storage;
	size_t m_col;
	size_t m_row;
	size_t m_curRow;
	size_t m_totalCount;

	
};

/*
*Test interface
*
*/
void TestTwoDimArray()
{
	TwoDimArray<int, 6, 7> dimArr;

	int len = 6 * 6;
	for( int i = 0; i < len; i++ )
	{
		dimArr.Store( i );
	}

	for( int i = 0; i < len; i++ )
	{
		std::cout << dimArr.At(i) << std::endl;
		assert( dimArr[i] == i );
	}

	TwoDimArray<int, 10, 10> newdimArr(dimArr);
	assert( newdimArr.Size() == dimArr.Size() );
	for( int i = 0; i < newdimArr.Size(); i++ )
	{
		assert( newdimArr[i] == dimArr[i] );
	}

}


#endif 

compile and run in visual studio 2005