Skip to content

Commit 0d9e23f

Browse files
committed
Reduce number of arguments of next_page!()
1 parent bb4e706 commit 0d9e23f

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/SortingAlgorithms.jl

+17-17
Original file line numberDiff line numberDiff line change
@@ -777,23 +777,23 @@ function merge!(v::AbstractVector{T}, lo::Integer, m::Integer, hi::Integer, o::O
777777
end
778778

779779
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
783784
end
784785

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)
787788

788-
function next_page!(pageLocations, pages, currentPageIndex, pagesize, lo, a)
789+
function next_page!(pageLocations, pages, pagesize, lo, a)
789790
if a > pages.nextA * pagesize + lo
790791
pages = next_page_A(pages)
791792
else
792793
pages = next_page_B(pages)
793794
end
794-
pageLocations[currentPageIndex] = pages.current
795-
currentPageIndex += 1
796-
pages, currentPageIndex
795+
pageLocations[pages.currentNumber] = pages.current
796+
pages
797797
end
798798

799799
# 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
832832
# initialize variable for merging into pages
833833
pageLocations .= 0
834834
pageLocations[1:3] = -1:-1:-3
835-
currentPageIndex = 4
836835
currentPage = 0
836+
currentPageNumber = 3
837837
nextPageA = 1
838838
nextPageB = (m + pagesize-lo) ÷ pagesize + 1
839-
pages = Pages(currentPage, nextPageA, nextPageB)
839+
pages = Pages(currentPage, currentPageNumber, nextPageA, nextPageB)
840840
k = 1
841841
# more efficient loop while more than pagesize elements of A and B are remaining
842842
while_condition1(offset) = (_,_,k) -> k <= offset + pagesize
843843
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)
845845
offset = page_offset(pages.current)
846846
a,b,k = merge!(while_condition1(offset),v,v,v,o,a,b,offset+1)
847847
end
848848
# merge until either A or B is empty
849849
while_condition2(offset) = (a,b,k) -> k <= offset + pagesize && a <= m && b <= hi
850850
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)
852852
offset = page_offset(pages.current)
853853
a,b,k = merge!(while_condition2(offset),v,v,v,o,a,b,offset+1)
854854
end
@@ -858,7 +858,7 @@ function paged_merge!(v::AbstractVector{T}, lo::Integer, m::Integer, hi::Integer
858858
# copy rest of A
859859
while a <= m
860860
if k_page > pagesize
861-
pages, currentPageIndex = next_page!(pageLocations, pages, currentPageIndex, pagesize, lo, a)
861+
pages = next_page!(pageLocations, pages, pagesize, lo, a)
862862
k_page = 1
863863
end
864864
offset = page_offset(pages.current)
@@ -871,7 +871,7 @@ function paged_merge!(v::AbstractVector{T}, lo::Integer, m::Integer, hi::Integer
871871
# copy rest of B
872872
while b <= hi
873873
if k_page > pagesize
874-
pages, currentPageIndex = next_page!(pageLocations, pages, currentPageIndex, pagesize, lo, a)
874+
pages = next_page!(pageLocations, pages, pagesize, lo, a)
875875
k_page = 1
876876
end
877877
offset = page_offset(pages.current)
@@ -889,7 +889,7 @@ function paged_merge!(v::AbstractVector{T}, lo::Integer, m::Integer, hi::Integer
889889
for j = 1:k_page-1
890890
v[offset2 + j] = v[offset + j]
891891
end
892-
pageLocations[currentPageIndex-1] = 0
892+
pageLocations[pages.currentNumber] = 0
893893
end
894894
#########################################
895895
# calculate location of the 3 free pages
@@ -912,7 +912,7 @@ function paged_merge!(v::AbstractVector{T}, lo::Integer, m::Integer, hi::Integer
912912
freePagesIndex = 3
913913
donePageIndex = 1
914914
# 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
916916
currentPage = freePages[end]
917917
##################
918918
# rearrange pages

0 commit comments

Comments
 (0)