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
|
/**************************************************************************
* *
* Regina - A Normal Surface Theory Calculator *
* Computational Engine *
* *
* Copyright (c) 2011-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 "regina-config.h" // for REGINA_HIGHDIM
#include "link/link.h"
#include "angle/anglestructures.h"
#include "hypersurface/normalhypersurfaces.h"
#include "snappea/snappeatriangulation.h"
#include "surface/normalsurfaces.h"
#include "triangulation/dim2.h"
#include "triangulation/dim3.h"
#include "triangulation/dim4.h"
#include "triangulation/facetpairing.h"
#include "triangulation/facetpairing3.h"
#include "triangulation/generic.h"
#include "utilities/typeutils.h"
#include <unordered_map>
/**
* The situation for comparing std::type_info / std::type_index objects:
*
* - If hash_code() is different, then the types are different.
* - If the type_info pointers are the same, then the types are the same.
* On "good" platforms, this is an if-and-only-if.
* - If neither is true, we need to use type_info's == operator, which on
* "bad" platforms may trigger a deep string comparison.
*
* I think "good" includes GNU/Linux, "bad" includes Windows, and Apple
* platforms (macOS / iOS) are "it depends". I could be wrong about this.
*
* So:
*
* - When looking up type information from a std::type_info pointer,
* we do the lookup by hash_code() (that is, hash_code() forms the key
* in the underlying map). This means that if there is no match, we will
* probably discover this on all platforms without a deep comparison.
*
* - If we do find a matching hash_code(), then we double-check by comparing
* type_info pointers. This means that if there *is* a match, we still
* have the opportunity to certify this without a deep comparison even
* on bad platforms.
*
* - If we have a matching hash but the type_info pointers do not match,
* we do the deep comparison (type_info operator ==). At this point
* this is unavoidable on bad platforms, and will still be fast on
* good platforms in the (probably never-occuring) scenario where the
* hashes match but the types are not actually the same.
*/
using TypeData = std::pair<const std::type_info*, std::string>;
using TypeMap = std::unordered_map<size_t, TypeData>;
#ifdef REGINA_HIGHDIM
#define REGINA_MAXDIM 15
#else
#define REGINA_MAXDIM 8
#endif
namespace {
TypeMap* pythonNames = nullptr;
inline void registerType(const std::type_info& t, std::string name) {
pythonNames->emplace(t.hash_code(),
TypeData(std::addressof(t), std::move(name)));
}
void initPythonNames() {
pythonNames = new TypeMap;
using namespace regina;
registerType(typeid(IntegerBase<true>), "regina.LargeInteger");
registerType(typeid(IntegerBase<false>), "regina.Integer");
// We use the hard-coded dimension strings to build names for
// Perm2..Perm15.
for_constexpr<2, 16>([](auto i) {
registerType(typeid(Perm<i>),
std::string("regina.Perm") + regina::detail::Strings<i>::dim);
});
// Perm16 does not have a hard-coded dimension string, so do
// this separately.
registerType(typeid(Perm<16>), "regina.Perm16");
for_constexpr<2, REGINA_MAXDIM + 1>([](auto i) {
registerType(typeid(Triangulation<i>),
std::string("regina.Triangulation") +
regina::detail::Strings<i>::dim);
registerType(typeid(Component<i>),
std::string("regina.Component") +
regina::detail::Strings<i>::dim);
registerType(
typeid(decltype(Triangulation<i>().components())),
std::string("<internal>.ListView[regina.Component") +
regina::detail::Strings<i>::dim + ']');
registerType(typeid(BoundaryComponent<i>),
std::string("regina.BoundaryComponent") +
regina::detail::Strings<i>::dim);
registerType(
typeid(decltype(Triangulation<i>().boundaryComponents())),
std::string("<internal>.ListView[regina.BoundaryComponent") +
regina::detail::Strings<i>::dim + ']');
registerType(
typeid(decltype(Triangulation<i>().component(0)->
boundaryComponents())),
std::string("<internal>.ListView[regina.BoundaryComponent") +
regina::detail::Strings<i>::dim + ']');
registerType(typeid(Isomorphism<i>),
std::string("regina.Isomorphism") +
regina::detail::Strings<i>::dim);
registerType(typeid(FacetSpec<i>),
std::string("regina.FacetSpec") +
regina::detail::Strings<i>::dim);
registerType(typeid(FacetPairing<i>),
std::string("regina.FacetPairing") +
regina::detail::Strings<i>::dim);
// We see FacetPairingBase show up in some swap() functions.
registerType(
typeid(regina::detail::FacetPairingBase<i>),
std::string("regina.FacetPairing") +
regina::detail::Strings<i>::dim);
if constexpr (i == 2) {
registerType(typeid(Triangle<2>), "regina.Triangle2");
registerType(
typeid(decltype(Triangulation<2>().simplices())),
"<internal>.ListView[regina.Triangle2]");
registerType(
typeid(decltype(Triangulation<2>().component(0)->
simplices())),
"<internal>.ListView[regina.Triangle2]");
registerType(
typeid(decltype(Triangulation<2>().boundaryComponent(0)->
facets())),
"<internal>.ListView[regina.Edge2]");
registerType(
typeid(decltype(Triangulation<2>().boundaryComponent(0)->
vertices())),
"<internal>.ListView[regina.Vertex2]");
} else if constexpr (i == 3) {
registerType(typeid(Tetrahedron<3>), "regina.Tetrahedron3");
registerType(
typeid(decltype(Triangulation<3>().simplices())),
"<internal>.ListView[regina.Tetrahedron3]");
registerType(
typeid(decltype(Triangulation<3>().component(0)->
simplices())),
"<internal>.ListView[regina.Tetrahedron3]");
registerType(
typeid(decltype(Triangulation<3>().boundaryComponent(0)->
facets())),
"<internal>.ListView[regina.Triangle3]");
registerType(
typeid(decltype(Triangulation<3>().boundaryComponent(0)->
edges())),
"<internal>.ListView[regina.Edge3]");
registerType(
typeid(decltype(Triangulation<3>().boundaryComponent(0)->
vertices())),
"<internal>.ListView[regina.Vertex3]");
} else if constexpr (i == 4) {
registerType(typeid(Pentachoron<4>), "regina.Pentachoron4");
registerType(
typeid(decltype(Triangulation<4>().simplices())),
"<internal>.ListView[regina.Pentachoron4]");
registerType(
typeid(decltype(Triangulation<4>().component(0)->
simplices())),
"<internal>.ListView[regina.Pentachoron4]");
registerType(
typeid(decltype(Triangulation<4>().boundaryComponent(0)->
facets())),
"<internal>.ListView[regina.Tetrahedron4]");
registerType(
typeid(decltype(Triangulation<4>().boundaryComponent(0)->
triangles())),
"<internal>.ListView[regina.Triangle4]");
registerType(
typeid(decltype(Triangulation<4>().boundaryComponent(0)->
edges())),
"<internal>.ListView[regina.Edge4]");
registerType(
typeid(decltype(Triangulation<4>().boundaryComponent(0)->
vertices())),
"<internal>.ListView[regina.Vertex4]");
} else {
registerType(typeid(Face<i, i>),
std::string("regina.Simplex") +
regina::detail::Strings<i>::dim);
registerType(
typeid(decltype(Triangulation<i>().simplices())),
std::string("<internal>.ListView[regina.Simplex") +
regina::detail::Strings<i>::dim + ']');
registerType(
typeid(decltype(Triangulation<i>().component(0)->
simplices())),
std::string("<internal>.ListView[regina.Simplex") +
regina::detail::Strings<i>::dim + ']');
if constexpr (i == 5) {
registerType(
typeid(decltype(Triangulation<5>().
boundaryComponent(0)->facets())),
"<internal>.ListView[regina.Pentachoron5]");
} else {
registerType(
typeid(decltype(Triangulation<i>().
boundaryComponent(0)->facets())),
std::string("<internal>.ListView[regina.Face") +
regina::detail::Strings<i>::dim + '_' +
regina::detail::Strings<i - 1>::dim + ']');
}
}
registerType(
typeid(Vertex<i>),
std::string("regina.Vertex") + regina::detail::Strings<i>::dim);
registerType(
typeid(decltype(Triangulation<i>().vertices())),
std::string("<internal>.ListView[regina.Vertex") +
regina::detail::Strings<i>::dim + ']');
registerType(
typeid(decltype(Triangulation<i>().
vertex(0)->embeddings())),
std::string(
"<internal>.ListView[regina.VertexEmbedding") +
regina::detail::Strings<i>::dim + ']');
registerType(
typeid(Edge<i>),
std::string("regina.Edge") + regina::detail::Strings<i>::dim);
registerType(
typeid(decltype(Triangulation<i>().edges())),
std::string("<internal>.ListView[regina.Edge") +
regina::detail::Strings<i>::dim + ']');
registerType(
typeid(decltype(Triangulation<i>().
edge(0)->embeddings())),
std::string(
"<internal>.ListView[regina.EdgeEmbedding") +
regina::detail::Strings<i>::dim + ']');
if constexpr (i > 2) {
registerType(
typeid(Triangle<i>),
std::string("regina.Triangle") +
regina::detail::Strings<i>::dim);
registerType(
typeid(decltype(Triangulation<i>().
triangles())),
std::string("<internal>.ListView[regina.Triangle") +
regina::detail::Strings<i>::dim + ']');
registerType(
typeid(decltype(Triangulation<i>().
triangle(0)->embeddings())),
std::string("<internal>.ListView["
"regina.TriangleEmbedding") +
regina::detail::Strings<i>::dim + ']');
}
if constexpr (i > 3) {
registerType(
typeid(Tetrahedron<i>),
std::string("regina.Tetrahedron") +
regina::detail::Strings<i>::dim);
registerType(
typeid(decltype(Triangulation<i>().
tetrahedra())),
std::string(
"<internal>.ListView[regina.Tetrahedron") +
regina::detail::Strings<i>::dim + ']');
registerType(
typeid(decltype(Triangulation<i>().
tetrahedron(0)->embeddings())),
std::string(
"<internal>.ListView["
"regina.TetrahedronEmbedding") +
regina::detail::Strings<i>::dim + ']');
}
if constexpr (i > 4) {
registerType(
typeid(Pentachoron<i>),
std::string("regina.Pentachoron") +
regina::detail::Strings<i>::dim);
registerType(
typeid(decltype(Triangulation<i>().
pentachora())),
std::string(
"<internal>.ListView[regina.Pentachoron") +
regina::detail::Strings<i>::dim + ']');
registerType(
typeid(decltype(Triangulation<i>().
pentachoron(0)->embeddings())),
std::string(
"<internal>.ListView["
"regina.PentachoronEmbedding") +
regina::detail::Strings<i>::dim + ']');
}
if constexpr (i > 5) {
// Make sure we can use i as a constexpr inside the lambda.
//
// Ideally the lambda would capture nothing, and we would
// declare the constexpr i_ out here. However, this causes an
// ICE in gcc7. Until we drop gcc7 support, we work around this
// by capturing i and declaring i_ inside the lambda instead.
for_constexpr<5, i>([i](auto j) {
static constexpr int i_ = i.value;
registerType(
typeid(Face<i_, j>),
std::string("regina.Face") +
regina::detail::Strings<i_>::dim + '_' +
regina::detail::Strings<j>::dim);
registerType(
typeid(decltype(Triangulation<i_>().
template faces<j>())),
std::string("<internal>.ListView[regina.Face") +
regina::detail::Strings<i_>::dim + '_' +
regina::detail::Strings<j>::dim + ']');
registerType(
typeid(decltype(Triangulation<i_>().
template face<j>(0)->embeddings())),
std::string(
"<internal>.ListView[regina.FaceEmbedding") +
regina::detail::Strings<i_>::dim + '_' +
regina::detail::Strings<j>::dim + ']');
});
}
registerType(
typeid(PacketOf<Triangulation<i>>),
std::string("regina.PacketOfTriangulation") +
regina::detail::Strings<i>::dim);
});
registerType(typeid(decltype(SnapPeaTriangulation().cusps())),
"<internal>.ListView[regina.Cusp]");
registerType(typeid(decltype(Link().crossings())),
"<internal>.ListView[regina.Crossing]");
registerType(typeid(decltype(Link().components())),
"<internal>.ListView[regina.StrandRef]");
registerType(typeid(decltype(ModelLinkGraph().nodes())),
"<internal>.ListView[regina.ModelLinkGraphNode]");
registerType(typeid(decltype(ModelLinkGraph().cells().arcs(0))),
"<internal>.ListView[regina.ModelLinkGraphArc]");
// Enums that live within individual face classes:
registerType(typeid(Vertex<3>::Link), "regina.Vertex3.Link");
registerType(typeid(PacketOf<Link>),
"regina.PacketOfLink");
registerType(typeid(PacketOf<AngleStructures>),
"regina.PacketOfAngleStructures");
registerType(typeid(PacketOf<NormalSurfaces>),
"regina.PacketOfNormalSurfaces");
registerType(typeid(PacketOf<NormalHypersurfaces>),
"regina.PacketOfNormalHypersurfaces");
registerType(typeid(PacketOf<SnapPeaTriangulation>),
"regina.PacketOfSnapPeaTriangulation");
}
}
namespace regina {
const char* pythonTypename(const std::type_info* t) {
if (! pythonNames)
initPythonNames();
auto r = pythonNames->equal_range(t->hash_code());
// We have a set of possible matches.
// See the discussion at the top of this file as to how we organise
// the following tests.
for ( ; r.first != r.second; ++r.first) {
// Compare pointers first, then do a deep type_info comparison.
const auto& value = r.first->second;
if (value.first == t || (*value.first == *t))
return value.second.c_str();
}
// We have no match.
return nullptr;
}
} // namespace regina
|