posts - 6,comments - 2,trackbacks - 0

#ifndef N_GRID_H_
#define N_GRID_H_
#include <cassert>
#include <iostream>

template <typename Type, int N>
class N_Grid
{
public:
 //constructors and destructors
 N_Grid ();
 N_Grid (int size);
 N_Grid (const N_Grid<Type, N> & );
 ~N_Grid ();
 //member methods
 N_Grid<Type, N> & operator = (const N_Grid<Type, N> & );
 void resize (int size );
 N_Grid<Type, N-1> & operator [] (int i);
 const N_Grid<Type, N-1> & operator [] (int i)const ;
 void print (std::ostream & os = std::cout)const;
 int get_size ()const { return _size; }
 static const int _Default_size = 10;
protected:
 void copy_from (const N_Grid<Type, N> & ); 
 int _size;
 N_Grid<Type, N-1> *_elem;
};
//definitions

template <typename Type, int N>
N_Grid<Type, N>::N_Grid(): _size (_Default_size)
{
 _elem = new N_Grid<Type, N-1> [_size];
}

template <typename Type, int N>
N_Grid<Type, N>::N_Grid(int size) : _size (size)
{
 _elem = new N_Grid<Type, N-1> [_size];
 //Allocate the array above calls the 0-argument
 //constructor for the N_Grid<Type, N-1>, which
 //constructs it with the default size.
 for (int i = 0; i < _size; i ++)
  _elem[i].resize (_size);
}

template <typename Type, int N>
N_Grid<Type, N>::N_Grid(const N_Grid<Type,N> & n_grid)
{
 copy_from (n_grid);
}

template <typename Type, int N>
N_Grid<Type, N>::~N_Grid ()
{
 delete [] _elem;
}

template <typename Type, int N>
void N_Grid<Type, N>::copy_from(const N_Grid<Type,N> & n_grid)
{
 _size = n_grid._size;
 _elem = new N_Grid<Type, N-1> [_size];
 for (int i = 0; i < _size; i++)
  _elem[i] = n_grid._elem [i];
}

template <typename Type, int N>
N_Grid<Type, N> & N_Grid<Type, N>::operator =(const N_Grid<Type,N> & n_grid)
{
 if (this == &n_grid)
  return *this;
 delete [] _elem;
 copy_from (n_grid);
 return *this;
}

template <typename Type, int N>
void N_Grid<Type, N>::resize(int size)
{
 N_Grid<Type, N> temp (*this);
 _size = size;
 _elem = new N_Grid<Type, N-1> [_size];
 for (int i = 0; i < _size && i < temp._size; i++)
 {
  _elem[i] = temp._elem[i];
  //risize the nested Grid elements recursively
  _elem[i].resize (_size);
 }
}

template <typename Type, int N>
N_Grid<Type, N-1> & N_Grid<Type, N>::operator [](int i)
{
 assert (i >= 0 && i < _size);
 return _elem[i];
}

template <typename Type, int N>
const N_Grid<Type, N-1> & N_Grid<Type, N>::operator [](int i) const
{
 assert (i >= 0 && i < _size);
 return _elem[i];
}

template <typename Type, int N>
void N_Grid<Type, N>::print(std::ostream &os = std::cout) const
{
 for (int i = 0; i < _size ; i++)
  _elem[i].print ();
 os << '\n';
}

template <typename Type>
class N_Grid<Type, 1>
{
public:
 //constructors and destructors
 N_Grid ();
 N_Grid (int size);
 N_Grid (const N_Grid<Type, 1> & );
 ~N_Grid ();
 //member methods
 N_Grid<Type, 1> & operator = (const N_Grid<Type, 1> & );
 void resize (int size );
 N_Grid<Type, 1> & operator [] (int i);
 const N_Grid<Type, 1> & operator [] (int i)const ;
 void print (std::ostream & os = std::cout)const;
 int get_size ()const { return _size; }
 static const int _Default_size = 10;
protected:
 void copy_from (const N_Grid<Type, 1> & ); 
 int _size;
 N_Grid<Type, 1> *_elem;
};
template <typename Type>
N_Grid<Type, 1>::N_Grid() : _size (_Default_size)
{
 _elem = new Type [_size];
}

template <typename Type>
N_Grid<Type, 1>::N_Grid(int size) : _size (size)
{
 _elem = new Type [_size];
}

template <typename Type>
N_Grid<Type, 1>::N_Grid(const N_Grid<Type, 1> & grid)
{
 copy_from (grid);
}

template <typename Type>
N_Grid<Type, 1>::~N_Grid()
{
 delete [] _elem;
}

template <typename Type>
void N_Grid<Type, 1>::copy_from(const N_Grid<Type,1> & grid)
{
 _size = grid._size;
 _elem = new Type [_size];
 for (int i = 0; i < _size; i++)
  _elem[i] = grid._elem[i];
}

template <typename Type>
N_Grid<Type, 1> & N_Grid<Type, 1>::operator =(const N_Grid<Type,1> & grid)
{
 if (this == &grid)
  return *this;
 delete [] _elem;
 copy_from (grid);
 return *this;
}

template <typename Type>
void N_Grid<Type, 1>::resize(int size)
{
 N_Grid<Type, 1> temp (*this);
 _size = size;
 _elem = new Type[_size];
 for (int i = 0; i < _size; i++)
  _elem[i] = temp._elem[i];
}

template <typename Type>
N_Grid<Type, 1> & N_Grid<Type, 1>::operator [](int i)
{
 assert (i >= 0 && i < _size);
 return _elem[i];
}

template <typename Type>
const N_Grid<Type, 1> & N_Grid<Type, 1>::operator [](int i) const
{
 assert (i >= 0 && i < _size);
 return _elem[i];
}

template <typename Type>
void N_Grid<Type, 1>::print(std::ostream &os = std::cout) const
{
 for (int i = 0; i < _size; i++)
   os << _size;
 os << '\n';
}
#endif

posted on 2006-04-11 20:29 Jonathan 阅读(203) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理