/* $NetBSD: nbperf-chm.c,v 1.5 2021/01/26 21:25:55 joerg Exp $ */
/*-
* Copyright (c) 2009 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.
*/
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
/*
* A full description of the algorithm can be found in:
* "An optimal algorithm for generating minimal perfect hash functions"
* by Czech, Havas and Majewski in Information Processing Letters,
* 43(5):256-264, October 1992.
*/
/*
* The algorithm is based on random, acyclic graphs.
*
* Each edge in the represents a key. The vertices are the reminder of
* the hash function mod n. n = cm with c > 2, otherwise the propability
* of finding an acyclic graph is very low (for 2-graphs). The constant
* for 3-graphs is 1.24.
*
* After the hashing phase, the graph is checked for cycles.
* A cycle-free graph is either empty or has a vertex of degree 1.
* Removing the edge for this vertex doesn't change this property,
* so applying this recursively reduces the size of the graph.
* If the graph is empty at the end of the process, it was acyclic.
*
* The assignment step now sets g[i] := 0 and processes the edges
* in reverse order of removal. That ensures that at least one vertex
* is always unvisited and can be assigned.
*/
if (nbperf->map_output != NULL) {
for (i = 0; i < state->graph.e; ++i)
fprintf(nbperf->map_output, "%" PRIu32 "\n", i);
}
}
int
#if GRAPH_SIZE == 3
chm3_compute(struct nbperf *nbperf)
#else
chm_compute(struct nbperf *nbperf)
#endif
{
struct state state;
int retval = -1;
uint32_t v, e;
#if GRAPH_SIZE == 3
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");
#else
if (nbperf->c == 0)
nbperf-> c = 2;
if (nbperf->c < 2)
errx(1, "The argument for option -c must be at least 2");
if (nbperf->hash_size < 2)
errx(1, "The hash function must generate at least 2 values");
#endif
(*nbperf->seed_hash)(nbperf);
e = nbperf->n;
v = nbperf->c * nbperf->n;
#if GRAPH_SIZE == 3
if (v == 1.24 * nbperf->n)
++v;
if (v < 10)
v = 10;
if (nbperf->allow_hash_fudging)
v = (v + 3) & ~3;
#else
if (v == 2 * nbperf->n)
++v;
if (nbperf->allow_hash_fudging)
v = (v + 1) & ~1;
#endif