expand_vector Subroutine

private pure subroutine expand_vector(vec, n, val, finished)

Add elements to the integer vector in chunks.

Arguments

Type IntentOptional Attributes Name
integer, intent(inout), dimension(:), allocatable :: vec
integer, intent(inout) :: n

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

integer, 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_vector~~CalledByGraph proc~expand_vector aoc_utilities::expand_vector proc~split1 aoc_utilities::split1 proc~split1->proc~expand_vector interface~split aoc_utilities::split interface~split->proc~split1 proc~split2 aoc_utilities::split2 interface~split->proc~split2 proc~split2->proc~split1 proc~go~2 problem_18::go proc~go~2->interface~split proc~go~8 problem_12::go proc~go~8->interface~split proc~initialize problem_22::initialize proc~initialize->interface~split proc~parse_nums64 aoc_utilities::parse_nums64 proc~parse_nums64->interface~split proc~parse_rule problem_19::parse_rule proc~parse_rule->interface~split proc~parse_rule~2 problem_19b::parse_rule proc~parse_rule~2->interface~split program~problem_12b problem_12b program~problem_12b->interface~split program~problem_15 problem_15 program~problem_15->interface~split program~problem_19 problem_19 program~problem_19->interface~split program~problem_19->proc~parse_rule program~problem_19b problem_19b program~problem_19b->interface~split program~problem_19b->proc~parse_rule~2 program~problem_2 problem_2 program~problem_2->interface~split program~problem_25 problem_25 program~problem_25->interface~split program~problem_4 problem_4 program~problem_4->interface~split program~problem_7 problem_7 program~problem_7->interface~split interface~parse aoc_utilities::parse interface~parse->proc~parse_nums64 program~problem_12 problem_12 program~problem_12->proc~go~8 program~problem_18 problem_18 program~problem_18->proc~go~2 program~problem_22 problem_22 program~problem_22->proc~initialize program~problem_9 problem_9 program~problem_9->interface~parse

Source Code

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

    integer,dimension(:),allocatable,intent(inout) :: vec
    integer,intent(inout)       :: n           !! counter for last element added to `vec`.
                                               !! must be initialized to `size(vec)`
                                               !! (or 0 if not allocated) before first call
    integer,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`)

    integer,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_vector