Home | History | Annotate | Line # | Download | only in nbperf
graph2.c revision 1.1
      1  1.1  joerg /*	$NetBSD: graph2.c,v 1.1 2009/08/15 16:21:04 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.1  joerg #include <sys/cdefs.h>
     35  1.1  joerg __RCSID("$NetBSD: graph2.c,v 1.1 2009/08/15 16:21:04 joerg Exp $");
     36  1.1  joerg 
     37  1.1  joerg #include <err.h>
     38  1.1  joerg #include <inttypes.h>
     39  1.1  joerg #include <stdio.h>
     40  1.1  joerg #include <stdlib.h>
     41  1.1  joerg 
     42  1.1  joerg #include "nbperf.h"
     43  1.1  joerg #include "graph2.h"
     44  1.1  joerg 
     45  1.1  joerg static const uint32_t unused = 0xffffffffU;
     46  1.1  joerg 
     47  1.1  joerg void
     48  1.1  joerg graph2_setup(struct graph2 *graph, uint32_t v, uint32_t e)
     49  1.1  joerg {
     50  1.1  joerg 	graph->v = v;
     51  1.1  joerg 	graph->e = e;
     52  1.1  joerg 
     53  1.1  joerg 	graph->verts = calloc(sizeof(struct vertex2), v);
     54  1.1  joerg 	graph->edges = calloc(sizeof(struct edge2), e);
     55  1.1  joerg 	graph->output_order = calloc(sizeof(uint32_t), e);
     56  1.1  joerg 
     57  1.1  joerg 	if (graph->verts == NULL || graph->edges == NULL ||
     58  1.1  joerg 	    graph->output_order == NULL)
     59  1.1  joerg 		err(1, "malloc failed");
     60  1.1  joerg }
     61  1.1  joerg 
     62  1.1  joerg void
     63  1.1  joerg graph2_free(struct graph2 *graph)
     64  1.1  joerg {
     65  1.1  joerg 	free(graph->verts);
     66  1.1  joerg 	free(graph->edges);
     67  1.1  joerg 	free(graph->output_order);
     68  1.1  joerg 
     69  1.1  joerg 	graph->verts = NULL;
     70  1.1  joerg 	graph->edges = NULL;
     71  1.1  joerg 	graph->output_order = NULL;
     72  1.1  joerg }
     73  1.1  joerg 
     74  1.1  joerg int
     75  1.1  joerg graph2_hash(struct nbperf *nbperf, struct graph2 *graph)
     76  1.1  joerg {
     77  1.1  joerg 	struct vertex2 *v;
     78  1.1  joerg 	uint32_t hashes[nbperf->hash_size];
     79  1.1  joerg 	size_t i;
     80  1.1  joerg 
     81  1.1  joerg 	for (i = 0; i < graph->e; ++i) {
     82  1.1  joerg 		(*nbperf->compute_hash)(nbperf,
     83  1.1  joerg 		    nbperf->keys[i], nbperf->keylens[i], hashes);
     84  1.1  joerg 		graph->edges[i].left = hashes[0] % graph->v;
     85  1.1  joerg 		graph->edges[i].right = hashes[1] % graph->v;
     86  1.1  joerg 		if (graph->edges[i].left == graph->edges[i].right)
     87  1.1  joerg 			return -1;
     88  1.1  joerg 	}
     89  1.1  joerg 
     90  1.1  joerg 	for (i = 0; i < graph->v; ++i) {
     91  1.1  joerg 		graph->verts[i].l_edge = unused;
     92  1.1  joerg 		graph->verts[i].r_edge = unused;
     93  1.1  joerg 	}
     94  1.1  joerg 
     95  1.1  joerg 	for (i = 0; i < graph->e; ++i) {
     96  1.1  joerg 		v = &graph->verts[graph->edges[i].left];
     97  1.1  joerg 		if (v->l_edge != unused)
     98  1.1  joerg 			graph->edges[v->l_edge].l_prev = i;
     99  1.1  joerg 		graph->edges[i].l_next = v->l_edge;
    100  1.1  joerg 		graph->edges[i].l_prev = unused;
    101  1.1  joerg 		v->l_edge = i;
    102  1.1  joerg 
    103  1.1  joerg 		v = &graph->verts[graph->edges[i].right];
    104  1.1  joerg 		if (v->r_edge != unused)
    105  1.1  joerg 			graph->edges[v->r_edge].r_prev = i;
    106  1.1  joerg 		graph->edges[i].r_next = v->r_edge;
    107  1.1  joerg 		graph->edges[i].r_prev = unused;
    108  1.1  joerg 		v->r_edge = i;
    109  1.1  joerg 	}
    110  1.1  joerg 
    111  1.1  joerg 	return 0;
    112  1.1  joerg }
    113  1.1  joerg 
    114  1.1  joerg static void
    115  1.1  joerg graph2_remove_vertex(struct graph2 *graph, struct vertex2 *v)
    116  1.1  joerg {
    117  1.1  joerg 	struct edge2 *e;
    118  1.1  joerg 	struct vertex2 *v2;
    119  1.1  joerg 
    120  1.1  joerg 	for (;;) {
    121  1.1  joerg 		if (v->l_edge != unused && v->r_edge != unused)
    122  1.1  joerg 			break;
    123  1.1  joerg 		if (v->l_edge == unused && v->r_edge == unused)
    124  1.1  joerg 			break;
    125  1.1  joerg 
    126  1.1  joerg 		if (v->l_edge != unused) {
    127  1.1  joerg 			e = &graph->edges[v->l_edge];
    128  1.1  joerg 			if (e->l_next != unused)
    129  1.1  joerg 				break;
    130  1.1  joerg 			v->l_edge = unused; /* No other elements possible! */
    131  1.1  joerg 			v2 = &graph->verts[e->right];
    132  1.1  joerg 			if (e->r_prev == unused)
    133  1.1  joerg 				v2->r_edge = e->r_next;
    134  1.1  joerg 			else
    135  1.1  joerg 				graph->edges[e->r_prev].r_next = e->r_next;
    136  1.1  joerg 			if (e->r_next != unused)
    137  1.1  joerg 				graph->edges[e->r_next].r_prev = e->r_prev;
    138  1.1  joerg 			v = v2;
    139  1.1  joerg 		} else {
    140  1.1  joerg 			e = &graph->edges[v->r_edge];
    141  1.1  joerg 			if (e->r_next != unused)
    142  1.1  joerg 				break;
    143  1.1  joerg 			v->r_edge = unused; /* No other elements possible! */
    144  1.1  joerg 			v2 = &graph->verts[e->left];
    145  1.1  joerg 			if (e->l_prev == unused)
    146  1.1  joerg 				v2->l_edge = e->l_next;
    147  1.1  joerg 			else
    148  1.1  joerg 				graph->edges[e->l_prev].l_next = e->l_next;
    149  1.1  joerg 			if (e->l_next != unused)
    150  1.1  joerg 				graph->edges[e->l_next].l_prev = e->l_prev;
    151  1.1  joerg 			v = v2;
    152  1.1  joerg 		}
    153  1.1  joerg 
    154  1.1  joerg 		graph->output_order[--graph->output_index] = e - graph->edges;
    155  1.1  joerg 	}
    156  1.1  joerg }
    157  1.1  joerg 
    158  1.1  joerg int
    159  1.1  joerg graph2_output_order(struct graph2 *graph)
    160  1.1  joerg {
    161  1.1  joerg 	size_t i;
    162  1.1  joerg 
    163  1.1  joerg 	graph->output_index = graph->e;
    164  1.1  joerg 
    165  1.1  joerg 	for (i = 0; i < graph->v; ++i)
    166  1.1  joerg 		graph2_remove_vertex(graph, &graph->verts[i]);
    167  1.1  joerg 
    168  1.1  joerg 	if (graph->output_index != 0)
    169  1.1  joerg 		return -1;
    170  1.1  joerg 
    171  1.1  joerg 	return 0;
    172  1.1  joerg }
    173