OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
ABinary.h
Go to the documentation of this file.
1 #ifndef OPAL_ABinary_HH
2 #define OPAL_ABinary_HH
3 
4 // ------------------------------------------------------------------------
5 // $RCSfile: ABinary.h,v $
6 // ------------------------------------------------------------------------
7 // $Revision: 1.2 $
8 // ------------------------------------------------------------------------
9 // Copyright: see Copyright.readme
10 // ------------------------------------------------------------------------
11 //
12 // Template class: ABinary<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/TFunction2.h"
23 #include "Utilities/DomainError.h"
26 #include <cerrno>
27 #include <iosfwd>
28 #include <vector>
29 
30 
31 namespace Expressions {
32 
33  // Template class ABinary
34  // ----------------------------------------------------------------------
36  // The expression first evaluates the two array operands, and then
37  // loops in parallel over the results and applies the given function
38  // to each pair of components.
39 
40  template <class T, class U>
41  class ABinary: public AList<T> {
42 
43  public:
44 
46  // Use scalar function with two operands and two array expressions.
47  ABinary(const TFunction2<T, U> &function,
48  PtrToArray<U> left, PtrToArray<U> right);
49 
50  ABinary(const ABinary<T, U> &);
51  virtual ~ABinary();
52 
54  virtual OArray<T> *clone() const;
55 
57  virtual std::vector<T> evaluate() const;
58 
60  virtual void print(std::ostream &str, int precedence = 99) const;
61 
62  private:
63 
64  // Not implemented.
65  ABinary();
66  void operator=(const ABinary &);
67 
68  // The operation object.
70 
71  // The two operands.
74  };
75 
76 
77  // Implementation
78  // ----------------------------------------------------------------------
79 
80  template <class T, class U> inline
82  AList<T>(), fun(rhs.fun), lft(rhs.lft->clone()), rgt(rhs.rgt->clone())
83  {}
84 
85 
86  template <class T, class U> inline
88  PtrToArray<U> left, PtrToArray<U> right):
89  AList<T>(), fun(function), lft(left), rgt(right)
90  {}
91 
92 
93  template <class T, class U> inline
95  {}
96 
97 
98  template <class T, class U> inline
100  return new ABinary<T, U>(*this);
101  }
102 
103 
104  template <class T, class U> inline
105  std::vector<T> ABinary<T, U>::evaluate() const {
106  errno = 0;
107  std::vector<U> op1 = lft->evaluate();
108  std::vector<U> op2 = rgt->evaluate();
109  std::vector<T> result;
110 
111  if(op1.size() == op2.size()) {
112  for(typename std::vector<U>::size_type i = 0; i < op1.size(); ++i) {
113  result.push_back((*fun.function)(op1[i], op2[i]));
114  }
115  } else if(op1.size() == 1) {
116  for(typename std::vector<U>::size_type i = 0; i < op2.size(); ++i) {
117  result.push_back((*fun.function)(op1[0], op2[i]));
118  }
119  } else if(op2.size() == 1) {
120  for(typename std::vector<U>::size_type i = 0; i < op1.size(); ++i) {
121  result.push_back((*fun.function)(op1[i], op2[0]));
122  }
123  } else {
124  throw OpalException("ABinary::evaluate()",
125  "Inconsistent array dimensions.");
126  }
127 
128  // Test for run-time evaluation errors.
129  switch(errno) {
130 
131  case EDOM:
132  throw DomainError("ABinary::evaluate()");
133 
134  case ERANGE:
135  throw OverflowError("ABinary::evaluate()");
136 
137  default:
138  return result;
139  }
140  }
141 
142 
143  template <class T, class U> inline
144  void ABinary<T, U>::print(std::ostream &os, int precedence) const {
145  if(fun.precedence >= 0) {
146  // Binary operation.
147  if(fun.precedence <= precedence) os << "(";
148  lft->print(os, fun.precedence - 1);
149  os << fun.name;
150  rgt->print(os, fun.precedence);
151  if(fun.precedence <= precedence) os << ")";
152  } else {
153  // Function.
154  os << fun.name << '(';
155  lft->print(os, 0);
156  os << ',';
157  rgt->print(os, 0);
158  os << ')';
159  }
160  }
161 
162 }
163 
164 #endif // OPAL_ABinary_HH
void operator=(const ABinary &)
virtual void print(std::ostream &str, int precedence=99) const
Print expression.
Definition: ABinary.h:144
Definition: rbendmap.h:8
virtual std::vector< T > evaluate() const
Evaluate.
Definition: ABinary.h:105
virtual ~ABinary()
Definition: ABinary.h:94
The base class for all OPAL exceptions.
Definition: OpalException.h:28
A function of two U&#39;s returning a T.
Definition: TFunction2.h:31
PtrToArray< U > lft
Definition: ABinary.h:72
virtual OArray< T > * clone() const
Make clone.
Definition: ABinary.h:99
An array expression with two array operands.
Definition: ABinary.h:41
Overflow exception.
Definition: OverflowError.h:32
const TFunction2< T, U > & fun
Definition: ABinary.h:69
Domain error exception.
Definition: DomainError.h:32
PtrToArray< U > rgt
Definition: ABinary.h:73
An array expression.
Definition: Expressions.h:149
An array expression defined by a list of scalar expressions.
Definition: AList.h:36