File: tangle.cpp

package info (click to toggle)
regina-normal 7.4.1-1.1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 154,244 kB
  • sloc: cpp: 295,026; xml: 9,992; sh: 1,344; python: 1,225; perl: 616; ansic: 138; makefile: 26
file content (567 lines) | stat: -rw-r--r-- 16,842 bytes parent folder | download
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567

/**************************************************************************
 *                                                                        *
 *  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 "tangle.h"
#include <algorithm>
#include <sstream>
#include <stack>

static constexpr char TANGLE_HORIZONTAL = '-';
static constexpr char TANGLE_VERTICAL = '|';
static constexpr char TANGLE_DIAGONAL = 'x';

static constexpr char OP_POS_TWIST = '+';
static constexpr char OP_NEG_TWIST = '-';
static constexpr char OP_TURN = 'o';

namespace regina {

Tangle::Tangle(int twists) {
    // Build the zero tangle.
    //
    // Recall that all members end_[i][j] are by default initialised to
    // null strand references.
    type_ = TANGLE_HORIZONTAL;

    // Now perform the requested number of twists.
    if (twists >= 0) {
        for ( ; twists > 0; --twists)
            twist(1);
    } else {
        for ( ; twists < 0; ++twists)
            twist(-1);
    }
}

Tangle::Tangle(int num, int den) {
    // Recall that all members end_[i][j] are by default initialised to
    // null strand references.
    if (den == 0) {
        // Build the infinity tangle.
        type_ = TANGLE_VERTICAL;
        return;
    }

    // Normalise so the denominator is positive.
    if (den < 0) {
        den = -den;
        num = -num;
    }

    // Find a sequence of twists and turns that brings us back to the
    // zero tangle.
    std::stack<char> ops;

    while (num != 0) {
        if (num <= -den) {
            ops.push(OP_NEG_TWIST);
            num += den;
        } else if (num >= den) {
            ops.push(OP_POS_TWIST);
            num -= den;
        /*
        } else if (num < 0 && (num + den) < -num) {
            ops.push(OP_NEG_TWIST);
            num += den;
        } else if (num > 0 && -(num - den) < num) {
            ops.push(OP_POS_TWIST);
            num -= den;
        */
        } else {
            ops.push(OP_TURN);
            std::swap(num, den);
            if (den < 0)
                den = -den;
            else
                num = -num;
        }
    }

    // Now construct the zero tangle and perform the corresponding
    // twists and turns.
    type_ = TANGLE_HORIZONTAL;

    char op;
    while (! ops.empty()) {
        op = ops.top();
        ops.pop();

        if (op == OP_TURN)
            turn(1);
        else
            twist(op == OP_POS_TWIST ? 1 : -1);
    }
}

Tangle::Tangle(const Link& knot) : type_(TANGLE_HORIZONTAL) {
    // Sanity checking:
    if (knot.countComponents() != 1) {
        std::cerr << "Tangle(const Link&): requires a one-component knot";
        return;
    }

    // Crossing i of knot:
    //
    // +ve:    |                 -ve:    ^
    //     --- | --->                --- | --->
    //         v                         |
    //
    // Crossings (4i, ..., 4i+3) of this tangle:
    //
    //    4i |   | 4i+2           4i+1 ^   ^ 4i+3
    //   --- | - | --->            --- | - | --->
    //   --- | - | --->            --- | - | --->
    //  4i+1 v   v 4i+3             4i |   | 4i+2

    // Create the four crossings for each original, and join them
    // together internally.
    Crossing *c0, *c1, *c2, *c3;
    for (Crossing* c : knot.crossings_) {
        crossings_.push_back(c0 = new Crossing(c->sign()));
        crossings_.push_back(c1 = new Crossing(c->sign()));
        crossings_.push_back(c2 = new Crossing(c->sign()));
        crossings_.push_back(c3 = new Crossing(c->sign()));

        Link::join(c0->upper(), c1->upper());
        Link::join(c2->upper(), c3->upper());
        Link::join(c0->lower(), c2->lower());
        Link::join(c1->lower(), c3->lower());
    }

    // Walk around the original knot, and keep track of the left-hand
    // and right-hand crossings of the new tangle where we (i) enter the
    // 4-crossing configuration, and (ii) leave this configuration.

    StrandRef start = knot.component(0);
    StrandRef s = start;
    size_t idx;
    StrandRef enterL, enterR, exitL, exitR;
    do {
        idx = s.crossing()->index();
        if (s.crossing()->sign() > 0) {
            if (s.strand() == 1) {
                enterL = crossings_[4 * idx + 2]->upper();
                enterR = crossings_[4 * idx]->upper();
            } else {
                enterL = crossings_[4 * idx]->lower();
                enterR = crossings_[4 * idx + 1]->lower();
            }
        } else {
            if (s.strand() == 1) {
                enterL = crossings_[4 * idx]->upper();
                enterR = crossings_[4 * idx + 2]->upper();
            } else {
                enterL = crossings_[4 * idx + 1]->lower();
                enterR = crossings_[4 * idx]->lower();
            }
        }

        // Connect the previous block of four to this.
        if (exitL) {
            Link::join(exitL, enterL);
            Link::join(exitR, enterR);
        } else {
            end_[0][0] = enterL;
            end_[1][0] = enterR;
        }

        exitL = enterL.next();
        exitR = enterR.next();

        ++s;
    } while (s != start);

    end_[0][1] = exitL;
    end_[1][1] = exitR;
}

Tangle::Tangle(const Tangle& cloneMe) : type_(cloneMe.type_) {
    crossings_.reserve(cloneMe.crossings_.size());
    for (Crossing* c : cloneMe.crossings_)
        crossings_.push_back(new Crossing(c->sign()));

    int i, j;
    auto it = cloneMe.crossings_.begin();
    for (Crossing* c : crossings_) {
        for (i = 0; i < 2; ++i) {
            c->next_[i] = translate((*it)->next_[i]);
            c->prev_[i] = translate((*it)->prev_[i]);
        }
        ++it;
    }

    for (i = 0; i < 2; ++i)
        for (j = 0; j < 2; ++j)
            end_[i][j] = translate(cloneMe.end_[i][j]);
}

Tangle::Tangle(Tangle&& src) noexcept :
        type_(src.type_), crossings_(std::move(src.crossings_)) {
    int i, j;
    for (i = 0; i < 2; ++i)
        for (j = 0; j < 2; ++j)
            end_[i][j] = src.end_[i][j];
}

Tangle& Tangle::operator = (const Tangle& src) {
    if (std::addressof(src) == this)
        return *this;

    type_ = src.type_;

    for (Crossing* c : crossings_)
        delete c;
    crossings_.clear();

    crossings_.reserve(src.crossings_.size());
    for (Crossing* c : src.crossings_)
        crossings_.push_back(new Crossing(c->sign()));

    int i, j;
    auto it = src.crossings_.begin();
    for (Crossing* c : crossings_) {
        for (i = 0; i < 2; ++i) {
            c->next_[i] = translate((*it)->next_[i]);
            c->prev_[i] = translate((*it)->prev_[i]);
        }
        ++it;
    }

    for (i = 0; i < 2; ++i)
        for (j = 0; j < 2; ++j)
            end_[i][j] = translate(src.end_[i][j]);

    return *this;
}

Tangle& Tangle::operator = (Tangle&& src) noexcept {
    type_ = src.type_;
    crossings_ = std::move(src.crossings_);

    int i, j;
    for (i = 0; i < 2; ++i)
        for (j = 0; j < 2; ++j)
            end_[i][j] = src.end_[i][j];

    return *this;
}

void Tangle::swap(Tangle& other) noexcept {
    if (&other == this)
        return;

    std::swap(type_, other.type_);

    crossings_.swap(other.crossings_);

    int i, j;
    for (i = 0; i < 2; ++i)
        for (j = 0; j < 2; ++j)
            std::swap(end_[i][j], other.end_[i][j]);
}

bool Tangle::operator == (const Tangle& other) const {
    if (type_ != other.type_)
        return false;
    if (crossings_.size() != other.crossings_.size())
        return false;

    for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 2; ++j)
            if (end_[i][j] != translate(other.end_[i][j]))
                return false;

    for (size_t i = 0; i < crossings_.size(); ++i) {
        Crossing* a = crossings_[i];
        Crossing* b = other.crossings_[i];

        if (a->sign() != b->sign())
            return false;
        if (a->next_[0] != translate(b->next_[0]))
            return false;
        if (a->next_[1] != translate(b->next_[1]))
            return false;

        // If everything is self-consistent then the prev strands should
        // match also; we don't need to test those.
    }

    return true;
}

void Tangle::twist(int sign) {
    Crossing* c;

    if (type_ == TANGLE_HORIZONTAL || type_ == TANGLE_DIAGONAL) {
        c = new Crossing(-sign);

        // Which string attaches to the lower strand of the new crossing?
        int attach = (
            ((sign > 0 && type_ == TANGLE_HORIZONTAL) ||
             (sign < 0 && type_ == TANGLE_DIAGONAL)) ?
            0 : 1);

        for (int s = 0; s < 2; ++s) {
            // Attach c->strand(s) to the appropriate string.
            if (end_[attach][1]) {
                c->prev_[s] = end_[attach][1];
                end_[attach][1].crossing()->
                    next_[end_[attach][1].strand()] = c->strand(s);
                end_[attach][1] = c->strand(s);
            } else
                end_[attach][0] = end_[attach][1] = c->strand(s);

            attach ^= 1;
        }

        if (type_ == TANGLE_HORIZONTAL)
            type_ = TANGLE_DIAGONAL;
        else
            type_ = TANGLE_HORIZONTAL;
    } else {
        // Vertical tangle.
        reverse(1);

        c = new Crossing(sign);

        // In which order do we see the two strands at c when traversing
        // the right-hand string?
        int first, last;
        if (sign > 0) {
            first = 1;
            last = 0;
        } else {
            first = 0;
            last = 1;
        }

        if (end_[1][0]) {
            c->next_[first] = end_[1][1];
            c->prev_[last] = end_[1][0];
            end_[1][1].crossing()->prev_[end_[1][1].strand()] =
                c->strand(first);
            end_[1][0].crossing()->next_[end_[1][0].strand()] =
                c->strand(last);
        } else {
            c->next_[first] = c->strand(last);
            c->prev_[last] = c->strand(first);
        }

        end_[1][0] = c->strand(first);
        end_[1][1] = c->strand(last);
    }

    crossings_.push_back(c);
}

void Tangle::turn(int direction) {
    int i;
    if (type_ == TANGLE_HORIZONTAL) {
        if (direction > 0) {
            // Switch the indexing of the two strings.
            for (i = 0; i < 2; ++i)
                std::swap(end_[0][i], end_[1][i]);
        } else {
            // Reverse each string, but keep the indexing.
            for (i = 0; i < 2; ++i) {
                reverse(i);
                std::swap(end_[i][0], end_[i][1]);
            }
        }
        type_ = TANGLE_VERTICAL;
    } else if (type_ == TANGLE_VERTICAL) {
        if (direction > 0) {
            // Reverse each string, but keep the indexing.
            for (i = 0; i < 2; ++i) {
                reverse(i);
                std::swap(end_[i][0], end_[i][1]);
            }
        } else {
            // Switch the indexing of the two strings.
            for (i = 0; i < 2; ++i)
                std::swap(end_[0][i], end_[1][i]);
        }
        type_ = TANGLE_HORIZONTAL;
    } else {
        // The diagonal case.

        // Reverse one of the strings...
        int toReverse = (direction > 0 ? 0 : 1);
        reverse(toReverse);
        std::swap(end_[toReverse][0], end_[toReverse][1]);

        // ... and also switch the indexing of the two strings.
        for (i = 0; i < 2; ++i)
            std::swap(end_[0][i], end_[1][i]);
    }
}

void Tangle::changeAll() {
    int i, j;

    for (Crossing* c : crossings_) {
        std::swap(c->next_[0], c->next_[1]);
        std::swap(c->prev_[0], c->prev_[1]);
        for (i = 0; i < 2; ++i) {
            c->next_[i].strand_ ^= 1;
            c->prev_[i].strand_ ^= 1;
        }
        c->sign_ = - c->sign_;
    }

    for (i = 0; i < 2; ++i)
        for (j = 0; j < 2; ++j)
            end_[i][j].strand_ ^= 1;
}

std::string Tangle::brief() const {
    std::ostringstream out;
    brief(out);
    return out.str();
}

void Tangle::brief(std::ostream& out) const {
    out << type_;

    if (crossings_.empty()) {
        out << " ( ) ( )";
        return;
    }

    out << ' ';
    for (Crossing* c : crossings_)
        out << (c->sign() > 0 ? '+' : '-');

    for (int i = 0; i < 2; ++i) {
        out << " (";
        for (StrandRef s = end_[i][0]; s; ++s) {
            out << ' ' << s;
        }
        out << " )";
    }
}

void Tangle::writeTextShort(std::ostream& out) const {
    out << crossings_.size() << "-crossing ";
    switch (type_) {
        case TANGLE_HORIZONTAL: out << "horizontal"; break;
        case TANGLE_VERTICAL: out << "vertical"; break;
        case TANGLE_DIAGONAL: out << "diagonal"; break;
    }
    out << " tangle: ";
    brief(out);
}

void Tangle::writeTextLong(std::ostream& out) const {
    out << crossings_.size() << "-crossing ";
    switch (type_) {
        case TANGLE_HORIZONTAL: out << "horizontal ( = )"; break;
        case TANGLE_VERTICAL: out << "vertical ( || )"; break;
        case TANGLE_DIAGONAL: out << "diagonal ( X )"; break;
    }
    out << " tangle\n\n";

    StrandRef s;
    for (int i = 0; i < 2; ++i) {
        out << "String " << i << ':';

        s = end_[i][0];
        if (! s)
            out << " no crossings";
        else
            for ( ; s; ++s)
                out << ' ' << s;
        out << '\n';
    }

    out << "\nCrossings:";
    for (Crossing* c : crossings_)
        out << ' ' << (c->sign() > 0 ? '+' : '-') << c->index();
    out << std::endl;
}

void Tangle::reverse(int string) {
    Crossing* c;
    for (StrandRef s = end_[string][0]; s; --s) {
        c = s.crossing();

        c->sign_ = - c->sign_;
        std::swap(c->prev_[s.strand()], c->next_[s.strand()]);
    }
}

void Tangle::rerouteTo(const StrandRef& oldDest, const StrandRef& newDest) {
    StrandRef src = oldDest.prev();

    if (! src.crossing()) {
        // This is the first crossing in one of the strings.
        if (end_[0][0] == oldDest)
            end_[0][0] = newDest;
        else if (end_[1][0] == oldDest)
            end_[1][0] = newDest;
        else
            std::cerr << "rerouteTo(): inconsistent end/prev/next arrays";
    } else {
        src.crossing()->next_[src.strand()] = newDest;
    }
}

void Tangle::rerouteFrom(const StrandRef& oldSrc, const StrandRef& newSrc) {
    StrandRef dest = oldSrc.next();

    if (! dest.crossing()) {
        // This is the last crossing in one of the strings.
        if (end_[0][1] == oldSrc)
            end_[0][1] = newSrc;
        else if (end_[1][1] == oldSrc)
            end_[1][1] = newSrc;
        else
            std::cerr << "rerouteFrom(): inconsistent end/prev/next arrays";
    } else {
        dest.crossing()->prev_[dest.strand()] = newSrc;
    }
}

char Tangle::extractChar(const char* s) {
    if (*s && ! *(s + 1))
        return *s;
    else
        return 0;
}

char Tangle::extractChar(const std::string& s) {
    if (s.length() == 1)
        return *s.begin();
    else
        return 0;
}

} // namespace regina