vtf-logo

FixedArray2.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00008 #if !defined(__FixedArray2_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<2,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[2];
00073 
00074 public:
00075 
00076   //--------------------------------------------------------------------------
00078   // @{
00079 
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   }
00093 
00095   template<typename T2> 
00096   FixedArray(const FixedArray<2,T2>& x) {
00097     _data[0] = static_cast<value_type>(x[0]);
00098     _data[1] = static_cast<value_type>(x[1]);
00099   }
00100 
00102   explicit 
00103   FixedArray(parameter_type x) {
00104     _data[0] = x;
00105     _data[1] = x;
00106   }
00107 
00109   FixedArray(const void* vp) {
00110     const_pointer p = static_cast<const_pointer>(vp);
00111     _data[0] = *p++;
00112     _data[1] = *p;
00113   }
00114 
00116   explicit 
00117   FixedArray(parameter_type x0, parameter_type x1) {
00118     _data[0] = x0;
00119     _data[1] = x1;
00120   }
00121 
00122   // @}
00123   //--------------------------------------------------------------------------
00125   // @{
00126 
00128   FixedArray& 
00129   operator=(const FixedArray& x) {
00130     if (&x != this) {
00131       _data[0] = x._data[0];
00132       _data[1] = x._data[1];
00133     }
00134     return *this;
00135   }
00136 
00138   FixedArray& 
00139   operator=(parameter_type x) {
00140     _data[0] = x;
00141     _data[1] = x;
00142     return *this;
00143   }
00144 
00146   template<typename T2> 
00147   FixedArray& 
00148   operator=(const FixedArray<2,T2>& x) {
00149     _data[0] = x[0];
00150     _data[1] = x[1];
00151     return *this;
00152   }
00153 
00155   template<typename T2, bool A>
00156   FixedArray& 
00157   operator=(const Array<1, T2, A>& x) {
00158 #ifdef DEBUG_FixedArray
00159     assert(size() == x.size());
00160 #endif
00161     _data[0] = x[0];
00162     _data[1] = x[1];
00163     return *this;
00164   }
00165 
00166   // @}
00167   //--------------------------------------------------------------------------
00169   // @{
00170 
00172   static
00173   size_type
00174   size() { 
00175     return 2;
00176   }
00177 
00179   static
00180   bool
00181   empty() { 
00182     return false;
00183   }
00184 
00186   static
00187   size_type
00188   max_size() { 
00189     return std::numeric_limits<int>::max() / sizeof(value_type); 
00190   }
00191 
00192   // @}
00193   //--------------------------------------------------------------------------
00195   // @{
00196 
00198   const_iterator 
00199   begin() const { 
00200     return _data;
00201   }
00202 
00204   const_iterator 
00205   end() const { 
00206     return _data + 2;
00207   }
00208 
00210   const_pointer 
00211   data() const { 
00212     return _data;
00213   }
00214 
00216   parameter_type
00217   operator()(const int i) const {
00218 #ifdef DEBUG_FixedArray
00219     assert(i == 0 || i == 1);
00220 #endif
00221     return _data[i];
00222   }
00223 
00225   parameter_type
00226   operator[](const int i) const {
00227 #ifdef DEBUG_FixedArray
00228     assert(i == 0 || i == 1);
00229 #endif
00230     return _data[i];
00231   }
00232 
00234   template<typename EqualityComparable>
00235   const_iterator
00236   find(const EqualityComparable& x) const {
00237     if (_data[0] == x) {
00238       return begin();
00239     }
00240     if (_data[1] == x) {
00241       return begin() + 1;
00242     }
00243     return end();
00244   }
00245 
00247   template<typename EqualityComparable>
00248   int
00249   find_index(const EqualityComparable& x) const {
00250     if (_data[0] == x) {
00251       return 0;
00252     }
00253     if (_data[1] == x) {
00254       return 1;
00255     }
00256     return 2;
00257   }
00258 
00260   template<typename EqualityComparable>
00261   bool
00262   has(const EqualityComparable& x) const {
00263     return (_data[0] == x || _data[1] == x);
00264   }
00265 
00267   bool
00268   is_sorted() const {
00269     if (_data[1] < _data[0]) {
00270       return false;
00271     }
00272     return true;
00273   }
00274 
00276   template<class StrictWeakOrdering>
00277   bool
00278   is_sorted(StrictWeakOrdering comp) const {
00279     if (comp(_data[1], _data[0])) {
00280       return false;
00281     }
00282     return true;
00283   }
00284 
00286   int
00287   min_index() const {
00288     if (_data[0] < _data[1]) {
00289       return 0;
00290     }
00291     return 1;
00292   }
00293 
00295   template<class StrictWeakOrdering>
00296   int
00297   min_index(StrictWeakOrdering comp) const {
00298     if (comp(_data[0], _data[1])) {
00299       return 0;
00300     }
00301     return 1;
00302   }
00303 
00305   int
00306   max_index() const {
00307     if (_data[0] > _data[1]) {
00308       return 0;
00309     }
00310     return 1;
00311   }
00312 
00314   template<class StrictWeakOrdering>
00315   int
00316   max_index(StrictWeakOrdering comp) const {
00317     if (comp(_data[0], _data[1])) {
00318       return 0;
00319     }
00320     return 1;
00321   }
00322 
00323   // @}
00324   //--------------------------------------------------------------------------
00326   // @{
00327 
00329   iterator 
00330   begin() { 
00331     return _data;
00332   }
00333 
00335   iterator 
00336   end() { 
00337     return _data + 2;
00338   }
00339 
00341   pointer 
00342   data() { 
00343     return _data;
00344   }
00345 
00347   reference
00348   operator()(const int i) { 
00349 #ifdef DEBUG_FixedArray
00350     assert(i == 0 || i == 1);
00351 #endif
00352     return _data[i];
00353   }
00354 
00356   reference
00357   operator[](const int i) {
00358 #ifdef DEBUG_FixedArray
00359     assert(i == 0 || i == 1);
00360 #endif
00361     return _data[i];
00362   }
00363 
00365   void
00366   swap(FixedArray& x) {
00367     std::swap(_data[0], x._data[0]);
00368     std::swap(_data[1], x._data[1]);
00369   }
00370 
00372   template<typename EqualityComparable>
00373   iterator
00374   find(const EqualityComparable& x) {
00375     if (_data[0] == x) {
00376       return begin();
00377     }
00378     if (_data[1] == x) {
00379       return begin() + 1;
00380     }
00381     return end();
00382   }
00383 
00385   void
00386   negate() {
00387     _data[0] = - _data[0];
00388     _data[1] = - _data[1];
00389   }
00390 
00392   void
00393   fill(parameter_type value) {
00394     _data[0] = value;
00395     _data[1] = value;
00396   }
00397 
00399   template<typename InputIterator>
00400   void
00401   copy(InputIterator start, InputIterator finish) {
00402     _data[0] = *start;
00403     ++start;
00404     _data[1] = *start;
00405 #ifdef DEBUG_FixedArray
00406     assert(++start == finish);
00407 #endif
00408   }
00409     
00411   void
00412   sort() {
00413     if (_data[1] < _data[0]) {
00414       std::swap(_data[0], _data[1]);
00415     }
00416   }
00417 
00419   template<class StrictWeakOrdering>
00420   void
00421   sort(StrictWeakOrdering comp) {
00422     if (comp(_data[1], _data[0])) {
00423       std::swap(_data[0], _data[1]);
00424     }
00425   }
00426 
00427   // @}
00428   //--------------------------------------------------------------------------
00430   // @{
00431 
00433   FixedArray& 
00434   operator+=(parameter_type x) {
00435     _data[0] += x;
00436     _data[1] += x;
00437     return *this;
00438   }
00439 
00441   FixedArray& 
00442   operator-=(parameter_type x) {
00443     _data[0] -= x;
00444     _data[1] -= x;
00445     return *this;
00446   }
00447 
00449   FixedArray& 
00450   operator*=(parameter_type x) {
00451     _data[0] *= x;
00452     _data[1] *= x;
00453     return *this;
00454   }
00455 
00457   FixedArray& 
00458   operator/=(parameter_type x) {
00459 #ifdef DEBUG_FixedArray
00460     assert(x != 0);
00461 #endif
00462     _data[0] /= x;
00463     _data[1] /= x;
00464     return *this;
00465   }
00466 
00468   FixedArray& 
00469   operator%=(parameter_type x) {
00470 #ifdef DEBUG_FixedArray
00471     assert(x != 0);
00472 #endif
00473     _data[0] %= x;
00474     _data[1] %= x;
00475     return *this;
00476   }
00477 
00479   FixedArray& 
00480   operator<<=(const int offset) {
00481     _data[0] <<= offset;
00482     _data[1] <<= offset;
00483     return *this;
00484   }
00485 
00487   FixedArray& 
00488   operator>>=(const int offset) {
00489     _data[0] >>= offset;
00490     _data[1] >>= offset;
00491     return *this;
00492   }
00493 
00494   // @}
00495   //--------------------------------------------------------------------------
00497   // @{
00498 
00500   template<typename T2>
00501   FixedArray& 
00502   operator+=(const FixedArray<2,T2>& x) {
00503     _data[0] += x[0];
00504     _data[1] += x[1];
00505     return *this;
00506   }
00507 
00509   template<typename T2>
00510   FixedArray& 
00511   operator-=(const FixedArray<2,T2>& x) {
00512     _data[0] -= x[0];
00513     _data[1] -= x[1];
00514     return *this;
00515   }
00516 
00518   template<typename T2>
00519   FixedArray& 
00520   operator*=(const FixedArray<2,T2>& x) {
00521     _data[0] *= x[0];
00522     _data[1] *= x[1];
00523     return *this;
00524   }
00525 
00527   template<typename T2>
00528   FixedArray& 
00529   operator/=(const FixedArray<2,T2>& x) {
00530 #ifdef DEBUG_FixedArray
00531     assert(x[0] != 0 && x[1] != 0);
00532 #endif
00533     _data[0] /= x[0];
00534     _data[1] /= x[1];
00535     return *this;
00536   }
00537 
00539   template<typename T2>
00540   FixedArray& 
00541   operator%=(const FixedArray<2,T2>& x) {
00542 #ifdef DEBUG_FixedArray
00543     assert(x[0] != 0 && x[1] != 0);
00544 #endif
00545     _data[0] %= x[0];
00546     _data[1] %= x[1];
00547     return *this;
00548   }
00549 
00550   // @}
00551 };
00552 
00553 
00554 END_NAMESPACE_ADS
00555 
00556 #define __FixedArray2_ipp__
00557 #include "FixedArray2.ipp"
00558 #undef __FixedArray2_ipp__

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