blob: b0121ed650655593d8c0756cf532d8f5d8152cad [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,
50 0
Guido van Rossum4b1302b1993-03-27 18:11:32 +000051};
52
53/* Object used as dummy key to fill deleted entries */
Guido van Rossuma9e7a811997-05-13 21:02:11 +000054static PyObject *dummy; /* Initialized by first call to newdictobject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +000055
56/*
Tim Petersea8f2bf2000-12-13 01:02:46 +000057There are three kinds of slots in the table:
58
591. Unused. me_key == me_value == NULL
60 Does not hold an active (key, value) pair now and never did. Unused can
61 transition to Active upon key insertion. This is the only case in which
62 me_key is NULL, and is each slot's initial state.
63
642. Active. me_key != NULL and me_key != dummy and me_value != NULL
65 Holds an active (key, value) pair. Active can transition to Dummy upon
66 key deletion. This is the only case in which me_value != NULL.
67
Tim Petersf1c7c882000-12-13 19:58:25 +0000683. Dummy. me_key == dummy and me_value == NULL
Tim Petersea8f2bf2000-12-13 01:02:46 +000069 Previously held an active (key, value) pair, but that was deleted and an
70 active pair has not yet overwritten the slot. Dummy can transition to
71 Active upon key insertion. Dummy slots cannot be made Unused again
72 (cannot have me_key set to NULL), else the probe sequence in case of
73 collision would have no way to know they were once active.
Tim Petersf1c7c882000-12-13 19:58:25 +000074
75Note: .popitem() abuses the me_hash field of an Unused or Dummy slot to
76hold a search finger. The me_hash field of Unused or Dummy slots has no
77meaning otherwise.
Guido van Rossum4b1302b1993-03-27 18:11:32 +000078*/
79typedef struct {
Tim Petersea8f2bf2000-12-13 01:02:46 +000080 long me_hash; /* cached hash code of me_key */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000081 PyObject *me_key;
82 PyObject *me_value;
Guido van Rossum36488841997-04-11 19:14:07 +000083#ifdef USE_CACHE_ALIGNED
84 long aligner;
85#endif
Guido van Rossuma9e7a811997-05-13 21:02:11 +000086} dictentry;
Guido van Rossum4b1302b1993-03-27 18:11:32 +000087
88/*
Tim Petersf1c7c882000-12-13 19:58:25 +000089To ensure the lookup algorithm terminates, there must be at least one Unused
Tim Petersea8f2bf2000-12-13 01:02:46 +000090slot (NULL key) in the table.
91The value ma_fill is the number of non-NULL keys (sum of Active and Dummy);
92ma_used is the number of non-NULL, non-dummy keys (== the number of non-NULL
93values == the number of Active items).
94To avoid slowing down lookups on a near-full table, we resize the table when
Tim Peters67830702001-03-21 19:23:56 +000095it's two-thirds full.
Guido van Rossum4b1302b1993-03-27 18:11:32 +000096*/
Fred Drake1bff34a2000-08-31 19:31:38 +000097typedef struct dictobject dictobject;
98struct dictobject {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000099 PyObject_HEAD
Tim Petersea8f2bf2000-12-13 01:02:46 +0000100 int ma_fill; /* # Active + # Dummy */
101 int ma_used; /* # Active */
102 int ma_size; /* total # slots in ma_table */
103 int ma_poly; /* appopriate entry from polys vector */
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000104 dictentry *ma_table;
Fred Drake1bff34a2000-08-31 19:31:38 +0000105 dictentry *(*ma_lookup)(dictobject *mp, PyObject *key, long hash);
106};
107
108/* forward declarations */
109static dictentry *
110lookdict_string(dictobject *mp, PyObject *key, long hash);
111
112#ifdef SHOW_CONVERSION_COUNTS
113static long created = 0L;
114static long converted = 0L;
115
116static void
117show_counts(void)
118{
119 fprintf(stderr, "created %ld string dicts\n", created);
120 fprintf(stderr, "converted %ld to normal dicts\n", converted);
121 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
122}
123#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000124
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000125PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000126PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000127{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000128 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000129 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000130 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000131 if (dummy == NULL)
132 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000133#ifdef SHOW_CONVERSION_COUNTS
134 Py_AtExit(show_counts);
135#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000136 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000137 mp = PyObject_NEW(dictobject, &PyDict_Type);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000138 if (mp == NULL)
139 return NULL;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000140 mp->ma_size = 0;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000141 mp->ma_poly = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000142 mp->ma_table = NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000143 mp->ma_fill = 0;
144 mp->ma_used = 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000145 mp->ma_lookup = lookdict_string;
146#ifdef SHOW_CONVERSION_COUNTS
147 ++created;
148#endif
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000149 PyObject_GC_Init(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000150 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000151}
152
153/*
154The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000155This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000156Open addressing is preferred over chaining since the link overhead for
157chaining would be substantial (100% with typical malloc overhead).
Guido van Rossum16e93a81997-01-28 00:00:11 +0000158However, instead of going through the table at constant steps, we cycle
Tim Petersea8f2bf2000-12-13 01:02:46 +0000159through the values of GF(2^n). This avoids modulo computations, being
Guido van Rossum16e93a81997-01-28 00:00:11 +0000160much cheaper on RISC machines, without leading to clustering.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000161
Guido van Rossum2bc13791999-03-24 19:06:42 +0000162The initial probe index is computed as hash mod the table size.
Tim Petersea8f2bf2000-12-13 01:02:46 +0000163Subsequent probe indices use the values of x^i in GF(2^n)-{0} as an offset,
164where x is a root. The initial offset is derived from hash, too.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000165
166All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000167
168(This version is due to Reimer Behrends, some ideas are also due to
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000169Jyrki Alakuijala and Vladimir Marangozov.)
Fred Drake1bff34a2000-08-31 19:31:38 +0000170
171This function must never return NULL; failures are indicated by returning
172a dictentry* for which the me_value field is NULL. Exceptions are never
173reported by this function, and outstanding exceptions are maintained.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000174*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000175static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000176lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000177{
Guido van Rossum16e93a81997-01-28 00:00:11 +0000178 register int i;
179 register unsigned incr;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000180 register dictentry *freeslot;
Guido van Rossum2095d241997-04-09 19:41:24 +0000181 register unsigned int mask = mp->ma_size-1;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000182 dictentry *ep0 = mp->ma_table;
183 register dictentry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000184 register int restore_error = 0;
185 register int checked_error = 0;
186 register int cmp;
187 PyObject *err_type, *err_value, *err_tb;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000188 /* We must come up with (i, incr) such that 0 <= i < ma_size
Tim Petersea8f2bf2000-12-13 01:02:46 +0000189 and 0 < incr < ma_size and both are a function of hash.
190 i is the initial table index and incr the initial probe offset. */
Tim Peters2f228e72001-05-13 00:19:31 +0000191 i = hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000192 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000193 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000194 return ep;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000195 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000196 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000197 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000198 if (ep->me_hash == hash) {
199 /* error can't have been checked yet */
200 checked_error = 1;
201 if (PyErr_Occurred()) {
202 restore_error = 1;
203 PyErr_Fetch(&err_type, &err_value, &err_tb);
204 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000205 cmp = PyObject_RichCompareBool(ep->me_key, key, Py_EQ);
206 if (cmp > 0) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000207 if (restore_error)
208 PyErr_Restore(err_type, err_value,
209 err_tb);
210 return ep;
211 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000212 else if (cmp < 0)
213 PyErr_Clear();
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000214 }
215 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000216 }
Guido van Rossum2bc13791999-03-24 19:06:42 +0000217 /* Derive incr from hash, just to make it more arbitrary. Note that
Guido van Rossum16e93a81997-01-28 00:00:11 +0000218 incr must not be 0, or we will get into an infinite loop.*/
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000219 incr = (hash ^ ((unsigned long)hash >> 3)) & mask;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000220 if (!incr)
221 incr = mask;
Tim Peters342c65e2001-05-13 06:43:53 +0000222 /* In the loop, me_key == dummy is by far (factor of 100s) the
223 least likely outcome, so test for that last. */
Guido van Rossum16e93a81997-01-28 00:00:11 +0000224 for (;;) {
Guido van Rossumefb46091997-01-29 15:53:56 +0000225 ep = &ep0[(i+incr)&mask];
Guido van Rossum16e93a81997-01-28 00:00:11 +0000226 if (ep->me_key == NULL) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000227 if (restore_error)
228 PyErr_Restore(err_type, err_value, err_tb);
Tim Peters342c65e2001-05-13 06:43:53 +0000229 return freeslot == NULL ? ep : freeslot;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000230 }
Tim Peters342c65e2001-05-13 06:43:53 +0000231 if (ep->me_key == key) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000232 if (restore_error)
233 PyErr_Restore(err_type, err_value, err_tb);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000234 return ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000235 }
Tim Peters342c65e2001-05-13 06:43:53 +0000236 else if (ep->me_hash == hash && ep->me_key != dummy) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000237 if (!checked_error) {
238 checked_error = 1;
239 if (PyErr_Occurred()) {
240 restore_error = 1;
241 PyErr_Fetch(&err_type, &err_value,
242 &err_tb);
243 }
244 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000245 cmp = PyObject_RichCompareBool(ep->me_key, key, Py_EQ);
246 if (cmp > 0) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000247 if (restore_error)
248 PyErr_Restore(err_type, err_value,
249 err_tb);
250 return ep;
251 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000252 else if (cmp < 0)
253 PyErr_Clear();
Guido van Rossum16e93a81997-01-28 00:00:11 +0000254 }
Tim Peters342c65e2001-05-13 06:43:53 +0000255 else if (ep->me_key == dummy && freeslot == NULL)
256 freeslot = ep;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000257 /* Cycle through GF(2^n)-{0} */
Tim Peters342c65e2001-05-13 06:43:53 +0000258 incr <<= 1;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000259 if (incr > mask)
Tim Peters342c65e2001-05-13 06:43:53 +0000260 incr ^= mp->ma_poly; /* clears the highest bit */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000261 }
262}
263
264/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000265 * Hacked up version of lookdict which can assume keys are always strings;
266 * this assumption allows testing for errors during PyObject_Compare() to
267 * be dropped; string-string comparisons never raise exceptions. This also
268 * means we don't need to go through PyObject_Compare(); we can always use
269 * the tp_compare slot of the string type object directly.
270 *
271 * This really only becomes meaningful if proper error handling in lookdict()
272 * is too expensive.
273 */
274static dictentry *
275lookdict_string(dictobject *mp, PyObject *key, register long hash)
276{
277 register int i;
278 register unsigned incr;
279 register dictentry *freeslot;
280 register unsigned int mask = mp->ma_size-1;
281 dictentry *ep0 = mp->ma_table;
282 register dictentry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000283 cmpfunc compare = PyString_Type.tp_compare;
Fred Drake1bff34a2000-08-31 19:31:38 +0000284
285 /* make sure this function doesn't have to handle non-string keys */
286 if (!PyString_Check(key)) {
287#ifdef SHOW_CONVERSION_COUNTS
288 ++converted;
289#endif
290 mp->ma_lookup = lookdict;
291 return lookdict(mp, key, hash);
292 }
293 /* We must come up with (i, incr) such that 0 <= i < ma_size
294 and 0 < incr < ma_size and both are a function of hash */
Tim Peters2f228e72001-05-13 00:19:31 +0000295 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000296 ep = &ep0[i];
297 if (ep->me_key == NULL || ep->me_key == key)
298 return ep;
299 if (ep->me_key == dummy)
300 freeslot = ep;
301 else {
302 if (ep->me_hash == hash
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000303 && compare(ep->me_key, key) == 0) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000304 return ep;
305 }
306 freeslot = NULL;
307 }
308 /* Derive incr from hash, just to make it more arbitrary. Note that
309 incr must not be 0, or we will get into an infinite loop.*/
310 incr = (hash ^ ((unsigned long)hash >> 3)) & mask;
311 if (!incr)
312 incr = mask;
Tim Peters342c65e2001-05-13 06:43:53 +0000313 /* In the loop, me_key == dummy is by far (factor of 100s) the
314 least likely outcome, so test for that last. */
Fred Drake1bff34a2000-08-31 19:31:38 +0000315 for (;;) {
316 ep = &ep0[(i+incr)&mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000317 if (ep->me_key == NULL)
318 return freeslot == NULL ? ep : freeslot;
319 if (ep->me_key == key
320 || (ep->me_hash == hash
321 && ep->me_key != dummy
322 && compare(ep->me_key, key) == 0))
Fred Drake1bff34a2000-08-31 19:31:38 +0000323 return ep;
Tim Peters342c65e2001-05-13 06:43:53 +0000324 else if (ep->me_key == dummy && freeslot == NULL)
325 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000326 /* Cycle through GF(2^n)-{0} */
Tim Peters342c65e2001-05-13 06:43:53 +0000327 incr <<= 1;
Fred Drake1bff34a2000-08-31 19:31:38 +0000328 if (incr > mask)
Tim Peters342c65e2001-05-13 06:43:53 +0000329 incr ^= mp->ma_poly; /* clears the highest bit */
Fred Drake1bff34a2000-08-31 19:31:38 +0000330 }
331}
332
333/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000334Internal routine to insert a new item into the table.
335Used both by the internal resize routine and by the public insert routine.
336Eats a reference to key and one to value.
337*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000338static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000339insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000340{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000341 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000342 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000343 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000344 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000345 old_value = ep->me_value;
346 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000347 Py_DECREF(old_value); /* which **CAN** re-enter */
348 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000349 }
350 else {
351 if (ep->me_key == NULL)
352 mp->ma_fill++;
353 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000354 Py_DECREF(ep->me_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000355 ep->me_key = key;
356 ep->me_hash = hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000357 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000358 mp->ma_used++;
359 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000360}
361
362/*
363Restructure the table by allocating a new table and reinserting all
364items again. When entries have been deleted, the new table may
365actually be smaller than the old one.
366*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000367static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000368dictresize(dictobject *mp, int minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000369{
370 register int oldsize = mp->ma_size;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000371 register int newsize, newpoly;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000372 register dictentry *oldtable = mp->ma_table;
373 register dictentry *newtable;
374 register dictentry *ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000375 register int i;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000376 for (i = 0, newsize = MINSIZE; ; i++, newsize <<= 1) {
377 if (i > sizeof(polys)/sizeof(polys[0])) {
378 /* Ran out of polynomials */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000379 PyErr_NoMemory();
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000380 return -1;
381 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000382 if (newsize > minused) {
Guido van Rossum16e93a81997-01-28 00:00:11 +0000383 newpoly = polys[i];
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000384 break;
385 }
386 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000387 newtable = PyMem_NEW(dictentry, newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000388 if (newtable == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000389 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000390 return -1;
391 }
Guido van Rossum0fd00331998-07-16 15:06:13 +0000392 memset(newtable, '\0', sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000393 mp->ma_size = newsize;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000394 mp->ma_poly = newpoly;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000395 mp->ma_table = newtable;
396 mp->ma_fill = 0;
397 mp->ma_used = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000398
399 /* Make two passes, so we can avoid decrefs
400 (and possible side effects) till the table is copied */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000401 for (i = 0, ep = oldtable; i < oldsize; i++, ep++) {
402 if (ep->me_value != NULL)
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000403 insertdict(mp,ep->me_key,ep->me_hash,ep->me_value);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000404 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000405 for (i = 0, ep = oldtable; i < oldsize; i++, ep++) {
Guido van Rossum255443b1998-04-10 22:47:14 +0000406 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000407 Py_XDECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000408 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000409 }
410
Guido van Rossumb18618d2000-05-03 23:44:39 +0000411 if (oldtable != NULL)
412 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000413 return 0;
414}
415
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000416PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000417PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000418{
419 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000420 dictobject *mp = (dictobject *)op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000421 if (!PyDict_Check(op)) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000422 return NULL;
423 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000424 if (mp->ma_table == NULL)
Guido van Rossumd7047b31995-01-02 19:07:15 +0000425 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000426#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000427 if (!PyString_Check(key) ||
428 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000429#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000430 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000431 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000432 if (hash == -1) {
433 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000434 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000435 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000436 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000437 return (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000438}
439
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000440/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
441 * dictionary if it is merely replacing the value for an existing key.
442 * This is means that it's safe to loop over a dictionary with
443 * PyDict_Next() and occasionally replace a value -- but you can't
444 * insert new keys or remove them.
445 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000446int
Tim Peters1f5871e2000-07-04 17:44:48 +0000447PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000448{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000449 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000450 register long hash;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000451 register int n_used;
452
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000453 if (!PyDict_Check(op)) {
454 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000455 return -1;
456 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000457 mp = (dictobject *)op;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000458#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000459 if (PyString_Check(key)) {
Guido van Rossum2a61e741997-01-18 07:55:05 +0000460#ifdef INTERN_STRINGS
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000461 if (((PyStringObject *)key)->ob_sinterned != NULL) {
462 key = ((PyStringObject *)key)->ob_sinterned;
463 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000464 }
465 else
466#endif
467 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000468 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000469 if (hash == -1)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000470 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000471 }
472 }
473 else
474#endif
475 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000476 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000477 if (hash == -1)
478 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000479 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000480 if (mp->ma_fill >= mp->ma_size) {
481 /* No room for a new key.
482 * This only happens when the dict is empty.
483 * Let dictresize() create a minimal dict.
484 */
485 assert(mp->ma_used == 0);
486 if (dictresize(mp, 0) != 0)
487 return -1;
488 assert(mp->ma_fill < mp->ma_size);
489 }
490 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000491 Py_INCREF(value);
492 Py_INCREF(key);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000493 insertdict(mp, key, hash, value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000494 /* If we added a key, we can safely resize. Otherwise skip this!
495 * If fill >= 2/3 size, adjust size. Normally, this doubles the
496 * size, but it's also possible for the dict to shrink (if ma_fill is
497 * much larger than ma_used, meaning a lot of dict keys have been
498 * deleted).
499 */
500 if (mp->ma_used > n_used && mp->ma_fill*3 >= mp->ma_size*2) {
501 if (dictresize(mp, mp->ma_used*2) != 0)
502 return -1;
503 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000504 return 0;
505}
506
507int
Tim Peters1f5871e2000-07-04 17:44:48 +0000508PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000509{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000510 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000511 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000512 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000513 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000514
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000515 if (!PyDict_Check(op)) {
516 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000517 return -1;
518 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000519#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000520 if (!PyString_Check(key) ||
521 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000522#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000523 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000524 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000525 if (hash == -1)
526 return -1;
527 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000528 mp = (dictobject *)op;
529 if (((dictobject *)op)->ma_table == NULL)
Guido van Rossumefc87131995-01-02 19:42:39 +0000530 goto empty;
Fred Drake1bff34a2000-08-31 19:31:38 +0000531 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000532 if (ep->me_value == NULL) {
Guido van Rossumefc87131995-01-02 19:42:39 +0000533 empty:
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000534 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000535 return -1;
536 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000537 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000538 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000539 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000540 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000541 ep->me_value = NULL;
542 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000543 Py_DECREF(old_value);
544 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000545 return 0;
546}
547
Guido van Rossum25831651993-05-19 14:50:45 +0000548void
Tim Peters1f5871e2000-07-04 17:44:48 +0000549PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000550{
Guido van Rossumd7047b31995-01-02 19:07:15 +0000551 int i, n;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000552 register dictentry *table;
553 dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000554 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000555 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000556 mp = (dictobject *)op;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000557 table = mp->ma_table;
558 if (table == NULL)
559 return;
560 n = mp->ma_size;
561 mp->ma_size = mp->ma_used = mp->ma_fill = 0;
562 mp->ma_table = NULL;
563 for (i = 0; i < n; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000564 Py_XDECREF(table[i].me_key);
565 Py_XDECREF(table[i].me_value);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000566 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000567 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000568}
569
Tim Peters67830702001-03-21 19:23:56 +0000570/* CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
571 * mutates the dict. One exception: it is safe if the loop merely changes
572 * the values associated with the keys (but doesn't insert new keys or
573 * delete keys), via PyDict_SetItem().
574 */
Guido van Rossum25831651993-05-19 14:50:45 +0000575int
Tim Peters1f5871e2000-07-04 17:44:48 +0000576PyDict_Next(PyObject *op, int *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000577{
Guido van Rossum25831651993-05-19 14:50:45 +0000578 int i;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000579 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000580 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000581 return 0;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000582 mp = (dictobject *)op;
Guido van Rossum25831651993-05-19 14:50:45 +0000583 i = *ppos;
584 if (i < 0)
585 return 0;
586 while (i < mp->ma_size && mp->ma_table[i].me_value == NULL)
587 i++;
588 *ppos = i+1;
589 if (i >= mp->ma_size)
590 return 0;
591 if (pkey)
592 *pkey = mp->ma_table[i].me_key;
593 if (pvalue)
594 *pvalue = mp->ma_table[i].me_value;
595 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000596}
597
598/* Methods */
599
600static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000601dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000602{
603 register int i;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000604 register dictentry *ep;
Guido van Rossumd724b232000-03-13 16:01:29 +0000605 Py_TRASHCAN_SAFE_BEGIN(mp)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000606 PyObject_GC_Fini(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000607 for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) {
Guido van Rossum255443b1998-04-10 22:47:14 +0000608 if (ep->me_key != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000609 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000610 }
611 if (ep->me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000612 Py_DECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000613 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000614 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000615 if (mp->ma_table != NULL)
616 PyMem_DEL(mp->ma_table);
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000617 mp = (dictobject *) PyObject_AS_GC(mp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000618 PyObject_DEL(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000619 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000620}
621
622static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000623dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000624{
625 register int i;
626 register int any;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000627 register dictentry *ep;
Guido van Rossum255443b1998-04-10 22:47:14 +0000628
629 i = Py_ReprEnter((PyObject*)mp);
630 if (i != 0) {
631 if (i < 0)
632 return i;
633 fprintf(fp, "{...}");
634 return 0;
635 }
636
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000637 fprintf(fp, "{");
638 any = 0;
639 for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) {
640 if (ep->me_value != NULL) {
641 if (any++ > 0)
642 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000643 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
644 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000645 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000646 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000647 fprintf(fp, ": ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000648 if (PyObject_Print(ep->me_value, fp, 0) != 0) {
649 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000650 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000651 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000652 }
653 }
654 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000655 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000656 return 0;
657}
658
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000659static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000660dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000661{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000662 auto PyObject *v;
663 PyObject *sepa, *colon;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000664 register int i;
665 register int any;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000666 register dictentry *ep;
Guido van Rossum255443b1998-04-10 22:47:14 +0000667
668 i = Py_ReprEnter((PyObject*)mp);
669 if (i != 0) {
670 if (i > 0)
671 return PyString_FromString("{...}");
672 return NULL;
673 }
674
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000675 v = PyString_FromString("{");
676 sepa = PyString_FromString(", ");
677 colon = PyString_FromString(": ");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000678 any = 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000679 for (i = 0, ep = mp->ma_table; i < mp->ma_size && v; i++, ep++) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000680 if (ep->me_value != NULL) {
681 if (any++)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000682 PyString_Concat(&v, sepa);
683 PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_key));
684 PyString_Concat(&v, colon);
685 PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_value));
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000686 }
687 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000688 PyString_ConcatAndDel(&v, PyString_FromString("}"));
Guido van Rossum255443b1998-04-10 22:47:14 +0000689 Py_ReprLeave((PyObject*)mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000690 Py_XDECREF(sepa);
691 Py_XDECREF(colon);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000692 return v;
693}
694
695static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000696dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000697{
698 return mp->ma_used;
699}
700
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000701static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000702dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000703{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000704 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000705 long hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000706 if (mp->ma_table == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000707 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossumd7047b31995-01-02 19:07:15 +0000708 return NULL;
709 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000710#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000711 if (!PyString_Check(key) ||
712 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000713#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000714 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000715 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000716 if (hash == -1)
717 return NULL;
718 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000719 v = (mp->ma_lookup)(mp, key, hash) -> me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000720 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000721 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000722 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000723 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000724 return v;
725}
726
727static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000728dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000729{
730 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000731 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000732 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000733 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000734}
735
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000736static PyMappingMethods dict_as_mapping = {
737 (inquiry)dict_length, /*mp_length*/
738 (binaryfunc)dict_subscript, /*mp_subscript*/
739 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000740};
741
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000742static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000743dict_keys(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000744{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000745 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000746 register int i, j, n;
747
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000748 if (!PyArg_NoArgs(args))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000749 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000750 again:
751 n = mp->ma_used;
752 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000753 if (v == NULL)
754 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000755 if (n != mp->ma_used) {
756 /* Durnit. The allocations caused the dict to resize.
757 * Just start over, this shouldn't normally happen.
758 */
759 Py_DECREF(v);
760 goto again;
761 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000762 for (i = 0, j = 0; i < mp->ma_size; i++) {
763 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000764 PyObject *key = mp->ma_table[i].me_key;
765 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000766 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000767 j++;
768 }
769 }
770 return v;
771}
772
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000773static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000774dict_values(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000775{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000776 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000777 register int i, j, n;
778
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000779 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000780 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000781 again:
782 n = mp->ma_used;
783 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000784 if (v == NULL)
785 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000786 if (n != mp->ma_used) {
787 /* Durnit. The allocations caused the dict to resize.
788 * Just start over, this shouldn't normally happen.
789 */
790 Py_DECREF(v);
791 goto again;
792 }
Guido van Rossum25831651993-05-19 14:50:45 +0000793 for (i = 0, j = 0; i < mp->ma_size; i++) {
794 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000795 PyObject *value = mp->ma_table[i].me_value;
796 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000797 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000798 j++;
799 }
800 }
801 return v;
802}
803
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000804static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000805dict_items(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000806{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000807 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000808 register int i, j, n;
809 PyObject *item, *key, *value;
810
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000811 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000812 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000813 /* Preallocate the list of tuples, to avoid allocations during
814 * the loop over the items, which could trigger GC, which
815 * could resize the dict. :-(
816 */
817 again:
818 n = mp->ma_used;
819 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000820 if (v == NULL)
821 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000822 for (i = 0; i < n; i++) {
823 item = PyTuple_New(2);
824 if (item == NULL) {
825 Py_DECREF(v);
826 return NULL;
827 }
828 PyList_SET_ITEM(v, i, item);
829 }
830 if (n != mp->ma_used) {
831 /* Durnit. The allocations caused the dict to resize.
832 * Just start over, this shouldn't normally happen.
833 */
834 Py_DECREF(v);
835 goto again;
836 }
837 /* Nothing we do below makes any function calls. */
Guido van Rossum25831651993-05-19 14:50:45 +0000838 for (i = 0, j = 0; i < mp->ma_size; i++) {
839 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000840 key = mp->ma_table[i].me_key;
841 value = mp->ma_table[i].me_value;
842 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000843 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000844 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000845 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000846 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000847 j++;
848 }
849 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000850 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +0000851 return v;
852}
853
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000854static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000855dict_update(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000856{
857 register int i;
858 dictobject *other;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000859 dictentry *entry;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000860 if (!PyArg_Parse(args, "O!", &PyDict_Type, &other))
861 return NULL;
Jeremy Hylton1fb60882001-01-03 22:34:59 +0000862 if (other == mp || other->ma_used == 0)
863 goto done; /* a.update(a) or a.update({}); nothing to do */
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000864 /* Do one big resize at the start, rather than incrementally
865 resizing as we insert new items. Expect that there will be
866 no (or few) overlapping keys. */
867 if ((mp->ma_fill + other->ma_used)*3 >= mp->ma_size*2) {
868 if (dictresize(mp, (mp->ma_used + other->ma_used)*3/2) != 0)
869 return NULL;
870 }
871 for (i = 0; i < other->ma_size; i++) {
872 entry = &other->ma_table[i];
873 if (entry->me_value != NULL) {
874 Py_INCREF(entry->me_key);
875 Py_INCREF(entry->me_value);
876 insertdict(mp, entry->me_key, entry->me_hash,
877 entry->me_value);
878 }
879 }
880 done:
881 Py_INCREF(Py_None);
882 return Py_None;
883}
884
885static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000886dict_copy(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000887{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000888 if (!PyArg_Parse(args, ""))
889 return NULL;
890 return PyDict_Copy((PyObject*)mp);
891}
892
893PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000894PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000895{
896 register dictobject *mp;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000897 register int i;
898 dictobject *copy;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000899 dictentry *entry;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000900
901 if (o == NULL || !PyDict_Check(o)) {
902 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000903 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000904 }
905 mp = (dictobject *)o;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000906 copy = (dictobject *)PyDict_New();
907 if (copy == NULL)
908 return NULL;
909 if (mp->ma_used > 0) {
910 if (dictresize(copy, mp->ma_used*3/2) != 0)
911 return NULL;
912 for (i = 0; i < mp->ma_size; i++) {
913 entry = &mp->ma_table[i];
914 if (entry->me_value != NULL) {
915 Py_INCREF(entry->me_key);
916 Py_INCREF(entry->me_value);
917 insertdict(copy, entry->me_key, entry->me_hash,
918 entry->me_value);
919 }
920 }
921 }
922 return (PyObject *)copy;
923}
924
Guido van Rossum4199fac1993-11-05 10:18:44 +0000925int
Tim Peters1f5871e2000-07-04 17:44:48 +0000926PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +0000927{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000928 if (mp == NULL || !PyDict_Check(mp)) {
929 PyErr_BadInternalCall();
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000930 return 0;
Guido van Rossum4199fac1993-11-05 10:18:44 +0000931 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000932 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +0000933}
934
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000935PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000936PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000937{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000938 if (mp == NULL || !PyDict_Check(mp)) {
939 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000940 return NULL;
941 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000942 return dict_keys((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000943}
944
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000945PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000946PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +0000947{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000948 if (mp == NULL || !PyDict_Check(mp)) {
949 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +0000950 return NULL;
951 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000952 return dict_values((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +0000953}
954
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000955PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000956PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +0000957{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000958 if (mp == NULL || !PyDict_Check(mp)) {
959 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +0000960 return NULL;
961 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000962 return dict_items((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +0000963}
964
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000965/* Subroutine which returns the smallest key in a for which b's value
966 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +0000967 pval argument. Both are NULL if no key in a is found for which b's status
968 differs. The refcounts on (and only on) non-NULL *pval and function return
969 values must be decremented by the caller (characterize() increments them
970 to ensure that mutating comparison and PyDict_GetItem calls can't delete
971 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000972
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000973static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000974characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000975{
Tim Peters95bf9392001-05-10 08:32:44 +0000976 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
977 PyObject *aval = NULL; /* a[akey] */
Guido van Rossumb9324202001-01-18 00:39:02 +0000978 int i, cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000979
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000980 for (i = 0; i < a->ma_size; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +0000981 PyObject *thiskey, *thisaval, *thisbval;
982 if (a->ma_table[i].me_value == NULL)
983 continue;
984 thiskey = a->ma_table[i].me_key;
985 Py_INCREF(thiskey); /* keep alive across compares */
986 if (akey != NULL) {
987 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
988 if (cmp < 0) {
989 Py_DECREF(thiskey);
990 goto Fail;
991 }
992 if (cmp > 0 ||
993 i >= a->ma_size ||
994 a->ma_table[i].me_value == NULL)
995 {
996 /* Not the *smallest* a key; or maybe it is
997 * but the compare shrunk the dict so we can't
998 * find its associated value anymore; or
999 * maybe it is but the compare deleted the
1000 * a[thiskey] entry.
1001 */
1002 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001003 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001004 }
Tim Peters95bf9392001-05-10 08:32:44 +00001005 }
1006
1007 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1008 thisaval = a->ma_table[i].me_value;
1009 assert(thisaval);
1010 Py_INCREF(thisaval); /* keep alive */
1011 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1012 if (thisbval == NULL)
1013 cmp = 0;
1014 else {
1015 /* both dicts have thiskey: same values? */
1016 cmp = PyObject_RichCompareBool(
1017 thisaval, thisbval, Py_EQ);
1018 if (cmp < 0) {
1019 Py_DECREF(thiskey);
1020 Py_DECREF(thisaval);
1021 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001022 }
1023 }
Tim Peters95bf9392001-05-10 08:32:44 +00001024 if (cmp == 0) {
1025 /* New winner. */
1026 Py_XDECREF(akey);
1027 Py_XDECREF(aval);
1028 akey = thiskey;
1029 aval = thisaval;
1030 }
1031 else {
1032 Py_DECREF(thiskey);
1033 Py_DECREF(thisaval);
1034 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001035 }
Tim Peters95bf9392001-05-10 08:32:44 +00001036 *pval = aval;
1037 return akey;
1038
1039Fail:
1040 Py_XDECREF(akey);
1041 Py_XDECREF(aval);
1042 *pval = NULL;
1043 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001044}
1045
1046static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001047dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001048{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001049 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001050 int res;
1051
1052 /* Compare lengths first */
1053 if (a->ma_used < b->ma_used)
1054 return -1; /* a is shorter */
1055 else if (a->ma_used > b->ma_used)
1056 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001057
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001058 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001059 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001060 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001061 if (adiff == NULL) {
1062 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001063 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001064 * must be equal.
1065 */
1066 res = PyErr_Occurred() ? -1 : 0;
1067 goto Finished;
1068 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001069 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001070 if (bdiff == NULL && PyErr_Occurred()) {
1071 assert(!bval);
1072 res = -1;
1073 goto Finished;
1074 }
1075 res = 0;
1076 if (bdiff) {
1077 /* bdiff == NULL "should be" impossible now, but perhaps
1078 * the last comparison done by the characterize() on a had
1079 * the side effect of making the dicts equal!
1080 */
1081 res = PyObject_Compare(adiff, bdiff);
1082 }
1083 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001084 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001085
1086Finished:
1087 Py_XDECREF(adiff);
1088 Py_XDECREF(bdiff);
1089 Py_XDECREF(aval);
1090 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001091 return res;
1092}
1093
Tim Peterse63415e2001-05-08 04:38:29 +00001094/* Return 1 if dicts equal, 0 if not, -1 if error.
1095 * Gets out as soon as any difference is detected.
1096 * Uses only Py_EQ comparison.
1097 */
1098static int
1099dict_equal(dictobject *a, dictobject *b)
1100{
1101 int i;
1102
1103 if (a->ma_used != b->ma_used)
1104 /* can't be equal if # of entries differ */
1105 return 0;
1106
1107 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1108 for (i = 0; i < a->ma_size; i++) {
1109 PyObject *aval = a->ma_table[i].me_value;
1110 if (aval != NULL) {
1111 int cmp;
1112 PyObject *bval;
1113 PyObject *key = a->ma_table[i].me_key;
1114 /* temporarily bump aval's refcount to ensure it stays
1115 alive until we're done with it */
1116 Py_INCREF(aval);
1117 bval = PyDict_GetItem((PyObject *)b, key);
1118 if (bval == NULL) {
1119 Py_DECREF(aval);
1120 return 0;
1121 }
1122 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1123 Py_DECREF(aval);
1124 if (cmp <= 0) /* error or not equal */
1125 return cmp;
1126 }
1127 }
1128 return 1;
1129 }
1130
1131static PyObject *
1132dict_richcompare(PyObject *v, PyObject *w, int op)
1133{
1134 int cmp;
1135 PyObject *res;
1136
1137 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1138 res = Py_NotImplemented;
1139 }
1140 else if (op == Py_EQ || op == Py_NE) {
1141 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1142 if (cmp < 0)
1143 return NULL;
1144 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1145 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001146 else
1147 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001148 Py_INCREF(res);
1149 return res;
1150 }
1151
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001152static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001153dict_has_key(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001154{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001155 PyObject *key;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001156 long hash;
1157 register long ok;
Fred Drake52fccfd2000-02-23 15:47:16 +00001158 if (!PyArg_ParseTuple(args, "O:has_key", &key))
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001159 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001160#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001161 if (!PyString_Check(key) ||
1162 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001163#endif
Guido van Rossumca756f21997-01-23 19:39:29 +00001164 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001165 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001166 if (hash == -1)
1167 return NULL;
1168 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001169 ok = (mp->ma_size != 0
1170 && (mp->ma_lookup)(mp, key, hash)->me_value != NULL);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001171 return PyInt_FromLong(ok);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001172}
1173
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001174static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001175dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001176{
1177 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001178 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001179 PyObject *val = NULL;
1180 long hash;
1181
Fred Drake52fccfd2000-02-23 15:47:16 +00001182 if (!PyArg_ParseTuple(args, "O|O:get", &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001183 return NULL;
Guido van Rossum6fcfa721997-10-20 20:10:00 +00001184 if (mp->ma_table == NULL)
1185 goto finally;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001186
Barry Warsawc38c5da1997-10-06 17:49:20 +00001187#ifdef CACHE_HASH
1188 if (!PyString_Check(key) ||
1189 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1190#endif
1191 {
1192 hash = PyObject_Hash(key);
1193 if (hash == -1)
1194 return NULL;
1195 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001196 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Barry Warsaw320ac331997-10-20 17:26:25 +00001197
Guido van Rossum6fcfa721997-10-20 20:10:00 +00001198 finally:
Barry Warsawc38c5da1997-10-06 17:49:20 +00001199 if (val == NULL)
1200 val = failobj;
1201 Py_INCREF(val);
1202 return val;
1203}
1204
1205
1206static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001207dict_setdefault(register dictobject *mp, PyObject *args)
1208{
1209 PyObject *key;
1210 PyObject *failobj = Py_None;
1211 PyObject *val = NULL;
1212 long hash;
1213
1214 if (!PyArg_ParseTuple(args, "O|O:setdefault", &key, &failobj))
1215 return NULL;
1216 if (mp->ma_table == NULL)
1217 goto finally;
1218
1219#ifdef CACHE_HASH
1220 if (!PyString_Check(key) ||
1221 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1222#endif
1223 {
1224 hash = PyObject_Hash(key);
1225 if (hash == -1)
1226 return NULL;
1227 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001228 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001229
1230 finally:
1231 if (val == NULL) {
1232 val = failobj;
1233 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1234 val = NULL;
1235 }
1236 Py_XINCREF(val);
1237 return val;
1238}
1239
1240
1241static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001242dict_clear(register dictobject *mp, PyObject *args)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001243{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001244 if (!PyArg_NoArgs(args))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001245 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001246 PyDict_Clear((PyObject *)mp);
1247 Py_INCREF(Py_None);
1248 return Py_None;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001249}
1250
Guido van Rossumba6ab842000-12-12 22:02:18 +00001251static PyObject *
1252dict_popitem(dictobject *mp, PyObject *args)
1253{
1254 int i = 0;
1255 dictentry *ep;
1256 PyObject *res;
1257
1258 if (!PyArg_NoArgs(args))
1259 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001260 /* Allocate the result tuple first. Believe it or not,
1261 * this allocation could trigger a garbage collection which
1262 * could resize the dict, which would invalidate the pointer
1263 * (ep) into the dict calculated below.
1264 * So we have to do this first.
1265 */
1266 res = PyTuple_New(2);
1267 if (res == NULL)
1268 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001269 if (mp->ma_used == 0) {
1270 Py_DECREF(res);
1271 PyErr_SetString(PyExc_KeyError,
1272 "popitem(): dictionary is empty");
1273 return NULL;
1274 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001275 /* Set ep to "the first" dict entry with a value. We abuse the hash
1276 * field of slot 0 to hold a search finger:
1277 * If slot 0 has a value, use slot 0.
1278 * Else slot 0 is being used to hold a search finger,
1279 * and we use its hash value as the first index to look.
1280 */
1281 ep = &mp->ma_table[0];
1282 if (ep->me_value == NULL) {
1283 i = (int)ep->me_hash;
1284 /* The hash field may be uninitialized trash, or it
1285 * may be a real hash value, or it may be a legit
1286 * search finger, or it may be a once-legit search
1287 * finger that's out of bounds now because it
1288 * wrapped around or the table shrunk -- simply
1289 * make sure it's in bounds now.
1290 */
1291 if (i >= mp->ma_size || i < 1)
1292 i = 1; /* skip slot 0 */
1293 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1294 i++;
1295 if (i >= mp->ma_size)
1296 i = 1;
1297 }
1298 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001299 PyTuple_SET_ITEM(res, 0, ep->me_key);
1300 PyTuple_SET_ITEM(res, 1, ep->me_value);
1301 Py_INCREF(dummy);
1302 ep->me_key = dummy;
1303 ep->me_value = NULL;
1304 mp->ma_used--;
1305 assert(mp->ma_table[0].me_value == NULL);
1306 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001307 return res;
1308}
1309
Jeremy Hylton8caad492000-06-23 14:18:11 +00001310static int
1311dict_traverse(PyObject *op, visitproc visit, void *arg)
1312{
1313 int i = 0, err;
1314 PyObject *pk;
1315 PyObject *pv;
1316
1317 while (PyDict_Next(op, &i, &pk, &pv)) {
1318 err = visit(pk, arg);
1319 if (err)
1320 return err;
1321 err = visit(pv, arg);
1322 if (err)
1323 return err;
1324 }
1325 return 0;
1326}
1327
1328static int
1329dict_tp_clear(PyObject *op)
1330{
1331 PyDict_Clear(op);
1332 return 0;
1333}
1334
Tim Petersf7f88b12000-12-13 23:18:45 +00001335
Guido van Rossum09e563a2001-05-01 12:10:21 +00001336staticforward PyObject *dictiter_new(dictobject *, binaryfunc);
1337
1338static PyObject *
1339select_key(PyObject *key, PyObject *value)
1340{
1341 Py_INCREF(key);
1342 return key;
1343}
1344
1345static PyObject *
1346select_value(PyObject *key, PyObject *value)
1347{
1348 Py_INCREF(value);
1349 return value;
1350}
1351
1352static PyObject *
1353select_item(PyObject *key, PyObject *value)
1354{
1355 PyObject *res = PyTuple_New(2);
1356
1357 if (res != NULL) {
1358 Py_INCREF(key);
1359 Py_INCREF(value);
1360 PyTuple_SET_ITEM(res, 0, key);
1361 PyTuple_SET_ITEM(res, 1, value);
1362 }
1363 return res;
1364}
1365
1366static PyObject *
1367dict_iterkeys(dictobject *dict, PyObject *args)
1368{
1369 if (!PyArg_ParseTuple(args, ""))
1370 return NULL;
1371 return dictiter_new(dict, select_key);
1372}
1373
1374static PyObject *
1375dict_itervalues(dictobject *dict, PyObject *args)
1376{
1377 if (!PyArg_ParseTuple(args, ""))
1378 return NULL;
1379 return dictiter_new(dict, select_value);
1380}
1381
1382static PyObject *
1383dict_iteritems(dictobject *dict, PyObject *args)
1384{
1385 if (!PyArg_ParseTuple(args, ""))
1386 return NULL;
1387 return dictiter_new(dict, select_item);
1388}
1389
1390
Tim Petersf7f88b12000-12-13 23:18:45 +00001391static char has_key__doc__[] =
1392"D.has_key(k) -> 1 if D has a key k, else 0";
1393
1394static char get__doc__[] =
1395"D.get(k[,d]) -> D[k] if D.has_key(k), else d. d defaults to None.";
1396
1397static char setdefault_doc__[] =
1398"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if not D.has_key(k)";
1399
1400static char popitem__doc__[] =
1401"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
14022-tuple; but raise KeyError if D is empty";
1403
1404static char keys__doc__[] =
1405"D.keys() -> list of D's keys";
1406
1407static char items__doc__[] =
1408"D.items() -> list of D's (key, value) pairs, as 2-tuples";
1409
1410static char values__doc__[] =
1411"D.values() -> list of D's values";
1412
1413static char update__doc__[] =
1414"D.update(E) -> None. Update D from E: for k in E.keys(): D[k] = E[k]";
1415
1416static char clear__doc__[] =
1417"D.clear() -> None. Remove all items from D.";
1418
1419static char copy__doc__[] =
1420"D.copy() -> a shallow copy of D";
1421
Guido van Rossum09e563a2001-05-01 12:10:21 +00001422static char iterkeys__doc__[] =
1423"D.iterkeys() -> an iterator over the keys of D";
1424
1425static char itervalues__doc__[] =
1426"D.itervalues() -> an iterator over the values of D";
1427
1428static char iteritems__doc__[] =
1429"D.iteritems() -> an iterator over the (key, value) items of D";
1430
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001431static PyMethodDef mapp_methods[] = {
Tim Petersf7f88b12000-12-13 23:18:45 +00001432 {"has_key", (PyCFunction)dict_has_key, METH_VARARGS,
1433 has_key__doc__},
1434 {"get", (PyCFunction)dict_get, METH_VARARGS,
1435 get__doc__},
1436 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1437 setdefault_doc__},
1438 {"popitem", (PyCFunction)dict_popitem, METH_OLDARGS,
1439 popitem__doc__},
1440 {"keys", (PyCFunction)dict_keys, METH_OLDARGS,
1441 keys__doc__},
1442 {"items", (PyCFunction)dict_items, METH_OLDARGS,
1443 items__doc__},
1444 {"values", (PyCFunction)dict_values, METH_OLDARGS,
1445 values__doc__},
1446 {"update", (PyCFunction)dict_update, METH_OLDARGS,
1447 update__doc__},
1448 {"clear", (PyCFunction)dict_clear, METH_OLDARGS,
1449 clear__doc__},
1450 {"copy", (PyCFunction)dict_copy, METH_OLDARGS,
1451 copy__doc__},
Guido van Rossum09e563a2001-05-01 12:10:21 +00001452 {"iterkeys", (PyCFunction)dict_iterkeys, METH_VARARGS,
1453 iterkeys__doc__},
1454 {"itervalues", (PyCFunction)dict_itervalues, METH_VARARGS,
1455 itervalues__doc__},
1456 {"iteritems", (PyCFunction)dict_iteritems, METH_VARARGS,
1457 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001458 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001459};
1460
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001461static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001462dict_getattr(dictobject *mp, char *name)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001463{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001464 return Py_FindMethod(mapp_methods, (PyObject *)mp, name);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001465}
1466
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001467static int
1468dict_contains(dictobject *mp, PyObject *key)
1469{
1470 long hash;
1471
1472#ifdef CACHE_HASH
1473 if (!PyString_Check(key) ||
1474 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1475#endif
1476 {
1477 hash = PyObject_Hash(key);
1478 if (hash == -1)
1479 return -1;
1480 }
1481 return (mp->ma_size != 0
1482 && (mp->ma_lookup)(mp, key, hash)->me_value != NULL);
1483}
1484
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001485/* Hack to implement "key in dict" */
1486static PySequenceMethods dict_as_sequence = {
1487 0, /* sq_length */
1488 0, /* sq_concat */
1489 0, /* sq_repeat */
1490 0, /* sq_item */
1491 0, /* sq_slice */
1492 0, /* sq_ass_item */
1493 0, /* sq_ass_slice */
1494 (objobjproc)dict_contains, /* sq_contains */
1495 0, /* sq_inplace_concat */
1496 0, /* sq_inplace_repeat */
1497};
1498
Guido van Rossum09e563a2001-05-01 12:10:21 +00001499static PyObject *
1500dict_iter(dictobject *dict)
1501{
1502 return dictiter_new(dict, select_key);
1503}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001504
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001505PyTypeObject PyDict_Type = {
1506 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001507 0,
Guido van Rossum9bfef441993-03-29 10:43:31 +00001508 "dictionary",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001509 sizeof(dictobject) + PyGC_HEAD_SIZE,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001510 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001511 (destructor)dict_dealloc, /* tp_dealloc */
1512 (printfunc)dict_print, /* tp_print */
1513 (getattrfunc)dict_getattr, /* tp_getattr */
1514 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00001515 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001516 (reprfunc)dict_repr, /* tp_repr */
1517 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001518 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001519 &dict_as_mapping, /* tp_as_mapping */
1520 0, /* tp_hash */
1521 0, /* tp_call */
1522 0, /* tp_str */
1523 0, /* tp_getattro */
1524 0, /* tp_setattro */
1525 0, /* tp_as_buffer */
1526 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
1527 0, /* tp_doc */
1528 (traverseproc)dict_traverse, /* tp_traverse */
1529 (inquiry)dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001530 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001531 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001532 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001533 0, /* tp_iternext */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001534};
1535
Guido van Rossum3cca2451997-05-16 14:23:33 +00001536/* For backward compatibility with old dictionary interface */
1537
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001538PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001539PyDict_GetItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001540{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001541 PyObject *kv, *rv;
1542 kv = PyString_FromString(key);
1543 if (kv == NULL)
1544 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001545 rv = PyDict_GetItem(v, kv);
1546 Py_DECREF(kv);
1547 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001548}
1549
1550int
Tim Peters1f5871e2000-07-04 17:44:48 +00001551PyDict_SetItemString(PyObject *v, char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001552{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001553 PyObject *kv;
1554 int err;
1555 kv = PyString_FromString(key);
1556 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001557 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00001558 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00001559 err = PyDict_SetItem(v, kv, item);
1560 Py_DECREF(kv);
1561 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001562}
1563
1564int
Tim Peters1f5871e2000-07-04 17:44:48 +00001565PyDict_DelItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001566{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001567 PyObject *kv;
1568 int err;
1569 kv = PyString_FromString(key);
1570 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001571 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001572 err = PyDict_DelItem(v, kv);
1573 Py_DECREF(kv);
1574 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001575}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001576
1577/* Dictionary iterator type */
1578
1579extern PyTypeObject PyDictIter_Type; /* Forward */
1580
1581typedef struct {
1582 PyObject_HEAD
1583 dictobject *di_dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001584 int di_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001585 int di_pos;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001586 binaryfunc di_select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001587} dictiterobject;
1588
1589static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001590dictiter_new(dictobject *dict, binaryfunc select)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001591{
1592 dictiterobject *di;
1593 di = PyObject_NEW(dictiterobject, &PyDictIter_Type);
1594 if (di == NULL)
1595 return NULL;
1596 Py_INCREF(dict);
1597 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001598 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001599 di->di_pos = 0;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001600 di->di_select = select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001601 return (PyObject *)di;
1602}
1603
1604static void
1605dictiter_dealloc(dictiterobject *di)
1606{
1607 Py_DECREF(di->di_dict);
1608 PyObject_DEL(di);
1609}
1610
1611static PyObject *
1612dictiter_next(dictiterobject *di, PyObject *args)
1613{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001614 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001615
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001616 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001617 PyErr_SetString(PyExc_RuntimeError,
1618 "dictionary changed size during iteration");
1619 return NULL;
1620 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001621 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1622 return (*di->di_select)(key, value);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001623 }
1624 PyErr_SetObject(PyExc_StopIteration, Py_None);
1625 return NULL;
1626}
1627
1628static PyObject *
1629dictiter_getiter(PyObject *it)
1630{
1631 Py_INCREF(it);
1632 return it;
1633}
1634
1635static PyMethodDef dictiter_methods[] = {
1636 {"next", (PyCFunction)dictiter_next, METH_VARARGS,
1637 "it.next() -- get the next value, or raise StopIteration"},
1638 {NULL, NULL} /* sentinel */
1639};
1640
1641static PyObject *
Guido van Rossum213c7a62001-04-23 14:08:49 +00001642dictiter_getattr(dictiterobject *di, char *name)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001643{
Guido van Rossum213c7a62001-04-23 14:08:49 +00001644 return Py_FindMethod(dictiter_methods, (PyObject *)di, name);
1645}
1646
1647static PyObject *dictiter_iternext(dictiterobject *di)
1648{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001649 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001650
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001651 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00001652 PyErr_SetString(PyExc_RuntimeError,
1653 "dictionary changed size during iteration");
1654 return NULL;
1655 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001656 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1657 return (*di->di_select)(key, value);
Guido van Rossum213c7a62001-04-23 14:08:49 +00001658 }
1659 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001660}
1661
1662PyTypeObject PyDictIter_Type = {
1663 PyObject_HEAD_INIT(&PyType_Type)
1664 0, /* ob_size */
1665 "dictionary-iterator", /* tp_name */
1666 sizeof(dictiterobject), /* tp_basicsize */
1667 0, /* tp_itemsize */
1668 /* methods */
1669 (destructor)dictiter_dealloc, /* tp_dealloc */
1670 0, /* tp_print */
1671 (getattrfunc)dictiter_getattr, /* tp_getattr */
1672 0, /* tp_setattr */
1673 0, /* tp_compare */
1674 0, /* tp_repr */
1675 0, /* tp_as_number */
1676 0, /* tp_as_sequence */
1677 0, /* tp_as_mapping */
1678 0, /* tp_hash */
1679 0, /* tp_call */
1680 0, /* tp_str */
1681 0, /* tp_getattro */
1682 0, /* tp_setattro */
1683 0, /* tp_as_buffer */
1684 Py_TPFLAGS_DEFAULT, /* tp_flags */
1685 0, /* tp_doc */
1686 0, /* tp_traverse */
1687 0, /* tp_clear */
1688 0, /* tp_richcompare */
1689 0, /* tp_weaklistoffset */
1690 (getiterfunc)dictiter_getiter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001691 (iternextfunc)dictiter_iternext, /* tp_iternext */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001692};