// File: /workspace/github_cpp_makefile_star2_O3/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 0x5851
movq %rdx, %r14
movq %rsi, %r15
movslq %ecx, %r12
leaq -0x1(%r12), %rax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
movsd 0x2b7d(%rip), %xmm0     # 0x81d0
movsd %xmm0, 0x8(%rsp)
movabsq $-0x5555555555555555, %rbp # imm = 0xAAAAAAAAAAAAAAAB
movl %r13d, %esi
movq 0x8(%r15), %rax
movq 0x10(%r15), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
imulq %rbp, %rdx
cmpq %rsi, %rdx
jbe 0x5863
leaq (%rsi,%rsi,2), %rcx
leaq (%rax,%rcx,8), %rsi
leaq 0x18(%rsp), %rdi
callq 0x38aa
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x38aa
movslq 0x4(%r15), %rax
movq 0x30(%rsp), %rdi
xorpd %xmm2, %xmm2
testq %rax, %rax
jle 0x56de
movq 0x18(%rsp), %rcx
xorl %edx, %edx
movapd 0x2b4e(%rip), %xmm1     # 0x8210
movsd (%rcx,%rdx,8), %xmm0
subsd (%rdi,%rdx,8), %xmm0
andpd %xmm1, %xmm0
addsd %xmm0, %xmm2
incq %rdx
cmpq %rdx, %rax
jne 0x56c2
jmp 0x56e3
testq %rdi, %rdi
je 0x56f2
movsd %xmm2, (%rsp)
callq 0x2220
movsd (%rsp), %xmm2
movsd %xmm2, (%rsp)
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x5706
callq 0x2220
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x576d
cmpq %r12, %rax
jae 0x5779
movsd (%rsp), %xmm0
ucomisd 0x8(%rsp), %xmm0
movq %rbx, %rbp
jae 0x57d1
movq (%rbp), %rbp
cmpq %rbx, %rbp
je 0x583b
movsd (%rsp), %xmm0
ucomisd 0x18(%rbp), %xmm0
jae 0x5728
movl $0x20, %edi
callq 0x2240
movl %r13d, 0x10(%rax)
movsd (%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2060
incq 0x10(%rbx)
jmp 0x583b
movl $0x20, %edi
callq 0x2240
jmp 0x57e5
jne 0x5871
movsd (%rsp), %xmm0
ucomisd 0x8(%rsp), %xmm0
jae 0x5845
movq %rbx, %rbp
movq (%rbp), %rbp
cmpq %rbx, %rbp
je 0x580f
movsd (%rsp), %xmm0
ucomisd 0x18(%rbp), %xmm0
jae 0x5793
movl $0x20, %edi
callq 0x2240
movl %r13d, 0x10(%rax)
movsd (%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2060
movq 0x10(%rbx), %rax
jmp 0x5814
movl $0x20, %edi
callq 0x2240
movabsq $-0x5555555555555555, %rbp # imm = 0xAAAAAAAAAAAAAAAB
movl %r13d, 0x10(%rax)
movsd (%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2060
incq 0x10(%rbx)
movsd (%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
jmp 0x5845
movq 0x10(%rsp), %rax
movq 0x8(%rbx), %rbp
movq %rax, 0x10(%rbx)
movq %rbp, %rdi
callq 0x2380
movq %rbp, %rdi
callq 0x2220
movq 0x8(%rbx), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movabsq $-0x5555555555555555, %rbp # imm = 0xAAAAAAAAAAAAAAAB
incl %r13d
cmpl (%r15), %r13d
jb 0x5663
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2afb(%rip), %rdi      # 0x8365
xorl %eax, %eax
callq 0x2320
movq 0x6760(%rip), %rdi      # 0xbfd8
leaq 0x2b30(%rip), %rsi      # 0x83af
movl $0x5, %edx
callq 0x2260
movq 0x10(%rbx), %rsi
movq 0x6744(%rip), %rdi      # 0xbfd8
callq 0x2170
movq %rax, %rdi
callq 0x20f0
movq 0x6730(%rip), %rdi      # 0xbfd8
leaq 0x2b06(%rip), %rsi      # 0x83b5
movl $0x10, %edx
callq 0x2260
movq 0x6718(%rip), %rdi      # 0xbfd8
callq 0x20f0
movl $0x1, %edi
callq 0x2290
jmp 0x58d9
jmp 0x58d9
jmp 0x58d9
jmp 0x58d9
jmp 0x58d9
movq %rax, %r14
movq (%rbx), %rdi
cmpq %rbx, %rdi
je 0x58f4
movq (%rdi), %r15
callq 0x2220
movq %r15, %rdi
cmpq %rbx, %r15
jne 0x58e4
movq %r14, %rdi
callq 0x2400
movq %rax, %r14
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x58dc
callq 0x2220
jmp 0x58dc