/src/libjxl/lib/jxl/base/span.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) the JPEG XL Project Authors. All rights reserved. |
2 | | // |
3 | | // Use of this source code is governed by a BSD-style |
4 | | // license that can be found in the LICENSE file. |
5 | | |
6 | | #ifndef LIB_JXL_BASE_SPAN_H_ |
7 | | #define LIB_JXL_BASE_SPAN_H_ |
8 | | |
9 | | // Span (array view) is a non-owning container that provides cheap "cut" |
10 | | // operations and could be used as "ArrayLike" data source for PaddedBytes. |
11 | | |
12 | | #include <cstddef> |
13 | | #include <cstdint> |
14 | | #include <type_traits> |
15 | | #include <vector> |
16 | | |
17 | | #include "lib/jxl/base/status.h" |
18 | | |
19 | | namespace jxl { |
20 | | |
21 | | template <typename T> |
22 | | class Span { |
23 | | public: |
24 | 120k | constexpr Span() noexcept : Span(nullptr, 0) {} |
25 | | |
26 | | constexpr Span(T* array, size_t length) noexcept |
27 | 661k | : ptr_(array), len_(length) {} jxl::Span<unsigned char const>::Span(unsigned char const*, unsigned long) Line | Count | Source | 27 | 647k | : ptr_(array), len_(length) {} |
jxl::Span<float>::Span(float*, unsigned long) Line | Count | Source | 27 | 14.4k | : ptr_(array), len_(length) {} |
|
28 | | |
29 | | template <size_t N> |
30 | 0 | explicit constexpr Span(T (&a)[N]) noexcept : Span(a, N) {} |
31 | | |
32 | | template <typename U> |
33 | | constexpr Span(U* array, size_t length) noexcept |
34 | 4.63k | : ptr_(reinterpret_cast<T*>(array)), len_(length) { |
35 | 4.63k | static_assert(sizeof(U) == sizeof(T), "Incompatible type of source."); |
36 | 4.63k | } |
37 | | |
38 | | template <typename ArrayLike> |
39 | | explicit constexpr Span(const ArrayLike& other) noexcept |
40 | 374k | : Span(reinterpret_cast<T*>(other.data()), other.size()) { |
41 | 374k | static_assert(sizeof(*other.data()) == sizeof(T), |
42 | 374k | "Incompatible type of source."); |
43 | 374k | } jxl::Span<unsigned char const>::Span<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&) Line | Count | Source | 40 | 374k | : Span(reinterpret_cast<T*>(other.data()), other.size()) { | 41 | 374k | static_assert(sizeof(*other.data()) == sizeof(T), | 42 | 374k | "Incompatible type of source."); | 43 | 374k | } |
jxl::Span<unsigned char const>::Span<jxl::PaddedBytes>(jxl::PaddedBytes const&) Line | Count | Source | 40 | 9 | : Span(reinterpret_cast<T*>(other.data()), other.size()) { | 41 | 9 | static_assert(sizeof(*other.data()) == sizeof(T), | 42 | 9 | "Incompatible type of source."); | 43 | 9 | } |
|
44 | | |
45 | | using NCT = typename std::remove_const<T>::type; |
46 | | |
47 | 1.33M | constexpr T* data() const noexcept { return ptr_; } jxl::Span<unsigned char const>::data() const Line | Count | Source | 47 | 1.27M | constexpr T* data() const noexcept { return ptr_; } |
jxl::Span<float>::data() const Line | Count | Source | 47 | 57.7k | constexpr T* data() const noexcept { return ptr_; } |
|
48 | | |
49 | 638k | constexpr size_t size() const noexcept { return len_; } jxl::Span<unsigned char const>::size() const Line | Count | Source | 49 | 580k | constexpr size_t size() const noexcept { return len_; } |
jxl::Span<float>::size() const Line | Count | Source | 49 | 57.7k | constexpr size_t size() const noexcept { return len_; } |
|
50 | | |
51 | 97 | constexpr bool empty() const noexcept { return len_ == 0; } |
52 | | |
53 | 432k | constexpr T* begin() const noexcept { return data(); } jxl::Span<unsigned char const>::begin() const Line | Count | Source | 53 | 374k | constexpr T* begin() const noexcept { return data(); } |
jxl::Span<float>::begin() const Line | Count | Source | 53 | 57.7k | constexpr T* begin() const noexcept { return data(); } |
|
54 | | |
55 | 374k | constexpr T* end() const noexcept { return data() + size(); } |
56 | | |
57 | 0 | constexpr T& operator[](size_t i) const noexcept { |
58 | | // MSVC 2015 accepts this as constexpr, but not ptr_[i] |
59 | 0 | return *(data() + i); |
60 | 0 | } |
61 | | |
62 | 43.3k | Status remove_prefix(size_t n) noexcept { |
63 | 43.3k | JXL_ENSURE(size() >= n); |
64 | 43.3k | ptr_ += n; |
65 | 43.3k | len_ -= n; |
66 | 43.3k | return true; |
67 | 43.3k | } jxl::Span<float>::remove_prefix(unsigned long) Line | Count | Source | 62 | 43.3k | Status remove_prefix(size_t n) noexcept { | 63 | 43.3k | JXL_ENSURE(size() >= n); | 64 | 43.3k | ptr_ += n; | 65 | 43.3k | len_ -= n; | 66 | 43.3k | return true; | 67 | 43.3k | } |
Unexecuted instantiation: jxl::Span<unsigned char const>::remove_prefix(unsigned long) |
68 | | |
69 | 374k | void AppendTo(std::vector<NCT>& dst) const { |
70 | 374k | dst.insert(dst.end(), begin(), end()); |
71 | 374k | } |
72 | | |
73 | | std::vector<NCT> Copy() const { return std::vector<NCT>(begin(), end()); } |
74 | | |
75 | | private: |
76 | | T* ptr_; |
77 | | size_t len_; |
78 | | }; |
79 | | |
80 | | using Bytes = Span<const uint8_t>; |
81 | | |
82 | | } // namespace jxl |
83 | | |
84 | | #endif // LIB_JXL_BASE_SPAN_H_ |