OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
ASUnary.h
Go to the documentation of this file.
1 #ifndef OPAL_ASUnary_HH
2 #define OPAL_ASUnary_HH 1
3 
4 // ------------------------------------------------------------------------
5 // $RCSfile: ASUnary.h,v $
6 // ------------------------------------------------------------------------
7 // $Revision: 1.1.1.1 $
8 // ------------------------------------------------------------------------
9 // Copyright: see Copyright.readme
10 // ------------------------------------------------------------------------
11 //
12 // Template class: ASUnary<T>
13 //
14 // ------------------------------------------------------------------------
15 //
16 // $Date: 2000/03/27 09:33:41 $
17 // $Author: Andreas Adelmann $
18 //
19 // ------------------------------------------------------------------------
20 
22 #include "Expressions/SConstant.h"
23 #include "Expressions/TFunction1.h"
24 #include "Utilities/DomainError.h"
26 #include <cerrno>
27 #include <iosfwd>
28 #include <vector>
29 
30 
31 namespace Expressions {
32 
33  // Template Class ASUnary
34  // ----------------------------------------------------------------------
36  // This expression first evaluates its operand to get an array.
37  // The scalar result is computed then by applying a scalar-valued
38  // function to this array (e. g. minimum or maximum).
39 
40  template <class T>
41  class ASUnary: public Scalar<T> {
42 
43  public:
44 
46  // Use a function which maps a std::vector<T> to a T and an array
47  // expression.
48  ASUnary(const TFunction1<T, const std::vector<T>&> &function,
49  PtrToArray<T> &oper);
50 
51  ASUnary(const ASUnary<T> &);
52  virtual ~ASUnary();
53 
55  virtual Scalar<T> *clone() const;
56 
58  virtual T evaluate() const;
59 
61  // If all component of the argument are constant, then evaluate
62  // and store the result as a constant.
63  static PtrToScalar<T> make
64  (const TFunction1<T, const std::vector<T>&> &function, PtrToArray<T> &oper);
65 
67  virtual void print(std::ostream &, int precedence = 99) const;
68 
69  private:
70 
71  // Not implemented.
72  ASUnary();
73  void operator=(const ASUnary &);
74 
75  // The operation object.
77 
78  // The operand.
80  };
81 
82 
83  // Implementation
84  // ----------------------------------------------------------------------
85 
86  template <class T> inline
88  Scalar<T>(rhs),
89  fun(rhs.fun), opr(rhs.opr->clone())
90  {}
91 
92 
93  template <class T> inline
94  ASUnary<T>::ASUnary(const TFunction1<T, const std::vector<T>&> &function,
95  PtrToArray<T> &oper):
96  fun(function), opr(oper)
97  {}
98 
99 
100  template <class T> inline
102  {}
103 
104 
105  template <class T> inline
107  return new ASUnary<T>(*this);
108  }
109 
110 
111  template <class T> inline
113  errno = 0;
114  const std::vector<T> arg = opr->evaluate();
115  T result = (*fun.function)(arg);
116 
117  // Test for run-time evaluation errors.
118  switch(errno) {
119 
120  case EDOM:
121  throw DomainError("ASUnary::evaluate()");
122 
123  case ERANGE:
124  // Ignore underflow.
125  if(result == T(0)) return result;
126  throw OverflowError("ASUnary::evaluate()");
127 
128  default:
129  ;
130  }
131 
132  return result;
133  }
134 
135 
136  template <class T> inline
138  (const TFunction1<T, const std::vector<T>&> &function, PtrToArray<T> &oper) {
139  // We must pick up the constant flag before the ownership of "oper"
140  // is transferred to "result".
141  bool isConst = oper->isConstant();
142  PtrToScalar<T> result = new ASUnary<T>(function, oper);
143 
144  if(function.precedence != -2) {
145  // Not a random expression.
146  if(isConst) {
147  // Replace constant expression by its value.
148  result = new SConstant<T>(result->evaluate());
149  }
150  }
151 
152  // Other expression.
153  return result.release();
154  }
155 
156 
157  template <class T> inline
158  void ASUnary<T>::print(std::ostream &os, int precedence) const {
159  if(fun.precedence >= 0) {
160  if(fun.precedence <= precedence) os << "(";
161  os << fun.name;
162  opr->print(os, fun.precedence);
163  if(fun.precedence <= precedence) os << ")";
164  } else {
165  os << fun.name << "(";
166  opr->print(os, 0);
167  os << ")";
168  }
169  }
170 
171 }
172 
173 #endif // OPAL_ASUnary_HH
A scalar expression.
Definition: Expressions.h:79
A scalar constant expression.
Definition: SConstant.h:35
Definition: rbendmap.h:8
A pointer to a scalar expression.
Definition: Expressions.h:116
static PtrToScalar< T > make(const TFunction1< T, const std::vector< T > & > &function, PtrToArray< T > &oper)
Make new expression.
Definition: ASUnary.h:138
virtual Scalar< T > * clone() const
Make clone.
Definition: ASUnary.h:106
virtual T evaluate() const
Evaluate expression.
Definition: ASUnary.h:112
Scalar< T > * release()
Release ownership.
virtual ~ASUnary()
Definition: ASUnary.h:101
virtual void print(std::ostream &, int precedence=99) const
Print expression.
Definition: ASUnary.h:158
A function of one U, returning a T.
Definition: TFunction1.h:31
const TFunction1< T, const std::vector< T > & > & fun
Definition: ASUnary.h:76
void operator=(const ASUnary &)
arg(a))
Overflow exception.
Definition: OverflowError.h:32
Domain error exception.
Definition: DomainError.h:32
PtrToArray< T > opr
Definition: ASUnary.h:79
A pointer to an array expression.
Definition: Expressions.h:186
A scalar expression with one array operand.
Definition: ASUnary.h:41