Improving Sort API, sort, sorted, and sort!

This commit is contained in:
Chris Hall 2018-06-26 15:38:23 +10:00
parent 88cf103b3a
commit a09db503d7
4 changed files with 35 additions and 11 deletions

View File

@ -163,10 +163,16 @@
; grow our tail, shrinking our head
(set! tail (- tail 1)))))))
(doc sort "Perform a heapsort in a new copy of given array.")
(defn sort [arr]
(doc sorted "Perform a heapsort in a new copy of given array.")
(defn sorted [arr]
(let-do [narr (Array.copy arr)]
(sort! &narr)
narr))
(doc sort "Perform an in-place heapsort of a given owned array.")
(defn sort [arr]
(do
(sort! &arr)
arr))
)

View File

@ -3,7 +3,11 @@
(defn sort! [arr]
(HeapSort.sort! arr))
(doc sort "Perform a heapsort in a new copy of given array.")
(doc sorted "Perform a heapsort in a new copy of given array.")
(defn sorted [arr]
(HeapSort.sorted arr))
(doc sort "Perform an in-place heapsort of a given owned array.")
(defn sort [arr]
(HeapSort.sort arr))
)

View File

@ -77,23 +77,29 @@
&exp
&arr
"HeapSort.sort! works"))
(let-do [arr [1 3 4 2 6 1]
exp [1 1 2 3 4 6]
res (HeapSort.sort &arr)]
(let-do [res (HeapSort.sort [1 3 4 2 6 1])
exp [1 1 2 3 4 6]]
(assert-equal test
&exp
&res
"HeapSort.sort works"))
; Check that HeapSort.sort does not modify input array
(let-do [arr [1 3 4 2 6 1]
exp [1 1 2 3 4 6]
_ (HeapSort.sort &arr)]
res (HeapSort.sorted &arr)]
(assert-equal test
&exp
&res
"HeapSort.sorted works"))
; Check that HeapSort.sorted does not modify input array
(let-do [arr [1 3 4 2 6 1]
exp [1 1 2 3 4 6]
_ (HeapSort.sorted &arr)]
(assert-equal test
&arr
&[1 3 4 2 6 1]
"HeapSort.sort does not modify array"))
"HeapSort.sorted does not modify array"))
; walk through HeapSort.sort! step by step
(let-do [arr [1 3 4 2 6 1]

View File

@ -15,11 +15,19 @@
(let-do [arr [1 3 4 2 6 1]
exp [1 1 2 3 4 6]
res (Sort.sort &arr)]
res (Sort.sorted &arr)]
(assert-equal test
&exp
&res
"Sort.sorted works"))
(let-do [res (Sort.sort [1 3 4 2 6 1])
exp [1 1 2 3 4 6]]
(assert-equal test
&exp
&res
"Sort.sort works"))
(print-test-results test)))