Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
WhereExpressionImpl.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/* WhereExpressionImpl.h (C) 2000-2004 */
9/* */
10/* Implementation of a conditional expression. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_EXPR_WHEREEXPRESSIONIMPL_H
13#define ARCANE_EXPR_WHEREEXPRESSIONIMPL_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/expr/BadOperandException.h"
18#include "arcane/expr/ExpressionImpl.h"
19#include "arcane/expr/ExpressionResult.h"
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Arcane
25{
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30class WhereOperator;
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
38class ARCANE_EXPR_EXPORT WhereExpressionImpl
39: public ExpressionImpl
40{
41 public:
42
43 WhereExpressionImpl(IExpressionImpl* test,
44 IExpressionImpl* iftrue,
45 IExpressionImpl* iffalse);
46
47 public:
48
49 virtual void assign(IExpressionImpl*) {}
50 virtual void assign(IExpressionImpl*, IntegerConstArrayView) {}
51 virtual void apply(ExpressionResult* result);
52 virtual Integer vectorSize() const { return 0; }
53
54 private:
55
59};
60
61/*---------------------------------------------------------------------------*/
62/*---------------------------------------------------------------------------*/
63
68{
69 public:
70
71 virtual ~WhereOperator() {}
72 virtual void evaluate(ExpressionResult* res,
73 ArrayVariant* test,
74 ArrayVariant* iftrue,
75 ArrayVariant* iffalse) = 0;
76};
77
78/*---------------------------------------------------------------------------*/
79/*---------------------------------------------------------------------------*/
80
81template <class T>
83: public WhereOperator
84{
85 public:
86
87 virtual ~WhereOperatorT() {}
88 virtual void evaluate(ExpressionResult* res,
89 ArrayVariant* test,
90 ArrayVariant* iftrue,
91 ArrayVariant* iffalse)
92 {
93 // verification of operation validity
94 if (test->type() != ArrayVariant::TBool)
95 throw BadOperandException("WhereOperatorT::evaluate");
96
97 Integer size = res->size();
98 if (size != test->size())
99 throw BadOperandException("WhereOperatorT::evaluate");
100
101 if (iftrue->type() || iffalse->type())
102 throw BadOperandException("WhereOperatorT::evaluate");
103
104 // allocation of the result based on the type of the if
105 res->allocate(iftrue->type());
106
107 // retrieval of operand values
108 ArrayView<bool> test_val;
109 test->value(test_val);
110 ArrayView<T> res_val;
111 res->data()->value(res_val);
112 ArrayView<T> iftrue_val;
113 iftrue->value(iftrue_val);
114 ArrayView<T> iffalse_val;
115 iffalse->value(iffalse_val);
116
117 Integer false_i = 0;
118 Integer true_i = 0;
119 for (Integer i = 0; i < size; ++i)
120 test_val[i] ? res_val[i] = iftrue_val[true_i++]
121 : res_val[i] = iffalse_val[false_i++];
122 }
123};
124
125/*---------------------------------------------------------------------------*/
126/*---------------------------------------------------------------------------*/
127
128} // namespace Arcane
129
130/*---------------------------------------------------------------------------*/
131/*---------------------------------------------------------------------------*/
132
133#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.
Polymorphic base type of an expression.
Reference to an expression.
Definition Expression.h:44
Interface for the different implementations of an expression.
virtual Integer vectorSize() const
Number of elements in the vector.
Expression m_iffalse
Expression evaluated when the test is negative.
Expression m_iftrue
Expression evaluated when the test is positive.
Expression m_test
Test expression.
Generic operator for conditional expressions.
-- 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