OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
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
21#include <cerrno>
22#include <iosfwd>
23#include <vector>
29
30namespace Expressions {
31
32 // Template Class ASUnary
33 // ----------------------------------------------------------------------
35 // This expression first evaluates its operand to get an array.
36 // The scalar result is computed then by applying a scalar-valued
37 // function to this array (e. g. minimum or maximum).
38
39 template <class T>
40 class ASUnary : public Scalar<T> {
41 public:
43 // Use a function which maps a std::vector<T> to a T and an array
44 // expression.
45 ASUnary(const TFunction1<T, const std::vector<T>&>& function, PtrToArray<T>& oper);
46
47 ASUnary(const ASUnary<T>&);
48 virtual ~ASUnary();
49
51 virtual Scalar<T>* clone() const;
52
54 virtual T evaluate() const;
55
57 // If all component of the argument are constant, then evaluate
58 // and store the result as a constant.
59 static PtrToScalar<T> make(
60 const TFunction1<T, const std::vector<T>&>& function, PtrToArray<T>& oper);
61
63 virtual void print(std::ostream&, int precedence = 99) const;
64
65 private:
66 // Not implemented.
68 void operator=(const ASUnary&);
69
70 // The operation object.
72
73 // The operand.
75 };
76
77 // Implementation
78 // ----------------------------------------------------------------------
79
80 template <class T>
82 : Scalar<T>(rhs), fun(rhs.fun), opr(rhs.opr->clone()) {}
83
84 template <class T>
86 const TFunction1<T, const std::vector<T>&>& function, PtrToArray<T>& oper)
87 : fun(function), opr(oper) {}
88
89 template <class T>
91
92 template <class T>
93 inline Scalar<T>* ASUnary<T>::clone() const {
94 return new ASUnary<T>(*this);
95 }
96
97 template <class T>
98 inline T ASUnary<T>::evaluate() const {
99 errno = 0;
100 const std::vector<T> arg = opr->evaluate();
101 T result = (*fun.function)(arg);
102
103 // Test for run-time evaluation errors.
104 switch (errno) {
105 case EDOM:
106 throw DomainError("ASUnary::evaluate()");
107
108 case ERANGE:
109 // Ignore underflow.
110 if (result == T(0)) return result;
111 throw OverflowError("ASUnary::evaluate()");
112
113 default:;
114 }
115
116 return result;
117 }
118
119 template <class T>
121 const TFunction1<T, const std::vector<T>&>& function, PtrToArray<T>& oper) {
122 // We must pick up the constant flag before the ownership of "oper"
123 // is transferred to "result".
124 bool isConst = oper->isConstant();
125 PtrToScalar<T> result = new ASUnary<T>(function, oper);
126
127 if (function.precedence != -2) {
128 // Not a random expression.
129 if (isConst) {
130 // Replace constant expression by its value.
131 result = new SConstant<T>(result->evaluate());
132 }
133 }
134
135 // Other expression.
136 return result.release();
137 }
138
139 template <class T>
140 inline void ASUnary<T>::print(std::ostream& os, int precedence) const {
141 if (fun.precedence >= 0) {
142 if (fun.precedence <= precedence) os << "(";
143 os << fun.name;
144 opr->print(os, fun.precedence);
145 if (fun.precedence <= precedence) os << ")";
146 } else {
147 os << fun.name << "(";
148 opr->print(os, 0);
149 os << ")";
150 }
151 }
152
153} // namespace Expressions
154
155#endif // OPAL_ASUnary_HH
double T
Definition OPALTypes.h:8
Domain error exception.
Definition DomainError.h:32
A scalar expression with one array operand.
Definition ASUnary.h:40
virtual ~ASUnary()
Definition ASUnary.h:90
virtual void print(std::ostream &, int precedence=99) const
Print expression.
Definition ASUnary.h:140
static PtrToScalar< T > make(const TFunction1< T, const std::vector< T > & > &function, PtrToArray< T > &oper)
Make new expression.
Definition ASUnary.h:120
virtual T evaluate() const
Evaluate expression.
Definition ASUnary.h:98
void operator=(const ASUnary &)
PtrToArray< T > opr
Definition ASUnary.h:74
virtual Scalar< T > * clone() const
Make clone.
Definition ASUnary.h:93
const TFunction1< T, const std::vector< T > & > & fun
Definition ASUnary.h:71
A pointer to an array expression.
A pointer to a scalar expression.
A scalar constant expression.
Definition SConstant.h:35
Overflow exception.
Representation objects and parsers for attribute expressions.
A function of one U, returning a T.
Definition TFunction1.h:31