// File: /workspace/github_cpp_makefile_star2_O2/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 0x43a6
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movl %r8d, %ebx
movq %rcx, %r14
movl %edx, %r12d
movq %rdi, %r15
movsd %xmm0, 0x20(%rsp)
movslq %esi, %rcx
shlq $0x7, %rcx
leaq (%rdi,%rcx), %r13
addq $0x44, %r13
movslq %edx, %rbp
cmpl $0x2, %eax
movl %esi, 0xc(%rsp)
je 0x408b
cmpl $0x1, %eax
jne 0x40b4
movsd 0x1820(%r15), %xmm0
xorl %eax, %eax
cmpq $0x4, %rax
je 0x40b4
movsd 0x1800(%r15,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x90(%rsp,%rax,8)
incq %rax
jmp 0x4069
movslq 0x17dc(%r15), %rax
imulq $-0x38, %rax, %rax
addq %r15, %rax
imulq $0x38, %rbp, %rcx
movl 0x17bc(%rcx,%rax), %esi
leaq 0x90(%rsp), %rdx
movq %r15, %rdi
callq 0x4cb8
movq %rbp, 0x50(%rsp)
leaq 0x44(%r15), %rdx
leaq 0x10(%r13), %rcx
leaq 0x30(%r13), %rbp
xorpd %xmm0, %xmm0
xorl %eax, %eax
movapd 0x1f7d(%rip), %xmm1     # 0x6050
cmpq $0x4, %rax
je 0x411f
movsd 0x90(%rsp,%rax,8), %xmm2
ucomisd -0x20(%rbp,%rax,8), %xmm2
jbe 0x4114
movsd (%rbp,%rax,8), %xmm3
ucomisd %xmm2, %xmm3
jbe 0x4114
subsd (%r14,%rax,8), %xmm2
movapd %xmm2, %xmm3
xorpd %xmm1, %xmm3
maxsd %xmm2, %xmm3
movsd %xmm3, 0x70(%rsp,%rax,8)
addsd %xmm3, %xmm0
jmp 0x411a
andq $0x0, 0x70(%rsp,%rax,8)
incq %rax
jmp 0x40d3
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x412f
jnp 0x439f
movq %rcx, 0x60(%rsp)
movl %r12d, 0x40(%rsp)
movl %ebx, 0x44(%rsp)
leaq 0x70(%rsp), %rdi
pushq $0x4
popq %r12
movl %r12d, %esi
movq %rdx, %rbx
callq 0x3994
leaq 0x70(%rsp), %rdi
movl %r12d, %esi
callq 0x39ee
movl %eax, 0x3c(%rsp)
cltq
movq %rax, 0x30(%rsp)
movsd 0x90(%rsp,%rax,8), %xmm2
movsd (%r14,%rax,8), %xmm0
movapd %xmm2, %xmm1
minsd %xmm0, %xmm1
maxsd %xmm0, %xmm2
subsd %xmm1, %xmm2
mulsd 0x1e90(%rip), %xmm2     # 0x6020
addsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
movq %r15, %rdi
callq 0x2378
movq %r13, 0x28(%rsp)
movsd 0x60(%r13), %xmm0
movsd 0x20(%rsp), %xmm1
subsd %xmm1, %xmm0
mulsd 0x1e61(%rip), %xmm0     # 0x6020
addsd %xmm1, %xmm0
movl %eax, 0x18(%rsp)
movslq %eax, %r13
shlq $0x7, %r13
leaq (%rbx,%r13), %r12
movsd %xmm0, 0x60(%rbx,%r13)
movq %r15, %rdi
callq 0x2378
movq %rbx, %r8
movl 0xc(%rsp), %esi
movsd 0x17e8(%r15), %xmm0
movl %eax, 0x38(%rsp)
cltq
shlq $0x7, %rax
addq %rax, %rbx
leaq 0x10(%r8,%rax), %rcx
movq %rcx, 0x58(%rsp)
movsd %xmm0, 0x50(%rcx)
movq %r15, 0x20(%rsp)
addl $-0x2, 0x17d8(%r15)
movq 0x30(%rsp), %rcx
movl %ecx, (%r8,%r13)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x8(%r8,%r13)
leaq (%r8,%rax), %r13
addq $0x30, %r13
xorl %eax, %eax
cmpq $0x20, %rax
je 0x4286
movsd -0x20(%rbp,%rax), %xmm0
minsd (%r14,%rax), %xmm0
movsd %xmm0, 0x10(%r12,%rax)
movsd (%rbp,%rax), %xmm0
maxsd (%r14,%rax), %xmm0
movsd %xmm0, 0x30(%r12,%rax)
movsd -0x20(%rbp,%rax), %xmm0
movsd %xmm0, -0x20(%r13,%rax)
movsd (%rbp,%rax), %xmm0
movsd %xmm0, (%r13,%rax)
addq $0x8, %rax
jmp 0x423a
andq $0x0, 0x68(%rbx)
movq 0x28(%rsp), %rax
movslq 0x58(%rax), %rax
cmpq $-0x1, %rax
movq 0x20(%rsp), %rdi
jne 0x42c8
movslq 0x17dc(%rdi), %rcx
imulq $-0x38, %rcx, %rcx
addq %rdi, %rcx
imulq $0x38, 0x50(%rsp), %rdx
cmpl %esi, 0x17bc(%rdx,%rcx)
jne 0x42c8
leaq (%rdx,%rcx), %rax
addq $0x17bc, %rax           # imm = 0x17BC
jmp 0x42df
shlq $0x7, %rax
leaq (%r8,%rax), %rcx
addq $0x50, %rcx
cmpl %esi, (%rcx)
leaq 0x54(%r8,%rax), %rax
cmoveq %rcx, %rax
movl 0x18(%rsp), %ecx
movl %ecx, (%rax)
leaq 0x4c(%rsp), %rcx
andl $0x0, (%rcx)
leaq 0x48(%rsp), %r8
andl $0x0, (%r8)
movl 0xc(%rsp), %esi
movq 0x30(%rsp), %r15
movl %r15d, %edx
movsd 0x10(%rsp), %xmm0
callq 0x44c2
movl 0xc(%rsp), %edx
leaq 0x4c(%rsp), %rax
movl (%rax), %edi
leaq 0x48(%rsp), %rax
movl (%rax), %r8d
cmpl %r8d, %edi
movq 0x60(%rsp), %rcx
cmovgq %rbp, %rcx
cmovgq 0x58(%rsp), %r13
movl %edx, %eax
movl 0x38(%rsp), %esi
cmovgl %esi, %eax
cmovgl %edx, %esi
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rcx,%r15,8)
movsd %xmm0, (%r13,%r15,8)
movq 0x20(%rsp), %r15
movl %eax, 0x54(%r12)
movl %esi, 0x50(%r12)
movq 0x28(%rsp), %rsi
movl 0x58(%rsi), %eax
movl %eax, 0x58(%r12)
movl 0x18(%rsp), %eax
movl %eax, 0x58(%rsi)
movl %eax, 0x58(%rbx)
movl 0x14(%r15), %eax
cmpq $0x3, %rax
ja 0x4405
leaq 0x1d1c(%rip), %rcx      # 0x60a8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
andl $0x0, 0x70(%rsi)
andl $0x0, 0x70(%rbx)
jmp 0x4405
xorl %eax, %eax
jmp 0x44af
xorl %eax, %eax
retq
leal (%r8,%rdi), %eax
cvtsi2sd %eax, %xmm0
cvtsi2sd %edi, %xmm1
divsd %xmm0, %xmm1
movsd 0x1c57(%rip), %xmm0     # 0x6018
cvtsi2sdl 0x70(%rsi), %xmm2
subsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %eax
jmp 0x43f2
andl $0x0, 0x70(%rbx)
decl 0x70(%rsi)
jmp 0x4405
cvtsi2sdl 0x70(%rsi), %xmm0
movsd 0x1c4e(%rip), %xmm1     # 0x6038
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x70(%rbx)
cvtsi2sdl 0x70(%rsi), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x70(%rsi)
leaq 0x54(%r12), %rax
movq %rax, 0x18(%rsp)
leaq 0x50(%r12), %rbp
xorl %ecx, %ecx
movq %rdi, 0x28(%rsp)
movq %r8, %r13
cmpl %r8d, %edi
setle %cl
andl $0x0, 0x70(%r12)
leaq 0x68(%rsp), %r8
andq $0x0, (%r8)
movq %r15, %rdi
movl %edx, %esi
movl 0x3c(%rsp), %r12d
movl %r12d, %edx
movsd 0x10(%rsp), %xmm0
callq 0x464c
xorl %eax, %eax
movl 0x40(%rsp), %r8d
cmpq $0x2, %rax
je 0x4465
movl 0x68(%rsp,%rax,4), %ecx
addl %ecx, 0x68(%rbx,%rax,4)
incq %rax
jmp 0x4452
xorl %ecx, %ecx
cmpl %r13d, 0x28(%rsp)
setg %cl
movq %r15, %rdi
movl 0xc(%rsp), %esi
movl %r12d, %edx
movsd 0x10(%rsp), %xmm0
callq 0x48ac
movq 0x30(%rsp), %rax
movsd (%r14,%rax,8), %xmm0
ucomisd 0x10(%rsp), %xmm0
cmovaq 0x18(%rsp), %rbp
movl (%rbp), %esi
movq %r15, %rdi
movq %r14, %rdx
movl 0x44(%rsp), %ecx
callq 0x4c36
movb $0x1, %al
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop