FLOPC++
|
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 using std::string; 00014 00015 #include <vector> 00016 using std::vector; 00017 00018 namespace flopc { 00019 00029 class Functor { 00030 public: 00031 virtual void operator()() const = 0; 00032 protected: 00033 Functor() {} 00034 virtual ~Functor() {} 00035 Functor(const Functor&); 00036 private: 00037 Functor& operator=(const Functor&); 00038 }; 00039 00044 template<int nbr, class T> 00045 vector<T> makeVector(T i1, T i2=0, T i3=0, T i4=0, T i5=0) { 00046 vector<T> S(nbr); 00047 S[0] = i1; 00048 if (nbr==1) return S; 00049 S[1] = i2; 00050 if (nbr==2) return S; 00051 S[2] = i3; 00052 if (nbr==3) return S; 00053 S[3] = i4; 00054 if (nbr==4) return S; 00055 S[4] = i5; 00056 return S; 00057 } 00058 00060 inline int mod(int a, int b) { 00061 int t = a % b; 00062 return (t>=0) ? t : t+b; 00063 } 00064 00066 const int outOfBound = -2; 00067 00072 class RowMajor { 00073 public: 00074 int size() const { return size_; } 00075 protected: 00076 RowMajor(int s1, int s2, int s3, int s4, int s5) : 00077 size1(s1), size2(s2), size3(s3), size4(s4), size5(s5), 00078 size_(s1*s2*s3*s4*s5) {} 00079 int f(int i1=0, int i2=0, int i3=0, int i4=0, int i5=0) const { 00080 if ( i1==outOfBound || i2==outOfBound || i3==outOfBound || 00081 i4==outOfBound || i5==outOfBound ) { 00082 return outOfBound; 00083 } else { 00084 int i = i1; 00085 i *= size2; i += i2; i *= size3; i += i3; 00086 i *= size4; i += i4; i *= size5; i += i5; 00087 return i; 00088 } 00089 } 00090 int size1,size2,size3,size4,size5,size_; 00091 }; 00092 00097 class Named { 00098 public: 00099 string getName() const { return name; } 00100 void setName(const string& n) { name = n; } 00101 private: 00102 string name; 00103 }; 00104 00108 template<class T> class Handle { 00109 public: 00110 const T &operator->() const { 00111 return root; 00112 } 00113 Handle(const T &r) : root(r) { 00114 increment(); 00115 } 00116 Handle(const Handle& h) : root(h.root) { 00117 increment(); 00118 } 00119 const Handle& operator=(const Handle& h) { 00120 if (root != h.root) { 00121 decrement(); 00122 root = h.root; 00123 increment(); 00124 } 00125 return *this; 00126 } 00127 bool isDefined() { 00128 return root != 0; 00129 } 00130 ~Handle() { 00131 decrement(); 00132 } 00133 protected: 00134 void increment() { 00135 if(root != 0) { 00136 (root->count)++; 00137 } 00138 } 00139 void decrement() { 00140 if(root != 0) { 00141 if(root->count == 1) { 00142 delete root; 00143 root = 0; 00144 } else { 00146 --(root->count); 00148 } 00149 } 00150 } 00151 private: 00152 Handle() : root(0) {} 00153 T root; 00154 }; 00155 00156 } // End of namespace flopc 00157 #endif