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

pre data pointer

parent 6960fc76
No related branches found
No related tags found
No related merge requests found
......@@ -32,7 +32,7 @@ void print_tree(b_tree tree, int depth){
print_tree(tree->children[i], depth+1);
}
else {
for(int i = 0; i < depth+1; i++){
for(int j = 0; j < depth+1; j++){
printf(" ");
}
printf("Empty\n");
......@@ -83,7 +83,7 @@ control create_control(b_tree pointer, uint64_t val){
return to_return;
}
int find_insertion_index(uint64_t* lst, int count, int val){
int find_insertion_index(uint64_t* lst, int count, uint64_t val){
int index = count;
for(int i = 0; i < count; i++){
if(val < lst[i]){
......@@ -96,7 +96,7 @@ int find_insertion_index(uint64_t* lst, int count, int val){
control split(b_tree node, b_tree next){
b_tree newNode = create_node();
int pivot = node->keys[M/2];
uint64_t pivot = node->keys[M/2];
if(is_leaf(node)){
for(int i = M/2; i < M; i++){
......
......@@ -21,9 +21,10 @@ typedef struct _control{
}control;
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, int val);
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);
......
......@@ -19,7 +19,7 @@ uint64_t hash(char* s){
val <<= 8;
val |= hash[i];
}
printf("%lu\n", val);
//printf("%lu\n", val);
return val;
}
......@@ -27,58 +27,93 @@ uint64_t record_hash(record_t* r){
return hash(r->phone);
}
void write_database_to_disk(char* fileName, record_t* database, int size){
void write_database_to_disk(char* fileName, b_tree node){
FILE* f;
f = fopen(fileName, "wb");
if(f == NULL){
assert(false);
}
fwrite(database, sizeof(record_t), size, f);
while (!is_leaf(node)) {
node = node->children[0];
}
while (node != NULL) {
for(int i = 1; i <= node->count; i++){
record_t* truc = node->children[i];
//print_record(*truc);
fwrite(truc, sizeof(record_t), 1, f);
}
node = node->children[M];
}
fclose(f);
}
void read_database_from_disk(char* fileName, record_t* database, int size){
record_t* read_data_from_disk(char* fileName, int index){
record_t* data = malloc(sizeof(record_t));
FILE* f;
f = fopen(fileName, "rb");
if(f == NULL){
assert(false);
}
fread(database, sizeof(record_t), size, f);
fseek(f, index*sizeof(record_t), SEEK_SET);
fread(data, sizeof(record_t), 1, f);
fclose(f);
return data;
}
int main(){
//hash("essdfuihfs");
b_tree node = create_node();
date_t date = create_date(1999, 7, 4);
record_t* r = create_record(date, "0765060219", "daniel", "rodriguez");
//print_record(*r);
printf("pointer to record: %p\n", r);
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* 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");
for(int i = 0; i <= 150; i += 10){
node = insert_into_tree(node, i, r);
}
//record_t* a = record_prompt();
//record_t* b = record_prompt();
node = insert_into_tree(node, 15, r);
node = insert_into_tree(node, 18, r);
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);
print_tree(node, 0);
record_t* s = search(node, 15);
printf("result of search: %p\n", s);
//write_database_to_disk("test.dat", node);
uint64_t a[4] = {0, 2, 4, 5};
printf("%d\n", find_insertion_index(a, 4, 2));
record_t* s0 = search(node, 17);//daniel
if(s0 == NULL){
printf("\n");
printf("could not find\n");
}
else{
print_record(*s0);
}
printf("\n");
record_t* s1 = search(node, record_hash(r2));//gaetan
print_record(*s1);
printf("\n");
record_t* s2 = search(node, record_hash(r4));//antoine
print_record(*s2);
//write_database_to_disk("test.dat", r, 1);
record_t* test = malloc(sizeof(record_t));
read_database_from_disk("test.dat", test, 1);
/*
record_t* test = read_data_from_disk("test.dat", 0);
print_record(*test);
free_b(node);
free(r);
free(test);
printf("\n");
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
......@@ -3,6 +3,13 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
void flush_input() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
date_t create_date(uint16_t year, uint8_t month, uint8_t day){
date_t newDate;
......@@ -12,9 +19,9 @@ date_t create_date(uint16_t year, uint8_t month, uint8_t day){
return newDate;
}
record_t* create_record(date_t birth_date, char phone[10], char first_name[20], char last_name[20]){
record_t* create_record(uint16_t year, uint8_t month, uint8_t day, char phone[10], char first_name[20], char last_name[20]){
record_t* newRecord = malloc(sizeof(record_t));
newRecord->birth_date = birth_date;
newRecord->birth_date = create_date(year, month, day);
strcpy(newRecord->phone, phone);
strcpy(newRecord->forename, first_name);
strcpy(newRecord->surname, last_name);
......@@ -28,11 +35,6 @@ void print_record(record_t record){
printf("birth date: %hu-%hhu-%hhu\n", record.birth_date.year, record.birth_date.month, record.birth_date.day);
}
void flush_input() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
record_t* record_prompt(){
printf("what is your first name ?\n");
char first_name[20];
......@@ -49,26 +51,73 @@ record_t* record_prompt(){
scanf(" %9s", phone);
flush_input();
printf("what is your birth year?\n");
printf("what is your birth year ? (input as number)\n");
uint16_t year;
scanf("%hu", &year);
flush_input();
printf("what is your birth month?\n");
printf("what is your birth month ? (input as number)\n");
uint8_t month;
scanf("%hhu", &month);
flush_input();
printf("what is your birth day?\n");
printf("what is your birth day ? (input as number)\n");
uint8_t day;
scanf("%hhu", &day);
flush_input();
date_t newDate = create_date(year, month, day);
return create_record(newDate, phone, first_name, last_name);
return create_record(year, month, day, phone, first_name, last_name);
}
/*int main(){
record_t r = record_prompt();
print_record(r);
}*/
\ No newline at end of file
/*
void handle_bounds(database_t* d, int index){
if(index < 0 || index >= d->count){
assert(0);
}
}
database_t* create_database(){
database_t* d = malloc(sizeof(database_t));
d->size = 4;
d->count = 0;
d->data = malloc(d->size * sizeof(record_t));
return d;
}
void database_add(database_t* d, record_t* record){
if(d->count >= d->size){
d->size *= 2;
d->data = realloc(d->data, d->size * sizeof(record_t));
}
d->data[d->count] = *record;
d->count += 1;
}
void database_remove(database_t* v, int index){
handle_bounds(v, index);
for(int i = index; i < v->count-1; i++){
v->data[i] = v->data[i+1];
}
v->count -= 1;
if(v->count < 0){
assert(0);
}
if(v->count < (v->size/4)){
v->size /= 2;
v->data = realloc(v->data, v->size * sizeof(record_t));
}
}
void database_print(database_t* d){
for(int i = 0; i < d->count; i++){
print_record(d->data[i]);
printf("\n");
}
}
void database_free(database_t* d){
free(d->data);
free(d);
}
*/
\ No newline at end of file
......@@ -15,9 +15,21 @@ typedef struct _record{
date_t birth_date;
}record_t;
typedef struct _database{
record_t* data;
int size;
int count;
}database_t;
date_t create_date(uint16_t year, uint8_t month, uint8_t day);
record_t* create_record(date_t birth_date, char phone[10], char first_name[20], char last_name[20]);
record_t* create_record(uint16_t year, uint8_t month, uint8_t day, char phone[10], char first_name[20], char last_name[20]);
record_t* record_prompt();
void print_record(record_t record);
database_t* create_database();
void database_add(database_t* d, record_t* record);
void database_remove(database_t* v, int index);
void database_print(database_t* d);
void database_free(database_t* d);
#endif
\ 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