# intersect_integer_int32 Function

## private pure function intersect_integer_int32(A, B) result(D)

### Arguments

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

## Source Code

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

integer (int32), 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_int32