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
|
$NetBSD: patch-ab,v 1.1.1.1 2002/02/05 21:05:58 drochner Exp $
--- src/tableobject.c.orig Mon May 28 23:00:41 2001
+++ src/tableobject.c Sun Nov 4 18:28:55 2001
@@ -185,6 +185,105 @@
};
/**
+ ** table_get
+ **
+ *
+ * Implements dictionary like get(k,[x]) method.
+ */
+
+static PyObject * table_get(tableobject *self, PyObject *args)
+{
+
+ PyObject *key,*v;
+ PyObject *failob = Py_None;
+ array_header *ah;
+ table_entry *elts;
+ int i, j;
+
+ if(!PyArg_ParseTuple(args, "O|O:get", &key, &failob))
+ return NULL;
+ v=tablegetitem(self,key);
+ if(v = NULL) {
+ v=failob;
+ Py_INCREF(failob);
+ }
+ return v;
+}
+
+/**
+ ** table_keys
+ **
+ *
+ * Implements dictionary like items() method.
+ */
+
+static PyObject * table_items(tableobject *self)
+{
+
+ PyObject *v;
+ array_header *ah;
+ table_entry *elts;
+ int i, j;
+
+ ah = ap_table_elts(self->table);
+ elts = (table_entry *) ah->elts;
+
+ v = PyList_New(ah->nelts);
+
+ for (i = 0, j = 0; i < ah->nelts; i++)
+ {
+ if (elts[i].key)
+ {
+ PyObject *vp = PyTuple_New(2);
+ if(vp == NULL) {
+ Py_DECREF(v);
+ return NULL;
+ }
+ PyTuple_SET_ITEM(vp,0,PyString_FromString(elts[i].key));
+ PyTuple_SET_ITEM(vp,1,PyString_FromString(ap_table_get(self->table,
+ elts[i].key)));
+ PyList_SetItem(v, j, vp);
+ j++;
+ }
+ }
+ return v;
+}
+/**
+ ** table_keys
+ **
+ *
+ * Implements dictionary like values() method.
+ * This mostly for completeness sake, I can't see use for it
+ */
+
+static PyObject * table_values(tableobject *self)
+{
+
+ PyObject *v;
+ array_header *ah;
+ table_entry *elts;
+ int i, j;
+
+ ah = ap_table_elts(self->table);
+ elts = (table_entry *) ah->elts;
+
+ v = PyList_New(ah->nelts);
+
+ for (i = 0, j = 0; i < ah->nelts; i++)
+ {
+ if (elts[i].key)
+ {
+ PyObject *val = PyString_FromString(ap_table_get(self->table,
+ elts[i].key));
+ PyList_SetItem(v, j, val);
+ j++;
+ }
+ }
+ return v;
+}
+
+
+/**
** table_keys
**
*
@@ -261,9 +360,13 @@
/* table method definitions */
static PyMethodDef tablemethods[] = {
- {"keys", (PyCFunction)table_keys, METH_VARARGS},
{"has_key", (PyCFunction)table_has_key, METH_VARARGS},
{"add", (PyCFunction)mp_table_add, METH_VARARGS},
+ {"items", (PyCFunction)table_items, METH_VARARGS},
+ {"keys", (PyCFunction)table_keys, METH_VARARGS},
+ {"get", (PyCFunction)table_get, METH_VARARGS},
+ {"values", (PyCFunction)table_values, METH_VARARGS},
+ /* Still needs: copy, update, clear, setdefault, popitem */
{NULL, NULL} /* sentinel */
};
|