Index Matrix Tiles
#include <stdio.h>
#include <assert.h>
int GetTileElement(int rowCount, int columnCount, int tileRow, int tileColumn, int rowInTile, int columnInTile, int partitionRow, int partitionColumn, int arrayLength, int *array)
{
int totalTilesAlongRow = rowCount / partitionRow;
int totalTilesAlongColumn = columnCount / columnCount;
assert(rowCount % partitionRow == 0);
assert(columnCount % columnCount == 0);
assert(rowInTile > -1);assert(columnInTile > -1);
assert(rowCount > -1);assert(columnCount > -1);
assert(tileRow > -1);assert(tileColumn > -1);
assert(rowInTile < partitionRow);assert(columnInTile < partitionColumn);
assert(tileRow < totalTilesAlongRow);assert(tileColumn < totalTilesAlongColumn);
// Calculate the starting row and column of the tile
int startRow = tileRow * partitionRow;
int startCol = tileColumn * partitionColumn;
// Calculate the absolute row and column in the matrix
int globalRow = startRow + rowInTile;
int globalCol = startCol + columnInTile;
// Return the value from the matrix
int index = globalRow * columnCount + globalCol;
assert(index < arrayLength);
assert(index > -1);
return array[index];
}
int main()
{
int rowCount = 6;
int columnCount = 9;
int partitionRow = 3;
int partitionColumn = 3;
int array[] =
{
1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27,
28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54
};
int arrayLength = sizeof(array) / sizeof(int);
int tileRow = 1;
int tileColumn = 1;
int rowInTile = 3;
int columnInTile = 2;
int element = GetTileElement(rowCount, columnCount, tileRow, tileColumn, rowInTile, columnInTile, partitionRow, partitionColumn, arrayLength, array);
printf("Element at tile (%d,%d) and position (%d,%d): %d\n",tileRow,tileColumn,rowInTile,columnInTile, element);
return 0;
}