Formatting to match Google style

This commit is contained in:
Miguel M 2023-04-30 20:21:12 +01:00
parent 216836f7f9
commit bba8aa9e17
2 changed files with 107 additions and 128 deletions

View File

@ -1,22 +1,18 @@
#include "../includes/matrix.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
// Prints a debug view of a `matrix_t` to standard output.
void DebugPrintMatrix(matrix_t *matrix)
{
printf("Grid ( len: %zu, head: %p ) { ",
(matrix->len), (void *)(matrix->head));
void DebugPrintMatrix(matrix_t *matrix) {
printf("Grid ( len: %zu, head: %p ) { ", (matrix->len),
(void *)(matrix->head));
printf("data: [");
for (size_t i = 0; i < matrix->len; i++)
{
if (i > 0)
{
for (size_t i = 0; i < matrix->len; i++) {
if (i > 0) {
printf(", ");
}
else
{
} else {
printf(" ");
}
printf("%" PRImDATA, *(matrix->head + i));
@ -26,7 +22,7 @@ void DebugPrintMatrix(matrix_t *matrix)
// Prints a row of a `matrix_t` to standard output.
void PrintMatrixRow(matrix_t *matrix, size_t row) {
for (size_t entry = 0; entry < matrix->row_len; entry ++) {
for (size_t entry = 0; entry < matrix->row_len; entry++) {
printf("%" PRImDATA, *(matrix->head + row * (matrix->row_len) + entry));
if (entry != matrix->row_len - 1) {
printf(", ");
@ -36,8 +32,7 @@ void PrintMatrixRow(matrix_t *matrix, size_t row) {
}
// Parameters for `increase_input_buffers`.
typedef struct
{
typedef struct {
size_t capacity;
data_t *head;
} matrix_bufs_t;
@ -46,12 +41,10 @@ typedef struct
//
// As reallocation will happen, the updated pointers and capacity are returned
// in an updated parameters object.
static matrix_bufs_t increase_input_buffers(matrix_bufs_t bufs)
{
static matrix_bufs_t increase_input_buffers(matrix_bufs_t bufs) {
bufs.capacity *= 2;
bufs.head = realloc(bufs.head, bufs.capacity * sizeof(data_t));
if (bufs.head == NULL)
{
if (bufs.head == NULL) {
printf(
"Failed to reallocate bigger buffer for input data. Was trying to "
"allocate %zu entries, aborting.",
@ -62,64 +55,54 @@ static matrix_bufs_t increase_input_buffers(matrix_bufs_t bufs)
}
// Compacts the allocated memory to fit exactly the number of elements.
static void compact_buffers(matrix_t *matrix)
{
static void compact_buffers(matrix_t *matrix) {
matrix->head = realloc(matrix->head, matrix->len * sizeof(data_t));
if ((matrix->head == NULL))
{
fprintf(
stderr,
"Failed to reallocate matrix data buffers. Was trying to "
"allocate for %zu data entries, aborting.",
matrix->len);
if ((matrix->head == NULL)) {
fprintf(stderr,
"Failed to reallocate matrix data buffers. Was trying to "
"allocate for %zu data entries, aborting.",
matrix->len);
exit(EXIT_FAILURE);
}
}
static data_t as_matrix_entry(char *str, size_t str_len) {
data_t value;
str[str_len] = 0; // Null-terminate the string.
int result = sscanf(str, "%" SCNmDATA, &value);
if (result == 0 || result == EOF)
{
// Failed to parse this as an entry.
fprintf(stderr, "Cannot parse %s as a numerical matrix entry, aborting.", str);
exit(EXIT_FAILURE);
}
return value;
data_t value;
str[str_len] = 0; // Null-terminate the string.
int result = sscanf(str, "%" SCNmDATA, &value);
if (result == 0 || result == EOF) {
// Failed to parse this as an entry.
fprintf(stderr, "Cannot parse %s as a numerical matrix entry, aborting.",
str);
exit(EXIT_FAILURE);
}
return value;
}
// Parse a `matrix_t` by reading from standard input.
//
// We expect whitespace separated (possibly signed) integer values.
matrix_t ParseMatrix(size_t row_len)
{
size_t capacity = 32; // Initial value is arbitrary.
size_t len = 0; // Number of entries stored.
matrix_t ParseMatrix(size_t row_len) {
size_t capacity = 32; // Initial value is arbitrary.
size_t len = 0; // Number of entries stored.
data_t *head = malloc(capacity * sizeof(data_t));
_Bool spaces = 1;
char scanned;
char scan_str[50];
size_t scan_len = 0;
while (scanf("%c", &scanned) != EOF)
{
if (isspace(scanned))
{
if (!spaces)
{
while (scanf("%c", &scanned) != EOF) {
if (isspace(scanned)) {
if (!spaces) {
// Finished reading string.
// Push the numerical entry into the matrix.
data_t value = as_matrix_entry(scan_str, scan_len);
head[len-1] = value;
head[len - 1] = value;
}
spaces = 1;
}
else
{
} else {
// A little input sanitization.
if (!(isdigit(scanned) || (scanned == '-')))
{
if (!(isdigit(scanned) || (scanned == '-'))) {
fprintf(
stderr,
"Foreign character %c (%x) found when processing input, aborting.",
@ -127,15 +110,13 @@ matrix_t ParseMatrix(size_t row_len)
exit(EXIT_FAILURE);
}
if (spaces)
{
if (spaces) {
// New string
len++;
scan_len = 0;
// Check if we need to resize the buffer.
if (len > capacity)
{
if (len > capacity) {
matrix_bufs_t bufs = {
.capacity = capacity,
.head = head,
@ -151,24 +132,23 @@ matrix_t ParseMatrix(size_t row_len)
scan_str[scan_len] = scanned;
scan_len++;
if (scan_len == 50)
{
fprintf(stderr, "An entry in the matrix is too long (over 50 characters).");
if (scan_len == 50) {
fprintf(stderr,
"An entry in the matrix is too long (over 50 characters).");
exit(EXIT_FAILURE);
}
}
} // End of scan loop
} // End of scan loop
// Write the last entry if necessary
if (!spaces) {
data_t value = as_matrix_entry(scan_str, scan_len);
head[len] = value;
data_t value = as_matrix_entry(scan_str, scan_len);
head[len] = value;
}
// Input sanitization: confirm that the number of entries read is a multiple
// of the number of entries in a row.
if (!((len % row_len) == 0))
{
if (!((len % row_len) == 0)) {
fprintf(stderr,
"Number of entries is not consistent with provided row length. "
"Got row length of %zu, and read %zu entries. Aborting.",

View File

@ -1,78 +1,77 @@
#include <stdlib.h>
#include "../includes/permutation.h"
permutation_generator_t PermutationNewGenerator(size_t len)
{
size_t *permutation = malloc(2 * len * sizeof(size_t));
size_t *stack = permutation + len;
#include <stdlib.h>
permutation_generator_t generator = {
.len = len,
.permutation = permutation,
.stack = stack,
};
permutation_generator_t PermutationNewGenerator(size_t len) {
size_t *permutation = malloc(2 * len * sizeof(size_t));
size_t *stack = permutation + len;
return generator;
permutation_generator_t generator = {
.len = len,
.permutation = permutation,
.stack = stack,
};
return generator;
}
void PermutationReset(permutation_generator_t *permutation_generator)
{
size_t len = permutation_generator->len;
size_t *permutation = permutation_generator->permutation;
size_t *stack = permutation_generator->stack;
void PermutationReset(permutation_generator_t *permutation_generator) {
size_t len = permutation_generator->len;
size_t *permutation = permutation_generator->permutation;
size_t *stack = permutation_generator->stack;
// Initialize the permutation array and stack.
for (size_t i = 0; i < len; i++)
{
permutation[i] = i;
stack[i] = 0;
}
permutation_generator->pointer = 1;
permutation_generator->exhausted = (len == 0);
// Initialize the permutation array and stack.
for (size_t i = 0; i < len; i++) {
permutation[i] = i;
stack[i] = 0;
}
permutation_generator->pointer = 1;
permutation_generator->exhausted = (len == 0);
}
static void _PermutationNext(size_t * restrict permutation, size_t * restrict stack, size_t *stack_ptr, _Bool *exhausted, size_t len) {
repeat:
if (stack[*stack_ptr] < *stack_ptr) {
if (((*stack_ptr) & 1) == 0) { // Is even
// Swap A[0] and A[i]
size_t tmp = permutation[0];
permutation[0] = permutation[*stack_ptr];
permutation[*stack_ptr] = tmp;
} else {
// Swap A[c[i]] and A[i]
size_t tmp = permutation[*stack_ptr];
permutation[*stack_ptr] = permutation[stack[*stack_ptr]];
permutation[stack[*stack_ptr]] = tmp;
}
// Permutation can now be accessed.
stack[*stack_ptr] += 1;
*stack_ptr = 1;
static void _PermutationNext(size_t *restrict permutation,
size_t *restrict stack, size_t *stack_ptr,
_Bool *exhausted, size_t len) {
repeat:
if (stack[*stack_ptr] < *stack_ptr) {
if (((*stack_ptr) & 1) == 0) { // Is even
// Swap A[0] and A[i]
size_t tmp = permutation[0];
permutation[0] = permutation[*stack_ptr];
permutation[*stack_ptr] = tmp;
} else {
stack[*stack_ptr] = 0;
*stack_ptr += 1;
if (*stack_ptr < len) {
goto repeat;
} else {
*exhausted = true;
}
// Swap A[c[i]] and A[i]
size_t tmp = permutation[*stack_ptr];
permutation[*stack_ptr] = permutation[stack[*stack_ptr]];
permutation[stack[*stack_ptr]] = tmp;
}
// Permutation can now be accessed.
stack[*stack_ptr] += 1;
*stack_ptr = 1;
} else {
stack[*stack_ptr] = 0;
*stack_ptr += 1;
if (*stack_ptr < len) {
goto repeat;
} else {
*exhausted = true;
}
}
}
void PermutationNext(permutation_generator_t *permutation_generator) {
size_t *permutation = permutation_generator->permutation;
size_t *stack = permutation_generator->stack;
size_t *stack_ptr = &(permutation_generator->pointer);
size_t len = permutation_generator->len;
_Bool *exhausted = &(permutation_generator->exhausted);
_PermutationNext(permutation, stack, stack_ptr, exhausted, len);
size_t *permutation = permutation_generator->permutation;
size_t *stack = permutation_generator->stack;
size_t *stack_ptr = &(permutation_generator->pointer);
size_t len = permutation_generator->len;
_Bool *exhausted = &(permutation_generator->exhausted);
_PermutationNext(permutation, stack, stack_ptr, exhausted, len);
}
void PermutationFree(permutation_generator_t *permutation_generator)
{
free(permutation_generator->permutation);
// No need to free permutation_generator->stack, since it's contiguous.
void PermutationFree(permutation_generator_t *permutation_generator) {
free(permutation_generator->permutation);
// No need to free permutation_generator->stack, since it's contiguous.
}