Anonymous user
Matrix transposition: Difference between revisions
→{{header|C}}
m (→{{header|C}}: Remove extra whitespace) |
|||
Line 232:
Playing more to C's strengths, the following implementation transposes a matrix of any type and dimensions
in place with only O(1) space. See the [[wp:In-place_matrix_transposition|Wikipedia article]] for more information.
<lang c>void *transpose_matrix(void *matrix, int rows, int cols, size_t item_size)▼
▲*transpose_matrix(matrix,rows,cols,item_size)
int rows;▼
{
#define ALIGNMENT 16 /* power of 2 >= minimum array boundary alignment; maybe unnecessary but machine dependent */
Line 243 ⟶ 238:
char *cursor;
char carry[ALIGNMENT];
size_t block_size, remaining_size;
int nadir, lag, orbit, ents;
if
return matrix;
ents = rows * cols;
cursor = (char *) matrix;
remaining_size = item_size;
while ((block_size =
{
while (block_size)▼
{
▲ nadir = 1; /* first and last entries are always fixed points so aren't visited */
▲ while (nadir + 1 < ents)
▲ while ((orbit = (lag / rows) + cols * (lag % rows)) > nadir) /* follow a complete cycle */
while (orbit < nadir && nadir + 1 < ents) /* find the next unvisited index by an exhaustive search */
▲ }
▲ memcpy(&(cursor[lag * item_size]),carry,block_size);
if (orbit < nadir)
remaining_size = remaining_size - block_size;▼
}
}
return matrix;
}</lang>
|