MP_utilities.hpp

Go to the documentation of this file.
00001 // ******************** FlopCpp **********************************************
00002 // File: MP_utilities.hpp
00003 // $Id$
00004 // Author: Tim Helge Hultberg (thh@mat.ua.pt)
00005 // Copyright (C) 2003 Tim Helge Hultberg
00006 // All Rights Reserved.
00007 // ****************************************************************************
00008 
00009 #ifndef _MP_utilities_hpp_
00010 #define _MP_utilities_hpp_
00011 
00012 #include <string>
00013 #include <vector>
00014 
00015 namespace flopc {
00016 
00026     class Functor {
00027     public:
00028         virtual void operator()() const = 0;
00029     protected:
00030         Functor() {}
00031         virtual ~Functor() {}
00032     private:
00033         Functor(const Functor&);
00034         Functor& operator=(const Functor&);
00035     };  
00036 
00041     template<int nbr, class T>
00042     std::vector<T> makeVector(T i1, T i2=0, T i3=0, T i4=0, T i5=0) {
00043         std::vector<T> S(nbr);
00044         S[0] = i1; 
00045         if (nbr==1) return S;
00046         S[1] = i2;
00047         if (nbr==2) return S;
00048         S[2] = i3;
00049         if (nbr==3) return S;
00050         S[3] = i4;
00051         if (nbr==4) return S;
00052         S[4] = i5; 
00053         return S;
00054     }
00055 
00057     inline int mod(int a, int b) {
00058         int t = a % b;
00059         return  (t>=0) ? t : t+b;
00060     }
00061 
00063     const int outOfBound = -2;    
00064 
00069     class RowMajor {
00070     public:
00071         int size() const { return size_; }
00072     protected:
00073         RowMajor(int s1, int s2, int s3, int s4, int s5) :
00074             size1(s1), size2(s2), size3(s3), size4(s4), size5(s5),
00075             size_(s1*s2*s3*s4*s5) {}
00076         int f(int i1=0, int i2=0, int i3=0, int i4=0, int i5=0) const {
00077             if ( i1==outOfBound || i2==outOfBound || i3==outOfBound ||
00078                  i4==outOfBound || i5==outOfBound ) {
00079                 return outOfBound;
00080             } else {
00081                 int i = i1;
00082                 i *= size2; i += i2;  i *= size3; i += i3;
00083                 i *= size4; i += i4;  i *= size5; i += i5;
00084                 return i;
00085             } 
00086         }
00087         int size1,size2,size3,size4,size5,size_;
00088     };
00089 
00094     class Named {
00095     public:
00096         std::string getName() const { return name; }
00097         void setName(const std::string& n) { name = n; }
00098     private:
00099         std::string name;
00100     };
00101 
00105     template<class T> class Handle {
00106     public:
00107         const T &operator->() const {
00108             return root;
00109         }
00110         Handle(const T &r) : root(r) {
00111             increment();
00112         }
00113         Handle(const Handle& h) : root(h.root) {
00114             increment();
00115         }
00116         const Handle& operator=(const Handle& h) {
00117             if (root != h.root) {
00118                 decrement();
00119                 root = h.root;
00120                 increment();
00121             }
00122             return *this;
00123         }
00124         ~Handle() {
00125             decrement();
00126         }
00127     protected:
00128         void increment() {
00129             if(root != 0) {
00130                 (root->count)++;
00131             }
00132         }
00133         void decrement() {
00134             if(root != 0) {
00135                 if(root->count == 1) {
00136                     delete root;
00137                     root = 0;
00138                 } else {
00140                     --(root->count);
00142                 }
00143             }
00144         }
00145     private:
00146         Handle() : root(0) {}
00147         T root;
00148     };
00149 
00150 } // End of namespace flopc
00151 #endif

Generated on Sun Nov 6 03:14:50 2011 for FLOPC++ by  doxygen 1.4.7