blob: d5700c9af66355f4956c8ab2eb9d22ee74b38703 [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/*
Tim Petersdea48ec2001-05-22 20:40:22 +00008 * MINSIZE is the minimum size of a dictionary. This many slots are
9 * allocated directly in the dict object (in the ma_smalltable member).
10 * This must be a power of 2, and the first entry in the polys[] vector must
11 * match.
Guido van Rossum16e93a81997-01-28 00:00:11 +000012 */
Tim Petersdea48ec2001-05-22 20:40:22 +000013#define MINSIZE 8
Guido van Rossum16e93a81997-01-28 00:00:11 +000014
Fred Drake1bff34a2000-08-31 19:31:38 +000015/* define this out if you don't want conversion statistics on exit */
16#undef SHOW_CONVERSION_COUNTS
17
Guido van Rossum16e93a81997-01-28 00:00:11 +000018/*
19Table of irreducible polynomials to efficiently cycle through
Tim Petersea8f2bf2000-12-13 01:02:46 +000020GF(2^n)-{0}, 2<=n<=30. A table size is always a power of 2.
Tim Petersdea48ec2001-05-22 20:40:22 +000021For a table size of 2**i, the polys entry is 2**i + j for some j in 1 thru
222**i-1 inclusive. The polys[] entries here happen to add in the smallest j
23values "that work". Work means this: given any integer k in 1 thru 2**i-1
24inclusive, a poly works if & only if repeating this code:
25 print k
26 k <<= 1
27 if k >= 2**i:
28 k ^= poly
29prints every integer in 1 thru 2**i-1 inclusive exactly once before printing
30k a second time. Theory can be used to find such polys efficiently, but the
31operational defn. of "works" is sufficient to find them in reasonable time
32via brute force program (hint: any poly that has an even number of 1 bits
33cannot work; ditto any poly with low bit 0; exploit those).
Guido van Rossum4b1302b1993-03-27 18:11:32 +000034*/
Tim Petersdea48ec2001-05-22 20:40:22 +000035
Guido van Rossum16e93a81997-01-28 00:00:11 +000036static long polys[] = {
Tim Petersdea48ec2001-05-22 20:40:22 +000037/* 4 + 3, */ /* first active entry if MINSIZE == 4 */
38 8 + 3, /* first active entry if MINSIZE == 8 */
Guido van Rossum9e5656c1997-01-29 04:45:16 +000039 16 + 3,
40 32 + 5,
41 64 + 3,
42 128 + 3,
43 256 + 29,
44 512 + 17,
45 1024 + 9,
46 2048 + 5,
47 4096 + 83,
48 8192 + 27,
49 16384 + 43,
50 32768 + 3,
51 65536 + 45,
52 131072 + 9,
53 262144 + 39,
54 524288 + 39,
55 1048576 + 9,
56 2097152 + 5,
57 4194304 + 3,
58 8388608 + 33,
59 16777216 + 27,
60 33554432 + 9,
61 67108864 + 71,
62 134217728 + 39,
63 268435456 + 9,
64 536870912 + 5,
Tim Petersdea48ec2001-05-22 20:40:22 +000065 1073741824 + 83
66 /* 2147483648 + 9 -- if we ever boost this to unsigned long */
Guido van Rossum4b1302b1993-03-27 18:11:32 +000067};
68
69/* Object used as dummy key to fill deleted entries */
Guido van Rossuma9e7a811997-05-13 21:02:11 +000070static PyObject *dummy; /* Initialized by first call to newdictobject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +000071
72/*
Tim Petersea8f2bf2000-12-13 01:02:46 +000073There are three kinds of slots in the table:
74
751. Unused. me_key == me_value == NULL
76 Does not hold an active (key, value) pair now and never did. Unused can
77 transition to Active upon key insertion. This is the only case in which
78 me_key is NULL, and is each slot's initial state.
79
802. Active. me_key != NULL and me_key != dummy and me_value != NULL
81 Holds an active (key, value) pair. Active can transition to Dummy upon
82 key deletion. This is the only case in which me_value != NULL.
83
Tim Petersf1c7c882000-12-13 19:58:25 +0000843. Dummy. me_key == dummy and me_value == NULL
Tim Petersea8f2bf2000-12-13 01:02:46 +000085 Previously held an active (key, value) pair, but that was deleted and an
86 active pair has not yet overwritten the slot. Dummy can transition to
87 Active upon key insertion. Dummy slots cannot be made Unused again
88 (cannot have me_key set to NULL), else the probe sequence in case of
89 collision would have no way to know they were once active.
Tim Petersf1c7c882000-12-13 19:58:25 +000090
91Note: .popitem() abuses the me_hash field of an Unused or Dummy slot to
92hold a search finger. The me_hash field of Unused or Dummy slots has no
93meaning otherwise.
Guido van Rossum4b1302b1993-03-27 18:11:32 +000094*/
95typedef struct {
Tim Petersea8f2bf2000-12-13 01:02:46 +000096 long me_hash; /* cached hash code of me_key */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000097 PyObject *me_key;
98 PyObject *me_value;
Guido van Rossum36488841997-04-11 19:14:07 +000099#ifdef USE_CACHE_ALIGNED
100 long aligner;
101#endif
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000102} dictentry;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000103
104/*
Tim Petersf1c7c882000-12-13 19:58:25 +0000105To ensure the lookup algorithm terminates, there must be at least one Unused
Tim Petersea8f2bf2000-12-13 01:02:46 +0000106slot (NULL key) in the table.
107The value ma_fill is the number of non-NULL keys (sum of Active and Dummy);
108ma_used is the number of non-NULL, non-dummy keys (== the number of non-NULL
109values == the number of Active items).
110To avoid slowing down lookups on a near-full table, we resize the table when
Tim Peters67830702001-03-21 19:23:56 +0000111it's two-thirds full.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000112*/
Fred Drake1bff34a2000-08-31 19:31:38 +0000113typedef struct dictobject dictobject;
114struct dictobject {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000115 PyObject_HEAD
Tim Petersea8f2bf2000-12-13 01:02:46 +0000116 int ma_fill; /* # Active + # Dummy */
117 int ma_used; /* # Active */
118 int ma_size; /* total # slots in ma_table */
119 int ma_poly; /* appopriate entry from polys vector */
Tim Petersdea48ec2001-05-22 20:40:22 +0000120 /* ma_table points to ma_smalltable for small tables, else to
121 * additional malloc'ed memory. ma_table is never NULL! This rule
122 * saves repeated runtime null-tests in the workhorse getitem and
123 * setitem calls.
124 */
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000125 dictentry *ma_table;
Fred Drake1bff34a2000-08-31 19:31:38 +0000126 dictentry *(*ma_lookup)(dictobject *mp, PyObject *key, long hash);
Tim Petersdea48ec2001-05-22 20:40:22 +0000127 dictentry ma_smalltable[MINSIZE];
Fred Drake1bff34a2000-08-31 19:31:38 +0000128};
129
130/* forward declarations */
131static dictentry *
132lookdict_string(dictobject *mp, PyObject *key, long hash);
133
134#ifdef SHOW_CONVERSION_COUNTS
135static long created = 0L;
136static long converted = 0L;
137
138static void
139show_counts(void)
140{
141 fprintf(stderr, "created %ld string dicts\n", created);
142 fprintf(stderr, "converted %ld to normal dicts\n", converted);
143 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
144}
145#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000146
Tim Petersdea48ec2001-05-22 20:40:22 +0000147/* Set dictobject* mp to empty but w/ MINSIZE slots, using ma_smalltable. */
148#define empty_to_minsize(mp) do { \
149 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
150 (mp)->ma_table = (mp)->ma_smalltable; \
151 (mp)->ma_size = MINSIZE; \
152 (mp)->ma_used = (mp)->ma_fill = 0; \
153 (mp)->ma_poly = polys[0]; \
154 assert(MINSIZE < (mp)->ma_poly && (mp)->ma_poly < MINSIZE*2); \
155 } while(0)
156
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000158PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000159{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000160 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000161 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000163 if (dummy == NULL)
164 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000165#ifdef SHOW_CONVERSION_COUNTS
166 Py_AtExit(show_counts);
167#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000168 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000169 mp = PyObject_NEW(dictobject, &PyDict_Type);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000170 if (mp == NULL)
171 return NULL;
Tim Petersdea48ec2001-05-22 20:40:22 +0000172 empty_to_minsize(mp);
Fred Drake1bff34a2000-08-31 19:31:38 +0000173 mp->ma_lookup = lookdict_string;
174#ifdef SHOW_CONVERSION_COUNTS
175 ++created;
176#endif
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000177 PyObject_GC_Init(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000178 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000179}
180
181/*
182The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000183This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000184Open addressing is preferred over chaining since the link overhead for
185chaining would be substantial (100% with typical malloc overhead).
Guido van Rossum16e93a81997-01-28 00:00:11 +0000186However, instead of going through the table at constant steps, we cycle
Tim Petersea8f2bf2000-12-13 01:02:46 +0000187through the values of GF(2^n). This avoids modulo computations, being
Guido van Rossum16e93a81997-01-28 00:00:11 +0000188much cheaper on RISC machines, without leading to clustering.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000189
Guido van Rossum2bc13791999-03-24 19:06:42 +0000190The initial probe index is computed as hash mod the table size.
Tim Petersea8f2bf2000-12-13 01:02:46 +0000191Subsequent probe indices use the values of x^i in GF(2^n)-{0} as an offset,
192where x is a root. The initial offset is derived from hash, too.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000193
194All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000195
196(This version is due to Reimer Behrends, some ideas are also due to
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000197Jyrki Alakuijala and Vladimir Marangozov.)
Fred Drake1bff34a2000-08-31 19:31:38 +0000198
199This function must never return NULL; failures are indicated by returning
200a dictentry* for which the me_value field is NULL. Exceptions are never
201reported by this function, and outstanding exceptions are maintained.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000202*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000203static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000204lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000205{
Guido van Rossum16e93a81997-01-28 00:00:11 +0000206 register int i;
207 register unsigned incr;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000208 register dictentry *freeslot;
Guido van Rossum2095d241997-04-09 19:41:24 +0000209 register unsigned int mask = mp->ma_size-1;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000210 dictentry *ep0 = mp->ma_table;
211 register dictentry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000212 register int restore_error = 0;
213 register int checked_error = 0;
214 register int cmp;
215 PyObject *err_type, *err_value, *err_tb;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000216 /* We must come up with (i, incr) such that 0 <= i < ma_size
Tim Petersea8f2bf2000-12-13 01:02:46 +0000217 and 0 < incr < ma_size and both are a function of hash.
218 i is the initial table index and incr the initial probe offset. */
Tim Peters2f228e72001-05-13 00:19:31 +0000219 i = hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000220 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000221 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000222 return ep;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000223 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000224 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000225 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000226 if (ep->me_hash == hash) {
227 /* error can't have been checked yet */
228 checked_error = 1;
229 if (PyErr_Occurred()) {
230 restore_error = 1;
231 PyErr_Fetch(&err_type, &err_value, &err_tb);
232 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000233 cmp = PyObject_RichCompareBool(ep->me_key, key, Py_EQ);
234 if (cmp > 0) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000235 if (restore_error)
236 PyErr_Restore(err_type, err_value,
237 err_tb);
238 return ep;
239 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000240 else if (cmp < 0)
241 PyErr_Clear();
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000242 }
243 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000244 }
Guido van Rossum2bc13791999-03-24 19:06:42 +0000245 /* Derive incr from hash, just to make it more arbitrary. Note that
Guido van Rossum16e93a81997-01-28 00:00:11 +0000246 incr must not be 0, or we will get into an infinite loop.*/
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000247 incr = (hash ^ ((unsigned long)hash >> 3)) & mask;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000248 if (!incr)
249 incr = mask;
Tim Peters342c65e2001-05-13 06:43:53 +0000250 /* In the loop, me_key == dummy is by far (factor of 100s) the
251 least likely outcome, so test for that last. */
Guido van Rossum16e93a81997-01-28 00:00:11 +0000252 for (;;) {
Guido van Rossumefb46091997-01-29 15:53:56 +0000253 ep = &ep0[(i+incr)&mask];
Guido van Rossum16e93a81997-01-28 00:00:11 +0000254 if (ep->me_key == NULL) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000255 if (restore_error)
256 PyErr_Restore(err_type, err_value, err_tb);
Tim Peters342c65e2001-05-13 06:43:53 +0000257 return freeslot == NULL ? ep : freeslot;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000258 }
Tim Peters342c65e2001-05-13 06:43:53 +0000259 if (ep->me_key == key) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000260 if (restore_error)
261 PyErr_Restore(err_type, err_value, err_tb);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000262 return ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000263 }
Tim Peters342c65e2001-05-13 06:43:53 +0000264 else if (ep->me_hash == hash && ep->me_key != dummy) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000265 if (!checked_error) {
266 checked_error = 1;
267 if (PyErr_Occurred()) {
268 restore_error = 1;
269 PyErr_Fetch(&err_type, &err_value,
270 &err_tb);
271 }
272 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000273 cmp = PyObject_RichCompareBool(ep->me_key, key, Py_EQ);
274 if (cmp > 0) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000275 if (restore_error)
276 PyErr_Restore(err_type, err_value,
277 err_tb);
278 return ep;
279 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000280 else if (cmp < 0)
281 PyErr_Clear();
Guido van Rossum16e93a81997-01-28 00:00:11 +0000282 }
Tim Peters342c65e2001-05-13 06:43:53 +0000283 else if (ep->me_key == dummy && freeslot == NULL)
284 freeslot = ep;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000285 /* Cycle through GF(2^n)-{0} */
Tim Peters342c65e2001-05-13 06:43:53 +0000286 incr <<= 1;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000287 if (incr > mask)
Tim Peters342c65e2001-05-13 06:43:53 +0000288 incr ^= mp->ma_poly; /* clears the highest bit */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000289 }
290}
291
292/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000293 * Hacked up version of lookdict which can assume keys are always strings;
294 * this assumption allows testing for errors during PyObject_Compare() to
295 * be dropped; string-string comparisons never raise exceptions. This also
296 * means we don't need to go through PyObject_Compare(); we can always use
Martin v. Löwiscd353062001-05-24 16:56:35 +0000297 * _PyString_Eq directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000298 *
299 * This really only becomes meaningful if proper error handling in lookdict()
300 * is too expensive.
301 */
302static dictentry *
303lookdict_string(dictobject *mp, PyObject *key, register long hash)
304{
305 register int i;
306 register unsigned incr;
307 register dictentry *freeslot;
308 register unsigned int mask = mp->ma_size-1;
309 dictentry *ep0 = mp->ma_table;
310 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000311
312 /* make sure this function doesn't have to handle non-string keys */
313 if (!PyString_Check(key)) {
314#ifdef SHOW_CONVERSION_COUNTS
315 ++converted;
316#endif
317 mp->ma_lookup = lookdict;
318 return lookdict(mp, key, hash);
319 }
320 /* We must come up with (i, incr) such that 0 <= i < ma_size
321 and 0 < incr < ma_size and both are a function of hash */
Tim Peters2f228e72001-05-13 00:19:31 +0000322 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000323 ep = &ep0[i];
324 if (ep->me_key == NULL || ep->me_key == key)
325 return ep;
326 if (ep->me_key == dummy)
327 freeslot = ep;
328 else {
329 if (ep->me_hash == hash
Martin v. Löwiscd353062001-05-24 16:56:35 +0000330 && _PyString_Eq(ep->me_key, key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000331 return ep;
332 }
333 freeslot = NULL;
334 }
335 /* Derive incr from hash, just to make it more arbitrary. Note that
336 incr must not be 0, or we will get into an infinite loop.*/
337 incr = (hash ^ ((unsigned long)hash >> 3)) & mask;
338 if (!incr)
339 incr = mask;
Tim Peters342c65e2001-05-13 06:43:53 +0000340 /* In the loop, me_key == dummy is by far (factor of 100s) the
341 least likely outcome, so test for that last. */
Fred Drake1bff34a2000-08-31 19:31:38 +0000342 for (;;) {
343 ep = &ep0[(i+incr)&mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000344 if (ep->me_key == NULL)
345 return freeslot == NULL ? ep : freeslot;
346 if (ep->me_key == key
347 || (ep->me_hash == hash
348 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000349 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000350 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000351 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000352 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000353 /* Cycle through GF(2^n)-{0} */
Tim Peters342c65e2001-05-13 06:43:53 +0000354 incr <<= 1;
Fred Drake1bff34a2000-08-31 19:31:38 +0000355 if (incr > mask)
Tim Peters342c65e2001-05-13 06:43:53 +0000356 incr ^= mp->ma_poly; /* clears the highest bit */
Fred Drake1bff34a2000-08-31 19:31:38 +0000357 }
358}
359
360/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000361Internal routine to insert a new item into the table.
362Used both by the internal resize routine and by the public insert routine.
363Eats a reference to key and one to value.
364*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000365static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000366insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000367{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000368 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000369 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000370 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000371 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000372 old_value = ep->me_value;
373 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000374 Py_DECREF(old_value); /* which **CAN** re-enter */
375 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000376 }
377 else {
378 if (ep->me_key == NULL)
379 mp->ma_fill++;
380 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000381 Py_DECREF(ep->me_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000382 ep->me_key = key;
383 ep->me_hash = hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000384 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000385 mp->ma_used++;
386 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000387}
388
389/*
390Restructure the table by allocating a new table and reinserting all
391items again. When entries have been deleted, the new table may
392actually be smaller than the old one.
393*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000394static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000395dictresize(dictobject *mp, int minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000396{
Tim Peters0c6010b2001-05-23 23:33:57 +0000397 int newsize, newpoly;
398 dictentry *oldtable, *newtable, *ep;
399 int i;
400 int is_oldtable_malloced;
401 dictentry small_copy[MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000402
403 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000404
405 /* Find the smallest table size > minused, and its poly[] entry. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000406 newpoly = 0;
407 newsize = MINSIZE;
408 for (i = 0; i < sizeof(polys)/sizeof(polys[0]); ++i) {
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000409 if (newsize > minused) {
Guido van Rossum16e93a81997-01-28 00:00:11 +0000410 newpoly = polys[i];
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000411 break;
412 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000413 newsize <<= 1;
414 if (newsize < 0) /* overflow */
415 break;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000416 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000417 if (newpoly == 0) {
418 /* Ran out of polynomials or newsize overflowed. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000419 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000420 return -1;
421 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000422
423 /* Get space for a new table. */
424 oldtable = mp->ma_table;
425 assert(oldtable != NULL);
426 is_oldtable_malloced = oldtable != mp->ma_smalltable;
427
Tim Petersdea48ec2001-05-22 20:40:22 +0000428 if (newsize == MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000429 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000430 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000431 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000432 if (mp->ma_fill == mp->ma_used) {
433 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000434 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000435 }
436 /* We're not going to resize it, but rebuild the
437 table anyway to purge old dummy entries.
438 Subtle: This is *necessary* if fill==size,
439 as lookdict needs at least one virgin slot to
440 terminate failing searches. If fill < size, it's
441 merely desirable, as dummies slow searches. */
442 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000443 memcpy(small_copy, oldtable, sizeof(small_copy));
444 oldtable = small_copy;
445 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000446 }
447 else {
448 newtable = PyMem_NEW(dictentry, newsize);
449 if (newtable == NULL) {
450 PyErr_NoMemory();
451 return -1;
452 }
453 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000454
455 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000456 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000457 mp->ma_table = newtable;
Tim Petersdea48ec2001-05-22 20:40:22 +0000458 mp->ma_size = newsize;
459 memset(newtable, 0, sizeof(dictentry) * newsize);
460 mp->ma_poly = newpoly;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000461 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000462 i = mp->ma_fill;
463 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000464
Tim Peters19283142001-05-17 22:25:34 +0000465 /* Copy the data over; this is refcount-neutral for active entries;
466 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000467 for (ep = oldtable; i > 0; ep++) {
468 if (ep->me_value != NULL) { /* active entry */
469 --i;
Tim Peters19283142001-05-17 22:25:34 +0000470 insertdict(mp, ep->me_key, ep->me_hash, ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000471 }
Tim Peters19283142001-05-17 22:25:34 +0000472 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000473 --i;
Tim Peters19283142001-05-17 22:25:34 +0000474 assert(ep->me_key == dummy);
475 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000476 }
Tim Peters19283142001-05-17 22:25:34 +0000477 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000478 }
479
Tim Peters0c6010b2001-05-23 23:33:57 +0000480 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000481 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000482 return 0;
483}
484
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000485PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000486PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000487{
488 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000489 dictobject *mp = (dictobject *)op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000490 if (!PyDict_Check(op)) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000491 return NULL;
492 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000493#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000494 if (!PyString_Check(key) ||
495 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000496#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000497 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000498 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000499 if (hash == -1) {
500 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000501 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000502 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000503 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000504 return (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000505}
506
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000507/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
508 * dictionary if it is merely replacing the value for an existing key.
509 * This is means that it's safe to loop over a dictionary with
510 * PyDict_Next() and occasionally replace a value -- but you can't
511 * insert new keys or remove them.
512 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000513int
Tim Peters1f5871e2000-07-04 17:44:48 +0000514PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000515{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000516 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000517 register long hash;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000518 register int n_used;
519
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000520 if (!PyDict_Check(op)) {
521 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000522 return -1;
523 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000524 mp = (dictobject *)op;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000525#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526 if (PyString_Check(key)) {
Guido van Rossum2a61e741997-01-18 07:55:05 +0000527#ifdef INTERN_STRINGS
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000528 if (((PyStringObject *)key)->ob_sinterned != NULL) {
529 key = ((PyStringObject *)key)->ob_sinterned;
530 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000531 }
532 else
533#endif
534 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000535 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000536 if (hash == -1)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000537 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000538 }
539 }
540 else
541#endif
542 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000543 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000544 if (hash == -1)
545 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000546 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000547 assert(mp->ma_fill < mp->ma_size);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000548 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000549 Py_INCREF(value);
550 Py_INCREF(key);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000551 insertdict(mp, key, hash, value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000552 /* If we added a key, we can safely resize. Otherwise skip this!
553 * If fill >= 2/3 size, adjust size. Normally, this doubles the
554 * size, but it's also possible for the dict to shrink (if ma_fill is
555 * much larger than ma_used, meaning a lot of dict keys have been
556 * deleted).
557 */
558 if (mp->ma_used > n_used && mp->ma_fill*3 >= mp->ma_size*2) {
559 if (dictresize(mp, mp->ma_used*2) != 0)
560 return -1;
561 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000562 return 0;
563}
564
565int
Tim Peters1f5871e2000-07-04 17:44:48 +0000566PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000567{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000568 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000569 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000570 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000571 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000572
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000573 if (!PyDict_Check(op)) {
574 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000575 return -1;
576 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000577#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000578 if (!PyString_Check(key) ||
579 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000580#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000581 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000582 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000583 if (hash == -1)
584 return -1;
585 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000586 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000587 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000588 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000589 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000590 return -1;
591 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000592 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000593 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000594 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000595 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000596 ep->me_value = NULL;
597 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000598 Py_DECREF(old_value);
599 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000600 return 0;
601}
602
Guido van Rossum25831651993-05-19 14:50:45 +0000603void
Tim Peters1f5871e2000-07-04 17:44:48 +0000604PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000605{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000606 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000607 dictentry *ep, *table;
608 int table_is_malloced;
609 int fill;
610 dictentry small_copy[MINSIZE];
611#ifdef Py_DEBUG
612 int i, n;
613#endif
614
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000615 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000616 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000617 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000618#ifdef Py_DEBUG
Guido van Rossumd7047b31995-01-02 19:07:15 +0000619 n = mp->ma_size;
Tim Petersdea48ec2001-05-22 20:40:22 +0000620 i = 0;
621#endif
622
623 table = mp->ma_table;
624 assert(table != NULL);
625 table_is_malloced = table != mp->ma_smalltable;
626
627 /* This is delicate. During the process of clearing the dict,
628 * decrefs can cause the dict to mutate. To avoid fatal confusion
629 * (voice of experience), we have to make the dict empty before
630 * clearing the slots, and never refer to anything via mp->xxx while
631 * clearing.
632 */
633 fill = mp->ma_fill;
634 if (table_is_malloced)
635 empty_to_minsize(mp);
636
637 else if (fill > 0) {
638 /* It's a small table with something that needs to be cleared.
639 * Afraid the only safe way is to copy the dict entries into
640 * another small table first.
641 */
642 memcpy(small_copy, table, sizeof(small_copy));
643 table = small_copy;
644 empty_to_minsize(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000645 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000646 /* else it's a small table that's already empty */
647
648 /* Now we can finally clear things. If C had refcounts, we could
649 * assert that the refcount on table is 1 now, i.e. that this function
650 * has unique access to it, so decref side-effects can't alter it.
651 */
652 for (ep = table; fill > 0; ++ep) {
653#ifdef Py_DEBUG
654 assert(i < n);
655 ++i;
656#endif
657 if (ep->me_key) {
658 --fill;
659 Py_DECREF(ep->me_key);
660 Py_XDECREF(ep->me_value);
661 }
662#ifdef Py_DEBUG
663 else
664 assert(ep->me_value == NULL);
665#endif
666 }
667
668 if (table_is_malloced)
669 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000670}
671
Tim Peters67830702001-03-21 19:23:56 +0000672/* CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
673 * mutates the dict. One exception: it is safe if the loop merely changes
674 * the values associated with the keys (but doesn't insert new keys or
675 * delete keys), via PyDict_SetItem().
676 */
Guido van Rossum25831651993-05-19 14:50:45 +0000677int
Tim Peters1f5871e2000-07-04 17:44:48 +0000678PyDict_Next(PyObject *op, int *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000679{
Guido van Rossum25831651993-05-19 14:50:45 +0000680 int i;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000681 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000682 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000683 return 0;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000684 mp = (dictobject *)op;
Guido van Rossum25831651993-05-19 14:50:45 +0000685 i = *ppos;
686 if (i < 0)
687 return 0;
688 while (i < mp->ma_size && mp->ma_table[i].me_value == NULL)
689 i++;
690 *ppos = i+1;
691 if (i >= mp->ma_size)
692 return 0;
693 if (pkey)
694 *pkey = mp->ma_table[i].me_key;
695 if (pvalue)
696 *pvalue = mp->ma_table[i].me_value;
697 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000698}
699
700/* Methods */
701
702static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000703dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000704{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000705 register dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000706 int fill = mp->ma_fill;
Guido van Rossumd724b232000-03-13 16:01:29 +0000707 Py_TRASHCAN_SAFE_BEGIN(mp)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000708 PyObject_GC_Fini(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000709 for (ep = mp->ma_table; fill > 0; ep++) {
710 if (ep->me_key) {
711 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000712 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000713 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000714 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000715 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000716 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000717 PyMem_DEL(mp->ma_table);
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000718 mp = (dictobject *) PyObject_AS_GC(mp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000719 PyObject_DEL(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000720 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000721}
722
723static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000724dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000725{
726 register int i;
727 register int any;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000728 register dictentry *ep;
Guido van Rossum255443b1998-04-10 22:47:14 +0000729
730 i = Py_ReprEnter((PyObject*)mp);
731 if (i != 0) {
732 if (i < 0)
733 return i;
734 fprintf(fp, "{...}");
735 return 0;
736 }
737
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000738 fprintf(fp, "{");
739 any = 0;
740 for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) {
741 if (ep->me_value != NULL) {
742 if (any++ > 0)
743 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000744 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
745 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000746 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000747 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000748 fprintf(fp, ": ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000749 if (PyObject_Print(ep->me_value, fp, 0) != 0) {
750 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000751 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000752 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000753 }
754 }
755 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000756 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000757 return 0;
758}
759
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000760static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000761dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000762{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000763 auto PyObject *v;
764 PyObject *sepa, *colon;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000765 register int i;
766 register int any;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000767 register dictentry *ep;
Guido van Rossum255443b1998-04-10 22:47:14 +0000768
769 i = Py_ReprEnter((PyObject*)mp);
770 if (i != 0) {
771 if (i > 0)
772 return PyString_FromString("{...}");
773 return NULL;
774 }
775
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000776 v = PyString_FromString("{");
777 sepa = PyString_FromString(", ");
778 colon = PyString_FromString(": ");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000779 any = 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000780 for (i = 0, ep = mp->ma_table; i < mp->ma_size && v; i++, ep++) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000781 if (ep->me_value != NULL) {
782 if (any++)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000783 PyString_Concat(&v, sepa);
784 PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_key));
785 PyString_Concat(&v, colon);
786 PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_value));
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000787 }
788 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000789 PyString_ConcatAndDel(&v, PyString_FromString("}"));
Guido van Rossum255443b1998-04-10 22:47:14 +0000790 Py_ReprLeave((PyObject*)mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000791 Py_XDECREF(sepa);
792 Py_XDECREF(colon);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000793 return v;
794}
795
796static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000797dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000798{
799 return mp->ma_used;
800}
801
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000802static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000803dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000804{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000805 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000806 long hash;
Tim Petersdea48ec2001-05-22 20:40:22 +0000807 assert(mp->ma_table != NULL);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000808#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000809 if (!PyString_Check(key) ||
810 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000811#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000812 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000813 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000814 if (hash == -1)
815 return NULL;
816 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000817 v = (mp->ma_lookup)(mp, key, hash) -> me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000818 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000819 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000820 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000821 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000822 return v;
823}
824
825static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000826dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000827{
828 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000829 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000830 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000831 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000832}
833
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000834static PyMappingMethods dict_as_mapping = {
835 (inquiry)dict_length, /*mp_length*/
836 (binaryfunc)dict_subscript, /*mp_subscript*/
837 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000838};
839
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000840static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000841dict_keys(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000842{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000843 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000844 register int i, j, n;
845
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000846 if (!PyArg_NoArgs(args))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000847 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000848 again:
849 n = mp->ma_used;
850 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000851 if (v == NULL)
852 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000853 if (n != mp->ma_used) {
854 /* Durnit. The allocations caused the dict to resize.
855 * Just start over, this shouldn't normally happen.
856 */
857 Py_DECREF(v);
858 goto again;
859 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000860 for (i = 0, j = 0; i < mp->ma_size; i++) {
861 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000862 PyObject *key = mp->ma_table[i].me_key;
863 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000864 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000865 j++;
866 }
867 }
868 return v;
869}
870
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000871static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000872dict_values(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000873{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000874 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000875 register int i, j, n;
876
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000877 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000878 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000879 again:
880 n = mp->ma_used;
881 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000882 if (v == NULL)
883 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000884 if (n != mp->ma_used) {
885 /* Durnit. The allocations caused the dict to resize.
886 * Just start over, this shouldn't normally happen.
887 */
888 Py_DECREF(v);
889 goto again;
890 }
Guido van Rossum25831651993-05-19 14:50:45 +0000891 for (i = 0, j = 0; i < mp->ma_size; i++) {
892 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000893 PyObject *value = mp->ma_table[i].me_value;
894 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000895 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000896 j++;
897 }
898 }
899 return v;
900}
901
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000902static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000903dict_items(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000904{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000905 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000906 register int i, j, n;
907 PyObject *item, *key, *value;
908
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000909 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000910 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000911 /* Preallocate the list of tuples, to avoid allocations during
912 * the loop over the items, which could trigger GC, which
913 * could resize the dict. :-(
914 */
915 again:
916 n = mp->ma_used;
917 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000918 if (v == NULL)
919 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000920 for (i = 0; i < n; i++) {
921 item = PyTuple_New(2);
922 if (item == NULL) {
923 Py_DECREF(v);
924 return NULL;
925 }
926 PyList_SET_ITEM(v, i, item);
927 }
928 if (n != mp->ma_used) {
929 /* Durnit. The allocations caused the dict to resize.
930 * Just start over, this shouldn't normally happen.
931 */
932 Py_DECREF(v);
933 goto again;
934 }
935 /* Nothing we do below makes any function calls. */
Guido van Rossum25831651993-05-19 14:50:45 +0000936 for (i = 0, j = 0; i < mp->ma_size; i++) {
937 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000938 key = mp->ma_table[i].me_key;
939 value = mp->ma_table[i].me_value;
940 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000941 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000942 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000943 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000944 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000945 j++;
946 }
947 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000948 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +0000949 return v;
950}
951
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000952static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000953dict_update(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000954{
955 register int i;
956 dictobject *other;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000957 dictentry *entry;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000958 if (!PyArg_Parse(args, "O!", &PyDict_Type, &other))
959 return NULL;
Jeremy Hylton1fb60882001-01-03 22:34:59 +0000960 if (other == mp || other->ma_used == 0)
961 goto done; /* a.update(a) or a.update({}); nothing to do */
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000962 /* Do one big resize at the start, rather than incrementally
963 resizing as we insert new items. Expect that there will be
964 no (or few) overlapping keys. */
965 if ((mp->ma_fill + other->ma_used)*3 >= mp->ma_size*2) {
966 if (dictresize(mp, (mp->ma_used + other->ma_used)*3/2) != 0)
967 return NULL;
968 }
969 for (i = 0; i < other->ma_size; i++) {
970 entry = &other->ma_table[i];
971 if (entry->me_value != NULL) {
972 Py_INCREF(entry->me_key);
973 Py_INCREF(entry->me_value);
974 insertdict(mp, entry->me_key, entry->me_hash,
975 entry->me_value);
976 }
977 }
978 done:
979 Py_INCREF(Py_None);
980 return Py_None;
981}
982
983static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000984dict_copy(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000985{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000986 if (!PyArg_Parse(args, ""))
987 return NULL;
988 return PyDict_Copy((PyObject*)mp);
989}
990
991PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000992PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000993{
994 register dictobject *mp;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000995 register int i;
996 dictobject *copy;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000997 dictentry *entry;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000998
999 if (o == NULL || !PyDict_Check(o)) {
1000 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001001 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001002 }
1003 mp = (dictobject *)o;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001004 copy = (dictobject *)PyDict_New();
1005 if (copy == NULL)
1006 return NULL;
1007 if (mp->ma_used > 0) {
1008 if (dictresize(copy, mp->ma_used*3/2) != 0)
1009 return NULL;
1010 for (i = 0; i < mp->ma_size; i++) {
1011 entry = &mp->ma_table[i];
1012 if (entry->me_value != NULL) {
1013 Py_INCREF(entry->me_key);
1014 Py_INCREF(entry->me_value);
1015 insertdict(copy, entry->me_key, entry->me_hash,
1016 entry->me_value);
1017 }
1018 }
1019 }
1020 return (PyObject *)copy;
1021}
1022
Guido van Rossum4199fac1993-11-05 10:18:44 +00001023int
Tim Peters1f5871e2000-07-04 17:44:48 +00001024PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001025{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001026 if (mp == NULL || !PyDict_Check(mp)) {
1027 PyErr_BadInternalCall();
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001028 return 0;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001029 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001030 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001031}
1032
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001033PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001034PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001035{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001036 if (mp == NULL || !PyDict_Check(mp)) {
1037 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001038 return NULL;
1039 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001040 return dict_keys((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001041}
1042
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001043PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001044PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001045{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001046 if (mp == NULL || !PyDict_Check(mp)) {
1047 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001048 return NULL;
1049 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001050 return dict_values((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +00001051}
1052
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001053PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001054PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001055{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001056 if (mp == NULL || !PyDict_Check(mp)) {
1057 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001058 return NULL;
1059 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001060 return dict_items((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +00001061}
1062
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001063/* Subroutine which returns the smallest key in a for which b's value
1064 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001065 pval argument. Both are NULL if no key in a is found for which b's status
1066 differs. The refcounts on (and only on) non-NULL *pval and function return
1067 values must be decremented by the caller (characterize() increments them
1068 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1069 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001070
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001071static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001072characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001073{
Tim Peters95bf9392001-05-10 08:32:44 +00001074 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1075 PyObject *aval = NULL; /* a[akey] */
Guido van Rossumb9324202001-01-18 00:39:02 +00001076 int i, cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001077
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001078 for (i = 0; i < a->ma_size; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001079 PyObject *thiskey, *thisaval, *thisbval;
1080 if (a->ma_table[i].me_value == NULL)
1081 continue;
1082 thiskey = a->ma_table[i].me_key;
1083 Py_INCREF(thiskey); /* keep alive across compares */
1084 if (akey != NULL) {
1085 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1086 if (cmp < 0) {
1087 Py_DECREF(thiskey);
1088 goto Fail;
1089 }
1090 if (cmp > 0 ||
1091 i >= a->ma_size ||
1092 a->ma_table[i].me_value == NULL)
1093 {
1094 /* Not the *smallest* a key; or maybe it is
1095 * but the compare shrunk the dict so we can't
1096 * find its associated value anymore; or
1097 * maybe it is but the compare deleted the
1098 * a[thiskey] entry.
1099 */
1100 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001101 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001102 }
Tim Peters95bf9392001-05-10 08:32:44 +00001103 }
1104
1105 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1106 thisaval = a->ma_table[i].me_value;
1107 assert(thisaval);
1108 Py_INCREF(thisaval); /* keep alive */
1109 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1110 if (thisbval == NULL)
1111 cmp = 0;
1112 else {
1113 /* both dicts have thiskey: same values? */
1114 cmp = PyObject_RichCompareBool(
1115 thisaval, thisbval, Py_EQ);
1116 if (cmp < 0) {
1117 Py_DECREF(thiskey);
1118 Py_DECREF(thisaval);
1119 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001120 }
1121 }
Tim Peters95bf9392001-05-10 08:32:44 +00001122 if (cmp == 0) {
1123 /* New winner. */
1124 Py_XDECREF(akey);
1125 Py_XDECREF(aval);
1126 akey = thiskey;
1127 aval = thisaval;
1128 }
1129 else {
1130 Py_DECREF(thiskey);
1131 Py_DECREF(thisaval);
1132 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001133 }
Tim Peters95bf9392001-05-10 08:32:44 +00001134 *pval = aval;
1135 return akey;
1136
1137Fail:
1138 Py_XDECREF(akey);
1139 Py_XDECREF(aval);
1140 *pval = NULL;
1141 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001142}
1143
1144static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001145dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001146{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001147 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001148 int res;
1149
1150 /* Compare lengths first */
1151 if (a->ma_used < b->ma_used)
1152 return -1; /* a is shorter */
1153 else if (a->ma_used > b->ma_used)
1154 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001155
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001156 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001157 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001158 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001159 if (adiff == NULL) {
1160 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001161 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001162 * must be equal.
1163 */
1164 res = PyErr_Occurred() ? -1 : 0;
1165 goto Finished;
1166 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001167 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001168 if (bdiff == NULL && PyErr_Occurred()) {
1169 assert(!bval);
1170 res = -1;
1171 goto Finished;
1172 }
1173 res = 0;
1174 if (bdiff) {
1175 /* bdiff == NULL "should be" impossible now, but perhaps
1176 * the last comparison done by the characterize() on a had
1177 * the side effect of making the dicts equal!
1178 */
1179 res = PyObject_Compare(adiff, bdiff);
1180 }
1181 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001182 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001183
1184Finished:
1185 Py_XDECREF(adiff);
1186 Py_XDECREF(bdiff);
1187 Py_XDECREF(aval);
1188 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001189 return res;
1190}
1191
Tim Peterse63415e2001-05-08 04:38:29 +00001192/* Return 1 if dicts equal, 0 if not, -1 if error.
1193 * Gets out as soon as any difference is detected.
1194 * Uses only Py_EQ comparison.
1195 */
1196static int
1197dict_equal(dictobject *a, dictobject *b)
1198{
1199 int i;
1200
1201 if (a->ma_used != b->ma_used)
1202 /* can't be equal if # of entries differ */
1203 return 0;
1204
1205 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1206 for (i = 0; i < a->ma_size; i++) {
1207 PyObject *aval = a->ma_table[i].me_value;
1208 if (aval != NULL) {
1209 int cmp;
1210 PyObject *bval;
1211 PyObject *key = a->ma_table[i].me_key;
1212 /* temporarily bump aval's refcount to ensure it stays
1213 alive until we're done with it */
1214 Py_INCREF(aval);
1215 bval = PyDict_GetItem((PyObject *)b, key);
1216 if (bval == NULL) {
1217 Py_DECREF(aval);
1218 return 0;
1219 }
1220 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1221 Py_DECREF(aval);
1222 if (cmp <= 0) /* error or not equal */
1223 return cmp;
1224 }
1225 }
1226 return 1;
1227 }
1228
1229static PyObject *
1230dict_richcompare(PyObject *v, PyObject *w, int op)
1231{
1232 int cmp;
1233 PyObject *res;
1234
1235 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1236 res = Py_NotImplemented;
1237 }
1238 else if (op == Py_EQ || op == Py_NE) {
1239 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1240 if (cmp < 0)
1241 return NULL;
1242 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1243 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001244 else
1245 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001246 Py_INCREF(res);
1247 return res;
1248 }
1249
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001250static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001251dict_has_key(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001252{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001253 PyObject *key;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001254 long hash;
1255 register long ok;
Fred Drake52fccfd2000-02-23 15:47:16 +00001256 if (!PyArg_ParseTuple(args, "O:has_key", &key))
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001257 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001258#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001259 if (!PyString_Check(key) ||
1260 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001261#endif
Guido van Rossumca756f21997-01-23 19:39:29 +00001262 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001263 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001264 if (hash == -1)
1265 return NULL;
1266 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001267 ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001268 return PyInt_FromLong(ok);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001269}
1270
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001271static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001272dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001273{
1274 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001275 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001276 PyObject *val = NULL;
1277 long hash;
1278
Fred Drake52fccfd2000-02-23 15:47:16 +00001279 if (!PyArg_ParseTuple(args, "O|O:get", &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001280 return NULL;
1281
Barry Warsawc38c5da1997-10-06 17:49:20 +00001282#ifdef CACHE_HASH
1283 if (!PyString_Check(key) ||
1284 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1285#endif
1286 {
1287 hash = PyObject_Hash(key);
1288 if (hash == -1)
1289 return NULL;
1290 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001291 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Barry Warsaw320ac331997-10-20 17:26:25 +00001292
Barry Warsawc38c5da1997-10-06 17:49:20 +00001293 if (val == NULL)
1294 val = failobj;
1295 Py_INCREF(val);
1296 return val;
1297}
1298
1299
1300static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001301dict_setdefault(register dictobject *mp, PyObject *args)
1302{
1303 PyObject *key;
1304 PyObject *failobj = Py_None;
1305 PyObject *val = NULL;
1306 long hash;
1307
1308 if (!PyArg_ParseTuple(args, "O|O:setdefault", &key, &failobj))
1309 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001310
1311#ifdef CACHE_HASH
1312 if (!PyString_Check(key) ||
1313 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1314#endif
1315 {
1316 hash = PyObject_Hash(key);
1317 if (hash == -1)
1318 return NULL;
1319 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001320 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001321 if (val == NULL) {
1322 val = failobj;
1323 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1324 val = NULL;
1325 }
1326 Py_XINCREF(val);
1327 return val;
1328}
1329
1330
1331static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001332dict_clear(register dictobject *mp, PyObject *args)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001333{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001334 if (!PyArg_NoArgs(args))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001335 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001336 PyDict_Clear((PyObject *)mp);
1337 Py_INCREF(Py_None);
1338 return Py_None;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001339}
1340
Guido van Rossumba6ab842000-12-12 22:02:18 +00001341static PyObject *
1342dict_popitem(dictobject *mp, PyObject *args)
1343{
1344 int i = 0;
1345 dictentry *ep;
1346 PyObject *res;
1347
1348 if (!PyArg_NoArgs(args))
1349 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001350 /* Allocate the result tuple first. Believe it or not,
1351 * this allocation could trigger a garbage collection which
1352 * could resize the dict, which would invalidate the pointer
1353 * (ep) into the dict calculated below.
1354 * So we have to do this first.
1355 */
1356 res = PyTuple_New(2);
1357 if (res == NULL)
1358 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001359 if (mp->ma_used == 0) {
1360 Py_DECREF(res);
1361 PyErr_SetString(PyExc_KeyError,
1362 "popitem(): dictionary is empty");
1363 return NULL;
1364 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001365 /* Set ep to "the first" dict entry with a value. We abuse the hash
1366 * field of slot 0 to hold a search finger:
1367 * If slot 0 has a value, use slot 0.
1368 * Else slot 0 is being used to hold a search finger,
1369 * and we use its hash value as the first index to look.
1370 */
1371 ep = &mp->ma_table[0];
1372 if (ep->me_value == NULL) {
1373 i = (int)ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001374 /* The hash field may be a real hash value, or it may be a
1375 * legit search finger, or it may be a once-legit search
1376 * finger that's out of bounds now because it wrapped around
1377 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001378 */
1379 if (i >= mp->ma_size || i < 1)
1380 i = 1; /* skip slot 0 */
1381 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1382 i++;
1383 if (i >= mp->ma_size)
1384 i = 1;
1385 }
1386 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001387 PyTuple_SET_ITEM(res, 0, ep->me_key);
1388 PyTuple_SET_ITEM(res, 1, ep->me_value);
1389 Py_INCREF(dummy);
1390 ep->me_key = dummy;
1391 ep->me_value = NULL;
1392 mp->ma_used--;
1393 assert(mp->ma_table[0].me_value == NULL);
1394 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001395 return res;
1396}
1397
Jeremy Hylton8caad492000-06-23 14:18:11 +00001398static int
1399dict_traverse(PyObject *op, visitproc visit, void *arg)
1400{
1401 int i = 0, err;
1402 PyObject *pk;
1403 PyObject *pv;
1404
1405 while (PyDict_Next(op, &i, &pk, &pv)) {
1406 err = visit(pk, arg);
1407 if (err)
1408 return err;
1409 err = visit(pv, arg);
1410 if (err)
1411 return err;
1412 }
1413 return 0;
1414}
1415
1416static int
1417dict_tp_clear(PyObject *op)
1418{
1419 PyDict_Clear(op);
1420 return 0;
1421}
1422
Tim Petersf7f88b12000-12-13 23:18:45 +00001423
Guido van Rossum09e563a2001-05-01 12:10:21 +00001424staticforward PyObject *dictiter_new(dictobject *, binaryfunc);
1425
1426static PyObject *
1427select_key(PyObject *key, PyObject *value)
1428{
1429 Py_INCREF(key);
1430 return key;
1431}
1432
1433static PyObject *
1434select_value(PyObject *key, PyObject *value)
1435{
1436 Py_INCREF(value);
1437 return value;
1438}
1439
1440static PyObject *
1441select_item(PyObject *key, PyObject *value)
1442{
1443 PyObject *res = PyTuple_New(2);
1444
1445 if (res != NULL) {
1446 Py_INCREF(key);
1447 Py_INCREF(value);
1448 PyTuple_SET_ITEM(res, 0, key);
1449 PyTuple_SET_ITEM(res, 1, value);
1450 }
1451 return res;
1452}
1453
1454static PyObject *
1455dict_iterkeys(dictobject *dict, PyObject *args)
1456{
1457 if (!PyArg_ParseTuple(args, ""))
1458 return NULL;
1459 return dictiter_new(dict, select_key);
1460}
1461
1462static PyObject *
1463dict_itervalues(dictobject *dict, PyObject *args)
1464{
1465 if (!PyArg_ParseTuple(args, ""))
1466 return NULL;
1467 return dictiter_new(dict, select_value);
1468}
1469
1470static PyObject *
1471dict_iteritems(dictobject *dict, PyObject *args)
1472{
1473 if (!PyArg_ParseTuple(args, ""))
1474 return NULL;
1475 return dictiter_new(dict, select_item);
1476}
1477
1478
Tim Petersf7f88b12000-12-13 23:18:45 +00001479static char has_key__doc__[] =
1480"D.has_key(k) -> 1 if D has a key k, else 0";
1481
1482static char get__doc__[] =
1483"D.get(k[,d]) -> D[k] if D.has_key(k), else d. d defaults to None.";
1484
1485static char setdefault_doc__[] =
1486"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if not D.has_key(k)";
1487
1488static char popitem__doc__[] =
1489"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
14902-tuple; but raise KeyError if D is empty";
1491
1492static char keys__doc__[] =
1493"D.keys() -> list of D's keys";
1494
1495static char items__doc__[] =
1496"D.items() -> list of D's (key, value) pairs, as 2-tuples";
1497
1498static char values__doc__[] =
1499"D.values() -> list of D's values";
1500
1501static char update__doc__[] =
1502"D.update(E) -> None. Update D from E: for k in E.keys(): D[k] = E[k]";
1503
1504static char clear__doc__[] =
1505"D.clear() -> None. Remove all items from D.";
1506
1507static char copy__doc__[] =
1508"D.copy() -> a shallow copy of D";
1509
Guido van Rossum09e563a2001-05-01 12:10:21 +00001510static char iterkeys__doc__[] =
1511"D.iterkeys() -> an iterator over the keys of D";
1512
1513static char itervalues__doc__[] =
1514"D.itervalues() -> an iterator over the values of D";
1515
1516static char iteritems__doc__[] =
1517"D.iteritems() -> an iterator over the (key, value) items of D";
1518
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001519static PyMethodDef mapp_methods[] = {
Tim Petersf7f88b12000-12-13 23:18:45 +00001520 {"has_key", (PyCFunction)dict_has_key, METH_VARARGS,
1521 has_key__doc__},
1522 {"get", (PyCFunction)dict_get, METH_VARARGS,
1523 get__doc__},
1524 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1525 setdefault_doc__},
1526 {"popitem", (PyCFunction)dict_popitem, METH_OLDARGS,
1527 popitem__doc__},
1528 {"keys", (PyCFunction)dict_keys, METH_OLDARGS,
1529 keys__doc__},
1530 {"items", (PyCFunction)dict_items, METH_OLDARGS,
1531 items__doc__},
1532 {"values", (PyCFunction)dict_values, METH_OLDARGS,
1533 values__doc__},
1534 {"update", (PyCFunction)dict_update, METH_OLDARGS,
1535 update__doc__},
1536 {"clear", (PyCFunction)dict_clear, METH_OLDARGS,
1537 clear__doc__},
1538 {"copy", (PyCFunction)dict_copy, METH_OLDARGS,
1539 copy__doc__},
Guido van Rossum09e563a2001-05-01 12:10:21 +00001540 {"iterkeys", (PyCFunction)dict_iterkeys, METH_VARARGS,
1541 iterkeys__doc__},
1542 {"itervalues", (PyCFunction)dict_itervalues, METH_VARARGS,
1543 itervalues__doc__},
1544 {"iteritems", (PyCFunction)dict_iteritems, METH_VARARGS,
1545 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001546 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001547};
1548
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001549static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001550dict_getattr(dictobject *mp, char *name)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001551{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001552 return Py_FindMethod(mapp_methods, (PyObject *)mp, name);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001553}
1554
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001555static int
1556dict_contains(dictobject *mp, PyObject *key)
1557{
1558 long hash;
1559
1560#ifdef CACHE_HASH
1561 if (!PyString_Check(key) ||
1562 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1563#endif
1564 {
1565 hash = PyObject_Hash(key);
1566 if (hash == -1)
1567 return -1;
1568 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001569 return (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001570}
1571
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001572/* Hack to implement "key in dict" */
1573static PySequenceMethods dict_as_sequence = {
1574 0, /* sq_length */
1575 0, /* sq_concat */
1576 0, /* sq_repeat */
1577 0, /* sq_item */
1578 0, /* sq_slice */
1579 0, /* sq_ass_item */
1580 0, /* sq_ass_slice */
1581 (objobjproc)dict_contains, /* sq_contains */
1582 0, /* sq_inplace_concat */
1583 0, /* sq_inplace_repeat */
1584};
1585
Guido van Rossum09e563a2001-05-01 12:10:21 +00001586static PyObject *
1587dict_iter(dictobject *dict)
1588{
1589 return dictiter_new(dict, select_key);
1590}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001591
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001592PyTypeObject PyDict_Type = {
1593 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001594 0,
Guido van Rossum9bfef441993-03-29 10:43:31 +00001595 "dictionary",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001596 sizeof(dictobject) + PyGC_HEAD_SIZE,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001597 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001598 (destructor)dict_dealloc, /* tp_dealloc */
1599 (printfunc)dict_print, /* tp_print */
1600 (getattrfunc)dict_getattr, /* tp_getattr */
1601 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00001602 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001603 (reprfunc)dict_repr, /* tp_repr */
1604 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001605 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001606 &dict_as_mapping, /* tp_as_mapping */
1607 0, /* tp_hash */
1608 0, /* tp_call */
1609 0, /* tp_str */
1610 0, /* tp_getattro */
1611 0, /* tp_setattro */
1612 0, /* tp_as_buffer */
1613 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
1614 0, /* tp_doc */
1615 (traverseproc)dict_traverse, /* tp_traverse */
1616 (inquiry)dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001617 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001618 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001619 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001620 0, /* tp_iternext */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001621};
1622
Guido van Rossum3cca2451997-05-16 14:23:33 +00001623/* For backward compatibility with old dictionary interface */
1624
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001625PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001626PyDict_GetItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001627{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001628 PyObject *kv, *rv;
1629 kv = PyString_FromString(key);
1630 if (kv == NULL)
1631 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001632 rv = PyDict_GetItem(v, kv);
1633 Py_DECREF(kv);
1634 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001635}
1636
1637int
Tim Peters1f5871e2000-07-04 17:44:48 +00001638PyDict_SetItemString(PyObject *v, char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001639{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001640 PyObject *kv;
1641 int err;
1642 kv = PyString_FromString(key);
1643 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001644 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00001645 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00001646 err = PyDict_SetItem(v, kv, item);
1647 Py_DECREF(kv);
1648 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001649}
1650
1651int
Tim Peters1f5871e2000-07-04 17:44:48 +00001652PyDict_DelItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001653{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001654 PyObject *kv;
1655 int err;
1656 kv = PyString_FromString(key);
1657 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001658 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001659 err = PyDict_DelItem(v, kv);
1660 Py_DECREF(kv);
1661 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001662}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001663
1664/* Dictionary iterator type */
1665
1666extern PyTypeObject PyDictIter_Type; /* Forward */
1667
1668typedef struct {
1669 PyObject_HEAD
1670 dictobject *di_dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001671 int di_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001672 int di_pos;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001673 binaryfunc di_select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001674} dictiterobject;
1675
1676static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001677dictiter_new(dictobject *dict, binaryfunc select)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001678{
1679 dictiterobject *di;
1680 di = PyObject_NEW(dictiterobject, &PyDictIter_Type);
1681 if (di == NULL)
1682 return NULL;
1683 Py_INCREF(dict);
1684 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001685 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001686 di->di_pos = 0;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001687 di->di_select = select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001688 return (PyObject *)di;
1689}
1690
1691static void
1692dictiter_dealloc(dictiterobject *di)
1693{
1694 Py_DECREF(di->di_dict);
1695 PyObject_DEL(di);
1696}
1697
1698static PyObject *
1699dictiter_next(dictiterobject *di, PyObject *args)
1700{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001701 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001702
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001703 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001704 PyErr_SetString(PyExc_RuntimeError,
1705 "dictionary changed size during iteration");
1706 return NULL;
1707 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001708 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1709 return (*di->di_select)(key, value);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001710 }
1711 PyErr_SetObject(PyExc_StopIteration, Py_None);
1712 return NULL;
1713}
1714
1715static PyObject *
1716dictiter_getiter(PyObject *it)
1717{
1718 Py_INCREF(it);
1719 return it;
1720}
1721
1722static PyMethodDef dictiter_methods[] = {
1723 {"next", (PyCFunction)dictiter_next, METH_VARARGS,
1724 "it.next() -- get the next value, or raise StopIteration"},
1725 {NULL, NULL} /* sentinel */
1726};
1727
1728static PyObject *
Guido van Rossum213c7a62001-04-23 14:08:49 +00001729dictiter_getattr(dictiterobject *di, char *name)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001730{
Guido van Rossum213c7a62001-04-23 14:08:49 +00001731 return Py_FindMethod(dictiter_methods, (PyObject *)di, name);
1732}
1733
1734static PyObject *dictiter_iternext(dictiterobject *di)
1735{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001736 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001737
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001738 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00001739 PyErr_SetString(PyExc_RuntimeError,
1740 "dictionary changed size during iteration");
1741 return NULL;
1742 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001743 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1744 return (*di->di_select)(key, value);
Guido van Rossum213c7a62001-04-23 14:08:49 +00001745 }
1746 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001747}
1748
1749PyTypeObject PyDictIter_Type = {
1750 PyObject_HEAD_INIT(&PyType_Type)
1751 0, /* ob_size */
1752 "dictionary-iterator", /* tp_name */
1753 sizeof(dictiterobject), /* tp_basicsize */
1754 0, /* tp_itemsize */
1755 /* methods */
1756 (destructor)dictiter_dealloc, /* tp_dealloc */
1757 0, /* tp_print */
1758 (getattrfunc)dictiter_getattr, /* tp_getattr */
1759 0, /* tp_setattr */
1760 0, /* tp_compare */
1761 0, /* tp_repr */
1762 0, /* tp_as_number */
1763 0, /* tp_as_sequence */
1764 0, /* tp_as_mapping */
1765 0, /* tp_hash */
1766 0, /* tp_call */
1767 0, /* tp_str */
1768 0, /* tp_getattro */
1769 0, /* tp_setattro */
1770 0, /* tp_as_buffer */
1771 Py_TPFLAGS_DEFAULT, /* tp_flags */
1772 0, /* tp_doc */
1773 0, /* tp_traverse */
1774 0, /* tp_clear */
1775 0, /* tp_richcompare */
1776 0, /* tp_weaklistoffset */
1777 (getiterfunc)dictiter_getiter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001778 (iternextfunc)dictiter_iternext, /* tp_iternext */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001779};