From 25b42d7947529a40c4e484955c9adc2bad0fecee Mon Sep 17 00:00:00 2001 From: Alec <alec.schmidt@hesge.ch> Date: Wed, 1 Jun 2022 11:58:38 +0200 Subject: [PATCH] faut trouver pourquoi unsigned sort du signed --- bp_tree.c | 20 ++++++++++++-------- bp_tree.h | 24 +++++++++++++++++++++--- hash.c | 30 ++++++++++++++++++++++++++++++ hash.h | 8 ++++++++ main.c | 39 +++++++++++++++++++++++++++++++++++++-- makefile | 10 +++++++--- 6 files changed, 115 insertions(+), 16 deletions(-) create mode 100644 hash.c create mode 100644 hash.h diff --git a/bp_tree.c b/bp_tree.c index b1d67df..30d01db 100644 --- a/bp_tree.c +++ b/bp_tree.c @@ -1,4 +1,5 @@ #include "bp_tree.h" +#include "hash.h" #include <math.h> #include <stdbool.h> #include <stdio.h> @@ -7,7 +8,7 @@ // UTILS ---------------------------------------------------------- typedef struct control { - int value; + uint64_t value; node *lhs; node *rhs; } control; @@ -23,10 +24,11 @@ node *bp_create_node() { nd->data[i] = 0; nd->count = 0; nd->next = NULL; + nd->user = NULL; return nd; } -control value_to_insert(int val) { +control value_to_insert(uint64_t val) { control c; c.value = val; c.lhs = c.rhs = NULL; @@ -228,16 +230,18 @@ control bp_insert(node *nd, control val, int depth) { } } - // if a split happened + // if a split happened (reaches this only when in leaves) if (c.value != 0) { return c; } return CONST_CONTR; } -node *bp_insert_val(node *tree, int val) { +node *bp_insert_val(node *tree, entry person) { + uint64_t hash_i = generate_key(person.phone); + // Parse the val into a control struct then insert it in the tree - control test = bp_insert(tree, value_to_insert(val), 0); + control test = bp_insert(tree, value_to_insert(hash_i), 0); /* depending on the control item, we act differently if the control has pointers to node, then we had a split in the direct @@ -303,7 +307,7 @@ void bp_print_as_ll(node *root) { // print every values in the node until hitting 0, then we can get on the // next node for (int i = 0; i < M; i++) { - printf(" %d |", root->data[i]); + printf(" %ld |", root->data[i]); if (root->data[i + 1] == 0) { printf(" -> "); return bp_print_as_ll(root->next); @@ -317,7 +321,7 @@ void bp_print(node *root, int depth) { for (int i = 0; i < depth; i++) printf(" "); for (int i = 0; i < M; i++) { - printf(" %d |", root->data[i]); + printf(" %ld |", root->data[i]); // if we reach a 0, we have seen every values in the node if (root->data[i + 1] == 0) break; @@ -332,7 +336,7 @@ void bp_print(node *root, int depth) { for (int i = 0; i < depth; i++) printf(" "); - printf(" %d |\n", root->data[i]); + printf(" %ld |\n", root->data[i]); if (root->data[i + 1] == 0) return bp_print(root->childs[i + 1], depth + 1); diff --git a/bp_tree.h b/bp_tree.h index 74c769d..3ef45d4 100644 --- a/bp_tree.h +++ b/bp_tree.h @@ -1,14 +1,32 @@ #ifndef _BP_TREE_C_ #define _BP_TREE_C_ -#define M 4 +#include <stdint.h> +#include <stdio.h> #include <stdlib.h> +#include <string.h> + +#define M 4 + +typedef struct date { + uint8_t day; + uint8_t month; // 0 = January, 11 = December + uint8_t year; // value stored is year - 1900 +} date; + +typedef struct entry { + char phone[11]; + char f_name[21]; + char l_name[21]; + struct date birth; +} entry; typedef struct node { struct node *childs[M + 1]; - int data[M]; + uint64_t data[M]; int count; struct node *next; + struct entry *user; } node; /** @@ -40,7 +58,7 @@ node *bp_create_node(); * @param val Value to insert * @return New root of the tree */ -node *bp_insert_val(node *tree, int val); +node *bp_insert_val(node *tree, entry person); /** * @brief Print in CLI the leaves of the B+ Tree as a linked list diff --git a/hash.c b/hash.c new file mode 100644 index 0000000..bb9dc24 --- /dev/null +++ b/hash.c @@ -0,0 +1,30 @@ +#include "hash.h" +#include <openssl/sha.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> + +uint64_t generate_key(char *string) { + unsigned char hash[SHA256_DIGEST_LENGTH]; + + SHA256_CTX ctx; + SHA256_Init(&ctx); + SHA256_Update(&ctx, string, strlen(string)); + SHA256_Final(hash, &ctx); + uint64_t val = 0; + for (int i = 0; i < 8; i++) { + val |= hash[i]; + val <<= 8; + } + + // Uncomment to print the full hash value + /* + char outputBuffer[65]; + for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { + sprintf(outputBuffer + (i * 2), "%02x", hash[i]); + } + outputBuffer[64] = 0; + printf("%s\n", outputBuffer); + */ + return val; +} \ No newline at end of file diff --git a/hash.h b/hash.h new file mode 100644 index 0000000..6496822 --- /dev/null +++ b/hash.h @@ -0,0 +1,8 @@ +#ifndef _HASH_H_ +#define _HASH_H_ + +#include <stdint.h> +#include <stdio.h> + +uint64_t generate_key(char *string); +#endif \ No newline at end of file diff --git a/main.c b/main.c index acbcdcd..4648af8 100644 --- a/main.c +++ b/main.c @@ -1,14 +1,48 @@ #include "bp_tree.h" +#include "hash.h" #include <stdio.h> #include <stdlib.h> - int main() { node *tree = bp_create_node(); + entry person; + + for (int i = 0; i < 10; i++) { + person.phone[i] = '1'; + } + person.phone[10] = '\0'; + + tree = bp_insert_val(tree, person); + bp_print(tree, 0); + + printf("\n|||||||||\n"); + + for (int i = 0; i < 10; i++) { + person.phone[i] = '2'; + } + + tree = bp_insert_val(tree, person); + bp_print(tree, 0); + + printf("\n|||||||||\n"); + + for (int i = 0; i < 10; i++) { + person.phone[i] = '3'; + } + + tree = bp_insert_val(tree, person); + bp_print(tree, 0); + + printf("\n|||||||||\n"); + + for (int i = 0; i < 10; i++) { + person.phone[i] = '4'; + } - tree = bp_insert_val(tree, 3); + tree = bp_insert_val(tree, person); bp_print(tree, 0); printf("\n|||||||||\n"); + /* tree = bp_insert_val(tree, 6); bp_print(tree, 0); @@ -68,6 +102,7 @@ int main() { tree = bp_insert_val(tree, 18); bp_print(tree, 0); printf("\n|||||||||\n"); + */ bp_print_as_ll(tree); bp_destroy(tree); return 0; diff --git a/makefile b/makefile index fc034e1..cfd646e 100644 --- a/makefile +++ b/makefile @@ -1,13 +1,13 @@ CC=gcc -FLAGS= -Wall -Werror -CFLAGS= -g -std=c99 +FLAGS= -Werror +CFLAGS= -g -std=c99 -lssl -lcrypto LDFLAGS= -fsanitize=address -fsanitize=leak exe: main ./$^ -main: main.o bp_tree.o +main: main.o bp_tree.o hash.o $(CC) $^ -o $@ $(CFLAGS) $(LDFLAGS) -lm main.o: main.c @@ -15,6 +15,10 @@ main.o: main.c bp_tree.o: bp_tree.c bp_tree.h $(CC) $(FLAGS) $(CFLAGS) $(LDFLAGS) -c $< + +hash.o: hash.c hash.h + $(CC) $(FLAGS) $(CFLAGS) $(LDFLAGS) -c $< + clean: rm -f *.o test -- GitLab