Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | bab9d03 | 1992-04-05 14:26:55 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 3 | Netherlands. |
| 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 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* List object implementation */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "allobjects.h" |
Guido van Rossum | fa3da8a | 1992-01-27 16:53:23 +0000 | [diff] [blame] | 28 | #include "modsupport.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 29 | #include "ceval.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 30 | |
| 31 | object * |
| 32 | newlistobject(size) |
| 33 | int size; |
| 34 | { |
| 35 | int i; |
| 36 | listobject *op; |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 37 | MALLARG nbytes; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 38 | if (size < 0) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 39 | err_badcall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 40 | return NULL; |
| 41 | } |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 42 | nbytes = size * sizeof(object *); |
| 43 | /* Check for overflow */ |
| 44 | if (nbytes / sizeof(object *) != size) { |
| 45 | return err_nomem(); |
| 46 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 47 | op = (listobject *) malloc(sizeof(listobject)); |
| 48 | if (op == NULL) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 49 | return err_nomem(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 50 | } |
| 51 | if (size <= 0) { |
| 52 | op->ob_item = NULL; |
| 53 | } |
| 54 | else { |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 55 | op->ob_item = (object **) malloc(nbytes); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | if (op->ob_item == NULL) { |
| 57 | free((ANY *)op); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 58 | return err_nomem(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | NEWREF(op); |
| 62 | op->ob_type = &Listtype; |
| 63 | op->ob_size = size; |
| 64 | for (i = 0; i < size; i++) |
| 65 | op->ob_item[i] = NULL; |
| 66 | return (object *) op; |
| 67 | } |
| 68 | |
| 69 | int |
| 70 | getlistsize(op) |
| 71 | object *op; |
| 72 | { |
| 73 | if (!is_listobject(op)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 74 | err_badcall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 75 | return -1; |
| 76 | } |
| 77 | else |
| 78 | return ((listobject *)op) -> ob_size; |
| 79 | } |
| 80 | |
| 81 | object * |
| 82 | getlistitem(op, i) |
| 83 | object *op; |
| 84 | int i; |
| 85 | { |
| 86 | if (!is_listobject(op)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 87 | err_badcall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 88 | return NULL; |
| 89 | } |
| 90 | if (i < 0 || i >= ((listobject *)op) -> ob_size) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 91 | err_setstr(IndexError, "list index out of range"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 92 | return NULL; |
| 93 | } |
| 94 | return ((listobject *)op) -> ob_item[i]; |
| 95 | } |
| 96 | |
| 97 | int |
| 98 | setlistitem(op, i, newitem) |
| 99 | register object *op; |
| 100 | register int i; |
| 101 | register object *newitem; |
| 102 | { |
| 103 | register object *olditem; |
| 104 | if (!is_listobject(op)) { |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 105 | XDECREF(newitem); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 106 | err_badcall(); |
| 107 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 108 | } |
| 109 | if (i < 0 || i >= ((listobject *)op) -> ob_size) { |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 110 | XDECREF(newitem); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 111 | err_setstr(IndexError, "list assignment index out of range"); |
| 112 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 113 | } |
| 114 | olditem = ((listobject *)op) -> ob_item[i]; |
| 115 | ((listobject *)op) -> ob_item[i] = newitem; |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 116 | XDECREF(olditem); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static int |
| 121 | ins1(self, where, v) |
| 122 | listobject *self; |
| 123 | int where; |
| 124 | object *v; |
| 125 | { |
| 126 | int i; |
| 127 | object **items; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 128 | if (v == NULL) { |
| 129 | err_badcall(); |
| 130 | return -1; |
| 131 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 132 | items = self->ob_item; |
| 133 | RESIZE(items, object *, self->ob_size+1); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 134 | if (items == NULL) { |
| 135 | err_nomem(); |
| 136 | return -1; |
| 137 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 138 | if (where < 0) |
| 139 | where = 0; |
| 140 | if (where > self->ob_size) |
| 141 | where = self->ob_size; |
| 142 | for (i = self->ob_size; --i >= where; ) |
| 143 | items[i+1] = items[i]; |
| 144 | INCREF(v); |
| 145 | items[where] = v; |
| 146 | self->ob_item = items; |
| 147 | self->ob_size++; |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | int |
| 152 | inslistitem(op, where, newitem) |
| 153 | object *op; |
| 154 | int where; |
| 155 | object *newitem; |
| 156 | { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 157 | if (!is_listobject(op)) { |
| 158 | err_badcall(); |
| 159 | return -1; |
| 160 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 161 | return ins1((listobject *)op, where, newitem); |
| 162 | } |
| 163 | |
| 164 | int |
| 165 | addlistitem(op, newitem) |
| 166 | object *op; |
| 167 | object *newitem; |
| 168 | { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 169 | if (!is_listobject(op)) { |
| 170 | err_badcall(); |
| 171 | return -1; |
| 172 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 173 | return ins1((listobject *)op, |
| 174 | (int) ((listobject *)op)->ob_size, newitem); |
| 175 | } |
| 176 | |
| 177 | /* Methods */ |
| 178 | |
| 179 | static void |
| 180 | list_dealloc(op) |
| 181 | listobject *op; |
| 182 | { |
| 183 | int i; |
| 184 | for (i = 0; i < op->ob_size; i++) { |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 185 | XDECREF(op->ob_item[i]); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 186 | } |
| 187 | if (op->ob_item != NULL) |
| 188 | free((ANY *)op->ob_item); |
| 189 | free((ANY *)op); |
| 190 | } |
| 191 | |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 192 | static int |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 193 | list_print(op, fp, flags) |
| 194 | listobject *op; |
| 195 | FILE *fp; |
| 196 | int flags; |
| 197 | { |
| 198 | int i; |
| 199 | fprintf(fp, "["); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 200 | for (i = 0; i < op->ob_size; i++) { |
| 201 | if (i > 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 202 | fprintf(fp, ", "); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 203 | if (printobject(op->ob_item[i], fp, flags) != 0) |
| 204 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 205 | } |
| 206 | fprintf(fp, "]"); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 207 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | object * |
| 211 | list_repr(v) |
| 212 | listobject *v; |
| 213 | { |
| 214 | object *s, *t, *comma; |
| 215 | int i; |
| 216 | s = newstringobject("["); |
| 217 | comma = newstringobject(", "); |
| 218 | for (i = 0; i < v->ob_size && s != NULL; i++) { |
| 219 | if (i > 0) |
| 220 | joinstring(&s, comma); |
| 221 | t = reprobject(v->ob_item[i]); |
| 222 | joinstring(&s, t); |
| 223 | DECREF(t); |
| 224 | } |
| 225 | DECREF(comma); |
| 226 | t = newstringobject("]"); |
| 227 | joinstring(&s, t); |
| 228 | DECREF(t); |
| 229 | return s; |
| 230 | } |
| 231 | |
| 232 | static int |
| 233 | list_compare(v, w) |
| 234 | listobject *v, *w; |
| 235 | { |
| 236 | int len = (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size; |
| 237 | int i; |
| 238 | for (i = 0; i < len; i++) { |
| 239 | int cmp = cmpobject(v->ob_item[i], w->ob_item[i]); |
| 240 | if (cmp != 0) |
| 241 | return cmp; |
| 242 | } |
| 243 | return v->ob_size - w->ob_size; |
| 244 | } |
| 245 | |
| 246 | static int |
| 247 | list_length(a) |
| 248 | listobject *a; |
| 249 | { |
| 250 | return a->ob_size; |
| 251 | } |
| 252 | |
| 253 | static object * |
| 254 | list_item(a, i) |
| 255 | listobject *a; |
| 256 | int i; |
| 257 | { |
| 258 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 259 | err_setstr(IndexError, "list index out of range"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 260 | return NULL; |
| 261 | } |
| 262 | INCREF(a->ob_item[i]); |
| 263 | return a->ob_item[i]; |
| 264 | } |
| 265 | |
| 266 | static object * |
| 267 | list_slice(a, ilow, ihigh) |
| 268 | listobject *a; |
| 269 | int ilow, ihigh; |
| 270 | { |
| 271 | listobject *np; |
| 272 | int i; |
| 273 | if (ilow < 0) |
| 274 | ilow = 0; |
| 275 | else if (ilow > a->ob_size) |
| 276 | ilow = a->ob_size; |
| 277 | if (ihigh < 0) |
| 278 | ihigh = 0; |
| 279 | if (ihigh < ilow) |
| 280 | ihigh = ilow; |
| 281 | else if (ihigh > a->ob_size) |
| 282 | ihigh = a->ob_size; |
| 283 | np = (listobject *) newlistobject(ihigh - ilow); |
| 284 | if (np == NULL) |
| 285 | return NULL; |
| 286 | for (i = ilow; i < ihigh; i++) { |
| 287 | object *v = a->ob_item[i]; |
| 288 | INCREF(v); |
| 289 | np->ob_item[i - ilow] = v; |
| 290 | } |
| 291 | return (object *)np; |
| 292 | } |
| 293 | |
| 294 | static object * |
| 295 | list_concat(a, bb) |
| 296 | listobject *a; |
| 297 | object *bb; |
| 298 | { |
| 299 | int size; |
| 300 | int i; |
| 301 | listobject *np; |
| 302 | if (!is_listobject(bb)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 303 | err_badarg(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 304 | return NULL; |
| 305 | } |
| 306 | #define b ((listobject *)bb) |
| 307 | size = a->ob_size + b->ob_size; |
| 308 | np = (listobject *) newlistobject(size); |
| 309 | if (np == NULL) { |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 310 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 311 | } |
| 312 | for (i = 0; i < a->ob_size; i++) { |
| 313 | object *v = a->ob_item[i]; |
| 314 | INCREF(v); |
| 315 | np->ob_item[i] = v; |
| 316 | } |
| 317 | for (i = 0; i < b->ob_size; i++) { |
| 318 | object *v = b->ob_item[i]; |
| 319 | INCREF(v); |
| 320 | np->ob_item[i + a->ob_size] = v; |
| 321 | } |
| 322 | return (object *)np; |
| 323 | #undef b |
| 324 | } |
| 325 | |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 326 | static object * |
| 327 | list_repeat(a, n) |
| 328 | listobject *a; |
| 329 | int n; |
| 330 | { |
| 331 | int i, j; |
| 332 | int size; |
| 333 | listobject *np; |
| 334 | object **p; |
| 335 | if (n < 0) |
| 336 | n = 0; |
| 337 | size = a->ob_size * n; |
| 338 | np = (listobject *) newlistobject(size); |
| 339 | if (np == NULL) |
| 340 | return NULL; |
| 341 | p = np->ob_item; |
| 342 | for (i = 0; i < n; i++) { |
| 343 | for (j = 0; j < a->ob_size; j++) { |
| 344 | *p = a->ob_item[j]; |
| 345 | INCREF(*p); |
| 346 | p++; |
| 347 | } |
| 348 | } |
| 349 | return (object *) np; |
| 350 | } |
| 351 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 352 | static int |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 353 | list_ass_slice(a, ilow, ihigh, v) |
| 354 | listobject *a; |
| 355 | int ilow, ihigh; |
| 356 | object *v; |
| 357 | { |
| 358 | object **item; |
| 359 | int n; /* Size of replacement list */ |
| 360 | int d; /* Change in size */ |
| 361 | int k; /* Loop index */ |
| 362 | #define b ((listobject *)v) |
| 363 | if (v == NULL) |
| 364 | n = 0; |
Guido van Rossum | 32dffaa | 1991-12-24 13:27:34 +0000 | [diff] [blame] | 365 | else if (is_listobject(v)) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 366 | n = b->ob_size; |
Guido van Rossum | 32dffaa | 1991-12-24 13:27:34 +0000 | [diff] [blame] | 367 | if (a == b) { |
| 368 | /* Special case "a[i:j] = a" -- copy b first */ |
| 369 | int ret; |
| 370 | v = list_slice(b, 0, n); |
| 371 | ret = list_ass_slice(a, ilow, ihigh, v); |
| 372 | DECREF(v); |
| 373 | return ret; |
| 374 | } |
| 375 | } |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 376 | else { |
| 377 | err_badarg(); |
| 378 | return -1; |
| 379 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 380 | if (ilow < 0) |
| 381 | ilow = 0; |
| 382 | else if (ilow > a->ob_size) |
| 383 | ilow = a->ob_size; |
| 384 | if (ihigh < 0) |
| 385 | ihigh = 0; |
| 386 | if (ihigh < ilow) |
| 387 | ihigh = ilow; |
| 388 | else if (ihigh > a->ob_size) |
| 389 | ihigh = a->ob_size; |
| 390 | item = a->ob_item; |
| 391 | d = n - (ihigh-ilow); |
| 392 | if (d <= 0) { /* Delete -d items; DECREF ihigh-ilow items */ |
| 393 | for (k = ilow; k < ihigh; k++) |
| 394 | DECREF(item[k]); |
| 395 | if (d < 0) { |
| 396 | for (/*k = ihigh*/; k < a->ob_size; k++) |
| 397 | item[k+d] = item[k]; |
| 398 | a->ob_size += d; |
| 399 | RESIZE(item, object *, a->ob_size); /* Can't fail */ |
| 400 | a->ob_item = item; |
| 401 | } |
| 402 | } |
| 403 | else { /* Insert d items; DECREF ihigh-ilow items */ |
| 404 | RESIZE(item, object *, a->ob_size + d); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 405 | if (item == NULL) { |
| 406 | err_nomem(); |
| 407 | return -1; |
| 408 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 409 | for (k = a->ob_size; --k >= ihigh; ) |
| 410 | item[k+d] = item[k]; |
| 411 | for (/*k = ihigh-1*/; k >= ilow; --k) |
| 412 | DECREF(item[k]); |
| 413 | a->ob_item = item; |
| 414 | a->ob_size += d; |
| 415 | } |
| 416 | for (k = 0; k < n; k++, ilow++) { |
| 417 | object *w = b->ob_item[k]; |
| 418 | INCREF(w); |
| 419 | item[ilow] = w; |
| 420 | } |
| 421 | return 0; |
| 422 | #undef b |
| 423 | } |
| 424 | |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 425 | static int |
| 426 | list_ass_item(a, i, v) |
| 427 | listobject *a; |
| 428 | int i; |
| 429 | object *v; |
| 430 | { |
| 431 | if (i < 0 || i >= a->ob_size) { |
| 432 | err_setstr(IndexError, "list assignment index out of range"); |
| 433 | return -1; |
| 434 | } |
| 435 | if (v == NULL) |
| 436 | return list_ass_slice(a, i, i+1, v); |
| 437 | INCREF(v); |
| 438 | DECREF(a->ob_item[i]); |
| 439 | a->ob_item[i] = v; |
| 440 | return 0; |
| 441 | } |
| 442 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 443 | static object * |
| 444 | ins(self, where, v) |
| 445 | listobject *self; |
| 446 | int where; |
| 447 | object *v; |
| 448 | { |
| 449 | if (ins1(self, where, v) != 0) |
| 450 | return NULL; |
| 451 | INCREF(None); |
| 452 | return None; |
| 453 | } |
| 454 | |
| 455 | static object * |
| 456 | listinsert(self, args) |
| 457 | listobject *self; |
| 458 | object *args; |
| 459 | { |
| 460 | int i; |
| 461 | if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 462 | err_badarg(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 463 | return NULL; |
| 464 | } |
| 465 | if (!getintarg(gettupleitem(args, 0), &i)) |
| 466 | return NULL; |
| 467 | return ins(self, i, gettupleitem(args, 1)); |
| 468 | } |
| 469 | |
| 470 | static object * |
| 471 | listappend(self, args) |
| 472 | listobject *self; |
| 473 | object *args; |
| 474 | { |
| 475 | return ins(self, (int) self->ob_size, args); |
| 476 | } |
| 477 | |
Guido van Rossum | e10a19e | 1992-08-03 19:05:37 +0000 | [diff] [blame] | 478 | static object *cmpfunc; |
| 479 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 480 | static int |
| 481 | cmp(v, w) |
| 482 | char *v, *w; |
| 483 | { |
Guido van Rossum | e10a19e | 1992-08-03 19:05:37 +0000 | [diff] [blame] | 484 | object *t, *res; |
| 485 | long i; |
| 486 | |
| 487 | if (err_occurred()) |
| 488 | return 0; |
| 489 | |
| 490 | if (cmpfunc == NULL) |
| 491 | return cmpobject(* (object **) v, * (object **) w); |
| 492 | |
| 493 | /* Call the user-supplied comparison function */ |
| 494 | t = newtupleobject(2); |
| 495 | if (t == NULL) |
| 496 | return 0; |
| 497 | INCREF(* (object **) v); |
| 498 | settupleitem(t, 0, * (object **) v); |
| 499 | INCREF(* (object **) w); |
| 500 | settupleitem(t, 1, * (object **) w); |
| 501 | res = call_object(cmpfunc, t); |
| 502 | DECREF(t); |
| 503 | if (res == NULL) |
| 504 | return 0; |
| 505 | if (!is_intobject(res)) { |
| 506 | err_setstr(TypeError, "comparison function should return int"); |
| 507 | i = 0; |
| 508 | } |
| 509 | else { |
| 510 | i = getintvalue(res); |
| 511 | if (i < 0) |
| 512 | i = -1; |
| 513 | else if (i > 0) |
| 514 | i = 1; |
| 515 | } |
| 516 | DECREF(res); |
| 517 | return (int) i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | static object * |
| 521 | listsort(self, args) |
| 522 | listobject *self; |
| 523 | object *args; |
| 524 | { |
Guido van Rossum | e10a19e | 1992-08-03 19:05:37 +0000 | [diff] [blame] | 525 | object *save_cmpfunc; |
| 526 | if (self->ob_size <= 1) { |
| 527 | INCREF(None); |
| 528 | return None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 529 | } |
Guido van Rossum | e10a19e | 1992-08-03 19:05:37 +0000 | [diff] [blame] | 530 | save_cmpfunc = cmpfunc; |
| 531 | cmpfunc = args; |
| 532 | if (cmpfunc != NULL) { |
| 533 | /* Test the comparison function for obvious errors */ |
| 534 | (void) cmp(&self->ob_item[0], &self->ob_item[1]); |
| 535 | if (err_occurred()) { |
| 536 | cmpfunc = save_cmpfunc; |
| 537 | return NULL; |
| 538 | } |
| 539 | } |
| 540 | qsort((char *)self->ob_item, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 541 | (int) self->ob_size, sizeof(object *), cmp); |
Guido van Rossum | e10a19e | 1992-08-03 19:05:37 +0000 | [diff] [blame] | 542 | cmpfunc = save_cmpfunc; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 543 | if (err_occurred()) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 544 | return NULL; |
| 545 | INCREF(None); |
| 546 | return None; |
| 547 | } |
| 548 | |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 549 | static object * |
| 550 | listreverse(self, args) |
| 551 | listobject *self; |
| 552 | object *args; |
| 553 | { |
| 554 | register object **p, **q; |
| 555 | register object *tmp; |
| 556 | |
| 557 | if (args != NULL) { |
| 558 | err_badarg(); |
| 559 | return NULL; |
| 560 | } |
| 561 | |
| 562 | if (self->ob_size > 1) { |
| 563 | for (p = self->ob_item, q = self->ob_item + self->ob_size - 1; |
| 564 | p < q; p++, q--) { |
| 565 | tmp = *p; |
| 566 | *p = *q; |
| 567 | *q = tmp; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | INCREF(None); |
| 572 | return None; |
| 573 | } |
| 574 | |
Guido van Rossum | 84c76f5 | 1990-10-30 13:32:20 +0000 | [diff] [blame] | 575 | int |
| 576 | sortlist(v) |
| 577 | object *v; |
| 578 | { |
| 579 | if (v == NULL || !is_listobject(v)) { |
| 580 | err_badcall(); |
| 581 | return -1; |
| 582 | } |
| 583 | v = listsort((listobject *)v, (object *)NULL); |
| 584 | if (v == NULL) |
| 585 | return -1; |
| 586 | DECREF(v); |
| 587 | return 0; |
| 588 | } |
| 589 | |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 590 | static object * |
| 591 | listindex(self, args) |
| 592 | listobject *self; |
| 593 | object *args; |
| 594 | { |
| 595 | int i; |
| 596 | |
| 597 | if (args == NULL) { |
| 598 | err_badarg(); |
| 599 | return NULL; |
| 600 | } |
| 601 | for (i = 0; i < self->ob_size; i++) { |
| 602 | if (cmpobject(self->ob_item[i], args) == 0) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 603 | return newintobject((long)i); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 604 | } |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 605 | err_setstr(ValueError, "list.index(x): x not in list"); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 606 | return NULL; |
| 607 | } |
| 608 | |
| 609 | static object * |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 610 | listcount(self, args) |
| 611 | listobject *self; |
| 612 | object *args; |
| 613 | { |
| 614 | int count = 0; |
| 615 | int i; |
| 616 | |
| 617 | if (args == NULL) { |
| 618 | err_badarg(); |
| 619 | return NULL; |
| 620 | } |
| 621 | for (i = 0; i < self->ob_size; i++) { |
| 622 | if (cmpobject(self->ob_item[i], args) == 0) |
| 623 | count++; |
| 624 | } |
| 625 | return newintobject((long)count); |
| 626 | } |
| 627 | |
| 628 | static object * |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 629 | listremove(self, args) |
| 630 | listobject *self; |
| 631 | object *args; |
| 632 | { |
| 633 | int i; |
| 634 | |
| 635 | if (args == NULL) { |
| 636 | err_badarg(); |
| 637 | return NULL; |
| 638 | } |
| 639 | for (i = 0; i < self->ob_size; i++) { |
| 640 | if (cmpobject(self->ob_item[i], args) == 0) { |
| 641 | if (list_ass_slice(self, i, i+1, (object *)NULL) != 0) |
| 642 | return NULL; |
| 643 | INCREF(None); |
| 644 | return None; |
| 645 | } |
| 646 | |
| 647 | } |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 648 | err_setstr(ValueError, "list.remove(x): x not in list"); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 649 | return NULL; |
| 650 | } |
| 651 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 652 | static struct methodlist list_methods[] = { |
| 653 | {"append", listappend}, |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 654 | {"count", listcount}, |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 655 | {"index", listindex}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 656 | {"insert", listinsert}, |
| 657 | {"sort", listsort}, |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 658 | {"remove", listremove}, |
| 659 | {"reverse", listreverse}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 660 | {NULL, NULL} /* sentinel */ |
| 661 | }; |
| 662 | |
| 663 | static object * |
| 664 | list_getattr(f, name) |
| 665 | listobject *f; |
| 666 | char *name; |
| 667 | { |
| 668 | return findmethod(list_methods, (object *)f, name); |
| 669 | } |
| 670 | |
| 671 | static sequence_methods list_as_sequence = { |
| 672 | list_length, /*sq_length*/ |
| 673 | list_concat, /*sq_concat*/ |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 674 | list_repeat, /*sq_repeat*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 675 | list_item, /*sq_item*/ |
| 676 | list_slice, /*sq_slice*/ |
| 677 | list_ass_item, /*sq_ass_item*/ |
| 678 | list_ass_slice, /*sq_ass_slice*/ |
| 679 | }; |
| 680 | |
| 681 | typeobject Listtype = { |
| 682 | OB_HEAD_INIT(&Typetype) |
| 683 | 0, |
| 684 | "list", |
| 685 | sizeof(listobject), |
| 686 | 0, |
| 687 | list_dealloc, /*tp_dealloc*/ |
| 688 | list_print, /*tp_print*/ |
| 689 | list_getattr, /*tp_getattr*/ |
| 690 | 0, /*tp_setattr*/ |
| 691 | list_compare, /*tp_compare*/ |
| 692 | list_repr, /*tp_repr*/ |
| 693 | 0, /*tp_as_number*/ |
| 694 | &list_as_sequence, /*tp_as_sequence*/ |
| 695 | 0, /*tp_as_mapping*/ |
| 696 | }; |