Box* _listSlice(BoxedList* self, i64 start, i64 stop, i64 step) { // printf("%ld %ld %ld\n", start, stop, step); assert(step != 0); if (step > 0) { assert(0 <= start); assert(stop <= self->size); } else { assert(start < self->size); assert(-1 <= stop); } BoxedList* rtn = new BoxedList(); if ((step == 1) && ((stop - start) > 0)) { listAppendArrayInternal(rtn, &self->elts->elts[start], stop - start); } else { int cur = start; while ((step > 0 && cur < stop) || (step < 0 && cur > stop)) { listAppendInternal(rtn, self->elts->elts[cur]); cur += step; } } return rtn; }
Box* listMul(BoxedList* self, Box* rhs) { if (rhs->cls != int_cls) { raiseExcHelper(TypeError, "can't multiply sequence by non-int of type '%s'", getTypeName(rhs)->c_str()); } LOCK_REGION(self->lock.asRead()); int n = static_cast<BoxedInt*>(rhs)->n; int s = self->size; BoxedList* rtn = new BoxedList(); rtn->ensure(n * s); if (s == 1) { for (int i = 0; i < n; i++) { listAppendInternal(rtn, self->elts->elts[0]); } } else { for (int i = 0; i < n; i++) { listAppendArrayInternal(rtn, &self->elts->elts[0], s); } } return rtn; }