add_val_int64_2D Subroutine

private pure subroutine add_val_int64_2D(this, val)

@brief Append a value to the buffer, expanding the capacity if necessary.

@param[in] val Value to be added

Type Bound

buffer_int64_2D_t

Arguments

Type IntentOptional Attributes Name
class(buffer_int64_2D_t), intent(inout) :: this
integer(kind=int64), intent(in), dimension(:) :: val

Contents

Source Code


Source Code

    pure subroutine add_val_int64_2D (this, val)
        class(buffer_int64_2D_t), intent(inout) :: this
        integer(int64), dimension(:), intent(in) :: val


        ! If the buffer still has room, add the entry
        if (this%pos < size(this%buf, 2, kind=int64)) then
            this%pos = this%pos + 1_int64
            this%buf(:, this%pos) = val
        else
            ! else, expand the buffer by another block
            block

                integer(int64), dimension(:, :), allocatable :: tmp
                integer(int64) :: new_buf_size
                ! Fortran 2003 automatic allocation/assignment
                tmp = this%buf

                ! We add a constant offset to allow growth if start_size == 0.
                ! The grow_factor then takes over for larger numbers and prevents the O(n^2) scaling.
                new_buf_size = ceiling(real(size(this%buf, 2), kind=dp) * this%grow_factor, kind=int64) + 10_int64
                deallocate(this%buf)
                allocate(this%buf(size(tmp, 1), new_buf_size))

                this%buf(:, : size(tmp, 2)) = tmp

                this%pos = this%pos + 1_int64
                this%buf(:, this%pos) = val
            end block
        end if
    end subroutine add_val_int64_2D