/* $NetBSD: nbperf-bdz.c,v 1.12 2023/07/31 21:07:50 andvar Exp $ */
/*-
* Copyright (c) 2009, 2012 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Joerg Sonnenberger.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* A full description of the algorithm can be found in:
* "Simple and Space-Efficient Minimal Perfect Hash Functions"
* by Botelho, Pagh and Ziviani, proceedings of WADS 2007.
*/
/*
* The algorithm is based on random, acyclic 3-graphs.
*
* Each edge in the represents a key. The vertices are the reminder of
* the hash function mod n. n = cm with c > 1.23. This ensures that
* an acyclic graph can be found with a very high probality.
*
* An acyclic graph has an edge order, where at least one vertex of
* each edge hasn't been seen before. It is declares the first unvisited
* vertex as authoritive for the edge and assigns a 2bit value to unvisited
* vertices, so that the sum of all vertices of the edge modulo 4 is
* the index of the authoritive vertex.
*/
static void
assign_nodes(struct state *state)
{
struct SIZED(edge) *e;
size_t i, j;
uint32_t t, r, holes;
for (i = 0; i < state->graph.v; ++i)
state->g[i] = 3;
for (i = 0; i < state->graph.e; ++i) {
j = state->graph.output_order[i];
e = &state->graph.edges[j];
if (!state->visited[e->vertices[0]]) {
r = 0;
t = e->vertices[0];
} else if (!state->visited[e->vertices[1]]) {
r = 1;
t = e->vertices[1];
} else {
if (state->visited[e->vertices[2]])
abort();
r = 2;
t = e->vertices[2];
}
state->visited[t] = 2 + j;
if (state->visited[e->vertices[0]] == 0)
state->visited[e->vertices[0]] = 1;
if (state->visited[e->vertices[1]] == 0)
state->visited[e->vertices[1]] = 1;
if (state->visited[e->vertices[2]] == 0)
state->visited[e->vertices[2]] = 1;
if (nbperf->map_output != NULL) {
for (i = 0; i < state->graph.e; ++i)
fprintf(nbperf->map_output, "%" PRIu32 "\n",
state->result_map[i]);
}
}
int
bpz_compute(struct nbperf *nbperf)
{
struct state state;
int retval = -1;
uint32_t v, e;
if (nbperf->c == 0)
nbperf->c = 1.24;
if (nbperf->c < 1.24)
errx(1, "The argument for option -c must be at least 1.24");
if (nbperf->hash_size < 3)
errx(1, "The hash function must generate at least 3 values");
(*nbperf->seed_hash)(nbperf);
e = nbperf->n;
v = nbperf->c * nbperf->n;
if (1.24 * nbperf->n > v)
++v;
if (v < 10)
v = 10;
if (nbperf->allow_hash_fudging)
v = (v + 3) & ~3;