chiark / gitweb /
debian: Fix package sections.
[mLib-python] / mapping.pyx
... / ...
CommitLineData
1# -*-pyrex-*-
2#
3# $Id$
4#
5# Common mapping stuff
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.
18#
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.
23#
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
28cdef class Mapping
29
30cdef class _MapIterator:
31 cdef void *_next(me):
32 return NULL
33
34cdef class Mapping:
35
36 ## Subclasses must implement these
37 cdef int _init(me) except -1:
38 raise TypeError, 'abstract class'
39 cdef void *_find(me, object key, unsigned *f) except NULL:
40 raise SystemError, 'unimplemented _find'
41 cdef object _key(me, void *e):
42 return None
43 cdef object _value(me, void *e):
44 return None
45 cdef void _setval(me, void *e, object val):
46 pass
47 cdef void _del(me, void *e):
48 pass
49 cdef _MapIterator _iter(me):
50 raise SystemError, 'unimplemented _iter'
51
52 ## Initialization
53 def __new__(me, *hunoz, **hukairz):
54 me._init()
55 def __init__(me, stuff = None, **kw):
56 me.update(stuff, kw)
57
58 ## Bulk update
59 def update(me, stuff = None, **kw):
60 cdef unsigned f
61 if stuff is None:
62 pass
63 elif hasattr(stuff, 'itemiter'):
64 for k, v in stuff.itemiter:
65 me._setval(me._find(k, &f), v)
66 elif hasattr(stuff, 'keys'):
67 for k in stuff.keys():
68 me._setval(me._find( k, &f), stuff[k])
69 else:
70 for k, v in stuff:
71 me._setval(me._find(k, &f), v)
72 for k, v in kw.iteritems():
73 me._setval(me._find(k, &f), v)
74 return me
75
76 ## Item access
77 def __getitem__(me, key):
78 cdef void *e
79 e = me._find(key, NULL)
80 if not e:
81 raise KeyError, key
82 return me._value(e)
83 def __setitem__(me, key, value):
84 cdef unsigned f
85 me._setval(me._find(key, &f), value)
86 def __delitem__(me, key):
87 cdef void *e
88 cdef unsigned f
89 e = me._find(key, &f)
90 if not e:
91 raise KeyError, key
92 me._del(e)
93 def get(me, key, default = None):
94 cdef void *e
95 e = me._find(key, NULL)
96 if not e:
97 return default
98 return me._value(e)
99 def setdefault(me, key, default = None):
100 cdef void *e
101 cdef unsigned f
102 e = me._find(key, &f)
103 if f:
104 return me._value(e)
105 else:
106 me._setval(e, default)
107 return default
108 def pop(me, key, default = None):
109 cdef void *e
110 e = me._find(key, NULL)
111 if not e:
112 return default
113 rc = me._value(e)
114 me._del(e)
115 return rc
116 def popitem(me):
117 cdef _MapIterator i
118 cdef void *e
119 i = me._iter()
120 e = i._next()
121 if not e:
122 raise ValueError, 'popitem(): table is empty'
123 return me._key(e), me._value(e)
124
125 ## Lists of items
126 cdef object _list(me, object (*func)(Mapping m, void *e)):
127 cdef _MapIterator i
128 cdef void *e
129 i = me._iter()
130 l = []
131 while 1:
132 e = i._next()
133 if not e:
134 break
135 l.append(func(me, e))
136 return l
137
138 def keys(me):
139 return me._list(_map_key)
140 def values(me):
141 return me._list(_map_value)
142 def items(me):
143 return me._list(_map_item)
144
145 def clear(me):
146 cdef _MapIterator i
147 cdef void *e
148 i = me._iter()
149 while 1:
150 e = i._next()
151 if not e:
152 break
153 me._del(e)
154 return me
155
156 ## Iteration
157 def __iter__(me):
158 return MapKeyIter(me)
159 def iterkeys(me):
160 return MapKeyIter(me)
161 def itervalues(me):
162 return MapValueIter(me)
163 def iteritems(me):
164 return MapItemIter(me)
165
166cdef class MapIterBase:
167 cdef Mapping m
168 cdef object (*func)(Mapping m, void *e)
169 cdef _MapIterator i
170 cdef int _init(me) except -1:
171 raise TypeError, 'abstract class'
172 def __new__(me):
173 me.i = m._iter()
174 me._init()
175 def __next__(me):
176 cdef void *e
177 e = me.i._next()
178 if not e:
179 raise StopIteration
180 return me.func(me.m, e)
181cdef class MapKeyIter (MapIterBase):
182 cdef int _init(me) except -1:
183 me.func = _map_key
184 return 0
185cdef class MapValueIter (MapIterBase):
186 cdef int _init(me) except -1:
187 me.func = _map_value
188 return 0
189cdef class MapItemIter (MapIterBase):
190 cdef int _init(me) except -1:
191 me.func = _map_item
192 return 0
193
194cdef object _map_key(Mapping m, void *e):
195 return m._key(e)
196cdef object _map_value(Mapping m, void *e):
197 return m._value(e)
198cdef object _map_item(Mapping m, void *e):
199 return m._key(e), m._value(e)
200
201#----- That's all, folks ----------------------------------------------------