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
@@ -685,22 +685,22 @@ end
685
685
###
686
686
687
687
# merge v[lo:hiA] and v[hiA+1:hi] ([A;B]) using buffer t[1:1 + hi-lo]
688
-
functiontwoended_merge!(v::AbstractVector{T}, t::AbstractVector{T}, lo::Integer, hi::Integer, hiA::Integer, o::Ordering) where T
688
+
functiontwoended_merge!(v::AbstractVector{T}, t::AbstractVector{T}, lo::Integer, hiA::Integer, hi::Integer, o::Ordering) where T
689
689
@assert lo <= hiA <= hi
690
690
loA = lo
691
691
loB = hiA +1
692
692
hiB = hi
693
-
694
-
# output array indices
693
+
694
+
# output array indices
695
695
oL =1
696
-
oR =1+ hi-lo
697
-
696
+
oR =1+ hi-lo
697
+
698
698
# input array indices
699
699
iAL = loA
700
700
iBL = loB
701
701
iAR = hiA
702
702
iBR = hiB
703
-
703
+
704
704
@inboundsbegin
705
705
# two ended merge
706
706
while iAL < iAR && iBL < iBR
@@ -712,7 +712,7 @@ function twoended_merge!(v::AbstractVector{T}, t::AbstractVector{T}, lo::Integer
712
712
iAL +=1
713
713
end
714
714
oL +=1
715
-
715
+
716
716
iflt(o,v[iAR], v[iBR])
717
717
t[oR] = v[iBR]
718
718
iBR -=1
@@ -721,7 +721,7 @@ function twoended_merge!(v::AbstractVector{T}, t::AbstractVector{T}, lo::Integer
721
721
iAR -=1
722
722
end
723
723
oR -=1
724
-
end
724
+
end
725
725
# cleanup
726
726
# regular merge
727
727
while iAL <= iAR && iBL <= iBR
@@ -756,7 +756,7 @@ end
756
756
757
757
# merge v[lo:lo+lenA-1] and v[lo+lenA:hi] using buffer t[1:lenA]
758
758
# based on Base.Sort MergeSort
759
-
functionmerge!(v::AbstractVector{T},t::AbstractVector{T}, lenA::Integer, lo::Integer, hi::Integer, o::Ordering) where T
759
+
functionmerge!(v::AbstractVector{T},t::AbstractVector{T}, lo::Integer, hi::Integer, lenA::Integer, o::Ordering) where T
760
760
@inboundsbegin
761
761
i =1
762
762
j = lo
@@ -787,11 +787,13 @@ function merge!(v::AbstractVector{T},t::AbstractVector{T}, lenA::Integer, lo::In
787
787
end
788
788
789
789
# 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
790
792
macrogetNextBlock!()
791
793
quote
792
794
if iA > nextBlockA * blocksize + lo
793
795
currentBlock = nextBlockA
794
-
nextBlockA +=1
796
+
nextBlockA +=1
795
797
else
796
798
currentBlock = nextBlockB
797
799
nextBlockB +=1
@@ -801,18 +803,18 @@ macro getNextBlock!()
801
803
end|> esc
802
804
end
803
805
804
-
# merge v[lo:endA] and v[endA+1:hi] using buffer buf in O(sqrt(n)) space
805
-
functionpagedMerge!(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
+
functionpagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer, hiA::Integer, hi::Integer, blockLocation::AbstractVector{<:Integer}, o::Ordering) where T
808
+
@assert lo <hiA< hi
807
809
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
812
814
813
815
# regular merge if buffer is big enough
814
816
if lenA <=length(buf)
815
-
merge!(v,buf,lenA,lo,hi,o)
817
+
merge!(v,buf,lo,hi, lenA, o)
816
818
return
817
819
elseif lenB <=length(buf)
818
820
#TODO ?
@@ -827,9 +829,9 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
827
829
@assertlength(buf) >=3blocksize
828
830
@assertlength(blockLocation) >= nBlocks+1
829
831
830
-
@inlinegetBlockOffset(block) = (block-1)*blocksize + lo -1
832
+
@inlinegetBlockOffset(block) = (block-1)*blocksize + lo -1
831
833
832
-
@inboundsbegin
834
+
@inboundsbegin
833
835
##################
834
836
# merge
835
837
##################
@@ -847,15 +849,15 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
847
849
end
848
850
849
851
nextBlockA =1
850
-
nextBlockB = (endA+blocksize-lo) ÷ blocksize +1
852
+
nextBlockB = (hiA+blocksize-lo) ÷ blocksize +1
851
853
blockLocation .=0
852
854
blockLocation[1:3] =-1:-1:-3
853
855
854
856
oIdx =1
855
857
currentBlock =0
856
858
currentBlockIdx =4
857
859
# 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
859
861
@getNextBlock!
860
862
offset = (currentBlock-1)*blocksize
861
863
oIdx = lo + offset
@@ -871,11 +873,11 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
871
873
end
872
874
end
873
875
# merge until either A or B is empty
874
-
while iA <=endA&& iB <=endB
876
+
while iA <=hiA&& iB <=hiB
875
877
@getNextBlock!
876
878
oIdx =1
877
879
offset =getBlockOffset(currentBlock)
878
-
while oIdx <= blocksize && iA <=endA&& iB <=endB
880
+
while oIdx <= blocksize && iA <=hiA&& iB <=hiB
879
881
iflt(o, v[iB], v[iA])
880
882
v[offset+oIdx] = v[iB]
881
883
iB +=1
@@ -889,26 +891,26 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
889
891
# copy remaining elements
890
892
# either A or B is empty
891
893
# copy rest of A
892
-
while iA <=endA
894
+
while iA <=hiA
893
895
if oIdx > blocksize
894
896
@getNextBlock!
895
897
oIdx =1
896
898
end
897
899
offset =getBlockOffset(currentBlock)
898
-
while oIdx <= blocksize && iA <=endA
900
+
while oIdx <= blocksize && iA <=hiA
899
901
v[offset + oIdx] = v[iA]
900
902
iA +=1
901
903
oIdx +=1
902
904
end
903
905
end
904
906
# copy rest of B
905
-
while iB <=endB
907
+
while iB <=hiB
906
908
if oIdx > blocksize
907
909
@getNextBlock!
908
910
oIdx =1
909
911
end
910
912
offset =getBlockOffset(currentBlock)
911
-
while oIdx <= blocksize && iB <=endB
913
+
while oIdx <= blocksize && iB <=hiB
912
914
v[offset + oIdx] = v[iB]
913
915
iB +=1
914
916
oIdx +=1
@@ -941,7 +943,7 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
941
943
end
942
944
if partialBlockPresent
943
945
freeBlocks[i] = currentBlock
944
-
end
946
+
end
945
947
freeBlocksIdx =3
946
948
doneBlockIdx =1
947
949
currentBlock = freeBlocks[end]
@@ -951,7 +953,7 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
951
953
whiletrue
952
954
blc = blockLocation[currentBlock] # index of block with data belonging to currentBlock
953
955
if blc >0
954
-
# data for currentBlock is in v
956
+
# data for currentBlock is in v
955
957
offset =getBlockOffset(currentBlock)
956
958
offset2 =getBlockOffset(blc)
957
959
for j =1:blocksize
@@ -978,7 +980,7 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
978
980
doneBlockIdx +=1
979
981
doneBlockIdx == nBlocks &&return
980
982
end
981
-
# copy misplaced block into buf and continue
983
+
# copy misplaced block into buf and continue
982
984
currentBlock = blockLocation[doneBlockIdx]
983
985
offset =getBlockOffset(currentBlock)
984
986
for j =1:blocksize
@@ -988,52 +990,51 @@ function pagedMerge!(v::AbstractVector{T}, buf::AbstractVector{T}, lo::Integer,
988
990
end
989
991
end
990
992
end
991
-
end
993
+
end
992
994
end
993
995
994
996
# midpoint was added to Base.sort in version 1.4 and later moved to Base
995
997
# -> 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)
997
999
998
1000
functionpagedmergesort!(v::AbstractVector{T}, lo::Integer, hi::Integer, buf::AbstractVector{T}, blockLocation, o=Base.Order.Forward) where T
0 commit comments