11 #define MAX_SUPPORTED_EDGE_NUM (((size_t) -1) >> 2)
12 #define MAX_SUPPORTED_VERTEX_NUM (((size_t) -1) >> 2)
14 /* Primes for reducing fractions */
15 static uint32_t primes
[] = {
17 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
18 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139,
19 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223,
20 227, 229, 233, 239, 241, 251
23 /* Attempt to store a fraction in out, reducing if possible */
24 static int reduce_fraction(int_fast32_t n
, uint_fast32_t d
, struct
25 rational
*out
, const char **out_errstr
)
30 for (j
= 0; j
< ((sizeof primes
) / (sizeof primes
[0])); ++j
) {
45 if (n
> INT_FAST8_MAX
||
48 IF_NZ_SET(out_errstr
, L("unrepresentable fraction"));
53 *out
= (struct rational
) { .p
= n
, .q
= d
};
59 static int add_to_fraction(int_fast8_t a
, uint_fast8_t b
, struct rational
*out
,
60 const char **out_errstr
)
62 int_fast32_t n
= a
* out
->q
+ out
->p
* b
;
63 uint_fast32_t d
= b
* out
->q
;
65 return reduce_fraction(n
, d
, out
, out_errstr
);
68 /* Add an arrow of weight a/b from i -> j, affecting e(i,j) and e(j,i) */
69 int quiver_add_to_edge(struct quiver
*q
, size_t i
, size_t j
, int_fast8_t a
,
70 uint_fast8_t b
, const char **out_errstr
)
73 struct rational
*eij
= &(q
->e
[i
* q
->v_len
+ j
]);
74 struct rational
*eji
= &(q
->e
[j
* q
->v_len
+ i
]);
77 IF_NZ_SET(out_errstr
, L("nonexistant quiver"));
84 IF_NZ_SET(out_errstr
, L("edge includes nonexistant vertex"));
89 if ((ret
= add_to_fraction(a
* q
->v
[j
].fatness
, b
, eij
, out_errstr
))) {
93 if ((ret
= add_to_fraction(-1 * a
* q
->v
[i
].fatness
, b
, eji
,
103 /* Add a vertex with a name and weight */
104 int quiver_add_vertex(struct quiver
*q
, size_t *out_i
, const char *name
,
105 uint_fast16_t fatness
, int x
, int y
, const
110 size_t l
= strlen(name
);
117 IF_NZ_SET(out_errstr
, L("invalid quiver"));
122 if (!(newname
= malloc(l
+ 1))) {
124 IF_NZ_SET(out_errstr
, L("malloc"));
129 strcpy(newname
, name
);
131 if (q
->v_num
>= q
->v_len
) {
132 newlen
= q
->v_len
+ 8;
134 /* XXX: check for overflow here */
135 if (!(newmem
= malloc(newlen
* newlen
* sizeof (*q
->e
)))) {
137 IF_NZ_SET(out_errstr
, L("too many vertices"));
142 for (j
= 0; j
< q
->v_num
; ++j
) {
143 for (k
= 0; k
< q
->v_num
; ++k
) {
144 ((struct rational
*) newmem
)[j
* newlen
+ k
] =
145 q
->e
[j
* q
->v_len
+ k
];
148 for (k
= q
->v_num
; k
< newlen
; ++k
) {
149 ((struct rational
*) newmem
)[j
* newlen
+ k
] =
150 (struct rational
) { .p
= 0, .q
= 1 };
154 for (j
= q
->v_num
; j
< newlen
; ++j
) {
155 for (k
= 0; k
< newlen
; ++k
) {
156 ((struct rational
*) newmem
)[j
* newlen
+ k
] =
157 (struct rational
) { .p
= 0, .q
= 1 };
163 if (!(newmem
= (realloc(q
->v
, newlen
* sizeof (*q
->v
))))) {
165 IF_NZ_SET(out_errstr
, L("too many vertices"));
174 for (k
= 0; k
<= q
->v_num
; ++k
) {
175 q
->e
[k
* q
->v_len
+ q
->v_num
] = (struct rational
) { .p
= 0, .q
=
177 q
->e
[q
->v_num
* q
->v_len
+ k
] = (struct rational
) { .p
= 0, .q
=
181 q
->v
[q
->v_num
] = (struct vertex
) { .name
= newname
, .fatness
= fatness
,
189 /* Mutate the quiver at vertex k */
190 int quiver_mutate(struct quiver
*q
, size_t k
, const char **out_errstr
)
194 struct rational
*eik
;
195 struct rational
*eij
;
196 struct rational
*ekj
;
200 IF_NZ_SET(out_errstr
, L("invalid quiver"));
205 /* Step one: complete all triangles */
206 for (i
= 0; i
< q
->v_num
; ++i
) {
211 eik
= &(q
->e
[i
* q
->v_len
+ k
]);
213 for (j
= 0; j
< q
->v_num
; ++j
) {
219 eij
= &(q
->e
[i
* q
->v_len
+ j
]);
220 ekj
= &(q
->e
[k
* q
->v_len
+ j
]);
222 if (eik
->p
* ekj
->p
<= 0) {
226 if ((ret
= add_to_fraction(abs(eik
->p
) * ekj
->p
,
227 eik
->q
* ekj
->q
, eij
,
234 /* Step two: invert all edges that touch k */
235 for (i
= 0; i
< q
->v_num
; ++i
) {
240 eik
= &(q
->e
[i
* q
->v_len
+ k
]);