home *** CD-ROM | disk | FTP | other *** search
- #ifndef __ARRAY2D_H__
- #define __ARRAY2D_H__
-
- #include <algorithm>
- #include "safe_new.h"
-
- template <class T, int Safe = 0>
- class Array2D
- {
- public:
- typedef unsigned size_type;
- typedef int difference_type;
- typedef T & reference;
- typedef const T & const_reference;
- typedef T value_type;
- typedef T * pointer;
- typedef const T * const_pointer;
- typedef pointer iterator;
- typedef const_pointer const_iterator;
-
- class ArrayRow
- {
- friend class Array2D<T>;
- pointer data;
- size_type s_x,s_y;
- mutable size_type x;
- public:
- reference operator[]( size_type y )
- {
- if( Safe && (x >= s_x || y >= s_y) )
- {
- throw std::out_of_range( "Array2D::ArrayRow::operator[]() array bounds error" );
- }
- return *(data + y * s_x + x);
- }
- const_reference operator[]( size_type y )const
- {
- if( Safe && (x >= s_x || y >= s_y) )
- {
- throw std::out_of_range( "Array2D::ArrayRow::operator[]()const array bounds error" );
- }
- return *(data + y * s_x + x);
- }
- };
-
- Array2D( size_type s_x, size_type s_y, const T & v )
- {
- create( s_x, s_y );
- clear( v );
- }
-
- Array2D( size_type s_x, size_type s_y )
- {
- create( s_x, s_y );
- }
-
- Array2D()
- {
- create( 0, 0 );
- }
-
- virtual ~Array2D()
- {
- delete [] row.data;
- }
-
- Array2D( const Array2D<T> & a )
- {
- create( a.cx(), a.cy() );
- std::copy( a.begin(), a.end(), begin() );
- }
-
- /* Σδ Ωε∞∩Φδ ≥ε≡εΓ, Ωε≥ε≡√σ ∩εΣΣσ≡µΦΓα■≥ member template functions
- template<class U>
- Array2D( const Array2D<U> & a )
- : Array2D( a.row.s_x, a.row.s_y )
- {
- for( int n = 0; n < row.s_y * row.s_x; ++n )
- row.data[ n ] = a.row.data[ n ];
- }*/
-
- const Array2D & operator= ( const Array2D<T> & a )
- {
- if( this != &a )
- {
- delete [] row.data; row.data = 0;
- create( a.cx(), a.cy() );
- std::copy( a.begin(), a.end(), begin() );
- }
- return *this;
- }
-
- /* Σδ Ωε∞∩Φδ ≥ε≡εΓ, Ωε≥ε≡√σ ∩εΣΣσ≡µΦΓα■≥ member template functions
- template<class U>
- const Array2D & operator= ( const Array2D<U> & a )
- {
- if( this != &a )
- {
- delete [] row.data; row.data = NULL;
- create( a.s_x, a.s_y );
- for( int n = 0; n < row.s_y * row.s_x; ++n )
- row.data[ n ] = a.row.data[ n ];
- }
- return *this;
- }*/
-
- ArrayRow & operator[]( size_type x )
- {
- row.x = x;
- return row;
- }
-
- const ArrayRow & operator[]( size_type x )const
- {
- row.x = x;
- return row;
- }
-
- void clear( const T & v = T() )
- {
- std::fill( begin(), end(), v );
- }
-
- size_type cy()const
- {
- return row.s_y;
- }
-
- size_type cx()const
- {
- return row.s_x;
- }
-
- const_iterator begin()const
- {
- return row.data;
- }
- iterator begin()
- {
- return row.data;
- }
- const_iterator end()const
- {
- return row.data + row.s_x * row.s_y;
- }
- iterator end()
- {
- return row.data + row.s_x * row.s_y;
- }
- size_type offset( size_type x, size_type y )const
- {
- return y * row.s_x + x;
- }
- bool is_inside( size_type x, size_type y )const
- {
- return x < cx() && y < cy();
- }
- protected:
- ArrayRow row;
- private:
- void create( size_type s_x, size_type s_y )
- {
- row.s_y = s_y;
- row.s_x = s_x;
- row.data = new T[ row.s_x * row.s_y ];
- }
- };
-
- #endif //__ARRAY2D_H__