// File: Luthaf[P]zeopp/feature.cc
// Function: FEATURE::merge_singlenode_segments()
void FEATURE::merge_singlenode_segments()
{
	int i,j,k;
	int mink=0;
	int count;

	int nsingle=0;
	double min=0;
	vector <int> nodelist;  // list of nodes in current segment
	vector <int> tobemerged; // flag for nodes to be merged with other segments
	vector <int> tobeextended; // flag for nodes that will be extended by toher nodes
	vector <int> listofsinglenodesegments;
	vector <int> tobemerged_segments;

	tobemerged.resize(nodes.size(),-1);
	tobeextended.resize(nodes.size(),0);
	tobemerged_segments.resize(nsegments,0);

	for(i=0;i<nsegments;i++)
	{
		nodelist.clear();
		for(j=0;j<(int)nodes.size();j++)
		{
			if(segments[j]==i) nodelist.push_back(j);
		};

		if(nodelist.size()==1)
		{


			if(tobeextended[nodelist[0]]==0)  // if the current node is not planned to be extended with other node, merge with others
			{
				tobemerged_segments[i]=1; // mark current segment as one to e merged

				for(k=0;k<(int)nodes.at(nodelist[0]).connections.size();k++) // iterate over all connections to find closes node
				{
					if(k==0) {min=nodes.at(nodelist[0]).connections.at(k).length;mink=nodes.at(nodelist[0]).connections.at(k).to;};
					if(k>0&&nodes.at(nodelist[0]).connections.at(k).length<min)
					{
						mink=nodes.at(nodelist[0]).connections.at(k).to;
						min=nodes.at(nodelist[0]).connections.at(k).length;
					};
				};
	// shortest connection leads to mink

				if(tobemerged[mink]>-1) mink=tobemerged[mink]; // if node to which we are going to merge the current node
													// also will be merged, merge into the other node

				tobemerged[nodelist[0]]=mink; // curr node to be connected to mink
				tobeextended[mink]=1;
				tobeextended[nodelist[0]]=1; // "write protect" current node

				nsingle++;
			};
		};

	};

/* now merging */

	nfeatures=nsegments-nsingle;

	feature_node_mapping.resize(nodes.size(),-1);

	count=0;
	for(i=0;i<nsegments;i++)
	{
		if(tobemerged_segments[i]==0)
		{
			features.push_back(count);
			feature_max_r.push_back(segment_max_r[i]);

			for(j=0;j<(int)nodes.size();j++)
			{
				if(segments[j]==i) feature_node_mapping[j]=count;
			};

			count++;
		};
	};
	count=0;
	for(i=0;i<(int)nodes.size();i++)
	{
		if(tobemerged[i]>-1)
		{
			feature_node_mapping[i]=feature_node_mapping[tobemerged[i]];
			count++;
		};
	};


	for(i=0;i<(int)nodes.size();i++)
	{
		if(feature_node_mapping[i]==-1) 
		{
			cout << "Feature construction failed. Abort."<< "\n";abort();
		}
	}

	cout << "After merging single segments, nfeatures = " << nfeatures << "\n";


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xd0(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
leaq -0x40(%rbp), %rdi
callq 0xd0d0
leaq -0x58(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
callq 0xd0d0
leaq -0x70(%rbp), %rdi
callq 0xd0d0
leaq -0x88(%rbp), %rdi
callq 0xd0d0
leaq -0xa0(%rbp), %rdi
callq 0xd0d0
movq -0xd0(%rbp), %rdi
addq $0x60, %rdi
callq 0x10100
movq -0xc8(%rbp), %rdi
movq %rax, %rsi
movl $0xffffffff, -0xa4(%rbp) # imm = 0xFFFFFFFF
leaq -0xa4(%rbp), %rdx
callq 0x84b00
jmp 0xdb79c
movq -0xd0(%rbp), %rdi
addq $0x60, %rdi
callq 0x10100
movq %rax, %rsi
movl $0x0, -0xb8(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0xb8(%rbp), %rdx
callq 0x84b00
jmp 0xdb7cb
movq -0xd0(%rbp), %rax
movslq 0x138(%rax), %rsi
movl $0x0, -0xbc(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xbc(%rbp), %rdx
callq 0x84b00
jmp 0xdb7f8
movl $0x0, -0xc(%rbp)
movq -0xd0(%rbp), %rcx
movl -0xc(%rbp), %eax
cmpl 0x138(%rcx), %eax
jge 0xdbc45
leaq -0x40(%rbp), %rdi
callq 0x163b0
movl $0x0, -0x10(%rbp)
movq -0xd0(%rbp), %rdi
movl -0x10(%rbp), %eax
movl %eax, -0xd4(%rbp)
addq $0x60, %rdi
callq 0x10100
movq %rax, %rcx
movl -0xd4(%rbp), %eax
cmpl %ecx, %eax
jge 0xdb8d8
movq -0xd0(%rbp), %rdi
addq $0x140, %rdi            # imm = 0x140
movslq -0x10(%rbp), %rsi
callq 0xed60
movl (%rax), %eax
cmpl -0xc(%rbp), %eax
jne 0xdb8c8
leaq -0x40(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0xd1b0
jmp 0xdb87c
jmp 0xdb8c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xec80
leaq -0x88(%rbp), %rdi
callq 0xec80
leaq -0x70(%rbp), %rdi
callq 0xec80
leaq -0x58(%rbp), %rdi
callq 0xec80
leaq -0x40(%rbp), %rdi
callq 0xec80
jmp 0xdbf97
jmp 0xdb8ca
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xdb825
leaq -0x40(%rbp), %rdi
callq 0x10090
cmpq $0x1, %rax
jne 0xdbc35
leaq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xed60
movslq (%rax), %rsi
leaq -0x70(%rbp), %rdi
callq 0xed60
cmpl $0x0, (%rax)
jne 0xdbc33
movslq -0xc(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0xed60
movl $0x1, (%rax)
movl $0x0, -0x14(%rbp)
movq -0xd0(%rbp), %rax
movl -0x14(%rbp), %ecx
movl %ecx, -0xec(%rbp)
addq $0x60, %rax
movq %rax, -0xe8(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0xed60
movq -0xe8(%rbp), %rdi
movslq (%rax), %rsi
callq 0x10130
movq %rax, -0xe0(%rbp)
jmp 0xdb96a
movq -0xe0(%rbp), %rdi
addq $0x20, %rdi
callq 0x2a9e0
movq %rax, %rcx
movl -0xec(%rbp), %eax
cmpl %ecx, %eax
jge 0xdbbaa
cmpl $0x0, -0x14(%rbp)
jne 0xdba5b
movq -0xd0(%rbp), %rax
addq $0x60, %rax
movq %rax, -0x100(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0xed60
movq -0x100(%rbp), %rdi
movslq (%rax), %rsi
callq 0x10130
movq %rax, -0xf8(%rbp)
jmp 0xdb9cc
movq -0xf8(%rbp), %rdi
addq $0x20, %rdi
movslq -0x14(%rbp), %rsi
callq 0x64470
movq %rax, -0x108(%rbp)
jmp 0xdb9e9
movq -0xd0(%rbp), %rax
movq -0x108(%rbp), %rcx
movsd 0x8(%rcx), %xmm0
movsd %xmm0, -0x28(%rbp)
addq $0x60, %rax
movq %rax, -0x118(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0xed60
movq -0x118(%rbp), %rdi
movslq (%rax), %rsi
callq 0x10130
movq %rax, -0x110(%rbp)
jmp 0xdba31
movq -0x110(%rbp), %rdi
addq $0x20, %rdi
movslq -0x14(%rbp), %rsi
callq 0x64470
movq %rax, -0x120(%rbp)
jmp 0xdba4e
movq -0x120(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x14(%rbp)
jle 0xdbb9a
movq -0xd0(%rbp), %rax
addq $0x60, %rax
movq %rax, -0x130(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0xed60
movq -0x130(%rbp), %rdi
movslq (%rax), %rsi
callq 0x10130
movq %rax, -0x128(%rbp)
jmp 0xdba9c
movq -0x128(%rbp), %rdi
addq $0x20, %rdi
movslq -0x14(%rbp), %rsi
callq 0x64470
movq %rax, -0x138(%rbp)
jmp 0xdbab9
movq -0x138(%rbp), %rax
movsd 0x8(%rax), %xmm1
movsd -0x28(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xdbb9a
movq -0xd0(%rbp), %rax
addq $0x60, %rax
movq %rax, -0x148(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0xed60
movq -0x148(%rbp), %rdi
movslq (%rax), %rsi
callq 0x10130
movq %rax, -0x140(%rbp)
jmp 0xdbb0b
movq -0x140(%rbp), %rdi
addq $0x20, %rdi
movslq -0x14(%rbp), %rsi
callq 0x64470
movq %rax, -0x150(%rbp)
jmp 0xdbb28
movq -0xd0(%rbp), %rax
movq -0x150(%rbp), %rcx
movl 0x4(%rcx), %ecx
movl %ecx, -0x18(%rbp)
addq $0x60, %rax
movq %rax, -0x160(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0xed60
movq -0x160(%rbp), %rdi
movslq (%rax), %rsi
callq 0x10130
movq %rax, -0x158(%rbp)
jmp 0xdbb6c
movq -0x158(%rbp), %rdi
addq $0x20, %rdi
movslq -0x14(%rbp), %rsi
callq 0x64470
movq %rax, -0x168(%rbp)
jmp 0xdbb89
movq -0x168(%rbp), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x28(%rbp)
jmp 0xdbb9c
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xdb92a
movslq -0x18(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0xed60
cmpl $-0x1, (%rax)
jle 0xdbbce
movslq -0x18(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0xed60
movl (%rax), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x16c(%rbp)
leaq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xed60
movslq (%rax), %rsi
leaq -0x58(%rbp), %rdi
callq 0xed60
movl -0x16c(%rbp), %ecx
movl %ecx, (%rax)
movslq -0x18(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0xed60
movl $0x1, (%rax)
leaq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xed60
movslq (%rax), %rsi
leaq -0x70(%rbp), %rdi
callq 0xed60
movl $0x1, (%rax)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xdbc35
jmp 0xdbc37
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xdb7ff
movq -0xd0(%rbp), %rdi
movl 0x138(%rdi), %eax
movl -0x20(%rbp), %ecx
subl %ecx, %eax
movl %eax, 0x1b8(%rdi)
movq %rdi, %rax
addq $0x1d8, %rax            # imm = 0x1D8
movq %rax, -0x178(%rbp)
addq $0x60, %rdi
callq 0x10100
movq -0x178(%rbp), %rdi
movq %rax, %rsi
movl $0xffffffff, -0xc0(%rbp) # imm = 0xFFFFFFFF
leaq -0xc0(%rbp), %rdx
callq 0x84b00
jmp 0xdbc98
movl $0x0, -0x1c(%rbp)
movl $0x0, -0xc(%rbp)
movq -0xd0(%rbp), %rcx
movl -0xc(%rbp), %eax
cmpl 0x138(%rcx), %eax
jge 0xdbdbf
movslq -0xc(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0xed60
cmpl $0x0, (%rax)
jne 0xdbdaf
movq -0xd0(%rbp), %rdi
addq $0x1c0, %rdi            # imm = 0x1C0
leaq -0x1c(%rbp), %rsi
callq 0xd1b0
jmp 0xdbcee
movq -0xd0(%rbp), %rdi
movq %rdi, %rax
addq $0x250, %rax            # imm = 0x250
movq %rax, -0x180(%rbp)
addq $0x238, %rdi            # imm = 0x238
movslq -0xc(%rbp), %rsi
callq 0xedb0
movq -0x180(%rbp), %rdi
movq %rax, %rsi
callq 0x105c0
jmp 0xdbd26
movl $0x0, -0x10(%rbp)
movq -0xd0(%rbp), %rdi
movl -0x10(%rbp), %eax
movl %eax, -0x184(%rbp)
addq $0x60, %rdi
callq 0x10100
movq %rax, %rcx
movl -0x184(%rbp), %eax
cmpl %ecx, %eax
jge 0xdbda6
movq -0xd0(%rbp), %rdi
addq $0x140, %rdi            # imm = 0x140
movslq -0x10(%rbp), %rsi
callq 0xed60
movl (%rax), %eax
cmpl -0xc(%rbp), %eax
jne 0xdbd99
movq -0xd0(%rbp), %rdi
movl -0x1c(%rbp), %eax
movl %eax, -0x188(%rbp)
addq $0x1d8, %rdi            # imm = 0x1D8
movslq -0x10(%rbp), %rsi
callq 0xed60
movl -0x188(%rbp), %ecx
movl %ecx, (%rax)
jmp 0xdbd9b
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xdbd2d
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xdbdb1
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xdbca6
movl $0x0, -0x1c(%rbp)
movl $0x0, -0xc(%rbp)
movq -0xd0(%rbp), %rdi
movl -0xc(%rbp), %eax
movl %eax, -0x18c(%rbp)
addq $0x60, %rdi
callq 0x10100
movq %rax, %rcx
movl -0x18c(%rbp), %eax
cmpl %ecx, %eax
jge 0xdbe79
movslq -0xc(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0xed60
cmpl $-0x1, (%rax)
jle 0xdbe69
movq -0xd0(%rbp), %rax
addq $0x1d8, %rax            # imm = 0x1D8
movq %rax, -0x198(%rbp)
movslq -0xc(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0xed60
movq -0x198(%rbp), %rdi
movslq (%rax), %rsi
callq 0xed60
movq -0xd0(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x190(%rbp)
addq $0x1d8, %rdi            # imm = 0x1D8
movslq -0xc(%rbp), %rsi
callq 0xed60
movl -0x190(%rbp), %ecx
movl %ecx, (%rax)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xdbe6b
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xdbdcd
movl $0x0, -0xc(%rbp)
movq -0xd0(%rbp), %rdi
movl -0xc(%rbp), %eax
movl %eax, -0x19c(%rbp)
addq $0x60, %rdi
callq 0x10100
movq %rax, %rcx
movl -0x19c(%rbp), %eax
cmpl %ecx, %eax
jge 0xdbf08
movq -0xd0(%rbp), %rdi
addq $0x1d8, %rdi            # imm = 0x1D8
movslq -0xc(%rbp), %rsi
callq 0xed60
cmpl $-0x1, (%rax)
jne 0xdbef8
movq 0x1970f7(%rip), %rdi    # 0x272fc0
leaq 0x11fbfc(%rip), %rsi    # 0x1fbacc
callq 0x5540
movq %rax, -0x1a8(%rbp)
jmp 0xdbede
movq -0x1a8(%rbp), %rdi
leaq 0x11d739(%rip), %rsi    # 0x1f9625
callq 0x5540
jmp 0xdbef3
callq 0x52b0
jmp 0xdbefa
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xdbe80
movq 0x1970b1(%rip), %rdi    # 0x272fc0
leaq 0x11fbda(%rip), %rsi    # 0x1fbaf0
callq 0x5540
movq %rax, -0x1b0(%rbp)
jmp 0xdbf24
movq -0x1b0(%rbp), %rdi
movq -0xd0(%rbp), %rax
movl 0x1b8(%rax), %esi
callq 0x58d0
movq %rax, -0x1b8(%rbp)
jmp 0xdbf46
movq -0x1b8(%rbp), %rdi
leaq 0x11d6d1(%rip), %rsi    # 0x1f9625
callq 0x5540
jmp 0xdbf5b
leaq -0xa0(%rbp), %rdi
callq 0xec80
leaq -0x88(%rbp), %rdi
callq 0xec80
leaq -0x70(%rbp), %rdi
callq 0xec80
leaq -0x58(%rbp), %rdi
callq 0xec80
leaq -0x40(%rbp), %rdi
callq 0xec80
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
movq -0xb0(%rbp), %rdi
callq 0x58f0
nopw %cs:(%rax,%rax)