diff --git a/src/bst.c b/src/bst.c new file mode 100644 index 0000000000000000000000000000000000000000..9f0d12efe61df963bfc40cf8bbe6efa9f817aebd --- /dev/null +++ b/src/bst.c @@ -0,0 +1,33 @@ +struct node_t { + unsigned long int key; + void* val; + struct node_t* left; + struct node_t* right; +}; + +typedef struct bst_t { + // Use this to compute a key from a val + // It is not really a hash function, the key must be unique + unsigned long int (*h)(void*); + + unsigned long int size; + struct node_t* tree; +} bst_t; + +bst_t bst_empty(void){ +} + +int bst_is_empty(bst_t t){ +} + +bst_t bst_add(bst_t t, void* x){ +} + +void bst_destroy(bst_t t, void (*free_void)(void*)){ +} + +int bst_in(bst_t t, void* x){ +} + +unsigned long int bst_size(bst_t t){ +} diff --git a/src/hash_tbl.c b/src/hash_tbl.c new file mode 100644 index 0000000000000000000000000000000000000000..647b6914f375509fdf144374d3ce710d70647a31 --- /dev/null +++ b/src/hash_tbl.c @@ -0,0 +1,30 @@ +#include <stddef.h> +#include "list.h" + +#define ARRAY_CAPACITY 1 << 16 + +struct hash_tbl { + size_t (*h)(void*); + int (*eq)(void*, void*); + void (*free)(void*); + unsigned long int size; + size_t capacity; // array length + list_t* array; +}; + +typedef struct hash_tbl* hash_tbl; + +hash_tbl htbl_empty(size_t (*h)(void*), + int (*eq)(void*, void*), + void (*free)(void*)){ +} + +int htbl_add(hash_tbl htbl, void* x){ +} + +int htbl_in(hash_tbl htbl, void* x){ +} + +void htbl_destroy(hash_tbl htbl){ +} + diff --git a/src/list.c b/src/list.c new file mode 100644 index 0000000000000000000000000000000000000000..8d62f80ddf97df6f5befafcfc042929931a06cb8 --- /dev/null +++ b/src/list.c @@ -0,0 +1,36 @@ +struct cell_t { + void* val; + unsigned long int id; + struct cell_t* next; +}; + +typedef struct cell_t* list_t; + +list_t list_empty(){ +} + +int list_is_empty(list_t l){ +} + +list_t list_push(list_t l, void* x){ +} + +list_t list_tail(list_t l){ +} + +void* list_pop(list_t* l){ +} + +void* list_top(list_t l){ +} + +void list_destroy(list_t l, void (*free)(void*)){ +} + +// return the found element or NULL +void* list_in(list_t l, void* x, int (*eq)(void*, void*)){ +} + +unsigned long int list_len(list_t l){ +} + diff --git a/src/linked_list.h b/src/list.h similarity index 87% rename from src/linked_list.h rename to src/list.h index 92494d724ab4c87b98ea3747d6118dfe76b0cd11..b1cc52096c6da589b0979da1978dc3545343d195 100644 --- a/src/linked_list.h +++ b/src/list.h @@ -16,7 +16,7 @@ list_t list_tail(list_t); void* list_pop(list_t*); void* list_top(list_t); void list_destroy(list_t, void (*free)(void*)); -int list_in(list_t, void*, int (*eq)(void*, void*)); +void* list_in(list_t, void*, int (*eq)(void*, void*)); unsigned long int list_len(list_t); #endif diff --git a/tests/exemple_collision.c b/tests/exemple_collision.c new file mode 100644 index 0000000000000000000000000000000000000000..9a3dd511319b61de452c76012497379b923e4677 --- /dev/null +++ b/tests/exemple_collision.c @@ -0,0 +1,50 @@ +#include <stdlib.h> +#include <stdio.h> +#include <time.h> +#include <openssl/md5.h> +#include "list.h" + +typedef struct pair_t { + char x[8]; + unsigned char h; +}* pair_t; + +int eq_pair(void* a, void* b){ + return ((pair_t)a)->h == ((pair_t)b)->h; +} + +/* char x[8] */ +unsigned char md5_first_byte(const char* x){ + unsigned char digest[MD5_DIGEST_LENGTH]; + + MD5((const unsigned char*)x, 8, digest); + return digest[0]; // we keep only the first byte +} + +int main(){ + pair_t p, q; + list_t l; + unsigned int i = 0; + + srand(time(NULL)); + + l = list_empty(); + + for(;;){ + p = (pair_t)malloc(sizeof(struct pair_t)); + for(int i = 0; i < 7; i++) + p->x[i] = rand() % (126-32-1) + 32; + p->x[7] = 0; + p->h = md5_first_byte(p->x); + + if((q = (pair_t)list_in(l, p, eq_pair)) != NULL){ + printf("After %d iterations, a collision happened !\n", i); + printf("%s and %s have the same MD5 first byte.\n", p->x, q->x); + + return 0; + } else { + l = list_push(l, p); + } + } +} +