FLOPC++
MP_utilities.hpp
Go to the documentation of this file.
1 // ******************** FlopCpp **********************************************
2 // File: MP_utilities.hpp
3 // $Id$
4 // Author: Tim Helge Hultberg (thh@mat.ua.pt)
5 // Copyright (C) 2003 Tim Helge Hultberg
6 // All Rights Reserved.
7 // ****************************************************************************
8 
9 #ifndef _MP_utilities_hpp_
10 #define _MP_utilities_hpp_
11 
12 #include <string>
13 using std::string;
14 
15 #include <vector>
16 using std::vector;
17 
18 namespace flopc {
19 
29  class Functor {
30  public:
31  virtual void operator()() const = 0;
32  protected:
33  Functor() {}
34  virtual ~Functor() {}
35  Functor(const Functor&);
36  private:
37  Functor& operator=(const Functor&);
38  };
39 
44  template<int nbr, class T>
45  vector<T> makeVector(T i1, T i2=0, T i3=0, T i4=0, T i5=0) {
46  vector<T> S(nbr);
47  S[0] = i1;
48  if (nbr==1) return S;
49  S[1] = i2;
50  if (nbr==2) return S;
51  S[2] = i3;
52  if (nbr==3) return S;
53  S[3] = i4;
54  if (nbr==4) return S;
55  S[4] = i5;
56  return S;
57  }
58 
60  inline int mod(int a, int b) {
61  int t = a % b;
62  return (t>=0) ? t : t+b;
63  }
64 
66  const int outOfBound = -2;
67 
72  class RowMajor {
73  public:
74  int size() const { return size_; }
75  protected:
76  RowMajor(int s1, int s2, int s3, int s4, int s5) :
77  size1(s1), size2(s2), size3(s3), size4(s4), size5(s5),
78  size_(s1*s2*s3*s4*s5) {}
79  int f(int i1=0, int i2=0, int i3=0, int i4=0, int i5=0) const {
80  if ( i1==outOfBound || i2==outOfBound || i3==outOfBound ||
81  i4==outOfBound || i5==outOfBound ) {
82  return outOfBound;
83  } else {
84  int i = i1;
85  i *= size2; i += i2; i *= size3; i += i3;
86  i *= size4; i += i4; i *= size5; i += i5;
87  return i;
88  }
89  }
90  int size1,size2,size3,size4,size5,size_;
91  };
92 
97  class Named {
98  public:
99  string getName() const { return name; }
100  void setName(const string& n) { name = n; }
101  private:
102  string name;
103  };
104 
108  template<class T> class Handle {
109  public:
110  const T &operator->() const {
111  return root;
112  }
113  Handle(const T &r) : root(r) {
114  increment();
115  }
116  Handle(const Handle& h) : root(h.root) {
117  increment();
118  }
119  const Handle& operator=(const Handle& h) {
120  if (root != h.root) {
121  decrement();
122  root = h.root;
123  increment();
124  }
125  return *this;
126  }
127  bool isDefined() {
128  return root != 0;
129  }
131  decrement();
132  }
133  protected:
134  void increment() {
135  if(root != 0) {
136  (root->count)++;
137  }
138  }
139  void decrement() {
140  if(root != 0) {
141  if(root->count == 1) {
142  delete root;
143  root = 0;
144  } else {
146  --(root->count);
148  }
149  }
150  }
151  private:
152  Handle() : root(0) {}
153  T root;
154  };
155 
156 } // End of namespace flopc
157 #endif
Handle(const Handle &h)
Functor & operator=(const Functor &)
vector< T > makeVector(T i1, T i2=0, T i3=0, T i4=0, T i5=0)
Utility for doing reference counted pointers.
string getName() const
Utility interface class for adding a string name onto a structure.
const int outOfBound
Distinct return value on conditions where an index goes out of bounds.
All flopc++ code is contained within the flopc namespace.
Definition: flopc.cpp:11
const T & operator->() const
Handle(const T &r)
int f(int i1=0, int i2=0, int i3=0, int i4=0, int i5=0) const
Function object. Often used.
RowMajor(int s1, int s2, int s3, int s4, int s5)
void setName(const string &n)
virtual void operator()() const =0
const Handle & operator=(const Handle &h)
int mod(int a, int b)
return the strictly positive modulus of two integers
int size() const
virtual ~Functor()