From 4676550961c10b5aec8761c52154c88153bbd4be Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 22 Jul 2014 14:08:48 +0900 Subject: [PATCH] rewrite vector-append in c --- piclib/prelude.scm | 12 +----------- src/vector.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/piclib/prelude.scm b/piclib/prelude.scm index e2aa17d5..71e5a11a 100644 --- a/piclib/prelude.scm +++ b/piclib/prelude.scm @@ -639,23 +639,13 @@ (define (vector . objs) (list->vector objs)) -(define (vector-append . vs) - (define (vector-append-2-inv w v) - (let ((res (make-vector (+ (vector-length v) (vector-length w))))) - (vector-copy! res 0 v) - (vector-copy! res (vector-length v) w) - res)) - (fold vector-append-2-inv #() vs)) - (define (vector->string . args) (list->string (apply vector->list args))) (define (string->vector . args) (list->vector (apply string->list args))) -(export vector vector-copy! vector-copy - vector-append vector-fill! - vector->string string->vector) +(export vector vector->string string->vector) ;;; 6.9 bytevector diff --git a/src/vector.c b/src/vector.c index 8fe9392c..d57214e7 100644 --- a/src/vector.c +++ b/src/vector.c @@ -173,6 +173,34 @@ pic_vec_vector_copy(pic_state *pic) return pic_obj_value(to); } +static pic_value +pic_vec_vector_append(pic_state *pic) +{ + size_t argc, i, j, len; + pic_value *argv; + pic_vec *vec; + + pic_get_args(pic, "*", &argc, &argv); + + len = 0; + for (i = 0; i < argc; ++i) { + pic_assert_type(pic, argv[i], vec); + len += pic_vec_ptr(argv[i])->len; + } + + vec = pic_vec_new(pic, len); + + len = 0; + for (i = 0; i < argc; ++i) { + for (j = 0; j < pic_vec_ptr(argv[i])->len; ++j) { + vec->data[len + j] = pic_vec_ptr(argv[i])->data[j]; + } + len += pic_vec_ptr(argv[i])->len; + } + + return pic_obj_value(vec); +} + static pic_value pic_vec_vector_fill_i(pic_state *pic) { @@ -248,6 +276,7 @@ pic_init_vector(pic_state *pic) pic_defun(pic, "vector-set!", pic_vec_vector_set); pic_defun(pic, "vector-copy!", pic_vec_vector_copy_i); pic_defun(pic, "vector-copy", pic_vec_vector_copy); + pic_defun(pic, "vector-append", pic_vec_vector_append); pic_defun(pic, "vector-fill!", pic_vec_vector_fill_i); pic_defun(pic, "list->vector", pic_vec_list_to_vector); pic_defun(pic, "vector->list", pic_vec_vector_to_list);