
Generate block indices for big data grid traversal
Source:R/func_generate_grid_Dan.R
generate_grid.RdPartitions a matrix or file into rectangular blocks and returns a data frame of block indices numbered from 1 to K (total number of blocks). Supports exact and flexible partitioning, and multiple traversal orders.
Usage
generate_grid(
fnrow,
fncol,
bnrow,
bncol,
traversal.mode = "rowsnake",
is.flexible = NULL,
is.flexible.row = FALSE,
is.flexible.col = FALSE
)Arguments
- fnrow
Integer. Number of rows in the file or matrix.
- fncol
Integer. Number of columns in the file or matrix.
- bnrow
Integer. Number of rows per block.
- bncol
Integer. Number of columns per block.
- traversal.mode
Character. Traversal order for the grid.
"rowsnake"uses alternating left-right traversal (snake/boustrophedon order);"lr"uses strict left-to-right traversal throughout. Defaults to"rowsnake".- is.flexible
Logical or
NULL. If non-NULL, overrides bothis.flexible.rowandis.flexible.col. WhenTRUE, allows block dimensions that do not evenly divide the file dimensions. Defaults toNULL.- is.flexible.row
Logical. If
FALSE, an error is thrown whenbnrowdoes not evenly dividefnrow. Defaults toFALSE.- is.flexible.col
Logical. If
FALSE, an error is thrown whenbncoldoes not evenly dividefncol. Defaults toFALSE.
Value
A data.frame with columns:
- block_no
Block index from 1 to K.
- U
Upper (first) row index of the block.
- D
Lower (last) row index of the block.
- L
Left (first) column index of the block.
- R
Right (last) column index of the block.
Examples
# Exact partition with snake traversal
generate_grid(100, 100, 10, 10)
#> block_no U D L R
#> 1 1 1 10 1 10
#> 2 2 1 10 11 20
#> 3 3 1 10 21 30
#> 4 4 1 10 31 40
#> 5 5 1 10 41 50
#> 6 6 1 10 51 60
#> 7 7 1 10 61 70
#> 8 8 1 10 71 80
#> 9 9 1 10 81 90
#> 10 10 1 10 91 100
#> 11 11 11 20 91 100
#> 12 12 11 20 81 90
#> 13 13 11 20 71 80
#> 14 14 11 20 61 70
#> 15 15 11 20 51 60
#> 16 16 11 20 41 50
#> 17 17 11 20 31 40
#> 18 18 11 20 21 30
#> 19 19 11 20 11 20
#> 20 20 11 20 1 10
#> 21 21 21 30 1 10
#> 22 22 21 30 11 20
#> 23 23 21 30 21 30
#> 24 24 21 30 31 40
#> 25 25 21 30 41 50
#> 26 26 21 30 51 60
#> 27 27 21 30 61 70
#> 28 28 21 30 71 80
#> 29 29 21 30 81 90
#> 30 30 21 30 91 100
#> 31 31 31 40 91 100
#> 32 32 31 40 81 90
#> 33 33 31 40 71 80
#> 34 34 31 40 61 70
#> 35 35 31 40 51 60
#> 36 36 31 40 41 50
#> 37 37 31 40 31 40
#> 38 38 31 40 21 30
#> 39 39 31 40 11 20
#> 40 40 31 40 1 10
#> 41 41 41 50 1 10
#> 42 42 41 50 11 20
#> 43 43 41 50 21 30
#> 44 44 41 50 31 40
#> 45 45 41 50 41 50
#> 46 46 41 50 51 60
#> 47 47 41 50 61 70
#> 48 48 41 50 71 80
#> 49 49 41 50 81 90
#> 50 50 41 50 91 100
#> 51 51 51 60 91 100
#> 52 52 51 60 81 90
#> 53 53 51 60 71 80
#> 54 54 51 60 61 70
#> 55 55 51 60 51 60
#> 56 56 51 60 41 50
#> 57 57 51 60 31 40
#> 58 58 51 60 21 30
#> 59 59 51 60 11 20
#> 60 60 51 60 1 10
#> 61 61 61 70 1 10
#> 62 62 61 70 11 20
#> 63 63 61 70 21 30
#> 64 64 61 70 31 40
#> 65 65 61 70 41 50
#> 66 66 61 70 51 60
#> 67 67 61 70 61 70
#> 68 68 61 70 71 80
#> 69 69 61 70 81 90
#> 70 70 61 70 91 100
#> 71 71 71 80 91 100
#> 72 72 71 80 81 90
#> 73 73 71 80 71 80
#> 74 74 71 80 61 70
#> 75 75 71 80 51 60
#> 76 76 71 80 41 50
#> 77 77 71 80 31 40
#> 78 78 71 80 21 30
#> 79 79 71 80 11 20
#> 80 80 71 80 1 10
#> 81 81 81 90 1 10
#> 82 82 81 90 11 20
#> 83 83 81 90 21 30
#> 84 84 81 90 31 40
#> 85 85 81 90 41 50
#> 86 86 81 90 51 60
#> 87 87 81 90 61 70
#> 88 88 81 90 71 80
#> 89 89 81 90 81 90
#> 90 90 81 90 91 100
#> 91 91 91 100 91 100
#> 92 92 91 100 81 90
#> 93 93 91 100 71 80
#> 94 94 91 100 61 70
#> 95 95 91 100 51 60
#> 96 96 91 100 41 50
#> 97 97 91 100 31 40
#> 98 98 91 100 21 30
#> 99 99 91 100 11 20
#> 100 100 91 100 1 10
# Flexible partition (block size does not divide file size evenly)
generate_grid(105, 103, 10, 10, is.flexible = TRUE)
#> block_no U D L R
#> 1 1 1 10 1 10
#> 2 2 1 10 11 20
#> 3 3 1 10 21 30
#> 4 4 1 10 31 40
#> 5 5 1 10 41 50
#> 6 6 1 10 51 60
#> 7 7 1 10 61 70
#> 8 8 1 10 71 80
#> 9 9 1 10 81 90
#> 10 10 1 10 91 100
#> 11 11 1 10 101 103
#> 12 12 11 20 101 103
#> 13 13 11 20 91 100
#> 14 14 11 20 81 90
#> 15 15 11 20 71 80
#> 16 16 11 20 61 70
#> 17 17 11 20 51 60
#> 18 18 11 20 41 50
#> 19 19 11 20 31 40
#> 20 20 11 20 21 30
#> 21 21 11 20 11 20
#> 22 22 11 20 1 10
#> 23 23 21 30 1 10
#> 24 24 21 30 11 20
#> 25 25 21 30 21 30
#> 26 26 21 30 31 40
#> 27 27 21 30 41 50
#> 28 28 21 30 51 60
#> 29 29 21 30 61 70
#> 30 30 21 30 71 80
#> 31 31 21 30 81 90
#> 32 32 21 30 91 100
#> 33 33 21 30 101 103
#> 34 34 31 40 101 103
#> 35 35 31 40 91 100
#> 36 36 31 40 81 90
#> 37 37 31 40 71 80
#> 38 38 31 40 61 70
#> 39 39 31 40 51 60
#> 40 40 31 40 41 50
#> 41 41 31 40 31 40
#> 42 42 31 40 21 30
#> 43 43 31 40 11 20
#> 44 44 31 40 1 10
#> 45 45 41 50 1 10
#> 46 46 41 50 11 20
#> 47 47 41 50 21 30
#> 48 48 41 50 31 40
#> 49 49 41 50 41 50
#> 50 50 41 50 51 60
#> 51 51 41 50 61 70
#> 52 52 41 50 71 80
#> 53 53 41 50 81 90
#> 54 54 41 50 91 100
#> 55 55 41 50 101 103
#> 56 56 51 60 101 103
#> 57 57 51 60 91 100
#> 58 58 51 60 81 90
#> 59 59 51 60 71 80
#> 60 60 51 60 61 70
#> 61 61 51 60 51 60
#> 62 62 51 60 41 50
#> 63 63 51 60 31 40
#> 64 64 51 60 21 30
#> 65 65 51 60 11 20
#> 66 66 51 60 1 10
#> 67 67 61 70 1 10
#> 68 68 61 70 11 20
#> 69 69 61 70 21 30
#> 70 70 61 70 31 40
#> 71 71 61 70 41 50
#> 72 72 61 70 51 60
#> 73 73 61 70 61 70
#> 74 74 61 70 71 80
#> 75 75 61 70 81 90
#> 76 76 61 70 91 100
#> 77 77 61 70 101 103
#> 78 78 71 80 101 103
#> 79 79 71 80 91 100
#> 80 80 71 80 81 90
#> 81 81 71 80 71 80
#> 82 82 71 80 61 70
#> 83 83 71 80 51 60
#> 84 84 71 80 41 50
#> 85 85 71 80 31 40
#> 86 86 71 80 21 30
#> 87 87 71 80 11 20
#> 88 88 71 80 1 10
#> 89 89 81 90 1 10
#> 90 90 81 90 11 20
#> 91 91 81 90 21 30
#> 92 92 81 90 31 40
#> 93 93 81 90 41 50
#> 94 94 81 90 51 60
#> 95 95 81 90 61 70
#> 96 96 81 90 71 80
#> 97 97 81 90 81 90
#> 98 98 81 90 91 100
#> 99 99 81 90 101 103
#> 100 100 91 100 101 103
#> 101 101 91 100 91 100
#> 102 102 91 100 81 90
#> 103 103 91 100 71 80
#> 104 104 91 100 61 70
#> 105 105 91 100 51 60
#> 106 106 91 100 41 50
#> 107 107 91 100 31 40
#> 108 108 91 100 21 30
#> 109 109 91 100 11 20
#> 110 110 91 100 1 10
#> 111 111 101 105 1 10
#> 112 112 101 105 11 20
#> 113 113 101 105 21 30
#> 114 114 101 105 31 40
#> 115 115 101 105 41 50
#> 116 116 101 105 51 60
#> 117 117 101 105 61 70
#> 118 118 101 105 71 80
#> 119 119 101 105 81 90
#> 120 120 101 105 91 100
#> 121 121 101 105 101 103