00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __VACCESSOR_HH
00021 #define __VACCESSOR_HH
00022
00023
00025
00026
00027
00029
00030 template <int Size, class AllocZone> class FixedVAccessor;
00031
00033
00034 class DynamicVAccessor {
00035 friend class VSizer;
00036 public:
00037 typedef DynamicVector<DynamicVAccessor> RefVector;
00038
00039 template<int Start, int Length>
00040 inline FixedVector<Length, FixedVAccessor<Length, Stack<Length> > >& slice()
00041 {
00042 return reinterpret_cast<FixedVector<Length, FixedVAccessor<Length, Stack<Length> > >&> (my_values[Start]);
00043 }
00044
00045 template<int Start, int Length>
00046 inline const FixedVector<Length, FixedVAccessor<Length, Stack<Length> > >& slice() const
00047 {
00048 return reinterpret_cast<const FixedVector<Length, FixedVAccessor<Length, Stack<Length> > >&> (my_values[Start]);
00049 }
00050
00051 inline RefVector slice(int start, int size)
00052 {
00053 assert(0 <= start && start < this->my_size && size >=0 && start+size <= this->my_size);
00054 RefVector ret;
00055 ret.set(size,this->my_values + start);
00056 return ret;
00057 }
00058
00059 inline const RefVector slice(int start, int size) const
00060 {
00061 assert(0 <= start && start < this->my_size && size >=0 && start+size <= this->my_size);
00062 RefVector ret;
00063 ret.set(size,this->my_values + start);
00064 return ret;
00065 }
00066 inline const double& operator[] (int i)const
00067 {
00068 return my_values[i];
00069 }
00070
00071 inline double& operator[] (int i)
00072 {
00073 return my_values[i];
00074 }
00075
00076 inline int size() const
00077 {
00078 return my_size;
00079 }
00080
00081 inline DynamicMatrix<DynamicMAccessor<RowMajor> > as_row();
00082 inline DynamicMatrix<DynamicMAccessor<ColMajor> > as_col();
00083 inline void set(int size, double* values) { my_size = size; my_values = values; }
00084
00085 typedef double* iterator;
00086 typedef const double* const_iterator;
00087
00088 iterator begin()
00089 {
00090 return my_values;
00091 }
00092
00093 iterator end()
00094 {
00095 return my_values + my_size;
00096 }
00097
00098 const_iterator begin() const
00099 {
00100 return my_values;
00101 }
00102 const_iterator end() const
00103 {
00104 return my_values + my_size;
00105 }
00106
00107 protected:
00108 int my_size;
00109 double* my_values;
00110 };
00111
00112 typedef DynamicVAccessor::RefVector RefVector;
00113 inline RefVector makeRefVector(int size, double* values) { RefVector ret; ret.set(size,values); return ret; }
00114
00115 class DynamicSkipAccessor{
00116 public:
00117 typedef DynamicVector<DynamicSkipAccessor> RefSkipVector;
00118
00119
00120 template<int Start, int Length>
00121 inline DynamicVector<DynamicSkipAccessor> slice()
00122 {
00123
00124
00125 DynamicVector<DynamicSkipAccessor> r;
00126
00127 r.my_size = Length;
00128 r.my_skip = my_skip;
00129 r.my_values = my_values + my_skip * Start;
00130
00131 return r;
00132 }
00133
00134 template<int Start, int Length>
00135 inline const DynamicVector<DynamicSkipAccessor> slice() const
00136 {
00137
00138
00139 DynamicVector<DynamicSkipAccessor> r;
00140 r.my_size = Length;
00141 r.my_skip = my_skip;
00142 r.my_values = my_values + my_skip * Start;
00143
00144 return r;
00145 }
00146
00147 inline RefSkipVector slice(int start, int size)
00148 {
00149 assert(0 <= start && start < this->my_size && size >=0 && start+size <= this->my_size);
00150 RefSkipVector ret;
00151 ret.set(size,my_skip,this->my_values + start*my_skip);
00152 return ret;
00153 }
00154
00155 inline const RefSkipVector slice(int start, int size) const
00156 {
00157 assert(0 <= start && start < this->my_size && size >=0 && start+size <= this->my_size);
00158 RefSkipVector ret;
00159 ret.set(size,my_skip,this->my_values + start*my_skip);
00160 return ret;
00161 }
00162
00163 inline const double& operator[] (int i)const
00164 {
00165 return my_values[i*my_skip];
00166 }
00167
00168 inline double& operator[] (int i)
00169 {
00170 return my_values[i*my_skip];
00171 }
00172
00173 inline int size() const
00174 {
00175 return my_size;
00176 }
00177
00178 inline DynamicMatrix<RefSkipMAccessor<ColMajor> > as_row();
00179 inline DynamicMatrix<RefSkipMAccessor<RowMajor> > as_col();
00180
00181 inline void set(int size, int skip, double* values) { my_size = size; my_skip = skip; my_values = values; }
00182
00183 private:
00184 template<class T> class It: public std::iterator<std::random_access_iterator_tag, double>
00185 {
00186 private:
00187 T* d;
00188 int skip;
00189 public:
00190 bool operator!=(const It&i){return d != i.d;}
00191 bool operator==(const It&i){return d == i.d;}
00192 bool operator<=(const It&i){return d <= i.d;}
00193 bool operator>=(const It&i){return d >= i.d;}
00194 bool operator<(const It&i){return d < i.d;}
00195 bool operator>(const It&i){return d > i.d;}
00196 It& operator+=(ptrdiff_t i) {d += i*skip;return *this;}
00197 It& operator-=(ptrdiff_t i) {d -= i*skip;return *this;}
00198 It& operator++() {d += skip;return *this;}
00199 It& operator--() {d -= skip;return *this;}
00200 It operator++(int) {T* t=d;d += skip;return iterator(t,skip);}
00201 It operator--(int) {T* t=d;d -= skip;return iterator(t,skip);}
00202 It operator+(ptrdiff_t i) {return It(d + i*skip,skip);}
00203 It operator-(ptrdiff_t i) {return It(d - i*skip,skip);}
00204 ptrdiff_t operator-(const It& i) {return (d - i.d)/skip;};
00205 T& operator*(){return *d;}
00206 It(T*a, int s):d(a),skip(s){}
00207 };
00208
00209 public:
00210
00211 typedef It<double> iterator;
00212 typedef It<const double> const_iterator;
00213
00214 iterator begin()
00215 {
00216 return iterator(my_values, my_skip);
00217 }
00218
00219 iterator end()
00220 {
00221 return iterator(my_values, my_skip) + my_size;
00222 }
00223
00224 const_iterator begin() const
00225 {
00226 return const_iterator(my_values, my_skip);
00227 }
00228 const_iterator end() const
00229 {
00230 return const_iterator(my_values, my_skip) + my_size;
00231 }
00232
00233 protected:
00234 int my_size;
00235 int my_skip;
00236 double* my_values;
00237 };
00238
00239 typedef DynamicSkipAccessor::RefSkipVector RefSkipVector;
00240
00241 inline RefSkipVector makeRefSkipVector(int size, int skip, double* values) { RefSkipVector ret; ret.set(size,skip,values); return ret; }
00242
00243
00245 template <int Size, class AllocZone>
00246 class FixedVAccessor : public AllocZone {
00247 public:
00248 typedef AllocZone parent;
00249 inline const double& operator[] (int i)const
00250 {
00251 return parent::my_values[i];
00252 }
00253
00254 inline double& operator[] (int i)
00255 {
00256 return parent::my_values[i];
00257 }
00258
00259
00260 inline static int size() {return Size;}
00261
00262 template<int Start, int Length>
00263 inline FixedVector<Length,FixedVAccessor<Length,Stack<Length> > >& slice()
00264 {
00265 util::Assert<(Start+Length <= Size)>();
00266 FixedVector<Length,FixedVAccessor<Length,Stack<Length> > >::dummy();
00267 return reinterpret_cast<FixedVector<Length,FixedVAccessor<Length,Stack<Length> > >&> (parent::my_values[Start]);
00268 }
00269
00270 typedef FixedVAccessor<Size,AllocZone> this_type;
00271 inline RefVector slice(int start, int size)
00272 {
00273 assert(0 <= start && start < Size && size >=0 && start+size <= Size);
00274 return makeRefVector(size, parent::my_values + start);
00275 }
00276
00277 inline const RefVector slice(int start, int size) const
00278 {
00279 assert(0 <= start && start < Size && size >=0 && start+size <= Size);
00280 return makeRefVector(size, const_cast<double*>( parent::my_values + start));
00281 }
00282
00283 template<int Start, int Length>
00284 inline const FixedVector<Length,FixedVAccessor<Length,Stack<Length> > >& slice() const
00285 {
00286 util::Assert<(Start+Length <= Size)>();
00287 return reinterpret_cast<const FixedVector<Length,FixedVAccessor<Length,Stack<Length> > >&> (parent::my_values[Start]);
00288 }
00289
00290
00291 inline FixedMatrix<Size,1,FixedMAccessor<Size,1,ColMajor,Stack<Size> > >& as_col()
00292 {
00293 FixedMatrix<Size,1,FixedMAccessor<Size,1,ColMajor,Stack<Size> > >::dummy();
00294 return reinterpret_cast<FixedMatrix<Size,1,FixedMAccessor<Size,1,ColMajor,Stack<Size> > >&>(*parent::my_values);
00295 }
00296
00297 inline const FixedMatrix<Size,1,FixedMAccessor<Size,1,ColMajor,Stack<Size> > >& as_col() const
00298 {
00299 FixedMatrix<Size,1,FixedMAccessor<Size,1,ColMajor,Stack<Size> > >::dummy();
00300 return reinterpret_cast<const FixedMatrix<Size,1,FixedMAccessor<Size,1,ColMajor,Stack<Size> > >&> (*parent::my_values);
00301 }
00302
00303 inline FixedMatrix<1,Size,FixedMAccessor<1,Size,RowMajor,Stack<Size> > >& as_row()
00304 {
00305 FixedMatrix<1,Size,FixedMAccessor<1,Size,RowMajor,Stack<Size> > >::dummy();
00306 return reinterpret_cast<FixedMatrix<1,Size,FixedMAccessor<1,Size,RowMajor,Stack<Size> > >&> (*parent::my_values);
00307 }
00308
00309 inline const FixedMatrix<1,Size,FixedMAccessor<1,Size,RowMajor,Stack<Size> > >& as_row() const
00310 {
00311 FixedMatrix<1,Size,FixedMAccessor<1,Size,RowMajor,Stack<Size> > >::dummy();
00312 return reinterpret_cast<const FixedMatrix<1,Size,FixedMAccessor<1,Size,RowMajor,Stack<Size> > >&> (*parent::my_values);
00313 }
00314
00315 typedef double* iterator;
00316 typedef const double* const_iterator;
00317
00318 iterator begin()
00319 {
00320 return parent::my_values;
00321 }
00322
00323 iterator end()
00324 {
00325 return parent::my_values + Size;
00326 }
00327
00328 const_iterator begin() const
00329 {
00330 return parent::my_values;
00331 }
00332 const_iterator end() const
00333 {
00334 return parent::my_values + Size;
00335 }
00336
00337 };
00338
00339
00340 template <int Size, int Skip>
00341 class SkipAccessor : public Stack<Size*Skip>{
00342 public:
00343 typedef Stack<Size*Skip> parent;
00344 typedef SkipAccessor<Size,Skip> this_type;
00345 inline const double& operator[] (int i) const
00346 {
00347 return parent::my_values[i*Skip];
00348 }
00349
00350 inline double& operator[] (int i)
00351 {
00352 return parent::my_values[i*Skip];
00353 }
00354
00355 inline static int size()
00356 {
00357 return Size;
00358 }
00359
00360 template<int Start, int Length>
00361 inline FixedVector<Length, SkipAccessor<Size, Skip> >& slice()
00362 {
00363 util::Assert<(Start+Length <= Size)>();
00364 return reinterpret_cast<FixedVector<Length, SkipAccessor<Size, Skip> >&>(parent::my_values[Start*Skip]);
00365 }
00366
00367 template<int Start, int Length>
00368 inline const FixedVector<Length, SkipAccessor<Size, Skip> >& slice() const
00369 {
00370 util::Assert<(Start+Length <= Size)>();
00371 return reinterpret_cast<const FixedVector<Length, SkipAccessor<Size, Skip> >&>(parent::my_values[Start*Skip]);
00372 }
00373
00374 RefSkipVector slice(int start, int size)
00375 {
00376 assert(0 <= start && start < Size && size >=0 && start+size <= Size);
00377 return makeRefSkipVector(size, Skip, parent::my_values + start*Skip);
00378 }
00379 const RefSkipVector slice(int start, int size) const
00380 {
00381 assert(0 <= start && start < Size && size >=0 && start+size <= Size);
00382 return makeRefSkipVector(size, Skip, parent::my_values + start*Skip);
00383 }
00384
00385 inline FixedMatrix<Size,1,SkipMAccessor<Size,1,Skip,RowMajor> >& as_col()
00386 {
00387 return reinterpret_cast<FixedMatrix<Size,1,SkipMAccessor<Size,1,Skip,RowMajor> >&>(*parent::my_values);
00388 }
00389
00390 inline const FixedMatrix<Size,1,SkipMAccessor<Size,1,Skip,RowMajor> >& as_col() const
00391 {
00392 return reinterpret_cast<FixedMatrix<Size,1,SkipMAccessor<Size,1,Skip,RowMajor> >&>(*parent::my_values);
00393 }
00394
00395 inline FixedMatrix<1,Size,SkipMAccessor<1,Size,Skip,ColMajor> >& as_row()
00396 {
00397 return reinterpret_cast<FixedMatrix<1,Size,SkipMAccessor<1,Size,Skip,ColMajor> >&>(*parent::my_values);
00398 }
00399
00400 inline const FixedMatrix<1,Size,SkipMAccessor<1,Size,Skip,ColMajor> >& as_row() const
00401 {
00402 return reinterpret_cast<const FixedMatrix<1,Size,SkipMAccessor<1,Size,Skip,ColMajor> >&>(*parent::my_values);
00403 }
00404
00405 private:
00406 template<class T> class It: public std::iterator<std::random_access_iterator_tag, double>
00407 {
00408 private:
00409 T* d;
00410 public:
00411 bool operator!=(const It&i){return d != i.d;}
00412 bool operator==(const It&i){return d == i.d;}
00413 bool operator<=(const It&i){return d <= i.d;}
00414 bool operator>=(const It&i){return d >= i.d;}
00415 bool operator<(const It&i){return d < i.d;}
00416 bool operator>(const It&i){return d > i.d;}
00417 It& operator+=(ptrdiff_t i) {d += i*Skip;return *this;}
00418 It& operator-=(ptrdiff_t i) {d -= i*Skip;return *this;}
00419 It& operator++() {d += Skip;return *this;}
00420 It& operator--() {d -= Skip;return *this;}
00421 It operator++(int) {T* t=d;d += Skip;return iterator(t);}
00422 It operator--(int) {T* t=d;d -= Skip;return iterator(t);}
00423 It operator+(ptrdiff_t i) {return It(d + i*Skip);}
00424 It operator-(ptrdiff_t i) {return It(d - i*Skip);}
00425 ptrdiff_t operator-(const It& i) {return (d - i.d)/Skip;};
00426 T& operator*(){return *d;}
00427 It(T*a):d(a){}
00428 };
00429
00430 public:
00431
00432 typedef It<double> iterator;
00433 typedef It<const double> const_iterator;
00434
00435 iterator begin()
00436 {
00437 return iterator(parent::my_values);
00438 }
00439
00440 iterator end()
00441 {
00442 return iterator(parent::my_values) + Size;
00443 }
00444
00445 const_iterator begin() const
00446 {
00447 return iterator(parent::my_values);
00448 }
00449 const_iterator end() const
00450 {
00451 return iterator(parent::my_values) + Size;
00452 }
00453
00454 };
00455
00456
00457 #endif