Coverage Report

Created: 2026-07-25 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/base/span.h
Line
Count
Source
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
211k
  constexpr Span() noexcept : Span(nullptr, 0) {}
jxl::Span<jxl::QuantizedSpline const>::Span()
Line
Count
Source
24
21.5k
  constexpr Span() noexcept : Span(nullptr, 0) {}
jxl::Span<jxl::Spline::Point const>::Span()
Line
Count
Source
24
21.5k
  constexpr Span() noexcept : Span(nullptr, 0) {}
jxl::Span<unsigned char const>::Span()
Line
Count
Source
24
168k
  constexpr Span() noexcept : Span(nullptr, 0) {}
25
26
  constexpr Span(T* array, size_t length) noexcept
27
573k
      : ptr_(array), len_(length) {}
jxl::Span<unsigned char const>::Span(unsigned char const*, unsigned long)
Line
Count
Source
27
528k
      : ptr_(array), len_(length) {}
jxl::Span<jxl::QuantizedSpline const>::Span(jxl::QuantizedSpline const*, unsigned long)
Line
Count
Source
27
22.1k
      : ptr_(array), len_(length) {}
jxl::Span<jxl::Spline::Point const>::Span(jxl::Spline::Point const*, unsigned long)
Line
Count
Source
27
22.1k
      : ptr_(array), len_(length) {}
Unexecuted instantiation: jxl::Span<unsigned char>::Span(unsigned char*, unsigned long)
Unexecuted instantiation: jxl::Span<hwy::float16_t>::Span(hwy::float16_t*, unsigned long)
Unexecuted instantiation: jxl::Span<float>::Span(float*, unsigned long)
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
33
      : ptr_(reinterpret_cast<T*>(array)), len_(length) {
35
33
    static_assert(sizeof(U) == sizeof(T), "Incompatible type of source.");
36
33
  }
37
38
  template <typename ArrayLike>
39
  explicit constexpr Span(const ArrayLike& other) noexcept
40
168k
      : Span(reinterpret_cast<T*>(other.data()), other.size()) {
41
168k
    static_assert(sizeof(*other.data()) == sizeof(T),
42
168k
                  "Incompatible type of source.");
43
168k
  }
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
163k
      : Span(reinterpret_cast<T*>(other.data()), other.size()) {
41
163k
    static_assert(sizeof(*other.data()) == sizeof(T),
42
163k
                  "Incompatible type of source.");
43
163k
  }
jxl::Span<unsigned char const>::Span<jxl::PaddedBytes>(jxl::PaddedBytes const&)
Line
Count
Source
40
3.26k
      : Span(reinterpret_cast<T*>(other.data()), other.size()) {
41
3.26k
    static_assert(sizeof(*other.data()) == sizeof(T),
42
3.26k
                  "Incompatible type of source.");
43
3.26k
  }
jxl::Span<jxl::QuantizedSpline const>::Span<std::__1::vector<jxl::QuantizedSpline, std::__1::allocator<jxl::QuantizedSpline> > >(std::__1::vector<jxl::QuantizedSpline, std::__1::allocator<jxl::QuantizedSpline> > const&)
Line
Count
Source
40
563
      : Span(reinterpret_cast<T*>(other.data()), other.size()) {
41
563
    static_assert(sizeof(*other.data()) == sizeof(T),
42
563
                  "Incompatible type of source.");
43
563
  }
jxl::Span<jxl::Spline::Point const>::Span<std::__1::vector<jxl::Spline::Point, std::__1::allocator<jxl::Spline::Point> > >(std::__1::vector<jxl::Spline::Point, std::__1::allocator<jxl::Spline::Point> > const&)
Line
Count
Source
40
563
      : Span(reinterpret_cast<T*>(other.data()), other.size()) {
41
563
    static_assert(sizeof(*other.data()) == sizeof(T),
42
563
                  "Incompatible type of source.");
43
563
  }
44
45
  using NCT = typename std::remove_const<T>::type;
46
47
931k
  constexpr T* data() const noexcept { return ptr_; }
jxl::Span<unsigned char const>::data() const
Line
Count
Source
47
930k
  constexpr T* data() const noexcept { return ptr_; }
jxl::Span<jxl::QuantizedSpline const>::data() const
Line
Count
Source
47
559
  constexpr T* data() const noexcept { return ptr_; }
jxl::Span<jxl::Spline::Point const>::data() const
Line
Count
Source
47
559
  constexpr T* data() const noexcept { return ptr_; }
Unexecuted instantiation: jxl::Span<hwy::float16_t>::data() const
Unexecuted instantiation: jxl::Span<unsigned char>::data() const
Unexecuted instantiation: jxl::Span<float>::data() const
48
49
385k
  constexpr size_t size() const noexcept { return len_; }
jxl::Span<unsigned char const>::size() const
Line
Count
Source
49
384k
  constexpr size_t size() const noexcept { return len_; }
jxl::Span<jxl::QuantizedSpline const>::size() const
Line
Count
Source
49
1.11k
  constexpr size_t size() const noexcept { return len_; }
Unexecuted instantiation: jxl::Span<jxl::Spline::Point const>::size() const
Unexecuted instantiation: jxl::Span<float>::size() const
50
51
563
  constexpr bool empty() const noexcept { return len_ == 0; }
jxl::Span<jxl::QuantizedSpline const>::empty() const
Line
Count
Source
51
563
  constexpr bool empty() const noexcept { return len_ == 0; }
Unexecuted instantiation: jxl::Span<unsigned char const>::empty() const
52
53
166k
  constexpr T* begin() const noexcept { return data(); }
jxl::Span<unsigned char const>::begin() const
Line
Count
Source
53
166k
  constexpr T* begin() const noexcept { return data(); }
Unexecuted instantiation: jxl::Span<jxl::QuantizedSpline const>::begin() const
Unexecuted instantiation: jxl::Span<float>::begin() const
54
55
166k
  constexpr T* end() const noexcept { return data() + size(); }
jxl::Span<unsigned char const>::end() const
Line
Count
Source
55
166k
  constexpr T* end() const noexcept { return data() + size(); }
Unexecuted instantiation: jxl::Span<jxl::QuantizedSpline const>::end() const
56
57
1.11k
  constexpr T& operator[](size_t i) const noexcept {
58
    // MSVC 2015 accepts this as constexpr, but not ptr_[i]
59
1.11k
    return *(data() + i);
60
1.11k
  }
Unexecuted instantiation: jxl::Span<unsigned char const>::operator[](unsigned long) const
jxl::Span<jxl::QuantizedSpline const>::operator[](unsigned long) const
Line
Count
Source
57
559
  constexpr T& operator[](size_t i) const noexcept {
58
    // MSVC 2015 accepts this as constexpr, but not ptr_[i]
59
559
    return *(data() + i);
60
559
  }
jxl::Span<jxl::Spline::Point const>::operator[](unsigned long) const
Line
Count
Source
57
559
  constexpr T& operator[](size_t i) const noexcept {
58
    // MSVC 2015 accepts this as constexpr, but not ptr_[i]
59
559
    return *(data() + i);
60
559
  }
Unexecuted instantiation: jxl::Span<hwy::float16_t>::operator[](unsigned long) const
Unexecuted instantiation: jxl::Span<unsigned char>::operator[](unsigned long) const
61
62
0
  Status remove_prefix(size_t n) noexcept {
63
0
    JXL_ENSURE(size() >= n);
64
0
    ptr_ += n;
65
0
    len_ -= n;
66
0
    return true;
67
0
  }
Unexecuted instantiation: jxl::Span<unsigned char const>::remove_prefix(unsigned long)
Unexecuted instantiation: jxl::Span<float>::remove_prefix(unsigned long)
68
69
166k
  void AppendTo(std::vector<NCT>& dst) const {
70
166k
    dst.insert(dst.end(), begin(), end());
71
166k
  }
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_