// File: richardposner[P]BioNetFit2/src/code/Swarm.cpp
// Function: Swarm::getNearestNeighbors(unsigned int, unsigned int)
map<double, unsigned int> Swarm::getNearestNeighbors(unsigned int it, unsigned int N) {
	map<double, unsigned int> neighbors;

	// Normalize it params
	vector<double> itParams = normalizeParams(particleCurrParamSets_.at(it));

	for (auto paramSet = particleCurrParamSets_.begin(); paramSet != particleCurrParamSets_.end(); ++paramSet) {
		// Skip this particle if it is the 'it', or if it has the same fit as 'it'
		if (paramSet->first == it || particleCurrParamSets_.at(it) == particleCurrParamSets_.at(paramSet->first)) {
			continue;
		}

		// Calculate sum of squares
		double sum = 0;
		vector<double> pParams = normalizeParams(particleCurrParamSets_.at(paramSet->first));
		auto itIt = itParams.begin();
		for (auto param = pParams.begin(); param != pParams.end(); ++param) {
			sum += pow((*itIt - *param), 2);
			++itIt;
		}

		// Insert sum and id to map. It is automatically ordered.
		neighbors.insert(pair<double, unsigned int>(sum, paramSet->first));
	}

	// We only need the top N, so delete the rest
	auto nIt = neighbors.begin();
	advance(nIt, N);
	neighbors.erase(nIt, neighbors.end());

	return neighbors;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x128(%rbp)
movq %rdi, %rax
movq %rax, -0x120(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x118(%rbp)
movb $0x0, -0x19(%rbp)
callq 0xcae50
movq -0x118(%rbp), %rdi
addq $0x1400, %rdi           # imm = 0x1400
leaq -0x14(%rbp), %rsi
callq 0x122470
movq %rax, -0x110(%rbp)
jmp 0x11a20e
movq -0x110(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0xb2bc0
jmp 0x11a220
movq -0x118(%rbp), %rsi
leaq -0x38(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x117e60
jmp 0x11a236
leaq -0x50(%rbp), %rdi
callq 0xb2660
movq -0x118(%rbp), %rdi
addq $0x1400, %rdi           # imm = 0x1400
callq 0x124ea0
movq %rax, -0x68(%rbp)
movq -0x118(%rbp), %rdi
addq $0x1400, %rdi           # imm = 0x1400
callq 0xdef70
movq %rax, -0x70(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x124ed0
testb $0x1, %al
jne 0x11a283
jmp 0x11a541
leaq -0x68(%rbp), %rdi
callq 0x124f00
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
je 0x11a30f
movq -0x118(%rbp), %rdi
addq $0x1400, %rdi           # imm = 0x1400
leaq -0x14(%rbp), %rsi
callq 0x122470
movq %rax, -0x130(%rbp)
jmp 0x11a2b3
movq -0x118(%rbp), %rax
addq $0x1400, %rax           # imm = 0x1400
movq %rax, -0x140(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x124f00
movq -0x140(%rbp), %rdi
movq %rax, %rsi
callq 0x122470
movq %rax, -0x138(%rbp)
jmp 0x11a2e8
movq -0x138(%rbp), %rsi
movq -0x130(%rbp), %rdi
callq 0x124f20
movb %al, -0x141(%rbp)
jmp 0x11a303
movb -0x141(%rbp), %al
testb $0x1, %al
jne 0x11a30f
jmp 0x11a350
jmp 0x11a533
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x11a60b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0xb2660
jmp 0x11a60b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x11a5e4
movq -0x118(%rbp), %rax
movq $0x0, -0x78(%rbp)
addq $0x1400, %rax           # imm = 0x1400
movq %rax, -0x158(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x124f00
movq -0x158(%rbp), %rdi
movq %rax, %rsi
callq 0x122470
movq %rax, -0x150(%rbp)
jmp 0x11a38d
movq -0x150(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0xb2bc0
jmp 0x11a3a2
movq -0x118(%rbp), %rsi
leaq -0x90(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
callq 0x117e60
jmp 0x11a3be
leaq -0xa8(%rbp), %rdi
callq 0xb2660
leaq -0x38(%rbp), %rdi
callq 0xb2ea0
movq %rax, -0xb0(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xb2ea0
movq %rax, -0xb8(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xbdfd0
movq %rax, -0xc0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x122510
testb $0x1, %al
jne 0x11a41c
jmp 0x11a4cd
leaq -0xb0(%rbp), %rdi
callq 0xb2ed0
movsd (%rax), %xmm0
movsd %xmm0, -0x168(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0xb2ed0
movsd -0x168(%rbp), %xmm0
movsd (%rax), %xmm1
subsd %xmm1, %xmm0
movl $0x2, %edi
callq 0x8e5b0
movsd %xmm0, -0x160(%rbp)
jmp 0x11a464
movsd -0x160(%rbp), %xmm0
addsd -0x78(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0xb2ef0
leaq -0xb8(%rbp), %rdi
callq 0xb2ef0
jmp 0x11a3ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0xb2660
jmp 0x11a5e4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xb2660
jmp 0x11a5e4
leaq -0x68(%rbp), %rdi
callq 0x124f00
movq %rax, %rdx
leaq -0xd0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x124fe0
jmp 0x11a4eb
movq -0x128(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x124fa0
movb %dl, -0x171(%rbp)
movq %rax, -0x170(%rbp)
jmp 0x11a50d
movb -0x171(%rbp), %al
movq -0x170(%rbp), %rcx
movq %rcx, -0xe0(%rbp)
movb %al, -0xd8(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xb2660
leaq -0x68(%rbp), %rdi
callq 0xdfc00
jmp 0x11a256
movq -0x128(%rbp), %rdi
callq 0x124b90
movq %rax, -0xe8(%rbp)
movl -0x18(%rbp), %esi
leaq -0xe8(%rbp), %rdi
callq 0x125010
jmp 0x11a565
leaq -0xf0(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0xcb370
movq -0x128(%rbp), %rdi
callq 0xcb1d0
movq %rax, -0x100(%rbp)
leaq -0xf8(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0xcb370
movq -0x128(%rbp), %rdi
movq -0xf0(%rbp), %rsi
movq -0xf8(%rbp), %rdx
callq 0x125060
movq %rax, -0x180(%rbp)
jmp 0x11a5c1
movq -0x180(%rbp), %rax
movq %rax, -0x108(%rbp)
movb $0x1, -0x19(%rbp)
leaq -0x38(%rbp), %rdi
callq 0xb2660
testb $0x1, -0x19(%rbp)
jne 0x11a5fb
jmp 0x11a5ef
leaq -0x38(%rbp), %rdi
callq 0xb2660
jmp 0x11a60b
movq -0x128(%rbp), %rdi
callq 0x84630
movq -0x120(%rbp), %rax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
movq -0x128(%rbp), %rdi
callq 0x84630
movq -0x58(%rbp), %rdi
callq 0x30170