File: order.c

package info (click to toggle)
libhomfly 1.02r6-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 220 kB
  • sloc: ansic: 1,740; makefile: 19; sh: 1
file content (289 lines) | stat: -rw-r--r-- 7,865 bytes parent folder | download | duplicates (2)
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
/*
------------------------------------------------------------------------------
 By Bob Jenkins, 1989, in association with his thesis project
 Public Domain
------------------------------------------------------------------------------
*/
#include <stdlib.h>
#include <gc.h>

#include "standard.h"
#include "dllink.h"
#include "knot.h"
#include "order.h"

/* o_tabs: mark the neighbors and the neighbors of the neighbors */
#define o_tabs( k, t, big) \
if (1) \
{ \
  (t)[(k)->o->c] += (big); \
  (t)[(k)->o->a->c] += 2; \
  (t)[(k)->o->z->c] -= 1; \
  (t)[(k)->u->a->c] += 2; \
  (t)[(k)->u->z->c] -= 1; \
} else

/*  This assumes that every crossing is used.
    This lists the crossings in an order my algorithm will hopefully like. */
static void o_order2(
    crossing  *k,
    word      *oldorder,
    word      *order,
    word       crossings)
{
  word       tab[MAXCROSS];
  word       j,
            *ip,
            *endp,
             tmp1,
             tmp2;
  crossing  *kp,
            *kp2;

  for (ip = tab, endp = tab+crossings; ip != endp;) *(ip++) = 0;
  *order = oldorder[crossings-1];
  for (ip = order+1, endp = order+crossings; ip < endp; ip++)
  {
    j       = *(ip-1);
    *ip     = j;
    tab[j] -= 100;
    kp2     = k+j;
    kp      = k+kp2->o->a->c;
    o_tabs(kp, tab, 16);
    kp = k+kp2->o->z->c;
    o_tabs(kp, tab, 20);
    kp = k+kp2->u->a->c;
    o_tabs(kp, tab, 16);
    kp = k+kp2->u->z->c;
    o_tabs(kp, tab, 20);
    tmp1 = *ip;
    for (j = 0; j < crossings; ++j)
    {
      tmp2 = oldorder[crossings-1-j];
      if (tab[tmp2] > tab[tmp1]) tmp1 = tmp2;
    }
    *ip = tmp1;
  }
}


/*  This assumes that every crossing is used.
    This lists the crossings in an order my algorithm will hopefully like. */
static void o_order1(crossing  *k,
                     word      *oldorder,
                     word      *order,
                     word       crossings)
{
  word       tab[MAXCROSS];
  word       j,
            *ip,
            *endp,
             tmp1,
             tmp2;
  crossing  *kp2;

  for (ip = tab, endp = tab+crossings; ip != endp;) *(ip++) = 0;
  *order = oldorder[crossings-1];
  for (ip = order+1, endp = order+crossings; ip != endp; ip++)
  {
    j                  = *(ip-1);
    *ip                = j;
    tab[j]            -= 100;
    kp2                = k+j;
    tab[kp2->o->a->c] += 20;
    tab[kp2->o->z->c] += 20;
    tab[kp2->u->a->c] += 20;
    tab[kp2->u->z->c] += 20;
    tmp1 = *ip;
    for (j = 0; j < crossings; ++j)
    {
      tmp2 = oldorder[crossings-1-j];
      if (tab[tmp2] > tab[tmp1]) tmp1 = tmp2;
    }
    *ip = tmp1;
  }
}

/**
 * Make instructions for just adding a crossing to the solved region
 */
static void o_add(word      *n,         /* in/out: the number of strings in the weave */
                  dllink   **boundary,
                  word      *going_in,  /* in/out: which strings are inputs */
                  crossing  *k,
                  word       newcross,
                  Instruct  *answer)
{
  word  i;
  word  old;
  word  go_in;
  word  righthanded;
  dllink *link;  /* link in crossing perpendicular to boundary */
  word  after;  /* is the new input after the original string? */

  answer->crossing = newcross;
  answer->oldn = *n;
  for (i = 0; i<*n; ++i)
  {
    answer->going_in[i] = going_in[i];
  }
  /*-------------- Find *old*, try to make it an input rather than an output */
  /* Imagine crossing c, input 0 and output n attached to c.  If you add the
     crossing at n, n becomes n+1 and you have input n and output n+2.  Then
     the pair of boundary crossings n+2 and 0 are removed, and string 0 becomes
     string n (really n-1 since things shift to fill in the missing 0).
     Changing a string from 0 to n-1 is likely to produce lots of bad
     crossings.  If you added the crossing at the input to begin with, the
     problem would have been avoided.  So try to add crossings at inputs. */
  for (i = *n; boundary[--i]->c != newcross;) ;
  old = i;
  if (!going_in[i])
  {
    while (i && (boundary[--i]->c != newcross || !going_in[i]));
    if (boundary[i]->c == newcross && going_in[i]) old = i;
  }
  answer->which = old;
  answer->over  = (k[newcross].o == boundary[old]);
  answer->right = (k[newcross].hand == 1);
  *n           += 2;
  for (i = *n; --i > old+2;)
  {
    boundary[i] = boundary[i-2];
    going_in[i] = going_in[i-2];
  }
  go_in = going_in[old];
  righthanded = answer->right;
  link = answer->over ? k[newcross].u : k[newcross].o;
  going_in[old+1] = go_in;
  boundary[old+1] = (go_in ? boundary[old]->a : boundary[old]->z);

  /* crossing are labeled counterclockwise */
  after = (go_in ^ righthanded ^ answer->over);

  if (after)
  {
    boundary[old]   = link->z;
    going_in[old]   = 0;
    boundary[old+2] = link->a;
    going_in[old+2] = 1;
  }
  else
  {
    boundary[old]   = link->a;
    going_in[old]   = 1;
    boundary[old+2] = link->z;
    going_in[old+2] = 0;
  }
  answer->prev       = going_in[old];
  answer->newn       = *n;
  answer->reductions = 0;
}


/**
 * Make instructions for removing one pair of boundary crossings
 */
static void o_delete(word      *n,
                     dllink   **boundary,
                     word      *going_in,
                     Instruct  *answer,
                     word      *done,
                     word       i)
{
  word    j;
  dllink *l,
         *m;

  j = (i == 0) ? *n-1 : i-1;
  l = (going_in[i]) ? boundary[i]->z : boundary[i]->a;
  m = (going_in[j]) ? boundary[j]->z : boundary[j]->a;
  if ((l == boundary[j]) && (m == boundary[i]))
  {

    *done                          = 0;
    answer->r0[answer->reductions] = i;
    answer->r1[answer->reductions] = j;
    if (i == 0)
    {
      for (j = 0; j < (*n)-2; j++)
      {
        boundary[j] = boundary[j+1];
        going_in[j] = going_in[j+1];
      }
    }
    else
    {
      for (j = i-1; j < (*n)-2; j++)
      {
        boundary[j] = boundary[j+2];
        going_in[j] = going_in[j+2];
      }
    }
    answer->reductions++;
    *n -= 2;
  }
}


/* make instructions for handling a single crossing */
static void o_one_make(word      *n,
                       dllink   **boundary,
                       word      *going_in,
                       crossing  *k,
                       word       which,
                       Instruct  *answer)
{
  word  i, done;

  /*  Move one crossing into the solved region */
  o_add(n, boundary, going_in, k, which, answer);

  /*  Join as many adjacent boundary pairs as possible */
  for (done = 0; !done;)
  {
    for (i = *n, done = 1; done && (--i >= 0);)
    {
      if (*n == 2) break;                        /* do not shrink to nothing */
      o_delete(n, boundary, going_in, answer, &done, i);
    }
  }
  answer->newn = *n;
}


/*  Make complete instructions for handling all the crossings */
void o_make(Link *link, Instruct **list)
{
  word     order1[MAXCROSS];                         /* order of crossings */
  word     order2[MAXCROSS];                 /* another order of crossings */
  dllink  *boundary[BIGWEAVE];
  word     going_in[BIGWEAVE];
  word     i, j, n;
  int num_crossings = link->num_crossings;
  const crossing *k = link->data;
  Instruct  *l;

  l = (Instruct *)GC_MALLOC(sizeof(Instruct) * num_crossings);
  for (i = 0; i < num_crossings; order1[i] = i, i++) ;
  for (i = 0; i < num_crossings; order2[i] = i, i++) ;
  o_order2(k, order2, order1, num_crossings);
  o_order1(k, order1, order2, num_crossings);
  o_order1(k, order2, order1, num_crossings);
  boundary[0] = k[order1[0]].o;
  boundary[1] = k[order1[0]].o->z;
  going_in[0] = 1;
  going_in[1] = 0;
  n           = 2;

  for (i = 0; i < num_crossings; ++i)
    o_one_make(&n, boundary, going_in, k, order1[i], (l+i));
  *list = l;
}


/*void o_show(Instruct *l, word num_crossings)
{
  word  i;
  word  j;
}*/