From 1e4bc0411214f1013b8873872b8c832eaf80915c Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 21 Jul 2014 22:51:33 +0900 Subject: [PATCH] rewrite list<->vector converters in c --- piclib/picrin/macro.scm | 18 ----------------- piclib/prelude.scm | 41 ++------------------------------------ src/vector.c | 44 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 57 deletions(-) diff --git a/piclib/picrin/macro.scm b/piclib/picrin/macro.scm index 2f9fe7e0..5682d8ca 100644 --- a/piclib/picrin/macro.scm +++ b/piclib/picrin/macro.scm @@ -6,24 +6,6 @@ ;; assumes no derived expressions are provided yet - (define (list->vector list) - (define vector (make-vector (length list))) - (define (go list i) - (if (null? list) - vector - (begin - (vector-set! vector i (car list)) - (go (cdr list) (+ i 1))))) - (go list 0)) - - (define (vector->list vector) - (define (go i) - (if (= i (vector-length vector)) - '() - (cons (vector-ref vector i) - (go (+ i 1))))) - (go 0)) - (define (walk proc expr) "walk on symbols" (if (null? expr) diff --git a/piclib/prelude.scm b/piclib/prelude.scm index 7049c2f0..2f5f9dae 100644 --- a/piclib/prelude.scm +++ b/piclib/prelude.scm @@ -91,22 +91,6 @@ (r 'it) (cons (r 'or) (cdr exprs)))))))))) - (define (list->vector list) - (let ((vector (make-vector (length list)))) - (let loop ((list list) (i 0)) - (if (null? list) - vector - (begin - (vector-set! vector i (car list)) - (loop (cdr list) (+ i 1))))))) - - (define (vector->list vector) - (let ((length (vector-length vector))) - (let loop ((list '()) (i 0)) - (if (= i length) - (reverse list) - (loop (cons (vector-ref vector i) list) (+ i 1)))))) - (define-syntax quasiquote (ir-macro-transformer (lambda (form inject compare) @@ -714,27 +698,7 @@ ;;; 6.8. Vector (define (vector . objs) - (let ((len (length objs))) - (let ((v (make-vector len))) - (do ((i 0 (+ i 1)) - (l objs (cdr l))) - ((= i len) - v) - (vector-set! v i (car l)))))) - -(define (vector->list vector . opts) - (let ((start (if (pair? opts) (car opts) 0)) - (end (if (>= (length opts) 2) - (cadr opts) - (vector-length vector)))) - (do ((i start (+ i 1)) - (res '())) - ((= i end) - (reverse res)) - (set! res (cons (vector-ref vector i) res))))) - -(define (list->vector list) - (apply vector list)) + (list->vector objs)) (define (vector-copy! to at from . opts) (let* ((start (if (pair? opts) (car opts) 0)) @@ -785,8 +749,7 @@ (define (string->vector . args) (list->vector (apply string->list args))) -(export vector vector->list list->vector - vector-copy! vector-copy +(export vector vector-copy! vector-copy vector-append vector-fill! vector->string string->vector) diff --git a/src/vector.c b/src/vector.c index 917f0878..1315753f 100644 --- a/src/vector.c +++ b/src/vector.c @@ -119,6 +119,48 @@ pic_vec_vector_set(pic_state *pic) return pic_none_value(); } +static pic_value +pic_vec_list_to_vector(pic_state *pic) +{ + struct pic_vector *vec; + pic_value list, e, *data; + + pic_get_args(pic, "o", &list); + + vec = pic_vec_new(pic, pic_length(pic, list)); + + data = vec->data; + + pic_for_each (e, list) { + *data++ = e; + } + return pic_obj_value(vec); +} + +static pic_value +pic_vec_vector_to_list(pic_state *pic) +{ + struct pic_vector *vec; + pic_value list; + int n, start, end, i; + + n = pic_get_args(pic, "v|ii", &vec, &start, &end); + + switch (n) { + case 1: + start = 0; + case 2: + end = vec->len; + } + + list = pic_nil_value(); + + for (i = start; i < end; ++i) { + pic_push(pic, vec->data[i], list); + } + return pic_reverse(pic, list); +} + void pic_init_vector(pic_state *pic) { @@ -127,4 +169,6 @@ pic_init_vector(pic_state *pic) pic_defun(pic, "vector-length", pic_vec_vector_length); pic_defun(pic, "vector-ref", pic_vec_vector_ref); pic_defun(pic, "vector-set!", pic_vec_vector_set); + pic_defun(pic, "list->vector", pic_vec_list_to_vector); + pic_defun(pic, "vector->list", pic_vec_vector_to_list); }