diff options
author | Marc-André Lemburg <mal@egenix.com> | 2000-06-28 16:48:05 (GMT) |
---|---|---|
committer | Marc-André Lemburg <mal@egenix.com> | 2000-06-28 16:48:05 (GMT) |
commit | 93c409a590b00ea5b28112fe2b7ca3bc862850b8 (patch) | |
tree | 430b34151286d5e99a1bc1085e21f283e3a33983 /Tools | |
parent | 0f774e39872f475fab9030e183230814f45fe2ad (diff) | |
download | cpython-93c409a590b00ea5b28112fe2b7ca3bc862850b8.zip cpython-93c409a590b00ea5b28112fe2b7ca3bc862850b8.tar.gz cpython-93c409a590b00ea5b28112fe2b7ca3bc862850b8.tar.bz2 |
Marc-Andre Lemburg <mal@lemburg.com>:
Utility extension module needed by perfect_hash.py
By Bill Tutt.
Diffstat (limited to 'Tools')
-rw-r--r-- | Tools/perfecthash/perfhash.c | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/Tools/perfecthash/perfhash.c b/Tools/perfecthash/perfhash.c new file mode 100644 index 0000000..e7ba0c7 --- /dev/null +++ b/Tools/perfecthash/perfhash.c @@ -0,0 +1,72 @@ +#include <Python.h> + +static PyObject * hashFunction(PyObject *self, PyObject *args, PyObject *kw) +{ + PyStringObject *a; + register int len; + register unsigned char *p; + register long x; + long lSeed; + unsigned long cchSeed; + + if (!PyArg_ParseTuple(args, "iiO:hash", &lSeed, &cchSeed, &a)) + return NULL; + if (!PyString_Check(a)) + { + PyErr_SetString(PyExc_TypeError, "arg 3 needs to be a string"); + return NULL; + } + + len = a->ob_size; + p = (unsigned char *) a->ob_sval; + x = lSeed; + while (--len >= 0) + x = (1000003*x) ^ *p++; + x ^= a->ob_size + cchSeed; + if (x == -1) + x = -2; + return PyInt_FromLong(x); +} + +static PyObject * calcSeed(PyObject *self, PyObject *args, PyObject *kw) +{ + PyStringObject *a; + register int len; + register unsigned char *p; + register long x; + + if (!PyString_Check(args)) + { + PyErr_SetString(PyExc_TypeError, "arg 1 expected a string, but didn't get it."); + return NULL; + } + + a = (PyStringObject *)args; + + len = a->ob_size; + p = (unsigned char *) a->ob_sval; + x = *p << 7; + while (--len >= 0) + x = (1000003*x) ^ *p++; + return PyInt_FromLong(x); +} + + +static struct PyMethodDef hashMethods[] = { + { "calcSeed", calcSeed, 0, NULL }, + { "hash", hashFunction, 0, NULL }, + { NULL, NULL, 0, NULL } /* sentinel */ +}; + +#ifdef _MSC_VER +_declspec(dllexport) +#endif +void initperfhash() +{ + PyObject *m; + + m = Py_InitModule4("perfhash", hashMethods, + NULL, NULL, PYTHON_API_VERSION); + if ( m == NULL ) + Py_FatalError("can't initialize module hashModule"); +} |