// File: /workspace/github_cpp_makefile_star2_O3/liujan[P]randomforest/Tree.cpp
// Function: Tree::selectBestPart(Node*)
pair<Node*, Node*> Tree::selectBestPart(Node *current){
    if (current == NULL)
        return pair<Node *, Node *>(NULL, NULL);
    
    vector<int> features = this->randomSelect(); //随机选出所有属性的60%作为分裂的候选属性
    vector<pair<int, vector<double> > > samples = current->samples;
    map<int, set<double> > values; //保存每个候选属性所能取到的值
    map<int, pair<double, double> > max_min; //保存每个候选属性的最大取值和最小取值
    
    for (int j = 0; j < features.size(); j++){
        int index = features[j];
        max_min[index].first = -1000;
        max_min[index].second = 1000;
    }
    
    //拿到所有候选属性的最大取值和最小取值
    for (int i = 0; i < samples.size(); i++){
        for (int j = 0; j < features.size(); j++){
            int index = features[j];
            double value = samples[i].second[index];
            
            if (value < max_min[index].second)
                max_min[index].second = value;
            if (value > max_min[index].first)
                max_min[index].first = value;
        }
    }
    
    
    map<int, pair<double, double> >::iterator it_value = max_min.begin();
    int numOfPart = 10;
    for (; it_value != max_min.end(); it_value++){
        double max = it_value->second.first;
        double min = it_value->second.second;
        int index = it_value->first;
        double step = (max - min) / numOfPart;
        if ((max - min) < 1e-5) {
            values[index].insert(min);
            continue;
        }
        for (int i = 1; i < numOfPart; i++){
            values[index].insert(min + step * i);
        }
    }
    
    
    //选出最好分裂属性的最好取值
    map<pair<int, double>, double> gini;
    map<int, set<double> >::iterator it = values.begin();
    
    for (; it != values.end(); it++){
        int attr = it->first;
        set<double>::iterator  it1 = it->second.begin();
        
        for (; it1 != it->second.end(); it1++){
            double value = *it1;
            int small[numOfTypes + 1];
            int large[numOfTypes + 1];
            int small_sum = 0;
            int large_sum = 0;
            memset(small, 0, sizeof(small));
            memset(large, 0, sizeof(large));
            for (int i = 0; i < samples.size(); i++){
                int type = samples[i].first;
                if (samples[i].second[attr] < value) {
                    small[type]++;
                    small_sum++;
                }
                else {
                    large[type]++;
                    large_sum++;
                }
            }
            double sumgini_small = 0.0;
            double sumgini_large = 0.0;
            
            for (int i = 0; i < numOfTypes + 1; i++){
                sumgini_small += ((small[i] * 1.0 / small_sum) * (small[i] * 1.0 / small_sum));
                sumgini_large += ((large[i] * 1.0 / large_sum) * (large[i] * 1.0 / large_sum));
            }
            gini[make_pair(attr, value)] = small_sum * 1.0 / samples.size() * (1 - sumgini_small) + large_sum * 1.0 / samples.size() * (1 - sumgini_large);
        }
    }
    
    
    int attr = 0;
    double value = 0;
    double min_value = 10000;
    //出分裂后基尼系数最小的分裂属性的值
    map<pair<int, double>, double>::iterator it_gini = gini.begin();
    for (; it_gini != gini.end(); it_gini++){
        if (it_gini->second <= min_value){
            min_value = it_gini->second;
            attr = it_gini->first.first;
            value = it_gini->first.second;
        }
    }
    
    Node *small = new Node();
    Node *large = new Node();
    //根据上面找出的基尼系数最小的属性和值，分裂成两个节点
    for (int i = 0; i < samples.size(); i++){
        //cout << attr << " " << samples[i].second[attr] << " " << value << endl;
        if (samples[i].second[attr] <= value) {
            small->samples.push_back(samples[i]);
            int type = samples[i].first;
            small->num[type - 1]++;
            small->sum++;
        }
        else {
            large->samples.push_back(samples[i]);
            int type = samples[i].first;
            large->num[type - 1]++;
            large->sum++;
        }
    }
    
    current->attribute = attr;
    current->value = value;
    small->depth = current->depth + 1;
    large->depth = current->depth + 1;
    return pair<Node *, Node *>(small, large);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1e8, %rsp            # imm = 0x1E8
testq %rsi, %rsi
je 0x35a6
movq %rsi, %rbx
leaq -0x78(%rbp), %rdi
callq 0x377a
leaq 0x90(%rbx), %rsi
leaq -0x60(%rbp), %rdi
callq 0x3a08
movq %rbx, -0x88(%rbp)
leaq -0x198(%rbp), %rcx
xorl %eax, %eax
movl %eax, (%rcx)
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
leaq -0xf8(%rbp), %rcx
movl %eax, (%rcx)
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
movq -0x78(%rbp), %rax
movq -0x70(%rbp), %rcx
cmpq %rax, %rcx
je 0x3007
xorl %r15d, %r15d
leaq -0x100(%rbp), %rbx
leaq -0x170(%rbp), %r14
movabsq $-0x3f70c00000000000, %r12 # imm = 0xC08F400000000000
movabsq $0x408f400000000000, %r13 # imm = 0x408F400000000000
movl (%rax,%r15,4), %eax
movl %eax, -0x170(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3a9c
movq %r12, (%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3a9c
movq %r13, 0x8(%rax)
incq %r15
movq -0x78(%rbp), %rax
movq -0x70(%rbp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %r15, %rdx
ja 0x2fc6
movq -0x60(%rbp), %rdx
movq -0x58(%rbp), %rsi
cmpq %rdx, %rsi
je 0x30ef
xorl %ebx, %ebx
leaq -0x100(%rbp), %r14
leaq -0x170(%rbp), %r15
cmpq %rax, %rcx
je 0x30d6
movq %rbx, %r12
shlq $0x5, %r12
xorl %r13d, %r13d
movslq (%rax,%r13,4), %rax
movl %eax, -0x170(%rbp)
movq -0x60(%rbp), %rcx
movq 0x8(%rcx,%r12), %rcx
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, -0x30(%rbp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x3a9c
movsd 0x8(%rax), %xmm0
ucomisd -0x30(%rbp), %xmm0
jbe 0x3084
movq %r14, %rdi
movq %r15, %rsi
callq 0x3a9c
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x8(%rax)
movq %r14, %rdi
movq %r15, %rsi
callq 0x3a9c
movsd -0x30(%rbp), %xmm0
ucomisd (%rax), %xmm0
jbe 0x30ae
movq %r14, %rdi
movq %r15, %rsi
callq 0x3a9c
movsd -0x30(%rbp), %xmm0
movsd %xmm0, (%rax)
incq %r13
movq -0x78(%rbp), %rax
movq -0x70(%rbp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %r13, %rdx
ja 0x303b
movq -0x60(%rbp), %rdx
movq -0x58(%rbp), %rsi
jmp 0x30d9
movq %rax, %rcx
incq %rbx
movq %rsi, %rdi
subq %rdx, %rdi
sarq $0x5, %rdi
cmpq %rbx, %rdi
ja 0x3028
movq -0xe8(%rbp), %r14
leaq -0xf8(%rbp), %rax
cmpq %rax, %r14
je 0x31d1
leaq -0x1a0(%rbp), %r15
leaq -0xd0(%rbp), %r12
leaq -0x210(%rbp), %rbx
movsd 0x28(%r14), %xmm1
movsd 0x30(%r14), %xmm0
movsd %xmm0, -0x170(%rbp)
movl 0x20(%r14), %eax
movl %eax, -0xd0(%rbp)
subsd %xmm0, %xmm1
movsd 0x3edb(%rip), %xmm0     # 0x7020
ucomisd %xmm1, %xmm0
divsd 0x3ec7(%rip), %xmm1     # 0x7018
movsd %xmm1, -0x30(%rbp)
jbe 0x3174
movq %r15, %rdi
movq %r12, %rsi
callq 0x3b04
movq %rax, %rdi
leaq -0x170(%rbp), %rsi
callq 0x477e
jmp 0x31b6
movl $0x1, %r13d
movq %r15, %rdi
movq %r12, %rsi
callq 0x3b04
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
mulsd -0x30(%rbp), %xmm0
addsd -0x170(%rbp), %xmm0
movsd %xmm0, -0x210(%rbp)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x48a8
incl %r13d
cmpl $0xa, %r13d
jne 0x317a
movq %r14, %rdi
callq 0x2130
movq %rax, %r14
leaq -0xf8(%rbp), %rax
cmpq %rax, %r14
jne 0x311b
leaq -0xc8(%rbp), %rax
movl $0x0, (%rax)
xorl %r14d, %r14d
movq %r14, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %r14, 0x20(%rax)
movq -0x188(%rbp), %rcx
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
leaq -0x198(%rbp), %rax
cmpq %rax, %rcx
je 0x347f
movq 0x40(%rcx), %r13
movq %rcx, -0x80(%rbp)
leaq 0x30(%rcx), %rbx
cmpq %rbx, %r13
je 0x3407
movq -0x80(%rbp), %rax
movl 0x20(%rax), %r14d
movslq %r14d, %r15
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
movq %rcx, %r12
subq %rax, %r12
sarq $0x5, %r12
movsd 0x20(%r13), %xmm0
xorpd %xmm1, %xmm1
movapd %xmm1, -0x170(%rbp)
movapd %xmm1, -0x160(%rbp)
movapd %xmm1, -0x150(%rbp)
movapd %xmm1, -0x140(%rbp)
movapd %xmm1, -0x130(%rbp)
movapd %xmm1, -0x120(%rbp)
movupd %xmm1, -0x114(%rbp)
movapd %xmm1, -0x210(%rbp)
movapd %xmm1, -0x200(%rbp)
movapd %xmm1, -0x1f0(%rbp)
movapd %xmm1, -0x1e0(%rbp)
movapd %xmm1, -0x1d0(%rbp)
movapd %xmm1, -0x1c0(%rbp)
movupd %xmm1, -0x1b4(%rbp)
xorpd %xmm2, %xmm2
xorpd %xmm4, %xmm4
xorpd %xmm3, %xmm3
cmpq %rax, %rcx
je 0x331b
cmpq $0x1, %r12
movq %r12, %rcx
adcq $0x0, %rcx
addq $0x8, %rax
xorl %edx, %edx
xorl %esi, %esi
movslq -0x8(%rax), %rdi
movq (%rax), %r8
ucomisd (%r8,%r15,8), %xmm0
jbe 0x32fb
incl -0x170(%rbp,%rdi,4)
incl %esi
jmp 0x3304
incl -0x210(%rbp,%rdi,4)
incl %edx
addq $0x20, %rax
decq %rcx
jne 0x32e1
xorps %xmm4, %xmm4
cvtsi2sd %esi, %xmm4
xorps %xmm3, %xmm3
cvtsi2sd %edx, %xmm3
xorl %eax, %eax
xorpd %xmm5, %xmm5
xorps %xmm1, %xmm1
cvtsi2sdl -0x170(%rbp,%rax,4), %xmm1
divsd %xmm4, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm5
xorps %xmm1, %xmm1
cvtsi2sdl -0x210(%rbp,%rax,4), %xmm1
divsd %xmm3, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm2
incq %rax
cmpq $0x1b, %rax
jne 0x3321
movsd %xmm5, -0x90(%rbp)
movsd %xmm4, -0x48(%rbp)
movsd %xmm3, -0x38(%rbp)
movsd %xmm2, -0x30(%rbp)
movl %r14d, -0xa0(%rbp)
movsd %xmm0, -0x98(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x3b6c
movq %r12, %xmm0
punpckldq 0x3ca0(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x3ca8(%rip), %xmm0     # 0x7050
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd -0x48(%rbp), %xmm3
divsd %xmm1, %xmm3
movsd 0x3c63(%rip), %xmm2     # 0x7028
movapd %xmm2, %xmm0
subsd -0x90(%rbp), %xmm0
mulsd %xmm3, %xmm0
movsd -0x38(%rbp), %xmm3
divsd %xmm1, %xmm3
movapd %xmm2, %xmm1
subsd -0x30(%rbp), %xmm1
mulsd %xmm3, %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rax)
movq %r13, %rdi
callq 0x21f0
movq %rax, %r13
cmpq %rbx, %rax
jne 0x3231
movq -0x80(%rbp), %rdi
callq 0x2130
movq %rax, %rcx
leaq -0x198(%rbp), %rax
cmpq %rax, %rcx
jne 0x3211
movq -0xb8(%rbp), %rax
leaq -0xc8(%rbp), %rbx
xorl %r14d, %r14d
cmpq %rbx, %rax
je 0x347f
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movsd 0x3be6(%rip), %xmm0     # 0x7030
movsd %xmm0, -0x30(%rbp)
movsd 0x30(%rax), %xmm0
movsd -0x30(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x3472
movl 0x20(%rax), %r14d
movsd 0x28(%rax), %xmm1
movsd %xmm1, -0x40(%rbp)
movsd %xmm0, -0x30(%rbp)
movq %rax, %rdi
callq 0x2130
cmpq %rbx, %rax
jne 0x344f
movl $0xa8, %edi
callq 0x21a0
movq %rax, %r15
movq %rax, %rdi
callq 0x250c
movl $0xa8, %edi
callq 0x21a0
movq %r15, -0x38(%rbp)
movq %r14, -0x48(%rbp)
movq %rax, -0x30(%rbp)
movq %rax, %rdi
callq 0x250c
movq -0x60(%rbp), %rsi
cmpq %rsi, -0x58(%rbp)
je 0x352e
movslq -0x48(%rbp), %r13
movl $0x90, %eax
movq -0x38(%rbp), %rbx
addq %rax, %rbx
movq -0x30(%rbp), %r12
addq %rax, %r12
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x8(%rsi,%r14), %rax
addq %r14, %rsi
movsd -0x40(%rbp), %xmm0
ucomisd (%rax,%r13,8), %xmm0
jae 0x34fc
movq %r12, %rdi
callq 0x39c0
movq -0x30(%rbp), %rax
jmp 0x3508
movq %rbx, %rdi
callq 0x39c0
movq -0x38(%rbp), %rax
movq -0x60(%rbp), %rsi
movslq (%rsi,%r14), %rcx
incl 0x20(%rax,%rcx,4)
incl 0x20(%rax)
incq %r15
movq -0x58(%rbp), %rax
subq %rsi, %rax
sarq $0x5, %rax
addq $0x20, %r14
cmpq %r15, %rax
ja 0x34d9
movq -0x88(%rbp), %rax
movq -0x48(%rbp), %rcx
movl %ecx, (%rax)
movsd -0x40(%rbp), %xmm0
movsd %xmm0, 0x8(%rax)
movl 0x8c(%rax), %eax
incl %eax
movq -0x38(%rbp), %rbx
movl %eax, 0x8c(%rbx)
movq -0x30(%rbp), %r14
movl %eax, 0x8c(%r14)
leaq -0xd0(%rbp), %rdi
callq 0x3c6a
leaq -0x100(%rbp), %rdi
callq 0x3cc8
leaq -0x1a0(%rbp), %rdi
callq 0x3d26
leaq -0x60(%rbp), %rdi
callq 0x2778
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0x35ab
movq -0x68(%rbp), %rsi
subq %rdi, %rsi
callq 0x21b0
jmp 0x35ab
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %rbx, %rax
movq %r14, %rdx
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0xa8, %esi
movq -0x30(%rbp), %rdi
jmp 0x35de
jmp 0x35f6
movq %rax, %rbx
movl $0xa8, %esi
movq %r15, %rdi
callq 0x21b0
jmp 0x35f9
jmp 0x35f6
movq %rax, %rbx
jmp 0x362b
jmp 0x35f6
jmp 0x3607
jmp 0x3607
jmp 0x3607
jmp 0x3607
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
callq 0x3c6a
jmp 0x360a
movq %rax, %rbx
leaq -0x100(%rbp), %rdi
callq 0x3cc8
leaq -0x1a0(%rbp), %rdi
callq 0x3d26
leaq -0x60(%rbp), %rdi
callq 0x2778
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0x3640
movq -0x68(%rbp), %rsi
subq %rdi, %rsi
callq 0x21b0
movq %rbx, %rdi
callq 0x2290