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