xref: /openbsd-src/usr.bin/yacc/warshall.c (revision 8d3728c6f703dfa010fc9e102c7d13f5b94bc31f)
1*8d3728c6Stedu /* $OpenBSD: warshall.c,v 1.11 2014/03/13 01:18:22 tedu Exp $	 */
2f3bae140Sderaadt /* $NetBSD: warshall.c,v 1.4 1996/03/19 03:21:51 jtc Exp $	 */
3f3bae140Sderaadt 
4f3bae140Sderaadt /*
5f3bae140Sderaadt  * Copyright (c) 1989 The Regents of the University of California.
6f3bae140Sderaadt  * All rights reserved.
7f3bae140Sderaadt  *
8f3bae140Sderaadt  * This code is derived from software contributed to Berkeley by
9f3bae140Sderaadt  * Robert Paul Corbett.
10f3bae140Sderaadt  *
11f3bae140Sderaadt  * Redistribution and use in source and binary forms, with or without
12f3bae140Sderaadt  * modification, are permitted provided that the following conditions
13f3bae140Sderaadt  * are met:
14f3bae140Sderaadt  * 1. Redistributions of source code must retain the above copyright
15f3bae140Sderaadt  *    notice, this list of conditions and the following disclaimer.
16f3bae140Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
17f3bae140Sderaadt  *    notice, this list of conditions and the following disclaimer in the
18f3bae140Sderaadt  *    documentation and/or other materials provided with the distribution.
19f75387cbSmillert  * 3. Neither the name of the University nor the names of its contributors
20f3bae140Sderaadt  *    may be used to endorse or promote products derived from this software
21f3bae140Sderaadt  *    without specific prior written permission.
22f3bae140Sderaadt  *
23f3bae140Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24f3bae140Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25f3bae140Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26f3bae140Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27f3bae140Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28f3bae140Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29f3bae140Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30f3bae140Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31f3bae140Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32f3bae140Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33f3bae140Sderaadt  * SUCH DAMAGE.
34f3bae140Sderaadt  */
35f3bae140Sderaadt 
36df930be7Sderaadt #include "defs.h"
37df930be7Sderaadt 
38d1e49392Spvalchev void transitive_closure(unsigned int *, int);
391d699125Spvalchev 
401d699125Spvalchev void
transitive_closure(unsigned int * R,int n)41d1e49392Spvalchev transitive_closure(unsigned int *R, int n)
42df930be7Sderaadt {
43c0932ef1Smpech 	int rowsize;
44*8d3728c6Stedu 	unsigned int i;
45*8d3728c6Stedu 	unsigned int *rowj, *rp, *rend, *ccol, *relend, *cword, *rowi;
46df930be7Sderaadt 
47df930be7Sderaadt 	rowsize = WORDSIZE(n);
48df930be7Sderaadt 	relend = R + n * rowsize;
49df930be7Sderaadt 
50df930be7Sderaadt 	cword = R;
51df930be7Sderaadt 	i = 0;
52df930be7Sderaadt 	rowi = R;
53bbfc4b6eStedu 	while (rowi < relend) {
54df930be7Sderaadt 		ccol = cword;
55df930be7Sderaadt 		rowj = R;
56df930be7Sderaadt 
57bbfc4b6eStedu 		while (rowj < relend) {
58bbfc4b6eStedu 			if (*ccol & (1 << i)) {
59df930be7Sderaadt 				rp = rowi;
60df930be7Sderaadt 				rend = rowj + rowsize;
61df930be7Sderaadt 				while (rowj < rend)
62df930be7Sderaadt 					*rowj++ |= *rp++;
63bbfc4b6eStedu 			} else {
64df930be7Sderaadt 				rowj += rowsize;
65df930be7Sderaadt 			}
66df930be7Sderaadt 
67df930be7Sderaadt 			ccol += rowsize;
68df930be7Sderaadt 		}
69df930be7Sderaadt 
70bbfc4b6eStedu 		if (++i >= BITS_PER_WORD) {
71df930be7Sderaadt 			i = 0;
72df930be7Sderaadt 			cword++;
73df930be7Sderaadt 		}
74df930be7Sderaadt 		rowi += rowsize;
75df930be7Sderaadt 	}
76df930be7Sderaadt }
77df930be7Sderaadt 
781d699125Spvalchev void
reflexive_transitive_closure(unsigned int * R,int n)79d1e49392Spvalchev reflexive_transitive_closure(unsigned int *R, int n)
80df930be7Sderaadt {
81c0932ef1Smpech 	int rowsize;
82*8d3728c6Stedu 	unsigned int i;
83*8d3728c6Stedu 	unsigned int *rp, *relend;
84df930be7Sderaadt 
85df930be7Sderaadt 	transitive_closure(R, n);
86df930be7Sderaadt 
87df930be7Sderaadt 	rowsize = WORDSIZE(n);
88df930be7Sderaadt 	relend = R + n * rowsize;
89df930be7Sderaadt 
90df930be7Sderaadt 	i = 0;
91df930be7Sderaadt 	rp = R;
92bbfc4b6eStedu 	while (rp < relend) {
93df930be7Sderaadt 		*rp |= (1 << i);
94bbfc4b6eStedu 		if (++i >= BITS_PER_WORD) {
95df930be7Sderaadt 			i = 0;
96df930be7Sderaadt 			rp++;
97df930be7Sderaadt 		}
98df930be7Sderaadt 		rp += rowsize;
99df930be7Sderaadt 	}
100df930be7Sderaadt }
101