blob: 22c413510911ad5044882c9e8ed1da85b3b23c06 [file] [log] [blame]
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum4b1302b1993-03-27 18:11:32 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum4b1302b1993-03-27 18:11:32 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum4b1302b1993-03-27 18:11:32 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum4b1302b1993-03-27 18:11:32 +000029
30******************************************************************/
31
Guido van Rossum2bc13791999-03-24 19:06:42 +000032/* Dictionary object implementation using a hash table */
Guido van Rossum9bfef441993-03-29 10:43:31 +000033
Guido van Rossumc0b618a1997-05-02 03:12:38 +000034#include "Python.h"
Guido van Rossum4b1302b1993-03-27 18:11:32 +000035
36
37/*
Guido van Rossuma9e7a811997-05-13 21:02:11 +000038 * MINSIZE is the minimum size of a dictionary.
Guido van Rossum16e93a81997-01-28 00:00:11 +000039 */
40
41#define MINSIZE 4
42
43/*
44Table of irreducible polynomials to efficiently cycle through
45GF(2^n)-{0}, 2<=n<=30.
Guido van Rossum4b1302b1993-03-27 18:11:32 +000046*/
Guido van Rossum16e93a81997-01-28 00:00:11 +000047static long polys[] = {
Guido van Rossum9e5656c1997-01-29 04:45:16 +000048 4 + 3,
49 8 + 3,
50 16 + 3,
51 32 + 5,
52 64 + 3,
53 128 + 3,
54 256 + 29,
55 512 + 17,
56 1024 + 9,
57 2048 + 5,
58 4096 + 83,
59 8192 + 27,
60 16384 + 43,
61 32768 + 3,
62 65536 + 45,
63 131072 + 9,
64 262144 + 39,
65 524288 + 39,
66 1048576 + 9,
67 2097152 + 5,
68 4194304 + 3,
69 8388608 + 33,
70 16777216 + 27,
71 33554432 + 9,
72 67108864 + 71,
73 134217728 + 39,
74 268435456 + 9,
75 536870912 + 5,
76 1073741824 + 83,
77 0
Guido van Rossum4b1302b1993-03-27 18:11:32 +000078};
79
80/* Object used as dummy key to fill deleted entries */
Guido van Rossuma9e7a811997-05-13 21:02:11 +000081static PyObject *dummy; /* Initialized by first call to newdictobject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +000082
83/*
Guido van Rossum2bc13791999-03-24 19:06:42 +000084Invariant for entries: when in use, me_value is not NULL and me_key is
85not NULL and not dummy; when not in use, me_value is NULL and me_key
Guido van Rossum4b1302b1993-03-27 18:11:32 +000086is either NULL or dummy. A dummy key value cannot be replaced by
87NULL, since otherwise other keys may be lost.
88*/
89typedef struct {
90 long me_hash;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091 PyObject *me_key;
92 PyObject *me_value;
Guido van Rossum36488841997-04-11 19:14:07 +000093#ifdef USE_CACHE_ALIGNED
94 long aligner;
95#endif
Guido van Rossuma9e7a811997-05-13 21:02:11 +000096} dictentry;
Guido van Rossum4b1302b1993-03-27 18:11:32 +000097
98/*
99To ensure the lookup algorithm terminates, the table size must be a
100prime number and there must be at least one NULL key in the table.
101The value ma_fill is the number of non-NULL keys; ma_used is the number
102of non-NULL, non-dummy keys.
103To avoid slowing down lookups on a near-full table, we resize the table
104when it is more than half filled.
105*/
106typedef struct {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000107 PyObject_HEAD
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000108 int ma_fill;
109 int ma_used;
110 int ma_size;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000111 int ma_poly;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000112 dictentry *ma_table;
113} dictobject;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000114
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000115PyObject *
116PyDict_New()
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000117{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000118 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000119 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000120 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000121 if (dummy == NULL)
122 return NULL;
123 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000124 mp = PyObject_NEW(dictobject, &PyDict_Type);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000125 if (mp == NULL)
126 return NULL;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000127 mp->ma_size = 0;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000128 mp->ma_poly = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000129 mp->ma_table = NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000130 mp->ma_fill = 0;
131 mp->ma_used = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000132 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000133}
134
135/*
136The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000137This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000138Open addressing is preferred over chaining since the link overhead for
139chaining would be substantial (100% with typical malloc overhead).
Guido van Rossum16e93a81997-01-28 00:00:11 +0000140However, instead of going through the table at constant steps, we cycle
141through the values of GF(2^n)-{0}. This avoids modulo computations, being
142much cheaper on RISC machines, without leading to clustering.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000143
Guido van Rossum2bc13791999-03-24 19:06:42 +0000144The initial probe index is computed as hash mod the table size.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000145Subsequent probe indices use the values of x^i in GF(2^n) as an offset,
Guido van Rossum2bc13791999-03-24 19:06:42 +0000146where x is a root. The initial value is derived from hash, too.
147
148All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000149
150(This version is due to Reimer Behrends, some ideas are also due to
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000151Jyrki Alakuijala and Vladimir Marangozov.)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000152*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000153static dictentry *lookdict Py_PROTO((dictobject *, PyObject *, long));
154static dictentry *
155lookdict(mp, key, hash)
156 dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157 PyObject *key;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000158 register long hash;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000159{
Guido van Rossum16e93a81997-01-28 00:00:11 +0000160 register int i;
161 register unsigned incr;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000162 register dictentry *freeslot;
Guido van Rossum2095d241997-04-09 19:41:24 +0000163 register unsigned int mask = mp->ma_size-1;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000164 dictentry *ep0 = mp->ma_table;
165 register dictentry *ep;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000166 /* We must come up with (i, incr) such that 0 <= i < ma_size
167 and 0 < incr < ma_size and both are a function of hash */
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000168 i = (~hash) & mask;
169 /* We use ~hash instead of hash, as degenerate hash functions, such
Guido van Rossum16e93a81997-01-28 00:00:11 +0000170 as for ints <sigh>, can have lots of leading zeros. It's not
171 really a performance risk, but better safe than sorry. */
Guido van Rossumefb46091997-01-29 15:53:56 +0000172 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000173 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000174 return ep;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000175 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000176 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000177 else {
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000178 if (ep->me_hash == hash &&
179 PyObject_Compare(ep->me_key, key) == 0)
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000180 {
181 return ep;
182 }
183 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000184 }
Guido van Rossum5b2121b1997-05-23 00:01:41 +0000185 /* XXX What if PyObject_Compare returned an exception? */
Guido van Rossum2bc13791999-03-24 19:06:42 +0000186 /* Derive incr from hash, just to make it more arbitrary. Note that
Guido van Rossum16e93a81997-01-28 00:00:11 +0000187 incr must not be 0, or we will get into an infinite loop.*/
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000188 incr = (hash ^ ((unsigned long)hash >> 3)) & mask;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000189 if (!incr)
190 incr = mask;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000191 for (;;) {
Guido van Rossumefb46091997-01-29 15:53:56 +0000192 ep = &ep0[(i+incr)&mask];
Guido van Rossum16e93a81997-01-28 00:00:11 +0000193 if (ep->me_key == NULL) {
194 if (freeslot != NULL)
195 return freeslot;
196 else
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000197 return ep;
198 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000199 if (ep->me_key == dummy) {
200 if (freeslot == NULL)
201 freeslot = ep;
202 }
203 else if (ep->me_key == key ||
204 (ep->me_hash == hash &&
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000205 PyObject_Compare(ep->me_key, key) == 0)) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000206 return ep;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000207 }
Guido van Rossum5b2121b1997-05-23 00:01:41 +0000208 /* XXX What if PyObject_Compare returned an exception? */
Guido van Rossum16e93a81997-01-28 00:00:11 +0000209 /* Cycle through GF(2^n)-{0} */
210 incr = incr << 1;
211 if (incr > mask)
Guido van Rossumf05fc711998-11-16 22:46:30 +0000212 incr ^= mp->ma_poly; /* This will implicitely clear
213 the highest bit */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000214 }
215}
216
217/*
218Internal routine to insert a new item into the table.
219Used both by the internal resize routine and by the public insert routine.
220Eats a reference to key and one to value.
221*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000222static void insertdict
223 Py_PROTO((dictobject *, PyObject *, long, PyObject *));
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000224static void
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000225insertdict(mp, key, hash, value)
226 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000227 PyObject *key;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000228 long hash;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000229 PyObject *value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000230{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000231 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000232 register dictentry *ep;
233 ep = lookdict(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000234 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000235 old_value = ep->me_value;
236 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000237 Py_DECREF(old_value); /* which **CAN** re-enter */
238 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000239 }
240 else {
241 if (ep->me_key == NULL)
242 mp->ma_fill++;
243 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000244 Py_DECREF(ep->me_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000245 ep->me_key = key;
246 ep->me_hash = hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000247 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000248 mp->ma_used++;
249 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000250}
251
252/*
253Restructure the table by allocating a new table and reinserting all
254items again. When entries have been deleted, the new table may
255actually be smaller than the old one.
256*/
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000257static int dictresize Py_PROTO((dictobject *, int));
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000258static int
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000259dictresize(mp, minused)
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000260 dictobject *mp;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000261 int minused;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000262{
263 register int oldsize = mp->ma_size;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000264 register int newsize, newpoly;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000265 register dictentry *oldtable = mp->ma_table;
266 register dictentry *newtable;
267 register dictentry *ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000268 register int i;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000269 for (i = 0, newsize = MINSIZE; ; i++, newsize <<= 1) {
270 if (i > sizeof(polys)/sizeof(polys[0])) {
271 /* Ran out of polynomials */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000272 PyErr_NoMemory();
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000273 return -1;
274 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000275 if (newsize > minused) {
Guido van Rossum16e93a81997-01-28 00:00:11 +0000276 newpoly = polys[i];
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000277 break;
278 }
279 }
Guido van Rossum0fd00331998-07-16 15:06:13 +0000280 newtable = (dictentry *) malloc(sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000281 if (newtable == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000282 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000283 return -1;
284 }
Guido van Rossum0fd00331998-07-16 15:06:13 +0000285 memset(newtable, '\0', sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000286 mp->ma_size = newsize;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000287 mp->ma_poly = newpoly;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000288 mp->ma_table = newtable;
289 mp->ma_fill = 0;
290 mp->ma_used = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000291
292 /* Make two passes, so we can avoid decrefs
293 (and possible side effects) till the table is copied */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000294 for (i = 0, ep = oldtable; i < oldsize; i++, ep++) {
295 if (ep->me_value != NULL)
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000296 insertdict(mp,ep->me_key,ep->me_hash,ep->me_value);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000297 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000298 for (i = 0, ep = oldtable; i < oldsize; i++, ep++) {
Guido van Rossum255443b1998-04-10 22:47:14 +0000299 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000300 Py_XDECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000301 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000302 }
303
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000304 PyMem_XDEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000305 return 0;
306}
307
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000308PyObject *
309PyDict_GetItem(op, key)
310 PyObject *op;
311 PyObject *key;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000312{
313 long hash;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000314 if (!PyDict_Check(op)) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000315 return NULL;
316 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000317 if (((dictobject *)op)->ma_table == NULL)
Guido van Rossumd7047b31995-01-02 19:07:15 +0000318 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000319#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000320 if (!PyString_Check(key) ||
321 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000322#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000323 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000324 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000325 if (hash == -1) {
326 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000327 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000328 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000329 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000330 return lookdict((dictobject *)op, key, hash) -> me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000331}
332
333int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000334PyDict_SetItem(op, key, value)
335 register PyObject *op;
336 PyObject *key;
337 PyObject *value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000338{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000339 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000340 register long hash;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000341 if (!PyDict_Check(op)) {
342 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000343 return -1;
344 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000345 mp = (dictobject *)op;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000346#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000347 if (PyString_Check(key)) {
Guido van Rossum2a61e741997-01-18 07:55:05 +0000348#ifdef INTERN_STRINGS
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000349 if (((PyStringObject *)key)->ob_sinterned != NULL) {
350 key = ((PyStringObject *)key)->ob_sinterned;
351 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000352 }
353 else
354#endif
355 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000356 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000357 if (hash == -1)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000358 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000359 }
360 }
361 else
362#endif
363 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000364 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000365 if (hash == -1)
366 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000367 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000368 /* if fill >= 2/3 size, double in size */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000369 if (mp->ma_fill*3 >= mp->ma_size*2) {
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000370 if (dictresize(mp, mp->ma_used*2) != 0) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000371 if (mp->ma_fill+1 > mp->ma_size)
372 return -1;
373 }
374 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000375 Py_INCREF(value);
376 Py_INCREF(key);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000377 insertdict(mp, key, hash, value);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000378 return 0;
379}
380
381int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000382PyDict_DelItem(op, key)
383 PyObject *op;
384 PyObject *key;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000385{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000386 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000387 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000388 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000389 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000390
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000391 if (!PyDict_Check(op)) {
392 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000393 return -1;
394 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000395#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000396 if (!PyString_Check(key) ||
397 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000398#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000399 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000400 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000401 if (hash == -1)
402 return -1;
403 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000404 mp = (dictobject *)op;
405 if (((dictobject *)op)->ma_table == NULL)
Guido van Rossumefc87131995-01-02 19:42:39 +0000406 goto empty;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000407 ep = lookdict(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000408 if (ep->me_value == NULL) {
Guido van Rossumefc87131995-01-02 19:42:39 +0000409 empty:
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000410 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000411 return -1;
412 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000413 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000414 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000415 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000416 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000417 ep->me_value = NULL;
418 mp->ma_used--;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000419 Py_DECREF(old_value);
420 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000421 return 0;
422}
423
Guido van Rossum25831651993-05-19 14:50:45 +0000424void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000425PyDict_Clear(op)
426 PyObject *op;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000427{
Guido van Rossumd7047b31995-01-02 19:07:15 +0000428 int i, n;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000429 register dictentry *table;
430 dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000431 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000432 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000433 mp = (dictobject *)op;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000434 table = mp->ma_table;
435 if (table == NULL)
436 return;
437 n = mp->ma_size;
438 mp->ma_size = mp->ma_used = mp->ma_fill = 0;
439 mp->ma_table = NULL;
440 for (i = 0; i < n; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000441 Py_XDECREF(table[i].me_key);
442 Py_XDECREF(table[i].me_value);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000443 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000444 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000445}
446
Guido van Rossum25831651993-05-19 14:50:45 +0000447int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000448PyDict_Next(op, ppos, pkey, pvalue)
449 PyObject *op;
Guido van Rossum25831651993-05-19 14:50:45 +0000450 int *ppos;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000451 PyObject **pkey;
452 PyObject **pvalue;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000453{
Guido van Rossum25831651993-05-19 14:50:45 +0000454 int i;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000455 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000456 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000457 return 0;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000458 mp = (dictobject *)op;
Guido van Rossum25831651993-05-19 14:50:45 +0000459 i = *ppos;
460 if (i < 0)
461 return 0;
462 while (i < mp->ma_size && mp->ma_table[i].me_value == NULL)
463 i++;
464 *ppos = i+1;
465 if (i >= mp->ma_size)
466 return 0;
467 if (pkey)
468 *pkey = mp->ma_table[i].me_key;
469 if (pvalue)
470 *pvalue = mp->ma_table[i].me_value;
471 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000472}
473
474/* Methods */
475
476static void
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000477dict_dealloc(mp)
478 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000479{
480 register int i;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000481 register dictentry *ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000482 for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) {
Guido van Rossum255443b1998-04-10 22:47:14 +0000483 if (ep->me_key != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000484 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000485 }
486 if (ep->me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000487 Py_DECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000488 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000489 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000490 PyMem_XDEL(mp->ma_table);
491 PyMem_DEL(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000492}
493
494static int
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000495dict_print(mp, fp, flags)
496 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000497 register FILE *fp;
498 register int flags;
499{
500 register int i;
501 register int any;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000502 register dictentry *ep;
Guido van Rossum255443b1998-04-10 22:47:14 +0000503
504 i = Py_ReprEnter((PyObject*)mp);
505 if (i != 0) {
506 if (i < 0)
507 return i;
508 fprintf(fp, "{...}");
509 return 0;
510 }
511
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000512 fprintf(fp, "{");
513 any = 0;
514 for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) {
515 if (ep->me_value != NULL) {
516 if (any++ > 0)
517 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000518 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
519 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000520 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000521 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000522 fprintf(fp, ": ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000523 if (PyObject_Print(ep->me_value, fp, 0) != 0) {
524 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000525 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000526 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000527 }
528 }
529 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000530 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000531 return 0;
532}
533
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000534static PyObject *
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000535dict_repr(mp)
536 dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000537{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000538 auto PyObject *v;
539 PyObject *sepa, *colon;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000540 register int i;
541 register int any;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000542 register dictentry *ep;
Guido van Rossum255443b1998-04-10 22:47:14 +0000543
544 i = Py_ReprEnter((PyObject*)mp);
545 if (i != 0) {
546 if (i > 0)
547 return PyString_FromString("{...}");
548 return NULL;
549 }
550
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000551 v = PyString_FromString("{");
552 sepa = PyString_FromString(", ");
553 colon = PyString_FromString(": ");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000554 any = 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000555 for (i = 0, ep = mp->ma_table; i < mp->ma_size && v; i++, ep++) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000556 if (ep->me_value != NULL) {
557 if (any++)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558 PyString_Concat(&v, sepa);
559 PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_key));
560 PyString_Concat(&v, colon);
561 PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_value));
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000562 }
563 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000564 PyString_ConcatAndDel(&v, PyString_FromString("}"));
Guido van Rossum255443b1998-04-10 22:47:14 +0000565 Py_ReprLeave((PyObject*)mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000566 Py_XDECREF(sepa);
567 Py_XDECREF(colon);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000568 return v;
569}
570
571static int
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000572dict_length(mp)
573 dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000574{
575 return mp->ma_used;
576}
577
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000578static PyObject *
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000579dict_subscript(mp, key)
580 dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000581 register PyObject *key;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000582{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000583 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000584 long hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000585 if (mp->ma_table == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000586 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossumd7047b31995-01-02 19:07:15 +0000587 return NULL;
588 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000589#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000590 if (!PyString_Check(key) ||
591 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000592#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000593 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000594 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000595 if (hash == -1)
596 return NULL;
597 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000598 v = lookdict(mp, key, hash) -> me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000599 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000600 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000601 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000602 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000603 return v;
604}
605
606static int
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000607dict_ass_sub(mp, v, w)
608 dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000609 PyObject *v, *w;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000610{
611 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000612 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000613 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000614 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000615}
616
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000617static PyMappingMethods dict_as_mapping = {
618 (inquiry)dict_length, /*mp_length*/
619 (binaryfunc)dict_subscript, /*mp_subscript*/
620 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000621};
622
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000623static PyObject *
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000624dict_keys(mp, args)
625 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000626 PyObject *args;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000627{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000628 register PyObject *v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000629 register int i, j;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000630 if (!PyArg_NoArgs(args))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000631 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000632 v = PyList_New(mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000633 if (v == NULL)
634 return NULL;
635 for (i = 0, j = 0; i < mp->ma_size; i++) {
636 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000637 PyObject *key = mp->ma_table[i].me_key;
638 Py_INCREF(key);
639 PyList_SetItem(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000640 j++;
641 }
642 }
643 return v;
644}
645
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646static PyObject *
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000647dict_values(mp, args)
648 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000649 PyObject *args;
Guido van Rossum25831651993-05-19 14:50:45 +0000650{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000651 register PyObject *v;
Guido van Rossum25831651993-05-19 14:50:45 +0000652 register int i, j;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000653 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000654 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000655 v = PyList_New(mp->ma_used);
Guido van Rossum25831651993-05-19 14:50:45 +0000656 if (v == NULL)
657 return NULL;
658 for (i = 0, j = 0; i < mp->ma_size; i++) {
659 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000660 PyObject *value = mp->ma_table[i].me_value;
661 Py_INCREF(value);
662 PyList_SetItem(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000663 j++;
664 }
665 }
666 return v;
667}
668
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000669static PyObject *
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000670dict_items(mp, args)
671 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000672 PyObject *args;
Guido van Rossum25831651993-05-19 14:50:45 +0000673{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000674 register PyObject *v;
Guido van Rossum25831651993-05-19 14:50:45 +0000675 register int i, j;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000676 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000677 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000678 v = PyList_New(mp->ma_used);
Guido van Rossum25831651993-05-19 14:50:45 +0000679 if (v == NULL)
680 return NULL;
681 for (i = 0, j = 0; i < mp->ma_size; i++) {
682 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000683 PyObject *key = mp->ma_table[i].me_key;
684 PyObject *value = mp->ma_table[i].me_value;
685 PyObject *item = PyTuple_New(2);
Guido van Rossum25831651993-05-19 14:50:45 +0000686 if (item == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000687 Py_DECREF(v);
Guido van Rossum25831651993-05-19 14:50:45 +0000688 return NULL;
689 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000690 Py_INCREF(key);
691 PyTuple_SetItem(item, 0, key);
692 Py_INCREF(value);
693 PyTuple_SetItem(item, 1, value);
694 PyList_SetItem(v, j, item);
Guido van Rossum25831651993-05-19 14:50:45 +0000695 j++;
696 }
697 }
698 return v;
699}
700
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000701static PyObject *
Guido van Rossuma8d51311997-06-02 17:13:37 +0000702dict_update(mp, args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000703 register dictobject *mp;
704 PyObject *args;
705{
706 register int i;
707 dictobject *other;
708 dictentry *entry;
709 if (!PyArg_Parse(args, "O!", &PyDict_Type, &other))
710 return NULL;
711 if (other == mp)
Guido van Rossuma8d51311997-06-02 17:13:37 +0000712 goto done; /* a.update(a); nothing to do */
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000713 /* Do one big resize at the start, rather than incrementally
714 resizing as we insert new items. Expect that there will be
715 no (or few) overlapping keys. */
716 if ((mp->ma_fill + other->ma_used)*3 >= mp->ma_size*2) {
717 if (dictresize(mp, (mp->ma_used + other->ma_used)*3/2) != 0)
718 return NULL;
719 }
720 for (i = 0; i < other->ma_size; i++) {
721 entry = &other->ma_table[i];
722 if (entry->me_value != NULL) {
723 Py_INCREF(entry->me_key);
724 Py_INCREF(entry->me_value);
725 insertdict(mp, entry->me_key, entry->me_hash,
726 entry->me_value);
727 }
728 }
729 done:
730 Py_INCREF(Py_None);
731 return Py_None;
732}
733
734static PyObject *
735dict_copy(mp, args)
736 register dictobject *mp;
737 PyObject *args;
738{
739 register int i;
740 dictobject *copy;
741 dictentry *entry;
742 if (!PyArg_Parse(args, ""))
743 return NULL;
744 copy = (dictobject *)PyDict_New();
745 if (copy == NULL)
746 return NULL;
747 if (mp->ma_used > 0) {
748 if (dictresize(copy, mp->ma_used*3/2) != 0)
749 return NULL;
750 for (i = 0; i < mp->ma_size; i++) {
751 entry = &mp->ma_table[i];
752 if (entry->me_value != NULL) {
753 Py_INCREF(entry->me_key);
754 Py_INCREF(entry->me_value);
755 insertdict(copy, entry->me_key, entry->me_hash,
756 entry->me_value);
757 }
758 }
759 }
760 return (PyObject *)copy;
761}
762
Guido van Rossum4199fac1993-11-05 10:18:44 +0000763int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000764PyDict_Size(mp)
765 PyObject *mp;
Guido van Rossum4199fac1993-11-05 10:18:44 +0000766{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000767 if (mp == NULL || !PyDict_Check(mp)) {
768 PyErr_BadInternalCall();
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000769 return 0;
Guido van Rossum4199fac1993-11-05 10:18:44 +0000770 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000771 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +0000772}
773
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000774PyObject *
775PyDict_Keys(mp)
776 PyObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000777{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000778 if (mp == NULL || !PyDict_Check(mp)) {
779 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000780 return NULL;
781 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000782 return dict_keys((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000783}
784
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000785PyObject *
786PyDict_Values(mp)
787 PyObject *mp;
Guido van Rossum25831651993-05-19 14:50:45 +0000788{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000789 if (mp == NULL || !PyDict_Check(mp)) {
790 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +0000791 return NULL;
792 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000793 return dict_values((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +0000794}
795
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000796PyObject *
797PyDict_Items(mp)
798 PyObject *mp;
Guido van Rossum25831651993-05-19 14:50:45 +0000799{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000800 if (mp == NULL || !PyDict_Check(mp)) {
801 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +0000802 return NULL;
803 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000804 return dict_items((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +0000805}
806
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000807#define NEWCMP
808
809#ifdef NEWCMP
810
811/* Subroutine which returns the smallest key in a for which b's value
812 is different or absent. The value is returned too, through the
813 pval argument. No reference counts are incremented. */
814
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000815static PyObject *
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000816characterize(a, b, pval)
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000817 dictobject *a;
818 dictobject *b;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000819 PyObject **pval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000820{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000821 PyObject *diff = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000822 int i;
823
824 *pval = NULL;
825 for (i = 0; i < a->ma_size; i++) {
826 if (a->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000827 PyObject *key = a->ma_table[i].me_key;
828 PyObject *aval, *bval;
Guido van Rossum5b2121b1997-05-23 00:01:41 +0000829 /* XXX What if PyObject_Compare raises an exception? */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000830 if (diff != NULL && PyObject_Compare(key, diff) > 0)
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000831 continue;
832 aval = a->ma_table[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000833 bval = PyDict_GetItem((PyObject *)b, key);
Guido van Rossum5b2121b1997-05-23 00:01:41 +0000834 /* XXX What if PyObject_Compare raises an exception? */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000835 if (bval == NULL || PyObject_Compare(aval, bval) != 0)
836 {
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000837 diff = key;
838 *pval = aval;
839 }
840 }
841 }
842 return diff;
843}
844
845static int
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000846dict_compare(a, b)
847 dictobject *a, *b;
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000848{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000849 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000850 int res;
851
852 /* Compare lengths first */
853 if (a->ma_used < b->ma_used)
854 return -1; /* a is shorter */
855 else if (a->ma_used > b->ma_used)
856 return 1; /* b is shorter */
857 /* Same length -- check all keys */
858 adiff = characterize(a, b, &aval);
Guido van Rossum5b2121b1997-05-23 00:01:41 +0000859 if (PyErr_Occurred())
860 return -1;
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000861 if (adiff == NULL)
862 return 0; /* a is a subset with the same length */
863 bdiff = characterize(b, a, &bval);
Guido van Rossum5b2121b1997-05-23 00:01:41 +0000864 if (PyErr_Occurred())
865 return -1;
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000866 /* bdiff == NULL would be impossible now */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000867 res = PyObject_Compare(adiff, bdiff);
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000868 if (res == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000869 res = PyObject_Compare(aval, bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000870 return res;
871}
872
873#else /* !NEWCMP */
874
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000875static int
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000876dict_compare(a, b)
877 dictobject *a, *b;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000878{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000879 PyObject *akeys, *bkeys;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000880 int i, n, res;
881 if (a == b)
882 return 0;
883 if (a->ma_used == 0) {
884 if (b->ma_used != 0)
885 return -1;
886 else
887 return 0;
888 }
889 else {
890 if (b->ma_used == 0)
891 return 1;
892 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000893 akeys = dict_keys(a, (PyObject *)NULL);
894 bkeys = dict_keys(b, (PyObject *)NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000895 if (akeys == NULL || bkeys == NULL) {
896 /* Oops, out of memory -- what to do? */
897 /* For now, sort on address! */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000898 Py_XDECREF(akeys);
899 Py_XDECREF(bkeys);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000900 if (a < b)
901 return -1;
902 else
903 return 1;
904 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000905 PyList_Sort(akeys);
906 PyList_Sort(bkeys);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000907 n = a->ma_used < b->ma_used ? a->ma_used : b->ma_used; /* smallest */
908 res = 0;
909 for (i = 0; i < n; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000910 PyObject *akey, *bkey, *aval, *bval;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000911 long ahash, bhash;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000912 akey = PyList_GetItem(akeys, i);
913 bkey = PyList_GetItem(bkeys, i);
914 res = PyObject_Compare(akey, bkey);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000915 if (res != 0)
916 break;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000917#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000918 if (!PyString_Check(akey) ||
919 (ahash = ((PyStringObject *) akey)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000920#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000921 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000922 ahash = PyObject_Hash(akey);
Guido van Rossumca756f21997-01-23 19:39:29 +0000923 if (ahash == -1)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000924 PyErr_Clear(); /* Don't want errors here */
Guido van Rossumca756f21997-01-23 19:39:29 +0000925 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000926#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000927 if (!PyString_Check(bkey) ||
928 (bhash = ((PyStringObject *) bkey)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000929#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000930 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000931 bhash = PyObject_Hash(bkey);
Guido van Rossumca756f21997-01-23 19:39:29 +0000932 if (bhash == -1)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000933 PyErr_Clear(); /* Don't want errors here */
Guido van Rossumca756f21997-01-23 19:39:29 +0000934 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000935 aval = lookdict(a, akey, ahash) -> me_value;
936 bval = lookdict(b, bkey, bhash) -> me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000937 res = PyObject_Compare(aval, bval);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000938 if (res != 0)
939 break;
940 }
941 if (res == 0) {
942 if (a->ma_used < b->ma_used)
943 res = -1;
944 else if (a->ma_used > b->ma_used)
945 res = 1;
946 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000947 Py_DECREF(akeys);
948 Py_DECREF(bkeys);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000949 return res;
950}
951
Guido van Rossuma0a69b81996-12-05 21:55:55 +0000952#endif /* !NEWCMP */
953
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000954static PyObject *
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000955dict_has_key(mp, args)
956 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000957 PyObject *args;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000958{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000959 PyObject *key;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000960 long hash;
961 register long ok;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000962 if (!PyArg_Parse(args, "O", &key))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000963 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000964#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000965 if (!PyString_Check(key) ||
966 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000967#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000968 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000969 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000970 if (hash == -1)
971 return NULL;
972 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000973 ok = mp->ma_size != 0 && lookdict(mp, key, hash)->me_value != NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000974 return PyInt_FromLong(ok);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000975}
976
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000977static PyObject *
Barry Warsawc38c5da1997-10-06 17:49:20 +0000978dict_get(mp, args)
979 register dictobject *mp;
980 PyObject *args;
981{
982 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +0000983 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +0000984 PyObject *val = NULL;
985 long hash;
986
Barry Warsawc38c5da1997-10-06 17:49:20 +0000987 if (!PyArg_ParseTuple(args, "O|O", &key, &failobj))
988 return NULL;
Guido van Rossum6fcfa721997-10-20 20:10:00 +0000989 if (mp->ma_table == NULL)
990 goto finally;
Barry Warsawc38c5da1997-10-06 17:49:20 +0000991
Barry Warsawc38c5da1997-10-06 17:49:20 +0000992#ifdef CACHE_HASH
993 if (!PyString_Check(key) ||
994 (hash = ((PyStringObject *) key)->ob_shash) == -1)
995#endif
996 {
997 hash = PyObject_Hash(key);
998 if (hash == -1)
999 return NULL;
1000 }
1001 val = lookdict(mp, key, hash)->me_value;
Barry Warsaw320ac331997-10-20 17:26:25 +00001002
Guido van Rossum6fcfa721997-10-20 20:10:00 +00001003 finally:
Barry Warsawc38c5da1997-10-06 17:49:20 +00001004 if (val == NULL)
1005 val = failobj;
1006 Py_INCREF(val);
1007 return val;
1008}
1009
1010
1011static PyObject *
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001012dict_clear(mp, args)
1013 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001014 PyObject *args;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001015{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001016 if (!PyArg_NoArgs(args))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001017 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001018 PyDict_Clear((PyObject *)mp);
1019 Py_INCREF(Py_None);
1020 return Py_None;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001021}
1022
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001023static PyMethodDef mapp_methods[] = {
Guido van Rossum5d8123f1997-07-13 03:58:01 +00001024 {"has_key", (PyCFunction)dict_has_key},
1025 {"keys", (PyCFunction)dict_keys},
1026 {"items", (PyCFunction)dict_items},
1027 {"values", (PyCFunction)dict_values},
Guido van Rossuma8d51311997-06-02 17:13:37 +00001028 {"update", (PyCFunction)dict_update},
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001029 {"clear", (PyCFunction)dict_clear},
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001030 {"copy", (PyCFunction)dict_copy},
Barry Warsawc38c5da1997-10-06 17:49:20 +00001031 {"get", (PyCFunction)dict_get, 1},
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001032 {NULL, NULL} /* sentinel */
1033};
1034
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001035static PyObject *
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001036dict_getattr(mp, name)
1037 dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001038 char *name;
1039{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001040 return Py_FindMethod(mapp_methods, (PyObject *)mp, name);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001041}
1042
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001043PyTypeObject PyDict_Type = {
1044 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001045 0,
Guido van Rossum9bfef441993-03-29 10:43:31 +00001046 "dictionary",
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001047 sizeof(dictobject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001048 0,
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001049 (destructor)dict_dealloc, /*tp_dealloc*/
1050 (printfunc)dict_print, /*tp_print*/
1051 (getattrfunc)dict_getattr, /*tp_getattr*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001052 0, /*tp_setattr*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001053 (cmpfunc)dict_compare, /*tp_compare*/
1054 (reprfunc)dict_repr, /*tp_repr*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001055 0, /*tp_as_number*/
1056 0, /*tp_as_sequence*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001057 &dict_as_mapping, /*tp_as_mapping*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001058};
1059
Guido van Rossum3cca2451997-05-16 14:23:33 +00001060/* For backward compatibility with old dictionary interface */
1061
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001062PyObject *
1063PyDict_GetItemString(v, key)
1064 PyObject *v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001065 char *key;
1066{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001067 PyObject *kv, *rv;
1068 kv = PyString_FromString(key);
1069 if (kv == NULL)
1070 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001071 rv = PyDict_GetItem(v, kv);
1072 Py_DECREF(kv);
1073 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001074}
1075
1076int
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001077PyDict_SetItemString(v, key, item)
1078 PyObject *v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001079 char *key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001080 PyObject *item;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001081{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001082 PyObject *kv;
1083 int err;
1084 kv = PyString_FromString(key);
1085 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001086 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00001087 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00001088 err = PyDict_SetItem(v, kv, item);
1089 Py_DECREF(kv);
1090 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001091}
1092
1093int
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001094PyDict_DelItemString(v, key)
1095 PyObject *v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001096 char *key;
1097{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001098 PyObject *kv;
1099 int err;
1100 kv = PyString_FromString(key);
1101 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001102 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001103 err = PyDict_DelItem(v, kv);
1104 Py_DECREF(kv);
1105 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001106}