libjxl

FORK: libjxl patches used on blog
git clone https://git.neptards.moe/blog/libjxl.git
Log | Files | Refs | Submodules | README | LICENSE

span.h (2121B)


      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 <vector>
     15 
     16 #include "lib/jxl/base/status.h"
     17 
     18 namespace jxl {
     19 
     20 template <typename T>
     21 class Span {
     22  public:
     23   constexpr Span() noexcept : Span(nullptr, 0) {}
     24 
     25   constexpr Span(T* array, size_t length) noexcept
     26       : ptr_(array), len_(length) {}
     27 
     28   template <size_t N>
     29   explicit constexpr Span(T (&a)[N]) noexcept : Span(a, N) {}
     30 
     31   template <typename U>
     32   constexpr Span(U* array, size_t length) noexcept
     33       : ptr_(reinterpret_cast<T*>(array)), len_(length) {
     34     static_assert(sizeof(U) == sizeof(T), "Incompatible type of source.");
     35   }
     36 
     37   template <typename ArrayLike>
     38   explicit constexpr Span(const ArrayLike& other) noexcept
     39       : Span(reinterpret_cast<T*>(other.data()), other.size()) {
     40     static_assert(sizeof(*other.data()) == sizeof(T),
     41                   "Incompatible type of source.");
     42   }
     43 
     44   constexpr T* data() const noexcept { return ptr_; }
     45 
     46   constexpr size_t size() const noexcept { return len_; }
     47 
     48   constexpr bool empty() const noexcept { return len_ == 0; }
     49 
     50   constexpr T* begin() const noexcept { return data(); }
     51 
     52   constexpr T* end() const noexcept { return data() + size(); }
     53 
     54   constexpr T& operator[](size_t i) const noexcept {
     55     // MSVC 2015 accepts this as constexpr, but not ptr_[i]
     56     return *(data() + i);
     57   }
     58 
     59   void remove_prefix(size_t n) noexcept {
     60     JXL_ASSERT(size() >= n);
     61     ptr_ += n;
     62     len_ -= n;
     63   }
     64 
     65   // NCT == non-const-T; compiler will complain if NCT is not compatible with T.
     66   template <typename NCT>
     67   void AppendTo(std::vector<NCT>& dst) const {
     68     dst.insert(dst.end(), begin(), end());
     69   }
     70 
     71  private:
     72   T* ptr_;
     73   size_t len_;
     74 };
     75 
     76 typedef Span<const uint8_t> Bytes;
     77 
     78 }  // namespace jxl
     79 
     80 #endif  // LIB_JXL_BASE_SPAN_H_