2 * Copyright (c) 2017, S. Gilles <sgilles@math.umd.edu>
4 * Permission to use, copy, modify, and/or distribute this software
5 * for any purpose with or without fee is hereby granted, provided
6 * that the above copyright notice and this permission notice appear
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
13 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
14 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 #define MAX_SUPPORTED_EDGE_NUM (((size_t) -1) >> 2)
29 #define MAX_SUPPORTED_VERTEX_NUM (((size_t) -1) >> 2)
31 /* Primes for reducing fractions */
32 static uint32_t primes
[] = {
34 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
35 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139,
36 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223,
37 227, 229, 233, 239, 241, 251
41 static int gcd(uint_fast8_t x
, uint_fast8_t y
)
59 /* Attempt to store a fraction in out, reducing if possible */
60 static int reduce_fraction(int_fast32_t n
, uint_fast32_t d
, struct
61 rational
*out
, const char **out_errstr
)
65 *out
= (struct rational
) { .p
= 0, .q
= 1 };
70 /* We special-case 1-3 since, in those cases, this actually needs to go fast */
91 for (size_t j
= 0; j
< ((sizeof primes
) / (sizeof primes
[0])); ++j
) {
92 uint32_t p
= primes
[j
];
108 if (n
> INT_FAST8_MAX
||
111 d
> UINT_FAST8_MAX
) {
112 IF_NZ_SET(out_errstr
, L("unrepresentable fraction"));
117 *out
= (struct rational
) { .p
= n
, .q
= d
};
123 static int add_to_fraction(int_fast8_t a
, uint_fast8_t b
, struct rational
*out
,
124 const char **out_errstr
)
126 int_fast32_t n
= a
* out
->q
+ out
->p
* b
;
127 uint_fast32_t d
= b
* out
->q
;
129 return reduce_fraction(n
, d
, out
, out_errstr
);
132 /* Add an arrow of weight a/b from i -> j, affecting e(i,j) and e(j,i) */
133 int quiver_add_to_edge(struct quiver
*q
, size_t i
, size_t j
, int_fast8_t a
,
134 uint_fast8_t b
, const char **out_errstr
)
139 IF_NZ_SET(out_errstr
, L("nonexistant quiver"));
146 IF_NZ_SET(out_errstr
, L("edge includes nonexistant vertex"));
152 IF_NZ_SET(out_errstr
, L("loops are not allowed"));
157 struct rational
*eij
= &(q
->e
[i
* q
->v_len
+ j
]);
158 struct rational
*eji
= &(q
->e
[j
* q
->v_len
+ i
]);
159 uint_fast8_t d
= gcd(q
->v
[i
].fatness
, q
->v
[j
].fatness
);
161 if ((ret
= add_to_fraction(a
* q
->v
[j
].fatness
, b
* d
, eij
,
166 if ((ret
= add_to_fraction(-1 * a
* q
->v
[i
].fatness
, b
* d
, eji
,
174 /* Add a vertex with a name and weight */
175 int quiver_add_vertex(struct quiver
*q
, size_t *out_i
, const char *name
,
176 uint_fast16_t fatness
, int x
, int y
, const
184 IF_NZ_SET(out_errstr
, L("invalid quiver"));
190 len
= snprintf(0, 0, "%zu", q
->v_num
);
193 IF_NZ_SET(out_errstr
, L("overflow"));
198 if (!(newname
= malloc(len
+ 1))) {
201 IF_NZ_SET(out_errstr
, L("malloc"));
206 sprintf(newname
, "%zu", q
->v_num
);
209 * Should be no need to check overflow, either name
210 * was malloc'd, or came from a file which was read
211 * in with a length cap.
213 if (!(newname
= malloc(strlen(name
) + 1))) {
216 IF_NZ_SET(out_errstr
, L("malloc"));
221 strcpy(newname
, name
);
224 if (q
->v_num
>= q
->v_len
) {
225 size_t newlen
= q
->v_len
+ 8;
227 if (newlen
>= ((size_t) -1) / newlen
||
228 (newlen
* newlen
) / newlen
!= newlen
) {
229 IF_NZ_SET(out_errstr
, L("too many vertices"));
234 if (!(newmem
= calloc(newlen
* newlen
, sizeof (*q
->e
)))) {
237 IF_NZ_SET(out_errstr
, L("too many vertices"));
242 for (size_t j
= 0; j
< q
->v_num
; ++j
) {
243 for (size_t k
= 0; k
< q
->v_num
; ++k
) {
244 ((struct rational
*) newmem
)[j
* newlen
+ k
] =
245 q
->e
[j
* q
->v_len
+ k
];
248 for (size_t k
= q
->v_num
; k
< newlen
; ++k
) {
249 ((struct rational
*) newmem
)[j
* newlen
+ k
] =
250 (struct rational
) { .p
= 0, .q
= 1 };
254 for (size_t j
= q
->v_num
; j
< newlen
; ++j
) {
255 for (size_t k
= 0; k
< newlen
; ++k
) {
256 ((struct rational
*) newmem
)[j
* newlen
+ k
] =
257 (struct rational
) { .p
= 0, .q
= 1 };
267 if (!(newmem
= (realloc(q
->v
, newlen
* sizeof (*q
->v
))))) {
270 IF_NZ_SET(out_errstr
, L("too many vertices"));
279 for (size_t k
= 0; k
<= q
->v_num
; ++k
) {
280 q
->e
[k
* q
->v_len
+ q
->v_num
] = (struct rational
) { .p
= 0, .q
=
282 q
->e
[q
->v_num
* q
->v_len
+ k
] = (struct rational
) { .p
= 0, .q
=
286 q
->v
[q
->v_num
] = (struct vertex
) { .name
= newname
, .fatness
= fatness
,
298 /* Increase or decrease the fatness of a vertex, readjusting edges as well */
299 int quiver_adjust_fatness(struct quiver
*q
, size_t i
, int_fast8_t fatness_adj
,
300 const char **out_errstr
)
305 IF_NZ_SET(out_errstr
, L("invalid vertex"));
310 int new_fatness
= q
->v
[i
].fatness
+ fatness_adj
;
312 if (new_fatness
<= 0 ||
313 new_fatness
> UINT_FAST8_MAX
) {
314 IF_NZ_SET(out_errstr
, L("invalid new fatness"));
319 for (size_t j
= 0; j
< q
->v_num
; ++j
) {
324 uint_fast8_t d_orig
= gcd(q
->v
[i
].fatness
, q
->v
[j
].fatness
);
325 uint_fast8_t d_new
= gcd(new_fatness
, q
->v
[j
].fatness
);
326 size_t k
= i
* q
->v_len
+ j
;
328 if ((ret
= reduce_fraction(q
->e
[k
].p
* d_orig
, q
->e
[k
].q
*
329 d_new
, &(q
->e
[k
]), out_errstr
))) {
333 k
= j
* q
->v_len
+ i
;
335 if ((ret
= reduce_fraction(q
->e
[k
].p
* d_orig
* q
->v
[i
].fatness
,
336 q
->e
[k
].q
* d_new
* new_fatness
,
337 &(q
->e
[k
]), out_errstr
))) {
342 q
->v
[i
].fatness
= new_fatness
;
347 /* Free all memory used by this quiver, resetting it to { 0 } */
348 int quiver_clean(struct quiver
*q
)
354 for (size_t j
= 0; j
< q
->v_num
; ++j
) {
360 *q
= (struct quiver
) { 0 };
365 /* Delete a vertex (and all associated edges) */
366 int quiver_delete_vertex(struct quiver
*q
, size_t i
, const char **out_errstr
)
369 IF_NZ_SET(out_errstr
, L("invalid vertex"));
382 /* First, shift everything over to the left */
383 for (size_t j
= 0; j
< q
->v_num
; ++j
) {
384 for (size_t k
= i
+ 1; k
< q
->v_num
; ++k
) {
385 q
->e
[q
->v_len
* j
+ k
- 1] = q
->e
[q
->v_len
* j
+ k
];
389 /* Second, shift everything below row i up */
390 for (size_t j
= i
+ 1; j
< q
->v_num
; ++j
) {
391 for (size_t k
= 0; k
< q
->v_num
; ++k
) {
392 q
->e
[q
->v_len
* (j
- 1) + k
] = q
->e
[q
->v_len
* j
+ k
];
396 /* Now shift the vertex names/lengths over */
400 for (size_t j
= i
+ 1; j
< q
->v_num
; ++j
) {
401 q
->v
[j
- 1] = q
->v
[j
];
405 q
->v
[q
->v_num
] = (struct vertex
) { 0 };
410 /* Read quiver from file */
411 int quiver_load_from_file(struct quiver
*q
, FILE *f
, const char **out_errstr
)
414 IF_NZ_SET(out_errstr
, L("invalid quiver"));
421 for (size_t k
= 0; k
< q
->v_num
; ++k
) {
422 struct vertex
*v
= &(q
->v
[k
]);
432 if (fscanf(f
, "%zu Vertices\n", &new_vnum
) != 1) {
433 IF_NZ_SET(out_errstr
, L("Invalid file (|V| unspecified)"));
438 for (size_t k
= 0; k
< new_vnum
; ++k
) {
445 if (fscanf(f
, " v[%zu] f:%zu x:%d y:%d l:%zu n:", &j
, &fatness
,
446 &x
, &y
, &len
) != 5) {
447 IF_NZ_SET(out_errstr
, L("Invalid file"));
451 IF_NZ_SET(out_errstr
, L(
452 "Invalid file (vertices out of order)"));
455 } else if (fatness
>= UINT_FAST16_MAX
) {
456 IF_NZ_SET(out_errstr
, L(
457 "Invalid file (fatness unsupported)"));
468 IF_NZ_SET(out_errstr
, L(
469 "Invalid file (nameless vertex)"));
475 IF_NZ_SET(out_errstr
, L(
476 "Invalid file (vertex with name too large)"));
481 if (!(p
= malloc(len
+ 1))) {
484 IF_NZ_SET(out_errstr
, L("malloc error"));
489 if (fread(p
, sizeof *p
, len
, f
) != (sizeof *p
) * len
) {
490 IF_NZ_SET(out_errstr
, L(
491 "Invalid file (short vertex name)"));
498 if ((ret
= quiver_add_vertex(q
, 0, p
, (uint_fast16_t) fatness
,
499 x
, y
, out_errstr
))) {
510 if (fscanf(f
, " e[%zu][%zu] %ld/%zu ", &i
, &j
, &n
, &d
) != 4) {
511 IF_NZ_SET(out_errstr
, L("Invalid file (bad edge)"));
518 IF_NZ_SET(out_errstr
, L(
519 "Invalid file (edge between nonexistent vertices)"));
524 struct rational
*e
= &(q
->e
[i
* q
->v_len
+ j
]);
526 if (n
<= INT_FAST32_MIN
||
527 n
>= INT_FAST32_MAX
||
528 d
> UINT_FAST32_MAX
) {
529 IF_NZ_SET(out_errstr
, L(
530 "Invalid file (edge weight out of range)"));
535 if ((ret
= reduce_fraction((int_fast32_t) n
, (uint_fast32_t) d
,
548 /* Rename a vertex */
549 int quiver_rename_vertex(struct quiver
*q
, size_t i
, const char *new_name
, const
553 IF_NZ_SET(out_errstr
, L("invalid quiver"));
559 IF_NZ_SET(out_errstr
, L("vertex out of range"));
567 if (!(newmem
= strdup(new_name
))) {
569 IF_NZ_SET(out_errstr
, L("cannot strdup()"));
574 q
->v
[i
].name
= newmem
;
580 /* Serialize the quiver */
581 int quiver_save_to_file(struct quiver
*q
, FILE *f
, const char **out_errstr
)
584 IF_NZ_SET(out_errstr
, L("invalid quiver"));
591 if (fprintf(f
, "%zu Vertices\n", q
->v_num
) < 0) {
593 IF_NZ_SET(out_errstr
, L("write failed"));
597 for (size_t k
= 0; k
< q
->v_num
; ++k
) {
598 struct vertex
*v
= &(q
->v
[k
]);
600 if (fprintf(f
, "v[%zu] f:%zu x:%d y:%d l:%zu n:%s\n", k
,
601 (size_t) v
->fatness
, v
->x
, v
->y
, strlen(v
->name
),
605 IF_NZ_SET(out_errstr
, L("write failed"));
610 for (size_t i
= 0; i
< q
->v_num
; ++i
) {
611 for (size_t j
= 0; j
< q
->v_num
; ++j
) {
612 struct rational
*e
= &(q
->e
[i
* q
->v_len
+ j
]);
618 if (fprintf(f
, "e[%zu][%zu] %d/%u\n", i
, j
, (int) e
->p
,
619 (unsigned) e
->q
) < 0) {
621 IF_NZ_SET(out_errstr
, L("write failed"));
629 IF_NZ_SET(out_errstr
, L("flush failed"));
637 /* Set threshold: if quiver_mutate() creates an edge above, warn_out is set */
638 int quiver_set_warning_threshold(struct quiver
*q
, unsigned int warn_threshold
,
639 const char **out_errstr
)
642 IF_NZ_SET(out_errstr
, L("invalid quiver"));
647 q
->edge_weight_threshold
= warn_threshold
;
652 /* Mutate the quiver at vertex k */
653 int quiver_mutate(struct quiver
*q
, size_t k
, int *out_warn
, const
659 IF_NZ_SET(out_errstr
, L("invalid quiver"));
664 /* Step one: complete all triangles */
665 for (size_t i
= 0; i
< q
->v_num
; ++i
) {
670 struct rational
*eik
= &(q
->e
[i
* q
->v_len
+ k
]);
672 for (size_t j
= 0; j
< q
->v_num
; ++j
) {
678 struct rational
*eij
= &(q
->e
[i
* q
->v_len
+ j
]);
679 struct rational
*ekj
= &(q
->e
[k
* q
->v_len
+ j
]);
681 if (eik
->p
* ekj
->p
<= 0) {
685 if ((ret
= add_to_fraction(abs(eik
->p
) * ekj
->p
,
686 eik
->q
* ekj
->q
, eij
,
692 q
->edge_weight_threshold
&&
693 eij
->q
* q
->edge_weight_threshold
<= (unsigned
701 /* Step two: invert all edges that touch k */
702 for (size_t i
= 0; i
< q
->v_num
; ++i
) {
707 struct rational
*eik
= &(q
->e
[i
* q
->v_len
+ k
]);
708 struct rational
*eki
= &(q
->e
[k
* q
->v_len
+ i
]);