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