// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%rsp)
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 0x3860
leaq 0xb0(%rsp), %rsi
movq %r12, %rdi
callq 0x6232
movl %eax, 0x2c(%rsp)
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x4deb
callq 0x2220
movslq 0x2c(%rsp), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0x28(%r12), %rsi
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x69aa
movq 0x30(%rsp), %r13
cmpq %r14, %r13
sete %al
je 0x50b1
movslq 0x8(%rsp), %r14
movq $0x0, 0x20(%rsp)
movsd 0x33bf(%rip), %xmm0     # 0x81f0
movsd %xmm0, 0x10(%rsp)
movl $0x0, 0x1c(%rsp)
movb %al, 0x48(%rsp)
movslq 0x10(%r13), %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 0x54f3
leaq (%rsi,%rsi,2), %rcx
leaq (%rax,%rcx,8), %rsi
leaq 0x98(%rsp), %rdi
callq 0x3860
leaq 0xe0(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x3860
movslq 0x20(%r12), %rax
testq %rax, %rax
jle 0x4eda
movq 0x98(%rsp), %rcx
xorpd %xmm2, %xmm2
movq 0xe0(%rsp), %rdx
xorl %esi, %esi
movapd 0x3372(%rip), %xmm1     # 0x8230
movsd (%rcx,%rsi,8), %xmm0
subsd (%rdx,%rsi,8), %xmm0
andpd %xmm1, %xmm0
addsd %xmm0, %xmm2
incq %rsi
cmpq %rsi, %rax
jne 0x4ebe
jmp 0x4ede
xorpd %xmm2, %xmm2
movsd %xmm2, (%rsp)
movq 0xe0(%rsp), %rdi
testq %rdi, %rdi
je 0x4ef5
callq 0x2220
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x4f07
callq 0x2220
movl 0x10(%r13), %r15d
movq (%rbx), %rbp
movq %rbp, %rax
cmpq %rbx, %rbp
je 0x4f33
movq %rbp, %rax
cmpl %r15d, 0x10(%rax)
jne 0x4f2b
movsd 0x18(%rax), %xmm0
ucomisd (%rsp), %xmm0
je 0x4f33
movq (%rax), %rax
cmpq %rbx, %rax
jne 0x4f19
incl 0x1c(%rsp)
movl $0x4, %ecx
cmpq %rbx, %rax
jne 0x5095
movq 0x10(%rbx), %rax
testq %rax, %rax
sete %cl
cmpq %r14, %rax
setae %dl
orb %cl, %dl
jne 0x4f85
movsd (%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x4fc1
cmpq %rbx, %rbp
je 0x507b
movsd (%rsp), %xmm0
ucomisd 0x18(%rbp), %xmm0
jb 0x5004
movq (%rbp), %rbp
jmp 0x4f66
testq %rax, %rax
sete %cl
cmpq %r14, %rax
setne %dl
orb %cl, %dl
jne 0x4fcd
movsd (%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x507b
cmpq %rbx, %rbp
je 0x5054
movsd (%rsp), %xmm0
ucomisd 0x18(%rbp), %xmm0
jb 0x502d
movq (%rbp), %rbp
jmp 0x4fa6
movl $0x20, %edi
callq 0x2240
jmp 0x4fe0
testq %rax, %rax
jne 0x54e9
movl $0x20, %edi
callq 0x2240
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 0x5075
movl $0x20, %edi
callq 0x2240
movl %r15d, 0x10(%rax)
movsd (%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2060
incq 0x10(%rbx)
jmp 0x507b
movl $0x20, %edi
callq 0x2240
movl %r15d, 0x10(%rax)
movsd (%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 0x2380
movq %rbp, %rdi
callq 0x2220
movq 0x8(%rbx), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %ecx, %ecx
movl 0x1c(%rsp), %eax
cmpl 0x10(%r12), %eax
setae %cl
movq 0x20(%rsp), %rax
orb %cl, %al
movq %rax, 0x20(%rsp)
testb $0x3, %cl
jne 0x50d2
movq (%r13), %r13
leaq 0x30(%rsp), %rax
cmpq %rax, %r13
sete %al
jne 0x4e3f
jmp 0x50d6
movl $0x0, 0x1c(%rsp)
movsd 0x312f(%rip), %xmm0     # 0x81f0
movsd %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
jmp 0x50d6
movb 0x48(%rsp), %al
testb $0x1, %al
je 0x5482
cmpl $0x2, 0x14(%r12)
jae 0x510e
movq 0x30(%rsp), %rdi
leaq 0x30(%rsp), %r15
cmpq %r15, %rdi
je 0x54c6
movq (%rdi), %r14
callq 0x2220
movq %r14, %rdi
cmpq %r15, %r14
jne 0x50f9
jmp 0x54c6
movslq 0x8(%rsp), %r15
movl $0x1, %edi
leaq 0x80(%rsp), %r13
movl $0x1, 0x28(%rsp)
movq %r15, 0x48(%rsp)
cmpl $0x0, 0x24(%r12)
setne %r8b
je 0x5476
xorl %r9d, %r9d
movl %edi, 0x54(%rsp)
movl 0x2c(%rsp), %ecx
xorl %eax, %eax
xorl %r9d, %ecx
jle 0x5161
movl %ecx, %edx
movl %ecx, %esi
andl $0x1, %esi
addl %esi, %eax
shrl %edx
cmpl $0x1, %ecx
movl %edx, %ecx
ja 0x5151
cmpl %edi, %eax
jne 0x5461
movb %r8b, 0xf(%rsp)
movq %r9, 0x60(%rsp)
leaq (%r9,%r9,2), %rsi
shlq $0x3, %rsi
addq 0x28(%r12), %rsi
movq %r13, %rdi
callq 0x69aa
movq 0x80(%rsp), %r15
movl $0x10, 0x8(%rsp)
cmpq %r13, %r15
je 0x5404
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 0x54db
leaq (%rsi,%rsi,2), %rcx
leaq (%rax,%rcx,8), %rsi
leaq 0x68(%rsp), %rdi
callq 0x3860
leaq 0xc8(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x3860
movslq 0x20(%r12), %rax
testq %rax, %rax
jle 0x5232
movq 0x68(%rsp), %rcx
xorpd %xmm2, %xmm2
movq 0xc8(%rsp), %rdx
xorl %esi, %esi
movapd 0x301a(%rip), %xmm1     # 0x8230
movsd (%rcx,%rsi,8), %xmm0
subsd (%rdx,%rsi,8), %xmm0
andpd %xmm1, %xmm0
addsd %xmm0, %xmm2
incq %rsi
cmpq %rsi, %rax
jne 0x5216
jmp 0x5236
xorpd %xmm2, %xmm2
movsd %xmm2, (%rsp)
movq 0xc8(%rsp), %rdi
testq %rdi, %rdi
je 0x524d
callq 0x2220
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x525c
callq 0x2220
movl 0x10(%r15), %r14d
movq (%rbx), %rbp
movq %rbp, %rcx
cmpq %rbx, %rbp
je 0x5288
movq %rbp, %rcx
cmpl %r14d, 0x10(%rcx)
jne 0x5280
movsd 0x18(%rcx), %xmm0
ucomisd (%rsp), %xmm0
je 0x5288
movq (%rcx), %rcx
cmpq %rbx, %rcx
jne 0x526e
incl 0x1c(%rsp)
movl $0x12, %eax
cmpq %rbx, %rcx
jne 0x53ef
movq 0x10(%rbx), %rax
testq %rax, %rax
sete %cl
movq 0x48(%rsp), %rsi
cmpq %rsi, %rax
setae %dl
orb %cl, %dl
jne 0x52df
movsd (%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x531b
cmpq %rbx, %rbp
je 0x53d5
movsd (%rsp), %xmm0
ucomisd 0x18(%rbp), %xmm0
jb 0x535e
movq (%rbp), %rbp
jmp 0x52c0
testq %rax, %rax
sete %cl
cmpq %rsi, %rax
setne %dl
orb %cl, %dl
jne 0x5327
movsd (%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x53d5
cmpq %rbx, %rbp
je 0x53ae
movsd (%rsp), %xmm0
ucomisd 0x18(%rbp), %xmm0
jb 0x5387
movq (%rbp), %rbp
jmp 0x5300
movl $0x20, %edi
callq 0x2240
jmp 0x533a
testq %rax, %rax
jne 0x54e9
movl $0x20, %edi
callq 0x2240
movl %r14d, 0x10(%rax)
movsd (%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2060
incq 0x10(%rbx)
movsd (%rsp), %xmm0
jmp 0x53cf
movl $0x20, %edi
callq 0x2240
movl %r14d, 0x10(%rax)
movsd (%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2060
incq 0x10(%rbx)
jmp 0x53d5
movl $0x20, %edi
callq 0x2240
movl %r14d, 0x10(%rax)
movsd (%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 0x2380
movq %rbp, %rdi
callq 0x2220
movq 0x8(%rbx), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %eax, %eax
movl 0x1c(%rsp), %ecx
cmpl 0x10(%r12), %ecx
setae %al
movq 0x20(%rsp), %rcx
orb %al, %cl
movq %rcx, 0x20(%rsp)
cmpl $0x12, %eax
je 0x53f8
testl %eax, %eax
jne 0x5400
movq (%r15), %r15
jmp 0x5198
movl %eax, 0x8(%rsp)
cmpl $0x10, 0x8(%rsp)
jne 0x542f
movl 0x28(%rsp), %eax
incl %eax
xorl %ecx, %ecx
movl %eax, 0x28(%rsp)
cmpl 0x14(%r12), %eax
setae %cl
movq 0x20(%rsp), %rax
movl %ecx, 0x8(%rsp)
orb %cl, %al
movq %rax, 0x20(%rsp)
movq 0x80(%rsp), %rdi
cmpq %r13, %rdi
je 0x544c
movq (%rdi), %r14
callq 0x2220
movq %r14, %rdi
cmpq %r13, %r14
jne 0x543c
cmpl $0x0, 0x8(%rsp)
movl 0x54(%rsp), %edi
movb 0xf(%rsp), %r8b
movq 0x60(%rsp), %r9
jne 0x5476
incq %r9
movl 0x24(%r12), %eax
cmpq %rax, %r9
setb %r8b
jb 0x5144
incl %edi
testb $0x1, %r8b
je 0x512d
movq 0x30(%rsp), %rdi
leaq 0x30(%rsp), %r15
cmpq %r15, %rdi
je 0x54a1
movq (%rdi), %r14
callq 0x2220
movq %r14, %rdi
cmpq %r15, %r14
jne 0x5491
movq (%rbx), %rdi
cmpq %rbx, %rdi
sete %al
movq 0x20(%rsp), %rcx
orb %al, %cl
testb $0x1, %cl
jne 0x54c6
movq (%rdi), %r14
callq 0x2220
movq %r14, %rdi
cmpq %rbx, %r14
jne 0x54b6
movq %rbx, %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2ea3(%rip), %rdi      # 0x8385
xorl %eax, %eax
callq 0x2320
movl $0x1, %edi
callq 0x2290
leaq 0x2e8b(%rip), %rdi      # 0x8385
xorl %eax, %eax
callq 0x2320
jmp 0x5544
jmp 0x5544
jmp 0x551e
movq %rax, %r14
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x558c
callq 0x2220
jmp 0x558c
movq %rax, %r14
jmp 0x558c
jmp 0x5544
jmp 0x554d
jmp 0x5544
jmp 0x5544
movq %rax, %r14
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x556d
callq 0x2220
jmp 0x556d
jmp 0x554d
movq %rax, %r14
jmp 0x556d
jmp 0x554d
jmp 0x554d
movq %rax, %r14
movq 0x80(%rsp), %rdi
cmpq %r13, %rdi
je 0x556d
movq (%rdi), %r15
callq 0x2220
movq %r15, %rdi
cmpq %r13, %r15
jne 0x555d
leaq 0x30(%rsp), %r12
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x558c
movq (%rdi), %r15
callq 0x2220
movq %r15, %rdi
cmpq %r12, %r15
jne 0x557c
movq (%rbx), %rdi
cmpq %rbx, %rdi
je 0x55a4
movq (%rdi), %r15
callq 0x2220
movq %r15, %rdi
cmpq %rbx, %r15
jne 0x5594
movq %r14, %rdi
callq 0x2400
movq %rax, %r14
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x5550
callq 0x2220
jmp 0x5550