tvm
span.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
25 #ifndef TVM_SUPPORT_SPAN_H_
26 #define TVM_SUPPORT_SPAN_H_
27 
28 #include <cstddef>
29 #include <iterator>
30 #include <type_traits>
31 #include <vector>
32 
33 namespace tvm {
34 namespace support {
35 
41 template <class T, class W>
42 class Span {
43  public:
44  using value_type = W;
45  using const_W = typename ::std::add_const<W>::type;
46 
47  template <class W1>
48  class iterator_base : public std::iterator<std::input_iterator_tag, W> {
49  public:
50  inline iterator_base(T* ptr, T* end) : ptr_{ptr}, end_{end} { CHECK_GE(end, ptr); }
51 
52  inline W1 operator*() { return W1(*ptr_); }
53 
55  if (ptr_ != end_) ptr_++;
56  return *this;
57  }
58 
59  inline bool operator==(iterator_base<W1> other) {
60  return ptr_ == other.ptr_ && end_ == other.end_;
61  }
62 
63  inline bool operator!=(iterator_base<W1> other) { return !(*this == other); }
64 
65  template <class X = W1, typename = ::std::enable_if_t<!::std::is_const<X>::value> >
66  inline operator iterator_base<const_W>() const {
67  return iterator_base<const_W>(ptr_, end_);
68  }
69 
70  private:
71  T* ptr_;
72  T* end_;
73  };
74 
77 
78  inline Span(T* begin, int num_elements) : begin_{begin}, end_{begin + num_elements} {}
79  inline Span(T* begin, T* end) : begin_{begin}, end_{end} {}
80 
81  inline iterator begin() const { return iterator(begin_, end_); }
82 
83  inline iterator end() const { return iterator(end_, end_); }
84 
85  size_t size() const { return end_ - begin_; }
86 
87  inline W operator[](int i) {
88  T* to_return = begin_ + i;
89  ICHECK_LT(to_return, end_) << "Span access out of bounds: " << i;
90  return W(*to_return);
91  }
92 
93  inline operator std::vector<W>() { return std::vector<W>(begin(), end()); }
94 
95  protected:
96  T* begin_;
97  T* end_;
98 };
99 
100 } // namespace support
101 } // namespace tvm
102 
103 #endif // TVM_SUPPORT_SPAN_H_
bool operator==(iterator_base< W1 > other)
Definition: span.h:59
iterator_base(T *ptr, T *end)
Definition: span.h:50
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Span(T *begin, int num_elements)
Definition: span.h:78
iterator end() const
Definition: span.h:83
Span(T *begin, T *end)
Definition: span.h:79
W operator[](int i)
Definition: span.h:87
iterator begin() const
Definition: span.h:81
iterator_base< W1 > & operator++()
Definition: span.h:54
iterator_base< W > iterator
Definition: span.h:75
typename ::std::add_const< W >::type const_W
Definition: span.h:45
W1 operator*()
Definition: span.h:52
A partial implementation of the C++20 std::span.
Definition: span.h:42
bool operator!=(iterator_base< W1 > other)
Definition: span.h:63
size_t size() const
Definition: span.h:85
T * begin_
Definition: span.h:96
T * end_
Definition: span.h:97
W value_type
Definition: span.h:44