Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
ItemLoop.h
Go to the documentation of this file.
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/* ItemLoop.h (C) 2000-2025 */
9/* */
10/* Utility classes for managing loops over entities. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CORE_ITEMLOOP_H
13#define ARCANE_CORE_ITEMLOOP_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22/*!
23 * \file ItemLoop.h
24 *
25 * \brief Types and macros for managing loops over mesh entities.
26 */
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31namespace Arcane
32{
33
34/*!
35 * \brief Namespace containing various classes managing loops
36 * over entities.
37 */
38namespace Loop
39{
40
41 /*!
42 * \internal
43 * \brief Entity loop functor that allows for the removal of
44 * indirections if the local indices of a view are consecutive.
45 */
46 template <typename IterType, typename Lambda> inline void
47 _InternalSimpleItemLoop(ItemVectorView view, const Lambda& lambda)
48 {
49 if (view.size() == 0)
50 return;
51 bool is_contigous = view.indexes().isContigous();
52 //is_contigous = false;
53 if (is_contigous) {
54 Int32 x0 = view.localIds()[0];
55 // Assuming iterations are independent
56 ARCANE_PRAGMA_IVDEP
57 for (Int32 i = 0, n = view.size(); i < n; ++i)
58 lambda(IterType(x0 + i));
59 }
60 else {
61 ENUMERATE_ITEM (iitem, view) {
62 lambda(IterType(iitem.localId()));
63 }
64 }
65 }
66
67 /*!
68 * \brief Template class to encapsulate a loop over entities.
69 */
70 template <typename ItemType>
71 class ItemLoopFunctor
72 {
73 public:
74
75 typedef typename ItemType::Index IterType;
76 typedef ItemVectorViewT<ItemType> VectorViewType;
77 typedef ItemGroupT<ItemType> ItemGroupType;
78 typedef ItemLoopFunctor<ItemType> ThatClass;
79
80 private:
81
82 ItemLoopFunctor(ItemVectorView items)
83 : m_items(items)
84 {}
85
86 public:
87
88 static ThatClass create(const ItemGroupType& items)
89 {
90 return ThatClass(items.view());
91 }
92 static ThatClass create(VectorViewType items)
93 {
94 return ThatClass(items);
95 }
96
97 public:
98
99 template <typename Lambda>
100 void operator<<(Lambda&& lambda)
101 {
102 _InternalSimpleItemLoop<IterType>(m_items, lambda);
103 }
104
105 private:
106
107 ItemVectorView m_items;
108 };
109
110 typedef ItemLoopFunctor<Cell> ItemLoopFunctorCell;
111 typedef ItemLoopFunctor<Node> ItemLoopFunctorNode;
112
113} // End of namespace Loop
114
115} // End of namespace Arcane
116
117/*---------------------------------------------------------------------------*/
118/*---------------------------------------------------------------------------*/
119
120/*!
121 * \brief Enumerator over an entity via a lambda function.
122 *
123 * \param item_type entity type (Arcane::Node, Arcane::Cell, Arcane::Edge, ....)
124 * \param iter name of the iterator
125 * \param container associated container (of type Arcane::ItemGroup or Arcane::ItemVectorView).
126 *
127 * This macro generates a lambda and therefore the expression must be terminated
128 * by a ';'.
129 *
130 * For example, to iterate over all cells:
131 * \code
132 * Real gamma = 1.4;
133 * ENUMERATE_ITEM_LAMBDA(Cell,icell,allCells()){
134 * Real pressure = pressure[icell];
135 * Real adiabatic_cst = adiabatic_cst[icell];
136 * Real density = density[icell];
137 * internal_energy[icell] = pressure / ((gamma-1.0) * density);
138 * };
139 * \endcode
140 * The iterator is of type \a item_type :: Index (for example Cell::Index
141 * for a cell). It therefore does not have the classic methods
142 * on entities (such as Arcane::Cell::nbNode()). The iterator
143 * only allows access to variable values.
144 *
145 * The lambda is declared with [=] and it is therefore forbidden to modify
146 * the captured variables.
147 *
148 * \warning The syntax and semantics of this macro are experimental.
149 * This macro should only be used for testing.
150 */
151#define ENUMERATE_ITEM_LAMBDA(item_type, iter, container) \
152 Arcane::Loop::ItemLoopFunctor##item_type ::create((container)) << [=](Arcane::Loop::ItemLoopFunctor##item_type ::IterType iter)
153
154/*---------------------------------------------------------------------------*/
155/*---------------------------------------------------------------------------*/
156
157/*---------------------------------------------------------------------------*/
158/*---------------------------------------------------------------------------*/
159
160#endif
Types and macros for iterating over mesh entities.
#define ENUMERATE_ITEM(name, group)
Generic enumerator for a node group.
Reference to a group of a given kind.
Definition ItemGroup.h:420
ItemVectorView view() const
View of the group entities.
Definition ItemGroup.cc:580
View on a typed array of entities.
View on a vector of entities.
Int32 size() const
Number of elements in the vector.
ItemIndexArrayView indexes() const
View on the array of indices.
Int32ConstArrayView localIds() const
Array of local IDs of entities.
Template class to encapsulate a loop over entities.
Definition ItemLoop.h:72
Namespace containing various classes managing loops over entities.
Definition ItemLoop.h:39
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int32_t Int32
Signed integer type of 32 bits.