2014-07-15 07:59:29 -04:00
|
|
|
(define-library (picrin array)
|
2015-07-08 12:59:14 -04:00
|
|
|
(import (scheme base))
|
2014-07-15 07:59:29 -04:00
|
|
|
|
2014-08-05 22:33:07 -04:00
|
|
|
(define-record-type <array>
|
2014-07-15 07:59:29 -04:00
|
|
|
(create-array data size head tail)
|
|
|
|
array?
|
|
|
|
(data array-data set-array-data!)
|
|
|
|
(size array-size set-array-size!)
|
|
|
|
(head array-head set-array-head!)
|
|
|
|
(tail array-tail set-array-tail!))
|
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
(define (floor-remainder i j)
|
|
|
|
(call-with-values (lambda () (floor/ i j))
|
|
|
|
(lambda (q r)
|
|
|
|
r)))
|
|
|
|
|
2014-07-15 07:59:29 -04:00
|
|
|
(define (translate ary i)
|
|
|
|
(floor-remainder i (array-size ary)))
|
|
|
|
|
|
|
|
(define (array-length ary)
|
|
|
|
(let ((size (- (array-tail ary) (array-head ary))))
|
|
|
|
(translate ary size)))
|
|
|
|
|
|
|
|
(define (array-rotate! ary)
|
|
|
|
(when (< (array-tail ary) (array-head ary))
|
|
|
|
(let ((xs (vector-copy (array-data ary) 0 (array-head ary)))
|
|
|
|
(ys (vector-copy (array-data ary) (array-head ary))))
|
|
|
|
(set-array-data! ary (vector-append ys xs))
|
|
|
|
(set-array-tail! ary (array-length ary))
|
|
|
|
(set-array-head! ary 0))))
|
|
|
|
|
|
|
|
(define (array-reserve! ary size)
|
|
|
|
(set! size (+ size 1)) ; capa == size - 1
|
|
|
|
(when (< (array-size ary) size)
|
|
|
|
(array-rotate! ary)
|
2014-07-15 08:26:08 -04:00
|
|
|
(set-array-data! ary (vector-append
|
|
|
|
(array-data ary)
|
|
|
|
(make-vector (- size (array-size ary)))))
|
2014-07-15 07:59:29 -04:00
|
|
|
(set-array-size! ary size)))
|
|
|
|
|
2014-07-15 09:20:32 -04:00
|
|
|
(define (make-array . rest)
|
2014-07-15 09:27:48 -04:00
|
|
|
(if (null? rest)
|
|
|
|
(make-array 0)
|
|
|
|
(let ((capacity (car rest))
|
2014-09-08 07:20:08 -04:00
|
|
|
(ary (create-array (make-vector 0) 0 0 0)))
|
2014-07-15 09:27:48 -04:00
|
|
|
(array-reserve! ary capacity)
|
|
|
|
ary)))
|
2014-07-15 09:20:32 -04:00
|
|
|
|
2014-07-15 07:59:29 -04:00
|
|
|
(define (array-ref ary i)
|
|
|
|
(let ((data (array-data ary)))
|
|
|
|
(vector-ref data (translate ary (+ (array-head ary) i)))))
|
|
|
|
|
|
|
|
(define (array-set! ary i obj)
|
|
|
|
(let ((data (array-data ary)))
|
|
|
|
(vector-set! data (translate ary (+ (array-head ary) i)) obj)))
|
|
|
|
|
|
|
|
(define (array-push! ary obj)
|
|
|
|
(array-reserve! ary (+ (array-length ary) 1))
|
|
|
|
(array-set! ary (array-length ary) obj)
|
|
|
|
(set-array-tail! ary (translate ary (+ (array-tail ary) 1))))
|
|
|
|
|
2014-07-15 08:26:08 -04:00
|
|
|
(define (array-pop! ary)
|
|
|
|
(set-array-tail! ary (translate ary (- (array-tail ary) 1)))
|
|
|
|
(array-ref ary (array-length ary)))
|
|
|
|
|
|
|
|
(define (array-shift! ary)
|
|
|
|
(set-array-head! ary (translate ary (+ (array-head ary) 1)))
|
|
|
|
(array-ref ary -1))
|
|
|
|
|
|
|
|
(define (array-unshift! ary obj)
|
|
|
|
(array-reserve! ary (+ (array-length ary) 1))
|
|
|
|
(array-set! ary -1 obj)
|
|
|
|
(set-array-head! ary (translate ary (- (array-head ary) 1))))
|
|
|
|
|
2014-07-15 09:20:32 -04:00
|
|
|
(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))
|
|
|
|
|
2014-07-15 09:45:04 -04:00
|
|
|
(define (array-map proc ary)
|
|
|
|
(list->array (map proc (array->list ary))))
|
|
|
|
|
|
|
|
(define (array-for-each proc ary)
|
|
|
|
(for-each proc (array->list ary)))
|
|
|
|
|
2014-07-15 07:59:29 -04:00
|
|
|
(export make-array
|
2014-07-15 09:20:32 -04:00
|
|
|
array
|
2014-07-15 07:59:29 -04:00
|
|
|
array?
|
|
|
|
array-length
|
|
|
|
array-ref
|
|
|
|
array-set!
|
2014-07-15 08:26:08 -04:00
|
|
|
array-push!
|
|
|
|
array-pop!
|
|
|
|
array-shift!
|
2014-07-15 09:20:32 -04:00
|
|
|
array-unshift!
|
2014-07-15 09:45:04 -04:00
|
|
|
array-map
|
|
|
|
array-for-each
|
2014-07-15 09:20:32 -04:00
|
|
|
array->list
|
|
|
|
list->array))
|