1*c75851fcSLionel Sambuc /* $NetBSD: graph2.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: graph2.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 "graph2.h"
49*c75851fcSLionel Sambuc
50*c75851fcSLionel Sambuc static const uint32_t unused = 0xffffffffU;
51*c75851fcSLionel Sambuc
52*c75851fcSLionel Sambuc void
graph2_setup(struct graph2 * graph,uint32_t v,uint32_t e)53*c75851fcSLionel Sambuc graph2_setup(struct graph2 *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 vertex2), v);
59*c75851fcSLionel Sambuc graph->edges = calloc(sizeof(struct edge2), 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
graph2_free(struct graph2 * graph)68*c75851fcSLionel Sambuc graph2_free(struct graph2 *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
graph2_check_duplicates(struct nbperf * nbperf,struct graph2 * graph)80*c75851fcSLionel Sambuc graph2_check_duplicates(struct nbperf *nbperf, struct graph2 *graph)
81*c75851fcSLionel Sambuc {
82*c75851fcSLionel Sambuc struct vertex2 *v;
83*c75851fcSLionel Sambuc struct edge2 *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->right == e2->right &&
93*c75851fcSLionel Sambuc nbperf->keylens[i] == nbperf->keylens[j] &&
94*c75851fcSLionel Sambuc memcmp(nbperf->keys[i], nbperf->keys[j],
95*c75851fcSLionel Sambuc nbperf->keylens[i]) == 0) {
96*c75851fcSLionel Sambuc nbperf->has_duplicates = 1;
97*c75851fcSLionel Sambuc return -1;
98*c75851fcSLionel Sambuc }
99*c75851fcSLionel Sambuc if (e2->l_next == unused)
100*c75851fcSLionel Sambuc break;
101*c75851fcSLionel Sambuc j = e2->l_next;
102*c75851fcSLionel Sambuc e2 = &graph->edges[j];
103*c75851fcSLionel Sambuc }
104*c75851fcSLionel Sambuc }
105*c75851fcSLionel Sambuc return 0;
106*c75851fcSLionel Sambuc }
107*c75851fcSLionel Sambuc
108*c75851fcSLionel Sambuc int
graph2_hash(struct nbperf * nbperf,struct graph2 * graph)109*c75851fcSLionel Sambuc graph2_hash(struct nbperf *nbperf, struct graph2 *graph)
110*c75851fcSLionel Sambuc {
111*c75851fcSLionel Sambuc struct vertex2 *v;
112*c75851fcSLionel Sambuc uint32_t hashes[NBPERF_MAX_HASH_SIZE];
113*c75851fcSLionel Sambuc size_t i;
114*c75851fcSLionel Sambuc
115*c75851fcSLionel Sambuc for (i = 0; i < graph->e; ++i) {
116*c75851fcSLionel Sambuc (*nbperf->compute_hash)(nbperf,
117*c75851fcSLionel Sambuc nbperf->keys[i], nbperf->keylens[i], hashes);
118*c75851fcSLionel Sambuc graph->edges[i].left = hashes[0] % graph->v;
119*c75851fcSLionel Sambuc graph->edges[i].right = hashes[1] % graph->v;
120*c75851fcSLionel Sambuc if (graph->edges[i].left == graph->edges[i].right)
121*c75851fcSLionel Sambuc return -1;
122*c75851fcSLionel Sambuc }
123*c75851fcSLionel Sambuc
124*c75851fcSLionel Sambuc for (i = 0; i < graph->v; ++i) {
125*c75851fcSLionel Sambuc graph->verts[i].l_edge = unused;
126*c75851fcSLionel Sambuc graph->verts[i].r_edge = unused;
127*c75851fcSLionel Sambuc }
128*c75851fcSLionel Sambuc
129*c75851fcSLionel Sambuc for (i = 0; i < graph->e; ++i) {
130*c75851fcSLionel Sambuc v = &graph->verts[graph->edges[i].left];
131*c75851fcSLionel Sambuc if (v->l_edge != unused)
132*c75851fcSLionel Sambuc graph->edges[v->l_edge].l_prev = i;
133*c75851fcSLionel Sambuc graph->edges[i].l_next = v->l_edge;
134*c75851fcSLionel Sambuc graph->edges[i].l_prev = unused;
135*c75851fcSLionel Sambuc v->l_edge = i;
136*c75851fcSLionel Sambuc
137*c75851fcSLionel Sambuc v = &graph->verts[graph->edges[i].right];
138*c75851fcSLionel Sambuc if (v->r_edge != unused)
139*c75851fcSLionel Sambuc graph->edges[v->r_edge].r_prev = i;
140*c75851fcSLionel Sambuc graph->edges[i].r_next = v->r_edge;
141*c75851fcSLionel Sambuc graph->edges[i].r_prev = unused;
142*c75851fcSLionel Sambuc v->r_edge = i;
143*c75851fcSLionel Sambuc }
144*c75851fcSLionel Sambuc
145*c75851fcSLionel Sambuc if (nbperf->first_round) {
146*c75851fcSLionel Sambuc nbperf->first_round = 0;
147*c75851fcSLionel Sambuc return graph2_check_duplicates(nbperf, graph);
148*c75851fcSLionel Sambuc }
149*c75851fcSLionel Sambuc
150*c75851fcSLionel Sambuc return 0;
151*c75851fcSLionel Sambuc }
152*c75851fcSLionel Sambuc
153*c75851fcSLionel Sambuc static void
graph2_remove_vertex(struct graph2 * graph,struct vertex2 * v)154*c75851fcSLionel Sambuc graph2_remove_vertex(struct graph2 *graph, struct vertex2 *v)
155*c75851fcSLionel Sambuc {
156*c75851fcSLionel Sambuc struct edge2 *e;
157*c75851fcSLionel Sambuc struct vertex2 *v2;
158*c75851fcSLionel Sambuc
159*c75851fcSLionel Sambuc for (;;) {
160*c75851fcSLionel Sambuc if (v->l_edge != unused && v->r_edge != unused)
161*c75851fcSLionel Sambuc break;
162*c75851fcSLionel Sambuc if (v->l_edge == unused && v->r_edge == unused)
163*c75851fcSLionel Sambuc break;
164*c75851fcSLionel Sambuc
165*c75851fcSLionel Sambuc if (v->l_edge != unused) {
166*c75851fcSLionel Sambuc e = &graph->edges[v->l_edge];
167*c75851fcSLionel Sambuc if (e->l_next != unused)
168*c75851fcSLionel Sambuc break;
169*c75851fcSLionel Sambuc v->l_edge = unused; /* No other elements possible! */
170*c75851fcSLionel Sambuc v2 = &graph->verts[e->right];
171*c75851fcSLionel Sambuc if (e->r_prev == unused)
172*c75851fcSLionel Sambuc v2->r_edge = e->r_next;
173*c75851fcSLionel Sambuc else
174*c75851fcSLionel Sambuc graph->edges[e->r_prev].r_next = e->r_next;
175*c75851fcSLionel Sambuc if (e->r_next != unused)
176*c75851fcSLionel Sambuc graph->edges[e->r_next].r_prev = e->r_prev;
177*c75851fcSLionel Sambuc v = v2;
178*c75851fcSLionel Sambuc } else {
179*c75851fcSLionel Sambuc e = &graph->edges[v->r_edge];
180*c75851fcSLionel Sambuc if (e->r_next != unused)
181*c75851fcSLionel Sambuc break;
182*c75851fcSLionel Sambuc v->r_edge = unused; /* No other elements possible! */
183*c75851fcSLionel Sambuc v2 = &graph->verts[e->left];
184*c75851fcSLionel Sambuc if (e->l_prev == unused)
185*c75851fcSLionel Sambuc v2->l_edge = e->l_next;
186*c75851fcSLionel Sambuc else
187*c75851fcSLionel Sambuc graph->edges[e->l_prev].l_next = e->l_next;
188*c75851fcSLionel Sambuc if (e->l_next != unused)
189*c75851fcSLionel Sambuc graph->edges[e->l_next].l_prev = e->l_prev;
190*c75851fcSLionel Sambuc v = v2;
191*c75851fcSLionel Sambuc }
192*c75851fcSLionel Sambuc
193*c75851fcSLionel Sambuc graph->output_order[--graph->output_index] = e - graph->edges;
194*c75851fcSLionel Sambuc }
195*c75851fcSLionel Sambuc }
196*c75851fcSLionel Sambuc
197*c75851fcSLionel Sambuc int
graph2_output_order(struct graph2 * graph)198*c75851fcSLionel Sambuc graph2_output_order(struct graph2 *graph)
199*c75851fcSLionel Sambuc {
200*c75851fcSLionel Sambuc size_t i;
201*c75851fcSLionel Sambuc
202*c75851fcSLionel Sambuc graph->output_index = graph->e;
203*c75851fcSLionel Sambuc
204*c75851fcSLionel Sambuc for (i = 0; i < graph->v; ++i)
205*c75851fcSLionel Sambuc graph2_remove_vertex(graph, &graph->verts[i]);
206*c75851fcSLionel Sambuc
207*c75851fcSLionel Sambuc if (graph->output_index != 0)
208*c75851fcSLionel Sambuc return -1;
209*c75851fcSLionel Sambuc
210*c75851fcSLionel Sambuc return 0;
211*c75851fcSLionel Sambuc }
212