// File: /workspace/github_cpp_makefile_star2_O1/rtackx[P]ComSim/community.cpp
// Function: Community::cycles(Graph*&, std::vector<unsigned int, std::allocator<unsigned int>>&, std::unordered_set<unsigned int, std::hash<unsigned int>, std::equal_to<unsigned int>, std::allocator<unsigned int>>&)
void Community::cycles(Graph*& graph_projection, vector<unsigned int>& list_index_nodes, unordered_set<unsigned int>& list_remaining_nodes)
{
	vector<unsigned int> list_visited_nodes;
	unsigned int current, neighbor, most_similar, max_depth;
	vector<unsigned int>::iterator it_list_visited, it_list_index, it_current;
	vector<unsigned int>::reverse_iterator rit_current;
	float max_similarity;
	vector<unsigned int> list_similarity;
	
	unordered_map<unsigned int, pair<unsigned int, unsigned int>> map_cycle_nodes;
	for(auto& index : list_index_nodes)
	{
		max_depth = depth_best;
		if(depth_best > graph_projection->list_nodes[index]->nb_neighbors)
			max_depth = graph_projection->list_nodes[index]->nb_neighbors;

		map_cycle_nodes[index] = make_pair(0, max_depth);
	}

	int i = rand() % list_index_nodes.size();
	bool new_node = true;
	do 
	{
		if(new_node)
		{
			list_visited_nodes.clear();
			i = rand() % list_index_nodes.size();
			current = list_index_nodes[i];
			new_node = false;
		}

		list_visited_nodes.push_back(current);

		max_similarity = 0.0;
		for(auto& neighbor : graph_projection->list_nodes[current]->neighbor_weights)
		{
			if(neighbor.second >= max_similarity)
			{
				if(neighbor.second > max_similarity)
					list_similarity.clear();

				max_similarity = neighbor.second;
				list_similarity.push_back(neighbor.first->index);
			}
		}
		if(list_visited_nodes.size() > 1)
		{
			rit_current = list_visited_nodes.rbegin();
			while(rit_current != list_visited_nodes.rend())
			{
				if(list_similarity.size() > 1 && find(list_similarity.begin(), list_similarity.end(), *rit_current) != list_similarity.end())
					list_similarity.erase(remove(list_similarity.begin(), list_similarity.end(), *rit_current));

				++rit_current;
			}
		}

		if(list_similarity.size() > 0)
		{
			most_similar = list_similarity[rand() % list_similarity.size()];

			it_list_visited = find(list_visited_nodes.begin(), list_visited_nodes.end(), most_similar);
			it_list_index = find(list_index_nodes.begin(), list_index_nodes.end(), most_similar);		
		}
		else
		{
			it_list_visited = list_visited_nodes.end();
			it_list_index = list_index_nodes.end();
		}

		
		if(it_list_visited == list_visited_nodes.end() && it_list_index != list_index_nodes.end())
			current = most_similar;
		else
		{
			// Already visited, cycle detected 
			if(it_list_visited != list_visited_nodes.end())
			{
				it_current = it_list_visited;
				while(it_current != list_visited_nodes.end())
				{
					current = *it_current;
					map_community[current].push_back(index_community);
										
					advance(it_current, 1);
					if(it_current != list_visited_nodes.end())
						neighbor = *it_current;
					else
						neighbor = *it_list_visited;

					graph_projection->list_nodes[current]->neighbor_weights.erase(graph_projection->list_nodes[neighbor]);
					graph_projection->list_nodes[neighbor]->neighbor_weights.erase(graph_projection->list_nodes[current]);
				}

				index_community++;

				it_current = it_list_visited;
				while(it_current != list_visited_nodes.end())
				{
					current = *it_current;
					map_cycle_nodes[current].first++;
					if(map_cycle_nodes[current].first == map_cycle_nodes[current].second)
							list_index_nodes.erase(remove(list_index_nodes.begin(), list_index_nodes.end(), current));
					
					++it_current;
				}
			}
			else
			{
				for(auto& current : list_visited_nodes)
				{					
					map_cycle_nodes[current].first++;
					if(map_cycle_nodes[current].first == map_cycle_nodes[current].second)
					{
						if(map_community.find(current) == map_community.end())
							list_remaining_nodes.insert(current);

						list_index_nodes.erase(remove(list_index_nodes.begin(), list_index_nodes.end(), current));
					}
				}
			}
			
			new_node = true;
		}

	} while(list_index_nodes.size() > 0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rcx, 0x70(%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, 0x50(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
xorl %eax, %eax
movq %rax, 0x20(%rsp)
movq %rax, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
leaq 0xb0(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
movq (%rdx), %rbx
movq 0x8(%rdx), %r13
cmpq %r13, %rbx
je 0x770f
movq 0x50(%rsp), %rax
movl 0xc(%rax), %ebp
movq (%r15), %rax
movq 0x8(%rax), %rax
movl (%rbx), %ecx
movq (%rax,%rcx,8), %rax
movl 0x28(%rax), %r12d
leaq 0x80(%rsp), %rdi
movq %rbx, %rsi
callq 0x856c
cmpl %r12d, %ebp
cmovbl %ebp, %r12d
movl $0x0, (%rax)
movl %r12d, 0x4(%rax)
addq $0x4, %rbx
jmp 0x76ca
callq 0x20a0
movq 0x50(%rsp), %rax
leaq 0x10(%rax), %rcx
movq %rcx, 0x68(%rsp)
addq $0x8, %rax
movq %rax, 0x60(%rsp)
movb $0x1, %al
movq %rax, 0x48(%rsp)
leaq 0x8(%rsp), %rdx
leaq 0xc(%rsp), %rbx
testb $0x1, 0x48(%rsp)
je 0x7786
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x7754
movq %rax, 0x18(%rsp)
callq 0x20a0
cltq
movq (%r14), %rcx
movq 0x8(%r14), %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
movq $0x0, 0x48(%rsp)
xorl %edx, %edx
divq %rsi
movslq %edx, %rax
movl (%rcx,%rax,4), %eax
movl %eax, 0x8(%rsp)
leaq 0x8(%rsp), %rdx
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x77a3
movl 0x8(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x77ad
leaq 0x10(%rsp), %rdi
callq 0x8498
movq (%r15), %rax
movq 0x8(%rax), %rax
movl 0x8(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x40(%rax), %r13
testq %r13, %r13
je 0x7827
xorps %xmm0, %xmm0
movss 0x10(%r13), %xmm1
ucomiss %xmm0, %xmm1
jb 0x781e
jbe 0x77e6
movq 0x30(%rsp), %rax
cmpq %rax, 0x38(%rsp)
je 0x77e6
movq %rax, 0x38(%rsp)
movq 0x8(%r13), %rdx
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x7808
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
movaps %xmm1, %xmm0
jmp 0x781e
leaq 0x30(%rsp), %rdi
movss %xmm1, 0x5c(%rsp)
callq 0x8498
movss 0x5c(%rsp), %xmm0
movq (%r13), %r13
testq %r13, %r13
jne 0x77c8
movq 0x18(%rsp), %r13
movq %r13, %rax
subq 0x10(%rsp), %rax
sete %cl
cmpq $0x5, %rax
setb %al
orb %cl, %al
jne 0x78a5
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq %rsi, %rax
subq %rdi, %rax
cmpq $0x5, %rax
jb 0x789a
leaq -0x4(%r13), %rbp
movq %rbp, %rdx
callq 0x8820
movq 0x38(%rsp), %rsi
cmpq %rsi, %rax
je 0x789a
movq 0x30(%rsp), %rdi
movq %rbp, %rdx
callq 0x88b9
leaq 0x4(%rax), %rsi
movq 0x38(%rsp), %rdx
cmpq %rdx, %rsi
je 0x7894
subq %rsi, %rdx
movq %rax, %rdi
callq 0x2390
addq $-0x4, 0x38(%rsp)
addq $-0x4, %r13
cmpq 0x10(%rsp), %r13
jne 0x7842
movq 0x38(%rsp), %rax
cmpq 0x30(%rsp), %rax
je 0x78fb
callq 0x20a0
cltq
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
xorl %edx, %edx
divq %rsi
movl (%rcx,%rdx,4), %eax
movl %eax, 0xc(%rsp)
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rbx, %rdx
callq 0x8820
movq %rax, %rbp
movq (%r14), %rdi
movq 0x8(%r14), %rsi
movq %rbx, %rdx
callq 0x8820
jmp 0x7904
movq 0x18(%rsp), %rbp
movq 0x8(%r14), %rax
movq 0x18(%rsp), %r13
cmpq %r13, %rbp
jne 0x7921
cmpq 0x8(%r14), %rax
je 0x7921
movl 0xc(%rsp), %eax
movl %eax, 0x8(%rsp)
jmp 0x7b36
cmpq %r13, %rbp
je 0x7a66
movq %rbp, %r13
cmpq 0x18(%rsp), %r13
je 0x79c9
movl (%r13), %eax
movl %eax, 0x8(%rsp)
movq 0x68(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x88fe
movq 0x8(%rax), %rsi
cmpq 0x10(%rax), %rsi
je 0x796c
movq 0x60(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rax)
jmp 0x7979
movq %rax, %rdi
movq 0x60(%rsp), %rdx
callq 0x8498
addq $0x4, %r13
cmpq 0x18(%rsp), %r13
movq %r13, %rax
cmoveq %rbp, %rax
movl (%rax), %r12d
movq (%r15), %rax
movq 0x8(%rax), %rax
movl 0x8(%rsp), %ecx
movq (%rax,%rcx,8), %rdi
addq $0x30, %rdi
leaq (%rax,%r12,8), %rsi
callq 0x8bfa
movq (%r15), %rax
movq 0x8(%rax), %rax
movq (%rax,%r12,8), %rdi
addq $0x30, %rdi
movl 0x8(%rsp), %ecx
leaq (%rax,%rcx,8), %rsi
callq 0x8bfa
jmp 0x792d
movq 0x50(%rsp), %rax
incl 0x8(%rax)
movb $0x1, %al
movq %rax, 0x48(%rsp)
cmpq 0x18(%rsp), %rbp
je 0x7b31
leaq 0x80(%rsp), %r13
movl (%rbp), %eax
movl %eax, 0x8(%rsp)
movq %r13, %rdi
leaq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x856c
incl (%rax)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x856c
movl (%rax), %r12d
movq %r13, %rdi
movq %rbx, %rsi
callq 0x856c
cmpl 0x4(%rax), %r12d
leaq 0xc(%rsp), %rbx
jne 0x7a56
movq (%r14), %rdi
movq 0x8(%r14), %rsi
leaq 0x8(%rsp), %rdx
callq 0x88b9
leaq 0x4(%rax), %rsi
movq 0x8(%r14), %rdx
cmpq %rdx, %rsi
je 0x7a51
subq %rsi, %rdx
movq %rax, %rdi
callq 0x2390
addq $-0x4, 0x8(%r14)
addq $0x4, %rbp
cmpq 0x18(%rsp), %rbp
jne 0x79eb
jmp 0x7b36
movq 0x10(%rsp), %rbp
movb $0x1, %al
movq %rax, 0x48(%rsp)
cmpq %r13, %rbp
je 0x7b36
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x856c
incl (%rax)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x856c
movl (%rax), %r12d
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x856c
cmpl 0x4(%rax), %r12d
leaq 0xc(%rsp), %rbx
jne 0x7b28
movl (%rbp), %ecx
movq %rcx, %rax
xorl %edx, %edx
movq 0x50(%rsp), %rsi
divq 0x18(%rsi)
movq 0x68(%rsp), %rdi
movq %rdx, %rsi
movq %rbp, %rdx
callq 0x8a56
testq %rax, %rax
je 0x7ae0
cmpq $0x0, (%rax)
jne 0x7af7
movq 0x70(%rsp), %rdi
movq %rdi, 0x78(%rsp)
movq %rbp, %rsi
leaq 0x78(%rsp), %rdx
callq 0x8cdc
movq (%r14), %rdi
movq 0x8(%r14), %rsi
movq %rbp, %rdx
callq 0x88b9
leaq 0x4(%rax), %rsi
movq 0x8(%r14), %rdx
cmpq %rdx, %rsi
leaq 0xc(%rsp), %rbx
je 0x7b23
subq %rsi, %rdx
movq %rax, %rdi
callq 0x2390
addq $-0x4, 0x8(%r14)
addq $0x4, %rbp
jmp 0x7a72
leaq 0xc(%rsp), %rbx
movq 0x8(%r14), %rax
cmpq (%r14), %rax
leaq 0x8(%rsp), %rdx
jne 0x773c
leaq 0x80(%rsp), %rdi
callq 0x82a0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x7b64
callq 0x2220
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x7b73
callq 0x2220
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7b9d
jmp 0x7b9d
jmp 0x7b9d
jmp 0x7b9d
jmp 0x7b9d
jmp 0x7b9d
jmp 0x7b9d
jmp 0x7b9d
jmp 0x7b9d
jmp 0x7b9d
jmp 0x7b9d
jmp 0x7b9d
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x82a0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x7bbc
callq 0x2220
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x7bcb
callq 0x2220
movq %rbx, %rdi
callq 0x23d0
nop