| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2025 Kevin Martin <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 9 |
* under the terms of the GNU Lesser General Public License as * |
| 10 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 11 |
* License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with FreeCAD. If not, see * |
| 20 |
* <https://www.gnu.org/licenses/>. * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
|
| 25 |
#ifndef SRC_BASE_UNLIMITEDUNSIGNED_H_ |
| 26 |
#define SRC_BASE_UNLIMITEDUNSIGNED_H_ |
| 27 |
|
| 28 |
#ifndef FC_GLOBAL_H |
| 29 |
# include <FCGlobal.h> |
| 30 |
#endif |
| 31 |
#include <vector> |
| 32 |
#include <string> |
| 33 |
#include <stdexcept> |
| 34 |
|
| 35 |
// ---------------------------------------------------------------------------- |
| 36 |
|
| 37 |
namespace Base |
| 38 |
{ |
| 39 |
class UnlimitedUnsigned |
| 40 |
{ |
| 41 |
private: |
| 42 |
// This should be a signed type so we can get transient negative values during calculations |
| 43 |
// without resorting to a larger type |
| 44 |
using PartType = int32_t; |
| 45 |
// The following must be a type that limits the value to >= 0 and < maxPartPlusOne |
| 46 |
using SmallDeltaType = unsigned char; |
| 47 |
explicit UnlimitedUnsigned(std::vector<PartType>&& partsVector) |
| 48 |
: parts(std::move(partsVector)) |
| 49 |
{} |
| 50 |
// We take a really cheap floor(bitcount * log10(max PartType)). 3/10 is just a bit less |
| 51 |
// than log10(2) The value should be sizeof(PartType) * CHAR_BIT * 3 / 10 but we can't |
| 52 |
// calculate the corresponding maxPartPlusOne in a static const initializer, so we just wire |
| 53 |
// in the values for 4-byte PartType. |
| 54 |
static const size_t partDigitCount = 9; |
| 55 |
static const PartType maxPartPlusOne = 1000000000; // (PartType)pow(10, partDigitCount); but |
| 56 |
// can't call pow in a const ctor. |
| 57 |
|
| 58 |
public: |
| 59 |
explicit UnlimitedUnsigned(SmallDeltaType value) |
| 60 |
: parts(std::vector<PartType>(1, value)) |
| 61 |
{} |
| 62 |
// Parse a decimal digit string into an UnlimitedUnsigned. There should be no white space, |
| 63 |
// only digits. a zero-length string parses as zero. |
| 64 |
static UnlimitedUnsigned fromString(const std::string& text) |
| 65 |
{ |
| 66 |
std::vector<PartType> result((text.size() + partDigitCount - 1) / partDigitCount); |
| 67 |
size_t minimumSize = 0; |
| 68 |
size_t lastStartPosition = text.size(); |
| 69 |
// Parse chunks of digits starting with the least significant |
| 70 |
for (size_t i = 0; i < result.size(); i++) { |
| 71 |
if (partDigitCount >= lastStartPosition) { |
| 72 |
// partial chunk of leading digits |
| 73 |
result[i] = static_cast<PartType>(std::stoul(text.substr(0, lastStartPosition))); |
| 74 |
} |
| 75 |
else { |
| 76 |
lastStartPosition -= partDigitCount; |
| 77 |
result[i] = static_cast<PartType>( |
| 78 |
std::stoul(text.substr(lastStartPosition, partDigitCount)) |
| 79 |
); |
| 80 |
} |
| 81 |
if (result[i] != 0) { |
| 82 |
minimumSize = i + 1; |
| 83 |
} |
| 84 |
} |
| 85 |
result.resize(minimumSize); |
| 86 |
return UnlimitedUnsigned(std::move(result)); |
| 87 |
} |
| 88 |
std::string toString() |
| 89 |
{ |
| 90 |
std::string result; |
| 91 |
result.reserve(parts.size() * partDigitCount); |
| 92 |
// Format the chunks from most- to least-significant |
| 93 |
for (auto it = parts.rbegin(); it != parts.rend(); it++) { |
| 94 |
std::string partDigits = std::to_string(*it); |
| 95 |
if (!result.empty()) { |
| 96 |
size_t padding = partDigitCount - partDigits.size(); |
| 97 |
if (padding > 0) { |
| 98 |
result += std::string(padding, '0'); |
| 99 |
} |
| 100 |
} |
| 101 |
result += partDigits; |
| 102 |
} |
| 103 |
return result; |
| 104 |
} |
| 105 |
// TODO: Some allocation of vector contents could be avoided by defining methods like |
| 106 |
// static UnlimitedUnsigned operator+(UnlimitedUnsigned&& left, const UnlimitedUnsigned& |
| 107 |
// right); static UnlimitedUnsigned operator+(const UnlimitedUnsigned& left, |
| 108 |
// UnlimitedUnsigned&& right); static UnlimitedUnsigned operator+(UnlimitedUnsigned&& left, |
| 109 |
// UnlimitedUnsigned&& right); which would reuse left.parts or right.parts after possibly |
| 110 |
// growing it. The last one would use the larger of the two buffers. |
| 111 |
UnlimitedUnsigned operator+(const UnlimitedUnsigned& right) const |
| 112 |
{ |
| 113 |
size_t resultSize = std::max(parts.size(), right.parts.size()); |
| 114 |
std::vector<PartType> result(resultSize); |
| 115 |
PartType carry = 0; |
| 116 |
for (size_t i = 0; i < resultSize; i++) { |
| 117 |
auto newPart = (i < parts.size() ? parts[i] : 0) |
| 118 |
+ (i < right.parts.size() ? right.parts[i] : 0) + carry; |
| 119 |
if (newPart < maxPartPlusOne) { |
| 120 |
carry = 0; |
| 121 |
} |
| 122 |
else { |
| 123 |
carry = 1; |
| 124 |
newPart -= maxPartPlusOne; |
| 125 |
} |
| 126 |
result[i] = newPart; |
| 127 |
} |
| 128 |
if (carry > 0) { |
| 129 |
result.resize(resultSize + 1); |
| 130 |
result[resultSize] = carry; |
| 131 |
} |
| 132 |
return UnlimitedUnsigned(std::move(result)); |
| 133 |
} |
| 134 |
UnlimitedUnsigned operator+(SmallDeltaType right) const |
| 135 |
{ |
| 136 |
size_t resultSize = parts.size(); |
| 137 |
std::vector<PartType> result(resultSize); |
| 138 |
PartType carry = right; |
| 139 |
for (size_t i = 0; i < resultSize; i++) { |
| 140 |
auto newPart = parts[i] + carry; |
| 141 |
if (newPart < maxPartPlusOne) { |
| 142 |
carry = 0; |
| 143 |
} |
| 144 |
else { |
| 145 |
carry = 1; |
| 146 |
newPart -= maxPartPlusOne; |
| 147 |
} |
| 148 |
result[i] = newPart; |
| 149 |
} |
| 150 |
if (carry > 0) { |
| 151 |
result.resize(resultSize + 1); |
| 152 |
result[resultSize] = carry; |
| 153 |
} |
| 154 |
return UnlimitedUnsigned(std::move(result)); |
| 155 |
} |
| 156 |
UnlimitedUnsigned operator-(const UnlimitedUnsigned& right) const |
| 157 |
{ |
| 158 |
size_t resultSize = std::max(parts.size(), right.parts.size()); |
| 159 |
std::vector<PartType> result(resultSize); |
| 160 |
PartType borrow = 0; |
| 161 |
size_t lastNonZero = 0; |
| 162 |
for (size_t i = 0; i < resultSize; i++) { |
| 163 |
auto newPart = (i < parts.size() ? parts[i] : 0) |
| 164 |
- (i < right.parts.size() ? right.parts[i] : 0) - borrow; |
| 165 |
if (newPart >= 0) { |
| 166 |
borrow = 0; |
| 167 |
} |
| 168 |
else { |
| 169 |
borrow = 1; |
| 170 |
newPart += maxPartPlusOne; |
| 171 |
} |
| 172 |
result[i] = newPart; |
| 173 |
if (newPart != 0) { |
| 174 |
lastNonZero = i + 1; |
| 175 |
} |
| 176 |
} |
| 177 |
if (borrow > 0) { |
| 178 |
throw std::overflow_error("UnlimitedUnsigned arithmetic produced a negative result"); |
| 179 |
} |
| 180 |
result.resize(lastNonZero); |
| 181 |
return UnlimitedUnsigned(std::move(result)); |
| 182 |
} |
| 183 |
UnlimitedUnsigned operator-(SmallDeltaType right) const |
| 184 |
{ |
| 185 |
size_t resultSize = parts.size(); |
| 186 |
std::vector<PartType> result(resultSize); |
| 187 |
PartType borrow = right; |
| 188 |
for (size_t i = 0; i < resultSize; i++) { |
| 189 |
auto newPart = parts[i] - borrow; |
| 190 |
if (newPart >= 0) { |
| 191 |
borrow = 0; |
| 192 |
} |
| 193 |
else { |
| 194 |
borrow = 1; |
| 195 |
newPart += maxPartPlusOne; |
| 196 |
} |
| 197 |
result[i] = newPart; |
| 198 |
} |
| 199 |
if (borrow > 0) { |
| 200 |
throw std::overflow_error("UnlimitedUnsigned arithmetic produced a negative result"); |
| 201 |
} |
| 202 |
// Note that by here resultSize > 0, otherwise the original this == 0 and we would have |
| 203 |
// the above exception. |
| 204 |
if (result[resultSize - 1] == 0) { |
| 205 |
result.resize(resultSize - 1); |
| 206 |
} |
| 207 |
return UnlimitedUnsigned(std::move(result)); |
| 208 |
} |
| 209 |
bool operator<=(const UnlimitedUnsigned& right) const |
| 210 |
{ |
| 211 |
// lexicographical_compare is logically a < operation, so we reverse the operands and |
| 212 |
// invert the result to get <= and we compare the most significant chunks first. |
| 213 |
return parts.size() < right.parts.size() |
| 214 |
|| (parts.size() == right.parts.size() |
| 215 |
&& !std::lexicographical_compare( |
| 216 |
right.parts.rbegin(), |
| 217 |
right.parts.rend(), |
| 218 |
parts.rbegin(), |
| 219 |
parts.rend() |
| 220 |
)); |
| 221 |
} |
| 222 |
bool operator>(const UnlimitedUnsigned& right) const |
| 223 |
{ |
| 224 |
return !(*this <= right); |
| 225 |
} |
| 226 |
bool operator==(const UnlimitedUnsigned& right) const |
| 227 |
{ |
| 228 |
return parts == right.parts; |
| 229 |
} |
| 230 |
bool operator==(SmallDeltaType right) const |
| 231 |
{ |
| 232 |
return right == 0 ? parts.empty() : (parts.size() == 1 && parts[0] == right); |
| 233 |
} |
| 234 |
|
| 235 |
private: |
| 236 |
std::vector<PartType> parts; |
| 237 |
}; |
| 238 |
} // namespace Base |
| 239 |
|
| 240 |
#endif // SRC_BASE_UNLIMITEDUNSIGNED_H_ |