// File: /workspace/github_cpp_makefile_star2_O3/nikosgalanis[P]Vectors-Similarity-Search-and-Clustering/programs/LSH/../../algorithms/Search/LSH/headers/lsh.h
// Function: LSH<double>::kNearestNeighbour[abi:cxx11](std::vector<double, std::allocator<double>>, int)
list<pair<int,T>> kNearestNeighbour(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;

					int i = 0;
					int visited = 0;

					// Traverse the hash tables
					for (std::list<lsh::AmplifiedHashFunction>::iterator it = amplified_hash_fns.begin(); 
						it != amplified_hash_fns.end(); it++) {
						// find the bucket that the query hashes into
						int index = it->assign_to_bucket(query_vector);
						list<int> bucket = lsh_tables.at(i)[index];
						// get all the indexes of the vectors in the bucket
						for (std::list<int>::iterator bucket_it = bucket.begin(); bucket_it != bucket.end(); bucket_it++) {
							visited++;
							// distance between vectors
							T distance =  metrics::ManhatanDistance(feature_vectors.at(*bucket_it), query_vector, space_dim);
							
							// if the element is already in the list, skip the checks
							pair<int, T> new_pair = make_pair(*bucket_it, distance);
							
							if (find(result.begin(), result.end(), new_pair) != result.end()) {
								continue;
							}

							// 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);
							}
							// if (visited > 10 * L) {
							//     return b;
							// }
						}
						i++;
					}
					return result;
				}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdx, 0x20(%rsp)
movq %rdi, %rbx
movq %rdi, 0x8(%rdi)
movq %rdi, (%rdi)
movq $0x0, 0x10(%rdi)
leaq 0x20(%rsi), %rax
movq 0x20(%rsi), %r15
movq %rax, 0x28(%rsp)
cmpq %rax, %r15
je 0x5062
movq %rsi, %r12
movslq %ecx, %rax
movq %rax, 0x10(%rsp)
movsd 0x2473(%rip), %xmm0     # 0x71c8
movsd %xmm0, 0x8(%rsp)
xorl %eax, %eax
leaq 0x68(%rsp), %r13
leaq 0x50(%rsp), %r14
movq %rax, 0x18(%rsp)
movq %r13, %rdi
movq 0x20(%rsp), %rsi
callq 0x382a
leaq 0x10(%r15), %rdi
movq %r13, %rsi
callq 0x5aa6
movl %eax, %ebp
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x4d96
callq 0x2200
movq 0x38(%r12), %rax
movq 0x40(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
movq 0x18(%rsp), %rsi
cmpq %rsi, %rdx
jbe 0x5085
movq %r15, 0x30(%rsp)
leaq (%rsi,%rsi,2), %rcx
movslq %ebp, %rdx
leaq (%rdx,%rdx,2), %rsi
shlq $0x3, %rsi
addq (%rax,%rcx,8), %rsi
movq %r14, %rdi
callq 0x67de
movq 0x50(%rsp), %rbp
cmpq %r14, %rbp
je 0x5029
movslq 0x10(%rbp), %rsi
movq 0x50(%r12), %rax
movq 0x58(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %rsi, %rdx
jbe 0x5077
leaq (%rsi,%rsi,2), %rcx
leaq (%rax,%rcx,8), %rsi
leaq 0x38(%rsp), %rdi
callq 0x382a
leaq 0x80(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x382a
movslq 0x18(%r12), %rax
movq 0x80(%rsp), %rdi
xorpd %xmm2, %xmm2
testq %rax, %rax
jle 0x4e82
movq 0x38(%rsp), %rcx
xorl %edx, %edx
movapd 0x239a(%rip), %xmm1     # 0x7200
movsd (%rcx,%rdx,8), %xmm0
subsd (%rdi,%rdx,8), %xmm0
andpd %xmm1, %xmm0
addsd %xmm0, %xmm2
incq %rdx
cmpq %rdx, %rax
jne 0x4e66
jmp 0x4e87
testq %rdi, %rdi
je 0x4e96
movsd %xmm2, (%rsp)
callq 0x2200
movsd (%rsp), %xmm2
movsd %xmm2, (%rsp)
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x4eaa
callq 0x2200
movl 0x10(%rbp), %r15d
movq (%rbx), %r13
cmpq %rbx, %r13
je 0x4ede
movq %r13, %rax
cmpl %r15d, 0x10(%rax)
jne 0x4ecb
movsd 0x18(%rax), %xmm0
ucomisd (%rsp), %xmm0
je 0x4ed5
movq (%rax), %rax
cmpq %rbx, %rax
jne 0x4eb9
jmp 0x4ede
cmpq %rbx, %rax
jne 0x5017
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x4f1b
cmpq 0x10(%rsp), %rax
jae 0x4f27
movsd (%rsp), %xmm0
ucomisd 0x8(%rsp), %xmm0
jae 0x4f6c
cmpq %rbx, %r13
je 0x5017
movsd (%rsp), %xmm0
ucomisd 0x18(%r13), %xmm0
jb 0x4f9a
movq (%r13), %r13
jmp 0x4efb
movl $0x20, %edi
callq 0x2220
jmp 0x4f76
jne 0x5017
movsd (%rsp), %xmm0
ucomisd 0x8(%rsp), %xmm0
jae 0x5017
movq 0x10(%rsp), %rax
cmpq %rbx, %r13
je 0x4fed
movsd (%rsp), %xmm0
ucomisd 0x18(%r13), %xmm0
jb 0x4fc3
movq (%r13), %r13
cmpq %rbx, %r13
jne 0x4f4c
movq 0x10(%rsp), %rax
jmp 0x4fed
movl $0x20, %edi
callq 0x2220
movl %r15d, 0x10(%rax)
movsd (%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2060
incq 0x10(%rbx)
movsd (%rsp), %xmm0
jmp 0x5011
movl $0x20, %edi
callq 0x2220
movl %r15d, 0x10(%rax)
movsd (%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movq %rax, %rdi
movq %r13, %rsi
callq 0x2060
incq 0x10(%rbx)
jmp 0x5017
movl $0x20, %edi
callq 0x2220
movl %r15d, 0x10(%rax)
movsd (%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movq %rax, %rdi
movq %r13, %rsi
callq 0x2060
movq 0x10(%rbx), %rax
incq %rax
movq 0x8(%rbx), %r13
decq %rax
movq %rax, 0x10(%rbx)
movq %r13, %rdi
callq 0x2340
movq %r13, %rdi
callq 0x2200
movq 0x8(%rbx), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq (%rbp), %rbp
cmpq %r14, %rbp
jne 0x4df1
movq 0x50(%rsp), %rbp
cmpq %r14, %rbp
je 0x5042
movq (%rbp), %r15
movq %rbp, %rdi
callq 0x2200
movq %r15, %rbp
cmpq %r14, %r15
jne 0x502e
movq 0x18(%rsp), %rax
incq %rax
movq 0x30(%rsp), %r15
movq (%r15), %r15
cmpq 0x28(%rsp), %r15
leaq 0x68(%rsp), %r13
jne 0x4d67
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x22c7(%rip), %rdi      # 0x7345
xorl %eax, %eax
callq 0x22e0
leaq 0x22b9(%rip), %rdi      # 0x7345
xorl %eax, %eax
callq 0x22e0
jmp 0x50b8
jmp 0x50b8
jmp 0x50af
movq %rax, %r15
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x50d5
callq 0x2200
jmp 0x50d5
jmp 0x50af
movq %rax, %r15
jmp 0x50d5
jmp 0x50b8
jmp 0x50b8
movq %rax, %r15
movq 0x50(%rsp), %rdi
cmpq %r14, %rdi
je 0x50d5
movq (%rdi), %r12
callq 0x2200
movq %r12, %rdi
cmpq %r14, %r12
jne 0x50c5
movq (%rbx), %rdi
cmpq %rbx, %rdi
je 0x50ed
movq (%rdi), %r14
callq 0x2200
movq %r14, %rdi
cmpq %rbx, %r14
jne 0x50dd
movq %r15, %rdi
callq 0x23c0
movq %rax, %r15
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x50bb
callq 0x2200
jmp 0x50bb
nop