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_CELL_ARRAY_H__
00030 #define __SEAL_CELL_ARRAY_H__
00031
00032 #include "exception.h"
00033 #include "array1d.h"
00034 #include <sstream>
00035 #include <iostream>
00036
00037 namespace seal
00038 {
00039
00040 template <typename DataType>
00041 class Cell;
00042
00043 template <typename DataType>
00044 class CellArray;
00045
00046 template <typename DataType>
00047 std::ostream& operator<<(std::ostream& ostr, seal::Cell<DataType> &c);
00048
00052 template <typename DataType>
00053 class Cell
00054 {
00055
00056 friend class CellArray<DataType>;
00057
00058 template <typename type>
00059 friend std::ostream& operator<<(std::ostream& ostr, seal::Cell<type> &c);
00060
00061 public:
00062
00065 Cell(void);
00066
00069 Cell(const Cell &cell);
00070
00073 ~Cell();
00074
00077 void operator=(const DataType &val);
00078
00083 void operator=(const Cell<DataType> &rhs);
00084
00085 private:
00086 DataType *_dataPtr;
00087 unsigned int *_refCountPtr;
00088 };
00089
00090 template <typename DataType>
00091 Cell<DataType>::Cell(void)
00092 {
00093 _refCountPtr = new unsigned int; *_refCountPtr = 1;
00094 _dataPtr = new DataType;
00095 }
00096
00097 template <typename DataType>
00098 Cell<DataType>::Cell(const Cell<DataType> &bit)
00099 : _dataPtr(bit._dataPtr), _refCountPtr(bit._refCountPtr)
00100 {
00101 (*_refCountPtr)++;
00102 }
00103
00104 template <typename DataType>
00105 Cell<DataType>::~Cell()
00106 {
00107 if (*_refCountPtr > 1)
00108 (*_refCountPtr)--;
00109 else
00110 {
00111 delete _refCountPtr;
00112 delete _dataPtr;
00113 }
00114 }
00115
00116 template <typename DataType>
00117 void Cell<DataType>::operator=(const DataType &val)
00118 {
00119 *_dataPtr = val;
00120 }
00121
00122 template <typename DataType>
00123 void Cell<DataType>::operator=(const Cell<DataType> &bit)
00124 {
00125 if (_refCountPtr == bit._refCountPtr)
00126 return;
00127
00128 if (*_refCountPtr > 1)
00129 {
00130 (*_refCountPtr)--;
00131
00132 _refCountPtr = bit._refCountPtr;
00133 _dataPtr = bit._dataPtr;
00134
00135 (*_refCountPtr)++;
00136 }
00137 else
00138 {
00139 delete _refCountPtr;
00140 delete _dataPtr;
00141
00142 _refCountPtr = bit._refCountPtr;
00143 _dataPtr = bit._dataPtr;
00144
00145 (*_refCountPtr)++;
00146 }
00147 }
00148
00152
00155 template <typename DataType>
00156 class CellArray : public Array1D< Cell<DataType> >
00157 {
00158 public:
00162 CellArray(unsigned int size) : Array1D< Cell<DataType> >(size) {}
00163
00166 CellArray(const CellArray<DataType> &a) : Array1D< Cell<DataType> >(a) {}
00167
00177 CellArray<DataType> operator()(int lower, int upper) throw (OutOfRangeException<int>);
00178
00188 const CellArray<DataType> operator()(int lower, int upper) const throw (OutOfRangeException<int>);
00189
00194 CellArray<DataType> operator,(const CellArray<DataType> &a);
00195
00196 };
00197
00198 template <typename DataType>
00199 CellArray<DataType> CellArray<DataType>::operator()(int lower, int upper) throw (OutOfRangeException<int>)
00200 {
00201 unsigned int size = Array1D< Cell<DataType> >::size();
00202 if (lower < 0 || lower > size-1)
00203 throw OutOfRangeException<int>(0, size-1, lower);
00204 if (upper < 0 || upper > size-1)
00205 throw OutOfRangeException<int>(0, size-1, lower);
00206
00207 CellArray<DataType> newArray(upper-lower+1);
00208 for (int i = lower; i <= upper; i++)
00209 newArray[i-lower] = Array1D< Cell<DataType> >::operator[](i);
00210
00211 return newArray;
00212 }
00213
00214 template <typename DataType>
00215 const CellArray<DataType> CellArray<DataType>::operator()(int lower, int upper) const throw (OutOfRangeException<int>)
00216 {
00217 unsigned int size = Array1D< Cell<DataType> >::size();
00218 if (lower < 0 || lower > size-1)
00219 throw OutOfRangeException<int>(0, size-1, lower);
00220 if (upper < 0 || upper > size-1)
00221 throw OutOfRangeException<int>(0, size-1, lower);
00222
00223 CellArray<DataType> newArray(upper-lower+1);
00224 for (int i = lower; i <= upper; i++)
00225 newArray[i-lower] = Array1D< Cell<DataType> >::operator[](i);
00226
00227 return newArray;
00228 }
00229
00230 template <typename DataType>
00231 CellArray<DataType> CellArray<DataType>::operator,(const CellArray<DataType> &a)
00232 {
00233 unsigned int os = Array1D< Cell<DataType> >::size(), ns = os + a.size();
00234 CellArray<DataType> na(ns);
00235
00236 for (int i = 0; i < os; i++)
00237 {
00238 *(na[i]._dataPtr) = *(Array1D< Cell<DataType> >::operator[](i)._dataPtr);
00239 }
00240
00241 for (int i = 0; i < a.size(); i++)
00242 {
00243 *(na[i+os]._dataPtr) = *(a[i]._dataPtr);
00244 }
00245
00246 return na;
00247 }
00248
00249 template <typename DataType>
00250 std::ostream& operator<<(std::ostream& ostr, seal::Cell<DataType> &c)
00251 {
00252 std::cout << *(c._dataPtr);
00253 return std::cout;
00254 }
00255
00256 }
00257
00258 #endif // __SEAL_CELL_ARRAY_H__
00259