2018-08-22 18:19:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkSpan_DEFINED
|
|
|
|
#define SkSpan_DEFINED
|
|
|
|
|
|
|
|
#include <cstddef>
|
2020-12-17 18:09:27 +00:00
|
|
|
#include <iterator>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
2020-10-27 13:45:40 +00:00
|
|
|
#include "include/private/SkTLogic.h"
|
2019-06-21 16:22:32 +00:00
|
|
|
|
2021-05-11 17:36:10 +00:00
|
|
|
/**
|
2021-05-11 17:41:46 +00:00
|
|
|
* An SkSpan is a view of a contiguous collection of elements of type T. It can be directly
|
|
|
|
* constructed from a pointer and size. SkMakeSpan can be used to construct one from an array,
|
|
|
|
* or a container (like std::vector).
|
|
|
|
*
|
2021-05-11 17:36:10 +00:00
|
|
|
* With C++17, we could add template deduction guides that eliminate the need for SkMakeSpan:
|
|
|
|
* https://skia-review.googlesource.com/c/skia/+/320264
|
|
|
|
*/
|
2018-08-22 18:19:04 +00:00
|
|
|
template <typename T>
|
|
|
|
class SkSpan {
|
|
|
|
public:
|
2018-05-08 01:08:02 +00:00
|
|
|
constexpr SkSpan() : fPtr{nullptr}, fSize{0} {}
|
2021-05-13 17:56:53 +00:00
|
|
|
constexpr SkSpan(T* ptr, size_t size) : fPtr{ptr}, fSize{size} {
|
|
|
|
SkASSERT(size < kMaxSize);
|
|
|
|
}
|
2019-07-02 20:06:00 +00:00
|
|
|
template <typename U, typename = typename std::enable_if<std::is_same<const U, T>::value>::type>
|
|
|
|
constexpr SkSpan(const SkSpan<U>& that) : fPtr(that.data()), fSize{that.size()} {}
|
2018-05-08 01:08:02 +00:00
|
|
|
constexpr SkSpan(const SkSpan& o) = default;
|
2020-05-26 16:58:59 +00:00
|
|
|
|
[graphite] Use SkTDArray in SkPaintParamsKeyBuilder
SkTArray::reset() got rid of any reserved space, so previously we never
actually got to a high-water allocation. Updated the stack to use
SkTDArray just for consistency so we only had to include that instead of
<vector>, too.
Cq-Include-Trybots: luci.skia.skia.primary:Build-Mac-Clang-arm64-Debug-ASAN_Graphite,Build-Mac-Clang-arm64-Debug-Graphite,Build-Mac-Clang-arm64-Debug-Graphite_NoGpu,Build-Mac-Clang-arm64-Debug-iOS_Graphite,Build-Mac-Clang-arm64-Release-Graphite,Build-Mac-Clang-arm64-Release-iOS_Graphite,Build-Mac-Clang-x86_64-Debug-Graphite,Build-Mac-Clang-x86_64-Release-Graphite,Perf-Mac10.15.7-Clang-MacBookPro11.5-GPU-RadeonHD8870M-x86_64-Release-All-Graphite,Perf-Mac11-Clang-MacMini9.1-GPU-AppleM1-arm64-Release-All-Graphite,Test-Mac11-Clang-MacMini9.1-GPU-AppleM1-arm64-Debug-All-ASAN_Graphite,Test-Mac10.15.7-Clang-MacBookPro11.5-GPU-RadeonHD8870M-x86_64-Debug-All-Graphite,Test-Mac11-Clang-MacMini9.1-GPU-AppleM1-arm64-Release-All-Graphite,Test-Mac12-Clang-MacBookPro16.2-GPU-IntelIrisPlus-x86_64-Debug-All-Graphite
Change-Id: I94c9a38d0e630c7d7f1a3eeeb85076067e066a2f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/524000
Reviewed-by: Robert Phillips <robertphillips@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
2022-03-23 20:36:14 +00:00
|
|
|
constexpr SkSpan& operator=(const SkSpan& that) = default;
|
|
|
|
|
2020-07-16 13:58:19 +00:00
|
|
|
constexpr T& operator [] (size_t i) const {
|
|
|
|
SkASSERT(i < this->size());
|
|
|
|
return fPtr[i];
|
|
|
|
}
|
2019-04-22 21:18:50 +00:00
|
|
|
constexpr T& front() const { return fPtr[0]; }
|
|
|
|
constexpr T& back() const { return fPtr[fSize - 1]; }
|
2018-05-08 01:08:02 +00:00
|
|
|
constexpr T* begin() const { return fPtr; }
|
|
|
|
constexpr T* end() const { return fPtr + fSize; }
|
2020-01-30 22:49:40 +00:00
|
|
|
constexpr auto rbegin() const { return std::make_reverse_iterator(this->end()); }
|
|
|
|
constexpr auto rend() const { return std::make_reverse_iterator(this->begin()); }
|
2020-05-26 16:58:59 +00:00
|
|
|
constexpr T* data() const { return this->begin(); }
|
2018-05-08 01:08:02 +00:00
|
|
|
constexpr size_t size() const { return fSize; }
|
|
|
|
constexpr bool empty() const { return fSize == 0; }
|
|
|
|
constexpr size_t size_bytes() const { return fSize * sizeof(T); }
|
2019-12-11 22:00:31 +00:00
|
|
|
constexpr SkSpan<T> first(size_t prefixLen) const {
|
|
|
|
SkASSERT(prefixLen <= this->size());
|
|
|
|
return SkSpan{fPtr, prefixLen};
|
|
|
|
}
|
|
|
|
constexpr SkSpan<T> last(size_t postfixLen) const {
|
|
|
|
SkASSERT(postfixLen <= this->size());
|
|
|
|
return SkSpan{fPtr + (this->size() - postfixLen), postfixLen};
|
|
|
|
}
|
2022-04-12 21:32:12 +00:00
|
|
|
constexpr SkSpan<T> subspan(size_t offset) const {
|
|
|
|
return this->subspan(offset, this->size() - offset);
|
|
|
|
}
|
2020-05-04 18:05:22 +00:00
|
|
|
constexpr SkSpan<T> subspan(size_t offset, size_t count) const {
|
|
|
|
SkASSERT(offset <= this->size());
|
2020-05-13 16:01:24 +00:00
|
|
|
SkASSERT(count <= this->size() - offset);
|
2020-05-04 18:05:22 +00:00
|
|
|
return SkSpan{fPtr + offset, count};
|
|
|
|
}
|
2018-08-22 18:19:04 +00:00
|
|
|
|
|
|
|
private:
|
2021-05-13 17:56:53 +00:00
|
|
|
static constexpr size_t kMaxSize = std::numeric_limits<size_t>::max() / sizeof(T);
|
2018-08-22 18:19:04 +00:00
|
|
|
T* fPtr;
|
|
|
|
size_t fSize;
|
|
|
|
};
|
|
|
|
|
2021-05-11 17:36:10 +00:00
|
|
|
template <typename T, typename S> inline constexpr SkSpan<T> SkMakeSpan(T* p, S s) {
|
|
|
|
return SkSpan<T>{p, SkTo<size_t>(s)};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N, typename T> inline constexpr SkSpan<T> SkMakeSpan(T (&a)[N]) {
|
|
|
|
return SkSpan<T>{a, N};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Container>
|
|
|
|
inline auto SkMakeSpan(Container& c)
|
|
|
|
-> SkSpan<typename std::remove_reference<decltype(*(c.data()))>::type> {
|
|
|
|
return {c.data(), c.size()};
|
|
|
|
}
|
2019-07-02 20:06:00 +00:00
|
|
|
|
2018-08-22 18:19:04 +00:00
|
|
|
#endif // SkSpan_DEFINED
|