module sorts
  implicit none
  integer, parameter :: prec=kind(1d0)
  
  private
  public :: smoothsort, prec

  ! Smoothsort is introduced by Edsger Dijkstra
  ! https://doi.org/10.1016/0167-6423(82)90016-8
  !
  ! This Fortran version owes much to https://www.keithschwarz.com/smoothsort/
  ! and the C version given by Martin Knoblauch Revuelta at
  ! http://code.google.com/archive/p/combsortcs2p-and-other-sorting-algorithms
  
  integer, parameter :: L(0:43) = (/ 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, &
    287, 465, 753, 1219, 1973, 3193, 5167, 8361, &
    13529, 21891, 35421, 57313, 92735, 150049, &
    242785, 392835, 635621, 1028457, 1664079, 2692537, &
    4356617, 7049155, 11405773, 18454929, 29860703, &
    48315633, 78176337, 126491971, 204668309, 331160281, &
    535828591, 866988873, 1402817465 /)
  ! mask must have one bit per element of L above
  ! 15 > 44*log(2)
  integer, parameter :: i64=selected_int_kind(15)
  
contains

  subroutine smoothsort(array)
    real(prec), intent(inout), contiguous::array(:)
    integer(i64) :: mask
    integer :: offset

    if (size(array).le.1) return

    call smooth_heapify(array,mask,offset)

    call smooth_extract(array,mask,offset)
  end subroutine smoothsort

  subroutine smooth_heapify(array,mask,offset)
    real(prec), intent(inout), contiguous::array(0:)
    integer(i64), intent(out) :: mask
    integer, intent(out) :: offset
    integer :: i,last
    logical :: wbf

    mask=1
    offset=1
    last=size(array)

    do i=1,last-1
      if (iand(mask,2_i64).eq.2) then
         mask=ior(ishft(mask,-2),1_i64)
         offset=offset+2
      else if (offset.eq.1) then
         mask=ior(ishft(mask,1),1_i64)
         offset=0
      else
         mask=ior(ishft(mask,offset-1),1_i64)
         offset=1
      endif
      wbf=(((iand(mask,2_i64).eq.2).and.(i+1.lt.last)).or. &
           ((offset.gt.0).and.(1+i+L(offset-1).lt.last)))
      if (wbf) then
         call sift_in(array,i,offset)
      else
         call interheap_sift(array,i,mask,offset)
      endif
   enddo
    
  end subroutine smooth_heapify

  subroutine smooth_extract(array,mask,offset)
    real(prec), intent(inout), contiguous::array(0:)
    integer :: i,j,last,ch(2)
    integer(i64),value :: mask
    integer, value :: offset

    last=size(array)

    do i=last-1,2,-1
       if (offset.lt.2) then
          do
             mask=ishft(mask,-1)
             offset=offset+1
             if (iand(mask,1_i64).eq.1) exit
          enddo
       else
          ch(2)=i-1
          ch(1)=ch(2)-L(offset-2)
          mask=iand(mask,not(1_i64))
          do j=1,2
             mask=ior(ishft(mask,1),1_i64)
             offset=offset-1
             call interheap_sift(array,ch(j),mask,offset)
          end do
       end if
    end do
    
    
  end subroutine smooth_extract
  
  
  subroutine sift_in(array,root,sz)
    real(prec), intent(inout), contiguous::array(0:)
    real(prec) :: tmp
    integer :: left,right,next
    integer,value :: root,sz

    if (sz.lt.2) return

    tmp=array(root)

    do
       right=root-1
       left=right-L(sz-2)

       if (array(right).lt.array(left)) then
          next=left
          sz=sz-1
       else
          next=right
          sz=sz-2
       end if

       if (array(next).le.tmp) exit

       array(root)=array(next)

       root=next
       if (sz.le.1) exit
    end do

    array(root)=tmp

  end subroutine sift_in
  
  subroutine interheap_sift(array,root,mask,offset)
    real(prec), intent(inout), contiguous::array(0:)
    real(prec) :: tmp,mx
    integer :: left,right,next
    integer(i64),value :: mask
    integer, value :: root,offset

    tmp=array(root)

    do while (mask.ne.1)
       mx=tmp

       if (offset.gt.1) then
          right=root-1
          left=right-L(offset-2)
          mx=max(mx,array(left),array(right))
       end if
       
       next=root-L(offset)

       if (array(next).le.mx) exit

       array(root)=array(next)
       root=next
          
       do
          mask=ishft(mask,-1)
          offset=offset+1
          if (iand(mask,1_i64).eq.1) exit
       end do
    end do
    

    array(root)=tmp
    call sift_in(array,root,offset)
  end subroutine interheap_sift
  
end module sorts

    
    
