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