Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
ArrayShape.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/* ArrayShape.h (C) 2000-2023 */
9/* */
10/* Represents the shape of an array. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_ARRAYSHAPE_H
13#define ARCANE_UTILS_ARRAYSHAPE_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18#include "arcane/utils/ArrayView.h"
19
20#include <array>
21
22/*
23 * ATTENTION:
24 *
25 * All classes in this file are experimental and the API is not
26 * finalized. DO NOT USE OUTSIDE OF ARCANE.
27 */
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32namespace Arcane
33{
34
35/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
38/*!
39 * \brief Array shape
40 */
41class ARCANE_UTILS_EXPORT ArrayShape
42{
43 public:
44
45 static constexpr int MAX_NB_DIMENSION = 8;
46
47 ArrayShape() = default;
48 explicit ArrayShape(Span<const Int32> v);
49
50 public:
51
52 //! Rank of the shape
53 Int32 nbDimension() const { return m_nb_dim; }
54
55 //! Values of each dimension
56 SmallSpan<const Int32> dimensions() const { return { m_dims.data(), m_nb_dim }; }
57
58 //! Number of elements in the index-th dimension
59 Int32 dimension(Int32 index) const { return m_dims[index]; }
60
61 //! Total number of elements
63 {
64 Int64 v = 1;
65 for (Int32 i = 0, n = m_nb_dim; i < n; ++i)
66 v *= (Int64)m_dims[i];
67 return v;
68 }
69
70 //! Sets the rank of the shape
71 void setNbDimension(Int32 nb_value);
72
73 //! Sets the value of the index-th dimension to \a value
74 void setDimension(Int32 index, Int32 value) { m_dims[index] = value; }
75
76 //! Sets the number and values of the dimensions
77 void setDimensions(Span<const Int32> dims);
78
79 friend std::ostream& operator<<(std::ostream& o, const ArrayShape& s)
80 {
81 s._print(o);
82 return o;
83 }
84 friend bool operator==(const ArrayShape& s1, const ArrayShape& s2)
85 {
86 return _isEqual(s1, s2);
87 }
88 friend bool operator!=(const ArrayShape& s1, const ArrayShape& s2)
89 {
90 return !_isEqual(s1, s2);
91 }
92
93 private:
94
95 Int32 m_nb_dim = 0;
96 std::array<Int32, MAX_NB_DIMENSION> m_dims = {};
97
98 private:
99
100 void _set(SmallSpan<const Int32> v);
101 static bool _isEqual(const ArrayShape& s1, const ArrayShape& s2);
102 void _print(std::ostream& o) const;
103};
104
105/*---------------------------------------------------------------------------*/
106/*---------------------------------------------------------------------------*/
107
108} // End namespace Arcane
109
110/*---------------------------------------------------------------------------*/
111/*---------------------------------------------------------------------------*/
112
113#endif
Arcane configuration file.
Array shape.
Definition ArrayShape.h:42
Int32 dimension(Int32 index) const
Number of elements in the index-th dimension.
Definition ArrayShape.h:59
void setDimension(Int32 index, Int32 value)
Sets the value of the index-th dimension to value.
Definition ArrayShape.h:74
Int32 nbDimension() const
Rank of the shape.
Definition ArrayShape.h:53
SmallSpan< const Int32 > dimensions() const
Values of each dimension.
Definition ArrayShape.h:56
Int64 totalNbElement() const
Total number of elements.
Definition ArrayShape.h:62
View of an array of elements of type T.
Definition Span.h:805
View of an array of elements of type T.
Definition Span.h:635
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int64_t Int64
Signed integer type of 64 bits.
std::ostream & operator<<(std::ostream &ostr, eItemKind item_kind)
Output operator for a stream.
std::int32_t Int32
Signed integer type of 32 bits.