buffer.inc

Go to the documentation of this file.
00001 
00029 #ifndef ABA_BUFFER_INC
00030 #define ABA_BUFFER_INC
00031 
00032 #ifdef ABACUS_PARALLEL
00033 #include "abacus/message.h"
00034 #endif
00035 
00036 template <class Type> 
00037 inline ABA_BUFFER<Type>::ABA_BUFFER(ABA_GLOBAL *glob, int size) 
00038   :
00039   glob_(glob),
00040   size_(size),
00041   n_(0)
00042 
00043 { 
00044   buf_ = new Type[size];
00045 }
00046 
00047 template <class Type>
00048 ABA_BUFFER<Type>::ABA_BUFFER(const ABA_BUFFER<Type> &rhs) 
00049   :  
00050   glob_(rhs.glob_),  
00051   size_(rhs.size_),  
00052   n_(rhs.n_)
00053 {
00054   buf_ = new Type[size_] ;
00055   for (int i = 0; i < n_; i++) buf_[i] = rhs.buf_[i];
00056 }
00057 
00058 template <class Type> 
00059 inline ABA_BUFFER<Type>::~ABA_BUFFER()
00060 {
00061   delete [] buf_;
00062 }
00063 
00064 #ifdef ABACUS_PARALLEL
00065 
00066   template <class Type>
00067   ABA_BUFFER<Type>::ABA_BUFFER(const ABA_GLOBAL *glob, ABA_MESSAGE &msg)
00068   :  
00069     glob_(glob)
00070   {
00071     glob_->err() << "ABA_BUFFER::ABA_BUFFER() : A ABA_BUFFER of some type coudn't"
00072                     " be received. You have to implement a template"
00073                     " specialization of the message constructor for"
00074                     "  that type!" << endl;
00075     exit(Fatal);
00076   }
00077 
00078   template<class Type>
00079   void ABA_BUFFER<Type>::pack(ABA_MESSAGE &msg) const
00080   {
00081     glob_->err() << "ABA_BUFFER::pack() : A ABA_BUFFER of some type coudn't"
00082                     " be sent. You first have to implement a template"
00083                     " specialization of the pack() template function"
00084                     " for that type!" << endl;
00085     exit(Fatal);
00086   }
00087 
00088 #endif
00089 
00090 template <class Type>
00091 const ABA_BUFFER<Type>& ABA_BUFFER<Type>::operator=(const ABA_BUFFER<Type>& rhs)
00092 {
00093   if (this != &rhs) {
00094     // check if the dimension of both buffers are equal
00095     /* If in an assignment operation of two buffers the dimension of both
00096      * buffers do not equal we terminate the program.
00097      */
00098      if (size_ != rhs.size_) {
00099        glob_->err() << "ABA_BUFFER::operator= : dimension of left and right hand side ";
00100        glob_->err() << "do not equal (" << size_ << " != " << rhs.size_ << ")" << endl;
00101        exit(Fatal);
00102      }
00103 
00104     // assign the elements of ABA_BUFFER
00105     glob_ = rhs.glob_;
00106     size_ = rhs.size_;
00107     n_    = rhs.n_;
00108 
00109     for (int i = 0; i < n_; i++) buf_[i] = rhs.buf_[i];
00110 
00111   }
00112 
00113   return *this;
00114 }
00115 
00116 template <class Type>
00117 ostream& operator<<(ostream &out, const ABA_BUFFER<Type> &buffer)
00118 {
00119   for (int i = 0; i < buffer.number(); i++)
00120     out << i << ": " << buffer[i] << endl;
00121   return out;
00122 }
00123 
00124 template <class Type>
00125 inline Type& ABA_BUFFER<Type>::operator[](int i)
00126 {
00127   // \a Buffer: body of operator \a []
00128   /* Only access to already buffered elements is permitted.
00129    */ 
00130 #ifdef ABACUSSAFE
00131   if(i < 0 || i >= n_) {
00132     glob_->err() << "ABA_BUFFER::operator[" << i << "]: only ";
00133     glob_->err() << n_ << " elements buffered." << endl;
00134     exit(Fatal);
00135   }
00136 #endif
00137 
00138   return buf_[i];
00139 
00140 }
00141 
00142 template <class Type>
00143 inline const Type& ABA_BUFFER<Type>::operator[](int i) const
00144 {
00145   // \a Buffer: body of operator \a []
00146   /* Only access to already buffered elements is permitted.
00147    */ 
00148 #ifdef ABACUSSAFE
00149   if(i < 0 || i >= n_) {
00150     glob_->err() << "ABA_BUFFER::operator[" << i << "]: only ";
00151     glob_->err() << n_ << " elements buffered." << endl;
00152     exit(Fatal);
00153   }
00154 #endif
00155 
00156   return buf_[i];
00157 
00158 }
00159 
00160 template <class Type>
00161 inline int ABA_BUFFER<Type>::size() const
00162 {
00163   return size_;
00164 }
00165 
00166 template <class Type>
00167 inline int ABA_BUFFER<Type>::number() const
00168 {
00169   return n_;
00170 }
00171 
00172 template <class Type>
00173 inline bool ABA_BUFFER<Type>::full() const
00174 {
00175   return n_ == size() ? true : false;
00176 }
00177 
00178 template <class Type>
00179 inline bool ABA_BUFFER<Type>::empty() const
00180 {
00181   return n_ == 0 ? true : false;
00182 }
00183 
00184 template <class Type>
00185 inline void ABA_BUFFER<Type>::push(Type item)
00186 {
00187 #ifdef ABACUSSAFE
00188   if(full()) {
00189    glob_->err() << "ABA_BUFFER::push(): buffer is full, " << n_ << " elements buffered" << endl;
00190    exit(Fatal);
00191   }
00192 #endif
00193 
00194   buf_[n_++] = item;
00195 }
00196 
00197 template <class Type>
00198 inline Type ABA_BUFFER<Type>::pop()
00199 {
00200 #ifdef ABACUSSAFE
00201   if(empty()) {
00202     glob_->err() << "ABA_BUFFER::pop(): buffer is empty." << endl;
00203     exit(Fatal);
00204   }
00205 #endif
00206 
00207   return buf_[--n_];
00208 }
00209 
00210 template <class Type>
00211 inline void ABA_BUFFER<Type>::clear()
00212 {
00213   n_ = 0;
00214 }
00215 
00216 template <class Type>
00217 void ABA_BUFFER<Type>::leftShift(ABA_BUFFER<int> &ind)
00218 {
00219   const int nInd = ind.number();
00220 
00221   if (nInd == 0) return;
00222   
00223   int i,j;
00224   int current = ind[0];
00225 
00228 #ifdef ABACUSSAFE
00229   if(ind[0] < 0 || ind[0] >= n_) {
00230     glob_->err() << "ABA_BUFFER:leftShift(): shift index 0 (";
00231     glob_->err() << ind[0] << ") not valid" << endl;
00232     exit(Fatal);
00233   }
00234 #endif      
00235 
00236   for (i = 0; i < nInd - 1; i++) {
00238 #ifdef ABACUSSAFE
00239     if(ind[i+1] < 0 || ind[i+1] >= n_) {
00240       glob_->err() << "ABA_BUFFER:leftShift(): shift index " << i+1;
00241       glob_->err() << " (" << ind[i+1] << ") not valid" << endl;
00242       exit(Fatal);
00243     }
00244 #endif      
00245 
00246     const int last = ind[i+1];
00247     for(j = ind[i]+1; j < last; j++)
00248       buf_[current++] = buf_[j];
00249   }
00250 
00252   for (j = ind[nInd - 1]+1; j < n_; j++)
00253     buf_[current++] = buf_[j];
00254 
00255   n_ -= nInd;
00256 
00257 }
00258 
00259 template <class Type>
00260 void ABA_BUFFER<Type>::realloc(int newSize)
00261 {
00262 
00263   Type *newBuf = new Type[newSize];
00264   int   newN;
00265 
00266   if (n_ < newSize) newN = n_;
00267   else              newN = newSize;
00268 
00269   for (int i = 0; i < newN; i++) newBuf[i] = buf_[i];
00270 
00271   delete [] buf_;
00272 
00273   buf_   = newBuf;
00274   size_  = newSize;
00275   n_     = newN;
00276 }
00277 
00278 #endif   

Generated on Tue Aug 14 18:09:53 2007 for ABACUS by  doxygen 1.5.1