Arcane  4.2.1.0
Developer documentation
Loading...
Searching...
No Matches
Span.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/* Span.h (C) 2000-2026 */
9/* */
10/* Views on C arrays. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_BASE_SPAN_H
13#define ARCCORE_BASE_SPAN_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18
19#include <type_traits>
20#include <optional>
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane::Impl
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31// To indicate that Span<T>::view() returns an ArrayView
32// and Span<const T>::view() returns a ConstArrayView.
33template <typename T>
35{
36 public:
37
38 using view_type = ArrayView<T>;
39};
40template <typename T>
41class ViewTypeT<const T>
42{
43 public:
44
45 using view_type = ConstArrayView<T>;
46};
47
49template <typename T, typename SizeType>
51
52template <typename T>
54{
55 public:
56
57 using SpanType = SmallSpan<T>;
58};
59
60template <typename T>
62{
63 public:
64
65 using SpanType = Span<T>;
66};
67
68/*---------------------------------------------------------------------------*/
69/*---------------------------------------------------------------------------*/
76template <typename SizeType>
77class DynamicExtentStorage
78{
79 template <typename T, typename SpanSizeType, SpanSizeType SpanExtent>
80 friend class ::Arcane::SpanImpl;
81
82 public:
83
84 explicit constexpr DynamicExtentStorage(SizeType s) noexcept
85 : m_size(s)
86 {}
87
88 public:
89
90 constexpr SizeType size() const noexcept { return m_size; }
91
92 private:
93
94 SizeType m_size;
95};
96
97class ARCCORE_BASE_EXPORT ExtentStorageBase
98{
99 public:
100
101 static void _throwBadSize [[noreturn]] (Int64 wanted_size, Int64 expected_size);
102};
103
105template <typename SizeType, SizeType FixedExtent>
106class ExtentStorage
107{
108 template <typename T, typename SpanSizeType, SpanSizeType SpanExtent>
109 friend class ::Arcane::SpanImpl;
110
111 public:
112
113 explicit constexpr ExtentStorage([[maybe_unused]] SizeType s) noexcept
114 {
115#if defined(ARCCORE_CHECK) && !defined(ARCCORE_DEVICE_CODE)
116 if (s != FixedExtent)
117 ExtentStorageBase::_throwBadSize(s, FixedExtent);
118#endif
119 }
120 ExtentStorage() = default;
121
122 public:
123
124 constexpr SizeType size() const noexcept { return FixedExtent; }
125
126 private:
127
128 static constexpr SizeType m_size = FixedExtent;
129};
130
132template <>
133class ExtentStorage<Int32, DynExtent>
134: public DynamicExtentStorage<Int32>
135{
136 using BaseClass = DynamicExtentStorage<Int32>;
137
138 public:
139
140 explicit constexpr ExtentStorage(Int32 s) noexcept
141 : BaseClass(s)
142 {}
143};
144
146template <>
147class ExtentStorage<Int64, DynExtent>
148: public DynamicExtentStorage<Int64>
149{
150 using BaseClass = DynamicExtentStorage<Int64>;
151
152 public:
153
154 explicit constexpr ExtentStorage(Int64 s) noexcept
155 : BaseClass(s)
156 {}
157};
158
159/*---------------------------------------------------------------------------*/
160/*---------------------------------------------------------------------------*/
161
162} // namespace Arcane::Impl
163
164/*---------------------------------------------------------------------------*/
165/*---------------------------------------------------------------------------*/
166
167namespace Arcane
168{
169
170/*---------------------------------------------------------------------------*/
171/*---------------------------------------------------------------------------*/
187template <typename T, typename SizeType, SizeType Extent>
189{
190 using ExtentStorageType = Impl::ExtentStorage<SizeType, Extent>;
191
192 public:
193
194 using ThatClass = SpanImpl<T, SizeType, Extent>;
195 using SubSpanType = SpanImpl<T, SizeType, DynExtent>;
196 using size_type = SizeType;
197 using ElementType = T;
198 using element_type = ElementType;
199 using value_type = typename std::remove_cv_t<ElementType>;
200 using const_value_type = typename std::add_const_t<value_type>;
201 using index_type = SizeType;
202 using difference_type = SizeType;
203 using pointer = ElementType*;
204 using const_pointer = const ElementType*;
205 using reference = ElementType&;
206 using const_reference = const ElementType&;
207 using iterator = ArrayIterator<pointer>;
208 using const_iterator = ArrayIterator<const_pointer>;
209 using view_type = typename Impl::ViewTypeT<ElementType>::view_type;
210 using reverse_iterator = std::reverse_iterator<iterator>;
211 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
212
214 template <typename X>
215 using is_same_const_type = std::enable_if_t<std::is_same_v<X, T> || std::is_same_v<std::add_const_t<X>, T>>;
216
217 static constexpr bool IsDynamic = (Extent == DynExtent);
218
219 public:
220
222 constexpr ARCCORE_HOST_DEVICE SpanImpl() noexcept
223 : m_ptr(nullptr)
224 , m_size(0)
225 {}
226
228 // For a Span<const T>, it is allowed to construct from a Span<T>
229 template <typename X, SizeType XExtent, typename = std::enable_if_t<std::is_same_v<const X, T>>>
230 constexpr ARCCORE_HOST_DEVICE SpanImpl(const SpanImpl<X, SizeType, XExtent>& from) noexcept
231 : m_ptr(from.data())
232 , m_size(from.size())
233 {}
234
235 template <SizeType XExtent>
236 constexpr ARCCORE_HOST_DEVICE SpanImpl(const SpanImpl<T, SizeType, XExtent>& from) noexcept
237 : m_ptr(from.data())
238 , m_size(from.size())
239 {}
240
243 constexpr ARCCORE_HOST_DEVICE SpanImpl(pointer ptr, SizeType asize) noexcept
244 : m_ptr(ptr)
245 , m_size(asize)
246 {}
247
249 template <std::size_t N, typename X, typename = is_same_const_type<X>>
250 constexpr ARCCORE_HOST_DEVICE SpanImpl(std::array<X, N>& from)
251 : m_ptr(from.data())
252 , m_size(ArraySizeChecker<SizeType>::check(from.size()))
253 {}
254
256 explicit constexpr ARCCORE_HOST_DEVICE SpanImpl(T* ptr) requires(!IsDynamic)
257 : m_ptr(ptr)
258 {}
259
261 template <std::size_t N, typename X, typename = is_same_const_type<X>>
262 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(std::array<X, N>& from)
263 {
264 m_ptr = from.data();
265 m_size = ExtentStorageType(ArraySizeChecker<SizeType>::check(from.size()));
266 return (*this);
267 }
268
269 public:
270
272 // containing \a asize elements.
273 static constexpr ThatClass create(pointer ptr, SizeType asize) noexcept
274 {
275 return ThatClass(ptr, asize);
276 }
277
278 public:
279
285 constexpr ARCCORE_HOST_DEVICE reference operator[](SizeType i) const
286 {
287 ARCCORE_CHECK_AT(i, m_size.m_size);
288 return m_ptr[i];
289 }
290
296 constexpr ARCCORE_HOST_DEVICE reference operator()(SizeType i) const
297 {
298 ARCCORE_CHECK_AT(i, m_size.m_size);
299 return m_ptr[i];
300 }
301
307 constexpr ARCCORE_HOST_DEVICE reference item(SizeType i) const
308 {
309 ARCCORE_CHECK_AT(i, m_size.m_size);
310 return m_ptr[i];
311 }
312
318 constexpr ARCCORE_HOST_DEVICE void setItem(SizeType i, const_reference v) noexcept
319 {
320 ARCCORE_CHECK_AT(i, m_size.m_size);
321 m_ptr[i] = v;
322 }
323
325 constexpr ARCCORE_HOST_DEVICE SizeType size() const noexcept { return m_size.m_size; }
327 constexpr ARCCORE_HOST_DEVICE SizeType sizeBytes() const noexcept
328 {
329 // TODO: always return an Int64
330 return static_cast<SizeType>(m_size.m_size * sizeof(value_type));
331 }
332
333 constexpr ARCCORE_HOST_DEVICE SizeType length() const noexcept { return m_size.m_size; }
334
338 constexpr ARCCORE_HOST_DEVICE iterator begin() const noexcept { return iterator(m_ptr); }
342 constexpr ARCCORE_HOST_DEVICE iterator end() const noexcept { return iterator(m_ptr + m_size.m_size); }
344 constexpr ARCCORE_HOST_DEVICE reverse_iterator rbegin() const noexcept { return std::make_reverse_iterator(end()); }
346 constexpr ARCCORE_HOST_DEVICE reverse_iterator rend() const noexcept { return std::make_reverse_iterator(begin()); }
347
348 public:
349
351 ARCCORE_DEPRECATED_REASON("Y2023: Use begin()/end() instead")
352 ArrayRange<pointer> range() const
353 {
354 return ArrayRange<pointer>(m_ptr, m_ptr + m_size.m_size);
355 }
356
357 public:
358
360 constexpr ARCCORE_HOST_DEVICE pointer ptrAt(SizeType index) const
361 {
362 ARCCORE_CHECK_AT(index, m_size.m_size);
363 return m_ptr + index;
364 }
365
366 // Element at index \a i. Always checks for bounds.
367 constexpr ARCCORE_HOST_DEVICE reference at(SizeType i) const
368 {
369 arccoreCheckAt(i, m_size.m_size);
370 return m_ptr[i];
371 }
372
373 // Sets the element at index \a i. Always checks for bounds.
374 constexpr ARCCORE_HOST_DEVICE void setAt(SizeType i, const_reference value)
375 {
376 arccoreCheckAt(i, m_size.m_size);
377 m_ptr[i] = value;
378 }
379
381 ARCCORE_HOST_DEVICE inline void fill(T o)
382 {
383 for (SizeType i = 0, n = m_size.m_size; i < n; ++i)
384 m_ptr[i] = o;
385 }
386
390 constexpr view_type smallView()
391 {
393 return view_type(s, m_ptr);
394 }
395
404
412 constexpr ARCCORE_HOST_DEVICE SubSpanType subSpan(SizeType abegin, SizeType asize) const
413 {
414 if (abegin >= m_size.m_size)
415 return {};
416 asize = _min(asize, m_size.m_size - abegin);
417 return { m_ptr + abegin, asize };
418 }
419
424 constexpr ARCCORE_HOST_DEVICE SubSpanType subPart(SizeType abegin, SizeType asize) const
425 {
426 return subSpan(abegin, asize);
427 }
428
436 ARCCORE_DEPRECATED_REASON("Y2023: use subSpan() instead")
437 constexpr SubSpanType subView(SizeType abegin, SizeType asize) const
438 {
439 return subSpan(abegin, asize);
440 }
441
443 constexpr ARCCORE_HOST_DEVICE SubSpanType subspan(SizeType abegin, SizeType asize) const
444 {
445 return subSpan(abegin, asize);
446 }
447
449 ARCCORE_DEPRECATED_REASON("Y2023: use subSpanInterval() instead")
450 constexpr SubSpanType subViewInterval(SizeType index, SizeType nb_interval) const
451 {
452 return Arcane::Impl::subViewInterval<ThatClass>(*this, index, nb_interval);
453 }
454
456 constexpr SubSpanType subSpanInterval(SizeType index, SizeType nb_interval) const
457 {
458 return Arcane::Impl::subViewInterval<ThatClass>(*this, index, nb_interval);
459 }
460
462 constexpr SubSpanType subPartInterval(SizeType index, SizeType nb_interval) const
463 {
464 return Arcane::Impl::subViewInterval<ThatClass>(*this, index, nb_interval);
465 }
466
475 template <class U> ARCCORE_HOST_DEVICE void copy(const U& copy_array)
476 {
477 Int64 n = copy_array.size();
478 Int64 size_as_int64 = m_size.m_size;
479 arccoreCheckAt(n, size_as_int64 + 1);
480 const_pointer copy_begin = copy_array.data();
481 pointer to_ptr = m_ptr;
482 // We are sure that \a fits into a 'SizeType' because it is smaller
483 // than \a m_size
484 SizeType n_as_sizetype = static_cast<SizeType>(n);
485 for (SizeType i = 0; i < n_as_sizetype; ++i)
486 to_ptr[i] = copy_begin[i];
487 }
488
490 constexpr ARCCORE_HOST_DEVICE bool empty() const noexcept { return m_size.m_size == 0; }
492 ARCCORE_HOST_DEVICE bool contains(const_reference v) const
493 {
494 for (SizeType i = 0; i < m_size.m_size; ++i) {
495 if (m_ptr[i] == v)
496 return true;
497 }
498 return false;
499 }
500
507 std::optional<SizeType> findFirst(const_reference v) const
508 {
509 for (SizeType i = 0; i < m_size.m_size; ++i) {
510 if (m_ptr[i] == v)
511 return i;
512 }
513 return std::nullopt;
514 }
515
516 public:
517
518 constexpr ARCCORE_HOST_DEVICE void setArray(const ArrayView<T>& v) noexcept
519 {
520 m_ptr = v.m_ptr;
521 m_size = v.m_size;
522 }
523 constexpr ARCCORE_HOST_DEVICE void setArray(const Span<T>& v) noexcept
524 {
525 m_ptr = v.m_ptr;
526 m_size = v.m_size;
527 }
528
537 constexpr ARCCORE_HOST_DEVICE pointer data() const noexcept { return m_ptr; }
538
540 template <typename X, SizeType Extent2, typename = std::enable_if_t<std::is_same_v<X, value_type>>> friend bool
542 {
543 return Arcane::Impl::areEqual(SpanImpl<T, SizeType>(rhs), SpanImpl<T, SizeType>(lhs));
544 }
545
547 template <typename X, SizeType Extent2, typename = std::enable_if_t<std::is_same_v<X, value_type>>> friend bool
549 {
550 return !operator==(rhs, lhs);
551 }
552
554 template <SizeType Extent2> friend bool
556 {
557 return Arcane::Impl::areEqual(SpanImpl<T, SizeType>(rhs), SpanImpl<T, SizeType>(lhs));
558 }
559
561 template <SizeType Extent2> friend bool
563 {
564 return !operator==(rhs, lhs);
565 }
566
567 friend inline std::ostream& operator<<(std::ostream& o, const ThatClass& val)
568 {
569 Arcane::Impl::dumpArray(o, Span<const T, DynExtent>(val.data(), val.size()), 500);
570 return o;
571 }
572
573 protected:
574
581 constexpr void _setArray(pointer v, SizeType s) noexcept
582 {
583 m_ptr = v;
584 m_size = s;
585 }
586
593 constexpr void _setPtr(pointer v) noexcept { m_ptr = v; }
594
601 constexpr void _setSize(SizeType s) noexcept { m_size = ExtentStorageType(s); }
602
603 private:
604
605 pointer m_ptr;
607 ARCCORE_NO_UNIQUE_ADDRESS ExtentStorageType m_size;
608
609 private:
610
611 static constexpr SizeType _min(SizeType a, SizeType b)
612 {
613 return ((a < b) ? a : b);
614 }
615};
616
617/*---------------------------------------------------------------------------*/
618/*---------------------------------------------------------------------------*/
619
630template <typename T, Int64 Extent>
631class Span
632: public SpanImpl<T, Int64, Extent>
633{
634 public:
635
636 using ThatClass = Span<T, Extent>;
637 using BaseClass = SpanImpl<T, Int64, Extent>;
638 using size_type = Int64;
639 using value_type = typename BaseClass::value_type;
640 using pointer = typename BaseClass::pointer;
641 template <typename X>
642 using is_same_const_type = std::enable_if_t<std::is_same_v<X, T> || std::is_same_v<std::add_const_t<X>, T>>;
643 static constexpr bool IsDynamic = (Extent == DynExtent);
644
645 public:
646
648 Span() = default;
650 constexpr ARCCORE_HOST_DEVICE Span(const ArrayView<value_type>& from) noexcept
651 : BaseClass(from.m_ptr, from.m_size)
652 {}
653 // Constructor from a ConstArrayView. This is only allowed
654 // if T is const.
655 template <typename X, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
656 constexpr ARCCORE_HOST_DEVICE Span(const ConstArrayView<X>& from) noexcept
657 : BaseClass(from.m_ptr, from.m_size)
658 {}
659 // For a Span<const T>, we are allowed to construct from a Span<T>
660 template <typename X, Int64 XExtent, typename = std::enable_if_t<std::is_same_v<const X, T>>>
661 constexpr ARCCORE_HOST_DEVICE Span(const Span<X, XExtent>& from) noexcept
662 : BaseClass(from)
663 {}
664 // For a Span<const T>, we are allowed to construct from a SmallSpan<T>
665 template <typename X, Int32 XExtent, typename = std::enable_if_t<std::is_same_v<const X, T>>>
666 constexpr ARCCORE_HOST_DEVICE Span(const SmallSpan<X, XExtent>& from) noexcept
667 : BaseClass(from.data(), from.size())
668 {}
669 template <Int64 XExtent>
670 constexpr ARCCORE_HOST_DEVICE Span(const SpanImpl<T, Int64, XExtent>& from) noexcept
671 : BaseClass(from)
672 {}
673 template <Int32 XExtent>
674 constexpr ARCCORE_HOST_DEVICE Span(const SpanImpl<T, Int32, XExtent>& from) noexcept
675 : BaseClass(from.data(), from.size())
676 {}
677
680 constexpr ARCCORE_HOST_DEVICE Span(pointer ptr, Int64 asize) noexcept
681 : BaseClass(ptr, asize)
682 {}
683
685 template <std::size_t N, typename X, typename = is_same_const_type<X>>
686 constexpr ARCCORE_HOST_DEVICE Span(std::array<X, N>& from) noexcept
687 : BaseClass(from.data(), from.size())
688 {}
689
691 explicit constexpr ARCCORE_HOST_DEVICE Span(T* ptr) requires(!IsDynamic)
692 : BaseClass(ptr)
693 {}
694
696 template <std::size_t N, typename X, typename = is_same_const_type<X>>
697 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(std::array<X, N>& from) noexcept
698 {
699 this->_setPtr(from.data());
700 this->_setSize(from.size());
701 return (*this);
702 }
703
704 public:
705
707 // containing \a asize elements.
708 static constexpr ThatClass create(pointer ptr, size_type asize) noexcept
709 {
710 return ThatClass(ptr, asize);
711 }
712
713 public:
714
722 constexpr ARCCORE_HOST_DEVICE Span<T, DynExtent> subspan(Int64 abegin, Int64 asize) const
723 {
724 return BaseClass::subspan(abegin, asize);
725 }
726
734 constexpr ARCCORE_HOST_DEVICE Span<T, DynExtent> subSpan(Int64 abegin, Int64 asize) const
735 {
736 return BaseClass::subSpan(abegin, asize);
737 }
738
746 constexpr ARCCORE_HOST_DEVICE Span<T, DynExtent> subPart(Int64 abegin, Int64 asize) const
747 {
748 return BaseClass::subPart(abegin, asize);
749 }
750
752 constexpr ARCCORE_HOST_DEVICE Span<T, DynExtent> subSpanInterval(Int64 index, Int64 nb_interval) const
753 {
754 return Arcane::Impl::subViewInterval<ThatClass>(*this, index, nb_interval);
755 }
756
758 constexpr ARCCORE_HOST_DEVICE Span<T, DynExtent> subPartInterval(Int64 index, Int64 nb_interval) const
759 {
760 return Arcane::Impl::subViewInterval<ThatClass>(*this, index, nb_interval);
761 }
762
770 ARCCORE_DEPRECATED_REASON("Y2023: use subSpan() instead")
771 constexpr ARCCORE_HOST_DEVICE Span<T> subView(Int64 abegin, Int64 asize) const
772 {
773 return subspan(abegin, asize);
774 }
775
777 ARCCORE_DEPRECATED_REASON("Y2023: use subSpanInterval() instead")
778 constexpr ARCCORE_HOST_DEVICE Span<T> subViewInterval(Int64 index, Int64 nb_interval) const
779 {
780 return Arcane::Impl::subViewInterval<ThatClass>(*this, index, nb_interval);
781 }
782};
783
784/*---------------------------------------------------------------------------*/
785/*---------------------------------------------------------------------------*/
786
800template <typename T, Int32 Extent>
802: public SpanImpl<T, Int32, Extent>
803{
804 public:
805
806 using ThatClass = SmallSpan<T, Extent>;
807 using BaseClass = SpanImpl<T, Int32, Extent>;
808 using size_type = Int32;
809 using value_type = typename BaseClass::value_type;
810 using pointer = typename BaseClass::pointer;
811 template <typename X>
812 using is_same_const_type = std::enable_if_t<std::is_same_v<X, T> || std::is_same_v<std::add_const_t<X>, T>>;
813 static constexpr bool IsDynamic = (Extent == DynExtent);
814
815 public:
816
818 SmallSpan() = default;
819
821 constexpr ARCCORE_HOST_DEVICE SmallSpan(const ArrayView<value_type>& from) noexcept
822 : BaseClass(from.m_ptr, from.m_size)
823 {}
824
825 // Constructor from a ConstArrayView. This is only allowed
826 // if T is const.
827 template <typename X, typename = std::enable_if_t<std::is_same<X, value_type>::value>>
828 constexpr ARCCORE_HOST_DEVICE SmallSpan(const ConstArrayView<X>& from) noexcept
829 : BaseClass(from.m_ptr, from.m_size)
830 {}
831
832 // For a Span<const T>, we are allowed to construct from a Span<T>
833 template <typename X, typename = std::enable_if_t<std::is_same<X, value_type>::value>>
834 constexpr ARCCORE_HOST_DEVICE SmallSpan(const SmallSpan<X>& from) noexcept
835 : BaseClass(from)
836 {}
837
838 template <Int32 XExtent>
839 constexpr ARCCORE_HOST_DEVICE SmallSpan(const SpanImpl<T, Int32, XExtent>& from) noexcept
840 : BaseClass(from)
841 {}
842
845 constexpr ARCCORE_HOST_DEVICE SmallSpan(pointer ptr, Int32 asize) noexcept
846 : BaseClass(ptr, asize)
847 {}
848
849 template <std::size_t N, typename X, typename = is_same_const_type<X>>
850 constexpr ARCCORE_HOST_DEVICE SmallSpan(std::array<X, N>& from)
851 : BaseClass(from)
852 {}
853
855 explicit constexpr ARCCORE_HOST_DEVICE SmallSpan(T* ptr) requires(!IsDynamic)
856 : BaseClass(ptr)
857 {}
858
860 template <std::size_t N, typename X, typename = is_same_const_type<X>>
861 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(std::array<X, N>& from)
862 {
864 return (*this);
865 }
866
867 public:
868
870 // containing \a asize elements.
871 static constexpr ThatClass create(pointer ptr, size_type asize) noexcept
872 {
873 return ThatClass(ptr, asize);
874 }
875
876 public:
877
885 constexpr ARCCORE_HOST_DEVICE SmallSpan<T, DynExtent> subspan(Int32 abegin, Int32 asize) const
886 {
887 return BaseClass::subspan(abegin, asize);
888 }
889
897 constexpr ARCCORE_HOST_DEVICE SmallSpan<T, DynExtent> subSpan(Int32 abegin, Int32 asize) const
898 {
899 return BaseClass::subSpan(abegin, asize);
900 }
901
909 constexpr ARCCORE_HOST_DEVICE SmallSpan<T, DynExtent> subPart(Int32 abegin, Int32 asize) const
910 {
911 return BaseClass::subSpan(abegin, asize);
912 }
913
915 constexpr ARCCORE_HOST_DEVICE SmallSpan<T, DynExtent> subSpanInterval(Int32 index, Int32 nb_interval) const
916 {
917 return Arcane::Impl::subViewInterval<ThatClass>(*this, index, nb_interval);
918 }
919
921 constexpr ARCCORE_HOST_DEVICE ThatClass subPartInterval(Int32 index, Int32 nb_interval) const
922 {
923 return subSpanInterval(index, nb_interval);
924 }
925
933 ARCCORE_DEPRECATED_REASON("Y2023: use subPart() instead")
934 constexpr ARCCORE_HOST_DEVICE SmallSpan<T> subView(Int32 abegin, Int32 asize) const
935 {
936 return subspan(abegin, asize);
937 }
938
940 ARCCORE_DEPRECATED_REASON("Y2023: use subPartInterval() instead")
941 constexpr ARCCORE_HOST_DEVICE SmallSpan<T> subViewInterval(Int32 index, Int32 nb_interval) const
942 {
943 return Arcane::Impl::subViewInterval<ThatClass>(*this, index, nb_interval);
944 }
945};
946
947/*---------------------------------------------------------------------------*/
948/*---------------------------------------------------------------------------*/
949
958template <typename T, typename SizeType> inline void
959dumpArray(std::ostream& o, SpanImpl<const T, SizeType> val, int max_print)
960{
961 Arcane::Impl::dumpArray(o, val, max_print);
962}
963
964/*---------------------------------------------------------------------------*/
965/*---------------------------------------------------------------------------*/
966
975template <typename DataType, typename IntegerType, typename SizeType> inline void
979{
980 const Int64 result_size = indexes.size();
981 [[maybe_unused]] const Int64 my_size = values.size();
982 const DataType* ptr = values.data();
983 for (Int64 i = 0; i < result_size; ++i) {
984 IntegerType index = indexes[i];
985 ARCCORE_CHECK_AT(index, my_size);
986 result[i] = ptr[index];
987 }
988}
989
990/*---------------------------------------------------------------------------*/
991/*---------------------------------------------------------------------------*/
992
1001template <typename DataType> inline void
1003{
1004 _sampleSpan<DataType, Int64, Int64>(values, indexes, result);
1005}
1006
1007/*---------------------------------------------------------------------------*/
1008/*---------------------------------------------------------------------------*/
1009
1016template <typename DataType> inline void
1018{
1019 _sampleSpan<DataType, Int32, Int64>(values, indexes, result);
1020}
1021
1022/*---------------------------------------------------------------------------*/
1023/*---------------------------------------------------------------------------*/
1024
1028template <typename DataType, typename SizeType, SizeType Extent>
1029inline typename Impl::SpanTypeFromSize<const std::byte, SizeType>::SpanType
1031{
1032 return { reinterpret_cast<const std::byte*>(s.data()), s.sizeBytes() };
1033}
1034
1038template <typename DataType>
1039inline SmallSpan<const std::byte>
1041{
1042 return asBytes(SmallSpan<DataType>(s));
1043}
1044
1048template <typename DataType>
1049inline SmallSpan<const std::byte>
1054
1055/*---------------------------------------------------------------------------*/
1056/*---------------------------------------------------------------------------*/
1057
1063template <typename DataType, typename SizeType, SizeType Extent,
1064 typename std::enable_if_t<!std::is_const<DataType>::value, int> = 0>
1065inline typename Impl::SpanTypeFromSize<std::byte, SizeType>::SpanType
1067{
1068 return { reinterpret_cast<std::byte*>(s.data()), s.sizeBytes() };
1069}
1070
1076template <typename DataType> inline SmallSpan<std::byte>
1081
1082/*---------------------------------------------------------------------------*/
1083/*---------------------------------------------------------------------------*/
1084
1085namespace Impl
1086{
1087 template <typename ByteType, typename DataType, Int64 Extent> inline Span<DataType>
1088 asSpanInternal(Span<ByteType, Extent> bytes)
1089 {
1090 Int64 size = bytes.size();
1091 if (size == 0)
1092 return {};
1093 static constexpr Int64 data_type_size = static_cast<Int64>(sizeof(DataType));
1094 static_assert(data_type_size > 0, "Bad datatype size");
1095 ARCCORE_ASSERT((size % data_type_size) == 0, ("Size is not a multiple of sizeof(DataType)"));
1096 auto* ptr = reinterpret_cast<DataType*>(bytes.data());
1097 return { ptr, size / data_type_size };
1098 }
1099
1100 template <typename ByteType, typename DataType, Int32 Extent> inline SmallSpan<DataType>
1101 asSmallSpanInternal(SmallSpan<ByteType, Extent> bytes)
1102 {
1103 Int32 size = bytes.size();
1104 if (size == 0)
1105 return {};
1106 static constexpr Int32 data_type_size = static_cast<Int32>(sizeof(DataType));
1107 static_assert(data_type_size > 0, "Bad datatype size");
1108 ARCCORE_ASSERT((size % data_type_size) == 0, ("Size is not a multiple of sizeof(DataType)"));
1109 auto* ptr = reinterpret_cast<DataType*>(bytes.data());
1110 return { ptr, size / data_type_size };
1111 }
1112} // namespace Impl
1113
1114/*---------------------------------------------------------------------------*/
1115/*---------------------------------------------------------------------------*/
1116
1121template <typename DataType, Int64 Extent> inline Span<DataType>
1123{
1124 return Arcane::Impl::asSpanInternal<std::byte, DataType, Extent>(bytes);
1125}
1126
1131template <typename DataType, Int64 Extent> inline Span<const DataType>
1133{
1134 return Arcane::Impl::asSpanInternal<const std::byte, const DataType, Extent>(bytes);
1135}
1136
1141template <typename DataType, Int32 Extent> inline SmallSpan<DataType>
1143{
1144 return Arcane::Impl::asSmallSpanInternal<std::byte, DataType, Extent>(bytes);
1145}
1146
1151template <typename DataType, Int32 Extent> inline SmallSpan<const DataType>
1153{
1154 return Arcane::Impl::asSmallSpanInternal<const std::byte, const DataType, Extent>(bytes);
1155}
1156
1157/*---------------------------------------------------------------------------*/
1158/*---------------------------------------------------------------------------*/
1159
1163template <typename DataType, size_t SizeType> inline Span<DataType, SizeType>
1164asSpan(std::array<DataType, SizeType>& s)
1165{
1166 Int64 size = static_cast<Int64>(s.size());
1167 return { s.data(), size };
1168}
1169
1173template <typename DataType, size_t SizeType> inline SmallSpan<DataType, SizeType>
1174asSmallSpan(std::array<DataType, SizeType>& s)
1175{
1176 Int32 size = static_cast<Int32>(s.size());
1177 return { s.data(), size };
1178}
1179
1180/*---------------------------------------------------------------------------*/
1181/*---------------------------------------------------------------------------*/
1182
1188extern "C++" ARCCORE_BASE_EXPORT void
1189binaryWrite(std::ostream& ostr, const Span<const std::byte>& bytes);
1190
1196extern "C++" ARCCORE_BASE_EXPORT void
1197binaryRead(std::istream& istr, const Span<std::byte>& bytes);
1198
1199/*---------------------------------------------------------------------------*/
1200/*---------------------------------------------------------------------------*/
1201
1202} // namespace Arcane
1203
1204/*---------------------------------------------------------------------------*/
1205/*---------------------------------------------------------------------------*/
1206
1207namespace Arccore
1208{
1209using Arcane::asBytes;
1211using Arcane::asSpan;
1213using Arcane::binaryRead;
1215using Arcane::sampleSpan;
1216} // namespace Arccore
1217
1218/*---------------------------------------------------------------------------*/
1219/*---------------------------------------------------------------------------*/
1220
1221#endif
Types and functions associated with the classes ArrayView and ConstArrayView.
Iterator over Arccore array classes.
Interval over Arccore array classes.
Modifiable view of an array of type T.
Constant view of an array of type T.
Specialization for the compile-time known number of elements.
Definition Span.h:107
To have the type (SmallSpan or Span) depending on the size (Int32 or Int64).
Definition Span.h:50
View of an array of elements of type T.
Definition Span.h:803
constexpr __host__ __device__ SmallSpan< T, DynExtent > subspan(Int32 abegin, Int32 asize) const
Sub-view starting from element abegin and containing asize elements.
Definition Span.h:885
constexpr __host__ __device__ SmallSpan< const ConstituentItemType_ > subViewInterval(Int32 index, Int32 nb_interval) const
Definition Span.h:941
constexpr __host__ __device__ ThatClass & operator=(std::array< X, N > &from)
Copy assignment operator.
Definition Span.h:861
constexpr __host__ __device__ SmallSpan< T, DynExtent > subSpanInterval(Int32 index, Int32 nb_interval) const
Sub-view corresponding to the interval index over nb_interval.
Definition Span.h:915
constexpr __host__ __device__ SmallSpan< T, DynExtent > subPart(Int32 abegin, Int32 asize) const
Sub-view starting from element abegin and containing asize elements.
Definition Span.h:909
SmallSpan()=default
Constructs an empty view.
constexpr __host__ __device__ SmallSpan(const ArrayView< value_type > &from) noexcept
Copy constructor from another view.
Definition Span.h:821
constexpr __host__ __device__ SmallSpan(pointer ptr, Int32 asize) noexcept
Definition Span.h:845
constexpr __host__ __device__ SmallSpan< const ConstituentItemType_ > subView(Int32 abegin, Int32 asize) const
Definition Span.h:934
constexpr __host__ __device__ SmallSpan(T *ptr)
Constructs a view from a pointer with a fixed size.
Definition Span.h:855
static constexpr ThatClass create(pointer ptr, size_type asize) noexcept
Constructs a view over a memory region starting at ptr and.
Definition Span.h:871
constexpr __host__ __device__ SmallSpan< T, DynExtent > subSpan(Int32 abegin, Int32 asize) const
Sub-view starting from element abegin and containing asize elements.
Definition Span.h:897
constexpr __host__ __device__ ThatClass subPartInterval(Int32 index, Int32 nb_interval) const
Sub-view corresponding to the interval index over nb_interval.
Definition Span.h:921
View of an array of elements of type T.
Definition Span.h:189
constexpr __host__ __device__ SpanImpl(pointer ptr, SizeType asize) noexcept
Definition Span.h:243
constexpr __host__ __device__ SpanImpl(const SpanImpl< X, SizeType, XExtent > &from) noexcept
Copy constructor from another view.
Definition Span.h:230
constexpr view_type smallView()
Constant view of this view.
Definition Span.h:390
__host__ __device__ void copy(const U &copy_array)
Copies the array copy_array into the instance.
Definition Span.h:475
constexpr SubSpanType subView(SizeType abegin, SizeType asize) const
Definition Span.h:437
friend bool operator==(const SpanImpl< T, SizeType, Extent > &rhs, const SpanImpl< X, SizeType, Extent2 > &lhs)
Equality operator (valid if T is const but not X).
Definition Span.h:541
constexpr __host__ __device__ SpanImpl(std::array< X, N > &from)
Constructs a view from a std::array.
Definition Span.h:250
std::optional< SizeType > findFirst(const_reference v) const
Definition Span.h:507
constexpr void _setArray(pointer v, SizeType s) noexcept
Modifies the array pointer and size.
Definition Span.h:581
constexpr __host__ __device__ SpanImpl(T *ptr)
Constructs a view from a pointer with a fixed size.
Definition Span.h:256
constexpr __host__ __device__ pointer data() const noexcept
Definition Span.h:537
constexpr __host__ __device__ SizeType length() const noexcept
Number of elements in the array.
Definition Span.h:333
constexpr __host__ __device__ iterator begin() const noexcept
Iterator for the first element of the array.
Definition Span.h:338
__host__ __device__ void fill(T o)
Fills the array with the value o.
Definition Span.h:381
constexpr __host__ __device__ pointer ptrAt(SizeType index) const
Address of the index-th element.
Definition Span.h:360
constexpr __host__ __device__ reference operator()(SizeType i) const
i-th element of the array.
Definition Span.h:296
constexpr __host__ __device__ reference operator[](SizeType i) const
i-th element of the array.
Definition Span.h:285
ArrayRange< pointer > range() const
Definition Span.h:352
constexpr __host__ __device__ bool empty() const noexcept
Returns true if the array is empty (zero dimension).
Definition Span.h:490
ARCCORE_NO_UNIQUE_ADDRESS ExtentStorageType m_size
Definition Span.h:607
constexpr SubSpanType subViewInterval(SizeType index, SizeType nb_interval) const
Definition Span.h:450
constexpr __host__ __device__ SubSpanType subSpan(SizeType abegin, SizeType asize) const
Sub-view starting from element abegin and containing asize elements.
Definition Span.h:412
constexpr __host__ __device__ reference item(SizeType i) const
i-th element of the array.
Definition Span.h:307
constexpr ConstArrayView< value_type > constSmallView() const
Constant view of this view.
Definition Span.h:399
friend bool operator==(const SpanImpl< T, SizeType, Extent > &rhs, const SpanImpl< T, SizeType, Extent2 > &lhs)
Equality operator.
Definition Span.h:555
constexpr SubSpanType subPartInterval(SizeType index, SizeType nb_interval) const
Sub-view corresponding to the interval index over nb_interval.
Definition Span.h:462
__host__ __device__ bool contains(const_reference v) const
Returns true if the array contains the element with value v.
Definition Span.h:492
constexpr __host__ __device__ ThatClass & operator=(std::array< X, N > &from)
Copy assignment operator.
Definition Span.h:262
constexpr __host__ __device__ iterator end() const noexcept
Iterator for the element after the end of the array.
Definition Span.h:342
constexpr void _setPtr(pointer v) noexcept
Modifies the array start pointer.
Definition Span.h:593
friend bool operator!=(const SpanImpl< T, SizeType, Extent > &rhs, const SpanImpl< X, SizeType, Extent2 > &lhs)
Inequality operator (valid if T is const but not X).
Definition Span.h:548
constexpr __host__ __device__ reverse_iterator rend() const noexcept
Reverse iterator for the element after the end of the array.
Definition Span.h:346
constexpr __host__ __device__ SizeType size() const noexcept
Definition Span.h:325
constexpr __host__ __device__ SizeType sizeBytes() const noexcept
Returns the size of the array in bytes.
Definition Span.h:327
constexpr __host__ __device__ reverse_iterator rbegin() const noexcept
Reverse iterator for the first element of the array.
Definition Span.h:344
friend bool operator!=(const SpanImpl< T, SizeType, Extent > &rhs, const SpanImpl< T, SizeType, Extent2 > &lhs)
Inequality operator.
Definition Span.h:562
constexpr void _setSize(SizeType s) noexcept
Modifies the array size.
Definition Span.h:601
constexpr __host__ __device__ SubSpanType subspan(SizeType abegin, SizeType asize) const
For C++20 compatibility.
Definition Span.h:443
constexpr __host__ __device__ SubSpanType subPart(SizeType abegin, SizeType asize) const
Sub-view starting from element abegin and containing asize elements.
Definition Span.h:424
constexpr __host__ __device__ SpanImpl() noexcept
Constructs an empty view.
Definition Span.h:222
constexpr SubSpanType subSpanInterval(SizeType index, SizeType nb_interval) const
Sub-view corresponding to the interval index over nb_interval.
Definition Span.h:456
constexpr __host__ __device__ void setItem(SizeType i, const_reference v) noexcept
Sets the i-th element of the array.
Definition Span.h:318
static constexpr ThatClass create(pointer ptr, SizeType asize) noexcept
Constructs a view on a memory region starting at ptr and.
Definition Span.h:273
std::enable_if_t< std::is_same_v< X, T >||std::is_same_v< std::add_const_t< X >, T > > is_same_const_type
Definition Span.h:215
View of an array of elements of type T.
Definition Span.h:633
constexpr __host__ __device__ Span< T, DynExtent > subPart(Int64 abegin, Int64 asize) const
Sub-view starting from element abegin and containing asize elements.
Definition Span.h:746
constexpr __host__ __device__ Span< T, DynExtent > subSpanInterval(Int64 index, Int64 nb_interval) const
Sub-view corresponding to the interval index over nb_interval.
Definition Span.h:752
static constexpr ThatClass create(pointer ptr, size_type asize) noexcept
Constructs a view on a memory area starting at ptr and.
Definition Span.h:708
constexpr __host__ __device__ Span(std::array< X, N > &from) noexcept
Constructs a view from a std::array.
Definition Span.h:686
constexpr __host__ __device__ Span< DataType > subView(Int64 abegin, Int64 asize) const
Definition Span.h:771
constexpr __host__ __device__ Span(pointer ptr, Int64 asize) noexcept
Definition Span.h:680
constexpr __host__ __device__ ThatClass & operator=(std::array< X, N > &from) noexcept
Copy assignment operator.
Definition Span.h:697
constexpr __host__ __device__ Span(T *ptr)
Constructs a view from a pointer with a fixed size.
Definition Span.h:691
constexpr __host__ __device__ Span(const ArrayView< value_type > &from) noexcept
Copy constructor from another view.
Definition Span.h:650
Span()=default
Constructs an empty view.
constexpr __host__ __device__ Span< DataType > subViewInterval(Int64 index, Int64 nb_interval) const
Definition Span.h:778
constexpr __host__ __device__ Span< T, DynExtent > subPartInterval(Int64 index, Int64 nb_interval) const
Sub-view corresponding to the interval index over nb_interval.
Definition Span.h:758
constexpr __host__ __device__ Span< T, DynExtent > subSpan(Int64 abegin, Int64 asize) const
Sub-view starting from element abegin and containing asize elements.
Definition Span.h:734
constexpr __host__ __device__ Span< T, DynExtent > subspan(Int64 abegin, Int64 asize) const
Sub-view starting from element abegin and containing asize elements.
Definition Span.h:722
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
constexpr __host__ __device__ Integer arccoreCheckArraySize(unsigned long long size)
Checks that size can be converted into an 'Integer' to serve as an array size. If possible,...
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
constexpr Int32 DynExtent
Constant to indicate that an array dimension is dynamic.
Definition BaseTypes.h:53
__host__ __device__ void arccoreCheckAt(Int64 i, Int64 max_size)
Checks for potential array overflow.
void dumpArray(std::ostream &o, ConstArrayView< T > val, int max_print)
Displays the values of array val to the stream o.
void sampleSpan(Span< const DataType > values, Span< const Int64 > indexes, Span< DataType > result)
Extracts a sub-array from a list of indices.
Definition Span.h:1002
SmallSpan< DataType > asSmallSpan(SmallSpan< std::byte, Extent > bytes)
Converts a SmallSpan<std::byte> into a SmallSpan<DataType>.
Definition Span.h:1142
Span< DataType > asSpan(Span< std::byte, Extent > bytes)
Converts a Span<std::byte> into a Span<DataType>.
Definition Span.h:1122
void binaryRead(std::istream &istr, const Span< std::byte > &bytes)
Reads the content of bytes from the stream istr in binary format.
Definition ArrayView.cc:102
Impl::SpanTypeFromSize< conststd::byte, SizeType >::SpanType asBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converts the view into an array of non-modifiable bytes.
Definition Span.h:1030
Impl::SpanTypeFromSize< std::byte, SizeType >::SpanType asWritableBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converts the view into an array of modifiable bytes.
Definition Span.h:1066
void binaryWrite(std::ostream &ostr, const Span< const std::byte > &bytes)
Writes the content of bytes to the stream ostr in binary format.
Definition ArrayView.cc:93
void _sampleSpan(SpanImpl< const DataType, SizeType > values, SpanImpl< const IntegerType, SizeType > indexes, SpanImpl< DataType, SizeType > result)
Extracts a sub-array from a list of indices.
Definition Span.h:976
std::int32_t Int32
Signed integer type of 32 bits.
Namespace of Arccore.
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.).
Definition rapidjson.h:416