vtf-logo

FixedArray3.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00008 #if !defined(__FixedArray3_h__)
00009 #error This file is an implementation detail of the class FixedArray.
00010 #endif
00011 
00012 #include "../defs.h"
00013 
00014 BEGIN_NAMESPACE_ADS
00015 
00017 template<typename T>
00018 class FixedArray<3,T> {
00019   //
00020   // Private types.
00021   //
00022 
00023 private:
00024 
00025   typedef ArrayTypes<T> Types;
00026 
00027   //
00028   // Public types.
00029   //
00030 
00031 public:
00032 
00033   //--------------------------------------------------------------------------
00035   // @{
00036 
00038   typedef typename Types::value_type value_type;
00040   typedef typename Types::parameter_type parameter_type;
00042   typedef typename Types::pointer pointer;
00044   typedef typename Types::const_pointer const_pointer;
00046   typedef typename Types::iterator iterator;
00048   typedef typename Types::const_iterator const_iterator;
00050   typedef typename Types::reference reference;
00052   typedef typename Types::const_reference const_reference;
00054 
00060   typedef typename Types::size_type size_type;
00062   typedef typename Types::difference_type difference_type;
00063 
00065 
00066   //
00067   // Data.
00068   //
00069 
00070 private:
00071 
00072   value_type _data[3];
00073 
00074 public:
00075 
00076   //--------------------------------------------------------------------------
00078   // @{
00079 
00080   // Default constructor.  Leave the data uninitialized.
00081   FixedArray()
00082   {}
00083 
00085   ~FixedArray() 
00086   {}
00087 
00089   FixedArray(const FixedArray& x) {
00090     _data[0] = x._data[0];
00091     _data[1] = x._data[1];
00092     _data[2] = x._data[2];
00093   }
00094 
00096   template<typename T2> 
00097   FixedArray(const FixedArray<3,T2>& x) {
00098     _data[0] = static_cast<value_type>(x[0]);
00099     _data[1] = static_cast<value_type>(x[1]);
00100     _data[2] = static_cast<value_type>(x[2]);
00101   }
00102 
00104   explicit 
00105   FixedArray(parameter_type x) {
00106     _data[0] = x;
00107     _data[1] = x;
00108     _data[2] = x;
00109   }
00110 
00112   FixedArray(const void* vp) {
00113     const_pointer p = static_cast<const_pointer>(vp);
00114     _data[0] = *p++;
00115     _data[1] = *p++;
00116     _data[2] = *p;
00117   }
00118 
00120   explicit 
00121   FixedArray(parameter_type x0, parameter_type x1, parameter_type x2) {
00122     _data[0] = x0;
00123     _data[1] = x1;
00124     _data[2] = x2;
00125   }
00126 
00127   // @}
00128   //--------------------------------------------------------------------------
00130   // @{
00131 
00133   FixedArray& 
00134   operator=(const FixedArray& other) {
00135     if (&other != this) {
00136       _data[0] = other._data[0];
00137       _data[1] = other._data[1];
00138       _data[2] = other._data[2];
00139     }
00140     return *this;
00141   }
00142 
00144   FixedArray& 
00145   operator=(parameter_type x) {
00146     _data[0] = x;
00147     _data[1] = x;
00148     _data[2] = x;
00149     return *this;
00150   }
00151 
00153   template<typename T2> 
00154   FixedArray& 
00155   operator=(const FixedArray<3,T2>& x) {
00156     _data[0] = x[0];
00157     _data[1] = x[1];
00158     _data[2] = x[2];
00159     return *this;
00160   }
00161 
00163   template<typename T2, bool A>
00164   FixedArray& 
00165   operator=(const Array<1, T2, A>& x) {
00166 #ifdef DEBUG_FixedArray
00167     assert(size() == x.size());
00168 #endif
00169     _data[0] = x[0];
00170     _data[1] = x[1];
00171     _data[2] = x[2];
00172     return *this;
00173   }
00174 
00175   // @}
00176   //--------------------------------------------------------------------------
00178   // @{
00179 
00181   static
00182   size_type
00183   size() { 
00184     return 3;
00185   }
00186 
00188   static
00189   bool
00190   empty() { 
00191     return false;
00192   }
00193 
00195   static
00196   size_type
00197   max_size() { 
00198     return std::numeric_limits<int>::max() / sizeof(value_type); 
00199   }
00200 
00201   // @}
00202   //--------------------------------------------------------------------------
00204   // @{
00205 
00207   const_iterator 
00208   begin() const { 
00209     return _data;
00210   }
00211 
00213   const_iterator 
00214   end() const { 
00215     return _data + 3;
00216   }
00217 
00219   const_pointer 
00220   data() const { 
00221     return _data;
00222   }
00223 
00225   parameter_type
00226   operator()(const int i) const {
00227 #ifdef DEBUG_FixedArray
00228     assert(i == 0 || i == 1 || i == 2);
00229 #endif
00230     return _data[i];
00231   }
00232 
00234   parameter_type
00235   operator[](const int i) const {
00236 #ifdef DEBUG_FixedArray
00237     assert(i == 0 || i == 1 || i == 2);
00238 #endif
00239     return _data[i];
00240   }
00241 
00243   template<typename EqualityComparable>
00244   const_iterator
00245   find(const EqualityComparable& x) const {
00246     if (_data[0] == x) {
00247       return begin();
00248     }
00249     if (_data[1] == x) {
00250       return begin() + 1;
00251     }
00252     if (_data[2] == x) {
00253       return begin() + 2;
00254     }
00255     return end();
00256   }
00257 
00259   template<typename EqualityComparable>
00260   int
00261   find_index(const EqualityComparable& x) const {
00262     if (_data[0] == x) {
00263       return 0;
00264     }
00265     if (_data[1] == x) {
00266       return 1;
00267     }
00268     if (_data[2] == x) {
00269       return 2;
00270     }
00271     return 3;
00272   }
00273 
00275   template<typename EqualityComparable>
00276   bool
00277   has(const EqualityComparable& x) const {
00278     return (_data[0] == x || _data[1] == x || _data[2] == x);
00279   }
00280 
00282   bool
00283   is_sorted() const {
00284     if (_data[1] < _data[0] || _data[2] < _data[1]) {
00285       return false;
00286     }
00287     return true;
00288   }
00289 
00291   template<class StrictWeakOrdering>
00292   bool
00293   is_sorted(StrictWeakOrdering comp) const {
00294     if (comp(_data[1], _data[0]) || comp(_data[2], _data[1])) {
00295       return false;
00296     }
00297     return true;
00298   }
00299 
00301   int
00302   min_index() const {
00303     if (_data[0] < _data[1]) {
00304       if (_data[0] < _data[2]) {
00305         return 0;
00306       }
00307       return 2;
00308     }
00309     if (_data[1] < _data[2]) {
00310       return 1;
00311     }
00312     return 2;
00313   }
00314 
00316   template<class StrictWeakOrdering>
00317   int
00318   min_index(StrictWeakOrdering comp) const {
00319     if (comp(_data[0], _data[1])) {
00320       if (comp(_data[0], _data[2])) {
00321         return 0;
00322       }
00323       return 2;
00324     }
00325     if (comp(_data[1], _data[2])) {
00326       return 1;
00327     }
00328     return 2;
00329   }
00330 
00332   int
00333   max_index() const {
00334     if (_data[0] > _data[1]) {
00335       if (_data[0] > _data[2]) {
00336         return 0;
00337       }
00338       return 2;
00339     }
00340     if (_data[1] > _data[2]) {
00341       return 1;
00342     }
00343     return 2;
00344   }
00345 
00347   template<class StrictWeakOrdering>
00348   int
00349   max_index(StrictWeakOrdering comp) const {
00350     if (comp(_data[0], _data[1])) {
00351       if (comp(_data[0], _data[2])) {
00352         return 0;
00353       }
00354       return 2;
00355     }
00356     if (comp(_data[1], _data[2])) {
00357       return 1;
00358     }
00359     return 2;
00360   }
00361 
00362   // @}
00363   //--------------------------------------------------------------------------
00365   // @{
00366 
00368   iterator 
00369   begin() { 
00370     return _data;
00371   }
00372 
00374   iterator 
00375   end() { 
00376     return _data + 3;
00377   }
00378 
00380   pointer 
00381   data() { 
00382     return _data;
00383   }
00384 
00386   reference
00387   operator()(const int i) { 
00388 #ifdef DEBUG_FixedArray
00389     assert(i == 0 || i == 1 || i == 2);
00390 #endif
00391     return _data[i];
00392   }
00393 
00395   reference
00396   operator[](const int i) {
00397 #ifdef DEBUG_FixedArray
00398     assert(i == 0 || i == 1 || i == 2);
00399 #endif
00400     return _data[i];
00401   }
00402 
00404   void
00405   swap(FixedArray& x) {
00406     std::swap(_data[0], x._data[0]);
00407     std::swap(_data[1], x._data[1]);
00408     std::swap(_data[2], x._data[2]);
00409   }
00410 
00412   template<typename EqualityComparable>
00413   iterator
00414   find(const EqualityComparable& x) {
00415     if (_data[0] == x) {
00416       return begin();
00417     }
00418     if (_data[1] == x) {
00419       return begin() + 1;
00420     }
00421     if (_data[2] == x) {
00422       return begin() + 2;
00423     }
00424     return end();
00425   }
00426 
00428   void
00429   negate() {
00430     _data[0] = - _data[0];
00431     _data[1] = - _data[1];
00432     _data[2] = - _data[2];
00433   }
00434 
00436   void
00437   fill(parameter_type value) {
00438     _data[0] = value;
00439     _data[1] = value;
00440     _data[2] = value;
00441   }
00442 
00444   template<typename InputIterator>
00445   void
00446   copy(InputIterator start, InputIterator finish) {
00447     _data[0] = *start;
00448     ++start;
00449     _data[1] = *start;
00450     ++start;
00451     _data[2] = *start;
00452 #ifdef DEBUG_FixedArray
00453     assert(++start == finish);
00454 #endif
00455   }
00456     
00458   void
00459   sort() {
00460     if (_data[1] < _data[0]) {
00461       std::swap(_data[0], _data[1]);
00462     }
00463     if (_data[2] < _data[1]) {
00464       std::swap(_data[1], _data[2]);
00465     }
00466     if (_data[1] < _data[0]) {
00467       std::swap(_data[0], _data[1]);
00468     }
00469   }
00470 
00472   template<class StrictWeakOrdering>
00473   void
00474   sort(StrictWeakOrdering comp) {
00475     if (comp(_data[1], _data[0])) {
00476       std::swap(_data[0], _data[1]);
00477     }
00478     if (comp(_data[2], _data[1])) {
00479       std::swap(_data[1], _data[2]);
00480     }
00481     if (comp(_data[1], _data[0])) {
00482       std::swap(_data[0], _data[1]);
00483     }
00484   }
00485 
00486   // @}
00487   //--------------------------------------------------------------------------
00489   // @{
00490 
00492   FixedArray& 
00493   operator+=(parameter_type x) {
00494     _data[0] += x;
00495     _data[1] += x;
00496     _data[2] += x;
00497     return *this;
00498   }
00499 
00501   FixedArray& 
00502   operator-=(parameter_type x) {
00503     _data[0] -= x;
00504     _data[1] -= x;
00505     _data[2] -= x;
00506     return *this;
00507   }
00508 
00510   FixedArray& 
00511   operator*=(parameter_type x) {
00512     _data[0] *= x;
00513     _data[1] *= x;
00514     _data[2] *= x;
00515     return *this;
00516   }
00517 
00519   FixedArray& 
00520   operator/=(parameter_type x) {
00521 #ifdef DEBUG_FixedArray
00522     assert(x != 0);
00523 #endif
00524     _data[0] /= x;
00525     _data[1] /= x;
00526     _data[2] /= x;
00527     return *this;
00528   }
00529 
00531   FixedArray& 
00532   operator%=(parameter_type x) {
00533 #ifdef DEBUG_FixedArray
00534     assert(x != 0);
00535 #endif
00536     _data[0] %= x;
00537     _data[1] %= x;
00538     _data[2] %= x;
00539     return *this;
00540   }
00541 
00543   FixedArray& 
00544   operator<<=(const int offset) {
00545     _data[0] <<= offset;
00546     _data[1] <<= offset;
00547     _data[2] <<= offset;
00548     return *this;
00549   }
00550 
00552   FixedArray& 
00553   operator>>=(const int offset) {
00554     _data[0] >>= offset;
00555     _data[1] >>= offset;
00556     _data[2] >>= offset;
00557     return *this;
00558   }
00559 
00560   // @}
00561   //--------------------------------------------------------------------------
00563   // @{
00564 
00566   template<typename T2>
00567   FixedArray& 
00568   operator+=(const FixedArray<3,T2>& x) {
00569     _data[0] += x[0];
00570     _data[1] += x[1];
00571     _data[2] += x[2];
00572     return *this;
00573   }
00574 
00576   template<typename T2>
00577   FixedArray& 
00578   operator-=(const FixedArray<3,T2>& x) {
00579     _data[0] -= x[0];
00580     _data[1] -= x[1];
00581     _data[2] -= x[2];
00582     return *this;
00583   }
00584 
00586   template<typename T2>
00587   FixedArray& 
00588   operator*=(const FixedArray<3,T2>& x) {
00589     _data[0] *= x[0];
00590     _data[1] *= x[1];
00591     _data[2] *= x[2];
00592     return *this;
00593   }
00594 
00596   template<typename T2>
00597   FixedArray& 
00598   operator/=(const FixedArray<3,T2>& x) {
00599 #ifdef DEBUG_FixedArray
00600     assert(x[0] != 0 && x[1] != 0 && x[2] != 0);
00601 #endif
00602     _data[0] /= x[0];
00603     _data[1] /= x[1];
00604     _data[2] /= x[2];
00605     return *this;
00606   }
00607 
00609   template<typename T2>
00610   FixedArray& 
00611   operator%=(const FixedArray<3,T2>& x) {
00612 #ifdef DEBUG_FixedArray
00613     assert(x[0] != 0 && x[1] != 0 && x[2] != 0);
00614 #endif
00615     _data[0] %= x[0];
00616     _data[1] %= x[1];
00617     _data[2] %= x[2];
00618     return *this;
00619   }
00620 
00621   // @}
00622 };
00623 
00624 
00625 END_NAMESPACE_ADS
00626 
00627 #define __FixedArray3_ipp__
00628 #include "FixedArray3.ipp"
00629 #undef __FixedArray3_ipp__

Generated on Fri Aug 24 12:55:24 2007 for Algorithms and Data Structures Package by  doxygen 1.4.7