00001
00002
00008 #if !defined(__ads_SparseArray1_h__)
00009 #error This file is an implementation detail of the class SparseArray.
00010 #endif
00011
00012 #include "../defs.h"
00013
00014 #include <vector>
00015
00016 BEGIN_NAMESPACE_ADS
00017
00019
00035 template<typename T>
00036 class SparseArray<1, T> : public ArrayContainer<T> {
00037
00038
00039
00040
00041 private:
00042
00043 typedef ArrayTypes<T> Types;
00044 typedef ArrayContainer<T> Base;
00045
00046
00047
00048
00049
00050 public:
00051
00053 typedef typename Types::value_type value_type;
00055
00058 typedef typename Types::parameter_type parameter_type;
00060
00063 typedef typename Types::unqualified_value_type unqualified_value_type;
00064
00066 typedef typename Types::pointer pointer;
00068 typedef typename Types::const_pointer const_pointer;
00069
00071 typedef typename Types::iterator iterator;
00073 typedef typename Types::const_iterator const_iterator;
00074
00076 typedef typename Types::reference reference;
00078 typedef typename Types::const_reference const_reference;
00079
00081
00087 typedef typename Types::size_type size_type;
00089 typedef typename Types::difference_type difference_type;
00090
00091
00093 typedef Array<1,int>::const_iterator IndexConstIterator;
00094
00095
00096
00097
00098
00099 protected:
00100
00102 Array<1,int> _indices;
00104 value_type _null;
00105
00106
00107
00108
00109
00110 public:
00111
00112
00113 using Base::size;
00114 using Base::empty;
00115 using Base::max_size;
00116 using Base::begin;
00117 using Base::end;
00118 using Base::data;
00119 using Base::operator[];
00120
00121
00122
00123
00124
00125 using Base::negate;
00126
00127
00128 using Base::write_elements_ascii;
00129 using Base::write_elements_binary;
00130 using Base::read_elements_ascii;
00131 using Base::read_elements_binary;
00132
00133 public:
00134
00135
00137
00138
00140 SparseArray() :
00141 Base(),
00142 _indices(),
00143 _null()
00144 {}
00145
00147
00152 template<typename IndexForwardIter, typename ValueForwardIter>
00153 SparseArray(IndexForwardIter indicesBeginning, IndexForwardIter indicesEnd,
00154 ValueForwardIter valuesBeginning, ValueForwardIter valuesEnd,
00155 parameter_type nullValue = value_type());
00156
00158 template<typename IndexForwardIter, typename ValueForwardIter>
00159 void
00160 rebuild(IndexForwardIter indicesBeginning, IndexForwardIter indicesEnd,
00161 ValueForwardIter valuesBeginning, ValueForwardIter valuesEnd,
00162 parameter_type nullValue);
00163
00165 template<typename IndexForwardIter, typename ValueForwardIter>
00166 void
00167 rebuild(IndexForwardIter indicesBeginning, IndexForwardIter indicesEnd,
00168 ValueForwardIter valuesBeginning, ValueForwardIter valuesEnd) {
00169 rebuild(indicesBeginning, indicesEnd, valuesBeginning, valuesEnd,
00170 getNull());
00171 }
00172
00174 template<typename T2, bool A>
00175 SparseArray(const Array<1,T2,A>& array, parameter_type nullValue);
00176
00178
00181 explicit
00182 SparseArray(const size_type size) :
00183 Base(size),
00184 _indices(size),
00185 _null()
00186 {}
00187
00189
00192 void
00193 rebuild(const size_type size) {
00194 Base::rebuild(size);
00195 _indices.rebuild(size);
00196 }
00197
00199 SparseArray(const SparseArray& other) :
00200 Base(other),
00201 _indices(other._indices),
00202 _null(other._null)
00203 {}
00204
00206 ~SparseArray()
00207 {}
00208
00209
00210
00212
00213
00215 SparseArray&
00216 operator=(const SparseArray& other) {
00217 if (&other != this) {
00218 Base::operator=(other);
00219 _indices = other._indices;
00220 _null = other._null;
00221 }
00222 return *this;
00223 }
00224
00226 template<typename T2, bool A>
00227 SparseArray&
00228 operator=(const Array<1,T2,A>& array);
00229
00230
00231
00233
00234
00236 static
00237 int
00238 getRank() {
00239 return 1;
00240 }
00241
00242
00243
00245
00246
00248 size_type
00249 getMemoryUsage() const {
00250 return Base::getMemoryUsage() + _indices.getMemoryUsage();
00251 }
00252
00254 parameter_type
00255 getNull() const {
00256 return _null;
00257 }
00258
00260 bool
00261 isNull(const int i) const;
00262
00264 bool
00265 isNonNull(const int i) const {
00266 return ! isNull(i);
00267 }
00268
00270 parameter_type
00271 operator()(const int i) const;
00272
00274 bool
00275 operator==(const SparseArray& x) const {
00276 return (Base::operator==(x) && _indices == x._indices && _null == x._null);
00277 }
00278
00280 bool
00281 operator!=(const SparseArray& x) const {
00282 return ! operator==(x);
00283 }
00284
00286 template<typename T2, bool A>
00287 void
00288 fill(ads::Array<1,T2,A>* array) const;
00289
00291 template<typename T2, bool A>
00292 void
00293 fillNonNull(ads::Array<1,T2,A>* array) const;
00294
00296 const Array<1,int>&
00297 getIndices() const {
00298 return _indices;
00299 }
00300
00302 int
00303 getIndex(const int n) const {
00304 return _indices[n];
00305 }
00306
00308 IndexConstIterator
00309 getIndicesBeginning() const {
00310 return _indices.begin();
00311 }
00312
00314 IndexConstIterator
00315 getIndicesEnd() const {
00316 return _indices.end();
00317 }
00318
00319
00320
00322
00323
00325 void
00326 swap(SparseArray& other) {
00327 Base::swap(other);
00328 _indices.swap(other._indices);
00329 }
00330
00331
00332
00335
00336
00338 SparseArray&
00339 operator=(parameter_type x) {
00340 Base::operator=(x);
00341 return *this;
00342 }
00343
00344
00345
00347
00348
00350 void
00351 put(std::ostream& out) const;
00352
00354 void
00355 get(std::istream& in);
00356
00357
00358 };
00359
00360
00361
00364
00365
00367 template<typename T>
00368 int
00369 countNonNullElementsInUnion(const SparseArray<1, T>& a,
00370 const SparseArray<1, T>& b);
00371
00372
00374 template<typename T>
00375 void
00376 computeSum(const SparseArray<1, T>& x, const SparseArray<1, T>& y,
00377 SparseArray<1, T>* result);
00378
00379
00381 template<typename T>
00382 void
00383 computeDifference(const SparseArray<1, T>& x, const SparseArray<1, T>& y,
00384 SparseArray<1, T>* result);
00385
00386
00388 template<typename T>
00389 void
00390 computeProduct(const SparseArray<1, T>& x, const SparseArray<1, T>& y,
00391 SparseArray<1, T>* result);
00392
00393
00395
00399 template<typename T, typename BinaryFunction>
00400 void
00401 computeBinaryOperation(const SparseArray<1, T>& x, const SparseArray<1, T>& y,
00402 SparseArray<1, T>* result,
00403 const BinaryFunction& function);
00404
00405
00406
00407
00408
00409
00411 template<typename T1, bool A, typename T2>
00412 Array<1,T1,A>&
00413 operator+=(Array<1,T1,A>& x, const SparseArray<1,T2> & y);
00414
00416 template<typename T1, bool A, typename T2>
00417 Array<1,T1,A>&
00418 operator-=(Array<1,T1,A>& x, const SparseArray<1,T2> & y);
00419
00421 template<typename T1, bool A, typename T2>
00422 Array<1,T1,A>&
00423 operator*=(Array<1,T1,A>& x, const SparseArray<1,T2> & y);
00424
00426 template<typename T1, bool A, typename T2>
00427 Array<1,T1,A>&
00428 operator/=(Array<1,T1,A>& x, const SparseArray<1,T2> & y);
00429
00431 template<typename T1, bool A, typename T2>
00432 Array<1,T1,A>&
00433 operator%=(Array<1,T1,A>& x, const SparseArray<1,T2> & y);
00434
00436 template<typename T1, bool A, typename T2, typename T3>
00437 void
00438 scaleAdd(Array<1,T1,A>* x, const T2 a, const SparseArray<1,T3> & y);
00439
00440
00441
00442
00443
00444
00445
00447 template<int _N, typename _T1, typename _T2>
00448 FixedArray<_N, _T1>&
00449 operator+=(FixedArray<_N, _T1>& x, const SparseArray<1, _T2>& y);
00450
00452 template<int _N, typename _T1, typename _T2>
00453 FixedArray<_N, _T1>&
00454 operator-=(FixedArray<_N, _T1>& x, const SparseArray<1, _T2>& y);
00455
00457 template<int _N, typename _T1, typename _T2>
00458 FixedArray<_N, _T1>&
00459 operator*=(FixedArray<_N, _T1>& x, const SparseArray<1, _T2>& y);
00460
00462 template<int _N, typename _T1, typename _T2>
00463 FixedArray<_N, _T1>&
00464 operator/=(FixedArray<_N, _T1>& x, const SparseArray<1, _T2>& y);
00465
00467 template<int _N, typename _T1, typename _T2>
00468 FixedArray<_N, _T1>&
00469 operator%=(FixedArray<_N, _T1>& x, const SparseArray<1, _T2>& y);
00470
00472 template<int _N, typename _T1, typename _T2, typename _T3>
00473 void
00474 scaleAdd(FixedArray<_N, _T1>* x, const _T2 a, const SparseArray<1, _T3>& y);
00475
00477
00478 END_NAMESPACE_ADS
00479
00480 #define __ads_SparseArray1_ipp__
00481 #include "SparseArray1.ipp"
00482 #undef __ads_SparseArray1_ipp__