Return A ∪ B Assume: 1. A and B are sorted. The result will be sorted.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=int64), | intent(in) | :: | A(:) | |||
integer(kind=int64), | intent(in) | :: | B(:) |
pure function union_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(size(A) + size(B)))
i = 1; j = 1; l = 1
do while (l <= size(tmp))
if (i > size(A) .and. j > size(B)) then
exit
else if (i > size(A)) then
tmp(l) = B(j)
j = j + 1
l = l + 1
else if (j > size(B)) then
tmp(l) = A(i)
i = i + 1
l = l + 1
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
tmp(l) = A(i)
i = i + 1
l = l + 1
else if (A(i) > B(j)) then
tmp(l) = B(j)
j = j + 1
l = l + 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 union_integer_int64