// File: /workspace/github_cpp_makefile_star2_O1/nikosgalanis[P]Vectors-Similarity-Search-and-Clustering/programs/Hypercube/../../utils/../algorithms/Search/BruteForce/headers/brute_force.h
// Function: BruteForce<double>::kNeighboursBF[abi:cxx11](std::vector<double, std::allocator<double>>, int)
std::list<std::pair<int,T>> kNeighboursBF(std::vector<T> query_vector, int k) {

			list<pair<int,T>> result;

			//the greatest of the k min distances
			T kth_min_distance = (T) INT_MAX;

			// Traverse the hash tables
			for (uint32_t i = 0; i < n_points; i++) {
				// distance between vectors
				T distance =  metrics::ManhatanDistance(feature_vectors.at(i), query_vector, space_dim);
				
				// if the element is already in the list, skip the checks
				pair<int, T> new_pair = make_pair(i, distance);

				// insert the pair into the list if it's distance is less than the kth_min
				if (result.size() > 0 && result.size() < (uint64_t)k) {
					// size < k so we just insert it
					
					if (distance >= kth_min_distance) {
						// insert at back if distance > kth_min_distance
						// kth_min_distance is the distance of the last element
						kth_min_distance = distance;
						result.push_back(new_pair);
					} else {
						// iterate through the list to insert it in the right position
						typename std::list<std::pair<int,T>>::iterator pair_it;
						for ( pair_it = result.begin(); pair_it !=result.end(); pair_it++) {
							if ( distance < pair_it->second) {
								result.insert(pair_it, new_pair);
								break;
							}
						}

					}
				} else if (result.size() > 0 && (result.size() == (uint64_t)k)) {
					// insert, pop the last, and make the kth_min the new last
					if (distance < kth_min_distance) {
						// iterate through the list to insert it in the right position
						for (typename std::list<pair<int,T>>::iterator pair_it = result.begin(); pair_it !=result.end(); pair_it++) {
							if ( distance < pair_it->second) {
								result.insert(pair_it, new_pair);
								break;
							}
						}
						result.pop_back();
						kth_min_distance = result.back().second;
					}
				} else if (result.size() == 0) {
					// size is 0, insert 1st element
					kth_min_distance = distance;
					result.push_back(new_pair);
				} else {
					cout << "size " << result.size() << endl;
					cout << "knearest problem" << endl;
					exit(EXIT_FAILURE);
				}
			}
			return result;
		}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movq %rdi, 0x8(%rdi)
movq %rdi, (%rdi)
movq $0x0, 0x10(%rdi)
cmpl $0x0, (%rsi)
je 0x57fb
movq %rdx, %r14
movq %rsi, %r15
movslq %ecx, %r12
xorl %r13d, %r13d
movsd 0x2bf3(%rip), %xmm0     # 0x81f0
movsd %xmm0, 0x10(%rsp)
movl %r13d, %esi
movq 0x8(%r15), %rax
movq 0x10(%r15), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %rsi, %rdx
jbe 0x580d
leaq (%rsi,%rsi,2), %rcx
leaq (%rax,%rcx,8), %rsi
leaq 0x18(%rsp), %rdi
callq 0x3860
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x3860
movslq 0x4(%r15), %rax
testq %rax, %rax
jle 0x5688
movq 0x18(%rsp), %rcx
xorpd %xmm2, %xmm2
movq 0x30(%rsp), %rdx
xorl %esi, %esi
movapd 0x2bc4(%rip), %xmm1     # 0x8230
movsd (%rcx,%rsi,8), %xmm0
subsd (%rdx,%rsi,8), %xmm0
andpd %xmm1, %xmm0
addsd %xmm0, %xmm2
incq %rsi
cmpq %rsi, %rax
jne 0x566c
jmp 0x568c
xorpd %xmm2, %xmm2
movsd %xmm2, 0x8(%rsp)
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x56a1
callq 0x2220
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x56b0
callq 0x2220
movq 0x10(%rbx), %rax
testq %rax, %rax
sete %cl
cmpq %r12, %rax
setae %dl
orb %cl, %dl
jne 0x5720
movsd 0x8(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
movq %rbx, %rbp
jae 0x57aa
movq (%rbp), %rbp
cmpq %rbx, %rbp
je 0x57ef
movsd 0x8(%rsp), %xmm0
ucomisd 0x18(%rbp), %xmm0
jae 0x56d9
movl $0x20, %edi
callq 0x2240
movl %r13d, 0x10(%rax)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2060
incq 0x10(%rbx)
jmp 0x57ef
testq %rax, %rax
sete %cl
cmpq %r12, %rax
setne %dl
orb %cl, %dl
jne 0x57b6
movsd 0x8(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x57ef
movq %rbx, %rbp
movq (%rbp), %rbp
cmpq %rbx, %rbp
je 0x5787
movsd 0x8(%rsp), %xmm0
ucomisd 0x18(%rbp), %xmm0
jae 0x5749
movl $0x20, %edi
callq 0x2240
movl %r13d, 0x10(%rax)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2060
incq 0x10(%rbx)
movq 0x8(%rbx), %rbp
decq 0x10(%rbx)
movq %rbp, %rdi
callq 0x2380
movq %rbp, %rdi
callq 0x2220
movq 0x8(%rbx), %rax
movsd 0x18(%rax), %xmm0
jmp 0x57e9
movl $0x20, %edi
callq 0x2240
jmp 0x57c5
testq %rax, %rax
jne 0x581b
movl $0x20, %edi
callq 0x2240
movl %r13d, 0x10(%rax)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2060
incq 0x10(%rbx)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
incl %r13d
cmpl (%r15), %r13d
jb 0x5603
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2b71(%rip), %rdi      # 0x8385
xorl %eax, %eax
callq 0x2320
movq 0x67b6(%rip), %rdi      # 0xbfd8
leaq 0x2ba6(%rip), %rsi      # 0x83cf
movl $0x5, %edx
callq 0x2260
movq 0x10(%rbx), %rsi
movq 0x679a(%rip), %rdi      # 0xbfd8
callq 0x2170
movq %rax, %rdi
callq 0x20f0
movq 0x6786(%rip), %rdi      # 0xbfd8
leaq 0x2b7c(%rip), %rsi      # 0x83d5
movl $0x10, %edx
callq 0x2260
movq 0x676e(%rip), %rdi      # 0xbfd8
callq 0x20f0
movl $0x1, %edi
callq 0x2290
jmp 0x5883
jmp 0x5883
jmp 0x5883
jmp 0x5883
jmp 0x5883
movq %rax, %r14
movq (%rbx), %rdi
cmpq %rbx, %rdi
je 0x589e
movq (%rdi), %r15
callq 0x2220
movq %r15, %rdi
cmpq %rbx, %r15
jne 0x588e
movq %r14, %rdi
callq 0x2400
movq %rax, %r14
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x5886
callq 0x2220
jmp 0x5886