Coverage Report

Created: 2025-07-23 08:18

/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
355k
  constexpr Span() noexcept : Span(nullptr, 0) {}
25
26
  constexpr Span(T* array, size_t length) noexcept
27
2.69M
      : ptr_(array), len_(length) {}
jxl::Span<unsigned char const>::Span(unsigned char const*, unsigned long)
Line
Count
Source
27
2.58M
      : ptr_(array), len_(length) {}
jxl::Span<float>::Span(float*, unsigned long)
Line
Count
Source
27
103k
      : 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
978
      : ptr_(reinterpret_cast<T*>(array)), len_(length) {
35
978
    static_assert(sizeof(U) == sizeof(T), "Incompatible type of source.");
36
978
  }
37
38
  template <typename ArrayLike>
39
  explicit constexpr Span(const ArrayLike& other) noexcept
40
1.75M
      : Span(reinterpret_cast<T*>(other.data()), other.size()) {
41
1.75M
    static_assert(sizeof(*other.data()) == sizeof(T),
42
1.75M
                  "Incompatible type of source.");
43
1.75M
  }
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
1.75M
      : Span(reinterpret_cast<T*>(other.data()), other.size()) {
41
1.75M
    static_assert(sizeof(*other.data()) == sizeof(T),
42
1.75M
                  "Incompatible type of source.");
43
1.75M
  }
jxl::Span<unsigned char const>::Span<jxl::PaddedBytes>(jxl::PaddedBytes const&)
Line
Count
Source
40
10
      : Span(reinterpret_cast<T*>(other.data()), other.size()) {
41
10
    static_assert(sizeof(*other.data()) == sizeof(T),
42
10
                  "Incompatible type of source.");
43
10
  }
44
45
  using NCT = typename std::remove_const<T>::type;
46
47
5.75M
  constexpr T* data() const noexcept { return ptr_; }
jxl::Span<unsigned char const>::data() const
Line
Count
Source
47
5.34M
  constexpr T* data() const noexcept { return ptr_; }
jxl::Span<float>::data() const
Line
Count
Source
47
415k
  constexpr T* data() const noexcept { return ptr_; }
48
49
2.89M
  constexpr size_t size() const noexcept { return len_; }
jxl::Span<unsigned char const>::size() const
Line
Count
Source
49
2.48M
  constexpr size_t size() const noexcept { return len_; }
jxl::Span<float>::size() const
Line
Count
Source
49
415k
  constexpr size_t size() const noexcept { return len_; }
50
51
1.00k
  constexpr bool empty() const noexcept { return len_ == 0; }
52
53
2.16M
  constexpr T* begin() const noexcept { return data(); }
jxl::Span<unsigned char const>::begin() const
Line
Count
Source
53
1.75M
  constexpr T* begin() const noexcept { return data(); }
jxl::Span<float>::begin() const
Line
Count
Source
53
415k
  constexpr T* begin() const noexcept { return data(); }
54
55
1.75M
  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
311k
  Status remove_prefix(size_t n) noexcept {
63
311k
    JXL_ENSURE(size() >= n);
64
311k
    ptr_ += n;
65
311k
    len_ -= n;
66
311k
    return true;
67
311k
  }
Unexecuted instantiation: jxl::Span<unsigned char const>::remove_prefix(unsigned long)
jxl::Span<float>::remove_prefix(unsigned long)
Line
Count
Source
62
311k
  Status remove_prefix(size_t n) noexcept {
63
311k
    JXL_ENSURE(size() >= n);
64
311k
    ptr_ += n;
65
311k
    len_ -= n;
66
311k
    return true;
67
311k
  }
68
69
1.75M
  void AppendTo(std::vector<NCT>& dst) const {
70
1.75M
    dst.insert(dst.end(), begin(), end());
71
1.75M
  }
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_