warshall.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Generate transitive closure of a matrix,
  2. Copyright (C) 1984, 1989 Free Software Foundation, Inc.
  3. This file is part of Bison, the GNU Compiler Compiler.
  4. Bison is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. Bison is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Bison; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. #include <stdio.h>
  16. #include "system.h"
  17. #include "machine.h"
  18. /* given n by n matrix of bits R, modify its contents
  19. to be the transive closure of what was given. */
  20. void
  21. TC(R, n)
  22. unsigned *R;
  23. int n;
  24. {
  25. register int rowsize;
  26. register unsigned mask;
  27. register unsigned *rowj;
  28. register unsigned *rp;
  29. register unsigned *rend;
  30. register unsigned *ccol;
  31. unsigned *relend;
  32. unsigned *cword;
  33. unsigned *rowi;
  34. rowsize = WORDSIZE(n) * sizeof(unsigned);
  35. relend = (unsigned *) ((char *) R + (n * rowsize));
  36. cword = R;
  37. mask = 1;
  38. rowi = R;
  39. while (rowi < relend)
  40. {
  41. ccol = cword;
  42. rowj = R;
  43. while (rowj < relend)
  44. {
  45. if (*ccol & mask)
  46. {
  47. rp = rowi;
  48. rend = (unsigned *) ((char *) rowj + rowsize);
  49. while (rowj < rend)
  50. *rowj++ |= *rp++;
  51. }
  52. else
  53. {
  54. rowj = (unsigned *) ((char *) rowj + rowsize);
  55. }
  56. ccol = (unsigned *) ((char *) ccol + rowsize);
  57. }
  58. mask <<= 1;
  59. if (mask == 0)
  60. {
  61. mask = 1;
  62. cword++;
  63. }
  64. rowi = (unsigned *) ((char *) rowi + rowsize);
  65. }
  66. }
  67. /* Reflexive Transitive Closure. Same as TC
  68. and then set all the bits on the diagonal of R. */
  69. void
  70. RTC(R, n)
  71. unsigned *R;
  72. int n;
  73. {
  74. register int rowsize;
  75. register unsigned mask;
  76. register unsigned *rp;
  77. register unsigned *relend;
  78. TC(R, n);
  79. rowsize = WORDSIZE(n) * sizeof(unsigned);
  80. relend = (unsigned *) ((char *) R + n*rowsize);
  81. mask = 1;
  82. rp = R;
  83. while (rp < relend)
  84. {
  85. *rp |= mask;
  86. mask <<= 1;
  87. if (mask == 0)
  88. {
  89. mask = 1;
  90. rp++;
  91. }
  92. rp = (unsigned *) ((char *) rp + rowsize);
  93. }
  94. }