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
|
/**************************************************************************
* *
* 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/>. *
* *
**************************************************************************/
/*! \file link/pd-impl.h
* \brief Contains implementation details for parsing planar diagram codes
* of links.
*
* This file is automatically included from link.h; there is no need
* for end users to include it explicitly.
*/
#ifndef __REGINA_PD_IMPL_H
#ifndef __DOXYGEN
#define __REGINA_PD_IMPL_H
#endif
#include <algorithm>
#include "utilities/fixedarray.h"
namespace regina {
template <typename Iterator>
Link Link::fromPD(Iterator begin, Iterator end) {
using InputInt = std::remove_cv_t<std::remove_reference_t<
decltype((*begin)[0])>>;
static_assert(std::is_integral_v<InputInt> &&
! std::is_unsigned_v<InputInt>, "fromPD(): the iterator type "
"needs to refer to a native signed C++ integer type.");
// Extract the number of crossings.
size_t n = end - begin;
if (n == 0) {
// PD codes do not handle zero-crossing unknots.
// Just return nothing at all.
return {};
}
if constexpr (sizeof(InputInt) <= sizeof(size_t)) {
if (2 * n > static_cast<size_t>(std::numeric_limits<InputInt>::max()))
throw InvalidArgument("fromPD(): too many crossings for "
"the given integer type");
}
const auto maxStrand = static_cast<InputInt>(2 * n);
// Represents (crossing index, position in 4-tuple):
using PDPos = std::pair<size_t, int>;
// The two occurrences of each strand in the PD code:
using PDOccurrence = std::pair<PDPos, PDPos>;
// The zero-based strand numbers that will begin each component:
std::vector<InputInt> components;
// Identify the two crossings that each strand meets.
// A position of -1 in the 4-tuple means "not yet seen".
FixedArray<PDOccurrence> occ(2 * n);
for (size_t i = 0; i < 2 * n; ++i)
occ[i].first.second = occ[i].second.second = -1;
size_t index;
Iterator it;
for (it = begin, index = 0; it != end; ++it, ++index) {
for (int i = 0; i < 4; ++i) {
InputInt s = (*it)[i];
if (s <= 0 || s > maxStrand) {
throw InvalidArgument("fromPD(): strand out of range");
}
auto o = occ.begin() + (s - 1);
if (o->first.second < 0) {
o->first.first = index;
o->first.second = i;
} else if (o->second.second < 0) {
o->second.first = index;
o->second.second = i;
} else {
throw InvalidArgument(
"fromPD(): strand appears more than twice");
}
}
}
// Identify the direction of each strand.
// 0 means unknown;
// 1 means first occurrence -> second occurrence;
// -1 means second occurrence -> first occurrence.
FixedArray<int> dir(2 * n, 0);
// First walk through the crossings and work out what we can.
for (it = begin, index = 0; it != end; ++it, ++index) {
// Examine the incoming lower strand (which is the only one
// whose direction is predetermined):
InputInt start = (*it)[0] - 1;
if (dir[start]) {
// We have already processed this strand (and the entire
// component that contains it).
continue;
}
// We know that start is the incoming lower strand.
// Follow this component around and identify the directions of
// all strands on the component.
// As we do this, we will also collect the minimum strand label on the
// component (which will become its starting point).
PDPos pos { index, 0 };
dir[start] = (occ[start].first == pos ? -1 : 1);
InputInt min = start;
InputInt s = start;
while (true) {
// Move s forward to the next strand on this component.
if (dir[s] > 0)
pos = occ[s].second;
else
pos = occ[s].first;
pos.second ^= 2;
s = (*(begin + pos.first))[pos.second] - 1;
if (s == start)
break;
// Since we already know each strand appears exactly twice,
// dir[s] should be unknown at this point. Update it.
dir[s] = (occ[s].first == pos ? 1 : -1);
if (s < min)
min = s;
}
// This finishes the current component.
// Collect its starting point.
components.push_back(min);
}
// Look for any components that haven't been processed (because they
// consist entirely of overcrossings, and so the PD code does not
// define their orientation).
for (it = begin, index = 0; it != end; ++it, ++index) {
// This time we look at one of the two (connected) upper strands.
InputInt start = (*it)[1] - 1;
if (dir[start])
continue;
// We found a component that has not been processed.
// Follow the component as before, but this time we choose an
// arbitrary direction for the starting strand (since we cannot
// deduce this from the PD code).
PDPos pos { index, 1 };
dir[start] = 1;
InputInt min = start;
InputInt s = start;
while (true) {
if (dir[s] > 0)
pos = occ[s].second;
else
pos = occ[s].first;
pos.second ^= 2;
s = (*(begin + pos.first))[pos.second] - 1;
if (s == start)
break;
dir[s] = (occ[s].first == pos ? 1 : -1);
if (s < min)
min = s;
}
components.push_back(min);
}
/*
for (size_t i = 0; i < 2 * n; ++i) {
std::cerr << "Strand " << (i + 1) << ": ";
if (dir[i] > 0) {
std::cerr << "(" << occ[i].first.first << ","
<< occ[i].first.second << ") --> (" << occ[i].second.first
<< "," << occ[i].second.second << ")" << std::endl;
} else {
std::cerr << "(" << occ[i].first.first << ","
<< occ[i].first.second << ") <-- (" << occ[i].second.first
<< "," << occ[i].second.second << ")" << std::endl;
}
}
*/
// Build and hook together the final list of crossings.
Link ans;
for (size_t i = 0; i < n; ++i)
ans.crossings_.push_back(new Crossing);
for (size_t i = 0; i < 2 * n; ++i) {
PDPos from, to;
if (dir[i] > 0) {
from = occ[i].first;
to = occ[i].second;
} else {
from = occ[i].second;
to = occ[i].first;
}
ans.join(
StrandRef(ans.crossings_[from.first], (from.second % 2 ? 1 : 0)),
StrandRef(ans.crossings_[to.first], (to.second % 2 ? 1 : 0)));
// If this strand exits from the upper side of its source crossing,
// use this to determine the crossing's sign.
if (from.second == 1)
ans.crossings_[from.first]->sign_ = 1;
else if (from.second == 3)
ans.crossings_[from.first]->sign_ = -1;
}
// Finally, mark the starting point of each component.
std::sort(components.begin(), components.end());
for (auto start : components) {
const PDPos& from = (dir[start] > 0 ? occ[start].first :
occ[start].second);
ans.components_.emplace_back(ans.crossings_[from.first],
(from.second % 2 ? 1 : 0));
}
return ans;
}
} // namespace regina
#endif
|