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

This commit is contained in:
Paul Sokolovsky 2014-01-21 00:19:19 +02:00
parent 51ee44a718
commit 439542f70c
4 changed files with 31 additions and 7 deletions

View File

@ -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);

View File

@ -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:

View File

@ -97,6 +97,7 @@ PY_O_BASENAME = \
objstr.o \
objtuple.o \
objtype.o \
sequence.o \
stream.o \
builtin.o \
builtinimport.o \

25
py/sequence.c Normal file
View File

@ -0,0 +1,25 @@
#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;
}
}