blob: 8b58166ca43ec5f006f6faa005f08a56957077e0 [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;
Fred Drakec88b99c2000-08-31 19:04:07 +0000254 register int restore_error = 0;
255 register int checked_error = 0;
256 register int cmp;
257 PyObject *err_type, *err_value, *err_tb;
Tim Peterseb28ef22001-06-02 05:27:19 +0000258
Tim Peters2f228e72001-05-13 00:19:31 +0000259 i = hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000260 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000261 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000262 return ep;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000263 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000264 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000265 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000266 if (ep->me_hash == hash) {
267 /* error can't have been checked yet */
268 checked_error = 1;
269 if (PyErr_Occurred()) {
270 restore_error = 1;
271 PyErr_Fetch(&err_type, &err_value, &err_tb);
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 Rossumfd7a0b81997-08-18 21:52:47 +0000282 }
283 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000284 }
Tim Peters15d49292001-05-27 07:39:22 +0000285
Tim Peters342c65e2001-05-13 06:43:53 +0000286 /* In the loop, me_key == dummy is by far (factor of 100s) the
287 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000288 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
289 i = (i << 2) + i + perturb + 1;
290 ep = &ep0[i & mask];
Guido van Rossum16e93a81997-01-28 00:00:11 +0000291 if (ep->me_key == NULL) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000292 if (restore_error)
293 PyErr_Restore(err_type, err_value, err_tb);
Tim Peters342c65e2001-05-13 06:43:53 +0000294 return freeslot == NULL ? ep : freeslot;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000295 }
Tim Peters342c65e2001-05-13 06:43:53 +0000296 if (ep->me_key == key) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000297 if (restore_error)
298 PyErr_Restore(err_type, err_value, err_tb);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000299 return ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000300 }
Tim Peters342c65e2001-05-13 06:43:53 +0000301 else if (ep->me_hash == hash && ep->me_key != dummy) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000302 if (!checked_error) {
303 checked_error = 1;
304 if (PyErr_Occurred()) {
305 restore_error = 1;
306 PyErr_Fetch(&err_type, &err_value,
307 &err_tb);
308 }
309 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000310 cmp = PyObject_RichCompareBool(ep->me_key, key, Py_EQ);
311 if (cmp > 0) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000312 if (restore_error)
313 PyErr_Restore(err_type, err_value,
314 err_tb);
315 return ep;
316 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000317 else if (cmp < 0)
318 PyErr_Clear();
Guido van Rossum16e93a81997-01-28 00:00:11 +0000319 }
Tim Peters342c65e2001-05-13 06:43:53 +0000320 else if (ep->me_key == dummy && freeslot == NULL)
321 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000322 }
323}
324
325/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000326 * Hacked up version of lookdict which can assume keys are always strings;
327 * this assumption allows testing for errors during PyObject_Compare() to
328 * be dropped; string-string comparisons never raise exceptions. This also
329 * means we don't need to go through PyObject_Compare(); we can always use
Martin v. Löwiscd353062001-05-24 16:56:35 +0000330 * _PyString_Eq directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000331 *
332 * This really only becomes meaningful if proper error handling in lookdict()
333 * is too expensive.
334 */
335static dictentry *
336lookdict_string(dictobject *mp, PyObject *key, register long hash)
337{
338 register int i;
Tim Peterseb28ef22001-06-02 05:27:19 +0000339 register unsigned int perturb;
Fred Drake1bff34a2000-08-31 19:31:38 +0000340 register dictentry *freeslot;
341 register unsigned int mask = mp->ma_size-1;
342 dictentry *ep0 = mp->ma_table;
343 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000344
345 /* make sure this function doesn't have to handle non-string keys */
346 if (!PyString_Check(key)) {
347#ifdef SHOW_CONVERSION_COUNTS
348 ++converted;
349#endif
350 mp->ma_lookup = lookdict;
351 return lookdict(mp, key, hash);
352 }
Tim Peters2f228e72001-05-13 00:19:31 +0000353 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000354 ep = &ep0[i];
355 if (ep->me_key == NULL || ep->me_key == key)
356 return ep;
357 if (ep->me_key == dummy)
358 freeslot = ep;
359 else {
360 if (ep->me_hash == hash
Martin v. Löwiscd353062001-05-24 16:56:35 +0000361 && _PyString_Eq(ep->me_key, key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000362 return ep;
363 }
364 freeslot = NULL;
365 }
Tim Peters15d49292001-05-27 07:39:22 +0000366
Tim Peters342c65e2001-05-13 06:43:53 +0000367 /* In the loop, me_key == dummy is by far (factor of 100s) the
368 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000369 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
370 i = (i << 2) + i + perturb + 1;
371 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000372 if (ep->me_key == NULL)
373 return freeslot == NULL ? ep : freeslot;
374 if (ep->me_key == key
375 || (ep->me_hash == hash
376 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000377 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000378 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000379 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000380 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000381 }
382}
383
384/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000385Internal routine to insert a new item into the table.
386Used both by the internal resize routine and by the public insert routine.
387Eats a reference to key and one to value.
388*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000389static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000390insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000391{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000392 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000393 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000394 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000395 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000396 old_value = ep->me_value;
397 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000398 Py_DECREF(old_value); /* which **CAN** re-enter */
399 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000400 }
401 else {
402 if (ep->me_key == NULL)
403 mp->ma_fill++;
404 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000405 Py_DECREF(ep->me_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000406 ep->me_key = key;
407 ep->me_hash = hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000408 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000409 mp->ma_used++;
410 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000411}
412
413/*
414Restructure the table by allocating a new table and reinserting all
415items again. When entries have been deleted, the new table may
416actually be smaller than the old one.
417*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000418static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000419dictresize(dictobject *mp, int minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000420{
Tim Peterseb28ef22001-06-02 05:27:19 +0000421 int newsize;
Tim Peters0c6010b2001-05-23 23:33:57 +0000422 dictentry *oldtable, *newtable, *ep;
423 int i;
424 int is_oldtable_malloced;
425 dictentry small_copy[MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000426
427 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000428
Tim Peterseb28ef22001-06-02 05:27:19 +0000429 /* Find the smallest table size > minused. */
430 for (newsize = MINSIZE;
431 newsize <= minused && newsize > 0;
432 newsize <<= 1)
433 ;
434 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000435 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000436 return -1;
437 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000438
439 /* Get space for a new table. */
440 oldtable = mp->ma_table;
441 assert(oldtable != NULL);
442 is_oldtable_malloced = oldtable != mp->ma_smalltable;
443
Tim Petersdea48ec2001-05-22 20:40:22 +0000444 if (newsize == MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000445 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000446 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000447 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000448 if (mp->ma_fill == mp->ma_used) {
449 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000450 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000451 }
452 /* We're not going to resize it, but rebuild the
453 table anyway to purge old dummy entries.
454 Subtle: This is *necessary* if fill==size,
455 as lookdict needs at least one virgin slot to
456 terminate failing searches. If fill < size, it's
457 merely desirable, as dummies slow searches. */
458 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000459 memcpy(small_copy, oldtable, sizeof(small_copy));
460 oldtable = small_copy;
461 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000462 }
463 else {
464 newtable = PyMem_NEW(dictentry, newsize);
465 if (newtable == NULL) {
466 PyErr_NoMemory();
467 return -1;
468 }
469 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000470
471 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000472 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000473 mp->ma_table = newtable;
Tim Petersdea48ec2001-05-22 20:40:22 +0000474 mp->ma_size = newsize;
475 memset(newtable, 0, sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000476 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000477 i = mp->ma_fill;
478 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000479
Tim Peters19283142001-05-17 22:25:34 +0000480 /* Copy the data over; this is refcount-neutral for active entries;
481 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000482 for (ep = oldtable; i > 0; ep++) {
483 if (ep->me_value != NULL) { /* active entry */
484 --i;
Tim Peters19283142001-05-17 22:25:34 +0000485 insertdict(mp, ep->me_key, ep->me_hash, ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000486 }
Tim Peters19283142001-05-17 22:25:34 +0000487 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000488 --i;
Tim Peters19283142001-05-17 22:25:34 +0000489 assert(ep->me_key == dummy);
490 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000491 }
Tim Peters19283142001-05-17 22:25:34 +0000492 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000493 }
494
Tim Peters0c6010b2001-05-23 23:33:57 +0000495 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000496 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000497 return 0;
498}
499
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000500PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000501PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000502{
503 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000504 dictobject *mp = (dictobject *)op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000505 if (!PyDict_Check(op)) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000506 return NULL;
507 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000508#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000509 if (!PyString_Check(key) ||
510 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000511#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000512 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000513 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000514 if (hash == -1) {
515 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000516 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000517 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000518 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000519 return (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000520}
521
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000522/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
523 * dictionary if it is merely replacing the value for an existing key.
524 * This is means that it's safe to loop over a dictionary with
525 * PyDict_Next() and occasionally replace a value -- but you can't
526 * insert new keys or remove them.
527 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000528int
Tim Peters1f5871e2000-07-04 17:44:48 +0000529PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000530{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000531 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000532 register long hash;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000533 register int n_used;
534
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000535 if (!PyDict_Check(op)) {
536 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000537 return -1;
538 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000539 mp = (dictobject *)op;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000540#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000541 if (PyString_Check(key)) {
Guido van Rossum2a61e741997-01-18 07:55:05 +0000542#ifdef INTERN_STRINGS
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000543 if (((PyStringObject *)key)->ob_sinterned != NULL) {
544 key = ((PyStringObject *)key)->ob_sinterned;
545 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000546 }
547 else
548#endif
549 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000550 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000551 if (hash == -1)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000552 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000553 }
554 }
555 else
556#endif
557 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000559 if (hash == -1)
560 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000561 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000562 assert(mp->ma_fill < mp->ma_size);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000563 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000564 Py_INCREF(value);
565 Py_INCREF(key);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000566 insertdict(mp, key, hash, value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000567 /* If we added a key, we can safely resize. Otherwise skip this!
568 * If fill >= 2/3 size, adjust size. Normally, this doubles the
569 * size, but it's also possible for the dict to shrink (if ma_fill is
570 * much larger than ma_used, meaning a lot of dict keys have been
571 * deleted).
572 */
573 if (mp->ma_used > n_used && mp->ma_fill*3 >= mp->ma_size*2) {
574 if (dictresize(mp, mp->ma_used*2) != 0)
575 return -1;
576 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000577 return 0;
578}
579
580int
Tim Peters1f5871e2000-07-04 17:44:48 +0000581PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000582{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000583 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000584 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000585 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000586 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000587
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000588 if (!PyDict_Check(op)) {
589 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000590 return -1;
591 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000592#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000593 if (!PyString_Check(key) ||
594 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000595#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000596 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000597 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000598 if (hash == -1)
599 return -1;
600 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000601 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000602 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000603 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000604 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000605 return -1;
606 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000607 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000608 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000609 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000610 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000611 ep->me_value = NULL;
612 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000613 Py_DECREF(old_value);
614 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000615 return 0;
616}
617
Guido van Rossum25831651993-05-19 14:50:45 +0000618void
Tim Peters1f5871e2000-07-04 17:44:48 +0000619PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000620{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000621 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000622 dictentry *ep, *table;
623 int table_is_malloced;
624 int fill;
625 dictentry small_copy[MINSIZE];
626#ifdef Py_DEBUG
627 int i, n;
628#endif
629
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000630 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000631 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000632 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000633#ifdef Py_DEBUG
Guido van Rossumd7047b31995-01-02 19:07:15 +0000634 n = mp->ma_size;
Tim Petersdea48ec2001-05-22 20:40:22 +0000635 i = 0;
636#endif
637
638 table = mp->ma_table;
639 assert(table != NULL);
640 table_is_malloced = table != mp->ma_smalltable;
641
642 /* This is delicate. During the process of clearing the dict,
643 * decrefs can cause the dict to mutate. To avoid fatal confusion
644 * (voice of experience), we have to make the dict empty before
645 * clearing the slots, and never refer to anything via mp->xxx while
646 * clearing.
647 */
648 fill = mp->ma_fill;
649 if (table_is_malloced)
650 empty_to_minsize(mp);
651
652 else if (fill > 0) {
653 /* It's a small table with something that needs to be cleared.
654 * Afraid the only safe way is to copy the dict entries into
655 * another small table first.
656 */
657 memcpy(small_copy, table, sizeof(small_copy));
658 table = small_copy;
659 empty_to_minsize(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000660 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000661 /* else it's a small table that's already empty */
662
663 /* Now we can finally clear things. If C had refcounts, we could
664 * assert that the refcount on table is 1 now, i.e. that this function
665 * has unique access to it, so decref side-effects can't alter it.
666 */
667 for (ep = table; fill > 0; ++ep) {
668#ifdef Py_DEBUG
669 assert(i < n);
670 ++i;
671#endif
672 if (ep->me_key) {
673 --fill;
674 Py_DECREF(ep->me_key);
675 Py_XDECREF(ep->me_value);
676 }
677#ifdef Py_DEBUG
678 else
679 assert(ep->me_value == NULL);
680#endif
681 }
682
683 if (table_is_malloced)
684 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000685}
686
Tim Peters67830702001-03-21 19:23:56 +0000687/* CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
688 * mutates the dict. One exception: it is safe if the loop merely changes
689 * the values associated with the keys (but doesn't insert new keys or
690 * delete keys), via PyDict_SetItem().
691 */
Guido van Rossum25831651993-05-19 14:50:45 +0000692int
Tim Peters1f5871e2000-07-04 17:44:48 +0000693PyDict_Next(PyObject *op, int *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000694{
Guido van Rossum25831651993-05-19 14:50:45 +0000695 int i;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000696 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000697 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000698 return 0;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000699 mp = (dictobject *)op;
Guido van Rossum25831651993-05-19 14:50:45 +0000700 i = *ppos;
701 if (i < 0)
702 return 0;
703 while (i < mp->ma_size && mp->ma_table[i].me_value == NULL)
704 i++;
705 *ppos = i+1;
706 if (i >= mp->ma_size)
707 return 0;
708 if (pkey)
709 *pkey = mp->ma_table[i].me_key;
710 if (pvalue)
711 *pvalue = mp->ma_table[i].me_value;
712 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000713}
714
715/* Methods */
716
717static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000718dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000719{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000720 register dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000721 int fill = mp->ma_fill;
Guido van Rossumd724b232000-03-13 16:01:29 +0000722 Py_TRASHCAN_SAFE_BEGIN(mp)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000723 PyObject_GC_Fini(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000724 for (ep = mp->ma_table; fill > 0; ep++) {
725 if (ep->me_key) {
726 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000727 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000728 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000729 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000730 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000731 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000732 PyMem_DEL(mp->ma_table);
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000733 mp = (dictobject *) PyObject_AS_GC(mp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000734 PyObject_DEL(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000735 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000736}
737
738static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000739dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000740{
741 register int i;
742 register int any;
Guido van Rossum255443b1998-04-10 22:47:14 +0000743
744 i = Py_ReprEnter((PyObject*)mp);
745 if (i != 0) {
746 if (i < 0)
747 return i;
748 fprintf(fp, "{...}");
749 return 0;
750 }
751
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000752 fprintf(fp, "{");
753 any = 0;
Tim Peters23cf6be2001-06-02 08:02:56 +0000754 for (i = 0; i < mp->ma_size; i++) {
755 dictentry *ep = mp->ma_table + i;
756 PyObject *pvalue = ep->me_value;
757 if (pvalue != NULL) {
758 /* Prevent PyObject_Repr from deleting value during
759 key format */
760 Py_INCREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000761 if (any++ > 0)
762 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000763 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000764 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000765 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000766 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000767 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000768 fprintf(fp, ": ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000769 if (PyObject_Print(ep->me_value, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000770 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000771 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000772 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000773 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000774 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000775 }
776 }
777 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000778 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000779 return 0;
780}
781
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000782static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000783dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000784{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000785 auto PyObject *v;
786 PyObject *sepa, *colon;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000787 register int i;
788 register int any;
Guido van Rossum255443b1998-04-10 22:47:14 +0000789
790 i = Py_ReprEnter((PyObject*)mp);
791 if (i != 0) {
792 if (i > 0)
793 return PyString_FromString("{...}");
794 return NULL;
795 }
796
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000797 v = PyString_FromString("{");
798 sepa = PyString_FromString(", ");
799 colon = PyString_FromString(": ");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000800 any = 0;
Tim Peters23cf6be2001-06-02 08:02:56 +0000801 for (i = 0; i < mp->ma_size && v; i++) {
802 dictentry *ep = mp->ma_table + i;
803 PyObject *pvalue = ep->me_value;
804 if (pvalue != NULL) {
805 /* Prevent PyObject_Repr from deleting value during
806 key format */
807 Py_INCREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000808 if (any++)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000809 PyString_Concat(&v, sepa);
810 PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_key));
811 PyString_Concat(&v, colon);
Tim Peters23cf6be2001-06-02 08:02:56 +0000812 PyString_ConcatAndDel(&v, PyObject_Repr(pvalue));
813 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000814 }
815 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000816 PyString_ConcatAndDel(&v, PyString_FromString("}"));
Guido van Rossum255443b1998-04-10 22:47:14 +0000817 Py_ReprLeave((PyObject*)mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000818 Py_XDECREF(sepa);
819 Py_XDECREF(colon);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000820 return v;
821}
822
823static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000824dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000825{
826 return mp->ma_used;
827}
828
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000829static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000830dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000831{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000832 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000833 long hash;
Tim Petersdea48ec2001-05-22 20:40:22 +0000834 assert(mp->ma_table != NULL);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000835#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000836 if (!PyString_Check(key) ||
837 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000838#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000839 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000840 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000841 if (hash == -1)
842 return NULL;
843 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000844 v = (mp->ma_lookup)(mp, key, hash) -> me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000845 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000846 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000847 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000848 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000849 return v;
850}
851
852static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000853dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000854{
855 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000856 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000857 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000858 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000859}
860
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000861static PyMappingMethods dict_as_mapping = {
862 (inquiry)dict_length, /*mp_length*/
863 (binaryfunc)dict_subscript, /*mp_subscript*/
864 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000865};
866
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000867static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000868dict_keys(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000869{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000870 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000871 register int i, j, n;
872
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000873 if (!PyArg_NoArgs(args))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000874 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000875 again:
876 n = mp->ma_used;
877 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000878 if (v == NULL)
879 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000880 if (n != mp->ma_used) {
881 /* Durnit. The allocations caused the dict to resize.
882 * Just start over, this shouldn't normally happen.
883 */
884 Py_DECREF(v);
885 goto again;
886 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000887 for (i = 0, j = 0; i < mp->ma_size; i++) {
888 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000889 PyObject *key = mp->ma_table[i].me_key;
890 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000891 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000892 j++;
893 }
894 }
895 return v;
896}
897
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000898static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000899dict_values(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000900{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000901 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000902 register int i, j, n;
903
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000904 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000905 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000906 again:
907 n = mp->ma_used;
908 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000909 if (v == NULL)
910 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000911 if (n != mp->ma_used) {
912 /* Durnit. The allocations caused the dict to resize.
913 * Just start over, this shouldn't normally happen.
914 */
915 Py_DECREF(v);
916 goto again;
917 }
Guido van Rossum25831651993-05-19 14:50:45 +0000918 for (i = 0, j = 0; i < mp->ma_size; i++) {
919 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000920 PyObject *value = mp->ma_table[i].me_value;
921 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000922 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000923 j++;
924 }
925 }
926 return v;
927}
928
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000929static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000930dict_items(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000931{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000932 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000933 register int i, j, n;
934 PyObject *item, *key, *value;
935
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000936 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000937 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000938 /* Preallocate the list of tuples, to avoid allocations during
939 * the loop over the items, which could trigger GC, which
940 * could resize the dict. :-(
941 */
942 again:
943 n = mp->ma_used;
944 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000945 if (v == NULL)
946 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000947 for (i = 0; i < n; i++) {
948 item = PyTuple_New(2);
949 if (item == NULL) {
950 Py_DECREF(v);
951 return NULL;
952 }
953 PyList_SET_ITEM(v, i, item);
954 }
955 if (n != mp->ma_used) {
956 /* Durnit. The allocations caused the dict to resize.
957 * Just start over, this shouldn't normally happen.
958 */
959 Py_DECREF(v);
960 goto again;
961 }
962 /* Nothing we do below makes any function calls. */
Guido van Rossum25831651993-05-19 14:50:45 +0000963 for (i = 0, j = 0; i < mp->ma_size; i++) {
964 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000965 key = mp->ma_table[i].me_key;
966 value = mp->ma_table[i].me_value;
967 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000968 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000969 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000970 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000971 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000972 j++;
973 }
974 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000975 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +0000976 return v;
977}
978
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000979static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000980dict_update(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000981{
982 register int i;
983 dictobject *other;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000984 dictentry *entry;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000985 if (!PyArg_Parse(args, "O!", &PyDict_Type, &other))
986 return NULL;
Jeremy Hylton1fb60882001-01-03 22:34:59 +0000987 if (other == mp || other->ma_used == 0)
988 goto done; /* a.update(a) or a.update({}); nothing to do */
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000989 /* Do one big resize at the start, rather than incrementally
990 resizing as we insert new items. Expect that there will be
991 no (or few) overlapping keys. */
992 if ((mp->ma_fill + other->ma_used)*3 >= mp->ma_size*2) {
993 if (dictresize(mp, (mp->ma_used + other->ma_used)*3/2) != 0)
994 return NULL;
995 }
996 for (i = 0; i < other->ma_size; i++) {
997 entry = &other->ma_table[i];
998 if (entry->me_value != NULL) {
999 Py_INCREF(entry->me_key);
1000 Py_INCREF(entry->me_value);
1001 insertdict(mp, entry->me_key, entry->me_hash,
1002 entry->me_value);
1003 }
1004 }
1005 done:
1006 Py_INCREF(Py_None);
1007 return Py_None;
1008}
1009
1010static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001011dict_copy(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001012{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001013 if (!PyArg_Parse(args, ""))
1014 return NULL;
1015 return PyDict_Copy((PyObject*)mp);
1016}
1017
1018PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001019PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001020{
1021 register dictobject *mp;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001022 register int i;
1023 dictobject *copy;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001024 dictentry *entry;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001025
1026 if (o == NULL || !PyDict_Check(o)) {
1027 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001028 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001029 }
1030 mp = (dictobject *)o;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001031 copy = (dictobject *)PyDict_New();
1032 if (copy == NULL)
1033 return NULL;
1034 if (mp->ma_used > 0) {
1035 if (dictresize(copy, mp->ma_used*3/2) != 0)
1036 return NULL;
1037 for (i = 0; i < mp->ma_size; i++) {
1038 entry = &mp->ma_table[i];
1039 if (entry->me_value != NULL) {
1040 Py_INCREF(entry->me_key);
1041 Py_INCREF(entry->me_value);
1042 insertdict(copy, entry->me_key, entry->me_hash,
1043 entry->me_value);
1044 }
1045 }
1046 }
1047 return (PyObject *)copy;
1048}
1049
Guido van Rossum4199fac1993-11-05 10:18:44 +00001050int
Tim Peters1f5871e2000-07-04 17:44:48 +00001051PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001052{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001053 if (mp == NULL || !PyDict_Check(mp)) {
1054 PyErr_BadInternalCall();
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001055 return 0;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001056 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001057 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001058}
1059
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001060PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001061PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001062{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001063 if (mp == NULL || !PyDict_Check(mp)) {
1064 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001065 return NULL;
1066 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001067 return dict_keys((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001068}
1069
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001070PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001071PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001072{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001073 if (mp == NULL || !PyDict_Check(mp)) {
1074 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001075 return NULL;
1076 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001077 return dict_values((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +00001078}
1079
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001080PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001081PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001082{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001083 if (mp == NULL || !PyDict_Check(mp)) {
1084 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001085 return NULL;
1086 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001087 return dict_items((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +00001088}
1089
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001090/* Subroutine which returns the smallest key in a for which b's value
1091 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001092 pval argument. Both are NULL if no key in a is found for which b's status
1093 differs. The refcounts on (and only on) non-NULL *pval and function return
1094 values must be decremented by the caller (characterize() increments them
1095 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1096 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001097
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001098static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001099characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001100{
Tim Peters95bf9392001-05-10 08:32:44 +00001101 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1102 PyObject *aval = NULL; /* a[akey] */
Guido van Rossumb9324202001-01-18 00:39:02 +00001103 int i, cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001104
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001105 for (i = 0; i < a->ma_size; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001106 PyObject *thiskey, *thisaval, *thisbval;
1107 if (a->ma_table[i].me_value == NULL)
1108 continue;
1109 thiskey = a->ma_table[i].me_key;
1110 Py_INCREF(thiskey); /* keep alive across compares */
1111 if (akey != NULL) {
1112 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1113 if (cmp < 0) {
1114 Py_DECREF(thiskey);
1115 goto Fail;
1116 }
1117 if (cmp > 0 ||
1118 i >= a->ma_size ||
1119 a->ma_table[i].me_value == NULL)
1120 {
1121 /* Not the *smallest* a key; or maybe it is
1122 * but the compare shrunk the dict so we can't
1123 * find its associated value anymore; or
1124 * maybe it is but the compare deleted the
1125 * a[thiskey] entry.
1126 */
1127 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001128 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001129 }
Tim Peters95bf9392001-05-10 08:32:44 +00001130 }
1131
1132 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1133 thisaval = a->ma_table[i].me_value;
1134 assert(thisaval);
1135 Py_INCREF(thisaval); /* keep alive */
1136 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1137 if (thisbval == NULL)
1138 cmp = 0;
1139 else {
1140 /* both dicts have thiskey: same values? */
1141 cmp = PyObject_RichCompareBool(
1142 thisaval, thisbval, Py_EQ);
1143 if (cmp < 0) {
1144 Py_DECREF(thiskey);
1145 Py_DECREF(thisaval);
1146 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001147 }
1148 }
Tim Peters95bf9392001-05-10 08:32:44 +00001149 if (cmp == 0) {
1150 /* New winner. */
1151 Py_XDECREF(akey);
1152 Py_XDECREF(aval);
1153 akey = thiskey;
1154 aval = thisaval;
1155 }
1156 else {
1157 Py_DECREF(thiskey);
1158 Py_DECREF(thisaval);
1159 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001160 }
Tim Peters95bf9392001-05-10 08:32:44 +00001161 *pval = aval;
1162 return akey;
1163
1164Fail:
1165 Py_XDECREF(akey);
1166 Py_XDECREF(aval);
1167 *pval = NULL;
1168 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001169}
1170
1171static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001172dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001173{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001174 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001175 int res;
1176
1177 /* Compare lengths first */
1178 if (a->ma_used < b->ma_used)
1179 return -1; /* a is shorter */
1180 else if (a->ma_used > b->ma_used)
1181 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001182
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001183 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001184 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001185 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001186 if (adiff == NULL) {
1187 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001188 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001189 * must be equal.
1190 */
1191 res = PyErr_Occurred() ? -1 : 0;
1192 goto Finished;
1193 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001194 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001195 if (bdiff == NULL && PyErr_Occurred()) {
1196 assert(!bval);
1197 res = -1;
1198 goto Finished;
1199 }
1200 res = 0;
1201 if (bdiff) {
1202 /* bdiff == NULL "should be" impossible now, but perhaps
1203 * the last comparison done by the characterize() on a had
1204 * the side effect of making the dicts equal!
1205 */
1206 res = PyObject_Compare(adiff, bdiff);
1207 }
1208 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001209 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001210
1211Finished:
1212 Py_XDECREF(adiff);
1213 Py_XDECREF(bdiff);
1214 Py_XDECREF(aval);
1215 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001216 return res;
1217}
1218
Tim Peterse63415e2001-05-08 04:38:29 +00001219/* Return 1 if dicts equal, 0 if not, -1 if error.
1220 * Gets out as soon as any difference is detected.
1221 * Uses only Py_EQ comparison.
1222 */
1223static int
1224dict_equal(dictobject *a, dictobject *b)
1225{
1226 int i;
1227
1228 if (a->ma_used != b->ma_used)
1229 /* can't be equal if # of entries differ */
1230 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001231
Tim Peterse63415e2001-05-08 04:38:29 +00001232 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1233 for (i = 0; i < a->ma_size; i++) {
1234 PyObject *aval = a->ma_table[i].me_value;
1235 if (aval != NULL) {
1236 int cmp;
1237 PyObject *bval;
1238 PyObject *key = a->ma_table[i].me_key;
1239 /* temporarily bump aval's refcount to ensure it stays
1240 alive until we're done with it */
1241 Py_INCREF(aval);
1242 bval = PyDict_GetItem((PyObject *)b, key);
1243 if (bval == NULL) {
1244 Py_DECREF(aval);
1245 return 0;
1246 }
1247 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1248 Py_DECREF(aval);
1249 if (cmp <= 0) /* error or not equal */
1250 return cmp;
1251 }
1252 }
1253 return 1;
1254 }
1255
1256static PyObject *
1257dict_richcompare(PyObject *v, PyObject *w, int op)
1258{
1259 int cmp;
1260 PyObject *res;
1261
1262 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1263 res = Py_NotImplemented;
1264 }
1265 else if (op == Py_EQ || op == Py_NE) {
1266 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1267 if (cmp < 0)
1268 return NULL;
1269 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1270 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001271 else
1272 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001273 Py_INCREF(res);
1274 return res;
1275 }
1276
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001277static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001278dict_has_key(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001279{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001280 PyObject *key;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001281 long hash;
1282 register long ok;
Fred Drake52fccfd2000-02-23 15:47:16 +00001283 if (!PyArg_ParseTuple(args, "O:has_key", &key))
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001284 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001285#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001286 if (!PyString_Check(key) ||
1287 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001288#endif
Guido van Rossumca756f21997-01-23 19:39:29 +00001289 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001290 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001291 if (hash == -1)
1292 return NULL;
1293 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001294 ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001295 return PyInt_FromLong(ok);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001296}
1297
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001298static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001299dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001300{
1301 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001302 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001303 PyObject *val = NULL;
1304 long hash;
1305
Fred Drake52fccfd2000-02-23 15:47:16 +00001306 if (!PyArg_ParseTuple(args, "O|O:get", &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001307 return NULL;
1308
Barry Warsawc38c5da1997-10-06 17:49:20 +00001309#ifdef CACHE_HASH
1310 if (!PyString_Check(key) ||
1311 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1312#endif
1313 {
1314 hash = PyObject_Hash(key);
1315 if (hash == -1)
1316 return NULL;
1317 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001318 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Barry Warsaw320ac331997-10-20 17:26:25 +00001319
Barry Warsawc38c5da1997-10-06 17:49:20 +00001320 if (val == NULL)
1321 val = failobj;
1322 Py_INCREF(val);
1323 return val;
1324}
1325
1326
1327static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001328dict_setdefault(register dictobject *mp, PyObject *args)
1329{
1330 PyObject *key;
1331 PyObject *failobj = Py_None;
1332 PyObject *val = NULL;
1333 long hash;
1334
1335 if (!PyArg_ParseTuple(args, "O|O:setdefault", &key, &failobj))
1336 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001337
1338#ifdef CACHE_HASH
1339 if (!PyString_Check(key) ||
1340 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1341#endif
1342 {
1343 hash = PyObject_Hash(key);
1344 if (hash == -1)
1345 return NULL;
1346 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001347 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001348 if (val == NULL) {
1349 val = failobj;
1350 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1351 val = NULL;
1352 }
1353 Py_XINCREF(val);
1354 return val;
1355}
1356
1357
1358static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001359dict_clear(register dictobject *mp, PyObject *args)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001360{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001361 if (!PyArg_NoArgs(args))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001362 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001363 PyDict_Clear((PyObject *)mp);
1364 Py_INCREF(Py_None);
1365 return Py_None;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001366}
1367
Guido van Rossumba6ab842000-12-12 22:02:18 +00001368static PyObject *
1369dict_popitem(dictobject *mp, PyObject *args)
1370{
1371 int i = 0;
1372 dictentry *ep;
1373 PyObject *res;
1374
1375 if (!PyArg_NoArgs(args))
1376 return NULL;
Tim Petersf4b33f62001-06-02 05:42:29 +00001377 /* Allocate the result tuple before checking the size. Believe it
1378 * or not, this allocation could trigger a garbage collection which
1379 * could empty the dict, so if we checked the size first and that
1380 * happened, the result would be an infinite loop (searching for an
1381 * entry that no longer exists). Note that the usual popitem()
1382 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1383 * tuple away if the dict *is* empty isn't a significant
1384 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001385 */
1386 res = PyTuple_New(2);
1387 if (res == NULL)
1388 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001389 if (mp->ma_used == 0) {
1390 Py_DECREF(res);
1391 PyErr_SetString(PyExc_KeyError,
1392 "popitem(): dictionary is empty");
1393 return NULL;
1394 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001395 /* Set ep to "the first" dict entry with a value. We abuse the hash
1396 * field of slot 0 to hold a search finger:
1397 * If slot 0 has a value, use slot 0.
1398 * Else slot 0 is being used to hold a search finger,
1399 * and we use its hash value as the first index to look.
1400 */
1401 ep = &mp->ma_table[0];
1402 if (ep->me_value == NULL) {
1403 i = (int)ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001404 /* The hash field may be a real hash value, or it may be a
1405 * legit search finger, or it may be a once-legit search
1406 * finger that's out of bounds now because it wrapped around
1407 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001408 */
1409 if (i >= mp->ma_size || i < 1)
1410 i = 1; /* skip slot 0 */
1411 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1412 i++;
1413 if (i >= mp->ma_size)
1414 i = 1;
1415 }
1416 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001417 PyTuple_SET_ITEM(res, 0, ep->me_key);
1418 PyTuple_SET_ITEM(res, 1, ep->me_value);
1419 Py_INCREF(dummy);
1420 ep->me_key = dummy;
1421 ep->me_value = NULL;
1422 mp->ma_used--;
1423 assert(mp->ma_table[0].me_value == NULL);
1424 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001425 return res;
1426}
1427
Jeremy Hylton8caad492000-06-23 14:18:11 +00001428static int
1429dict_traverse(PyObject *op, visitproc visit, void *arg)
1430{
1431 int i = 0, err;
1432 PyObject *pk;
1433 PyObject *pv;
1434
1435 while (PyDict_Next(op, &i, &pk, &pv)) {
1436 err = visit(pk, arg);
1437 if (err)
1438 return err;
1439 err = visit(pv, arg);
1440 if (err)
1441 return err;
1442 }
1443 return 0;
1444}
1445
1446static int
1447dict_tp_clear(PyObject *op)
1448{
1449 PyDict_Clear(op);
1450 return 0;
1451}
1452
Tim Petersf7f88b12000-12-13 23:18:45 +00001453
Guido van Rossum09e563a2001-05-01 12:10:21 +00001454staticforward PyObject *dictiter_new(dictobject *, binaryfunc);
1455
1456static PyObject *
1457select_key(PyObject *key, PyObject *value)
1458{
1459 Py_INCREF(key);
1460 return key;
1461}
1462
1463static PyObject *
1464select_value(PyObject *key, PyObject *value)
1465{
1466 Py_INCREF(value);
1467 return value;
1468}
1469
1470static PyObject *
1471select_item(PyObject *key, PyObject *value)
1472{
1473 PyObject *res = PyTuple_New(2);
1474
1475 if (res != NULL) {
1476 Py_INCREF(key);
1477 Py_INCREF(value);
1478 PyTuple_SET_ITEM(res, 0, key);
1479 PyTuple_SET_ITEM(res, 1, value);
1480 }
1481 return res;
1482}
1483
1484static PyObject *
1485dict_iterkeys(dictobject *dict, PyObject *args)
1486{
1487 if (!PyArg_ParseTuple(args, ""))
1488 return NULL;
1489 return dictiter_new(dict, select_key);
1490}
1491
1492static PyObject *
1493dict_itervalues(dictobject *dict, PyObject *args)
1494{
1495 if (!PyArg_ParseTuple(args, ""))
1496 return NULL;
1497 return dictiter_new(dict, select_value);
1498}
1499
1500static PyObject *
1501dict_iteritems(dictobject *dict, PyObject *args)
1502{
1503 if (!PyArg_ParseTuple(args, ""))
1504 return NULL;
1505 return dictiter_new(dict, select_item);
1506}
1507
1508
Tim Petersf7f88b12000-12-13 23:18:45 +00001509static char has_key__doc__[] =
1510"D.has_key(k) -> 1 if D has a key k, else 0";
1511
1512static char get__doc__[] =
1513"D.get(k[,d]) -> D[k] if D.has_key(k), else d. d defaults to None.";
1514
1515static char setdefault_doc__[] =
1516"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if not D.has_key(k)";
1517
1518static char popitem__doc__[] =
1519"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
15202-tuple; but raise KeyError if D is empty";
1521
1522static char keys__doc__[] =
1523"D.keys() -> list of D's keys";
1524
1525static char items__doc__[] =
1526"D.items() -> list of D's (key, value) pairs, as 2-tuples";
1527
1528static char values__doc__[] =
1529"D.values() -> list of D's values";
1530
1531static char update__doc__[] =
1532"D.update(E) -> None. Update D from E: for k in E.keys(): D[k] = E[k]";
1533
1534static char clear__doc__[] =
1535"D.clear() -> None. Remove all items from D.";
1536
1537static char copy__doc__[] =
1538"D.copy() -> a shallow copy of D";
1539
Guido van Rossum09e563a2001-05-01 12:10:21 +00001540static char iterkeys__doc__[] =
1541"D.iterkeys() -> an iterator over the keys of D";
1542
1543static char itervalues__doc__[] =
1544"D.itervalues() -> an iterator over the values of D";
1545
1546static char iteritems__doc__[] =
1547"D.iteritems() -> an iterator over the (key, value) items of D";
1548
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001549static PyMethodDef mapp_methods[] = {
Tim Petersf7f88b12000-12-13 23:18:45 +00001550 {"has_key", (PyCFunction)dict_has_key, METH_VARARGS,
1551 has_key__doc__},
1552 {"get", (PyCFunction)dict_get, METH_VARARGS,
1553 get__doc__},
1554 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1555 setdefault_doc__},
1556 {"popitem", (PyCFunction)dict_popitem, METH_OLDARGS,
1557 popitem__doc__},
1558 {"keys", (PyCFunction)dict_keys, METH_OLDARGS,
1559 keys__doc__},
1560 {"items", (PyCFunction)dict_items, METH_OLDARGS,
1561 items__doc__},
1562 {"values", (PyCFunction)dict_values, METH_OLDARGS,
1563 values__doc__},
1564 {"update", (PyCFunction)dict_update, METH_OLDARGS,
1565 update__doc__},
1566 {"clear", (PyCFunction)dict_clear, METH_OLDARGS,
1567 clear__doc__},
1568 {"copy", (PyCFunction)dict_copy, METH_OLDARGS,
1569 copy__doc__},
Guido van Rossum09e563a2001-05-01 12:10:21 +00001570 {"iterkeys", (PyCFunction)dict_iterkeys, METH_VARARGS,
1571 iterkeys__doc__},
1572 {"itervalues", (PyCFunction)dict_itervalues, METH_VARARGS,
1573 itervalues__doc__},
1574 {"iteritems", (PyCFunction)dict_iteritems, METH_VARARGS,
1575 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001576 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001577};
1578
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001579static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001580dict_getattr(dictobject *mp, char *name)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001581{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001582 return Py_FindMethod(mapp_methods, (PyObject *)mp, name);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001583}
1584
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001585static int
1586dict_contains(dictobject *mp, PyObject *key)
1587{
1588 long hash;
1589
1590#ifdef CACHE_HASH
1591 if (!PyString_Check(key) ||
1592 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1593#endif
1594 {
1595 hash = PyObject_Hash(key);
1596 if (hash == -1)
1597 return -1;
1598 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001599 return (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001600}
1601
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001602/* Hack to implement "key in dict" */
1603static PySequenceMethods dict_as_sequence = {
1604 0, /* sq_length */
1605 0, /* sq_concat */
1606 0, /* sq_repeat */
1607 0, /* sq_item */
1608 0, /* sq_slice */
1609 0, /* sq_ass_item */
1610 0, /* sq_ass_slice */
1611 (objobjproc)dict_contains, /* sq_contains */
1612 0, /* sq_inplace_concat */
1613 0, /* sq_inplace_repeat */
1614};
1615
Guido van Rossum09e563a2001-05-01 12:10:21 +00001616static PyObject *
1617dict_iter(dictobject *dict)
1618{
1619 return dictiter_new(dict, select_key);
1620}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001621
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001622PyTypeObject PyDict_Type = {
1623 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001624 0,
Guido van Rossum9bfef441993-03-29 10:43:31 +00001625 "dictionary",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001626 sizeof(dictobject) + PyGC_HEAD_SIZE,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001627 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001628 (destructor)dict_dealloc, /* tp_dealloc */
1629 (printfunc)dict_print, /* tp_print */
1630 (getattrfunc)dict_getattr, /* tp_getattr */
1631 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00001632 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001633 (reprfunc)dict_repr, /* tp_repr */
1634 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001635 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001636 &dict_as_mapping, /* tp_as_mapping */
1637 0, /* tp_hash */
1638 0, /* tp_call */
1639 0, /* tp_str */
1640 0, /* tp_getattro */
1641 0, /* tp_setattro */
1642 0, /* tp_as_buffer */
1643 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
1644 0, /* tp_doc */
1645 (traverseproc)dict_traverse, /* tp_traverse */
1646 (inquiry)dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001647 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001648 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001649 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001650 0, /* tp_iternext */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001651};
1652
Guido van Rossum3cca2451997-05-16 14:23:33 +00001653/* For backward compatibility with old dictionary interface */
1654
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001655PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001656PyDict_GetItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001657{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001658 PyObject *kv, *rv;
1659 kv = PyString_FromString(key);
1660 if (kv == NULL)
1661 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001662 rv = PyDict_GetItem(v, kv);
1663 Py_DECREF(kv);
1664 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001665}
1666
1667int
Tim Peters1f5871e2000-07-04 17:44:48 +00001668PyDict_SetItemString(PyObject *v, char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001669{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001670 PyObject *kv;
1671 int err;
1672 kv = PyString_FromString(key);
1673 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001674 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00001675 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00001676 err = PyDict_SetItem(v, kv, item);
1677 Py_DECREF(kv);
1678 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001679}
1680
1681int
Tim Peters1f5871e2000-07-04 17:44:48 +00001682PyDict_DelItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001683{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001684 PyObject *kv;
1685 int err;
1686 kv = PyString_FromString(key);
1687 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001688 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001689 err = PyDict_DelItem(v, kv);
1690 Py_DECREF(kv);
1691 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001692}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001693
1694/* Dictionary iterator type */
1695
1696extern PyTypeObject PyDictIter_Type; /* Forward */
1697
1698typedef struct {
1699 PyObject_HEAD
1700 dictobject *di_dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001701 int di_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001702 int di_pos;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001703 binaryfunc di_select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001704} dictiterobject;
1705
1706static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001707dictiter_new(dictobject *dict, binaryfunc select)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001708{
1709 dictiterobject *di;
1710 di = PyObject_NEW(dictiterobject, &PyDictIter_Type);
1711 if (di == NULL)
1712 return NULL;
1713 Py_INCREF(dict);
1714 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001715 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001716 di->di_pos = 0;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001717 di->di_select = select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001718 return (PyObject *)di;
1719}
1720
1721static void
1722dictiter_dealloc(dictiterobject *di)
1723{
1724 Py_DECREF(di->di_dict);
1725 PyObject_DEL(di);
1726}
1727
1728static PyObject *
1729dictiter_next(dictiterobject *di, PyObject *args)
1730{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001731 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001732
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001733 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001734 PyErr_SetString(PyExc_RuntimeError,
1735 "dictionary changed size during iteration");
1736 return NULL;
1737 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001738 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1739 return (*di->di_select)(key, value);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001740 }
1741 PyErr_SetObject(PyExc_StopIteration, Py_None);
1742 return NULL;
1743}
1744
1745static PyObject *
1746dictiter_getiter(PyObject *it)
1747{
1748 Py_INCREF(it);
1749 return it;
1750}
1751
1752static PyMethodDef dictiter_methods[] = {
1753 {"next", (PyCFunction)dictiter_next, METH_VARARGS,
1754 "it.next() -- get the next value, or raise StopIteration"},
1755 {NULL, NULL} /* sentinel */
1756};
1757
1758static PyObject *
Guido van Rossum213c7a62001-04-23 14:08:49 +00001759dictiter_getattr(dictiterobject *di, char *name)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001760{
Guido van Rossum213c7a62001-04-23 14:08:49 +00001761 return Py_FindMethod(dictiter_methods, (PyObject *)di, name);
1762}
1763
1764static PyObject *dictiter_iternext(dictiterobject *di)
1765{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001766 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001767
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001768 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00001769 PyErr_SetString(PyExc_RuntimeError,
1770 "dictionary changed size during iteration");
1771 return NULL;
1772 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001773 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1774 return (*di->di_select)(key, value);
Guido van Rossum213c7a62001-04-23 14:08:49 +00001775 }
1776 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001777}
1778
1779PyTypeObject PyDictIter_Type = {
1780 PyObject_HEAD_INIT(&PyType_Type)
1781 0, /* ob_size */
1782 "dictionary-iterator", /* tp_name */
1783 sizeof(dictiterobject), /* tp_basicsize */
1784 0, /* tp_itemsize */
1785 /* methods */
1786 (destructor)dictiter_dealloc, /* tp_dealloc */
1787 0, /* tp_print */
1788 (getattrfunc)dictiter_getattr, /* tp_getattr */
1789 0, /* tp_setattr */
1790 0, /* tp_compare */
1791 0, /* tp_repr */
1792 0, /* tp_as_number */
1793 0, /* tp_as_sequence */
1794 0, /* tp_as_mapping */
1795 0, /* tp_hash */
1796 0, /* tp_call */
1797 0, /* tp_str */
1798 0, /* tp_getattro */
1799 0, /* tp_setattro */
1800 0, /* tp_as_buffer */
1801 Py_TPFLAGS_DEFAULT, /* tp_flags */
1802 0, /* tp_doc */
1803 0, /* tp_traverse */
1804 0, /* tp_clear */
1805 0, /* tp_richcompare */
1806 0, /* tp_weaklistoffset */
1807 (getiterfunc)dictiter_getiter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001808 (iternextfunc)dictiter_iternext, /* tp_iternext */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001809};