OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
AUnary.h
Go to the documentation of this file.
1#ifndef OPAL_AUnary_HH
2#define OPAL_AUnary_HH
3
4// ------------------------------------------------------------------------
5// $RCSfile: AUnary.h,v $
6// ------------------------------------------------------------------------
7// $Revision: 1.2 $
8// ------------------------------------------------------------------------
9// Copyright: see Copyright.readme
10// ------------------------------------------------------------------------
11//
12// Template class: AUnary<T,U>
13//
14// ------------------------------------------------------------------------
15//
16// $Date: 2002/01/17 22:18:36 $
17// $Author: jsberg $
18//
19// ------------------------------------------------------------------------
20
21#include <cerrno>
22#include <iostream>
23#include <vector>
24#include "Expressions/AList.h"
28
29namespace Expressions {
30
31 // Template Class AUnary
32 // ----------------------------------------------------------------------
34 // This expression first computes its array operand, and then applies
35 // the scalar function to each component and stores the results in the
36 // result vector.
37
38 template <class T, class U>
39 class AUnary : public AList<T> {
40 public:
42 // Use scalar function of one operand and an array.
43 AUnary(const TFunction1<T, U>& function, PtrToArray<U> operand);
44
45 AUnary(const AUnary<T, U>&);
46 virtual ~AUnary();
47
49 virtual OArray<T>* clone() const;
50
52 virtual std::vector<T> evaluate() const;
53
55 virtual void print(std::ostream&, int precedence = 99) const;
56
57 private:
58 // Not implemented.
60 void operator=(const AUnary&);
61
62 // The operation object.
64
65 // The operand.
67 };
68
69 // Implementation
70 // ----------------------------------------------------------------------
71
72 template <class T, class U>
74 : AList<T>(), fun(rhs.fun), opr(rhs.opr->clone()) {}
75
76 template <class T, class U>
78 : AList<T>(), fun(function), opr(oper) {}
79
80 template <class T, class U>
82
83 template <class T, class U>
85 return new AUnary<T, U>(*this);
86 }
87
88 template <class T, class U>
89 inline std::vector<T> AUnary<T, U>::evaluate() const {
90 errno = 0;
91 std::vector<U> arg = opr->evaluate();
92 std::vector<T> result;
93
94 for (typename std::vector<U>::size_type i = 0; i < arg.size(); ++i) {
95 result.push_back((*fun.function)(arg[i]));
96 }
97
98 // Test for run-time evaluation errors.
99 switch (errno) {
100 case EDOM:
101 throw DomainError("AUnary::evaluate()");
102
103 case ERANGE:
104 throw OverflowError("AUnary::evaluate()");
105
106 default:;
107 }
108
109 return result;
110 }
111
112 template <class T, class U>
113 inline void AUnary<T, U>::print(std::ostream& os, int precedence) const {
114 if (fun.precedence >= 0) {
115 if (fun.precedence <= precedence) os << "(";
116 os << fun.name;
117 opr->print(os, fun.precedence);
118 if (fun.precedence <= precedence) os << ")";
119 } else {
120 os << fun.name << "(";
121 opr->print(os, 0);
122 os << ")";
123 }
124 }
125
126} // namespace Expressions
127
128#endif // OPAL_AUnary_HH
double T
Definition OPALTypes.h:8
Domain error exception.
Definition DomainError.h:32
An array expression defined by a list of scalar expressions.
Definition AList.h:35
An array expression with one array operand.
Definition AUnary.h:39
virtual void print(std::ostream &, int precedence=99) const
Print expression.
Definition AUnary.h:113
PtrToArray< U > opr
Definition AUnary.h:66
virtual ~AUnary()
Definition AUnary.h:81
const TFunction1< T, U > & fun
Definition AUnary.h:63
void operator=(const AUnary &)
virtual std::vector< T > evaluate() const
Evaluate.
Definition AUnary.h:89
virtual OArray< T > * clone() const
Make clone.
Definition AUnary.h:84
A pointer to an array expression.
Overflow exception.
Representation objects and parsers for attribute expressions.
A function of one U, returning a T.
Definition TFunction1.h:31