Commit | Line | Data |
---|---|---|
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 | 26 | cdef struct _sym_entry: |
20bce5e9 | 27 | sym_base _b |
28 | PyObject *v | |
29 | ||
efb0bf0e | 30 | cdef class SymTable (_Mapping): |
addc0c37 MW |
31 | """ |
32 | SymTable([DICT], **KW) | |
33 | ||
34 | A mapping keyed by strings. | |
35 | """ | |
20bce5e9 | 36 | cdef sym_table _t |
efb0bf0e | 37 | def __cinit__(me): |
20bce5e9 | 38 | sym_create(&me._t) |
efb0bf0e | 39 | cdef int _init(me) except -1: |
579d0169 | 40 | return 0 |
41 | cdef void *_find(me, object key, unsigned *f) except NULL: | |
efb0bf0e | 42 | cdef const char *p |
78911cdb | 43 | cdef Py_ssize_t n |
efb0bf0e MW |
44 | cdef _sym_entry *e = NULL |
45 | TEXT_PTRLEN(key, &p, &n) | |
46 | if not f: | |
47 | e = <_sym_entry *>sym_find(&me._t, <char *>p, n, 0, NULL) | |
48 | if not e: | |
49 | raise KeyError(key) | |
50 | else: | |
579d0169 | 51 | e = <_sym_entry *>sym_find(&me._t, <char *>p, n, PSIZEOF(e), f) |
52 | if not f[0]: | |
53 | e.v = NULL | |
579d0169 | 54 | return <void *>e |
55 | cdef object _key(me, void *e): | |
efb0bf0e | 56 | return TEXT_FROMSTRLEN(SYM_NAME(e), SYM_LEN(e)) |
579d0169 | 57 | cdef object _value(me, void *e): |
efb0bf0e | 58 | cdef _sym_entry *ee = <_sym_entry *>e |
579d0169 | 59 | Py_INCREF(ee.v) |
60 | return <object>ee.v | |
d8d81d1b | 61 | cdef void _setval(me, void *e, object val): |
efb0bf0e | 62 | cdef _sym_entry *ee = <_sym_entry *>e |
579d0169 | 63 | if ee.v: |
64 | Py_DECREF(ee.v) | |
efb0bf0e | 65 | ee.v = <PyObject *>val |
579d0169 | 66 | Py_INCREF(ee.v) |
67 | cdef void _del(me, void *e): | |
efb0bf0e | 68 | cdef _sym_entry *ee = <_sym_entry *>e |
579d0169 | 69 | if ee.v: |
70 | Py_DECREF(ee.v) | |
71 | sym_remove(&me._t, <void *>ee) | |
72 | cdef _MapIterator _iter(me): | |
73 | return _SymIter(me) | |
20bce5e9 | 74 | |
579d0169 | 75 | cdef class _SymIter (_MapIterator): |
76 | cdef SymTable t | |
77 | cdef sym_iter i | |
6deaef47 | 78 | def __cinit__(me, SymTable t not None): |
579d0169 | 79 | me.t = t |
80 | sym_mkiter(&me.i, &me.t._t) | |
81 | cdef void *_next(me): | |
82 | return sym_next(&me.i) | |
20bce5e9 | 83 | |
5b1830f3 | 84 | ###----- That's all, folks -------------------------------------------------- |