intersect_integer_int64 Function

private pure function intersect_integer_int64(A, B) result(D)

Return A ∩ B Assume: 1. A and B are sorted. The result will be sorted.

Arguments

Type IntentOptional Attributes Name
integer(kind=int64), intent(in) :: A(:)
integer(kind=int64), intent(in) :: B(:)

Return Value integer(kind=int64), allocatable, (:)


Contents


Source Code

    pure function intersect_integer_int64 (A, B) result(D)
        integer (int64), intent(in) :: A(:), B(:)
        integer (int64), allocatable :: D(:)
        character(*), parameter :: this_routine = 'union_complement'

        integer (int64), allocatable :: tmp(:)
        integer :: i, j, l

#ifdef DEBUG_
    block
        use util_mod, only: stop_all
        if (.not. (is_sorted(A))) then
            call stop_all (this_routine, "Assert fail: is_sorted(A)")
        end if
    end block
#endif
#ifdef DEBUG_
    block
        use util_mod, only: stop_all
        if (.not. (is_sorted(B))) then
            call stop_all (this_routine, "Assert fail: is_sorted(B)")
        end if
    end block
#endif

        allocate(tmp(min(size(A), size(B))))

        i = 1; j = 1; l = 1
        do while (l <= size(tmp))
            if (i > size(A) .or. j > size(B)) then
                exit
            else if (A(i) == B(j)) then
                tmp(l) = A(i)
                i = i + 1
                j = j + 1
                l = l + 1
            else if (A(i) < B(j)) then
                i = i + 1
            else if (A(i) > B(j)) then
                j = j + 1
            end if
        end do

        associate(final_size => l - 1)
            D = tmp(:final_size)
        end associate
#ifdef DEBUG_
    block
        use util_mod, only: stop_all
        if (.not. (is_sorted(D))) then
            call stop_all (this_routine, "Assert fail: is_sorted(D)")
        end if
    end block
#endif
    end function intersect_integer_int64