// File: shuhaosun[P]Splitter-Windowed-CountMin/main.cpp
// Function: main
int main(int argc, char** argv)
{
    if(argc == 2)
    {
        filename_FlowTraffic = argv[1];
    }

	unmp.clear();
    unmp1.clear();
	double val;


    double memory = 0.05;  //MB

    int w = memory * 1024 * 1024 * 8.0 / COUNTER_SIZE;
    printf("error_allowed = %f\n", tau*WINDOW_SIZE/((double)w / (double)LOW_HASH_NUM));


    printf("\n******************************************************************************\n\n");
    printf("Evaluation starts!\n\n");


    Splitter *splitter;

    int packageNum = 0;

 	FILE *file_FlowTraffic = fopen(filename_FlowTraffic, "r");

    while(fgets(insert[packageNum], 200, file_FlowTraffic) != NULL)
    {
        unmp[string(insert[packageNum])]++;

        packageNum++;

        if(packageNum == N_INSERT)
            break;
    }
    fclose(file_FlowTraffic);


    for (int i = 7000; i < 10000; i++)
    {
        unmp1[string(insert[i])]++;
    }

    query = new char*[unmp.size()];
    for(int i = 0; i < unmp.size(); ++i)
        query[i] = new char[200];


    int k = 0, max_freq = 0;
    for(unordered_map<string, int>::iterator it = unmp.begin(); it != unmp.end(); it++, k++)
    {
        strcpy(query[k], (it->first).c_str());

        int tmpVal = it->second;
        max_freq = max_freq > tmpVal ? max_freq : tmpVal;
    }
    printf("max_freq = %d\n", max_freq);


    splitter = new Splitter(w / LOW_HASH_NUM, LOW_HASH_NUM);
    for(int i = 0; i < 10000; i++)
    {
        splitter->Insert(insert[i], i);
    }
    printf("packageNum = %d\n", packageNum);


    double re_cm = 0.0;
    double re_cm_sum = 0.0;
    double ae_cm = 0.0;
    double ae_cm_sum = 0.0;
	double val_cm = 0.0;    
    int cm_cor_num = 00;

    char temp[200];

    int under_E = 0;
    int over_E = 0;
    for(unordered_map<string, int>::iterator it = unmp1.begin(); it != unmp1.end(); it++)
    {
        strcpy(temp, (it->first).c_str());
    	val = (double)it->second;

		val_cm = splitter->Query(temp, 9999);
        if (val_cm < val)
        {
            under_E++;
        }
        if (val_cm > val)
        {
            over_E++;
        }

        re_cm = fabs(val_cm - val) / (val * 1.0);
        ae_cm = fabs(val_cm - val);
        re_cm_sum += re_cm;
        ae_cm_sum += ae_cm;

        if(val == val_cm)
            cm_cor_num++;
    }

    double extra;
    extra = (double)splitter->memoryusage / 8 / 1024 / 1024;
    printf("sketch memoryusage is : %f\n", memory);
    printf("extra memoryusage is : %f\n\n", extra);

    double a = unmp1.size() * 1.0;
    double b = packageNum;

    printf("under_E = : %d\n", under_E);
    printf("over_E = : %d\n\n\n", over_E);
    printf("cm\tcorrect rate = %lf\n", cm_cor_num * 1.0 / a);
    printf("-------------------------------------\n\n");

    printf("are_cm = %lf\n", re_cm_sum / a);
    printf("-------------------------------------\n\n");

    printf("aae_cm = %lf\n", ae_cm_sum / a);
    printf("-------------------------------------\n\n");


    printf("Evaluation Ends!\n");
    printf("******************************************************************************\n\n");

    return 0;
}

// Assembly:
subq $0x268, %rsp            # imm = 0x268
movl $0x0, 0x264(%rsp)
movl %edi, 0x260(%rsp)
movq %rsi, 0x258(%rsp)
cmpl $0x2, 0x260(%rsp)
jne 0x29ce
movq 0x258(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x7b6a(%rip)      # 0xa538
leaq 0xe4e97a3(%rip), %rdi   # 0xe4ec178
callq 0x3370
leaq 0xe4e97cf(%rip), %rdi   # 0xe4ec1b0
callq 0x3370
movsd 0x3672(%rip), %xmm0     # 0x6060
movsd %xmm0, 0x248(%rsp)
movsd 0x3659(%rip), %xmm0     # 0x6058
mulsd 0x248(%rsp), %xmm0
movsd 0x3648(%rip), %xmm1     # 0x6058
mulsd %xmm1, %xmm0
movsd 0x3634(%rip), %xmm1     # 0x6050
mulsd %xmm1, %xmm0
movsd 0x3620(%rip), %xmm1     # 0x6048
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x244(%rsp)
cvtsi2sdl 0x244(%rsp), %xmm1
movsd 0x35f8(%rip), %xmm0     # 0x6040
divsd %xmm0, %xmm1
movsd 0x35e4(%rip), %xmm0     # 0x6038
divsd %xmm1, %xmm0
leaq 0x3670(%rip), %rdi      # 0x60cf
movb $0x1, %al
callq 0x2040
leaq 0x3676(%rip), %rdi      # 0x60e3
movb $0x0, %al
callq 0x2040
leaq 0x36ba(%rip), %rdi      # 0x6135
movb $0x0, %al
callq 0x2040
movl $0x0, 0x234(%rsp)
movq 0x7aa4(%rip), %rdi      # 0xa538
leaq 0x36af(%rip), %rsi      # 0x614a
callq 0x2170
movq %rax, 0x228(%rsp)
movslq 0x234(%rsp), %rax
leaq 0x7ab9(%rip), %rdi      # 0xa570
imulq $0xc8, %rax, %rax
addq %rax, %rdi
movq 0x228(%rsp), %rdx
movl $0xc8, %esi
callq 0x2200
cmpq $0x0, %rax
je 0x2bd5
movslq 0x234(%rsp), %rax
imulq $0xc8, %rax, %rcx
leaq 0x7a7d(%rip), %rax      # 0xa570
addq %rcx, %rax
movq %rax, 0x58(%rsp)
leaq 0x207(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x2230
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
leaq 0x208(%rsp), %rdi
callq 0x21c0
jmp 0x2b26
leaq 0xe4e964b(%rip), %rdi   # 0xe4ec178
leaq 0x208(%rsp), %rsi
callq 0x3380
movq %rax, 0x50(%rsp)
jmp 0x2b41
movq 0x50(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
leaq 0x208(%rsp), %rdi
callq 0x20e0
leaq 0x207(%rsp), %rdi
callq 0x2160
movl 0x234(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x234(%rsp)
cmpl $0xf4240, 0x234(%rsp) # imm = 0xF4240
jne 0x2bd0
jmp 0x2bd5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f8(%rsp)
movl %eax, 0x1f4(%rsp)
jmp 0x2bbe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f8(%rsp)
movl %eax, 0x1f4(%rsp)
leaq 0x208(%rsp), %rdi
callq 0x20e0
leaq 0x207(%rsp), %rdi
callq 0x2160
jmp 0x3341
jmp 0x2aa8
movq 0x228(%rsp), %rdi
callq 0x2140
movl $0x1b58, 0x1f0(%rsp) # imm = 0x1B58
cmpl $0x2710, 0x1f0(%rsp) # imm = 0x2710
jge 0x2ce7
movslq 0x1f0(%rsp), %rax
imulq $0xc8, %rax, %rcx
leaq 0x795c(%rip), %rax      # 0xa570
addq %rcx, %rax
movq %rax, 0x40(%rsp)
leaq 0x1cf(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x2230
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
leaq 0x1d0(%rsp), %rdi
callq 0x21c0
jmp 0x2c47
leaq 0xe4e9562(%rip), %rdi   # 0xe4ec1b0
leaq 0x1d0(%rsp), %rsi
callq 0x3380
movq %rax, 0x38(%rsp)
jmp 0x2c62
movq 0x38(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
leaq 0x1d0(%rsp), %rdi
callq 0x20e0
leaq 0x1cf(%rsp), %rdi
callq 0x2160
movl 0x1f0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1f0(%rsp)
jmp 0x2bed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f8(%rsp)
movl %eax, 0x1f4(%rsp)
jmp 0x2cd5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f8(%rsp)
movl %eax, 0x1f4(%rsp)
leaq 0x1d0(%rsp), %rdi
callq 0x20e0
leaq 0x1cf(%rsp), %rdi
callq 0x2160
jmp 0x3341
leaq 0xe4e948a(%rip), %rdi   # 0xe4ec178
callq 0x33b0
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, 0xe4e9455(%rip)   # 0xe4ec170
movl $0x0, 0x1c8(%rsp)
movslq 0x1c8(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0xe4e943e(%rip), %rdi   # 0xe4ec178
callq 0x33b0
movq %rax, %rcx
movq 0x30(%rsp), %rax
cmpq %rcx, %rax
jae 0x2d7f
movl $0xc8, %edi
callq 0x2030
movq %rax, %rdx
movq 0xe4e9410(%rip), %rax   # 0xe4ec170
movslq 0x1c8(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x1c8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c8(%rsp)
jmp 0x2d26
movl $0x0, 0x1c4(%rsp)
movl $0x0, 0x1c0(%rsp)
leaq 0xe4e93dc(%rip), %rdi   # 0xe4ec178
callq 0x33c0
movq %rax, 0x1b8(%rsp)
leaq 0xe4e93c8(%rip), %rdi   # 0xe4ec178
callq 0x3410
movq %rax, 0x1b0(%rsp)
leaq 0x1b8(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
callq 0x33f0
testb $0x1, %al
jne 0x2ddb
jmp 0x2e8c
movq 0xe4e938e(%rip), %rax   # 0xe4ec170
movslq 0x1c4(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x28(%rsp)
leaq 0x1b8(%rsp), %rdi
callq 0x3440
movq %rax, %rdi
callq 0x2090
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x2120
leaq 0x1b8(%rsp), %rdi
callq 0x3440
movl 0x20(%rax), %eax
movl %eax, 0x1ac(%rsp)
movl 0x1c0(%rsp), %eax
cmpl 0x1ac(%rsp), %eax
jle 0x2e49
movl 0x1c0(%rsp), %eax
movl %eax, 0x24(%rsp)
jmp 0x2e54
movl 0x1ac(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x1c0(%rsp)
leaq 0x1b8(%rsp), %rdi
xorl %esi, %esi
callq 0x3460
movq %rax, 0x1a0(%rsp)
movl 0x1c4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c4(%rsp)
jmp 0x2da9
movl 0x1c0(%rsp), %esi
leaq 0x32b2(%rip), %rdi      # 0x614c
xorl %eax, %eax
callq 0x2040
movl $0x7a13b0, %edi         # imm = 0x7A13B0
callq 0x2150
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x18(%rsp)
movl 0x244(%rsp), %esi
movl %esi, %eax
addl $0x3, %eax
testl %esi, %esi
cmovsl %eax, %esi
sarl $0x2, %esi
movl $0x4, %edx
callq 0x3490
jmp 0x2ed6
movq 0x18(%rsp), %rax
movq %rax, 0x238(%rsp)
movl $0x0, 0x19c(%rsp)
cmpl $0x2710, 0x19c(%rsp) # imm = 0x2710
jge 0x2f5e
movq 0x238(%rsp), %rdi
movslq 0x19c(%rsp), %rax
leaq 0x765e(%rip), %rsi      # 0xa570
imulq $0xc8, %rax, %rax
addq %rax, %rsi
movl 0x19c(%rsp), %edx
callq 0x3600
movl 0x19c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x19c(%rsp)
jmp 0x2eee
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f8(%rsp)
movl %eax, 0x1f4(%rsp)
callq 0x2130
jmp 0x3341
movl 0x234(%rsp), %esi
leaq 0x31ef(%rip), %rdi      # 0x615b
movb $0x0, %al
callq 0x2040
xorps %xmm0, %xmm0
movsd %xmm0, 0x190(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x188(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x180(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x178(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x170(%rsp)
movl $0x0, 0x16c(%rsp)
movl $0x0, 0x9c(%rsp)
movl $0x0, 0x98(%rsp)
leaq 0xe4e91d9(%rip), %rdi   # 0xe4ec1b0
callq 0x33c0
movq %rax, 0x90(%rsp)
leaq 0xe4e91c5(%rip), %rdi   # 0xe4ec1b0
callq 0x3410
movq %rax, 0x88(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x33f0
testb $0x1, %al
jne 0x3016
jmp 0x31ad
leaq 0xa0(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x3440
movq %rax, %rdi
callq 0x2090
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x2120
leaq 0x90(%rsp), %rdi
callq 0x3440
cvtsi2sdl 0x20(%rax), %xmm0
movsd %xmm0, 0x250(%rsp)
movq 0x238(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
movl $0x270f, %edx           # imm = 0x270F
callq 0x3a30
movsd %xmm0, 0x170(%rsp)
movsd 0x170(%rsp), %xmm1
movsd 0x250(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x30ac
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
movsd 0x170(%rsp), %xmm0
ucomisd 0x250(%rsp), %xmm0
jbe 0x30d1
movl 0x98(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
movsd 0x170(%rsp), %xmm0
subsd 0x250(%rsp), %xmm0
movaps 0x2fc6(%rip), %xmm1     # 0x60b0
pand %xmm1, %xmm0
movsd 0x2f72(%rip), %xmm1     # 0x6068
mulsd 0x250(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x190(%rsp)
movsd 0x170(%rsp), %xmm0
subsd 0x250(%rsp), %xmm0
movaps 0x2f8b(%rip), %xmm1     # 0x60b0
pand %xmm1, %xmm0
movsd %xmm0, 0x180(%rsp)
movsd 0x190(%rsp), %xmm0
addsd 0x188(%rsp), %xmm0
movsd %xmm0, 0x188(%rsp)
movsd 0x180(%rsp), %xmm0
addsd 0x178(%rsp), %xmm0
movsd %xmm0, 0x178(%rsp)
movsd 0x250(%rsp), %xmm0
ucomisd 0x170(%rsp), %xmm0
jne 0x318f
jp 0x318f
movl 0x16c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x16c(%rsp)
jmp 0x3191
leaq 0x90(%rsp), %rdi
xorl %esi, %esi
callq 0x3460
movq %rax, 0x80(%rsp)
jmp 0x2fe4
movq 0x238(%rsp), %rax
cvtsi2sdl 0x7a13ac(%rax), %xmm0
movsd 0x2eab(%rip), %xmm1     # 0x6070
mulsd %xmm1, %xmm0
movsd 0x2ea7(%rip), %xmm1     # 0x6078
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x248(%rsp), %xmm0
leaq 0x2f7d(%rip), %rdi      # 0x616c
movb $0x1, %al
movb %al, 0xf(%rsp)
callq 0x2040
movb 0xf(%rsp), %al
movsd 0x78(%rsp), %xmm0
leaq 0x2f7d(%rip), %rdi      # 0x6188
callq 0x2040
leaq 0xe4e8f99(%rip), %rdi   # 0xe4ec1b0
callq 0x33b0
movq %rax, %xmm0
movaps 0x2e68(%rip), %xmm1     # 0x6090
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x2e6c(%rip), %xmm1     # 0x60a0
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x2e1d(%rip), %xmm1     # 0x6068
mulsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
cvtsi2sdl 0x234(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movl 0x9c(%rsp), %esi
leaq 0x2f32(%rip), %rdi      # 0x61a4
movb $0x0, %al
callq 0x2040
movl 0x98(%rsp), %esi
leaq 0x2f2d(%rip), %rdi      # 0x61b4
movb $0x0, %al
callq 0x2040
cvtsi2sdl 0x16c(%rsp), %xmm0
movsd 0x2dc9(%rip), %xmm1     # 0x6068
mulsd %xmm1, %xmm0
divsd 0x70(%rsp), %xmm0
leaq 0x2f15(%rip), %rdi      # 0x61c5
movb $0x1, %al
callq 0x2040
leaq 0x2f1e(%rip), %rdi      # 0x61dc
movb $0x0, %al
callq 0x2040
movsd 0x188(%rsp), %xmm0
divsd 0x70(%rsp), %xmm0
leaq 0x2f29(%rip), %rdi      # 0x6204
movb $0x1, %al
callq 0x2040
leaq 0x2ef3(%rip), %rdi      # 0x61dc
movb $0x0, %al
callq 0x2040
movsd 0x178(%rsp), %xmm0
divsd 0x70(%rsp), %xmm0
leaq 0x2f0c(%rip), %rdi      # 0x6212
movb $0x1, %al
callq 0x2040
leaq 0x2ec8(%rip), %rdi      # 0x61dc
movb $0x0, %al
callq 0x2040
leaq 0x2efe(%rip), %rdi      # 0x6220
movb $0x0, %al
callq 0x2040
leaq 0x2db4(%rip), %rdi      # 0x60e4
movb $0x0, %al
callq 0x2040
xorl %eax, %eax
addq $0x268, %rsp            # imm = 0x268
retq
movq 0x1f8(%rsp), %rdi
callq 0x2220
nop