Skip to content
Snippets Groups Projects
Commit 3bac9301 authored by Daniel Rodriguez's avatar Daniel Rodriguez
Browse files

version post data pointer

parent d41dc241
Branches
No related tags found
No related merge requests found
......@@ -5,6 +5,13 @@
#include <stdio.h>
#include <sys/types.h>
P_and_val create_P_and_val(void* pointer, uint64_t val){
P_and_val to_return;
to_return.pointer = pointer;
to_return.val = val;
return to_return;
}
b_tree create_node(){
b_tree root = malloc(sizeof(b_node));
for(int i = 0; i < M+1; i++){
......@@ -12,6 +19,7 @@ b_tree create_node(){
}
for(int i = 0; i < M; i++){
root->keys[i] = 0;
root->data[i] = create_P_and_val(NULL, 0);
}
root->count = 0;
return root;
......@@ -63,7 +71,7 @@ void print_node(b_tree node){
print_int_list(node->keys, 0, node->count, "", ", ", "");
// print unused spaces in keys(for debug)
//print_int_list(node->keys, node->count, M, ",(", ",", ")");
print_int_list(node->keys, node->count, M, ",(", ",", ")");
// print pointers(for debug)
//print_p_list(node->children, 0, M+1, " [", ",", "]");
......@@ -76,13 +84,6 @@ void print_node(b_tree node){
printf(" count=%d\n",node->count);
}
control create_control(b_tree pointer, uint64_t val){
control to_return;
to_return.pointer = pointer;
to_return.val = val;
return to_return;
}
int find_insertion_index(uint64_t* lst, int count, uint64_t val){
int index = count;
for(int i = 0; i < count; i++){
......@@ -94,22 +95,22 @@ int find_insertion_index(uint64_t* lst, int count, uint64_t val){
return index;
}
control split(b_tree node, b_tree next){
P_and_val split(b_tree node){
b_tree newNode = create_node();
uint64_t pivot = node->keys[M/2];
if(is_leaf(node)){
for(int i = M/2; i < M; i++){
newNode->keys[i - M/2] = node->keys[i];
newNode->data[i - M/2] = node->data[i];
node->keys[i] = 0;
node->data[i] = create_P_and_val(NULL, 0);
}
for(int i = M/2+1; i <= M; i++){
newNode->children[i - M/2] = node->children[i];
node->children[i] = NULL;
}
// resize two nodes
node->count = M/2;
newNode->count = M - M/2;
newNode->children[M] = next;
// connect node to newNode
node->children[M] = newNode;
}
else{
......@@ -119,69 +120,59 @@ control split(b_tree node, b_tree next){
node->keys[i] = 0;
}
for(int i = (M/2)+1; i <= M; i++){
newNode->children[i-M/2-1] = node->children[i];
newNode->children[i-(M/2 + 1)] = node->children[i];
node->children[i] = NULL;
}
// resize two nodes
node->count = M/2;
newNode->count = M - M/2 - 1;
}
return create_control(newNode, pivot);
return create_P_and_val(newNode, pivot);
}
control insert_into_node(b_tree node, uint64_t key, record_t* record){
P_and_val insert_into_node(b_tree node, uint64_t key, P_and_val data){
int index = find_insertion_index(node->keys, node->count, key);
// insert val into not full list of keys
// insert key and data into list their respective list
for(int i = node->count-1; i >= index; i--){
node->keys[i+1] = node->keys[i];
node->data[i+1] = node->data[i];
}
node->keys[index] = key;
node->data[index] = data;
node->count += 1;
// pointer to next leaf
b_tree next = node->children[M];
// insert pointer to record into not full list of children
if(is_leaf(node)){
for(int i = node->count-1; i >= index+1; i--){
node->children[i+1] = node->children[i];
}
node->children[index+1] = record;
}
if(node->count < M){
return create_control(NULL, 0);
return create_P_and_val(NULL, 0);
}
else{
return split(node, next);
return split(node);
}
}
control insert(b_tree tree, uint64_t key, record_t* record){
control retenue = create_control(NULL, 0);
P_and_val insert(b_tree tree, uint64_t key, P_and_val data){
P_and_val retenue = create_P_and_val(NULL, 0);
if (is_leaf(tree)){
retenue = insert_into_node(tree, key, record);
retenue = insert_into_node(tree, key, data);
}
else{
int index = find_insertion_index(tree->keys, tree->count, key);
retenue = insert(tree->children[index], key, record);
retenue = insert(tree->children[index], key, data);
if(retenue.pointer != NULL){
for(int i = tree->count; i >=index+1; i--){
tree->children[i+1] = tree->children[i];
}
tree->children[index+1] = retenue.pointer;
retenue = insert_into_node(tree, retenue.val, record);
retenue = insert_into_node(tree, retenue.val, data);
}
}
return retenue;
}
b_tree insert_into_tree(b_tree tree, uint64_t key, record_t* record){
control retenue = insert(tree, key, record);
b_tree insert_into_tree(b_tree tree, uint64_t key, P_and_val data){
P_and_val retenue = insert(tree, key, data);
if(retenue.pointer != NULL){
//printf("il y a une retenue\n");
b_tree newRoot = create_node();
newRoot->children[0] = tree;
newRoot->keys[0] = retenue.val;
......@@ -192,20 +183,18 @@ b_tree insert_into_tree(b_tree tree, uint64_t key, record_t* record){
return tree;
}
record_t* search(b_tree tree, uint64_t key){
P_and_val search(b_tree tree, uint64_t key){
int index = find_insertion_index(tree->keys, tree->count, key);
//printf("%d\n", index);
if(!is_leaf(tree)){
return search(tree->children[index], key);
}
else{
//printf("leaf: %lu, %lu\n", tree->keys[index-1], key);
// gerer collisions ?
if(tree->keys[index-1] == key){
return tree->children[index];
return tree->data[index-1];
}
else{
return NULL;
return create_P_and_val(NULL, 0);
}
}
}
......@@ -218,5 +207,11 @@ void free_b(b_tree node){
}
}
}
for(int i = 0; i < node->count; i++){
record_t* data = node->data[i].pointer;
if(data != NULL){
free(data);
}
}
free(node);
}
\ No newline at end of file
......@@ -7,30 +7,31 @@
#define M 4
typedef struct _P_and_val{
uint64_t val;
void* pointer;
}P_and_val;
typedef struct _b_node{
void* children[M+1];
struct _b_node* children[M+1];
uint64_t keys[M];
P_and_val data[M];
int count;
}b_node;
typedef b_node* b_tree;
typedef struct _control{
uint64_t val;
b_tree pointer;
}control;
P_and_val create_P_and_val(void* pointer, uint64_t val);
b_tree create_node();
bool is_leaf(b_tree node);
void print_node(b_tree node);
void print_tree(b_tree tree, int depth);
int find_insertion_index(uint64_t* lst, int size, uint64_t val);
void insert_into_not_full_list(uint64_t* lst, int count, int val);
control split(b_tree node, b_tree next);
control insert_into_node(b_tree node, uint64_t key, record_t* record);
control insert(b_tree tree, uint64_t key, record_t* record);
b_tree insert_into_tree(b_tree tree, uint64_t key, record_t* record);
record_t* search(b_tree tree, uint64_t key);
P_and_val split(b_tree node);
P_and_val insert_into_node(b_tree node, uint64_t key, P_and_val data);
P_and_val insert(b_tree tree, uint64_t key, P_and_val data);
b_tree insert_into_tree(b_tree tree, uint64_t key, P_and_val data);
P_and_val search(b_tree tree, uint64_t key);
void free_b(b_tree node);
#endif
\ No newline at end of file
......@@ -37,8 +37,8 @@ void write_database_to_disk(char* fileName, b_tree node){
node = node->children[0];
}
while (node != NULL) {
for(int i = 1; i <= node->count; i++){
record_t* truc = node->children[i];
for(int i = 0; i < node->count; i++){
record_t* truc = node->data[i].pointer;
//print_record(*truc);
fwrite(truc, sizeof(record_t), 1, f);
}
......@@ -64,7 +64,7 @@ record_t* read_data_from_disk(char* fileName, int index){
int main(){
record_t* r0 = create_record(2000, 0, 0, "0", "daniel", "rodriguez");
record_t* r1 = create_record(2001, 1, 1, "1", "pierre", "roden");
record_t* r2 = create_record(2002, 2, 2, "2", "gaetan", "siffert");
record_t* r2 = create_record(2002, 2, 2, "2", "gagaetan", "siffert");
record_t* r3 = create_record(2003, 3, 3, "3", "lucas", "tschaler");
record_t* r4 = create_record(2004, 4, 4, "4", "antoine", "gilles");
record_t* r5 = create_record(2005, 5, 5, "5", "william", "ho");
......@@ -74,17 +74,17 @@ int main(){
b_tree node = create_node();
node = insert_into_tree(node, record_hash(r0), r0);
node = insert_into_tree(node, record_hash(r1), r1);
node = insert_into_tree(node, record_hash(r2), r2);
node = insert_into_tree(node, record_hash(r3), r3);
node = insert_into_tree(node, record_hash(r4), r4);
node = insert_into_tree(node, record_hash(r5), r5);
node = insert_into_tree(node, record_hash(r0), create_P_and_val(r0, 0));
node = insert_into_tree(node, record_hash(r1), create_P_and_val(r1, 0));
node = insert_into_tree(node, record_hash(r2), create_P_and_val(r2, 0));
node = insert_into_tree(node, record_hash(r3), create_P_and_val(r3, 0));
node = insert_into_tree(node, record_hash(r4), create_P_and_val(r4, 0));
node = insert_into_tree(node, record_hash(r5), create_P_and_val(r5, 0));
print_tree(node, 0);
//write_database_to_disk("test.dat", node);
write_database_to_disk("test.dat", node);
record_t* s0 = search(node, 17);//daniel
record_t* s0 = search(node, record_hash(r0)).pointer;//daniel
if(s0 == NULL){
printf("\n");
printf("could not find\n");
......@@ -93,10 +93,10 @@ int main(){
print_record(*s0);
}
printf("\n");
record_t* s1 = search(node, record_hash(r2));//gaetan
record_t* s1 = search(node, record_hash(r2)).pointer;//gaetan
print_record(*s1);
printf("\n");
record_t* s2 = search(node, record_hash(r4));//antoine
record_t* s2 = search(node, record_hash(r4)).pointer;//antoine
print_record(*s2);
/*
......@@ -107,13 +107,13 @@ int main(){
record_t* test1 = read_data_from_disk("test.dat", 1);
print_record(*test1);
free(test1);
*/
free(r0);
free(r1);
free(r2);
free(r3);
free(r4);
free(r5);
*/
free_b(node);
return EXIT_SUCCESS;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment