-/*-------------------------------------------------------------------.
-| Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
-| replaced with the result. |
-| |
-| R_ARG[I] is NULL or a -1 terminated list of numbers. |
-| |
-| RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM |
-| is in R_ARG[I]. |
-`-------------------------------------------------------------------*/
-
-static short **
-transpose (short **R_arg, int n)
-{
- /* The result. */
- short **new_R = XCALLOC (short *, n);
- /* END_R[I] -- next entry of NEW_R[I]. */
- short **end_R = XCALLOC (short *, n);
- /* NEDGES[I] -- total size of NEW_R[I]. */
- short *nedges = XCALLOC (short, n);
- int i, j;
-
- if (trace_flag)
- {
- fputs ("transpose: input\n", stderr);
- matrix_print (stderr, R_arg, n);
- }
-
- /* Count. */
- for (i = 0; i < n; i++)
- if (R_arg[i])
- for (j = 0; R_arg[i][j] >= 0; ++j)
- ++nedges[R_arg[i][j]];
-
- /* Allocate. */
- for (i = 0; i < n; i++)
- if (nedges[i] > 0)
- {
- short *sp = XCALLOC (short, nedges[i] + 1);
- sp[nedges[i]] = -1;
- new_R[i] = sp;
- end_R[i] = sp;
- }
-
- /* Store. */
- for (i = 0; i < n; i++)
- if (R_arg[i])
- for (j = 0; R_arg[i][j] >= 0; ++j)
- {
- *end_R[R_arg[i][j]] = i;
- ++end_R[R_arg[i][j]];
- }
-
- free (nedges);
- free (end_R);
-
- /* Free the input: it is replaced with the result. */
- for (i = 0; i < n; i++)
- XFREE (R_arg[i]);
- free (R_arg);
-
- if (trace_flag)
- {
- fputs ("transpose: output\n", stderr);
- matrix_print (stderr, new_R, n);
- }
-
- return new_R;
-}