On 09/27/2012 05:13 AM, Paul Eggert wrote:

> On 09/26/2012 09:40 AM, Jim Meyering wrote:
>> Yes, switching it to LGPLv2+ is fine with me.
> OK, thanks, I did that.


Thank you. Attached you'll find a patch to add hash_pjw_s() and a
different patch which returns the full number if requested in hash_pjw().

regards,
Nikos
>From 82b53fda2a73bbdbd7483fb0a74dc00ada7a0b64 Mon Sep 17 00:00:00 2001
From: Nikos Mavrogiannopoulos <n...@gnutls.org>
Date: Thu, 27 Sep 2012 08:42:55 +0200
Subject: [PATCH 2/2] If tablesize is zero then return the full h.

---
 lib/hash-pjw.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/lib/hash-pjw.c b/lib/hash-pjw.c
index 0966598..927271d 100644
--- a/lib/hash-pjw.c
+++ b/lib/hash-pjw.c
@@ -36,5 +36,8 @@ hash_pjw (const void *x, size_t tablesize)
   for (s = x; *s; s++)
     h = *s + ((h << 9) | (h >> (SIZE_BITS - 9)));
 
-  return h % tablesize;
+  if (tablesize)
+    return h % tablesize;
+  else
+    return h;
 }
-- 
1.7.10.4

>From d48f54a232b388c184124cc0b2e7998662cb4091 Mon Sep 17 00:00:00 2001
From: Nikos Mavrogiannopoulos <n...@gnutls.org>
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 <http://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#include "hash-pjw.h"
+
+#include <limits.h>
+
+#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<x_size;i++)
+    h = s[i] + ((h << 9) | (h >> (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 <http://www.gnu.org/licenses/>.  */
+
+#include <stddef.h>
+
+/* 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

Reply via email to