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