#lang racket/base
(provide disk-sort)
(require racket/vector
(only-in (lib "43.ss" "srfi") vector-for-each)
(only-in (lib "32.ss" "srfi") vector-sort!))
(define (read-block-from-file filename position block-size read-record)
(let ([port (open-input-file filename)])
(begin0
(read-block port position block-size read-record)
(close-input-port port))))
(define (read-block port position block-size read-record)
(let ([v (make-vector block-size)])
(file-position port position)
(let loop ([n 0])
(cond
[(= n block-size) v]
[else (let ([r (read-record port)])
(if (and (eof-object? r) (< n block-size))
(set! v (vector-copy v 0 n))
(begin
(vector-set! v n r)
(loop (+ n 1)))))]))
(when (for/or ([x (in-vector v)])
(eof-object? x))
(error))
v))
(define (merge-vectors v1 v2 less?)
(let* ([s1 (vector-length v1)]
[s2 (vector-length v2)]
[v (make-vector (+ s1 s2))])
(let loop ([n1 0] [n2 0] [n 0])
(cond
[(and (= n1 s1) (= n2 s2)) v]
[(= n1 s1) (begin
(vector-set! v n (vector-ref v2 n2))
(loop n1 (+ n2 1) (+ n 1)))]
[(= n2 s2) (begin
(vector-set! v n (vector-ref v1 n1))
(loop (+ n1 1) n2 (+ n 1)))]
[(less? (vector-ref v1 n1)
(vector-ref v2 n2)) (begin
(vector-set! v n (vector-ref v1 n1))
(loop (+ n1 1) n2 (+ n 1)))]
[else (begin
(vector-set! v n (vector-ref v2 n2))
(loop n1 (+ n2 1) (+ n 1)))]))))
(define (write-block vector write-record port)
(vector-for-each (lambda (i r) (write-record r port))
vector))
(define (merge-blocks-in-file-once in-file out-file block-size read-record write-record less?)
(let ([in (open-input-file in-file)]
[out (open-output-file out-file #:exists 'update)])
(define (read-next-block)
(read-block in (file-position in) block-size read-record))
(begin0
(let loop ([b1 (read-next-block)]
[b2 (read-next-block)])
(if (zero? (+ (vector-length b1) (vector-length b2)))
(void)
(begin
(write-block (merge-vectors b1 b2 less?) write-record out)
(loop (read-next-block) (read-next-block)))))
(close-input-port in)
(close-output-port out))))
(define (filename->number-of-records filename read-record)
(let ([in (open-input-file filename)])
(begin0
(let loop ([n 0])
(if (eof-object? (read-record in))
n
(loop (+ n 1))))
(close-input-port in))))
(define (number-of-merges-needed size block-size)
(if (>= block-size size)
0
(+ 1 (number-of-merges-needed size (* 2 block-size)))))
(define (merge-blocks-in-file file size block-size read-record write-record less?)
(if (zero? (number-of-merges-needed size block-size))
(void)
(begin
(merge-blocks-in-file-once file file block-size read-record write-record less?)
(merge-blocks-in-file file size (* 2 block-size) read-record write-record less?))))
(define (disk-sort in-file out-file block-size read-record write-record less?)
(let ([in (open-input-file in-file)]
[out (open-output-file out-file #:exists 'update)])
(define (read-next-block)
(read-block in (file-position in) block-size read-record))
(let loop ([b (read-next-block)])
(if (zero? (vector-length b))
(begin
(close-input-port in)
(close-output-port out))
(begin
(vector-sort! less? b)
(write-block b write-record out)
(loop (read-next-block)))))
(let ([size (filename->number-of-records out-file read-record)])
(merge-blocks-in-file out-file size block-size read-record write-record less?))))