home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frozen Fish 2: PC
/
frozenfish_august_1995.bin
/
bbs
/
d09xx
/
d0963.lha
/
SIOD
/
scm
/
binsearch.scm
< prev
next >
Wrap
Text File
|
1993-10-01
|
5KB
|
99 lines
(define archivio-environment
(make-environment
(define ord-test nil)
(define eq-test nil)
(define (qsort! v l)
(define (interchange i j)
(let ((t (vector-ref v i)))
(vector-set! v i (vector-ref v j))
(vector-set! v j t)))
(define (qsort-i m n)
(if (< m n)
(do ((i m)
(j (1+ n))
(k (vector-ref v m)))
((>= i j) (interchange m j)
(qsort-i m (-1+ j))
(qsort-i (1+ j) n))
(set! i (1+ i))
(do ()
((or (ord-test (vector-ref v i) k)
(>= i n)))
(set! i (1+ i)))
(set! j (-1+ j))
(do ()
((or (ord-test k (vector-ref v j))
(<= j m)))
(set! j (-1+ j)))
(if (< i j)
(interchange i j)))))
(qsort-i 0 l))
(define (binsearch x y n)
(let ((m 0))
(do ((mid (quotient (+ m n) 2)
(quotient (+ m n) 2)))
((or (>= mid n)
(<= mid m)
(eq-test y (vector-ref x mid)))
(if (eq-test y (vector-ref x mid))
mid
nil))
(if (ord-test y (vector-ref x mid))
(set! m mid)
(set! n mid)))))
(define (insert-el! x y n)
(if (or (= n 0) (ord-test y (vector-ref x (-1+ n))))
(vector-set! x n y)
(begin (vector-set! x n (vector-ref x (-1+ n)))
(insert-el! x y (-1+ n)))))
(define (make-dispatcher o-test e-test size)
(define archivio (make-vector size nil))
(define last-el 0)
(define (dispatch message value)
(cond ((eq? message 'save)
(let ((p (open-output-file value)))
(print archivio p)
(close-output-port p))
#t)
((eq? message 'load)
(let ((p (open-input-file value)))
(set! archivio (read p))
(close-input-port p))
#t)
((eq? message 'add-el)
(set! ord-test o-test)
(insert-el! archivio value last-el)
(set! last-el (1+ last-el))
#t)
((eq? message 'add)
(set! ord-test o-test)
(vector-set! archivio last-el value)
(set! last-el (1+ last-el))
#t)
((eq? message 'last-el)
last-el)
((eq? message 'read)
(vector-ref archivio value))
((eq? message 'sort)
(set! ord-test o-test)
(qsort! archivio (-1+ last-el))
#t)
((eq? message 'search)
(set! ord-test o-test)
(set! eq-test e-test)
(binsearch archivio value last-el))
(else (error "unknown message: " message))))
dispatch)))
(define standard-catalogo-size 100)
(define standard-schedario-size 100)
(define (make-catalogo)
(eval (list 'make-dispatcher
>
=
standard-catalogo-size)
archivio-environment))