OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
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 <cerrno>
22#include <iosfwd>
23#include <vector>
24#include "Expressions/AList.h"
29
30namespace Expressions {
31
32 // Template class ABinary
33 // ----------------------------------------------------------------------
35 // The expression first evaluates the two array operands, and then
36 // loops in parallel over the results and applies the given function
37 // to each pair of components.
38
39 template <class T, class U>
40 class ABinary : public AList<T> {
41 public:
43 // Use scalar function with two operands and two array expressions.
44 ABinary(const TFunction2<T, U>& function, PtrToArray<U> left, PtrToArray<U> right);
45
47 virtual ~ABinary();
48
50 virtual OArray<T>* clone() const;
51
53 virtual std::vector<T> evaluate() const;
54
56 virtual void print(std::ostream& str, int precedence = 99) const;
57
58 private:
59 // Not implemented.
61 void operator=(const ABinary&);
62
63 // The operation object.
65
66 // The two operands.
69 };
70
71 // Implementation
72 // ----------------------------------------------------------------------
73
74 template <class T, class U>
75 inline ABinary<T, U>::ABinary(const ABinary& rhs)
76 : AList<T>(), fun(rhs.fun), lft(rhs.lft->clone()), rgt(rhs.rgt->clone()) {}
77
78 template <class T, class U>
80 const TFunction2<T, U>& function, PtrToArray<U> left, PtrToArray<U> right)
81 : AList<T>(), fun(function), lft(left), rgt(right) {}
82
83 template <class T, class U>
85
86 template <class T, class U>
88 return new ABinary<T, U>(*this);
89 }
90
91 template <class T, class U>
92 inline std::vector<T> ABinary<T, U>::evaluate() const {
93 errno = 0;
94 std::vector<U> op1 = lft->evaluate();
95 std::vector<U> op2 = rgt->evaluate();
96 std::vector<T> result;
97
98 if (op1.size() == op2.size()) {
99 for (typename std::vector<U>::size_type i = 0; i < op1.size(); ++i) {
100 result.push_back((*fun.function)(op1[i], op2[i]));
101 }
102 } else if (op1.size() == 1) {
103 for (typename std::vector<U>::size_type i = 0; i < op2.size(); ++i) {
104 result.push_back((*fun.function)(op1[0], op2[i]));
105 }
106 } else if (op2.size() == 1) {
107 for (typename std::vector<U>::size_type i = 0; i < op1.size(); ++i) {
108 result.push_back((*fun.function)(op1[i], op2[0]));
109 }
110 } else {
111 throw OpalException("ABinary::evaluate()", "Inconsistent array dimensions.");
112 }
113
114 // Test for run-time evaluation errors.
115 switch (errno) {
116 case EDOM:
117 throw DomainError("ABinary::evaluate()");
118
119 case ERANGE:
120 throw OverflowError("ABinary::evaluate()");
121
122 default:
123 return result;
124 }
125 }
126
127 template <class T, class U>
128 inline void ABinary<T, U>::print(std::ostream& os, int precedence) const {
129 if (fun.precedence >= 0) {
130 // Binary operation.
131 if (fun.precedence <= precedence) os << "(";
132 lft->print(os, fun.precedence - 1);
133 os << fun.name;
134 rgt->print(os, fun.precedence);
135 if (fun.precedence <= precedence) os << ")";
136 } else {
137 // Function.
138 os << fun.name << '(';
139 lft->print(os, 0);
140 os << ',';
141 rgt->print(os, 0);
142 os << ')';
143 }
144 }
145
146} // namespace Expressions
147
148#endif // OPAL_ABinary_HH
double T
Definition OPALTypes.h:8
Domain error exception.
Definition DomainError.h:32
An array expression with two array operands.
Definition ABinary.h:40
virtual OArray< T > * clone() const
Make clone.
Definition ABinary.h:87
PtrToArray< U > rgt
Definition ABinary.h:68
void operator=(const ABinary &)
virtual void print(std::ostream &str, int precedence=99) const
Print expression.
Definition ABinary.h:128
PtrToArray< U > lft
Definition ABinary.h:67
const TFunction2< T, U > & fun
Definition ABinary.h:64
ABinary(const ABinary< T, U > &)
virtual ~ABinary()
Definition ABinary.h:84
virtual std::vector< T > evaluate() const
Evaluate.
Definition ABinary.h:92
An array expression defined by a list of scalar expressions.
Definition AList.h:35
A pointer to an array expression.
Overflow exception.
Representation objects and parsers for attribute expressions.
A function of two U's returning a T.
Definition TFunction2.h:31