// File: 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:
subq $0x168, %rsp            # imm = 0x168
movq %rdx, 0x58(%rsp)
movq %rdi, 0x60(%rsp)
movq %rdi, %rax
movq %rax, 0x68(%rsp)
movq %rdi, 0x160(%rsp)
movq %rsi, 0x158(%rsp)
movq %rdx, 0x150(%rsp)
movl %ecx, 0x14c(%rsp)
movq 0x158(%rsp), %rax
movq %rax, 0x70(%rsp)
movb $0x0, 0x14b(%rsp)
callq 0x92c0
movsd 0xa546(%rip), %xmm0     # 0x121e0
movsd %xmm0, 0x140(%rsp)
movl $0x0, 0x13c(%rsp)
movq 0x70(%rsp), %rcx
movl 0x13c(%rsp), %eax
cmpl (%rcx), %eax
jae 0x818a
movq 0x70(%rsp), %rdi
addq $0x8, %rdi
movl 0x13c(%rsp), %eax
movl %eax, %esi
callq 0x6640
movq %rax, 0x50(%rsp)
jmp 0x7ce0
movq 0x50(%rsp), %rsi
leaq 0x118(%rsp), %rdi
callq 0x4560
jmp 0x7cf4
movq 0x58(%rsp), %rsi
leaq 0xf0(%rsp), %rdi
callq 0x4560
jmp 0x7d08
movq 0x70(%rsp), %rax
movl 0x4(%rax), %edx
leaq 0x118(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x92d0
movsd %xmm0, 0x48(%rsp)
jmp 0x7d2d
leaq 0xf0(%rsp), %rdi
callq 0x3510
leaq 0x118(%rsp), %rdi
callq 0x3510
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x130(%rsp)
leaq 0x13c(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x9370
movsd %xmm0, 0x38(%rsp)
movl %eax, 0x44(%rsp)
jmp 0x7d77
movsd 0x38(%rsp), %xmm0
movl 0x44(%rsp), %eax
movl %eax, 0xd0(%rsp)
movsd %xmm0, 0xd8(%rsp)
leaq 0xe0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x93b0
jmp 0x7da8
movq 0x60(%rsp), %rdi
callq 0x93e0
cmpq $0x0, %rax
jbe 0x7f72
movq 0x60(%rsp), %rdi
callq 0x93e0
movslq 0x14c(%rsp), %rcx
cmpq %rcx, %rax
jae 0x7f72
movsd 0x130(%rsp), %xmm0
ucomisd 0x140(%rsp), %xmm0
jb 0x7e7c
movq 0x60(%rsp), %rdi
movsd 0x130(%rsp), %xmm0
movsd %xmm0, 0x140(%rsp)
leaq 0xe0(%rsp), %rsi
callq 0x9410
jmp 0x7e15
jmp 0x7f6d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
jmp 0x81b3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
jmp 0x7e6a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x3510
leaq 0x118(%rsp), %rdi
callq 0x3510
jmp 0x81b3
leaq 0xc8(%rsp), %rdi
callq 0x9450
movq 0x60(%rsp), %rdi
callq 0x81d0
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0x60(%rsp), %rdi
callq 0x8220
movq %rax, 0xb8(%rsp)
leaq 0xc8(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x8200
testb $0x1, %al
jne 0x7edb
jmp 0x7f6b
movsd 0x130(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x8250
movsd 0x30(%rsp), %xmm1
movsd 0x8(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x7f4d
leaq 0xb0(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x94f0
movq 0x60(%rsp), %rdi
movq 0xb0(%rsp), %rsi
leaq 0xe0(%rsp), %rdx
callq 0x9470
movq %rax, 0x28(%rsp)
jmp 0x7f3e
movq 0x28(%rsp), %rax
movq %rax, 0xa8(%rsp)
jmp 0x7f6b
jmp 0x7f4f
leaq 0xc8(%rsp), %rdi
xorl %esi, %esi
callq 0x8270
movq %rax, 0xa0(%rsp)
jmp 0x7eab
jmp 0x7f6d
jmp 0x8172
movq 0x60(%rsp), %rdi
callq 0x93e0
cmpq $0x0, %rax
jbe 0x80b3
movq 0x60(%rsp), %rdi
callq 0x93e0
movslq 0x14c(%rsp), %rcx
cmpq %rcx, %rax
jne 0x80b3
movsd 0x130(%rsp), %xmm1
movsd 0x140(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x80ae
movq 0x60(%rsp), %rdi
callq 0x81d0
movq %rax, 0x98(%rsp)
movq 0x60(%rsp), %rdi
callq 0x8220
movq %rax, 0x90(%rsp)
leaq 0x98(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x8200
testb $0x1, %al
jne 0x7fff
jmp 0x808c
movsd 0x130(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x8250
movsd 0x20(%rsp), %xmm1
movsd 0x8(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x8071
leaq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x94f0
movq 0x60(%rsp), %rdi
movq 0x88(%rsp), %rsi
leaq 0xe0(%rsp), %rdx
callq 0x9470
movq %rax, 0x18(%rsp)
jmp 0x8062
movq 0x18(%rsp), %rax
movq %rax, 0x80(%rsp)
jmp 0x808c
jmp 0x8073
leaq 0x98(%rsp), %rdi
xorl %esi, %esi
callq 0x8270
movq %rax, 0x78(%rsp)
jmp 0x7fcf
movq 0x60(%rsp), %rdi
callq 0x9510
movq 0x60(%rsp), %rdi
callq 0x8a30
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x140(%rsp)
jmp 0x8170
movq 0x60(%rsp), %rdi
callq 0x93e0
cmpq $0x0, %rax
jne 0x80ee
movq 0x60(%rsp), %rdi
movsd 0x130(%rsp), %xmm0
movsd %xmm0, 0x140(%rsp)
leaq 0xe0(%rsp), %rsi
callq 0x9410
jmp 0x80e9
jmp 0x816e
movq 0x11ee3(%rip), %rdi     # 0x19fd8
leaq 0xa297(%rip), %rsi      # 0x12393
callq 0x21c0
movq %rax, 0x10(%rsp)
jmp 0x8108
movq 0x60(%rsp), %rdi
callq 0x93e0
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x2070
movq %rax, 0x8(%rsp)
jmp 0x8126
movq 0x8(%rsp), %rdi
movq 0x11e96(%rip), %rsi     # 0x19fc8
callq 0x21e0
jmp 0x8139
movq 0x11e98(%rip), %rdi     # 0x19fd8
leaq 0xa252(%rip), %rsi      # 0x12399
callq 0x21c0
movq %rax, (%rsp)
jmp 0x8152
movq (%rsp), %rdi
movq 0x11e6b(%rip), %rsi     # 0x19fc8
callq 0x21e0
jmp 0x8164
movl $0x1, %edi
callq 0x2220
jmp 0x8170
jmp 0x8172
jmp 0x8174
movl 0x13c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x13c(%rsp)
jmp 0x7cae
movb $0x1, 0x14b(%rsp)
testb $0x1, 0x14b(%rsp)
jne 0x81a6
movq 0x60(%rsp), %rdi
callq 0x89b0
movq 0x68(%rsp), %rax
addq $0x168, %rsp            # imm = 0x168
retq
movq 0x60(%rsp), %rdi
callq 0x89b0
movq 0x110(%rsp), %rdi
callq 0x23c0
nopw (%rax,%rax)