// File: /workspace/github_cpp_makefile_star2_O1/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 0x33fc
movq %rsi, %rbx
leaq -0x78(%rbp), %rdi
callq 0x3722
movq %rbx, -0x88(%rbp)
leaq 0x90(%rbx), %rsi
leaq -0x60(%rbp), %rdi
callq 0x39a4
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 -0xf0(%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
cmpq %rax, -0x70(%rbp)
je 0x2fab
xorl %r15d, %r15d
leaq -0xf8(%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 0x3a38
movq %r12, (%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3a38
movq %r13, 0x8(%rax)
incq %r15
movq -0x78(%rbp), %rax
movq -0x70(%rbp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r15, %rcx
ja 0x2f6d
movq -0x58(%rbp), %rax
cmpq -0x60(%rbp), %rax
je 0x3087
xorl %ebx, %ebx
leaq -0xf8(%rbp), %r14
leaq -0x170(%rbp), %r15
movq -0x78(%rbp), %rax
cmpq %rax, -0x70(%rbp)
je 0x306f
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 0x3a38
movsd 0x8(%rax), %xmm0
ucomisd -0x30(%rbp), %xmm0
jbe 0x302a
movq %r14, %rdi
movq %r15, %rsi
callq 0x3a38
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x8(%rax)
movq %r14, %rdi
movq %r15, %rsi
callq 0x3a38
movsd -0x30(%rbp), %xmm0
ucomisd (%rax), %xmm0
jbe 0x3054
movq %r14, %rdi
movq %r15, %rsi
callq 0x3a38
movsd -0x30(%rbp), %xmm0
movsd %xmm0, (%rax)
incq %r13
movq -0x78(%rbp), %rax
movq -0x70(%rbp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r13, %rcx
ja 0x2fe1
incq %rbx
movq -0x58(%rbp), %rax
subq -0x60(%rbp), %rax
sarq $0x5, %rax
cmpq %rbx, %rax
ja 0x2fc9
movq -0xe0(%rbp), %r14
leaq -0xf0(%rbp), %rax
cmpq %rax, %r14
je 0x3169
leaq -0x1a0(%rbp), %r15
leaq -0xc8(%rbp), %r12
leaq -0x210(%rbp), %rbx
movsd 0x28(%r14), %xmm1
movsd 0x30(%r14), %xmm0
movsd %xmm0, -0x170(%rbp)
movl 0x20(%r14), %eax
movl %eax, -0xc8(%rbp)
subsd %xmm0, %xmm1
movsd 0x3f43(%rip), %xmm0     # 0x7020
ucomisd %xmm1, %xmm0
divsd 0x3f2f(%rip), %xmm1     # 0x7018
movsd %xmm1, -0x30(%rbp)
jbe 0x310c
movq %r15, %rdi
movq %r12, %rsi
callq 0x3aa0
movq %rax, %rdi
leaq -0x170(%rbp), %rsi
callq 0x476e
jmp 0x314e
movl $0x1, %r13d
movq %r15, %rdi
movq %r12, %rsi
callq 0x3aa0
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 0x48aa
incl %r13d
cmpl $0xa, %r13d
jne 0x3112
movq %r14, %rdi
callq 0x2130
movq %rax, %r14
leaq -0xf0(%rbp), %rax
cmpq %rax, %r14
jne 0x30b3
leaq -0xc0(%rbp), %rcx
movl $0x0, (%rcx)
xorl %eax, %eax
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
movq -0x188(%rbp), %rcx
leaq -0x198(%rbp), %rax
cmpq %rax, %rcx
je 0x33a0
movq 0x40(%rcx), %r13
movq %rcx, -0x80(%rbp)
leaq 0x30(%rcx), %rbx
cmpq %rbx, %r13
je 0x338f
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 0x32a9
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 0x3289
incl -0x170(%rbp,%rdi,4)
incl %esi
jmp 0x3292
incl -0x210(%rbp,%rdi,4)
incl %edx
addq $0x20, %rax
decq %rcx
jne 0x326f
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 0x32af
movsd %xmm5, -0x48(%rbp)
movsd %xmm4, -0x40(%rbp)
movsd %xmm3, -0x38(%rbp)
movsd %xmm2, -0x30(%rbp)
movl %r14d, -0x98(%rbp)
movsd %xmm0, -0x90(%rbp)
leaq -0xc8(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x3b08
movq %r12, %xmm0
punpckldq 0x3d15(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x3d1d(%rip), %xmm0     # 0x7050
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd -0x40(%rbp), %xmm3
divsd %xmm1, %xmm3
movsd 0x3cd8(%rip), %xmm2     # 0x7028
movapd %xmm2, %xmm0
subsd -0x48(%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 0x31bf
movq -0x80(%rbp), %rdi
callq 0x2130
movq %rax, %rcx
jmp 0x318f
movq -0xb0(%rbp), %rax
leaq -0xc0(%rbp), %r14
cmpq %r14, %rax
je 0x3406
xorl %ebx, %ebx
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd 0x3c6a(%rip), %xmm0     # 0x7030
movsd %xmm0, -0x38(%rbp)
movsd 0x30(%rax), %xmm0
movsd -0x38(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x33ed
movl 0x20(%rax), %ebx
movsd 0x28(%rax), %xmm1
movsd %xmm1, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movq %rax, %rdi
callq 0x2130
cmpq %r14, %rax
jne 0x33cb
jmp 0x3411
xorl %ebx, %ebx
xorl %r14d, %r14d
jmp 0x353a
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
xorl %ebx, %ebx
movl $0xa8, %edi
callq 0x21a0
movq %rax, %r14
movq %rax, %rdi
callq 0x250c
movl $0xa8, %edi
callq 0x21a0
movq %r14, -0x40(%rbp)
movq %rbx, -0x48(%rbp)
movq %rax, -0x38(%rbp)
movq %rax, %rdi
callq 0x250c
movq -0x60(%rbp), %rsi
cmpq %rsi, -0x58(%rbp)
je 0x34c4
movslq -0x48(%rbp), %r13
movl $0x90, %eax
movq -0x40(%rbp), %rbx
addq %rax, %rbx
movq -0x38(%rbp), %r12
addq %rax, %r12
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x8(%rsi,%r14), %rax
addq %r14, %rsi
movsd -0x30(%rbp), %xmm0
ucomisd (%rax,%r13,8), %xmm0
jae 0x348e
movq %r12, %rdi
callq 0x395c
movq -0x38(%rbp), %rax
jmp 0x349a
movq %rbx, %rdi
callq 0x395c
movq -0x40(%rbp), %rax
movq -0x60(%rbp), %rcx
movslq (%rcx,%r14), %rcx
incl 0x20(%rax,%rcx,4)
incl 0x20(%rax)
incq %r15
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rax
subq %rsi, %rax
sarq $0x5, %rax
addq $0x20, %r14
cmpq %r15, %rax
ja 0x346b
movq -0x88(%rbp), %rax
movq -0x48(%rbp), %rcx
movl %ecx, (%rax)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x8(%rax)
movl 0x8c(%rax), %eax
incl %eax
movq -0x40(%rbp), %rbx
movl %eax, 0x8c(%rbx)
movq -0x38(%rbp), %r14
movl %eax, 0x8c(%r14)
leaq -0xc8(%rbp), %rdi
callq 0x3bfe
leaq -0xf8(%rbp), %rdi
callq 0x3c5c
leaq -0x1a0(%rbp), %rdi
callq 0x3cba
leaq -0x60(%rbp), %rdi
callq 0x275a
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0x353a
movq -0x68(%rbp), %rsi
subq %rdi, %rsi
callq 0x21b0
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 -0x38(%rbp), %rdi
jmp 0x356d
jmp 0x3585
movq %rax, %rbx
movl $0xa8, %esi
movq %r14, %rdi
callq 0x21b0
jmp 0x3588
jmp 0x3585
movq %rax, %rbx
jmp 0x35ba
jmp 0x3585
jmp 0x3596
jmp 0x3596
jmp 0x3596
jmp 0x3596
movq %rax, %rbx
leaq -0xc8(%rbp), %rdi
callq 0x3bfe
jmp 0x3599
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
callq 0x3c5c
leaq -0x1a0(%rbp), %rdi
callq 0x3cba
leaq -0x60(%rbp), %rdi
callq 0x275a
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0x35cf
movq -0x68(%rbp), %rsi
subq %rdi, %rsi
callq 0x21b0
movq %rbx, %rdi
callq 0x2290