// File: /workspace/github_cpp_makefile_star2_O1/big-data-lab-team[P]OrpailleCC/./src/mondrian_coarse.hpp
// Function: CoarseMondrianForest<double, funct, KappaMetrics<2, false>, 4, 2, 6000>::split_barycenter(int, int, double const*, int, int, double)
bool split_barycenter(int const node_id, int const tree_id, feature_type const* features, int const label, int const parent_id, double const parent_tau){
	int new_parent_id, new_sibling_id;
	double sum = 0;
	Node& node = nodes()[node_id];
	double barycenter[feature_count];
	double probabilities[feature_count];
	if(split_helper == SPLIT_HELPER_WEIGHTED)
		find_barycenter(tree_bases()[tree_id].root, barycenter);
	else if(split_helper == SPLIT_HELPER_AVG){
		for(int i = 0; i < feature_count; i++){
			barycenter[i] = sum_features[i]/count_points;
		}
	}
	else if(split_helper == SPLIT_HELPER_NONE)
		return false;

	for(int i = 0; i < feature_count; ++i){
		if(barycenter[i] > node.bound_lower[i] && barycenter[i] < node.bound_upper[i]){
			probabilities[i] = Utils::abs(barycenter[i] - features[i]);
			sum += probabilities[i];
		}
		else{
			probabilities[i] = 0;
		}
	}
	double const E = sum == 0 ?  -1 : Utils::rand_exponential<func>(sum);
	if(sum == 0){
		return false;
	}

	Utils::turn_array_into_probability(probabilities, feature_count, sum);
	//sample features with probability proportional to e_lower[i] + e_upper[i]
	int const dimension = Utils::pick_from_distribution<func>(probabilities, feature_count);

	//Select the bound to choose the split from
	double lower_value = Utils::min(barycenter[dimension], features[dimension]);
	double upper_value = Utils::max(barycenter[dimension], features[dimension]);

	//sample the split between [lower_value, upper_value]
	double const random_value = func::rand_uniform();
	double const split_value = random_value*(upper_value - lower_value) + lower_value;

	new_parent_id = available_node();
	nodes()[new_parent_id].tau = parent_tau + (node.tau - parent_tau)*random_value;
	new_sibling_id = available_node();
	nodes()[new_sibling_id].tau = lifetime;
	node_available -= 2;

	Node &new_parent = nodes()[new_parent_id];
	Node &new_sibling = nodes()[new_sibling_id];

	new_parent.split_dimension = dimension;
	new_parent.split_value = split_value;
	//Set the box for the new children and the new_parent and the node (depending on the values in features
	//The current datapoint is passed through after readjusting counters and shape
	for(int i = 0; i < feature_count; ++i){
		new_parent.bound_lower[i] = Utils::min(node.bound_lower[i], features[i]);
		new_parent.bound_upper[i] = Utils::max(node.bound_upper[i], features[i]);
		new_sibling.bound_lower[i] = node.bound_lower[i];
		new_sibling.bound_upper[i] = node.bound_upper[i];
	}

	//Creates counters for the label of the new sibling
	for(int i = 0; i < label_count; ++i)
		new_sibling.counters[i] = 0;
	//No need to increase the counter for the current label because we will do it later, when passing down.

	//Make the connections between the new nodes
	if(!node.has_parent() && node_id == tree_bases()[tree_id].root)//We introduce a parent to the root
		tree_bases()[tree_id].root = new_parent_id;
	else{ //Otherwise, replace the existing pointer to parent
		Node& parent = nodes()[node.parent];
		if(parent.child_left == node_id)
			parent.child_left = new_parent_id;
		else
			parent.child_right = new_parent_id;
	}

	//Count sub-trees !
	int count_left = 0, count_right = 0;
	count_sides(node_id, dimension, split_value, count_left, count_right);
	bool const subtree_on_left = (count_left > count_right);
	//If more count are higher than split ---> new sibling goes left
	//set the connections
	if(subtree_on_left){
		new_parent.child_right = new_sibling_id;
		new_parent.child_left = node_id;
		node.bound_upper[dimension] = split_value;
		new_sibling.bound_lower[dimension] = split_value;
	}
	else{
		new_parent.child_right = node_id;
		new_parent.child_left = new_sibling_id;
		node.bound_lower[dimension] = split_value;
		new_sibling.bound_upper[dimension] = split_value;
	}
	new_parent.parent = node.parent;
	node.parent = new_parent_id;
	new_sibling.parent = new_parent_id;

	//Distribute Forced_extend
	if(fe_distribution == FE_DISTRIBUTION_ZERO){
		node.forced_extend = 0;
		new_sibling.forced_extend = 0;
	}
	else if(fe_distribution == FE_DISTRIBUTION_SPLIT){
		new_sibling.forced_extend = 0.5 * node.forced_extend;
		node.forced_extend *= 0.5;
	}
	else if(fe_distribution == FE_DISTRIBUTION_PROPORTIONAL){
		double const sum_count = static_cast<double>(count_left + count_right);
		double const ratio_fe = subtree_on_left ? static_cast<double>(count_left)/sum_count : static_cast<double>(count_left)/sum_count;
		new_sibling.forced_extend = (1-ratio_fe) * node.forced_extend;
		node.forced_extend *= ratio_fe;
	}
	else if(fe_distribution == FE_DISTRIBUTION_DECREMENT){
		new_sibling.forced_extend = 0;
		node.forced_extend --;
	}
	new_parent.forced_extend = 0;

	int to_remove[label_count] = {0};
	//If subtree_on_left, then discharge on right
	adjust_counters(node_id, dimension, split_value, !subtree_on_left, to_remove);
	//Place the new counters in new_sibling
	for(int i = 0; i < label_count; ++i)
		new_sibling.counters[i] += to_remove[i];
	//Adjust the boxes' bound and reshape the tree is needed
	adjust_boxes(node_id, dimension, split_value, subtree_on_left, tree_id);

	//Propage the datapoint on the right side of the new split
	if(features[dimension] > split_value){
		pass_data_point_down(new_parent.child_right, features, label);
	}
	else{
		pass_data_point_down(new_parent.child_left, features, label);
	}
	return true;
}

// Assembly:
movl 0x38(%rdi), %eax
testl %eax, %eax
je 0x466a
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %r8d, %r12d
movq %rcx, %r14
movq %rdi, %r15
movsd %xmm0, 0x8(%rsp)
movslq %esi, %rbx
shlq $0x7, %rbx
cmpl $0x2, %eax
movl %esi, 0x4(%rsp)
movl %edx, 0x1c(%rsp)
je 0x4548
cmpl $0x1, %eax
jne 0x4571
movsd 0x1820(%r15), %xmm0
xorl %eax, %eax
movsd 0x1800(%r15,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x70(%rsp,%rax,8)
incq %rax
cmpq $0x4, %rax
jne 0x4529
jmp 0x4571
movslq 0x17dc(%r15), %rax
imulq $-0x38, %rax, %rax
addq %r15, %rax
movslq %edx, %rcx
imulq $0x38, %rcx, %rcx
movl 0x17bc(%rcx,%rax), %esi
leaq 0x70(%rsp), %rdx
movq %r15, %rdi
callq 0x5388
leaq 0x44(%r15), %rcx
leaq (%r15,%rbx), %rbp
addq $0x44, %rbp
xorpd %xmm0, %xmm0
xorl %eax, %eax
movapd 0x1ad5(%rip), %xmm1     # 0x6060
movsd 0x70(%rsp,%rax,8), %xmm2
ucomisd 0x10(%rbp,%rax,8), %xmm2
jbe 0x45c3
movsd 0x30(%rbp,%rax,8), %xmm3
ucomisd %xmm2, %xmm3
jbe 0x45c3
subsd (%r14,%rax,8), %xmm2
movapd %xmm2, %xmm3
xorpd %xmm1, %xmm3
maxsd %xmm2, %xmm3
movsd %xmm3, 0x40(%rsp,%rax,8)
addsd %xmm3, %xmm0
jmp 0x45cc
movq $0x0, 0x40(%rsp,%rax,8)
incq %rax
cmpq $0x4, %rax
jne 0x458b
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x45e1
jnp 0x4658
ucomisd %xmm0, %xmm1
jbe 0x45fc
xorpd %xmm0, %xmm0
xorl %eax, %eax
addsd 0x40(%rsp,%rax,8), %xmm0
incq %rax
cmpq $0x4, %rax
jne 0x45ed
movsd 0x40(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rsp)
movl $0x1, %eax
movsd 0x40(%rsp,%rax,8), %xmm2
divsd %xmm0, %xmm2
addsd %xmm2, %xmm1
movsd %xmm1, 0x40(%rsp,%rax,8)
incq %rax
cmpq $0x4, %rax
jne 0x4611
xorl %eax, %eax
xorpd %xmm0, %xmm0
movl %r12d, 0x34(%rsp)
movsd 0x40(%rsp,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
jae 0x466d
incq %rax
cmpq $0x4, %rax
jne 0x4639
movq %rcx, %rbx
movl $0x4, %eax
jmp 0x4670
cmpneqsd %xmm1, %xmm0
movq %xmm0, %rax
andl $0x1, %eax
jmp 0x49b1
xorl %eax, %eax
retq
movq %rcx, %rbx
cmpl $0x4, %eax
movl $0x4, %ecx
cmovll %eax, %ecx
movl %ecx, 0x30(%rsp)
movslq %ecx, %rax
movq %rax, 0x20(%rsp)
movsd 0x70(%rsp,%rax,8), %xmm2
movsd (%r14,%rax,8), %xmm0
movapd %xmm2, %xmm1
minsd %xmm0, %xmm1
maxsd %xmm0, %xmm2
subsd %xmm1, %xmm2
xorpd %xmm0, %xmm0
mulsd %xmm0, %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
movq %r15, %rdi
callq 0x2600
movsd 0x60(%rbp), %xmm0
movsd 0x8(%rsp), %xmm1
subsd %xmm1, %xmm0
mulsd 0x1964(%rip), %xmm0     # 0x6038
addsd %xmm1, %xmm0
movl %eax, 0x8(%rsp)
movslq %eax, %r13
shlq $0x7, %r13
movq %rbx, %r12
addq %r13, %rbx
movsd %xmm0, 0x60(%r12,%r13)
movq %r15, %rdi
callq 0x2600
movq %r12, %rdi
movsd 0x17e8(%r15), %xmm0
movl %eax, 0x28(%rsp)
cltq
shlq $0x7, %rax
movsd %xmm0, 0x60(%r12,%rax)
addl $-0x2, 0x17d8(%r15)
addq %rax, %r12
movq 0x20(%rsp), %rax
movl %eax, (%rdi,%r13)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x8(%rdi,%r13)
xorl %eax, %eax
movsd 0x10(%rbp,%rax), %xmm0
minsd (%r14,%rax), %xmm0
movsd %xmm0, 0x10(%rbx,%rax)
movsd 0x30(%rbp,%rax), %xmm0
maxsd (%r14,%rax), %xmm0
movsd %xmm0, 0x30(%rbx,%rax)
movsd 0x10(%rbp,%rax), %xmm0
movsd %xmm0, 0x10(%r12,%rax)
movsd 0x30(%rbp,%rax), %xmm0
movsd %xmm0, 0x30(%r12,%rax)
addq $0x8, %rax
cmpq $0x20, %rax
jne 0x4738
movq $0x0, 0x68(%r12)
movslq 0x58(%rbp), %rax
cmpq $-0x1, %rax
jne 0x47c8
movslq 0x17dc(%r15), %rcx
imulq $-0x38, %rcx, %rcx
addq %r15, %rcx
movslq 0x1c(%rsp), %rdx
imulq $0x38, %rdx, %rdx
movl 0x4(%rsp), %esi
cmpl %esi, 0x17bc(%rdx,%rcx)
jne 0x47c8
leaq (%rdx,%rcx), %rax
addq $0x17bc, %rax           # imm = 0x17BC
movl 0x4(%rsp), %r13d
jmp 0x47e5
shlq $0x7, %rax
leaq (%rdi,%rax), %rcx
addq $0x50, %rcx
movl 0x4(%rsp), %r13d
cmpl %r13d, (%rcx)
leaq 0x54(%rdi,%rax), %rax
cmoveq %rcx, %rax
movl 0x8(%rsp), %ecx
movl %ecx, (%rax)
xorl %eax, %eax
leaq 0x3c(%rsp), %rcx
movl %eax, (%rcx)
leaq 0x38(%rsp), %r8
movl %eax, (%r8)
movq %r15, %rdi
movl %r13d, %esi
movq 0x20(%rsp), %rdx
movsd 0x10(%rsp), %xmm0
callq 0x49c4
leaq 0x3c(%rsp), %rax
movl (%rax), %esi
leaq 0x38(%rsp), %rax
movl (%rax), %edi
xorl %eax, %eax
xorl %ecx, %ecx
cmpl %edi, %esi
setle %al
setg %cl
movl %r13d, %edx
movl 0x28(%rsp), %r8d
cmovgl %r8d, %edx
cmovgl %r13d, %r8d
shll $0x5, %ecx
shll $0x5, %eax
movl %edx, 0x54(%rbx)
movl %r8d, 0x50(%rbx)
addq %rbp, %rcx
movq 0x20(%rsp), %rdx
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x10(%rcx,%rdx,8)
addq %r12, %rax
movsd %xmm0, 0x10(%rax,%rdx,8)
movl 0x58(%rbp), %eax
movl %eax, 0x58(%rbx)
movl 0x8(%rsp), %eax
movl %eax, 0x58(%rbp)
movl %eax, 0x58(%r12)
movl 0x14(%r15), %eax
cmpq $0x3, %rax
movl 0x30(%rsp), %edx
ja 0x4911
leaq 0x1817(%rip), %rcx      # 0x60a8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
xorl %eax, %eax
movl %eax, 0x70(%rbp)
movl %eax, 0x70(%r12)
jmp 0x4911
leal (%rdi,%rsi), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
cvtsi2sd %esi, %xmm1
divsd %xmm0, %xmm1
movsd 0x1758(%rip), %xmm0     # 0x6018
cvtsi2sdl 0x70(%rbp), %xmm2
subsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %eax
jmp 0x48f9
movl $0x0, 0x70(%r12)
decl 0x70(%rbp)
jmp 0x4911
xorps %xmm0, %xmm0
cvtsi2sdl 0x70(%rbp), %xmm0
movsd 0x173f(%rip), %xmm1     # 0x6030
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x70(%r12)
xorps %xmm0, %xmm0
cvtsi2sdl 0x70(%rbp), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x70(%rbp)
xorl %ecx, %ecx
movq %rsi, 0x8(%rsp)
movq %rdi, 0x28(%rsp)
cmpl %edi, %esi
setle %cl
movl $0x0, 0x70(%rbx)
leaq 0x68(%rsp), %r8
movq $0x0, (%r8)
movq %r15, %rdi
movl 0x4(%rsp), %ebp
movl %ebp, %esi
movl %edx, %r13d
movsd 0x10(%rsp), %xmm0
callq 0x4b90
xorl %eax, %eax
movl 0x68(%rsp,%rax,4), %ecx
addl %ecx, 0x68(%r12,%rax,4)
incq %rax
cmpq $0x1, %rax
je 0x494e
xorl %ecx, %ecx
movq 0x28(%rsp), %rax
cmpl %eax, 0x8(%rsp)
setg %cl
movq %r15, %rdi
movl %ebp, %esi
movl %r13d, %edx
movsd 0x10(%rsp), %xmm0
movl 0x1c(%rsp), %r8d
callq 0x4e60
movq 0x20(%rsp), %rax
movsd (%r14,%rax,8), %xmm0
xorl %eax, %eax
ucomisd 0x10(%rsp), %xmm0
seta %al
movl 0x50(%rbx,%rax,4), %esi
movq %r15, %rdi
movq %r14, %rdx
movl 0x34(%rsp), %ecx
callq 0x52f6
movb $0x1, %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop