Skip to content

Commit ce9ad62

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 58b06fb commit ce9ad62

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
@@ -680,22 +680,22 @@ end
680680
###
681681

682682
# merge v[lo:hiA] and v[hiA+1:hi] ([A;B]) using buffer t[1:1 + hi-lo]
683-
function twoended_merge!(v::AbstractVector{T}, t::AbstractVector{T}, lo::Integer, hi::Integer, hiA::Integer, o::Ordering) where T
683+
function twoended_merge!(v::AbstractVector{T}, t::AbstractVector{T}, lo::Integer, hiA::Integer, hi::Integer, o::Ordering) where T
684684
@assert lo <= hiA <= hi
685685
loA = lo
686686
loB = hiA + 1
687687
hiB = hi
688-
689-
# output array indices
688+
689+
# output array indices
690690
oL = 1
691-
oR = 1 + hi-lo
692-
691+
oR = 1 + hi - lo
692+
693693
# input array indices
694694
iAL = loA
695695
iBL = loB
696696
iAR = hiA
697697
iBR = hiB
698-
698+
699699
@inbounds begin
700700
# two ended merge
701701
while iAL < iAR && iBL < iBR
@@ -707,7 +707,7 @@ function twoended_merge!(v::AbstractVector{T}, t::AbstractVector{T}, lo::Integer
707707
iAL += 1
708708
end
709709
oL +=1
710-
710+
711711
if lt(o,v[iAR], v[iBR])
712712
t[oR] = v[iBR]
713713
iBR -= 1
@@ -716,7 +716,7 @@ function twoended_merge!(v::AbstractVector{T}, t::AbstractVector{T}, lo::Integer
716716
iAR -= 1
717717
end
718718
oR -=1
719-
end
719+
end
720720
# cleanup
721721
# regular merge
722722
while iAL <= iAR && iBL <= iBR
@@ -751,7 +751,7 @@ end
751751

752752
# merge v[lo:lo+lenA-1] and v[lo+lenA:hi] using buffer t[1:lenA]
753753
# based on Base.Sort MergeSort
754-
function merge!(v::AbstractVector{T},t::AbstractVector{T}, lenA::Integer, lo::Integer, hi::Integer, o::Ordering) where T
754+
function merge!(v::AbstractVector{T}, t::AbstractVector{T}, lo::Integer, hi::Integer, lenA::Integer, o::Ordering) where T
755755
@inbounds begin
756756
i = 1
757757
j = lo
@@ -782,11 +782,13 @@ function merge!(v::AbstractVector{T},t::AbstractVector{T}, lenA::Integer, lo::In
782782
end
783783

784784
# macro used for block management in pagedMerge!
785+
# use next block in A (left subarray) if it is free,
786+
# otherwise use next block in B
785787
macro getNextBlock!()
786788
quote
787789
if iA > nextBlockA * blocksize + lo
788790
currentBlock = nextBlockA
789-
nextBlockA += 1
791+
nextBlockA += 1
790792
else
791793
currentBlock = nextBlockB
792794
nextBlockB += 1
@@ -796,18 +798,18 @@ macro getNextBlock!()
796798
end |> esc
797799
end
798800

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

808810
# regular merge if buffer is big enough
809811
if lenA <= length(buf)
810-
merge!(v,buf,lenA,lo,hi,o)
812+
merge!(v, buf, lo, hi, lenA, o)
811813
return
812814
elseif lenB <= length(buf)
813815
# TODO ?
@@ -822,9 +824,9 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
822824
@assert length(buf) >= 3blocksize
823825
@assert length(blockLocation) >= nBlocks+1
824826

825-
@inline getBlockOffset(block) = (block-1)*blocksize + lo - 1
827+
@inline getBlockOffset(block) = (block-1)*blocksize + lo - 1
826828

827-
@inbounds begin
829+
@inbounds begin
828830
##################
829831
# merge
830832
##################
@@ -842,15 +844,15 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
842844
end
843845

844846
nextBlockA = 1
845-
nextBlockB = (endA+blocksize-lo) ÷ blocksize + 1
847+
nextBlockB = (hiA+blocksize-lo) ÷ blocksize + 1
846848
blockLocation .= 0
847849
blockLocation[1:3] = -1:-1:-3
848850

849851
oIdx = 1
850852
currentBlock = 0
851853
currentBlockIdx = 4
852854
# more efficient loop while more than blocksize elements of A and B are remaining
853-
while iA < endA-blocksize && iB < endB-blocksize
855+
while iA < hiA-blocksize && iB < hiB-blocksize
854856
@getNextBlock!
855857
offset = (currentBlock-1)*blocksize
856858
oIdx = lo + offset
@@ -866,11 +868,11 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
866868
end
867869
end
868870
# merge until either A or B is empty
869-
while iA <= endA && iB <= endB
871+
while iA <= hiA && iB <= hiB
870872
@getNextBlock!
871873
oIdx = 1
872874
offset = getBlockOffset(currentBlock)
873-
while oIdx <= blocksize && iA <= endA && iB <= endB
875+
while oIdx <= blocksize && iA <= hiA && iB <= hiB
874876
if lt(o, v[iB], v[iA])
875877
v[offset+oIdx] = v[iB]
876878
iB += 1
@@ -884,26 +886,26 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
884886
# copy remaining elements
885887
# either A or B is empty
886888
# copy rest of A
887-
while iA <= endA
889+
while iA <= hiA
888890
if oIdx > blocksize
889891
@getNextBlock!
890892
oIdx = 1
891893
end
892894
offset = getBlockOffset(currentBlock)
893-
while oIdx <= blocksize && iA <= endA
895+
while oIdx <= blocksize && iA <= hiA
894896
v[offset + oIdx] = v[iA]
895897
iA += 1
896898
oIdx += 1
897899
end
898900
end
899901
# copy rest of B
900-
while iB <= endB
902+
while iB <= hiB
901903
if oIdx > blocksize
902904
@getNextBlock!
903905
oIdx = 1
904906
end
905907
offset = getBlockOffset(currentBlock)
906-
while oIdx <= blocksize && iB <= endB
908+
while oIdx <= blocksize && iB <= hiB
907909
v[offset + oIdx] = v[iB]
908910
iB += 1
909911
oIdx += 1
@@ -936,7 +938,7 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
936938
end
937939
if partialBlockPresent
938940
freeBlocks[i] = currentBlock
939-
end
941+
end
940942
freeBlocksIdx = 3
941943
doneBlockIdx = 1
942944
currentBlock = freeBlocks[end]
@@ -946,7 +948,7 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
946948
while true
947949
blc = blockLocation[currentBlock] # index of block with data belonging to currentBlock
948950
if blc > 0
949-
# data for currentBlock is in v
951+
# data for currentBlock is in v
950952
offset = getBlockOffset(currentBlock)
951953
offset2 = getBlockOffset(blc)
952954
for j = 1:blocksize
@@ -973,7 +975,7 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
973975
doneBlockIdx += 1
974976
doneBlockIdx == nBlocks && return
975977
end
976-
# copy misplaced block into buf and continue
978+
# copy misplaced block into buf and continue
977979
currentBlock = blockLocation[doneBlockIdx]
978980
offset = getBlockOffset(currentBlock)
979981
for j = 1:blocksize
@@ -983,52 +985,51 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
983985
end
984986
end
985987
end
986-
end
988+
end
987989
end
988990

989991
# midpoint was added to Base.sort in version 1.4 and later moved to Base
990992
# -> redefine for compatibility with earlier versions
991-
_midpoint(lo::Integer,hi::Integer) = lo + ((hi - lo) >>> 0x01)
993+
_midpoint(lo::Integer, hi::Integer) = lo + ((hi - lo) >>> 0x01)
992994

993995
function pagedmergesort!(v::AbstractVector{T}, lo::Integer, hi::Integer, buf::AbstractVector{T}, blockLocation, o=Base.Order.Forward) where T
994996
len = hi + 1 -lo
995997
if len <= Base.SMALL_THRESHOLD
996998
return Base.Sort.sort!(v, lo, hi, Base.Sort.InsertionSortAlg(), o)
997999
end
998-
m = _midpoint(lo,hi)
999-
pagedmergesort!(v,lo,m,buf,blockLocation,o)
1000-
pagedmergesort!(v,m+1,hi,buf,blockLocation,o)
1000+
m = _midpoint(lo, hi)
1001+
pagedmergesort!(v, lo, m, buf, blockLocation, o)
1002+
pagedmergesort!(v, m+1, hi, buf, blockLocation, o)
10011003
if len <= length(buf)
1002-
twoended_merge!(v, buf, lo, hi, m,o)
1004+
twoended_merge!(v, buf, lo, m, hi, o)
10031005
else
1004-
pagedMerge!(v, buf, lo, m, hi, blockLocation, o)
1006+
pagedMerge!(v, buf, lo, m, hi, blockLocation, o)
10051007
end
10061008
return v
10071009
end
10081010

1009-
const PAGEDMERGESORT_THREADING_THRESHOLD = 2^13
1010-
10111011
function sort!(v::AbstractVector, lo::Integer, hi::Integer, a::PagedMergeSortAlg, o::Ordering)
10121012
lo >= hi && return v
10131013
n = hi + 1 - lo
10141014
blocksize = isqrt(n)
1015-
buf = Vector{eltype(v)}(undef,3blocksize)
1015+
buf = Vector{eltype(v)}(undef, 3blocksize)
10161016
nBlocks = n ÷ blocksize
1017-
blockLocation = Vector{Int}(undef,nBlocks+1)
1018-
pagedmergesort!(v,lo,hi,buf,blockLocation,o)
1017+
blockLocation = Vector{Int}(undef, nBlocks+1)
1018+
pagedmergesort!(v, lo, hi, buf, blockLocation, o)
10191019
return v
10201020
end
10211021

10221022
Base.@static if VERSION >= v"1.3"
1023-
function threaded_pagedmergesort!(v::AbstractVector, lo::Integer, hi::Integer, bufs, blockLocations, c::Channel, threadingThreshold::Integer, o=Base.Order.Forward)
1023+
const PAGEDMERGESORT_THREADING_THRESHOLD = 2^13
1024+
function threaded_pagedmergesort!(v::AbstractVector, lo::Integer, hi::Integer, bufs, blockLocations, c::Channel, threadingThreshold::Integer, o=Base.Order.Forward)
10241025
len = hi + 1 -lo
10251026
if len <= Base.SMALL_THRESHOLD
10261027
return Base.Sort.sort!(v, lo, hi, Base.Sort.InsertionSortAlg(), o)
10271028
end
1028-
m = _midpoint(lo,hi)
1029+
m = _midpoint(lo, hi)
10291030
if len > threadingThreshold
1030-
thr = Threads.@spawn threaded_pagedmergesort!(v,lo,m,bufs,blockLocations,c,threadingThreshold,o)
1031-
threaded_pagedmergesort!(v,m+1,hi,bufs,blockLocations,c,threadingThreshold,o)
1031+
thr = Threads.@spawn threaded_pagedmergesort!(v, lo, m, bufs, blockLocations, c, threadingThreshold, o)
1032+
threaded_pagedmergesort!(v, m+1, hi, bufs, blockLocations, c, threadingThreshold, o)
10321033
wait(thr)
10331034
id = take!(c)
10341035
buf = bufs[id]
@@ -1037,15 +1038,15 @@ function threaded_pagedmergesort!(v::AbstractVector, lo::Integer, hi::Integer, b
10371038
id = take!(c)
10381039
buf = bufs[id]
10391040
blockLocation = blockLocations[id]
1040-
pagedmergesort!(v,lo,m,buf,blockLocation,o)
1041-
pagedmergesort!(v,m+1,hi,buf,blockLocation,o)
1041+
pagedmergesort!(v, lo, m, buf, blockLocation, o)
1042+
pagedmergesort!(v, m+1, hi, buf, blockLocation, o)
10421043
end
10431044
if len <= length(buf)
10441045
twoended_merge!(v, buf, lo, hi, m, o)
10451046
else
1046-
pagedMerge!(v, buf, lo, m, hi, blockLocation, o)
1047+
pagedMerge!(v, buf, lo, m, hi, blockLocation, o)
10471048
end
1048-
put!(c,id)
1049+
put!(c, id)
10491050
return v
10501051
end
10511052
function sort!(v::AbstractVector, lo::Integer, hi::Integer, a::ThreadedPagedMergeSortAlg, o::Ordering)
@@ -1056,17 +1057,17 @@ function sort!(v::AbstractVector, lo::Integer, hi::Integer, a::ThreadedPagedMerg
10561057
threadingThreshold = max(n ÷ 4nThreads, PAGEDMERGESORT_THREADING_THRESHOLD)
10571058
blocksize = isqrt(n)
10581059
nBlocks = n ÷ blocksize
1059-
bufs = [Vector{eltype(v)}(undef,3blocksize) for _ in 1:nThreads] # allocate buffer for each thread
1060-
blockLocation = [Vector{Int}(undef,nBlocks+1) for _ in 1:nThreads]
1060+
bufs = [Vector{eltype(v)}(undef, 3blocksize) for _ in 1:nThreads] # allocate buffer for each thread
1061+
blockLocation = [Vector{Int}(undef, nBlocks+1) for _ in 1:nThreads]
10611062
c = Channel{Int}(nThreads) # channel holds indices of available buffers
10621063
for i=1:nThreads
1063-
put!(c,i)
1064+
put!(c, i)
10641065
end
1065-
threaded_pagedmergesort!(v,lo,hi,bufs,blockLocation,c,threadingThreshold,o)
1066+
threaded_pagedmergesort!(v, lo, hi, bufs, blockLocation, c, threadingThreshold, o)
10661067
return v
10671068
end
10681069
else
1069-
# no multithreading in earlier versions -> use single threaded version instead
1070+
# use single threaded function when VERSION < v"1.3"
10701071
sort!(v::AbstractVector, lo::Integer, hi::Integer, a::ThreadedPagedMergeSortAlg, o::Ordering) = sort!(v, lo, hi, PagedMergeSort, o)
10711072
end
10721073
end # module

0 commit comments

Comments
 (0)