Nikos Mavrogiannopoulos wrote: > On Thu, Sep 27, 2012 at 12:12 PM, Nikos Mavrogiannopoulos > <n...@gnutls.org> wrote: >> 4. hash_pjw_s_no_tablesize() >> We can eliminate (2), but still there are 3 variants of the same >> function. Maybe it is better to keep only (4) and (1) for backwards >> compatibility, and anybody who wants to do the % tablesize to do it >> after calling the hash_pjw_s_no_tablesize() function? > > And having said that, here is that function. ... > Subject: [PATCH] Added hash-pjw-s. ...
Hi Nikos, Thanks for working on this. > diff --git a/lib/hash-pjw-s.c b/lib/hash-pjw-s.c ... > +/* hash-pjw-s.c -- compute a hash value from a string. This new function does not care if the data being hashed are a string. I.e., X need not be NUL-terminated. ... > + > +#define SIZE_BITS (sizeof (size_t) * CHAR_BIT) > + > +/* A hash function for char* strings of known size using Please describe the function in terms of the parameters. i.e., /* Return a hash of the N bytes of X using the method described by Bruno Haible in http://www.haible.de/bruno/hashfunc.html. Note that while many hash functions reduce their result via modulo to a 0..table_size-1 range, this function does not do that. */ > + the method described by Bruno Haible. > + The hash output isn't restricted to any number. > + See http://www.haible.de/bruno/hashfunc.html. */ > + > +size_t > +hash_pjw_s (const void *x, size_t x_size) I'd rename s/x_size/n/ > +{ > + const unsigned char *s=x; > + size_t h = 0; > + unsigned i; > + > + for (i=0; i<x_size;i++) spacing: for (i = 0; i < x_size; i++) > + h = s[i] + ((h << 9) | (h >> (SIZE_BITS - 9))); > + > + return h; > +} > diff --git a/lib/hash-pjw-s.h b/lib/hash-pjw-s.h > new file mode 100644 > index 0000000..8d685d6 > --- /dev/null > +++ b/lib/hash-pjw-s.h > @@ -0,0 +1,24 @@ > +/* 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 <http://www.gnu.org/licenses/>. */ > + > +#include <stddef.h> > + > +/* Compute a hash code for a string starting at X and of size X_SIZE, This is good, but I'd remove the reference to "a string", as above. > + and return the hash code. Note that unlike hash_pjw_s, it does not Typo: s/hash_pjw_s/hash_pjw/ > + return it 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 (const void *x, size_t x_size) _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. Another unwanted "string" > +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