@@ -777,23 +777,23 @@ function merge!(v::AbstractVector{T}, lo::Integer, m::Integer, hi::Integer, o::O
777
777
end
778
778
779
779
struct Pages
780
- current:: Int # current page being merged into
781
- nextA:: Int # next possible page in A
782
- nextB:: Int # next possible page in B
780
+ current:: Int # current page being merged into
781
+ currentNumber:: Int # number of current page (=index in pageLocations)
782
+ nextA:: Int # next possible page in A
783
+ nextB:: Int # next possible page in B
783
784
end
784
785
785
- next_page_A (pages:: Pages ) = Pages (pages. nextA, pages. nextA + 1 , pages. nextB)
786
- next_page_B (pages:: Pages ) = Pages (pages. nextB, pages. nextA, pages. nextB + 1 )
786
+ next_page_A (pages:: Pages ) = Pages (pages. nextA, pages. currentNumber + 1 , pages . nextA + 1 , pages. nextB)
787
+ next_page_B (pages:: Pages ) = Pages (pages. nextB, pages. currentNumber + 1 , pages . nextA, pages. nextB + 1 )
787
788
788
- function next_page! (pageLocations, pages, currentPageIndex, pagesize, lo, a)
789
+ function next_page! (pageLocations, pages, pagesize, lo, a)
789
790
if a > pages. nextA * pagesize + lo
790
791
pages = next_page_A (pages)
791
792
else
792
793
pages = next_page_B (pages)
793
794
end
794
- pageLocations[currentPageIndex] = pages. current
795
- currentPageIndex += 1
796
- pages, currentPageIndex
795
+ pageLocations[pages. currentNumber] = pages. current
796
+ pages
797
797
end
798
798
799
799
# merge v[lo:m] (A) and v[m+1:hi] (B) using buffer buf in O(sqrt(n)) space
@@ -832,23 +832,23 @@ function paged_merge!(v::AbstractVector{T}, lo::Integer, m::Integer, hi::Integer
832
832
# initialize variable for merging into pages
833
833
pageLocations .= 0
834
834
pageLocations[1 : 3 ] = - 1 : - 1 : - 3
835
- currentPageIndex = 4
836
835
currentPage = 0
836
+ currentPageNumber = 3
837
837
nextPageA = 1
838
838
nextPageB = (m + pagesize- lo) ÷ pagesize + 1
839
- pages = Pages (currentPage, nextPageA, nextPageB)
839
+ pages = Pages (currentPage, currentPageNumber, nextPageA, nextPageB)
840
840
k = 1
841
841
# more efficient loop while more than pagesize elements of A and B are remaining
842
842
while_condition1 (offset) = (_,_,k) -> k <= offset + pagesize
843
843
while a < m- pagesize && b < hi- pagesize
844
- pages, currentPageIndex = next_page! (pageLocations, pages, currentPageIndex , pagesize, lo, a)
844
+ pages = next_page! (pageLocations, pages, pagesize, lo, a)
845
845
offset = page_offset (pages. current)
846
846
a,b,k = merge! (while_condition1 (offset),v,v,v,o,a,b,offset+ 1 )
847
847
end
848
848
# merge until either A or B is empty
849
849
while_condition2 (offset) = (a,b,k) -> k <= offset + pagesize && a <= m && b <= hi
850
850
while a <= m && b <= hi
851
- pages, currentPageIndex = next_page! (pageLocations, pages, currentPageIndex , pagesize, lo, a)
851
+ pages = next_page! (pageLocations, pages, pagesize, lo, a)
852
852
offset = page_offset (pages. current)
853
853
a,b,k = merge! (while_condition2 (offset),v,v,v,o,a,b,offset+ 1 )
854
854
end
@@ -858,7 +858,7 @@ function paged_merge!(v::AbstractVector{T}, lo::Integer, m::Integer, hi::Integer
858
858
# copy rest of A
859
859
while a <= m
860
860
if k_page > pagesize
861
- pages, currentPageIndex = next_page! (pageLocations, pages, currentPageIndex , pagesize, lo, a)
861
+ pages = next_page! (pageLocations, pages, pagesize, lo, a)
862
862
k_page = 1
863
863
end
864
864
offset = page_offset (pages. current)
@@ -871,7 +871,7 @@ function paged_merge!(v::AbstractVector{T}, lo::Integer, m::Integer, hi::Integer
871
871
# copy rest of B
872
872
while b <= hi
873
873
if k_page > pagesize
874
- pages, currentPageIndex = next_page! (pageLocations, pages, currentPageIndex , pagesize, lo, a)
874
+ pages = next_page! (pageLocations, pages, pagesize, lo, a)
875
875
k_page = 1
876
876
end
877
877
offset = page_offset (pages. current)
@@ -889,7 +889,7 @@ function paged_merge!(v::AbstractVector{T}, lo::Integer, m::Integer, hi::Integer
889
889
for j = 1 : k_page- 1
890
890
v[offset2 + j] = v[offset + j]
891
891
end
892
- pageLocations[currentPageIndex - 1 ] = 0
892
+ pageLocations[pages . currentNumber ] = 0
893
893
end
894
894
# ########################################
895
895
# calculate location of the 3 free pages
@@ -912,7 +912,7 @@ function paged_merge!(v::AbstractVector{T}, lo::Integer, m::Integer, hi::Integer
912
912
freePagesIndex = 3
913
913
donePageIndex = 1
914
914
# use currentPage instead of pages.current because
915
- # pages.nextA and pages.nextB are no longer needed
915
+ # pages.nextA, pages.nextB and page.currentNumber are no longer needed
916
916
currentPage = freePages[end ]
917
917
# #################
918
918
# rearrange pages
0 commit comments