Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 2bc1379 | 1999-03-24 19:06:42 +0000 | [diff] [blame] | 32 | /* Dictionary object implementation using a hash table */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | /* |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 38 | * MINSIZE is the minimum size of a dictionary. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 39 | */ |
| 40 | |
| 41 | #define MINSIZE 4 |
| 42 | |
| 43 | /* |
| 44 | Table of irreducible polynomials to efficiently cycle through |
| 45 | GF(2^n)-{0}, 2<=n<=30. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 46 | */ |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 47 | static long polys[] = { |
Guido van Rossum | 9e5656c | 1997-01-29 04:45:16 +0000 | [diff] [blame] | 48 | 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 Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | /* Object used as dummy key to fill deleted entries */ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 81 | static PyObject *dummy; /* Initialized by first call to newdictobject() */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 82 | |
| 83 | /* |
Guido van Rossum | 2bc1379 | 1999-03-24 19:06:42 +0000 | [diff] [blame] | 84 | Invariant for entries: when in use, me_value is not NULL and me_key is |
| 85 | not NULL and not dummy; when not in use, me_value is NULL and me_key |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 86 | is either NULL or dummy. A dummy key value cannot be replaced by |
| 87 | NULL, since otherwise other keys may be lost. |
| 88 | */ |
| 89 | typedef struct { |
| 90 | long me_hash; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 91 | PyObject *me_key; |
| 92 | PyObject *me_value; |
Guido van Rossum | 3648884 | 1997-04-11 19:14:07 +0000 | [diff] [blame] | 93 | #ifdef USE_CACHE_ALIGNED |
| 94 | long aligner; |
| 95 | #endif |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 96 | } dictentry; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | To ensure the lookup algorithm terminates, the table size must be a |
| 100 | prime number and there must be at least one NULL key in the table. |
| 101 | The value ma_fill is the number of non-NULL keys; ma_used is the number |
| 102 | of non-NULL, non-dummy keys. |
| 103 | To avoid slowing down lookups on a near-full table, we resize the table |
| 104 | when it is more than half filled. |
| 105 | */ |
| 106 | typedef struct { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 107 | PyObject_HEAD |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 108 | int ma_fill; |
| 109 | int ma_used; |
| 110 | int ma_size; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 111 | int ma_poly; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 112 | dictentry *ma_table; |
| 113 | } dictobject; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 115 | PyObject * |
| 116 | PyDict_New() |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 117 | { |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 118 | register dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 119 | if (dummy == NULL) { /* Auto-initialize dummy */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 120 | dummy = PyString_FromString("<dummy key>"); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 121 | if (dummy == NULL) |
| 122 | return NULL; |
| 123 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 124 | mp = PyObject_NEW(dictobject, &PyDict_Type); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 125 | if (mp == NULL) |
| 126 | return NULL; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 127 | mp->ma_size = 0; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 128 | mp->ma_poly = 0; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 129 | mp->ma_table = NULL; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 130 | mp->ma_fill = 0; |
| 131 | mp->ma_used = 0; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 132 | PyObject_GC_Init(mp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 133 | return (PyObject *)mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* |
| 137 | The basic lookup function used by all operations. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 138 | This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 139 | Open addressing is preferred over chaining since the link overhead for |
| 140 | chaining would be substantial (100% with typical malloc overhead). |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 141 | However, instead of going through the table at constant steps, we cycle |
| 142 | through the values of GF(2^n)-{0}. This avoids modulo computations, being |
| 143 | much cheaper on RISC machines, without leading to clustering. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 144 | |
Guido van Rossum | 2bc1379 | 1999-03-24 19:06:42 +0000 | [diff] [blame] | 145 | The initial probe index is computed as hash mod the table size. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 146 | Subsequent probe indices use the values of x^i in GF(2^n) as an offset, |
Guido van Rossum | 2bc1379 | 1999-03-24 19:06:42 +0000 | [diff] [blame] | 147 | where x is a root. The initial value is derived from hash, too. |
| 148 | |
| 149 | All arithmetic on hash should ignore overflow. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 150 | |
| 151 | (This version is due to Reimer Behrends, some ideas are also due to |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 152 | Jyrki Alakuijala and Vladimir Marangozov.) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 153 | */ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 154 | static dictentry *lookdict Py_PROTO((dictobject *, PyObject *, long)); |
| 155 | static dictentry * |
| 156 | lookdict(mp, key, hash) |
| 157 | dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 158 | PyObject *key; |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 159 | register long hash; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 160 | { |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 161 | register int i; |
| 162 | register unsigned incr; |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 163 | register dictentry *freeslot; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 164 | register unsigned int mask = mp->ma_size-1; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 165 | dictentry *ep0 = mp->ma_table; |
| 166 | register dictentry *ep; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 167 | /* We must come up with (i, incr) such that 0 <= i < ma_size |
| 168 | and 0 < incr < ma_size and both are a function of hash */ |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 169 | i = (~hash) & mask; |
| 170 | /* We use ~hash instead of hash, as degenerate hash functions, such |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 171 | as for ints <sigh>, can have lots of leading zeros. It's not |
| 172 | really a performance risk, but better safe than sorry. */ |
Guido van Rossum | efb4609 | 1997-01-29 15:53:56 +0000 | [diff] [blame] | 173 | ep = &ep0[i]; |
Guido van Rossum | c1c7b1a | 1998-10-06 16:01:14 +0000 | [diff] [blame] | 174 | if (ep->me_key == NULL || ep->me_key == key) |
Guido van Rossum | 7d18159 | 1997-01-16 21:06:45 +0000 | [diff] [blame] | 175 | return ep; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 176 | if (ep->me_key == dummy) |
Guido van Rossum | 7d18159 | 1997-01-16 21:06:45 +0000 | [diff] [blame] | 177 | freeslot = ep; |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 178 | else { |
Guido van Rossum | c1c7b1a | 1998-10-06 16:01:14 +0000 | [diff] [blame] | 179 | if (ep->me_hash == hash && |
| 180 | PyObject_Compare(ep->me_key, key) == 0) |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 181 | { |
| 182 | return ep; |
| 183 | } |
| 184 | freeslot = NULL; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 185 | } |
Guido van Rossum | 5b2121b | 1997-05-23 00:01:41 +0000 | [diff] [blame] | 186 | /* XXX What if PyObject_Compare returned an exception? */ |
Guido van Rossum | 2bc1379 | 1999-03-24 19:06:42 +0000 | [diff] [blame] | 187 | /* Derive incr from hash, just to make it more arbitrary. Note that |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 188 | incr must not be 0, or we will get into an infinite loop.*/ |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 189 | incr = (hash ^ ((unsigned long)hash >> 3)) & mask; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 190 | if (!incr) |
| 191 | incr = mask; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 192 | for (;;) { |
Guido van Rossum | efb4609 | 1997-01-29 15:53:56 +0000 | [diff] [blame] | 193 | ep = &ep0[(i+incr)&mask]; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 194 | if (ep->me_key == NULL) { |
| 195 | if (freeslot != NULL) |
| 196 | return freeslot; |
| 197 | else |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 198 | return ep; |
| 199 | } |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 200 | if (ep->me_key == dummy) { |
| 201 | if (freeslot == NULL) |
| 202 | freeslot = ep; |
| 203 | } |
| 204 | else if (ep->me_key == key || |
| 205 | (ep->me_hash == hash && |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 206 | PyObject_Compare(ep->me_key, key) == 0)) { |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 207 | return ep; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 208 | } |
Guido van Rossum | 5b2121b | 1997-05-23 00:01:41 +0000 | [diff] [blame] | 209 | /* XXX What if PyObject_Compare returned an exception? */ |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 210 | /* Cycle through GF(2^n)-{0} */ |
| 211 | incr = incr << 1; |
| 212 | if (incr > mask) |
Guido van Rossum | f05fc71 | 1998-11-16 22:46:30 +0000 | [diff] [blame] | 213 | incr ^= mp->ma_poly; /* This will implicitely clear |
| 214 | the highest bit */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | Internal routine to insert a new item into the table. |
| 220 | Used both by the internal resize routine and by the public insert routine. |
| 221 | Eats a reference to key and one to value. |
| 222 | */ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 223 | static void insertdict |
| 224 | Py_PROTO((dictobject *, PyObject *, long, PyObject *)); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 225 | static void |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 226 | insertdict(mp, key, hash, value) |
| 227 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 228 | PyObject *key; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 229 | long hash; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 230 | PyObject *value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 231 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 232 | PyObject *old_value; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 233 | register dictentry *ep; |
| 234 | ep = lookdict(mp, key, hash); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 235 | if (ep->me_value != NULL) { |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 236 | old_value = ep->me_value; |
| 237 | ep->me_value = value; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 238 | Py_DECREF(old_value); /* which **CAN** re-enter */ |
| 239 | Py_DECREF(key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 240 | } |
| 241 | else { |
| 242 | if (ep->me_key == NULL) |
| 243 | mp->ma_fill++; |
| 244 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 245 | Py_DECREF(ep->me_key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 246 | ep->me_key = key; |
| 247 | ep->me_hash = hash; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 248 | ep->me_value = value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 249 | mp->ma_used++; |
| 250 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* |
| 254 | Restructure the table by allocating a new table and reinserting all |
| 255 | items again. When entries have been deleted, the new table may |
| 256 | actually be smaller than the old one. |
| 257 | */ |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 258 | static int dictresize Py_PROTO((dictobject *, int)); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 259 | static int |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 260 | dictresize(mp, minused) |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 261 | dictobject *mp; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 262 | int minused; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 263 | { |
| 264 | register int oldsize = mp->ma_size; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 265 | register int newsize, newpoly; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 266 | register dictentry *oldtable = mp->ma_table; |
| 267 | register dictentry *newtable; |
| 268 | register dictentry *ep; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 269 | register int i; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 270 | for (i = 0, newsize = MINSIZE; ; i++, newsize <<= 1) { |
| 271 | if (i > sizeof(polys)/sizeof(polys[0])) { |
| 272 | /* Ran out of polynomials */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 273 | PyErr_NoMemory(); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 274 | return -1; |
| 275 | } |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 276 | if (newsize > minused) { |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 277 | newpoly = polys[i]; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 278 | break; |
| 279 | } |
| 280 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 281 | newtable = PyMem_NEW(dictentry, newsize); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 282 | if (newtable == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 283 | PyErr_NoMemory(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 284 | return -1; |
| 285 | } |
Guido van Rossum | 0fd0033 | 1998-07-16 15:06:13 +0000 | [diff] [blame] | 286 | memset(newtable, '\0', sizeof(dictentry) * newsize); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 287 | mp->ma_size = newsize; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 288 | mp->ma_poly = newpoly; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 289 | mp->ma_table = newtable; |
| 290 | mp->ma_fill = 0; |
| 291 | mp->ma_used = 0; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 292 | |
| 293 | /* Make two passes, so we can avoid decrefs |
| 294 | (and possible side effects) till the table is copied */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 295 | for (i = 0, ep = oldtable; i < oldsize; i++, ep++) { |
| 296 | if (ep->me_value != NULL) |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 297 | insertdict(mp,ep->me_key,ep->me_hash,ep->me_value); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 298 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 299 | for (i = 0, ep = oldtable; i < oldsize; i++, ep++) { |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 300 | if (ep->me_value == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 301 | Py_XDECREF(ep->me_key); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 302 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 305 | if (oldtable != NULL) |
| 306 | PyMem_DEL(oldtable); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 310 | PyObject * |
| 311 | PyDict_GetItem(op, key) |
| 312 | PyObject *op; |
| 313 | PyObject *key; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 314 | { |
| 315 | long hash; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 316 | if (!PyDict_Check(op)) { |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 317 | return NULL; |
| 318 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 319 | if (((dictobject *)op)->ma_table == NULL) |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 320 | return NULL; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 321 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 322 | if (!PyString_Check(key) || |
| 323 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 324 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 325 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 326 | hash = PyObject_Hash(key); |
Guido van Rossum | 474b19e | 1998-05-14 01:00:51 +0000 | [diff] [blame] | 327 | if (hash == -1) { |
| 328 | PyErr_Clear(); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 329 | return NULL; |
Guido van Rossum | 474b19e | 1998-05-14 01:00:51 +0000 | [diff] [blame] | 330 | } |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 331 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 332 | return lookdict((dictobject *)op, key, hash) -> me_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 336 | PyDict_SetItem(op, key, value) |
| 337 | register PyObject *op; |
| 338 | PyObject *key; |
| 339 | PyObject *value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 340 | { |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 341 | register dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 342 | register long hash; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 343 | if (!PyDict_Check(op)) { |
| 344 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 345 | return -1; |
| 346 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 347 | mp = (dictobject *)op; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 348 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 349 | if (PyString_Check(key)) { |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 350 | #ifdef INTERN_STRINGS |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 351 | if (((PyStringObject *)key)->ob_sinterned != NULL) { |
| 352 | key = ((PyStringObject *)key)->ob_sinterned; |
| 353 | hash = ((PyStringObject *)key)->ob_shash; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 354 | } |
| 355 | else |
| 356 | #endif |
| 357 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 358 | hash = ((PyStringObject *)key)->ob_shash; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 359 | if (hash == -1) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 360 | hash = PyObject_Hash(key); |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | else |
| 364 | #endif |
| 365 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 366 | hash = PyObject_Hash(key); |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 367 | if (hash == -1) |
| 368 | return -1; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 369 | } |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 370 | /* if fill >= 2/3 size, double in size */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 371 | if (mp->ma_fill*3 >= mp->ma_size*2) { |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 372 | if (dictresize(mp, mp->ma_used*2) != 0) { |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 373 | if (mp->ma_fill+1 > mp->ma_size) |
| 374 | return -1; |
| 375 | } |
| 376 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 377 | Py_INCREF(value); |
| 378 | Py_INCREF(key); |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 379 | insertdict(mp, key, hash, value); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 384 | PyDict_DelItem(op, key) |
| 385 | PyObject *op; |
| 386 | PyObject *key; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 387 | { |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 388 | register dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 389 | register long hash; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 390 | register dictentry *ep; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 391 | PyObject *old_value, *old_key; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 392 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 393 | if (!PyDict_Check(op)) { |
| 394 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 395 | return -1; |
| 396 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 397 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 398 | if (!PyString_Check(key) || |
| 399 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 400 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 401 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 402 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 403 | if (hash == -1) |
| 404 | return -1; |
| 405 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 406 | mp = (dictobject *)op; |
| 407 | if (((dictobject *)op)->ma_table == NULL) |
Guido van Rossum | efc8713 | 1995-01-02 19:42:39 +0000 | [diff] [blame] | 408 | goto empty; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 409 | ep = lookdict(mp, key, hash); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 410 | if (ep->me_value == NULL) { |
Guido van Rossum | efc8713 | 1995-01-02 19:42:39 +0000 | [diff] [blame] | 411 | empty: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 412 | PyErr_SetObject(PyExc_KeyError, key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 413 | return -1; |
| 414 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 415 | old_key = ep->me_key; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 416 | Py_INCREF(dummy); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 417 | ep->me_key = dummy; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 418 | old_value = ep->me_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 419 | ep->me_value = NULL; |
| 420 | mp->ma_used--; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 421 | Py_DECREF(old_value); |
| 422 | Py_DECREF(old_key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 423 | return 0; |
| 424 | } |
| 425 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 426 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 427 | PyDict_Clear(op) |
| 428 | PyObject *op; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 429 | { |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 430 | int i, n; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 431 | register dictentry *table; |
| 432 | dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 433 | if (!PyDict_Check(op)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 434 | return; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 435 | mp = (dictobject *)op; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 436 | table = mp->ma_table; |
| 437 | if (table == NULL) |
| 438 | return; |
| 439 | n = mp->ma_size; |
| 440 | mp->ma_size = mp->ma_used = mp->ma_fill = 0; |
| 441 | mp->ma_table = NULL; |
| 442 | for (i = 0; i < n; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 443 | Py_XDECREF(table[i].me_key); |
| 444 | Py_XDECREF(table[i].me_value); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 445 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 446 | PyMem_DEL(table); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 449 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 450 | PyDict_Next(op, ppos, pkey, pvalue) |
| 451 | PyObject *op; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 452 | int *ppos; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 453 | PyObject **pkey; |
| 454 | PyObject **pvalue; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 455 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 456 | int i; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 457 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 458 | if (!PyDict_Check(op)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 459 | return 0; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 460 | mp = (dictobject *)op; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 461 | i = *ppos; |
| 462 | if (i < 0) |
| 463 | return 0; |
| 464 | while (i < mp->ma_size && mp->ma_table[i].me_value == NULL) |
| 465 | i++; |
| 466 | *ppos = i+1; |
| 467 | if (i >= mp->ma_size) |
| 468 | return 0; |
| 469 | if (pkey) |
| 470 | *pkey = mp->ma_table[i].me_key; |
| 471 | if (pvalue) |
| 472 | *pvalue = mp->ma_table[i].me_value; |
| 473 | return 1; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /* Methods */ |
| 477 | |
| 478 | static void |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 479 | dict_dealloc(mp) |
| 480 | register dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 481 | { |
| 482 | register int i; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 483 | register dictentry *ep; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 484 | Py_TRASHCAN_SAFE_BEGIN(mp) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 485 | PyObject_GC_Fini(mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 486 | for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) { |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 487 | if (ep->me_key != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 488 | Py_DECREF(ep->me_key); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 489 | } |
| 490 | if (ep->me_value != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 491 | Py_DECREF(ep->me_value); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 492 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 493 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 494 | if (mp->ma_table != NULL) |
| 495 | PyMem_DEL(mp->ma_table); |
| 496 | PyObject_DEL(mp); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 497 | Py_TRASHCAN_SAFE_END(mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static int |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 501 | dict_print(mp, fp, flags) |
| 502 | register dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 503 | register FILE *fp; |
| 504 | register int flags; |
| 505 | { |
| 506 | register int i; |
| 507 | register int any; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 508 | register dictentry *ep; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 509 | |
| 510 | i = Py_ReprEnter((PyObject*)mp); |
| 511 | if (i != 0) { |
| 512 | if (i < 0) |
| 513 | return i; |
| 514 | fprintf(fp, "{...}"); |
| 515 | return 0; |
| 516 | } |
| 517 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 518 | fprintf(fp, "{"); |
| 519 | any = 0; |
| 520 | for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) { |
| 521 | if (ep->me_value != NULL) { |
| 522 | if (any++ > 0) |
| 523 | fprintf(fp, ", "); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 524 | if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) { |
| 525 | Py_ReprLeave((PyObject*)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 526 | return -1; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 527 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 528 | fprintf(fp, ": "); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 529 | if (PyObject_Print(ep->me_value, fp, 0) != 0) { |
| 530 | Py_ReprLeave((PyObject*)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 531 | return -1; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 532 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | fprintf(fp, "}"); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 536 | Py_ReprLeave((PyObject*)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 537 | return 0; |
| 538 | } |
| 539 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 540 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 541 | dict_repr(mp) |
| 542 | dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 543 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 544 | auto PyObject *v; |
| 545 | PyObject *sepa, *colon; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 546 | register int i; |
| 547 | register int any; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 548 | register dictentry *ep; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 549 | |
| 550 | i = Py_ReprEnter((PyObject*)mp); |
| 551 | if (i != 0) { |
| 552 | if (i > 0) |
| 553 | return PyString_FromString("{...}"); |
| 554 | return NULL; |
| 555 | } |
| 556 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 557 | v = PyString_FromString("{"); |
| 558 | sepa = PyString_FromString(", "); |
| 559 | colon = PyString_FromString(": "); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 560 | any = 0; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 561 | for (i = 0, ep = mp->ma_table; i < mp->ma_size && v; i++, ep++) { |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 562 | if (ep->me_value != NULL) { |
| 563 | if (any++) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 564 | PyString_Concat(&v, sepa); |
| 565 | PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_key)); |
| 566 | PyString_Concat(&v, colon); |
| 567 | PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_value)); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 570 | PyString_ConcatAndDel(&v, PyString_FromString("}")); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 571 | Py_ReprLeave((PyObject*)mp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 572 | Py_XDECREF(sepa); |
| 573 | Py_XDECREF(colon); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 574 | return v; |
| 575 | } |
| 576 | |
| 577 | static int |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 578 | dict_length(mp) |
| 579 | dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 580 | { |
| 581 | return mp->ma_used; |
| 582 | } |
| 583 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 584 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 585 | dict_subscript(mp, key) |
| 586 | dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 587 | register PyObject *key; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 588 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 589 | PyObject *v; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 590 | long hash; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 591 | if (mp->ma_table == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 592 | PyErr_SetObject(PyExc_KeyError, key); |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 593 | return NULL; |
| 594 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 595 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 596 | if (!PyString_Check(key) || |
| 597 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 598 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 599 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 600 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 601 | if (hash == -1) |
| 602 | return NULL; |
| 603 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 604 | v = lookdict(mp, key, hash) -> me_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 605 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 606 | PyErr_SetObject(PyExc_KeyError, key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 607 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 608 | Py_INCREF(v); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 609 | return v; |
| 610 | } |
| 611 | |
| 612 | static int |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 613 | dict_ass_sub(mp, v, w) |
| 614 | dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 615 | PyObject *v, *w; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 616 | { |
| 617 | if (w == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 618 | return PyDict_DelItem((PyObject *)mp, v); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 619 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 620 | return PyDict_SetItem((PyObject *)mp, v, w); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 623 | static PyMappingMethods dict_as_mapping = { |
| 624 | (inquiry)dict_length, /*mp_length*/ |
| 625 | (binaryfunc)dict_subscript, /*mp_subscript*/ |
| 626 | (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 627 | }; |
| 628 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 629 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 630 | dict_keys(mp, args) |
| 631 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 632 | PyObject *args; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 633 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 634 | register PyObject *v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 635 | register int i, j; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 636 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 637 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 638 | v = PyList_New(mp->ma_used); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 639 | if (v == NULL) |
| 640 | return NULL; |
| 641 | for (i = 0, j = 0; i < mp->ma_size; i++) { |
| 642 | if (mp->ma_table[i].me_value != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 643 | PyObject *key = mp->ma_table[i].me_key; |
| 644 | Py_INCREF(key); |
| 645 | PyList_SetItem(v, j, key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 646 | j++; |
| 647 | } |
| 648 | } |
| 649 | return v; |
| 650 | } |
| 651 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 652 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 653 | dict_values(mp, args) |
| 654 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 655 | PyObject *args; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 656 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 657 | register PyObject *v; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 658 | register int i, j; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 659 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 660 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 661 | v = PyList_New(mp->ma_used); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 662 | if (v == NULL) |
| 663 | return NULL; |
| 664 | for (i = 0, j = 0; i < mp->ma_size; i++) { |
| 665 | if (mp->ma_table[i].me_value != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 666 | PyObject *value = mp->ma_table[i].me_value; |
| 667 | Py_INCREF(value); |
| 668 | PyList_SetItem(v, j, value); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 669 | j++; |
| 670 | } |
| 671 | } |
| 672 | return v; |
| 673 | } |
| 674 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 675 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 676 | dict_items(mp, args) |
| 677 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 678 | PyObject *args; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 679 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 680 | register PyObject *v; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 681 | register int i, j; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 682 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 683 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 684 | v = PyList_New(mp->ma_used); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 685 | if (v == NULL) |
| 686 | return NULL; |
| 687 | for (i = 0, j = 0; i < mp->ma_size; i++) { |
| 688 | if (mp->ma_table[i].me_value != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 689 | PyObject *key = mp->ma_table[i].me_key; |
| 690 | PyObject *value = mp->ma_table[i].me_value; |
| 691 | PyObject *item = PyTuple_New(2); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 692 | if (item == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 693 | Py_DECREF(v); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 694 | return NULL; |
| 695 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 696 | Py_INCREF(key); |
| 697 | PyTuple_SetItem(item, 0, key); |
| 698 | Py_INCREF(value); |
| 699 | PyTuple_SetItem(item, 1, value); |
| 700 | PyList_SetItem(v, j, item); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 701 | j++; |
| 702 | } |
| 703 | } |
| 704 | return v; |
| 705 | } |
| 706 | |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 707 | static PyObject * |
Guido van Rossum | a8d5131 | 1997-06-02 17:13:37 +0000 | [diff] [blame] | 708 | dict_update(mp, args) |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 709 | register dictobject *mp; |
| 710 | PyObject *args; |
| 711 | { |
| 712 | register int i; |
| 713 | dictobject *other; |
| 714 | dictentry *entry; |
| 715 | if (!PyArg_Parse(args, "O!", &PyDict_Type, &other)) |
| 716 | return NULL; |
| 717 | if (other == mp) |
Guido van Rossum | a8d5131 | 1997-06-02 17:13:37 +0000 | [diff] [blame] | 718 | goto done; /* a.update(a); nothing to do */ |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 719 | /* Do one big resize at the start, rather than incrementally |
| 720 | resizing as we insert new items. Expect that there will be |
| 721 | no (or few) overlapping keys. */ |
| 722 | if ((mp->ma_fill + other->ma_used)*3 >= mp->ma_size*2) { |
| 723 | if (dictresize(mp, (mp->ma_used + other->ma_used)*3/2) != 0) |
| 724 | return NULL; |
| 725 | } |
| 726 | for (i = 0; i < other->ma_size; i++) { |
| 727 | entry = &other->ma_table[i]; |
| 728 | if (entry->me_value != NULL) { |
| 729 | Py_INCREF(entry->me_key); |
| 730 | Py_INCREF(entry->me_value); |
| 731 | insertdict(mp, entry->me_key, entry->me_hash, |
| 732 | entry->me_value); |
| 733 | } |
| 734 | } |
| 735 | done: |
| 736 | Py_INCREF(Py_None); |
| 737 | return Py_None; |
| 738 | } |
| 739 | |
| 740 | static PyObject * |
| 741 | dict_copy(mp, args) |
| 742 | register dictobject *mp; |
| 743 | PyObject *args; |
| 744 | { |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 745 | if (!PyArg_Parse(args, "")) |
| 746 | return NULL; |
| 747 | return PyDict_Copy((PyObject*)mp); |
| 748 | } |
| 749 | |
| 750 | PyObject * |
| 751 | PyDict_Copy(o) |
| 752 | PyObject *o; |
| 753 | { |
| 754 | register dictobject *mp; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 755 | register int i; |
| 756 | dictobject *copy; |
| 757 | dictentry *entry; |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 758 | |
| 759 | if (o == NULL || !PyDict_Check(o)) { |
| 760 | PyErr_BadInternalCall(); |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 761 | return NULL; |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 762 | } |
| 763 | mp = (dictobject *)o; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 764 | copy = (dictobject *)PyDict_New(); |
| 765 | if (copy == NULL) |
| 766 | return NULL; |
| 767 | if (mp->ma_used > 0) { |
| 768 | if (dictresize(copy, mp->ma_used*3/2) != 0) |
| 769 | return NULL; |
| 770 | for (i = 0; i < mp->ma_size; i++) { |
| 771 | entry = &mp->ma_table[i]; |
| 772 | if (entry->me_value != NULL) { |
| 773 | Py_INCREF(entry->me_key); |
| 774 | Py_INCREF(entry->me_value); |
| 775 | insertdict(copy, entry->me_key, entry->me_hash, |
| 776 | entry->me_value); |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | return (PyObject *)copy; |
| 781 | } |
| 782 | |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 783 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 784 | PyDict_Size(mp) |
| 785 | PyObject *mp; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 786 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 787 | if (mp == NULL || !PyDict_Check(mp)) { |
| 788 | PyErr_BadInternalCall(); |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 789 | return 0; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 790 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 791 | return ((dictobject *)mp)->ma_used; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 794 | PyObject * |
| 795 | PyDict_Keys(mp) |
| 796 | PyObject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 797 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 798 | if (mp == NULL || !PyDict_Check(mp)) { |
| 799 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 800 | return NULL; |
| 801 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 802 | return dict_keys((dictobject *)mp, (PyObject *)NULL); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 805 | PyObject * |
| 806 | PyDict_Values(mp) |
| 807 | PyObject *mp; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 808 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 809 | if (mp == NULL || !PyDict_Check(mp)) { |
| 810 | PyErr_BadInternalCall(); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 811 | return NULL; |
| 812 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 813 | return dict_values((dictobject *)mp, (PyObject *)NULL); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 816 | PyObject * |
| 817 | PyDict_Items(mp) |
| 818 | PyObject *mp; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 819 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 820 | if (mp == NULL || !PyDict_Check(mp)) { |
| 821 | PyErr_BadInternalCall(); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 822 | return NULL; |
| 823 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 824 | return dict_items((dictobject *)mp, (PyObject *)NULL); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 827 | #define NEWCMP |
| 828 | |
| 829 | #ifdef NEWCMP |
| 830 | |
| 831 | /* Subroutine which returns the smallest key in a for which b's value |
| 832 | is different or absent. The value is returned too, through the |
| 833 | pval argument. No reference counts are incremented. */ |
| 834 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 835 | static PyObject * |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 836 | characterize(a, b, pval) |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 837 | dictobject *a; |
| 838 | dictobject *b; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 839 | PyObject **pval; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 840 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 841 | PyObject *diff = NULL; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 842 | int i; |
| 843 | |
| 844 | *pval = NULL; |
| 845 | for (i = 0; i < a->ma_size; i++) { |
| 846 | if (a->ma_table[i].me_value != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 847 | PyObject *key = a->ma_table[i].me_key; |
| 848 | PyObject *aval, *bval; |
Guido van Rossum | 5b2121b | 1997-05-23 00:01:41 +0000 | [diff] [blame] | 849 | /* XXX What if PyObject_Compare raises an exception? */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 850 | if (diff != NULL && PyObject_Compare(key, diff) > 0) |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 851 | continue; |
| 852 | aval = a->ma_table[i].me_value; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 853 | bval = PyDict_GetItem((PyObject *)b, key); |
Guido van Rossum | 5b2121b | 1997-05-23 00:01:41 +0000 | [diff] [blame] | 854 | /* XXX What if PyObject_Compare raises an exception? */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 855 | if (bval == NULL || PyObject_Compare(aval, bval) != 0) |
| 856 | { |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 857 | diff = key; |
| 858 | *pval = aval; |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | return diff; |
| 863 | } |
| 864 | |
| 865 | static int |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 866 | dict_compare(a, b) |
| 867 | dictobject *a, *b; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 868 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 869 | PyObject *adiff, *bdiff, *aval, *bval; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 870 | int res; |
| 871 | |
| 872 | /* Compare lengths first */ |
| 873 | if (a->ma_used < b->ma_used) |
| 874 | return -1; /* a is shorter */ |
| 875 | else if (a->ma_used > b->ma_used) |
| 876 | return 1; /* b is shorter */ |
| 877 | /* Same length -- check all keys */ |
| 878 | adiff = characterize(a, b, &aval); |
Guido van Rossum | 5b2121b | 1997-05-23 00:01:41 +0000 | [diff] [blame] | 879 | if (PyErr_Occurred()) |
| 880 | return -1; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 881 | if (adiff == NULL) |
| 882 | return 0; /* a is a subset with the same length */ |
| 883 | bdiff = characterize(b, a, &bval); |
Guido van Rossum | 5b2121b | 1997-05-23 00:01:41 +0000 | [diff] [blame] | 884 | if (PyErr_Occurred()) |
| 885 | return -1; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 886 | /* bdiff == NULL would be impossible now */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 887 | res = PyObject_Compare(adiff, bdiff); |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 888 | if (res == 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 889 | res = PyObject_Compare(aval, bval); |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 890 | return res; |
| 891 | } |
| 892 | |
| 893 | #else /* !NEWCMP */ |
| 894 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 895 | static int |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 896 | dict_compare(a, b) |
| 897 | dictobject *a, *b; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 898 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 899 | PyObject *akeys, *bkeys; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 900 | int i, n, res; |
| 901 | if (a == b) |
| 902 | return 0; |
| 903 | if (a->ma_used == 0) { |
| 904 | if (b->ma_used != 0) |
| 905 | return -1; |
| 906 | else |
| 907 | return 0; |
| 908 | } |
| 909 | else { |
| 910 | if (b->ma_used == 0) |
| 911 | return 1; |
| 912 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 913 | akeys = dict_keys(a, (PyObject *)NULL); |
| 914 | bkeys = dict_keys(b, (PyObject *)NULL); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 915 | if (akeys == NULL || bkeys == NULL) { |
| 916 | /* Oops, out of memory -- what to do? */ |
| 917 | /* For now, sort on address! */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 918 | Py_XDECREF(akeys); |
| 919 | Py_XDECREF(bkeys); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 920 | if (a < b) |
| 921 | return -1; |
| 922 | else |
| 923 | return 1; |
| 924 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 925 | PyList_Sort(akeys); |
| 926 | PyList_Sort(bkeys); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 927 | n = a->ma_used < b->ma_used ? a->ma_used : b->ma_used; /* smallest */ |
| 928 | res = 0; |
| 929 | for (i = 0; i < n; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 930 | PyObject *akey, *bkey, *aval, *bval; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 931 | long ahash, bhash; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 932 | akey = PyList_GetItem(akeys, i); |
| 933 | bkey = PyList_GetItem(bkeys, i); |
| 934 | res = PyObject_Compare(akey, bkey); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 935 | if (res != 0) |
| 936 | break; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 937 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 938 | if (!PyString_Check(akey) || |
| 939 | (ahash = ((PyStringObject *) akey)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 940 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 941 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 942 | ahash = PyObject_Hash(akey); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 943 | if (ahash == -1) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 944 | PyErr_Clear(); /* Don't want errors here */ |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 945 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 946 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 947 | if (!PyString_Check(bkey) || |
| 948 | (bhash = ((PyStringObject *) bkey)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 949 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 950 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 951 | bhash = PyObject_Hash(bkey); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 952 | if (bhash == -1) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 953 | PyErr_Clear(); /* Don't want errors here */ |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 954 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 955 | aval = lookdict(a, akey, ahash) -> me_value; |
| 956 | bval = lookdict(b, bkey, bhash) -> me_value; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 957 | res = PyObject_Compare(aval, bval); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 958 | if (res != 0) |
| 959 | break; |
| 960 | } |
| 961 | if (res == 0) { |
| 962 | if (a->ma_used < b->ma_used) |
| 963 | res = -1; |
| 964 | else if (a->ma_used > b->ma_used) |
| 965 | res = 1; |
| 966 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 967 | Py_DECREF(akeys); |
| 968 | Py_DECREF(bkeys); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 969 | return res; |
| 970 | } |
| 971 | |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 972 | #endif /* !NEWCMP */ |
| 973 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 974 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 975 | dict_has_key(mp, args) |
| 976 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 977 | PyObject *args; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 978 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 979 | PyObject *key; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 980 | long hash; |
| 981 | register long ok; |
Fred Drake | 52fccfd | 2000-02-23 15:47:16 +0000 | [diff] [blame] | 982 | if (!PyArg_ParseTuple(args, "O:has_key", &key)) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 983 | return NULL; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 984 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 985 | if (!PyString_Check(key) || |
| 986 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 987 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 988 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 989 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 990 | if (hash == -1) |
| 991 | return NULL; |
| 992 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 993 | ok = mp->ma_size != 0 && lookdict(mp, key, hash)->me_value != NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 994 | return PyInt_FromLong(ok); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 997 | static PyObject * |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 998 | dict_get(mp, args) |
| 999 | register dictobject *mp; |
| 1000 | PyObject *args; |
| 1001 | { |
| 1002 | PyObject *key; |
Barry Warsaw | 320ac33 | 1997-10-20 17:26:25 +0000 | [diff] [blame] | 1003 | PyObject *failobj = Py_None; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1004 | PyObject *val = NULL; |
| 1005 | long hash; |
| 1006 | |
Fred Drake | 52fccfd | 2000-02-23 15:47:16 +0000 | [diff] [blame] | 1007 | if (!PyArg_ParseTuple(args, "O|O:get", &key, &failobj)) |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1008 | return NULL; |
Guido van Rossum | 6fcfa72 | 1997-10-20 20:10:00 +0000 | [diff] [blame] | 1009 | if (mp->ma_table == NULL) |
| 1010 | goto finally; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1011 | |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1012 | #ifdef CACHE_HASH |
| 1013 | if (!PyString_Check(key) || |
| 1014 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
| 1015 | #endif |
| 1016 | { |
| 1017 | hash = PyObject_Hash(key); |
| 1018 | if (hash == -1) |
| 1019 | return NULL; |
| 1020 | } |
| 1021 | val = lookdict(mp, key, hash)->me_value; |
Barry Warsaw | 320ac33 | 1997-10-20 17:26:25 +0000 | [diff] [blame] | 1022 | |
Guido van Rossum | 6fcfa72 | 1997-10-20 20:10:00 +0000 | [diff] [blame] | 1023 | finally: |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1024 | if (val == NULL) |
| 1025 | val = failobj; |
| 1026 | Py_INCREF(val); |
| 1027 | return val; |
| 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1032 | dict_clear(mp, args) |
| 1033 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1034 | PyObject *args; |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 1035 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1036 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 1037 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1038 | PyDict_Clear((PyObject *)mp); |
| 1039 | Py_INCREF(Py_None); |
| 1040 | return Py_None; |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1043 | static int |
| 1044 | dict_traverse(PyObject *op, visitproc visit, void *arg) |
| 1045 | { |
| 1046 | int i = 0, err; |
| 1047 | PyObject *pk; |
| 1048 | PyObject *pv; |
| 1049 | |
| 1050 | while (PyDict_Next(op, &i, &pk, &pv)) { |
| 1051 | err = visit(pk, arg); |
| 1052 | if (err) |
| 1053 | return err; |
| 1054 | err = visit(pv, arg); |
| 1055 | if (err) |
| 1056 | return err; |
| 1057 | } |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | static int |
| 1062 | dict_tp_clear(PyObject *op) |
| 1063 | { |
| 1064 | PyDict_Clear(op); |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1068 | static PyMethodDef mapp_methods[] = { |
Fred Drake | 52fccfd | 2000-02-23 15:47:16 +0000 | [diff] [blame] | 1069 | {"has_key", (PyCFunction)dict_has_key, METH_VARARGS}, |
Guido van Rossum | 5d8123f | 1997-07-13 03:58:01 +0000 | [diff] [blame] | 1070 | {"keys", (PyCFunction)dict_keys}, |
| 1071 | {"items", (PyCFunction)dict_items}, |
| 1072 | {"values", (PyCFunction)dict_values}, |
Guido van Rossum | a8d5131 | 1997-06-02 17:13:37 +0000 | [diff] [blame] | 1073 | {"update", (PyCFunction)dict_update}, |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1074 | {"clear", (PyCFunction)dict_clear}, |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1075 | {"copy", (PyCFunction)dict_copy}, |
Fred Drake | 52fccfd | 2000-02-23 15:47:16 +0000 | [diff] [blame] | 1076 | {"get", (PyCFunction)dict_get, METH_VARARGS}, |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1077 | {NULL, NULL} /* sentinel */ |
| 1078 | }; |
| 1079 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1080 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1081 | dict_getattr(mp, name) |
| 1082 | dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1083 | char *name; |
| 1084 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1085 | return Py_FindMethod(mapp_methods, (PyObject *)mp, name); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1088 | PyTypeObject PyDict_Type = { |
| 1089 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1090 | 0, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1091 | "dictionary", |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1092 | sizeof(dictobject) + PyGC_HEAD_SIZE, |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1093 | 0, |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1094 | (destructor)dict_dealloc, /*tp_dealloc*/ |
| 1095 | (printfunc)dict_print, /*tp_print*/ |
| 1096 | (getattrfunc)dict_getattr, /*tp_getattr*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1097 | 0, /*tp_setattr*/ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1098 | (cmpfunc)dict_compare, /*tp_compare*/ |
| 1099 | (reprfunc)dict_repr, /*tp_repr*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1100 | 0, /*tp_as_number*/ |
| 1101 | 0, /*tp_as_sequence*/ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1102 | &dict_as_mapping, /*tp_as_mapping*/ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1103 | 0, /* tp_hash */ |
| 1104 | 0, /* tp_call */ |
| 1105 | 0, /* tp_str */ |
| 1106 | 0, /* tp_getattro */ |
| 1107 | 0, /* tp_setattro */ |
| 1108 | 0, /* tp_as_buffer */ |
Jeremy Hylton | d08b4c4 | 2000-06-23 19:37:02 +0000 | [diff] [blame] | 1109 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1110 | 0, /* tp_doc */ |
| 1111 | (traverseproc)dict_traverse, /* tp_traverse */ |
| 1112 | (inquiry)dict_tp_clear, /* tp_clear */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1113 | }; |
| 1114 | |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1115 | /* For backward compatibility with old dictionary interface */ |
| 1116 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1117 | PyObject * |
| 1118 | PyDict_GetItemString(v, key) |
| 1119 | PyObject *v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1120 | char *key; |
| 1121 | { |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1122 | PyObject *kv, *rv; |
| 1123 | kv = PyString_FromString(key); |
| 1124 | if (kv == NULL) |
| 1125 | return NULL; |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1126 | rv = PyDict_GetItem(v, kv); |
| 1127 | Py_DECREF(kv); |
| 1128 | return rv; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1132 | PyDict_SetItemString(v, key, item) |
| 1133 | PyObject *v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1134 | char *key; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1135 | PyObject *item; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1136 | { |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1137 | PyObject *kv; |
| 1138 | int err; |
| 1139 | kv = PyString_FromString(key); |
| 1140 | if (kv == NULL) |
Guido van Rossum | 037b220 | 1997-05-20 18:35:19 +0000 | [diff] [blame] | 1141 | return -1; |
Guido van Rossum | 4f3bf1e | 1997-09-29 23:31:11 +0000 | [diff] [blame] | 1142 | PyString_InternInPlace(&kv); /* XXX Should we really? */ |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1143 | err = PyDict_SetItem(v, kv, item); |
| 1144 | Py_DECREF(kv); |
| 1145 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1149 | PyDict_DelItemString(v, key) |
| 1150 | PyObject *v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1151 | char *key; |
| 1152 | { |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1153 | PyObject *kv; |
| 1154 | int err; |
| 1155 | kv = PyString_FromString(key); |
| 1156 | if (kv == NULL) |
Guido van Rossum | 037b220 | 1997-05-20 18:35:19 +0000 | [diff] [blame] | 1157 | return -1; |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1158 | err = PyDict_DelItem(v, kv); |
| 1159 | Py_DECREF(kv); |
| 1160 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1161 | } |