unique_entries Subroutine

public subroutine unique_entries(input, output)

search for unique occurences in the array

Arguments

TypeIntentOptionalAttributesName
integer, intent(in), dimension(:,:):: input

original array

integer, intent(out), dimension(:,:), allocatable:: output

resulting array, containing unique entries from the original one


Contents

Source Code


Source Code

subroutine unique_entries(input, output)

    !> original array
    integer, dimension(:,:), intent(in) :: input
    !> resulting array, containing unique entries from the original one
    integer, dimension(:,:), intent(out), allocatable :: output

    integer, dimension(:,:), allocatable :: temp

    integer :: i, j, n_code
    integer :: out_size, entry_num
    logical :: found

    out_size = 0
    found = .false.
    if (allocated(output)) deallocate(output)
    allocate(output(0,0))

    do n_code = 1, size(input, 2)
        do i = 1, size(input, 1)
            if (input(i, n_code) == -1) then
                cycle
            end if

            if (out_size /= 0) then
                do j = 1, out_size
                    if (input(i, n_code) == output(j, n_code)) then
                        found = .true.
                        exit
                    end if
                end do
            end if


            if (.not. found) then
                out_size = out_size + 1
                if (out_size > size(output, 1)) then
                    entry_num = out_size
                else
                    entry_num = size(output, 1)
                end if
                allocate(temp(entry_num, size(input, 2)))
                temp(:,:) = -1
                if (size(output, 1) > 0) then
                    temp(1:size(output, 1), 1:size(input, 2)) = output
                end if
                call move_alloc(temp, output)
                output(out_size, n_code) = input(i, n_code)
            end if
            found = .false.
        end do
        out_size = 0
    end do

end subroutine unique_entries