• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkSpan_DEFINED
9 #define SkSpan_DEFINED
10 
11 #include <cstddef>
12 #include <iterator>
13 #include <type_traits>
14 #include <utility>
15 #include "include/private/SkTLogic.h"
16 
17 /**
18  * An SkSpan is a view of a contiguous collection of elements of type T. It can be directly
19  * constructed from a pointer and size. SkMakeSpan can be used to construct one from an array,
20  * or a container (like std::vector).
21  *
22  * With C++17, we could add template deduction guides that eliminate the need for SkMakeSpan:
23  *     https://skia-review.googlesource.com/c/skia/+/320264
24  */
25 template <typename T>
26 class SkSpan {
27 public:
SkSpan()28     constexpr SkSpan() : fPtr{nullptr}, fSize{0} {}
SkSpan(T * ptr,size_t size)29     constexpr SkSpan(T* ptr, size_t size) : fPtr{ptr}, fSize{size} {
30         SkASSERT(size < kMaxSize);
31     }
32     template <typename U, typename = typename std::enable_if<std::is_same<const U, T>::value>::type>
SkSpan(const SkSpan<U> & that)33     constexpr SkSpan(const SkSpan<U>& that) : fPtr(that.data()), fSize{that.size()} {}
34     constexpr SkSpan(const SkSpan& o) = default;
35 
36     constexpr SkSpan& operator=(const SkSpan& that) {
37         fPtr = that.fPtr;
38         fSize = that.fSize;
39         return *this;
40     }
41     constexpr T& operator [] (size_t i) const {
42         SkASSERT(i < this->size());
43         return fPtr[i];
44     }
front()45     constexpr T& front() const { return fPtr[0]; }
back()46     constexpr T& back()  const { return fPtr[fSize - 1]; }
begin()47     constexpr T* begin() const { return fPtr; }
end()48     constexpr T* end() const { return fPtr + fSize; }
rbegin()49     constexpr auto rbegin() const { return std::make_reverse_iterator(this->end()); }
rend()50     constexpr auto rend() const { return std::make_reverse_iterator(this->begin()); }
data()51     constexpr T* data() const { return this->begin(); }
size()52     constexpr size_t size() const { return fSize; }
empty()53     constexpr bool empty() const { return fSize == 0; }
size_bytes()54     constexpr size_t size_bytes() const { return fSize * sizeof(T); }
first(size_t prefixLen)55     constexpr SkSpan<T> first(size_t prefixLen) const {
56         SkASSERT(prefixLen <= this->size());
57         return SkSpan{fPtr, prefixLen};
58     }
last(size_t postfixLen)59     constexpr SkSpan<T> last(size_t postfixLen) const {
60         SkASSERT(postfixLen <= this->size());
61         return SkSpan{fPtr + (this->size() - postfixLen), postfixLen};
62     }
subspan(size_t offset,size_t count)63     constexpr SkSpan<T> subspan(size_t offset, size_t count) const {
64         SkASSERT(offset <= this->size());
65         SkASSERT(count <= this->size() - offset);
66         return SkSpan{fPtr + offset, count};
67     }
68 
69 private:
70     static constexpr size_t kMaxSize = std::numeric_limits<size_t>::max() / sizeof(T);
71     T* fPtr;
72     size_t fSize;
73 };
74 
SkMakeSpan(T * p,S s)75 template <typename T, typename S> inline constexpr SkSpan<T> SkMakeSpan(T* p, S s) {
76     return SkSpan<T>{p, SkTo<size_t>(s)};
77 }
78 
SkMakeSpan(T (& a)[N])79 template <size_t N, typename T> inline constexpr SkSpan<T> SkMakeSpan(T (&a)[N]) {
80     return SkSpan<T>{a, N};
81 }
82 
83 template <typename Container>
84 inline auto SkMakeSpan(Container& c)
85         -> SkSpan<typename std::remove_reference<decltype(*(c.data()))>::type> {
86     return {c.data(), c.size()};
87 }
88 
89 #endif  // SkSpan_DEFINED
90