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