// File: /workspace/github_cpp_makefile_star2_O1/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, 0x30(%rsp)
movq %rdi, %rbx
movq %rdi, 0x8(%rdi)
movq %rdi, (%rdi)
movq $0x0, 0x10(%rdi)
leaq 0x20(%rsi), %rax
movq 0x20(%rsi), %r13
movq %rax, 0x38(%rsp)
cmpq %rax, %r13
je 0x5068
movq %rsi, %r12
movslq %ecx, %rbp
movsd 0x249c(%rip), %xmm0     # 0x71e8
movsd %xmm0, 0x10(%rsp)
xorl %r15d, %r15d
movq %rbp, %r14
leaq 0x80(%rsp), %rbp
movq %rbp, %rdi
movq 0x30(%rsp), %rsi
callq 0x3810
leaq 0x10(%r13), %rdi
movq %rbp, %rsi
callq 0x5a90
movl %eax, %ebp
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x4d8d
callq 0x2200
movq 0x38(%r12), %rax
movq 0x40(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %r15, %rdx
jbe 0x5090
movq %r13, 0x40(%rsp)
movq %r15, 0x48(%rsp)
leaq (%r15,%r15,2), %rcx
movslq %ebp, %rdx
leaq (%rdx,%rdx,2), %rsi
shlq $0x3, %rsi
addq (%rax,%rcx,8), %rsi
leaq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x67a0
movq 0x18(%rsp), %r13
cmpq %r15, %r13
movq %r14, %rbp
je 0x502d
movslq 0x10(%r13), %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 0x507d
leaq (%rsi,%rsi,2), %rcx
leaq (%rax,%rcx,8), %rsi
leaq 0x50(%rsp), %rdi
leaq 0x18(%rsp), %rbp
callq 0x3810
leaq 0x68(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x3810
movslq 0x18(%r12), %rax
testq %rax, %rax
jle 0x4e80
movq 0x50(%rsp), %rcx
xorpd %xmm2, %xmm2
movq 0x68(%rsp), %rdx
xorl %esi, %esi
movapd 0x23bc(%rip), %xmm1     # 0x7220
movsd (%rcx,%rsi,8), %xmm0
subsd (%rdx,%rsi,8), %xmm0
andpd %xmm1, %xmm0
addsd %xmm0, %xmm2
incq %rsi
cmpq %rsi, %rax
jne 0x4e64
jmp 0x4e84
xorpd %xmm2, %xmm2
movsd %xmm2, 0x8(%rsp)
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x4e99
callq 0x2200
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x4ea8
callq 0x2200
movl 0x10(%r13), %r15d
movq (%rbx), %rbp
movq %rbp, %rax
cmpq %rbx, %rbp
je 0x4ed5
movq %rbp, %rax
cmpl %r15d, 0x10(%rax)
jne 0x4ecd
movsd 0x18(%rax), %xmm0
ucomisd 0x8(%rsp), %xmm0
je 0x4ed5
movq (%rax), %rax
cmpq %rbx, %rax
jne 0x4eba
cmpq %rbx, %rax
jne 0x501c
movq 0x10(%rbx), %rax
testq %rax, %rax
sete %cl
cmpq %r14, %rax
setae %dl
orb %cl, %dl
jne 0x4f20
movsd 0x8(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x4f5e
cmpq %rbx, %rbp
je 0x501c
movsd 0x8(%rsp), %xmm0
ucomisd 0x18(%rbp), %xmm0
jb 0x4fa3
movq (%rbp), %rbp
jmp 0x4f00
testq %rax, %rax
sete %cl
cmpq %r14, %rax
setne %dl
orb %cl, %dl
jne 0x4f6a
movsd 0x8(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x501c
cmpq %rbx, %rbp
je 0x4ff5
movsd 0x8(%rsp), %xmm0
ucomisd 0x18(%rbp), %xmm0
jb 0x4fcd
movq (%rbp), %rbp
jmp 0x4f42
movl $0x20, %edi
callq 0x2220
jmp 0x4f7d
testq %rax, %rax
jne 0x501c
movl $0x20, %edi
callq 0x2220
movl %r15d, 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
jmp 0x5016
movl $0x20, %edi
callq 0x2220
movl %r15d, 0x10(%rax)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2060
incq 0x10(%rbx)
jmp 0x501c
movl $0x20, %edi
callq 0x2220
movl %r15d, 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 0x2340
movq %rbp, %rdi
callq 0x2200
movq 0x8(%rbx), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq (%r13), %r13
leaq 0x18(%rsp), %rax
cmpq %rax, %r13
jmp 0x4de7
movq 0x18(%rsp), %rdi
leaq 0x18(%rsp), %r14
cmpq %r14, %rdi
je 0x504c
movq (%rdi), %r15
callq 0x2200
movq %r15, %rdi
cmpq %r14, %r15
jne 0x503c
movq 0x48(%rsp), %r15
incq %r15
movq 0x40(%rsp), %r13
movq (%r13), %r13
cmpq 0x38(%rsp), %r13
jne 0x4d55
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x22e1(%rip), %rdi      # 0x7365
xorl %eax, %eax
leaq 0x18(%rsp), %rbp
callq 0x22e0
leaq 0x22ce(%rip), %rdi      # 0x7365
movq %r15, %rsi
xorl %eax, %eax
callq 0x22e0
jmp 0x50c5
jmp 0x50c5
jmp 0x50c0
movq %rax, %r15
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x5102
callq 0x2200
jmp 0x5102
jmp 0x50c0
movq %rax, %r15
jmp 0x5102
movq %rax, %r15
leaq 0x18(%rsp), %rbp
jmp 0x50e8
movq %rax, %r15
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x50e8
callq 0x2200
jmp 0x50e8
jmp 0x50e5
movq %rax, %r15
movq 0x18(%rsp), %rdi
cmpq %rbp, %rdi
je 0x5102
movq (%rdi), %r12
callq 0x2200
movq %r12, %rdi
cmpq %rbp, %r12
jne 0x50f2
movq (%rbx), %rdi
cmpq %rbx, %rdi
je 0x511a
movq (%rdi), %r14
callq 0x2200
movq %r14, %rdi
cmpq %rbx, %r14
jne 0x510a
movq %r15, %rdi
callq 0x23c0