Arcane  4.1.12.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-2025 */
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/*---------------------------------------------------------------------------*/
70
77template <typename SizeType>
78class DynamicExtentStorage
79{
80 template <typename T, typename SpanSizeType, SpanSizeType SpanExtent>
81 friend class ::Arcane::SpanImpl;
82
83 public:
84
85 explicit constexpr DynamicExtentStorage(SizeType s) noexcept
86 : m_size(s)
87 {}
88
89 public:
90
91 constexpr SizeType size() const noexcept { return m_size; }
92
93 private:
94
95 SizeType m_size;
96};
97
98class ARCCORE_BASE_EXPORT ExtentStorageBase
99{
100 public:
101
102 static void _throwBadSize [[noreturn]] (Int64 wanted_size, Int64 expected_size);
103};
104
106template <typename SizeType, SizeType FixedExtent>
107class ExtentStorage
108{
109 template <typename T, typename SpanSizeType, SpanSizeType SpanExtent>
110 friend class ::Arcane::SpanImpl;
111
112 public:
113
114 explicit constexpr ExtentStorage([[maybe_unused]] SizeType s) noexcept
115 {
116#if defined(ARCCORE_CHECK) && !defined(ARCCORE_DEVICE_CODE)
117 if (s != FixedExtent)
118 ExtentStorageBase::_throwBadSize(s, FixedExtent);
119#endif
120 }
121 ExtentStorage() = default;
122
123 public:
124
125 constexpr SizeType size() const noexcept { return FixedExtent; }
126
127 private:
128
129 static constexpr SizeType m_size = FixedExtent;
130};
131
133template <>
134class ExtentStorage<Int32, DynExtent>
135: public DynamicExtentStorage<Int32>
136{
137 using BaseClass = DynamicExtentStorage<Int32>;
138
139 public:
140
141 explicit constexpr ExtentStorage(Int32 s) noexcept
142 : BaseClass(s)
143 {}
144};
145
147template <>
148class ExtentStorage<Int64, DynExtent>
149: public DynamicExtentStorage<Int64>
150{
151 using BaseClass = DynamicExtentStorage<Int64>;
152
153 public:
154
155 explicit constexpr ExtentStorage(Int64 s) noexcept
156 : BaseClass(s)
157 {}
158};
159
160/*---------------------------------------------------------------------------*/
161/*---------------------------------------------------------------------------*/
162
163} // namespace Arcane::Impl
164
165/*---------------------------------------------------------------------------*/
166/*---------------------------------------------------------------------------*/
167
168namespace Arcane
169{
170
171/*---------------------------------------------------------------------------*/
172/*---------------------------------------------------------------------------*/
173
189template <typename T, typename SizeType, SizeType Extent>
191{
192 using ExtentStorageType = Impl::ExtentStorage<SizeType, Extent>;
193
194 public:
195
196 using ThatClass = SpanImpl<T, SizeType, Extent>;
197 using SubSpanType = SpanImpl<T, SizeType, DynExtent>;
198 using size_type = SizeType;
199 using ElementType = T;
200 using element_type = ElementType;
201 using value_type = typename std::remove_cv_t<ElementType>;
202 using const_value_type = typename std::add_const_t<value_type>;
203 using index_type = SizeType;
204 using difference_type = SizeType;
205 using pointer = ElementType*;
206 using const_pointer = const ElementType*;
207 using reference = ElementType&;
208 using const_reference = const ElementType&;
209 using iterator = ArrayIterator<pointer>;
210 using const_iterator = ArrayIterator<const_pointer>;
211 using view_type = typename Impl::ViewTypeT<ElementType>::view_type;
212 using reverse_iterator = std::reverse_iterator<iterator>;
213 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
214
216 template <typename X>
217 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>>;
218
219 static constexpr bool IsDynamic = (Extent == DynExtent);
220
221 public:
222
224 constexpr ARCCORE_HOST_DEVICE SpanImpl() noexcept
225 : m_ptr(nullptr)
226 , m_size(0)
227 {}
228
230 // For a Span<const T>, it is allowed to construct from a Span<T>
231 template <typename X, SizeType XExtent, typename = std::enable_if_t<std::is_same_v<const X, T>>>
232 constexpr ARCCORE_HOST_DEVICE SpanImpl(const SpanImpl<X, SizeType, XExtent>& from) noexcept
233 : m_ptr(from.data())
234 , m_size(from.size())
235 {}
236
237 template <SizeType XExtent>
238 constexpr ARCCORE_HOST_DEVICE SpanImpl(const SpanImpl<T, SizeType, XExtent>& from) noexcept
239 : m_ptr(from.data())
240 , m_size(from.size())
241 {}
242
245 constexpr ARCCORE_HOST_DEVICE SpanImpl(pointer ptr, SizeType asize) noexcept
246 : m_ptr(ptr)
247 , m_size(asize)
248 {}
249
251 template <std::size_t N, typename X, typename = is_same_const_type<X>>
252 constexpr ARCCORE_HOST_DEVICE SpanImpl(std::array<X, N>& from)
253 : m_ptr(from.data())
254 , m_size(ArraySizeChecker<SizeType>::check(from.size()))
255 {}
256
258 explicit constexpr ARCCORE_HOST_DEVICE SpanImpl(T* ptr) requires(!IsDynamic)
259 : m_ptr(ptr)
260 {}
261
263 template <std::size_t N, typename X, typename = is_same_const_type<X>>
264 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(std::array<X, N>& from)
265 {
266 m_ptr = from.data();
267 m_size = ExtentStorageType(ArraySizeChecker<SizeType>::check(from.size()));
268 return (*this);
269 }
270
271 public:
272
274 // containing \a asize elements.
275 static constexpr ThatClass create(pointer ptr, SizeType asize) noexcept
276 {
277 return ThatClass(ptr, asize);
278 }
279
280 public:
281
287 constexpr ARCCORE_HOST_DEVICE reference operator[](SizeType i) const
288 {
289 ARCCORE_CHECK_AT(i, m_size.m_size);
290 return m_ptr[i];
291 }
292
298 constexpr ARCCORE_HOST_DEVICE reference operator()(SizeType i) const
299 {
300 ARCCORE_CHECK_AT(i, m_size.m_size);
301 return m_ptr[i];
302 }
303
309 constexpr ARCCORE_HOST_DEVICE reference item(SizeType i) const
310 {
311 ARCCORE_CHECK_AT(i, m_size.m_size);
312 return m_ptr[i];
313 }
314
320 constexpr ARCCORE_HOST_DEVICE void setItem(SizeType i, const_reference v) noexcept
321 {
322 ARCCORE_CHECK_AT(i, m_size.m_size);
323 m_ptr[i] = v;
324 }
325
327 constexpr ARCCORE_HOST_DEVICE SizeType size() const noexcept { return m_size.m_size; }
329 constexpr ARCCORE_HOST_DEVICE SizeType sizeBytes() const noexcept
330 {
331 // TODO: always return an Int64
332 return static_cast<SizeType>(m_size.m_size * sizeof(value_type));
333 }
334
335 constexpr ARCCORE_HOST_DEVICE SizeType length() const noexcept { return m_size.m_size; }
336
340 constexpr ARCCORE_HOST_DEVICE iterator begin() const noexcept { return iterator(m_ptr); }
344 constexpr ARCCORE_HOST_DEVICE iterator end() const noexcept { return iterator(m_ptr + m_size.m_size); }
346 constexpr ARCCORE_HOST_DEVICE reverse_iterator rbegin() const noexcept { return std::make_reverse_iterator(end()); }
348 constexpr ARCCORE_HOST_DEVICE reverse_iterator rend() const noexcept { return std::make_reverse_iterator(begin()); }
349
350 public:
351
353 ARCCORE_DEPRECATED_REASON("Y2023: Use begin()/end() instead")
354 ArrayRange<pointer> range() const
355 {
356 return ArrayRange<pointer>(m_ptr, m_ptr + m_size.m_size);
357 }
358
359 public:
360
362 constexpr ARCCORE_HOST_DEVICE pointer ptrAt(SizeType index) const
363 {
364 ARCCORE_CHECK_AT(index, m_size.m_size);
365 return m_ptr + index;
366 }
367
368 // Element at index \a i. Always checks for bounds.
369 constexpr ARCCORE_HOST_DEVICE reference at(SizeType i) const
370 {
371 arccoreCheckAt(i, m_size.m_size);
372 return m_ptr[i];
373 }
374
375 // Sets the element at index \a i. Always checks for bounds.
376 constexpr ARCCORE_HOST_DEVICE void setAt(SizeType i, const_reference value)
377 {
378 arccoreCheckAt(i, m_size.m_size);
379 m_ptr[i] = value;
380 }
381
383 ARCCORE_HOST_DEVICE inline void fill(T o)
384 {
385 for (SizeType i = 0, n = m_size.m_size; i < n; ++i)
386 m_ptr[i] = o;
387 }
388
392 constexpr view_type smallView()
393 {
395 return view_type(s, m_ptr);
396 }
397
406
414 constexpr ARCCORE_HOST_DEVICE SubSpanType subSpan(SizeType abegin, SizeType asize) const
415 {
416 if (abegin >= m_size.m_size)
417 return {};
418 asize = _min(asize, m_size.m_size - abegin);
419 return { m_ptr + abegin, asize };
420 }
421
426 constexpr ARCCORE_HOST_DEVICE SubSpanType subPart(SizeType abegin, SizeType asize) const
427 {
428 return subSpan(abegin, asize);
429 }
430
438 ARCCORE_DEPRECATED_REASON("Y2023: use subSpan() instead")
439 constexpr SubSpanType subView(SizeType abegin, SizeType asize) const
440 {
441 return subSpan(abegin, asize);
442 }
443
445 constexpr ARCCORE_HOST_DEVICE SubSpanType subspan(SizeType abegin, SizeType asize) const
446 {
447 return subSpan(abegin, asize);
448 }
449
451 ARCCORE_DEPRECATED_REASON("Y2023: use subSpanInterval() instead")
452 constexpr SubSpanType subViewInterval(SizeType index, SizeType nb_interval) const
453 {
454 return impl::subViewInterval<ThatClass>(*this, index, nb_interval);
455 }
456
458 constexpr SubSpanType subSpanInterval(SizeType index, SizeType nb_interval) const
459 {
460 return impl::subViewInterval<ThatClass>(*this, index, nb_interval);
461 }
462
464 constexpr SubSpanType subPartInterval(SizeType index, SizeType nb_interval) const
465 {
466 return impl::subViewInterval<ThatClass>(*this, index, nb_interval);
467 }
468
477 template <class U> ARCCORE_HOST_DEVICE void copy(const U& copy_array)
478 {
479 Int64 n = copy_array.size();
480 Int64 size_as_int64 = m_size.m_size;
481 arccoreCheckAt(n, size_as_int64 + 1);
482 const_pointer copy_begin = copy_array.data();
483 pointer to_ptr = m_ptr;
484 // We are sure that \a fits into a 'SizeType' because it is smaller
485 // than \a m_size
486 SizeType n_as_sizetype = static_cast<SizeType>(n);
487 for (SizeType i = 0; i < n_as_sizetype; ++i)
488 to_ptr[i] = copy_begin[i];
489 }
490
492 constexpr ARCCORE_HOST_DEVICE bool empty() const noexcept { return m_size.m_size == 0; }
494 ARCCORE_HOST_DEVICE bool contains(const_reference v) const
495 {
496 for (SizeType i = 0; i < m_size.m_size; ++i) {
497 if (m_ptr[i] == v)
498 return true;
499 }
500 return false;
501 }
502
509 std::optional<SizeType> findFirst(const_reference v) const
510 {
511 for (SizeType i = 0; i < m_size.m_size; ++i) {
512 if (m_ptr[i] == v)
513 return i;
514 }
515 return std::nullopt;
516 }
517
518 public:
519
520 constexpr ARCCORE_HOST_DEVICE void setArray(const ArrayView<T>& v) noexcept
521 {
522 m_ptr = v.m_ptr;
523 m_size = v.m_size;
524 }
525 constexpr ARCCORE_HOST_DEVICE void setArray(const Span<T>& v) noexcept
526 {
527 m_ptr = v.m_ptr;
528 m_size = v.m_size;
529 }
530
539 constexpr ARCCORE_HOST_DEVICE pointer data() const noexcept { return m_ptr; }
540
542 template <typename X, SizeType Extent2, typename = std::enable_if_t<std::is_same_v<X, value_type>>> friend bool
544 {
545 return impl::areEqual(SpanImpl<T, SizeType>(rhs), SpanImpl<T, SizeType>(lhs));
546 }
547
549 template <typename X, SizeType Extent2, typename = std::enable_if_t<std::is_same_v<X, value_type>>> friend bool
551 {
552 return !operator==(rhs, lhs);
553 }
554
556 template <SizeType Extent2> friend bool
558 {
559 return impl::areEqual(SpanImpl<T, SizeType>(rhs), SpanImpl<T, SizeType>(lhs));
560 }
561
563 template <SizeType Extent2> friend bool
565 {
566 return !operator==(rhs, lhs);
567 }
568
569 friend inline std::ostream& operator<<(std::ostream& o, const ThatClass& val)
570 {
571 impl::dumpArray(o, Span<const T, DynExtent>(val.data(), val.size()), 500);
572 return o;
573 }
574
575 protected:
576
583 constexpr void _setArray(pointer v, SizeType s) noexcept
584 {
585 m_ptr = v;
586 m_size = s;
587 }
588
595 constexpr void _setPtr(pointer v) noexcept { m_ptr = v; }
596
603 constexpr void _setSize(SizeType s) noexcept { m_size = ExtentStorageType(s); }
604
605 private:
606
607 pointer m_ptr;
609 ARCCORE_NO_UNIQUE_ADDRESS ExtentStorageType m_size;
610
611 private:
612
613 static constexpr SizeType _min(SizeType a, SizeType b)
614 {
615 return ((a < b) ? a : b);
616 }
617};
618
619/*---------------------------------------------------------------------------*/
620/*---------------------------------------------------------------------------*/
621
632template <typename T, Int64 Extent>
633class Span
634: public SpanImpl<T, Int64, Extent>
635{
636 public:
637
638 using ThatClass = Span<T, Extent>;
639 using BaseClass = SpanImpl<T, Int64, Extent>;
640 using size_type = Int64;
641 using value_type = typename BaseClass::value_type;
642 using pointer = typename BaseClass::pointer;
643 template <typename X>
644 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>>;
645 static constexpr bool IsDynamic = (Extent == DynExtent);
646
647 public:
648
650 Span() = default;
652 constexpr ARCCORE_HOST_DEVICE Span(const ArrayView<value_type>& from) noexcept
653 : BaseClass(from.m_ptr, from.m_size)
654 {}
655 // Constructor from a ConstArrayView. This is only allowed
656 // if T is const.
657 template <typename X, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
658 constexpr ARCCORE_HOST_DEVICE Span(const ConstArrayView<X>& from) noexcept
659 : BaseClass(from.m_ptr, from.m_size)
660 {}
661 // For a Span<const T>, we are allowed to construct from a Span<T>
662 template <typename X, Int64 XExtent, typename = std::enable_if_t<std::is_same_v<const X, T>>>
663 constexpr ARCCORE_HOST_DEVICE Span(const Span<X, XExtent>& from) noexcept
664 : BaseClass(from)
665 {}
666 // For a Span<const T>, we are allowed to construct from a SmallSpan<T>
667 template <typename X, Int32 XExtent, typename = std::enable_if_t<std::is_same_v<const X, T>>>
668 constexpr ARCCORE_HOST_DEVICE Span(const SmallSpan<X, XExtent>& from) noexcept
669 : BaseClass(from.data(), from.size())
670 {}
671 template <Int64 XExtent>
672 constexpr ARCCORE_HOST_DEVICE Span(const SpanImpl<T, Int64, XExtent>& from) noexcept
673 : BaseClass(from)
674 {}
675 template <Int32 XExtent>
676 constexpr ARCCORE_HOST_DEVICE Span(const SpanImpl<T, Int32, XExtent>& from) noexcept
677 : BaseClass(from.data(), from.size())
678 {}
679
682 constexpr ARCCORE_HOST_DEVICE Span(pointer ptr, Int64 asize) noexcept
683 : BaseClass(ptr, asize)
684 {}
685
687 template <std::size_t N, typename X, typename = is_same_const_type<X>>
688 constexpr ARCCORE_HOST_DEVICE Span(std::array<X, N>& from) noexcept
689 : BaseClass(from.data(), from.size())
690 {}
691
693 explicit constexpr ARCCORE_HOST_DEVICE Span(T* ptr) requires(!IsDynamic)
694 : BaseClass(ptr)
695 {}
696
698 template <std::size_t N, typename X, typename = is_same_const_type<X>>
699 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(std::array<X, N>& from) noexcept
700 {
701 this->_setPtr(from.data());
702 this->_setSize(from.size());
703 return (*this);
704 }
705
706 public:
707
709 // containing \a asize elements.
710 static constexpr ThatClass create(pointer ptr, size_type asize) noexcept
711 {
712 return ThatClass(ptr, asize);
713 }
714
715 public:
716
724 constexpr ARCCORE_HOST_DEVICE Span<T, DynExtent> subspan(Int64 abegin, Int64 asize) const
725 {
726 return BaseClass::subspan(abegin, asize);
727 }
728
736 constexpr ARCCORE_HOST_DEVICE Span<T, DynExtent> subSpan(Int64 abegin, Int64 asize) const
737 {
738 return BaseClass::subSpan(abegin, asize);
739 }
740
748 constexpr ARCCORE_HOST_DEVICE Span<T, DynExtent> subPart(Int64 abegin, Int64 asize) const
749 {
750 return BaseClass::subPart(abegin, asize);
751 }
752
754 constexpr ARCCORE_HOST_DEVICE Span<T, DynExtent> subSpanInterval(Int64 index, Int64 nb_interval) const
755 {
756 return impl::subViewInterval<ThatClass>(*this, index, nb_interval);
757 }
758
760 constexpr ARCCORE_HOST_DEVICE Span<T, DynExtent> subPartInterval(Int64 index, Int64 nb_interval) const
761 {
762 return impl::subViewInterval<ThatClass>(*this, index, nb_interval);
763 }
764
772 ARCCORE_DEPRECATED_REASON("Y2023: use subSpan() instead")
773 constexpr ARCCORE_HOST_DEVICE Span<T> subView(Int64 abegin, Int64 asize) const
774 {
775 return subspan(abegin, asize);
776 }
777
779 ARCCORE_DEPRECATED_REASON("Y2023: use subSpanInterval() instead")
780 constexpr ARCCORE_HOST_DEVICE Span<T> subViewInterval(Int64 index, Int64 nb_interval) const
781 {
782 return impl::subViewInterval<ThatClass>(*this, index, nb_interval);
783 }
784};
785
786/*---------------------------------------------------------------------------*/
787/*---------------------------------------------------------------------------*/
788
802template <typename T, Int32 Extent>
804: public SpanImpl<T, Int32, Extent>
805{
806 public:
807
808 using ThatClass = SmallSpan<T, Extent>;
809 using BaseClass = SpanImpl<T, Int32, Extent>;
810 using size_type = Int32;
811 using value_type = typename BaseClass::value_type;
812 using pointer = typename BaseClass::pointer;
813 template <typename X>
814 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>>;
815 static constexpr bool IsDynamic = (Extent == DynExtent);
816
817 public:
818
820 SmallSpan() = default;
821
823 constexpr ARCCORE_HOST_DEVICE SmallSpan(const ArrayView<value_type>& from) noexcept
824 : BaseClass(from.m_ptr, from.m_size)
825 {}
826
827 // Constructor from a ConstArrayView. This is only allowed
828 // if T is const.
829 template <typename X, typename = std::enable_if_t<std::is_same<X, value_type>::value>>
830 constexpr ARCCORE_HOST_DEVICE SmallSpan(const ConstArrayView<X>& from) noexcept
831 : BaseClass(from.m_ptr, from.m_size)
832 {}
833
834 // For a Span<const T>, we are allowed to construct from a Span<T>
835 template <typename X, typename = std::enable_if_t<std::is_same<X, value_type>::value>>
836 constexpr ARCCORE_HOST_DEVICE SmallSpan(const SmallSpan<X>& from) noexcept
837 : BaseClass(from)
838 {}
839
840 template <Int32 XExtent>
841 constexpr ARCCORE_HOST_DEVICE SmallSpan(const SpanImpl<T, Int32, XExtent>& from) noexcept
842 : BaseClass(from)
843 {}
844
847 constexpr ARCCORE_HOST_DEVICE SmallSpan(pointer ptr, Int32 asize) noexcept
848 : BaseClass(ptr, asize)
849 {}
850
851 template <std::size_t N, typename X, typename = is_same_const_type<X>>
852 constexpr ARCCORE_HOST_DEVICE SmallSpan(std::array<X, N>& from)
853 : BaseClass(from)
854 {}
855
857 explicit constexpr ARCCORE_HOST_DEVICE SmallSpan(T* ptr) requires(!IsDynamic)
858 : BaseClass(ptr)
859 {}
860
862 template <std::size_t N, typename X, typename = is_same_const_type<X>>
863 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(std::array<X, N>& from)
864 {
866 return (*this);
867 }
868
869 public:
870
872 // containing \a asize elements.
873 static constexpr ThatClass create(pointer ptr, size_type asize) noexcept
874 {
875 return ThatClass(ptr, asize);
876 }
877
878 public:
879
887 constexpr ARCCORE_HOST_DEVICE SmallSpan<T, DynExtent> subspan(Int32 abegin, Int32 asize) const
888 {
889 return BaseClass::subspan(abegin, asize);
890 }
891
899 constexpr ARCCORE_HOST_DEVICE SmallSpan<T, DynExtent> subSpan(Int32 abegin, Int32 asize) const
900 {
901 return BaseClass::subSpan(abegin, asize);
902 }
903
911 constexpr ARCCORE_HOST_DEVICE SmallSpan<T, DynExtent> subPart(Int32 abegin, Int32 asize) const
912 {
913 return BaseClass::subSpan(abegin, asize);
914 }
915
917 constexpr ARCCORE_HOST_DEVICE SmallSpan<T, DynExtent> subSpanInterval(Int32 index, Int32 nb_interval) const
918 {
919 return impl::subViewInterval<ThatClass>(*this, index, nb_interval);
920 }
921
923 constexpr ARCCORE_HOST_DEVICE ThatClass subPartInterval(Int32 index, Int32 nb_interval) const
924 {
925 return subSpanInterval(index, nb_interval);
926 }
927
935 ARCCORE_DEPRECATED_REASON("Y2023: use subPart() instead")
936 constexpr ARCCORE_HOST_DEVICE SmallSpan<T> subView(Int32 abegin, Int32 asize) const
937 {
938 return subspan(abegin, asize);
939 }
940
942 ARCCORE_DEPRECATED_REASON("Y2023: use subPartInterval() instead")
943 constexpr ARCCORE_HOST_DEVICE SmallSpan<T> subViewInterval(Int32 index, Int32 nb_interval) const
944 {
945 return impl::subViewInterval<ThatClass>(*this, index, nb_interval);
946 }
947};
948
949/*---------------------------------------------------------------------------*/
950/*---------------------------------------------------------------------------*/
951
960template <typename T, typename SizeType> inline void
961dumpArray(std::ostream& o, SpanImpl<const T, SizeType> val, int max_print)
962{
963 impl::dumpArray(o, val, max_print);
964}
965
966/*---------------------------------------------------------------------------*/
967/*---------------------------------------------------------------------------*/
968
977template <typename DataType, typename IntegerType, typename SizeType> inline void
981{
982 const Int64 result_size = indexes.size();
983 [[maybe_unused]] const Int64 my_size = values.size();
984 const DataType* ptr = values.data();
985 for (Int64 i = 0; i < result_size; ++i) {
986 IntegerType index = indexes[i];
987 ARCCORE_CHECK_AT(index, my_size);
988 result[i] = ptr[index];
989 }
990}
991
992/*---------------------------------------------------------------------------*/
993/*---------------------------------------------------------------------------*/
994
1003template <typename DataType> inline void
1005{
1006 _sampleSpan<DataType, Int64, Int64>(values, indexes, result);
1007}
1008
1009/*---------------------------------------------------------------------------*/
1010/*---------------------------------------------------------------------------*/
1011
1018template <typename DataType> inline void
1020{
1021 _sampleSpan<DataType, Int32, Int64>(values, indexes, result);
1022}
1023
1024/*---------------------------------------------------------------------------*/
1025/*---------------------------------------------------------------------------*/
1026
1030template <typename DataType, typename SizeType, SizeType Extent>
1031inline typename Impl::SpanTypeFromSize<const std::byte, SizeType>::SpanType
1033{
1034 return { reinterpret_cast<const std::byte*>(s.data()), s.sizeBytes() };
1035}
1036
1040template <typename DataType>
1041inline SmallSpan<const std::byte>
1043{
1044 return asBytes(SmallSpan<DataType>(s));
1045}
1046
1050template <typename DataType>
1051inline SmallSpan<const std::byte>
1056
1057/*---------------------------------------------------------------------------*/
1058/*---------------------------------------------------------------------------*/
1059
1065template <typename DataType, typename SizeType, SizeType Extent,
1066 typename std::enable_if_t<!std::is_const<DataType>::value, int> = 0>
1067inline typename Impl::SpanTypeFromSize<std::byte, SizeType>::SpanType
1069{
1070 return { reinterpret_cast<std::byte*>(s.data()), s.sizeBytes() };
1071}
1072
1078template <typename DataType> inline SmallSpan<std::byte>
1083
1084/*---------------------------------------------------------------------------*/
1085/*---------------------------------------------------------------------------*/
1086
1087namespace impl
1088{
1089
1090 template <typename ByteType, typename DataType, Int64 Extent> inline Span<DataType>
1091 asSpanInternal(Span<ByteType, Extent> bytes)
1092 {
1093 Int64 size = bytes.size();
1094 if (size == 0)
1095 return {};
1096 static constexpr Int64 data_type_size = static_cast<Int64>(sizeof(DataType));
1097 static_assert(data_type_size > 0, "Bad datatype size");
1098 ARCCORE_ASSERT((size % data_type_size) == 0, ("Size is not a multiple of sizeof(DataType)"));
1099 auto* ptr = reinterpret_cast<DataType*>(bytes.data());
1100 return { ptr, size / data_type_size };
1101 }
1102
1103 template <typename ByteType, typename DataType, Int32 Extent> inline SmallSpan<DataType>
1104 asSmallSpanInternal(SmallSpan<ByteType, Extent> bytes)
1105 {
1106 Int32 size = bytes.size();
1107 if (size == 0)
1108 return {};
1109 static constexpr Int32 data_type_size = static_cast<Int32>(sizeof(DataType));
1110 static_assert(data_type_size > 0, "Bad datatype size");
1111 ARCCORE_ASSERT((size % data_type_size) == 0, ("Size is not a multiple of sizeof(DataType)"));
1112 auto* ptr = reinterpret_cast<DataType*>(bytes.data());
1113 return { ptr, size / data_type_size };
1114 }
1115
1116} // namespace impl
1117
1118/*---------------------------------------------------------------------------*/
1119/*---------------------------------------------------------------------------*/
1120
1125template <typename DataType, Int64 Extent> inline Span<DataType>
1127{
1128 return impl::asSpanInternal<std::byte, DataType, Extent>(bytes);
1129}
1130
1135template <typename DataType, Int64 Extent> inline Span<const DataType>
1137{
1138 return impl::asSpanInternal<const std::byte, const DataType, Extent>(bytes);
1139}
1140
1145template <typename DataType, Int32 Extent> inline SmallSpan<DataType>
1147{
1148 return impl::asSmallSpanInternal<std::byte, DataType, Extent>(bytes);
1149}
1150
1155template <typename DataType, Int32 Extent> inline SmallSpan<const DataType>
1157{
1158 return impl::asSmallSpanInternal<const std::byte, const DataType, Extent>(bytes);
1159}
1160
1161/*---------------------------------------------------------------------------*/
1162/*---------------------------------------------------------------------------*/
1163
1167template <typename DataType, size_t SizeType> inline Span<DataType, SizeType>
1168asSpan(std::array<DataType, SizeType>& s)
1169{
1170 Int64 size = static_cast<Int64>(s.size());
1171 return { s.data(), size };
1172}
1173
1177template <typename DataType, size_t SizeType> inline SmallSpan<DataType, SizeType>
1178asSmallSpan(std::array<DataType, SizeType>& s)
1179{
1180 Int32 size = static_cast<Int32>(s.size());
1181 return { s.data(), size };
1182}
1183
1184/*---------------------------------------------------------------------------*/
1185/*---------------------------------------------------------------------------*/
1186
1192extern "C++" ARCCORE_BASE_EXPORT void
1193binaryWrite(std::ostream& ostr, const Span<const std::byte>& bytes);
1194
1200extern "C++" ARCCORE_BASE_EXPORT void
1201binaryRead(std::istream& istr, const Span<std::byte>& bytes);
1202
1203/*---------------------------------------------------------------------------*/
1204/*---------------------------------------------------------------------------*/
1205
1206} // namespace Arcane
1207
1208/*---------------------------------------------------------------------------*/
1209/*---------------------------------------------------------------------------*/
1210
1211namespace Arccore
1212{
1213using Arcane::asBytes;
1215using Arcane::asSpan;
1217using Arcane::binaryRead;
1219using Arcane::sampleSpan;
1220} // namespace Arccore
1221
1222/*---------------------------------------------------------------------------*/
1223/*---------------------------------------------------------------------------*/
1224
1225#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:108
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:805
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:887
constexpr __host__ __device__ SmallSpan< const ConstituentItemType_ > subViewInterval(Int32 index, Int32 nb_interval) const
Definition Span.h:943
constexpr __host__ __device__ ThatClass & operator=(std::array< X, N > &from)
Copy assignment operator.
Definition Span.h:863
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:917
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:911
SmallSpan()=default
Constructs an empty view.
constexpr __host__ __device__ SmallSpan(const ArrayView< value_type > &from) noexcept
Copy constructor from another view.
Definition Span.h:823
constexpr __host__ __device__ SmallSpan(pointer ptr, Int32 asize) noexcept
Definition Span.h:847
constexpr __host__ __device__ SmallSpan< const ConstituentItemType_ > subView(Int32 abegin, Int32 asize) const
Definition Span.h:936
constexpr __host__ __device__ SmallSpan(T *ptr)
Constructs a view from a pointer with a fixed size.
Definition Span.h:857
static constexpr ThatClass create(pointer ptr, size_type asize) noexcept
Constructs a view over a memory region starting at ptr and.
Definition Span.h:873
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:899
constexpr __host__ __device__ ThatClass subPartInterval(Int32 index, Int32 nb_interval) const
Sub-view corresponding to the interval index over nb_interval.
Definition Span.h:923
View of an array of elements of type T.
Definition Span.h:191
constexpr __host__ __device__ SpanImpl(pointer ptr, SizeType asize) noexcept
Definition Span.h:245
constexpr __host__ __device__ SpanImpl(const SpanImpl< X, SizeType, XExtent > &from) noexcept
Copy constructor from another view.
Definition Span.h:232
constexpr view_type smallView()
Constant view of this view.
Definition Span.h:392
__host__ __device__ void copy(const U &copy_array)
Copies the array copy_array into the instance.
Definition Span.h:477
constexpr SubSpanType subView(SizeType abegin, SizeType asize) const
Definition Span.h:439
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:543
constexpr __host__ __device__ SpanImpl(std::array< X, N > &from)
Constructs a view from a std::array.
Definition Span.h:252
std::optional< SizeType > findFirst(const_reference v) const
Definition Span.h:509
constexpr void _setArray(pointer v, SizeType s) noexcept
Modifies the array pointer and size.
Definition Span.h:583
constexpr __host__ __device__ SpanImpl(T *ptr)
Constructs a view from a pointer with a fixed size.
Definition Span.h:258
constexpr __host__ __device__ pointer data() const noexcept
Definition Span.h:539
constexpr __host__ __device__ SizeType length() const noexcept
Number of elements in the array.
Definition Span.h:335
constexpr __host__ __device__ iterator begin() const noexcept
Iterator for the first element of the array.
Definition Span.h:340
__host__ __device__ void fill(T o)
Fills the array with the value o.
Definition Span.h:383
constexpr __host__ __device__ pointer ptrAt(SizeType index) const
Address of the index-th element.
Definition Span.h:362
constexpr __host__ __device__ reference operator()(SizeType i) const
i-th element of the array.
Definition Span.h:298
constexpr __host__ __device__ reference operator[](SizeType i) const
i-th element of the array.
Definition Span.h:287
ArrayRange< pointer > range() const
Definition Span.h:354
constexpr __host__ __device__ bool empty() const noexcept
Returns true if the array is empty (zero dimension).
Definition Span.h:492
ARCCORE_NO_UNIQUE_ADDRESS ExtentStorageType m_size
Definition Span.h:609
constexpr SubSpanType subViewInterval(SizeType index, SizeType nb_interval) const
Definition Span.h:452
constexpr __host__ __device__ SubSpanType subSpan(SizeType abegin, SizeType asize) const
Sub-view starting from element abegin and containing asize elements.
Definition Span.h:414
constexpr __host__ __device__ reference item(SizeType i) const
i-th element of the array.
Definition Span.h:309
constexpr ConstArrayView< value_type > constSmallView() const
Constant view of this view.
Definition Span.h:401
friend bool operator==(const SpanImpl< T, SizeType, Extent > &rhs, const SpanImpl< T, SizeType, Extent2 > &lhs)
Equality operator.
Definition Span.h:557
constexpr SubSpanType subPartInterval(SizeType index, SizeType nb_interval) const
Sub-view corresponding to the interval index over nb_interval.
Definition Span.h:464
__host__ __device__ bool contains(const_reference v) const
Returns true if the array contains the element with value v.
Definition Span.h:494
constexpr __host__ __device__ ThatClass & operator=(std::array< X, N > &from)
Copy assignment operator.
Definition Span.h:264
constexpr __host__ __device__ iterator end() const noexcept
Iterator for the element after the end of the array.
Definition Span.h:344
constexpr void _setPtr(pointer v) noexcept
Modifies the array start pointer.
Definition Span.h:595
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:550
constexpr __host__ __device__ reverse_iterator rend() const noexcept
Reverse iterator for the element after the end of the array.
Definition Span.h:348
constexpr __host__ __device__ SizeType size() const noexcept
Definition Span.h:327
constexpr __host__ __device__ SizeType sizeBytes() const noexcept
Returns the size of the array in bytes.
Definition Span.h:329
constexpr __host__ __device__ reverse_iterator rbegin() const noexcept
Reverse iterator for the first element of the array.
Definition Span.h:346
friend bool operator!=(const SpanImpl< T, SizeType, Extent > &rhs, const SpanImpl< T, SizeType, Extent2 > &lhs)
Inequality operator.
Definition Span.h:564
constexpr void _setSize(SizeType s) noexcept
Modifies the array size.
Definition Span.h:603
constexpr __host__ __device__ SubSpanType subspan(SizeType abegin, SizeType asize) const
For C++20 compatibility.
Definition Span.h:445
constexpr __host__ __device__ SubSpanType subPart(SizeType abegin, SizeType asize) const
Sub-view starting from element abegin and containing asize elements.
Definition Span.h:426
constexpr __host__ __device__ SpanImpl() noexcept
Constructs an empty view.
Definition Span.h:224
constexpr SubSpanType subSpanInterval(SizeType index, SizeType nb_interval) const
Sub-view corresponding to the interval index over nb_interval.
Definition Span.h:458
constexpr __host__ __device__ void setItem(SizeType i, const_reference v) noexcept
Sets the i-th element of the array.
Definition Span.h:320
static constexpr ThatClass create(pointer ptr, SizeType asize) noexcept
Constructs a view on a memory region starting at ptr and.
Definition Span.h:275
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:217
View of an array of elements of type T.
Definition Span.h:635
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:748
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:754
static constexpr ThatClass create(pointer ptr, size_type asize) noexcept
Constructs a view on a memory area starting at ptr and.
Definition Span.h:710
constexpr __host__ __device__ Span(std::array< X, N > &from) noexcept
Constructs a view from a std::array.
Definition Span.h:688
constexpr __host__ __device__ Span< DataType > subView(Int64 abegin, Int64 asize) const
Definition Span.h:773
constexpr __host__ __device__ Span(pointer ptr, Int64 asize) noexcept
Definition Span.h:682
constexpr __host__ __device__ ThatClass & operator=(std::array< X, N > &from) noexcept
Copy assignment operator.
Definition Span.h:699
constexpr __host__ __device__ Span(T *ptr)
Constructs a view from a pointer with a fixed size.
Definition Span.h:693
constexpr __host__ __device__ Span(const ArrayView< value_type > &from) noexcept
Copy constructor from another view.
Definition Span.h:652
Span()=default
Constructs an empty view.
constexpr __host__ __device__ Span< DataType > subViewInterval(Int64 index, Int64 nb_interval) const
Definition Span.h:780
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:760
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:736
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:724
-- 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:54
__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:1004
SmallSpan< DataType > asSmallSpan(SmallSpan< std::byte, Extent > bytes)
Converts a SmallSpan<std::byte> into a SmallSpan<DataType>.
Definition Span.h:1146
Span< DataType > asSpan(Span< std::byte, Extent > bytes)
Converts a Span<std::byte> into a Span<DataType>.
Definition Span.h:1126
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:1032
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:1068
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:978
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