// File: /workspace/github_cpp_makefile_star2_O2/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 0x3386
movq %rsi, %rbx
leaq -0x78(%rbp), %rdi
callq 0x34a8
movq %rbx, -0x80(%rbp)
leaq 0x90(%rbx), %rsi
leaq -0x60(%rbp), %rdi
callq 0x364a
leaq -0x198(%rbp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq -0x168(%rbp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
xorl %r15d, %r15d
leaq -0x170(%rbp), %rbx
leaq -0x140(%rbp), %r14
movabsq $-0x3f70c00000000000, %r12 # imm = 0xC08F400000000000
movabsq $0x408f400000000000, %r13 # imm = 0x408F400000000000
movq -0x78(%rbp), %rax
movq -0x70(%rbp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %r15, %rdx
jbe 0x2e73
movl (%rax,%r15,4), %eax
movl %eax, -0x140(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x369e
movq %r12, (%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x369e
movq %r13, 0x8(%rax)
incq %r15
jmp 0x2e30
xorl %ebx, %ebx
leaq -0x170(%rbp), %r14
leaq -0x140(%rbp), %r15
movq -0x58(%rbp), %rdx
subq -0x60(%rbp), %rdx
sarq $0x5, %rdx
cmpq %rbx, %rdx
jbe 0x2f40
movq %rbx, %r12
shlq $0x5, %r12
xorl %r13d, %r13d
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %r13, %rdx
jbe 0x2f38
movslq (%rax,%r13,4), %rax
movl %eax, -0x140(%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 0x369e
movsd 0x8(%rax), %xmm0
ucomisd -0x30(%rbp), %xmm0
jbe 0x2efe
movq %r14, %rdi
movq %r15, %rsi
callq 0x369e
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x8(%rax)
movq %r14, %rdi
movq %r15, %rsi
callq 0x369e
movsd -0x30(%rbp), %xmm0
ucomisd (%rax), %xmm0
jbe 0x2f28
movq %r14, %rdi
movq %r15, %rsi
callq 0x369e
movsd -0x30(%rbp), %xmm0
movsd %xmm0, (%rax)
incq %r13
movq -0x78(%rbp), %rax
movq -0x70(%rbp), %rcx
jmp 0x2ea2
incq %rbx
jmp 0x2e83
movq -0x158(%rbp), %r14
leaq -0x1a0(%rbp), %r15
leaq -0xc8(%rbp), %r12
leaq -0x210(%rbp), %rbx
leaq -0x168(%rbp), %rax
cmpq %rax, %r14
je 0x300f
movsd 0x28(%r14), %xmm1
movsd 0x30(%r14), %xmm0
movsd %xmm0, -0x140(%rbp)
movl 0x20(%r14), %eax
movl %eax, -0xc8(%rbp)
subsd %xmm0, %xmm1
movsd 0x308a(%rip), %xmm0     # 0x6020
ucomisd %xmm1, %xmm0
divsd 0x3076(%rip), %xmm1     # 0x6018
movsd %xmm1, -0x30(%rbp)
jbe 0x2fd0
movq %r15, %rdi
movq %r12, %rsi
callq 0x36f0
movq %rax, %rdi
leaq -0x140(%rbp), %rsi
callq 0x4646
movq %r14, %rdi
callq 0x2140
movq %rax, %r14
jmp 0x2f5c
pushq $0x1
popq %r13
cmpl $0xa, %r13d
je 0x2fc3
movq %r15, %rdi
movq %r12, %rsi
callq 0x36f0
cvtsi2sd %r13d, %xmm0
mulsd -0x30(%rbp), %xmm0
addsd -0x140(%rbp), %xmm0
movsd %xmm0, -0x210(%rbp)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x47ea
incl %r13d
jmp 0x2fd4
leaq -0xc0(%rbp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq -0x188(%rbp), %rcx
leaq -0x198(%rbp), %rax
cmpq %rax, %rcx
je 0x3220
movl 0x20(%rcx), %ebx
movq 0x40(%rcx), %r13
movq %rcx, -0x88(%rbp)
leaq 0x30(%rcx), %r15
movslq %ebx, %r14
cmpq %r15, %r13
je 0x320c
movsd 0x20(%r13), %xmm0
xorpd %xmm1, %xmm1
movapd %xmm1, -0x140(%rbp)
movapd %xmm1, -0x130(%rbp)
movapd %xmm1, -0x120(%rbp)
movapd %xmm1, -0x110(%rbp)
movapd %xmm1, -0x100(%rbp)
movapd %xmm1, -0xf0(%rbp)
movupd %xmm1, -0xe4(%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)
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %r12
subq %rax, %r12
sarq $0x5, %r12
addq $0x8, %rax
xorl %ecx, %ecx
movq %r12, %rsi
xorl %edx, %edx
subq $0x1, %rsi
jb 0x3123
movslq -0x8(%rax), %rdi
movq (%rax), %r8
ucomisd (%r8,%r14,8), %xmm0
jbe 0x3114
incl -0x140(%rbp,%rdi,4)
incl %ecx
jmp 0x311d
incl -0x210(%rbp,%rdi,4)
incl %edx
addq $0x20, %rax
jmp 0x30f4
cvtsi2sd %ecx, %xmm3
cvtsi2sd %edx, %xmm2
xorpd %xmm5, %xmm5
xorl %eax, %eax
xorpd %xmm4, %xmm4
cmpq $0x1b, %rax
je 0x316a
cvtsi2sdl -0x140(%rbp,%rax,4), %xmm1
divsd %xmm3, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm5
cvtsi2sdl -0x210(%rbp,%rax,4), %xmm1
divsd %xmm2, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm4
incq %rax
jmp 0x3135
movsd %xmm5, -0x48(%rbp)
movsd %xmm4, -0x40(%rbp)
movsd %xmm3, -0x38(%rbp)
movsd %xmm2, -0x30(%rbp)
movl %ebx, -0x98(%rbp)
movsd %xmm0, -0x90(%rbp)
leaq -0xc8(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x3742
movq %r12, %xmm0
punpckldq 0x2e94(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2e9c(%rip), %xmm0     # 0x6050
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd -0x38(%rbp), %xmm3
divsd %xmm1, %xmm3
movsd 0x2e57(%rip), %xmm2     # 0x6028
movapd %xmm2, %xmm0
subsd -0x48(%rbp), %xmm0
mulsd %xmm3, %xmm0
movsd -0x30(%rbp), %xmm3
divsd %xmm1, %xmm3
movapd %xmm2, %xmm1
subsd -0x40(%rbp), %xmm1
mulsd %xmm3, %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rax)
movq %r13, %rdi
callq 0x21f0
movq %rax, %r13
jmp 0x3057
movq -0x88(%rbp), %rdi
callq 0x2140
movq %rax, %rcx
jmp 0x3032
movsd 0x2e08(%rip), %xmm0     # 0x6030
movsd %xmm0, -0x38(%rbp)
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
xorl %ebx, %ebx
movq -0xb0(%rbp), %rax
leaq -0xc0(%rbp), %rcx
cmpq %rcx, %rax
je 0x3277
movsd 0x30(%rax), %xmm0
movsd -0x38(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x326d
movl 0x20(%rax), %ebx
movsd 0x28(%rax), %xmm1
movsd %xmm1, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movq %rax, %rdi
callq 0x2140
jmp 0x323f
movl $0xa8, %edi
callq 0x21b0
movq %rax, %r14
movq %rax, %rdi
callq 0x245c
movl $0xa8, %edi
callq 0x21b0
movq %rax, %r12
movq %rax, %rdi
callq 0x245c
movl %ebx, -0x48(%rbp)
movslq %ebx, %r13
movl $0x90, %eax
movq %r14, -0x38(%rbp)
movq %r14, %rbx
addq %rax, %rbx
movq %r12, -0x40(%rbp)
addq %rax, %r12
movq -0x60(%rbp), %rsi
xorl %r14d, %r14d
xorl %r15d, %r15d
movq -0x58(%rbp), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq %r15, %rax
jbe 0x331e
movq 0x8(%rsi,%r14), %rax
addq %r14, %rsi
movsd -0x30(%rbp), %xmm0
ucomisd (%rax,%r13,8), %xmm0
jae 0x32fa
movq %r12, %rdi
callq 0x35ee
movq -0x40(%rbp), %rax
jmp 0x3306
movq %rbx, %rdi
callq 0x35ee
movq -0x38(%rbp), %rax
movq -0x60(%rbp), %rsi
movslq (%rsi,%r14), %rcx
incl 0x20(%rax,%rcx,4)
incl 0x20(%rax)
incq %r15
addq $0x20, %r14
jmp 0x32c7
movq -0x80(%rbp), %rax
movl -0x48(%rbp), %ecx
movl %ecx, (%rax)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x8(%rax)
movl 0x8c(%rax), %eax
incl %eax
movq -0x38(%rbp), %rbx
movl %eax, 0x8c(%rbx)
movq -0x40(%rbp), %r14
movl %eax, 0x8c(%r14)
leaq -0xc8(%rbp), %rdi
callq 0x3834
leaq -0x170(%rbp), %rdi
callq 0x3890
leaq -0x1a0(%rbp), %rdi
callq 0x38ec
leaq -0x60(%rbp), %rdi
callq 0x25e4
leaq -0x78(%rbp), %rdi
callq 0x406c
jmp 0x338b
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 %r12, %rdi
jmp 0x33bd
jmp 0x33d5
movq %rax, %rbx
movl $0xa8, %esi
movq %r14, %rdi
callq 0x21c0
jmp 0x33d8
jmp 0x33d5
movq %rax, %rbx
jmp 0x340a
jmp 0x33d5
jmp 0x33e6
jmp 0x33e6
jmp 0x33e6
jmp 0x33e6
movq %rax, %rbx
leaq -0xc8(%rbp), %rdi
callq 0x3834
jmp 0x33e9
movq %rax, %rbx
leaq -0x170(%rbp), %rdi
callq 0x3890
leaq -0x1a0(%rbp), %rdi
callq 0x38ec
leaq -0x60(%rbp), %rdi
callq 0x25e4
leaq -0x78(%rbp), %rdi
callq 0x406c
movq %rbx, %rdi
callq 0x2290