Skip to content
Snippets Groups Projects
Commit 25b42d79 authored by Alec's avatar Alec
Browse files

faut trouver pourquoi unsigned sort du signed

parent e09a4e27
No related branches found
No related tags found
No related merge requests found
#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);
......
#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
......
hash.c 0 → 100644
#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
hash.h 0 → 100644
#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
#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;
......
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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment