xref: /netbsd/usr.bin/nbperf/graph2.c (revision 6550d01e)
1 /*	$NetBSD: graph2.c,v 1.3 2010/03/03 01:55:04 joerg Exp $	*/
2 /*-
3  * Copyright (c) 2009 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Joerg Sonnenberger.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: graph2.c,v 1.3 2010/03/03 01:55:04 joerg Exp $");
36 
37 #include <err.h>
38 #include <inttypes.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #include "nbperf.h"
44 #include "graph2.h"
45 
46 static const uint32_t unused = 0xffffffffU;
47 
48 void
49 graph2_setup(struct graph2 *graph, uint32_t v, uint32_t e)
50 {
51 	graph->v = v;
52 	graph->e = e;
53 
54 	graph->verts = calloc(sizeof(struct vertex2), v);
55 	graph->edges = calloc(sizeof(struct edge2), e);
56 	graph->output_order = calloc(sizeof(uint32_t), e);
57 
58 	if (graph->verts == NULL || graph->edges == NULL ||
59 	    graph->output_order == NULL)
60 		err(1, "malloc failed");
61 }
62 
63 void
64 graph2_free(struct graph2 *graph)
65 {
66 	free(graph->verts);
67 	free(graph->edges);
68 	free(graph->output_order);
69 
70 	graph->verts = NULL;
71 	graph->edges = NULL;
72 	graph->output_order = NULL;
73 }
74 
75 static int
76 graph2_check_duplicates(struct nbperf *nbperf, struct graph2 *graph)
77 {
78 	struct vertex2 *v;
79 	struct edge2 *e, *e2;
80 	uint32_t i, j;
81 
82 	for (i = 0; i < graph->e; ++i) {
83 		e = &graph->edges[i];
84 		v = &graph->verts[e->left];
85 		j = v->l_edge;
86 		e2 = &graph->edges[j];
87 		for (;;) {
88 			if (i < j && e->right == e2->right &&
89 			    nbperf->keylens[i] == nbperf->keylens[j] &&
90 			    memcmp(nbperf->keys[i], nbperf->keys[j],
91 			    nbperf->keylens[i]) == 0) {
92 				nbperf->has_duplicates = 1;
93 				return -1;
94 			}
95 			if (e2->l_next == unused)
96 				break;
97 			j = e2->l_next;
98 			e2 = &graph->edges[j];
99 		}
100 	}
101 	return 0;
102 }
103 
104 int
105 graph2_hash(struct nbperf *nbperf, struct graph2 *graph)
106 {
107 	struct vertex2 *v;
108 	uint32_t hashes[NBPERF_MAX_HASH_SIZE];
109 	size_t i;
110 
111 	for (i = 0; i < graph->e; ++i) {
112 		(*nbperf->compute_hash)(nbperf,
113 		    nbperf->keys[i], nbperf->keylens[i], hashes);
114 		graph->edges[i].left = hashes[0] % graph->v;
115 		graph->edges[i].right = hashes[1] % graph->v;
116 		if (graph->edges[i].left == graph->edges[i].right)
117 			return -1;
118 	}
119 
120 	for (i = 0; i < graph->v; ++i) {
121 		graph->verts[i].l_edge = unused;
122 		graph->verts[i].r_edge = unused;
123 	}
124 
125 	for (i = 0; i < graph->e; ++i) {
126 		v = &graph->verts[graph->edges[i].left];
127 		if (v->l_edge != unused)
128 			graph->edges[v->l_edge].l_prev = i;
129 		graph->edges[i].l_next = v->l_edge;
130 		graph->edges[i].l_prev = unused;
131 		v->l_edge = i;
132 
133 		v = &graph->verts[graph->edges[i].right];
134 		if (v->r_edge != unused)
135 			graph->edges[v->r_edge].r_prev = i;
136 		graph->edges[i].r_next = v->r_edge;
137 		graph->edges[i].r_prev = unused;
138 		v->r_edge = i;
139 	}
140 
141 	if (nbperf->first_round) {
142 		nbperf->first_round = 0;
143 		return graph2_check_duplicates(nbperf, graph);
144 	}
145 
146 	return 0;
147 }
148 
149 static void
150 graph2_remove_vertex(struct graph2 *graph, struct vertex2 *v)
151 {
152 	struct edge2 *e;
153 	struct vertex2 *v2;
154 
155 	for (;;) {
156 		if (v->l_edge != unused && v->r_edge != unused)
157 			break;
158 		if (v->l_edge == unused && v->r_edge == unused)
159 			break;
160 
161 		if (v->l_edge != unused) {
162 			e = &graph->edges[v->l_edge];
163 			if (e->l_next != unused)
164 				break;
165 			v->l_edge = unused; /* No other elements possible! */
166 			v2 = &graph->verts[e->right];
167 			if (e->r_prev == unused)
168 				v2->r_edge = e->r_next;
169 			else
170 				graph->edges[e->r_prev].r_next = e->r_next;
171 			if (e->r_next != unused)
172 				graph->edges[e->r_next].r_prev = e->r_prev;
173 			v = v2;
174 		} else {
175 			e = &graph->edges[v->r_edge];
176 			if (e->r_next != unused)
177 				break;
178 			v->r_edge = unused; /* No other elements possible! */
179 			v2 = &graph->verts[e->left];
180 			if (e->l_prev == unused)
181 				v2->l_edge = e->l_next;
182 			else
183 				graph->edges[e->l_prev].l_next = e->l_next;
184 			if (e->l_next != unused)
185 				graph->edges[e->l_next].l_prev = e->l_prev;
186 			v = v2;
187 		}
188 
189 		graph->output_order[--graph->output_index] = e - graph->edges;
190 	}
191 }
192 
193 int
194 graph2_output_order(struct graph2 *graph)
195 {
196 	size_t i;
197 
198 	graph->output_index = graph->e;
199 
200 	for (i = 0; i < graph->v; ++i)
201 		graph2_remove_vertex(graph, &graph->verts[i]);
202 
203 	if (graph->output_index != 0)
204 		return -1;
205 
206 	return 0;
207 }
208