comparison mupdf-source/thirdparty/zxing-cpp/core/src/GenericGFPoly.h @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /*
2 * Copyright 2016 Nu-book Inc.
3 * Copyright 2016 ZXing authors
4 */
5 // SPDX-License-Identifier: Apache-2.0
6
7 #pragma once
8
9 #include "ZXAlgorithms.h"
10
11 #include <algorithm>
12 #include <cassert>
13 #include <cstddef>
14 #include <vector>
15
16 namespace ZXing {
17
18 class GenericGF;
19
20 /**
21 * <p>Represents a polynomial whose coefficients are elements of a GF.
22 * Instances of this class are immutable.</p>
23 *
24 * <p>Much credit is due to William Rucklidge since portions of this code are an indirect
25 * port of his C++ Reed-Solomon implementation.</p>
26 *
27 * @author Sean Owen
28 */
29 class GenericGFPoly
30 {
31 struct Coefficients : public std::vector<int>
32 {
33 void reserve(size_t s)
34 {
35 if (capacity() < s)
36 std::vector<int>::reserve(std::max(size_t(32), s));
37 }
38
39 void resize(size_t s)
40 {
41 reserve(s);
42 std::vector<int>::resize(s);
43 }
44
45 void resize(size_t s, int i)
46 {
47 reserve(s);
48 std::vector<int>::resize(s, i);
49 }
50 };
51
52 public:
53 // Build a invalid object, so that this can be used in container or return by reference,
54 // any access to invalid object is undefined behavior.
55 GenericGFPoly() = default;
56
57 /**
58 * @param field the {@link GenericGF} instance representing the field to use
59 * to perform computations
60 * @param coefficients coefficients as ints representing elements of GF(size), arranged
61 * from most significant (highest-power term) coefficient to least significant
62 */
63 GenericGFPoly(const GenericGF& field, std::vector<int>&& coefficients) : _field(&field)
64 {
65 assert(!coefficients.empty());
66 _coefficients.swap(coefficients); // _coefficients = coefficients
67 normalize();
68 }
69 GenericGFPoly(const GenericGF& field, const std::vector<int>& coefficients) : GenericGFPoly(field, std::vector<int>(coefficients)) {}
70
71 GenericGFPoly& operator=(GenericGFPoly&& other) noexcept = default;
72 GenericGFPoly(GenericGFPoly&& other) noexcept = default;
73
74 GenericGFPoly& operator=(const GenericGFPoly& other) {
75 assert(_field == other._field);
76 _coefficients.reserve(other._coefficients.size());
77 _coefficients = other._coefficients;
78 return *this;
79 }
80
81 GenericGFPoly(const GenericGFPoly& other) {
82 _field = other._field;
83 *this = other;
84 }
85
86 GenericGFPoly& setField(const GenericGF& field)
87 {
88 _field = &field;
89 return *this;
90 }
91 const GenericGF& field() const noexcept { return *_field; }
92 const auto& coefficients() const noexcept { return _coefficients; }
93
94 /**
95 * @return degree of this polynomial
96 */
97 int degree() const {
98 return Size(_coefficients) - 1;
99 }
100
101 /**
102 * @return true iff this polynomial is the monomial "0"
103 */
104 bool isZero() const {
105 return _coefficients[0] == 0;
106 }
107
108 int leadingCoefficient() const noexcept {
109 return _coefficients.front();
110 }
111
112 int constant() const noexcept {
113 return _coefficients.back();
114 }
115
116 /**
117 * @brief set to the monomial representing coefficient * x^degree
118 */
119 GenericGFPoly& setMonomial(int coefficient, int degree = 0)
120 {
121 assert(degree >= 0 && (coefficient != 0 || degree == 0));
122
123 _coefficients.resize(degree + 1);
124 std::fill(_coefficients.begin(), _coefficients.end(), 0);
125 _coefficients.front() = coefficient;
126
127 return *this;
128 }
129
130 /**
131 * @return evaluation of this polynomial at a given point
132 */
133 int evaluateAt(int a) const;
134
135 GenericGFPoly& addOrSubtract(GenericGFPoly& other);
136 GenericGFPoly& multiply(const GenericGFPoly& other);
137 GenericGFPoly& multiplyByMonomial(int coefficient, int degree = 0);
138 GenericGFPoly& divide(const GenericGFPoly& other, GenericGFPoly& quotient);
139
140 friend void swap(GenericGFPoly& a, GenericGFPoly& b)
141 {
142 std::swap(a._field, b._field);
143 std::swap(a._coefficients, b._coefficients);
144 }
145
146 private:
147 void normalize();
148
149 const GenericGF* _field = nullptr;
150 Coefficients _coefficients, _cache; // _cache is used for malloc caching
151 };
152
153 } // ZXing