rewrite list<->vector converters in c
This commit is contained in:
parent
12fb80b857
commit
1e4bc04112
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
44
src/vector.c
44
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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue