102 lines
2.5 KiB
C
102 lines
2.5 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
#include "linklist.h"
|
|
#include "hashmap.h"
|
|
#include "state.h"
|
|
#define D 8
|
|
|
|
// 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
|
|
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;
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
usize hasher(void* a){
|
|
struct SizedSubmask* mask=a;
|
|
return mask->mask.raw;
|
|
}
|
|
|
|
void print_binary(usize j){
|
|
for(size_t i=1;i<=D;i++){
|
|
printf("%s",(0==(j&(1<<(D-i))))?"0":"1");
|
|
}
|
|
}
|
|
|
|
// std::collections::HashMap<SizedSubmask,XorLinklist<SizeSubmask>>
|
|
void segment(struct State* state){
|
|
state->hashmap=malloc(sizeof(struct HashMap));
|
|
hash_new(state->hashmap,hasher,eq);
|
|
|
|
struct Node* current=state->head;
|
|
|
|
state->head=NULL;
|
|
|
|
while (current!=NULL){
|
|
struct SizedSubmask* mask= current->val;
|
|
struct SizedSubmask* ip=clone_submask(mask);
|
|
current=xor_remove(NULL,current);
|
|
if(reduce_submask(mask)){
|
|
struct Node* ll=hash_pop(state->hashmap,mask);
|
|
struct Node* new=xor_insert_mid(NULL,ll,ip);
|
|
hash_insert(state->hashmap,mask,new);
|
|
}else{
|
|
state->head=xor_insert_condition(state->head,ip,cmp);
|
|
free(mask);
|
|
}
|
|
}
|
|
|
|
// C suck! we need iterator!
|
|
for(usize i=0;i<(1<<D);i++){
|
|
struct SizedSubmask mask;
|
|
mask.len=D;
|
|
mask.mask.raw=i<<(32-D);
|
|
|
|
printf("| ");
|
|
print_binary(i);
|
|
printf(" |");
|
|
|
|
struct Node* current=hash_get(state->hashmap,&mask);
|
|
struct Node* previous=NULL;
|
|
|
|
while(current!=NULL){
|
|
struct Node* next=xor_next(previous,current);
|
|
|
|
printf(" ---> | ");
|
|
display_ip(current->val);
|
|
printf(" |");
|
|
|
|
previous=current;
|
|
current=next;
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
{
|
|
printf("--------special group--------\n");
|
|
|
|
struct Node* current=state->head;
|
|
struct Node* previous=NULL;
|
|
|
|
while(current!=NULL){
|
|
struct Node* next=xor_next(previous,current);
|
|
|
|
display_ip(current->val);
|
|
printf("\n");
|
|
|
|
previous=current;
|
|
current=next;
|
|
}
|
|
|
|
printf("-----------------------------\n");
|
|
}
|
|
|
|
} |