00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef __SEAL_MULTI_DIM_ARRAY_H__
00030 #define __SEAL_MULTI_DIM_ARRAY_H__
00031
00032 #include "coord.h"
00033
00034 namespace seal
00035 {
00036
00052 template <typename DataType, unsigned int dimension>
00053 class MultiDimArray : public MultiDimArray<MultiDimArray<DataType, dimension-1>, 1>
00054 {
00055
00056 friend class MultiDimArray<DataType, 1>;
00057
00058 public:
00059
00064 MultiDimArray(const Coordinate<dimension> &s);
00065
00068 MultiDimArray(const MultiDimArray<DataType, dimension> &a);
00069
00072 virtual ~MultiDimArray() {}
00073
00076 Coordinate<dimension> size(void) { return _size; }
00077
00078 MultiDimArray(void) {}
00079
00080 private:
00081 Coordinate<dimension> _size;
00082 };
00083
00084 template <typename DataType, unsigned int dimension>
00085 MultiDimArray<DataType, dimension>::MultiDimArray(const Coordinate<dimension> &s)
00086 : MultiDimArray< MultiDimArray<DataType, dimension-1>, 1 >(),
00087 _size(s)
00088 {
00089 MultiDimArray<MultiDimArray<DataType, dimension-1>, 1>::create(s[0]);
00090 Coordinate<dimension-1> rDim;
00091 for (int i = 1; i < dimension; i++)
00092 rDim[i-1] = s[i];
00093
00094 for (int i = 0; i < s[0]; i++)
00095 {
00096 MultiDimArray<MultiDimArray<DataType, dimension-1>, 1>::operator[](i) =
00097 MultiDimArray<DataType, dimension-1>(rDim);
00098 }
00099 }
00100
00101 template <typename DataType, unsigned int dimension>
00102 MultiDimArray<DataType, dimension>::MultiDimArray(const MultiDimArray<DataType, dimension> &a)
00103 : MultiDimArray<MultiDimArray<DataType, dimension-1>, 1>(a),
00104 _size(a._size)
00105 {}
00106
00110
00113 template <typename DataType>
00114 class MultiDimArray<DataType, 1>
00115 {
00116
00117 public:
00120 MultiDimArray(const Coordinate<1> &s);
00121
00124 MultiDimArray(const MultiDimArray<DataType, 1> &a);
00125
00128 virtual ~MultiDimArray();
00129
00132 Coordinate<1> size(void) { return _size; }
00133
00137 const DataType& operator[](const int index) const throw(OutOfRangeException<int>);
00138
00142 DataType& operator[](const int index) throw(OutOfRangeException<int>);
00143
00146 void operator=(const MultiDimArray<DataType, 1> &a);
00147
00148 void create(unsigned int size);
00149 MultiDimArray(void);
00150
00151 private:
00152 unsigned int *_refCountPtr;
00153 DataType *_dataPtr;
00154 Coordinate<1> _size;
00155 };
00156
00157 template<typename DataType>
00158 MultiDimArray<DataType, 1>::MultiDimArray(void)
00159 {
00160 _refCountPtr = new unsigned int; *_refCountPtr = 1;
00161 _size[0] = 0;
00162 _dataPtr = NULL;
00163 }
00164
00165 template <typename DataType>
00166 MultiDimArray<DataType, 1>::MultiDimArray(const Coordinate<1> &s)
00167 {
00168 _dataPtr = new DataType[s[0]];
00169 _refCountPtr = new unsigned int; *_refCountPtr = 1;
00170 _size[0] = s[0];
00171 }
00172
00173 template <typename DataType>
00174 MultiDimArray<DataType, 1>::MultiDimArray(const MultiDimArray<DataType, 1> &a)
00175 : _refCountPtr(a._refCountPtr), _dataPtr(a._dataPtr),
00176 _size(a._size)
00177 {
00178 (*_refCountPtr)++;
00179 }
00180
00181 template <typename DataType>
00182 MultiDimArray<DataType, 1>::~MultiDimArray()
00183 {
00184 if (*_refCountPtr > 1)
00185 (*_refCountPtr)--;
00186 else
00187 {
00188 delete _refCountPtr;
00189 delete [] _dataPtr;
00190 }
00191 }
00192
00193 template <typename DataType>
00194 void MultiDimArray<DataType, 1>::create(unsigned int s)
00195 {
00196 if (*_refCountPtr > 1)
00197 {
00198 (*_refCountPtr)--;
00199 _dataPtr = new DataType[s];
00200 _refCountPtr = new unsigned int; *_refCountPtr = 1;
00201 _size[0] = s;
00202 }
00203 else
00204 {
00205 delete _refCountPtr;
00206 delete [] _dataPtr;
00207
00208 _dataPtr = new DataType[s];
00209 _refCountPtr = new unsigned int; *_refCountPtr = 1;
00210 _size[0] = s;
00211 }
00212 }
00213
00214 template <typename DataType>
00215 void MultiDimArray<DataType, 1>::operator=(const MultiDimArray<DataType, 1> &a)
00216 {
00217 if (_refCountPtr == a._refCountPtr)
00218 return;
00219
00220 if (*_refCountPtr > 1)
00221 {
00222 (*_refCountPtr)--;
00223 _refCountPtr = a._refCountPtr;
00224 _dataPtr = a._dataPtr;
00225 _size[0] = a._size[0];
00226 (*_refCountPtr)++;
00227 }
00228 else
00229 {
00230 delete _refCountPtr;
00231 delete _dataPtr;
00232
00233 _refCountPtr = a._refCountPtr;
00234 _dataPtr = a._dataPtr;
00235 _size[0] = a._size[0];
00236 (*_refCountPtr)++;
00237
00238 }
00239 }
00240
00241 template <typename DataType>
00242 const DataType& MultiDimArray<DataType, 1>::operator[](const int index) const throw(OutOfRangeException<int>)
00243 {
00244 if (index >= _size[0] || index < 0)
00245 throw OutOfRangeException<int>(0, _size[0]-1, index);
00246
00247 return _dataPtr[index];
00248 }
00249
00250 template <typename DataType>
00251 DataType& MultiDimArray<DataType, 1>::operator[](const int index) throw(OutOfRangeException<int>)
00252 {
00253 if (index >= _size[0] || index < 0)
00254 throw OutOfRangeException<int>(0, _size[0]-1, index);
00255
00256 return _dataPtr[index];
00257 }
00258
00259 }
00260
00261 #endif // __SEAL_MULTI_DIM_ARRAY_H__
00262