expand_queue Subroutine

pure subroutine expand_queue(vec, n, val, finished)

Add elements to the integer vector in chunks.

Arguments

Type IntentOptional Attributes Name
type(item), intent(inout), dimension(:), allocatable :: vec
integer(kind=ip), intent(inout) :: n

counter for last element added to vec. must be initialized to size(vec) (or 0 if not allocated) before first call

type(item), intent(in), optional :: val

the value to add to vec

logical, intent(in), optional :: finished

set to true to return vec as its correct size (n)


Called by

proc~~expand_queue~~CalledByGraph proc~expand_queue problem_17::expand_queue proc~add_to_queue~2 problem_17::add_to_queue proc~add_to_queue~2->proc~expand_queue proc~check~2 problem_17::check proc~check~2->proc~add_to_queue~2 program~problem_17~2 problem_17 program~problem_17~2->proc~add_to_queue~2 program~problem_17~2->proc~check~2

Source Code

    pure subroutine expand_queue(vec,n,val,finished)

    integer(ip),parameter :: chunk_size = 1000

    type(item),dimension(:),allocatable,intent(inout) :: vec
    integer(ip),intent(inout)       :: n           !! counter for last element added to `vec`.
                                               !! must be initialized to `size(vec)`
                                               !! (or 0 if not allocated) before first call
    type(item),intent(in),optional :: val         !! the value to add to `vec`
    logical,intent(in),optional :: finished    !! set to true to return `vec`
                                               !! as its correct size (`n`)

    type(item),dimension(:),allocatable :: tmp  !! temporary array

    if (present(val)) then
        if (allocated(vec)) then
            if (n==size(vec)) then
                ! have to add another chunk:
                allocate(tmp(size(vec)+chunk_size))
                tmp(1:size(vec)) = vec
                call move_alloc(tmp,vec)
            end if
            n = n + 1
        else
            ! the first element:
            allocate(vec(chunk_size))
            n = 1
        end if
        vec(n) = val
    end if

    if (present(finished)) then
        if (finished) then
            ! set vec to actual size (n):
            if (allocated(tmp)) deallocate(tmp)
            allocate(tmp(n))
            tmp = vec(1:n)
            call move_alloc(tmp,vec)
        end if
    end if

    end subroutine expand_queue