HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
span_utils.h
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License.
3 
4 #pragma once
5 
6 #include <algorithm>
7 #include <cstddef>
8 
9 #include "core/common/gsl.h"
10 
11 namespace onnxruntime {
12 
13 // AsSpan inspired by Fekir's Blog https://fekir.info/post/span-the-missing-constructor/
14 // Used under MIT license
15 
16 // Use AsSpan for less typing on any container including initializer list to create a span
17 // (unnamed, untyped initializer list does not automatically convert to gsl::span).
18 // {1, 2, 3} as such does not have a type
19 // (see https://scottmeyers.blogspot.com/2014/03/if-braced-initializers-have-no-type-why.html)
20 //
21 // Example: AsSpan({1, 2, 3}) results in gsl::span<const int>
22 //
23 // The above would deduce to std::initializer_list<int> and the result is gsl::span<const int>
24 //
25 // AsSpan<int64_t>({1, 2, 3}) produces gsl::span<const int64_t>
26 //
27 // We can also do std::array<int64_t, 3>{1, 2, 3} that can be automatically converted to span
28 // without memory allocation.
29 //
30 // If type conversion is not required, then for C++17 std::array template parameters are
31 // auto-deduced. Example: std::array{1, 2, 3}.
32 // We are aiming at not allocating memory dynamically.
33 
34 namespace details {
35 template <class P>
36 constexpr auto AsSpanImpl(P* p, size_t s) {
37  return gsl::span<P>(p, s);
38 }
39 } // namespace details
40 
41 template <class C>
42 constexpr auto AsSpan(C& c) {
43  return details::AsSpanImpl(c.data(), c.size());
44 }
45 
46 template <class C>
47 constexpr auto AsSpan(const C& c) {
48  return details::AsSpanImpl(c.data(), c.size());
49 }
50 
51 template <class C>
52 constexpr auto AsSpan(C&& c) {
53  return details::AsSpanImpl(c.data(), c.size());
54 }
55 
56 template <class T>
57 constexpr auto AsSpan(std::initializer_list<T> c) {
58  return details::AsSpanImpl(c.begin(), c.size());
59 }
60 
61 template <class T, size_t N>
62 constexpr auto AsSpan(T (&arr)[N]) {
63  return details::AsSpanImpl(arr, N);
64 }
65 
66 template <class T, size_t N>
67 constexpr auto AsSpan(const T (&arr)[N]) {
68  return details::AsSpanImpl(arr, N);
69 }
70 
71 template <class T>
72 inline gsl::span<const T> EmptySpan() { return gsl::span<const T>(); }
73 
74 template <class U, class T>
75 [[nodiscard]] inline gsl::span<U> ReinterpretAsSpan(gsl::span<T> src) {
76  // adapted from gsl-lite span::as_span():
77  // https://github.com/gsl-lite/gsl-lite/blob/4720a2980a30da085b4ddb4a0ea2a71af7351a48/include/gsl/gsl-lite.hpp#L4102-L4108
78  Expects(src.size_bytes() % sizeof(U) == 0);
79  return gsl::span<U>(reinterpret_cast<U*>(src.data()), src.size_bytes() / sizeof(U));
80 }
81 
82 [[nodiscard]] inline gsl::span<const std::byte> AsByteSpan(const void* data, size_t length) {
83  return gsl::span<const std::byte>(reinterpret_cast<const std::byte*>(data), length);
84 }
85 
86 template <class T1, size_t Extent1, class T2, size_t Extent2>
87 [[nodiscard]] inline bool SpanEq(gsl::span<T1, Extent1> a, gsl::span<T2, Extent2> b) {
88  static_assert(std::is_same_v<std::remove_const_t<T1>, std::remove_const_t<T2>>,
89  "T1 and T2 should be the same type except for const qualification");
90  return std::equal(a.begin(), a.end(), b.begin(), b.end());
91 }
92 
93 } // namespace onnxruntime
constexpr auto AsSpan(C &c)
Definition: span_utils.h:42
GLboolean * data
Definition: glcorearb.h:131
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
IMATH_HOSTDEVICE constexpr bool equal(T1 a, T2 b, T3 t) IMATH_NOEXCEPT
Definition: ImathFun.h:105
gsl::span< const T > EmptySpan()
Definition: span_utils.h:72
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
constexpr auto AsSpanImpl(P *p, size_t s)
Definition: span_utils.h:36
gsl::span< U > ReinterpretAsSpan(gsl::span< T > src)
Definition: span_utils.h:75
GA_API const UT_StringHolder N
gsl::span< const std::byte > AsByteSpan(const void *data, size_t length)
Definition: span_utils.h:82
bool SpanEq(gsl::span< T1, Extent1 > a, gsl::span< T2, Extent2 > b)
Definition: span_utils.h:87
Definition: format.h:895
GLenum src
Definition: glcorearb.h:1793
unsigned char byte
Definition: UT_Span.h:163