Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
zip.h
Go to the documentation of this file.
1// Copyright 2024 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CORE_FXCRT_ZIP_H_
6#define CORE_FXCRT_ZIP_H_
7
8#include <stdint.h>
9
10#include <tuple>
11#include <utility>
12
13#include "core/fxcrt/check_op.h"
14#include "core/fxcrt/compiler_specific.h"
15#include "core/fxcrt/span.h"
16
17namespace fxcrt {
18
19// Vastly simplified implementation of ideas from C++23 zip_view<>. Allows
20// safe traversal of two or three ranges with a single bounds check per
21// iteration.
22
23// Example two range usage:
24// struct RGB { uint8_t r; uint8_t g; uint8_t b; };
25// const uint8_t gray[256] = { ... };
26// RGB rgbs[260];
27// for (auto [in, out] : Zip(gray, rgbs)) {
28// out.r = in;
29// out.g = in;
30// out.b = in;
31// }
32// which fills the first 256 elements of rgbs with the corresponding gray
33// value in each component, say.
34
35// Differences include:
36// - Only zips together two or three views instead of N.
37// - Size is determined by the first view, which must be smaller than the
38// other view(s).
39// - With two views, the first view is presumed to be "input-like" and is const,
40// second view is presumed to be "output-like" and is non-const.
41// - With three views, the first two views are presumed to be "input-like" and
42// are const.
43// - Only those methods required to support use in a range-based for-loop
44// are provided.
45
46template <typename T, typename U>
47class ZipView2 {
48 public:
49 struct Iter {
50 bool operator==(const Iter& that) const { return first == that.first; }
51
52 bool operator!=(const Iter& that) const { return first != that.first; }
53
55 // SAFETY: required from caller, enforced by UNSAFE_BUFFER_USAGE.
58 return *this;
59 }
60
61 std::pair<typename T::reference, typename U::reference> operator*() const {
62 return {*first, *second};
63 }
64
65 typename T::iterator first;
66 typename U::iterator second;
67 };
68
69 ZipView2(T first, U second) : first_(first), second_(second) {
70 CHECK_LE(first.size(), second.size());
71 }
72
73 Iter begin() { return {first_.begin(), second_.begin()}; }
74 Iter end() { return {first_.end(), second_.end()}; }
75
76 private:
77 T first_;
78 U second_;
79};
80
81// Same as `ZipView2`, but with 2 inputs and 1 output.
82template <typename T, typename U, typename V>
83class ZipView3 {
84 public:
85 struct Iter {
86 bool operator==(const Iter& that) const { return first == that.first; }
87
88 bool operator!=(const Iter& that) const { return first != that.first; }
89
91 // SAFETY: required from caller, enforced by UNSAFE_BUFFER_USAGE.
95 return *this;
96 }
97
98 std::tuple<typename T::reference,
99 typename U::reference,
100 typename V::reference>
101 operator*() const {
102 return {*first, *second, *third};
103 }
104
105 typename T::iterator first;
106 typename U::iterator second;
107 typename V::iterator third;
108 };
109
110 ZipView3(T first, U second, V third)
111 : first_(first), second_(second), third_(third) {
112 CHECK_LE(first.size(), second.size());
113 CHECK_LE(first.size(), third.size());
114 }
115
116 Iter begin() { return {first_.begin(), second_.begin(), third_.begin()}; }
117 Iter end() { return {first_.end(), second_.end(), third_.end()}; }
118
119 private:
120 T first_;
121 U second_;
122 V third_;
123};
124
125template <typename T, typename U>
126auto Zip(const T& first, U&& second) {
127 return ZipView2(pdfium::span(first), pdfium::span(second));
128}
129
130template <typename T, typename U, typename V>
131auto Zip(const T& first, const U& second, V&& third) {
132 return ZipView3(pdfium::span(first), pdfium::span(second),
133 pdfium::span(third));
134}
135
136} // namespace fxcrt
137
138#endif // CORE_FXCRT_ZIP_H_
fxcrt::ByteString ByteString
Definition bytestring.h:180
#define DCHECK
Definition check.h:33
#define CHECK_LE(x, y)
Definition check_op.h:14
#define DCHECK_LT(x, y)
Definition check_op.h:19
bool IsEmpty() const
Definition cpdf_array.h:40
std::vector< RetainPtr< CPDF_Object > >::const_iterator const_iterator
Definition cpdf_array.h:29
CPDF_BasedCS(Family family)
virtual const CPDF_IndexedCS * AsIndexedCS() const
static void InitializeGlobals()
Family GetFamily() const
~CPDF_ColorSpace() override
static void DestroyGlobals()
virtual const CPDF_PatternCS * AsPatternCS() const
virtual bool IsNormal() const
virtual void EnableStdConversion(bool bEnabled)
std::vector< float > CreateBufAndSetDefaultColor() const
CPDF_ColorSpace(Family family)
static uint32_t ComponentsForFamily(Family family)
virtual void TranslateImageLine(pdfium::span< uint8_t > dest_span, pdfium::span< const uint8_t > src_span, int pixels, int image_width, int image_height, bool bTransMask) const
static RetainPtr< CPDF_ColorSpace > GetStockCSForName(const ByteString &name)
uint32_t ComponentCount() const
bool HasSameArray(const CPDF_Object *pObj) const
void SetComponentsForStockCS(uint32_t nComponents)
static RetainPtr< CPDF_ColorSpace > GetStockCS(Family family)
virtual void GetDefaultValue(int iComponent, float *value, float *min, float *max) const
~CPDF_DeviceCS() override
std::optional< FX_RGB_STRUCT< float > > GetRGB(pdfium::span< const float > pBuf) const override
void TranslateImageLine(pdfium::span< uint8_t > dest_span, pdfium::span< const uint8_t > src_span, int pixels, int image_width, int image_height, bool bTransMask) const override
uint32_t v_Load(CPDF_Document *pDoc, const CPDF_Array *pArray, std::set< const CPDF_Object * > *pVisited) override
std::map< ByteString, RetainPtr< CPDF_Object >, std::less<> > DictMap
uint32_t InputCount() const
uint32_t m_nOutputs
uint32_t m_nInputs
virtual bool v_Call(pdfium::span< const float > inputs, pdfium::span< float > results) const =0
std::vector< float > m_Ranges
virtual ~CPDF_Function()
static std::unique_ptr< CPDF_Function > Load(RetainPtr< const CPDF_Object > pFuncObj, VisitedSet *pVisited)
float GetRange(int i) const
bool Init(const CPDF_Object *pObj, VisitedSet *pVisited)
CPDF_Function(Type type)
const Type m_Type
std::vector< float > m_Domains
float GetDomain(int i) const
std::optional< uint32_t > Call(pdfium::span< const float > inputs, pdfium::span< float > results) const
virtual bool v_Init(const CPDF_Object *pObj, VisitedSet *pVisited)=0
uint32_t OutputCount() const
static std::unique_ptr< CPDF_Function > Load(RetainPtr< const CPDF_Object > pFuncObj)
float Interpolate(float x, float xmin, float xmax, float ymin, float ymax) const
bool IsNormal() const
void TranslateScanline(pdfium::span< uint8_t > pDest, pdfium::span< const uint8_t > pSrc, int pixels)
bool IsSRGB() const
void Translate(pdfium::span< const float > pSrcValues, pdfium::span< float > pDestValues)
uint32_t GetComponents() const
~CPDF_IccProfile() override
bool IsSupported() const
bool IsValid() const
const CPDF_IndexedCS * AsIndexedCS() const override
int GetMaxIndex() const
uint32_t v_Load(CPDF_Document *pDoc, const CPDF_Array *pArray, std::set< const CPDF_Object * > *pVisited) override
~CPDF_IndexedCS() override
std::optional< FX_RGB_STRUCT< float > > GetRGB(pdfium::span< const float > pBuf) const override
virtual ByteString GetString() const
bool IsName() const
const CPDF_Array * AsArray() const
const CPDF_Stream * AsStream() const
void SetComps(pdfium::span< const float > comps)
PatternValue(const PatternValue &that)
static bool IsValidIccComponents(int components)
void Translate(pdfium::span< const float > pSrcValues, pdfium::span< float > pDestValues)
static std::unique_ptr< IccTransform > CreateTransformSRGB(pdfium::span< const uint8_t > span)
void TranslateScanline(pdfium::span< uint8_t > pDest, pdfium::span< const uint8_t > pSrc, int pixels)
bool operator==(const char *ptr) const
ScopedSetInsertion & operator=(const ScopedSetInsertion &)=delete
ScopedSetInsertion(const ScopedSetInsertion &)=delete
ScopedSetInsertion(std::set< T > *org_set, const T &elem)
ZipView2(T first, U second)
Definition zip.h:69
Iter end()
Definition zip.h:74
Iter begin()
Definition zip.h:73
Iter end()
Definition zip.h:117
Iter begin()
Definition zip.h:116
ZipView3(T first, U second, V third)
Definition zip.h:110
#define UNSAFE_BUFFERS(...)
#define UNSAFE_BUFFER_USAGE
#define UNSAFE_TODO(...)
size_t Fx2DSizeOrDie(const T &w, const U &h)
Definition fx_2d_size.h:11
FXCODEC_STATUS
#define FX_DATA_PARTITION_EXCEPTION(T)
pdfium::CheckedNumeric< int32_t > FX_SAFE_INT32
constexpr uint32_t FXBSTR_ID(uint8_t c1, uint8_t c2, uint8_t c3, uint8_t c4)
Definition fx_string.h:19
void ReverseRGB(pdfium::span< uint8_t > pDestBuf, pdfium::span< const uint8_t > pSrcBuf, int pixels)
Definition fx_codec.cpp:23
auto Zip(const T &first, const U &second, V &&third)
Definition zip.h:131
auto Zip(const T &first, U &&second)
Definition zip.h:126
#define NOTREACHED_NORETURN()
Definition notreached.h:22
#define CHECK(cvref)
#define CONSTRUCT_VIA_MAKE_RETAIN
Definition retain_ptr.h:222
fxcrt::ByteStringView ByteStringView
std::pair< typename T::reference, typename U::reference > operator*() const
Definition zip.h:61
bool operator!=(const Iter &that) const
Definition zip.h:52
UNSAFE_BUFFER_USAGE Iter & operator++()
Definition zip.h:54
U::iterator second
Definition zip.h:66
T::iterator first
Definition zip.h:65
bool operator==(const Iter &that) const
Definition zip.h:50
bool operator==(const Iter &that) const
Definition zip.h:86
bool operator!=(const Iter &that) const
Definition zip.h:88
U::iterator second
Definition zip.h:106
T::iterator first
Definition zip.h:105
std::tuple< typename T::reference, typename U::reference, typename V::reference > operator*() const
Definition zip.h:101
UNSAFE_BUFFER_USAGE Iter & operator++()
Definition zip.h:90
V::iterator third
Definition zip.h:107