home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frozen Fish 2: PC
/
frozenfish_august_1995.bin
/
bbs
/
d09xx
/
d0963.lha
/
SIOD
/
scm
/
bi-ar.s
< prev
next >
Wrap
Text File
|
1993-10-01
|
9KB
|
187 lines
(define archivio-environment
(make-environment
(define (binsearch archivio elem last ord-test eq-test)
(let ((first 0))
(do ((mid (quotient (+ first last) 2)
(quotient (+ first last) 2)))
((or (>= mid last)
(<= mid first)
(eq-test elem (vector-ref archivio mid)))
(if (eq-test elem (vector-ref archivio mid))
(do ()
((or (= mid 0)
(not (eq-test elem
(vector-ref archivio
(-1+ mid)))))
mid)
(set! mid (-1+ mid)))
'not-found))
(if (ord-test (vector-ref archivio mid) elem)
(set! first mid)
(set! last mid)))))
(define (interchange archivio i j)
(define tmp (vector-ref archivio i))
(vector-set! archivio i (vector-ref archivio j))
(vector-set! archivio j tmp))
(define (qsort archivio m n ord-test)
(if (< m n)
(do ((i m)
(j (1+ n))
(k (begin (interchange archivio
m
(quotient (+ m n)
2))
(vector-ref archivio m))))
((>= i j) (interchange archivio m j)
(qsort archivio m (-1+ j) ord-test)
(qsort archivio (1+ j) n ord-test))
(set! i (1+ i))
(do ()
((or (ord-test k (vector-ref archivio i))
(>= i n)))
(set! i (1+ i)))
(set! j (-1+ j))
(do ()
((or (ord-test (vector-ref archivio j) k)
(<= j m)))
(set! j (-1+ j)))
(if (< i j)
(interchange archivio i j)))))
(define (delete-el! archivio index last)
(do ()
((= last index))
(vector-set! archivio
index
(vector-ref archivio (1+ index)))
(set! index (1+ index))))
(define (ar-for-each archivio fun last)
(do ((i 0 (1+ i)))
((= i last))
(fun (vector-ref archivio i))))
(define (insert-el! archivio y last ord-test)
(do ()
((or (= last 0)
(ord-test (vector-ref archivio (-1+ last)) y))
(vector-set! archivio last y))
(vector-set! archivio
last
(vector-ref archivio (-1+ last)))
(set! last (-1+ last))))
(define (load-ar nome)
(define port nil)
(define res nil)
(if (file-exists? nome)
(begin (set! port (open-input-file nome))
(if (eq? (read port) 'archivio-v1.0)
(begin (set! res (cons (read port) res))
(set! res (cons (read port) res))
(set! res (cons (read port) res))
(set! res (cons (read port) res))
(close-input-port port)
res)
(begin (close-input-port port)
'not-archive-v1.0)))
'not-found))
(define (save-ar archivio last nome user-data order)
(define port (open-output-file nome))
(print 'archivio-v1.0 port)
(print archivio port)
(print last port)
(print user-data port)
(print order port)
(close-output-port port))
(define (make-dispatcher size o-test e-test user-data)
(define archivio (make-vector (1+ size) nil))
(define order #t)
(define last-el 0)
(define nome "arch.dat")
(define (dispatch message value)
(cond ((eq? message 'save)
(if (string? value)
(set! nome value))
(save-ar archivio last-el nome user-data order))
((eq? message 'load)
(let ((r (load-ar value)))
(if (pair? r)
(begin (set! archivio (cadddr r))
(set! last-el (caddr r))
(set! user-data (cadr r))
(set! order (car r))
(set! nome value)
(set! size
(-1+ (vector-length archivio)))
'done)
r)))
((eq? message 'add-ord)
(if (< last-el size)
(begin (if order
(begin (insert-el! archivio
value
last-el
o-test)
(set! last-el
(1+ last-el))
'done)
'not-in-order))
'full))
((eq? message 'del-el)
(if (and (< value last-el) (>= value 0))
(begin (delete-el! archivio value last-el)
(set! last-el (-1+ last-el))
'done)
'out-of-range))
((eq? message 'for-each)
(ar-for-each archivio value last-el)
'done)
((eq? message 'last-el)
last-el)
((eq? message 'us-data)
user-data)
((eq? message 'order)
order)
((eq? message 'ch-us-da)
(set! user-data value)
'done)
((eq? message 'sort)
(if (or (not order) value)
(begin (qsort archivio
0
(-1+ last-el)
o-test)
(set! order #t)))
'done)
((eq? message 'add)
(if (< last-el size)
(begin (vector-set! archivio
last-el
value)
(set! last-el (1+ last-el))
(set! order #f)
'done)
'full))
((eq? message 'ch-ord)
(set! o-test (car value))
(set! e-test (cdr value))
'done)
((eq? message 'read)
(if (and (< value last-el) (>= value 0))
(vector-ref archivio value)
'out-of-range))
((eq? message 'search)
(if order
(binsearch archivio
value
last-el
o-test
e-test)
'not-in-order))
(else 'unknown-message)))
dispatch)))
(define (make-archivio size ord-test eq-test user-data)
(eval (list 'make-dispatcher
size
ord-test
eq-test
(list 'quote user-data))
archivio-environment))