// File: /workspace/github_cpp_makefile_star2_O3/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, 0x60(%rsp)
movq %rdx, %r14
movq %rsi, 0x58(%rsp)
movq %rdi, %r12
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 0x7605
movl 0xc(%r12), %ebp
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movl (%rbx), %ecx
movq (%rax,%rcx,8), %rax
movl 0x28(%rax), %r15d
leaq 0x80(%rsp), %rdi
movq %rbx, %rsi
callq 0x83e4
cmpl %r15d, %ebp
cmovbl %ebp, %r15d
movl $0x0, (%rax)
movl %r15d, 0x4(%rax)
addq $0x4, %rbx
jmp 0x75be
callq 0x20a0
leaq 0x10(%r12), %rax
movq %rax, 0x70(%rsp)
addq $0x8, %r12
movq %r12, 0x68(%rsp)
movb $0x1, %al
movl %eax, 0xc(%rsp)
leaq 0x8(%rsp), %r15
leaq 0x80(%rsp), %rbx
movq 0x60(%rsp), %r13
movq 0x18(%rsp), %rsi
testb $0x1, 0xc(%rsp)
je 0x7679
movq 0x10(%rsp), %rax
cmpq %rax, %rsi
je 0x7650
movq %rax, 0x18(%rsp)
callq 0x20a0
cltq
movq (%r14), %rcx
movq 0x8(%r14), %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
xorl %edx, %edx
divq %rsi
movslq %edx, %rax
movl (%rcx,%rax,4), %eax
movl %eax, 0x8(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x7691
movl 0x8(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x76a2
leaq 0x10(%rsp), %rdi
movq %r15, %rdx
callq 0x8310
movl 0x8(%rsp), %eax
movq 0x58(%rsp), %rcx
movq (%rcx), %rcx
movq 0x8(%rcx), %rcx
movl %eax, %eax
movq (%rcx,%rax,8), %rax
movq 0x40(%rax), %r12
testq %r12, %r12
je 0x7722
xorps %xmm0, %xmm0
movss 0x10(%r12), %xmm1
ucomiss %xmm0, %xmm1
jb 0x7719
movq 0x38(%rsp), %rsi
jbe 0x76e5
movq 0x30(%rsp), %rax
cmpq %rax, %rsi
je 0x76e5
movq %rax, 0x38(%rsp)
movq %rax, %rsi
movq 0x8(%r12), %rdx
cmpq 0x40(%rsp), %rsi
je 0x7703
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
movaps %xmm1, %xmm0
jmp 0x7719
leaq 0x30(%rsp), %rdi
movss %xmm1, 0xc(%rsp)
callq 0x8310
movss 0xc(%rsp), %xmm0
movq (%r12), %r12
testq %r12, %r12
jne 0x76c0
movq 0x18(%rsp), %rbp
movq %rbp, %rax
subq 0x10(%rsp), %rax
sete %cl
cmpq $0x5, %rax
setb %al
orb %cl, %al
movq 0x38(%rsp), %rdx
jne 0x77ae
movq 0x30(%rsp), %rdi
movq %rdx, %rax
subq %rdi, %rax
cmpq $0x5, %rax
jb 0x77a3
leaq -0x4(%rbp), %r12
movq %rdx, %rsi
movq %r12, %rdx
callq 0x8662
movq 0x38(%rsp), %rdx
cmpq %rdx, %rax
je 0x77a3
movq 0x30(%rsp), %rdi
movq %rdx, %rsi
movq %r12, %rdx
callq 0x8707
leaq 0x4(%rax), %rsi
movq 0x38(%rsp), %rdx
cmpq %rdx, %rsi
je 0x779a
subq %rsi, %rdx
movq %rax, %rdi
callq 0x2390
movq 0x38(%rsp), %rdx
addq $-0x4, %rdx
movq %rdx, 0x38(%rsp)
addq $-0x4, %rbp
cmpq 0x10(%rsp), %rbp
jne 0x7742
cmpq 0x30(%rsp), %rdx
je 0x7827
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, 0x54(%rsp)
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
leaq 0x54(%rsp), %rbp
movq %rbp, %rdx
callq 0x8662
movq %rax, %r12
movq (%r14), %rdi
movq 0x8(%r14), %rsi
movq %rbp, %rdx
callq 0x8662
movq 0x18(%rsp), %rbp
cmpq %rbp, %r12
jne 0x7831
cmpq 0x8(%r14), %rax
je 0x7831
movl 0x54(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
jmp 0x7a2c
movq 0x18(%rsp), %rbp
jmp 0x78ec
cmpq %rbp, %r12
je 0x78ec
leaq 0x4(%r12), %r13
xorl %ebp, %ebp
leaq (%r12,%rbp), %rax
cmpq 0x18(%rsp), %rax
je 0x7999
movl (%rax), %eax
movl %eax, 0x8(%rsp)
movq 0x70(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x874c
movq 0x8(%rax), %rsi
cmpq 0x10(%rax), %rsi
je 0x7882
movq 0x68(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rax)
jmp 0x788f
movq %rax, %rdi
movq 0x68(%rsp), %rdx
callq 0x8310
movq %r13, %rax
addq %rbp, %rax
cmpq 0x18(%rsp), %rax
cmoveq %r12, %rax
movl (%rax), %r15d
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movl 0x8(%rsp), %ecx
movq (%rax,%rcx,8), %rdi
addq $0x30, %rdi
leaq (%rax,%r15,8), %rsi
callq 0x8a16
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq (%rax,%r15,8), %rdi
addq $0x30, %rdi
movl 0x8(%rsp), %ecx
leaq (%rax,%rcx,8), %rsi
addq $0x4, %rbp
callq 0x8a16
jmp 0x7841
movq 0x10(%rsp), %r12
movb $0x1, %al
movl %eax, 0xc(%rsp)
cmpq %rbp, %r12
je 0x7a2c
movq %rbx, %rdi
movq %r12, %rsi
callq 0x83e4
incl (%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x83e4
movl (%rax), %r15d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x83e4
cmpl 0x4(%rax), %r15d
leaq 0x8(%rsp), %r15
jne 0x7990
movq 0x70(%rsp), %rdi
movq %r12, %rsi
callq 0x8b00
testq %rax, %rax
jne 0x7958
movq %r13, 0x78(%rsp)
movq %r13, %rdi
movq %r12, %rsi
leaq 0x78(%rsp), %rdx
callq 0x8b5c
movq (%r14), %rdi
movq 0x8(%r14), %rsi
movq %r12, %rdx
callq 0x8707
leaq 0x4(%rax), %rsi
movq 0x8(%r14), %rdx
cmpq %rdx, %rsi
je 0x7983
subq %rsi, %rdx
movq %rax, %rdi
callq 0x2390
movq 0x8(%r14), %rdx
leaq 0x8(%rsp), %r15
addq $-0x4, %rdx
movq %rdx, 0x8(%r14)
addq $0x4, %r12
jmp 0x78f7
movq 0x68(%rsp), %rax
incl (%rax)
movb $0x1, %al
movl %eax, 0xc(%rsp)
testq %rbp, %rbp
leaq 0x8(%rsp), %r15
je 0x7a27
movq 0x60(%rsp), %r13
movl (%r12), %eax
movl %eax, 0x8(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x83e4
incl (%rax)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x83e4
movl (%rax), %ebp
movq %rbx, %rdi
movq %r15, %rsi
callq 0x83e4
cmpl 0x4(%rax), %ebp
jne 0x7a1a
movq (%r14), %rdi
movq 0x8(%r14), %rsi
movq %r15, %rdx
callq 0x8707
leaq 0x4(%rax), %rsi
movq 0x8(%r14), %rdx
cmpq %rdx, %rsi
je 0x7a12
subq %rsi, %rdx
movq %rax, %rdi
callq 0x2390
movq 0x8(%r14), %rdx
addq $-0x4, %rdx
movq %rdx, 0x8(%r14)
addq $0x4, %r12
cmpq 0x18(%rsp), %r12
jne 0x79b5
jmp 0x7a2c
movq 0x60(%rsp), %r13
movq 0x8(%r14), %rax
cmpq (%r14), %rax
jne 0x7635
leaq 0x80(%rsp), %rdi
callq 0x8118
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x7a55
callq 0x2220
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x7a64
callq 0x2220
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7a8e
jmp 0x7a8e
jmp 0x7a8e
jmp 0x7a8e
jmp 0x7a8e
jmp 0x7a8e
jmp 0x7a8e
jmp 0x7a8e
jmp 0x7a8e
jmp 0x7a8e
jmp 0x7a8e
jmp 0x7a8e
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x8118
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x7aad
callq 0x2220
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x7abc
callq 0x2220
movq %rbx, %rdi
callq 0x23d0