// File: /workspace/github_cpp_makefile_star2_O2/nikosgalanis[P]Vectors-Similarity-Search-and-Clustering/programs/Hypercube/../../algorithms/Search/Hypercube/headers/hypercube.h
// Function: Hypercube<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;

			uint32_t visited = 0;
			uint32_t probes_checked = 0;

			/**
			 Step 1: Hash the given vector with the random projection technique
			**/
			int bucket_int = hash_to_hypercube(query_vector);

			/**
			 Step 2: Search in the bucket that the query hashes for neighbors
			**/
			list<int> bucket = hypercube[bucket_int];

			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);
				} else {
					exit(EXIT_FAILURE);
				}

				// if we've reached the threshold, just return the result list
				if (visited >= threshold) 
					return result;
			}

			// return if max probes were traversed
			if (++probes_checked >= max_probes)
				return result;
			
			/**
			 Step 3 .. n: If necessary, check all the buckets with hamming distance 1 .. n
						until the threshold is reached.
			**/
			// set the desired hamming distance to 1 initially
			int dist_count = 1;
			// while we are under the threshold
			while (true) {
				// parse through all the buckets
				for (uint32_t i = 0; i < n_buckets; i++) {
					// if the hamming distance from the bucket_int is the desired
					if (our_math::hamming_distance(i, bucket_int) == dist_count) {
						// check the bucket for neighbors in the range given
						list<int> bucket = hypercube[i];
						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);
							} else {
								exit(EXIT_FAILURE);
							}

							// if we've reached the threshold, just return the result list
							if (visited >= threshold) 
								return result;
						}
						// return if max probes were traversed
						if (++probes_checked >= max_probes)
							return result;
					}
				}
				// again, if necessary, increase the hamming distance, so we check further buckets
				dist_count++;
			}
		}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movl %ecx, %ebp
movq %rsi, %r15
movq %rdi, %rbx
movq %rdi, 0x8(%rdi)
movq %rdi, (%rdi)
andq $0x0, 0x10(%rdi)
leaq 0x90(%rsp), %rdi
movq %rdx, 0x50(%rsp)
movq %rdx, %rsi
callq 0x31a2
leaq 0x90(%rsp), %rsi
movq %r15, %rdi
callq 0x4f46
movl %eax, %r14d
leaq 0x90(%rsp), %rdi
callq 0x2f56
movl %r14d, 0x2c(%rsp)
movslq %r14d, %rax
imulq $0x18, %rax, %rsi
addq 0x28(%r15), %rsi
leaq 0x78(%rsp), %r13
movq %r13, %rdi
callq 0x56c8
leaq 0x40(%r15), %rax
movq %rax, 0x58(%rsp)
movslq %ebp, %rax
movq %rax, 0x48(%rsp)
movsd 0x3086(%rip), %xmm0     # 0x71d0
movsd %xmm0, 0x10(%rsp)
xorl %r14d, %r14d
leaq 0xf0(%rsp), %rbp
movq (%r13), %r13
leaq 0x78(%rsp), %rax
cmpq %rax, %r13
je 0x42ab
movslq 0x10(%r13), %rsi
movq 0x58(%rsp), %rdi
callq 0x3cce
leaq 0xa8(%rsp), %rdi
movq %rax, %rsi
callq 0x31a2
movq %rbp, %rdi
movq 0x50(%rsp), %rsi
callq 0x31a2
movl 0x20(%r15), %edx
leaq 0xa8(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x4bd7
movsd %xmm0, 0x8(%rsp)
movq %rbp, %rdi
callq 0x2f56
movq %r12, %rdi
callq 0x2f56
movl 0x10(%r13), %eax
movl %eax, 0x30(%rsp)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movq (%rbx), %rdi
movq %rbx, %rsi
leaq 0x30(%rsp), %rdx
callq 0x6781
incl %r14d
cmpq %rbx, %rax
jne 0x415b
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x423a
cmpq 0x48(%rsp), %rax
jae 0x4255
movsd 0x8(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x423a
movq %rbx, %rsi
movsd 0x8(%rsp), %xmm0
movq (%rsi), %rsi
cmpq %rbx, %rsi
je 0x429f
ucomisd 0x18(%rsi), %xmm0
jae 0x421c
movq %rbx, %rdi
leaq 0x30(%rsp), %rdx
callq 0x4c1a
jmp 0x429f
movq %rbx, %rdi
leaq 0x30(%rsp), %rsi
callq 0x4c0e
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
jmp 0x429f
movsd 0x8(%rsp), %xmm0
jne 0x44e8
ucomisd 0x10(%rsp), %xmm0
jae 0x429f
movq %rbx, %rsi
movq (%rsi), %rsi
cmpq %rbx, %rsi
je 0x4288
ucomisd 0x18(%rsi), %xmm0
jae 0x426c
movq %rbx, %rdi
leaq 0x30(%rsp), %rdx
callq 0x4c1a
movq %rbx, %rdi
callq 0x4c48
movq 0x8(%rbx), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
cmpl 0x10(%r15), %r14d
jb 0x415b
jmp 0x42b2
cmpl $0x2, 0x14(%r15)
jae 0x42c1
leaq 0x78(%rsp), %rdi
callq 0x57ee
jmp 0x44d3
pushq $0x1
popq %rax
movq $0x0, 0x20(%rsp)
leaq 0xd8(%rsp), %rbp
leaq 0xc0(%rsp), %r13
movl %eax, 0x1c(%rsp)
movl %eax, 0x18(%rsp)
xorl %r12d, %r12d
movl 0x24(%r15), %eax
cmpq %rax, %r12
jae 0x44b1
movl %r12d, %edi
movl 0x2c(%rsp), %esi
callq 0x676d
cmpl 0x1c(%rsp), %eax
jne 0x44a9
movq %r12, 0x70(%rsp)
imulq $0x18, %r12, %rsi
addq 0x28(%r15), %rsi
leaq 0x30(%rsp), %r12
movq %r12, %rdi
callq 0x56c8
movq (%r12), %r12
leaq 0x30(%rsp), %rax
cmpq %rax, %r12
je 0x4474
movslq 0x10(%r12), %rsi
movq 0x58(%rsp), %rdi
callq 0x3cce
movq %rbp, %rdi
movq %rax, %rsi
callq 0x31a2
movq %r13, %rdi
movq 0x50(%rsp), %rsi
callq 0x31a2
movl 0x20(%r15), %edx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x4bd7
movq %r13, %rdi
movsd %xmm0, 0x8(%rsp)
callq 0x2f56
movq %rbp, %rdi
callq 0x2f56
movl 0x10(%r12), %eax
movl %eax, 0x60(%rsp)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movq (%rbx), %rdi
movq %rbx, %rsi
leaq 0x60(%rsp), %rdx
callq 0x6781
incl %r14d
cmpq %rbx, %rax
jne 0x4325
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x43f9
cmpq 0x48(%rsp), %rax
jae 0x4414
movsd 0x8(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x43f9
movq %rbx, %rsi
movsd 0x8(%rsp), %xmm0
movq (%rsi), %rsi
cmpq %rbx, %rsi
je 0x445e
ucomisd 0x18(%rsi), %xmm0
jae 0x43db
movq %rbx, %rdi
leaq 0x60(%rsp), %rdx
callq 0x4c1a
jmp 0x445e
movq %rbx, %rdi
leaq 0x60(%rsp), %rsi
callq 0x4c0e
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
jmp 0x445e
movsd 0x8(%rsp), %xmm0
jne 0x44e8
ucomisd 0x10(%rsp), %xmm0
jae 0x445e
movq %rbx, %rsi
movq (%rsi), %rsi
cmpq %rbx, %rsi
je 0x4447
ucomisd 0x18(%rsi), %xmm0
jae 0x442b
movq %rbx, %rdi
leaq 0x60(%rsp), %rdx
callq 0x4c1a
movq %rbx, %rdi
callq 0x4c48
movq 0x8(%rbx), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
cmpl 0x10(%r15), %r14d
jb 0x4325
movb $0x1, %al
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
jmp 0x4495
movl 0x18(%rsp), %eax
incl %eax
movl %eax, 0x18(%rsp)
cmpl 0x14(%r15), %eax
setb %r12b
setae %al
movq 0x20(%rsp), %rcx
orb %al, %cl
movq %rcx, 0x20(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x57ee
testb %r12b, %r12b
je 0x44ba
movq 0x70(%rsp), %r12
incq %r12
jmp 0x42e8
incl 0x1c(%rsp)
jmp 0x42e5
leaq 0x78(%rsp), %rdi
callq 0x57ee
testb $0x1, 0x20(%rsp)
jne 0x44d3
movq %rbx, %rdi
callq 0x4af4
movq %rbx, %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rdi
callq 0x2220
jmp 0x453b
jmp 0x453b
jmp 0x4508
movq %rax, %r14
leaq 0x90(%rsp), %rdi
callq 0x2f56
jmp 0x457f
movq %rax, %r14
jmp 0x457f
jmp 0x453b
jmp 0x4568
jmp 0x4568
jmp 0x453b
jmp 0x453b
movq %rax, %r14
leaq 0xf0(%rsp), %rdi
callq 0x2f56
jmp 0x452c
movq %rax, %r14
leaq 0xa8(%rsp), %rdi
callq 0x2f56
jmp 0x4575
movq %rax, %r14
jmp 0x4575
jmp 0x4568
jmp 0x4568
movq %rax, %r14
leaq 0xc0(%rsp), %rdi
callq 0x2f56
jmp 0x4559
movq %rax, %r14
leaq 0xd8(%rsp), %rdi
callq 0x2f56
jmp 0x456b
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x57ee
leaq 0x78(%rsp), %rdi
callq 0x57ee
movq %rbx, %rdi
callq 0x4af4
movq %r14, %rdi
callq 0x2380
nop