Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

MNArray.h

Go to the documentation of this file.
00001 /* Copyright (C) 2000 KOM/Darmstadt University of Technology 00002 * 00003 * You are allowed to use all other parts of the code under the following terms: 00004 * 00005 * For non-commercial use, code may be used in unmodified form provided 00006 * that this copyright notice and this permission notice appear in 00007 * supporting documentation. 00008 * 00009 * This software is provided "as is" and without any express or implied 00010 * warranties, including, without limitation, the implied warranty of 00011 * fitness for a particular purpose. 00012 * 00013 * The code may be subjected to the GNU General Public License, Version 2, 00014 * and re-distributed under the terms of this license. 00015 * As a special exception, permission is granted to link this code 00016 * with the Qt library and distribute executables, as long as you 00017 * follow the requirements of the GNU GPL in regard to all of the 00018 * software in the executable aside from Qt. 00019 * 00020 * Commercial use other than under the terms of the GNU General Public 00021 * License is allowed only after express negotiation of conditions 00022 * with the authors. 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 /* MN_ARRAY_H */ 00138

Generated on Sun Mar 6 13:35:49 2005 for Komssys by doxygen 1.3.8