| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#ifndef XGBOOST_SPAN_H_ |
| 30 |
#define XGBOOST_SPAN_H_ |
| 31 |
|
| 32 |
#include <xgboost/base.h> |
| 33 |
|
| 34 |
#include <cstddef> |
| 35 |
#include <cstdio> |
| 36 |
#include <iterator> |
| 37 |
#include <limits> |
| 38 |
#include <type_traits> |
| 39 |
#include <utility> |
| 40 |
#include <vector> |
| 41 |
|
| 42 |
#if defined(__CUDACC__) |
| 43 |
#include <cuda_runtime.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
#if defined(_MSC_VER) && _MSC_VER < 1910 |
| 64 |
|
| 65 |
#define __span_noexcept |
| 66 |
|
| 67 |
#pragma push_macro("constexpr") |
| 68 |
#define constexpr |
| 69 |
|
| 70 |
#else |
| 71 |
|
| 72 |
#define __span_noexcept noexcept |
| 73 |
|
| 74 |
#endif |
| 75 |
|
| 76 |
namespace xgboost::common { |
| 77 |
|
| 78 |
#if defined(__CUDA_ARCH__) |
| 79 |
|
| 80 |
|
| 81 |
#if defined(_MSC_VER) |
| 82 |
|
| 83 |
|
| 84 |
#define CUDA_KERNEL_CHECK(cond) \ |
| 85 |
do { \ |
| 86 |
if (XGBOOST_EXPECT(!(cond), false)) { \ |
| 87 |
asm("trap;"); \ |
| 88 |
} \ |
| 89 |
} while (0) |
| 90 |
|
| 91 |
#else |
| 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 |
| 101 |
|
| 102 |
#define KERNEL_CHECK CUDA_KERNEL_CHECK |
| 103 |
|
| 104 |
#define SPAN_CHECK KERNEL_CHECK |
| 105 |
|
| 106 |
#else |
| 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 |
|
| 119 |
#define SYCL_KERNEL_CHECK(cond) |
| 120 |
|
| 121 |
#define KERNEL_CHECK(cond) SYCL_KERNEL_CHECK(cond) |
| 122 |
|
| 123 |
#else |
| 124 |
#define KERNEL_CHECK(cond) (XGBOOST_EXPECT((cond), true) ? static_cast<void>(0) : std::terminate()) |
| 125 |
#endif |
| 126 |
|
| 127 |
#define SPAN_CHECK(cond) KERNEL_CHECK(cond) |
| 128 |
|
| 129 |
#endif |
| 130 |
|
| 131 |
#endif |
| 132 |
|
| 133 |
#define SPAN_LT(lhs, rhs) SPAN_CHECK((lhs) < (rhs)) |
| 134 |
|
| 135 |
namespace detail { |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
using ptrdiff_t = typename std::conditional_t< |
| 143 |
std::is_same_v<std::ptrdiff_t, std::int64_t>, std::ptrdiff_t, std::int64_t>; |
| 144 |
} |
| 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(); |
| 149 |
#else |
| 150 |
constexpr std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max(); |
| 151 |
#endif |
| 152 |
|
| 153 |
enum class byte : unsigned char {}; |
| 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; |
| 166 |
using value_type = typename SpanType::value_type; |
| 167 |
using difference_type = detail::ptrdiff_t; |
| 168 |
|
| 169 |
using reference = typename std::conditional< |
| 170 |
IsConst, const ElementType, ElementType>::type&; |
| 171 |
using pointer = typename std::add_pointer<reference>::type; |
| 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( |
| 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 |
|
| 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 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 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 |
|
| 302 |
|
| 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 (*)[]>> {}; |
| 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 |
|
| 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 |
} |
| 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 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
template <typename T, |
| 434 |
std::size_t Extent = dynamic_extent> |
| 435 |
class Span { |
| 436 |
public: |
| 437 |
using element_type = T; |
| 438 |
using value_type = typename std::remove_cv<T>::type; |
| 439 |
using index_type = std::size_t; |
| 440 |
using difference_type = detail::ptrdiff_t; |
| 441 |
using pointer = T*; |
| 442 |
using reference = T&; |
| 443 |
|
| 444 |
using iterator = detail::SpanIterator<Span<T, Extent>, false>; |
| 445 |
using const_iterator = const detail::SpanIterator<Span<T, Extent>, true>; |
| 446 |
using reverse_iterator = std::reverse_iterator<iterator>; |
| 447 |
using const_reverse_iterator = const std::reverse_iterator<const_iterator>; |
| 448 |
|
| 449 |
|
| 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]) |
| 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) |
| 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) |
| 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) |
| 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 { |
| 504 |
return {this, 0}; |
| 505 |
} |
| 506 |
|
| 507 |
XGBOOST_DEVICE constexpr iterator end() const __span_noexcept { |
| 508 |
return {this, size()}; |
| 509 |
} |
| 510 |
|
| 511 |
XGBOOST_DEVICE constexpr const_iterator cbegin() const __span_noexcept { |
| 512 |
return {this, 0}; |
| 513 |
} |
| 514 |
|
| 515 |
XGBOOST_DEVICE constexpr const_iterator cend() const __span_noexcept { |
| 516 |
return {this, size()}; |
| 517 |
} |
| 518 |
|
| 519 |
constexpr reverse_iterator rbegin() const __span_noexcept { |
| 520 |
return reverse_iterator{end()}; |
| 521 |
} |
| 522 |
|
| 523 |
constexpr reverse_iterator rend() const __span_noexcept { |
| 524 |
return reverse_iterator{begin()}; |
| 525 |
} |
| 526 |
|
| 527 |
XGBOOST_DEVICE constexpr const_reverse_iterator crbegin() const __span_noexcept { |
| 528 |
return const_reverse_iterator{cend()}; |
| 529 |
} |
| 530 |
|
| 531 |
XGBOOST_DEVICE constexpr const_reverse_iterator crend() const __span_noexcept { |
| 532 |
return const_reverse_iterator{cbegin()}; |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
XGBOOST_DEVICE reference front() const { |
| 538 |
return (*this)[0]; |
| 539 |
} |
| 540 |
|
| 541 |
XGBOOST_DEVICE reference back() const { |
| 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 { |
| 555 |
return data_; |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
XGBOOST_DEVICE constexpr index_type size() const __span_noexcept { |
| 560 |
return size_; |
| 561 |
} |
| 562 |
XGBOOST_DEVICE constexpr index_type size_bytes() const __span_noexcept { |
| 563 |
return SizeBytes<T>(size()); |
| 564 |
} |
| 565 |
|
| 566 |
XGBOOST_DEVICE constexpr bool empty() const __span_noexcept { |
| 567 |
return size() == 0; |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
template <std::size_t Count> |
| 572 |
XGBOOST_DEVICE Span<element_type, Count> first() const { |
| 573 |
SPAN_CHECK(Count <= size()); |
| 574 |
return {data(), Count}; |
| 575 |
} |
| 576 |
|
| 577 |
XGBOOST_DEVICE Span<element_type, dynamic_extent> first( |
| 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 { |
| 585 |
SPAN_CHECK(Count <= size()); |
| 586 |
return {data() + size() - Count, Count}; |
| 587 |
} |
| 588 |
|
| 589 |
XGBOOST_DEVICE Span<element_type, dynamic_extent> last( |
| 590 |
std::size_t _count) const { |
| 591 |
SPAN_CHECK(_count <= size()); |
| 592 |
return subspan(size() - _count, _count); |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
template <std::size_t Offset, |
| 600 |
std::size_t Count = dynamic_extent> |
| 601 |
XGBOOST_DEVICE auto subspan() const -> |
| 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( |
| 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 -> |
| 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 -> |
| 674 |
Span<byte, detail::ExtentAsBytesValue<T, E>::value> { |
| 675 |
return {reinterpret_cast<byte*>(s.data()), s.size_bytes()}; |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
template <typename It> |
| 682 |
class IterSpan { |
| 683 |
public: |
| 684 |
using value_type = typename std::iterator_traits<It>::value_type; |
| 685 |
using index_type = std::size_t; |
| 686 |
using iterator = It; |
| 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_; } |
| 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; } |
| 702 |
[[nodiscard]] XGBOOST_DEVICE It data() const noexcept { return it_; } |
| 703 |
[[nodiscard]] XGBOOST_DEVICE IterSpan<It> subspan( |
| 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 { |
| 709 |
return it_; |
| 710 |
} |
| 711 |
[[nodiscard]] XGBOOST_DEVICE constexpr iterator end() const noexcept { |
| 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 |
} |
| 722 |
|
| 723 |
|
| 724 |
#if defined(_MSC_VER) &&_MSC_VER < 1910 |
| 725 |
#undef constexpr |
| 726 |
#pragma pop_macro("constexpr") |
| 727 |
#undef __span_noexcept |
| 728 |
#endif |
| 729 |
|
| 730 |
#endif |