Arcane  v3.16.0.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
BinaryExpressionImpl.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2022 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 d'une expression binaire. */
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
25ARCANE_BEGIN_NAMESPACE
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30class BinaryOperator;
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
38class BinaryExpressionImpl
39: public ExpressionImpl
40{
41 public:
42 enum eOperationType
43 {
44 Add = 0,
45 Substract,
46 Multiply,
47 Divide,
48 Minimum,
49 Maximum,
50 Pow,
51 LessThan,
52 GreaterThan,
53 LessOrEqualThan,
54 GreaterOrEqualThan,
55 Or,
56 And,
57 Equal,
58 NbOperationType
59 };
60
61 public:
62 BinaryExpressionImpl (IExpressionImpl* first,
63 IExpressionImpl* second,
64 eOperationType operation);
65
66 public:
67 virtual void assign(IExpressionImpl*) {}
68 virtual void assign(IExpressionImpl*, IntegerConstArrayView) {}
69 virtual void apply(ExpressionResult* result);
70 virtual Integer vectorSize() const { return 0; }
71 String operationName() const { return operationName(m_operation); }
72 static String operationName(eOperationType type);
73
74 private:
75 Expression m_first;
76 Expression m_second;
77 eOperationType m_operation;
78};
79
80/*---------------------------------------------------------------------------*/
81/*---------------------------------------------------------------------------*/
86{
87 public:
88 virtual ~BinaryOperator() {}
89 virtual void evaluate(ExpressionResult* res,
90 ArrayVariant* a,
91 ArrayVariant* b)=0;
92};
93
94/*---------------------------------------------------------------------------*/
95/*---------------------------------------------------------------------------*/
96
97template<class T> class DefaultBinaryOperator
98: public BinaryOperator
99{
100 public:
101 virtual void evaluate(ArrayView<T> res,
102 ArrayView<T> a,
103 ArrayView<T> b)=0;
104
105 virtual void evaluate(ExpressionResult* res,
106 ArrayVariant* a,
107 ArrayVariant* b)
108 {
109 // verification de la validite de l'operation
110 if (a->type() != b->type())
111 throw BadOperandException("DefaultBinaryOperator::evaluate");
112
113 Integer size = res->size();
114 if (size != a->size() || size != b->size())
115 throw BadOperandException("DefaultBinaryOperator::evaluate");
116
117 // allocation du résultat en fonction du type de a
118 res->allocate(a->type());
119
120 // recuperation des valeurs des operandes
121 ArrayView<T> res_val;
122 res->data()->value(res_val);
123 ArrayView<T> a_val;
124 a->value(a_val);
125 ArrayView<T> b_val;
126 b->value(b_val);
127
128 // evaluation des tableaux
129 evaluate(res_val, a_val, b_val);
130 }
131};
132
133/*---------------------------------------------------------------------------*/
134/*---------------------------------------------------------------------------*/
135
136#define DEFAULT_BINARY_OP(classname,expression) \
137template<class T> \
138class classname : public DefaultBinaryOperator<T> \
139{ \
140 public:\
141 virtual void evaluate(ExpressionResult* res, \
142 ArrayVariant* a, \
143 ArrayVariant* b)\
144 { DefaultBinaryOperator<T>::evaluate(res,a,b); } \
145 virtual void evaluate(ArrayView<T> res, \
146 ArrayView<T> a, \
147 ArrayView<T> b) \
148 { \
149 Integer size=res.size(); \
150 for (Integer i=0 ; i<size ; ++i) \
151 expression; \
152 } \
153};
154
155/*---------------------------------------------------------------------------*/
156/*---------------------------------------------------------------------------*/
157
158DEFAULT_BINARY_OP(AddOperator,res[i]=a[i]+b[i])
159DEFAULT_BINARY_OP(SubstractOperator,res[i]=a[i]-b[i])
160DEFAULT_BINARY_OP(MultiplyOperator,res[i]=a[i]*b[i])
161DEFAULT_BINARY_OP(DivideOperator,res[i]=a[i]/b[i])
162DEFAULT_BINARY_OP(MinimumOperator,(a[i]<b[i])?res[i]=a[i]:res[i]=b[i])
163DEFAULT_BINARY_OP(MaximumOperator,(a[i]<b[i])?res[i]=b[i]:res[i]=a[i])
164DEFAULT_BINARY_OP(PowOperator,res[i]=pow(a[i],b[i]))
165
166/*---------------------------------------------------------------------------*/
167/*---------------------------------------------------------------------------*/
168
169template<class T>
170class BoolBinaryOperator
171: public BinaryOperator
172{
173 public:
174 virtual void evaluate(ArrayView<bool> res,
175 ArrayView<T> a,
176 ArrayView<T> b)=0;
177
178 virtual void evaluate(ExpressionResult* res,
179 ArrayVariant* a,
180 ArrayVariant* b)
181 {
182 // verification de la validite de l'operation
183 if (a->type() != b->type())
184 throw BadOperandException("BoolBinaryOperator::evaluate");
185
186 Integer size = res->size();
187 if (size != a->size() || size != b->size())
188 throw BadOperandException("BoolBinaryOperator::evaluate");
189
190 // allocation du résultat qui doit etre booléen
191 res->allocate(ArrayVariant::TBool);
192
193 // recuperation des valeurs des operandes
194 ArrayView<bool> res_val;
195 res->data()->value(res_val);
196 ArrayView<T> a_val;
197 a->value(a_val);
198 ArrayView<T> b_val;
199 b->value(b_val);
200
201 // evaluation des tableaux
202 evaluate(res_val, a_val, b_val);
203 }
204};
205
206/*---------------------------------------------------------------------------*/
207/*---------------------------------------------------------------------------*/
208
209#define BOOL_BINARY_OP(classname,expression) \
210template<class T> \
211class classname : public BoolBinaryOperator<T> \
212{ \
213 public:\
214 virtual void evaluate(ExpressionResult* res, \
215 ArrayVariant* a, \
216 ArrayVariant* b)\
217 { BoolBinaryOperator<T>::evaluate(res,a,b); }\
218 virtual void evaluate(ArrayView<bool> res, \
219 ArrayView<T> a, \
220 ArrayView<T> b) \
221 { \
222 Integer size=res.size(); \
223 for (Integer i=0 ; i<size ; ++i) \
224 expression; \
225 } \
226};
227
228/*---------------------------------------------------------------------------*/
229/*---------------------------------------------------------------------------*/
230
231BOOL_BINARY_OP(EQOperator,res[i]=(a[i]==b[i]))
232BOOL_BINARY_OP(LTOperator,res[i]=(a[i]<b[i]))
233BOOL_BINARY_OP(GTOperator,res[i]=(a[i]>b[i]))
234BOOL_BINARY_OP(LOETOperator,res[i]=(a[i]<=b[i]))
235BOOL_BINARY_OP(GOETOperator,res[i]=(a[i]>=b[i]))
236BOOL_BINARY_OP(AndOperator,res[i]=(a[i]&&b[i]))
237BOOL_BINARY_OP(OrOperator,res[i]=(a[i]||b[i]))
238
239/*---------------------------------------------------------------------------*/
240/*---------------------------------------------------------------------------*/
241
242ARCANE_END_NAMESPACE
243
244/*---------------------------------------------------------------------------*/
245/*---------------------------------------------------------------------------*/
246
247#endif
Type de base polymorphe pour les tableaux (dimension 1).
Vue modifiable d'un tableau d'un type T.
constexpr const_pointer data() const noexcept
Pointeur sur le début de la vue.
Exception sur les opérandes des opérations des expressions.
virtual Integer vectorSize() const
Nombre d'éléments du vecteur.
Operateur binaire generique pour les expressions.
Type de base polymorphe d'une expression.
Interface pour les différentes implémentations d'une expression.
Chaîne de caractères unicode.
Int32 Integer
Type représentant un entier.
ConstArrayView< Integer > IntegerConstArrayView
Equivalent C d'un tableau à une dimension d'entiers.
Definition UtilsTypes.h:573