Commit 439542f7 authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

sequence.c: Start to refactor sequence operations for reuse among types.

parent 51ee44a7
......@@ -376,3 +376,6 @@ typedef struct _mp_obj_classmethod_t {
mp_obj_base_t base;
mp_obj_t fun;
} mp_obj_classmethod_t;
// sequence helpers
void mp_seq_multiply(const void *items, uint item_sz, uint len, uint times, void *dest);
......@@ -153,13 +153,8 @@ static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
return NULL;
}
int n = MP_OBJ_SMALL_INT_VALUE(rhs);
int len = o->len;
mp_obj_list_t *s = list_new(len * n);
mp_obj_t *dest = s->items;
for (int i = 0; i < n; i++) {
memcpy(dest, o->items, sizeof(mp_obj_t) * len);
dest += len;
}
mp_obj_list_t *s = list_new(o->len * n);
mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items);
return s;
}
case RT_COMPARE_OP_EQUAL:
......
......@@ -97,6 +97,7 @@ PY_O_BASENAME = \
objstr.o \
objtuple.o \
objtype.o \
sequence.o \
stream.o \
builtin.o \
builtinimport.o \
......
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
#include "mpqstr.h"
#include "obj.h"
#include "map.h"
#include "runtime0.h"
#include "runtime.h"
// Helpers for sequence types
// Implements backend of sequence * integer operation. Assumes elements are
// memory-adjacent in sequence.
void mp_seq_multiply(const void *items, uint item_sz, uint len, uint times, void *dest) {
for (int i = 0; i < times; i++) {
uint copy_sz = item_sz * len;
memcpy(dest, items, copy_sz);
dest = (char*)dest + copy_sz;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment