blob: c17664efc5f11367543447aa9c0b1df8625ed21d [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 */
Tim Petersafb6ae82001-06-04 21:00:21 +0000165
166 /* The table contains ma_mask + 1 slots, and that's a power of 2.
167 * We store the mask instead of the size because the mask is more
168 * frequently needed.
169 */
170 int ma_mask;
171
Tim Petersdea48ec2001-05-22 20:40:22 +0000172 /* ma_table points to ma_smalltable for small tables, else to
173 * additional malloc'ed memory. ma_table is never NULL! This rule
174 * saves repeated runtime null-tests in the workhorse getitem and
175 * setitem calls.
176 */
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000177 dictentry *ma_table;
Fred Drake1bff34a2000-08-31 19:31:38 +0000178 dictentry *(*ma_lookup)(dictobject *mp, PyObject *key, long hash);
Tim Petersdea48ec2001-05-22 20:40:22 +0000179 dictentry ma_smalltable[MINSIZE];
Fred Drake1bff34a2000-08-31 19:31:38 +0000180};
181
182/* forward declarations */
183static dictentry *
184lookdict_string(dictobject *mp, PyObject *key, long hash);
185
186#ifdef SHOW_CONVERSION_COUNTS
187static long created = 0L;
188static long converted = 0L;
189
190static void
191show_counts(void)
192{
193 fprintf(stderr, "created %ld string dicts\n", created);
194 fprintf(stderr, "converted %ld to normal dicts\n", converted);
195 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
196}
197#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000198
Tim Petersdea48ec2001-05-22 20:40:22 +0000199/* Set dictobject* mp to empty but w/ MINSIZE slots, using ma_smalltable. */
200#define empty_to_minsize(mp) do { \
201 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
202 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Petersafb6ae82001-06-04 21:00:21 +0000203 (mp)->ma_mask = MINSIZE - 1; \
Tim Petersdea48ec2001-05-22 20:40:22 +0000204 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Petersdea48ec2001-05-22 20:40:22 +0000205 } while(0)
206
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000207PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000208PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000209{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000210 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000211 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000213 if (dummy == NULL)
214 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000215#ifdef SHOW_CONVERSION_COUNTS
216 Py_AtExit(show_counts);
217#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000218 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000219 mp = PyObject_NEW(dictobject, &PyDict_Type);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000220 if (mp == NULL)
221 return NULL;
Tim Petersdea48ec2001-05-22 20:40:22 +0000222 empty_to_minsize(mp);
Fred Drake1bff34a2000-08-31 19:31:38 +0000223 mp->ma_lookup = lookdict_string;
224#ifdef SHOW_CONVERSION_COUNTS
225 ++created;
226#endif
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000227 PyObject_GC_Init(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000228 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000229}
230
231/*
232The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000233This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000234Open addressing is preferred over chaining since the link overhead for
235chaining would be substantial (100% with typical malloc overhead).
236
Tim Peterseb28ef22001-06-02 05:27:19 +0000237The initial probe index is computed as hash mod the table size. Subsequent
238probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000239
240All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000241
Tim Peterseb28ef22001-06-02 05:27:19 +0000242(The details in this version are due to Tim Peters, building on many past
243contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
244Christian Tismer).
Fred Drake1bff34a2000-08-31 19:31:38 +0000245
246This function must never return NULL; failures are indicated by returning
247a dictentry* for which the me_value field is NULL. Exceptions are never
248reported by this function, and outstanding exceptions are maintained.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000249*/
Tim Peterseb28ef22001-06-02 05:27:19 +0000250
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000251static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000252lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000253{
Guido van Rossum16e93a81997-01-28 00:00:11 +0000254 register int i;
Tim Peterseb28ef22001-06-02 05:27:19 +0000255 register unsigned int perturb;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000256 register dictentry *freeslot;
Tim Petersafb6ae82001-06-04 21:00:21 +0000257 register unsigned int mask = mp->ma_mask;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000258 dictentry *ep0 = mp->ma_table;
259 register dictentry *ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000260 register int restore_error;
261 register int checked_error;
Fred Drakec88b99c2000-08-31 19:04:07 +0000262 register int cmp;
263 PyObject *err_type, *err_value, *err_tb;
Tim Peters453163d2001-06-03 04:54:32 +0000264 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000265
Tim Peters2f228e72001-05-13 00:19:31 +0000266 i = hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000267 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000268 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000269 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000270
271 restore_error = checked_error = 0;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000272 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000273 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000274 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000275 if (ep->me_hash == hash) {
276 /* error can't have been checked yet */
277 checked_error = 1;
278 if (PyErr_Occurred()) {
279 restore_error = 1;
280 PyErr_Fetch(&err_type, &err_value, &err_tb);
281 }
Tim Peters453163d2001-06-03 04:54:32 +0000282 startkey = ep->me_key;
283 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000284 if (cmp < 0)
Guido van Rossumb9324202001-01-18 00:39:02 +0000285 PyErr_Clear();
Tim Peters453163d2001-06-03 04:54:32 +0000286 if (ep0 == mp->ma_table && ep->me_key == startkey) {
287 if (cmp > 0)
288 goto Done;
289 }
290 else {
291 /* The compare did major nasty stuff to the
292 * dict: start over.
293 * XXX A clever adversary could prevent this
294 * XXX from terminating.
295 */
296 ep = lookdict(mp, key, hash);
297 goto Done;
298 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000299 }
300 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000301 }
Tim Peters15d49292001-05-27 07:39:22 +0000302
Tim Peters342c65e2001-05-13 06:43:53 +0000303 /* In the loop, me_key == dummy is by far (factor of 100s) the
304 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000305 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
306 i = (i << 2) + i + perturb + 1;
307 ep = &ep0[i & mask];
Guido van Rossum16e93a81997-01-28 00:00:11 +0000308 if (ep->me_key == NULL) {
Tim Peters7b5d0af2001-06-03 04:14:43 +0000309 if (freeslot != NULL)
310 ep = freeslot;
311 break;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000312 }
Tim Peters7b5d0af2001-06-03 04:14:43 +0000313 if (ep->me_key == key)
314 break;
315 if (ep->me_hash == hash && ep->me_key != dummy) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000316 if (!checked_error) {
317 checked_error = 1;
318 if (PyErr_Occurred()) {
319 restore_error = 1;
320 PyErr_Fetch(&err_type, &err_value,
321 &err_tb);
322 }
323 }
Tim Peters453163d2001-06-03 04:54:32 +0000324 startkey = ep->me_key;
325 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000326 if (cmp < 0)
Guido van Rossumb9324202001-01-18 00:39:02 +0000327 PyErr_Clear();
Tim Peters453163d2001-06-03 04:54:32 +0000328 if (ep0 == mp->ma_table && ep->me_key == startkey) {
329 if (cmp > 0)
330 break;
331 }
332 else {
333 /* The compare did major nasty stuff to the
334 * dict: start over.
335 * XXX A clever adversary could prevent this
336 * XXX from terminating.
337 */
338 ep = lookdict(mp, key, hash);
339 break;
340 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000341 }
Tim Peters342c65e2001-05-13 06:43:53 +0000342 else if (ep->me_key == dummy && freeslot == NULL)
343 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000344 }
Tim Peters7b5d0af2001-06-03 04:14:43 +0000345
346Done:
347 if (restore_error)
348 PyErr_Restore(err_type, err_value, err_tb);
349 return ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000350}
351
352/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000353 * Hacked up version of lookdict which can assume keys are always strings;
354 * this assumption allows testing for errors during PyObject_Compare() to
355 * be dropped; string-string comparisons never raise exceptions. This also
356 * means we don't need to go through PyObject_Compare(); we can always use
Martin v. Löwiscd353062001-05-24 16:56:35 +0000357 * _PyString_Eq directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000358 *
359 * This really only becomes meaningful if proper error handling in lookdict()
360 * is too expensive.
361 */
362static dictentry *
363lookdict_string(dictobject *mp, PyObject *key, register long hash)
364{
365 register int i;
Tim Peterseb28ef22001-06-02 05:27:19 +0000366 register unsigned int perturb;
Fred Drake1bff34a2000-08-31 19:31:38 +0000367 register dictentry *freeslot;
Tim Petersafb6ae82001-06-04 21:00:21 +0000368 register unsigned int mask = mp->ma_mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000369 dictentry *ep0 = mp->ma_table;
370 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000371
372 /* make sure this function doesn't have to handle non-string keys */
373 if (!PyString_Check(key)) {
374#ifdef SHOW_CONVERSION_COUNTS
375 ++converted;
376#endif
377 mp->ma_lookup = lookdict;
378 return lookdict(mp, key, hash);
379 }
Tim Peters2f228e72001-05-13 00:19:31 +0000380 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000381 ep = &ep0[i];
382 if (ep->me_key == NULL || ep->me_key == key)
383 return ep;
384 if (ep->me_key == dummy)
385 freeslot = ep;
386 else {
387 if (ep->me_hash == hash
Martin v. Löwiscd353062001-05-24 16:56:35 +0000388 && _PyString_Eq(ep->me_key, key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000389 return ep;
390 }
391 freeslot = NULL;
392 }
Tim Peters15d49292001-05-27 07:39:22 +0000393
Tim Peters342c65e2001-05-13 06:43:53 +0000394 /* In the loop, me_key == dummy is by far (factor of 100s) the
395 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000396 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
397 i = (i << 2) + i + perturb + 1;
398 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000399 if (ep->me_key == NULL)
400 return freeslot == NULL ? ep : freeslot;
401 if (ep->me_key == key
402 || (ep->me_hash == hash
403 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000404 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000405 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000406 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000407 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000408 }
409}
410
411/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000412Internal routine to insert a new item into the table.
413Used both by the internal resize routine and by the public insert routine.
414Eats a reference to key and one to value.
415*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000416static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000417insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000418{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000419 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000420 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000421 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000422 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000423 old_value = ep->me_value;
424 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000425 Py_DECREF(old_value); /* which **CAN** re-enter */
426 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000427 }
428 else {
429 if (ep->me_key == NULL)
430 mp->ma_fill++;
431 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000432 Py_DECREF(ep->me_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000433 ep->me_key = key;
434 ep->me_hash = hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000435 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000436 mp->ma_used++;
437 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000438}
439
440/*
441Restructure the table by allocating a new table and reinserting all
442items again. When entries have been deleted, the new table may
443actually be smaller than the old one.
444*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000445static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000446dictresize(dictobject *mp, int minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000447{
Tim Peterseb28ef22001-06-02 05:27:19 +0000448 int newsize;
Tim Peters0c6010b2001-05-23 23:33:57 +0000449 dictentry *oldtable, *newtable, *ep;
450 int i;
451 int is_oldtable_malloced;
452 dictentry small_copy[MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000453
454 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000455
Tim Peterseb28ef22001-06-02 05:27:19 +0000456 /* Find the smallest table size > minused. */
457 for (newsize = MINSIZE;
458 newsize <= minused && newsize > 0;
459 newsize <<= 1)
460 ;
461 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000462 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000463 return -1;
464 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000465
466 /* Get space for a new table. */
467 oldtable = mp->ma_table;
468 assert(oldtable != NULL);
469 is_oldtable_malloced = oldtable != mp->ma_smalltable;
470
Tim Petersdea48ec2001-05-22 20:40:22 +0000471 if (newsize == MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000472 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000473 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000474 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000475 if (mp->ma_fill == mp->ma_used) {
476 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000477 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000478 }
479 /* We're not going to resize it, but rebuild the
480 table anyway to purge old dummy entries.
481 Subtle: This is *necessary* if fill==size,
482 as lookdict needs at least one virgin slot to
483 terminate failing searches. If fill < size, it's
484 merely desirable, as dummies slow searches. */
485 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000486 memcpy(small_copy, oldtable, sizeof(small_copy));
487 oldtable = small_copy;
488 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000489 }
490 else {
491 newtable = PyMem_NEW(dictentry, newsize);
492 if (newtable == NULL) {
493 PyErr_NoMemory();
494 return -1;
495 }
496 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000497
498 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000499 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000500 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000501 mp->ma_mask = newsize - 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000502 memset(newtable, 0, sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000503 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000504 i = mp->ma_fill;
505 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000506
Tim Peters19283142001-05-17 22:25:34 +0000507 /* Copy the data over; this is refcount-neutral for active entries;
508 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000509 for (ep = oldtable; i > 0; ep++) {
510 if (ep->me_value != NULL) { /* active entry */
511 --i;
Tim Peters19283142001-05-17 22:25:34 +0000512 insertdict(mp, ep->me_key, ep->me_hash, ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000513 }
Tim Peters19283142001-05-17 22:25:34 +0000514 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000515 --i;
Tim Peters19283142001-05-17 22:25:34 +0000516 assert(ep->me_key == dummy);
517 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000518 }
Tim Peters19283142001-05-17 22:25:34 +0000519 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000520 }
521
Tim Peters0c6010b2001-05-23 23:33:57 +0000522 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000523 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000524 return 0;
525}
526
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000527PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000528PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000529{
530 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000531 dictobject *mp = (dictobject *)op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000532 if (!PyDict_Check(op)) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000533 return NULL;
534 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000535#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000536 if (!PyString_Check(key) ||
537 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000538#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000539 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000540 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000541 if (hash == -1) {
542 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000543 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000544 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000545 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000546 return (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000547}
548
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000549/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
550 * dictionary if it is merely replacing the value for an existing key.
551 * This is means that it's safe to loop over a dictionary with
552 * PyDict_Next() and occasionally replace a value -- but you can't
553 * insert new keys or remove them.
554 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000555int
Tim Peters1f5871e2000-07-04 17:44:48 +0000556PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000557{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000558 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000559 register long hash;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000560 register int n_used;
561
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000562 if (!PyDict_Check(op)) {
563 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000564 return -1;
565 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000566 mp = (dictobject *)op;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000567#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000568 if (PyString_Check(key)) {
Guido van Rossum2a61e741997-01-18 07:55:05 +0000569#ifdef INTERN_STRINGS
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000570 if (((PyStringObject *)key)->ob_sinterned != NULL) {
571 key = ((PyStringObject *)key)->ob_sinterned;
572 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000573 }
574 else
575#endif
576 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000577 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000578 if (hash == -1)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000579 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000580 }
581 }
582 else
583#endif
584 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000585 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000586 if (hash == -1)
587 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000588 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000589 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000590 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000591 Py_INCREF(value);
592 Py_INCREF(key);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000593 insertdict(mp, key, hash, value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000594 /* If we added a key, we can safely resize. Otherwise skip this!
595 * If fill >= 2/3 size, adjust size. Normally, this doubles the
596 * size, but it's also possible for the dict to shrink (if ma_fill is
597 * much larger than ma_used, meaning a lot of dict keys have been
598 * deleted).
599 */
Tim Petersafb6ae82001-06-04 21:00:21 +0000600 if (mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2) {
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000601 if (dictresize(mp, mp->ma_used*2) != 0)
602 return -1;
603 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000604 return 0;
605}
606
607int
Tim Peters1f5871e2000-07-04 17:44:48 +0000608PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000609{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000610 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000611 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000612 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000613 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000614
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000615 if (!PyDict_Check(op)) {
616 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000617 return -1;
618 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000619#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000620 if (!PyString_Check(key) ||
621 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000622#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000623 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000624 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000625 if (hash == -1)
626 return -1;
627 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000628 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000629 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000630 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000631 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000632 return -1;
633 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000634 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000635 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000636 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000637 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000638 ep->me_value = NULL;
639 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000640 Py_DECREF(old_value);
641 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000642 return 0;
643}
644
Guido van Rossum25831651993-05-19 14:50:45 +0000645void
Tim Peters1f5871e2000-07-04 17:44:48 +0000646PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000647{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000648 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000649 dictentry *ep, *table;
650 int table_is_malloced;
651 int fill;
652 dictentry small_copy[MINSIZE];
653#ifdef Py_DEBUG
654 int i, n;
655#endif
656
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000658 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000659 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000660#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000661 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000662 i = 0;
663#endif
664
665 table = mp->ma_table;
666 assert(table != NULL);
667 table_is_malloced = table != mp->ma_smalltable;
668
669 /* This is delicate. During the process of clearing the dict,
670 * decrefs can cause the dict to mutate. To avoid fatal confusion
671 * (voice of experience), we have to make the dict empty before
672 * clearing the slots, and never refer to anything via mp->xxx while
673 * clearing.
674 */
675 fill = mp->ma_fill;
676 if (table_is_malloced)
677 empty_to_minsize(mp);
678
679 else if (fill > 0) {
680 /* It's a small table with something that needs to be cleared.
681 * Afraid the only safe way is to copy the dict entries into
682 * another small table first.
683 */
684 memcpy(small_copy, table, sizeof(small_copy));
685 table = small_copy;
686 empty_to_minsize(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000687 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000688 /* else it's a small table that's already empty */
689
690 /* Now we can finally clear things. If C had refcounts, we could
691 * assert that the refcount on table is 1 now, i.e. that this function
692 * has unique access to it, so decref side-effects can't alter it.
693 */
694 for (ep = table; fill > 0; ++ep) {
695#ifdef Py_DEBUG
696 assert(i < n);
697 ++i;
698#endif
699 if (ep->me_key) {
700 --fill;
701 Py_DECREF(ep->me_key);
702 Py_XDECREF(ep->me_value);
703 }
704#ifdef Py_DEBUG
705 else
706 assert(ep->me_value == NULL);
707#endif
708 }
709
710 if (table_is_malloced)
711 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000712}
713
Tim Peters67830702001-03-21 19:23:56 +0000714/* CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
715 * mutates the dict. One exception: it is safe if the loop merely changes
716 * the values associated with the keys (but doesn't insert new keys or
717 * delete keys), via PyDict_SetItem().
718 */
Guido van Rossum25831651993-05-19 14:50:45 +0000719int
Tim Peters1f5871e2000-07-04 17:44:48 +0000720PyDict_Next(PyObject *op, int *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000721{
Guido van Rossum25831651993-05-19 14:50:45 +0000722 int i;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000723 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000724 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000725 return 0;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000726 mp = (dictobject *)op;
Guido van Rossum25831651993-05-19 14:50:45 +0000727 i = *ppos;
728 if (i < 0)
729 return 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000730 while (i <= mp->ma_mask && mp->ma_table[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000731 i++;
732 *ppos = i+1;
Tim Petersafb6ae82001-06-04 21:00:21 +0000733 if (i > mp->ma_mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000734 return 0;
735 if (pkey)
736 *pkey = mp->ma_table[i].me_key;
737 if (pvalue)
738 *pvalue = mp->ma_table[i].me_value;
739 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000740}
741
742/* Methods */
743
744static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000745dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000746{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000747 register dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000748 int fill = mp->ma_fill;
Guido van Rossumd724b232000-03-13 16:01:29 +0000749 Py_TRASHCAN_SAFE_BEGIN(mp)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000750 PyObject_GC_Fini(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000751 for (ep = mp->ma_table; fill > 0; ep++) {
752 if (ep->me_key) {
753 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000754 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000755 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000756 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000757 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000758 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000759 PyMem_DEL(mp->ma_table);
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000760 mp = (dictobject *) PyObject_AS_GC(mp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000761 PyObject_DEL(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000762 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000763}
764
765static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000766dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000767{
768 register int i;
769 register int any;
Guido van Rossum255443b1998-04-10 22:47:14 +0000770
771 i = Py_ReprEnter((PyObject*)mp);
772 if (i != 0) {
773 if (i < 0)
774 return i;
775 fprintf(fp, "{...}");
776 return 0;
777 }
778
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000779 fprintf(fp, "{");
780 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000781 for (i = 0; i <= mp->ma_mask; i++) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000782 dictentry *ep = mp->ma_table + i;
783 PyObject *pvalue = ep->me_value;
784 if (pvalue != NULL) {
785 /* Prevent PyObject_Repr from deleting value during
786 key format */
787 Py_INCREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000788 if (any++ > 0)
789 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000790 if (PyObject_Print((PyObject *)ep->me_key, 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 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000795 fprintf(fp, ": ");
Tim Peters19b77cf2001-06-02 08:27:39 +0000796 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000797 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000798 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000799 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000800 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000801 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000802 }
803 }
804 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000805 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000806 return 0;
807}
808
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000809static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000810dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000811{
Tim Petersc6057842001-06-16 07:52:53 +0000812 int i;
Tim Petersa7259592001-06-16 05:11:17 +0000813 PyObject *s, *temp, *colon = NULL;
814 PyObject *pieces = NULL, *result = NULL;
815 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000816
Tim Petersa7259592001-06-16 05:11:17 +0000817 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000818 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000819 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000820 }
821
Tim Petersa7259592001-06-16 05:11:17 +0000822 if (mp->ma_used == 0) {
823 result = PyString_FromString("{}");
824 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000825 }
Tim Petersa7259592001-06-16 05:11:17 +0000826
827 pieces = PyList_New(0);
828 if (pieces == NULL)
829 goto Done;
830
831 colon = PyString_FromString(": ");
832 if (colon == NULL)
833 goto Done;
834
835 /* Do repr() on each key+value pair, and insert ": " between them.
836 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000837 i = 0;
838 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000839 int status;
840 /* Prevent repr from deleting value during key format. */
841 Py_INCREF(value);
842 s = PyObject_Repr(key);
843 PyString_Concat(&s, colon);
844 PyString_ConcatAndDel(&s, PyObject_Repr(value));
845 Py_DECREF(value);
846 if (s == NULL)
847 goto Done;
848 status = PyList_Append(pieces, s);
849 Py_DECREF(s); /* append created a new ref */
850 if (status < 0)
851 goto Done;
852 }
853
854 /* Add "{}" decorations to the first and last items. */
855 assert(PyList_GET_SIZE(pieces) > 0);
856 s = PyString_FromString("{");
857 if (s == NULL)
858 goto Done;
859 temp = PyList_GET_ITEM(pieces, 0);
860 PyString_ConcatAndDel(&s, temp);
861 PyList_SET_ITEM(pieces, 0, s);
862 if (s == NULL)
863 goto Done;
864
865 s = PyString_FromString("}");
866 if (s == NULL)
867 goto Done;
868 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
869 PyString_ConcatAndDel(&temp, s);
870 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
871 if (temp == NULL)
872 goto Done;
873
874 /* Paste them all together with ", " between. */
875 s = PyString_FromString(", ");
876 if (s == NULL)
877 goto Done;
878 result = _PyString_Join(s, pieces);
879 Py_DECREF(s);
880
881Done:
882 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000883 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000884 Py_ReprLeave((PyObject *)mp);
885 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000886}
887
888static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000889dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000890{
891 return mp->ma_used;
892}
893
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000894static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000895dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000896{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000897 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000898 long hash;
Tim Petersdea48ec2001-05-22 20:40:22 +0000899 assert(mp->ma_table != NULL);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000900#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000901 if (!PyString_Check(key) ||
902 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000903#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000904 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000905 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000906 if (hash == -1)
907 return NULL;
908 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000909 v = (mp->ma_lookup)(mp, key, hash) -> me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000910 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000911 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000912 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000913 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000914 return v;
915}
916
917static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000918dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000919{
920 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000921 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000922 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000923 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000924}
925
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000926static PyMappingMethods dict_as_mapping = {
927 (inquiry)dict_length, /*mp_length*/
928 (binaryfunc)dict_subscript, /*mp_subscript*/
929 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000930};
931
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000932static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000933dict_keys(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000934{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000935 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000936 register int i, j, n;
937
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000938 if (!PyArg_NoArgs(args))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000939 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000940 again:
941 n = mp->ma_used;
942 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000943 if (v == NULL)
944 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000945 if (n != mp->ma_used) {
946 /* Durnit. The allocations caused the dict to resize.
947 * Just start over, this shouldn't normally happen.
948 */
949 Py_DECREF(v);
950 goto again;
951 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000952 for (i = 0, j = 0; i <= mp->ma_mask; i++) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000953 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000954 PyObject *key = mp->ma_table[i].me_key;
955 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000956 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000957 j++;
958 }
959 }
960 return v;
961}
962
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000963static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000964dict_values(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000965{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000966 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000967 register int i, j, n;
968
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000969 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000970 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000971 again:
972 n = mp->ma_used;
973 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000974 if (v == NULL)
975 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000976 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 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000983 for (i = 0, j = 0; i <= mp->ma_mask; i++) {
Guido van Rossum25831651993-05-19 14:50:45 +0000984 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000985 PyObject *value = mp->ma_table[i].me_value;
986 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000987 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000988 j++;
989 }
990 }
991 return v;
992}
993
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000994static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000995dict_items(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000996{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000997 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000998 register int i, j, n;
999 PyObject *item, *key, *value;
1000
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001001 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +00001002 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001003 /* Preallocate the list of tuples, to avoid allocations during
1004 * the loop over the items, which could trigger GC, which
1005 * could resize the dict. :-(
1006 */
1007 again:
1008 n = mp->ma_used;
1009 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001010 if (v == NULL)
1011 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001012 for (i = 0; i < n; i++) {
1013 item = PyTuple_New(2);
1014 if (item == NULL) {
1015 Py_DECREF(v);
1016 return NULL;
1017 }
1018 PyList_SET_ITEM(v, i, item);
1019 }
1020 if (n != mp->ma_used) {
1021 /* Durnit. The allocations caused the dict to resize.
1022 * Just start over, this shouldn't normally happen.
1023 */
1024 Py_DECREF(v);
1025 goto again;
1026 }
1027 /* Nothing we do below makes any function calls. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001028 for (i = 0, j = 0; i <= mp->ma_mask; i++) {
Guido van Rossum25831651993-05-19 14:50:45 +00001029 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001030 key = mp->ma_table[i].me_key;
1031 value = mp->ma_table[i].me_value;
1032 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001033 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001034 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001035 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001036 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001037 j++;
1038 }
1039 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001040 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001041 return v;
1042}
1043
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001044static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001045dict_update(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001046{
1047 register int i;
1048 dictobject *other;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001049 dictentry *entry;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001050 PyObject *param;
1051 /* We accept for the argument either a concrete dictionary object,
1052 * or an abstract "mapping" object. For the former, we can do
1053 * things quite efficiently. For the latter, we only require that
1054 * PyMapping_Keys() and PyObject_GetItem() be supported.
1055 */
1056 if (!PyArg_ParseTuple(args, "O:update", &param))
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001057 return NULL;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001058
1059 if (PyDict_Check(param)) {
1060 other = (dictobject*)param;
1061 if (other == mp || other->ma_used == 0)
1062 /* a.update(a) or a.update({}); nothing to do */
1063 goto done;
1064 /* Do one big resize at the start, rather than
1065 * incrementally resizing as we insert new items. Expect
1066 * that there will be no (or few) overlapping keys.
1067 */
1068 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
1069 if (dictresize(mp, (mp->ma_used + other->ma_used)*3/2) != 0)
1070 return NULL;
1071 }
1072 for (i = 0; i <= other->ma_mask; i++) {
1073 entry = &other->ma_table[i];
1074 if (entry->me_value != NULL) {
1075 Py_INCREF(entry->me_key);
1076 Py_INCREF(entry->me_value);
1077 insertdict(mp, entry->me_key, entry->me_hash,
1078 entry->me_value);
1079 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001080 }
1081 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001082 else {
1083 /* Do it the generic, slower way */
1084 PyObject *keys = PyMapping_Keys(param);
1085 PyObject *iter;
1086 PyObject *key, *value;
1087 int status;
1088
1089 if (keys == NULL)
1090 /* Docstring says this is equivalent to E.keys() so
1091 * if E doesn't have a .keys() method we want
1092 * AttributeError to percolate up. Might as well
1093 * do the same for any other error.
1094 */
1095 return NULL;
1096
1097 iter = PyObject_GetIter(keys);
1098 Py_DECREF(keys);
1099 if (iter == NULL)
1100 return NULL;
1101
1102 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1103 value = PyObject_GetItem(param, key);
1104 if (value == NULL) {
1105 Py_DECREF(iter);
1106 Py_DECREF(key);
1107 return NULL;
1108 }
1109 status = PyDict_SetItem((PyObject*)mp, key, value);
1110 Py_DECREF(key);
1111 Py_DECREF(value);
1112 if (status < 0) {
1113 Py_DECREF(iter);
1114 return NULL;
1115 }
1116 }
1117 Py_DECREF(iter);
1118 if (PyErr_Occurred())
1119 /* Iterator completed, via error */
1120 return NULL;
1121 }
1122
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001123 done:
1124 Py_INCREF(Py_None);
1125 return Py_None;
1126}
1127
1128static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001129dict_copy(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001130{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001131 if (!PyArg_Parse(args, ""))
1132 return NULL;
1133 return PyDict_Copy((PyObject*)mp);
1134}
1135
1136PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001137PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001138{
1139 register dictobject *mp;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001140 register int i;
1141 dictobject *copy;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001142 dictentry *entry;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001143
1144 if (o == NULL || !PyDict_Check(o)) {
1145 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001146 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001147 }
1148 mp = (dictobject *)o;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001149 copy = (dictobject *)PyDict_New();
1150 if (copy == NULL)
1151 return NULL;
1152 if (mp->ma_used > 0) {
1153 if (dictresize(copy, mp->ma_used*3/2) != 0)
1154 return NULL;
Tim Petersafb6ae82001-06-04 21:00:21 +00001155 for (i = 0; i <= mp->ma_mask; i++) {
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001156 entry = &mp->ma_table[i];
1157 if (entry->me_value != NULL) {
1158 Py_INCREF(entry->me_key);
1159 Py_INCREF(entry->me_value);
1160 insertdict(copy, entry->me_key, entry->me_hash,
1161 entry->me_value);
1162 }
1163 }
1164 }
1165 return (PyObject *)copy;
1166}
1167
Guido van Rossum4199fac1993-11-05 10:18:44 +00001168int
Tim Peters1f5871e2000-07-04 17:44:48 +00001169PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001170{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001171 if (mp == NULL || !PyDict_Check(mp)) {
1172 PyErr_BadInternalCall();
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001173 return 0;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001174 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001175 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001176}
1177
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001178PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001179PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001180{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001181 if (mp == NULL || !PyDict_Check(mp)) {
1182 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001183 return NULL;
1184 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001185 return dict_keys((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001186}
1187
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001188PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001189PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001190{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001191 if (mp == NULL || !PyDict_Check(mp)) {
1192 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001193 return NULL;
1194 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001195 return dict_values((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +00001196}
1197
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001198PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001199PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001200{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001201 if (mp == NULL || !PyDict_Check(mp)) {
1202 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001203 return NULL;
1204 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001205 return dict_items((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +00001206}
1207
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001208/* Subroutine which returns the smallest key in a for which b's value
1209 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001210 pval argument. Both are NULL if no key in a is found for which b's status
1211 differs. The refcounts on (and only on) non-NULL *pval and function return
1212 values must be decremented by the caller (characterize() increments them
1213 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1214 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001215
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001216static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001217characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001218{
Tim Peters95bf9392001-05-10 08:32:44 +00001219 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1220 PyObject *aval = NULL; /* a[akey] */
Guido van Rossumb9324202001-01-18 00:39:02 +00001221 int i, cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001222
Tim Petersafb6ae82001-06-04 21:00:21 +00001223 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001224 PyObject *thiskey, *thisaval, *thisbval;
1225 if (a->ma_table[i].me_value == NULL)
1226 continue;
1227 thiskey = a->ma_table[i].me_key;
1228 Py_INCREF(thiskey); /* keep alive across compares */
1229 if (akey != NULL) {
1230 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1231 if (cmp < 0) {
1232 Py_DECREF(thiskey);
1233 goto Fail;
1234 }
1235 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001236 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001237 a->ma_table[i].me_value == NULL)
1238 {
1239 /* Not the *smallest* a key; or maybe it is
1240 * but the compare shrunk the dict so we can't
1241 * find its associated value anymore; or
1242 * maybe it is but the compare deleted the
1243 * a[thiskey] entry.
1244 */
1245 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001246 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001247 }
Tim Peters95bf9392001-05-10 08:32:44 +00001248 }
1249
1250 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1251 thisaval = a->ma_table[i].me_value;
1252 assert(thisaval);
1253 Py_INCREF(thisaval); /* keep alive */
1254 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1255 if (thisbval == NULL)
1256 cmp = 0;
1257 else {
1258 /* both dicts have thiskey: same values? */
1259 cmp = PyObject_RichCompareBool(
1260 thisaval, thisbval, Py_EQ);
1261 if (cmp < 0) {
1262 Py_DECREF(thiskey);
1263 Py_DECREF(thisaval);
1264 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001265 }
1266 }
Tim Peters95bf9392001-05-10 08:32:44 +00001267 if (cmp == 0) {
1268 /* New winner. */
1269 Py_XDECREF(akey);
1270 Py_XDECREF(aval);
1271 akey = thiskey;
1272 aval = thisaval;
1273 }
1274 else {
1275 Py_DECREF(thiskey);
1276 Py_DECREF(thisaval);
1277 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001278 }
Tim Peters95bf9392001-05-10 08:32:44 +00001279 *pval = aval;
1280 return akey;
1281
1282Fail:
1283 Py_XDECREF(akey);
1284 Py_XDECREF(aval);
1285 *pval = NULL;
1286 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001287}
1288
1289static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001290dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001291{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001292 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001293 int res;
1294
1295 /* Compare lengths first */
1296 if (a->ma_used < b->ma_used)
1297 return -1; /* a is shorter */
1298 else if (a->ma_used > b->ma_used)
1299 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001300
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001301 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001302 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001303 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001304 if (adiff == NULL) {
1305 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001306 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001307 * must be equal.
1308 */
1309 res = PyErr_Occurred() ? -1 : 0;
1310 goto Finished;
1311 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001312 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001313 if (bdiff == NULL && PyErr_Occurred()) {
1314 assert(!bval);
1315 res = -1;
1316 goto Finished;
1317 }
1318 res = 0;
1319 if (bdiff) {
1320 /* bdiff == NULL "should be" impossible now, but perhaps
1321 * the last comparison done by the characterize() on a had
1322 * the side effect of making the dicts equal!
1323 */
1324 res = PyObject_Compare(adiff, bdiff);
1325 }
1326 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001327 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001328
1329Finished:
1330 Py_XDECREF(adiff);
1331 Py_XDECREF(bdiff);
1332 Py_XDECREF(aval);
1333 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001334 return res;
1335}
1336
Tim Peterse63415e2001-05-08 04:38:29 +00001337/* Return 1 if dicts equal, 0 if not, -1 if error.
1338 * Gets out as soon as any difference is detected.
1339 * Uses only Py_EQ comparison.
1340 */
1341static int
1342dict_equal(dictobject *a, dictobject *b)
1343{
1344 int i;
1345
1346 if (a->ma_used != b->ma_used)
1347 /* can't be equal if # of entries differ */
1348 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001349
Tim Peterse63415e2001-05-08 04:38:29 +00001350 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001351 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001352 PyObject *aval = a->ma_table[i].me_value;
1353 if (aval != NULL) {
1354 int cmp;
1355 PyObject *bval;
1356 PyObject *key = a->ma_table[i].me_key;
1357 /* temporarily bump aval's refcount to ensure it stays
1358 alive until we're done with it */
1359 Py_INCREF(aval);
1360 bval = PyDict_GetItem((PyObject *)b, key);
1361 if (bval == NULL) {
1362 Py_DECREF(aval);
1363 return 0;
1364 }
1365 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1366 Py_DECREF(aval);
1367 if (cmp <= 0) /* error or not equal */
1368 return cmp;
1369 }
1370 }
1371 return 1;
1372 }
1373
1374static PyObject *
1375dict_richcompare(PyObject *v, PyObject *w, int op)
1376{
1377 int cmp;
1378 PyObject *res;
1379
1380 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1381 res = Py_NotImplemented;
1382 }
1383 else if (op == Py_EQ || op == Py_NE) {
1384 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1385 if (cmp < 0)
1386 return NULL;
1387 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1388 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001389 else
1390 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001391 Py_INCREF(res);
1392 return res;
1393 }
1394
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001395static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001396dict_has_key(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001397{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001398 PyObject *key;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001399 long hash;
1400 register long ok;
Fred Drake52fccfd2000-02-23 15:47:16 +00001401 if (!PyArg_ParseTuple(args, "O:has_key", &key))
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001402 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001403#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001404 if (!PyString_Check(key) ||
1405 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001406#endif
Guido van Rossumca756f21997-01-23 19:39:29 +00001407 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001408 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001409 if (hash == -1)
1410 return NULL;
1411 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001412 ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001413 return PyInt_FromLong(ok);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001414}
1415
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001416static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001417dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001418{
1419 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001420 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001421 PyObject *val = NULL;
1422 long hash;
1423
Fred Drake52fccfd2000-02-23 15:47:16 +00001424 if (!PyArg_ParseTuple(args, "O|O:get", &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001425 return NULL;
1426
Barry Warsawc38c5da1997-10-06 17:49:20 +00001427#ifdef CACHE_HASH
1428 if (!PyString_Check(key) ||
1429 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1430#endif
1431 {
1432 hash = PyObject_Hash(key);
1433 if (hash == -1)
1434 return NULL;
1435 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001436 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Barry Warsaw320ac331997-10-20 17:26:25 +00001437
Barry Warsawc38c5da1997-10-06 17:49:20 +00001438 if (val == NULL)
1439 val = failobj;
1440 Py_INCREF(val);
1441 return val;
1442}
1443
1444
1445static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001446dict_setdefault(register dictobject *mp, PyObject *args)
1447{
1448 PyObject *key;
1449 PyObject *failobj = Py_None;
1450 PyObject *val = NULL;
1451 long hash;
1452
1453 if (!PyArg_ParseTuple(args, "O|O:setdefault", &key, &failobj))
1454 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001455
1456#ifdef CACHE_HASH
1457 if (!PyString_Check(key) ||
1458 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1459#endif
1460 {
1461 hash = PyObject_Hash(key);
1462 if (hash == -1)
1463 return NULL;
1464 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001465 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001466 if (val == NULL) {
1467 val = failobj;
1468 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1469 val = NULL;
1470 }
1471 Py_XINCREF(val);
1472 return val;
1473}
1474
1475
1476static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001477dict_clear(register dictobject *mp, PyObject *args)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001478{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001479 if (!PyArg_NoArgs(args))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001480 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001481 PyDict_Clear((PyObject *)mp);
1482 Py_INCREF(Py_None);
1483 return Py_None;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001484}
1485
Guido van Rossumba6ab842000-12-12 22:02:18 +00001486static PyObject *
1487dict_popitem(dictobject *mp, PyObject *args)
1488{
1489 int i = 0;
1490 dictentry *ep;
1491 PyObject *res;
1492
1493 if (!PyArg_NoArgs(args))
1494 return NULL;
Tim Petersf4b33f62001-06-02 05:42:29 +00001495 /* Allocate the result tuple before checking the size. Believe it
1496 * or not, this allocation could trigger a garbage collection which
1497 * could empty the dict, so if we checked the size first and that
1498 * happened, the result would be an infinite loop (searching for an
1499 * entry that no longer exists). Note that the usual popitem()
1500 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1501 * tuple away if the dict *is* empty isn't a significant
1502 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001503 */
1504 res = PyTuple_New(2);
1505 if (res == NULL)
1506 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001507 if (mp->ma_used == 0) {
1508 Py_DECREF(res);
1509 PyErr_SetString(PyExc_KeyError,
1510 "popitem(): dictionary is empty");
1511 return NULL;
1512 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001513 /* Set ep to "the first" dict entry with a value. We abuse the hash
1514 * field of slot 0 to hold a search finger:
1515 * If slot 0 has a value, use slot 0.
1516 * Else slot 0 is being used to hold a search finger,
1517 * and we use its hash value as the first index to look.
1518 */
1519 ep = &mp->ma_table[0];
1520 if (ep->me_value == NULL) {
1521 i = (int)ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001522 /* The hash field may be a real hash value, or it may be a
1523 * legit search finger, or it may be a once-legit search
1524 * finger that's out of bounds now because it wrapped around
1525 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001526 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001527 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001528 i = 1; /* skip slot 0 */
1529 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1530 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001531 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001532 i = 1;
1533 }
1534 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001535 PyTuple_SET_ITEM(res, 0, ep->me_key);
1536 PyTuple_SET_ITEM(res, 1, ep->me_value);
1537 Py_INCREF(dummy);
1538 ep->me_key = dummy;
1539 ep->me_value = NULL;
1540 mp->ma_used--;
1541 assert(mp->ma_table[0].me_value == NULL);
1542 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001543 return res;
1544}
1545
Jeremy Hylton8caad492000-06-23 14:18:11 +00001546static int
1547dict_traverse(PyObject *op, visitproc visit, void *arg)
1548{
1549 int i = 0, err;
1550 PyObject *pk;
1551 PyObject *pv;
1552
1553 while (PyDict_Next(op, &i, &pk, &pv)) {
1554 err = visit(pk, arg);
1555 if (err)
1556 return err;
1557 err = visit(pv, arg);
1558 if (err)
1559 return err;
1560 }
1561 return 0;
1562}
1563
1564static int
1565dict_tp_clear(PyObject *op)
1566{
1567 PyDict_Clear(op);
1568 return 0;
1569}
1570
Tim Petersf7f88b12000-12-13 23:18:45 +00001571
Guido van Rossum09e563a2001-05-01 12:10:21 +00001572staticforward PyObject *dictiter_new(dictobject *, binaryfunc);
1573
1574static PyObject *
1575select_key(PyObject *key, PyObject *value)
1576{
1577 Py_INCREF(key);
1578 return key;
1579}
1580
1581static PyObject *
1582select_value(PyObject *key, PyObject *value)
1583{
1584 Py_INCREF(value);
1585 return value;
1586}
1587
1588static PyObject *
1589select_item(PyObject *key, PyObject *value)
1590{
1591 PyObject *res = PyTuple_New(2);
1592
1593 if (res != NULL) {
1594 Py_INCREF(key);
1595 Py_INCREF(value);
1596 PyTuple_SET_ITEM(res, 0, key);
1597 PyTuple_SET_ITEM(res, 1, value);
1598 }
1599 return res;
1600}
1601
1602static PyObject *
1603dict_iterkeys(dictobject *dict, PyObject *args)
1604{
1605 if (!PyArg_ParseTuple(args, ""))
1606 return NULL;
1607 return dictiter_new(dict, select_key);
1608}
1609
1610static PyObject *
1611dict_itervalues(dictobject *dict, PyObject *args)
1612{
1613 if (!PyArg_ParseTuple(args, ""))
1614 return NULL;
1615 return dictiter_new(dict, select_value);
1616}
1617
1618static PyObject *
1619dict_iteritems(dictobject *dict, PyObject *args)
1620{
1621 if (!PyArg_ParseTuple(args, ""))
1622 return NULL;
1623 return dictiter_new(dict, select_item);
1624}
1625
1626
Tim Petersf7f88b12000-12-13 23:18:45 +00001627static char has_key__doc__[] =
1628"D.has_key(k) -> 1 if D has a key k, else 0";
1629
1630static char get__doc__[] =
1631"D.get(k[,d]) -> D[k] if D.has_key(k), else d. d defaults to None.";
1632
1633static char setdefault_doc__[] =
1634"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if not D.has_key(k)";
1635
1636static char popitem__doc__[] =
1637"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
16382-tuple; but raise KeyError if D is empty";
1639
1640static char keys__doc__[] =
1641"D.keys() -> list of D's keys";
1642
1643static char items__doc__[] =
1644"D.items() -> list of D's (key, value) pairs, as 2-tuples";
1645
1646static char values__doc__[] =
1647"D.values() -> list of D's values";
1648
1649static char update__doc__[] =
1650"D.update(E) -> None. Update D from E: for k in E.keys(): D[k] = E[k]";
1651
1652static char clear__doc__[] =
1653"D.clear() -> None. Remove all items from D.";
1654
1655static char copy__doc__[] =
1656"D.copy() -> a shallow copy of D";
1657
Guido van Rossum09e563a2001-05-01 12:10:21 +00001658static char iterkeys__doc__[] =
1659"D.iterkeys() -> an iterator over the keys of D";
1660
1661static char itervalues__doc__[] =
1662"D.itervalues() -> an iterator over the values of D";
1663
1664static char iteritems__doc__[] =
1665"D.iteritems() -> an iterator over the (key, value) items of D";
1666
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001667static PyMethodDef mapp_methods[] = {
Tim Petersf7f88b12000-12-13 23:18:45 +00001668 {"has_key", (PyCFunction)dict_has_key, METH_VARARGS,
1669 has_key__doc__},
1670 {"get", (PyCFunction)dict_get, METH_VARARGS,
1671 get__doc__},
1672 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1673 setdefault_doc__},
1674 {"popitem", (PyCFunction)dict_popitem, METH_OLDARGS,
1675 popitem__doc__},
1676 {"keys", (PyCFunction)dict_keys, METH_OLDARGS,
1677 keys__doc__},
1678 {"items", (PyCFunction)dict_items, METH_OLDARGS,
1679 items__doc__},
1680 {"values", (PyCFunction)dict_values, METH_OLDARGS,
1681 values__doc__},
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001682 {"update", (PyCFunction)dict_update, METH_VARARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001683 update__doc__},
1684 {"clear", (PyCFunction)dict_clear, METH_OLDARGS,
1685 clear__doc__},
1686 {"copy", (PyCFunction)dict_copy, METH_OLDARGS,
1687 copy__doc__},
Guido van Rossum09e563a2001-05-01 12:10:21 +00001688 {"iterkeys", (PyCFunction)dict_iterkeys, METH_VARARGS,
1689 iterkeys__doc__},
1690 {"itervalues", (PyCFunction)dict_itervalues, METH_VARARGS,
1691 itervalues__doc__},
1692 {"iteritems", (PyCFunction)dict_iteritems, METH_VARARGS,
1693 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001694 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001695};
1696
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001697static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001698dict_getattr(dictobject *mp, char *name)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001699{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001700 return Py_FindMethod(mapp_methods, (PyObject *)mp, name);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001701}
1702
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001703static int
1704dict_contains(dictobject *mp, PyObject *key)
1705{
1706 long hash;
1707
1708#ifdef CACHE_HASH
1709 if (!PyString_Check(key) ||
1710 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1711#endif
1712 {
1713 hash = PyObject_Hash(key);
1714 if (hash == -1)
1715 return -1;
1716 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001717 return (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001718}
1719
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001720/* Hack to implement "key in dict" */
1721static PySequenceMethods dict_as_sequence = {
1722 0, /* sq_length */
1723 0, /* sq_concat */
1724 0, /* sq_repeat */
1725 0, /* sq_item */
1726 0, /* sq_slice */
1727 0, /* sq_ass_item */
1728 0, /* sq_ass_slice */
1729 (objobjproc)dict_contains, /* sq_contains */
1730 0, /* sq_inplace_concat */
1731 0, /* sq_inplace_repeat */
1732};
1733
Guido van Rossum09e563a2001-05-01 12:10:21 +00001734static PyObject *
1735dict_iter(dictobject *dict)
1736{
1737 return dictiter_new(dict, select_key);
1738}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001739
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001740PyTypeObject PyDict_Type = {
1741 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001742 0,
Guido van Rossum9bfef441993-03-29 10:43:31 +00001743 "dictionary",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001744 sizeof(dictobject) + PyGC_HEAD_SIZE,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001745 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001746 (destructor)dict_dealloc, /* tp_dealloc */
1747 (printfunc)dict_print, /* tp_print */
1748 (getattrfunc)dict_getattr, /* tp_getattr */
1749 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00001750 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001751 (reprfunc)dict_repr, /* tp_repr */
1752 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001753 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001754 &dict_as_mapping, /* tp_as_mapping */
1755 0, /* tp_hash */
1756 0, /* tp_call */
1757 0, /* tp_str */
1758 0, /* tp_getattro */
1759 0, /* tp_setattro */
1760 0, /* tp_as_buffer */
1761 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
1762 0, /* tp_doc */
1763 (traverseproc)dict_traverse, /* tp_traverse */
1764 (inquiry)dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001765 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001766 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001767 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001768 0, /* tp_iternext */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001769};
1770
Guido van Rossum3cca2451997-05-16 14:23:33 +00001771/* For backward compatibility with old dictionary interface */
1772
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001773PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001774PyDict_GetItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001775{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001776 PyObject *kv, *rv;
1777 kv = PyString_FromString(key);
1778 if (kv == NULL)
1779 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001780 rv = PyDict_GetItem(v, kv);
1781 Py_DECREF(kv);
1782 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001783}
1784
1785int
Tim Peters1f5871e2000-07-04 17:44:48 +00001786PyDict_SetItemString(PyObject *v, char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001787{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001788 PyObject *kv;
1789 int err;
1790 kv = PyString_FromString(key);
1791 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001792 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00001793 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00001794 err = PyDict_SetItem(v, kv, item);
1795 Py_DECREF(kv);
1796 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001797}
1798
1799int
Tim Peters1f5871e2000-07-04 17:44:48 +00001800PyDict_DelItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001801{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001802 PyObject *kv;
1803 int err;
1804 kv = PyString_FromString(key);
1805 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001806 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001807 err = PyDict_DelItem(v, kv);
1808 Py_DECREF(kv);
1809 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001810}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001811
1812/* Dictionary iterator type */
1813
1814extern PyTypeObject PyDictIter_Type; /* Forward */
1815
1816typedef struct {
1817 PyObject_HEAD
1818 dictobject *di_dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001819 int di_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001820 int di_pos;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001821 binaryfunc di_select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001822} dictiterobject;
1823
1824static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001825dictiter_new(dictobject *dict, binaryfunc select)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001826{
1827 dictiterobject *di;
1828 di = PyObject_NEW(dictiterobject, &PyDictIter_Type);
1829 if (di == NULL)
1830 return NULL;
1831 Py_INCREF(dict);
1832 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001833 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001834 di->di_pos = 0;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001835 di->di_select = select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001836 return (PyObject *)di;
1837}
1838
1839static void
1840dictiter_dealloc(dictiterobject *di)
1841{
1842 Py_DECREF(di->di_dict);
1843 PyObject_DEL(di);
1844}
1845
1846static PyObject *
1847dictiter_next(dictiterobject *di, PyObject *args)
1848{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001849 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001850
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001851 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001852 PyErr_SetString(PyExc_RuntimeError,
1853 "dictionary changed size during iteration");
1854 return NULL;
1855 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001856 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1857 return (*di->di_select)(key, value);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001858 }
1859 PyErr_SetObject(PyExc_StopIteration, Py_None);
1860 return NULL;
1861}
1862
1863static PyObject *
1864dictiter_getiter(PyObject *it)
1865{
1866 Py_INCREF(it);
1867 return it;
1868}
1869
1870static PyMethodDef dictiter_methods[] = {
1871 {"next", (PyCFunction)dictiter_next, METH_VARARGS,
1872 "it.next() -- get the next value, or raise StopIteration"},
1873 {NULL, NULL} /* sentinel */
1874};
1875
1876static PyObject *
Guido van Rossum213c7a62001-04-23 14:08:49 +00001877dictiter_getattr(dictiterobject *di, char *name)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001878{
Guido van Rossum213c7a62001-04-23 14:08:49 +00001879 return Py_FindMethod(dictiter_methods, (PyObject *)di, name);
1880}
1881
1882static PyObject *dictiter_iternext(dictiterobject *di)
1883{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001884 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001885
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001886 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00001887 PyErr_SetString(PyExc_RuntimeError,
1888 "dictionary changed size during iteration");
1889 return NULL;
1890 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001891 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1892 return (*di->di_select)(key, value);
Guido van Rossum213c7a62001-04-23 14:08:49 +00001893 }
1894 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001895}
1896
1897PyTypeObject PyDictIter_Type = {
1898 PyObject_HEAD_INIT(&PyType_Type)
1899 0, /* ob_size */
1900 "dictionary-iterator", /* tp_name */
1901 sizeof(dictiterobject), /* tp_basicsize */
1902 0, /* tp_itemsize */
1903 /* methods */
1904 (destructor)dictiter_dealloc, /* tp_dealloc */
1905 0, /* tp_print */
1906 (getattrfunc)dictiter_getattr, /* tp_getattr */
1907 0, /* tp_setattr */
1908 0, /* tp_compare */
1909 0, /* tp_repr */
1910 0, /* tp_as_number */
1911 0, /* tp_as_sequence */
1912 0, /* tp_as_mapping */
1913 0, /* tp_hash */
1914 0, /* tp_call */
1915 0, /* tp_str */
1916 0, /* tp_getattro */
1917 0, /* tp_setattro */
1918 0, /* tp_as_buffer */
1919 Py_TPFLAGS_DEFAULT, /* tp_flags */
1920 0, /* tp_doc */
1921 0, /* tp_traverse */
1922 0, /* tp_clear */
1923 0, /* tp_richcompare */
1924 0, /* tp_weaklistoffset */
1925 (getiterfunc)dictiter_getiter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001926 (iternextfunc)dictiter_iternext, /* tp_iternext */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001927};