Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
ArrayExpressionImpl.cc
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/* ArrayExpressionImpl.cc (C) 2000-2005 */
9/* */
10/* Expression handling an array. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/ArcanePrecomp.h"
15
16#include "arcane/datatype/ArrayVariant.h"
17
18#include "arcane/expr/ArrayExpressionImpl.h"
19#include "arcane/expr/OperatorMng.h"
20#include "arcane/expr/BadOperationException.h"
21
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27namespace Arcane
28{
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
37{
38 public:
39
40 virtual ~ArrayOperator() {}
41
42 public:
43
44 virtual void assign(ExpressionResult* res, ArrayVariant* var) = 0;
45 virtual void evaluate(ExpressionResult* res, ArrayVariant* var) = 0;
46};
47
48/*---------------------------------------------------------------------------*/
49/*---------------------------------------------------------------------------*/
50
51template <class T>
53: public ArrayOperator
54{
55 public:
56
57 virtual void assign(ExpressionResult* res, ArrayVariant* var)
58 {
59 // Note that the size of the variable may be larger
60 // than that of the result following the retained indices (cf WhereExpression)
61 Integer size = res->size();
62
63 // recuperation des valeurs du resultat et de la variable
64 ArrayView<T> res_val;
65 res->data()->value(res_val);
66 ExpressionResult var_res(var);
67 ArrayView<T> var_val;
68 var_res.data()->value(var_val);
69 IntegerConstArrayView res_indices = res->indices();
70
71 for (Integer i = 0; i < size; ++i)
72 var_val[res_indices[i]] = res_val[i];
73 }
74
75 virtual void evaluate(ExpressionResult* res, ArrayVariant* var)
76 {
77 // Note that the size of the variable may be larger
78 // than that of the result following the retained indices (cf WhereExpression)
79 Integer size = res->size();
80 Integer vsize = var->size();
81 cerr << "** SIZE res=" << size << " var=" << vsize << " res=" << res << '\n';
82 Integer max_size = math::min(size, vsize);
83 //if (size > var->size())
84 //throw BadOperandException("VariableOperatorT::evaluate");
85
86 // allocation du résultat en fonction du type de la variable
87 VariantBase::eType type = var->type();
88 res->allocate(type);
89
90 // recuperation des valeurs des operandes
91 ArrayView<T> res_val;
92 res->data()->value(res_val);
93 ExpressionResult var_res(var);
94 ArrayView<T> var_val;
95 var_res.data()->value(var_val);
96 IntegerConstArrayView res_indices = res->indices();
97
98 for (Integer i = 0; i < max_size; ++i)
99 res_val[i] = var_val[res_indices[i]];
100 }
101};
102
103/*---------------------------------------------------------------------------*/
104/*---------------------------------------------------------------------------*/
105
106ArrayExpressionImpl::
107ArrayExpressionImpl(ArrayVariant* variant)
109, m_variant(variant)
110, m_op(0)
111{
112 switch (variant->type()) {
113 case VariantBase::TReal:
114 m_op = new ArrayOperatorT<Real>();
115 break;
116 default:
117 throw BadOperationException("ArrayExpressionImpl::ArrayExpressionImpl",
118 "bad type", variant->type());
119 }
120}
121
122/*---------------------------------------------------------------------------*/
123/*---------------------------------------------------------------------------*/
124
125ArrayExpressionImpl::
126~ArrayExpressionImpl()
127{
128 //TODO The delete crashes...
129 //delete m_variant;
130 delete m_op;
131}
132
133/*---------------------------------------------------------------------------*/
134/*---------------------------------------------------------------------------*/
135
136void ArrayExpressionImpl::
137assign(IExpressionImpl* expr)
138{
139 ExpressionResult result(m_variant);
140 expr->apply(&result);
141}
142
143/*---------------------------------------------------------------------------*/
144/*---------------------------------------------------------------------------*/
145
146void ArrayExpressionImpl::
147assign(IExpressionImpl* expr, ConstArrayView<Integer> indices)
148{
149 ExpressionResult result(indices);
150 result.allocate(m_variant->type());
151 expr->apply(&result);
152 m_op->assign(&result, m_variant);
153}
154
155/*---------------------------------------------------------------------------*/
156/*---------------------------------------------------------------------------*/
157
158void ArrayExpressionImpl::
159apply(ExpressionResult* result)
160{
161 m_op->evaluate(result, m_variant);
162}
163
164/*---------------------------------------------------------------------------*/
165/*---------------------------------------------------------------------------*/
166
168vectorSize() const
169{
170 return m_variant->size();
171}
172
173/*---------------------------------------------------------------------------*/
174/*---------------------------------------------------------------------------*/
175
176} // namespace Arcane
177
178/*---------------------------------------------------------------------------*/
179/*---------------------------------------------------------------------------*/
Various mathematical functions.
virtual Integer vectorSize() const
Number of elements in the vector.
Generic binary operator for expressions.
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.
Constant view of an array of type T.
Base class for the expression implementation.
Polymorphic base type of an expression.
Interface for the different implementations of an expression.
__host__ __device__ Real2 min(Real2 a, Real2 b)
Returns the minimum of two Real2.
Definition MathUtils.h:346
-- 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