Skip to content

Commit 2da1f83

Browse files
committed
Improve style
- unify function signatures - remove trailing whitespace - rename variables - always use space after comma in fuction definitions/calls - improve comments
1 parent c2ff440 commit 2da1f83

File tree

1 file changed

+57
-56
lines changed

1 file changed

+57
-56
lines changed

src/SortingAlgorithms.jl

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -685,22 +685,22 @@ end
685685
###
686686

687687
# merge v[lo:hiA] and v[hiA+1:hi] ([A;B]) using buffer t[1:1 + hi-lo]
688-
function twoended_merge!(v::AbstractVector{T}, t::AbstractVector{T}, lo::Integer, hi::Integer, hiA::Integer, o::Ordering) where T
688+
function twoended_merge!(v::AbstractVector{T}, t::AbstractVector{T}, lo::Integer, hiA::Integer, hi::Integer, o::Ordering) where T
689689
@assert lo <= hiA <= hi
690690
loA = lo
691691
loB = hiA + 1
692692
hiB = hi
693-
694-
# output array indices
693+
694+
# output array indices
695695
oL = 1
696-
oR = 1 + hi-lo
697-
696+
oR = 1 + hi - lo
697+
698698
# input array indices
699699
iAL = loA
700700
iBL = loB
701701
iAR = hiA
702702
iBR = hiB
703-
703+
704704
@inbounds begin
705705
# two ended merge
706706
while iAL < iAR && iBL < iBR
@@ -712,7 +712,7 @@ function twoended_merge!(v::AbstractVector{T}, t::AbstractVector{T}, lo::Integer
712712
iAL += 1
713713
end
714714
oL +=1
715-
715+
716716
if lt(o,v[iAR], v[iBR])
717717
t[oR] = v[iBR]
718718
iBR -= 1
@@ -721,7 +721,7 @@ function twoended_merge!(v::AbstractVector{T}, t::AbstractVector{T}, lo::Integer
721721
iAR -= 1
722722
end
723723
oR -=1
724-
end
724+
end
725725
# cleanup
726726
# regular merge
727727
while iAL <= iAR && iBL <= iBR
@@ -756,7 +756,7 @@ end
756756

757757
# merge v[lo:lo+lenA-1] and v[lo+lenA:hi] using buffer t[1:lenA]
758758
# based on Base.Sort MergeSort
759-
function merge!(v::AbstractVector{T},t::AbstractVector{T}, lenA::Integer, lo::Integer, hi::Integer, o::Ordering) where T
759+
function merge!(v::AbstractVector{T}, t::AbstractVector{T}, lo::Integer, hi::Integer, lenA::Integer, o::Ordering) where T
760760
@inbounds begin
761761
i = 1
762762
j = lo
@@ -787,11 +787,13 @@ function merge!(v::AbstractVector{T},t::AbstractVector{T}, lenA::Integer, lo::In
787787
end
788788

789789
# macro used for block management in pagedMerge!
790+
# use next block in A (left subarray) if it is free,
791+
# otherwise use next block in B
790792
macro getNextBlock!()
791793
quote
792794
if iA > nextBlockA * blocksize + lo
793795
currentBlock = nextBlockA
794-
nextBlockA += 1
796+
nextBlockA += 1
795797
else
796798
currentBlock = nextBlockB
797799
nextBlockB += 1
@@ -801,18 +803,18 @@ macro getNextBlock!()
801803
end |> esc
802804
end
803805

804-
# merge v[lo:endA] and v[endA+1:hi] using buffer buf in O(sqrt(n)) space
805-
function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer, endA::Integer, hi::Integer, blockLocation::AbstractVector{<:Integer}, o::Ordering) where T
806-
@assert lo < endA < hi
806+
# merge v[lo:hiA] and v[hiA+1:hi] using buffer buf in O(sqrt(n)) space
807+
function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer, hiA::Integer, hi::Integer, blockLocation::AbstractVector{<:Integer}, o::Ordering) where T
808+
@assert lo < hiA < hi
807809
iA = lo
808-
iB = endA + 1
809-
endB = hi
810-
lenA = endA + 1 - lo
811-
lenB = endB - endA
810+
iB = hiA + 1
811+
hiB = hi
812+
lenA = hiA + 1 - lo
813+
lenB = hiB - hiA
812814

813815
# regular merge if buffer is big enough
814816
if lenA <= length(buf)
815-
merge!(v,buf,lenA,lo,hi,o)
817+
merge!(v, buf, lo, hi, lenA, o)
816818
return
817819
elseif lenB <= length(buf)
818820
# TODO ?
@@ -827,9 +829,9 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
827829
@assert length(buf) >= 3blocksize
828830
@assert length(blockLocation) >= nBlocks+1
829831

830-
@inline getBlockOffset(block) = (block-1)*blocksize + lo - 1
832+
@inline getBlockOffset(block) = (block-1)*blocksize + lo - 1
831833

832-
@inbounds begin
834+
@inbounds begin
833835
##################
834836
# merge
835837
##################
@@ -847,15 +849,15 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
847849
end
848850

849851
nextBlockA = 1
850-
nextBlockB = (endA+blocksize-lo) ÷ blocksize + 1
852+
nextBlockB = (hiA+blocksize-lo) ÷ blocksize + 1
851853
blockLocation .= 0
852854
blockLocation[1:3] = -1:-1:-3
853855

854856
oIdx = 1
855857
currentBlock = 0
856858
currentBlockIdx = 4
857859
# more efficient loop while more than blocksize elements of A and B are remaining
858-
while iA < endA-blocksize && iB < endB-blocksize
860+
while iA < hiA-blocksize && iB < hiB-blocksize
859861
@getNextBlock!
860862
offset = (currentBlock-1)*blocksize
861863
oIdx = lo + offset
@@ -871,11 +873,11 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
871873
end
872874
end
873875
# merge until either A or B is empty
874-
while iA <= endA && iB <= endB
876+
while iA <= hiA && iB <= hiB
875877
@getNextBlock!
876878
oIdx = 1
877879
offset = getBlockOffset(currentBlock)
878-
while oIdx <= blocksize && iA <= endA && iB <= endB
880+
while oIdx <= blocksize && iA <= hiA && iB <= hiB
879881
if lt(o, v[iB], v[iA])
880882
v[offset+oIdx] = v[iB]
881883
iB += 1
@@ -889,26 +891,26 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
889891
# copy remaining elements
890892
# either A or B is empty
891893
# copy rest of A
892-
while iA <= endA
894+
while iA <= hiA
893895
if oIdx > blocksize
894896
@getNextBlock!
895897
oIdx = 1
896898
end
897899
offset = getBlockOffset(currentBlock)
898-
while oIdx <= blocksize && iA <= endA
900+
while oIdx <= blocksize && iA <= hiA
899901
v[offset + oIdx] = v[iA]
900902
iA += 1
901903
oIdx += 1
902904
end
903905
end
904906
# copy rest of B
905-
while iB <= endB
907+
while iB <= hiB
906908
if oIdx > blocksize
907909
@getNextBlock!
908910
oIdx = 1
909911
end
910912
offset = getBlockOffset(currentBlock)
911-
while oIdx <= blocksize && iB <= endB
913+
while oIdx <= blocksize && iB <= hiB
912914
v[offset + oIdx] = v[iB]
913915
iB += 1
914916
oIdx += 1
@@ -941,7 +943,7 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
941943
end
942944
if partialBlockPresent
943945
freeBlocks[i] = currentBlock
944-
end
946+
end
945947
freeBlocksIdx = 3
946948
doneBlockIdx = 1
947949
currentBlock = freeBlocks[end]
@@ -951,7 +953,7 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
951953
while true
952954
blc = blockLocation[currentBlock] # index of block with data belonging to currentBlock
953955
if blc > 0
954-
# data for currentBlock is in v
956+
# data for currentBlock is in v
955957
offset = getBlockOffset(currentBlock)
956958
offset2 = getBlockOffset(blc)
957959
for j = 1:blocksize
@@ -978,7 +980,7 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
978980
doneBlockIdx += 1
979981
doneBlockIdx == nBlocks && return
980982
end
981-
# copy misplaced block into buf and continue
983+
# copy misplaced block into buf and continue
982984
currentBlock = blockLocation[doneBlockIdx]
983985
offset = getBlockOffset(currentBlock)
984986
for j = 1:blocksize
@@ -988,52 +990,51 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
988990
end
989991
end
990992
end
991-
end
993+
end
992994
end
993995

994996
# midpoint was added to Base.sort in version 1.4 and later moved to Base
995997
# -> redefine for compatibility with earlier versions
996-
_midpoint(lo::Integer,hi::Integer) = lo + ((hi - lo) >>> 0x01)
998+
_midpoint(lo::Integer, hi::Integer) = lo + ((hi - lo) >>> 0x01)
997999

9981000
function pagedmergesort!(v::AbstractVector{T}, lo::Integer, hi::Integer, buf::AbstractVector{T}, blockLocation, o=Base.Order.Forward) where T
9991001
len = hi + 1 -lo
10001002
if len <= Base.SMALL_THRESHOLD
10011003
return Base.Sort.sort!(v, lo, hi, Base.Sort.InsertionSortAlg(), o)
10021004
end
1003-
m = _midpoint(lo,hi)
1004-
pagedmergesort!(v,lo,m,buf,blockLocation,o)
1005-
pagedmergesort!(v,m+1,hi,buf,blockLocation,o)
1005+
m = _midpoint(lo, hi)
1006+
pagedmergesort!(v, lo, m, buf, blockLocation, o)
1007+
pagedmergesort!(v, m+1, hi, buf, blockLocation, o)
10061008
if len <= length(buf)
1007-
twoended_merge!(v, buf, lo, hi, m,o)
1009+
twoended_merge!(v, buf, lo, m, hi, o)
10081010
else
1009-
pagedMerge!(v, buf, lo, m, hi, blockLocation, o)
1011+
pagedMerge!(v, buf, lo, m, hi, blockLocation, o)
10101012
end
10111013
return v
10121014
end
10131015

1014-
const PAGEDMERGESORT_THREADING_THRESHOLD = 2^13
1015-
10161016
function sort!(v::AbstractVector, lo::Integer, hi::Integer, a::PagedMergeSortAlg, o::Ordering)
10171017
lo >= hi && return v
10181018
n = hi + 1 - lo
10191019
blocksize = isqrt(n)
1020-
buf = Vector{eltype(v)}(undef,3blocksize)
1020+
buf = Vector{eltype(v)}(undef, 3blocksize)
10211021
nBlocks = n ÷ blocksize
1022-
blockLocation = Vector{Int}(undef,nBlocks+1)
1023-
pagedmergesort!(v,lo,hi,buf,blockLocation,o)
1022+
blockLocation = Vector{Int}(undef, nBlocks+1)
1023+
pagedmergesort!(v, lo, hi, buf, blockLocation, o)
10241024
return v
10251025
end
10261026

10271027
Base.@static if VERSION >= v"1.3"
1028-
function threaded_pagedmergesort!(v::AbstractVector, lo::Integer, hi::Integer, bufs, blockLocations, c::Channel, threadingThreshold::Integer, o=Base.Order.Forward)
1028+
const PAGEDMERGESORT_THREADING_THRESHOLD = 2^13
1029+
function threaded_pagedmergesort!(v::AbstractVector, lo::Integer, hi::Integer, bufs, blockLocations, c::Channel, threadingThreshold::Integer, o=Base.Order.Forward)
10291030
len = hi + 1 -lo
10301031
if len <= Base.SMALL_THRESHOLD
10311032
return Base.Sort.sort!(v, lo, hi, Base.Sort.InsertionSortAlg(), o)
10321033
end
1033-
m = _midpoint(lo,hi)
1034+
m = _midpoint(lo, hi)
10341035
if len > threadingThreshold
1035-
thr = Threads.@spawn threaded_pagedmergesort!(v,lo,m,bufs,blockLocations,c,threadingThreshold,o)
1036-
threaded_pagedmergesort!(v,m+1,hi,bufs,blockLocations,c,threadingThreshold,o)
1036+
thr = Threads.@spawn threaded_pagedmergesort!(v, lo, m, bufs, blockLocations, c, threadingThreshold, o)
1037+
threaded_pagedmergesort!(v, m+1, hi, bufs, blockLocations, c, threadingThreshold, o)
10371038
wait(thr)
10381039
id = take!(c)
10391040
buf = bufs[id]
@@ -1042,15 +1043,15 @@ function threaded_pagedmergesort!(v::AbstractVector, lo::Integer, hi::Integer, b
10421043
id = take!(c)
10431044
buf = bufs[id]
10441045
blockLocation = blockLocations[id]
1045-
pagedmergesort!(v,lo,m,buf,blockLocation,o)
1046-
pagedmergesort!(v,m+1,hi,buf,blockLocation,o)
1046+
pagedmergesort!(v, lo, m, buf, blockLocation, o)
1047+
pagedmergesort!(v, m+1, hi, buf, blockLocation, o)
10471048
end
10481049
if len <= length(buf)
10491050
twoended_merge!(v, buf, lo, hi, m, o)
10501051
else
1051-
pagedMerge!(v, buf, lo, m, hi, blockLocation, o)
1052+
pagedMerge!(v, buf, lo, m, hi, blockLocation, o)
10521053
end
1053-
put!(c,id)
1054+
put!(c, id)
10541055
return v
10551056
end
10561057
function sort!(v::AbstractVector, lo::Integer, hi::Integer, a::ThreadedPagedMergeSortAlg, o::Ordering)
@@ -1061,17 +1062,17 @@ function sort!(v::AbstractVector, lo::Integer, hi::Integer, a::ThreadedPagedMerg
10611062
threadingThreshold = max(n ÷ 4nThreads, PAGEDMERGESORT_THREADING_THRESHOLD)
10621063
blocksize = isqrt(n)
10631064
nBlocks = n ÷ blocksize
1064-
bufs = [Vector{eltype(v)}(undef,3blocksize) for _ in 1:nThreads] # allocate buffer for each thread
1065-
blockLocation = [Vector{Int}(undef,nBlocks+1) for _ in 1:nThreads]
1065+
bufs = [Vector{eltype(v)}(undef, 3blocksize) for _ in 1:nThreads] # allocate buffer for each thread
1066+
blockLocation = [Vector{Int}(undef, nBlocks+1) for _ in 1:nThreads]
10661067
c = Channel{Int}(nThreads) # channel holds indices of available buffers
10671068
for i=1:nThreads
1068-
put!(c,i)
1069+
put!(c, i)
10691070
end
1070-
threaded_pagedmergesort!(v,lo,hi,bufs,blockLocation,c,threadingThreshold,o)
1071+
threaded_pagedmergesort!(v, lo, hi, bufs, blockLocation, c, threadingThreshold, o)
10711072
return v
10721073
end
10731074
else
1074-
# no multithreading in earlier versions -> use single threaded version instead
1075+
# use single threaded function when VERSION < v"1.3"
10751076
sort!(v::AbstractVector, lo::Integer, hi::Integer, a::ThreadedPagedMergeSortAlg, o::Ordering) = sort!(v, lo, hi, PagedMergeSort, o)
10761077
end
10771078
end # module

0 commit comments

Comments
 (0)