Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 2bc1379 | 1999-03-24 19:06:42 +0000 | [diff] [blame] | 2 | /* Dictionary object implementation using a hash table */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 5 | |
| 6 | |
| 7 | /* |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 8 | * MINSIZE is the minimum size of a dictionary. This many slots are |
| 9 | * allocated directly in the dict object (in the ma_smalltable member). |
| 10 | * This must be a power of 2, and the first entry in the polys[] vector must |
| 11 | * match. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 12 | */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 13 | #define MINSIZE 8 |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 14 | |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 15 | /* define this out if you don't want conversion statistics on exit */ |
| 16 | #undef SHOW_CONVERSION_COUNTS |
| 17 | |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 18 | /* |
| 19 | Table of irreducible polynomials to efficiently cycle through |
Tim Peters | ea8f2bf | 2000-12-13 01:02:46 +0000 | [diff] [blame] | 20 | GF(2^n)-{0}, 2<=n<=30. A table size is always a power of 2. |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 21 | For a table size of 2**i, the polys entry is 2**i + j for some j in 1 thru |
| 22 | 2**i-1 inclusive. The polys[] entries here happen to add in the smallest j |
| 23 | values "that work". Work means this: given any integer k in 1 thru 2**i-1 |
| 24 | inclusive, a poly works if & only if repeating this code: |
| 25 | print k |
| 26 | k <<= 1 |
| 27 | if k >= 2**i: |
| 28 | k ^= poly |
| 29 | prints every integer in 1 thru 2**i-1 inclusive exactly once before printing |
| 30 | k a second time. Theory can be used to find such polys efficiently, but the |
| 31 | operational defn. of "works" is sufficient to find them in reasonable time |
| 32 | via brute force program (hint: any poly that has an even number of 1 bits |
| 33 | cannot work; ditto any poly with low bit 0; exploit those). |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 34 | */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 36 | static long polys[] = { |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 37 | /* 4 + 3, */ /* first active entry if MINSIZE == 4 */ |
| 38 | 8 + 3, /* first active entry if MINSIZE == 8 */ |
Guido van Rossum | 9e5656c | 1997-01-29 04:45:16 +0000 | [diff] [blame] | 39 | 16 + 3, |
| 40 | 32 + 5, |
| 41 | 64 + 3, |
| 42 | 128 + 3, |
| 43 | 256 + 29, |
| 44 | 512 + 17, |
| 45 | 1024 + 9, |
| 46 | 2048 + 5, |
| 47 | 4096 + 83, |
| 48 | 8192 + 27, |
| 49 | 16384 + 43, |
| 50 | 32768 + 3, |
| 51 | 65536 + 45, |
| 52 | 131072 + 9, |
| 53 | 262144 + 39, |
| 54 | 524288 + 39, |
| 55 | 1048576 + 9, |
| 56 | 2097152 + 5, |
| 57 | 4194304 + 3, |
| 58 | 8388608 + 33, |
| 59 | 16777216 + 27, |
| 60 | 33554432 + 9, |
| 61 | 67108864 + 71, |
| 62 | 134217728 + 39, |
| 63 | 268435456 + 9, |
| 64 | 536870912 + 5, |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 65 | 1073741824 + 83 |
| 66 | /* 2147483648 + 9 -- if we ever boost this to unsigned long */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | /* Object used as dummy key to fill deleted entries */ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 70 | static PyObject *dummy; /* Initialized by first call to newdictobject() */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 71 | |
| 72 | /* |
Tim Peters | ea8f2bf | 2000-12-13 01:02:46 +0000 | [diff] [blame] | 73 | There are three kinds of slots in the table: |
| 74 | |
| 75 | 1. Unused. me_key == me_value == NULL |
| 76 | Does not hold an active (key, value) pair now and never did. Unused can |
| 77 | transition to Active upon key insertion. This is the only case in which |
| 78 | me_key is NULL, and is each slot's initial state. |
| 79 | |
| 80 | 2. Active. me_key != NULL and me_key != dummy and me_value != NULL |
| 81 | Holds an active (key, value) pair. Active can transition to Dummy upon |
| 82 | key deletion. This is the only case in which me_value != NULL. |
| 83 | |
Tim Peters | f1c7c88 | 2000-12-13 19:58:25 +0000 | [diff] [blame] | 84 | 3. Dummy. me_key == dummy and me_value == NULL |
Tim Peters | ea8f2bf | 2000-12-13 01:02:46 +0000 | [diff] [blame] | 85 | Previously held an active (key, value) pair, but that was deleted and an |
| 86 | active pair has not yet overwritten the slot. Dummy can transition to |
| 87 | Active upon key insertion. Dummy slots cannot be made Unused again |
| 88 | (cannot have me_key set to NULL), else the probe sequence in case of |
| 89 | collision would have no way to know they were once active. |
Tim Peters | f1c7c88 | 2000-12-13 19:58:25 +0000 | [diff] [blame] | 90 | |
| 91 | Note: .popitem() abuses the me_hash field of an Unused or Dummy slot to |
| 92 | hold a search finger. The me_hash field of Unused or Dummy slots has no |
| 93 | meaning otherwise. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 94 | */ |
| 95 | typedef struct { |
Tim Peters | ea8f2bf | 2000-12-13 01:02:46 +0000 | [diff] [blame] | 96 | long me_hash; /* cached hash code of me_key */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 97 | PyObject *me_key; |
| 98 | PyObject *me_value; |
Guido van Rossum | 3648884 | 1997-04-11 19:14:07 +0000 | [diff] [blame] | 99 | #ifdef USE_CACHE_ALIGNED |
| 100 | long aligner; |
| 101 | #endif |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 102 | } dictentry; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 103 | |
| 104 | /* |
Tim Peters | f1c7c88 | 2000-12-13 19:58:25 +0000 | [diff] [blame] | 105 | To ensure the lookup algorithm terminates, there must be at least one Unused |
Tim Peters | ea8f2bf | 2000-12-13 01:02:46 +0000 | [diff] [blame] | 106 | slot (NULL key) in the table. |
| 107 | The value ma_fill is the number of non-NULL keys (sum of Active and Dummy); |
| 108 | ma_used is the number of non-NULL, non-dummy keys (== the number of non-NULL |
| 109 | values == the number of Active items). |
| 110 | To avoid slowing down lookups on a near-full table, we resize the table when |
Tim Peters | 6783070 | 2001-03-21 19:23:56 +0000 | [diff] [blame] | 111 | it's two-thirds full. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 112 | */ |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 113 | typedef struct dictobject dictobject; |
| 114 | struct dictobject { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 115 | PyObject_HEAD |
Tim Peters | ea8f2bf | 2000-12-13 01:02:46 +0000 | [diff] [blame] | 116 | int ma_fill; /* # Active + # Dummy */ |
| 117 | int ma_used; /* # Active */ |
| 118 | int ma_size; /* total # slots in ma_table */ |
| 119 | int ma_poly; /* appopriate entry from polys vector */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 120 | /* ma_table points to ma_smalltable for small tables, else to |
| 121 | * additional malloc'ed memory. ma_table is never NULL! This rule |
| 122 | * saves repeated runtime null-tests in the workhorse getitem and |
| 123 | * setitem calls. |
| 124 | */ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 125 | dictentry *ma_table; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 126 | dictentry *(*ma_lookup)(dictobject *mp, PyObject *key, long hash); |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 127 | dictentry ma_smalltable[MINSIZE]; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | /* forward declarations */ |
| 131 | static dictentry * |
| 132 | lookdict_string(dictobject *mp, PyObject *key, long hash); |
| 133 | |
| 134 | #ifdef SHOW_CONVERSION_COUNTS |
| 135 | static long created = 0L; |
| 136 | static long converted = 0L; |
| 137 | |
| 138 | static void |
| 139 | show_counts(void) |
| 140 | { |
| 141 | fprintf(stderr, "created %ld string dicts\n", created); |
| 142 | fprintf(stderr, "converted %ld to normal dicts\n", converted); |
| 143 | fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created); |
| 144 | } |
| 145 | #endif |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 146 | |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 147 | /* Set dictobject* mp to empty but w/ MINSIZE slots, using ma_smalltable. */ |
| 148 | #define empty_to_minsize(mp) do { \ |
| 149 | memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \ |
| 150 | (mp)->ma_table = (mp)->ma_smalltable; \ |
| 151 | (mp)->ma_size = MINSIZE; \ |
| 152 | (mp)->ma_used = (mp)->ma_fill = 0; \ |
| 153 | (mp)->ma_poly = polys[0]; \ |
| 154 | assert(MINSIZE < (mp)->ma_poly && (mp)->ma_poly < MINSIZE*2); \ |
| 155 | } while(0) |
| 156 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 157 | PyObject * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 158 | PyDict_New(void) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 159 | { |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 160 | register dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 161 | if (dummy == NULL) { /* Auto-initialize dummy */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 162 | dummy = PyString_FromString("<dummy key>"); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 163 | if (dummy == NULL) |
| 164 | return NULL; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 165 | #ifdef SHOW_CONVERSION_COUNTS |
| 166 | Py_AtExit(show_counts); |
| 167 | #endif |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 168 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 169 | mp = PyObject_NEW(dictobject, &PyDict_Type); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 170 | if (mp == NULL) |
| 171 | return NULL; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 172 | empty_to_minsize(mp); |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 173 | mp->ma_lookup = lookdict_string; |
| 174 | #ifdef SHOW_CONVERSION_COUNTS |
| 175 | ++created; |
| 176 | #endif |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 177 | PyObject_GC_Init(mp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 178 | return (PyObject *)mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* |
| 182 | The basic lookup function used by all operations. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 183 | 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] | 184 | Open addressing is preferred over chaining since the link overhead for |
| 185 | chaining would be substantial (100% with typical malloc overhead). |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 186 | However, instead of going through the table at constant steps, we cycle |
Tim Peters | ea8f2bf | 2000-12-13 01:02:46 +0000 | [diff] [blame] | 187 | through the values of GF(2^n). This avoids modulo computations, being |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 188 | much cheaper on RISC machines, without leading to clustering. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 189 | |
Guido van Rossum | 2bc1379 | 1999-03-24 19:06:42 +0000 | [diff] [blame] | 190 | The initial probe index is computed as hash mod the table size. |
Tim Peters | ea8f2bf | 2000-12-13 01:02:46 +0000 | [diff] [blame] | 191 | Subsequent probe indices use the values of x^i in GF(2^n)-{0} as an offset, |
| 192 | where x is a root. The initial offset is derived from hash, too. |
Guido van Rossum | 2bc1379 | 1999-03-24 19:06:42 +0000 | [diff] [blame] | 193 | |
| 194 | All arithmetic on hash should ignore overflow. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 195 | |
| 196 | (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] | 197 | Jyrki Alakuijala and Vladimir Marangozov.) |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 198 | |
| 199 | This function must never return NULL; failures are indicated by returning |
| 200 | a dictentry* for which the me_value field is NULL. Exceptions are never |
| 201 | reported by this function, and outstanding exceptions are maintained. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 202 | */ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 203 | static dictentry * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 204 | lookdict(dictobject *mp, PyObject *key, register long hash) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 205 | { |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 206 | register int i; |
| 207 | register unsigned incr; |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 208 | register dictentry *freeslot; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 209 | register unsigned int mask = mp->ma_size-1; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 210 | dictentry *ep0 = mp->ma_table; |
| 211 | register dictentry *ep; |
Fred Drake | c88b99c | 2000-08-31 19:04:07 +0000 | [diff] [blame] | 212 | register int restore_error = 0; |
| 213 | register int checked_error = 0; |
| 214 | register int cmp; |
| 215 | PyObject *err_type, *err_value, *err_tb; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 216 | /* We must come up with (i, incr) such that 0 <= i < ma_size |
Tim Peters | ea8f2bf | 2000-12-13 01:02:46 +0000 | [diff] [blame] | 217 | and 0 < incr < ma_size and both are a function of hash. |
| 218 | i is the initial table index and incr the initial probe offset. */ |
Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame] | 219 | i = hash & mask; |
Guido van Rossum | efb4609 | 1997-01-29 15:53:56 +0000 | [diff] [blame] | 220 | ep = &ep0[i]; |
Guido van Rossum | c1c7b1a | 1998-10-06 16:01:14 +0000 | [diff] [blame] | 221 | if (ep->me_key == NULL || ep->me_key == key) |
Guido van Rossum | 7d18159 | 1997-01-16 21:06:45 +0000 | [diff] [blame] | 222 | return ep; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 223 | if (ep->me_key == dummy) |
Guido van Rossum | 7d18159 | 1997-01-16 21:06:45 +0000 | [diff] [blame] | 224 | freeslot = ep; |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 225 | else { |
Fred Drake | c88b99c | 2000-08-31 19:04:07 +0000 | [diff] [blame] | 226 | if (ep->me_hash == hash) { |
| 227 | /* error can't have been checked yet */ |
| 228 | checked_error = 1; |
| 229 | if (PyErr_Occurred()) { |
| 230 | restore_error = 1; |
| 231 | PyErr_Fetch(&err_type, &err_value, &err_tb); |
| 232 | } |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 233 | cmp = PyObject_RichCompareBool(ep->me_key, key, Py_EQ); |
| 234 | if (cmp > 0) { |
Fred Drake | c88b99c | 2000-08-31 19:04:07 +0000 | [diff] [blame] | 235 | if (restore_error) |
| 236 | PyErr_Restore(err_type, err_value, |
| 237 | err_tb); |
| 238 | return ep; |
| 239 | } |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 240 | else if (cmp < 0) |
| 241 | PyErr_Clear(); |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 242 | } |
| 243 | freeslot = NULL; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 244 | } |
Guido van Rossum | 2bc1379 | 1999-03-24 19:06:42 +0000 | [diff] [blame] | 245 | /* 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] | 246 | 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] | 247 | incr = (hash ^ ((unsigned long)hash >> 3)) & mask; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 248 | if (!incr) |
| 249 | incr = mask; |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 250 | /* In the loop, me_key == dummy is by far (factor of 100s) the |
| 251 | least likely outcome, so test for that last. */ |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 252 | for (;;) { |
Guido van Rossum | efb4609 | 1997-01-29 15:53:56 +0000 | [diff] [blame] | 253 | ep = &ep0[(i+incr)&mask]; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 254 | if (ep->me_key == NULL) { |
Fred Drake | c88b99c | 2000-08-31 19:04:07 +0000 | [diff] [blame] | 255 | if (restore_error) |
| 256 | PyErr_Restore(err_type, err_value, err_tb); |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 257 | return freeslot == NULL ? ep : freeslot; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 258 | } |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 259 | if (ep->me_key == key) { |
Fred Drake | c88b99c | 2000-08-31 19:04:07 +0000 | [diff] [blame] | 260 | if (restore_error) |
| 261 | PyErr_Restore(err_type, err_value, err_tb); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 262 | return ep; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 263 | } |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 264 | else if (ep->me_hash == hash && ep->me_key != dummy) { |
Fred Drake | c88b99c | 2000-08-31 19:04:07 +0000 | [diff] [blame] | 265 | if (!checked_error) { |
| 266 | checked_error = 1; |
| 267 | if (PyErr_Occurred()) { |
| 268 | restore_error = 1; |
| 269 | PyErr_Fetch(&err_type, &err_value, |
| 270 | &err_tb); |
| 271 | } |
| 272 | } |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 273 | cmp = PyObject_RichCompareBool(ep->me_key, key, Py_EQ); |
| 274 | if (cmp > 0) { |
Fred Drake | c88b99c | 2000-08-31 19:04:07 +0000 | [diff] [blame] | 275 | if (restore_error) |
| 276 | PyErr_Restore(err_type, err_value, |
| 277 | err_tb); |
| 278 | return ep; |
| 279 | } |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 280 | else if (cmp < 0) |
| 281 | PyErr_Clear(); |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 282 | } |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 283 | else if (ep->me_key == dummy && freeslot == NULL) |
| 284 | freeslot = ep; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 285 | /* Cycle through GF(2^n)-{0} */ |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 286 | incr <<= 1; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 287 | if (incr > mask) |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 288 | incr ^= mp->ma_poly; /* clears the highest bit */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | /* |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 293 | * Hacked up version of lookdict which can assume keys are always strings; |
| 294 | * this assumption allows testing for errors during PyObject_Compare() to |
| 295 | * be dropped; string-string comparisons never raise exceptions. This also |
| 296 | * means we don't need to go through PyObject_Compare(); we can always use |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 297 | * _PyString_Eq directly. |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 298 | * |
| 299 | * This really only becomes meaningful if proper error handling in lookdict() |
| 300 | * is too expensive. |
| 301 | */ |
| 302 | static dictentry * |
| 303 | lookdict_string(dictobject *mp, PyObject *key, register long hash) |
| 304 | { |
| 305 | register int i; |
| 306 | register unsigned incr; |
| 307 | register dictentry *freeslot; |
| 308 | register unsigned int mask = mp->ma_size-1; |
| 309 | dictentry *ep0 = mp->ma_table; |
| 310 | register dictentry *ep; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 311 | |
| 312 | /* make sure this function doesn't have to handle non-string keys */ |
| 313 | if (!PyString_Check(key)) { |
| 314 | #ifdef SHOW_CONVERSION_COUNTS |
| 315 | ++converted; |
| 316 | #endif |
| 317 | mp->ma_lookup = lookdict; |
| 318 | return lookdict(mp, key, hash); |
| 319 | } |
| 320 | /* We must come up with (i, incr) such that 0 <= i < ma_size |
| 321 | and 0 < incr < ma_size and both are a function of hash */ |
Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame] | 322 | i = hash & mask; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 323 | ep = &ep0[i]; |
| 324 | if (ep->me_key == NULL || ep->me_key == key) |
| 325 | return ep; |
| 326 | if (ep->me_key == dummy) |
| 327 | freeslot = ep; |
| 328 | else { |
| 329 | if (ep->me_hash == hash |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 330 | && _PyString_Eq(ep->me_key, key)) { |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 331 | return ep; |
| 332 | } |
| 333 | freeslot = NULL; |
| 334 | } |
| 335 | /* Derive incr from hash, just to make it more arbitrary. Note that |
| 336 | incr must not be 0, or we will get into an infinite loop.*/ |
| 337 | incr = (hash ^ ((unsigned long)hash >> 3)) & mask; |
| 338 | if (!incr) |
| 339 | incr = mask; |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 340 | /* In the loop, me_key == dummy is by far (factor of 100s) the |
| 341 | least likely outcome, so test for that last. */ |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 342 | for (;;) { |
| 343 | ep = &ep0[(i+incr)&mask]; |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 344 | if (ep->me_key == NULL) |
| 345 | return freeslot == NULL ? ep : freeslot; |
| 346 | if (ep->me_key == key |
| 347 | || (ep->me_hash == hash |
| 348 | && ep->me_key != dummy |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 349 | && _PyString_Eq(ep->me_key, key))) |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 350 | return ep; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 351 | if (ep->me_key == dummy && freeslot == NULL) |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 352 | freeslot = ep; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 353 | /* Cycle through GF(2^n)-{0} */ |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 354 | incr <<= 1; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 355 | if (incr > mask) |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 356 | incr ^= mp->ma_poly; /* clears the highest bit */ |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | /* |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 361 | Internal routine to insert a new item into the table. |
| 362 | Used both by the internal resize routine and by the public insert routine. |
| 363 | Eats a reference to key and one to value. |
| 364 | */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 365 | static void |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 366 | insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 367 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 368 | PyObject *old_value; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 369 | register dictentry *ep; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 370 | ep = (mp->ma_lookup)(mp, key, hash); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 371 | if (ep->me_value != NULL) { |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 372 | old_value = ep->me_value; |
| 373 | ep->me_value = value; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 374 | Py_DECREF(old_value); /* which **CAN** re-enter */ |
| 375 | Py_DECREF(key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 376 | } |
| 377 | else { |
| 378 | if (ep->me_key == NULL) |
| 379 | mp->ma_fill++; |
| 380 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 381 | Py_DECREF(ep->me_key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 382 | ep->me_key = key; |
| 383 | ep->me_hash = hash; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 384 | ep->me_value = value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 385 | mp->ma_used++; |
| 386 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /* |
| 390 | Restructure the table by allocating a new table and reinserting all |
| 391 | items again. When entries have been deleted, the new table may |
| 392 | actually be smaller than the old one. |
| 393 | */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 394 | static int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 395 | dictresize(dictobject *mp, int minused) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 396 | { |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 397 | int newsize, newpoly; |
| 398 | dictentry *oldtable, *newtable, *ep; |
| 399 | int i; |
| 400 | int is_oldtable_malloced; |
| 401 | dictentry small_copy[MINSIZE]; |
Tim Peters | 91a364d | 2001-05-19 07:04:38 +0000 | [diff] [blame] | 402 | |
| 403 | assert(minused >= 0); |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 404 | |
| 405 | /* Find the smallest table size > minused, and its poly[] entry. */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 406 | newpoly = 0; |
| 407 | newsize = MINSIZE; |
| 408 | for (i = 0; i < sizeof(polys)/sizeof(polys[0]); ++i) { |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 409 | if (newsize > minused) { |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 410 | newpoly = polys[i]; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 411 | break; |
| 412 | } |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 413 | newsize <<= 1; |
| 414 | if (newsize < 0) /* overflow */ |
| 415 | break; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 416 | } |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 417 | if (newpoly == 0) { |
| 418 | /* Ran out of polynomials or newsize overflowed. */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 419 | PyErr_NoMemory(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 420 | return -1; |
| 421 | } |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 422 | |
| 423 | /* Get space for a new table. */ |
| 424 | oldtable = mp->ma_table; |
| 425 | assert(oldtable != NULL); |
| 426 | is_oldtable_malloced = oldtable != mp->ma_smalltable; |
| 427 | |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 428 | if (newsize == MINSIZE) { |
Tim Peters | f8a548c | 2001-05-24 16:26:40 +0000 | [diff] [blame] | 429 | /* A large table is shrinking, or we can't get any smaller. */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 430 | newtable = mp->ma_smalltable; |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 431 | if (newtable == oldtable) { |
Tim Peters | f8a548c | 2001-05-24 16:26:40 +0000 | [diff] [blame] | 432 | if (mp->ma_fill == mp->ma_used) { |
| 433 | /* No dummies, so no point doing anything. */ |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 434 | return 0; |
Tim Peters | f8a548c | 2001-05-24 16:26:40 +0000 | [diff] [blame] | 435 | } |
| 436 | /* We're not going to resize it, but rebuild the |
| 437 | table anyway to purge old dummy entries. |
| 438 | Subtle: This is *necessary* if fill==size, |
| 439 | as lookdict needs at least one virgin slot to |
| 440 | terminate failing searches. If fill < size, it's |
| 441 | merely desirable, as dummies slow searches. */ |
| 442 | assert(mp->ma_fill > mp->ma_used); |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 443 | memcpy(small_copy, oldtable, sizeof(small_copy)); |
| 444 | oldtable = small_copy; |
| 445 | } |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 446 | } |
| 447 | else { |
| 448 | newtable = PyMem_NEW(dictentry, newsize); |
| 449 | if (newtable == NULL) { |
| 450 | PyErr_NoMemory(); |
| 451 | return -1; |
| 452 | } |
| 453 | } |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 454 | |
| 455 | /* Make the dict empty, using the new table. */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 456 | assert(newtable != oldtable); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 457 | mp->ma_table = newtable; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 458 | mp->ma_size = newsize; |
| 459 | memset(newtable, 0, sizeof(dictentry) * newsize); |
| 460 | mp->ma_poly = newpoly; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 461 | mp->ma_used = 0; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 462 | i = mp->ma_fill; |
| 463 | mp->ma_fill = 0; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 464 | |
Tim Peters | 1928314 | 2001-05-17 22:25:34 +0000 | [diff] [blame] | 465 | /* Copy the data over; this is refcount-neutral for active entries; |
| 466 | dummy entries aren't copied over, of course */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 467 | for (ep = oldtable; i > 0; ep++) { |
| 468 | if (ep->me_value != NULL) { /* active entry */ |
| 469 | --i; |
Tim Peters | 1928314 | 2001-05-17 22:25:34 +0000 | [diff] [blame] | 470 | insertdict(mp, ep->me_key, ep->me_hash, ep->me_value); |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 471 | } |
Tim Peters | 1928314 | 2001-05-17 22:25:34 +0000 | [diff] [blame] | 472 | else if (ep->me_key != NULL) { /* dummy entry */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 473 | --i; |
Tim Peters | 1928314 | 2001-05-17 22:25:34 +0000 | [diff] [blame] | 474 | assert(ep->me_key == dummy); |
| 475 | Py_DECREF(ep->me_key); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 476 | } |
Tim Peters | 1928314 | 2001-05-17 22:25:34 +0000 | [diff] [blame] | 477 | /* else key == value == NULL: nothing to do */ |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 480 | if (is_oldtable_malloced) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 481 | PyMem_DEL(oldtable); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 482 | return 0; |
| 483 | } |
| 484 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 485 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 486 | PyDict_GetItem(PyObject *op, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 487 | { |
| 488 | long hash; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 489 | dictobject *mp = (dictobject *)op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 490 | if (!PyDict_Check(op)) { |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 491 | return NULL; |
| 492 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 493 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 494 | if (!PyString_Check(key) || |
| 495 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 496 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 497 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 498 | hash = PyObject_Hash(key); |
Guido van Rossum | 474b19e | 1998-05-14 01:00:51 +0000 | [diff] [blame] | 499 | if (hash == -1) { |
| 500 | PyErr_Clear(); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 501 | return NULL; |
Guido van Rossum | 474b19e | 1998-05-14 01:00:51 +0000 | [diff] [blame] | 502 | } |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 503 | } |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 504 | return (mp->ma_lookup)(mp, key, hash)->me_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 507 | /* CAUTION: PyDict_SetItem() must guarantee that it won't resize the |
| 508 | * dictionary if it is merely replacing the value for an existing key. |
| 509 | * This is means that it's safe to loop over a dictionary with |
| 510 | * PyDict_Next() and occasionally replace a value -- but you can't |
| 511 | * insert new keys or remove them. |
| 512 | */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 513 | int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 514 | PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 515 | { |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 516 | register dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 517 | register long hash; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 518 | register int n_used; |
| 519 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 520 | if (!PyDict_Check(op)) { |
| 521 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 522 | return -1; |
| 523 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 524 | mp = (dictobject *)op; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 525 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 526 | if (PyString_Check(key)) { |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 527 | #ifdef INTERN_STRINGS |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 528 | if (((PyStringObject *)key)->ob_sinterned != NULL) { |
| 529 | key = ((PyStringObject *)key)->ob_sinterned; |
| 530 | hash = ((PyStringObject *)key)->ob_shash; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 531 | } |
| 532 | else |
| 533 | #endif |
| 534 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 535 | hash = ((PyStringObject *)key)->ob_shash; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 536 | if (hash == -1) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 537 | hash = PyObject_Hash(key); |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | else |
| 541 | #endif |
| 542 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 543 | hash = PyObject_Hash(key); |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 544 | if (hash == -1) |
| 545 | return -1; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 546 | } |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 547 | assert(mp->ma_fill < mp->ma_size); |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 548 | n_used = mp->ma_used; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 549 | Py_INCREF(value); |
| 550 | Py_INCREF(key); |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 551 | insertdict(mp, key, hash, value); |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 552 | /* If we added a key, we can safely resize. Otherwise skip this! |
| 553 | * If fill >= 2/3 size, adjust size. Normally, this doubles the |
| 554 | * size, but it's also possible for the dict to shrink (if ma_fill is |
| 555 | * much larger than ma_used, meaning a lot of dict keys have been |
| 556 | * deleted). |
| 557 | */ |
| 558 | if (mp->ma_used > n_used && mp->ma_fill*3 >= mp->ma_size*2) { |
| 559 | if (dictresize(mp, mp->ma_used*2) != 0) |
| 560 | return -1; |
| 561 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 566 | PyDict_DelItem(PyObject *op, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 567 | { |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 568 | register dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 569 | register long hash; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 570 | register dictentry *ep; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 571 | PyObject *old_value, *old_key; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 572 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 573 | if (!PyDict_Check(op)) { |
| 574 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 575 | return -1; |
| 576 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 577 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 578 | if (!PyString_Check(key) || |
| 579 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 580 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 581 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 582 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 583 | if (hash == -1) |
| 584 | return -1; |
| 585 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 586 | mp = (dictobject *)op; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 587 | ep = (mp->ma_lookup)(mp, key, hash); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 588 | if (ep->me_value == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 589 | PyErr_SetObject(PyExc_KeyError, key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 590 | return -1; |
| 591 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 592 | old_key = ep->me_key; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 593 | Py_INCREF(dummy); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 594 | ep->me_key = dummy; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 595 | old_value = ep->me_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 596 | ep->me_value = NULL; |
| 597 | mp->ma_used--; |
Tim Peters | ea8f2bf | 2000-12-13 01:02:46 +0000 | [diff] [blame] | 598 | Py_DECREF(old_value); |
| 599 | Py_DECREF(old_key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 600 | return 0; |
| 601 | } |
| 602 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 603 | void |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 604 | PyDict_Clear(PyObject *op) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 605 | { |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 606 | dictobject *mp; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 607 | dictentry *ep, *table; |
| 608 | int table_is_malloced; |
| 609 | int fill; |
| 610 | dictentry small_copy[MINSIZE]; |
| 611 | #ifdef Py_DEBUG |
| 612 | int i, n; |
| 613 | #endif |
| 614 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 615 | if (!PyDict_Check(op)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 616 | return; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 617 | mp = (dictobject *)op; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 618 | #ifdef Py_DEBUG |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 619 | n = mp->ma_size; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 620 | i = 0; |
| 621 | #endif |
| 622 | |
| 623 | table = mp->ma_table; |
| 624 | assert(table != NULL); |
| 625 | table_is_malloced = table != mp->ma_smalltable; |
| 626 | |
| 627 | /* This is delicate. During the process of clearing the dict, |
| 628 | * decrefs can cause the dict to mutate. To avoid fatal confusion |
| 629 | * (voice of experience), we have to make the dict empty before |
| 630 | * clearing the slots, and never refer to anything via mp->xxx while |
| 631 | * clearing. |
| 632 | */ |
| 633 | fill = mp->ma_fill; |
| 634 | if (table_is_malloced) |
| 635 | empty_to_minsize(mp); |
| 636 | |
| 637 | else if (fill > 0) { |
| 638 | /* It's a small table with something that needs to be cleared. |
| 639 | * Afraid the only safe way is to copy the dict entries into |
| 640 | * another small table first. |
| 641 | */ |
| 642 | memcpy(small_copy, table, sizeof(small_copy)); |
| 643 | table = small_copy; |
| 644 | empty_to_minsize(mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 645 | } |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 646 | /* else it's a small table that's already empty */ |
| 647 | |
| 648 | /* Now we can finally clear things. If C had refcounts, we could |
| 649 | * assert that the refcount on table is 1 now, i.e. that this function |
| 650 | * has unique access to it, so decref side-effects can't alter it. |
| 651 | */ |
| 652 | for (ep = table; fill > 0; ++ep) { |
| 653 | #ifdef Py_DEBUG |
| 654 | assert(i < n); |
| 655 | ++i; |
| 656 | #endif |
| 657 | if (ep->me_key) { |
| 658 | --fill; |
| 659 | Py_DECREF(ep->me_key); |
| 660 | Py_XDECREF(ep->me_value); |
| 661 | } |
| 662 | #ifdef Py_DEBUG |
| 663 | else |
| 664 | assert(ep->me_value == NULL); |
| 665 | #endif |
| 666 | } |
| 667 | |
| 668 | if (table_is_malloced) |
| 669 | PyMem_DEL(table); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Tim Peters | 6783070 | 2001-03-21 19:23:56 +0000 | [diff] [blame] | 672 | /* CAUTION: In general, it isn't safe to use PyDict_Next in a loop that |
| 673 | * mutates the dict. One exception: it is safe if the loop merely changes |
| 674 | * the values associated with the keys (but doesn't insert new keys or |
| 675 | * delete keys), via PyDict_SetItem(). |
| 676 | */ |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 677 | int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 678 | PyDict_Next(PyObject *op, int *ppos, PyObject **pkey, PyObject **pvalue) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 679 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 680 | int i; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 681 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 682 | if (!PyDict_Check(op)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 683 | return 0; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 684 | mp = (dictobject *)op; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 685 | i = *ppos; |
| 686 | if (i < 0) |
| 687 | return 0; |
| 688 | while (i < mp->ma_size && mp->ma_table[i].me_value == NULL) |
| 689 | i++; |
| 690 | *ppos = i+1; |
| 691 | if (i >= mp->ma_size) |
| 692 | return 0; |
| 693 | if (pkey) |
| 694 | *pkey = mp->ma_table[i].me_key; |
| 695 | if (pvalue) |
| 696 | *pvalue = mp->ma_table[i].me_value; |
| 697 | return 1; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /* Methods */ |
| 701 | |
| 702 | static void |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 703 | dict_dealloc(register dictobject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 704 | { |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 705 | register dictentry *ep; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 706 | int fill = mp->ma_fill; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 707 | Py_TRASHCAN_SAFE_BEGIN(mp) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 708 | PyObject_GC_Fini(mp); |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 709 | for (ep = mp->ma_table; fill > 0; ep++) { |
| 710 | if (ep->me_key) { |
| 711 | --fill; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 712 | Py_DECREF(ep->me_key); |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 713 | Py_XDECREF(ep->me_value); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 714 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 715 | } |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 716 | if (mp->ma_table != mp->ma_smalltable) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 717 | PyMem_DEL(mp->ma_table); |
Guido van Rossum | 4cc6ac7 | 2000-07-01 01:00:38 +0000 | [diff] [blame] | 718 | mp = (dictobject *) PyObject_AS_GC(mp); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 719 | PyObject_DEL(mp); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 720 | Py_TRASHCAN_SAFE_END(mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | static int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 724 | dict_print(register dictobject *mp, register FILE *fp, register int flags) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 725 | { |
| 726 | register int i; |
| 727 | register int any; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 728 | register dictentry *ep; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 729 | |
| 730 | i = Py_ReprEnter((PyObject*)mp); |
| 731 | if (i != 0) { |
| 732 | if (i < 0) |
| 733 | return i; |
| 734 | fprintf(fp, "{...}"); |
| 735 | return 0; |
| 736 | } |
| 737 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 738 | fprintf(fp, "{"); |
| 739 | any = 0; |
| 740 | for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) { |
| 741 | if (ep->me_value != NULL) { |
| 742 | if (any++ > 0) |
| 743 | fprintf(fp, ", "); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 744 | if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) { |
| 745 | Py_ReprLeave((PyObject*)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 746 | return -1; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 747 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 748 | fprintf(fp, ": "); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 749 | if (PyObject_Print(ep->me_value, fp, 0) != 0) { |
| 750 | Py_ReprLeave((PyObject*)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 751 | return -1; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 752 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | fprintf(fp, "}"); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 756 | Py_ReprLeave((PyObject*)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 757 | return 0; |
| 758 | } |
| 759 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 760 | static PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 761 | dict_repr(dictobject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 762 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 763 | auto PyObject *v; |
| 764 | PyObject *sepa, *colon; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 765 | register int i; |
| 766 | register int any; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 767 | register dictentry *ep; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 768 | |
| 769 | i = Py_ReprEnter((PyObject*)mp); |
| 770 | if (i != 0) { |
| 771 | if (i > 0) |
| 772 | return PyString_FromString("{...}"); |
| 773 | return NULL; |
| 774 | } |
| 775 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 776 | v = PyString_FromString("{"); |
| 777 | sepa = PyString_FromString(", "); |
| 778 | colon = PyString_FromString(": "); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 779 | any = 0; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 780 | 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] | 781 | if (ep->me_value != NULL) { |
| 782 | if (any++) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 783 | PyString_Concat(&v, sepa); |
| 784 | PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_key)); |
| 785 | PyString_Concat(&v, colon); |
| 786 | PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_value)); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 787 | } |
| 788 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 789 | PyString_ConcatAndDel(&v, PyString_FromString("}")); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 790 | Py_ReprLeave((PyObject*)mp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 791 | Py_XDECREF(sepa); |
| 792 | Py_XDECREF(colon); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 793 | return v; |
| 794 | } |
| 795 | |
| 796 | static int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 797 | dict_length(dictobject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 798 | { |
| 799 | return mp->ma_used; |
| 800 | } |
| 801 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 802 | static PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 803 | dict_subscript(dictobject *mp, register PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 804 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 805 | PyObject *v; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 806 | long hash; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 807 | assert(mp->ma_table != NULL); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 808 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 809 | if (!PyString_Check(key) || |
| 810 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 811 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 812 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 813 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 814 | if (hash == -1) |
| 815 | return NULL; |
| 816 | } |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 817 | v = (mp->ma_lookup)(mp, key, hash) -> me_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 818 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 819 | PyErr_SetObject(PyExc_KeyError, key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 820 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 821 | Py_INCREF(v); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 822 | return v; |
| 823 | } |
| 824 | |
| 825 | static int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 826 | dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 827 | { |
| 828 | if (w == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 829 | return PyDict_DelItem((PyObject *)mp, v); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 830 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 831 | return PyDict_SetItem((PyObject *)mp, v, w); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 834 | static PyMappingMethods dict_as_mapping = { |
| 835 | (inquiry)dict_length, /*mp_length*/ |
| 836 | (binaryfunc)dict_subscript, /*mp_subscript*/ |
| 837 | (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 838 | }; |
| 839 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 840 | static PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 841 | dict_keys(register dictobject *mp, PyObject *args) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 842 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 843 | register PyObject *v; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 844 | register int i, j, n; |
| 845 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 846 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 847 | return NULL; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 848 | again: |
| 849 | n = mp->ma_used; |
| 850 | v = PyList_New(n); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 851 | if (v == NULL) |
| 852 | return NULL; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 853 | if (n != mp->ma_used) { |
| 854 | /* Durnit. The allocations caused the dict to resize. |
| 855 | * Just start over, this shouldn't normally happen. |
| 856 | */ |
| 857 | Py_DECREF(v); |
| 858 | goto again; |
| 859 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 860 | for (i = 0, j = 0; i < mp->ma_size; i++) { |
| 861 | if (mp->ma_table[i].me_value != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 862 | PyObject *key = mp->ma_table[i].me_key; |
| 863 | Py_INCREF(key); |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 864 | PyList_SET_ITEM(v, j, key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 865 | j++; |
| 866 | } |
| 867 | } |
| 868 | return v; |
| 869 | } |
| 870 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 871 | static PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 872 | dict_values(register dictobject *mp, PyObject *args) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 873 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 874 | register PyObject *v; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 875 | register int i, j, n; |
| 876 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 877 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 878 | return NULL; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 879 | again: |
| 880 | n = mp->ma_used; |
| 881 | v = PyList_New(n); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 882 | if (v == NULL) |
| 883 | return NULL; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 884 | if (n != mp->ma_used) { |
| 885 | /* Durnit. The allocations caused the dict to resize. |
| 886 | * Just start over, this shouldn't normally happen. |
| 887 | */ |
| 888 | Py_DECREF(v); |
| 889 | goto again; |
| 890 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 891 | for (i = 0, j = 0; i < mp->ma_size; i++) { |
| 892 | if (mp->ma_table[i].me_value != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 893 | PyObject *value = mp->ma_table[i].me_value; |
| 894 | Py_INCREF(value); |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 895 | PyList_SET_ITEM(v, j, value); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 896 | j++; |
| 897 | } |
| 898 | } |
| 899 | return v; |
| 900 | } |
| 901 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 902 | static PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 903 | dict_items(register dictobject *mp, PyObject *args) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 904 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 905 | register PyObject *v; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 906 | register int i, j, n; |
| 907 | PyObject *item, *key, *value; |
| 908 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 909 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 910 | return NULL; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 911 | /* Preallocate the list of tuples, to avoid allocations during |
| 912 | * the loop over the items, which could trigger GC, which |
| 913 | * could resize the dict. :-( |
| 914 | */ |
| 915 | again: |
| 916 | n = mp->ma_used; |
| 917 | v = PyList_New(n); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 918 | if (v == NULL) |
| 919 | return NULL; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 920 | for (i = 0; i < n; i++) { |
| 921 | item = PyTuple_New(2); |
| 922 | if (item == NULL) { |
| 923 | Py_DECREF(v); |
| 924 | return NULL; |
| 925 | } |
| 926 | PyList_SET_ITEM(v, i, item); |
| 927 | } |
| 928 | if (n != mp->ma_used) { |
| 929 | /* Durnit. The allocations caused the dict to resize. |
| 930 | * Just start over, this shouldn't normally happen. |
| 931 | */ |
| 932 | Py_DECREF(v); |
| 933 | goto again; |
| 934 | } |
| 935 | /* Nothing we do below makes any function calls. */ |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 936 | for (i = 0, j = 0; i < mp->ma_size; i++) { |
| 937 | if (mp->ma_table[i].me_value != NULL) { |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 938 | key = mp->ma_table[i].me_key; |
| 939 | value = mp->ma_table[i].me_value; |
| 940 | item = PyList_GET_ITEM(v, j); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 941 | Py_INCREF(key); |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 942 | PyTuple_SET_ITEM(item, 0, key); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 943 | Py_INCREF(value); |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 944 | PyTuple_SET_ITEM(item, 1, value); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 945 | j++; |
| 946 | } |
| 947 | } |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 948 | assert(j == n); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 949 | return v; |
| 950 | } |
| 951 | |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 952 | static PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 953 | dict_update(register dictobject *mp, PyObject *args) |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 954 | { |
| 955 | register int i; |
| 956 | dictobject *other; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 957 | dictentry *entry; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 958 | if (!PyArg_Parse(args, "O!", &PyDict_Type, &other)) |
| 959 | return NULL; |
Jeremy Hylton | 1fb6088 | 2001-01-03 22:34:59 +0000 | [diff] [blame] | 960 | if (other == mp || other->ma_used == 0) |
| 961 | goto done; /* a.update(a) or a.update({}); nothing to do */ |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 962 | /* Do one big resize at the start, rather than incrementally |
| 963 | resizing as we insert new items. Expect that there will be |
| 964 | no (or few) overlapping keys. */ |
| 965 | if ((mp->ma_fill + other->ma_used)*3 >= mp->ma_size*2) { |
| 966 | if (dictresize(mp, (mp->ma_used + other->ma_used)*3/2) != 0) |
| 967 | return NULL; |
| 968 | } |
| 969 | for (i = 0; i < other->ma_size; i++) { |
| 970 | entry = &other->ma_table[i]; |
| 971 | if (entry->me_value != NULL) { |
| 972 | Py_INCREF(entry->me_key); |
| 973 | Py_INCREF(entry->me_value); |
| 974 | insertdict(mp, entry->me_key, entry->me_hash, |
| 975 | entry->me_value); |
| 976 | } |
| 977 | } |
| 978 | done: |
| 979 | Py_INCREF(Py_None); |
| 980 | return Py_None; |
| 981 | } |
| 982 | |
| 983 | static PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 984 | dict_copy(register dictobject *mp, PyObject *args) |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 985 | { |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 986 | if (!PyArg_Parse(args, "")) |
| 987 | return NULL; |
| 988 | return PyDict_Copy((PyObject*)mp); |
| 989 | } |
| 990 | |
| 991 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 992 | PyDict_Copy(PyObject *o) |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 993 | { |
| 994 | register dictobject *mp; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 995 | register int i; |
| 996 | dictobject *copy; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 997 | dictentry *entry; |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 998 | |
| 999 | if (o == NULL || !PyDict_Check(o)) { |
| 1000 | PyErr_BadInternalCall(); |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1001 | return NULL; |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 1002 | } |
| 1003 | mp = (dictobject *)o; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1004 | copy = (dictobject *)PyDict_New(); |
| 1005 | if (copy == NULL) |
| 1006 | return NULL; |
| 1007 | if (mp->ma_used > 0) { |
| 1008 | if (dictresize(copy, mp->ma_used*3/2) != 0) |
| 1009 | return NULL; |
| 1010 | for (i = 0; i < mp->ma_size; i++) { |
| 1011 | entry = &mp->ma_table[i]; |
| 1012 | if (entry->me_value != NULL) { |
| 1013 | Py_INCREF(entry->me_key); |
| 1014 | Py_INCREF(entry->me_value); |
| 1015 | insertdict(copy, entry->me_key, entry->me_hash, |
| 1016 | entry->me_value); |
| 1017 | } |
| 1018 | } |
| 1019 | } |
| 1020 | return (PyObject *)copy; |
| 1021 | } |
| 1022 | |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 1023 | int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1024 | PyDict_Size(PyObject *mp) |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 1025 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1026 | if (mp == NULL || !PyDict_Check(mp)) { |
| 1027 | PyErr_BadInternalCall(); |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 1028 | return 0; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 1029 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1030 | return ((dictobject *)mp)->ma_used; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1033 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1034 | PyDict_Keys(PyObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1035 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1036 | if (mp == NULL || !PyDict_Check(mp)) { |
| 1037 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1038 | return NULL; |
| 1039 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1040 | return dict_keys((dictobject *)mp, (PyObject *)NULL); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1043 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1044 | PyDict_Values(PyObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1045 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1046 | if (mp == NULL || !PyDict_Check(mp)) { |
| 1047 | PyErr_BadInternalCall(); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1048 | return NULL; |
| 1049 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1050 | return dict_values((dictobject *)mp, (PyObject *)NULL); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1053 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1054 | PyDict_Items(PyObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1055 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1056 | if (mp == NULL || !PyDict_Check(mp)) { |
| 1057 | PyErr_BadInternalCall(); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1058 | return NULL; |
| 1059 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1060 | return dict_items((dictobject *)mp, (PyObject *)NULL); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1063 | /* Subroutine which returns the smallest key in a for which b's value |
| 1064 | is different or absent. The value is returned too, through the |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1065 | pval argument. Both are NULL if no key in a is found for which b's status |
| 1066 | differs. The refcounts on (and only on) non-NULL *pval and function return |
| 1067 | values must be decremented by the caller (characterize() increments them |
| 1068 | to ensure that mutating comparison and PyDict_GetItem calls can't delete |
| 1069 | them before the caller is done looking at them). */ |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1070 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1071 | static PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1072 | characterize(dictobject *a, dictobject *b, PyObject **pval) |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1073 | { |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1074 | PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */ |
| 1075 | PyObject *aval = NULL; /* a[akey] */ |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 1076 | int i, cmp; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1077 | |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1078 | for (i = 0; i < a->ma_size; i++) { |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1079 | PyObject *thiskey, *thisaval, *thisbval; |
| 1080 | if (a->ma_table[i].me_value == NULL) |
| 1081 | continue; |
| 1082 | thiskey = a->ma_table[i].me_key; |
| 1083 | Py_INCREF(thiskey); /* keep alive across compares */ |
| 1084 | if (akey != NULL) { |
| 1085 | cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT); |
| 1086 | if (cmp < 0) { |
| 1087 | Py_DECREF(thiskey); |
| 1088 | goto Fail; |
| 1089 | } |
| 1090 | if (cmp > 0 || |
| 1091 | i >= a->ma_size || |
| 1092 | a->ma_table[i].me_value == NULL) |
| 1093 | { |
| 1094 | /* Not the *smallest* a key; or maybe it is |
| 1095 | * but the compare shrunk the dict so we can't |
| 1096 | * find its associated value anymore; or |
| 1097 | * maybe it is but the compare deleted the |
| 1098 | * a[thiskey] entry. |
| 1099 | */ |
| 1100 | Py_DECREF(thiskey); |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1101 | continue; |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 1102 | } |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */ |
| 1106 | thisaval = a->ma_table[i].me_value; |
| 1107 | assert(thisaval); |
| 1108 | Py_INCREF(thisaval); /* keep alive */ |
| 1109 | thisbval = PyDict_GetItem((PyObject *)b, thiskey); |
| 1110 | if (thisbval == NULL) |
| 1111 | cmp = 0; |
| 1112 | else { |
| 1113 | /* both dicts have thiskey: same values? */ |
| 1114 | cmp = PyObject_RichCompareBool( |
| 1115 | thisaval, thisbval, Py_EQ); |
| 1116 | if (cmp < 0) { |
| 1117 | Py_DECREF(thiskey); |
| 1118 | Py_DECREF(thisaval); |
| 1119 | goto Fail; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1120 | } |
| 1121 | } |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1122 | if (cmp == 0) { |
| 1123 | /* New winner. */ |
| 1124 | Py_XDECREF(akey); |
| 1125 | Py_XDECREF(aval); |
| 1126 | akey = thiskey; |
| 1127 | aval = thisaval; |
| 1128 | } |
| 1129 | else { |
| 1130 | Py_DECREF(thiskey); |
| 1131 | Py_DECREF(thisaval); |
| 1132 | } |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1133 | } |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1134 | *pval = aval; |
| 1135 | return akey; |
| 1136 | |
| 1137 | Fail: |
| 1138 | Py_XDECREF(akey); |
| 1139 | Py_XDECREF(aval); |
| 1140 | *pval = NULL; |
| 1141 | return NULL; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | static int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1145 | dict_compare(dictobject *a, dictobject *b) |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1146 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1147 | PyObject *adiff, *bdiff, *aval, *bval; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1148 | int res; |
| 1149 | |
| 1150 | /* Compare lengths first */ |
| 1151 | if (a->ma_used < b->ma_used) |
| 1152 | return -1; /* a is shorter */ |
| 1153 | else if (a->ma_used > b->ma_used) |
| 1154 | return 1; /* b is shorter */ |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1155 | |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1156 | /* Same length -- check all keys */ |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1157 | bdiff = bval = NULL; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1158 | adiff = characterize(a, b, &aval); |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1159 | if (adiff == NULL) { |
| 1160 | assert(!aval); |
Tim Peters | 3918fb2 | 2001-05-10 18:58:31 +0000 | [diff] [blame] | 1161 | /* Either an error, or a is a subset with the same length so |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1162 | * must be equal. |
| 1163 | */ |
| 1164 | res = PyErr_Occurred() ? -1 : 0; |
| 1165 | goto Finished; |
| 1166 | } |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1167 | bdiff = characterize(b, a, &bval); |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1168 | if (bdiff == NULL && PyErr_Occurred()) { |
| 1169 | assert(!bval); |
| 1170 | res = -1; |
| 1171 | goto Finished; |
| 1172 | } |
| 1173 | res = 0; |
| 1174 | if (bdiff) { |
| 1175 | /* bdiff == NULL "should be" impossible now, but perhaps |
| 1176 | * the last comparison done by the characterize() on a had |
| 1177 | * the side effect of making the dicts equal! |
| 1178 | */ |
| 1179 | res = PyObject_Compare(adiff, bdiff); |
| 1180 | } |
| 1181 | if (res == 0 && bval != NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1182 | res = PyObject_Compare(aval, bval); |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1183 | |
| 1184 | Finished: |
| 1185 | Py_XDECREF(adiff); |
| 1186 | Py_XDECREF(bdiff); |
| 1187 | Py_XDECREF(aval); |
| 1188 | Py_XDECREF(bval); |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1189 | return res; |
| 1190 | } |
| 1191 | |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1192 | /* Return 1 if dicts equal, 0 if not, -1 if error. |
| 1193 | * Gets out as soon as any difference is detected. |
| 1194 | * Uses only Py_EQ comparison. |
| 1195 | */ |
| 1196 | static int |
| 1197 | dict_equal(dictobject *a, dictobject *b) |
| 1198 | { |
| 1199 | int i; |
| 1200 | |
| 1201 | if (a->ma_used != b->ma_used) |
| 1202 | /* can't be equal if # of entries differ */ |
| 1203 | return 0; |
| 1204 | |
| 1205 | /* Same # of entries -- check all of 'em. Exit early on any diff. */ |
| 1206 | for (i = 0; i < a->ma_size; i++) { |
| 1207 | PyObject *aval = a->ma_table[i].me_value; |
| 1208 | if (aval != NULL) { |
| 1209 | int cmp; |
| 1210 | PyObject *bval; |
| 1211 | PyObject *key = a->ma_table[i].me_key; |
| 1212 | /* temporarily bump aval's refcount to ensure it stays |
| 1213 | alive until we're done with it */ |
| 1214 | Py_INCREF(aval); |
| 1215 | bval = PyDict_GetItem((PyObject *)b, key); |
| 1216 | if (bval == NULL) { |
| 1217 | Py_DECREF(aval); |
| 1218 | return 0; |
| 1219 | } |
| 1220 | cmp = PyObject_RichCompareBool(aval, bval, Py_EQ); |
| 1221 | Py_DECREF(aval); |
| 1222 | if (cmp <= 0) /* error or not equal */ |
| 1223 | return cmp; |
| 1224 | } |
| 1225 | } |
| 1226 | return 1; |
| 1227 | } |
| 1228 | |
| 1229 | static PyObject * |
| 1230 | dict_richcompare(PyObject *v, PyObject *w, int op) |
| 1231 | { |
| 1232 | int cmp; |
| 1233 | PyObject *res; |
| 1234 | |
| 1235 | if (!PyDict_Check(v) || !PyDict_Check(w)) { |
| 1236 | res = Py_NotImplemented; |
| 1237 | } |
| 1238 | else if (op == Py_EQ || op == Py_NE) { |
| 1239 | cmp = dict_equal((dictobject *)v, (dictobject *)w); |
| 1240 | if (cmp < 0) |
| 1241 | return NULL; |
| 1242 | res = (cmp == (op == Py_EQ)) ? Py_True : Py_False; |
| 1243 | } |
Tim Peters | 4fa58bf | 2001-05-10 21:45:19 +0000 | [diff] [blame] | 1244 | else |
| 1245 | res = Py_NotImplemented; |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1246 | Py_INCREF(res); |
| 1247 | return res; |
| 1248 | } |
| 1249 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1250 | static PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1251 | dict_has_key(register dictobject *mp, PyObject *args) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1252 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1253 | PyObject *key; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1254 | long hash; |
| 1255 | register long ok; |
Fred Drake | 52fccfd | 2000-02-23 15:47:16 +0000 | [diff] [blame] | 1256 | if (!PyArg_ParseTuple(args, "O:has_key", &key)) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1257 | return NULL; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1258 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1259 | if (!PyString_Check(key) || |
| 1260 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1261 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 1262 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1263 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 1264 | if (hash == -1) |
| 1265 | return NULL; |
| 1266 | } |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 1267 | ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1268 | return PyInt_FromLong(ok); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1271 | static PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1272 | dict_get(register dictobject *mp, PyObject *args) |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1273 | { |
| 1274 | PyObject *key; |
Barry Warsaw | 320ac33 | 1997-10-20 17:26:25 +0000 | [diff] [blame] | 1275 | PyObject *failobj = Py_None; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1276 | PyObject *val = NULL; |
| 1277 | long hash; |
| 1278 | |
Fred Drake | 52fccfd | 2000-02-23 15:47:16 +0000 | [diff] [blame] | 1279 | if (!PyArg_ParseTuple(args, "O|O:get", &key, &failobj)) |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1280 | return NULL; |
| 1281 | |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1282 | #ifdef CACHE_HASH |
| 1283 | if (!PyString_Check(key) || |
| 1284 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
| 1285 | #endif |
| 1286 | { |
| 1287 | hash = PyObject_Hash(key); |
| 1288 | if (hash == -1) |
| 1289 | return NULL; |
| 1290 | } |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 1291 | val = (mp->ma_lookup)(mp, key, hash)->me_value; |
Barry Warsaw | 320ac33 | 1997-10-20 17:26:25 +0000 | [diff] [blame] | 1292 | |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1293 | if (val == NULL) |
| 1294 | val = failobj; |
| 1295 | Py_INCREF(val); |
| 1296 | return val; |
| 1297 | } |
| 1298 | |
| 1299 | |
| 1300 | static PyObject * |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 1301 | dict_setdefault(register dictobject *mp, PyObject *args) |
| 1302 | { |
| 1303 | PyObject *key; |
| 1304 | PyObject *failobj = Py_None; |
| 1305 | PyObject *val = NULL; |
| 1306 | long hash; |
| 1307 | |
| 1308 | if (!PyArg_ParseTuple(args, "O|O:setdefault", &key, &failobj)) |
| 1309 | return NULL; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 1310 | |
| 1311 | #ifdef CACHE_HASH |
| 1312 | if (!PyString_Check(key) || |
| 1313 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
| 1314 | #endif |
| 1315 | { |
| 1316 | hash = PyObject_Hash(key); |
| 1317 | if (hash == -1) |
| 1318 | return NULL; |
| 1319 | } |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 1320 | val = (mp->ma_lookup)(mp, key, hash)->me_value; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 1321 | if (val == NULL) { |
| 1322 | val = failobj; |
| 1323 | if (PyDict_SetItem((PyObject*)mp, key, failobj)) |
| 1324 | val = NULL; |
| 1325 | } |
| 1326 | Py_XINCREF(val); |
| 1327 | return val; |
| 1328 | } |
| 1329 | |
| 1330 | |
| 1331 | static PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1332 | dict_clear(register dictobject *mp, PyObject *args) |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 1333 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1334 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 1335 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1336 | PyDict_Clear((PyObject *)mp); |
| 1337 | Py_INCREF(Py_None); |
| 1338 | return Py_None; |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1341 | static PyObject * |
| 1342 | dict_popitem(dictobject *mp, PyObject *args) |
| 1343 | { |
| 1344 | int i = 0; |
| 1345 | dictentry *ep; |
| 1346 | PyObject *res; |
| 1347 | |
| 1348 | if (!PyArg_NoArgs(args)) |
| 1349 | return NULL; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1350 | /* Allocate the result tuple first. Believe it or not, |
| 1351 | * this allocation could trigger a garbage collection which |
| 1352 | * could resize the dict, which would invalidate the pointer |
| 1353 | * (ep) into the dict calculated below. |
| 1354 | * So we have to do this first. |
| 1355 | */ |
| 1356 | res = PyTuple_New(2); |
| 1357 | if (res == NULL) |
| 1358 | return NULL; |
Guido van Rossum | e04eaec | 2001-04-16 00:02:32 +0000 | [diff] [blame] | 1359 | if (mp->ma_used == 0) { |
| 1360 | Py_DECREF(res); |
| 1361 | PyErr_SetString(PyExc_KeyError, |
| 1362 | "popitem(): dictionary is empty"); |
| 1363 | return NULL; |
| 1364 | } |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1365 | /* Set ep to "the first" dict entry with a value. We abuse the hash |
| 1366 | * field of slot 0 to hold a search finger: |
| 1367 | * If slot 0 has a value, use slot 0. |
| 1368 | * Else slot 0 is being used to hold a search finger, |
| 1369 | * and we use its hash value as the first index to look. |
| 1370 | */ |
| 1371 | ep = &mp->ma_table[0]; |
| 1372 | if (ep->me_value == NULL) { |
| 1373 | i = (int)ep->me_hash; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 1374 | /* The hash field may be a real hash value, or it may be a |
| 1375 | * legit search finger, or it may be a once-legit search |
| 1376 | * finger that's out of bounds now because it wrapped around |
| 1377 | * or the table shrunk -- simply make sure it's in bounds now. |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1378 | */ |
| 1379 | if (i >= mp->ma_size || i < 1) |
| 1380 | i = 1; /* skip slot 0 */ |
| 1381 | while ((ep = &mp->ma_table[i])->me_value == NULL) { |
| 1382 | i++; |
| 1383 | if (i >= mp->ma_size) |
| 1384 | i = 1; |
| 1385 | } |
| 1386 | } |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1387 | PyTuple_SET_ITEM(res, 0, ep->me_key); |
| 1388 | PyTuple_SET_ITEM(res, 1, ep->me_value); |
| 1389 | Py_INCREF(dummy); |
| 1390 | ep->me_key = dummy; |
| 1391 | ep->me_value = NULL; |
| 1392 | mp->ma_used--; |
| 1393 | assert(mp->ma_table[0].me_value == NULL); |
| 1394 | mp->ma_table[0].me_hash = i + 1; /* next place to start */ |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1395 | return res; |
| 1396 | } |
| 1397 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1398 | static int |
| 1399 | dict_traverse(PyObject *op, visitproc visit, void *arg) |
| 1400 | { |
| 1401 | int i = 0, err; |
| 1402 | PyObject *pk; |
| 1403 | PyObject *pv; |
| 1404 | |
| 1405 | while (PyDict_Next(op, &i, &pk, &pv)) { |
| 1406 | err = visit(pk, arg); |
| 1407 | if (err) |
| 1408 | return err; |
| 1409 | err = visit(pv, arg); |
| 1410 | if (err) |
| 1411 | return err; |
| 1412 | } |
| 1413 | return 0; |
| 1414 | } |
| 1415 | |
| 1416 | static int |
| 1417 | dict_tp_clear(PyObject *op) |
| 1418 | { |
| 1419 | PyDict_Clear(op); |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 1423 | |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1424 | staticforward PyObject *dictiter_new(dictobject *, binaryfunc); |
| 1425 | |
| 1426 | static PyObject * |
| 1427 | select_key(PyObject *key, PyObject *value) |
| 1428 | { |
| 1429 | Py_INCREF(key); |
| 1430 | return key; |
| 1431 | } |
| 1432 | |
| 1433 | static PyObject * |
| 1434 | select_value(PyObject *key, PyObject *value) |
| 1435 | { |
| 1436 | Py_INCREF(value); |
| 1437 | return value; |
| 1438 | } |
| 1439 | |
| 1440 | static PyObject * |
| 1441 | select_item(PyObject *key, PyObject *value) |
| 1442 | { |
| 1443 | PyObject *res = PyTuple_New(2); |
| 1444 | |
| 1445 | if (res != NULL) { |
| 1446 | Py_INCREF(key); |
| 1447 | Py_INCREF(value); |
| 1448 | PyTuple_SET_ITEM(res, 0, key); |
| 1449 | PyTuple_SET_ITEM(res, 1, value); |
| 1450 | } |
| 1451 | return res; |
| 1452 | } |
| 1453 | |
| 1454 | static PyObject * |
| 1455 | dict_iterkeys(dictobject *dict, PyObject *args) |
| 1456 | { |
| 1457 | if (!PyArg_ParseTuple(args, "")) |
| 1458 | return NULL; |
| 1459 | return dictiter_new(dict, select_key); |
| 1460 | } |
| 1461 | |
| 1462 | static PyObject * |
| 1463 | dict_itervalues(dictobject *dict, PyObject *args) |
| 1464 | { |
| 1465 | if (!PyArg_ParseTuple(args, "")) |
| 1466 | return NULL; |
| 1467 | return dictiter_new(dict, select_value); |
| 1468 | } |
| 1469 | |
| 1470 | static PyObject * |
| 1471 | dict_iteritems(dictobject *dict, PyObject *args) |
| 1472 | { |
| 1473 | if (!PyArg_ParseTuple(args, "")) |
| 1474 | return NULL; |
| 1475 | return dictiter_new(dict, select_item); |
| 1476 | } |
| 1477 | |
| 1478 | |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 1479 | static char has_key__doc__[] = |
| 1480 | "D.has_key(k) -> 1 if D has a key k, else 0"; |
| 1481 | |
| 1482 | static char get__doc__[] = |
| 1483 | "D.get(k[,d]) -> D[k] if D.has_key(k), else d. d defaults to None."; |
| 1484 | |
| 1485 | static char setdefault_doc__[] = |
| 1486 | "D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if not D.has_key(k)"; |
| 1487 | |
| 1488 | static char popitem__doc__[] = |
| 1489 | "D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\ |
| 1490 | 2-tuple; but raise KeyError if D is empty"; |
| 1491 | |
| 1492 | static char keys__doc__[] = |
| 1493 | "D.keys() -> list of D's keys"; |
| 1494 | |
| 1495 | static char items__doc__[] = |
| 1496 | "D.items() -> list of D's (key, value) pairs, as 2-tuples"; |
| 1497 | |
| 1498 | static char values__doc__[] = |
| 1499 | "D.values() -> list of D's values"; |
| 1500 | |
| 1501 | static char update__doc__[] = |
| 1502 | "D.update(E) -> None. Update D from E: for k in E.keys(): D[k] = E[k]"; |
| 1503 | |
| 1504 | static char clear__doc__[] = |
| 1505 | "D.clear() -> None. Remove all items from D."; |
| 1506 | |
| 1507 | static char copy__doc__[] = |
| 1508 | "D.copy() -> a shallow copy of D"; |
| 1509 | |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1510 | static char iterkeys__doc__[] = |
| 1511 | "D.iterkeys() -> an iterator over the keys of D"; |
| 1512 | |
| 1513 | static char itervalues__doc__[] = |
| 1514 | "D.itervalues() -> an iterator over the values of D"; |
| 1515 | |
| 1516 | static char iteritems__doc__[] = |
| 1517 | "D.iteritems() -> an iterator over the (key, value) items of D"; |
| 1518 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1519 | static PyMethodDef mapp_methods[] = { |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 1520 | {"has_key", (PyCFunction)dict_has_key, METH_VARARGS, |
| 1521 | has_key__doc__}, |
| 1522 | {"get", (PyCFunction)dict_get, METH_VARARGS, |
| 1523 | get__doc__}, |
| 1524 | {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS, |
| 1525 | setdefault_doc__}, |
| 1526 | {"popitem", (PyCFunction)dict_popitem, METH_OLDARGS, |
| 1527 | popitem__doc__}, |
| 1528 | {"keys", (PyCFunction)dict_keys, METH_OLDARGS, |
| 1529 | keys__doc__}, |
| 1530 | {"items", (PyCFunction)dict_items, METH_OLDARGS, |
| 1531 | items__doc__}, |
| 1532 | {"values", (PyCFunction)dict_values, METH_OLDARGS, |
| 1533 | values__doc__}, |
| 1534 | {"update", (PyCFunction)dict_update, METH_OLDARGS, |
| 1535 | update__doc__}, |
| 1536 | {"clear", (PyCFunction)dict_clear, METH_OLDARGS, |
| 1537 | clear__doc__}, |
| 1538 | {"copy", (PyCFunction)dict_copy, METH_OLDARGS, |
| 1539 | copy__doc__}, |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1540 | {"iterkeys", (PyCFunction)dict_iterkeys, METH_VARARGS, |
| 1541 | iterkeys__doc__}, |
| 1542 | {"itervalues", (PyCFunction)dict_itervalues, METH_VARARGS, |
| 1543 | itervalues__doc__}, |
| 1544 | {"iteritems", (PyCFunction)dict_iteritems, METH_VARARGS, |
| 1545 | iteritems__doc__}, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 1546 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1547 | }; |
| 1548 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1549 | static PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1550 | dict_getattr(dictobject *mp, char *name) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1551 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1552 | return Py_FindMethod(mapp_methods, (PyObject *)mp, name); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 1555 | static int |
| 1556 | dict_contains(dictobject *mp, PyObject *key) |
| 1557 | { |
| 1558 | long hash; |
| 1559 | |
| 1560 | #ifdef CACHE_HASH |
| 1561 | if (!PyString_Check(key) || |
| 1562 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
| 1563 | #endif |
| 1564 | { |
| 1565 | hash = PyObject_Hash(key); |
| 1566 | if (hash == -1) |
| 1567 | return -1; |
| 1568 | } |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 1569 | return (mp->ma_lookup)(mp, key, hash)->me_value != NULL; |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 1572 | /* Hack to implement "key in dict" */ |
| 1573 | static PySequenceMethods dict_as_sequence = { |
| 1574 | 0, /* sq_length */ |
| 1575 | 0, /* sq_concat */ |
| 1576 | 0, /* sq_repeat */ |
| 1577 | 0, /* sq_item */ |
| 1578 | 0, /* sq_slice */ |
| 1579 | 0, /* sq_ass_item */ |
| 1580 | 0, /* sq_ass_slice */ |
| 1581 | (objobjproc)dict_contains, /* sq_contains */ |
| 1582 | 0, /* sq_inplace_concat */ |
| 1583 | 0, /* sq_inplace_repeat */ |
| 1584 | }; |
| 1585 | |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1586 | static PyObject * |
| 1587 | dict_iter(dictobject *dict) |
| 1588 | { |
| 1589 | return dictiter_new(dict, select_key); |
| 1590 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1591 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1592 | PyTypeObject PyDict_Type = { |
| 1593 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1594 | 0, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1595 | "dictionary", |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1596 | sizeof(dictobject) + PyGC_HEAD_SIZE, |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1597 | 0, |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 1598 | (destructor)dict_dealloc, /* tp_dealloc */ |
| 1599 | (printfunc)dict_print, /* tp_print */ |
| 1600 | (getattrfunc)dict_getattr, /* tp_getattr */ |
| 1601 | 0, /* tp_setattr */ |
Tim Peters | 4fa58bf | 2001-05-10 21:45:19 +0000 | [diff] [blame] | 1602 | (cmpfunc)dict_compare, /* tp_compare */ |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 1603 | (reprfunc)dict_repr, /* tp_repr */ |
| 1604 | 0, /* tp_as_number */ |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 1605 | &dict_as_sequence, /* tp_as_sequence */ |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 1606 | &dict_as_mapping, /* tp_as_mapping */ |
| 1607 | 0, /* tp_hash */ |
| 1608 | 0, /* tp_call */ |
| 1609 | 0, /* tp_str */ |
| 1610 | 0, /* tp_getattro */ |
| 1611 | 0, /* tp_setattro */ |
| 1612 | 0, /* tp_as_buffer */ |
| 1613 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */ |
| 1614 | 0, /* tp_doc */ |
| 1615 | (traverseproc)dict_traverse, /* tp_traverse */ |
| 1616 | (inquiry)dict_tp_clear, /* tp_clear */ |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1617 | dict_richcompare, /* tp_richcompare */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1618 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1619 | (getiterfunc)dict_iter, /* tp_iter */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 1620 | 0, /* tp_iternext */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1621 | }; |
| 1622 | |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1623 | /* For backward compatibility with old dictionary interface */ |
| 1624 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1625 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1626 | PyDict_GetItemString(PyObject *v, char *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1627 | { |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1628 | PyObject *kv, *rv; |
| 1629 | kv = PyString_FromString(key); |
| 1630 | if (kv == NULL) |
| 1631 | return NULL; |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1632 | rv = PyDict_GetItem(v, kv); |
| 1633 | Py_DECREF(kv); |
| 1634 | return rv; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1638 | PyDict_SetItemString(PyObject *v, char *key, PyObject *item) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1639 | { |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1640 | PyObject *kv; |
| 1641 | int err; |
| 1642 | kv = PyString_FromString(key); |
| 1643 | if (kv == NULL) |
Guido van Rossum | 037b220 | 1997-05-20 18:35:19 +0000 | [diff] [blame] | 1644 | return -1; |
Guido van Rossum | 4f3bf1e | 1997-09-29 23:31:11 +0000 | [diff] [blame] | 1645 | PyString_InternInPlace(&kv); /* XXX Should we really? */ |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1646 | err = PyDict_SetItem(v, kv, item); |
| 1647 | Py_DECREF(kv); |
| 1648 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1652 | PyDict_DelItemString(PyObject *v, char *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1653 | { |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1654 | PyObject *kv; |
| 1655 | int err; |
| 1656 | kv = PyString_FromString(key); |
| 1657 | if (kv == NULL) |
Guido van Rossum | 037b220 | 1997-05-20 18:35:19 +0000 | [diff] [blame] | 1658 | return -1; |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1659 | err = PyDict_DelItem(v, kv); |
| 1660 | Py_DECREF(kv); |
| 1661 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1662 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1663 | |
| 1664 | /* Dictionary iterator type */ |
| 1665 | |
| 1666 | extern PyTypeObject PyDictIter_Type; /* Forward */ |
| 1667 | |
| 1668 | typedef struct { |
| 1669 | PyObject_HEAD |
| 1670 | dictobject *di_dict; |
Guido van Rossum | b1f35bf | 2001-05-02 15:13:44 +0000 | [diff] [blame] | 1671 | int di_used; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1672 | int di_pos; |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1673 | binaryfunc di_select; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1674 | } dictiterobject; |
| 1675 | |
| 1676 | static PyObject * |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1677 | dictiter_new(dictobject *dict, binaryfunc select) |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1678 | { |
| 1679 | dictiterobject *di; |
| 1680 | di = PyObject_NEW(dictiterobject, &PyDictIter_Type); |
| 1681 | if (di == NULL) |
| 1682 | return NULL; |
| 1683 | Py_INCREF(dict); |
| 1684 | di->di_dict = dict; |
Guido van Rossum | b1f35bf | 2001-05-02 15:13:44 +0000 | [diff] [blame] | 1685 | di->di_used = dict->ma_used; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1686 | di->di_pos = 0; |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1687 | di->di_select = select; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1688 | return (PyObject *)di; |
| 1689 | } |
| 1690 | |
| 1691 | static void |
| 1692 | dictiter_dealloc(dictiterobject *di) |
| 1693 | { |
| 1694 | Py_DECREF(di->di_dict); |
| 1695 | PyObject_DEL(di); |
| 1696 | } |
| 1697 | |
| 1698 | static PyObject * |
| 1699 | dictiter_next(dictiterobject *di, PyObject *args) |
| 1700 | { |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1701 | PyObject *key, *value; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 1702 | |
Guido van Rossum | b1f35bf | 2001-05-02 15:13:44 +0000 | [diff] [blame] | 1703 | if (di->di_used != di->di_dict->ma_used) { |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1704 | PyErr_SetString(PyExc_RuntimeError, |
| 1705 | "dictionary changed size during iteration"); |
| 1706 | return NULL; |
| 1707 | } |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1708 | if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) { |
| 1709 | return (*di->di_select)(key, value); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1710 | } |
| 1711 | PyErr_SetObject(PyExc_StopIteration, Py_None); |
| 1712 | return NULL; |
| 1713 | } |
| 1714 | |
| 1715 | static PyObject * |
| 1716 | dictiter_getiter(PyObject *it) |
| 1717 | { |
| 1718 | Py_INCREF(it); |
| 1719 | return it; |
| 1720 | } |
| 1721 | |
| 1722 | static PyMethodDef dictiter_methods[] = { |
| 1723 | {"next", (PyCFunction)dictiter_next, METH_VARARGS, |
| 1724 | "it.next() -- get the next value, or raise StopIteration"}, |
| 1725 | {NULL, NULL} /* sentinel */ |
| 1726 | }; |
| 1727 | |
| 1728 | static PyObject * |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 1729 | dictiter_getattr(dictiterobject *di, char *name) |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1730 | { |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 1731 | return Py_FindMethod(dictiter_methods, (PyObject *)di, name); |
| 1732 | } |
| 1733 | |
| 1734 | static PyObject *dictiter_iternext(dictiterobject *di) |
| 1735 | { |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1736 | PyObject *key, *value; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 1737 | |
Guido van Rossum | b1f35bf | 2001-05-02 15:13:44 +0000 | [diff] [blame] | 1738 | if (di->di_used != di->di_dict->ma_used) { |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 1739 | PyErr_SetString(PyExc_RuntimeError, |
| 1740 | "dictionary changed size during iteration"); |
| 1741 | return NULL; |
| 1742 | } |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1743 | if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) { |
| 1744 | return (*di->di_select)(key, value); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 1745 | } |
| 1746 | return NULL; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | PyTypeObject PyDictIter_Type = { |
| 1750 | PyObject_HEAD_INIT(&PyType_Type) |
| 1751 | 0, /* ob_size */ |
| 1752 | "dictionary-iterator", /* tp_name */ |
| 1753 | sizeof(dictiterobject), /* tp_basicsize */ |
| 1754 | 0, /* tp_itemsize */ |
| 1755 | /* methods */ |
| 1756 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 1757 | 0, /* tp_print */ |
| 1758 | (getattrfunc)dictiter_getattr, /* tp_getattr */ |
| 1759 | 0, /* tp_setattr */ |
| 1760 | 0, /* tp_compare */ |
| 1761 | 0, /* tp_repr */ |
| 1762 | 0, /* tp_as_number */ |
| 1763 | 0, /* tp_as_sequence */ |
| 1764 | 0, /* tp_as_mapping */ |
| 1765 | 0, /* tp_hash */ |
| 1766 | 0, /* tp_call */ |
| 1767 | 0, /* tp_str */ |
| 1768 | 0, /* tp_getattro */ |
| 1769 | 0, /* tp_setattro */ |
| 1770 | 0, /* tp_as_buffer */ |
| 1771 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 1772 | 0, /* tp_doc */ |
| 1773 | 0, /* tp_traverse */ |
| 1774 | 0, /* tp_clear */ |
| 1775 | 0, /* tp_richcompare */ |
| 1776 | 0, /* tp_weaklistoffset */ |
| 1777 | (getiterfunc)dictiter_getiter, /* tp_iter */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 1778 | (iternextfunc)dictiter_iternext, /* tp_iternext */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1779 | }; |