Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
BinaryExpressionImpl.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* BinaryExpressionImpl.h (C) 2000-2005 */
9/* */
10/* Implementation of a binary expression. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_EXPR_BINARYEXPRESSIONIMPL_H
13#define ARCANE_EXPR_BINARYEXPRESSIONIMPL_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/expr/ExpressionImpl.h"
18#include "arcane/expr/Expression.h"
19#include "arcane/expr/ExpressionResult.h"
20#include "arcane/expr/BadOperandException.h"
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31class BinaryOperator;
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
39class BinaryExpressionImpl
40: public ExpressionImpl
41{
42 public:
43
44 enum eOperationType
45 {
46 Add = 0,
47 Substract,
48 Multiply,
49 Divide,
50 Minimum,
51 Maximum,
52 Pow,
53 LessThan,
54 GreaterThan,
55 LessOrEqualThan,
56 GreaterOrEqualThan,
57 Or,
58 And,
59 Equal,
60 NbOperationType
61 };
62
63 public:
64
65 BinaryExpressionImpl(IExpressionImpl* first,
66 IExpressionImpl* second,
67 eOperationType operation);
68
69 public:
70
71 virtual void assign(IExpressionImpl*) {}
72 virtual void assign(IExpressionImpl*, IntegerConstArrayView) {}
73 virtual void apply(ExpressionResult* result);
74 virtual Integer vectorSize() const { return 0; }
75 String operationName() const { return operationName(m_operation); }
76 static String operationName(eOperationType type);
77
78 private:
79
80 Expression m_first;
81 Expression m_second;
82 eOperationType m_operation;
83};
84
85/*---------------------------------------------------------------------------*/
86/*---------------------------------------------------------------------------*/
87
92{
93 public:
94
95 virtual ~BinaryOperator() {}
96 virtual void evaluate(ExpressionResult* res,
97 ArrayVariant* a,
98 ArrayVariant* b) = 0;
99};
100
101/*---------------------------------------------------------------------------*/
102/*---------------------------------------------------------------------------*/
103
104template <class T> class DefaultBinaryOperator
105: public BinaryOperator
106{
107 public:
108
109 virtual void evaluate(ArrayView<T> res,
110 ArrayView<T> a,
111 ArrayView<T> b) = 0;
112
113 virtual void evaluate(ExpressionResult* res,
114 ArrayVariant* a,
115 ArrayVariant* b)
116 {
117 // Check operation validity
118 if (a->type() != b->type())
119 throw BadOperandException("DefaultBinaryOperator::evaluate");
120
121 Integer size = res->size();
122 if (size != a->size() || size != b->size())
123 throw BadOperandException("DefaultBinaryOperator::evaluate");
124
125 // Allocate the result based on the type of a
126 res->allocate(a->type());
127
128 // Retrieve operand values
129 ArrayView<T> res_val;
130 res->data()->value(res_val);
131 ArrayView<T> a_val;
132 a->value(a_val);
133 ArrayView<T> b_val;
134 b->value(b_val);
135
136 // Evaluate arrays
137 evaluate(res_val, a_val, b_val);
138 }
139};
140
141/*---------------------------------------------------------------------------*/
142/*---------------------------------------------------------------------------*/
143
144#define DEFAULT_BINARY_OP(classname, expression) \
145 template <class T> \
146 class classname : public DefaultBinaryOperator<T> \
147 { \
148 public: \
149\
150 virtual void evaluate(ExpressionResult* res, \
151 ArrayVariant* a, \
152 ArrayVariant* b) \
153 { \
154 DefaultBinaryOperator<T>::evaluate(res, a, b); \
155 } \
156 virtual void evaluate(ArrayView<T> res, \
157 ArrayView<T> a, \
158 ArrayView<T> b) \
159 { \
160 Integer size = res.size(); \
161 for (Integer i = 0; i < size; ++i) \
162 expression; \
163 } \
164 };
165
166/*---------------------------------------------------------------------------*/
167/*---------------------------------------------------------------------------*/
168
169DEFAULT_BINARY_OP(AddOperator, res[i] = a[i] + b[i])
170DEFAULT_BINARY_OP(SubstractOperator, res[i] = a[i] - b[i])
171DEFAULT_BINARY_OP(MultiplyOperator, res[i] = a[i] * b[i])
172DEFAULT_BINARY_OP(DivideOperator, res[i] = a[i] / b[i])
173DEFAULT_BINARY_OP(MinimumOperator, (a[i] < b[i]) ? res[i] = a[i] : res[i] = b[i])
174DEFAULT_BINARY_OP(MaximumOperator, (a[i] < b[i]) ? res[i] = b[i] : res[i] = a[i])
175DEFAULT_BINARY_OP(PowOperator, res[i] = pow(a[i], b[i]))
176
177/*---------------------------------------------------------------------------*/
178/*---------------------------------------------------------------------------*/
179
180template <class T>
181class BoolBinaryOperator
182: public BinaryOperator
183{
184 public:
185
186 virtual void evaluate(ArrayView<bool> res,
187 ArrayView<T> a,
188 ArrayView<T> b) = 0;
189
190 virtual void evaluate(ExpressionResult* res,
191 ArrayVariant* a,
192 ArrayVariant* b)
193 {
194 // Check operation validity
195 if (a->type() != b->type())
196 throw BadOperandException("BoolBinaryOperator::evaluate");
197
198 Integer size = res->size();
199 if (size != a->size() || size != b->size())
200 throw BadOperandException("BoolBinaryOperator::evaluate");
201
202 // Allocate the result, which must be boolean
203 res->allocate(ArrayVariant::TBool);
204
205 // Retrieve operand values
206 ArrayView<bool> res_val;
207 res->data()->value(res_val);
208 ArrayView<T> a_val;
209 a->value(a_val);
210 ArrayView<T> b_val;
211 b->value(b_val);
212
213 // Evaluate arrays
214 evaluate(res_val, a_val, b_val);
215 }
216};
217
218/*---------------------------------------------------------------------------*/
219/*---------------------------------------------------------------------------*/
220
221#define BOOL_BINARY_OP(classname, expression) \
222 template <class T> \
223 class classname : public BoolBinaryOperator<T> \
224 { \
225 public: \
226\
227 virtual void evaluate(ExpressionResult* res, \
228 ArrayVariant* a, \
229 ArrayVariant* b) \
230 { \
231 BoolBinaryOperator<T>::evaluate(res, a, b); \
232 } \
233 virtual void evaluate(ArrayView<bool> res, \
234 ArrayView<T> a, \
235 ArrayView<T> b) \
236 { \
237 Integer size = res.size(); \
238 for (Integer i = 0; i < size; ++i) \
239 expression; \
240 } \
241 };
242
243/*---------------------------------------------------------------------------*/
244/*---------------------------------------------------------------------------*/
245
246BOOL_BINARY_OP(EQOperator, res[i] = (a[i] == b[i]))
247BOOL_BINARY_OP(LTOperator, res[i] = (a[i] < b[i]))
248BOOL_BINARY_OP(GTOperator, res[i] = (a[i] > b[i]))
249BOOL_BINARY_OP(LOETOperator, res[i] = (a[i] <= b[i]))
250BOOL_BINARY_OP(GOETOperator, res[i] = (a[i] >= b[i]))
251BOOL_BINARY_OP(AndOperator, res[i] = (a[i] && b[i]))
252BOOL_BINARY_OP(OrOperator, res[i] = (a[i] || b[i]))
253
254/*---------------------------------------------------------------------------*/
255/*---------------------------------------------------------------------------*/
256
257} // namespace Arcane
258
259/*---------------------------------------------------------------------------*/
260/*---------------------------------------------------------------------------*/
261
262#endif
Polymorphic base type for arrays (dimension 1).
Modifiable view of an array of type T.
constexpr const_pointer data() const noexcept
Pointer to the start of the view.
Exception for operands in expression operations.
virtual Integer vectorSize() const
Number of elements in the vector.
Generic binary operator for expressions.
Polymorphic base type of an expression.
Interface for the different implementations of an expression.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
ConstArrayView< Integer > IntegerConstArrayView
C equivalent of a 1D array of integers.
Definition UtilsTypes.h:486