re-coded hash tables to match type + cleaned some fct

This commit is contained in:
Alexandre 2024-12-30 16:22:01 +01:00
parent 8a6f109d8a
commit 69a16c4920
14 changed files with 163 additions and 290 deletions

View File

@ -2,6 +2,7 @@
"files.associations": { "files.associations": {
"ncurses.h": "c", "ncurses.h": "c",
"stdio.h": "c", "stdio.h": "c",
"math.h": "c" "math.h": "c",
"sdl_image.h": "c"
} }
} }

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -109,66 +109,6 @@ int str_to_int(char* s) {
return res; return res;
} }
int get_integer(FILE* ptr) {
char c = fgetc(ptr);
int res = 0 ;
int sign = 1;
if(c == '-') {
sign = -1;
c = fgetc(ptr);
};
while(is_an_integer(c)) {
res = 10*res + (int)c - 48;
c = fgetc(ptr);
};
return (res*sign);
}
int get_integer_plus_align(FILE* ptr, FILE* ptr2) {
char c = fgetc(ptr);
char c2 = fgetc(ptr2);
int res = 0 ;
int sign = 1;
if(c == '-') {
sign = -1;
c = fgetc(ptr);
c2 = fgetc(ptr2);
};
while(is_an_integer(c)) {
res = 10*res + (int)c - 48;
c = fgetc(ptr);
c2 = fgetc(ptr2);
};
return (res*sign);
}
int count_char_in_line(FILE* ptr, char target) {
int res = 0 ;
char c = fgetc(ptr);
while(c != EOF && c != '\n') {
if(c == target) {
res += 1;
}
c = fgetc(ptr);
};
return res;
}
void terminate_line(FILE* ptr) {
char c = fgetc(ptr);
while(c != '\n' && c != EOF) {
c = fgetc(ptr);
}
}
bool str_equal(char* s1, char* s2) { bool str_equal(char* s1, char* s2) {
if(s1[0] == '\0' || s2[0] == '\0') { if(s1[0] == '\0' || s2[0] == '\0') {
return (s1[0] == '\0' && s2[0] == '\0'); return (s1[0] == '\0' && s2[0] == '\0');

View File

@ -13,10 +13,6 @@ double to_double(int n);
int to_int(double n); int to_int(double n);
int line_count(char* filename); int line_count(char* filename);
int str_to_int(char* s); int str_to_int(char* s);
int get_integer(FILE* ptr);
int get_integer_plus_align(FILE* ptr, FILE* ptr2);
int count_char_in_line(FILE* ptr, char c);
void terminate_line(FILE* ptr);
bool str_equal(char* s1, char* s2); bool str_equal(char* s1, char* s2);
cube_0 create_cube_0(double x, double y, double z, double w, double h, double d, int r, int g, int b); cube_0 create_cube_0(double x, double y, double z, double w, double h, double d, int r, int g, int b);

View File

@ -11,9 +11,66 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_image.h> #include <SDL2/SDL_image.h>
#include "structure.h"
#include "hash.h" #include "hash.h"
// Cantor pairing function // ------------------------------------------------------------------------------------------------------- //
bool linkedList_mem(linkedList lst, int chx, int chy) {
if(lst == NULL) {
return false ;
} else if(lst->chx == chx && lst->chy == chy) {
return true ;
}
return linkedList_mem(lst->next, chx, chy) ;
}
room linkedList_find(linkedList lst, int chx, int chy) {
if(lst == NULL) {
fprintf(stderr, "ERROR : (%d, %d) is not a valid key in hash\n", chx, chy);
exit(1);
} else if(lst->chx == chx && lst->chy == chy) {
return *(lst->area) ;
}
return linkedList_find(lst->next, chx, chy) ;
}
room* linkedList_find_opt(linkedList lst, int chx, int chy) {
if(lst == NULL) {
return NULL ;
} else if(lst->chx == chx && lst->chy == chy) {
return (lst->area) ;
}
return linkedList_find_opt(lst->next, chx, chy) ;
}
void linkedList_add(linkedList lst, int chx, int chy, room* area) {
if(lst == NULL) {
fprintf(stderr, "ERROR : cannot add to NULL\n");
exit(1) ;
} else if(lst->next == NULL) {
cell* nex = malloc(sizeof(cell));
nex->area = area ;
nex->chx = chx ;
nex->chy = chy ;
nex->next = NULL ;
lst->next = nex ;
} else {
linkedList_add(lst->next, chx, chy, area) ;
}
}
void linkedList_free(linkedList lst) {
// frees rooms as well (require all created rooms to be there)
if(lst != NULL) {
linkedList_free(lst->next);
free(lst->area);
free(lst);
}
}
// ------------------------------------------------------------------------------------------------------- //
int cantor(int x, int y) { int cantor(int x, int y) {
return ((x + y) * (x + y + 1) / 2) + y; return ((x + y) * (x + y + 1) / 2) + y;
} }
@ -32,165 +89,51 @@ int generate_hash(int x, int y) {
return cantor(x, y); return cantor(x, y);
} }
void freeGrid(Grid grid) { hashtbl hashtbl_generate(int initial_size) {
for (int i = 0; i < grid->capacity; i++) { hashtbl h = malloc(sizeof(hashtbl_0));
List list = grid->data[i]; h->insertedElts = 0 ;
while (list != NULL) { h->tabLength = initial_size ;
List next = list->next; h->maxInserted = initial_size/2 ;
free(list); h->tab = malloc(sizeof(linkedList)*initial_size) ;
list = next; for(int k = 0; k < initial_size; k++) {
} h->tab[k] = NULL ;
} }
free(grid->data); return h ;
free(grid);
} }
void subGridInsert(Grid grid, int hash, chunk* elt, int x, int y) { bool hashtbl_mem(hashtbl tbl, int chx, int chy) {
int index = hash % grid->capacity; int id = generate_hash(chx, chy)%(tbl->tabLength) ;
//printf(" inserting at index [%d]", index); return linkedList_mem(tbl->tab[id], chx, chy) ;
List* data = grid->data; }
if (data[index] == NULL) {
//printf(" <-->\n"); room hashtbl_find(hashtbl tbl, int chx, int chy) {
List list = malloc(sizeof(struct List)); int id = generate_hash(chx, chy)%(tbl->tabLength) ;
list->hash = hash; return linkedList_find(tbl->tab[id], chx, chy) ;
list->x = x; }
list->y = y;
list->elt = elt; room* hashtbl_find_opt(hashtbl tbl, int chx, int chy) {
list->next = NULL; int id = generate_hash(chx, chy)%(tbl->tabLength) ;
grid->size++; return linkedList_find_opt(tbl->tab[id], chx, chy) ;
data[index] = list; }
void hashtbl_add(hashtbl tbl, int chx, int chy, room* area) {
int id = generate_hash(chx, chy)%(tbl->tabLength) ;
tbl->insertedElts += 1 ;
if(tbl->tab[id] != NULL) {
linkedList_add(tbl->tab[id], chx, chy, area);
} else { } else {
//printf(" <>\n"); linkedList nex = malloc(sizeof(cell));
List list = data[index]; nex->area = area ;
while (list->next != NULL && list->hash != hash) { nex->chx = chx ;
list = list->next; nex->chy = chy ;
}; nex->next = NULL ;
//printf(" <>\n"); tbl->tab[id] = nex ;
if (list->x == x && list->y == y) {
list->elt = elt;
} else {
List nex = malloc(sizeof(struct List));
nex->hash = hash;
nex->x = x;
nex->y = y;
nex->elt = elt;
nex->next = NULL;
list->next = nex;
grid->size++;
}
} }
} }
void gridInsert(Grid grid, int x, int z, chunk* elt) { void hashtbl_free(hashtbl tbl) {
if (grid->size >= grid->capacity * grid->loadFactor) { for(int k = 0; k < tbl->tabLength; k++) {
grid->size = 0; free(tbl->tab[k]);
grid->capacity *= 2;
printf("resizing\n");
List* old_data = grid->data;
grid->data = malloc(sizeof(List) * grid->capacity);;
for (int i = 0; i < grid->capacity; i++) {
grid->data[i] = NULL;
};
for (int i = 0; i < grid->capacity/2; i++) {
List list = old_data[i];
while (list != NULL) {
subGridInsert(grid, list->hash, list->elt, list->x, list->y);
list = list->next;
}
}
};
int hash = generate_hash(x, z);
subGridInsert(grid, hash, elt, x, z);
}
chunk* gridGet(Grid grid, int x, int z) {
int hash = generate_hash(x, z);
int index = hash % grid->capacity;
List list = grid->data[index];
while (list != NULL && (list->x != x || list->y != z)) {
list = list->next;
} }
if (list == NULL) { free(tbl);
return NULL; }
}
return list->elt;
}
bool gridMem(Grid grid, int x, int z) {
int hash = generate_hash(x, z);
int index = hash % grid->capacity;
List list = grid->data[index];
while (list != NULL && (list->x != x || list->y != z)) {
list = list->next;
}
if (list == NULL) {
return false;
}
return true;
}
chunk* gridRemove(Grid grid, int x, int z) {
int hash = generate_hash(x, z);
int index = hash % grid->capacity;
List list = grid->data[index];
List prev = NULL;
while (list != NULL && list->hash != hash) {
prev = list;
list = list->next;
}
if (list == NULL) {
return NULL;
}
chunk* elt = list->elt;
if (prev == NULL) {
grid->data[index] = list->next;
} else {
prev->next = list->next;
}
free(list);
grid->size--;
return elt;
}
Grid newGrid() {
Grid grid = malloc(sizeof(struct Grid));
grid->data = malloc(sizeof(List) * 800);
grid->size = 0;
grid->capacity = 800;
grid->loadFactor = 0.75;
for (int i = 0; i < grid->capacity; i++) {
grid->data[i] = NULL;
}
grid->push = &gridInsert;
grid->pop = &gridRemove;
grid->get = &gridGet;
grid->mem = &gridMem;
return grid;
}
chunk** gridIter(Grid grid) {
chunk** array = malloc(sizeof(chunk*) * grid->size);
int index = 0;
for (int i = 0; i < grid->capacity; i++) {
List list = grid->data[i];
while (list != NULL) {
array[index++] = list->elt;
list = list->next;
}
}
return array;
}
ChunkCoord newChunkCoord(int x, int z) {
ChunkCoord coord = malloc(sizeof(struct ChunkCoord));
coord->x = x;
coord->z = z;
return coord;
}
void freeChunkCoord(ChunkCoord coord) {
free(coord);
}

View File

@ -1,75 +1,26 @@
// credit to Benoit // // credit to Benoit //
#ifndef GRID_H // (int * int) -> room*
#define GRID_H
#ifndef HASH_H
#define HASH_H
#include "structure.h"
#include <stdbool.h> #include <stdbool.h>
typedef struct linkedList { bool linkedList_mem(linkedList lst, int chx, int chy);
uint8_t coord ; // coord%8 = x ; coord/16 = y room linkedList_find(linkedList lst, int chx, int chy);
char* flag ; room* linkedList_find_opt(linkedList lst, int chx, int chy);
struct linkedList* next ; void linkedList_add(linkedList lst, int chx, int chy, room* area);
} linkedList ; void linkedList_free(linkedList lst);
typedef struct template {
uint8_t id ;
uint8_t* lines ; // len = 8
bool checkCompat ;
uint8_t eastsig ;
uint8_t westsig ;
linkedList* meta ;
} template ;
typedef struct chunk {
int16_t chx ;
int16_t chy ;
int draw_id ;
template chdata ;
} chunk ;
//
typedef struct List {
int hash;
int x;
int y;
chunk* elt;
struct List* next;
} *List;
typedef struct Grid {
List* data;
int capacity;
int size;
double loadFactor;
void (*push)(struct Grid *grid, int x, int z, chunk* elt);
chunk* (*pop)(struct Grid *grid, int x, int z);
chunk* (*get)(struct Grid *grid, int x, int z);
bool (*mem)(struct Grid *grid, int x, int z);
} *Grid;
int cantor(int x, int y);
int generate_hash(int x, int y); int generate_hash(int x, int y);
Grid newGrid(); hashtbl hashtbl_generate(int initial_size);
void freeGrid(Grid grid); bool hashtbl_mem(hashtbl tbl, int chx, int chy);
room hashtbl_find(hashtbl tbl, int chx, int chy);
void gridInsert(Grid grid, int x, int z, chunk* elt); room* hashtbl_find_opt(hashtbl tbl, int chx, int chy);
chunk* gridGet(Grid grid, int x, int z); void hashtbl_add(hashtbl tbl, int chx, int chy, room* area);
bool gridMem(Grid grid, int x, int z); void hashtbl_free(hashtbl tbl);
chunk* gridRemove(Grid grid, int x, int z);
chunk** gridIter(Grid grid);
typedef struct ChunkCoord {
int x;
int z;
} *ChunkCoord;
ChunkCoord newChunkCoord(int x, int z);
void freeChunkCoord(ChunkCoord coord);
#endif #endif

View File

@ -34,6 +34,7 @@ int main(int argc, char** argv) {
Uint32 render_flags = SDL_RENDERER_ACCELERATED; Uint32 render_flags = SDL_RENDERER_ACCELERATED;
SDL_Renderer* rend = SDL_CreateRenderer(win, -1, render_flags); SDL_Renderer* rend = SDL_CreateRenderer(win, -1, render_flags);
SDL_SetRenderDrawBlendMode(rend, SDL_BLENDMODE_BLEND); SDL_SetRenderDrawBlendMode(rend, SDL_BLENDMODE_BLEND);
//-------------------------------------------------------------------------------// //-------------------------------------------------------------------------------//
if(SDL_Init(SDL_INIT_AUDIO)) { if(SDL_Init(SDL_INIT_AUDIO)) {

View File

@ -67,16 +67,18 @@ void playerActions() {
has_changed = true ; has_changed = true ;
switch (event.key.keysym.sym) { switch (event.key.keysym.sym) {
case SDLK_z: case SDLK_z:
camz += speed*cos(rot_hz); camz += speed*cos(rot_hz)*cos(rot_vt);
camx -= speed*sin(rot_hz); camx -= speed*sin(rot_hz)*cos(rot_vt);
camy += speed*sin(rot_vt);
break; break;
case SDLK_q: case SDLK_q:
camx -= speed*cos(rot_hz); camx -= speed*cos(rot_hz);
camz -= speed*sin(rot_hz); camz -= speed*sin(rot_hz);
break; break;
case SDLK_s: case SDLK_s:
camz -= speed*cos(rot_hz); camz -= speed*cos(rot_hz)*cos(rot_vt);
camx += speed*sin(rot_hz); camx += speed*sin(rot_hz)*cos(rot_vt);
camy -= speed*sin(rot_vt);
break; break;
case SDLK_d: case SDLK_d:
camx += speed*cos(rot_hz); camx += speed*cos(rot_hz);

View File

@ -20,6 +20,43 @@ struct cube_0 {
typedef struct cube_0 cube_0 ; typedef struct cube_0 cube_0 ;
typedef cube_0* cube ; typedef cube_0* cube ;
typedef struct teleporter {
cube_0 hitbox ;
int dest_chx ;
int dest_chy ;
double dest_x ;
double dest_y ;
} teleporter ;
struct room {
// (0, 0, 0) = bottom, left and down
int chunk_x ;
int chunk_y ;
cube* map ;
} ;
typedef struct room room ;
struct cell {
int chx ;
int chy ;
room* area ;
struct cell* next ;
} ;
typedef struct cell cell ;
typedef struct cell* linkedList ;
struct hashtbl_0 {
int tabLength ;
int insertedElts ;
int maxInserted ;
linkedList* tab ;
} ;
typedef struct hashtbl_0 hashtbl_0 ;
typedef hashtbl_0* hashtbl ;
// ------------------------------------------------ //
extern imgs digits ; extern imgs digits ;
extern imgs letters ; extern imgs letters ;
@ -34,4 +71,6 @@ extern double tan_fov ;
extern bool has_changed ; extern bool has_changed ;
extern hashtbl visited ;
#endif #endif