* Added list-sort and vector-sort.

This commit is contained in:
Abdulaziz Ghuloum 2007-09-04 12:56:40 -04:00
parent cb40f0ae3c
commit a291ed8ffb
4 changed files with 82 additions and 2 deletions

Binary file not shown.

77
src/ikarus.sort.ss Normal file
View File

@ -0,0 +1,77 @@
(library (ikarus.sort)
(export list-sort vector-sort)
(import
(ikarus system $fx)
(except (ikarus) list-sort vector-sort))
(define (merge1 <? a1 ls1 ls2)
(cond
[(null? ls2) ls1]
[else
(let ([a2 (car ls2)])
(cond
[(<? a2 a1)
(cons a2 (merge1 <? a1 ls1 (cdr ls2)))]
[else
(cons a1 (merge2 <? a2 (cdr ls1) ls2))]))]))
(define (merge2 <? a2 ls1 ls2)
(cond
[(null? ls1) ls2]
[else
(let ([a1 (car ls1)])
(cond
[(<? a2 a1)
(cons a2 (merge1 <? a1 ls1 (cdr ls2)))]
[else
(cons a1 (merge2 <? a2 (cdr ls1) ls2))]))]))
(define (merge <? ls1 ls2)
(cond
[(null? ls2) ls1]
[else
(let ([a1 (car ls1)] [a2 (car ls2)])
(cond
[(<? a2 a1)
(cons a2 (merge1 <? a1 ls1 (cdr ls2)))]
[else
(cons a1 (merge2 <? a2 (cdr ls1) ls2))]))]))
(define (sort-head <? ls n)
(cond
[($fx= n 0) (values '() ls)]
[($fx= n 1)
(values (cons (car ls) '()) (cdr ls))]
[else
(let-values ([(sorted-head tail)
(sort-head <? ls ($fxsra n 1))])
(let-values ([(sorted-tail tail)
(sort-head <? tail ($fx- n ($fxsra n 1)))])
(values (merge <? sorted-head sorted-tail) tail)))]))
(define (sort-tail <? ls n)
(cond
[($fx<= n 1) ls]
[else
(let-values ([(sorted-head tail)
(sort-head <? ls ($fxsra n 1))])
(merge <? sorted-head (sort-tail <? tail ($fx- n ($fxsra n 1)))))]))
(define (list-sort <? ls)
(unless (procedure? <?)
(error 'list-sort "~s is not a procedure" <?))
(sort-tail <? ls (length ls)))
(define (vector-sort <? v)
;;; FIXME: improve
(unless (procedure? <?)
(error 'vector-sort "~s is not a procedure" <?))
(unless (vector? v)
(error 'vector-sort "~s is not a vector" v))
(list->vector
(sort-tail <? (vector->list v) (vector-length v))))
)

View File

@ -71,6 +71,7 @@
"ikarus.posix.ss" "ikarus.posix.ss"
"ikarus.timer.ss" "ikarus.timer.ss"
"ikarus.bytevectors.ss" "ikarus.bytevectors.ss"
"ikarus.sort.ss"
"ikarus.promises.ss" "ikarus.promises.ss"
"ikarus.main.ss")) "ikarus.main.ss"))
@ -323,6 +324,8 @@
[memq i r] [memq i r]
[memv i r] [memv i r]
[member i r] [member i r]
[list-sort i]
[vector-sort i]
[bwp-object? i] [bwp-object? i]
[weak-cons i] [weak-cons i]
[weak-pair? i] [weak-pair? i]

View File

@ -684,8 +684,8 @@
[string-ci-hash D ht] [string-ci-hash D ht]
[symbol-hash D ht] [symbol-hash D ht]
;;; ;;;
[list-sort S sr] [list-sort C sr]
[vector-sort S sr] [vector-sort C sr]
[vector-sort! S sr] [vector-sort! S sr]
;;; ;;;
[file-exists? C fi] [file-exists? C fi]