add array<->list converters

This commit is contained in:
Yuichi Nishiwaki 2014-07-15 22:20:32 +09:00
parent 2da5d440a8
commit d358c8873d
1 changed files with 21 additions and 4 deletions

View File

@ -12,9 +12,6 @@
(define (translate ary i)
(floor-remainder i (array-size ary)))
(define (make-array)
(create-array (vector) 0 0 0))
(define (array-length ary)
(let ((size (- (array-tail ary) (array-head ary))))
(translate ary size)))
@ -36,6 +33,9 @@
(make-vector (- size (array-size ary)))))
(set-array-size! ary size)))
(define (make-array . rest)
(create-array (vector) 0 0 0))
(define (array-ref ary i)
(let ((data (array-data ary)))
(vector-ref data (translate ary (+ (array-head ary) i)))))
@ -62,7 +62,22 @@
(array-set! ary -1 obj)
(set-array-head! ary (translate ary (- (array-head ary) 1))))
(define (array->list ary)
(do ((i 0 (+ i 1))
(x '() (cons (array-ref ary i) x)))
((= i (array-length ary))
(reverse x))))
(define (list->array list)
(let ((ary (make-array)))
(for-each (lambda (x) (array-push! ary x)) list)
ary))
(define (array . objs)
(list->array objs))
(export make-array
array
array?
array-length
array-ref
@ -70,4 +85,6 @@
array-push!
array-pop!
array-shift!
array-unshift!))
array-unshift!
array->list
list->array))