// File: /workspace/github_cpp_makefile_star2_O2/nikosgalanis[P]Vectors-Similarity-Search-and-Clustering/programs/LSH/lsh_main.cc
// Function: main
int main(int argc, char* argv[]) {
	// first step: parse the arguments
	char* input_file = NULL, *query_file = NULL, *output_file = NULL;
	int k, L, n_neighbors, radius;
	double sum1 = 0, sum2 = 0;
	
	// parse the comand line arguments given
	parse_lsh_args(argc, argv, &input_file, &output_file, &query_file,
	&k, &L, &n_neighbors, &radius);
	// parse the dataset in order to get a vector of our feature vector
	vector<vector<double>> feature_vectors = parse_input(input_file);

	// parse the query set in order to find all of the queries that the user wants to ask
	vector<vector<double>> query_vectors = parse_input(query_file);

	// make sure that there is at least one element in the dataset and the queryset
	assert(feature_vectors.size() && query_vectors.size());
	
	// create the output file that we are going to use to print the LSH results
	ofstream output;
	output.open(output_file);

	// lsh initialization values that we've learned from our dataset
	int n_points = feature_vectors.size();
	int space_dimension = feature_vectors.at(1).size();

	// defalt lsh values, that we've learned for theory
	uint64_t m = pow(2,32) - 5;
	uint32_t M = 256;
	uint32_t w = our_math::compute_w_value(feature_vectors, 1000);
	uint32_t c = 1; 
	int correct_computed = 0;
	// initialize our LSH class with the deature vectors and the aprropriate given values
	LSH<double> lsh_instant(L, m, M, n_points, k, space_dimension, w, feature_vectors);

	// initialize our brute force class, in order to find the true distance of the query and its neighbors
	BruteForce<double> bf_instant(n_points, space_dimension, feature_vectors);

	// run all the queries with LSH
	for (uint64_t i = 0; i < query_vectors.size(); i++) {
		// output the query number in the file
		output << "Query: " << i << endl;

		auto k_start = std::chrono::high_resolution_clock::now();
		// run the k-nearest neighbor algorithm, in order to obtain n neighbors
		list<pair<int,double>> kNNs = lsh_instant.kNearestNeighbour(query_vectors.at(i), n_neighbors);
		auto k_end = std::chrono::high_resolution_clock::now();

		duration<double> knn_time_span = duration_cast<duration<double>>(k_end - k_start);  
		
		auto b_start = std::chrono::high_resolution_clock::now();
		// run the brute force algorithm, in order to obtain the true nearest neighbors
		list<pair<int, double>> kBF = bf_instant.kNeighboursBF(query_vectors.at(i), n_neighbors);
		auto b_end = std::chrono::high_resolution_clock::now();

		duration<double> bf_time_span = duration_cast<duration<double>>(b_end - b_start);  

		// denote the number of neighbor we are dealing with
		int k_value = 0;

		// itterate the list of results of the knn
		list<pair<int,double>>::iterator knn_it = kNNs.begin();
		list<pair<int,double>>::iterator bf_it = kBF.begin();

		for (; knn_it != kNNs.end() && bf_it != kBF.end() ; knn_it++, bf_it++) {
			// hold the results of the knn here
			auto knn_tuple = knn_it;
			auto bf_tuple = bf_it;
			// output the findings of kNN
			output << "Nearest neighbor-" << k_value + 1 << ": " << knn_tuple->first << endl;
			// by the LSH
			output << "DistanceLSH:" << knn_tuple->second << endl;
			// and by brute force
			output << "DistanceTrue:" << bf_tuple->second << endl;
			if (knn_tuple->second == bf_tuple->second) {
				correct_computed++;
			}

			k_value++;
		}
		
		// Print the time elapsed while computing the neighbors with kNN and BF
		output << "tLSH: " << knn_time_span.count() << endl;
		output << "tTrue: " << bf_time_span.count() << endl;
		sum1 += knn_time_span.count();
		sum2 += bf_time_span.count();

		// RUn the Range Search algorithm
		list<pair<int, double>> range_search = lsh_instant.RangeSearch(query_vectors.at(i), radius, c);

		// print in the output file all of our findings
		output << "R-near neighbors:" << endl;
		for (list<pair<int,double>>::iterator it = range_search.begin(); it != range_search.end(); it++) {
			output << it->first << endl;
		}
		
		output << "\n\n\n";
	}
	cout << "mean lsh time " << sum1 / query_vectors.size() << endl;
	cout << "mean bf time " << sum2 / query_vectors.size() << endl;
	cout << "corectly computed neighbours " << correct_computed  << " out of " << n_neighbors * query_vectors.size() << endl;

	output.close();
	
	free(input_file);
	free(query_file);
	free(output_file);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
leaq 0x80(%rsp), %rbx
andq $0x0, (%rbx)
leaq 0x78(%rsp), %r8
andq $0x0, (%r8)
leaq 0x70(%rsp), %rcx
andq $0x0, (%rcx)
subq $0x8, %rsp
leaq 0x44(%rsp), %rax
leaq 0xc(%rsp), %r10
leaq 0x48(%rsp), %r11
leaq 0x4c(%rsp), %r9
movq %rbx, %rdx
pushq %rax
pushq %r10
pushq %r11
callq 0x2657
addq $0x20, %rsp
movq (%rbx), %rsi
leaq 0x140(%rsp), %rdi
leaq 0x228(%rsp), %rdx
callq 0x20a0
leaq 0x58(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x24ac
leaq 0x140(%rsp), %rdi
callq 0x20c0
movq 0x78(%rsp), %rsi
leaq 0x120(%rsp), %rdi
leaq 0x228(%rsp), %rdx
callq 0x20a0
leaq 0x8(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x24ac
leaq 0x120(%rsp), %rdi
callq 0x20c0
movq 0x60(%rsp), %rax
cmpq 0x58(%rsp), %rax
je 0x398f
movq 0x10(%rsp), %rax
cmpq 0x8(%rsp), %rax
je 0x398f
leaq 0x228(%rsp), %rdi
callq 0x2310
movq 0x70(%rsp), %rsi
leaq 0x228(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x2030
leaq 0x58(%rsp), %rdi
movq 0x8(%rdi), %rax
subq (%rdi), %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
movq %rax, %rbx
pushq $0x1
popq %rsi
callq 0x3af2
movq 0x8(%rax), %r12
subq (%rax), %r12
leaq 0xb8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x3c32
leaq 0xb8(%rsp), %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x3b10
movsd %xmm0, 0x20(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x2e8e
movl 0x40(%rsp), %r14d
movl 0x44(%rsp), %ebp
leaq 0x108(%rsp), %r15
leaq 0x58(%rsp), %rsi
movq %r15, %rdi
callq 0x3c32
shrq $0x3, %r12
cvttsd2si 0x20(%rsp), %rax
subq $0x8, %rsp
leaq 0x1c8(%rsp), %rdi
movl $0xfffffffb, %edx       # imm = 0xFFFFFFFB
movl %r14d, %esi
movl $0x100, %ecx            # imm = 0x100
movl %ebx, %r8d
movl %ebp, %r9d
pushq %r15
pushq %rax
pushq %r12
callq 0x3c8c
addq $0x20, %rsp
leaq 0x108(%rsp), %rdi
callq 0x2e8e
leaq 0xa0(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x3c32
leaq 0xf0(%rsp), %rdi
movl %ebx, -0x8(%rdi)
movl %r12d, -0x4(%rdi)
leaq 0xa0(%rsp), %rsi
callq 0x3c32
leaq 0xa0(%rsp), %rdi
callq 0x2e8e
xorpd %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
xorl %r14d, %r14d
leaq 0x228(%rsp), %r12
leaq 0x190(%rsp), %r13
movsd %xmm0, 0x30(%rsp)
xorl %ebp, %ebp
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
cmpq %rax, %rbp
jae 0x37fa
movq %r12, %rdi
leaq 0x2d6c(%rip), %rsi      # 0x6277
callq 0x21b0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2140
movq %rax, %rdi
callq 0x20d0
callq 0x2040
movq %rax, %r15
leaq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x3af2
leaq 0x1a8(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3152
movl 0x4(%rsp), %ecx
leaq 0x88(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
movq %rbx, %rdx
callq 0x3fd2
movq %rbx, %rdi
callq 0x2f06
callq 0x2040
subq %r15, %rax
cvtsi2sd %rax, %xmm0
mulsd 0x2c6c(%rip), %xmm0     # 0x61f0
movsd %xmm0, 0x20(%rsp)
callq 0x2040
movq %rax, %r15
leaq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x3af2
leaq 0x178(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3152
movq %rbp, 0x50(%rsp)
movl 0x4(%rsp), %ecx
movq %r13, %rdi
leaq 0xe8(%rsp), %rsi
movq %rbx, %rdx
callq 0x4284
movq %rbx, %rdi
callq 0x2f06
callq 0x2040
subq %r15, %rax
cvtsi2sd %rax, %xmm0
mulsd 0x2c05(%rip), %xmm0     # 0x61f0
movsd %xmm0, 0x48(%rsp)
pushq $0x1
popq %r15
movq %r13, %rbp
leaq 0x88(%rsp), %rbx
movq (%rbx), %rbx
movq (%r13), %r13
cmpq %rbp, %r13
setne %al
leaq 0x88(%rsp), %rcx
cmpq %rcx, %rbx
je 0x36c6
testb %al, %al
je 0x36c6
movq %r12, %rdi
leaq 0x2c4f(%rip), %rsi      # 0x627f
callq 0x21b0
movq %rax, %rdi
movl %r15d, %esi
callq 0x2330
movq %rax, %rdi
leaq 0x2c66(%rip), %rsi      # 0x62b0
callq 0x21b0
movl 0x10(%rbx), %esi
movq %rax, %rdi
callq 0x2330
movq %rax, %rdi
callq 0x20d0
movq %r12, %rdi
leaq 0x2c25(%rip), %rsi      # 0x6291
callq 0x21b0
movsd 0x18(%rbx), %xmm0
movq %rax, %rdi
callq 0x22a0
movq %rax, %rdi
callq 0x20d0
movq %r12, %rdi
leaq 0x2c0e(%rip), %rsi      # 0x629e
callq 0x21b0
movsd 0x18(%r13), %xmm0
movq %rax, %rdi
callq 0x22a0
movq %rax, %rdi
callq 0x20d0
movsd 0x18(%rbx), %xmm0
xorl %eax, %eax
ucomisd 0x18(%r13), %xmm0
sete %al
addl %eax, %r14d
incl %r15d
jmp 0x3600
movq %r12, %rdi
leaq 0x2bdc(%rip), %rsi      # 0x62ac
callq 0x21b0
movq %rax, %rdi
movsd 0x20(%rsp), %xmm0
callq 0x22a0
movq %rax, %rdi
callq 0x20d0
movq %r12, %rdi
leaq 0x2bbe(%rip), %rsi      # 0x62b3
callq 0x21b0
movq %rax, %rdi
movsd 0x48(%rsp), %xmm0
callq 0x22a0
movq %rax, %rdi
callq 0x20d0
leaq 0x8(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x3af2
leaq 0x160(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3152
cvtsi2sdl 0x3c(%rsp), %xmm0
leaq 0xd0(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
movq %rbx, %rdx
pushq $0x1
popq %rcx
callq 0x4466
movq %rbp, %r13
movq %rbx, %rdi
callq 0x2f06
movq %r12, %rdi
leaq 0x2b53(%rip), %rsi      # 0x62bb
callq 0x21b0
leaq 0xd0(%rsp), %r15
movq 0x50(%rsp), %rbp
movq %rax, %rdi
callq 0x20d0
movsd 0x30(%rsp), %xmm0
addsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x28(%rsp), %xmm0
addsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movq %r15, %rbx
movq (%rbx), %rbx
cmpq %r15, %rbx
je 0x37c6
movl 0x10(%rbx), %esi
movq %r12, %rdi
callq 0x2330
movq %rax, %rdi
callq 0x20d0
jmp 0x37a9
movq %r12, %rdi
leaq 0x2afd(%rip), %rsi      # 0x62cd
callq 0x21b0
movq %r15, %rdi
callq 0x468c
movq %r13, %rdi
callq 0x468c
leaq 0x88(%rsp), %rdi
callq 0x468c
incq %rbp
jmp 0x34e6
movq 0x67d7(%rip), %rdi      # 0x9fd8
leaq 0x2ac9(%rip), %rsi      # 0x62d1
callq 0x21b0
movq %rax, %rdi
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
movq %rax, %xmm0
punpckldq 0x2991(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2999(%rip), %xmm0     # 0x61d0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
divsd %xmm1, %xmm0
callq 0x22a0
movq %rax, %rdi
callq 0x20d0
movq 0x6777(%rip), %rdi      # 0x9fd8
leaq 0x2a78(%rip), %rsi      # 0x62e0
callq 0x21b0
movq %rax, %rdi
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
movq %rax, %xmm0
punpckldq 0x2931(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2939(%rip), %xmm0     # 0x61d0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
divsd %xmm1, %xmm0
callq 0x22a0
movq %rax, %rdi
callq 0x20d0
movq 0x6717(%rip), %rdi      # 0x9fd8
leaq 0x2a26(%rip), %rsi      # 0x62ee
callq 0x21b0
movq %rax, %rdi
movl %r14d, %esi
callq 0x2330
leaq 0x2a2d(%rip), %rsi      # 0x630c
movq %rax, %rdi
callq 0x21b0
movq %rax, %rdi
movslq 0x4(%rsp), %rsi
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
imulq %rax, %rsi
callq 0x2140
movq %rax, %rdi
callq 0x20d0
leaq 0x228(%rsp), %rdi
callq 0x2230
movq 0x80(%rsp), %rdi
callq 0x21e0
movq 0x78(%rsp), %rdi
callq 0x21e0
movq 0x70(%rsp), %rdi
callq 0x21e0
leaq 0xf0(%rsp), %rdi
callq 0x2e8e
leaq 0x1c0(%rsp), %rdi
callq 0x4668
leaq 0x228(%rsp), %rdi
callq 0x2260
leaq 0x8(%rsp), %rdi
callq 0x2e8e
leaq 0x58(%rsp), %rdi
callq 0x2e8e
xorl %eax, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2882(%rip), %rdi      # 0x6218
leaq 0x28aa(%rip), %rsi      # 0x6247
leaq 0x28bc(%rip), %rcx      # 0x6260
pushq $0x25
popq %rdx
callq 0x2130
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
jmp 0x3ab7
movq %rax, %rbx
jmp 0x3abc
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
jmp 0x39de
jmp 0x39ec
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x2e8e
jmp 0x3ac9
jmp 0x39ec
jmp 0x39ec
movq %rax, %rbx
jmp 0x3ac9
movq %rax, %rbx
jmp 0x3ad6
movq %rax, %rbx
leaq 0x120(%rsp), %rdi
callq 0x20c0
jmp 0x3ae0
movq %rax, %rbx
jmp 0x3ae0
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x20c0
jmp 0x3aea
movq %rax, %rbx
jmp 0x3aea
jmp 0x3a79
movq %rax, %rbx
leaq 0x160(%rsp), %rdi
callq 0x2f06
jmp 0x3a95
movq %rax, %rbx
leaq 0x178(%rsp), %rdi
callq 0x2f06
jmp 0x3aa2
movq %rax, %rbx
leaq 0x1a8(%rsp), %rdi
callq 0x2f06
jmp 0x3aaf
jmp 0x3a92
movq %rax, %rbx
jmp 0x3aa2
jmp 0x3a79
jmp 0x3a80
movq %rax, %rbx
jmp 0x3aaf
jmp 0x3a92
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x468c
jmp 0x3a95
movq %rax, %rbx
leaq 0x190(%rsp), %rdi
callq 0x468c
leaq 0x88(%rsp), %rdi
callq 0x468c
leaq 0xf0(%rsp), %rdi
callq 0x2e8e
leaq 0x1c0(%rsp), %rdi
callq 0x4668
leaq 0x228(%rsp), %rdi
callq 0x2260
leaq 0x8(%rsp), %rdi
callq 0x2e8e
leaq 0x58(%rsp), %rdi
callq 0x2e8e
movq %rbx, %rdi
callq 0x2340