relation.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Binary relations.
  2. Copyright (C) 2002, 2004-2005, 2009-2015, 2018-2019 Free Software
  3. Foundation, Inc.
  4. This file is part of Bison, the GNU Compiler Compiler.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include <config.h>
  16. #include "system.h"
  17. #include <bitsetv.h>
  18. #include "getargs.h"
  19. #include "relation.h"
  20. void
  21. relation_print (relation r, relation_node size, FILE *out)
  22. {
  23. relation_node i;
  24. relation_node j;
  25. for (i = 0; i < size; ++i)
  26. {
  27. fprintf (out, "%3lu: ", (unsigned long) i);
  28. if (r[i])
  29. for (j = 0; r[i][j] != END_NODE; ++j)
  30. fprintf (out, "%3lu ", (unsigned long) r[i][j]);
  31. fputc ('\n', out);
  32. }
  33. fputc ('\n', out);
  34. }
  35. /*---------------------------------------------------------------.
  36. | digraph & traverse. |
  37. | |
  38. | The following variables are used as common storage between the |
  39. | two. |
  40. `---------------------------------------------------------------*/
  41. static relation R;
  42. static relation_nodes INDEX;
  43. static relation_nodes VERTICES;
  44. static relation_node top;
  45. static relation_node infinity;
  46. static bitsetv F;
  47. static void
  48. traverse (relation_node i)
  49. {
  50. relation_node j;
  51. relation_node height;
  52. VERTICES[++top] = i;
  53. INDEX[i] = height = top;
  54. if (R[i])
  55. for (j = 0; R[i][j] != END_NODE; ++j)
  56. {
  57. if (INDEX[R[i][j]] == 0)
  58. traverse (R[i][j]);
  59. if (INDEX[i] > INDEX[R[i][j]])
  60. INDEX[i] = INDEX[R[i][j]];
  61. bitset_or (F[i], F[i], F[R[i][j]]);
  62. }
  63. if (INDEX[i] == height)
  64. for (;;)
  65. {
  66. j = VERTICES[top--];
  67. INDEX[j] = infinity;
  68. if (i == j)
  69. break;
  70. bitset_copy (F[j], F[i]);
  71. }
  72. }
  73. void
  74. relation_digraph (relation r, relation_node size, bitsetv *function)
  75. {
  76. relation_node i;
  77. infinity = size + 2;
  78. INDEX = xcalloc (size + 1, sizeof *INDEX);
  79. VERTICES = xnmalloc (size + 1, sizeof *VERTICES);
  80. top = 0;
  81. R = r;
  82. F = *function;
  83. for (i = 0; i < size; i++)
  84. if (INDEX[i] == 0 && R[i])
  85. traverse (i);
  86. free (INDEX);
  87. free (VERTICES);
  88. *function = F;
  89. }
  90. /*-------------------------------------------.
  91. | Destructively transpose R_ARG, of size N. |
  92. `-------------------------------------------*/
  93. void
  94. relation_transpose (relation *R_arg, relation_node n)
  95. {
  96. relation r = *R_arg;
  97. /* The result. */
  98. relation new_R = xnmalloc (n, sizeof *new_R);
  99. /* END_R[I] -- next entry of NEW_R[I]. */
  100. relation end_R = xnmalloc (n, sizeof *end_R);
  101. /* NEDGES[I] -- total size of NEW_R[I]. */
  102. size_t *nedges = xcalloc (n, sizeof *nedges);
  103. relation_node i;
  104. relation_node j;
  105. if (trace_flag & trace_sets)
  106. {
  107. fputs ("relation_transpose: input\n", stderr);
  108. relation_print (r, n, stderr);
  109. }
  110. /* Count. */
  111. for (i = 0; i < n; i++)
  112. if (r[i])
  113. for (j = 0; r[i][j] != END_NODE; ++j)
  114. ++nedges[r[i][j]];
  115. /* Allocate. */
  116. for (i = 0; i < n; i++)
  117. {
  118. relation_node *sp = NULL;
  119. if (nedges[i] > 0)
  120. {
  121. sp = xnmalloc (nedges[i] + 1, sizeof *sp);
  122. sp[nedges[i]] = END_NODE;
  123. }
  124. new_R[i] = sp;
  125. end_R[i] = sp;
  126. }
  127. /* Store. */
  128. for (i = 0; i < n; i++)
  129. if (r[i])
  130. for (j = 0; r[i][j] != END_NODE; ++j)
  131. *end_R[r[i][j]]++ = i;
  132. free (nedges);
  133. free (end_R);
  134. /* Free the input: it is replaced with the result. */
  135. for (i = 0; i < n; i++)
  136. free (r[i]);
  137. free (r);
  138. if (trace_flag & trace_sets)
  139. {
  140. fputs ("relation_transpose: output\n", stderr);
  141. relation_print (new_R, n, stderr);
  142. }
  143. *R_arg = new_R;
  144. }