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 | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 32 | /* Dictionaru 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 | /* |
| 84 | Invariant for entries: when in use, de_value is not NULL and de_key is |
| 85 | not NULL and not dummy; when not in use, de_value is NULL and de_key |
| 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; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 132 | return (PyObject *)mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* |
| 136 | The basic lookup function used by all operations. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 137 | 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] | 138 | Open addressing is preferred over chaining since the link overhead for |
| 139 | chaining would be substantial (100% with typical malloc overhead). |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 140 | However, instead of going through the table at constant steps, we cycle |
| 141 | through the values of GF(2^n)-{0}. This avoids modulo computations, being |
| 142 | much cheaper on RISC machines, without leading to clustering. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 143 | |
| 144 | First a 32-bit hash value, 'sum', is computed from the key string. |
| 145 | The first character is added an extra time shifted by 8 to avoid hashing |
| 146 | single-character keys (often heavily used variables) too close together. |
| 147 | All arithmetic on sum should ignore overflow. |
| 148 | |
| 149 | The initial probe index is then computed as sum mod the table size. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 150 | Subsequent probe indices use the values of x^i in GF(2^n) as an offset, |
| 151 | where x is a root. The initial value is derived from sum, too. |
| 152 | |
| 153 | (This version is due to Reimer Behrends, some ideas are also due to |
| 154 | Jyrki Alakuijala.) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 155 | */ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 156 | static dictentry *lookdict Py_PROTO((dictobject *, PyObject *, long)); |
| 157 | static dictentry * |
| 158 | lookdict(mp, key, hash) |
| 159 | dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 160 | PyObject *key; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 161 | long hash; |
| 162 | { |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 163 | register int i; |
| 164 | register unsigned incr; |
| 165 | register unsigned long sum = (unsigned long) hash; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 166 | register dictentry *freeslot = NULL; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 167 | register unsigned int mask = mp->ma_size-1; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 168 | dictentry *ep0 = mp->ma_table; |
| 169 | register dictentry *ep; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 170 | /* We must come up with (i, incr) such that 0 <= i < ma_size |
| 171 | and 0 < incr < ma_size and both are a function of hash */ |
| 172 | i = (~sum) & mask; |
| 173 | /* We use ~sum instead if sum, as degenerate hash functions, such |
| 174 | as for ints <sigh>, can have lots of leading zeros. It's not |
| 175 | really a performance risk, but better safe than sorry. */ |
Guido van Rossum | efb4609 | 1997-01-29 15:53:56 +0000 | [diff] [blame] | 176 | ep = &ep0[i]; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 177 | if (ep->me_key == NULL) |
Guido van Rossum | 7d18159 | 1997-01-16 21:06:45 +0000 | [diff] [blame] | 178 | return ep; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 179 | if (ep->me_key == dummy) |
Guido van Rossum | 7d18159 | 1997-01-16 21:06:45 +0000 | [diff] [blame] | 180 | freeslot = ep; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 181 | else if (ep->me_key == key || |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 182 | (ep->me_hash == hash && |
| 183 | PyObject_Compare(ep->me_key, key) == 0)) |
| 184 | { |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 185 | return ep; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 186 | } |
Guido van Rossum | efb4609 | 1997-01-29 15:53:56 +0000 | [diff] [blame] | 187 | /* Derive incr from sum, 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 | efb4609 | 1997-01-29 15:53:56 +0000 | [diff] [blame] | 189 | incr = (sum ^ (sum >> 3)) & mask; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 190 | if (!incr) |
| 191 | incr = mask; |
| 192 | if (incr > mask) /* Cycle through GF(2^n)-{0} */ |
| 193 | incr ^= mp->ma_poly; /* This will implicitly clear the |
| 194 | highest bit */ |
| 195 | for (;;) { |
Guido van Rossum | efb4609 | 1997-01-29 15:53:56 +0000 | [diff] [blame] | 196 | ep = &ep0[(i+incr)&mask]; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 197 | if (ep->me_key == NULL) { |
| 198 | if (freeslot != NULL) |
| 199 | return freeslot; |
| 200 | else |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 201 | return ep; |
| 202 | } |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 203 | if (ep->me_key == dummy) { |
| 204 | if (freeslot == NULL) |
| 205 | freeslot = ep; |
| 206 | } |
| 207 | else if (ep->me_key == key || |
| 208 | (ep->me_hash == hash && |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 209 | PyObject_Compare(ep->me_key, key) == 0)) { |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 210 | return ep; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 211 | } |
| 212 | /* Cycle through GF(2^n)-{0} */ |
| 213 | incr = incr << 1; |
| 214 | if (incr > mask) |
| 215 | incr ^= mp->ma_poly; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | Internal routine to insert a new item into the table. |
| 221 | Used both by the internal resize routine and by the public insert routine. |
| 222 | Eats a reference to key and one to value. |
| 223 | */ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 224 | static void insertdict |
| 225 | Py_PROTO((dictobject *, PyObject *, long, PyObject *)); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 226 | static void |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 227 | insertdict(mp, key, hash, value) |
| 228 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 229 | PyObject *key; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 230 | long hash; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 231 | PyObject *value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 232 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 233 | PyObject *old_value; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 234 | register dictentry *ep; |
| 235 | ep = lookdict(mp, key, hash); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 236 | if (ep->me_value != NULL) { |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 237 | old_value = ep->me_value; |
| 238 | ep->me_value = value; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 239 | Py_DECREF(old_value); /* which **CAN** re-enter */ |
| 240 | Py_DECREF(key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 241 | } |
| 242 | else { |
| 243 | if (ep->me_key == NULL) |
| 244 | mp->ma_fill++; |
| 245 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 246 | Py_DECREF(ep->me_key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 247 | ep->me_key = key; |
| 248 | ep->me_hash = hash; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 249 | ep->me_value = value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 250 | mp->ma_used++; |
| 251 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* |
| 255 | Restructure the table by allocating a new table and reinserting all |
| 256 | items again. When entries have been deleted, the new table may |
| 257 | actually be smaller than the old one. |
| 258 | */ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 259 | static int dictresize Py_PROTO((dictobject *)); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 260 | static int |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 261 | dictresize(mp) |
| 262 | dictobject *mp; |
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; |
| 270 | newsize = mp->ma_size; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 271 | for (i = 0, newsize = MINSIZE; ; i++, newsize <<= 1) { |
| 272 | if (i > sizeof(polys)/sizeof(polys[0])) { |
| 273 | /* Ran out of polynomials */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 274 | PyErr_NoMemory(); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 275 | return -1; |
| 276 | } |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 277 | if (newsize > mp->ma_used*2) { |
| 278 | newpoly = polys[i]; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 279 | break; |
| 280 | } |
| 281 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 282 | newtable = (dictentry *) calloc(sizeof(dictentry), newsize); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 283 | if (newtable == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 284 | PyErr_NoMemory(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 285 | return -1; |
| 286 | } |
| 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++) { |
| 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 | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 304 | PyMem_XDEL(oldtable); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 305 | return 0; |
| 306 | } |
| 307 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 308 | PyObject * |
| 309 | PyDict_GetItem(op, key) |
| 310 | PyObject *op; |
| 311 | PyObject *key; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 312 | { |
| 313 | long hash; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 314 | if (!PyDict_Check(op)) { |
| 315 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 316 | return NULL; |
| 317 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 318 | if (((dictobject *)op)->ma_table == NULL) |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 319 | return NULL; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 320 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 321 | if (!PyString_Check(key) || |
| 322 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 323 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 324 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 325 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 326 | if (hash == -1) |
| 327 | return NULL; |
| 328 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 329 | return lookdict((dictobject *)op, key, hash) -> me_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 333 | PyDict_SetItem(op, key, value) |
| 334 | register PyObject *op; |
| 335 | PyObject *key; |
| 336 | PyObject *value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 337 | { |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 338 | register dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 339 | register long hash; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 340 | if (!PyDict_Check(op)) { |
| 341 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 342 | return -1; |
| 343 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 344 | mp = (dictobject *)op; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 345 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 346 | if (PyString_Check(key)) { |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 347 | #ifdef INTERN_STRINGS |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 348 | if (((PyStringObject *)key)->ob_sinterned != NULL) { |
| 349 | key = ((PyStringObject *)key)->ob_sinterned; |
| 350 | hash = ((PyStringObject *)key)->ob_shash; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 351 | } |
| 352 | else |
| 353 | #endif |
| 354 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 355 | hash = ((PyStringObject *)key)->ob_shash; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 356 | if (hash == -1) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 357 | hash = PyObject_Hash(key); |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | else |
| 361 | #endif |
| 362 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 363 | hash = PyObject_Hash(key); |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 364 | if (hash == -1) |
| 365 | return -1; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 366 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 367 | /* if fill >= 2/3 size, resize */ |
| 368 | if (mp->ma_fill*3 >= mp->ma_size*2) { |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 369 | if (dictresize(mp) != 0) { |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 370 | if (mp->ma_fill+1 > mp->ma_size) |
| 371 | return -1; |
| 372 | } |
| 373 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 374 | Py_INCREF(value); |
| 375 | Py_INCREF(key); |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 376 | insertdict(mp, key, hash, value); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 381 | PyDict_DelItem(op, key) |
| 382 | PyObject *op; |
| 383 | PyObject *key; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 384 | { |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 385 | register dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 386 | register long hash; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 387 | register dictentry *ep; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 388 | PyObject *old_value, *old_key; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 389 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 390 | if (!PyDict_Check(op)) { |
| 391 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 392 | return -1; |
| 393 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 394 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 395 | if (!PyString_Check(key) || |
| 396 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 397 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 398 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 399 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 400 | if (hash == -1) |
| 401 | return -1; |
| 402 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 403 | mp = (dictobject *)op; |
| 404 | if (((dictobject *)op)->ma_table == NULL) |
Guido van Rossum | efc8713 | 1995-01-02 19:42:39 +0000 | [diff] [blame] | 405 | goto empty; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 406 | ep = lookdict(mp, key, hash); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 407 | if (ep->me_value == NULL) { |
Guido van Rossum | efc8713 | 1995-01-02 19:42:39 +0000 | [diff] [blame] | 408 | empty: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 409 | PyErr_SetObject(PyExc_KeyError, key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 410 | return -1; |
| 411 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 412 | old_key = ep->me_key; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 413 | Py_INCREF(dummy); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 414 | ep->me_key = dummy; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 415 | old_value = ep->me_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 416 | ep->me_value = NULL; |
| 417 | mp->ma_used--; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 418 | Py_DECREF(old_value); |
| 419 | Py_DECREF(old_key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 423 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 424 | PyDict_Clear(op) |
| 425 | PyObject *op; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 426 | { |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 427 | int i, n; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 428 | register dictentry *table; |
| 429 | dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 430 | if (!PyDict_Check(op)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 431 | return; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 432 | mp = (dictobject *)op; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 433 | table = mp->ma_table; |
| 434 | if (table == NULL) |
| 435 | return; |
| 436 | n = mp->ma_size; |
| 437 | mp->ma_size = mp->ma_used = mp->ma_fill = 0; |
| 438 | mp->ma_table = NULL; |
| 439 | for (i = 0; i < n; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 440 | Py_XDECREF(table[i].me_key); |
| 441 | Py_XDECREF(table[i].me_value); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 442 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 443 | PyMem_DEL(table); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 446 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 447 | PyDict_Next(op, ppos, pkey, pvalue) |
| 448 | PyObject *op; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 449 | int *ppos; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 450 | PyObject **pkey; |
| 451 | PyObject **pvalue; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 452 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 453 | int i; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 454 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 455 | if (!PyDict_Check(op)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 456 | return 0; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 457 | mp = (dictobject *)op; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 458 | i = *ppos; |
| 459 | if (i < 0) |
| 460 | return 0; |
| 461 | while (i < mp->ma_size && mp->ma_table[i].me_value == NULL) |
| 462 | i++; |
| 463 | *ppos = i+1; |
| 464 | if (i >= mp->ma_size) |
| 465 | return 0; |
| 466 | if (pkey) |
| 467 | *pkey = mp->ma_table[i].me_key; |
| 468 | if (pvalue) |
| 469 | *pvalue = mp->ma_table[i].me_value; |
| 470 | return 1; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /* Methods */ |
| 474 | |
| 475 | static void |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 476 | dict_dealloc(mp) |
| 477 | register dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 478 | { |
| 479 | register int i; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 480 | register dictentry *ep; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 481 | for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) { |
| 482 | if (ep->me_key != NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 483 | Py_DECREF(ep->me_key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 484 | if (ep->me_value != NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 485 | Py_DECREF(ep->me_value); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 486 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 487 | PyMem_XDEL(mp->ma_table); |
| 488 | PyMem_DEL(mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | static int |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 492 | dict_print(mp, fp, flags) |
| 493 | register dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 494 | register FILE *fp; |
| 495 | register int flags; |
| 496 | { |
| 497 | register int i; |
| 498 | register int any; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 499 | register dictentry *ep; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 500 | fprintf(fp, "{"); |
| 501 | any = 0; |
| 502 | for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) { |
| 503 | if (ep->me_value != NULL) { |
| 504 | if (any++ > 0) |
| 505 | fprintf(fp, ", "); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 506 | if (PyObject_Print((PyObject *)ep->me_key, fp, 0) != 0) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 507 | return -1; |
| 508 | fprintf(fp, ": "); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 509 | if (PyObject_Print(ep->me_value, fp, 0) != 0) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 510 | return -1; |
| 511 | } |
| 512 | } |
| 513 | fprintf(fp, "}"); |
| 514 | return 0; |
| 515 | } |
| 516 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 517 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 518 | dict_repr(mp) |
| 519 | dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 520 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 521 | auto PyObject *v; |
| 522 | PyObject *sepa, *colon; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 523 | register int i; |
| 524 | register int any; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 525 | register dictentry *ep; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 526 | v = PyString_FromString("{"); |
| 527 | sepa = PyString_FromString(", "); |
| 528 | colon = PyString_FromString(": "); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 529 | any = 0; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 530 | 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] | 531 | if (ep->me_value != NULL) { |
| 532 | if (any++) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 533 | PyString_Concat(&v, sepa); |
| 534 | PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_key)); |
| 535 | PyString_Concat(&v, colon); |
| 536 | PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_value)); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 537 | } |
| 538 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 539 | PyString_ConcatAndDel(&v, PyString_FromString("}")); |
| 540 | Py_XDECREF(sepa); |
| 541 | Py_XDECREF(colon); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 542 | return v; |
| 543 | } |
| 544 | |
| 545 | static int |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 546 | dict_length(mp) |
| 547 | dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 548 | { |
| 549 | return mp->ma_used; |
| 550 | } |
| 551 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 552 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 553 | dict_subscript(mp, key) |
| 554 | dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 555 | register PyObject *key; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 556 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 557 | PyObject *v; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 558 | long hash; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 559 | if (mp->ma_table == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 560 | PyErr_SetObject(PyExc_KeyError, key); |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 561 | return NULL; |
| 562 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 563 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 564 | if (!PyString_Check(key) || |
| 565 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 566 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 567 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 568 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 569 | if (hash == -1) |
| 570 | return NULL; |
| 571 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 572 | v = lookdict(mp, key, hash) -> me_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 573 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 574 | PyErr_SetObject(PyExc_KeyError, key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 575 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 576 | Py_INCREF(v); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 577 | return v; |
| 578 | } |
| 579 | |
| 580 | static int |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 581 | dict_ass_sub(mp, v, w) |
| 582 | dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 583 | PyObject *v, *w; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 584 | { |
| 585 | if (w == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 586 | return PyDict_DelItem((PyObject *)mp, v); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 587 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 588 | return PyDict_SetItem((PyObject *)mp, v, w); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 591 | static PyMappingMethods dict_as_mapping = { |
| 592 | (inquiry)dict_length, /*mp_length*/ |
| 593 | (binaryfunc)dict_subscript, /*mp_subscript*/ |
| 594 | (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 595 | }; |
| 596 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 597 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 598 | dict_keys(mp, args) |
| 599 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 600 | PyObject *args; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 601 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 602 | register PyObject *v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 603 | register int i, j; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 604 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 605 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 606 | v = PyList_New(mp->ma_used); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 607 | if (v == NULL) |
| 608 | return NULL; |
| 609 | for (i = 0, j = 0; i < mp->ma_size; i++) { |
| 610 | if (mp->ma_table[i].me_value != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 611 | PyObject *key = mp->ma_table[i].me_key; |
| 612 | Py_INCREF(key); |
| 613 | PyList_SetItem(v, j, key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 614 | j++; |
| 615 | } |
| 616 | } |
| 617 | return v; |
| 618 | } |
| 619 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 620 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 621 | dict_values(mp, args) |
| 622 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 623 | PyObject *args; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 624 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 625 | register PyObject *v; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 626 | register int i, j; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 627 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 628 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 629 | v = PyList_New(mp->ma_used); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 630 | if (v == NULL) |
| 631 | return NULL; |
| 632 | for (i = 0, j = 0; i < mp->ma_size; i++) { |
| 633 | if (mp->ma_table[i].me_value != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 634 | PyObject *value = mp->ma_table[i].me_value; |
| 635 | Py_INCREF(value); |
| 636 | PyList_SetItem(v, j, value); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 637 | j++; |
| 638 | } |
| 639 | } |
| 640 | return v; |
| 641 | } |
| 642 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 643 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 644 | dict_items(mp, args) |
| 645 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 646 | PyObject *args; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 647 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 648 | register PyObject *v; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 649 | register int i, j; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 650 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 651 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 652 | v = PyList_New(mp->ma_used); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 653 | if (v == NULL) |
| 654 | return NULL; |
| 655 | for (i = 0, j = 0; i < mp->ma_size; i++) { |
| 656 | if (mp->ma_table[i].me_value != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 657 | PyObject *key = mp->ma_table[i].me_key; |
| 658 | PyObject *value = mp->ma_table[i].me_value; |
| 659 | PyObject *item = PyTuple_New(2); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 660 | if (item == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 661 | Py_DECREF(v); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 662 | return NULL; |
| 663 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 664 | Py_INCREF(key); |
| 665 | PyTuple_SetItem(item, 0, key); |
| 666 | Py_INCREF(value); |
| 667 | PyTuple_SetItem(item, 1, value); |
| 668 | PyList_SetItem(v, j, item); |
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 | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 675 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 676 | PyDict_Size(mp) |
| 677 | PyObject *mp; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 678 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 679 | if (mp == NULL || !PyDict_Check(mp)) { |
| 680 | PyErr_BadInternalCall(); |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 681 | return 0; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 682 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 683 | return ((dictobject *)mp)->ma_used; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 686 | PyObject * |
| 687 | PyDict_Keys(mp) |
| 688 | PyObject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 689 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 690 | if (mp == NULL || !PyDict_Check(mp)) { |
| 691 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 692 | return NULL; |
| 693 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 694 | return dict_keys((dictobject *)mp, (PyObject *)NULL); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 697 | PyObject * |
| 698 | PyDict_Values(mp) |
| 699 | PyObject *mp; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 700 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 701 | if (mp == NULL || !PyDict_Check(mp)) { |
| 702 | PyErr_BadInternalCall(); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 703 | return NULL; |
| 704 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 705 | return dict_values((dictobject *)mp, (PyObject *)NULL); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 708 | PyObject * |
| 709 | PyDict_Items(mp) |
| 710 | PyObject *mp; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 711 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 712 | if (mp == NULL || !PyDict_Check(mp)) { |
| 713 | PyErr_BadInternalCall(); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 714 | return NULL; |
| 715 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 716 | return dict_items((dictobject *)mp, (PyObject *)NULL); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 719 | #define NEWCMP |
| 720 | |
| 721 | #ifdef NEWCMP |
| 722 | |
| 723 | /* Subroutine which returns the smallest key in a for which b's value |
| 724 | is different or absent. The value is returned too, through the |
| 725 | pval argument. No reference counts are incremented. */ |
| 726 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 727 | static PyObject * |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 728 | characterize(a, b, pval) |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 729 | dictobject *a; |
| 730 | dictobject *b; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 731 | PyObject **pval; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 732 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 733 | PyObject *diff = NULL; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 734 | int i; |
| 735 | |
| 736 | *pval = NULL; |
| 737 | for (i = 0; i < a->ma_size; i++) { |
| 738 | if (a->ma_table[i].me_value != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 739 | PyObject *key = a->ma_table[i].me_key; |
| 740 | PyObject *aval, *bval; |
| 741 | if (diff != NULL && PyObject_Compare(key, diff) > 0) |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 742 | continue; |
| 743 | aval = a->ma_table[i].me_value; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 744 | bval = PyDict_GetItem((PyObject *)b, key); |
| 745 | if (bval == NULL || PyObject_Compare(aval, bval) != 0) |
| 746 | { |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 747 | diff = key; |
| 748 | *pval = aval; |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | return diff; |
| 753 | } |
| 754 | |
| 755 | static int |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 756 | dict_compare(a, b) |
| 757 | dictobject *a, *b; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 758 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 759 | PyObject *adiff, *bdiff, *aval, *bval; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 760 | int res; |
| 761 | |
| 762 | /* Compare lengths first */ |
| 763 | if (a->ma_used < b->ma_used) |
| 764 | return -1; /* a is shorter */ |
| 765 | else if (a->ma_used > b->ma_used) |
| 766 | return 1; /* b is shorter */ |
| 767 | /* Same length -- check all keys */ |
| 768 | adiff = characterize(a, b, &aval); |
| 769 | if (adiff == NULL) |
| 770 | return 0; /* a is a subset with the same length */ |
| 771 | bdiff = characterize(b, a, &bval); |
| 772 | /* bdiff == NULL would be impossible now */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 773 | res = PyObject_Compare(adiff, bdiff); |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 774 | if (res == 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 775 | res = PyObject_Compare(aval, bval); |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 776 | return res; |
| 777 | } |
| 778 | |
| 779 | #else /* !NEWCMP */ |
| 780 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 781 | static int |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 782 | dict_compare(a, b) |
| 783 | dictobject *a, *b; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 784 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 785 | PyObject *akeys, *bkeys; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 786 | int i, n, res; |
| 787 | if (a == b) |
| 788 | return 0; |
| 789 | if (a->ma_used == 0) { |
| 790 | if (b->ma_used != 0) |
| 791 | return -1; |
| 792 | else |
| 793 | return 0; |
| 794 | } |
| 795 | else { |
| 796 | if (b->ma_used == 0) |
| 797 | return 1; |
| 798 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 799 | akeys = dict_keys(a, (PyObject *)NULL); |
| 800 | bkeys = dict_keys(b, (PyObject *)NULL); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 801 | if (akeys == NULL || bkeys == NULL) { |
| 802 | /* Oops, out of memory -- what to do? */ |
| 803 | /* For now, sort on address! */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 804 | Py_XDECREF(akeys); |
| 805 | Py_XDECREF(bkeys); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 806 | if (a < b) |
| 807 | return -1; |
| 808 | else |
| 809 | return 1; |
| 810 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 811 | PyList_Sort(akeys); |
| 812 | PyList_Sort(bkeys); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 813 | n = a->ma_used < b->ma_used ? a->ma_used : b->ma_used; /* smallest */ |
| 814 | res = 0; |
| 815 | for (i = 0; i < n; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 816 | PyObject *akey, *bkey, *aval, *bval; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 817 | long ahash, bhash; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 818 | akey = PyList_GetItem(akeys, i); |
| 819 | bkey = PyList_GetItem(bkeys, i); |
| 820 | res = PyObject_Compare(akey, bkey); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 821 | if (res != 0) |
| 822 | break; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 823 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 824 | if (!PyString_Check(akey) || |
| 825 | (ahash = ((PyStringObject *) akey)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 826 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 827 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 828 | ahash = PyObject_Hash(akey); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 829 | if (ahash == -1) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 830 | PyErr_Clear(); /* Don't want errors here */ |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 831 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 832 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 833 | if (!PyString_Check(bkey) || |
| 834 | (bhash = ((PyStringObject *) bkey)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 835 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 836 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 837 | bhash = PyObject_Hash(bkey); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 838 | if (bhash == -1) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 839 | PyErr_Clear(); /* Don't want errors here */ |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 840 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 841 | aval = lookdict(a, akey, ahash) -> me_value; |
| 842 | bval = lookdict(b, bkey, bhash) -> me_value; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 843 | res = PyObject_Compare(aval, bval); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 844 | if (res != 0) |
| 845 | break; |
| 846 | } |
| 847 | if (res == 0) { |
| 848 | if (a->ma_used < b->ma_used) |
| 849 | res = -1; |
| 850 | else if (a->ma_used > b->ma_used) |
| 851 | res = 1; |
| 852 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 853 | Py_DECREF(akeys); |
| 854 | Py_DECREF(bkeys); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 855 | return res; |
| 856 | } |
| 857 | |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 858 | #endif /* !NEWCMP */ |
| 859 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 860 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 861 | dict_has_key(mp, args) |
| 862 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 863 | PyObject *args; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 864 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 865 | PyObject *key; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 866 | long hash; |
| 867 | register long ok; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 868 | if (!PyArg_Parse(args, "O", &key)) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 869 | return NULL; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 870 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 871 | if (!PyString_Check(key) || |
| 872 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 873 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 874 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 875 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 876 | if (hash == -1) |
| 877 | return NULL; |
| 878 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 879 | 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] | 880 | return PyInt_FromLong(ok); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 883 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 884 | dict_clear(mp, args) |
| 885 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 886 | PyObject *args; |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 887 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 888 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 889 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 890 | PyDict_Clear((PyObject *)mp); |
| 891 | Py_INCREF(Py_None); |
| 892 | return Py_None; |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 895 | static PyMethodDef mapp_methods[] = { |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 896 | {"clear", (PyCFunction)dict_clear}, |
| 897 | {"has_key", (PyCFunction)dict_has_key}, |
| 898 | {"items", (PyCFunction)dict_items}, |
| 899 | {"keys", (PyCFunction)dict_keys}, |
| 900 | {"values", (PyCFunction)dict_values}, |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 901 | {NULL, NULL} /* sentinel */ |
| 902 | }; |
| 903 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 904 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 905 | dict_getattr(mp, name) |
| 906 | dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 907 | char *name; |
| 908 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 909 | return Py_FindMethod(mapp_methods, (PyObject *)mp, name); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 912 | PyTypeObject PyDict_Type = { |
| 913 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 914 | 0, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 915 | "dictionary", |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 916 | sizeof(dictobject), |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 917 | 0, |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 918 | (destructor)dict_dealloc, /*tp_dealloc*/ |
| 919 | (printfunc)dict_print, /*tp_print*/ |
| 920 | (getattrfunc)dict_getattr, /*tp_getattr*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 921 | 0, /*tp_setattr*/ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 922 | (cmpfunc)dict_compare, /*tp_compare*/ |
| 923 | (reprfunc)dict_repr, /*tp_repr*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 924 | 0, /*tp_as_number*/ |
| 925 | 0, /*tp_as_sequence*/ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 926 | &dict_as_mapping, /*tp_as_mapping*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 927 | }; |
| 928 | |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 929 | /* For backward compatibility with old dictionary interface */ |
| 930 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 931 | PyObject * |
| 932 | PyDict_GetItemString(v, key) |
| 933 | PyObject *v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 934 | char *key; |
| 935 | { |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 936 | PyObject *kv, *rv; |
| 937 | kv = PyString_FromString(key); |
| 938 | if (kv == NULL) |
| 939 | return NULL; |
| 940 | PyString_InternInPlace(&kv); |
| 941 | rv = PyDict_GetItem(v, kv); |
| 942 | Py_DECREF(kv); |
| 943 | return rv; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 947 | PyDict_SetItemString(v, key, item) |
| 948 | PyObject *v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 949 | char *key; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 950 | PyObject *item; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 951 | { |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 952 | PyObject *kv; |
| 953 | int err; |
| 954 | kv = PyString_FromString(key); |
| 955 | if (kv == NULL) |
Guido van Rossum | 037b220 | 1997-05-20 18:35:19 +0000 | [diff] [blame] | 956 | return -1; |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 957 | PyString_InternInPlace(&kv); |
| 958 | err = PyDict_SetItem(v, kv, item); |
| 959 | Py_DECREF(kv); |
| 960 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 964 | PyDict_DelItemString(v, key) |
| 965 | PyObject *v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 966 | char *key; |
| 967 | { |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 968 | PyObject *kv; |
| 969 | int err; |
| 970 | kv = PyString_FromString(key); |
| 971 | if (kv == NULL) |
Guido van Rossum | 037b220 | 1997-05-20 18:35:19 +0000 | [diff] [blame] | 972 | return -1; |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 973 | PyString_InternInPlace(&kv); |
| 974 | err = PyDict_DelItem(v, kv); |
| 975 | Py_DECREF(kv); |
| 976 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 977 | } |