// File: /workspace/github_cpp_makefile_star2_O3/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 $0xf8, %rsp
movl %ecx, %ebp
movq %rsi, %r12
movq %rdi, %rbx
movq %rdi, 0x8(%rdi)
movq %rdi, (%rdi)
movq $0x0, 0x10(%rdi)
leaq 0xb0(%rsp), %rdi
movq %rdx, 0x58(%rsp)
movq %rdx, %rsi
callq 0x38aa
leaq 0xb0(%rsp), %rsi
movq %r12, %rdi
callq 0x6274
movl %eax, 0x3c(%rsp)
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x4e39
callq 0x2220
movslq 0x3c(%rsp), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0x28(%r12), %rsi
leaq 0x20(%rsp), %r13
movq %r13, %rdi
callq 0x6a66
movq 0x20(%rsp), %r15
cmpq %r13, %r15
movl %ebp, 0x48(%rsp)
je 0x50dd
movslq %ebp, %rax
movq %rax, 0x18(%rsp)
xorl %ebp, %ebp
movsd 0x3354(%rip), %xmm0     # 0x81d0
movsd %xmm0, 0x10(%rsp)
movslq 0x10(%r15), %rsi
movq 0x40(%r12), %rax
movq 0x48(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %rsi, %rdx
jbe 0x5520
leaq (%rsi,%rsi,2), %rcx
leaq (%rax,%rcx,8), %rsi
leaq 0x98(%rsp), %rdi
callq 0x38aa
leaq 0xe0(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x38aa
movslq 0x20(%r12), %rax
movq 0xe0(%rsp), %rdi
xorpd %xmm2, %xmm2
testq %rax, %rax
jle 0x4f19
movq 0x98(%rsp), %rcx
xorl %edx, %edx
movapd 0x3313(%rip), %xmm1     # 0x8210
movsd (%rcx,%rdx,8), %xmm0
subsd (%rdi,%rdx,8), %xmm0
andpd %xmm1, %xmm0
addsd %xmm0, %xmm2
incq %rdx
cmpq %rdx, %rax
jne 0x4efd
jmp 0x4f1e
testq %rdi, %rdi
je 0x4f2f
movsd %xmm2, 0x8(%rsp)
callq 0x2220
movsd 0x8(%rsp), %xmm2
movsd %xmm2, 0x8(%rsp)
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x4f47
callq 0x2220
incl %ebp
movl 0x10(%r15), %r14d
movq (%rbx), %r13
cmpq %rbx, %r13
je 0x4f7e
movq %r13, %rax
cmpl %r14d, 0x10(%rax)
jne 0x4f6b
movsd 0x18(%rax), %xmm0
ucomisd 0x8(%rsp), %xmm0
je 0x4f75
movq (%rax), %rax
cmpq %rbx, %rax
jne 0x4f58
jmp 0x4f7e
cmpq %rbx, %rax
jne 0x50ca
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x4fbd
cmpq 0x18(%rsp), %rax
jae 0x4fc9
movsd 0x8(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x5010
cmpq %rbx, %r13
je 0x50bf
movsd 0x8(%rsp), %xmm0
ucomisd 0x18(%r13), %xmm0
jb 0x5040
movq (%r13), %r13
jmp 0x4f9c
movl $0x20, %edi
callq 0x2240
jmp 0x501a
jne 0x5516
movsd 0x8(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x50bf
movq 0x18(%rsp), %rax
cmpq %rbx, %r13
je 0x5095
movsd 0x8(%rsp), %xmm0
ucomisd 0x18(%r13), %xmm0
jb 0x506a
movq (%r13), %r13
cmpq %rbx, %r13
jne 0x4fef
movq 0x18(%rsp), %rax
jmp 0x5095
movl $0x20, %edi
callq 0x2240
movl %r14d, 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 0x50b9
movl $0x20, %edi
callq 0x2240
movl %r14d, 0x10(%rax)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movq %rax, %rdi
movq %r13, %rsi
callq 0x2060
incq 0x10(%rbx)
jmp 0x50bf
movl $0x20, %edi
callq 0x2240
movl %r14d, 0x10(%rax)
movsd 0x8(%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 0x2380
movq %r13, %rdi
callq 0x2220
movq 0x8(%rbx), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
cmpl 0x10(%r12), %ebp
jae 0x54af
movq (%r15), %r15
leaq 0x20(%rsp), %r13
cmpq %r13, %r15
jne 0x4e82
jmp 0x50ed
xorl %ebp, %ebp
movsd 0x30e9(%rip), %xmm0     # 0x81d0
movsd %xmm0, 0x10(%rsp)
cmpl $0x2, 0x14(%r12)
jae 0x5118
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x54f3
movq (%rdi), %r14
callq 0x2220
movq %r14, %rdi
cmpq %r13, %r14
jne 0x5103
jmp 0x54f3
movslq 0x48(%rsp), %r8
movl 0x24(%r12), %eax
movl $0x1, %r9d
movq $0x0, 0x40(%rsp)
movl %eax, %ecx
movl $0x1, 0x38(%rsp)
movq %r12, 0x18(%rsp)
movq %r8, 0x78(%rsp)
testl %ecx, %ecx
je 0x549e
xorl %r10d, %r10d
movl %r9d, 0x54(%rsp)
movl 0x3c(%rsp), %ecx
xorl %r10d, %ecx
jle 0x548c
xorl %edx, %edx
movl %ecx, %esi
movl %ecx, %edi
andl $0x1, %edi
addl %edi, %edx
shrl %esi
cmpl $0x1, %ecx
movl %esi, %ecx
ja 0x5166
cmpl %r9d, %edx
jne 0x548c
movq %r10, 0x48(%rsp)
leaq (%r10,%r10,2), %rsi
shlq $0x3, %rsi
addq 0x28(%r12), %rsi
leaq 0x60(%rsp), %r15
movq %r15, %rdi
callq 0x6a66
movq 0x60(%rsp), %r14
cmpq %r15, %r14
movq 0x78(%rsp), %r15
je 0x541a
movslq 0x10(%r14), %rsi
movq 0x40(%r12), %rax
movq 0x48(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %rsi, %rdx
jbe 0x5508
leaq (%rsi,%rsi,2), %rcx
leaq (%rax,%rcx,8), %rsi
leaq 0x80(%rsp), %rdi
callq 0x38aa
leaq 0xc8(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x38aa
movslq 0x20(%r12), %rax
movq 0xc8(%rsp), %rdi
xorpd %xmm2, %xmm2
testq %rax, %rax
jle 0x5248
movq 0x80(%rsp), %rcx
xorl %edx, %edx
movapd 0x2fe4(%rip), %xmm1     # 0x8210
movsd (%rcx,%rdx,8), %xmm0
subsd (%rdi,%rdx,8), %xmm0
andpd %xmm1, %xmm0
addsd %xmm0, %xmm2
incq %rdx
cmpq %rdx, %rax
jne 0x522c
jmp 0x524d
testq %rdi, %rdi
je 0x525e
movsd %xmm2, 0x8(%rsp)
callq 0x2220
movsd 0x8(%rsp), %xmm2
movsd %xmm2, 0x8(%rsp)
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x5276
callq 0x2220
incl %ebp
movq %rbp, %r12
movl 0x10(%r14), %r13d
movq (%rbx), %rbp
cmpq %rbx, %rbp
je 0x52b9
movq %rbp, %rax
cmpl %r13d, 0x10(%rax)
jne 0x529d
movsd 0x18(%rax), %xmm0
ucomisd 0x8(%rsp), %xmm0
je 0x52a7
movq (%rax), %rax
cmpq %rbx, %rax
jne 0x528a
jmp 0x52b9
cmpq %rbx, %rax
je 0x52b9
leaq 0x20(%rsp), %r13
movq %r12, %rbp
jmp 0x5404
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x52f5
cmpq %r15, %rax
jae 0x5301
movsd 0x8(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x5343
cmpq %rbx, %rbp
je 0x53f2
movsd 0x8(%rsp), %xmm0
ucomisd 0x18(%rbp), %xmm0
jb 0x5373
movq (%rbp), %rbp
jmp 0x52d5
movl $0x20, %edi
callq 0x2240
jmp 0x534d
jne 0x5516
movsd 0x8(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x53f2
movq %r15, %rax
cmpq %rbx, %rbp
je 0x53c8
movsd 0x8(%rsp), %xmm0
ucomisd 0x18(%rbp), %xmm0
jb 0x539d
movq (%rbp), %rbp
cmpq %rbx, %rbp
jne 0x5325
movq %r15, %rax
jmp 0x53c8
movl $0x20, %edi
callq 0x2240
movl %r13d, 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 0x53ec
movl $0x20, %edi
callq 0x2240
movl %r13d, 0x10(%rax)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2060
incq 0x10(%rbx)
jmp 0x53f2
movl $0x20, %edi
callq 0x2240
movl %r13d, 0x10(%rax)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2060
movq 0x10(%rbx), %rax
incq %rax
movq 0x8(%rbx), %rbp
decq %rax
movq %rax, 0x10(%rbx)
movq %rbp, %rdi
callq 0x2380
movq %rbp, %rdi
callq 0x2220
movq 0x8(%rbx), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r12, %rbp
movq 0x18(%rsp), %rax
cmpl 0x10(%rax), %ebp
jae 0x5442
leaq 0x20(%rsp), %r13
movq (%r14), %r14
leaq 0x60(%rsp), %rax
cmpq %rax, %r14
movq 0x18(%rsp), %r12
jne 0x51b1
movq %r12, %rax
movq %rbp, %r12
movl 0x38(%rsp), %ecx
incl %ecx
movl %ecx, 0x38(%rsp)
cmpl 0x14(%rax), %ecx
setb %bpl
setae %al
movq 0x40(%rsp), %rcx
orb %al, %cl
movq %rcx, 0x40(%rsp)
jmp 0x5450
movb $0x1, %al
movq %rax, 0x40(%rsp)
xorl %ebp, %ebp
leaq 0x20(%rsp), %r13
movq 0x60(%rsp), %rdi
leaq 0x60(%rsp), %r15
cmpq %r15, %rdi
je 0x546f
movq (%rdi), %r14
callq 0x2220
movq %r14, %rdi
cmpq %r15, %r14
jne 0x545f
testb %bpl, %bpl
je 0x54a8
movq 0x18(%rsp), %rcx
movl 0x24(%rcx), %eax
movq %r12, %rbp
movq %rcx, %r12
movl 0x54(%rsp), %r9d
movq 0x48(%rsp), %r10
incq %r10
movl %eax, %edx
movl %eax, %ecx
cmpq %rdx, %r10
jb 0x5155
jmp 0x54a0
xorl %ecx, %ecx
incl %r9d
jmp 0x5145
movq 0x40(%rsp), %r15
jmp 0x54b7
movb $0x1, %r15b
leaq 0x20(%rsp), %r13
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x54d1
movq (%rdi), %r14
callq 0x2220
movq %r14, %rdi
cmpq %r13, %r14
jne 0x54c1
movq (%rbx), %rdi
cmpq %rbx, %rdi
sete %al
orb %al, %r15b
testb $0x1, %r15b
jne 0x54f3
movq (%rdi), %r14
callq 0x2220
movq %r14, %rdi
cmpq %rbx, %r14
jne 0x54e3
movq %rbx, %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2e56(%rip), %rdi      # 0x8365
xorl %eax, %eax
callq 0x2320
movl $0x1, %edi
callq 0x2290
leaq 0x2e3e(%rip), %rdi      # 0x8365
xorl %eax, %eax
callq 0x2320
jmp 0x5560
jmp 0x5560
jmp 0x5552
movq %rax, %r15
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x55ec
callq 0x2220
jmp 0x55ec
movq %rax, %r15
jmp 0x55ec
jmp 0x5560
jmp 0x5588
jmp 0x5588
movq %rax, %r15
jmp 0x557c
jmp 0x5583
movq %rax, %r15
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x557c
callq 0x2220
leaq 0x20(%rsp), %r13
jmp 0x55d2
movq %rax, %r15
jmp 0x55d2
movq %rax, %r15
leaq 0x20(%rsp), %r13
jmp 0x55ae
jmp 0x5594
movq %rax, %r15
jmp 0x55ae
movq %rax, %r15
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x55ae
callq 0x2220
movq 0x60(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x55d2
leaq 0x60(%rsp), %r12
movq (%rdi), %r14
callq 0x2220
movq %r14, %rdi
cmpq %r12, %r14
jne 0x55c2
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x55ec
movq (%rdi), %r14
callq 0x2220
movq %r14, %rdi
cmpq %r13, %r14
jne 0x55dc
movq (%rbx), %rdi
cmpq %rbx, %rdi
je 0x5604
movq (%rdi), %r14
callq 0x2220
movq %r14, %rdi
cmpq %rbx, %r14
jne 0x55f4
movq %r15, %rdi
callq 0x2400