summaryrefslogtreecommitdiff
path: root/components/rtgui/utils/perfect_hash/example-PyModule/stationsmodule.c
blob: bf14e1bbeef2b0b93dec9acfe6cd8ec2877073f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

#include <Python.h>

#include "stations-code.h"

static struct {
  char *callsign;
  char *locator;
} station_list[] = {
#include "stations.dat.h"
};

static int hash_f (const char *s, const int *T)
{
  register int i, sum = 0;

  for (i = 0; s[i] != '\0'; i++) {
    sum += T[i] * s[i];
    sum %= NG;
  }
  return sum;
}

static int perf_hash (const char *k)
{
  if (strlen (k) > NS)
    return 0;
  
  return (G[ hash_f(k, T1) ] + G[ hash_f(k, T2)] ) % NG;
}

static int getlocator (char *locator, const char *callsign)
{
  int hashval = perf_hash (callsign);
  
  if (hashval < NK && strcmp(callsign, station_list[hashval].callsign) == 0) {
    strcpy (locator, station_list[hashval].locator);
    return 1;
  }
  return 0;
}

static PyObject *
stations_locator(PyObject *self, PyObject *args)
{
  const char *callsign;
  char locator[6];
  
  if (!PyArg_ParseTuple(args, "s", &callsign))
    return NULL;
  
  return Py_BuildValue("s", (getlocator (locator, callsign) == 1) ?
		       locator : NULL);
}

static PyMethodDef StationsMethods[] = {

    {"locator",  stations_locator, METH_VARARGS,
     "Get locator from callsign."},

    {NULL, NULL, 0, NULL}        /* Sentinel */
};


PyMODINIT_FUNC
initstations(void)
{
  (void) Py_InitModule("stations", StationsMethods);
}