home *** CD-ROM | disk | FTP | other *** search
/ Game.EXE 2002 April / Game.EXE_04_2002.iso / Alawar / Array2D.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-02  |  3.1 KB  |  169 lines

  1. #ifndef __ARRAY2D_H__
  2. #define __ARRAY2D_H__
  3.  
  4. #include <algorithm>
  5. #include "safe_new.h"
  6.  
  7. template <class T, int Safe = 0>
  8. class Array2D
  9. {
  10. public:
  11.     typedef unsigned size_type;
  12.     typedef int difference_type;
  13.     typedef T & reference;
  14.     typedef const T & const_reference;
  15.     typedef T value_type;
  16.     typedef T * pointer;
  17.     typedef const T * const_pointer;
  18.     typedef pointer iterator;
  19.     typedef const_pointer const_iterator;
  20.  
  21.     class ArrayRow
  22.     {
  23.         friend class Array2D<T>;
  24.         pointer data;
  25.         size_type s_x,s_y;
  26.         mutable size_type x;
  27.     public:
  28.         reference operator[]( size_type y )
  29.         {
  30.             if( Safe && (x >= s_x || y >= s_y) )
  31.             {
  32.                 throw std::out_of_range( "Array2D::ArrayRow::operator[]() array bounds error" );
  33.             }
  34.             return *(data + y * s_x + x);
  35.         }
  36.         const_reference operator[]( size_type y )const
  37.         {
  38.             if( Safe && (x >= s_x || y >= s_y) )
  39.             {
  40.                 throw std::out_of_range( "Array2D::ArrayRow::operator[]()const array bounds error" );
  41.             }
  42.             return *(data + y * s_x + x);
  43.         }
  44.     };
  45.  
  46.     Array2D( size_type s_x, size_type s_y, const T & v )
  47.     {
  48.         create( s_x, s_y );
  49.         clear( v );
  50.     }
  51.  
  52.     Array2D( size_type s_x, size_type s_y )
  53.     {
  54.         create( s_x, s_y );
  55.     }
  56.  
  57.     Array2D()
  58.     {
  59.         create( 0, 0 );
  60.     }
  61.  
  62.     virtual ~Array2D()
  63.     {
  64.         delete [] row.data;
  65.     }
  66.  
  67.     Array2D( const Array2D<T> & a )
  68.     {
  69.         create( a.cx(), a.cy() );
  70.         std::copy( a.begin(), a.end(), begin() );
  71.     }
  72.  
  73. /*    Σδ  Ωε∞∩Φδ ≥ε≡εΓ, Ωε≥ε≡√σ ∩εΣΣσ≡µΦΓα■≥ member template functions
  74.     template<class U>
  75.     Array2D( const Array2D<U> & a )
  76.         :    Array2D( a.row.s_x, a.row.s_y )
  77.     {
  78.         for( int n = 0; n < row.s_y * row.s_x; ++n )
  79.             row.data[ n ] = a.row.data[ n ];
  80.     }*/
  81.  
  82.     const Array2D & operator= ( const Array2D<T> & a )
  83.     {
  84.         if( this != &a )
  85.         {
  86.             delete [] row.data; row.data = 0;
  87.             create( a.cx(), a.cy() );
  88.             std::copy( a.begin(), a.end(), begin() );
  89.         }
  90.         return *this;
  91.     }
  92.     
  93. /*    Σδ  Ωε∞∩Φδ ≥ε≡εΓ, Ωε≥ε≡√σ ∩εΣΣσ≡µΦΓα■≥ member template functions
  94.     template<class U>
  95.     const Array2D & operator= ( const Array2D<U> & a )
  96.     {
  97.         if( this != &a )
  98.         {
  99.             delete [] row.data; row.data = NULL;
  100.             create( a.s_x, a.s_y );
  101.             for( int n = 0; n < row.s_y * row.s_x; ++n )
  102.                 row.data[ n ] = a.row.data[ n ];
  103.         }
  104.         return *this;
  105.     }*/
  106.     
  107.     ArrayRow & operator[]( size_type x )
  108.     {
  109.         row.x = x;
  110.         return row;
  111.     }
  112.  
  113.     const ArrayRow & operator[]( size_type x )const
  114.     {
  115.         row.x = x;
  116.         return row;
  117.     }
  118.  
  119.     void clear( const T & v = T() )
  120.     {
  121.         std::fill( begin(), end(), v );
  122.     }
  123.  
  124.     size_type cy()const
  125.     {
  126.         return row.s_y;
  127.     }
  128.  
  129.     size_type cx()const
  130.     {
  131.         return row.s_x;
  132.     }
  133.  
  134.     const_iterator begin()const
  135.     {
  136.         return row.data;
  137.     }
  138.     iterator begin()
  139.     {
  140.         return row.data;
  141.     }
  142.     const_iterator end()const
  143.     {
  144.         return row.data + row.s_x * row.s_y;
  145.     }
  146.     iterator end()
  147.     {
  148.         return row.data + row.s_x * row.s_y;
  149.     }
  150.     size_type offset( size_type x, size_type y )const
  151.     {
  152.         return y * row.s_x + x;
  153.     }
  154.     bool is_inside( size_type x, size_type y )const
  155.     {
  156.         return x < cx() && y < cy();
  157.     }
  158. protected:
  159.     ArrayRow row;
  160. private:
  161.     void create( size_type s_x, size_type s_y )
  162.     {
  163.         row.s_y = s_y;
  164.         row.s_x = s_x;
  165.         row.data = new T[ row.s_x * row.s_y ];
  166.     }
  167. };
  168.  
  169. #endif //__ARRAY2D_H__