Debian Codegraph @

src/include/xgboost/span.h

raw symbols SCIP
1 /**
2 * Copyright 2018-2025, XGBoost contributors
3 * \brief span class based on ISO++20 span
4 *
5 * About NOLINTs in this file:
6 *
7 * If we want Span to work with std interface, like range for loop, the
8 * naming must be consistent with std, not XGBoost. Also, the interface also
9 * conflicts with XGBoost coding style, specifically, the use of `explicit'
10 * keyword.
11 *
12 *
13 * Some of the code is copied from Guidelines Support Library, here is the
14 * license:
15 *
16 * Copyright (c) 2015 Microsoft Corporation. All rights reserved.
17 *
18 * This code is licensed under the MIT License (MIT).
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
27 */
28
29 #ifndef XGBOOST_SPAN_H_
30 #define XGBOOST_SPAN_H_
31
32 #include <xgboost/base.h>
33
34 #include <cstddef> // size_t
35 #include <cstdio>
36 #include <iterator>
37 #include <limits> // numeric_limits
38 #include <type_traits>
39 #include <utility> // for move
40 #include <vector> // for vector
41
42 #if defined(__CUDACC__)
43 #include <cuda_runtime.h>
44 #endif // defined(__CUDACC__)
45
46 /*!
47 * The version number 1910 is picked up from GSL.
48 *
49 * We might want to use MOODYCAMEL_NOEXCEPT from dmlc/concurrentqueue.h. But
50 * there are a lot more definitions in that file would cause warnings/troubles
51 * in MSVC 2013. Currently we try to keep the closure of Span as minimal as
52 * possible.
53 *
54 * There are other workarounds for MSVC, like _Unwrapped, _Verify_range ...
55 * Some of these are hidden magics of MSVC and I tried to avoid them. Should any
56 * of them become needed, please consult the source code of GSL, and possibly
57 * some explanations from this thread:
58 *
59 * https://github.com/Microsoft/GSL/pull/664
60 *
61 * TODO(trivialfis): Group these MSVC workarounds into a manageable place.
62 */
63 #if defined(_MSC_VER) && _MSC_VER < 1910
64
65 #define __span_noexcept
66
67 #pragma push_macro("constexpr")
68 #define constexpr /*constexpr*/
69
70 #else
71
72 #define __span_noexcept noexcept
73
74 #endif // defined(_MSC_VER) && _MSC_VER < 1910
75
76 namespace xgboost::common {
77
78 #if defined(__CUDA_ARCH__)
79 // Usual logging facility is not available inside device code.
80
81 #if defined(_MSC_VER)
82
83 // Windows CUDA doesn't have __assert_fail.
84 #define CUDA_KERNEL_CHECK(cond) \
85 do { \
86 if (XGBOOST_EXPECT(!(cond), false)) { \
87 asm("trap;"); \
88 } \
89 } while (0)
90
91 #else // defined(_MSC_VER)
92
93 #define __ASSERT_STR_HELPER(x) #x
94
95 #define CUDA_KERNEL_CHECK(cond) \
96 (XGBOOST_EXPECT((cond), true) \
97 ? static_cast<void>(0) \
98 : __assert_fail(__ASSERT_STR_HELPER((cond)), __FILE__, __LINE__, __PRETTY_FUNCTION__))
99
100 #endif // defined(_MSC_VER)
101
102 #define KERNEL_CHECK CUDA_KERNEL_CHECK
103
104 #define SPAN_CHECK KERNEL_CHECK
105
106 #else // ------------------------------ not CUDA ----------------------------
107
108 #if defined(XGBOOST_STRICT_R_MODE) && XGBOOST_STRICT_R_MODE == 1
109
110 #define KERNEL_CHECK(cond)
111
112 #define SPAN_CHECK(cond) KERNEL_CHECK(cond)
113
114 #else
115
116 #if defined(__SYCL_DEVICE_ONLY__)
117
118 // SYCL doesn't support termination
119 #define SYCL_KERNEL_CHECK(cond)
120
121 #define KERNEL_CHECK(cond) SYCL_KERNEL_CHECK(cond)
122
123 #else // defined(__SYCL_DEVICE_ONLY__)
124 #define KERNEL_CHECK(cond) (XGBOOST_EXPECT((cond), true) ? static_cast<void>(0) : std::terminate())
125 #endif // defined(__SYCL_DEVICE_ONLY__)
126
127 #define SPAN_CHECK(cond) KERNEL_CHECK(cond)
128
129 #endif // defined(XGBOOST_STRICT_R_MODE)
130
131 #endif // __CUDA_ARCH__
132
133 #define SPAN_LT(lhs, rhs) SPAN_CHECK((lhs) < (rhs))
134
135 namespace detail {
136 /*!
137 * By default, XGBoost uses uint32_t for indexing data. int64_t covers all
138 * values uint32_t can represent. Also, On x86-64 Linux, GCC uses long int to
139 * represent ptrdiff_t, which is just int64_t. So we make it deterministic
140 * here.
141 */
142 using ptrdiff_t = typename std::conditional_t< // NOLINT
143 std::is_same_v<std::ptrdiff_t, std::int64_t>, std::ptrdiff_t, std::int64_t>;
144 } // namespace detail
145
146 #if defined(_MSC_VER) && _MSC_VER < 1910
147 constexpr const std::size_t
148 dynamic_extent = std::numeric_limits<std::size_t>::max(); // NOLINT
149 #else
150 constexpr std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max(); // NOLINT
151 #endif // defined(_MSC_VER) && _MSC_VER < 1910
152
153 enum class byte : unsigned char {}; // NOLINT
154
155 template <class ElementType, std::size_t Extent>
156 class Span;
157
158 namespace detail {
159
160 template <typename SpanType, bool IsConst>
161 class SpanIterator {
162 using ElementType = typename SpanType::element_type;
163
164 public:
165 using iterator_category = std::random_access_iterator_tag; // NOLINT
166 using value_type = typename SpanType::value_type; // NOLINT
167 using difference_type = detail::ptrdiff_t; // NOLINT
168
169 using reference = typename std::conditional< // NOLINT
170 IsConst, const ElementType, ElementType>::type&;
171 using pointer = typename std::add_pointer<reference>::type; // NOLINT
172
173 constexpr SpanIterator() = default;
174
175 XGBOOST_DEVICE constexpr SpanIterator(
176 const SpanType* _span,
177 typename SpanType::index_type _idx) __span_noexcept :
178 span_(_span), index_(_idx) {}
179
180 friend SpanIterator<SpanType, true>;
181 template <bool B, typename std::enable_if_t<!B && IsConst>* = nullptr>
182 XGBOOST_DEVICE constexpr SpanIterator( // NOLINT
183 const SpanIterator<SpanType, B>& other_) __span_noexcept
184 : SpanIterator(other_.span_, other_.index_) {}
185
186 XGBOOST_DEVICE reference operator*() const {
187 SPAN_CHECK(index_ < span_->size());
188 return *(span_->data() + index_);
189 }
190 XGBOOST_DEVICE reference operator[](difference_type n) const {
191 return *(*this + n);
192 }
193
194 XGBOOST_DEVICE pointer operator->() const {
195 SPAN_CHECK(index_ != span_->size());
196 return span_->data() + index_;
197 }
198
199 XGBOOST_DEVICE SpanIterator& operator++() {
200 SPAN_CHECK(index_ != span_->size());
201 index_++;
202 return *this;
203 }
204
205 XGBOOST_DEVICE SpanIterator operator++(int) {
206 auto ret = *this;
207 ++(*this);
208 return ret;
209 }
210
211 XGBOOST_DEVICE SpanIterator& operator--() {
212 SPAN_CHECK(index_ != 0 && index_ <= span_->size());
213 index_--;
214 return *this;
215 }
216
217 XGBOOST_DEVICE SpanIterator operator--(int) {
218 auto ret = *this;
219 --(*this);
220 return ret;
221 }
222
223 XGBOOST_DEVICE SpanIterator operator+(difference_type n) const {
224 auto ret = *this;
225 return ret += n;
226 }
227
228 XGBOOST_DEVICE SpanIterator& operator+=(difference_type n) {
229 SPAN_CHECK((index_ + n) <= span_->size());
230 index_ += n;
231 return *this;
232 }
233
234 XGBOOST_DEVICE difference_type operator-(SpanIterator rhs) const {
235 SPAN_CHECK(span_ == rhs.span_);
236 return index_ - rhs.index_;
237 }
238
239 XGBOOST_DEVICE SpanIterator operator-(difference_type n) const {
240 auto ret = *this;
241 return ret -= n;
242 }
243
244 XGBOOST_DEVICE SpanIterator& operator-=(difference_type n) {
245 return *this += -n;
246 }
247
248 // friends
249 XGBOOST_DEVICE constexpr friend bool operator==(
250 SpanIterator _lhs, SpanIterator _rhs) __span_noexcept {
251 return _lhs.span_ == _rhs.span_ && _lhs.index_ == _rhs.index_;
252 }
253
254 XGBOOST_DEVICE constexpr friend bool operator!=(
255 SpanIterator _lhs, SpanIterator _rhs) __span_noexcept {
256 return !(_lhs == _rhs);
257 }
258
259 XGBOOST_DEVICE constexpr friend bool operator<(
260 SpanIterator _lhs, SpanIterator _rhs) __span_noexcept {
261 return _lhs.index_ < _rhs.index_;
262 }
263
264 XGBOOST_DEVICE constexpr friend bool operator<=(
265 SpanIterator _lhs, SpanIterator _rhs) __span_noexcept {
266 return !(_rhs < _lhs);
267 }
268
269 XGBOOST_DEVICE constexpr friend bool operator>(
270 SpanIterator _lhs, SpanIterator _rhs) __span_noexcept {
271 return _rhs < _lhs;
272 }
273
274 XGBOOST_DEVICE constexpr friend bool operator>=(
275 SpanIterator _lhs, SpanIterator _rhs) __span_noexcept {
276 return !(_rhs > _lhs);
277 }
278
279 protected:
280 const SpanType *span_ { nullptr };
281 typename SpanType::index_type index_ { 0 };
282 };
283
284
285 // It's tempting to use constexpr instead of structs to do the following meta
286 // programming. But remember that we are supporting MSVC 2013 here.
287
288 /*!
289 * The extent E of the span returned by subspan is determined as follows:
290 *
291 * - If Count is not dynamic_extent, Count;
292 * - Otherwise, if Extent is not dynamic_extent, Extent - Offset;
293 * - Otherwise, dynamic_extent.
294 */
295 template <std::size_t Extent, std::size_t Offset, std::size_t Count>
296 struct ExtentValue : public std::integral_constant<
297 std::size_t, Count != dynamic_extent ?
298 Count : (Extent != dynamic_extent ? Extent - Offset : Extent)> {};
299
300 /*!
301 * If N is dynamic_extent, the extent of the returned span E is also
302 * dynamic_extent; otherwise it is std::size_t(sizeof(T)) * N.
303 */
304 template <typename T, std::size_t Extent>
305 struct ExtentAsBytesValue : public std::integral_constant<
306 std::size_t,
307 Extent == dynamic_extent ?
308 Extent : sizeof(T) * Extent> {};
309
310 template <std::size_t From, std::size_t To>
311 struct IsAllowedExtentConversion : public std::integral_constant<
312 bool, From == To || From == dynamic_extent || To == dynamic_extent> {};
313
314 template <class From, class To>
315 struct IsAllowedElementTypeConversion
316 : public std::integral_constant<bool, std::is_convertible_v<From (*)[], To (*)[]>> {}; // NOLINT
317
318 template <class T>
319 struct IsSpanOracle : std::false_type {};
320
321 template <class T, std::size_t Extent>
322 struct IsSpanOracle<Span<T, Extent>> : std::true_type {};
323
324 template <class T>
325 struct IsSpan : public IsSpanOracle<typename std::remove_cv_t<T>> {};
326
327 // Re-implement std algorithms here to adopt CUDA.
328 template <typename T>
329 struct Less {
330 XGBOOST_DEVICE constexpr bool operator()(const T& _x, const T& _y) const {
331 return _x < _y;
332 }
333 };
334
335 template <typename T>
336 struct Greater {
337 XGBOOST_DEVICE constexpr bool operator()(const T& _x, const T& _y) const {
338 return _x > _y;
339 }
340 };
341
342 template <class InputIt1, class InputIt2,
343 class Compare =
344 detail::Less<decltype(std::declval<InputIt1>().operator*())>>
345 XGBOOST_DEVICE bool LexicographicalCompare(InputIt1 first1, InputIt1 last1,
346 InputIt2 first2, InputIt2 last2) {
347 Compare comp;
348 for (; first1 != last1 && first2 != last2; ++first1, ++first2) {
349 if (comp(*first1, *first2)) {
350 return true;
351 }
352 if (comp(*first2, *first1)) {
353 return false;
354 }
355 }
356 return first1 == last1 && first2 != last2;
357 }
358
359 } // namespace detail
360
361 template <typename T>
362 XGBOOST_DEVICE std::enable_if_t<!std::is_reference_v<T>, std::size_t> SizeBytes(std::size_t n) {
363 return n * sizeof(T);
364 }
365
366 /*!
367 * \brief span class implementation, based on ISO++20 span<T>. The interface
368 * should be the same.
369 *
370 * What's different from span<T> in Guidelines Support Library (GSL)
371 *
372 * Interface might be slightly different, we stick with ISO.
373 *
374 * GSL uses C++14/17 features, which are not available here.
375 * GSL uses constexpr extensively, which is not possible with limitation
376 * of C++11.
377 * GSL doesn't concern about CUDA.
378 *
379 * GSL is more thoroughly implemented and tested.
380 * GSL is more optimized, especially for static extent.
381 *
382 * GSL uses __buildin_unreachable() when error, Span<T> uses dmlc LOG and
383 * customized CUDA logging.
384 *
385 *
386 * What's different from span<T> in ISO++20 (ISO)
387 *
388 * ISO uses functions/structs from std library, which might be not available
389 * in CUDA.
390 * Initializing from std::array is not supported.
391 *
392 * ISO uses constexpr extensively, which is not possible with limitation
393 * of C++11.
394 * ISO uses C++14/17 features, which is not available here.
395 * ISO doesn't concern about CUDA.
396 *
397 * ISO uses std::terminate(), Span<T> uses dmlc LOG and customized CUDA
398 * logging.
399 *
400 *
401 * Limitations:
402 * With thrust:
403 * It's not adviced to initialize Span with host_vector directly, since
404 * host_vector::data() is a host function.
405 * It's not possible to initialize Span with device_vector directly, since
406 * device_vector::data() returns a wrapped pointer.
407 * It's unclear that what kind of thrust algorithm can be used without
408 * memory error. See the test case "GPUSpan.WithTrust"
409 *
410 * Pass iterator to kernel:
411 * Not possible. Use subspan instead.
412 *
413 * The underlying Span in SpanIterator is a pointer, but CUDA pass kernel
414 * parameter by value. If we were to hold a Span value instead of a
415 * pointer, the following snippet will crash, violating the safety
416 * purpose of Span:
417 *
418 * \code{.cpp}
419 * Span<float> span {arr_a};
420 * auto beg = span.begin();
421 *
422 * Span<float> span_b = arr_b;
423 * span = span_b;
424 *
425 * delete arr_a;
426 * beg++; // crash
427 * \endcode
428 *
429 * While holding a pointer or reference should avoid the problem, it's a
430 * compromise. Since we have subspan, it's acceptable not to support
431 * passing iterator.
432 */
433 template <typename T,
434 std::size_t Extent = dynamic_extent>
435 class Span {
436 public:
437 using element_type = T; // NOLINT
438 using value_type = typename std::remove_cv<T>::type; // NOLINT
439 using index_type = std::size_t; // NOLINT
440 using difference_type = detail::ptrdiff_t; // NOLINT
441 using pointer = T*; // NOLINT
442 using reference = T&; // NOLINT
443
444 using iterator = detail::SpanIterator<Span<T, Extent>, false>; // NOLINT
445 using const_iterator = const detail::SpanIterator<Span<T, Extent>, true>; // NOLINT
446 using reverse_iterator = std::reverse_iterator<iterator>; // NOLINT
447 using const_reverse_iterator = const std::reverse_iterator<const_iterator>; // NOLINT
448
449 // constructors
450 constexpr Span() = default;
451
452 XGBOOST_DEVICE Span(pointer _ptr, index_type _count) :
453 size_(_count), data_(_ptr) {
454 SPAN_CHECK(!(Extent != dynamic_extent && _count != Extent));
455 SPAN_CHECK(_ptr || _count == 0);
456 }
457
458 XGBOOST_DEVICE Span(pointer _first, pointer _last) :
459 size_(_last - _first), data_(_first) {
460 SPAN_CHECK(data_ || size_ == 0);
461 }
462
463 template <std::size_t N>
464 XGBOOST_DEVICE constexpr Span(element_type (&arr)[N]) // NOLINT
465 __span_noexcept : size_(N), data_(&arr[0]) {}
466
467 template <class Container,
468 class = typename std::enable_if_t<
469 !std::is_const_v<element_type> && !detail::IsSpan<Container>::value &&
470 std::is_convertible_v<typename Container::pointer, pointer> &&
471 std::is_convertible_v<typename Container::pointer,
472 decltype(std::declval<Container>().data())>>>
473 Span(Container& _cont) // NOLINT
474 : size_(_cont.size()), data_(_cont.data()) {
475 static_assert(!detail::IsSpan<Container>::value, "Wrong constructor of Span is called.");
476 }
477
478 template <class Container,
479 class = typename std::enable_if_t<
480 std::is_const_v<element_type> && !detail::IsSpan<Container>::value &&
481 std::is_convertible_v<typename Container::pointer, pointer> &&
482 std::is_convertible_v<typename Container::pointer,
483 decltype(std::declval<Container>().data())>>>
484 Span(const Container& _cont) // NOLINT
485 : size_(_cont.size()), data_(_cont.data()) {
486 static_assert(!detail::IsSpan<Container>::value, "Wrong constructor of Span is called.");
487 }
488
489 template <class U, std::size_t OtherExtent,
490 class = typename std::enable_if_t<
491 detail::IsAllowedElementTypeConversion<U, T>::value &&
492 detail::IsAllowedExtentConversion<OtherExtent, Extent>::value>>
493 XGBOOST_DEVICE constexpr Span(const Span<U, OtherExtent>& _other) // NOLINT
494 __span_noexcept : size_(_other.size()),
495 data_(_other.data()) {}
496
497 constexpr Span(Span const& _other) noexcept(true) = default;
498 constexpr Span& operator=(Span const& _other) noexcept(true) = default;
499 constexpr Span(Span&& _other) noexcept(true) = default;
500 constexpr Span& operator=(Span&& _other) noexcept(true) = default;
501 ~Span() noexcept(true) = default;
502
503 XGBOOST_DEVICE constexpr iterator begin() const __span_noexcept { // NOLINT
504 return {this, 0};
505 }
506
507 XGBOOST_DEVICE constexpr iterator end() const __span_noexcept { // NOLINT
508 return {this, size()};
509 }
510
511 XGBOOST_DEVICE constexpr const_iterator cbegin() const __span_noexcept { // NOLINT
512 return {this, 0};
513 }
514
515 XGBOOST_DEVICE constexpr const_iterator cend() const __span_noexcept { // NOLINT
516 return {this, size()};
517 }
518
519 constexpr reverse_iterator rbegin() const __span_noexcept { // NOLINT
520 return reverse_iterator{end()};
521 }
522
523 constexpr reverse_iterator rend() const __span_noexcept { // NOLINT
524 return reverse_iterator{begin()};
525 }
526
527 XGBOOST_DEVICE constexpr const_reverse_iterator crbegin() const __span_noexcept { // NOLINT
528 return const_reverse_iterator{cend()};
529 }
530
531 XGBOOST_DEVICE constexpr const_reverse_iterator crend() const __span_noexcept { // NOLINT
532 return const_reverse_iterator{cbegin()};
533 }
534
535 // element access
536
537 XGBOOST_DEVICE reference front() const { // NOLINT
538 return (*this)[0];
539 }
540
541 XGBOOST_DEVICE reference back() const { // NOLINT
542 return (*this)[size() - 1];
543 }
544
545 XGBOOST_DEVICE reference operator[](index_type _idx) const {
546 SPAN_LT(_idx, size());
547 return data()[_idx];
548 }
549
550 XGBOOST_DEVICE reference operator()(index_type _idx) const {
551 return this->operator[](_idx);
552 }
553
554 XGBOOST_DEVICE constexpr pointer data() const __span_noexcept { // NOLINT
555 return data_;
556 }
557
558 // Observers
559 XGBOOST_DEVICE constexpr index_type size() const __span_noexcept { // NOLINT
560 return size_;
561 }
562 XGBOOST_DEVICE constexpr index_type size_bytes() const __span_noexcept { // NOLINT
563 return SizeBytes<T>(size());
564 }
565
566 XGBOOST_DEVICE constexpr bool empty() const __span_noexcept { // NOLINT
567 return size() == 0;
568 }
569
570 // Subviews
571 template <std::size_t Count>
572 XGBOOST_DEVICE Span<element_type, Count> first() const { // NOLINT
573 SPAN_CHECK(Count <= size());
574 return {data(), Count};
575 }
576
577 XGBOOST_DEVICE Span<element_type, dynamic_extent> first( // NOLINT
578 std::size_t _count) const {
579 SPAN_CHECK(_count <= size());
580 return {data(), _count};
581 }
582
583 template <std::size_t Count>
584 XGBOOST_DEVICE Span<element_type, Count> last() const { // NOLINT
585 SPAN_CHECK(Count <= size());
586 return {data() + size() - Count, Count};
587 }
588
589 XGBOOST_DEVICE Span<element_type, dynamic_extent> last( // NOLINT
590 std::size_t _count) const {
591 SPAN_CHECK(_count <= size());
592 return subspan(size() - _count, _count);
593 }
594
595 /*!
596 * If Count is std::dynamic_extent, r.size() == this->size() - Offset;
597 * Otherwise r.size() == Count.
598 */
599 template <std::size_t Offset,
600 std::size_t Count = dynamic_extent>
601 XGBOOST_DEVICE auto subspan() const -> // NOLINT
602 Span<element_type,
603 detail::ExtentValue<Extent, Offset, Count>::value> {
604 SPAN_CHECK((Count == dynamic_extent) ?
605 (Offset <= size()) : (Offset + Count <= size()));
606 return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count};
607 }
608
609 XGBOOST_DEVICE Span<element_type, dynamic_extent> subspan( // NOLINT
610 index_type _offset,
611 index_type _count = dynamic_extent) const {
612 SPAN_CHECK((_count == dynamic_extent) ?
613 (_offset <= size()) : (_offset + _count <= size()));
614 return {data() + _offset, _count ==
615 dynamic_extent ? size() - _offset : _count};
616 }
617
618 private:
619 index_type size_ { 0 };
620 pointer data_ { nullptr };
621 };
622
623 template <class T, std::size_t X, class U, std::size_t Y>
624 XGBOOST_DEVICE bool operator==(Span<T, X> l, Span<U, Y> r) {
625 if (l.size() != r.size()) {
626 return false;
627 }
628 for (auto l_beg = l.cbegin(), r_beg = r.cbegin(); l_beg != l.cend();
629 ++l_beg, ++r_beg) {
630 if (*l_beg != *r_beg) {
631 return false;
632 }
633 }
634 return true;
635 }
636
637 template <class T, std::size_t X, class U, std::size_t Y>
638 XGBOOST_DEVICE constexpr bool operator!=(Span<T, X> l, Span<U, Y> r) {
639 return !(l == r);
640 }
641
642 template <class T, std::size_t X, class U, std::size_t Y>
643 XGBOOST_DEVICE constexpr bool operator<(Span<T, X> l, Span<U, Y> r) {
644 return detail::LexicographicalCompare(l.begin(), l.end(),
645 r.begin(), r.end());
646 }
647
648 template <class T, std::size_t X, class U, std::size_t Y>
649 XGBOOST_DEVICE constexpr bool operator<=(Span<T, X> l, Span<U, Y> r) {
650 return !(l > r);
651 }
652
653 template <class T, std::size_t X, class U, std::size_t Y>
654 XGBOOST_DEVICE constexpr bool operator>(Span<T, X> l, Span<U, Y> r) {
655 return detail::LexicographicalCompare<
656 typename Span<T, X>::iterator, typename Span<U, Y>::iterator,
657 detail::Greater<typename Span<T, X>::element_type>>(l.begin(), l.end(),
658 r.begin(), r.end());
659 }
660
661 template <class T, std::size_t X, class U, std::size_t Y>
662 XGBOOST_DEVICE constexpr bool operator>=(Span<T, X> l, Span<U, Y> r) {
663 return !(l < r);
664 }
665
666 template <class T, std::size_t E>
667 XGBOOST_DEVICE auto as_bytes(Span<T, E> s) __span_noexcept -> // NOLINT
668 Span<const byte, detail::ExtentAsBytesValue<T, E>::value> {
669 return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
670 }
671
672 template <class T, std::size_t E>
673 XGBOOST_DEVICE auto as_writable_bytes(Span<T, E> s) __span_noexcept -> // NOLINT
674 Span<byte, detail::ExtentAsBytesValue<T, E>::value> {
675 return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
676 }
677
678 /**
679 * \brief A simple custom Span type that uses general iterator instead of pointer.
680 */
681 template <typename It>
682 class IterSpan {
683 public:
684 using value_type = typename std::iterator_traits<It>::value_type; // NOLINT
685 using index_type = std::size_t; // NOLINT
686 using iterator = It; // NOLINT
687
688 private:
689 It it_;
690 index_type size_{0};
691
692 public:
693 IterSpan() = default;
694 XGBOOST_DEVICE IterSpan(It it, index_type size) : it_{std::move(it)}, size_{size} {}
695 XGBOOST_DEVICE explicit IterSpan(common::Span<It, dynamic_extent> span)
696 : it_{span.data()}, size_{span.size()} {}
697
698 [[nodiscard]] XGBOOST_DEVICE index_type size() const noexcept { return size_; } // NOLINT
699 [[nodiscard]] XGBOOST_DEVICE decltype(auto) operator[](index_type i) const { return it_[i]; }
700 [[nodiscard]] XGBOOST_DEVICE decltype(auto) operator[](index_type i) { return it_[i]; }
701 [[nodiscard]] XGBOOST_DEVICE bool empty() const noexcept { return size() == 0; } // NOLINT
702 [[nodiscard]] XGBOOST_DEVICE It data() const noexcept { return it_; } // NOLINT
703 [[nodiscard]] XGBOOST_DEVICE IterSpan<It> subspan( // NOLINT
704 index_type _offset, index_type _count = dynamic_extent) const {
705 SPAN_CHECK((_count == dynamic_extent) ? (_offset <= size()) : (_offset + _count <= size()));
706 return {data() + _offset, _count == dynamic_extent ? size() - _offset : _count};
707 }
708 [[nodiscard]] XGBOOST_DEVICE constexpr iterator begin() const noexcept { // NOLINT
709 return it_;
710 }
711 [[nodiscard]] XGBOOST_DEVICE constexpr iterator end() const noexcept { // NOLINT
712 return it_ + size();
713 }
714 };
715
716 template <typename T>
717 Span(std::vector<T> const&) -> Span<T const>;
718
719 template <typename T>
720 Span(std::vector<T>&) -> Span<T>;
721 } // namespace xgboost::common
722
723
724 #if defined(_MSC_VER) &&_MSC_VER < 1910
725 #undef constexpr
726 #pragma pop_macro("constexpr")
727 #undef __span_noexcept
728 #endif // _MSC_VER < 1910
729
730 #endif // XGBOOST_SPAN_H_