blob: ce44abef23377658d944b2202688985386200a59 [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
Tim Peterseb28ef22001-06-02 05:27:19 +00006/* MINSIZE is the minimum size of a dictionary. This many slots are
Tim Petersdea48ec2001-05-22 20:40:22 +00007 * allocated directly in the dict object (in the ma_smalltable member).
Tim Peterseb28ef22001-06-02 05:27:19 +00008 * It must be a power of 2, and at least 4. 8 allows dicts with no more than
9 * 5 active entries to live in ma_smalltable (and so avoid an additional
10 * malloc); instrumentation suggested this suffices for the majority of
11 * dicts (consisting mostly of usually-small instance dicts and usually-small
12 * dicts created to pass keyword arguments).
Guido van Rossum16e93a81997-01-28 00:00:11 +000013 */
Tim Petersdea48ec2001-05-22 20:40:22 +000014#define MINSIZE 8
Guido van Rossum16e93a81997-01-28 00:00:11 +000015
Tim Peterseb28ef22001-06-02 05:27:19 +000016/* Define this out if you don't want conversion statistics on exit. */
Fred Drake1bff34a2000-08-31 19:31:38 +000017#undef SHOW_CONVERSION_COUNTS
18
Tim Peterseb28ef22001-06-02 05:27:19 +000019/* See large comment block below. This must be >= 1. */
20#define PERTURB_SHIFT 5
21
Guido van Rossum16e93a81997-01-28 00:00:11 +000022/*
Tim Peterseb28ef22001-06-02 05:27:19 +000023Major subtleties ahead: Most hash schemes depend on having a "good" hash
24function, in the sense of simulating randomness. Python doesn't: its most
25important hash functions (for strings and ints) are very regular in common
26cases:
Tim Peters15d49292001-05-27 07:39:22 +000027
Tim Peterseb28ef22001-06-02 05:27:19 +000028>>> map(hash, (0, 1, 2, 3))
29[0, 1, 2, 3]
30>>> map(hash, ("namea", "nameb", "namec", "named"))
31[-1658398457, -1658398460, -1658398459, -1658398462]
32>>>
Tim Peters15d49292001-05-27 07:39:22 +000033
Tim Peterseb28ef22001-06-02 05:27:19 +000034This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
35the low-order i bits as the initial table index is extremely fast, and there
36are no collisions at all for dicts indexed by a contiguous range of ints.
37The same is approximately true when keys are "consecutive" strings. So this
38gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +000039
Tim Peterseb28ef22001-06-02 05:27:19 +000040OTOH, when collisions occur, the tendency to fill contiguous slices of the
41hash table makes a good collision resolution strategy crucial. Taking only
42the last i bits of the hash code is also vulnerable: for example, consider
43[i << 16 for i in range(20000)] as a set of keys. Since ints are their own
44hash codes, and this fits in a dict of size 2**15, the last 15 bits of every
45hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +000046
Tim Peterseb28ef22001-06-02 05:27:19 +000047But catering to unusual cases should not slow the usual ones, so we just take
48the last i bits anyway. It's up to collision resolution to do the rest. If
49we *usually* find the key we're looking for on the first try (and, it turns
50out, we usually do -- the table load factor is kept under 2/3, so the odds
51are solidly in our favor), then it makes best sense to keep the initial index
52computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +000053
Tim Peterseb28ef22001-06-02 05:27:19 +000054The first half of collision resolution is to visit table indices via this
55recurrence:
Tim Peters15d49292001-05-27 07:39:22 +000056
Tim Peterseb28ef22001-06-02 05:27:19 +000057 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +000058
Tim Peterseb28ef22001-06-02 05:27:19 +000059For any initial j in range(2**i), repeating that 2**i times generates each
60int in range(2**i) exactly once (see any text on random-number generation for
61proof). By itself, this doesn't help much: like linear probing (setting
62j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
63order. This would be bad, except that's not the only thing we do, and it's
64actually *good* in the common cases where hash keys are consecutive. In an
65example that's really too small to make this entirely clear, for a table of
66size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +000067
Tim Peterseb28ef22001-06-02 05:27:19 +000068 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
69
70If two things come in at index 5, the first place we look after is index 2,
71not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
72Linear probing is deadly in this case because there the fixed probe order
73is the *same* as the order consecutive keys are likely to arrive. But it's
74extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
75and certain that consecutive hash codes do not.
76
77The other half of the strategy is to get the other bits of the hash code
78into play. This is done by initializing a (unsigned) vrbl "perturb" to the
79full hash code, and changing the recurrence to:
80
81 j = (5*j) + 1 + perturb;
82 perturb >>= PERTURB_SHIFT;
83 use j % 2**i as the next table index;
84
85Now the probe sequence depends (eventually) on every bit in the hash code,
86and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
87because it quickly magnifies small differences in the bits that didn't affect
88the initial index. Note that because perturb is unsigned, if the recurrence
89is executed often enough perturb eventually becomes and remains 0. At that
90point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
91that's certain to find an empty slot eventually (since it generates every int
92in range(2**i), and we make sure there's always at least one empty slot).
93
94Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
95small so that the high bits of the hash code continue to affect the probe
96sequence across iterations; but you want it large so that in really bad cases
97the high-order hash bits have an effect on early iterations. 5 was "the
98best" in minimizing total collisions across experiments Tim Peters ran (on
99both normal and pathological cases), but 4 and 6 weren't significantly worse.
100
101Historical: Reimer Behrends contributed the idea of using a polynomial-based
102approach, using repeated multiplication by x in GF(2**n) where an irreducible
103polynomial for each table size was chosen such that x was a primitive root.
104Christian Tismer later extended that to use division by x instead, as an
105efficient way to get the high bits of the hash code into play. This scheme
106also gave excellent collision statistics, but was more expensive: two
107if-tests were required inside the loop; computing "the next" index took about
108the same number of operations but without as much potential parallelism
109(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
110above, and then shifting perturb can be done while the table index is being
111masked); and the dictobject struct required a member to hold the table's
112polynomial. In Tim's experiments the current scheme ran faster, produced
113equally good collision statistics, needed less code & used less memory.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000114*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000115
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000116/* Object used as dummy key to fill deleted entries */
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000117static PyObject *dummy; /* Initialized by first call to newdictobject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000118
119/*
Tim Petersea8f2bf2000-12-13 01:02:46 +0000120There are three kinds of slots in the table:
121
1221. Unused. me_key == me_value == NULL
123 Does not hold an active (key, value) pair now and never did. Unused can
124 transition to Active upon key insertion. This is the only case in which
125 me_key is NULL, and is each slot's initial state.
126
1272. Active. me_key != NULL and me_key != dummy and me_value != NULL
128 Holds an active (key, value) pair. Active can transition to Dummy upon
129 key deletion. This is the only case in which me_value != NULL.
130
Tim Petersf1c7c882000-12-13 19:58:25 +00001313. Dummy. me_key == dummy and me_value == NULL
Tim Petersea8f2bf2000-12-13 01:02:46 +0000132 Previously held an active (key, value) pair, but that was deleted and an
133 active pair has not yet overwritten the slot. Dummy can transition to
134 Active upon key insertion. Dummy slots cannot be made Unused again
135 (cannot have me_key set to NULL), else the probe sequence in case of
136 collision would have no way to know they were once active.
Tim Petersf1c7c882000-12-13 19:58:25 +0000137
138Note: .popitem() abuses the me_hash field of an Unused or Dummy slot to
139hold a search finger. The me_hash field of Unused or Dummy slots has no
140meaning otherwise.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000141*/
142typedef struct {
Tim Petersea8f2bf2000-12-13 01:02:46 +0000143 long me_hash; /* cached hash code of me_key */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000144 PyObject *me_key;
145 PyObject *me_value;
Guido van Rossum36488841997-04-11 19:14:07 +0000146#ifdef USE_CACHE_ALIGNED
147 long aligner;
148#endif
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000149} dictentry;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000150
151/*
Tim Petersf1c7c882000-12-13 19:58:25 +0000152To ensure the lookup algorithm terminates, there must be at least one Unused
Tim Petersea8f2bf2000-12-13 01:02:46 +0000153slot (NULL key) in the table.
154The value ma_fill is the number of non-NULL keys (sum of Active and Dummy);
155ma_used is the number of non-NULL, non-dummy keys (== the number of non-NULL
156values == the number of Active items).
157To avoid slowing down lookups on a near-full table, we resize the table when
Tim Peters67830702001-03-21 19:23:56 +0000158it's two-thirds full.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000159*/
Fred Drake1bff34a2000-08-31 19:31:38 +0000160typedef struct dictobject dictobject;
161struct dictobject {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162 PyObject_HEAD
Tim Petersea8f2bf2000-12-13 01:02:46 +0000163 int ma_fill; /* # Active + # Dummy */
164 int ma_used; /* # Active */
165 int ma_size; /* total # slots in ma_table */
Tim Petersdea48ec2001-05-22 20:40:22 +0000166 /* ma_table points to ma_smalltable for small tables, else to
167 * additional malloc'ed memory. ma_table is never NULL! This rule
168 * saves repeated runtime null-tests in the workhorse getitem and
169 * setitem calls.
170 */
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000171 dictentry *ma_table;
Fred Drake1bff34a2000-08-31 19:31:38 +0000172 dictentry *(*ma_lookup)(dictobject *mp, PyObject *key, long hash);
Tim Petersdea48ec2001-05-22 20:40:22 +0000173 dictentry ma_smalltable[MINSIZE];
Fred Drake1bff34a2000-08-31 19:31:38 +0000174};
175
176/* forward declarations */
177static dictentry *
178lookdict_string(dictobject *mp, PyObject *key, long hash);
179
180#ifdef SHOW_CONVERSION_COUNTS
181static long created = 0L;
182static long converted = 0L;
183
184static void
185show_counts(void)
186{
187 fprintf(stderr, "created %ld string dicts\n", created);
188 fprintf(stderr, "converted %ld to normal dicts\n", converted);
189 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
190}
191#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000192
Tim Petersdea48ec2001-05-22 20:40:22 +0000193/* Set dictobject* mp to empty but w/ MINSIZE slots, using ma_smalltable. */
194#define empty_to_minsize(mp) do { \
195 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
196 (mp)->ma_table = (mp)->ma_smalltable; \
197 (mp)->ma_size = MINSIZE; \
198 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Petersdea48ec2001-05-22 20:40:22 +0000199 } while(0)
200
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000201PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000202PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000203{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000204 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000205 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000206 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000207 if (dummy == NULL)
208 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000209#ifdef SHOW_CONVERSION_COUNTS
210 Py_AtExit(show_counts);
211#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000212 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000213 mp = PyObject_NEW(dictobject, &PyDict_Type);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000214 if (mp == NULL)
215 return NULL;
Tim Petersdea48ec2001-05-22 20:40:22 +0000216 empty_to_minsize(mp);
Fred Drake1bff34a2000-08-31 19:31:38 +0000217 mp->ma_lookup = lookdict_string;
218#ifdef SHOW_CONVERSION_COUNTS
219 ++created;
220#endif
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000221 PyObject_GC_Init(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000222 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000223}
224
225/*
226The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000227This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000228Open addressing is preferred over chaining since the link overhead for
229chaining would be substantial (100% with typical malloc overhead).
230
Tim Peterseb28ef22001-06-02 05:27:19 +0000231The initial probe index is computed as hash mod the table size. Subsequent
232probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000233
234All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000235
Tim Peterseb28ef22001-06-02 05:27:19 +0000236(The details in this version are due to Tim Peters, building on many past
237contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
238Christian Tismer).
Fred Drake1bff34a2000-08-31 19:31:38 +0000239
240This function must never return NULL; failures are indicated by returning
241a dictentry* for which the me_value field is NULL. Exceptions are never
242reported by this function, and outstanding exceptions are maintained.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000243*/
Tim Peterseb28ef22001-06-02 05:27:19 +0000244
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000245static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000246lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000247{
Guido van Rossum16e93a81997-01-28 00:00:11 +0000248 register int i;
Tim Peterseb28ef22001-06-02 05:27:19 +0000249 register unsigned int perturb;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000250 register dictentry *freeslot;
Guido van Rossum2095d241997-04-09 19:41:24 +0000251 register unsigned int mask = mp->ma_size-1;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000252 dictentry *ep0 = mp->ma_table;
253 register dictentry *ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000254 register int restore_error;
255 register int checked_error;
Fred Drakec88b99c2000-08-31 19:04:07 +0000256 register int cmp;
257 PyObject *err_type, *err_value, *err_tb;
Tim Peters453163d2001-06-03 04:54:32 +0000258 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000259
Tim Peters2f228e72001-05-13 00:19:31 +0000260 i = hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000261 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000262 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000263 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000264
265 restore_error = checked_error = 0;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000266 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000267 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000268 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000269 if (ep->me_hash == hash) {
270 /* error can't have been checked yet */
271 checked_error = 1;
272 if (PyErr_Occurred()) {
273 restore_error = 1;
274 PyErr_Fetch(&err_type, &err_value, &err_tb);
275 }
Tim Peters453163d2001-06-03 04:54:32 +0000276 startkey = ep->me_key;
277 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000278 if (cmp < 0)
Guido van Rossumb9324202001-01-18 00:39:02 +0000279 PyErr_Clear();
Tim Peters453163d2001-06-03 04:54:32 +0000280 if (ep0 == mp->ma_table && ep->me_key == startkey) {
281 if (cmp > 0)
282 goto Done;
283 }
284 else {
285 /* The compare did major nasty stuff to the
286 * dict: start over.
287 * XXX A clever adversary could prevent this
288 * XXX from terminating.
289 */
290 ep = lookdict(mp, key, hash);
291 goto Done;
292 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000293 }
294 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000295 }
Tim Peters15d49292001-05-27 07:39:22 +0000296
Tim Peters342c65e2001-05-13 06:43:53 +0000297 /* In the loop, me_key == dummy is by far (factor of 100s) the
298 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000299 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
300 i = (i << 2) + i + perturb + 1;
301 ep = &ep0[i & mask];
Guido van Rossum16e93a81997-01-28 00:00:11 +0000302 if (ep->me_key == NULL) {
Tim Peters7b5d0af2001-06-03 04:14:43 +0000303 if (freeslot != NULL)
304 ep = freeslot;
305 break;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000306 }
Tim Peters7b5d0af2001-06-03 04:14:43 +0000307 if (ep->me_key == key)
308 break;
309 if (ep->me_hash == hash && ep->me_key != dummy) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000310 if (!checked_error) {
311 checked_error = 1;
312 if (PyErr_Occurred()) {
313 restore_error = 1;
314 PyErr_Fetch(&err_type, &err_value,
315 &err_tb);
316 }
317 }
Tim Peters453163d2001-06-03 04:54:32 +0000318 startkey = ep->me_key;
319 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000320 if (cmp < 0)
Guido van Rossumb9324202001-01-18 00:39:02 +0000321 PyErr_Clear();
Tim Peters453163d2001-06-03 04:54:32 +0000322 if (ep0 == mp->ma_table && ep->me_key == startkey) {
323 if (cmp > 0)
324 break;
325 }
326 else {
327 /* The compare did major nasty stuff to the
328 * dict: start over.
329 * XXX A clever adversary could prevent this
330 * XXX from terminating.
331 */
332 ep = lookdict(mp, key, hash);
333 break;
334 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000335 }
Tim Peters342c65e2001-05-13 06:43:53 +0000336 else if (ep->me_key == dummy && freeslot == NULL)
337 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000338 }
Tim Peters7b5d0af2001-06-03 04:14:43 +0000339
340Done:
341 if (restore_error)
342 PyErr_Restore(err_type, err_value, err_tb);
343 return ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000344}
345
346/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000347 * Hacked up version of lookdict which can assume keys are always strings;
348 * this assumption allows testing for errors during PyObject_Compare() to
349 * be dropped; string-string comparisons never raise exceptions. This also
350 * means we don't need to go through PyObject_Compare(); we can always use
Martin v. Löwiscd353062001-05-24 16:56:35 +0000351 * _PyString_Eq directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000352 *
353 * This really only becomes meaningful if proper error handling in lookdict()
354 * is too expensive.
355 */
356static dictentry *
357lookdict_string(dictobject *mp, PyObject *key, register long hash)
358{
359 register int i;
Tim Peterseb28ef22001-06-02 05:27:19 +0000360 register unsigned int perturb;
Fred Drake1bff34a2000-08-31 19:31:38 +0000361 register dictentry *freeslot;
362 register unsigned int mask = mp->ma_size-1;
363 dictentry *ep0 = mp->ma_table;
364 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000365
366 /* make sure this function doesn't have to handle non-string keys */
367 if (!PyString_Check(key)) {
368#ifdef SHOW_CONVERSION_COUNTS
369 ++converted;
370#endif
371 mp->ma_lookup = lookdict;
372 return lookdict(mp, key, hash);
373 }
Tim Peters2f228e72001-05-13 00:19:31 +0000374 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000375 ep = &ep0[i];
376 if (ep->me_key == NULL || ep->me_key == key)
377 return ep;
378 if (ep->me_key == dummy)
379 freeslot = ep;
380 else {
381 if (ep->me_hash == hash
Martin v. Löwiscd353062001-05-24 16:56:35 +0000382 && _PyString_Eq(ep->me_key, key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000383 return ep;
384 }
385 freeslot = NULL;
386 }
Tim Peters15d49292001-05-27 07:39:22 +0000387
Tim Peters342c65e2001-05-13 06:43:53 +0000388 /* In the loop, me_key == dummy is by far (factor of 100s) the
389 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000390 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
391 i = (i << 2) + i + perturb + 1;
392 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000393 if (ep->me_key == NULL)
394 return freeslot == NULL ? ep : freeslot;
395 if (ep->me_key == key
396 || (ep->me_hash == hash
397 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000398 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000399 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000400 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000401 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000402 }
403}
404
405/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000406Internal routine to insert a new item into the table.
407Used both by the internal resize routine and by the public insert routine.
408Eats a reference to key and one to value.
409*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000410static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000411insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000412{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000413 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000414 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000415 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000416 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000417 old_value = ep->me_value;
418 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000419 Py_DECREF(old_value); /* which **CAN** re-enter */
420 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000421 }
422 else {
423 if (ep->me_key == NULL)
424 mp->ma_fill++;
425 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000426 Py_DECREF(ep->me_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000427 ep->me_key = key;
428 ep->me_hash = hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000429 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000430 mp->ma_used++;
431 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000432}
433
434/*
435Restructure the table by allocating a new table and reinserting all
436items again. When entries have been deleted, the new table may
437actually be smaller than the old one.
438*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000439static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000440dictresize(dictobject *mp, int minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000441{
Tim Peterseb28ef22001-06-02 05:27:19 +0000442 int newsize;
Tim Peters0c6010b2001-05-23 23:33:57 +0000443 dictentry *oldtable, *newtable, *ep;
444 int i;
445 int is_oldtable_malloced;
446 dictentry small_copy[MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000447
448 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000449
Tim Peterseb28ef22001-06-02 05:27:19 +0000450 /* Find the smallest table size > minused. */
451 for (newsize = MINSIZE;
452 newsize <= minused && newsize > 0;
453 newsize <<= 1)
454 ;
455 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000456 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000457 return -1;
458 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000459
460 /* Get space for a new table. */
461 oldtable = mp->ma_table;
462 assert(oldtable != NULL);
463 is_oldtable_malloced = oldtable != mp->ma_smalltable;
464
Tim Petersdea48ec2001-05-22 20:40:22 +0000465 if (newsize == MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000466 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000467 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000468 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000469 if (mp->ma_fill == mp->ma_used) {
470 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000471 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000472 }
473 /* We're not going to resize it, but rebuild the
474 table anyway to purge old dummy entries.
475 Subtle: This is *necessary* if fill==size,
476 as lookdict needs at least one virgin slot to
477 terminate failing searches. If fill < size, it's
478 merely desirable, as dummies slow searches. */
479 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000480 memcpy(small_copy, oldtable, sizeof(small_copy));
481 oldtable = small_copy;
482 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000483 }
484 else {
485 newtable = PyMem_NEW(dictentry, newsize);
486 if (newtable == NULL) {
487 PyErr_NoMemory();
488 return -1;
489 }
490 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000491
492 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000493 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000494 mp->ma_table = newtable;
Tim Petersdea48ec2001-05-22 20:40:22 +0000495 mp->ma_size = newsize;
496 memset(newtable, 0, sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000497 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000498 i = mp->ma_fill;
499 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000500
Tim Peters19283142001-05-17 22:25:34 +0000501 /* Copy the data over; this is refcount-neutral for active entries;
502 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000503 for (ep = oldtable; i > 0; ep++) {
504 if (ep->me_value != NULL) { /* active entry */
505 --i;
Tim Peters19283142001-05-17 22:25:34 +0000506 insertdict(mp, ep->me_key, ep->me_hash, ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000507 }
Tim Peters19283142001-05-17 22:25:34 +0000508 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000509 --i;
Tim Peters19283142001-05-17 22:25:34 +0000510 assert(ep->me_key == dummy);
511 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000512 }
Tim Peters19283142001-05-17 22:25:34 +0000513 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000514 }
515
Tim Peters0c6010b2001-05-23 23:33:57 +0000516 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000517 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000518 return 0;
519}
520
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000521PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000522PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000523{
524 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000525 dictobject *mp = (dictobject *)op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526 if (!PyDict_Check(op)) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000527 return NULL;
528 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000529#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000530 if (!PyString_Check(key) ||
531 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000532#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000533 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000534 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000535 if (hash == -1) {
536 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000537 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000538 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000539 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000540 return (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000541}
542
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000543/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
544 * dictionary if it is merely replacing the value for an existing key.
545 * This is means that it's safe to loop over a dictionary with
546 * PyDict_Next() and occasionally replace a value -- but you can't
547 * insert new keys or remove them.
548 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000549int
Tim Peters1f5871e2000-07-04 17:44:48 +0000550PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000551{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000552 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000553 register long hash;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000554 register int n_used;
555
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000556 if (!PyDict_Check(op)) {
557 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000558 return -1;
559 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000560 mp = (dictobject *)op;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000561#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000562 if (PyString_Check(key)) {
Guido van Rossum2a61e741997-01-18 07:55:05 +0000563#ifdef INTERN_STRINGS
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000564 if (((PyStringObject *)key)->ob_sinterned != NULL) {
565 key = ((PyStringObject *)key)->ob_sinterned;
566 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000567 }
568 else
569#endif
570 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000571 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000572 if (hash == -1)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000573 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000574 }
575 }
576 else
577#endif
578 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000579 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000580 if (hash == -1)
581 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000582 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000583 assert(mp->ma_fill < mp->ma_size);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000584 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000585 Py_INCREF(value);
586 Py_INCREF(key);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000587 insertdict(mp, key, hash, value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000588 /* If we added a key, we can safely resize. Otherwise skip this!
589 * If fill >= 2/3 size, adjust size. Normally, this doubles the
590 * size, but it's also possible for the dict to shrink (if ma_fill is
591 * much larger than ma_used, meaning a lot of dict keys have been
592 * deleted).
593 */
594 if (mp->ma_used > n_used && mp->ma_fill*3 >= mp->ma_size*2) {
595 if (dictresize(mp, mp->ma_used*2) != 0)
596 return -1;
597 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000598 return 0;
599}
600
601int
Tim Peters1f5871e2000-07-04 17:44:48 +0000602PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000603{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000604 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000605 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000606 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000607 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000608
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000609 if (!PyDict_Check(op)) {
610 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000611 return -1;
612 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000613#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000614 if (!PyString_Check(key) ||
615 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000616#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000617 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000618 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000619 if (hash == -1)
620 return -1;
621 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000622 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000623 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000624 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000625 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000626 return -1;
627 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000628 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000629 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000630 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000631 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000632 ep->me_value = NULL;
633 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000634 Py_DECREF(old_value);
635 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000636 return 0;
637}
638
Guido van Rossum25831651993-05-19 14:50:45 +0000639void
Tim Peters1f5871e2000-07-04 17:44:48 +0000640PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000641{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000642 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000643 dictentry *ep, *table;
644 int table_is_malloced;
645 int fill;
646 dictentry small_copy[MINSIZE];
647#ifdef Py_DEBUG
648 int i, n;
649#endif
650
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000651 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000652 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000653 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000654#ifdef Py_DEBUG
Guido van Rossumd7047b31995-01-02 19:07:15 +0000655 n = mp->ma_size;
Tim Petersdea48ec2001-05-22 20:40:22 +0000656 i = 0;
657#endif
658
659 table = mp->ma_table;
660 assert(table != NULL);
661 table_is_malloced = table != mp->ma_smalltable;
662
663 /* This is delicate. During the process of clearing the dict,
664 * decrefs can cause the dict to mutate. To avoid fatal confusion
665 * (voice of experience), we have to make the dict empty before
666 * clearing the slots, and never refer to anything via mp->xxx while
667 * clearing.
668 */
669 fill = mp->ma_fill;
670 if (table_is_malloced)
671 empty_to_minsize(mp);
672
673 else if (fill > 0) {
674 /* It's a small table with something that needs to be cleared.
675 * Afraid the only safe way is to copy the dict entries into
676 * another small table first.
677 */
678 memcpy(small_copy, table, sizeof(small_copy));
679 table = small_copy;
680 empty_to_minsize(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000681 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000682 /* else it's a small table that's already empty */
683
684 /* Now we can finally clear things. If C had refcounts, we could
685 * assert that the refcount on table is 1 now, i.e. that this function
686 * has unique access to it, so decref side-effects can't alter it.
687 */
688 for (ep = table; fill > 0; ++ep) {
689#ifdef Py_DEBUG
690 assert(i < n);
691 ++i;
692#endif
693 if (ep->me_key) {
694 --fill;
695 Py_DECREF(ep->me_key);
696 Py_XDECREF(ep->me_value);
697 }
698#ifdef Py_DEBUG
699 else
700 assert(ep->me_value == NULL);
701#endif
702 }
703
704 if (table_is_malloced)
705 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000706}
707
Tim Peters67830702001-03-21 19:23:56 +0000708/* CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
709 * mutates the dict. One exception: it is safe if the loop merely changes
710 * the values associated with the keys (but doesn't insert new keys or
711 * delete keys), via PyDict_SetItem().
712 */
Guido van Rossum25831651993-05-19 14:50:45 +0000713int
Tim Peters1f5871e2000-07-04 17:44:48 +0000714PyDict_Next(PyObject *op, int *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000715{
Guido van Rossum25831651993-05-19 14:50:45 +0000716 int i;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000717 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000719 return 0;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000720 mp = (dictobject *)op;
Guido van Rossum25831651993-05-19 14:50:45 +0000721 i = *ppos;
722 if (i < 0)
723 return 0;
724 while (i < mp->ma_size && mp->ma_table[i].me_value == NULL)
725 i++;
726 *ppos = i+1;
727 if (i >= mp->ma_size)
728 return 0;
729 if (pkey)
730 *pkey = mp->ma_table[i].me_key;
731 if (pvalue)
732 *pvalue = mp->ma_table[i].me_value;
733 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000734}
735
736/* Methods */
737
738static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000739dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000740{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000741 register dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000742 int fill = mp->ma_fill;
Guido van Rossumd724b232000-03-13 16:01:29 +0000743 Py_TRASHCAN_SAFE_BEGIN(mp)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000744 PyObject_GC_Fini(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000745 for (ep = mp->ma_table; fill > 0; ep++) {
746 if (ep->me_key) {
747 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000748 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000749 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000750 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000751 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000752 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000753 PyMem_DEL(mp->ma_table);
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000754 mp = (dictobject *) PyObject_AS_GC(mp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000755 PyObject_DEL(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000756 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000757}
758
759static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000760dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000761{
762 register int i;
763 register int any;
Guido van Rossum255443b1998-04-10 22:47:14 +0000764
765 i = Py_ReprEnter((PyObject*)mp);
766 if (i != 0) {
767 if (i < 0)
768 return i;
769 fprintf(fp, "{...}");
770 return 0;
771 }
772
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000773 fprintf(fp, "{");
774 any = 0;
Tim Peters23cf6be2001-06-02 08:02:56 +0000775 for (i = 0; i < mp->ma_size; i++) {
776 dictentry *ep = mp->ma_table + i;
777 PyObject *pvalue = ep->me_value;
778 if (pvalue != NULL) {
779 /* Prevent PyObject_Repr from deleting value during
780 key format */
781 Py_INCREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000782 if (any++ > 0)
783 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000784 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000785 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000786 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000787 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000788 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000789 fprintf(fp, ": ");
Tim Peters19b77cf2001-06-02 08:27:39 +0000790 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000791 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000792 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000793 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000794 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000795 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000796 }
797 }
798 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000799 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000800 return 0;
801}
802
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000803static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000804dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000805{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000806 auto PyObject *v;
807 PyObject *sepa, *colon;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000808 register int i;
809 register int any;
Guido van Rossum255443b1998-04-10 22:47:14 +0000810
811 i = Py_ReprEnter((PyObject*)mp);
812 if (i != 0) {
813 if (i > 0)
814 return PyString_FromString("{...}");
815 return NULL;
816 }
817
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000818 v = PyString_FromString("{");
819 sepa = PyString_FromString(", ");
820 colon = PyString_FromString(": ");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000821 any = 0;
Tim Peters23cf6be2001-06-02 08:02:56 +0000822 for (i = 0; i < mp->ma_size && v; i++) {
823 dictentry *ep = mp->ma_table + i;
824 PyObject *pvalue = ep->me_value;
825 if (pvalue != NULL) {
826 /* Prevent PyObject_Repr from deleting value during
827 key format */
828 Py_INCREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000829 if (any++)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000830 PyString_Concat(&v, sepa);
831 PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_key));
832 PyString_Concat(&v, colon);
Tim Peters23cf6be2001-06-02 08:02:56 +0000833 PyString_ConcatAndDel(&v, PyObject_Repr(pvalue));
834 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000835 }
836 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000837 PyString_ConcatAndDel(&v, PyString_FromString("}"));
Guido van Rossum255443b1998-04-10 22:47:14 +0000838 Py_ReprLeave((PyObject*)mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000839 Py_XDECREF(sepa);
840 Py_XDECREF(colon);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000841 return v;
842}
843
844static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000845dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000846{
847 return mp->ma_used;
848}
849
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000850static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000851dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000852{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000853 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000854 long hash;
Tim Petersdea48ec2001-05-22 20:40:22 +0000855 assert(mp->ma_table != NULL);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000856#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000857 if (!PyString_Check(key) ||
858 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000859#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000860 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000861 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000862 if (hash == -1)
863 return NULL;
864 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000865 v = (mp->ma_lookup)(mp, key, hash) -> me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000866 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000867 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000868 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000869 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000870 return v;
871}
872
873static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000874dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000875{
876 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000877 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000878 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000879 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000880}
881
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000882static PyMappingMethods dict_as_mapping = {
883 (inquiry)dict_length, /*mp_length*/
884 (binaryfunc)dict_subscript, /*mp_subscript*/
885 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000886};
887
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000888static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000889dict_keys(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000890{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000891 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000892 register int i, j, n;
893
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000894 if (!PyArg_NoArgs(args))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000895 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000896 again:
897 n = mp->ma_used;
898 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000899 if (v == NULL)
900 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000901 if (n != mp->ma_used) {
902 /* Durnit. The allocations caused the dict to resize.
903 * Just start over, this shouldn't normally happen.
904 */
905 Py_DECREF(v);
906 goto again;
907 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000908 for (i = 0, j = 0; i < mp->ma_size; i++) {
909 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000910 PyObject *key = mp->ma_table[i].me_key;
911 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000912 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000913 j++;
914 }
915 }
916 return v;
917}
918
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000919static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000920dict_values(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000921{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000922 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000923 register int i, j, n;
924
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000925 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000926 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000927 again:
928 n = mp->ma_used;
929 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000930 if (v == NULL)
931 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000932 if (n != mp->ma_used) {
933 /* Durnit. The allocations caused the dict to resize.
934 * Just start over, this shouldn't normally happen.
935 */
936 Py_DECREF(v);
937 goto again;
938 }
Guido van Rossum25831651993-05-19 14:50:45 +0000939 for (i = 0, j = 0; i < mp->ma_size; i++) {
940 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000941 PyObject *value = mp->ma_table[i].me_value;
942 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000943 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000944 j++;
945 }
946 }
947 return v;
948}
949
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000950static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000951dict_items(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000952{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000953 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000954 register int i, j, n;
955 PyObject *item, *key, *value;
956
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000957 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000958 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000959 /* Preallocate the list of tuples, to avoid allocations during
960 * the loop over the items, which could trigger GC, which
961 * could resize the dict. :-(
962 */
963 again:
964 n = mp->ma_used;
965 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000966 if (v == NULL)
967 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000968 for (i = 0; i < n; i++) {
969 item = PyTuple_New(2);
970 if (item == NULL) {
971 Py_DECREF(v);
972 return NULL;
973 }
974 PyList_SET_ITEM(v, i, item);
975 }
976 if (n != mp->ma_used) {
977 /* Durnit. The allocations caused the dict to resize.
978 * Just start over, this shouldn't normally happen.
979 */
980 Py_DECREF(v);
981 goto again;
982 }
983 /* Nothing we do below makes any function calls. */
Guido van Rossum25831651993-05-19 14:50:45 +0000984 for (i = 0, j = 0; i < mp->ma_size; i++) {
985 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000986 key = mp->ma_table[i].me_key;
987 value = mp->ma_table[i].me_value;
988 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000989 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000990 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000991 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000992 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000993 j++;
994 }
995 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000996 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +0000997 return v;
998}
999
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001000static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001001dict_update(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001002{
1003 register int i;
1004 dictobject *other;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001005 dictentry *entry;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001006 if (!PyArg_Parse(args, "O!", &PyDict_Type, &other))
1007 return NULL;
Jeremy Hylton1fb60882001-01-03 22:34:59 +00001008 if (other == mp || other->ma_used == 0)
1009 goto done; /* a.update(a) or a.update({}); nothing to do */
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001010 /* Do one big resize at the start, rather than incrementally
1011 resizing as we insert new items. Expect that there will be
1012 no (or few) overlapping keys. */
1013 if ((mp->ma_fill + other->ma_used)*3 >= mp->ma_size*2) {
1014 if (dictresize(mp, (mp->ma_used + other->ma_used)*3/2) != 0)
1015 return NULL;
1016 }
1017 for (i = 0; i < other->ma_size; i++) {
1018 entry = &other->ma_table[i];
1019 if (entry->me_value != NULL) {
1020 Py_INCREF(entry->me_key);
1021 Py_INCREF(entry->me_value);
1022 insertdict(mp, entry->me_key, entry->me_hash,
1023 entry->me_value);
1024 }
1025 }
1026 done:
1027 Py_INCREF(Py_None);
1028 return Py_None;
1029}
1030
1031static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001032dict_copy(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001033{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001034 if (!PyArg_Parse(args, ""))
1035 return NULL;
1036 return PyDict_Copy((PyObject*)mp);
1037}
1038
1039PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001040PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001041{
1042 register dictobject *mp;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001043 register int i;
1044 dictobject *copy;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001045 dictentry *entry;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001046
1047 if (o == NULL || !PyDict_Check(o)) {
1048 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001049 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001050 }
1051 mp = (dictobject *)o;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001052 copy = (dictobject *)PyDict_New();
1053 if (copy == NULL)
1054 return NULL;
1055 if (mp->ma_used > 0) {
1056 if (dictresize(copy, mp->ma_used*3/2) != 0)
1057 return NULL;
1058 for (i = 0; i < mp->ma_size; i++) {
1059 entry = &mp->ma_table[i];
1060 if (entry->me_value != NULL) {
1061 Py_INCREF(entry->me_key);
1062 Py_INCREF(entry->me_value);
1063 insertdict(copy, entry->me_key, entry->me_hash,
1064 entry->me_value);
1065 }
1066 }
1067 }
1068 return (PyObject *)copy;
1069}
1070
Guido van Rossum4199fac1993-11-05 10:18:44 +00001071int
Tim Peters1f5871e2000-07-04 17:44:48 +00001072PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001073{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001074 if (mp == NULL || !PyDict_Check(mp)) {
1075 PyErr_BadInternalCall();
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001076 return 0;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001077 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001078 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001079}
1080
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001081PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001082PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001083{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001084 if (mp == NULL || !PyDict_Check(mp)) {
1085 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001086 return NULL;
1087 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001088 return dict_keys((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001089}
1090
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001091PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001092PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001093{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001094 if (mp == NULL || !PyDict_Check(mp)) {
1095 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001096 return NULL;
1097 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001098 return dict_values((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +00001099}
1100
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001101PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001102PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001103{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001104 if (mp == NULL || !PyDict_Check(mp)) {
1105 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001106 return NULL;
1107 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001108 return dict_items((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +00001109}
1110
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001111/* Subroutine which returns the smallest key in a for which b's value
1112 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001113 pval argument. Both are NULL if no key in a is found for which b's status
1114 differs. The refcounts on (and only on) non-NULL *pval and function return
1115 values must be decremented by the caller (characterize() increments them
1116 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1117 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001118
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001119static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001120characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001121{
Tim Peters95bf9392001-05-10 08:32:44 +00001122 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1123 PyObject *aval = NULL; /* a[akey] */
Guido van Rossumb9324202001-01-18 00:39:02 +00001124 int i, cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001125
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001126 for (i = 0; i < a->ma_size; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001127 PyObject *thiskey, *thisaval, *thisbval;
1128 if (a->ma_table[i].me_value == NULL)
1129 continue;
1130 thiskey = a->ma_table[i].me_key;
1131 Py_INCREF(thiskey); /* keep alive across compares */
1132 if (akey != NULL) {
1133 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1134 if (cmp < 0) {
1135 Py_DECREF(thiskey);
1136 goto Fail;
1137 }
1138 if (cmp > 0 ||
1139 i >= a->ma_size ||
1140 a->ma_table[i].me_value == NULL)
1141 {
1142 /* Not the *smallest* a key; or maybe it is
1143 * but the compare shrunk the dict so we can't
1144 * find its associated value anymore; or
1145 * maybe it is but the compare deleted the
1146 * a[thiskey] entry.
1147 */
1148 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001149 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001150 }
Tim Peters95bf9392001-05-10 08:32:44 +00001151 }
1152
1153 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1154 thisaval = a->ma_table[i].me_value;
1155 assert(thisaval);
1156 Py_INCREF(thisaval); /* keep alive */
1157 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1158 if (thisbval == NULL)
1159 cmp = 0;
1160 else {
1161 /* both dicts have thiskey: same values? */
1162 cmp = PyObject_RichCompareBool(
1163 thisaval, thisbval, Py_EQ);
1164 if (cmp < 0) {
1165 Py_DECREF(thiskey);
1166 Py_DECREF(thisaval);
1167 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001168 }
1169 }
Tim Peters95bf9392001-05-10 08:32:44 +00001170 if (cmp == 0) {
1171 /* New winner. */
1172 Py_XDECREF(akey);
1173 Py_XDECREF(aval);
1174 akey = thiskey;
1175 aval = thisaval;
1176 }
1177 else {
1178 Py_DECREF(thiskey);
1179 Py_DECREF(thisaval);
1180 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001181 }
Tim Peters95bf9392001-05-10 08:32:44 +00001182 *pval = aval;
1183 return akey;
1184
1185Fail:
1186 Py_XDECREF(akey);
1187 Py_XDECREF(aval);
1188 *pval = NULL;
1189 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001190}
1191
1192static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001193dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001194{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001195 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001196 int res;
1197
1198 /* Compare lengths first */
1199 if (a->ma_used < b->ma_used)
1200 return -1; /* a is shorter */
1201 else if (a->ma_used > b->ma_used)
1202 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001203
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001204 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001205 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001206 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001207 if (adiff == NULL) {
1208 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001209 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001210 * must be equal.
1211 */
1212 res = PyErr_Occurred() ? -1 : 0;
1213 goto Finished;
1214 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001215 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001216 if (bdiff == NULL && PyErr_Occurred()) {
1217 assert(!bval);
1218 res = -1;
1219 goto Finished;
1220 }
1221 res = 0;
1222 if (bdiff) {
1223 /* bdiff == NULL "should be" impossible now, but perhaps
1224 * the last comparison done by the characterize() on a had
1225 * the side effect of making the dicts equal!
1226 */
1227 res = PyObject_Compare(adiff, bdiff);
1228 }
1229 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001230 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001231
1232Finished:
1233 Py_XDECREF(adiff);
1234 Py_XDECREF(bdiff);
1235 Py_XDECREF(aval);
1236 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001237 return res;
1238}
1239
Tim Peterse63415e2001-05-08 04:38:29 +00001240/* Return 1 if dicts equal, 0 if not, -1 if error.
1241 * Gets out as soon as any difference is detected.
1242 * Uses only Py_EQ comparison.
1243 */
1244static int
1245dict_equal(dictobject *a, dictobject *b)
1246{
1247 int i;
1248
1249 if (a->ma_used != b->ma_used)
1250 /* can't be equal if # of entries differ */
1251 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001252
Tim Peterse63415e2001-05-08 04:38:29 +00001253 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1254 for (i = 0; i < a->ma_size; i++) {
1255 PyObject *aval = a->ma_table[i].me_value;
1256 if (aval != NULL) {
1257 int cmp;
1258 PyObject *bval;
1259 PyObject *key = a->ma_table[i].me_key;
1260 /* temporarily bump aval's refcount to ensure it stays
1261 alive until we're done with it */
1262 Py_INCREF(aval);
1263 bval = PyDict_GetItem((PyObject *)b, key);
1264 if (bval == NULL) {
1265 Py_DECREF(aval);
1266 return 0;
1267 }
1268 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1269 Py_DECREF(aval);
1270 if (cmp <= 0) /* error or not equal */
1271 return cmp;
1272 }
1273 }
1274 return 1;
1275 }
1276
1277static PyObject *
1278dict_richcompare(PyObject *v, PyObject *w, int op)
1279{
1280 int cmp;
1281 PyObject *res;
1282
1283 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1284 res = Py_NotImplemented;
1285 }
1286 else if (op == Py_EQ || op == Py_NE) {
1287 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1288 if (cmp < 0)
1289 return NULL;
1290 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1291 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001292 else
1293 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001294 Py_INCREF(res);
1295 return res;
1296 }
1297
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001298static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001299dict_has_key(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001300{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001301 PyObject *key;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001302 long hash;
1303 register long ok;
Fred Drake52fccfd2000-02-23 15:47:16 +00001304 if (!PyArg_ParseTuple(args, "O:has_key", &key))
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001305 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001306#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001307 if (!PyString_Check(key) ||
1308 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001309#endif
Guido van Rossumca756f21997-01-23 19:39:29 +00001310 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001311 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001312 if (hash == -1)
1313 return NULL;
1314 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001315 ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001316 return PyInt_FromLong(ok);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001317}
1318
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001319static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001320dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001321{
1322 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001323 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001324 PyObject *val = NULL;
1325 long hash;
1326
Fred Drake52fccfd2000-02-23 15:47:16 +00001327 if (!PyArg_ParseTuple(args, "O|O:get", &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001328 return NULL;
1329
Barry Warsawc38c5da1997-10-06 17:49:20 +00001330#ifdef CACHE_HASH
1331 if (!PyString_Check(key) ||
1332 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1333#endif
1334 {
1335 hash = PyObject_Hash(key);
1336 if (hash == -1)
1337 return NULL;
1338 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001339 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Barry Warsaw320ac331997-10-20 17:26:25 +00001340
Barry Warsawc38c5da1997-10-06 17:49:20 +00001341 if (val == NULL)
1342 val = failobj;
1343 Py_INCREF(val);
1344 return val;
1345}
1346
1347
1348static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001349dict_setdefault(register dictobject *mp, PyObject *args)
1350{
1351 PyObject *key;
1352 PyObject *failobj = Py_None;
1353 PyObject *val = NULL;
1354 long hash;
1355
1356 if (!PyArg_ParseTuple(args, "O|O:setdefault", &key, &failobj))
1357 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001358
1359#ifdef CACHE_HASH
1360 if (!PyString_Check(key) ||
1361 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1362#endif
1363 {
1364 hash = PyObject_Hash(key);
1365 if (hash == -1)
1366 return NULL;
1367 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001368 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001369 if (val == NULL) {
1370 val = failobj;
1371 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1372 val = NULL;
1373 }
1374 Py_XINCREF(val);
1375 return val;
1376}
1377
1378
1379static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001380dict_clear(register dictobject *mp, PyObject *args)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001381{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001382 if (!PyArg_NoArgs(args))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001383 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001384 PyDict_Clear((PyObject *)mp);
1385 Py_INCREF(Py_None);
1386 return Py_None;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001387}
1388
Guido van Rossumba6ab842000-12-12 22:02:18 +00001389static PyObject *
1390dict_popitem(dictobject *mp, PyObject *args)
1391{
1392 int i = 0;
1393 dictentry *ep;
1394 PyObject *res;
1395
1396 if (!PyArg_NoArgs(args))
1397 return NULL;
Tim Petersf4b33f62001-06-02 05:42:29 +00001398 /* Allocate the result tuple before checking the size. Believe it
1399 * or not, this allocation could trigger a garbage collection which
1400 * could empty the dict, so if we checked the size first and that
1401 * happened, the result would be an infinite loop (searching for an
1402 * entry that no longer exists). Note that the usual popitem()
1403 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1404 * tuple away if the dict *is* empty isn't a significant
1405 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001406 */
1407 res = PyTuple_New(2);
1408 if (res == NULL)
1409 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001410 if (mp->ma_used == 0) {
1411 Py_DECREF(res);
1412 PyErr_SetString(PyExc_KeyError,
1413 "popitem(): dictionary is empty");
1414 return NULL;
1415 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001416 /* Set ep to "the first" dict entry with a value. We abuse the hash
1417 * field of slot 0 to hold a search finger:
1418 * If slot 0 has a value, use slot 0.
1419 * Else slot 0 is being used to hold a search finger,
1420 * and we use its hash value as the first index to look.
1421 */
1422 ep = &mp->ma_table[0];
1423 if (ep->me_value == NULL) {
1424 i = (int)ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001425 /* The hash field may be a real hash value, or it may be a
1426 * legit search finger, or it may be a once-legit search
1427 * finger that's out of bounds now because it wrapped around
1428 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001429 */
1430 if (i >= mp->ma_size || i < 1)
1431 i = 1; /* skip slot 0 */
1432 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1433 i++;
1434 if (i >= mp->ma_size)
1435 i = 1;
1436 }
1437 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001438 PyTuple_SET_ITEM(res, 0, ep->me_key);
1439 PyTuple_SET_ITEM(res, 1, ep->me_value);
1440 Py_INCREF(dummy);
1441 ep->me_key = dummy;
1442 ep->me_value = NULL;
1443 mp->ma_used--;
1444 assert(mp->ma_table[0].me_value == NULL);
1445 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001446 return res;
1447}
1448
Jeremy Hylton8caad492000-06-23 14:18:11 +00001449static int
1450dict_traverse(PyObject *op, visitproc visit, void *arg)
1451{
1452 int i = 0, err;
1453 PyObject *pk;
1454 PyObject *pv;
1455
1456 while (PyDict_Next(op, &i, &pk, &pv)) {
1457 err = visit(pk, arg);
1458 if (err)
1459 return err;
1460 err = visit(pv, arg);
1461 if (err)
1462 return err;
1463 }
1464 return 0;
1465}
1466
1467static int
1468dict_tp_clear(PyObject *op)
1469{
1470 PyDict_Clear(op);
1471 return 0;
1472}
1473
Tim Petersf7f88b12000-12-13 23:18:45 +00001474
Guido van Rossum09e563a2001-05-01 12:10:21 +00001475staticforward PyObject *dictiter_new(dictobject *, binaryfunc);
1476
1477static PyObject *
1478select_key(PyObject *key, PyObject *value)
1479{
1480 Py_INCREF(key);
1481 return key;
1482}
1483
1484static PyObject *
1485select_value(PyObject *key, PyObject *value)
1486{
1487 Py_INCREF(value);
1488 return value;
1489}
1490
1491static PyObject *
1492select_item(PyObject *key, PyObject *value)
1493{
1494 PyObject *res = PyTuple_New(2);
1495
1496 if (res != NULL) {
1497 Py_INCREF(key);
1498 Py_INCREF(value);
1499 PyTuple_SET_ITEM(res, 0, key);
1500 PyTuple_SET_ITEM(res, 1, value);
1501 }
1502 return res;
1503}
1504
1505static PyObject *
1506dict_iterkeys(dictobject *dict, PyObject *args)
1507{
1508 if (!PyArg_ParseTuple(args, ""))
1509 return NULL;
1510 return dictiter_new(dict, select_key);
1511}
1512
1513static PyObject *
1514dict_itervalues(dictobject *dict, PyObject *args)
1515{
1516 if (!PyArg_ParseTuple(args, ""))
1517 return NULL;
1518 return dictiter_new(dict, select_value);
1519}
1520
1521static PyObject *
1522dict_iteritems(dictobject *dict, PyObject *args)
1523{
1524 if (!PyArg_ParseTuple(args, ""))
1525 return NULL;
1526 return dictiter_new(dict, select_item);
1527}
1528
1529
Tim Petersf7f88b12000-12-13 23:18:45 +00001530static char has_key__doc__[] =
1531"D.has_key(k) -> 1 if D has a key k, else 0";
1532
1533static char get__doc__[] =
1534"D.get(k[,d]) -> D[k] if D.has_key(k), else d. d defaults to None.";
1535
1536static char setdefault_doc__[] =
1537"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if not D.has_key(k)";
1538
1539static char popitem__doc__[] =
1540"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
15412-tuple; but raise KeyError if D is empty";
1542
1543static char keys__doc__[] =
1544"D.keys() -> list of D's keys";
1545
1546static char items__doc__[] =
1547"D.items() -> list of D's (key, value) pairs, as 2-tuples";
1548
1549static char values__doc__[] =
1550"D.values() -> list of D's values";
1551
1552static char update__doc__[] =
1553"D.update(E) -> None. Update D from E: for k in E.keys(): D[k] = E[k]";
1554
1555static char clear__doc__[] =
1556"D.clear() -> None. Remove all items from D.";
1557
1558static char copy__doc__[] =
1559"D.copy() -> a shallow copy of D";
1560
Guido van Rossum09e563a2001-05-01 12:10:21 +00001561static char iterkeys__doc__[] =
1562"D.iterkeys() -> an iterator over the keys of D";
1563
1564static char itervalues__doc__[] =
1565"D.itervalues() -> an iterator over the values of D";
1566
1567static char iteritems__doc__[] =
1568"D.iteritems() -> an iterator over the (key, value) items of D";
1569
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001570static PyMethodDef mapp_methods[] = {
Tim Petersf7f88b12000-12-13 23:18:45 +00001571 {"has_key", (PyCFunction)dict_has_key, METH_VARARGS,
1572 has_key__doc__},
1573 {"get", (PyCFunction)dict_get, METH_VARARGS,
1574 get__doc__},
1575 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1576 setdefault_doc__},
1577 {"popitem", (PyCFunction)dict_popitem, METH_OLDARGS,
1578 popitem__doc__},
1579 {"keys", (PyCFunction)dict_keys, METH_OLDARGS,
1580 keys__doc__},
1581 {"items", (PyCFunction)dict_items, METH_OLDARGS,
1582 items__doc__},
1583 {"values", (PyCFunction)dict_values, METH_OLDARGS,
1584 values__doc__},
1585 {"update", (PyCFunction)dict_update, METH_OLDARGS,
1586 update__doc__},
1587 {"clear", (PyCFunction)dict_clear, METH_OLDARGS,
1588 clear__doc__},
1589 {"copy", (PyCFunction)dict_copy, METH_OLDARGS,
1590 copy__doc__},
Guido van Rossum09e563a2001-05-01 12:10:21 +00001591 {"iterkeys", (PyCFunction)dict_iterkeys, METH_VARARGS,
1592 iterkeys__doc__},
1593 {"itervalues", (PyCFunction)dict_itervalues, METH_VARARGS,
1594 itervalues__doc__},
1595 {"iteritems", (PyCFunction)dict_iteritems, METH_VARARGS,
1596 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001597 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001598};
1599
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001600static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001601dict_getattr(dictobject *mp, char *name)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001602{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001603 return Py_FindMethod(mapp_methods, (PyObject *)mp, name);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001604}
1605
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001606static int
1607dict_contains(dictobject *mp, PyObject *key)
1608{
1609 long hash;
1610
1611#ifdef CACHE_HASH
1612 if (!PyString_Check(key) ||
1613 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1614#endif
1615 {
1616 hash = PyObject_Hash(key);
1617 if (hash == -1)
1618 return -1;
1619 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001620 return (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001621}
1622
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001623/* Hack to implement "key in dict" */
1624static PySequenceMethods dict_as_sequence = {
1625 0, /* sq_length */
1626 0, /* sq_concat */
1627 0, /* sq_repeat */
1628 0, /* sq_item */
1629 0, /* sq_slice */
1630 0, /* sq_ass_item */
1631 0, /* sq_ass_slice */
1632 (objobjproc)dict_contains, /* sq_contains */
1633 0, /* sq_inplace_concat */
1634 0, /* sq_inplace_repeat */
1635};
1636
Guido van Rossum09e563a2001-05-01 12:10:21 +00001637static PyObject *
1638dict_iter(dictobject *dict)
1639{
1640 return dictiter_new(dict, select_key);
1641}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001642
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001643PyTypeObject PyDict_Type = {
1644 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001645 0,
Guido van Rossum9bfef441993-03-29 10:43:31 +00001646 "dictionary",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001647 sizeof(dictobject) + PyGC_HEAD_SIZE,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001648 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001649 (destructor)dict_dealloc, /* tp_dealloc */
1650 (printfunc)dict_print, /* tp_print */
1651 (getattrfunc)dict_getattr, /* tp_getattr */
1652 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00001653 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001654 (reprfunc)dict_repr, /* tp_repr */
1655 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001656 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001657 &dict_as_mapping, /* tp_as_mapping */
1658 0, /* tp_hash */
1659 0, /* tp_call */
1660 0, /* tp_str */
1661 0, /* tp_getattro */
1662 0, /* tp_setattro */
1663 0, /* tp_as_buffer */
1664 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
1665 0, /* tp_doc */
1666 (traverseproc)dict_traverse, /* tp_traverse */
1667 (inquiry)dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001668 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001669 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001670 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001671 0, /* tp_iternext */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001672};
1673
Guido van Rossum3cca2451997-05-16 14:23:33 +00001674/* For backward compatibility with old dictionary interface */
1675
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001676PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001677PyDict_GetItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001678{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001679 PyObject *kv, *rv;
1680 kv = PyString_FromString(key);
1681 if (kv == NULL)
1682 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001683 rv = PyDict_GetItem(v, kv);
1684 Py_DECREF(kv);
1685 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001686}
1687
1688int
Tim Peters1f5871e2000-07-04 17:44:48 +00001689PyDict_SetItemString(PyObject *v, char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001690{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001691 PyObject *kv;
1692 int err;
1693 kv = PyString_FromString(key);
1694 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001695 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00001696 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00001697 err = PyDict_SetItem(v, kv, item);
1698 Py_DECREF(kv);
1699 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001700}
1701
1702int
Tim Peters1f5871e2000-07-04 17:44:48 +00001703PyDict_DelItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001704{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001705 PyObject *kv;
1706 int err;
1707 kv = PyString_FromString(key);
1708 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001709 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001710 err = PyDict_DelItem(v, kv);
1711 Py_DECREF(kv);
1712 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001713}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001714
1715/* Dictionary iterator type */
1716
1717extern PyTypeObject PyDictIter_Type; /* Forward */
1718
1719typedef struct {
1720 PyObject_HEAD
1721 dictobject *di_dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001722 int di_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001723 int di_pos;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001724 binaryfunc di_select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001725} dictiterobject;
1726
1727static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001728dictiter_new(dictobject *dict, binaryfunc select)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001729{
1730 dictiterobject *di;
1731 di = PyObject_NEW(dictiterobject, &PyDictIter_Type);
1732 if (di == NULL)
1733 return NULL;
1734 Py_INCREF(dict);
1735 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001736 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001737 di->di_pos = 0;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001738 di->di_select = select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001739 return (PyObject *)di;
1740}
1741
1742static void
1743dictiter_dealloc(dictiterobject *di)
1744{
1745 Py_DECREF(di->di_dict);
1746 PyObject_DEL(di);
1747}
1748
1749static PyObject *
1750dictiter_next(dictiterobject *di, PyObject *args)
1751{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001752 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001753
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001754 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001755 PyErr_SetString(PyExc_RuntimeError,
1756 "dictionary changed size during iteration");
1757 return NULL;
1758 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001759 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1760 return (*di->di_select)(key, value);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001761 }
1762 PyErr_SetObject(PyExc_StopIteration, Py_None);
1763 return NULL;
1764}
1765
1766static PyObject *
1767dictiter_getiter(PyObject *it)
1768{
1769 Py_INCREF(it);
1770 return it;
1771}
1772
1773static PyMethodDef dictiter_methods[] = {
1774 {"next", (PyCFunction)dictiter_next, METH_VARARGS,
1775 "it.next() -- get the next value, or raise StopIteration"},
1776 {NULL, NULL} /* sentinel */
1777};
1778
1779static PyObject *
Guido van Rossum213c7a62001-04-23 14:08:49 +00001780dictiter_getattr(dictiterobject *di, char *name)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001781{
Guido van Rossum213c7a62001-04-23 14:08:49 +00001782 return Py_FindMethod(dictiter_methods, (PyObject *)di, name);
1783}
1784
1785static PyObject *dictiter_iternext(dictiterobject *di)
1786{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001787 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001788
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001789 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00001790 PyErr_SetString(PyExc_RuntimeError,
1791 "dictionary changed size during iteration");
1792 return NULL;
1793 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001794 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1795 return (*di->di_select)(key, value);
Guido van Rossum213c7a62001-04-23 14:08:49 +00001796 }
1797 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001798}
1799
1800PyTypeObject PyDictIter_Type = {
1801 PyObject_HEAD_INIT(&PyType_Type)
1802 0, /* ob_size */
1803 "dictionary-iterator", /* tp_name */
1804 sizeof(dictiterobject), /* tp_basicsize */
1805 0, /* tp_itemsize */
1806 /* methods */
1807 (destructor)dictiter_dealloc, /* tp_dealloc */
1808 0, /* tp_print */
1809 (getattrfunc)dictiter_getattr, /* tp_getattr */
1810 0, /* tp_setattr */
1811 0, /* tp_compare */
1812 0, /* tp_repr */
1813 0, /* tp_as_number */
1814 0, /* tp_as_sequence */
1815 0, /* tp_as_mapping */
1816 0, /* tp_hash */
1817 0, /* tp_call */
1818 0, /* tp_str */
1819 0, /* tp_getattro */
1820 0, /* tp_setattro */
1821 0, /* tp_as_buffer */
1822 Py_TPFLAGS_DEFAULT, /* tp_flags */
1823 0, /* tp_doc */
1824 0, /* tp_traverse */
1825 0, /* tp_clear */
1826 0, /* tp_richcompare */
1827 0, /* tp_weaklistoffset */
1828 (getiterfunc)dictiter_getiter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001829 (iternextfunc)dictiter_iternext, /* tp_iternext */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001830};