-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathflattery.c
296 lines (225 loc) · 6.37 KB
/
flattery.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "Python.h"
#include <ctype.h> // for isdigit()
#include <stdlib.h> // for atol()
static PyObject *
unflatten(PyObject *ignore, PyObject *args)
{
PyObject *src = NULL;
PyObject *dst = NULL;
PyObject *nonelist = NULL;
PyObject *slot = NULL;
PyObject *slotvalue = NULL;
PyObject *part = NULL;
if (!PyArg_ParseTuple(args, "O!:unflatten", &PyDict_Type, &src))
return NULL;
if (!(dst = PyDict_New()))
goto error;
/* Create a [None] list. Used for extending lists to higher indices. */
if (!(nonelist = PyList_New(0)))
goto error;
if (PyList_Append(nonelist, Py_None) < 0)
goto error;
/* Iterate through key value pairs in the src dict,
building the nested data structure in dst as we go. */
PyObject *k, *v;
Py_ssize_t pos = 0;
while (PyDict_Next(src, &pos, &k, &v))
{
const char *key = PyString_AsString(k);
const char *p;
p = key;
slot = dst;
Py_INCREF(slot);
do
{
/* Extract current part of the key path. */
const char *start = p;
while (*p && *p != '.') p++;
part = PyString_FromStringAndSize(start, p-start);
/* Advance to next part of key path, unless at the end. */
if (*p == '.')
p++;
/* What value should we insert under this slot?
- if this is the last path part, insert the value from src.
- if the next path part is numeric, insert an empty list.
- otherwise, insert an empty hash.
*/
if (!*p) {
slotvalue = v;
Py_INCREF(slotvalue);
}
else if (isdigit(*p))
slotvalue = PyList_New(0);
else
slotvalue = PyDict_New();
if (!slotvalue)
goto error;
/* Assign to the current slot. */
if (isdigit(*start))
{
/* If the current path part is numeric, index into a list. */
if (!PyList_Check(slot))
goto error;
// FIXME thorough error checking here
Py_ssize_t len = PyList_Size(slot);
Py_ssize_t index = atol(PyString_AsString(part));
/* Extend the list with [None,None,...] if necessary. */
if (index >= len)
{
PyObject *tail = PySequence_Repeat(nonelist, index-len+1);
PyObject *extended = PySequence_InPlaceConcat(slot, tail);
Py_DECREF(tail);
Py_DECREF(extended);
}
/* Don't clobber an existing entry.
Caveat: PyList_SetItem() steals a reference to slotvalue. */
PyObject *extant = NULL;
if ((extant = PyList_GetItem(slot, index)) == Py_None) {
PyList_SetItem(slot, index, slotvalue);
Py_INCREF(slotvalue);
}
else {
Py_DECREF(slotvalue);
slotvalue = extant;
Py_INCREF(slotvalue);
}
}
else
{
/* If the current path part is non-numeric, index into a dict. */
if (!PyDict_Check(slot))
goto error;
/* Don't clobber an existing entry. */
PyObject *extant = NULL;
if (!(extant = PyDict_GetItem(slot, part)))
PyDict_SetItem(slot, part, slotvalue);
else {
Py_DECREF(slotvalue);
slotvalue = extant;
Py_INCREF(slotvalue);
}
}
/* Descend further into the dst data structure. */
Py_DECREF(slot);
slot = slotvalue;
slotvalue = NULL;
Py_DECREF(part);
part = NULL;
}
while (*p);
Py_DECREF(slot);
slot = NULL;
}
Py_DECREF(nonelist);
return dst;
error:
Py_XDECREF(dst);
Py_XDECREF(nonelist);
Py_XDECREF(slot);
Py_XDECREF(slotvalue);
Py_XDECREF(part);
return NULL;
}
static PyObject *
flatten_internal(PyObject *src)
{
PyObject *flat = NULL;
PyObject *dst = NULL;
if (PyList_Check(src))
{
if (!(flat = PyDict_New()))
goto error;
/* Iterate through elements in the list src, recursively flattening.
Skip any entries which are None -- use a sparse encoding. */
Py_ssize_t i;
Py_ssize_t len = PyList_Size(src);
for (i=0; i<len; i++)
{
PyObject *elem = PyList_GetItem(src,i);
if (elem == Py_None && i<len-1) continue;
Py_INCREF(elem);
PyObject *o = flatten_internal(elem);
Py_DECREF(elem);
PyObject *k = PyString_FromFormat("%zd",i);
PyDict_SetItem(flat, k, o);
Py_DECREF(k);
Py_DECREF(o);
}
}
else if (PyDict_Check(src))
{
if (!(flat = PyDict_New()))
goto error;
/* Iterate through pairs in the dict src, recursively flattening. */
PyObject *k, *v;
Py_ssize_t pos = 0;
while (PyDict_Next(src, &pos, &k, &v))
{
Py_INCREF(v);
PyObject *o = flatten_internal(v);
Py_DECREF(v);
PyDict_SetItem(flat, k, o);
Py_DECREF(o);
}
}
else
{
/* The Python object is a scalar or something we don't know how
to flatten, return it as-is. */
Py_INCREF(src);
return src;
}
/* Roll up recursively flattened dictionaries. */
if (!(dst = PyDict_New()))
goto error;
PyObject *k1, *v1;
Py_ssize_t pos1 = 0;
while (PyDict_Next(flat, &pos1, &k1, &v1))
{
if (PyDict_Check(v1))
{
PyObject *k2, *v2;
Py_ssize_t pos2 = 0;
while (PyDict_Next(v1, &pos2, &k2, &v2))
{
const char *k1c = PyString_AsString(k1);
const char *k2c = PyString_AsString(k2);
PyObject *k = PyString_FromFormat("%s.%s",k1c,k2c);
PyDict_SetItem(dst, k, v2);
Py_DECREF(k);
}
}
else
PyDict_SetItem(dst, k1, v1);
}
Py_DECREF(flat);
return dst;
error:
Py_XDECREF(dst);
Py_XDECREF(flat);
return NULL;
}
static PyObject *
flatten(PyObject *ignore, PyObject *args)
{
PyObject *src = NULL;
if (!PyArg_ParseTuple(args, "O!:flatten", &PyDict_Type, &src))
return NULL;
return flatten_internal(src);
}
/* List of free functions defined in the module */
static PyMethodDef flattery_methods[] = {
{"unflatten", unflatten, METH_VARARGS, "unflatten(dict) -> dict"},
{"flatten", flatten, METH_VARARGS, "flatten(dict) -> dict"},
{NULL, NULL} /* sentinel */
};
PyDoc_STRVAR(module_doc, "Flattery: fast flattening and unflattening of nested data structures.");
/* Initialization function for the module (*must* be called initcext) */
PyMODINIT_FUNC
initcext(void)
{
/* Create the module and add the functions */
PyObject* mod = Py_InitModule3("flattery.cext", flattery_methods, module_doc);
if (mod == NULL)
return;
}