sync with rtgui-0.6

This commit is contained in:
Grissiom
2013-02-01 10:27:10 +08:00
parent 4f02e67bef
commit f06c70feed
109 changed files with 6724 additions and 1164 deletions
@@ -0,0 +1,26 @@
CC = gcc -Wall
stations.so: stationsmodule.c stations.dat.h stations-code.h
$(CC) -shared -fPIC -I/usr/local/include/python2.5 \
-o stations.so stationsmodule.c
stations.dat.h: stations.dat
sed <$< >$@ -e 's:\([^,]*\),\([^,]*\): { "\1", "\2" },:'
stations-code.h: stations.dat stations-tmpl.h
../perfect_hash.py --trails 2 $^
clean:
rm stations-code.h stations.dat.h stations.so
test:
python -c "import stations; print stations.locator('DL5BAC')"
@@ -0,0 +1,12 @@
#define NK $NK /* number of keys */
#define NG $NG /* number of vertices */
#define NS $NS /* elements in T */
int G[] = { $G };
int T1[] = { $S1 };
int T2[] = { $S2 };
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,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);
}
@@ -0,0 +1,28 @@
#!/usr/bin/env python
import sys
from timeit import Timer
from stations import locator
call = sys.argv[1]
print repr(call)
D = {}
for line in file('stations.dat'):
c, l = [x.strip() for x in line.split(',')]
D[c] = l
def test1(c):
return D[c]
print repr(test1(call))
t = Timer("test1(%r)" % call, "from __main__ import test1")
print t.timeit()
# -----
def test2(c):
return locator(c)
print repr(test2(call))
t = Timer("test2(%r)" % call, "from __main__ import test2")
print t.timeit()