Browse Source

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

pull/202/head
Paul Sokolovsky 11 years ago
parent
commit
439542f70c
  1. 3
      py/obj.h
  2. 9
      py/objlist.c
  3. 1
      py/py.mk
  4. 25
      py/sequence.c

3
py/obj.h

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

9
py/objlist.c

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

1
py/py.mk

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

@ -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;
}
}
Loading…
Cancel
Save