// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %rdx, 0x30(%rsp)
movq %rdi, %rbx
movq %rdi, 0x8(%rdi)
movq %rdi, (%rdi)
andq $0x0, 0x10(%rdi)
movq %rsi, %r15
leaq 0x8(%rsi), %r12
movslq %ecx, %rax
movq %rax, 0x28(%rsp)
movsd 0x2c07(%rip), %xmm0     # 0x71d0
movsd %xmm0, 0x8(%rsp)
xorl %r13d, %r13d
leaq 0x50(%rsp), %rbp
leaq 0x38(%rsp), %r14
movl (%r15), %eax
cmpq %rax, %r13
jae 0x46dd
movq %r12, %rdi
movq %r13, %rsi
callq 0x3cce
movq %rbp, %rdi
movq %rax, %rsi
callq 0x31a2
movq %r14, %rdi
movq 0x30(%rsp), %rsi
callq 0x31a2
movl 0x4(%r15), %edx
movq %rbp, %rdi
movq %r14, %rsi
callq 0x4bd7
movsd %xmm0, 0x10(%rsp)
movq %r14, %rdi
callq 0x2f56
movq %rbp, %rdi
callq 0x2f56
movl %r13d, 0x18(%rsp)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x467a
cmpq 0x28(%rsp), %rax
jae 0x4695
ucomisd 0x8(%rsp), %xmm0
movq %rbx, %rsi
jae 0x467a
movq (%rsi), %rsi
cmpq %rbx, %rsi
je 0x46d5
ucomisd 0x18(%rsi), %xmm0
jae 0x465c
movq %rbx, %rdi
leaq 0x18(%rsp), %rdx
callq 0x4c1a
jmp 0x46d5
movq %rbx, %rdi
leaq 0x18(%rsp), %rsi
callq 0x4c0e
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
jmp 0x46d5
jne 0x46ef
ucomisd 0x8(%rsp), %xmm0
jae 0x46d5
movq %rbx, %rsi
movq (%rsi), %rsi
cmpq %rbx, %rsi
je 0x46be
ucomisd 0x18(%rsi), %xmm0
jae 0x46a2
movq %rbx, %rdi
leaq 0x18(%rsp), %rdx
callq 0x4c1a
movq %rbx, %rdi
callq 0x4c48
movq 0x8(%rbx), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
incq %r13
jmp 0x45dc
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x68e2(%rip), %rdi      # 0xafd8
leaq 0x2c82(%rip), %rsi      # 0x737f
callq 0x21d0
movq 0x10(%rbx), %rsi
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x20d0
movq 0x68bb(%rip), %rdi      # 0xafd8
leaq 0x2c61(%rip), %rsi      # 0x7385
callq 0x21d0
movq %rax, %rdi
callq 0x20d0
pushq $0x1
popq %rdi
callq 0x2220
jmp 0x475f
jmp 0x475f
jmp 0x475f
jmp 0x475f
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0x2f56
jmp 0x4753
movq %rax, %r14
leaq 0x50(%rsp), %rdi
callq 0x2f56
jmp 0x4762
movq %rax, %r14
movq %rbx, %rdi
callq 0x4af4
movq %r14, %rdi
callq 0x2380