00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
#ifndef MN_ARRAY_H
00025
#define MN_ARRAY_H
00026
00027
#include "MNValid.h"
00028
00029 template <
class T>
class MNArray2
00030 {
00031 DECLARE_VALID
00032
private:
00033
int _xsize;
00034
int _ysize;
00035 T* _array;
00036
00037
typedef T Item;
00038
public:
00039 MNArray2(
int x,
int y ) {
00040 _xsize = x;
00041 _ysize = y;
00042 _array =
new Item[ x * y ];
00043 memset ( _array,
'\0', x*y*
sizeof(Item) );
00044 MAKE_VALID
00045 }
00046
00047 MNArray2(
const MNArray2<T>& ar ) {
00048 _xsize = ar.
xsize();
00049 _ysize = ar.
ysize();
00050 _array =
new Item[ _xsize * _ysize ];
00051 memcpy ( _array, ar.
array(), _xsize*_ysize*
sizeof(Item) );
00052 MAKE_VALID
00053 }
00054
00055 ~MNArray2( ) {
00056 MAKE_INVALID
00057
delete [] _array;
00058 }
00059
00060 inline T&
operator()(
int x,
int y) {
00061 CHECK_VALID
00062 assert( 0 <= x );
00063 assert( 0 <= y );
00064 assert( x < _xsize);
00065 assert( y < _ysize);
00066
return _array[ y*_xsize + x ];
00067 }
00068
00069 inline int xsize()
const { CHECK_VALID
return _xsize; }
00070 inline int ysize()
const { CHECK_VALID
return _ysize; }
00071 inline int low1() const { CHECK_VALID
return 0; }
00072 inline int low2() const { CHECK_VALID
return 0; }
00073 inline int high1()
const { CHECK_VALID
return _xsize; }
00074 inline int high2()
const { CHECK_VALID
return _ysize; }
00075
00076 inline T*
array()
const {
00077 CHECK_VALID
00078
return _array;
00079 }
00080 };
00081
00082 template <
class T>
class MNArrayN
00083 {
00084 DECLARE_VALID
00085
private:
00086
int _dimension;
00087
int _length;
00088
int* _sizes;
00089 T* _array;
00090
00091
typedef T Item;
00092
public:
00093 MNArrayN(
int dimension,
int* sizes ) {
00094 _dimension = dimension;
00095 _sizes =
new int[dimension];
00096 _length = 1;
00097
for (
int i=0; i<_dimension; i++ )
00098 {
00099 _length *= sizes[i];
00100 _sizes[i] = sizes[i];
00101 }
00102 _array =
new Item[ _length ];
00103 memset ( _array,
'\0', _length*
sizeof(Item) );
00104 MAKE_VALID
00105 }
00106
00107 ~MNArrayN( ) {
00108 MAKE_INVALID
00109
delete [] _array;
00110
delete [] _sizes;
00111 }
00112
00113 inline T&
operator()(
int* v) {
00114 CHECK_VALID
00115
int offset = 0;
00116
int factor = 1;
00117
for (
int i=0; i<_dimension; i++ )
00118 {
00119 assert ( 0 <= v[i] );
00120 assert ( v[i] <= _sizes[i] );
00121 offset += v[i] * factor;
00122 factor *= _sizes[i];
00123 }
00124
return _array[ offset ];
00125 }
00126
00127 inline int size(
int d )
const { CHECK_VALID
return _sizes[d]; }
00128 inline int low (
int d )
const { CHECK_VALID
return 0; }
00129 inline int high(
int d )
const { CHECK_VALID
return _sizes[d]; }
00130
00131 inline T*
array()
const {
00132 CHECK_VALID
00133
return _array;
00134 }
00135 };
00136
00137
#endif
00138