chiark / gitweb /
codec.pyx.in: Cast arguments to `xfree'.
[mLib-python] / sym.pyx
CommitLineData
5b1830f3
MW
1### -*-pyrex-*-
2###
3### Symbol table, using universal hashing
4###
5### (c) 2005 Straylight/Edgeware
6###
20bce5e9 7
5b1830f3
MW
8###----- Licensing notice ---------------------------------------------------
9###
10### This file is part of the Python interface to mLib.
11###
12### mLib/Python is free software; you can redistribute it and/or modify
13### it under the terms of the GNU General Public License as published by
14### the Free Software Foundation; either version 2 of the License, or
15### (at your option) any later version.
16###
17### mLib/Python is distributed in the hope that it will be useful,
18### but WITHOUT ANY WARRANTY; without even the implied warranty of
19### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20### GNU General Public License for more details.
21###
22### You should have received a copy of the GNU General Public License
23### along with mLib/Python; if not, write to the Free Software Foundation,
24### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20bce5e9 25
579d0169 26cdef struct _sym_entry:
20bce5e9 27 sym_base _b
28 PyObject *v
29
579d0169 30cdef class SymTable (Mapping):
20bce5e9 31 cdef sym_table _t
579d0169 32 cdef int _init(me) except -1:
20bce5e9 33 sym_create(&me._t)
579d0169 34 return 0
35 cdef void *_find(me, object key, unsigned *f) except NULL:
36 cdef void *p
78911cdb 37 cdef Py_ssize_t n
579d0169 38 cdef _sym_entry *e
704500e1 39 PyObject_AsReadBuffer(key, <cvp *>&p, &n)
20bce5e9 40 if f:
579d0169 41 f[0] = 0
42 e = <_sym_entry *>sym_find(&me._t, <char *>p, n, PSIZEOF(e), f)
43 if not f[0]:
44 e.v = NULL
20bce5e9 45 else:
579d0169 46 e = <_sym_entry *>sym_find(&me._t, <char *>p, n, 0, NULL)
47 return <void *>e
48 cdef object _key(me, void *e):
49 return PyString_FromStringAndSize(SYM_NAME(e), SYM_LEN(e))
50 cdef object _value(me, void *e):
51 cdef _sym_entry *ee
52 ee = <_sym_entry *>e
53 Py_INCREF(ee.v)
54 return <object>ee.v
d8d81d1b 55 cdef void _setval(me, void *e, object val):
579d0169 56 cdef _sym_entry *ee
57 ee = <_sym_entry *>e
58 if ee.v:
59 Py_DECREF(ee.v)
60 ee.v = <PyObject *>v
61 Py_INCREF(ee.v)
62 cdef void _del(me, void *e):
63 cdef _sym_entry *ee
64 ee = <_sym_entry *>e
65 if ee.v:
66 Py_DECREF(ee.v)
67 sym_remove(&me._t, <void *>ee)
68 cdef _MapIterator _iter(me):
69 return _SymIter(me)
20bce5e9 70
579d0169 71cdef class _SymIter (_MapIterator):
72 cdef SymTable t
73 cdef sym_iter i
6deaef47 74 def __cinit__(me, SymTable t not None):
579d0169 75 me.t = t
76 sym_mkiter(&me.i, &me.t._t)
77 cdef void *_next(me):
78 return sym_next(&me.i)
20bce5e9 79
5b1830f3 80###----- That's all, folks --------------------------------------------------