line_ids Subroutine

private subroutine line_ids(la, lb, ia, ib)

Number the distinct lines, so that equal lines have equal numbers.

Arguments

Type IntentOptional Attributes Name
type(string_t), intent(in) :: la(:)

the lines of the two texts

type(string_t), intent(in) :: lb(:)

the lines of the two texts

integer, intent(out), allocatable :: ia(:)

the number of each line of la and lb

integer, intent(out), allocatable :: ib(:)

the number of each line of la and lb


Calls

proc~~line_ids~~CallsGraph proc~line_ids line_ids proc~hash hash proc~line_ids->proc~hash

Called by

proc~~line_ids~~CalledByGraph proc~line_ids line_ids proc~unified_diff unified_diff proc~unified_diff->proc~line_ids proc~process_text process_text proc~process_text->proc~unified_diff proc~process_file process_file proc~process_file->proc~process_text proc~process_stdin process_stdin proc~process_stdin->proc~process_text proc~run run proc~run->proc~process_file proc~run->proc~process_stdin

Source Code

subroutine line_ids(la, lb, ia, ib)
    !! Number the distinct lines, so that equal lines have equal numbers.
    type(string_t), intent(in) :: la(:), lb(:)          !! the lines of the two texts
    integer, allocatable, intent(out) :: ia(:), ib(:)   !! the number of each line of `la` and `lb`
    integer, allocatable :: slot(:)     ! hash table: id of the line in each slot, or 0
    type(string_t), allocatable :: seen(:)
    integer :: k, nseen, mask

    mask = 1
    do while (mask < 2*(size(la) + size(lb)) + 1)
        mask = 2*mask
    end do
    allocate (slot(0:mask - 1), seen(size(la) + size(lb)), ia(size(la)), ib(size(lb)))
    mask = mask - 1
    slot = 0
    nseen = 0
    do k = 1, size(la)
        ia(k) = id_of(la(k)%s)
    end do
    do k = 1, size(lb)
        ib(k) = id_of(lb(k)%s)
    end do

contains

    integer function id_of(line)
        !! The number of `line`: that of an equal line seen before, or the next new number.
        character(len=*), intent(in) :: line   !! a line of `la` or `lb`
        integer :: h
        h = int(iand(hash(line), int(mask, int64)))
        do
            if (slot(h) == 0) then
                nseen = nseen + 1
                seen(nseen)%s = line
                slot(h) = nseen
                id_of = nseen
                return
            end if
            if (len(seen(slot(h))%s) == len(line)) then
                if (seen(slot(h))%s == line) then
                    id_of = slot(h)
                    return
                end if
            end if
            h = iand(h + 1, mask)
        end do
    end function id_of

end subroutine line_ids