>From d48f54a232b388c184124cc0b2e7998662cb4091 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Thu, 27 Sep 2012 08:42:07 +0200 Subject: [PATCH 1/2] Added hash-pjw-s. --- lib/hash-pjw-s.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ lib/hash-pjw-s.h | 23 +++++++++++++++++++++++ modules/hash-pjw-s | 22 ++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 lib/hash-pjw-s.c create mode 100644 lib/hash-pjw-s.h create mode 100644 modules/hash-pjw-s diff --git a/lib/hash-pjw-s.c b/lib/hash-pjw-s.c new file mode 100644 index 0000000..9baaf35 --- /dev/null +++ b/lib/hash-pjw-s.c @@ -0,0 +1,44 @@ +/* hash-pjw.c -- compute a hash value from a NUL-terminated string. + + Copyright (C) 2001, 2003, 2006, 2009-2012 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +#include "hash-pjw.h" + +#include + +#define SIZE_BITS (sizeof (size_t) * CHAR_BIT) + +/* A hash function for char* strings of known size using + the method described by Bruno Haible. + See http://www.haible.de/bruno/hashfunc.html. */ + +size_t +hash_pjw_s (const void *x, size_t x_size, size_t tablesize) +{ + const unsigned char *s=x; + size_t h = 0; + unsigned i; + + for (i=0; i> (SIZE_BITS - 9))); + + if (tablesize) + return h % tablesize; + else + return h; +} diff --git a/lib/hash-pjw-s.h b/lib/hash-pjw-s.h new file mode 100644 index 0000000..197eaa5 --- /dev/null +++ b/lib/hash-pjw-s.h @@ -0,0 +1,23 @@ +/* hash-pjw.h -- declaration for a simple hash function + Copyright (C) 2001, 2003, 2009-2012 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +/* Compute a hash code for a NUL-terminated string starting at X, + and return the hash code modulo TABLESIZE. + The result is platform dependent: it depends on the size of the 'size_t' + type and on the signedness of the 'char' type. */ +extern size_t hash_pjw_s (void const *x, size_t tablesize) _GL_ATTRIBUTE_PURE; diff --git a/modules/hash-pjw-s b/modules/hash-pjw-s new file mode 100644 index 0000000..1748e65 --- /dev/null +++ b/modules/hash-pjw-s @@ -0,0 +1,22 @@ +Description: +Compute a hash value for a string of known size. + +Files: +lib/hash-pjw-s.h +lib/hash-pjw-s.c + +Depends-on: + +configure.ac: + +Makefile.am: +lib_SOURCES += hash-pjw-s.h hash-pjw-s.c + +Include: +"hash-pjw-s.h" + +License: +LGPLv2+ + +Maintainer: +Jim Meyering -- 1.7.10.4