OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
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 "Expressions/AList.h"
22 #include "Expressions/TFunction1.h"
23 #include "Utilities/DomainError.h"
25 #include <cerrno>
26 #include <iostream>
27 #include <vector>
28 
29 
30 namespace Expressions {
31 
32  // Template Class AUnary
33  // ----------------------------------------------------------------------
35  // This expression first computes its array operand, and then applies
36  // the scalar function to each component and stores the results in the
37  // result vector.
38 
39  template <class T, class U>
40  class AUnary: public AList<T> {
41 
42  public:
43 
45  // Use scalar function of one operand and an array.
46  AUnary(const TFunction1<T, U> &function, PtrToArray<U> operand);
47 
48  AUnary(const AUnary<T, U> &);
49  virtual ~AUnary();
50 
52  virtual OArray<T> *clone() const;
53 
55  virtual std::vector<T> evaluate() const;
56 
58  virtual void print(std::ostream &, int precedence = 99) const;
59 
60  private:
61 
62  // Not implemented.
63  AUnary();
64  void operator=(const AUnary &);
65 
66  // The operation object.
68 
69  // The operand.
71  };
72 
73 
74  // Implementation
75  // ----------------------------------------------------------------------
76 
77  template <class T, class U> inline
79  AList<T>(), fun(rhs.fun), opr(rhs.opr->clone())
80  {}
81 
82 
83  template <class T, class U> inline
85  AList<T>(), fun(function), opr(oper)
86  {}
87 
88 
89  template <class T, class U> inline
91  {}
92 
93 
94  template <class T, class U> inline
96  return new AUnary<T, U>(*this);
97  }
98 
99 
100  template <class T, class U> inline
101  std::vector<T> AUnary<T, U>::evaluate() const {
102  errno = 0;
103  std::vector<U> arg = opr->evaluate();
104  std::vector<T> result;
105 
106  for(typename std::vector<U>::size_type i = 0; i < arg.size(); ++i) {
107  result.push_back((*fun.function)(arg[i]));
108  }
109 
110  // Test for run-time evaluation errors.
111  switch(errno) {
112 
113  case EDOM:
114  throw DomainError("AUnary::evaluate()");
115 
116  case ERANGE:
117  throw OverflowError("AUnary::evaluate()");
118 
119  default:
120  ;
121  }
122 
123  return result;
124  }
125 
126 
127  template <class T, class U> inline
128  void AUnary<T, U>::print(std::ostream &os, int precedence) const {
129  if(fun.precedence >= 0) {
130  if(fun.precedence <= precedence) os << "(";
131  os << fun.name;
132  opr->print(os, fun.precedence);
133  if(fun.precedence <= precedence) os << ")";
134  } else {
135  os << fun.name << "(";
136  opr->print(os, 0);
137  os << ")";
138  }
139  }
140 
141 }
142 
143 #endif // OPAL_AUnary_HH
Definition: rbendmap.h:8
PtrToArray< U > opr
Definition: AUnary.h:70
A function of one U, returning a T.
Definition: TFunction1.h:31
arg(a))
Overflow exception.
Definition: OverflowError.h:32
virtual ~AUnary()
Definition: AUnary.h:90
const TFunction1< T, U > & fun
Definition: AUnary.h:67
Domain error exception.
Definition: DomainError.h:32
An array expression with one array operand.
Definition: AUnary.h:40
virtual void print(std::ostream &, int precedence=99) const
Print expression.
Definition: AUnary.h:128
virtual std::vector< T > evaluate() const
Evaluate.
Definition: AUnary.h:101
void operator=(const AUnary &)
boost::variant< nil, bool, unsigned int, double, identifier, boost::recursive_wrapper< unary >, boost::recursive_wrapper< function_call >, boost::recursive_wrapper< expression > > operand
Definition: ast.hpp:53
An array expression.
Definition: Expressions.h:149
virtual OArray< T > * clone() const
Make clone.
Definition: AUnary.h:95
An array expression defined by a list of scalar expressions.
Definition: AList.h:36