This commit is contained in:
eason 2023-12-24 20:21:40 +08:00
parent de07c4cb23
commit fd9d919761
6 changed files with 40 additions and 88 deletions

4
.gitignore vendored
View File

@ -5,4 +5,6 @@
/Makefile
/CMakeCache.txt
*.profdata
*.profraw
*.profraw
*.txt
/homework_11

View File

@ -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)
```

View File

@ -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);

View File

@ -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;
}

View File

@ -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<SizedSubmask,XorLinklist<SizeSubmask>>
// segment state->head into HashMap<SizedSubmask,XorLinklist<SizeSubmask>>
//
// 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<<D);i++){
struct SizedSubmask mask;
mask.len=D;

View File

@ -7,12 +7,14 @@
#include "hashmap.h"
#include "clock.h"
// check if two SizedSubmask are equal
bool eq_v3(void* a,void* b){
struct SizedSubmask* mask_a=a;
struct SizedSubmask* mask_b=b;
return mask_a->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<<r_bit)==(ip->raw<<r_bit);
}
// remove match element, return new head
struct Node* xor_remove_match(struct Node* head,void* target,bool (*eq)(void *,void *)){
struct Node* previous=NULL;
@ -41,6 +42,8 @@ struct Node* xor_remove_match(struct Node* head,void* target,bool (*eq)(void *,v
else return head;
}
// iterate all element in target, return false if for
// every element eq(element[i], target) return false, return true otherwise
bool xor_contain(struct Node* current,void* target,bool (*eq)(void *,void *)){
struct Node* previous=NULL;
while(current!=NULL){