From fd9d9197612606e33f4923e8f617865a6c85485a Mon Sep 17 00:00:00 2001 From: eason <30045503+Eason0729@users.noreply.github.com> Date: Sun, 24 Dec 2023 20:21:40 +0800 Subject: [PATCH] finish --- .gitignore | 4 +++- readme.md | 54 --------------------------------------------------- src/hashmap.c | 5 +++++ src/main.c | 48 ++++++++++++++++----------------------------- src/v2.c | 12 +++++++++++- src/v3.c | 5 ++++- 6 files changed, 40 insertions(+), 88 deletions(-) diff --git a/.gitignore b/.gitignore index 248df88..151b80d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,6 @@ /Makefile /CMakeCache.txt *.profdata -*.profraw \ No newline at end of file +*.profraw +*.txt +/homework_11 \ No newline at end of file diff --git a/readme.md b/readme.md index 773f0aa..01ed192 100644 --- a/readme.md +++ b/readme.md @@ -1,55 +1 @@ # homework 11 (HW11) - -## Quick Start - -1. Get a computer with x86 feature level v3 -2. Run `./homework_11` -3. See `./Doc` to skip running the actual program - -## Setup - -1. install `clang` and `just` -2. run `just profile` -3. run `just release-profile` -4. run `./homework_11` - -## Shellshot?~~Screenshot~~ - -```console -Welcome to homework 11 (HW11)! -Does it look very familiar? - - [ OK ] Reached target input. - There are 1 prefix which have prefix len of 3 - There are 1 prefix which have prefix len of 5 - There are 3 prefix which have prefix len of 6 - There are 4 prefix which have prefix len of 7 - There are 107 prefix which have prefix len of 8 - There are 3 prefix which have prefix len of 9 - There are 5 prefix which have prefix len of 10 - There are 9 prefix which have prefix len of 11 - There are 47 prefix which have prefix len of 12 - There are 70 prefix which have prefix len of 13 - There are 164 prefix which have prefix len of 14 - There are 317 prefix which have prefix len of 15 - There are 6117 prefix which have prefix len of 16 - There are 1083 prefix which have prefix len of 17 - There are 1769 prefix which have prefix len of 18 - There are 4489 prefix which have prefix len of 19 - There are 5553 prefix which have prefix len of 20 - There are 4246 prefix which have prefix len of 21 - There are 6090 prefix which have prefix len of 22 - There are 7772 prefix which have prefix len of 23 - There are 43679 prefix which have prefix len of 24 - There are 383 prefix which have prefix len of 25 - There are 387 prefix which have prefix len of 26 - There are 439 prefix which have prefix len of 27 - There are 338 prefix which have prefix len of 28 - There are 989 prefix which have prefix len of 29 - There are 454 prefix which have prefix len of 30 - There are 1 prefix which have prefix len of 31 - There are 113 prefix which have prefix len of 32 - [ OK ] Reached target length_distribution. - 'd' is 8 - There are 4949 prefix in group 3355443200 (200.0.0.0/8) -``` \ No newline at end of file diff --git a/src/hashmap.c b/src/hashmap.c index 733a338..0a8de5f 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -22,6 +22,8 @@ struct HashMap{ //.please return '1' in clang, because only exactly '1' are guarantee to be true, // value other than '0' and '1' in bool can cause UB. + +// initialize hashmap void hash_new(struct HashMap* map,usize (*hasher)(void *), bool (*eq)(void *,void *)){ map->eq=eq; map->hasher=hasher; @@ -32,6 +34,7 @@ void hash_new(struct HashMap* map,usize (*hasher)(void *), bool (*eq)(void *,voi } } +// insert a key value pair void hash_insert(struct HashMap* map,void* key,void* val){ usize hashed=map->hasher(key); struct Entry* entry=map->entry+(hashed%B); @@ -44,6 +47,7 @@ void hash_insert(struct HashMap* map,void* key,void* val){ } } +// insert get pointer of key, return NULL if not found void* hash_get(struct HashMap* map,void* key){ usize hashed=map->hasher(key); struct Entry* entry=map->entry+(hashed%B); @@ -56,6 +60,7 @@ void* hash_get(struct HashMap* map,void* key){ return NULL; } +// insert get pointer of key, return NULL if not pop void* hash_pop(struct HashMap* map,void* key){ usize hashed=map->hasher(key); struct Entry* entry=map->entry+(hashed%B); diff --git a/src/main.c b/src/main.c index 3ecf87e..a00d7f8 100644 --- a/src/main.c +++ b/src/main.c @@ -9,38 +9,21 @@ int main() { - // struct State state; - // state.head=NULL; + struct State state; + state.head=NULL; - // input(&state); - // length_distribution(&state); - // segment(&state); - // prefix_insert(&state); - // prefix_delete(&state); - // search(&state); + input(&state); + length_distribution(&state); + segment(&state); + prefix_insert(&state); + prefix_delete(&state); + search(&state); - FILE * table = fopen("routing_table.txt","r"); - usize amount=0; - usize store[256]; + // FILE * table = fopen("routing_table.txt","r"); + // usize amount=0; + // usize store[256]; - for(usize i=0;i<256;i++)store[i]=0; - - while(true){ - char buffer[20]={'\0'}; - fgets(buffer,20,table); - if(*buffer=='\0') break; - - amount++; - - struct SizedSubmask mask; - parse_submask(buffer,&mask); - if(reduce_submask(&mask)) store[mask.mask.mask[3]]++; - } - printf("The total number of prefixes in the input file is : %zu.\n",amount); - - fclose(table); - - // table = fopen("deleted_prefixes.txt","r"); + // for(usize i=0;i<256;i++)store[i]=0; // while(true){ // char buffer[20]={'\0'}; @@ -51,10 +34,13 @@ int main() // struct SizedSubmask mask; // parse_submask(buffer,&mask); - // if(reduce_submask(&mask)) store[mask.mask.mask[3]]--; + // if(reduce_submask(&mask)) store[mask.mask.mask[3]]++; // } + // printf("The total number of prefixes in the input file is : %zu.\n",amount); - for(usize i=0;i<256;i++)printf("The number of prefixes in group %zu = %zu\n",i,store[i]); + // fclose(table); + + // for(usize i=0;i<256;i++)printf("The number of prefixes in group %zu = %zu\n",i,store[i]); return 0; } diff --git a/src/v2.c b/src/v2.c index 559176b..44206e8 100644 --- a/src/v2.c +++ b/src/v2.c @@ -8,23 +8,28 @@ // assume there is no prefix like this 192.0.0.0/1, this is a strange prefix, // because 192.0.0.0 has first two bit set, but is has submask smaller than two + +// determine whether two SizedSubmask are equal bool eq(void* a,void* b){ struct SizedSubmask* mask_a=a; struct SizedSubmask* mask_b=b; return mask_a->mask.raw==mask_b->mask.raw; } +// if a>b, return true bool cmp(void* a,void* b){ struct SizedSubmask* mask_a=a; struct SizedSubmask* mask_b=b; return mask_a->mask.raw>mask_b->mask.raw;// for debug } +// hasher for SizedSubmask usize hasher(void* a){ struct SizedSubmask* mask=a; return mask->mask.raw; } +// printf usize as binary void print_binary(usize j){ for(size_t i=1;i<=D;i++){ printf("%s",(0==(j&(1<<(D-i))))?"0":"1"); @@ -32,6 +37,10 @@ void print_binary(usize j){ } // std::collections::HashMap> + +// segment state->head into HashMap> +// +// where the key is reduced(only retain first D bit) void segment(struct State* state){ state->hashmap=malloc(sizeof(struct HashMap)); hash_new(state->hashmap,hasher,eq); @@ -40,6 +49,7 @@ void segment(struct State* state){ state->head=NULL; + // iterator all element in the LinkList while (current!=NULL){ struct SizedSubmask* mask= current->val; struct SizedSubmask* ip=clone_submask(mask); @@ -54,7 +64,7 @@ void segment(struct State* state){ } } - // C suck! we need iterator! + // list all ips for(usize i=0;i<(1<mask.raw==mask_b->mask.raw; } +// check if target(Submask) is within val(SizedSubmask) bool eq_v3_subset(void* val,void* target){ struct SizedSubmask* mask=val; union Submask* ip=target; @@ -20,7 +22,6 @@ bool eq_v3_subset(void* val,void* target){ return (mask->mask.raw<raw<