Mercurial > cpython-lru_cache-weakref
annotate Objects/listobject.c @ 1330:409ea74feb21 legacy-trunk
Minor fixes / changes for Mac compatibility.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 29 Jul 1993 08:25:09 +0000 |
| parents | 6ee380349c84 |
| children | a22a350d3a5d |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
1198
2937897a7d8b
* Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents:
1177
diff
changeset
|
2 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
|
2937897a7d8b
* Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents:
1177
diff
changeset
|
3 Amsterdam, The Netherlands. |
| 145 | 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 | |
| 6 | 25 /* List object implementation */ |
| 26 | |
| 86 | 27 #include "allobjects.h" |
|
645
c0e7b0b9b750
Include modsupport.h for getargs().
Guido van Rossum <guido@python.org>
parents:
511
diff
changeset
|
28 #include "modsupport.h" |
| 926 | 29 #include "ceval.h" |
| 6 | 30 |
| 31 object * | |
| 32 newlistobject(size) | |
| 33 int size; | |
| 34 { | |
| 35 int i; | |
| 36 listobject *op; | |
|
978
13c99eaf32a8
* renamed malloc.h mymalloc.h, and added MALLARG as the type of the
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
37 MALLARG nbytes; |
| 6 | 38 if (size < 0) { |
| 27 | 39 err_badcall(); |
| 6 | 40 return NULL; |
| 41 } | |
|
978
13c99eaf32a8
* renamed malloc.h mymalloc.h, and added MALLARG as the type of the
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
42 nbytes = size * sizeof(object *); |
|
13c99eaf32a8
* renamed malloc.h mymalloc.h, and added MALLARG as the type of the
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
43 /* Check for overflow */ |
|
13c99eaf32a8
* renamed malloc.h mymalloc.h, and added MALLARG as the type of the
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
44 if (nbytes / sizeof(object *) != size) { |
|
13c99eaf32a8
* renamed malloc.h mymalloc.h, and added MALLARG as the type of the
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
45 return err_nomem(); |
|
13c99eaf32a8
* renamed malloc.h mymalloc.h, and added MALLARG as the type of the
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
46 } |
| 6 | 47 op = (listobject *) malloc(sizeof(listobject)); |
| 48 if (op == NULL) { | |
| 27 | 49 return err_nomem(); |
| 6 | 50 } |
| 51 if (size <= 0) { | |
| 52 op->ob_item = NULL; | |
| 53 } | |
| 54 else { | |
|
978
13c99eaf32a8
* renamed malloc.h mymalloc.h, and added MALLARG as the type of the
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
55 op->ob_item = (object **) malloc(nbytes); |
| 6 | 56 if (op->ob_item == NULL) { |
| 57 free((ANY *)op); | |
| 27 | 58 return err_nomem(); |
| 6 | 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)) { | |
| 27 | 74 err_badcall(); |
| 6 | 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)) { | |
| 27 | 87 err_badcall(); |
| 6 | 88 return NULL; |
| 89 } | |
| 90 if (i < 0 || i >= ((listobject *)op) -> ob_size) { | |
| 27 | 91 err_setstr(IndexError, "list index out of range"); |
| 6 | 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)) { | |
| 394 | 105 XDECREF(newitem); |
| 27 | 106 err_badcall(); |
| 107 return -1; | |
| 6 | 108 } |
| 109 if (i < 0 || i >= ((listobject *)op) -> ob_size) { | |
| 394 | 110 XDECREF(newitem); |
| 27 | 111 err_setstr(IndexError, "list assignment index out of range"); |
| 112 return -1; | |
| 6 | 113 } |
| 114 olditem = ((listobject *)op) -> ob_item[i]; | |
| 115 ((listobject *)op) -> ob_item[i] = newitem; | |
| 394 | 116 XDECREF(olditem); |
| 6 | 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; | |
| 27 | 128 if (v == NULL) { |
| 129 err_badcall(); | |
| 130 return -1; | |
| 131 } | |
| 6 | 132 items = self->ob_item; |
| 133 RESIZE(items, object *, self->ob_size+1); | |
| 27 | 134 if (items == NULL) { |
| 135 err_nomem(); | |
| 136 return -1; | |
| 137 } | |
| 6 | 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 { | |
| 27 | 157 if (!is_listobject(op)) { |
| 158 err_badcall(); | |
| 159 return -1; | |
| 160 } | |
| 6 | 161 return ins1((listobject *)op, where, newitem); |
| 162 } | |
| 163 | |
| 164 int | |
| 165 addlistitem(op, newitem) | |
| 166 object *op; | |
| 167 object *newitem; | |
| 168 { | |
| 27 | 169 if (!is_listobject(op)) { |
| 170 err_badcall(); | |
| 171 return -1; | |
| 172 } | |
| 6 | 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++) { | |
| 394 | 185 XDECREF(op->ob_item[i]); |
| 6 | 186 } |
| 187 if (op->ob_item != NULL) | |
| 188 free((ANY *)op->ob_item); | |
| 189 free((ANY *)op); | |
| 190 } | |
| 191 | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
205
diff
changeset
|
192 static int |
| 6 | 193 list_print(op, fp, flags) |
| 194 listobject *op; | |
| 195 FILE *fp; | |
| 196 int flags; | |
| 197 { | |
| 198 int i; | |
| 199 fprintf(fp, "["); | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
205
diff
changeset
|
200 for (i = 0; i < op->ob_size; i++) { |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
205
diff
changeset
|
201 if (i > 0) |
| 6 | 202 fprintf(fp, ", "); |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
205
diff
changeset
|
203 if (printobject(op->ob_item[i], fp, flags) != 0) |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
205
diff
changeset
|
204 return -1; |
| 6 | 205 } |
| 206 fprintf(fp, "]"); | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
205
diff
changeset
|
207 return 0; |
| 6 | 208 } |
| 209 | |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
210 static object * |
| 6 | 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) { | |
| 27 | 259 err_setstr(IndexError, "list index out of range"); |
| 6 | 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 | |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
294 object * |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
295 getlistslice(a, ilow, ihigh) |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
296 object *a; |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
297 int ilow, ihigh; |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
298 { |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
299 if (!is_listobject(a)) { |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
300 err_badcall(); |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
301 return NULL; |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
302 } |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
303 return list_slice((listobject *)a, ilow, ihigh); |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
304 } |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
305 |
| 6 | 306 static object * |
| 307 list_concat(a, bb) | |
| 308 listobject *a; | |
| 309 object *bb; | |
| 310 { | |
| 311 int size; | |
| 312 int i; | |
| 313 listobject *np; | |
| 314 if (!is_listobject(bb)) { | |
| 27 | 315 err_badarg(); |
| 6 | 316 return NULL; |
| 317 } | |
| 318 #define b ((listobject *)bb) | |
| 319 size = a->ob_size + b->ob_size; | |
| 320 np = (listobject *) newlistobject(size); | |
| 321 if (np == NULL) { | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
205
diff
changeset
|
322 return NULL; |
| 6 | 323 } |
| 324 for (i = 0; i < a->ob_size; i++) { | |
| 325 object *v = a->ob_item[i]; | |
| 326 INCREF(v); | |
| 327 np->ob_item[i] = v; | |
| 328 } | |
| 329 for (i = 0; i < b->ob_size; i++) { | |
| 330 object *v = b->ob_item[i]; | |
| 331 INCREF(v); | |
| 332 np->ob_item[i + a->ob_size] = v; | |
| 333 } | |
| 334 return (object *)np; | |
| 335 #undef b | |
| 336 } | |
| 337 | |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
338 static object * |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
339 list_repeat(a, n) |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
340 listobject *a; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
341 int n; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
342 { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
343 int i, j; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
344 int size; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
345 listobject *np; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
346 object **p; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
347 if (n < 0) |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
348 n = 0; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
349 size = a->ob_size * n; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
350 np = (listobject *) newlistobject(size); |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
351 if (np == NULL) |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
352 return NULL; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
353 p = np->ob_item; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
354 for (i = 0; i < n; i++) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
355 for (j = 0; j < a->ob_size; j++) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
356 *p = a->ob_item[j]; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
357 INCREF(*p); |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
358 p++; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
359 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
360 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
361 return (object *) np; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
362 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
363 |
| 6 | 364 static int |
| 365 list_ass_slice(a, ilow, ihigh, v) | |
| 366 listobject *a; | |
| 367 int ilow, ihigh; | |
| 368 object *v; | |
| 369 { | |
| 370 object **item; | |
| 371 int n; /* Size of replacement list */ | |
| 372 int d; /* Change in size */ | |
| 373 int k; /* Loop index */ | |
| 374 #define b ((listobject *)v) | |
| 375 if (v == NULL) | |
| 376 n = 0; | |
|
511
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
377 else if (is_listobject(v)) { |
| 6 | 378 n = b->ob_size; |
|
511
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
379 if (a == b) { |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
380 /* Special case "a[i:j] = a" -- copy b first */ |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
381 int ret; |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
382 v = list_slice(b, 0, n); |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
383 ret = list_ass_slice(a, ilow, ihigh, v); |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
384 DECREF(v); |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
385 return ret; |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
386 } |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
387 } |
| 27 | 388 else { |
| 389 err_badarg(); | |
| 390 return -1; | |
| 391 } | |
| 6 | 392 if (ilow < 0) |
| 393 ilow = 0; | |
| 394 else if (ilow > a->ob_size) | |
| 395 ilow = a->ob_size; | |
| 396 if (ihigh < 0) | |
| 397 ihigh = 0; | |
| 398 if (ihigh < ilow) | |
| 399 ihigh = ilow; | |
| 400 else if (ihigh > a->ob_size) | |
| 401 ihigh = a->ob_size; | |
| 402 item = a->ob_item; | |
| 403 d = n - (ihigh-ilow); | |
| 404 if (d <= 0) { /* Delete -d items; DECREF ihigh-ilow items */ | |
| 405 for (k = ilow; k < ihigh; k++) | |
| 406 DECREF(item[k]); | |
| 407 if (d < 0) { | |
| 408 for (/*k = ihigh*/; k < a->ob_size; k++) | |
| 409 item[k+d] = item[k]; | |
| 410 a->ob_size += d; | |
| 411 RESIZE(item, object *, a->ob_size); /* Can't fail */ | |
| 412 a->ob_item = item; | |
| 413 } | |
| 414 } | |
| 415 else { /* Insert d items; DECREF ihigh-ilow items */ | |
| 416 RESIZE(item, object *, a->ob_size + d); | |
| 27 | 417 if (item == NULL) { |
| 418 err_nomem(); | |
| 419 return -1; | |
| 420 } | |
| 6 | 421 for (k = a->ob_size; --k >= ihigh; ) |
| 422 item[k+d] = item[k]; | |
| 423 for (/*k = ihigh-1*/; k >= ilow; --k) | |
| 424 DECREF(item[k]); | |
| 425 a->ob_item = item; | |
| 426 a->ob_size += d; | |
| 427 } | |
| 428 for (k = 0; k < n; k++, ilow++) { | |
| 429 object *w = b->ob_item[k]; | |
| 430 INCREF(w); | |
| 431 item[ilow] = w; | |
| 432 } | |
| 433 return 0; | |
| 434 #undef b | |
| 435 } | |
| 436 | |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
437 int |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
438 setlistslice(a, ilow, ihigh, v) |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
439 object *a; |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
440 int ilow, ihigh; |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
441 object *v; |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
442 { |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
443 if (!is_listobject(a)) { |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
444 err_badcall(); |
|
1330
409ea74feb21
Minor fixes / changes for Mac compatibility.
Guido van Rossum <guido@python.org>
parents:
1313
diff
changeset
|
445 return -1; |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
446 } |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
447 return list_ass_slice((listobject *)a, ilow, ihigh, v); |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
448 } |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
449 |
|
167
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
450 static int |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
451 list_ass_item(a, i, v) |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
452 listobject *a; |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
453 int i; |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
454 object *v; |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
455 { |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
456 if (i < 0 || i >= a->ob_size) { |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
457 err_setstr(IndexError, "list assignment index out of range"); |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
458 return -1; |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
459 } |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
460 if (v == NULL) |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
461 return list_ass_slice(a, i, i+1, v); |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
462 INCREF(v); |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
463 DECREF(a->ob_item[i]); |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
464 a->ob_item[i] = v; |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
465 return 0; |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
466 } |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
467 |
| 6 | 468 static object * |
| 469 ins(self, where, v) | |
| 470 listobject *self; | |
| 471 int where; | |
| 472 object *v; | |
| 473 { | |
| 474 if (ins1(self, where, v) != 0) | |
| 475 return NULL; | |
| 476 INCREF(None); | |
| 477 return None; | |
| 478 } | |
| 479 | |
| 480 static object * | |
| 481 listinsert(self, args) | |
| 482 listobject *self; | |
| 483 object *args; | |
| 484 { | |
| 485 int i; | |
|
1211
406860dfaac7
* stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
486 object *v; |
|
406860dfaac7
* stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
487 if (!getargs(args, "(iO)", &i, &v)) |
| 6 | 488 return NULL; |
|
1211
406860dfaac7
* stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
489 return ins(self, i, v); |
| 6 | 490 } |
| 491 | |
| 492 static object * | |
| 493 listappend(self, args) | |
| 494 listobject *self; | |
| 495 object *args; | |
| 496 { | |
|
1211
406860dfaac7
* stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
497 object *v; |
|
406860dfaac7
* stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
498 if (!getargs(args, "O", &v)) |
|
406860dfaac7
* stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
499 return NULL; |
|
406860dfaac7
* stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
500 return ins(self, (int) self->ob_size, v); |
| 6 | 501 } |
| 502 | |
|
920
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
503 static object *cmpfunc; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
504 |
| 6 | 505 static int |
| 506 cmp(v, w) | |
|
1177
533e36de1e4d
* Configure.py: use #!/usr/local/bin/python
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
507 #ifdef __STDC__ |
|
533e36de1e4d
* Configure.py: use #!/usr/local/bin/python
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
508 void *v, *w; |
|
533e36de1e4d
* Configure.py: use #!/usr/local/bin/python
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
509 #else |
| 6 | 510 char *v, *w; |
|
1177
533e36de1e4d
* Configure.py: use #!/usr/local/bin/python
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
511 #endif |
| 6 | 512 { |
|
920
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
513 object *t, *res; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
514 long i; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
515 |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
516 if (err_occurred()) |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
517 return 0; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
518 |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
519 if (cmpfunc == NULL) |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
520 return cmpobject(* (object **) v, * (object **) w); |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
521 |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
522 /* Call the user-supplied comparison function */ |
|
1211
406860dfaac7
* stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
523 t = mkvalue("OO", v, w); |
|
920
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
524 if (t == NULL) |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
525 return 0; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
526 res = call_object(cmpfunc, t); |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
527 DECREF(t); |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
528 if (res == NULL) |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
529 return 0; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
530 if (!is_intobject(res)) { |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
531 err_setstr(TypeError, "comparison function should return int"); |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
532 i = 0; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
533 } |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
534 else { |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
535 i = getintvalue(res); |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
536 if (i < 0) |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
537 i = -1; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
538 else if (i > 0) |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
539 i = 1; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
540 } |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
541 DECREF(res); |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
542 return (int) i; |
| 6 | 543 } |
| 544 | |
| 545 static object * | |
| 546 listsort(self, args) | |
| 547 listobject *self; | |
| 548 object *args; | |
| 549 { | |
|
920
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
550 object *save_cmpfunc; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
551 if (self->ob_size <= 1) { |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
552 INCREF(None); |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
553 return None; |
| 6 | 554 } |
|
920
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
555 save_cmpfunc = cmpfunc; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
556 cmpfunc = args; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
557 if (cmpfunc != NULL) { |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
558 /* Test the comparison function for obvious errors */ |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
559 (void) cmp(&self->ob_item[0], &self->ob_item[1]); |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
560 if (err_occurred()) { |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
561 cmpfunc = save_cmpfunc; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
562 return NULL; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
563 } |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
564 } |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
565 qsort((char *)self->ob_item, |
| 6 | 566 (int) self->ob_size, sizeof(object *), cmp); |
|
920
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
567 cmpfunc = save_cmpfunc; |
| 27 | 568 if (err_occurred()) |
| 6 | 569 return NULL; |
| 570 INCREF(None); | |
| 571 return None; | |
| 572 } | |
| 573 | |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
574 static object * |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
575 listreverse(self, args) |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
576 listobject *self; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
577 object *args; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
578 { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
579 register object **p, **q; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
580 register object *tmp; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
581 |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
582 if (args != NULL) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
583 err_badarg(); |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
584 return NULL; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
585 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
586 |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
587 if (self->ob_size > 1) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
588 for (p = self->ob_item, q = self->ob_item + self->ob_size - 1; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
589 p < q; p++, q--) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
590 tmp = *p; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
591 *p = *q; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
592 *q = tmp; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
593 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
594 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
595 |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
596 INCREF(None); |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
597 return None; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
598 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
599 |
|
47
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
600 int |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
601 sortlist(v) |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
602 object *v; |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
603 { |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
604 if (v == NULL || !is_listobject(v)) { |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
605 err_badcall(); |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
606 return -1; |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
607 } |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
608 v = listsort((listobject *)v, (object *)NULL); |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
609 if (v == NULL) |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
610 return -1; |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
611 DECREF(v); |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
612 return 0; |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
613 } |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
614 |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
615 static object * |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
616 listindex(self, args) |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
617 listobject *self; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
618 object *args; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
619 { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
620 int i; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
621 |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
622 if (args == NULL) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
623 err_badarg(); |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
624 return NULL; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
625 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
626 for (i = 0; i < self->ob_size; i++) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
627 if (cmpobject(self->ob_item[i], args) == 0) |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
1024
diff
changeset
|
628 return newintobject((long)i); |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
629 } |
| 475 | 630 err_setstr(ValueError, "list.index(x): x not in list"); |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
631 return NULL; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
632 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
633 |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
634 static object * |
| 394 | 635 listcount(self, args) |
| 636 listobject *self; | |
| 637 object *args; | |
| 638 { | |
| 639 int count = 0; | |
| 640 int i; | |
| 641 | |
| 642 if (args == NULL) { | |
| 643 err_badarg(); | |
| 644 return NULL; | |
| 645 } | |
| 646 for (i = 0; i < self->ob_size; i++) { | |
| 647 if (cmpobject(self->ob_item[i], args) == 0) | |
| 648 count++; | |
| 649 } | |
| 650 return newintobject((long)count); | |
| 651 } | |
| 652 | |
| 653 static object * | |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
654 listremove(self, args) |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
655 listobject *self; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
656 object *args; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
657 { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
658 int i; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
659 |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
660 if (args == NULL) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
661 err_badarg(); |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
662 return NULL; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
663 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
664 for (i = 0; i < self->ob_size; i++) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
665 if (cmpobject(self->ob_item[i], args) == 0) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
666 if (list_ass_slice(self, i, i+1, (object *)NULL) != 0) |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
667 return NULL; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
668 INCREF(None); |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
669 return None; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
670 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
671 |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
672 } |
| 475 | 673 err_setstr(ValueError, "list.remove(x): x not in list"); |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
674 return NULL; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
675 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
676 |
| 6 | 677 static struct methodlist list_methods[] = { |
| 678 {"append", listappend}, | |
| 394 | 679 {"count", listcount}, |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
680 {"index", listindex}, |
| 6 | 681 {"insert", listinsert}, |
| 682 {"sort", listsort}, | |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
683 {"remove", listremove}, |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
684 {"reverse", listreverse}, |
| 6 | 685 {NULL, NULL} /* sentinel */ |
| 686 }; | |
| 687 | |
| 688 static object * | |
| 689 list_getattr(f, name) | |
| 690 listobject *f; | |
| 691 char *name; | |
| 692 { | |
| 693 return findmethod(list_methods, (object *)f, name); | |
| 694 } | |
| 695 | |
| 696 static sequence_methods list_as_sequence = { | |
| 697 list_length, /*sq_length*/ | |
| 698 list_concat, /*sq_concat*/ | |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
699 list_repeat, /*sq_repeat*/ |
| 6 | 700 list_item, /*sq_item*/ |
| 701 list_slice, /*sq_slice*/ | |
| 702 list_ass_item, /*sq_ass_item*/ | |
| 703 list_ass_slice, /*sq_ass_slice*/ | |
| 704 }; | |
| 705 | |
| 706 typeobject Listtype = { | |
| 707 OB_HEAD_INIT(&Typetype) | |
| 708 0, | |
| 709 "list", | |
| 710 sizeof(listobject), | |
| 711 0, | |
| 712 list_dealloc, /*tp_dealloc*/ | |
| 713 list_print, /*tp_print*/ | |
| 714 list_getattr, /*tp_getattr*/ | |
| 715 0, /*tp_setattr*/ | |
| 716 list_compare, /*tp_compare*/ | |
| 717 list_repr, /*tp_repr*/ | |
| 718 0, /*tp_as_number*/ | |
| 719 &list_as_sequence, /*tp_as_sequence*/ | |
| 720 0, /*tp_as_mapping*/ | |
| 721 }; |
