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 "include/private/SkTo.h" |
13 | |
14 | template <typename T> |
15 | class SkSpan { |
16 | public: |
17 | constexpr SkSpan() : fPtr{nullptr}, fSize{0} {} |
18 | constexpr SkSpan(T* ptr, size_t size) : fPtr{ptr}, fSize{size} {} |
19 | template <typename U, typename = typename std::enable_if<std::is_same<const U, T>::value>::type> |
20 | constexpr SkSpan(const SkSpan<U>& that) : fPtr(that.data()), fSize{that.size()} {} |
21 | constexpr SkSpan(const SkSpan& o) = default; |
22 | constexpr SkSpan& operator=(const SkSpan& that) { |
23 | fPtr = that.fPtr; |
24 | fSize = that.fSize; |
25 | return *this; |
26 | } |
27 | constexpr T& operator [] (size_t i) const { return fPtr[i]; } |
28 | constexpr T& front() const { return fPtr[0]; } |
29 | constexpr T& back() const { return fPtr[fSize - 1]; } |
30 | constexpr T* begin() const { return fPtr; } |
31 | constexpr T* end() const { return fPtr + fSize; } |
32 | constexpr const T* cbegin() const { return fPtr; } |
33 | constexpr const T* cend() const { return fPtr + fSize; } |
34 | constexpr auto rbegin() const { return std::make_reverse_iterator(this->end()); } |
35 | constexpr auto rend() const { return std::make_reverse_iterator(this->begin()); } |
36 | constexpr auto crbegin() const { return std::make_reverse_iterator(this->cend()); } |
37 | constexpr auto crend() const { return std::make_reverse_iterator(this->cbegin()); } |
38 | constexpr T* data() const { return fPtr; } |
39 | constexpr int count() const { return SkTo<int>(fSize); } |
40 | constexpr size_t size() const { return fSize; } |
41 | constexpr bool empty() const { return fSize == 0; } |
42 | constexpr size_t size_bytes() const { return fSize * sizeof(T); } |
43 | constexpr SkSpan<T> first(size_t prefixLen) const { |
44 | SkASSERT(prefixLen <= this->size()); |
45 | if (prefixLen == 0) { return SkSpan{}; } |
46 | return SkSpan{fPtr, prefixLen}; |
47 | } |
48 | constexpr SkSpan<T> last(size_t postfixLen) const { |
49 | SkASSERT(postfixLen <= this->size()); |
50 | if (postfixLen == 0) { return SkSpan{}; } |
51 | return SkSpan{fPtr + (this->size() - postfixLen), postfixLen}; |
52 | } |
53 | |
54 | private: |
55 | T* fPtr; |
56 | size_t fSize; |
57 | }; |
58 | |
59 | template <typename T, typename S> |
60 | inline constexpr SkSpan<T> SkMakeSpan(T* p, S s) { return SkSpan<T>{p, SkTo<size_t>(s)}; } |
61 | |
62 | template <size_t N, typename T> |
63 | inline constexpr SkSpan<T> SkMakeSpan(T(&a)[N]) { return SkSpan<T>{a, N}; } |
64 | |
65 | template <typename Container> |
66 | inline auto SkMakeSpan(Container& c) |
67 | -> SkSpan<typename std::remove_reference<decltype(*(c.data()))>::type> { |
68 | return {c.data(), c.size()}; |
69 | } |
70 | #endif // SkSpan_DEFINED |
71 | |