// File: nikosgalanis[P]Vectors-Similarity-Search-and-Clustering/programs/Hypercube/../../algorithms/Search/Hypercube/headers/hypercube.h
// Function: Hypercube<double>::RangeSearch[abi:cxx11](std::vector<double, std::allocator<double>>, double, int)
list<pair<int,T>> RangeSearch(vector<T> query_vector, double radius, int c) {
			list<pair<int,T>> result;

			// store the visited vectors, to find out when we do reach the threshold
			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];
			// 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++) {
				// increase the visited items
				visited++;
				
				// calculate the distance between the selected vector and our query vector
				T distance = metrics::ManhatanDistance(feature_vectors.at(*bucket_it), query_vector, space_dim);
				
				// create the new pair
				pair<int, T> new_pair = make_pair(*bucket_it, distance);

				// if the distance is smaller than the radius, push it in our result list
				if (distance < (T)(radius * c)) {
					result.push_back(new_pair);
				}

				// 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];
							// 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++) {
								// increase the visited items
								visited++;
								
								// calculate the distance between the selected vector and our query vector
								T distance = metrics::ManhatanDistance(feature_vectors.at(*bucket_it), query_vector, space_dim);
								
								// create the new pair
								pair<int, T> new_pair = make_pair(*bucket_it, distance);

								// if the distance is smaller than the radius, push it in our result list
								if (distance < (T)(radius * c)) {
									result.push_back(new_pair);
								}

								// 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:
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rdx, 0x70(%rsp)
movq %rdi, 0x58(%rsp)
movq %rdi, %rax
movq %rax, 0x60(%rsp)
movq %rdi, 0x1d0(%rsp)
movq %rsi, 0x1c8(%rsp)
movq %rdx, 0x1c0(%rsp)
movsd %xmm0, 0x1b8(%rsp)
movl %ecx, 0x1b4(%rsp)
movq 0x1c8(%rsp), %rax
movq %rax, 0x68(%rsp)
movb $0x0, 0x1b3(%rsp)
callq 0x92c0
movq 0x70(%rsp), %rsi
movl $0x0, 0x1ac(%rsp)
movl $0x0, 0x1a8(%rsp)
leaq 0x188(%rsp), %rdi
callq 0x4560
jmp 0x8335
movq 0x68(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0xa0c0
movl %eax, 0x54(%rsp)
jmp 0x834d
leaq 0x188(%rsp), %rdi
callq 0x3510
movl 0x54(%rsp), %eax
movq 0x68(%rsp), %rdi
movl %eax, 0x1a4(%rsp)
addq $0x28, %rdi
movslq 0x1a4(%rsp), %rsi
callq 0xa2e0
movq %rax, %rsi
leaq 0x160(%rsp), %rdi
callq 0xc4d0
jmp 0x838d
leaq 0x160(%rsp), %rdi
callq 0x11990
movq %rax, 0x158(%rsp)
leaq 0x160(%rsp), %rdi
callq 0xc8c0
movq %rax, 0x150(%rsp)
leaq 0x158(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0x119c0
testb $0x1, %al
jne 0x83d5
jmp 0x85f7
movq 0x68(%rsp), %rax
movl 0x1ac(%rsp), %ecx
incl %ecx
movl %ecx, 0x1ac(%rsp)
addq $0x40, %rax
movq %rax, 0x40(%rsp)
leaq 0x158(%rsp), %rdi
callq 0x119e0
movq 0x40(%rsp), %rdi
movslq (%rax), %rsi
callq 0x6640
movq %rax, 0x48(%rsp)
jmp 0x8414
movq 0x48(%rsp), %rsi
leaq 0x130(%rsp), %rdi
callq 0x4560
jmp 0x8428
movq 0x70(%rsp), %rsi
leaq 0x118(%rsp), %rdi
callq 0x4560
jmp 0x843c
movq 0x68(%rsp), %rax
movl 0x20(%rax), %edx
leaq 0x130(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x92d0
movsd %xmm0, 0x38(%rsp)
jmp 0x8461
leaq 0x118(%rsp), %rdi
callq 0x3510
leaq 0x130(%rsp), %rdi
callq 0x3510
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x148(%rsp)
leaq 0x158(%rsp), %rdi
callq 0x119e0
movq %rax, %rdi
leaq 0x148(%rsp), %rsi
callq 0x11a00
movsd %xmm0, 0x28(%rsp)
movl %eax, 0x34(%rsp)
jmp 0x84b3
movsd 0x28(%rsp), %xmm0
movl 0x34(%rsp), %eax
movl %eax, 0x108(%rsp)
movsd %xmm0, 0x110(%rsp)
movsd 0x148(%rsp), %xmm1
movsd 0x1b8(%rsp), %xmm0
cvtsi2sdl 0x1b4(%rsp), %xmm2
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x85b0
movq 0x58(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x9410
jmp 0x850a
jmp 0x85b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
jmp 0x898c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0x188(%rsp), %rdi
callq 0x3510
jmp 0x898c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
jmp 0x8966
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
jmp 0x859e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x3510
leaq 0x130(%rsp), %rdi
callq 0x3510
jmp 0x8966
movq 0x68(%rsp), %rcx
movl 0x1ac(%rsp), %eax
cmpl 0x10(%rcx), %eax
jb 0x85d9
movb $0x1, 0x1b3(%rsp)
movl $0x1, 0x104(%rsp)
jmp 0x894d
jmp 0x85db
leaq 0x158(%rsp), %rdi
xorl %esi, %esi
callq 0x11aa0
movq %rax, 0xf8(%rsp)
jmp 0x83a2
movq 0x68(%rsp), %rcx
movl 0x1a8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1a8(%rsp)
cmpl 0x14(%rcx), %eax
jb 0x862a
movb $0x1, 0x1b3(%rsp)
movl $0x1, 0x104(%rsp)
jmp 0x894d
movl $0x1, 0xf4(%rsp)
jmp 0x8637
movl $0x0, 0xf0(%rsp)
movq 0x68(%rsp), %rcx
movl 0xf0(%rsp), %eax
cmpl 0x24(%rcx), %eax
jae 0x8937
movl 0xf0(%rsp), %edi
movl 0x1a4(%rsp), %esi
callq 0x11ad0
cmpl 0xf4(%rsp), %eax
jne 0x88ef
movq 0x68(%rsp), %rdi
addq $0x28, %rdi
movl 0xf0(%rsp), %eax
movl %eax, %esi
callq 0xa2e0
movq %rax, %rsi
leaq 0xd8(%rsp), %rdi
callq 0xc4d0
jmp 0x86a0
leaq 0xd8(%rsp), %rdi
callq 0x11990
movq %rax, 0xd0(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0xc8c0
movq %rax, 0xc8(%rsp)
leaq 0xd0(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x119c0
testb $0x1, %al
jne 0x86e8
jmp 0x88b9
movq 0x68(%rsp), %rax
movl 0x1ac(%rsp), %ecx
incl %ecx
movl %ecx, 0x1ac(%rsp)
addq $0x40, %rax
movq %rax, 0x18(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x119e0
movq 0x18(%rsp), %rdi
movslq (%rax), %rsi
callq 0x6640
movq %rax, 0x20(%rsp)
jmp 0x8727
movq 0x20(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
callq 0x4560
jmp 0x873b
movq 0x70(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0x4560
jmp 0x874f
movq 0x68(%rsp), %rax
movl 0x20(%rax), %edx
leaq 0xa8(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x92d0
movsd %xmm0, 0x10(%rsp)
jmp 0x8774
leaq 0x90(%rsp), %rdi
callq 0x3510
leaq 0xa8(%rsp), %rdi
callq 0x3510
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x119e0
movq %rax, %rdi
leaq 0xc0(%rsp), %rsi
callq 0x11a00
movsd %xmm0, (%rsp)
movl %eax, 0xc(%rsp)
jmp 0x87c5
movsd (%rsp), %xmm0
movl 0xc(%rsp), %eax
movl %eax, 0x80(%rsp)
movsd %xmm0, 0x88(%rsp)
movsd 0xc0(%rsp), %xmm1
movsd 0x1b8(%rsp), %xmm0
cvtsi2sdl 0x1b4(%rsp), %xmm2
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x8878
movq 0x58(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x9410
jmp 0x8817
jmp 0x8878
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
jmp 0x88e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
jmp 0x8869
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x3510
leaq 0xa8(%rsp), %rdi
callq 0x3510
jmp 0x88e0
movq 0x68(%rsp), %rcx
movl 0x1ac(%rsp), %eax
cmpl 0x10(%rcx), %eax
jb 0x889e
movb $0x1, 0x1b3(%rsp)
movl $0x1, 0x104(%rsp)
jmp 0x88c4
jmp 0x88a0
leaq 0xd0(%rsp), %rdi
xorl %esi, %esi
callq 0x11aa0
movq %rax, 0x78(%rsp)
jmp 0x86b5
movl $0x0, 0x104(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x9e60
movl 0x104(%rsp), %eax
testl %eax, %eax
jne 0x894d
jmp 0x88de
jmp 0x88ef
leaq 0xd8(%rsp), %rdi
callq 0x9e60
jmp 0x8966
movq 0x68(%rsp), %rcx
movl 0x1a8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1a8(%rsp)
cmpl 0x14(%rcx), %eax
jb 0x891f
movb $0x1, 0x1b3(%rsp)
movl $0x1, 0x104(%rsp)
jmp 0x894d
jmp 0x8921
movl 0xf0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf0(%rsp)
jmp 0x8642
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
jmp 0x8635
leaq 0x160(%rsp), %rdi
callq 0x9e60
testb $0x1, 0x1b3(%rsp)
jne 0x897f
jmp 0x8975
leaq 0x160(%rsp), %rdi
callq 0x9e60
jmp 0x898c
movq 0x58(%rsp), %rdi
callq 0x89b0
movq 0x60(%rsp), %rax
addq $0x1d8, %rsp            # imm = 0x1D8
retq
movq 0x58(%rsp), %rdi
callq 0x89b0
movq 0x180(%rsp), %rdi
callq 0x23c0
nopw %cs:(%rax,%rax)
nopl (%rax)