c_version: better datatypes

This commit is contained in:
ddidderr 2022-08-26 08:34:00 +02:00
parent 6a8208188a
commit 647286529b
Signed by: ddidderr
GPG Key ID: 3841F1C27E6F0E14

View File

@ -1,14 +1,16 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
static inline void Print_Clear() { printf("\x1b\x5b\x48\x1b\x5b\x32\x4a"); }
typedef struct Sudoku {
size_t *field;
size_t *fixed;
uint8_t *field;
bool *fixed;
size_t size;
size_t block_size;
size_t pos;
@ -21,7 +23,7 @@ static Sudoku *Sudoku_New(size_t block_size) {
Sudoku *s = calloc(1, sizeof(Sudoku));
// clang-format off
size_t many_solutions_field[81] = {
uint8_t many_solutions_field[81] = {
0, 0, 0, 0, 0, 0, 0, 0, 9,
0, 0, 0, 0, 8, 9, 0, 2, 0,
0, 0, 0, 0, 2, 0, 4, 0, 0,
@ -35,8 +37,8 @@ static Sudoku *Sudoku_New(size_t block_size) {
// clang-format on
size_t size = block_size * block_size;
size_t *field = calloc(size * size, sizeof(size_t));
size_t *fixed = calloc(size * size, sizeof(size_t));
uint8_t *field = calloc(size * size, sizeof(uint8_t));
bool *fixed = calloc(size * size, sizeof(bool));
printf("num_fields: %zu\n", size * size);
@ -49,7 +51,7 @@ static Sudoku *Sudoku_New(size_t block_size) {
for (size_t idx = 0; idx < 81; idx++) {
if (s->field[idx] != 0) {
s->fixed[idx] = 1;
s->fixed[idx] = true;
}
}
@ -70,7 +72,7 @@ static inline void Sudoku_Print(Sudoku *s) {
printf("\n");
// nr
printf("%zu", s->field[idx]);
printf("%hhu", s->field[idx]);
// space
if ((idx + 1) % s->size != 0)
@ -96,20 +98,20 @@ static inline bool Sudoku_Prev(Sudoku *s) {
return true;
}
static inline bool Sudoku_IsFixed(Sudoku *s) { return s->fixed[s->pos] == 1; }
static inline bool Sudoku_IsFixed(Sudoku *s) { return s->fixed[s->pos]; }
static inline size_t Sudoku_GetFieldAtPos(Sudoku *s, size_t pos) {
static inline uint8_t Sudoku_GetFieldAtPos(Sudoku *s, size_t pos) {
return s->field[pos];
}
static inline void Sudoku_Set(Sudoku *s, size_t nr) {
static inline void Sudoku_Set(Sudoku *s, uint8_t nr) {
s->field[s->pos] = nr;
s->pos_last_placed = s->pos;
}
static inline void Sudoku_ClearCurrentField(Sudoku *s) { Sudoku_Set(s, 0); }
static inline bool Array_Contains(size_t *arr, size_t len, size_t nr) {
static inline bool Array_Contains(uint8_t *arr, size_t len, uint8_t nr) {
for (size_t idx = 0; idx < len; idx++) {
if (arr[idx] == nr) {
return true;
@ -140,19 +142,19 @@ static inline bool Sudoku_GotoNextFreeField(Sudoku *s) {
return false;
}
static inline void Sudoku_GetRow(Sudoku *s, size_t *row) {
static inline void Sudoku_GetRow(Sudoku *s, uint8_t *row) {
for (size_t pos = 0; pos < s->size; pos++) {
row[pos] = Sudoku_GetFieldAtPos(s, (s->pos / s->size) * s->size + pos);
}
}
static inline void Sudoku_GetCol(Sudoku *s, size_t *col) {
static inline void Sudoku_GetCol(Sudoku *s, uint8_t *col) {
for (size_t pos = 0; pos < s->size; pos++) {
col[pos] = Sudoku_GetFieldAtPos(s, pos * s->size + s->pos % s->size);
}
}
static inline void Sudoku_GetBlock(Sudoku *s, size_t *block) {
static inline void Sudoku_GetBlock(Sudoku *s, uint8_t *block) {
size_t block_start_row = s->pos / s->size / s->block_size * s->block_size;
size_t block_start_col = s->pos % s->size / s->block_size * s->block_size;
@ -164,37 +166,37 @@ static inline void Sudoku_GetBlock(Sudoku *s, size_t *block) {
}
}
static inline bool Sudoku_BlockOk(Sudoku *s, size_t nr) {
static inline bool Sudoku_BlockOk(Sudoku *s, uint8_t nr) {
bool ok;
size_t block[9] = {0};
uint8_t block[9] = {0};
Sudoku_GetBlock(s, block);
ok = !Array_Contains(block, s->size, nr);
return ok;
}
static inline bool Sudoku_RowOk(Sudoku *s, size_t nr) {
static inline bool Sudoku_RowOk(Sudoku *s, uint8_t nr) {
bool ok;
size_t row[9] = {0};
uint8_t row[9] = {0};
Sudoku_GetRow(s, row);
ok = !Array_Contains(row, s->size, nr);
return ok;
}
static inline bool Sudoku_ColOk(Sudoku *s, size_t nr) {
static inline bool Sudoku_ColOk(Sudoku *s, uint8_t nr) {
bool ok;
size_t col[9] = {0};
uint8_t col[9] = {0};
Sudoku_GetCol(s, col);
ok = !Array_Contains(col, s->size, nr);
return ok;
}
static inline bool Sudoku_Ok(Sudoku *s, size_t nr) {
static inline bool Sudoku_Ok(Sudoku *s, uint8_t nr) {
return Sudoku_BlockOk(s, nr) && Sudoku_ColOk(s, nr) && Sudoku_RowOk(s, nr);
}
static inline bool Sudoku_PutValidNr(Sudoku *s) {
size_t current_nr = Sudoku_GetFieldAtPos(s, s->pos);
for (size_t nr = current_nr; nr < s->size + 1; nr++) {
uint8_t current_nr = Sudoku_GetFieldAtPos(s, s->pos);
for (uint8_t nr = current_nr; nr < s->size + 1; nr++) {
if (Sudoku_Ok(s, nr)) {
Sudoku_Set(s, nr);
return true;