Skip to content

Commit

Permalink
Apply @LSchwerdt's suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Lilith Hafner authored and Lilith Hafner committed May 28, 2023
1 parent 5bcdbb8 commit 5dab2a1
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/SortingAlgorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -780,11 +780,14 @@ struct Pages
nextB::Int # next possible page in B
end

next_page_A(pages::Pages) = Pages(pages.nextA, pages.currentNumber + 1, pages.nextA + 1, pages.nextB)
next_page_B(pages::Pages) = Pages(pages.nextB, pages.currentNumber + 1, pages.nextA, pages.nextB + 1)

Base.@propagate_inbounds function next_page!(pageLocations, pages, pagesize, lo, a)
pages = if a > pages.nextA * pagesize + lo
Pages(pages.nextA, pages.currentNumber + 1, pages.nextA + 1, pages.nextB)
if a > pages.nextA * pagesize + lo
pages = next_page_A(pages)
else
Pages(pages.nextB, pages.currentNumber + 1, pages.nextA, pages.nextB + 1)
pages = next_page_B(pages)
end
pageLocations[pages.currentNumber] = pages.current
pages
Expand Down Expand Up @@ -857,10 +860,10 @@ function paged_merge!(v::AbstractVector{T}, lo::Integer, m::Integer, hi::Integer
else
use_a = a <= m
# copy the incomplete page
amt = offset + pagesize - k + 1
copyto!(v, k, v, use_a ? a : b, amt)
use_a && (a += amt)
use_a || (b += amt)
partial_page_size = offset + pagesize - k + 1
copyto!(v, k, v, use_a ? a : b, partial_page_size)
use_a && (a += partial_page_size)
use_a || (b += partial_page_size)
# copy the remaining full pages
while use_a ? a <= m - pagesize + 1 : b <= hi - pagesize + 1
pages = next_page!(pageLocations, pages, pagesize, lo, a)
Expand All @@ -873,7 +876,7 @@ function paged_merge!(v::AbstractVector{T}, lo::Integer, m::Integer, hi::Integer
# If sourcing from B, it is already in place.
use_a && copyto!(v, hi - m + a, v, a, m - a + 1)
end
# return vcat(v, 40, buf)

##################
# rearrange pages
##################
Expand Down

0 comments on commit 5dab2a1

Please sign in to comment.