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