You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- unify function signatures
- remove trailing whitespace
- rename variables
- always use space after comma in fuction definitions/calls
- improve comments
Copy file name to clipboardExpand all lines: src/SortingAlgorithms.jl
+57-56Lines changed: 57 additions & 56 deletions
Original file line number
Diff line number
Diff line change
@@ -680,22 +680,22 @@ end
680
680
###
681
681
682
682
# merge v[lo:hiA] and v[hiA+1:hi] ([A;B]) using buffer t[1:1 + hi-lo]
683
-
functiontwoended_merge!(v::AbstractVector{T}, t::AbstractVector{T}, lo::Integer, hi::Integer, hiA::Integer, o::Ordering) where T
683
+
functiontwoended_merge!(v::AbstractVector{T}, t::AbstractVector{T}, lo::Integer, hiA::Integer, hi::Integer, o::Ordering) where T
684
684
@assert lo <= hiA <= hi
685
685
loA = lo
686
686
loB = hiA +1
687
687
hiB = hi
688
-
689
-
# output array indices
688
+
689
+
# output array indices
690
690
oL =1
691
-
oR =1+ hi-lo
692
-
691
+
oR =1+ hi-lo
692
+
693
693
# input array indices
694
694
iAL = loA
695
695
iBL = loB
696
696
iAR = hiA
697
697
iBR = hiB
698
-
698
+
699
699
@inboundsbegin
700
700
# two ended merge
701
701
while iAL < iAR && iBL < iBR
@@ -707,7 +707,7 @@ function twoended_merge!(v::AbstractVector{T}, t::AbstractVector{T}, lo::Integer
707
707
iAL +=1
708
708
end
709
709
oL +=1
710
-
710
+
711
711
iflt(o,v[iAR], v[iBR])
712
712
t[oR] = v[iBR]
713
713
iBR -=1
@@ -716,7 +716,7 @@ function twoended_merge!(v::AbstractVector{T}, t::AbstractVector{T}, lo::Integer
716
716
iAR -=1
717
717
end
718
718
oR -=1
719
-
end
719
+
end
720
720
# cleanup
721
721
# regular merge
722
722
while iAL <= iAR && iBL <= iBR
@@ -751,7 +751,7 @@ end
751
751
752
752
# merge v[lo:lo+lenA-1] and v[lo+lenA:hi] using buffer t[1:lenA]
753
753
# based on Base.Sort MergeSort
754
-
functionmerge!(v::AbstractVector{T},t::AbstractVector{T}, lenA::Integer, lo::Integer, hi::Integer, o::Ordering) where T
754
+
functionmerge!(v::AbstractVector{T},t::AbstractVector{T}, lo::Integer, hi::Integer, lenA::Integer, o::Ordering) where T
755
755
@inboundsbegin
756
756
i =1
757
757
j = lo
@@ -782,11 +782,13 @@ function merge!(v::AbstractVector{T},t::AbstractVector{T}, lenA::Integer, lo::In
782
782
end
783
783
784
784
# 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
785
787
macrogetNextBlock!()
786
788
quote
787
789
if iA > nextBlockA * blocksize + lo
788
790
currentBlock = nextBlockA
789
-
nextBlockA +=1
791
+
nextBlockA +=1
790
792
else
791
793
currentBlock = nextBlockB
792
794
nextBlockB +=1
@@ -796,18 +798,18 @@ macro getNextBlock!()
796
798
end|> esc
797
799
end
798
800
799
-
# merge v[lo:endA] and v[endA+1:hi] using buffer buf in O(sqrt(n)) space
800
-
functionpagedMerge!(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
+
functionpagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer, hiA::Integer, hi::Integer, blockLocation::AbstractVector{<:Integer}, o::Ordering) where T
803
+
@assert lo <hiA< hi
802
804
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
807
809
808
810
# regular merge if buffer is big enough
809
811
if lenA <=length(buf)
810
-
merge!(v,buf,lenA,lo,hi,o)
812
+
merge!(v,buf,lo,hi, lenA, o)
811
813
return
812
814
elseif lenB <=length(buf)
813
815
#TODO ?
@@ -822,9 +824,9 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
822
824
@assertlength(buf) >=3blocksize
823
825
@assertlength(blockLocation) >= nBlocks+1
824
826
825
-
@inlinegetBlockOffset(block) = (block-1)*blocksize + lo -1
827
+
@inlinegetBlockOffset(block) = (block-1)*blocksize + lo -1
826
828
827
-
@inboundsbegin
829
+
@inboundsbegin
828
830
##################
829
831
# merge
830
832
##################
@@ -842,15 +844,15 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
842
844
end
843
845
844
846
nextBlockA =1
845
-
nextBlockB = (endA+blocksize-lo) ÷ blocksize +1
847
+
nextBlockB = (hiA+blocksize-lo) ÷ blocksize +1
846
848
blockLocation .=0
847
849
blockLocation[1:3] =-1:-1:-3
848
850
849
851
oIdx =1
850
852
currentBlock =0
851
853
currentBlockIdx =4
852
854
# 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
854
856
@getNextBlock!
855
857
offset = (currentBlock-1)*blocksize
856
858
oIdx = lo + offset
@@ -866,11 +868,11 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
866
868
end
867
869
end
868
870
# merge until either A or B is empty
869
-
while iA <=endA&& iB <=endB
871
+
while iA <=hiA&& iB <=hiB
870
872
@getNextBlock!
871
873
oIdx =1
872
874
offset =getBlockOffset(currentBlock)
873
-
while oIdx <= blocksize && iA <=endA&& iB <=endB
875
+
while oIdx <= blocksize && iA <=hiA&& iB <=hiB
874
876
iflt(o, v[iB], v[iA])
875
877
v[offset+oIdx] = v[iB]
876
878
iB +=1
@@ -884,26 +886,26 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
884
886
# copy remaining elements
885
887
# either A or B is empty
886
888
# copy rest of A
887
-
while iA <=endA
889
+
while iA <=hiA
888
890
if oIdx > blocksize
889
891
@getNextBlock!
890
892
oIdx =1
891
893
end
892
894
offset =getBlockOffset(currentBlock)
893
-
while oIdx <= blocksize && iA <=endA
895
+
while oIdx <= blocksize && iA <=hiA
894
896
v[offset + oIdx] = v[iA]
895
897
iA +=1
896
898
oIdx +=1
897
899
end
898
900
end
899
901
# copy rest of B
900
-
while iB <=endB
902
+
while iB <=hiB
901
903
if oIdx > blocksize
902
904
@getNextBlock!
903
905
oIdx =1
904
906
end
905
907
offset =getBlockOffset(currentBlock)
906
-
while oIdx <= blocksize && iB <=endB
908
+
while oIdx <= blocksize && iB <=hiB
907
909
v[offset + oIdx] = v[iB]
908
910
iB +=1
909
911
oIdx +=1
@@ -936,7 +938,7 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
936
938
end
937
939
if partialBlockPresent
938
940
freeBlocks[i] = currentBlock
939
-
end
941
+
end
940
942
freeBlocksIdx =3
941
943
doneBlockIdx =1
942
944
currentBlock = freeBlocks[end]
@@ -946,7 +948,7 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
946
948
whiletrue
947
949
blc = blockLocation[currentBlock] # index of block with data belonging to currentBlock
948
950
if blc >0
949
-
# data for currentBlock is in v
951
+
# data for currentBlock is in v
950
952
offset =getBlockOffset(currentBlock)
951
953
offset2 =getBlockOffset(blc)
952
954
for j =1:blocksize
@@ -973,7 +975,7 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
973
975
doneBlockIdx +=1
974
976
doneBlockIdx == nBlocks &&return
975
977
end
976
-
# copy misplaced block into buf and continue
978
+
# copy misplaced block into buf and continue
977
979
currentBlock = blockLocation[doneBlockIdx]
978
980
offset =getBlockOffset(currentBlock)
979
981
for j =1:blocksize
@@ -983,52 +985,51 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
983
985
end
984
986
end
985
987
end
986
-
end
988
+
end
987
989
end
988
990
989
991
# midpoint was added to Base.sort in version 1.4 and later moved to Base
990
992
# -> 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)
992
994
993
995
functionpagedmergesort!(v::AbstractVector{T}, lo::Integer, hi::Integer, buf::AbstractVector{T}, blockLocation, o=Base.Order.Forward) where T
0 commit comments