// File: /workspace/github_cpp_makefile_star2_O2/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 $0xb8, %rsp
movq %rdx, %r14
movq %rdi, %rbx
movq %rdi, 0x8(%rdi)
movq %rdi, (%rdi)
andq $0x0, 0x10(%rdi)
leaq 0x20(%rsi), %rax
leaq 0x38(%rsi), %rdx
movq %rdx, 0x38(%rsp)
movq %rsi, 0x50(%rsp)
leaq 0x50(%rsi), %r13
movslq %ecx, %rcx
movq %rcx, 0x48(%rsp)
movsd 0x21dd(%rip), %xmm0     # 0x61f8
movsd %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
leaq 0x70(%rsp), %r15
leaq 0xa0(%rsp), %rbp
leaq 0x88(%rsp), %r12
movq %rax, 0x40(%rsp)
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x18(%rsp)
cmpq 0x40(%rsp), %rax
je 0x4210
movq %r15, %rdi
movq %r14, %rsi
callq 0x3152
movq 0x18(%rsp), %rax
leaq 0x10(%rax), %rdi
movq %r15, %rsi
callq 0x4a9e
movl %eax, 0x8(%rsp)
movq %r15, %rdi
callq 0x2f06
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x4a26
leaq 0x58(%rsp), %r15
movslq 0x8(%rsp), %rcx
imulq $0x18, %rcx, %rsi
addq (%rax), %rsi
movq %r15, %rdi
callq 0x5a44
movq (%r15), %r15
leaq 0x58(%rsp), %rax
cmpq %rax, %r15
je 0x41f7
movslq 0x10(%r15), %rsi
movq %r13, %rdi
callq 0x3af2
movq %rbp, %rdi
movq %rax, %rsi
callq 0x3152
movq %r12, %rdi
movq %r14, %rsi
callq 0x3152
movq 0x50(%rsp), %rax
movl 0x18(%rax), %edx
movq %rbp, %rdi
movq %r12, %rsi
callq 0x476f
movq %r12, %rdi
movsd %xmm0, 0x8(%rsp)
callq 0x2f06
movq %rbp, %rdi
callq 0x2f06
movl 0x10(%r15), %eax
movl %eax, 0x28(%rsp)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movq (%rbx), %rdi
movq %rbx, %rsi
leaq 0x28(%rsp), %rdx
callq 0x5d77
cmpq %rbx, %rax
jne 0x40b1
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x4186
cmpq 0x48(%rsp), %rax
jae 0x41a4
movsd 0x8(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x4186
movq %rbx, %rsi
movsd 0x8(%rsp), %xmm0
movq (%rsi), %rsi
cmpq %rbx, %rsi
je 0x40b1
ucomisd 0x18(%rsi), %xmm0
jae 0x4161
movq %rbx, %rdi
leaq 0x28(%rsp), %rdx
callq 0x47b2
jmp 0x40b1
movq %rbx, %rdi
leaq 0x28(%rsp), %rsi
callq 0x47a6
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
jmp 0x40b1
movsd 0x8(%rsp), %xmm0
jne 0x40b1
ucomisd 0x10(%rsp), %xmm0
jae 0x40b1
movq %rbx, %rsi
movq (%rsi), %rsi
cmpq %rbx, %rsi
je 0x41db
ucomisd 0x18(%rsi), %xmm0
jae 0x41bf
movq %rbx, %rdi
leaq 0x28(%rsp), %rdx
callq 0x47b2
movq %rbx, %rdi
callq 0x47e0
movq 0x8(%rbx), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
jmp 0x40b1
incq 0x20(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x5744
leaq 0x70(%rsp), %r15
jmp 0x4049
movq %rbx, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4267
jmp 0x4267
jmp 0x423a
movq %rax, %r14
leaq 0x70(%rsp), %rdi
callq 0x2f06
jmp 0x4274
movq %rax, %r14
jmp 0x4274
jmp 0x4267
movq %rax, %r14
jmp 0x4256
movq %rax, %r14
leaq 0x88(%rsp), %rdi
callq 0x2f06
leaq 0xa0(%rsp), %rdi
callq 0x2f06
jmp 0x426a
jmp 0x4267
movq %rax, %r14
leaq 0x58(%rsp), %rdi
callq 0x5744
movq %rbx, %rdi
callq 0x468c
movq %r14, %rdi
callq 0x2340