blob: f6f9c96cab7b9e045ea857eb322747e1b3518c7c [file] [log] [blame]
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001
Guido van Rossum2bc13791999-03-24 19:06:42 +00002/* Dictionary object implementation using a hash table */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Guido van Rossum4b1302b1993-03-27 18:11:32 +00005
6
7/*
Guido van Rossuma9e7a811997-05-13 21:02:11 +00008 * MINSIZE is the minimum size of a dictionary.
Guido van Rossum16e93a81997-01-28 00:00:11 +00009 */
10
11#define MINSIZE 4
12
Fred Drake1bff34a2000-08-31 19:31:38 +000013/* define this out if you don't want conversion statistics on exit */
14#undef SHOW_CONVERSION_COUNTS
15
Guido van Rossum16e93a81997-01-28 00:00:11 +000016/*
17Table of irreducible polynomials to efficiently cycle through
Tim Petersea8f2bf2000-12-13 01:02:46 +000018GF(2^n)-{0}, 2<=n<=30. A table size is always a power of 2.
Guido van Rossum4b1302b1993-03-27 18:11:32 +000019*/
Guido van Rossum16e93a81997-01-28 00:00:11 +000020static long polys[] = {
Guido van Rossum9e5656c1997-01-29 04:45:16 +000021 4 + 3,
22 8 + 3,
23 16 + 3,
24 32 + 5,
25 64 + 3,
26 128 + 3,
27 256 + 29,
28 512 + 17,
29 1024 + 9,
30 2048 + 5,
31 4096 + 83,
32 8192 + 27,
33 16384 + 43,
34 32768 + 3,
35 65536 + 45,
36 131072 + 9,
37 262144 + 39,
38 524288 + 39,
39 1048576 + 9,
40 2097152 + 5,
41 4194304 + 3,
42 8388608 + 33,
43 16777216 + 27,
44 33554432 + 9,
45 67108864 + 71,
46 134217728 + 39,
47 268435456 + 9,
48 536870912 + 5,
49 1073741824 + 83,
Guido van Rossum4b1302b1993-03-27 18:11:32 +000050};
51
52/* Object used as dummy key to fill deleted entries */
Guido van Rossuma9e7a811997-05-13 21:02:11 +000053static PyObject *dummy; /* Initialized by first call to newdictobject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +000054
55/*
Tim Petersea8f2bf2000-12-13 01:02:46 +000056There are three kinds of slots in the table:
57
581. Unused. me_key == me_value == NULL
59 Does not hold an active (key, value) pair now and never did. Unused can
60 transition to Active upon key insertion. This is the only case in which
61 me_key is NULL, and is each slot's initial state.
62
632. Active. me_key != NULL and me_key != dummy and me_value != NULL
64 Holds an active (key, value) pair. Active can transition to Dummy upon
65 key deletion. This is the only case in which me_value != NULL.
66
Tim Petersf1c7c882000-12-13 19:58:25 +0000673. Dummy. me_key == dummy and me_value == NULL
Tim Petersea8f2bf2000-12-13 01:02:46 +000068 Previously held an active (key, value) pair, but that was deleted and an
69 active pair has not yet overwritten the slot. Dummy can transition to
70 Active upon key insertion. Dummy slots cannot be made Unused again
71 (cannot have me_key set to NULL), else the probe sequence in case of
72 collision would have no way to know they were once active.
Tim Petersf1c7c882000-12-13 19:58:25 +000073
74Note: .popitem() abuses the me_hash field of an Unused or Dummy slot to
75hold a search finger. The me_hash field of Unused or Dummy slots has no
76meaning otherwise.
Guido van Rossum4b1302b1993-03-27 18:11:32 +000077*/
78typedef struct {
Tim Petersea8f2bf2000-12-13 01:02:46 +000079 long me_hash; /* cached hash code of me_key */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000080 PyObject *me_key;
81 PyObject *me_value;
Guido van Rossum36488841997-04-11 19:14:07 +000082#ifdef USE_CACHE_ALIGNED
83 long aligner;
84#endif
Guido van Rossuma9e7a811997-05-13 21:02:11 +000085} dictentry;
Guido van Rossum4b1302b1993-03-27 18:11:32 +000086
87/*
Tim Petersf1c7c882000-12-13 19:58:25 +000088To ensure the lookup algorithm terminates, there must be at least one Unused
Tim Petersea8f2bf2000-12-13 01:02:46 +000089slot (NULL key) in the table.
90The value ma_fill is the number of non-NULL keys (sum of Active and Dummy);
91ma_used is the number of non-NULL, non-dummy keys (== the number of non-NULL
92values == the number of Active items).
93To avoid slowing down lookups on a near-full table, we resize the table when
Tim Peters67830702001-03-21 19:23:56 +000094it's two-thirds full.
Guido van Rossum4b1302b1993-03-27 18:11:32 +000095*/
Fred Drake1bff34a2000-08-31 19:31:38 +000096typedef struct dictobject dictobject;
97struct dictobject {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098 PyObject_HEAD
Tim Petersea8f2bf2000-12-13 01:02:46 +000099 int ma_fill; /* # Active + # Dummy */
100 int ma_used; /* # Active */
101 int ma_size; /* total # slots in ma_table */
102 int ma_poly; /* appopriate entry from polys vector */
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000103 dictentry *ma_table;
Fred Drake1bff34a2000-08-31 19:31:38 +0000104 dictentry *(*ma_lookup)(dictobject *mp, PyObject *key, long hash);
105};
106
107/* forward declarations */
108static dictentry *
109lookdict_string(dictobject *mp, PyObject *key, long hash);
110
111#ifdef SHOW_CONVERSION_COUNTS
112static long created = 0L;
113static long converted = 0L;
114
115static void
116show_counts(void)
117{
118 fprintf(stderr, "created %ld string dicts\n", created);
119 fprintf(stderr, "converted %ld to normal dicts\n", converted);
120 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
121}
122#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000123
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000124PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000125PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000126{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000127 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000128 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000129 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000130 if (dummy == NULL)
131 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000132#ifdef SHOW_CONVERSION_COUNTS
133 Py_AtExit(show_counts);
134#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000135 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000136 mp = PyObject_NEW(dictobject, &PyDict_Type);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000137 if (mp == NULL)
138 return NULL;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000139 mp->ma_size = 0;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000140 mp->ma_poly = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000141 mp->ma_table = NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000142 mp->ma_fill = 0;
143 mp->ma_used = 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000144 mp->ma_lookup = lookdict_string;
145#ifdef SHOW_CONVERSION_COUNTS
146 ++created;
147#endif
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000148 PyObject_GC_Init(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000149 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000150}
151
152/*
153The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000154This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000155Open addressing is preferred over chaining since the link overhead for
156chaining would be substantial (100% with typical malloc overhead).
Guido van Rossum16e93a81997-01-28 00:00:11 +0000157However, instead of going through the table at constant steps, we cycle
Tim Petersea8f2bf2000-12-13 01:02:46 +0000158through the values of GF(2^n). This avoids modulo computations, being
Guido van Rossum16e93a81997-01-28 00:00:11 +0000159much cheaper on RISC machines, without leading to clustering.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000160
Guido van Rossum2bc13791999-03-24 19:06:42 +0000161The initial probe index is computed as hash mod the table size.
Tim Petersea8f2bf2000-12-13 01:02:46 +0000162Subsequent probe indices use the values of x^i in GF(2^n)-{0} as an offset,
163where x is a root. The initial offset is derived from hash, too.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000164
165All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000166
167(This version is due to Reimer Behrends, some ideas are also due to
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000168Jyrki Alakuijala and Vladimir Marangozov.)
Fred Drake1bff34a2000-08-31 19:31:38 +0000169
170This function must never return NULL; failures are indicated by returning
171a dictentry* for which the me_value field is NULL. Exceptions are never
172reported by this function, and outstanding exceptions are maintained.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000173*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000174static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000175lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000176{
Guido van Rossum16e93a81997-01-28 00:00:11 +0000177 register int i;
178 register unsigned incr;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000179 register dictentry *freeslot;
Guido van Rossum2095d241997-04-09 19:41:24 +0000180 register unsigned int mask = mp->ma_size-1;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000181 dictentry *ep0 = mp->ma_table;
182 register dictentry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000183 register int restore_error = 0;
184 register int checked_error = 0;
185 register int cmp;
186 PyObject *err_type, *err_value, *err_tb;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000187 /* We must come up with (i, incr) such that 0 <= i < ma_size
Tim Petersea8f2bf2000-12-13 01:02:46 +0000188 and 0 < incr < ma_size and both are a function of hash.
189 i is the initial table index and incr the initial probe offset. */
Tim Peters2f228e72001-05-13 00:19:31 +0000190 i = hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000191 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000192 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000193 return ep;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000194 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000195 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000196 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000197 if (ep->me_hash == hash) {
198 /* error can't have been checked yet */
199 checked_error = 1;
200 if (PyErr_Occurred()) {
201 restore_error = 1;
202 PyErr_Fetch(&err_type, &err_value, &err_tb);
203 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000204 cmp = PyObject_RichCompareBool(ep->me_key, key, Py_EQ);
205 if (cmp > 0) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000206 if (restore_error)
207 PyErr_Restore(err_type, err_value,
208 err_tb);
209 return ep;
210 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000211 else if (cmp < 0)
212 PyErr_Clear();
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000213 }
214 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000215 }
Guido van Rossum2bc13791999-03-24 19:06:42 +0000216 /* Derive incr from hash, just to make it more arbitrary. Note that
Guido van Rossum16e93a81997-01-28 00:00:11 +0000217 incr must not be 0, or we will get into an infinite loop.*/
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000218 incr = (hash ^ ((unsigned long)hash >> 3)) & mask;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000219 if (!incr)
220 incr = mask;
Tim Peters342c65e2001-05-13 06:43:53 +0000221 /* In the loop, me_key == dummy is by far (factor of 100s) the
222 least likely outcome, so test for that last. */
Guido van Rossum16e93a81997-01-28 00:00:11 +0000223 for (;;) {
Guido van Rossumefb46091997-01-29 15:53:56 +0000224 ep = &ep0[(i+incr)&mask];
Guido van Rossum16e93a81997-01-28 00:00:11 +0000225 if (ep->me_key == NULL) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000226 if (restore_error)
227 PyErr_Restore(err_type, err_value, err_tb);
Tim Peters342c65e2001-05-13 06:43:53 +0000228 return freeslot == NULL ? ep : freeslot;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000229 }
Tim Peters342c65e2001-05-13 06:43:53 +0000230 if (ep->me_key == key) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000231 if (restore_error)
232 PyErr_Restore(err_type, err_value, err_tb);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000233 return ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000234 }
Tim Peters342c65e2001-05-13 06:43:53 +0000235 else if (ep->me_hash == hash && ep->me_key != dummy) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000236 if (!checked_error) {
237 checked_error = 1;
238 if (PyErr_Occurred()) {
239 restore_error = 1;
240 PyErr_Fetch(&err_type, &err_value,
241 &err_tb);
242 }
243 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000244 cmp = PyObject_RichCompareBool(ep->me_key, key, Py_EQ);
245 if (cmp > 0) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000246 if (restore_error)
247 PyErr_Restore(err_type, err_value,
248 err_tb);
249 return ep;
250 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000251 else if (cmp < 0)
252 PyErr_Clear();
Guido van Rossum16e93a81997-01-28 00:00:11 +0000253 }
Tim Peters342c65e2001-05-13 06:43:53 +0000254 else if (ep->me_key == dummy && freeslot == NULL)
255 freeslot = ep;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000256 /* Cycle through GF(2^n)-{0} */
Tim Peters342c65e2001-05-13 06:43:53 +0000257 incr <<= 1;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000258 if (incr > mask)
Tim Peters342c65e2001-05-13 06:43:53 +0000259 incr ^= mp->ma_poly; /* clears the highest bit */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000260 }
261}
262
263/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000264 * Hacked up version of lookdict which can assume keys are always strings;
265 * this assumption allows testing for errors during PyObject_Compare() to
266 * be dropped; string-string comparisons never raise exceptions. This also
267 * means we don't need to go through PyObject_Compare(); we can always use
268 * the tp_compare slot of the string type object directly.
269 *
270 * This really only becomes meaningful if proper error handling in lookdict()
271 * is too expensive.
272 */
273static dictentry *
274lookdict_string(dictobject *mp, PyObject *key, register long hash)
275{
276 register int i;
277 register unsigned incr;
278 register dictentry *freeslot;
279 register unsigned int mask = mp->ma_size-1;
280 dictentry *ep0 = mp->ma_table;
281 register dictentry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000282 cmpfunc compare = PyString_Type.tp_compare;
Fred Drake1bff34a2000-08-31 19:31:38 +0000283
284 /* make sure this function doesn't have to handle non-string keys */
285 if (!PyString_Check(key)) {
286#ifdef SHOW_CONVERSION_COUNTS
287 ++converted;
288#endif
289 mp->ma_lookup = lookdict;
290 return lookdict(mp, key, hash);
291 }
292 /* We must come up with (i, incr) such that 0 <= i < ma_size
293 and 0 < incr < ma_size and both are a function of hash */
Tim Peters2f228e72001-05-13 00:19:31 +0000294 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000295 ep = &ep0[i];
296 if (ep->me_key == NULL || ep->me_key == key)
297 return ep;
298 if (ep->me_key == dummy)
299 freeslot = ep;
300 else {
301 if (ep->me_hash == hash
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000302 && compare(ep->me_key, key) == 0) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000303 return ep;
304 }
305 freeslot = NULL;
306 }
307 /* Derive incr from hash, just to make it more arbitrary. Note that
308 incr must not be 0, or we will get into an infinite loop.*/
309 incr = (hash ^ ((unsigned long)hash >> 3)) & mask;
310 if (!incr)
311 incr = mask;
Tim Peters342c65e2001-05-13 06:43:53 +0000312 /* In the loop, me_key == dummy is by far (factor of 100s) the
313 least likely outcome, so test for that last. */
Fred Drake1bff34a2000-08-31 19:31:38 +0000314 for (;;) {
315 ep = &ep0[(i+incr)&mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000316 if (ep->me_key == NULL)
317 return freeslot == NULL ? ep : freeslot;
318 if (ep->me_key == key
319 || (ep->me_hash == hash
320 && ep->me_key != dummy
321 && compare(ep->me_key, key) == 0))
Fred Drake1bff34a2000-08-31 19:31:38 +0000322 return ep;
Tim Peters342c65e2001-05-13 06:43:53 +0000323 else if (ep->me_key == dummy && freeslot == NULL)
324 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000325 /* Cycle through GF(2^n)-{0} */
Tim Peters342c65e2001-05-13 06:43:53 +0000326 incr <<= 1;
Fred Drake1bff34a2000-08-31 19:31:38 +0000327 if (incr > mask)
Tim Peters342c65e2001-05-13 06:43:53 +0000328 incr ^= mp->ma_poly; /* clears the highest bit */
Fred Drake1bff34a2000-08-31 19:31:38 +0000329 }
330}
331
332/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000333Internal routine to insert a new item into the table.
334Used both by the internal resize routine and by the public insert routine.
335Eats a reference to key and one to value.
336*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000337static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000338insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000339{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000340 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000341 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000342 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000343 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000344 old_value = ep->me_value;
345 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000346 Py_DECREF(old_value); /* which **CAN** re-enter */
347 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000348 }
349 else {
350 if (ep->me_key == NULL)
351 mp->ma_fill++;
352 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000353 Py_DECREF(ep->me_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000354 ep->me_key = key;
355 ep->me_hash = hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000356 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000357 mp->ma_used++;
358 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000359}
360
361/*
362Restructure the table by allocating a new table and reinserting all
363items again. When entries have been deleted, the new table may
364actually be smaller than the old one.
365*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000366static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000367dictresize(dictobject *mp, int minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000368{
369 register int oldsize = mp->ma_size;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000370 register int newsize, newpoly;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000371 register dictentry *oldtable = mp->ma_table;
372 register dictentry *newtable;
373 register dictentry *ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000374 register int i;
Tim Peters91a364d2001-05-19 07:04:38 +0000375
376 assert(minused >= 0);
Guido van Rossum16e93a81997-01-28 00:00:11 +0000377 for (i = 0, newsize = MINSIZE; ; i++, newsize <<= 1) {
Tim Peters91a364d2001-05-19 07:04:38 +0000378 if (i >= sizeof(polys)/sizeof(polys[0])) {
Guido van Rossum16e93a81997-01-28 00:00:11 +0000379 /* Ran out of polynomials */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000380 PyErr_NoMemory();
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000381 return -1;
382 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000383 if (newsize > minused) {
Guido van Rossum16e93a81997-01-28 00:00:11 +0000384 newpoly = polys[i];
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000385 break;
386 }
387 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000388 newtable = PyMem_NEW(dictentry, newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000389 if (newtable == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000390 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000391 return -1;
392 }
Guido van Rossum0fd00331998-07-16 15:06:13 +0000393 memset(newtable, '\0', sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000394 mp->ma_size = newsize;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000395 mp->ma_poly = newpoly;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000396 mp->ma_table = newtable;
397 mp->ma_fill = 0;
398 mp->ma_used = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000399
Tim Peters19283142001-05-17 22:25:34 +0000400 /* Copy the data over; this is refcount-neutral for active entries;
401 dummy entries aren't copied over, of course */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000402 for (i = 0, ep = oldtable; i < oldsize; i++, ep++) {
Tim Peters19283142001-05-17 22:25:34 +0000403 if (ep->me_value != NULL) /* active entry */
404 insertdict(mp, ep->me_key, ep->me_hash, ep->me_value);
405
406 else if (ep->me_key != NULL) { /* dummy entry */
407 assert(ep->me_key == dummy);
408 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000409 }
Tim Peters19283142001-05-17 22:25:34 +0000410 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000411 }
412
Guido van Rossumb18618d2000-05-03 23:44:39 +0000413 if (oldtable != NULL)
414 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000415 return 0;
416}
417
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000418PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000419PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000420{
421 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000422 dictobject *mp = (dictobject *)op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000423 if (!PyDict_Check(op)) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000424 return NULL;
425 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000426 if (mp->ma_table == NULL)
Guido van Rossumd7047b31995-01-02 19:07:15 +0000427 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000428#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000429 if (!PyString_Check(key) ||
430 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000431#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000432 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000433 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000434 if (hash == -1) {
435 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000436 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000437 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000438 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000439 return (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000440}
441
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000442/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
443 * dictionary if it is merely replacing the value for an existing key.
444 * This is means that it's safe to loop over a dictionary with
445 * PyDict_Next() and occasionally replace a value -- but you can't
446 * insert new keys or remove them.
447 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000448int
Tim Peters1f5871e2000-07-04 17:44:48 +0000449PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000450{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000451 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000452 register long hash;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000453 register int n_used;
454
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000455 if (!PyDict_Check(op)) {
456 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000457 return -1;
458 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000459 mp = (dictobject *)op;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000460#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000461 if (PyString_Check(key)) {
Guido van Rossum2a61e741997-01-18 07:55:05 +0000462#ifdef INTERN_STRINGS
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000463 if (((PyStringObject *)key)->ob_sinterned != NULL) {
464 key = ((PyStringObject *)key)->ob_sinterned;
465 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000466 }
467 else
468#endif
469 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000470 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000471 if (hash == -1)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000472 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000473 }
474 }
475 else
476#endif
477 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000478 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000479 if (hash == -1)
480 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000481 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000482 if (mp->ma_fill >= mp->ma_size) {
483 /* No room for a new key.
484 * This only happens when the dict is empty.
485 * Let dictresize() create a minimal dict.
486 */
487 assert(mp->ma_used == 0);
488 if (dictresize(mp, 0) != 0)
489 return -1;
490 assert(mp->ma_fill < mp->ma_size);
491 }
492 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000493 Py_INCREF(value);
494 Py_INCREF(key);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000495 insertdict(mp, key, hash, value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000496 /* If we added a key, we can safely resize. Otherwise skip this!
497 * If fill >= 2/3 size, adjust size. Normally, this doubles the
498 * size, but it's also possible for the dict to shrink (if ma_fill is
499 * much larger than ma_used, meaning a lot of dict keys have been
500 * deleted).
501 */
502 if (mp->ma_used > n_used && mp->ma_fill*3 >= mp->ma_size*2) {
503 if (dictresize(mp, mp->ma_used*2) != 0)
504 return -1;
505 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000506 return 0;
507}
508
509int
Tim Peters1f5871e2000-07-04 17:44:48 +0000510PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000511{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000512 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000513 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000514 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000515 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000516
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000517 if (!PyDict_Check(op)) {
518 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000519 return -1;
520 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000521#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000522 if (!PyString_Check(key) ||
523 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000524#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000525 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000527 if (hash == -1)
528 return -1;
529 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000530 mp = (dictobject *)op;
531 if (((dictobject *)op)->ma_table == NULL)
Guido van Rossumefc87131995-01-02 19:42:39 +0000532 goto empty;
Fred Drake1bff34a2000-08-31 19:31:38 +0000533 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000534 if (ep->me_value == NULL) {
Guido van Rossumefc87131995-01-02 19:42:39 +0000535 empty:
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000536 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000537 return -1;
538 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000539 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000540 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000541 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000542 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000543 ep->me_value = NULL;
544 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000545 Py_DECREF(old_value);
546 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000547 return 0;
548}
549
Guido van Rossum25831651993-05-19 14:50:45 +0000550void
Tim Peters1f5871e2000-07-04 17:44:48 +0000551PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000552{
Guido van Rossumd7047b31995-01-02 19:07:15 +0000553 int i, n;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000554 register dictentry *table;
555 dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000556 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000557 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000558 mp = (dictobject *)op;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000559 table = mp->ma_table;
560 if (table == NULL)
561 return;
562 n = mp->ma_size;
563 mp->ma_size = mp->ma_used = mp->ma_fill = 0;
564 mp->ma_table = NULL;
565 for (i = 0; i < n; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000566 Py_XDECREF(table[i].me_key);
567 Py_XDECREF(table[i].me_value);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000568 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000569 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000570}
571
Tim Peters67830702001-03-21 19:23:56 +0000572/* CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
573 * mutates the dict. One exception: it is safe if the loop merely changes
574 * the values associated with the keys (but doesn't insert new keys or
575 * delete keys), via PyDict_SetItem().
576 */
Guido van Rossum25831651993-05-19 14:50:45 +0000577int
Tim Peters1f5871e2000-07-04 17:44:48 +0000578PyDict_Next(PyObject *op, int *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000579{
Guido van Rossum25831651993-05-19 14:50:45 +0000580 int i;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000581 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000582 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000583 return 0;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000584 mp = (dictobject *)op;
Guido van Rossum25831651993-05-19 14:50:45 +0000585 i = *ppos;
586 if (i < 0)
587 return 0;
588 while (i < mp->ma_size && mp->ma_table[i].me_value == NULL)
589 i++;
590 *ppos = i+1;
591 if (i >= mp->ma_size)
592 return 0;
593 if (pkey)
594 *pkey = mp->ma_table[i].me_key;
595 if (pvalue)
596 *pvalue = mp->ma_table[i].me_value;
597 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000598}
599
600/* Methods */
601
602static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000603dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000604{
605 register int i;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000606 register dictentry *ep;
Guido van Rossumd724b232000-03-13 16:01:29 +0000607 Py_TRASHCAN_SAFE_BEGIN(mp)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000608 PyObject_GC_Fini(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000609 for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) {
Guido van Rossum255443b1998-04-10 22:47:14 +0000610 if (ep->me_key != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000611 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000612 }
613 if (ep->me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000614 Py_DECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000615 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000616 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000617 if (mp->ma_table != NULL)
618 PyMem_DEL(mp->ma_table);
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000619 mp = (dictobject *) PyObject_AS_GC(mp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000620 PyObject_DEL(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000621 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000622}
623
624static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000625dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000626{
627 register int i;
628 register int any;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000629 register dictentry *ep;
Guido van Rossum255443b1998-04-10 22:47:14 +0000630
631 i = Py_ReprEnter((PyObject*)mp);
632 if (i != 0) {
633 if (i < 0)
634 return i;
635 fprintf(fp, "{...}");
636 return 0;
637 }
638
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000639 fprintf(fp, "{");
640 any = 0;
641 for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) {
642 if (ep->me_value != NULL) {
643 if (any++ > 0)
644 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000645 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
646 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000647 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000648 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000649 fprintf(fp, ": ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000650 if (PyObject_Print(ep->me_value, fp, 0) != 0) {
651 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000652 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000653 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000654 }
655 }
656 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000657 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000658 return 0;
659}
660
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000661static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000662dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000663{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000664 auto PyObject *v;
665 PyObject *sepa, *colon;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000666 register int i;
667 register int any;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000668 register dictentry *ep;
Guido van Rossum255443b1998-04-10 22:47:14 +0000669
670 i = Py_ReprEnter((PyObject*)mp);
671 if (i != 0) {
672 if (i > 0)
673 return PyString_FromString("{...}");
674 return NULL;
675 }
676
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000677 v = PyString_FromString("{");
678 sepa = PyString_FromString(", ");
679 colon = PyString_FromString(": ");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000680 any = 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000681 for (i = 0, ep = mp->ma_table; i < mp->ma_size && v; i++, ep++) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000682 if (ep->me_value != NULL) {
683 if (any++)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000684 PyString_Concat(&v, sepa);
685 PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_key));
686 PyString_Concat(&v, colon);
687 PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_value));
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000688 }
689 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000690 PyString_ConcatAndDel(&v, PyString_FromString("}"));
Guido van Rossum255443b1998-04-10 22:47:14 +0000691 Py_ReprLeave((PyObject*)mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000692 Py_XDECREF(sepa);
693 Py_XDECREF(colon);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000694 return v;
695}
696
697static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000698dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000699{
700 return mp->ma_used;
701}
702
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000703static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000704dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000705{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000706 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000707 long hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000708 if (mp->ma_table == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000709 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossumd7047b31995-01-02 19:07:15 +0000710 return NULL;
711 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000712#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000713 if (!PyString_Check(key) ||
714 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000715#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000716 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000717 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000718 if (hash == -1)
719 return NULL;
720 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000721 v = (mp->ma_lookup)(mp, key, hash) -> me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000722 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000723 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000724 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000725 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000726 return v;
727}
728
729static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000730dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000731{
732 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000733 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000734 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000735 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000736}
737
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000738static PyMappingMethods dict_as_mapping = {
739 (inquiry)dict_length, /*mp_length*/
740 (binaryfunc)dict_subscript, /*mp_subscript*/
741 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000742};
743
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000744static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000745dict_keys(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000746{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000747 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000748 register int i, j, n;
749
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000750 if (!PyArg_NoArgs(args))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000751 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000752 again:
753 n = mp->ma_used;
754 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000755 if (v == NULL)
756 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000757 if (n != mp->ma_used) {
758 /* Durnit. The allocations caused the dict to resize.
759 * Just start over, this shouldn't normally happen.
760 */
761 Py_DECREF(v);
762 goto again;
763 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000764 for (i = 0, j = 0; i < mp->ma_size; i++) {
765 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000766 PyObject *key = mp->ma_table[i].me_key;
767 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000768 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000769 j++;
770 }
771 }
772 return v;
773}
774
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000775static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000776dict_values(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000777{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000778 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000779 register int i, j, n;
780
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000781 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000782 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000783 again:
784 n = mp->ma_used;
785 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000786 if (v == NULL)
787 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000788 if (n != mp->ma_used) {
789 /* Durnit. The allocations caused the dict to resize.
790 * Just start over, this shouldn't normally happen.
791 */
792 Py_DECREF(v);
793 goto again;
794 }
Guido van Rossum25831651993-05-19 14:50:45 +0000795 for (i = 0, j = 0; i < mp->ma_size; i++) {
796 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000797 PyObject *value = mp->ma_table[i].me_value;
798 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000799 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000800 j++;
801 }
802 }
803 return v;
804}
805
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000806static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000807dict_items(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000808{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000809 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000810 register int i, j, n;
811 PyObject *item, *key, *value;
812
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000813 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000814 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000815 /* Preallocate the list of tuples, to avoid allocations during
816 * the loop over the items, which could trigger GC, which
817 * could resize the dict. :-(
818 */
819 again:
820 n = mp->ma_used;
821 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000822 if (v == NULL)
823 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000824 for (i = 0; i < n; i++) {
825 item = PyTuple_New(2);
826 if (item == NULL) {
827 Py_DECREF(v);
828 return NULL;
829 }
830 PyList_SET_ITEM(v, i, item);
831 }
832 if (n != mp->ma_used) {
833 /* Durnit. The allocations caused the dict to resize.
834 * Just start over, this shouldn't normally happen.
835 */
836 Py_DECREF(v);
837 goto again;
838 }
839 /* Nothing we do below makes any function calls. */
Guido van Rossum25831651993-05-19 14:50:45 +0000840 for (i = 0, j = 0; i < mp->ma_size; i++) {
841 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000842 key = mp->ma_table[i].me_key;
843 value = mp->ma_table[i].me_value;
844 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000845 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000846 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000847 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000848 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000849 j++;
850 }
851 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000852 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +0000853 return v;
854}
855
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000856static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000857dict_update(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000858{
859 register int i;
860 dictobject *other;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000861 dictentry *entry;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000862 if (!PyArg_Parse(args, "O!", &PyDict_Type, &other))
863 return NULL;
Jeremy Hylton1fb60882001-01-03 22:34:59 +0000864 if (other == mp || other->ma_used == 0)
865 goto done; /* a.update(a) or a.update({}); nothing to do */
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000866 /* Do one big resize at the start, rather than incrementally
867 resizing as we insert new items. Expect that there will be
868 no (or few) overlapping keys. */
869 if ((mp->ma_fill + other->ma_used)*3 >= mp->ma_size*2) {
870 if (dictresize(mp, (mp->ma_used + other->ma_used)*3/2) != 0)
871 return NULL;
872 }
873 for (i = 0; i < other->ma_size; i++) {
874 entry = &other->ma_table[i];
875 if (entry->me_value != NULL) {
876 Py_INCREF(entry->me_key);
877 Py_INCREF(entry->me_value);
878 insertdict(mp, entry->me_key, entry->me_hash,
879 entry->me_value);
880 }
881 }
882 done:
883 Py_INCREF(Py_None);
884 return Py_None;
885}
886
887static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000888dict_copy(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000889{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000890 if (!PyArg_Parse(args, ""))
891 return NULL;
892 return PyDict_Copy((PyObject*)mp);
893}
894
895PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000896PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000897{
898 register dictobject *mp;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000899 register int i;
900 dictobject *copy;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000901 dictentry *entry;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000902
903 if (o == NULL || !PyDict_Check(o)) {
904 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000905 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000906 }
907 mp = (dictobject *)o;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000908 copy = (dictobject *)PyDict_New();
909 if (copy == NULL)
910 return NULL;
911 if (mp->ma_used > 0) {
912 if (dictresize(copy, mp->ma_used*3/2) != 0)
913 return NULL;
914 for (i = 0; i < mp->ma_size; i++) {
915 entry = &mp->ma_table[i];
916 if (entry->me_value != NULL) {
917 Py_INCREF(entry->me_key);
918 Py_INCREF(entry->me_value);
919 insertdict(copy, entry->me_key, entry->me_hash,
920 entry->me_value);
921 }
922 }
923 }
924 return (PyObject *)copy;
925}
926
Guido van Rossum4199fac1993-11-05 10:18:44 +0000927int
Tim Peters1f5871e2000-07-04 17:44:48 +0000928PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +0000929{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000930 if (mp == NULL || !PyDict_Check(mp)) {
931 PyErr_BadInternalCall();
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000932 return 0;
Guido van Rossum4199fac1993-11-05 10:18:44 +0000933 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000934 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +0000935}
936
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000937PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000938PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000939{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000940 if (mp == NULL || !PyDict_Check(mp)) {
941 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000942 return NULL;
943 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000944 return dict_keys((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000945}
946
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000947PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000948PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +0000949{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000950 if (mp == NULL || !PyDict_Check(mp)) {
951 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +0000952 return NULL;
953 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000954 return dict_values((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +0000955}
956
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000957PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000958PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +0000959{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000960 if (mp == NULL || !PyDict_Check(mp)) {
961 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +0000962 return NULL;
963 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000964 return dict_items((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +0000965}
966
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000967/* Subroutine which returns the smallest key in a for which b's value
968 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +0000969 pval argument. Both are NULL if no key in a is found for which b's status
970 differs. The refcounts on (and only on) non-NULL *pval and function return
971 values must be decremented by the caller (characterize() increments them
972 to ensure that mutating comparison and PyDict_GetItem calls can't delete
973 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000974
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000975static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000976characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000977{
Tim Peters95bf9392001-05-10 08:32:44 +0000978 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
979 PyObject *aval = NULL; /* a[akey] */
Guido van Rossumb9324202001-01-18 00:39:02 +0000980 int i, cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000981
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000982 for (i = 0; i < a->ma_size; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +0000983 PyObject *thiskey, *thisaval, *thisbval;
984 if (a->ma_table[i].me_value == NULL)
985 continue;
986 thiskey = a->ma_table[i].me_key;
987 Py_INCREF(thiskey); /* keep alive across compares */
988 if (akey != NULL) {
989 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
990 if (cmp < 0) {
991 Py_DECREF(thiskey);
992 goto Fail;
993 }
994 if (cmp > 0 ||
995 i >= a->ma_size ||
996 a->ma_table[i].me_value == NULL)
997 {
998 /* Not the *smallest* a key; or maybe it is
999 * but the compare shrunk the dict so we can't
1000 * find its associated value anymore; or
1001 * maybe it is but the compare deleted the
1002 * a[thiskey] entry.
1003 */
1004 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001005 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001006 }
Tim Peters95bf9392001-05-10 08:32:44 +00001007 }
1008
1009 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1010 thisaval = a->ma_table[i].me_value;
1011 assert(thisaval);
1012 Py_INCREF(thisaval); /* keep alive */
1013 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1014 if (thisbval == NULL)
1015 cmp = 0;
1016 else {
1017 /* both dicts have thiskey: same values? */
1018 cmp = PyObject_RichCompareBool(
1019 thisaval, thisbval, Py_EQ);
1020 if (cmp < 0) {
1021 Py_DECREF(thiskey);
1022 Py_DECREF(thisaval);
1023 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001024 }
1025 }
Tim Peters95bf9392001-05-10 08:32:44 +00001026 if (cmp == 0) {
1027 /* New winner. */
1028 Py_XDECREF(akey);
1029 Py_XDECREF(aval);
1030 akey = thiskey;
1031 aval = thisaval;
1032 }
1033 else {
1034 Py_DECREF(thiskey);
1035 Py_DECREF(thisaval);
1036 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001037 }
Tim Peters95bf9392001-05-10 08:32:44 +00001038 *pval = aval;
1039 return akey;
1040
1041Fail:
1042 Py_XDECREF(akey);
1043 Py_XDECREF(aval);
1044 *pval = NULL;
1045 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001046}
1047
1048static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001049dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001050{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001051 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001052 int res;
1053
1054 /* Compare lengths first */
1055 if (a->ma_used < b->ma_used)
1056 return -1; /* a is shorter */
1057 else if (a->ma_used > b->ma_used)
1058 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001059
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001060 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001061 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001062 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001063 if (adiff == NULL) {
1064 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001065 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001066 * must be equal.
1067 */
1068 res = PyErr_Occurred() ? -1 : 0;
1069 goto Finished;
1070 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001071 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001072 if (bdiff == NULL && PyErr_Occurred()) {
1073 assert(!bval);
1074 res = -1;
1075 goto Finished;
1076 }
1077 res = 0;
1078 if (bdiff) {
1079 /* bdiff == NULL "should be" impossible now, but perhaps
1080 * the last comparison done by the characterize() on a had
1081 * the side effect of making the dicts equal!
1082 */
1083 res = PyObject_Compare(adiff, bdiff);
1084 }
1085 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001086 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001087
1088Finished:
1089 Py_XDECREF(adiff);
1090 Py_XDECREF(bdiff);
1091 Py_XDECREF(aval);
1092 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001093 return res;
1094}
1095
Tim Peterse63415e2001-05-08 04:38:29 +00001096/* Return 1 if dicts equal, 0 if not, -1 if error.
1097 * Gets out as soon as any difference is detected.
1098 * Uses only Py_EQ comparison.
1099 */
1100static int
1101dict_equal(dictobject *a, dictobject *b)
1102{
1103 int i;
1104
1105 if (a->ma_used != b->ma_used)
1106 /* can't be equal if # of entries differ */
1107 return 0;
1108
1109 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1110 for (i = 0; i < a->ma_size; i++) {
1111 PyObject *aval = a->ma_table[i].me_value;
1112 if (aval != NULL) {
1113 int cmp;
1114 PyObject *bval;
1115 PyObject *key = a->ma_table[i].me_key;
1116 /* temporarily bump aval's refcount to ensure it stays
1117 alive until we're done with it */
1118 Py_INCREF(aval);
1119 bval = PyDict_GetItem((PyObject *)b, key);
1120 if (bval == NULL) {
1121 Py_DECREF(aval);
1122 return 0;
1123 }
1124 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1125 Py_DECREF(aval);
1126 if (cmp <= 0) /* error or not equal */
1127 return cmp;
1128 }
1129 }
1130 return 1;
1131 }
1132
1133static PyObject *
1134dict_richcompare(PyObject *v, PyObject *w, int op)
1135{
1136 int cmp;
1137 PyObject *res;
1138
1139 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1140 res = Py_NotImplemented;
1141 }
1142 else if (op == Py_EQ || op == Py_NE) {
1143 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1144 if (cmp < 0)
1145 return NULL;
1146 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1147 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001148 else
1149 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001150 Py_INCREF(res);
1151 return res;
1152 }
1153
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001154static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001155dict_has_key(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001156{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001157 PyObject *key;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001158 long hash;
1159 register long ok;
Fred Drake52fccfd2000-02-23 15:47:16 +00001160 if (!PyArg_ParseTuple(args, "O:has_key", &key))
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001161 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001162#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001163 if (!PyString_Check(key) ||
1164 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001165#endif
Guido van Rossumca756f21997-01-23 19:39:29 +00001166 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001167 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001168 if (hash == -1)
1169 return NULL;
1170 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001171 ok = (mp->ma_size != 0
1172 && (mp->ma_lookup)(mp, key, hash)->me_value != NULL);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001173 return PyInt_FromLong(ok);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001174}
1175
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001176static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001177dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001178{
1179 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001180 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001181 PyObject *val = NULL;
1182 long hash;
1183
Fred Drake52fccfd2000-02-23 15:47:16 +00001184 if (!PyArg_ParseTuple(args, "O|O:get", &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001185 return NULL;
Guido van Rossum6fcfa721997-10-20 20:10:00 +00001186 if (mp->ma_table == NULL)
1187 goto finally;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001188
Barry Warsawc38c5da1997-10-06 17:49:20 +00001189#ifdef CACHE_HASH
1190 if (!PyString_Check(key) ||
1191 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1192#endif
1193 {
1194 hash = PyObject_Hash(key);
1195 if (hash == -1)
1196 return NULL;
1197 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001198 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Barry Warsaw320ac331997-10-20 17:26:25 +00001199
Guido van Rossum6fcfa721997-10-20 20:10:00 +00001200 finally:
Barry Warsawc38c5da1997-10-06 17:49:20 +00001201 if (val == NULL)
1202 val = failobj;
1203 Py_INCREF(val);
1204 return val;
1205}
1206
1207
1208static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001209dict_setdefault(register dictobject *mp, PyObject *args)
1210{
1211 PyObject *key;
1212 PyObject *failobj = Py_None;
1213 PyObject *val = NULL;
1214 long hash;
1215
1216 if (!PyArg_ParseTuple(args, "O|O:setdefault", &key, &failobj))
1217 return NULL;
1218 if (mp->ma_table == NULL)
1219 goto finally;
1220
1221#ifdef CACHE_HASH
1222 if (!PyString_Check(key) ||
1223 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1224#endif
1225 {
1226 hash = PyObject_Hash(key);
1227 if (hash == -1)
1228 return NULL;
1229 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001230 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001231
1232 finally:
1233 if (val == NULL) {
1234 val = failobj;
1235 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1236 val = NULL;
1237 }
1238 Py_XINCREF(val);
1239 return val;
1240}
1241
1242
1243static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001244dict_clear(register dictobject *mp, PyObject *args)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001245{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001246 if (!PyArg_NoArgs(args))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001247 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001248 PyDict_Clear((PyObject *)mp);
1249 Py_INCREF(Py_None);
1250 return Py_None;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001251}
1252
Guido van Rossumba6ab842000-12-12 22:02:18 +00001253static PyObject *
1254dict_popitem(dictobject *mp, PyObject *args)
1255{
1256 int i = 0;
1257 dictentry *ep;
1258 PyObject *res;
1259
1260 if (!PyArg_NoArgs(args))
1261 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001262 /* Allocate the result tuple first. Believe it or not,
1263 * this allocation could trigger a garbage collection which
1264 * could resize the dict, which would invalidate the pointer
1265 * (ep) into the dict calculated below.
1266 * So we have to do this first.
1267 */
1268 res = PyTuple_New(2);
1269 if (res == NULL)
1270 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001271 if (mp->ma_used == 0) {
1272 Py_DECREF(res);
1273 PyErr_SetString(PyExc_KeyError,
1274 "popitem(): dictionary is empty");
1275 return NULL;
1276 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001277 /* Set ep to "the first" dict entry with a value. We abuse the hash
1278 * field of slot 0 to hold a search finger:
1279 * If slot 0 has a value, use slot 0.
1280 * Else slot 0 is being used to hold a search finger,
1281 * and we use its hash value as the first index to look.
1282 */
1283 ep = &mp->ma_table[0];
1284 if (ep->me_value == NULL) {
1285 i = (int)ep->me_hash;
1286 /* The hash field may be uninitialized trash, or it
1287 * may be a real hash value, or it may be a legit
1288 * search finger, or it may be a once-legit search
1289 * finger that's out of bounds now because it
1290 * wrapped around or the table shrunk -- simply
1291 * make sure it's in bounds now.
1292 */
1293 if (i >= mp->ma_size || i < 1)
1294 i = 1; /* skip slot 0 */
1295 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1296 i++;
1297 if (i >= mp->ma_size)
1298 i = 1;
1299 }
1300 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001301 PyTuple_SET_ITEM(res, 0, ep->me_key);
1302 PyTuple_SET_ITEM(res, 1, ep->me_value);
1303 Py_INCREF(dummy);
1304 ep->me_key = dummy;
1305 ep->me_value = NULL;
1306 mp->ma_used--;
1307 assert(mp->ma_table[0].me_value == NULL);
1308 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001309 return res;
1310}
1311
Jeremy Hylton8caad492000-06-23 14:18:11 +00001312static int
1313dict_traverse(PyObject *op, visitproc visit, void *arg)
1314{
1315 int i = 0, err;
1316 PyObject *pk;
1317 PyObject *pv;
1318
1319 while (PyDict_Next(op, &i, &pk, &pv)) {
1320 err = visit(pk, arg);
1321 if (err)
1322 return err;
1323 err = visit(pv, arg);
1324 if (err)
1325 return err;
1326 }
1327 return 0;
1328}
1329
1330static int
1331dict_tp_clear(PyObject *op)
1332{
1333 PyDict_Clear(op);
1334 return 0;
1335}
1336
Tim Petersf7f88b12000-12-13 23:18:45 +00001337
Guido van Rossum09e563a2001-05-01 12:10:21 +00001338staticforward PyObject *dictiter_new(dictobject *, binaryfunc);
1339
1340static PyObject *
1341select_key(PyObject *key, PyObject *value)
1342{
1343 Py_INCREF(key);
1344 return key;
1345}
1346
1347static PyObject *
1348select_value(PyObject *key, PyObject *value)
1349{
1350 Py_INCREF(value);
1351 return value;
1352}
1353
1354static PyObject *
1355select_item(PyObject *key, PyObject *value)
1356{
1357 PyObject *res = PyTuple_New(2);
1358
1359 if (res != NULL) {
1360 Py_INCREF(key);
1361 Py_INCREF(value);
1362 PyTuple_SET_ITEM(res, 0, key);
1363 PyTuple_SET_ITEM(res, 1, value);
1364 }
1365 return res;
1366}
1367
1368static PyObject *
1369dict_iterkeys(dictobject *dict, PyObject *args)
1370{
1371 if (!PyArg_ParseTuple(args, ""))
1372 return NULL;
1373 return dictiter_new(dict, select_key);
1374}
1375
1376static PyObject *
1377dict_itervalues(dictobject *dict, PyObject *args)
1378{
1379 if (!PyArg_ParseTuple(args, ""))
1380 return NULL;
1381 return dictiter_new(dict, select_value);
1382}
1383
1384static PyObject *
1385dict_iteritems(dictobject *dict, PyObject *args)
1386{
1387 if (!PyArg_ParseTuple(args, ""))
1388 return NULL;
1389 return dictiter_new(dict, select_item);
1390}
1391
1392
Tim Petersf7f88b12000-12-13 23:18:45 +00001393static char has_key__doc__[] =
1394"D.has_key(k) -> 1 if D has a key k, else 0";
1395
1396static char get__doc__[] =
1397"D.get(k[,d]) -> D[k] if D.has_key(k), else d. d defaults to None.";
1398
1399static char setdefault_doc__[] =
1400"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if not D.has_key(k)";
1401
1402static char popitem__doc__[] =
1403"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
14042-tuple; but raise KeyError if D is empty";
1405
1406static char keys__doc__[] =
1407"D.keys() -> list of D's keys";
1408
1409static char items__doc__[] =
1410"D.items() -> list of D's (key, value) pairs, as 2-tuples";
1411
1412static char values__doc__[] =
1413"D.values() -> list of D's values";
1414
1415static char update__doc__[] =
1416"D.update(E) -> None. Update D from E: for k in E.keys(): D[k] = E[k]";
1417
1418static char clear__doc__[] =
1419"D.clear() -> None. Remove all items from D.";
1420
1421static char copy__doc__[] =
1422"D.copy() -> a shallow copy of D";
1423
Guido van Rossum09e563a2001-05-01 12:10:21 +00001424static char iterkeys__doc__[] =
1425"D.iterkeys() -> an iterator over the keys of D";
1426
1427static char itervalues__doc__[] =
1428"D.itervalues() -> an iterator over the values of D";
1429
1430static char iteritems__doc__[] =
1431"D.iteritems() -> an iterator over the (key, value) items of D";
1432
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001433static PyMethodDef mapp_methods[] = {
Tim Petersf7f88b12000-12-13 23:18:45 +00001434 {"has_key", (PyCFunction)dict_has_key, METH_VARARGS,
1435 has_key__doc__},
1436 {"get", (PyCFunction)dict_get, METH_VARARGS,
1437 get__doc__},
1438 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1439 setdefault_doc__},
1440 {"popitem", (PyCFunction)dict_popitem, METH_OLDARGS,
1441 popitem__doc__},
1442 {"keys", (PyCFunction)dict_keys, METH_OLDARGS,
1443 keys__doc__},
1444 {"items", (PyCFunction)dict_items, METH_OLDARGS,
1445 items__doc__},
1446 {"values", (PyCFunction)dict_values, METH_OLDARGS,
1447 values__doc__},
1448 {"update", (PyCFunction)dict_update, METH_OLDARGS,
1449 update__doc__},
1450 {"clear", (PyCFunction)dict_clear, METH_OLDARGS,
1451 clear__doc__},
1452 {"copy", (PyCFunction)dict_copy, METH_OLDARGS,
1453 copy__doc__},
Guido van Rossum09e563a2001-05-01 12:10:21 +00001454 {"iterkeys", (PyCFunction)dict_iterkeys, METH_VARARGS,
1455 iterkeys__doc__},
1456 {"itervalues", (PyCFunction)dict_itervalues, METH_VARARGS,
1457 itervalues__doc__},
1458 {"iteritems", (PyCFunction)dict_iteritems, METH_VARARGS,
1459 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001460 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001461};
1462
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001463static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001464dict_getattr(dictobject *mp, char *name)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001465{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001466 return Py_FindMethod(mapp_methods, (PyObject *)mp, name);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001467}
1468
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001469static int
1470dict_contains(dictobject *mp, PyObject *key)
1471{
1472 long hash;
1473
1474#ifdef CACHE_HASH
1475 if (!PyString_Check(key) ||
1476 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1477#endif
1478 {
1479 hash = PyObject_Hash(key);
1480 if (hash == -1)
1481 return -1;
1482 }
1483 return (mp->ma_size != 0
1484 && (mp->ma_lookup)(mp, key, hash)->me_value != NULL);
1485}
1486
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001487/* Hack to implement "key in dict" */
1488static PySequenceMethods dict_as_sequence = {
1489 0, /* sq_length */
1490 0, /* sq_concat */
1491 0, /* sq_repeat */
1492 0, /* sq_item */
1493 0, /* sq_slice */
1494 0, /* sq_ass_item */
1495 0, /* sq_ass_slice */
1496 (objobjproc)dict_contains, /* sq_contains */
1497 0, /* sq_inplace_concat */
1498 0, /* sq_inplace_repeat */
1499};
1500
Guido van Rossum09e563a2001-05-01 12:10:21 +00001501static PyObject *
1502dict_iter(dictobject *dict)
1503{
1504 return dictiter_new(dict, select_key);
1505}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001506
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001507PyTypeObject PyDict_Type = {
1508 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001509 0,
Guido van Rossum9bfef441993-03-29 10:43:31 +00001510 "dictionary",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001511 sizeof(dictobject) + PyGC_HEAD_SIZE,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001512 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001513 (destructor)dict_dealloc, /* tp_dealloc */
1514 (printfunc)dict_print, /* tp_print */
1515 (getattrfunc)dict_getattr, /* tp_getattr */
1516 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00001517 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001518 (reprfunc)dict_repr, /* tp_repr */
1519 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001520 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001521 &dict_as_mapping, /* tp_as_mapping */
1522 0, /* tp_hash */
1523 0, /* tp_call */
1524 0, /* tp_str */
1525 0, /* tp_getattro */
1526 0, /* tp_setattro */
1527 0, /* tp_as_buffer */
1528 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
1529 0, /* tp_doc */
1530 (traverseproc)dict_traverse, /* tp_traverse */
1531 (inquiry)dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001532 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001533 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001534 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001535 0, /* tp_iternext */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001536};
1537
Guido van Rossum3cca2451997-05-16 14:23:33 +00001538/* For backward compatibility with old dictionary interface */
1539
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001540PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001541PyDict_GetItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001542{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001543 PyObject *kv, *rv;
1544 kv = PyString_FromString(key);
1545 if (kv == NULL)
1546 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001547 rv = PyDict_GetItem(v, kv);
1548 Py_DECREF(kv);
1549 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001550}
1551
1552int
Tim Peters1f5871e2000-07-04 17:44:48 +00001553PyDict_SetItemString(PyObject *v, char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001554{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001555 PyObject *kv;
1556 int err;
1557 kv = PyString_FromString(key);
1558 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001559 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00001560 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00001561 err = PyDict_SetItem(v, kv, item);
1562 Py_DECREF(kv);
1563 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001564}
1565
1566int
Tim Peters1f5871e2000-07-04 17:44:48 +00001567PyDict_DelItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001568{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001569 PyObject *kv;
1570 int err;
1571 kv = PyString_FromString(key);
1572 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001573 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001574 err = PyDict_DelItem(v, kv);
1575 Py_DECREF(kv);
1576 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001577}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001578
1579/* Dictionary iterator type */
1580
1581extern PyTypeObject PyDictIter_Type; /* Forward */
1582
1583typedef struct {
1584 PyObject_HEAD
1585 dictobject *di_dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001586 int di_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001587 int di_pos;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001588 binaryfunc di_select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001589} dictiterobject;
1590
1591static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001592dictiter_new(dictobject *dict, binaryfunc select)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001593{
1594 dictiterobject *di;
1595 di = PyObject_NEW(dictiterobject, &PyDictIter_Type);
1596 if (di == NULL)
1597 return NULL;
1598 Py_INCREF(dict);
1599 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001600 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001601 di->di_pos = 0;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001602 di->di_select = select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001603 return (PyObject *)di;
1604}
1605
1606static void
1607dictiter_dealloc(dictiterobject *di)
1608{
1609 Py_DECREF(di->di_dict);
1610 PyObject_DEL(di);
1611}
1612
1613static PyObject *
1614dictiter_next(dictiterobject *di, PyObject *args)
1615{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001616 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001617
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001618 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001619 PyErr_SetString(PyExc_RuntimeError,
1620 "dictionary changed size during iteration");
1621 return NULL;
1622 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001623 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1624 return (*di->di_select)(key, value);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001625 }
1626 PyErr_SetObject(PyExc_StopIteration, Py_None);
1627 return NULL;
1628}
1629
1630static PyObject *
1631dictiter_getiter(PyObject *it)
1632{
1633 Py_INCREF(it);
1634 return it;
1635}
1636
1637static PyMethodDef dictiter_methods[] = {
1638 {"next", (PyCFunction)dictiter_next, METH_VARARGS,
1639 "it.next() -- get the next value, or raise StopIteration"},
1640 {NULL, NULL} /* sentinel */
1641};
1642
1643static PyObject *
Guido van Rossum213c7a62001-04-23 14:08:49 +00001644dictiter_getattr(dictiterobject *di, char *name)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001645{
Guido van Rossum213c7a62001-04-23 14:08:49 +00001646 return Py_FindMethod(dictiter_methods, (PyObject *)di, name);
1647}
1648
1649static PyObject *dictiter_iternext(dictiterobject *di)
1650{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001651 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001652
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001653 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00001654 PyErr_SetString(PyExc_RuntimeError,
1655 "dictionary changed size during iteration");
1656 return NULL;
1657 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001658 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1659 return (*di->di_select)(key, value);
Guido van Rossum213c7a62001-04-23 14:08:49 +00001660 }
1661 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001662}
1663
1664PyTypeObject PyDictIter_Type = {
1665 PyObject_HEAD_INIT(&PyType_Type)
1666 0, /* ob_size */
1667 "dictionary-iterator", /* tp_name */
1668 sizeof(dictiterobject), /* tp_basicsize */
1669 0, /* tp_itemsize */
1670 /* methods */
1671 (destructor)dictiter_dealloc, /* tp_dealloc */
1672 0, /* tp_print */
1673 (getattrfunc)dictiter_getattr, /* tp_getattr */
1674 0, /* tp_setattr */
1675 0, /* tp_compare */
1676 0, /* tp_repr */
1677 0, /* tp_as_number */
1678 0, /* tp_as_sequence */
1679 0, /* tp_as_mapping */
1680 0, /* tp_hash */
1681 0, /* tp_call */
1682 0, /* tp_str */
1683 0, /* tp_getattro */
1684 0, /* tp_setattro */
1685 0, /* tp_as_buffer */
1686 Py_TPFLAGS_DEFAULT, /* tp_flags */
1687 0, /* tp_doc */
1688 0, /* tp_traverse */
1689 0, /* tp_clear */
1690 0, /* tp_richcompare */
1691 0, /* tp_weaklistoffset */
1692 (getiterfunc)dictiter_getiter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001693 (iternextfunc)dictiter_iternext, /* tp_iternext */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001694};