reorder_all_columns Subroutine

private subroutine reorder_all_columns(df, indices)

Reorder all columns based on index array

Internal helper that reorders all data frame columns according to an index array

@param[in,out] df The data frame instance @param[in] indices Array specifying the new row order

Arguments

Type IntentOptional Attributes Name
type(data_frame), intent(inout) :: df
integer, intent(in), dimension(:) :: indices

Source Code

    subroutine reorder_all_columns(df, indices)
        type(data_frame), intent(inout) :: df
        integer, dimension(:), intent(in) :: indices

        integer :: i, j
        real(rk), allocatable :: real_temp(:)
        integer(ik), allocatable :: int_temp(:)
        logical, allocatable :: logical_temp(:)
        character(len=:), allocatable :: char_temp(:)
        complex(rk), allocatable :: complex_temp(:)
        type(column) :: col, new_col

        do i = 1, df % ncols()
            col = df % get_data_col(i)

            select case (col % get_type())
            case (REAL_NUM)
                allocate (real_temp(df % nrows()))
                do j = 1, df % nrows()
                    real_temp(j) = col % getr(indices(j))
                end do
                call col % destroy()
                call new_col % new(real_temp)
                call df % set_data_col(i, new_col)
                deallocate (real_temp)
            case (INTEGER_NUM)
                allocate (int_temp(df % nrows()))
                do j = 1, df % nrows()
                    int_temp(j) = col % geti(indices(j))
                end do
                call col % destroy()
                call new_col % new(int_temp)
                call df % set_data_col(i, new_col)
                deallocate (int_temp)
            case (LOGICAL_NUM)
                allocate (logical_temp(df % nrows()))
                do j = 1, df % nrows()
                    logical_temp(j) = col % getl(indices(j))
                end do
                call col % destroy()
                call new_col % new(logical_temp)
                call df % set_data_col(i, new_col)
                deallocate (logical_temp)
            case (CHARACTER_NUM)
                allocate (character(len=len(col % getch(1))) :: char_temp(df % nrows()))
                do j = 1, df % nrows()
                    char_temp(j) = col % getch(indices(j))
                end do
                call col % destroy()
                call new_col % new(char_temp)
                call df % set_data_col(i, new_col)
                deallocate (char_temp)
            case (COMPLEX_NUM)
                allocate (complex_temp(df % nrows()))
                do j = 1, df % nrows()
                    complex_temp(j) = col % getc(indices(j))
                end do
                call col % destroy()
                call new_col % new(complex_temp)
                call df % set_data_col(i, new_col)
                deallocate (complex_temp)
            end select
        end do
    end subroutine reorder_all_columns