-
Notifications
You must be signed in to change notification settings - Fork 0
/
transpose.c
272 lines (235 loc) · 7.35 KB
/
transpose.c
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// This software is licensed under the following licenses. Choose whichever you
// prefer.
// -----------------------------------------------------------------------------
// ISC License
// Copyright (c) 2021 Karl Nack
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
// -----------------------------------------------------------------------------
// Public Domain (www.unlicense.org)
//
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or distribute
// this software, either in source code form or as a compiled binary, for any
// purpose, commercial or non-commercial, and by any means.
//
// In jurisdictions that recognize copyright laws, the author or authors of
// this software dedicate any and all copyright interest in the software to the
// public domain. We make this dedication for the benefit of the public at
// large and to the detriment of our heirs and successors. We intend this
// dedication to be an overt act of relinquishment in perpetuity of all present
// and future rights to this software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <assert.h> // assert
#include <errno.h> // errno
#include <limits.h> // ULONG_MAX
#include <stdarg.h> // va_list, va_start, va_end
#include <stdio.h> // stdout, stderr, fputs, printf, puts, vfprintf
#include <stdlib.h> // calloc
static unsigned long
gcd(unsigned long a, unsigned long b)
{
// Compute greatest common divisor using Euclid's algorithm
while (b)
{
unsigned long temp = a % b;
a = b;
b = temp;
}
return a;
}
static unsigned long
transpose_index(unsigned long rows, unsigned long cols, unsigned long index)
{
unsigned long from_row = index / cols;
unsigned long from_col = index - (from_row * cols);
unsigned long to_col = from_row;
unsigned long to_row = from_col;
unsigned long to = to_row * rows + to_col;
return to;
}
static int
should_transpose_cycle(unsigned long rows, unsigned long cols, unsigned long start)
{
unsigned long count = 0;
unsigned long from = start;
for (;;)
{
from = transpose_index(rows, cols, from);
if (from > start)
{
++count;
}
else
{
break;
}
}
int result = (from == start) && count;
return result;
}
static unsigned long
transpose_cycle(
unsigned long rows, unsigned long cols, unsigned long *matrix,
unsigned long start)
{
unsigned long count = 0;
unsigned long from = start;
unsigned long prev = matrix[from];
for (;;)
{
++count;
unsigned long to = transpose_index(rows, cols, from);
unsigned long temp = matrix[to];
matrix[to] = prev;
if (to == start)
{
break;
}
else
{
from = to;
prev = temp;
}
}
assert(count > 1);
return count;
}
static void
transpose_matrix(unsigned long rows, unsigned long cols, unsigned long *matrix)
{
unsigned long nswaps = rows * cols;
// Reduce the number of swaps by the number of self-cycles
nswaps -= gcd(rows - 1, cols - 1) + 1;
if (nswaps)
{
unsigned long index = 1;
// Not sure if element 1 could ever be a self-cycle, but just to be safe.
while (transpose_index(rows, cols, index) == index)
{
++index;
}
nswaps -= transpose_cycle(rows, cols, matrix, index);
while (nswaps)
{
if (should_transpose_cycle(rows, cols, ++index))
{
nswaps -= transpose_cycle(rows, cols, matrix, index);
}
}
}
}
static unsigned
count_digits(unsigned long value)
{
unsigned result = 1;
while (value /= 10)
{
++result;
}
return result;
}
static void
print_matrix(unsigned long rows, unsigned long cols, const unsigned long *matrix)
{
unsigned long size = rows * cols;
unsigned ndigits = count_digits(size);
// Generate a format string to justify matrix values based on the number of
// digits of the largest value. So for a 100-element matrix, the format
// string would be "%3lu". Assuming ULONG_MAX maxes out at 20 digits
// (assuming 64-bit), the longest format string is "%20lu", which is 5
// characters + 1 for the null terminator.
assert(ndigits <= 20);
char format[6];
sprintf(format, "%%%ulu", ndigits);
for (unsigned i = 0; i < size; ++i)
{
printf(format, matrix[i]);
if (0 == ((i + 1) % cols))
{
fputs("\n", stdout);
}
else
{
fputs(" ", stdout);
}
}
fputs("\n", stdout);
}
static _Noreturn void
error_and_exit(int show_usage, const char *format, ...)
{
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
if (show_usage)
{
fputs("Usage: transpose ROWS COLS\n", stderr);
}
exit(EXIT_FAILURE);
}
static unsigned long
read_ul(const char *arg, const char *param)
{
char *end;
errno = 0;
unsigned long result = strtoul(arg, &end, 0);
if (!result || *end)
{
error_and_exit(1, "Invalid value for %s: '%s'\n", param, arg);
}
else if (result == ULONG_MAX && errno == ERANGE)
{
error_and_exit(0, "The number of %s may not exceed %lu\n", param, ULONG_MAX);
}
return result;
}
int
main(int argc, char **argv)
{
if (argc != 3)
{
error_and_exit(1, "Received %d arguments but 2 are required\n", argc - 1);
}
unsigned long rows = read_ul(argv[1], "rows");
unsigned long cols = read_ul(argv[2], "columns");
if (rows > (ULONG_MAX / cols))
{
error_and_exit(0, "rows × columns may not exceed %lu\n", ULONG_MAX);
}
unsigned long *matrix = calloc(sizeof(*matrix), rows * cols);
if (!matrix)
{
error_and_exit(0, "Unable to allocate memory for %d×%d matrix\n", rows, cols);
}
for (unsigned long n = 0; n < rows * cols; ++n)
{
matrix[n] = n + 1;
}
puts("Before:");
print_matrix(rows, cols, matrix);
transpose_matrix(rows, cols, matrix);
puts("After:");
print_matrix(cols, rows, matrix);
puts("Done!");
return 0;
}