Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
| 25 | /* Mapping object implementation; using a hash table */ |
| 26 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 27 | /* This file should really be called "dictobject.c", since "mapping" |
| 28 | is the generic name for objects with an unorderred arbitrary key |
| 29 | set (just like lists are sequences), but since it improves (and was |
| 30 | originally derived from) a file by that name I had to change its |
| 31 | name. For the user these objects are still called "dictionaries". */ |
| 32 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 33 | #include "allobjects.h" |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 34 | #include "modsupport.h" |
| 35 | |
| 36 | |
| 37 | /* |
| 38 | Table of primes suitable as keys, in ascending order. |
| 39 | The first line are the largest primes less than some powers of two, |
| 40 | the second line is the largest prime less than 6000, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 41 | the third line is a selection from Knuth, Vol. 3, Sec. 6.1, Table 1, |
| 42 | and the next three lines were suggested by Steve Kirsch. |
| 43 | The final value is a sentinel. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 44 | */ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 45 | static long primes[] = { |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 46 | 3, 7, 13, 31, 61, 127, 251, 509, 1021, 2017, 4093, |
| 47 | 5987, |
| 48 | 9551, 15683, 19609, 31397, |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 49 | 65521L, 131071L, 262139L, 524287L, 1048573L, 2097143L, |
| 50 | 4194301L, 8388593L, 16777213L, 33554393L, 67108859L, |
| 51 | 134217689L, 268435399L, 536870909L, 1073741789L, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 52 | 0 |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /* Object used as dummy key to fill deleted entries */ |
| 56 | static object *dummy; /* Initialized by first call to newmappingobject() */ |
| 57 | |
| 58 | /* |
| 59 | Invariant for entries: when in use, de_value is not NULL and de_key is |
| 60 | not NULL and not dummy; when not in use, de_value is NULL and de_key |
| 61 | is either NULL or dummy. A dummy key value cannot be replaced by |
| 62 | NULL, since otherwise other keys may be lost. |
| 63 | */ |
| 64 | typedef struct { |
| 65 | long me_hash; |
| 66 | object *me_key; |
| 67 | object *me_value; |
| 68 | } mappingentry; |
| 69 | |
| 70 | /* |
| 71 | To ensure the lookup algorithm terminates, the table size must be a |
| 72 | prime number and there must be at least one NULL key in the table. |
| 73 | The value ma_fill is the number of non-NULL keys; ma_used is the number |
| 74 | of non-NULL, non-dummy keys. |
| 75 | To avoid slowing down lookups on a near-full table, we resize the table |
| 76 | when it is more than half filled. |
| 77 | */ |
| 78 | typedef struct { |
| 79 | OB_HEAD |
| 80 | int ma_fill; |
| 81 | int ma_used; |
| 82 | int ma_size; |
| 83 | mappingentry *ma_table; |
| 84 | } mappingobject; |
| 85 | |
| 86 | object * |
| 87 | newmappingobject() |
| 88 | { |
| 89 | register mappingobject *mp; |
| 90 | if (dummy == NULL) { /* Auto-initialize dummy */ |
| 91 | dummy = newstringobject("<dummy key>"); |
| 92 | if (dummy == NULL) |
| 93 | return NULL; |
| 94 | } |
| 95 | mp = NEWOBJ(mappingobject, &Mappingtype); |
| 96 | if (mp == NULL) |
| 97 | return NULL; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 98 | mp->ma_size = 0; |
| 99 | mp->ma_table = NULL; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 100 | mp->ma_fill = 0; |
| 101 | mp->ma_used = 0; |
| 102 | return (object *)mp; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | The basic lookup function used by all operations. |
| 107 | This is essentially Algorithm D from Knuth Vol. 3, Sec. 6.4. |
| 108 | Open addressing is preferred over chaining since the link overhead for |
| 109 | chaining would be substantial (100% with typical malloc overhead). |
| 110 | |
| 111 | First a 32-bit hash value, 'sum', is computed from the key string. |
| 112 | The first character is added an extra time shifted by 8 to avoid hashing |
| 113 | single-character keys (often heavily used variables) too close together. |
| 114 | All arithmetic on sum should ignore overflow. |
| 115 | |
| 116 | The initial probe index is then computed as sum mod the table size. |
| 117 | Subsequent probe indices are incr apart (mod table size), where incr |
| 118 | is also derived from sum, with the additional requirement that it is |
| 119 | relative prime to the table size (i.e., 1 <= incr < size, since the size |
| 120 | is a prime number). My choice for incr is somewhat arbitrary. |
| 121 | */ |
| 122 | static mappingentry *lookmapping PROTO((mappingobject *, object *, long)); |
| 123 | static mappingentry * |
| 124 | lookmapping(mp, key, hash) |
| 125 | register mappingobject *mp; |
| 126 | object *key; |
| 127 | long hash; |
| 128 | { |
| 129 | register int i, incr; |
| 130 | register unsigned long sum = (unsigned long) hash; |
| 131 | register mappingentry *freeslot = NULL; |
Guido van Rossum | 310968d | 1996-07-30 16:45:31 +0000 | [diff] [blame] | 132 | register int size = mp->ma_size; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 133 | /* We must come up with (i, incr) such that 0 <= i < ma_size |
| 134 | and 0 < incr < ma_size and both are a function of hash */ |
Guido van Rossum | 310968d | 1996-07-30 16:45:31 +0000 | [diff] [blame] | 135 | i = sum % size; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 136 | do { |
Guido van Rossum | 5fe6058 | 1995-03-09 12:12:50 +0000 | [diff] [blame] | 137 | sum = 3*sum + 1; |
Guido van Rossum | 310968d | 1996-07-30 16:45:31 +0000 | [diff] [blame] | 138 | incr = sum % size; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 139 | } while (incr == 0); |
| 140 | for (;;) { |
| 141 | register mappingentry *ep = &mp->ma_table[i]; |
| 142 | if (ep->me_key == NULL) { |
| 143 | if (freeslot != NULL) |
| 144 | return freeslot; |
| 145 | else |
| 146 | return ep; |
| 147 | } |
| 148 | if (ep->me_key == dummy) { |
Guido van Rossum | 52f2c05 | 1993-11-10 12:53:24 +0000 | [diff] [blame] | 149 | if (freeslot == NULL) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 150 | freeslot = ep; |
| 151 | } |
| 152 | else if (ep->me_hash == hash && |
| 153 | cmpobject(ep->me_key, key) == 0) { |
| 154 | return ep; |
| 155 | } |
Guido van Rossum | 310968d | 1996-07-30 16:45:31 +0000 | [diff] [blame] | 156 | i = (i + incr) % size; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | Internal routine to insert a new item into the table. |
| 162 | Used both by the internal resize routine and by the public insert routine. |
| 163 | Eats a reference to key and one to value. |
| 164 | */ |
| 165 | static void insertmapping PROTO((mappingobject *, object *, long, object *)); |
| 166 | static void |
| 167 | insertmapping(mp, key, hash, value) |
| 168 | register mappingobject *mp; |
| 169 | object *key; |
| 170 | long hash; |
| 171 | object *value; |
| 172 | { |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 173 | object *old_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 174 | register mappingentry *ep; |
| 175 | ep = lookmapping(mp, key, hash); |
| 176 | if (ep->me_value != NULL) { |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 177 | old_value = ep->me_value; |
| 178 | ep->me_value = value; |
| 179 | DECREF(old_value); /* which **CAN** re-enter */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 180 | DECREF(key); |
| 181 | } |
| 182 | else { |
| 183 | if (ep->me_key == NULL) |
| 184 | mp->ma_fill++; |
| 185 | else |
| 186 | DECREF(ep->me_key); |
| 187 | ep->me_key = key; |
| 188 | ep->me_hash = hash; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 189 | ep->me_value = value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 190 | mp->ma_used++; |
| 191 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /* |
| 195 | Restructure the table by allocating a new table and reinserting all |
| 196 | items again. When entries have been deleted, the new table may |
| 197 | actually be smaller than the old one. |
| 198 | */ |
| 199 | static int mappingresize PROTO((mappingobject *)); |
| 200 | static int |
| 201 | mappingresize(mp) |
| 202 | mappingobject *mp; |
| 203 | { |
| 204 | register int oldsize = mp->ma_size; |
| 205 | register int newsize; |
| 206 | register mappingentry *oldtable = mp->ma_table; |
| 207 | register mappingentry *newtable; |
| 208 | register mappingentry *ep; |
| 209 | register int i; |
| 210 | newsize = mp->ma_size; |
| 211 | for (i = 0; ; i++) { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 212 | if (primes[i] <= 0) { |
| 213 | /* Ran out of primes */ |
| 214 | err_nomem(); |
| 215 | return -1; |
| 216 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 217 | if (primes[i] > mp->ma_used*2) { |
| 218 | newsize = primes[i]; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 219 | if (newsize != primes[i]) { |
| 220 | /* Integer truncation */ |
| 221 | err_nomem(); |
| 222 | return -1; |
| 223 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | } |
| 227 | newtable = (mappingentry *) calloc(sizeof(mappingentry), newsize); |
| 228 | if (newtable == NULL) { |
| 229 | err_nomem(); |
| 230 | return -1; |
| 231 | } |
| 232 | mp->ma_size = newsize; |
| 233 | mp->ma_table = newtable; |
| 234 | mp->ma_fill = 0; |
| 235 | mp->ma_used = 0; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 236 | |
| 237 | /* Make two passes, so we can avoid decrefs |
| 238 | (and possible side effects) till the table is copied */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 239 | for (i = 0, ep = oldtable; i < oldsize; i++, ep++) { |
| 240 | if (ep->me_value != NULL) |
| 241 | insertmapping(mp,ep->me_key,ep->me_hash,ep->me_value); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 242 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 243 | for (i = 0, ep = oldtable; i < oldsize; i++, ep++) { |
| 244 | if (ep->me_value == NULL) |
| 245 | XDECREF(ep->me_key); |
| 246 | } |
| 247 | |
| 248 | XDEL(oldtable); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | object * |
| 253 | mappinglookup(op, key) |
| 254 | object *op; |
| 255 | object *key; |
| 256 | { |
| 257 | long hash; |
| 258 | if (!is_mappingobject(op)) { |
| 259 | err_badcall(); |
| 260 | return NULL; |
| 261 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 262 | if (((mappingobject *)op)->ma_table == NULL) |
| 263 | return NULL; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 264 | #ifdef CACHE_HASH |
| 265 | if (!is_stringobject(key) || (hash = ((stringobject *) key)->ob_shash) == -1) |
| 266 | #endif |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 267 | hash = hashobject(key); |
| 268 | if (hash == -1) |
| 269 | return NULL; |
| 270 | return lookmapping((mappingobject *)op, key, hash) -> me_value; |
| 271 | } |
| 272 | |
| 273 | int |
| 274 | mappinginsert(op, key, value) |
| 275 | register object *op; |
| 276 | object *key; |
| 277 | object *value; |
| 278 | { |
| 279 | register mappingobject *mp; |
| 280 | register long hash; |
| 281 | if (!is_mappingobject(op)) { |
| 282 | err_badcall(); |
| 283 | return -1; |
| 284 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 285 | #ifdef CACHE_HASH |
| 286 | if (!is_stringobject(key) || (hash = ((stringobject *) key)->ob_shash) == -1) |
| 287 | #endif |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 288 | hash = hashobject(key); |
| 289 | if (hash == -1) |
| 290 | return -1; |
| 291 | mp = (mappingobject *)op; |
| 292 | /* if fill >= 2/3 size, resize */ |
| 293 | if (mp->ma_fill*3 >= mp->ma_size*2) { |
| 294 | if (mappingresize(mp) != 0) { |
| 295 | if (mp->ma_fill+1 > mp->ma_size) |
| 296 | return -1; |
| 297 | } |
| 298 | } |
| 299 | INCREF(value); |
| 300 | INCREF(key); |
| 301 | insertmapping(mp, key, hash, value); |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | int |
| 306 | mappingremove(op, key) |
| 307 | object *op; |
| 308 | object *key; |
| 309 | { |
| 310 | register mappingobject *mp; |
| 311 | register long hash; |
| 312 | register mappingentry *ep; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 313 | object *old_value, *old_key; |
| 314 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 315 | if (!is_mappingobject(op)) { |
| 316 | err_badcall(); |
| 317 | return -1; |
| 318 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 319 | #ifdef CACHE_HASH |
| 320 | if (!is_stringobject(key) || (hash = ((stringobject *) key)->ob_shash) == -1) |
| 321 | #endif |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 322 | hash = hashobject(key); |
| 323 | if (hash == -1) |
| 324 | return -1; |
| 325 | mp = (mappingobject *)op; |
Guido van Rossum | efc8713 | 1995-01-02 19:42:39 +0000 | [diff] [blame] | 326 | if (((mappingobject *)op)->ma_table == NULL) |
| 327 | goto empty; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 328 | ep = lookmapping(mp, key, hash); |
| 329 | if (ep->me_value == NULL) { |
Guido van Rossum | efc8713 | 1995-01-02 19:42:39 +0000 | [diff] [blame] | 330 | empty: |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 331 | err_setval(KeyError, key); |
| 332 | return -1; |
| 333 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 334 | old_key = ep->me_key; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 335 | INCREF(dummy); |
| 336 | ep->me_key = dummy; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 337 | old_value = ep->me_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 338 | ep->me_value = NULL; |
| 339 | mp->ma_used--; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 340 | DECREF(old_value); |
| 341 | DECREF(old_key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 345 | void |
| 346 | mappingclear(op) |
| 347 | object *op; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 348 | { |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 349 | int i, n; |
| 350 | register mappingentry *table; |
| 351 | mappingobject *mp; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 352 | if (!is_mappingobject(op)) |
| 353 | return; |
| 354 | mp = (mappingobject *)op; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 355 | table = mp->ma_table; |
| 356 | if (table == NULL) |
| 357 | return; |
| 358 | n = mp->ma_size; |
| 359 | mp->ma_size = mp->ma_used = mp->ma_fill = 0; |
| 360 | mp->ma_table = NULL; |
| 361 | for (i = 0; i < n; i++) { |
| 362 | XDECREF(table[i].me_key); |
| 363 | XDECREF(table[i].me_value); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 364 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 365 | DEL(table); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 368 | int |
| 369 | mappinggetnext(op, ppos, pkey, pvalue) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 370 | object *op; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 371 | int *ppos; |
| 372 | object **pkey; |
| 373 | object **pvalue; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 374 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 375 | int i; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 376 | register mappingobject *mp; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 377 | if (!is_dictobject(op)) |
| 378 | return 0; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 379 | mp = (mappingobject *)op; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 380 | i = *ppos; |
| 381 | if (i < 0) |
| 382 | return 0; |
| 383 | while (i < mp->ma_size && mp->ma_table[i].me_value == NULL) |
| 384 | i++; |
| 385 | *ppos = i+1; |
| 386 | if (i >= mp->ma_size) |
| 387 | return 0; |
| 388 | if (pkey) |
| 389 | *pkey = mp->ma_table[i].me_key; |
| 390 | if (pvalue) |
| 391 | *pvalue = mp->ma_table[i].me_value; |
| 392 | return 1; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* Methods */ |
| 396 | |
| 397 | static void |
| 398 | mapping_dealloc(mp) |
| 399 | register mappingobject *mp; |
| 400 | { |
| 401 | register int i; |
| 402 | register mappingentry *ep; |
| 403 | for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) { |
| 404 | if (ep->me_key != NULL) |
| 405 | DECREF(ep->me_key); |
| 406 | if (ep->me_value != NULL) |
| 407 | DECREF(ep->me_value); |
| 408 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 409 | XDEL(mp->ma_table); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 410 | DEL(mp); |
| 411 | } |
| 412 | |
| 413 | static int |
| 414 | mapping_print(mp, fp, flags) |
| 415 | register mappingobject *mp; |
| 416 | register FILE *fp; |
| 417 | register int flags; |
| 418 | { |
| 419 | register int i; |
| 420 | register int any; |
| 421 | register mappingentry *ep; |
| 422 | fprintf(fp, "{"); |
| 423 | any = 0; |
| 424 | for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) { |
| 425 | if (ep->me_value != NULL) { |
| 426 | if (any++ > 0) |
| 427 | fprintf(fp, ", "); |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 428 | if (printobject((object *)ep->me_key, fp, 0) != 0) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 429 | return -1; |
| 430 | fprintf(fp, ": "); |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 431 | if (printobject(ep->me_value, fp, 0) != 0) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 432 | return -1; |
| 433 | } |
| 434 | } |
| 435 | fprintf(fp, "}"); |
| 436 | return 0; |
| 437 | } |
| 438 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 439 | static object * |
| 440 | mapping_repr(mp) |
| 441 | mappingobject *mp; |
| 442 | { |
| 443 | auto object *v; |
| 444 | object *sepa, *colon; |
| 445 | register int i; |
| 446 | register int any; |
| 447 | register mappingentry *ep; |
| 448 | v = newstringobject("{"); |
| 449 | sepa = newstringobject(", "); |
| 450 | colon = newstringobject(": "); |
| 451 | any = 0; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 452 | 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] | 453 | if (ep->me_value != NULL) { |
| 454 | if (any++) |
| 455 | joinstring(&v, sepa); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 456 | joinstring_decref(&v, reprobject(ep->me_key)); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 457 | joinstring(&v, colon); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 458 | joinstring_decref(&v, reprobject(ep->me_value)); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 461 | joinstring_decref(&v, newstringobject("}")); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 462 | XDECREF(sepa); |
| 463 | XDECREF(colon); |
| 464 | return v; |
| 465 | } |
| 466 | |
| 467 | static int |
| 468 | mapping_length(mp) |
| 469 | mappingobject *mp; |
| 470 | { |
| 471 | return mp->ma_used; |
| 472 | } |
| 473 | |
| 474 | static object * |
| 475 | mapping_subscript(mp, key) |
| 476 | mappingobject *mp; |
| 477 | register object *key; |
| 478 | { |
| 479 | object *v; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 480 | long hash; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 481 | if (mp->ma_table == NULL) { |
| 482 | err_setval(KeyError, key); |
| 483 | return NULL; |
| 484 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 485 | #ifdef CACHE_HASH |
| 486 | if (!is_stringobject(key) || (hash = ((stringobject *) key)->ob_shash) == -1) |
| 487 | #endif |
| 488 | hash = hashobject(key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 489 | if (hash == -1) |
| 490 | return NULL; |
| 491 | v = lookmapping(mp, key, hash) -> me_value; |
| 492 | if (v == NULL) |
| 493 | err_setval(KeyError, key); |
| 494 | else |
| 495 | INCREF(v); |
| 496 | return v; |
| 497 | } |
| 498 | |
| 499 | static int |
| 500 | mapping_ass_sub(mp, v, w) |
| 501 | mappingobject *mp; |
| 502 | object *v, *w; |
| 503 | { |
| 504 | if (w == NULL) |
| 505 | return mappingremove((object *)mp, v); |
| 506 | else |
| 507 | return mappinginsert((object *)mp, v, w); |
| 508 | } |
| 509 | |
| 510 | static mapping_methods mapping_as_mapping = { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 511 | (inquiry)mapping_length, /*mp_length*/ |
| 512 | (binaryfunc)mapping_subscript, /*mp_subscript*/ |
| 513 | (objobjargproc)mapping_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 514 | }; |
| 515 | |
| 516 | static object * |
| 517 | mapping_keys(mp, args) |
| 518 | register mappingobject *mp; |
| 519 | object *args; |
| 520 | { |
| 521 | register object *v; |
| 522 | register int i, j; |
| 523 | if (!getnoarg(args)) |
| 524 | return NULL; |
| 525 | v = newlistobject(mp->ma_used); |
| 526 | if (v == NULL) |
| 527 | return NULL; |
| 528 | for (i = 0, j = 0; i < mp->ma_size; i++) { |
| 529 | if (mp->ma_table[i].me_value != NULL) { |
| 530 | object *key = mp->ma_table[i].me_key; |
| 531 | INCREF(key); |
| 532 | setlistitem(v, j, key); |
| 533 | j++; |
| 534 | } |
| 535 | } |
| 536 | return v; |
| 537 | } |
| 538 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 539 | static object * |
| 540 | mapping_values(mp, args) |
| 541 | register mappingobject *mp; |
| 542 | object *args; |
| 543 | { |
| 544 | register object *v; |
| 545 | register int i, j; |
| 546 | if (!getnoarg(args)) |
| 547 | return NULL; |
| 548 | v = newlistobject(mp->ma_used); |
| 549 | if (v == NULL) |
| 550 | return NULL; |
| 551 | for (i = 0, j = 0; i < mp->ma_size; i++) { |
| 552 | if (mp->ma_table[i].me_value != NULL) { |
| 553 | object *value = mp->ma_table[i].me_value; |
| 554 | INCREF(value); |
| 555 | setlistitem(v, j, value); |
| 556 | j++; |
| 557 | } |
| 558 | } |
| 559 | return v; |
| 560 | } |
| 561 | |
| 562 | static object * |
| 563 | mapping_items(mp, args) |
| 564 | register mappingobject *mp; |
| 565 | object *args; |
| 566 | { |
| 567 | register object *v; |
| 568 | register int i, j; |
| 569 | if (!getnoarg(args)) |
| 570 | return NULL; |
| 571 | v = newlistobject(mp->ma_used); |
| 572 | if (v == NULL) |
| 573 | return NULL; |
| 574 | for (i = 0, j = 0; i < mp->ma_size; i++) { |
| 575 | if (mp->ma_table[i].me_value != NULL) { |
| 576 | object *key = mp->ma_table[i].me_key; |
| 577 | object *value = mp->ma_table[i].me_value; |
| 578 | object *item = newtupleobject(2); |
| 579 | if (item == NULL) { |
| 580 | DECREF(v); |
| 581 | return NULL; |
| 582 | } |
| 583 | INCREF(key); |
| 584 | settupleitem(item, 0, key); |
| 585 | INCREF(value); |
| 586 | settupleitem(item, 1, value); |
| 587 | setlistitem(v, j, item); |
| 588 | j++; |
| 589 | } |
| 590 | } |
| 591 | return v; |
| 592 | } |
| 593 | |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 594 | int |
| 595 | getmappingsize(mp) |
| 596 | object *mp; |
| 597 | { |
| 598 | if (mp == NULL || !is_mappingobject(mp)) { |
| 599 | err_badcall(); |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 600 | return 0; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 601 | } |
| 602 | return ((mappingobject *)mp)->ma_used; |
| 603 | } |
| 604 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 605 | object * |
| 606 | getmappingkeys(mp) |
| 607 | object *mp; |
| 608 | { |
| 609 | if (mp == NULL || !is_mappingobject(mp)) { |
| 610 | err_badcall(); |
| 611 | return NULL; |
| 612 | } |
| 613 | return mapping_keys((mappingobject *)mp, (object *)NULL); |
| 614 | } |
| 615 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 616 | object * |
| 617 | getmappingvalues(mp) |
| 618 | object *mp; |
| 619 | { |
| 620 | if (mp == NULL || !is_mappingobject(mp)) { |
| 621 | err_badcall(); |
| 622 | return NULL; |
| 623 | } |
| 624 | return mapping_values((mappingobject *)mp, (object *)NULL); |
| 625 | } |
| 626 | |
| 627 | object * |
| 628 | getmappingitems(mp) |
| 629 | object *mp; |
| 630 | { |
| 631 | if (mp == NULL || !is_mappingobject(mp)) { |
| 632 | err_badcall(); |
| 633 | return NULL; |
| 634 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 635 | return mapping_items((mappingobject *)mp, (object *)NULL); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 638 | static int |
| 639 | mapping_compare(a, b) |
| 640 | mappingobject *a, *b; |
| 641 | { |
| 642 | object *akeys, *bkeys; |
| 643 | int i, n, res; |
| 644 | if (a == b) |
| 645 | return 0; |
| 646 | if (a->ma_used == 0) { |
| 647 | if (b->ma_used != 0) |
| 648 | return -1; |
| 649 | else |
| 650 | return 0; |
| 651 | } |
| 652 | else { |
| 653 | if (b->ma_used == 0) |
| 654 | return 1; |
| 655 | } |
| 656 | akeys = mapping_keys(a, (object *)NULL); |
| 657 | bkeys = mapping_keys(b, (object *)NULL); |
| 658 | if (akeys == NULL || bkeys == NULL) { |
| 659 | /* Oops, out of memory -- what to do? */ |
| 660 | /* For now, sort on address! */ |
| 661 | XDECREF(akeys); |
| 662 | XDECREF(bkeys); |
| 663 | if (a < b) |
| 664 | return -1; |
| 665 | else |
| 666 | return 1; |
| 667 | } |
| 668 | sortlist(akeys); |
| 669 | sortlist(bkeys); |
| 670 | n = a->ma_used < b->ma_used ? a->ma_used : b->ma_used; /* smallest */ |
| 671 | res = 0; |
| 672 | for (i = 0; i < n; i++) { |
| 673 | object *akey, *bkey, *aval, *bval; |
| 674 | long ahash, bhash; |
| 675 | akey = getlistitem(akeys, i); |
| 676 | bkey = getlistitem(bkeys, i); |
| 677 | res = cmpobject(akey, bkey); |
| 678 | if (res != 0) |
| 679 | break; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 680 | #ifdef CACHE_HASH |
| 681 | if (!is_stringobject(akey) || (ahash = ((stringobject *) akey)->ob_shash) == -1) |
| 682 | #endif |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 683 | ahash = hashobject(akey); |
| 684 | if (ahash == -1) |
| 685 | err_clear(); /* Don't want errors here */ |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 686 | #ifdef CACHE_HASH |
| 687 | if (!is_stringobject(bkey) || (bhash = ((stringobject *) bkey)->ob_shash) == -1) |
| 688 | #endif |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 689 | bhash = hashobject(bkey); |
| 690 | if (bhash == -1) |
| 691 | err_clear(); /* Don't want errors here */ |
| 692 | aval = lookmapping(a, akey, ahash) -> me_value; |
| 693 | bval = lookmapping(b, bkey, bhash) -> me_value; |
| 694 | res = cmpobject(aval, bval); |
| 695 | if (res != 0) |
| 696 | break; |
| 697 | } |
| 698 | if (res == 0) { |
| 699 | if (a->ma_used < b->ma_used) |
| 700 | res = -1; |
| 701 | else if (a->ma_used > b->ma_used) |
| 702 | res = 1; |
| 703 | } |
| 704 | DECREF(akeys); |
| 705 | DECREF(bkeys); |
| 706 | return res; |
| 707 | } |
| 708 | |
| 709 | static object * |
| 710 | mapping_has_key(mp, args) |
| 711 | register mappingobject *mp; |
| 712 | object *args; |
| 713 | { |
| 714 | object *key; |
| 715 | long hash; |
| 716 | register long ok; |
| 717 | if (!getargs(args, "O", &key)) |
| 718 | return NULL; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 719 | #ifdef CACHE_HASH |
| 720 | if (!is_stringobject(key) || (hash = ((stringobject *) key)->ob_shash) == -1) |
| 721 | #endif |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 722 | hash = hashobject(key); |
| 723 | if (hash == -1) |
| 724 | return NULL; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 725 | ok = mp->ma_size != 0 && lookmapping(mp, key, hash)->me_value != NULL; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 726 | return newintobject(ok); |
| 727 | } |
| 728 | |
| 729 | static struct methodlist mapp_methods[] = { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 730 | {"has_key", (method)mapping_has_key}, |
| 731 | {"items", (method)mapping_items}, |
| 732 | {"keys", (method)mapping_keys}, |
| 733 | {"values", (method)mapping_values}, |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 734 | {NULL, NULL} /* sentinel */ |
| 735 | }; |
| 736 | |
| 737 | static object * |
| 738 | mapping_getattr(mp, name) |
| 739 | mappingobject *mp; |
| 740 | char *name; |
| 741 | { |
| 742 | return findmethod(mapp_methods, (object *)mp, name); |
| 743 | } |
| 744 | |
| 745 | typeobject Mappingtype = { |
| 746 | OB_HEAD_INIT(&Typetype) |
| 747 | 0, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 748 | "dictionary", |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 749 | sizeof(mappingobject), |
| 750 | 0, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 751 | (destructor)mapping_dealloc, /*tp_dealloc*/ |
| 752 | (printfunc)mapping_print, /*tp_print*/ |
| 753 | (getattrfunc)mapping_getattr, /*tp_getattr*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 754 | 0, /*tp_setattr*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 755 | (cmpfunc)mapping_compare, /*tp_compare*/ |
| 756 | (reprfunc)mapping_repr, /*tp_repr*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 757 | 0, /*tp_as_number*/ |
| 758 | 0, /*tp_as_sequence*/ |
| 759 | &mapping_as_mapping, /*tp_as_mapping*/ |
| 760 | }; |
| 761 | |
| 762 | /* For backward compatibility with old dictionary interface */ |
| 763 | |
| 764 | static object *last_name_object; |
Guido van Rossum | 992ded8 | 1995-12-08 01:16:31 +0000 | [diff] [blame] | 765 | static char *last_name_char; /* NULL or == getstringvalue(last_name_object) */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 766 | |
| 767 | object * |
| 768 | getattro(v, name) |
| 769 | object *v; |
| 770 | object *name; |
| 771 | { |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 772 | if (v->ob_type->tp_getattro != NULL) |
| 773 | return (*v->ob_type->tp_getattro)(v, name); |
| 774 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 775 | if (name != last_name_object) { |
| 776 | XDECREF(last_name_object); |
| 777 | INCREF(name); |
| 778 | last_name_object = name; |
| 779 | last_name_char = getstringvalue(name); |
| 780 | } |
| 781 | return getattr(v, last_name_char); |
| 782 | } |
| 783 | |
| 784 | int |
| 785 | setattro(v, name, value) |
| 786 | object *v; |
| 787 | object *name; |
| 788 | object *value; |
| 789 | { |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 790 | if (v->ob_type->tp_setattro != NULL) |
| 791 | return (*v->ob_type->tp_setattro)(v, name, value); |
| 792 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 793 | if (name != last_name_object) { |
| 794 | XDECREF(last_name_object); |
| 795 | INCREF(name); |
| 796 | last_name_object = name; |
| 797 | last_name_char = getstringvalue(name); |
| 798 | } |
| 799 | return setattr(v, last_name_char, value); |
| 800 | } |
| 801 | |
| 802 | object * |
| 803 | dictlookup(v, key) |
| 804 | object *v; |
| 805 | char *key; |
| 806 | { |
Guido van Rossum | 992ded8 | 1995-12-08 01:16:31 +0000 | [diff] [blame] | 807 | if (key != last_name_char) { |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 808 | XDECREF(last_name_object); |
| 809 | last_name_object = newstringobject(key); |
| 810 | if (last_name_object == NULL) { |
| 811 | last_name_char = NULL; |
| 812 | return NULL; |
| 813 | } |
Guido van Rossum | 992ded8 | 1995-12-08 01:16:31 +0000 | [diff] [blame] | 814 | last_name_char = getstringvalue(last_name_object); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 815 | } |
| 816 | return mappinglookup(v, last_name_object); |
| 817 | } |
| 818 | |
| 819 | int |
| 820 | dictinsert(v, key, item) |
| 821 | object *v; |
| 822 | char *key; |
| 823 | object *item; |
| 824 | { |
Guido van Rossum | 992ded8 | 1995-12-08 01:16:31 +0000 | [diff] [blame] | 825 | if (key != last_name_char) { |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 826 | XDECREF(last_name_object); |
| 827 | last_name_object = newstringobject(key); |
| 828 | if (last_name_object == NULL) { |
| 829 | last_name_char = NULL; |
Guido van Rossum | 1fc238a | 1993-07-29 08:25:09 +0000 | [diff] [blame] | 830 | return -1; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 831 | } |
Guido van Rossum | 992ded8 | 1995-12-08 01:16:31 +0000 | [diff] [blame] | 832 | last_name_char = getstringvalue(last_name_object); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 833 | } |
| 834 | return mappinginsert(v, last_name_object, item); |
| 835 | } |
| 836 | |
| 837 | int |
| 838 | dictremove(v, key) |
| 839 | object *v; |
| 840 | char *key; |
| 841 | { |
Guido van Rossum | 992ded8 | 1995-12-08 01:16:31 +0000 | [diff] [blame] | 842 | if (key != last_name_char) { |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 843 | XDECREF(last_name_object); |
| 844 | last_name_object = newstringobject(key); |
| 845 | if (last_name_object == NULL) { |
| 846 | last_name_char = NULL; |
Guido van Rossum | 1fc238a | 1993-07-29 08:25:09 +0000 | [diff] [blame] | 847 | return -1; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 848 | } |
Guido van Rossum | 992ded8 | 1995-12-08 01:16:31 +0000 | [diff] [blame] | 849 | last_name_char = getstringvalue(last_name_object); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 850 | } |
| 851 | return mappingremove(v, last_name_object); |
| 852 | } |