array.hh

Go to the documentation of this file.
00001 #ifndef DUNE_ARRAY_HH
00002 #define DUNE_ARRAY_HH
00003 
00008 #warning This file is deprecated.  Please use std::vector instead of Dune::Array!
00009 
00010 #include<iostream>
00011 #include<iomanip>
00012 #include<string>
00013 #include<cassert>
00014 
00015 #include<rpc/rpc.h>
00016 #include"exceptions.hh"
00017 
00018 namespace Dune 
00019 {
00025 
00026   template <class T> 
00027   class Array {
00028   public:
00029   
00033       class Iterator {  
00034       private:          
00035           T* p;         // Iterator ist Zeiger auf Feldelement
00036 
00037       public:        
00039           Iterator();
00040  
00042           bool operator!= (const Iterator& x);
00043 
00045           bool operator== (const Iterator& x);
00046 
00048           Iterator operator++ ();   // prefix Stroustrup p. 292
00049           
00051           Iterator operator++ (int);// postfix
00052 
00054           const T& operator* () const;
00055 
00057           const T* operator-> () const; // Stroustrup p. 289
00058 
00059           friend class Array<T>;
00060         } ;
00061 
00063         Iterator begin () const; 
00064 
00066         Iterator end () const;
00067 
00069         Array();
00070         
00072         explicit Array(int m);
00073 
00075         Array (const Array<T>&);
00076 
00078         ~Array();
00079 
00081         Array<T>& operator= (const Array<T>&);
00082 
00084         Array<T>& operator= (const T& a);
00085 
00087         void resize (int m);
00088 
00090       void realloc (int m) {resize(m);}
00091 
00093       void swap ( Array<T> &copy)
00094       {
00095         T *tmp = copy.p;
00096         copy.p = p;
00097         p = tmp;
00098        
00099         int fake=copy.n;
00100         copy.n = n;
00101         n = fake;
00102       }
00103       
00105       int size () const;
00106 
00108       void set(const T& v) {
00109           for (int i=0; i<size(); ++i)
00110               (*this)[i] = v;
00111       }
00112 
00114       T& operator[](int i);
00115 
00117       const T& operator[](int i) const;
00118 
00120       typedef T MemberType;  
00121       typedef T value_type; // definition conforming to STL  
00122 
00129       void print (int k, std::string s, std::string row)
00130         {
00131           char buf[96];
00132           std::cout << s << " size=" << n << " {" << std::endl;
00133           for (int i=0; i<n/k; i++)
00134                 {
00135                   sprintf(buf,"%4d",k*i);
00136                   std::cout << buf << " " << row << " ";
00137                   for (int j=i*k; j<(i+1)*k; j++)
00138                         {
00139                           sprintf(buf,"%10.3E ",p[j]);
00140                           std::cout << buf;
00141                         }
00142                   std::cout << std::endl;
00143                 }
00144           int i = (n/k)*k;
00145           if (i<n)
00146                 {
00147                   sprintf(buf,"%4d",i);
00148                   std::cout << buf << " " << row << " ";
00149                   for (int j=i; j<n; j++)
00150                         {
00151                           sprintf(buf,"%10.3E ",p[j]);
00152                           std::cout << buf;
00153                         }
00154                   std::cout << std::endl;
00155                 }
00156           std::cout << "}" << std::endl;          
00157         }
00158 
00161       bool processXdr(XDR *xdrs)
00162       {
00163         if(xdrs != NULL)
00164         {
00165           int len = n;
00166           xdr_int( xdrs, &len );
00167           if(len != n) resize(len);
00168           xdr_vector(xdrs,(char *) p,n,sizeof(T),(xdrproc_t)xdr_double);
00169           return true;
00170         }
00171         else 
00172           return false;
00173       }
00174     
00175       T * raw()
00176       {
00177         return p;
00178       }
00179   protected:
00180       int n;  // Anzahl Elemente; n=0 heisst, dass kein array allokiert ist!
00181       T *p;   // Zeiger auf built-in array
00182   } ;
00183 
00184 
00186   template<class T>
00187   inline typename Array<T>::Iterator Array<T>::begin () const
00188   {
00189         Iterator tmp;  // hat Zeiger 0
00190         tmp.p = p;     // Zeiger auf Feldelement 0
00191         return tmp;
00192   }
00193   
00195   template<class T>
00196   inline typename Array<T>::Iterator Array<T>::end () const
00197   {
00198         Iterator tmp;
00199         tmp.p = &(p[n]); // Zeiger auf Feldelement NACH letztem !
00200         return tmp;      // das funktioniert: Stroustrup p. 92.
00201   }
00202 
00206   template <class T>
00207   inline Array<T>::~Array () 
00208   { 
00209         if (n>0) delete[] p; 
00210   }
00211   
00215   template <class T>
00216   inline Array<T>::Array () : n(0), p(0) {}
00217   
00221   template <class T>
00222   inline Array<T>::Array (int m) : n(m) ,p(0)
00223   {
00224         if (n<=0)
00225           {
00226                 n = 0;
00227                 return;
00228           }
00229         try {
00230           p = new T[n];
00231         }
00232         catch (std::bad_alloc) {
00233             DUNE_THROW(OutOfMemoryError, "Not enough memory!");
00234         }
00235   }
00236 
00240   template <class T>
00241   inline void Array<T>::resize (int m)
00242   {
00243         if (m!=n)
00244           {
00245                 if (n>0) 
00246                 {
00247                   delete[] p;
00248                   p = NULL;
00249                 }
00250                 n = m;
00251                 if (n==0) 
00252                 {
00253                   p = NULL;
00254                   return;
00255                 }
00256                 try {
00257                   p = new T[n];
00258                 }
00259                 catch (std::bad_alloc) {
00260                     DUNE_THROW(OutOfMemoryError, "Not enough memory!");
00261                 }
00262           }
00263      return;    
00264   }
00265 
00266   /* \brief Copy-constructor
00267      Implements deep copy.
00268   */
00269   template <class T>
00270   inline Array<T>::Array (const Array<T>& a)
00271   {
00272         // Erzeuge Feld mit selber Groesse wie a
00273         n = a.n;
00274         if (n>0)
00275           {
00276                 try {
00277                   p = new T[n];
00278                 }
00279                 catch (std::bad_alloc) {
00280                     DUNE_THROW(OutOfMemoryError, "Not enough memory!");
00281                 }
00282           }
00283 
00284         // und kopiere Elemente
00285         for (int i=0; i<n; i=i+1) p[i]=a.p[i];
00286   }
00287 
00288   /* \brief Assignment operator
00289    */
00290   template <class T>
00291   inline Array<T>& Array<T>::operator= (const Array<T>& a)
00292   {
00293         if (&a!=this) // nur bei verschiedenen Objekten was tun
00294           {
00295                 if (n!=a.n)
00296                   {
00297                         // allokiere fuer this ein Feld der Groesse a.n
00298                         if (n>0) delete[] p; // altes Feld loeschen
00299                         n = a.n;
00300                         if (n>0)
00301                           {
00302                                 try {
00303                                   p = new T[n];
00304                                 }
00305                                 catch (std::bad_alloc) {
00306                                     DUNE_THROW(OutOfMemoryError, "Not enough memory!");
00307                                 }
00308                           }
00309                   }
00310                 for (int i=0; i<n; i++) p[i]=a.p[i];
00311           }
00312         return *this; // Gebe Referenz zurueck damit a=b=c; klappt
00313   }
00314 
00315   /* \brief Assignment operator
00316      All elements of the Array get the value a
00317   */
00318   template <class T>
00319   inline Array<T>& Array<T>::operator= (const T& a)
00320   {
00321         for (int i=0; i<n; i++) p[i]=a;
00322         return *this; // Gebe Referenz zurueck damit a=b=c; klappt
00323   }
00324   
00325   /* \brief Access operator
00326      Accessing of elements in Array. With bounds check.
00327    */
00328   template <class T>
00329   inline T& Array<T>::operator[] (int i)
00330   {
00331       assert( ((i<0) || (i>=size()) ? (std::cout << std::endl << i << " i|size " << size() << std::endl, 0) : 1));
00332       return p[i];
00333   }
00334 
00335   /* \brief Access operator
00336      Accessing of elements in Array. With bounds check. Const version
00337   */
00338   template <class T>
00339   inline const T& Array<T>::operator[] (int i) const
00340   {
00341       assert(i>=0);
00342       assert(i<size());
00343       return p[i];
00344   }
00345 
00346   /* \brief Size of Array
00347    */
00348   template <class T>
00349   inline int Array<T>::size () const
00350   { 
00351         return n; 
00352   }
00353 
00354   /* \brief Output operator
00355    */
00356   template <class T>
00357   std::ostream& operator<< (std::ostream& s, const Array<T>& a)
00358   {
00359         s << "array " << a.size() << " elements = [" << std::endl;
00360         for (int i=0; i<a.size(); i++)
00361           s << "    " << i << "  " << a[i] << std::endl;
00362         s << "]" << std::endl;
00363         return s;
00364   }
00365 
00367   template<class T>
00368   inline Array<T>::Iterator::Iterator () 
00369   {
00370         p=0; // nicht initialisierter Iterator
00371   }
00372 
00374   template<class T>
00375   inline bool Array<T>::Iterator::operator!= 
00376   (const typename Array<T>::Iterator& x)
00377   {
00378         return p != x.p;
00379   }
00380 
00382   template<class T>
00383   inline bool Array<T>::Iterator::operator== 
00384   (const typename Array<T>::Iterator& x)
00385   {
00386         return p == x.p;
00387   }
00388 
00390   template<class T>
00391   inline typename Array<T>::Iterator Array<T>::Iterator::operator++ () // prefix
00392   {
00393         p++;  // C Zeigerarithmetik: p zeigt auf naechstes Feldelement
00394         return *this;
00395   }
00396 
00398   template<class T>
00399   inline typename Array<T>::Iterator Array<T>::Iterator::operator++ (int) // postfix
00400   {
00401         Iterator tmp = *this;
00402         ++*this;
00403         return tmp;
00404   }
00405 
00407   template<class T>
00408   inline const T& Array<T>::Iterator::operator* () const
00409   {
00410         return *p;
00411   }
00412                 
00414   template<class T>
00415   inline const T* Array<T>::Iterator::operator-> () const
00416   {
00417         return p;
00418   }
00419 
00422 } // end namespace Dune
00423 #endif

Generated on 9 Apr 2008 with Doxygen (ver 1.5.2) [logfile].