1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
|
/**************************************************************************
* *
* Regina - A Normal Surface Theory Calculator *
* Computational Engine *
* *
* Copyright (c) 1999-2025, Ben Burton *
* For further details contact Ben Burton (bab@debian.org). *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* As an exception, when this program is distributed through (i) the *
* App Store by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or *
* (iii) Google Play by Google Inc., then that store may impose any *
* digital rights management, device limits and/or redistribution *
* restrictions that are required by its terms of service. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#include "link/tangle.h"
#include "utilities/stringutils.h"
#include <cctype>
#include <climits>
#include <iterator>
// We wish to use std::to_chars(), but GCC only implements it in gcc-8.1.
// We will need a fallback option for (in particular) gcc-7.
//
// We only test the presence of the <charconv> include, not the feature test
// macro __cpp_lib_to_chars, since although gcc-8.1 implements std::to_chars()
// for integer types (which is enough for us), a full implementation for all
// types (with the feature test macro now defined) only appeared in gcc-11.1.
//
// Note that macOS provides <charconv> but only supports std::to_chars() when
// targeting macOS 10.15 or above. However, macOS 10.14 is now past its
// end-of-life (as of October 2021); moreover, other parts of Regina also
// require macOS 10.15 (e.g., parts that use std::filesystem::path).
// So on macOS we will assume that std::to_chars() is available.
//
#if __has_include(<charconv>)
#include <charconv>
// We can use std::to_chars().
#define __regina_has_to_chars 1
#else
#warning This compiler does not support std::to_chars().
// Fall back to snprintf().
#include <cstdio>
#undef __regina_has_to_chars
#endif
namespace regina {
Link Link::fromGauss(const std::string& s) {
// Work with the largest integer type that we could possibly need.
using Int = std::make_signed_t<size_t>;
std::istringstream in(s);
std::vector<Int> terms;
Int i;
while (true) {
in >> i;
if (! in) {
if (in.eof())
break;
throw InvalidArgument("fromGauss(): invalid integer in sequence");
}
terms.push_back(i);
}
return fromGauss(terms.begin(), terms.end());
}
Link Link::fromOrientedGauss(const std::string& s) {
std::vector<std::string> terms = basicTokenise(s);
return fromOrientedGauss(terms.begin(), terms.end());
}
Link Link::fromSignedGauss(const std::string& s) {
// TODO: Make this a vector of substrings, now we have C++20.
std::vector<std::string> terms;
std::string::size_type len = s.length();
std::string::size_type pos = 0;
// Skip initial whitespace.
while (pos < len && isspace(s[pos]))
++pos;
if (pos == len)
return { 1 }; // Zero-crossing unknot
// Extract each token.
std::string::size_type tokStart;
while (pos < len) {
// Find the characters making up this token.
tokStart = pos;
while (pos < len && s[pos] != '+' && s[pos] != '-')
++pos;
if (pos == len) {
// We never found the terminating sign.
// The only scenario where this is acceptable is if the leftover
// characters are entirely whitespace.
for ( ; tokStart < len; ++tokStart)
if (! isspace(s[tokStart]))
throw InvalidArgument("fromSignedGauss(): unexpected "
"characters at the end of the code");
// We're fine; this was legitimately the end of the list of terms.
break;
}
++pos;
terms.push_back(s.substr(tokStart, pos - tokStart));
}
// Now build the link!
return fromSignedGauss(terms.begin(), terms.end());
}
bool Link::parseOrientedGaussTerm(const std::string& s,
size_t nCross, size_t& crossing, int& strand, int& sign) {
if (s.length() < 3)
return false;
if (s[0] == '+')
strand = 1;
else if (s[0] == '-')
strand = 0;
else
return false;
if (s[1] == '<')
sign = (strand == 1 ? 1 : -1);
else if (s[1] == '>')
sign = (strand == 1 ? -1 : 1);
else
return false;
if (! valueOf(s.substr(2), crossing))
return false;
return (crossing > 0 && crossing <= nCross);
}
bool Link::parseSignedGaussTerm(const std::string& s,
size_t nCross, size_t& crossing, int& strand, int& sign) {
size_t len = s.length();
if (len < 3)
return false;
if (s[0] == 'U' || s[0] == 'u')
strand = 0;
else if (s[0] == 'O' || s[0] == 'o')
strand = 1;
else
return false;
if (s[len - 1] == '+')
sign = 1;
else if (s[len - 1] == '-')
sign = -1;
else
return false;
if (! valueOf(s.substr(1, len - 2), crossing))
return false;
return (crossing > 0 && crossing <= nCross);
}
std::string Link::gauss() const {
std::ostringstream out;
gauss(out);
return out.str();
}
void Link::gauss(std::ostream& out) const {
if (components_.size() != 1)
throw NotImplemented(
"Gauss codes are only implemented for single-component links");
if (crossings_.empty())
return;
StrandRef start = components_.front();
StrandRef s = start;
do {
if (s != start)
out << ' ';
if (s.strand() == 0)
out << '-';
out << (s.crossing()->index() + 1);
++s;
} while (s != start);
}
std::vector<int> Link::gaussData() const {
if (components_.size() != 1)
throw NotImplemented(
"Gauss codes are only implemented for single-component links");
if (crossings_.empty())
return {};
if (crossings_.size() > INT_MAX)
throw NotImplemented("This Gauss code has entries that cannot "
"fit into a C++ int");
std::vector<int> ans;
ans.reserve(crossings_.size() * 2);
StrandRef start = components_.front();
StrandRef s = start;
do {
if (s.strand() == 0)
ans.push_back(- static_cast<int>(s.crossing()->index() + 1));
else
ans.push_back(static_cast<int>(s.crossing()->index() + 1));
++s;
} while (s != start);
return ans;
}
std::string Link::orientedGauss() const {
std::ostringstream out;
orientedGauss(out);
return out.str();
}
void Link::orientedGauss(std::ostream& out) const {
if (components_.size() != 1)
throw NotImplemented(
"Gauss codes are only implemented for single-component links");
if (crossings_.empty())
return;
StrandRef start = components_.front();
StrandRef s = start;
do {
if (s != start)
out << ' ';
if (s.strand() == 0)
out << '-';
else
out << '+';
if ((s.strand() == 0 && s.crossing()->sign() < 0) ||
(s.strand() == 1 && s.crossing()->sign() > 0))
out << '<';
else
out << '>';
out << (s.crossing()->index() + 1);
++s;
} while (s != start);
}
std::vector<std::string> Link::orientedGaussData() const {
if (components_.size() != 1)
throw NotImplemented(
"Gauss codes are only implemented for single-component links");
if (crossings_.empty())
return {};
std::vector<std::string> ans;
ans.reserve(2 * crossings_.size());
// It seems safe to use 2^64 as an upper bound on the number of crossings.
// On typical machines, size_t should not exceed this; moreover, even if
// we did have more crossings than this, none of Regina's algorithms
// would ever finish for a knot of this size.
//
// Since 2^64 is a 20-digit number in base 10, this gives a maximum token
// length of 22 (allowing for the prefixes +> +< -> -<).
static constexpr int maxTokenLen = 22;
char token[maxTokenLen + 1]; // allow for null termination
StrandRef start = components_.front();
StrandRef s = start;
do {
token[0] = (s.strand() == 0 ? '-' : '+');
token[1] = ((s.strand() == 0 && s.crossing()->sign() < 0) ||
(s.strand() == 1 && s.crossing()->sign() > 0) ? '<' : '>');
#if __regina_has_to_chars
auto result = std::to_chars(token + 2, token + maxTokenLen,
s.crossing()->index() + 1);
if (result.ec != std::errc())
throw ImpossibleScenario("Could not convert crossing index "
" to a string via std::to_chars()");
*result.ptr = 0;
#else
int result = snprintf(token + 2,
maxTokenLen - 1 /* includes null terminator */, "%zu",
s.crossing()->index() + 1);
if (result < 0 || result >= maxTokenLen - 1)
throw ImpossibleScenario("Could not convert crossing index "
" to a string via snprintf()");
#endif
ans.emplace_back(token);
++s;
} while (s != start);
return ans;
}
std::string Tangle::orientedGauss() const {
std::ostringstream out;
orientedGauss(out);
return out.str();
}
void Tangle::orientedGauss(std::ostream& out) const {
out << type_;
for (int i = 0; i < 2; ++i) {
for (StrandRef s = end_[i][0]; s; ++s) {
out << ' ';
if (s.strand() == 0)
out << '-';
else
out << '+';
if ((s.strand() == 0 && s.crossing()->sign() < 0) ||
(s.strand() == 1 && s.crossing()->sign() > 0))
out << '<';
else
out << '>';
out << (s.crossing()->index() + 1);
}
if (i == 0)
out << " _";
}
}
Tangle Tangle::fromOrientedGauss(const std::string& s) {
std::vector<std::string> terms = basicTokenise(s);
return fromOrientedGauss(terms.begin(), terms.end());
}
std::string Link::signedGauss() const {
std::ostringstream out;
signedGauss(out);
return out.str();
}
void Link::signedGauss(std::ostream& out) const {
if (components_.size() != 1)
throw NotImplemented(
"Gauss codes are only implemented for single-component links");
if (crossings_.empty())
return;
StrandRef start = components_.front();
StrandRef s = start;
do {
if (s.strand() == 0)
out << 'U';
else
out << 'O';
out << (s.crossing()->index() + 1);
if (s.crossing()->sign() > 0)
out << '+';
else
out << '-';
++s;
} while (s != start);
}
std::vector<std::string> Link::signedGaussData() const {
if (components_.size() != 1)
throw NotImplemented(
"Gauss codes are only implemented for single-component links");
if (crossings_.empty())
return {};
std::vector<std::string> ans;
ans.reserve(2 * crossings_.size());
// It seems safe to use 2^64 as an upper bound on the number of crossings.
// On typical machines, size_t should not exceed this; moreover, even if
// we did have more crossings than this, none of Regina's algorithms
// would ever finish for a knot of this size.
//
// Since 2^64 is a 20-digit number in base 10, this gives a maximum token
// length of 22 (allowing for the prefix U/O and the suffix +/-).
static constexpr int maxTokenLen = 22;
char token[maxTokenLen + 1]; // allow for null termination
StrandRef start = components_.front();
StrandRef s = start;
do {
token[0] = (s.strand() == 0 ? 'U' : 'O');
#if __regina_has_to_chars
auto result = std::to_chars(token + 1, token + maxTokenLen - 1,
s.crossing()->index() + 1);
if (result.ec != std::errc())
throw ImpossibleScenario("Could not convert crossing index "
" to a string via std::to_chars()");
*(result.ptr++) = (s.crossing()->sign() > 0 ? '+' : '-');
*result.ptr = 0;
#else
int result = snprintf(token + 1,
maxTokenLen - 2 /* includes null terminator */, "%zu",
s.crossing()->index() + 1);
if (result < 0 || result >= maxTokenLen - 1)
throw ImpossibleScenario("Could not convert crossing index "
" to a string via snprintf()");
token[result + 1] = (s.crossing()->sign() > 0 ? '+' : '-');
token[result + 2] = 0;
#endif
ans.emplace_back(token);
++s;
} while (s != start);
return ans;
}
} // namespace regina
|