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