blob: 5944b6ef89f70fea64d69b070832c38dfe51f9fd [file] [log] [blame]
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001
Guido van Rossum2bc13791999-03-24 19:06:42 +00002/* Dictionary object implementation using a hash table */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Guido van Rossum4b1302b1993-03-27 18:11:32 +00005
6
7/*
Tim Petersdea48ec2001-05-22 20:40:22 +00008 * MINSIZE is the minimum size of a dictionary. This many slots are
9 * allocated directly in the dict object (in the ma_smalltable member).
10 * This must be a power of 2, and the first entry in the polys[] vector must
11 * match.
Guido van Rossum16e93a81997-01-28 00:00:11 +000012 */
Tim Petersdea48ec2001-05-22 20:40:22 +000013#define MINSIZE 8
Guido van Rossum16e93a81997-01-28 00:00:11 +000014
Fred Drake1bff34a2000-08-31 19:31:38 +000015/* define this out if you don't want conversion statistics on exit */
16#undef SHOW_CONVERSION_COUNTS
17
Guido van Rossum16e93a81997-01-28 00:00:11 +000018/*
19Table of irreducible polynomials to efficiently cycle through
Tim Petersea8f2bf2000-12-13 01:02:46 +000020GF(2^n)-{0}, 2<=n<=30. A table size is always a power of 2.
Tim Petersdea48ec2001-05-22 20:40:22 +000021For a table size of 2**i, the polys entry is 2**i + j for some j in 1 thru
222**i-1 inclusive. The polys[] entries here happen to add in the smallest j
23values "that work". Work means this: given any integer k in 1 thru 2**i-1
24inclusive, a poly works if & only if repeating this code:
25 print k
26 k <<= 1
27 if k >= 2**i:
28 k ^= poly
29prints every integer in 1 thru 2**i-1 inclusive exactly once before printing
30k a second time. Theory can be used to find such polys efficiently, but the
31operational defn. of "works" is sufficient to find them in reasonable time
32via brute force program (hint: any poly that has an even number of 1 bits
33cannot work; ditto any poly with low bit 0; exploit those).
Tim Peters15d49292001-05-27 07:39:22 +000034
35Some major subtleties: Most hash schemes depend on having a "good" hash
36function, in the sense of simulating randomness. Python doesn't: some of
37its hash functions are trivial, such as hash(i) == i for ints i (excepting
38i == -1, because -1 is the "error occurred" return value from tp_hash).
39
40This isn't necessarily bad! To the contrary, that our hash tables are powers
41of 2 in size, and that we take the low-order bits as the initial table index,
42means that there are no collisions at all for dicts indexed by a contiguous
43range of ints. This is "better than random" behavior, and that's very
44desirable.
45
46On the other hand, when collisions occur, the tendency to fill contiguous
47slices of the hash table makes a good collision resolution strategy crucial;
48e.g., linear probing is right out.
49
50Reimer Behrends contributed the idea of using a polynomial-based approach,
51using repeated multiplication by x in GF(2**n) where a polynomial is chosen
52such that x is a primitive root. This visits every table location exactly
53once, and the sequence of locations probed is highly non-linear.
54
55The same is also largely true of quadratic probing for power-of-2 tables, of
56the specific
57
58 (i + comb(1, 2)) mod size
59 (i + comb(2, 2)) mod size
60 (i + comb(3, 2)) mod size
61 (i + comb(4, 2)) mod size
62 ...
63 (i + comb(j, 2)) mod size
64
65flavor. The polynomial approach "scrambles" the probe indices better, but
66more importantly allows to get *some* additional bits of the hash code into
67play via computing the initial increment, thus giving a weak form of double
68hashing. Quadratic probing cannot be extended that way (the first probe
69offset must be 1, the second 3, the third 6, etc).
70
71Christian Tismer later contributed the idea of using polynomial division
72instead of multiplication. The problem is that the multiplicative method
73can't get *all* the bits of the hash code into play without expensive
74computations that slow down the initial index and/or initial increment
75computation. For a set of keys like [i << 16 for i in range(20000)], under
76the multiplicative method the initial index and increment were the same for
77all keys, so every key followed exactly the same probe sequence, and so
78this degenerated into a (very slow) linear search. The division method uses
79all the bits of the hash code naturally in the increment, although it *may*
80visit locations more than once until such time as all the high bits of the
81increment have been shifted away. It's also impossible to tell in advance
82whether incr is congruent to 0 modulo poly, so each iteration of the loop has
83to guard against incr becoming 0. These are minor costs, as we usually don't
84get into the probe loop, and when we do we usually get out on its first
85iteration.
Guido van Rossum4b1302b1993-03-27 18:11:32 +000086*/
Tim Petersdea48ec2001-05-22 20:40:22 +000087
Guido van Rossum16e93a81997-01-28 00:00:11 +000088static long polys[] = {
Tim Petersdea48ec2001-05-22 20:40:22 +000089/* 4 + 3, */ /* first active entry if MINSIZE == 4 */
90 8 + 3, /* first active entry if MINSIZE == 8 */
Guido van Rossum9e5656c1997-01-29 04:45:16 +000091 16 + 3,
92 32 + 5,
93 64 + 3,
94 128 + 3,
95 256 + 29,
96 512 + 17,
97 1024 + 9,
98 2048 + 5,
99 4096 + 83,
100 8192 + 27,
101 16384 + 43,
102 32768 + 3,
103 65536 + 45,
104 131072 + 9,
105 262144 + 39,
106 524288 + 39,
107 1048576 + 9,
108 2097152 + 5,
109 4194304 + 3,
110 8388608 + 33,
111 16777216 + 27,
112 33554432 + 9,
113 67108864 + 71,
114 134217728 + 39,
115 268435456 + 9,
116 536870912 + 5,
Tim Petersdea48ec2001-05-22 20:40:22 +0000117 1073741824 + 83
118 /* 2147483648 + 9 -- if we ever boost this to unsigned long */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000119};
120
121/* Object used as dummy key to fill deleted entries */
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000122static PyObject *dummy; /* Initialized by first call to newdictobject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000123
124/*
Tim Petersea8f2bf2000-12-13 01:02:46 +0000125There are three kinds of slots in the table:
126
1271. Unused. me_key == me_value == NULL
128 Does not hold an active (key, value) pair now and never did. Unused can
129 transition to Active upon key insertion. This is the only case in which
130 me_key is NULL, and is each slot's initial state.
131
1322. Active. me_key != NULL and me_key != dummy and me_value != NULL
133 Holds an active (key, value) pair. Active can transition to Dummy upon
134 key deletion. This is the only case in which me_value != NULL.
135
Tim Petersf1c7c882000-12-13 19:58:25 +00001363. Dummy. me_key == dummy and me_value == NULL
Tim Petersea8f2bf2000-12-13 01:02:46 +0000137 Previously held an active (key, value) pair, but that was deleted and an
138 active pair has not yet overwritten the slot. Dummy can transition to
139 Active upon key insertion. Dummy slots cannot be made Unused again
140 (cannot have me_key set to NULL), else the probe sequence in case of
141 collision would have no way to know they were once active.
Tim Petersf1c7c882000-12-13 19:58:25 +0000142
143Note: .popitem() abuses the me_hash field of an Unused or Dummy slot to
144hold a search finger. The me_hash field of Unused or Dummy slots has no
145meaning otherwise.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000146*/
147typedef struct {
Tim Petersea8f2bf2000-12-13 01:02:46 +0000148 long me_hash; /* cached hash code of me_key */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000149 PyObject *me_key;
150 PyObject *me_value;
Guido van Rossum36488841997-04-11 19:14:07 +0000151#ifdef USE_CACHE_ALIGNED
152 long aligner;
153#endif
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000154} dictentry;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000155
156/*
Tim Petersf1c7c882000-12-13 19:58:25 +0000157To ensure the lookup algorithm terminates, there must be at least one Unused
Tim Petersea8f2bf2000-12-13 01:02:46 +0000158slot (NULL key) in the table.
159The value ma_fill is the number of non-NULL keys (sum of Active and Dummy);
160ma_used is the number of non-NULL, non-dummy keys (== the number of non-NULL
161values == the number of Active items).
162To avoid slowing down lookups on a near-full table, we resize the table when
Tim Peters67830702001-03-21 19:23:56 +0000163it's two-thirds full.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000164*/
Fred Drake1bff34a2000-08-31 19:31:38 +0000165typedef struct dictobject dictobject;
166struct dictobject {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000167 PyObject_HEAD
Tim Petersea8f2bf2000-12-13 01:02:46 +0000168 int ma_fill; /* # Active + # Dummy */
169 int ma_used; /* # Active */
170 int ma_size; /* total # slots in ma_table */
171 int ma_poly; /* appopriate entry from polys vector */
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; \
203 (mp)->ma_size = MINSIZE; \
204 (mp)->ma_used = (mp)->ma_fill = 0; \
205 (mp)->ma_poly = polys[0]; \
206 assert(MINSIZE < (mp)->ma_poly && (mp)->ma_poly < MINSIZE*2); \
207 } while(0)
208
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000209PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000210PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000211{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000212 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000213 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000215 if (dummy == NULL)
216 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000217#ifdef SHOW_CONVERSION_COUNTS
218 Py_AtExit(show_counts);
219#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000220 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000221 mp = PyObject_NEW(dictobject, &PyDict_Type);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000222 if (mp == NULL)
223 return NULL;
Tim Petersdea48ec2001-05-22 20:40:22 +0000224 empty_to_minsize(mp);
Fred Drake1bff34a2000-08-31 19:31:38 +0000225 mp->ma_lookup = lookdict_string;
226#ifdef SHOW_CONVERSION_COUNTS
227 ++created;
228#endif
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000229 PyObject_GC_Init(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000230 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000231}
232
233/*
234The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000235This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000236Open addressing is preferred over chaining since the link overhead for
237chaining would be substantial (100% with typical malloc overhead).
Guido van Rossum16e93a81997-01-28 00:00:11 +0000238However, instead of going through the table at constant steps, we cycle
Tim Petersea8f2bf2000-12-13 01:02:46 +0000239through the values of GF(2^n). This avoids modulo computations, being
Guido van Rossum16e93a81997-01-28 00:00:11 +0000240much cheaper on RISC machines, without leading to clustering.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000241
Guido van Rossum2bc13791999-03-24 19:06:42 +0000242The initial probe index is computed as hash mod the table size.
Tim Petersea8f2bf2000-12-13 01:02:46 +0000243Subsequent probe indices use the values of x^i in GF(2^n)-{0} as an offset,
244where x is a root. The initial offset is derived from hash, too.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000245
246All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000247
248(This version is due to Reimer Behrends, some ideas are also due to
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000249Jyrki Alakuijala and Vladimir Marangozov.)
Fred Drake1bff34a2000-08-31 19:31:38 +0000250
251This function must never return NULL; failures are indicated by returning
252a dictentry* for which the me_value field is NULL. Exceptions are never
253reported by this function, and outstanding exceptions are maintained.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000254*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000255static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000256lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000257{
Guido van Rossum16e93a81997-01-28 00:00:11 +0000258 register int i;
Tim Peters15d49292001-05-27 07:39:22 +0000259 register unsigned int incr;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000260 register dictentry *freeslot;
Guido van Rossum2095d241997-04-09 19:41:24 +0000261 register unsigned int mask = mp->ma_size-1;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000262 dictentry *ep0 = mp->ma_table;
263 register dictentry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000264 register int restore_error = 0;
265 register int checked_error = 0;
266 register int cmp;
267 PyObject *err_type, *err_value, *err_tb;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000268 /* We must come up with (i, incr) such that 0 <= i < ma_size
Tim Petersea8f2bf2000-12-13 01:02:46 +0000269 and 0 < incr < ma_size and both are a function of hash.
270 i is the initial table index and incr the initial probe offset. */
Tim Peters2f228e72001-05-13 00:19:31 +0000271 i = hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000272 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000273 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000274 return ep;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000275 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000276 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000277 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000278 if (ep->me_hash == hash) {
279 /* error can't have been checked yet */
280 checked_error = 1;
281 if (PyErr_Occurred()) {
282 restore_error = 1;
283 PyErr_Fetch(&err_type, &err_value, &err_tb);
284 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000285 cmp = PyObject_RichCompareBool(ep->me_key, key, Py_EQ);
286 if (cmp > 0) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000287 if (restore_error)
288 PyErr_Restore(err_type, err_value,
289 err_tb);
290 return ep;
291 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000292 else if (cmp < 0)
293 PyErr_Clear();
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000294 }
295 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000296 }
Guido van Rossum2bc13791999-03-24 19:06:42 +0000297 /* Derive incr from hash, just to make it more arbitrary. Note that
Guido van Rossum16e93a81997-01-28 00:00:11 +0000298 incr must not be 0, or we will get into an infinite loop.*/
Tim Peters15d49292001-05-27 07:39:22 +0000299 incr = hash ^ ((unsigned long)hash >> 3);
300
Tim Peters342c65e2001-05-13 06:43:53 +0000301 /* In the loop, me_key == dummy is by far (factor of 100s) the
302 least likely outcome, so test for that last. */
Guido van Rossum16e93a81997-01-28 00:00:11 +0000303 for (;;) {
Tim Peters15d49292001-05-27 07:39:22 +0000304 if (!incr)
305 incr = 1; /* and incr will never be 0 again */
306 ep = &ep0[(i + incr) & mask];
Guido van Rossum16e93a81997-01-28 00:00:11 +0000307 if (ep->me_key == NULL) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000308 if (restore_error)
309 PyErr_Restore(err_type, err_value, err_tb);
Tim Peters342c65e2001-05-13 06:43:53 +0000310 return freeslot == NULL ? ep : freeslot;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000311 }
Tim Peters342c65e2001-05-13 06:43:53 +0000312 if (ep->me_key == key) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000313 if (restore_error)
314 PyErr_Restore(err_type, err_value, err_tb);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000315 return ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000316 }
Tim Peters342c65e2001-05-13 06:43:53 +0000317 else if (ep->me_hash == hash && ep->me_key != dummy) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000318 if (!checked_error) {
319 checked_error = 1;
320 if (PyErr_Occurred()) {
321 restore_error = 1;
322 PyErr_Fetch(&err_type, &err_value,
323 &err_tb);
324 }
325 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000326 cmp = PyObject_RichCompareBool(ep->me_key, key, Py_EQ);
327 if (cmp > 0) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000328 if (restore_error)
329 PyErr_Restore(err_type, err_value,
330 err_tb);
331 return ep;
332 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000333 else if (cmp < 0)
334 PyErr_Clear();
Guido van Rossum16e93a81997-01-28 00:00:11 +0000335 }
Tim Peters342c65e2001-05-13 06:43:53 +0000336 else if (ep->me_key == dummy && freeslot == NULL)
337 freeslot = ep;
Tim Peters15d49292001-05-27 07:39:22 +0000338 /* Cycle through GF(2**n). */
339 if (incr & 1)
340 incr ^= mp->ma_poly; /* clears the lowest bit */
341 incr >>= 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000342 }
343}
344
345/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000346 * Hacked up version of lookdict which can assume keys are always strings;
347 * this assumption allows testing for errors during PyObject_Compare() to
348 * be dropped; string-string comparisons never raise exceptions. This also
349 * means we don't need to go through PyObject_Compare(); we can always use
Martin v. Löwiscd353062001-05-24 16:56:35 +0000350 * _PyString_Eq directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000351 *
352 * This really only becomes meaningful if proper error handling in lookdict()
353 * is too expensive.
354 */
355static dictentry *
356lookdict_string(dictobject *mp, PyObject *key, register long hash)
357{
358 register int i;
Tim Peters15d49292001-05-27 07:39:22 +0000359 register unsigned int incr;
Fred Drake1bff34a2000-08-31 19:31:38 +0000360 register dictentry *freeslot;
361 register unsigned int mask = mp->ma_size-1;
362 dictentry *ep0 = mp->ma_table;
363 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000364
365 /* make sure this function doesn't have to handle non-string keys */
366 if (!PyString_Check(key)) {
367#ifdef SHOW_CONVERSION_COUNTS
368 ++converted;
369#endif
370 mp->ma_lookup = lookdict;
371 return lookdict(mp, key, hash);
372 }
373 /* We must come up with (i, incr) such that 0 <= i < ma_size
374 and 0 < incr < ma_size and both are a function of hash */
Tim Peters2f228e72001-05-13 00:19:31 +0000375 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000376 ep = &ep0[i];
377 if (ep->me_key == NULL || ep->me_key == key)
378 return ep;
379 if (ep->me_key == dummy)
380 freeslot = ep;
381 else {
382 if (ep->me_hash == hash
Martin v. Löwiscd353062001-05-24 16:56:35 +0000383 && _PyString_Eq(ep->me_key, key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000384 return ep;
385 }
386 freeslot = NULL;
387 }
388 /* Derive incr from hash, just to make it more arbitrary. Note that
389 incr must not be 0, or we will get into an infinite loop.*/
Tim Peters15d49292001-05-27 07:39:22 +0000390 incr = hash ^ ((unsigned long)hash >> 3);
391
Tim Peters342c65e2001-05-13 06:43:53 +0000392 /* In the loop, me_key == dummy is by far (factor of 100s) the
393 least likely outcome, so test for that last. */
Fred Drake1bff34a2000-08-31 19:31:38 +0000394 for (;;) {
Tim Peters15d49292001-05-27 07:39:22 +0000395 if (!incr)
396 incr = 1; /* and incr will never be 0 again */
397 ep = &ep0[(i + incr) & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000398 if (ep->me_key == NULL)
399 return freeslot == NULL ? ep : freeslot;
400 if (ep->me_key == key
401 || (ep->me_hash == hash
402 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000403 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000404 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000405 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000406 freeslot = ep;
Tim Peters15d49292001-05-27 07:39:22 +0000407 /* Cycle through GF(2**n). */
408 if (incr & 1)
409 incr ^= mp->ma_poly; /* clears the lowest bit */
410 incr >>= 1;
Fred Drake1bff34a2000-08-31 19:31:38 +0000411 }
412}
413
414/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000415Internal routine to insert a new item into the table.
416Used both by the internal resize routine and by the public insert routine.
417Eats a reference to key and one to value.
418*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000419static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000420insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000421{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000422 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000423 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000424 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000425 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000426 old_value = ep->me_value;
427 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000428 Py_DECREF(old_value); /* which **CAN** re-enter */
429 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000430 }
431 else {
432 if (ep->me_key == NULL)
433 mp->ma_fill++;
434 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000435 Py_DECREF(ep->me_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000436 ep->me_key = key;
437 ep->me_hash = hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000438 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000439 mp->ma_used++;
440 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000441}
442
443/*
444Restructure the table by allocating a new table and reinserting all
445items again. When entries have been deleted, the new table may
446actually be smaller than the old one.
447*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000448static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000449dictresize(dictobject *mp, int minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000450{
Tim Peters0c6010b2001-05-23 23:33:57 +0000451 int newsize, newpoly;
452 dictentry *oldtable, *newtable, *ep;
453 int i;
454 int is_oldtable_malloced;
455 dictentry small_copy[MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000456
457 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000458
459 /* Find the smallest table size > minused, and its poly[] entry. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000460 newpoly = 0;
461 newsize = MINSIZE;
462 for (i = 0; i < sizeof(polys)/sizeof(polys[0]); ++i) {
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000463 if (newsize > minused) {
Guido van Rossum16e93a81997-01-28 00:00:11 +0000464 newpoly = polys[i];
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000465 break;
466 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000467 newsize <<= 1;
468 if (newsize < 0) /* overflow */
469 break;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000470 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000471 if (newpoly == 0) {
472 /* Ran out of polynomials or newsize overflowed. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000473 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000474 return -1;
475 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000476
477 /* Get space for a new table. */
478 oldtable = mp->ma_table;
479 assert(oldtable != NULL);
480 is_oldtable_malloced = oldtable != mp->ma_smalltable;
481
Tim Petersdea48ec2001-05-22 20:40:22 +0000482 if (newsize == MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000483 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000484 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000485 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000486 if (mp->ma_fill == mp->ma_used) {
487 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000488 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000489 }
490 /* We're not going to resize it, but rebuild the
491 table anyway to purge old dummy entries.
492 Subtle: This is *necessary* if fill==size,
493 as lookdict needs at least one virgin slot to
494 terminate failing searches. If fill < size, it's
495 merely desirable, as dummies slow searches. */
496 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000497 memcpy(small_copy, oldtable, sizeof(small_copy));
498 oldtable = small_copy;
499 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000500 }
501 else {
502 newtable = PyMem_NEW(dictentry, newsize);
503 if (newtable == NULL) {
504 PyErr_NoMemory();
505 return -1;
506 }
507 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000508
509 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000510 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000511 mp->ma_table = newtable;
Tim Petersdea48ec2001-05-22 20:40:22 +0000512 mp->ma_size = newsize;
513 memset(newtable, 0, sizeof(dictentry) * newsize);
514 mp->ma_poly = newpoly;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000515 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000516 i = mp->ma_fill;
517 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000518
Tim Peters19283142001-05-17 22:25:34 +0000519 /* Copy the data over; this is refcount-neutral for active entries;
520 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000521 for (ep = oldtable; i > 0; ep++) {
522 if (ep->me_value != NULL) { /* active entry */
523 --i;
Tim Peters19283142001-05-17 22:25:34 +0000524 insertdict(mp, ep->me_key, ep->me_hash, ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000525 }
Tim Peters19283142001-05-17 22:25:34 +0000526 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000527 --i;
Tim Peters19283142001-05-17 22:25:34 +0000528 assert(ep->me_key == dummy);
529 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000530 }
Tim Peters19283142001-05-17 22:25:34 +0000531 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000532 }
533
Tim Peters0c6010b2001-05-23 23:33:57 +0000534 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000535 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000536 return 0;
537}
538
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000539PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000540PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000541{
542 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000543 dictobject *mp = (dictobject *)op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000544 if (!PyDict_Check(op)) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000545 return NULL;
546 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000547#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000548 if (!PyString_Check(key) ||
549 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000550#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000551 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000552 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000553 if (hash == -1) {
554 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000555 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000556 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000557 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000558 return (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000559}
560
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000561/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
562 * dictionary if it is merely replacing the value for an existing key.
563 * This is means that it's safe to loop over a dictionary with
564 * PyDict_Next() and occasionally replace a value -- but you can't
565 * insert new keys or remove them.
566 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000567int
Tim Peters1f5871e2000-07-04 17:44:48 +0000568PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000569{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000570 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000571 register long hash;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000572 register int n_used;
573
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000574 if (!PyDict_Check(op)) {
575 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000576 return -1;
577 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000578 mp = (dictobject *)op;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000579#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000580 if (PyString_Check(key)) {
Guido van Rossum2a61e741997-01-18 07:55:05 +0000581#ifdef INTERN_STRINGS
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000582 if (((PyStringObject *)key)->ob_sinterned != NULL) {
583 key = ((PyStringObject *)key)->ob_sinterned;
584 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000585 }
586 else
587#endif
588 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000589 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000590 if (hash == -1)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000591 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000592 }
593 }
594 else
595#endif
596 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000597 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000598 if (hash == -1)
599 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000600 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000601 assert(mp->ma_fill < mp->ma_size);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000602 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000603 Py_INCREF(value);
604 Py_INCREF(key);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000605 insertdict(mp, key, hash, value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000606 /* If we added a key, we can safely resize. Otherwise skip this!
607 * If fill >= 2/3 size, adjust size. Normally, this doubles the
608 * size, but it's also possible for the dict to shrink (if ma_fill is
609 * much larger than ma_used, meaning a lot of dict keys have been
610 * deleted).
611 */
612 if (mp->ma_used > n_used && mp->ma_fill*3 >= mp->ma_size*2) {
613 if (dictresize(mp, mp->ma_used*2) != 0)
614 return -1;
615 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000616 return 0;
617}
618
619int
Tim Peters1f5871e2000-07-04 17:44:48 +0000620PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000621{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000622 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000623 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000624 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000625 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000626
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000627 if (!PyDict_Check(op)) {
628 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000629 return -1;
630 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000631#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000632 if (!PyString_Check(key) ||
633 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000634#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000635 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000636 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000637 if (hash == -1)
638 return -1;
639 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000640 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000641 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000642 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000643 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000644 return -1;
645 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000646 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000647 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000648 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000649 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000650 ep->me_value = NULL;
651 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000652 Py_DECREF(old_value);
653 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000654 return 0;
655}
656
Guido van Rossum25831651993-05-19 14:50:45 +0000657void
Tim Peters1f5871e2000-07-04 17:44:48 +0000658PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000659{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000660 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000661 dictentry *ep, *table;
662 int table_is_malloced;
663 int fill;
664 dictentry small_copy[MINSIZE];
665#ifdef Py_DEBUG
666 int i, n;
667#endif
668
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000669 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000670 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000671 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000672#ifdef Py_DEBUG
Guido van Rossumd7047b31995-01-02 19:07:15 +0000673 n = mp->ma_size;
Tim Petersdea48ec2001-05-22 20:40:22 +0000674 i = 0;
675#endif
676
677 table = mp->ma_table;
678 assert(table != NULL);
679 table_is_malloced = table != mp->ma_smalltable;
680
681 /* This is delicate. During the process of clearing the dict,
682 * decrefs can cause the dict to mutate. To avoid fatal confusion
683 * (voice of experience), we have to make the dict empty before
684 * clearing the slots, and never refer to anything via mp->xxx while
685 * clearing.
686 */
687 fill = mp->ma_fill;
688 if (table_is_malloced)
689 empty_to_minsize(mp);
690
691 else if (fill > 0) {
692 /* It's a small table with something that needs to be cleared.
693 * Afraid the only safe way is to copy the dict entries into
694 * another small table first.
695 */
696 memcpy(small_copy, table, sizeof(small_copy));
697 table = small_copy;
698 empty_to_minsize(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000699 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000700 /* else it's a small table that's already empty */
701
702 /* Now we can finally clear things. If C had refcounts, we could
703 * assert that the refcount on table is 1 now, i.e. that this function
704 * has unique access to it, so decref side-effects can't alter it.
705 */
706 for (ep = table; fill > 0; ++ep) {
707#ifdef Py_DEBUG
708 assert(i < n);
709 ++i;
710#endif
711 if (ep->me_key) {
712 --fill;
713 Py_DECREF(ep->me_key);
714 Py_XDECREF(ep->me_value);
715 }
716#ifdef Py_DEBUG
717 else
718 assert(ep->me_value == NULL);
719#endif
720 }
721
722 if (table_is_malloced)
723 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000724}
725
Tim Peters67830702001-03-21 19:23:56 +0000726/* CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
727 * mutates the dict. One exception: it is safe if the loop merely changes
728 * the values associated with the keys (but doesn't insert new keys or
729 * delete keys), via PyDict_SetItem().
730 */
Guido van Rossum25831651993-05-19 14:50:45 +0000731int
Tim Peters1f5871e2000-07-04 17:44:48 +0000732PyDict_Next(PyObject *op, int *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000733{
Guido van Rossum25831651993-05-19 14:50:45 +0000734 int i;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000735 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000736 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000737 return 0;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000738 mp = (dictobject *)op;
Guido van Rossum25831651993-05-19 14:50:45 +0000739 i = *ppos;
740 if (i < 0)
741 return 0;
742 while (i < mp->ma_size && mp->ma_table[i].me_value == NULL)
743 i++;
744 *ppos = i+1;
745 if (i >= mp->ma_size)
746 return 0;
747 if (pkey)
748 *pkey = mp->ma_table[i].me_key;
749 if (pvalue)
750 *pvalue = mp->ma_table[i].me_value;
751 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000752}
753
754/* Methods */
755
756static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000757dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000758{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000759 register dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000760 int fill = mp->ma_fill;
Guido van Rossumd724b232000-03-13 16:01:29 +0000761 Py_TRASHCAN_SAFE_BEGIN(mp)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000762 PyObject_GC_Fini(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000763 for (ep = mp->ma_table; fill > 0; ep++) {
764 if (ep->me_key) {
765 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000766 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000767 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000768 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000769 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000770 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000771 PyMem_DEL(mp->ma_table);
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000772 mp = (dictobject *) PyObject_AS_GC(mp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000773 PyObject_DEL(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000774 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000775}
776
777static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000778dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000779{
780 register int i;
781 register int any;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000782 register dictentry *ep;
Guido van Rossum255443b1998-04-10 22:47:14 +0000783
784 i = Py_ReprEnter((PyObject*)mp);
785 if (i != 0) {
786 if (i < 0)
787 return i;
788 fprintf(fp, "{...}");
789 return 0;
790 }
791
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000792 fprintf(fp, "{");
793 any = 0;
794 for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) {
795 if (ep->me_value != NULL) {
796 if (any++ > 0)
797 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000798 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
799 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000800 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000801 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000802 fprintf(fp, ": ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000803 if (PyObject_Print(ep->me_value, fp, 0) != 0) {
804 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000805 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000806 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000807 }
808 }
809 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000810 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000811 return 0;
812}
813
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000814static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000815dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000816{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000817 auto PyObject *v;
818 PyObject *sepa, *colon;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000819 register int i;
820 register int any;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000821 register dictentry *ep;
Guido van Rossum255443b1998-04-10 22:47:14 +0000822
823 i = Py_ReprEnter((PyObject*)mp);
824 if (i != 0) {
825 if (i > 0)
826 return PyString_FromString("{...}");
827 return NULL;
828 }
829
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000830 v = PyString_FromString("{");
831 sepa = PyString_FromString(", ");
832 colon = PyString_FromString(": ");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000833 any = 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000834 for (i = 0, ep = mp->ma_table; i < mp->ma_size && v; i++, ep++) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000835 if (ep->me_value != NULL) {
836 if (any++)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000837 PyString_Concat(&v, sepa);
838 PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_key));
839 PyString_Concat(&v, colon);
840 PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_value));
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000841 }
842 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000843 PyString_ConcatAndDel(&v, PyString_FromString("}"));
Guido van Rossum255443b1998-04-10 22:47:14 +0000844 Py_ReprLeave((PyObject*)mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000845 Py_XDECREF(sepa);
846 Py_XDECREF(colon);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000847 return v;
848}
849
850static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000851dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000852{
853 return mp->ma_used;
854}
855
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000856static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000857dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000858{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000859 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000860 long hash;
Tim Petersdea48ec2001-05-22 20:40:22 +0000861 assert(mp->ma_table != NULL);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000862#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000863 if (!PyString_Check(key) ||
864 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000865#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000866 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000867 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000868 if (hash == -1)
869 return NULL;
870 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000871 v = (mp->ma_lookup)(mp, key, hash) -> me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000872 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000873 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000874 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000875 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000876 return v;
877}
878
879static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000880dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000881{
882 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000883 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000884 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000885 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000886}
887
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000888static PyMappingMethods dict_as_mapping = {
889 (inquiry)dict_length, /*mp_length*/
890 (binaryfunc)dict_subscript, /*mp_subscript*/
891 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000892};
893
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000894static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000895dict_keys(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000896{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000897 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000898 register int i, j, n;
899
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000900 if (!PyArg_NoArgs(args))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000901 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000902 again:
903 n = mp->ma_used;
904 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000905 if (v == NULL)
906 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000907 if (n != mp->ma_used) {
908 /* Durnit. The allocations caused the dict to resize.
909 * Just start over, this shouldn't normally happen.
910 */
911 Py_DECREF(v);
912 goto again;
913 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000914 for (i = 0, j = 0; i < mp->ma_size; i++) {
915 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000916 PyObject *key = mp->ma_table[i].me_key;
917 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000918 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000919 j++;
920 }
921 }
922 return v;
923}
924
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000925static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000926dict_values(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000927{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000928 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000929 register int i, j, n;
930
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000931 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000932 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000933 again:
934 n = mp->ma_used;
935 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000936 if (v == NULL)
937 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000938 if (n != mp->ma_used) {
939 /* Durnit. The allocations caused the dict to resize.
940 * Just start over, this shouldn't normally happen.
941 */
942 Py_DECREF(v);
943 goto again;
944 }
Guido van Rossum25831651993-05-19 14:50:45 +0000945 for (i = 0, j = 0; i < mp->ma_size; i++) {
946 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000947 PyObject *value = mp->ma_table[i].me_value;
948 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000949 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000950 j++;
951 }
952 }
953 return v;
954}
955
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000956static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000957dict_items(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000958{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000959 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000960 register int i, j, n;
961 PyObject *item, *key, *value;
962
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000963 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000964 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000965 /* Preallocate the list of tuples, to avoid allocations during
966 * the loop over the items, which could trigger GC, which
967 * could resize the dict. :-(
968 */
969 again:
970 n = mp->ma_used;
971 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000972 if (v == NULL)
973 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000974 for (i = 0; i < n; i++) {
975 item = PyTuple_New(2);
976 if (item == NULL) {
977 Py_DECREF(v);
978 return NULL;
979 }
980 PyList_SET_ITEM(v, i, item);
981 }
982 if (n != mp->ma_used) {
983 /* Durnit. The allocations caused the dict to resize.
984 * Just start over, this shouldn't normally happen.
985 */
986 Py_DECREF(v);
987 goto again;
988 }
989 /* Nothing we do below makes any function calls. */
Guido van Rossum25831651993-05-19 14:50:45 +0000990 for (i = 0, j = 0; i < mp->ma_size; i++) {
991 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000992 key = mp->ma_table[i].me_key;
993 value = mp->ma_table[i].me_value;
994 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000995 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000996 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000997 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000998 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000999 j++;
1000 }
1001 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001002 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001003 return v;
1004}
1005
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001006static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001007dict_update(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001008{
1009 register int i;
1010 dictobject *other;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001011 dictentry *entry;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001012 if (!PyArg_Parse(args, "O!", &PyDict_Type, &other))
1013 return NULL;
Jeremy Hylton1fb60882001-01-03 22:34:59 +00001014 if (other == mp || other->ma_used == 0)
1015 goto done; /* a.update(a) or a.update({}); nothing to do */
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001016 /* Do one big resize at the start, rather than incrementally
1017 resizing as we insert new items. Expect that there will be
1018 no (or few) overlapping keys. */
1019 if ((mp->ma_fill + other->ma_used)*3 >= mp->ma_size*2) {
1020 if (dictresize(mp, (mp->ma_used + other->ma_used)*3/2) != 0)
1021 return NULL;
1022 }
1023 for (i = 0; i < other->ma_size; i++) {
1024 entry = &other->ma_table[i];
1025 if (entry->me_value != NULL) {
1026 Py_INCREF(entry->me_key);
1027 Py_INCREF(entry->me_value);
1028 insertdict(mp, entry->me_key, entry->me_hash,
1029 entry->me_value);
1030 }
1031 }
1032 done:
1033 Py_INCREF(Py_None);
1034 return Py_None;
1035}
1036
1037static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001038dict_copy(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001039{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001040 if (!PyArg_Parse(args, ""))
1041 return NULL;
1042 return PyDict_Copy((PyObject*)mp);
1043}
1044
1045PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001046PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001047{
1048 register dictobject *mp;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001049 register int i;
1050 dictobject *copy;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001051 dictentry *entry;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001052
1053 if (o == NULL || !PyDict_Check(o)) {
1054 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001055 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001056 }
1057 mp = (dictobject *)o;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001058 copy = (dictobject *)PyDict_New();
1059 if (copy == NULL)
1060 return NULL;
1061 if (mp->ma_used > 0) {
1062 if (dictresize(copy, mp->ma_used*3/2) != 0)
1063 return NULL;
1064 for (i = 0; i < mp->ma_size; i++) {
1065 entry = &mp->ma_table[i];
1066 if (entry->me_value != NULL) {
1067 Py_INCREF(entry->me_key);
1068 Py_INCREF(entry->me_value);
1069 insertdict(copy, entry->me_key, entry->me_hash,
1070 entry->me_value);
1071 }
1072 }
1073 }
1074 return (PyObject *)copy;
1075}
1076
Guido van Rossum4199fac1993-11-05 10:18:44 +00001077int
Tim Peters1f5871e2000-07-04 17:44:48 +00001078PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001079{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001080 if (mp == NULL || !PyDict_Check(mp)) {
1081 PyErr_BadInternalCall();
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001082 return 0;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001083 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001084 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001085}
1086
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001087PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001088PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001089{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001090 if (mp == NULL || !PyDict_Check(mp)) {
1091 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001092 return NULL;
1093 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001094 return dict_keys((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001095}
1096
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001097PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001098PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001099{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001100 if (mp == NULL || !PyDict_Check(mp)) {
1101 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001102 return NULL;
1103 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001104 return dict_values((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +00001105}
1106
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001107PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001108PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001109{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001110 if (mp == NULL || !PyDict_Check(mp)) {
1111 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001112 return NULL;
1113 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001114 return dict_items((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +00001115}
1116
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001117/* Subroutine which returns the smallest key in a for which b's value
1118 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001119 pval argument. Both are NULL if no key in a is found for which b's status
1120 differs. The refcounts on (and only on) non-NULL *pval and function return
1121 values must be decremented by the caller (characterize() increments them
1122 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1123 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001124
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001125static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001126characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001127{
Tim Peters95bf9392001-05-10 08:32:44 +00001128 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1129 PyObject *aval = NULL; /* a[akey] */
Guido van Rossumb9324202001-01-18 00:39:02 +00001130 int i, cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001131
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001132 for (i = 0; i < a->ma_size; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001133 PyObject *thiskey, *thisaval, *thisbval;
1134 if (a->ma_table[i].me_value == NULL)
1135 continue;
1136 thiskey = a->ma_table[i].me_key;
1137 Py_INCREF(thiskey); /* keep alive across compares */
1138 if (akey != NULL) {
1139 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1140 if (cmp < 0) {
1141 Py_DECREF(thiskey);
1142 goto Fail;
1143 }
1144 if (cmp > 0 ||
1145 i >= a->ma_size ||
1146 a->ma_table[i].me_value == NULL)
1147 {
1148 /* Not the *smallest* a key; or maybe it is
1149 * but the compare shrunk the dict so we can't
1150 * find its associated value anymore; or
1151 * maybe it is but the compare deleted the
1152 * a[thiskey] entry.
1153 */
1154 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001155 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001156 }
Tim Peters95bf9392001-05-10 08:32:44 +00001157 }
1158
1159 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1160 thisaval = a->ma_table[i].me_value;
1161 assert(thisaval);
1162 Py_INCREF(thisaval); /* keep alive */
1163 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1164 if (thisbval == NULL)
1165 cmp = 0;
1166 else {
1167 /* both dicts have thiskey: same values? */
1168 cmp = PyObject_RichCompareBool(
1169 thisaval, thisbval, Py_EQ);
1170 if (cmp < 0) {
1171 Py_DECREF(thiskey);
1172 Py_DECREF(thisaval);
1173 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001174 }
1175 }
Tim Peters95bf9392001-05-10 08:32:44 +00001176 if (cmp == 0) {
1177 /* New winner. */
1178 Py_XDECREF(akey);
1179 Py_XDECREF(aval);
1180 akey = thiskey;
1181 aval = thisaval;
1182 }
1183 else {
1184 Py_DECREF(thiskey);
1185 Py_DECREF(thisaval);
1186 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001187 }
Tim Peters95bf9392001-05-10 08:32:44 +00001188 *pval = aval;
1189 return akey;
1190
1191Fail:
1192 Py_XDECREF(akey);
1193 Py_XDECREF(aval);
1194 *pval = NULL;
1195 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001196}
1197
1198static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001199dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001200{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001201 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001202 int res;
1203
1204 /* Compare lengths first */
1205 if (a->ma_used < b->ma_used)
1206 return -1; /* a is shorter */
1207 else if (a->ma_used > b->ma_used)
1208 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001209
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001210 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001211 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001212 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001213 if (adiff == NULL) {
1214 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001215 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001216 * must be equal.
1217 */
1218 res = PyErr_Occurred() ? -1 : 0;
1219 goto Finished;
1220 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001221 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001222 if (bdiff == NULL && PyErr_Occurred()) {
1223 assert(!bval);
1224 res = -1;
1225 goto Finished;
1226 }
1227 res = 0;
1228 if (bdiff) {
1229 /* bdiff == NULL "should be" impossible now, but perhaps
1230 * the last comparison done by the characterize() on a had
1231 * the side effect of making the dicts equal!
1232 */
1233 res = PyObject_Compare(adiff, bdiff);
1234 }
1235 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001236 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001237
1238Finished:
1239 Py_XDECREF(adiff);
1240 Py_XDECREF(bdiff);
1241 Py_XDECREF(aval);
1242 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001243 return res;
1244}
1245
Tim Peterse63415e2001-05-08 04:38:29 +00001246/* Return 1 if dicts equal, 0 if not, -1 if error.
1247 * Gets out as soon as any difference is detected.
1248 * Uses only Py_EQ comparison.
1249 */
1250static int
1251dict_equal(dictobject *a, dictobject *b)
1252{
1253 int i;
1254
1255 if (a->ma_used != b->ma_used)
1256 /* can't be equal if # of entries differ */
1257 return 0;
1258
1259 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1260 for (i = 0; i < a->ma_size; i++) {
1261 PyObject *aval = a->ma_table[i].me_value;
1262 if (aval != NULL) {
1263 int cmp;
1264 PyObject *bval;
1265 PyObject *key = a->ma_table[i].me_key;
1266 /* temporarily bump aval's refcount to ensure it stays
1267 alive until we're done with it */
1268 Py_INCREF(aval);
1269 bval = PyDict_GetItem((PyObject *)b, key);
1270 if (bval == NULL) {
1271 Py_DECREF(aval);
1272 return 0;
1273 }
1274 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1275 Py_DECREF(aval);
1276 if (cmp <= 0) /* error or not equal */
1277 return cmp;
1278 }
1279 }
1280 return 1;
1281 }
1282
1283static PyObject *
1284dict_richcompare(PyObject *v, PyObject *w, int op)
1285{
1286 int cmp;
1287 PyObject *res;
1288
1289 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1290 res = Py_NotImplemented;
1291 }
1292 else if (op == Py_EQ || op == Py_NE) {
1293 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1294 if (cmp < 0)
1295 return NULL;
1296 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1297 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001298 else
1299 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001300 Py_INCREF(res);
1301 return res;
1302 }
1303
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001304static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001305dict_has_key(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001306{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001307 PyObject *key;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001308 long hash;
1309 register long ok;
Fred Drake52fccfd2000-02-23 15:47:16 +00001310 if (!PyArg_ParseTuple(args, "O:has_key", &key))
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001311 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001312#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001313 if (!PyString_Check(key) ||
1314 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001315#endif
Guido van Rossumca756f21997-01-23 19:39:29 +00001316 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001317 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001318 if (hash == -1)
1319 return NULL;
1320 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001321 ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001322 return PyInt_FromLong(ok);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001323}
1324
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001325static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001326dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001327{
1328 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001329 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001330 PyObject *val = NULL;
1331 long hash;
1332
Fred Drake52fccfd2000-02-23 15:47:16 +00001333 if (!PyArg_ParseTuple(args, "O|O:get", &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001334 return NULL;
1335
Barry Warsawc38c5da1997-10-06 17:49:20 +00001336#ifdef CACHE_HASH
1337 if (!PyString_Check(key) ||
1338 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1339#endif
1340 {
1341 hash = PyObject_Hash(key);
1342 if (hash == -1)
1343 return NULL;
1344 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001345 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Barry Warsaw320ac331997-10-20 17:26:25 +00001346
Barry Warsawc38c5da1997-10-06 17:49:20 +00001347 if (val == NULL)
1348 val = failobj;
1349 Py_INCREF(val);
1350 return val;
1351}
1352
1353
1354static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001355dict_setdefault(register dictobject *mp, PyObject *args)
1356{
1357 PyObject *key;
1358 PyObject *failobj = Py_None;
1359 PyObject *val = NULL;
1360 long hash;
1361
1362 if (!PyArg_ParseTuple(args, "O|O:setdefault", &key, &failobj))
1363 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001364
1365#ifdef CACHE_HASH
1366 if (!PyString_Check(key) ||
1367 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1368#endif
1369 {
1370 hash = PyObject_Hash(key);
1371 if (hash == -1)
1372 return NULL;
1373 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001374 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001375 if (val == NULL) {
1376 val = failobj;
1377 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1378 val = NULL;
1379 }
1380 Py_XINCREF(val);
1381 return val;
1382}
1383
1384
1385static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001386dict_clear(register dictobject *mp, PyObject *args)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001387{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001388 if (!PyArg_NoArgs(args))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001389 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001390 PyDict_Clear((PyObject *)mp);
1391 Py_INCREF(Py_None);
1392 return Py_None;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001393}
1394
Guido van Rossumba6ab842000-12-12 22:02:18 +00001395static PyObject *
1396dict_popitem(dictobject *mp, PyObject *args)
1397{
1398 int i = 0;
1399 dictentry *ep;
1400 PyObject *res;
1401
1402 if (!PyArg_NoArgs(args))
1403 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001404 /* Allocate the result tuple first. Believe it or not,
1405 * this allocation could trigger a garbage collection which
1406 * could resize the dict, which would invalidate the pointer
1407 * (ep) into the dict calculated below.
1408 * So we have to do this first.
1409 */
1410 res = PyTuple_New(2);
1411 if (res == NULL)
1412 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001413 if (mp->ma_used == 0) {
1414 Py_DECREF(res);
1415 PyErr_SetString(PyExc_KeyError,
1416 "popitem(): dictionary is empty");
1417 return NULL;
1418 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001419 /* Set ep to "the first" dict entry with a value. We abuse the hash
1420 * field of slot 0 to hold a search finger:
1421 * If slot 0 has a value, use slot 0.
1422 * Else slot 0 is being used to hold a search finger,
1423 * and we use its hash value as the first index to look.
1424 */
1425 ep = &mp->ma_table[0];
1426 if (ep->me_value == NULL) {
1427 i = (int)ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001428 /* The hash field may be a real hash value, or it may be a
1429 * legit search finger, or it may be a once-legit search
1430 * finger that's out of bounds now because it wrapped around
1431 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001432 */
1433 if (i >= mp->ma_size || i < 1)
1434 i = 1; /* skip slot 0 */
1435 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1436 i++;
1437 if (i >= mp->ma_size)
1438 i = 1;
1439 }
1440 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001441 PyTuple_SET_ITEM(res, 0, ep->me_key);
1442 PyTuple_SET_ITEM(res, 1, ep->me_value);
1443 Py_INCREF(dummy);
1444 ep->me_key = dummy;
1445 ep->me_value = NULL;
1446 mp->ma_used--;
1447 assert(mp->ma_table[0].me_value == NULL);
1448 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001449 return res;
1450}
1451
Jeremy Hylton8caad492000-06-23 14:18:11 +00001452static int
1453dict_traverse(PyObject *op, visitproc visit, void *arg)
1454{
1455 int i = 0, err;
1456 PyObject *pk;
1457 PyObject *pv;
1458
1459 while (PyDict_Next(op, &i, &pk, &pv)) {
1460 err = visit(pk, arg);
1461 if (err)
1462 return err;
1463 err = visit(pv, arg);
1464 if (err)
1465 return err;
1466 }
1467 return 0;
1468}
1469
1470static int
1471dict_tp_clear(PyObject *op)
1472{
1473 PyDict_Clear(op);
1474 return 0;
1475}
1476
Tim Petersf7f88b12000-12-13 23:18:45 +00001477
Guido van Rossum09e563a2001-05-01 12:10:21 +00001478staticforward PyObject *dictiter_new(dictobject *, binaryfunc);
1479
1480static PyObject *
1481select_key(PyObject *key, PyObject *value)
1482{
1483 Py_INCREF(key);
1484 return key;
1485}
1486
1487static PyObject *
1488select_value(PyObject *key, PyObject *value)
1489{
1490 Py_INCREF(value);
1491 return value;
1492}
1493
1494static PyObject *
1495select_item(PyObject *key, PyObject *value)
1496{
1497 PyObject *res = PyTuple_New(2);
1498
1499 if (res != NULL) {
1500 Py_INCREF(key);
1501 Py_INCREF(value);
1502 PyTuple_SET_ITEM(res, 0, key);
1503 PyTuple_SET_ITEM(res, 1, value);
1504 }
1505 return res;
1506}
1507
1508static PyObject *
1509dict_iterkeys(dictobject *dict, PyObject *args)
1510{
1511 if (!PyArg_ParseTuple(args, ""))
1512 return NULL;
1513 return dictiter_new(dict, select_key);
1514}
1515
1516static PyObject *
1517dict_itervalues(dictobject *dict, PyObject *args)
1518{
1519 if (!PyArg_ParseTuple(args, ""))
1520 return NULL;
1521 return dictiter_new(dict, select_value);
1522}
1523
1524static PyObject *
1525dict_iteritems(dictobject *dict, PyObject *args)
1526{
1527 if (!PyArg_ParseTuple(args, ""))
1528 return NULL;
1529 return dictiter_new(dict, select_item);
1530}
1531
1532
Tim Petersf7f88b12000-12-13 23:18:45 +00001533static char has_key__doc__[] =
1534"D.has_key(k) -> 1 if D has a key k, else 0";
1535
1536static char get__doc__[] =
1537"D.get(k[,d]) -> D[k] if D.has_key(k), else d. d defaults to None.";
1538
1539static char setdefault_doc__[] =
1540"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if not D.has_key(k)";
1541
1542static char popitem__doc__[] =
1543"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
15442-tuple; but raise KeyError if D is empty";
1545
1546static char keys__doc__[] =
1547"D.keys() -> list of D's keys";
1548
1549static char items__doc__[] =
1550"D.items() -> list of D's (key, value) pairs, as 2-tuples";
1551
1552static char values__doc__[] =
1553"D.values() -> list of D's values";
1554
1555static char update__doc__[] =
1556"D.update(E) -> None. Update D from E: for k in E.keys(): D[k] = E[k]";
1557
1558static char clear__doc__[] =
1559"D.clear() -> None. Remove all items from D.";
1560
1561static char copy__doc__[] =
1562"D.copy() -> a shallow copy of D";
1563
Guido van Rossum09e563a2001-05-01 12:10:21 +00001564static char iterkeys__doc__[] =
1565"D.iterkeys() -> an iterator over the keys of D";
1566
1567static char itervalues__doc__[] =
1568"D.itervalues() -> an iterator over the values of D";
1569
1570static char iteritems__doc__[] =
1571"D.iteritems() -> an iterator over the (key, value) items of D";
1572
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001573static PyMethodDef mapp_methods[] = {
Tim Petersf7f88b12000-12-13 23:18:45 +00001574 {"has_key", (PyCFunction)dict_has_key, METH_VARARGS,
1575 has_key__doc__},
1576 {"get", (PyCFunction)dict_get, METH_VARARGS,
1577 get__doc__},
1578 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1579 setdefault_doc__},
1580 {"popitem", (PyCFunction)dict_popitem, METH_OLDARGS,
1581 popitem__doc__},
1582 {"keys", (PyCFunction)dict_keys, METH_OLDARGS,
1583 keys__doc__},
1584 {"items", (PyCFunction)dict_items, METH_OLDARGS,
1585 items__doc__},
1586 {"values", (PyCFunction)dict_values, METH_OLDARGS,
1587 values__doc__},
1588 {"update", (PyCFunction)dict_update, METH_OLDARGS,
1589 update__doc__},
1590 {"clear", (PyCFunction)dict_clear, METH_OLDARGS,
1591 clear__doc__},
1592 {"copy", (PyCFunction)dict_copy, METH_OLDARGS,
1593 copy__doc__},
Guido van Rossum09e563a2001-05-01 12:10:21 +00001594 {"iterkeys", (PyCFunction)dict_iterkeys, METH_VARARGS,
1595 iterkeys__doc__},
1596 {"itervalues", (PyCFunction)dict_itervalues, METH_VARARGS,
1597 itervalues__doc__},
1598 {"iteritems", (PyCFunction)dict_iteritems, METH_VARARGS,
1599 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001600 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001601};
1602
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001603static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001604dict_getattr(dictobject *mp, char *name)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001605{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001606 return Py_FindMethod(mapp_methods, (PyObject *)mp, name);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001607}
1608
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001609static int
1610dict_contains(dictobject *mp, PyObject *key)
1611{
1612 long hash;
1613
1614#ifdef CACHE_HASH
1615 if (!PyString_Check(key) ||
1616 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1617#endif
1618 {
1619 hash = PyObject_Hash(key);
1620 if (hash == -1)
1621 return -1;
1622 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001623 return (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001624}
1625
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001626/* Hack to implement "key in dict" */
1627static PySequenceMethods dict_as_sequence = {
1628 0, /* sq_length */
1629 0, /* sq_concat */
1630 0, /* sq_repeat */
1631 0, /* sq_item */
1632 0, /* sq_slice */
1633 0, /* sq_ass_item */
1634 0, /* sq_ass_slice */
1635 (objobjproc)dict_contains, /* sq_contains */
1636 0, /* sq_inplace_concat */
1637 0, /* sq_inplace_repeat */
1638};
1639
Guido van Rossum09e563a2001-05-01 12:10:21 +00001640static PyObject *
1641dict_iter(dictobject *dict)
1642{
1643 return dictiter_new(dict, select_key);
1644}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001645
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001646PyTypeObject PyDict_Type = {
1647 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001648 0,
Guido van Rossum9bfef441993-03-29 10:43:31 +00001649 "dictionary",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001650 sizeof(dictobject) + PyGC_HEAD_SIZE,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001651 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001652 (destructor)dict_dealloc, /* tp_dealloc */
1653 (printfunc)dict_print, /* tp_print */
1654 (getattrfunc)dict_getattr, /* tp_getattr */
1655 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00001656 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001657 (reprfunc)dict_repr, /* tp_repr */
1658 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001659 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001660 &dict_as_mapping, /* tp_as_mapping */
1661 0, /* tp_hash */
1662 0, /* tp_call */
1663 0, /* tp_str */
1664 0, /* tp_getattro */
1665 0, /* tp_setattro */
1666 0, /* tp_as_buffer */
1667 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
1668 0, /* tp_doc */
1669 (traverseproc)dict_traverse, /* tp_traverse */
1670 (inquiry)dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001671 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001672 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001673 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001674 0, /* tp_iternext */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001675};
1676
Guido van Rossum3cca2451997-05-16 14:23:33 +00001677/* For backward compatibility with old dictionary interface */
1678
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001679PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001680PyDict_GetItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001681{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001682 PyObject *kv, *rv;
1683 kv = PyString_FromString(key);
1684 if (kv == NULL)
1685 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001686 rv = PyDict_GetItem(v, kv);
1687 Py_DECREF(kv);
1688 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001689}
1690
1691int
Tim Peters1f5871e2000-07-04 17:44:48 +00001692PyDict_SetItemString(PyObject *v, char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001693{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001694 PyObject *kv;
1695 int err;
1696 kv = PyString_FromString(key);
1697 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001698 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00001699 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00001700 err = PyDict_SetItem(v, kv, item);
1701 Py_DECREF(kv);
1702 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001703}
1704
1705int
Tim Peters1f5871e2000-07-04 17:44:48 +00001706PyDict_DelItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001707{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001708 PyObject *kv;
1709 int err;
1710 kv = PyString_FromString(key);
1711 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001712 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001713 err = PyDict_DelItem(v, kv);
1714 Py_DECREF(kv);
1715 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001716}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001717
1718/* Dictionary iterator type */
1719
1720extern PyTypeObject PyDictIter_Type; /* Forward */
1721
1722typedef struct {
1723 PyObject_HEAD
1724 dictobject *di_dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001725 int di_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001726 int di_pos;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001727 binaryfunc di_select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001728} dictiterobject;
1729
1730static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001731dictiter_new(dictobject *dict, binaryfunc select)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001732{
1733 dictiterobject *di;
1734 di = PyObject_NEW(dictiterobject, &PyDictIter_Type);
1735 if (di == NULL)
1736 return NULL;
1737 Py_INCREF(dict);
1738 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001739 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001740 di->di_pos = 0;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001741 di->di_select = select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001742 return (PyObject *)di;
1743}
1744
1745static void
1746dictiter_dealloc(dictiterobject *di)
1747{
1748 Py_DECREF(di->di_dict);
1749 PyObject_DEL(di);
1750}
1751
1752static PyObject *
1753dictiter_next(dictiterobject *di, PyObject *args)
1754{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001755 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001756
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001757 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001758 PyErr_SetString(PyExc_RuntimeError,
1759 "dictionary changed size during iteration");
1760 return NULL;
1761 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001762 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1763 return (*di->di_select)(key, value);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001764 }
1765 PyErr_SetObject(PyExc_StopIteration, Py_None);
1766 return NULL;
1767}
1768
1769static PyObject *
1770dictiter_getiter(PyObject *it)
1771{
1772 Py_INCREF(it);
1773 return it;
1774}
1775
1776static PyMethodDef dictiter_methods[] = {
1777 {"next", (PyCFunction)dictiter_next, METH_VARARGS,
1778 "it.next() -- get the next value, or raise StopIteration"},
1779 {NULL, NULL} /* sentinel */
1780};
1781
1782static PyObject *
Guido van Rossum213c7a62001-04-23 14:08:49 +00001783dictiter_getattr(dictiterobject *di, char *name)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001784{
Guido van Rossum213c7a62001-04-23 14:08:49 +00001785 return Py_FindMethod(dictiter_methods, (PyObject *)di, name);
1786}
1787
1788static PyObject *dictiter_iternext(dictiterobject *di)
1789{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001790 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001791
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001792 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00001793 PyErr_SetString(PyExc_RuntimeError,
1794 "dictionary changed size during iteration");
1795 return NULL;
1796 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001797 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1798 return (*di->di_select)(key, value);
Guido van Rossum213c7a62001-04-23 14:08:49 +00001799 }
1800 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001801}
1802
1803PyTypeObject PyDictIter_Type = {
1804 PyObject_HEAD_INIT(&PyType_Type)
1805 0, /* ob_size */
1806 "dictionary-iterator", /* tp_name */
1807 sizeof(dictiterobject), /* tp_basicsize */
1808 0, /* tp_itemsize */
1809 /* methods */
1810 (destructor)dictiter_dealloc, /* tp_dealloc */
1811 0, /* tp_print */
1812 (getattrfunc)dictiter_getattr, /* tp_getattr */
1813 0, /* tp_setattr */
1814 0, /* tp_compare */
1815 0, /* tp_repr */
1816 0, /* tp_as_number */
1817 0, /* tp_as_sequence */
1818 0, /* tp_as_mapping */
1819 0, /* tp_hash */
1820 0, /* tp_call */
1821 0, /* tp_str */
1822 0, /* tp_getattro */
1823 0, /* tp_setattro */
1824 0, /* tp_as_buffer */
1825 Py_TPFLAGS_DEFAULT, /* tp_flags */
1826 0, /* tp_doc */
1827 0, /* tp_traverse */
1828 0, /* tp_clear */
1829 0, /* tp_richcompare */
1830 0, /* tp_weaklistoffset */
1831 (getiterfunc)dictiter_getiter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001832 (iternextfunc)dictiter_iternext, /* tp_iternext */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001833};