// File: /workspace/github_cpp_makefile_star2_O2/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 $0xa8, %rsp
movq %rcx, 0x68(%rsp)
movq %rdx, %r14
movq %rsi, 0x48(%rsp)
movq %rdi, %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
leaq 0xa0(%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), %r15
cmpq %r15, %rbx
je 0x6f7f
movl 0xc(%r12), %ebp
movq 0x48(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movl (%rbx), %ecx
movq (%rax,%rcx,8), %rax
movl 0x28(%rax), %r13d
leaq 0x70(%rsp), %rdi
movq %rbx, %rsi
callq 0x7cb4
cmpl %r13d, %ebp
cmovbl %ebp, %r13d
andl $0x0, (%rax)
movl %r13d, 0x4(%rax)
addq $0x4, %rbx
jmp 0x6f3e
callq 0x2090
leaq 0x10(%r12), %rax
movq %rax, 0x60(%rsp)
addq $0x8, %r12
movq %r12, 0x58(%rsp)
leaq 0x8(%rsp), %rbp
leaq 0x70(%rsp), %rbx
leaq 0xc(%rsp), %r13
movq 0x30(%rsp), %rax
cmpq %rax, 0x38(%rsp)
je 0x6fb7
movq %rax, 0x38(%rsp)
callq 0x2090
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)
leaq 0x30(%rsp), %rdi
movq %rbp, %rsi
callq 0x774a
movq 0x48(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movl 0x8(%rsp), %ecx
movq (%rax,%rcx,8), %r15
addq $0x40, %r15
xorps %xmm0, %xmm0
movq (%r15), %r15
testq %r15, %r15
je 0x7045
movss 0x10(%r15), %xmm1
ucomiss %xmm0, %xmm1
jb 0x7003
movss %xmm1, 0x54(%rsp)
jbe 0x702f
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x702f
movq %rax, 0x18(%rsp)
movq 0x8(%r15), %rsi
leaq 0x10(%rsp), %rdi
callq 0x774a
movss 0x54(%rsp), %xmm0
jmp 0x7003
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %r15
movq %r15, %rcx
subq %rax, %rcx
cmpq $0x5, %rcx
jb 0x70cb
cmpq %rax, %r15
je 0x70cb
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rsi, %rax
subq %rdi, %rax
cmpq $0x5, %rax
jb 0x70c0
leaq -0x4(%r15), %r12
movq %r12, %rdx
callq 0x7ff4
movq 0x18(%rsp), %rsi
cmpq %rsi, %rax
je 0x70c0
movq 0x10(%rsp), %rdi
movq %r12, %rdx
callq 0x807b
leaq 0x4(%rax), %rsi
movq 0x18(%rsp), %rdx
cmpq %rdx, %rsi
je 0x70b7
subq %rsi, %rdx
movq %rax, %rdi
callq 0x2360
movq 0x18(%rsp), %rdx
addq $-0x4, %rdx
movq %rdx, 0x18(%rsp)
addq $-0x4, %r15
movq 0x30(%rsp), %rax
jmp 0x705b
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
je 0x713e
callq 0x2090
cltq
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
xorl %edx, %edx
divq %rsi
movl (%rcx,%rdx,4), %eax
movl %eax, 0xc(%rsp)
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq %r13, %rdx
callq 0x7ff4
movq %rax, %r12
movq (%r14), %rdi
movq 0x8(%r14), %rsi
movq %r13, %rdx
callq 0x7ff4
movq 0x38(%rsp), %r15
cmpq %r15, %r12
jne 0x7148
cmpq 0x8(%r14), %rax
je 0x7148
movl 0xc(%rsp), %eax
movl %eax, 0x8(%rsp)
xorl %eax, %eax
jmp 0x72c4
movq 0x38(%rsp), %r15
jmp 0x71da
cmpq %r15, %r12
je 0x71da
movq %r12, %r15
cmpq 0x38(%rsp), %r15
je 0x7258
movl (%r15), %eax
movl %eax, 0x8(%rsp)
movq 0x60(%rsp), %rdi
movq %rbp, %rsi
callq 0x80bc
movq %rax, %rdi
movq 0x58(%rsp), %rsi
callq 0x774a
addq $0x4, %r15
cmpq 0x38(%rsp), %r15
movq %r15, %rax
cmoveq %r12, %rax
movl (%rax), %r13d
movq 0x48(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movl 0x8(%rsp), %ecx
movq (%rax,%rcx,8), %rdi
addq $0x30, %rdi
leaq (%rax,%r13,8), %rsi
callq 0x8410
movq 0x48(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq (%rax,%r13,8), %rdi
addq $0x30, %rdi
movl 0x8(%rsp), %ecx
leaq (%rax,%rcx,8), %rsi
callq 0x8410
jmp 0x7154
movq 0x30(%rsp), %r12
cmpq %r15, %r12
je 0x72c2
movq %rbx, %rdi
movq %r12, %rsi
callq 0x7cb4
incl (%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x7cb4
movl (%rax), %r13d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x7cb4
cmpl 0x4(%rax), %r13d
jne 0x724d
movq 0x60(%rsp), %rdi
movq %r12, %rsi
callq 0x8510
testq %rax, %rax
jne 0x7233
movq 0x68(%rsp), %rdi
movq %r12, %rsi
callq 0x852e
movq (%r14), %rdi
movq 0x8(%r14), %rsi
movq %r12, %rdx
callq 0x807b
movq %r14, %rdi
movq %rax, %rsi
callq 0x7768
addq $0x4, %r12
leaq 0xc(%rsp), %r13
jmp 0x71df
movq 0x58(%rsp), %rax
incl (%rax)
leaq 0xc(%rsp), %r13
cmpq %r15, %r12
je 0x72c2
movl (%r12), %eax
movl %eax, 0x8(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x7cb4
incl (%rax)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x7cb4
movl (%rax), %r15d
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x7cb4
cmpl 0x4(%rax), %r15d
jne 0x72b7
movq (%r14), %rdi
movq 0x8(%r14), %rsi
movq %rbp, %rdx
callq 0x807b
movq %r14, %rdi
movq %rax, %rsi
callq 0x7768
addq $0x4, %r12
movq 0x38(%rsp), %r15
jmp 0x7264
movb $0x1, %al
movq 0x8(%r14), %rcx
cmpq (%r14), %rcx
je 0x72da
testb %al, %al
je 0x6fdb
jmp 0x6fa6
leaq 0x70(%rsp), %rdi
callq 0x7978
leaq 0x10(%rsp), %rdi
callq 0x7a84
leaq 0x30(%rsp), %rdi
callq 0x7a84
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7322
jmp 0x7322
jmp 0x7322
jmp 0x7322
jmp 0x7322
jmp 0x7322
jmp 0x7322
jmp 0x7322
jmp 0x7322
jmp 0x7322
jmp 0x7322
jmp 0x7322
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x7978
leaq 0x10(%rsp), %rdi
callq 0x7a84
leaq 0x30(%rsp), %rdi
callq 0x7a84
movq %rbx, %rdi
callq 0x23a0
nop