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