Number the distinct lines, so that equal lines have equal numbers.
| Type | Intent | Optional | 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 |
|
| integer, | intent(out), | allocatable | :: | ib(:) |
the number of each line of |
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