Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
ItemVector.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/* ItemVector.h (C) 2000-2024 */
9/* */
10/* Vector of entities of the same type. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CORE_ITEMVECTOR_H
13#define ARCANE_CORE_ITEMVECTOR_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18#include "arcane/core/ItemVectorView.h"
19#include "arcane/core/IItemFamily.h"
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Arcane
25{
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30/*!
31 * \brief Entity vector.
32 *
33 * The ItemVector class uses a reference semantics.
34 *
35 * \note This class is not thread-safe and should not be used by
36 * different threads simultaneously.
37 *
38 * \warning A vector must necessarily be associated with an entity family
39 * (ItemFamily*) before being used. This can be done either via
40 * calling setFamily(), or via a constructor that takes a family
41 * as an argument.
42 *
43 * \a ItemVector is the generic class. It is possible to have a
44 * specialized version by entity type via ItemVectorT.
45 *
46 * The operation of the entity vector is similar to that of
47 * the ItemGroup entity group, with the following differences:
48 * - the vector is local to the subdomain
49 * - the vector is invalidated if the associated family (IItemFamily) changes
50 * (after calling IItemFamily::endUpdate() if compaction or sorting is
51 * active)
52 * - the vector can contain the same entity multiple times.
53 *
54 * A vector is useful for building a temporary list
55 * of entities. It inherits functionalities similar to the Array class
56 * and it is therefore possible, for example, to add elements one by one,
57 * either via a localId(), or via an entity.
58 */
59class ARCANE_CORE_EXPORT ItemVector
60{
61 public:
62
63 using ItemType = Item;
64
65 public:
66
67 //! Creates an empty vector associated with the family \a family.
68 explicit ItemVector(IItemFamily* afamily);
69
70 //! Creates a vector associated with the family \a family and containing the entities \a local_ids.
71 ItemVector(IItemFamily* afamily, Int32ConstArrayView local_ids);
72
73 //! Creates a vector for \a size elements associated with the family \a family.
74 ItemVector(IItemFamily* afamily, Integer asize);
75
76 //! Creates a null vector. You must then call setFamily() to use it
77 ItemVector();
78
79 public:
80
81 //! Cast operator to ItemVectorView
82 operator ItemVectorView() const { return view(); }
83
84 public:
85
86 /*!
87 * \brief Sets the associated family
88 *
89 * The vector is cleared of its elements
90 */
91 void setFamily(IItemFamily* afamily);
92
93 //! Adds an entity with local ID \a local_id to the end of the vector
94 void add(Int32 local_id) { m_local_ids.add(local_id); }
95
96 //! Adds a list of entity local IDs \a local_ids to the end of the vector
97 void add(ConstArrayView<Int32> local_ids) { m_local_ids.addRange(local_ids); }
98
99 //! Adds an entity with local ID \a local_id to the end of the vector
100 void addItem(ItemLocalId local_id) { m_local_ids.add(local_id); }
101
102 //! Adds an entity to the end of the vector
103 void addItem(Item item) { m_local_ids.add(item.localId()); }
104
105 //! Number of elements in the vector
106 Int32 size() const { return m_local_ids.size(); }
107
108 //! Reserves memory for \a capacity entities
109 void reserve(Integer capacity) { m_local_ids.reserve(capacity); }
110
111 //! Removes all entities from the vector.
112 void clear() { m_local_ids.clear(); }
113
114 //! View of the vector
115 ItemVectorView view() const { return ItemVectorView(m_shared_info, m_local_ids, 0); }
116
117 //! View of the local IDs
118 ArrayView<Int32> viewAsArray() { return m_local_ids.view(); }
119
120 //! Constant view of the local IDs
121 ConstArrayView<Int32> viewAsArray() const { return m_local_ids.constView(); }
122
123 //! Removes the entity at index \a index
124 void removeAt(Int32 index) { m_local_ids.remove(index); }
125
126 /*!
127 * \brief Sets the number of elements in the array.
128 *
129 * If the new size is greater than the old size, the
130 * added elements are undefined.
131 */
132 void resize(Integer new_size) { m_local_ids.resize(new_size); }
133
134 //! Clones this vector
135 ItemVector clone() { return ItemVector(m_family, m_local_ids.constView()); }
136
137 //! Entity at position \a index of the vector
138 Item operator[](Int32 index) const { return Item(m_local_ids[index], m_shared_info); }
139
140 //! Family associated with the vector
141 IItemFamily* family() const { return m_family; }
142
143 //! Enumerator
144 ItemEnumerator enumerator() const { return ItemEnumerator(m_shared_info, m_local_ids); }
145
146 protected:
147
148 SharedArray<Int32> m_local_ids;
149 IItemFamily* m_family = nullptr;
150 ItemSharedInfo* m_shared_info = ItemSharedInfo::nullInstance();
151
152 private:
153
154 void _init();
155};
156
157/*---------------------------------------------------------------------------*/
158/*---------------------------------------------------------------------------*/
159
160/*!
161 * \brief Typed entity vector.
162 *
163 * For more information, see ItemVector.
164 */
165template <typename VectorItemType>
167: public ItemVector
168{
169 public:
170
171 using ItemType = VectorItemType;
172
173 public:
174
175 //! Empty constructor
176 ItemVectorT() = default;
177
178 //! Empty constructor with family
179 explicit ItemVectorT(IItemFamily* afamily)
180 : ItemVector(afamily)
181 {}
182
183 //! Creates a vector associated with the family \a afamily and containing the entities \a local_ids.
185 : ItemVector(afamily, local_ids)
186 {}
187
188 //! Copy constructor
190 : ItemVector(rhs)
191 {}
192
193 //! Constructor for \a asize elements for the family \a afamily
195 : ItemVector(afamily, asize)
196 {}
197
198 public:
199
200 //! Cast operator to ItemVectorView
201 operator ItemVectorViewT<VectorItemType>() const { return view(); }
202
203 public:
204
205 //! Entity at position \a index of the vector
206 ItemType operator[](Int32 index) const
207 {
208 return ItemType(m_local_ids[index], m_shared_info);
209 }
210
211 //! Adds an entity to the end of the vector
212 void addItem(ItemType item) { m_local_ids.add(item.localId()); }
213
214 //! Adds an entity to the end of the vector
215 void addItem(ItemLocalIdT<ItemType> local_id) { m_local_ids.add(local_id); }
216
217 //! View of the entire array
219 {
220 return ItemVectorViewT<ItemType>(m_shared_info, m_local_ids.constView(), 0);
221 }
222
223 //! Enumerator
225 {
226 return ItemEnumeratorT<ItemType>(m_shared_info, m_local_ids);
227 }
228};
229
230/*---------------------------------------------------------------------------*/
231/*---------------------------------------------------------------------------*/
232
233template <typename ItemType> inline ItemVectorViewT<ItemType>::
234ItemVectorViewT(const ItemVectorT<ItemType>& rhs)
235: ItemVectorView(rhs.view())
236{
237}
238
239/*---------------------------------------------------------------------------*/
240/*---------------------------------------------------------------------------*/
241
242} // namespace Arcane
243
244/*---------------------------------------------------------------------------*/
245/*---------------------------------------------------------------------------*/
246
247#endif
Types and macros for iterating over mesh entities.
Modifiable view of an array of type T.
Constant view of an array of type T.
Interface of an entity family.
Definition IItemFamily.h:83
Enumerator over a typed list of entities of type ItemType.
Enumerator over a list of entities.
Index of an ItemType entity in a variable.
Definition ItemLocalId.h:92
Index of an Item in a variable.
Definition ItemLocalId.h:42
ItemEnumeratorT< ItemType > enumerator() const
Enumerator.
Definition ItemVector.h:224
void addItem(ItemType item)
Adds an entity to the end of the vector.
Definition ItemVector.h:212
ItemType operator[](Int32 index) const
Entity at position index of the vector.
Definition ItemVector.h:206
void addItem(ItemLocalIdT< ItemType > local_id)
Adds an entity to the end of the vector.
Definition ItemVector.h:215
ItemVectorT()=default
Empty constructor.
ItemVectorT(IItemFamily *afamily, Integer asize)
Constructor for asize elements for the family afamily.
Definition ItemVector.h:194
ItemVectorT(IItemFamily *afamily, ConstArrayView< Int32 > local_ids)
Creates a vector associated with the family afamily and containing the entities local_ids.
Definition ItemVector.h:184
ItemVectorT(IItemFamily *afamily)
Empty constructor with family.
Definition ItemVector.h:179
ItemVectorViewT< ItemType > view() const
View of the entire array.
Definition ItemVector.h:218
ItemVectorT(const ItemVector &rhs)
Copy constructor.
Definition ItemVector.h:189
View on a typed array of entities.
View on a vector of entities.
ConstArrayView< Int32 > viewAsArray() const
Constant view of the local IDs.
Definition ItemVector.h:121
void add(Int32 local_id)
Adds an entity with local ID local_id to the end of the vector.
Definition ItemVector.h:94
void removeAt(Int32 index)
Removes the entity at index index.
Definition ItemVector.h:124
void clear()
Removes all entities from the vector.
Definition ItemVector.h:112
ItemVector clone()
Clones this vector.
Definition ItemVector.h:135
IItemFamily * family() const
Family associated with the vector.
Definition ItemVector.h:141
ItemVector(IItemFamily *afamily)
Creates an empty vector associated with the family family.
Definition ItemVector.cc:38
ItemVectorView view() const
View of the vector.
Definition ItemVector.h:115
void addItem(Item item)
Adds an entity to the end of the vector.
Definition ItemVector.h:103
void resize(Integer new_size)
Sets the number of elements in the array.
Definition ItemVector.h:132
Int32 size() const
Number of elements in the vector.
Definition ItemVector.h:106
ArrayView< Int32 > viewAsArray()
View of the local IDs.
Definition ItemVector.h:118
void addItem(ItemLocalId local_id)
Adds an entity with local ID local_id to the end of the vector.
Definition ItemVector.h:100
Item operator[](Int32 index) const
Entity at position index of the vector.
Definition ItemVector.h:138
ItemEnumerator enumerator() const
Enumerator.
Definition ItemVector.h:144
void reserve(Integer capacity)
Reserves memory for capacity entities.
Definition ItemVector.h:109
void add(ConstArrayView< Int32 > local_ids)
Adds a list of entity local IDs local_ids to the end of the vector.
Definition ItemVector.h:97
Base class for a mesh element.
Definition Item.h:84
constexpr Int32 localId() const
Local identifier of the entity in the processor subdomain.
Definition Item.h:233
1D vector of data with reference semantics.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
ConstArrayView< Int32 > Int32ConstArrayView
C equivalent of a 1D array of 32-bit integers.
Definition UtilsTypes.h:482
std::int32_t Int32
Signed integer type of 32 bits.