// File: /workspace/github_cpp_makefile_star2_O2/nikosgalanis[P]Vectors-Similarity-Search-and-Clustering/programs/Hypercube/hypercube_main.cc
// Function: main
int main(int argc, char* argv[]) {
    // parsing the arguments

    char* input_file = NULL, *query_file = NULL, *output_file = NULL;
	int k, M, probes, n_neighbors, radius;
	double sum1 = 0, sum2 = 0;

	parse_hc_args(argc, argv, &input_file, &output_file, &query_file, &k, &M, &probes, &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 Hypercube results
	ofstream output;
	output.open(output_file);

    // default hypercube values
    uint64_t m = pow(2,32) - 5;
    uint32_t w = 20 * our_math::compute_w_value(feature_vectors, 1000);
	uint32_t c = 1; 

    int correct_computed = 0;

    int n_points = feature_vectors.size();
	int space_dimension = feature_vectors.at(0).size();

    // Initialize hypercube class
    Hypercube<double> hypercube_instant(k, m, M, probes, n_points, w, space_dimension, 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 Hypercube
    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 = hypercube_instant.kNearestNeighbour(query_vectors.at(i), n_neighbors);
		auto k_end = std::chrono::high_resolution_clock::now();

		duration<double> hc_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() ; bf_it++, knn_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 Hypercube
			output << "DistanceHypercube:" << 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 << "tHC: " << hc_time_span.count() << endl;
		output << "tTrue: " << bf_time_span.count() << endl;
		sum1 += hc_time_span.count();
		sum2 += bf_time_span.count();

		// RUn the Range Search algorithm
		list<pair<int, double>> range_search = hypercube_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 hc 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 $0x448, %rsp            # imm = 0x448
leaq 0x80(%rsp), %rbx
andq $0x0, (%rbx)
leaq 0x78(%rsp), %r8
andq $0x0, (%r8)
leaq 0x70(%rsp), %rcx
andq $0x0, (%rcx)
leaq 0x38(%rsp), %rax
leaq 0x4(%rsp), %r10
leaq 0x3c(%rsp), %r11
leaq 0x40(%rsp), %r14
leaq 0x44(%rsp), %r9
movq %rbx, %rdx
pushq %rax
pushq %r10
pushq %r11
pushq %r14
callq 0x289e
addq $0x20, %rsp
movq (%rbx), %rsi
leaq 0x140(%rsp), %rdi
leaq 0x248(%rsp), %rdx
callq 0x20a0
leaq 0x58(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x24fc
leaq 0x140(%rsp), %rdi
callq 0x20c0
movq 0x78(%rsp), %rsi
leaq 0x120(%rsp), %rdi
leaq 0x248(%rsp), %rdx
callq 0x20a0
leaq 0x8(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x24fc
leaq 0x120(%rsp), %rdi
callq 0x20c0
movq 0x60(%rsp), %rax
cmpq 0x58(%rsp), %rax
je 0x39ef
movq 0x10(%rsp), %rax
cmpq 0x8(%rsp), %rax
je 0x39ef
leaq 0x248(%rsp), %rdi
callq 0x2350
movq 0x70(%rsp), %rsi
leaq 0x248(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x2030
leaq 0xb8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x3c74
leaq 0xb8(%rsp), %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x3b52
movsd %xmm0, 0x20(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x2ede
leaq 0x58(%rsp), %rdi
movq 0x8(%rdi), %rax
subq (%rdi), %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
movq %rax, %rbx
xorl %esi, %esi
callq 0x3cce
movq 0x8(%rax), %r13
subq (%rax), %r13
movl 0x44(%rsp), %ebp
movl 0x40(%rsp), %r14d
movl 0x3c(%rsp), %r15d
leaq 0x108(%rsp), %r12
leaq 0x58(%rsp), %rsi
movq %r12, %rdi
callq 0x3c74
movsd 0x20(%rsp), %xmm0
mulsd 0x3d1a(%rip), %xmm0     # 0x71c0
cvttsd2si %xmm0, %rax
shrq $0x3, %r13
subq $0x8, %rsp
leaq 0x1c8(%rsp), %rdi
movl $0xfffffffb, %edx       # imm = 0xFFFFFFFB
movl %ebp, %esi
movl %r14d, %ecx
movl %r15d, %r8d
movl %ebx, %r9d
pushq %r12
pushq %r13
pushq %rax
callq 0x3cec
addq $0x20, %rsp
leaq 0x108(%rsp), %rdi
callq 0x2ede
leaq 0xa0(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x3c74
leaq 0xf0(%rsp), %rdi
movl %ebx, -0x8(%rdi)
movl %r13d, -0x4(%rdi)
leaq 0xa0(%rsp), %rsi
callq 0x3c74
leaq 0xa0(%rsp), %rdi
callq 0x2ede
xorpd %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
xorl %r14d, %r14d
leaq 0x248(%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 0x385a
movq %r12, %rdi
leaq 0x3d28(%rip), %rsi      # 0x7293
callq 0x21d0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2150
movq %rax, %rdi
callq 0x20d0
callq 0x2040
movq %rax, %r15
leaq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x3cce
leaq 0x1a8(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x31a2
movl 0x4(%rsp), %ecx
leaq 0x88(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
movq %rbx, %rdx
callq 0x40ba
movq %rbx, %rdi
callq 0x2f56
callq 0x2040
subq %r15, %rax
cvtsi2sd %rax, %xmm0
mulsd 0x3be4(%rip), %xmm0     # 0x71c8
movsd %xmm0, 0x20(%rsp)
callq 0x2040
movq %rax, %r15
leaq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x3cce
leaq 0x178(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x31a2
movq %rbp, 0x50(%rsp)
movl 0x4(%rsp), %ecx
movq %r13, %rdi
leaq 0xe8(%rsp), %rsi
movq %rbx, %rdx
callq 0x4590
movq %rbx, %rdi
callq 0x2f56
callq 0x2040
subq %r15, %rax
cvtsi2sd %rax, %xmm0
mulsd 0x3b7d(%rip), %xmm0     # 0x71c8
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 0x3726
testb %al, %al
je 0x3726
movq %r12, %rdi
leaq 0x3c0b(%rip), %rsi      # 0x729b
callq 0x21d0
movq %rax, %rdi
movl %r15d, %esi
callq 0x2370
movq %rax, %rdi
leaq 0x3c27(%rip), %rsi      # 0x72d1
callq 0x21d0
movl 0x10(%rbx), %esi
movq %rax, %rdi
callq 0x2370
movq %rax, %rdi
callq 0x20d0
movq %r12, %rdi
leaq 0x3be1(%rip), %rsi      # 0x72ad
callq 0x21d0
movsd 0x18(%rbx), %xmm0
movq %rax, %rdi
callq 0x22e0
movq %rax, %rdi
callq 0x20d0
movq %r12, %rdi
leaq 0x3bd0(%rip), %rsi      # 0x72c0
callq 0x21d0
movsd 0x18(%r13), %xmm0
movq %rax, %rdi
callq 0x22e0
movq %rax, %rdi
callq 0x20d0
movsd 0x18(%rbx), %xmm0
xorl %eax, %eax
ucomisd 0x18(%r13), %xmm0
sete %al
addl %eax, %r14d
incl %r15d
jmp 0x3660
movq %r12, %rdi
leaq 0x3b9e(%rip), %rsi      # 0x72ce
callq 0x21d0
movq %rax, %rdi
movsd 0x20(%rsp), %xmm0
callq 0x22e0
movq %rax, %rdi
callq 0x20d0
movq %r12, %rdi
leaq 0x3b7f(%rip), %rsi      # 0x72d4
callq 0x21d0
movq %rax, %rdi
movsd 0x48(%rsp), %xmm0
callq 0x22e0
movq %rax, %rdi
callq 0x20d0
leaq 0x8(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x3cce
leaq 0x160(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x31a2
cvtsi2sdl 0x38(%rsp), %xmm0
leaq 0xd0(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
movq %rbx, %rdx
pushq $0x1
popq %rcx
callq 0x4772
movq %rbp, %r13
movq %rbx, %rdi
callq 0x2f56
movq %r12, %rdi
leaq 0x3b14(%rip), %rsi      # 0x72dc
callq 0x21d0
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 0x3826
movl 0x10(%rbx), %esi
movq %r12, %rdi
callq 0x2370
movq %rax, %rdi
callq 0x20d0
jmp 0x3809
movq %r12, %rdi
leaq 0x3abe(%rip), %rsi      # 0x72ee
callq 0x21d0
movq %r15, %rdi
callq 0x4af4
movq %r13, %rdi
callq 0x4af4
leaq 0x88(%rsp), %rdi
callq 0x4af4
incq %rbp
jmp 0x3546
movq 0x7777(%rip), %rdi      # 0xafd8
leaq 0x3a8a(%rip), %rsi      # 0x72f2
callq 0x21d0
movq %rax, %rdi
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
movq %rax, %xmm0
punpckldq 0x3961(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x3969(%rip), %xmm0     # 0x7200
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
divsd %xmm1, %xmm0
callq 0x22e0
movq %rax, %rdi
callq 0x20d0
movq 0x7717(%rip), %rdi      # 0xafd8
leaq 0x3a38(%rip), %rsi      # 0x7300
callq 0x21d0
movq %rax, %rdi
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
movq %rax, %xmm0
punpckldq 0x3901(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x3909(%rip), %xmm0     # 0x7200
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
divsd %xmm1, %xmm0
callq 0x22e0
movq %rax, %rdi
callq 0x20d0
movq 0x76b7(%rip), %rdi      # 0xafd8
leaq 0x39e6(%rip), %rsi      # 0x730e
callq 0x21d0
movq %rax, %rdi
movl %r14d, %esi
callq 0x2370
leaq 0x39ed(%rip), %rsi      # 0x732c
movq %rax, %rdi
callq 0x21d0
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 0x2150
movq %rax, %rdi
callq 0x20d0
leaq 0x248(%rsp), %rdi
callq 0x2270
movq 0x80(%rsp), %rdi
callq 0x2210
movq 0x78(%rsp), %rdi
callq 0x2210
movq 0x70(%rsp), %rdi
callq 0x2210
leaq 0xf0(%rsp), %rdi
callq 0x2ede
leaq 0x1c0(%rsp), %rdi
callq 0x4ac8
leaq 0x248(%rsp), %rdi
callq 0x22a0
leaq 0x8(%rsp), %rdi
callq 0x2ede
leaq 0x58(%rsp), %rdi
callq 0x2ede
xorl %eax, %eax
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3832(%rip), %rdi      # 0x7228
leaq 0x385a(%rip), %rsi      # 0x7257
leaq 0x3878(%rip), %rcx      # 0x727c
pushq $0x25
popq %rdx
callq 0x2140
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
jmp 0x3b17
movq %rax, %rbx
jmp 0x3b1c
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
jmp 0x3a40
jmp 0x3a4c
jmp 0x3a4c
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x2ede
jmp 0x3b29
jmp 0x3a4c
movq %rax, %rbx
jmp 0x3b29
movq %rax, %rbx
jmp 0x3b36
movq %rax, %rbx
leaq 0x120(%rsp), %rdi
callq 0x20c0
jmp 0x3b40
movq %rax, %rbx
jmp 0x3b40
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x20c0
jmp 0x3b4a
movq %rax, %rbx
jmp 0x3b4a
jmp 0x3ad9
movq %rax, %rbx
leaq 0x160(%rsp), %rdi
callq 0x2f56
jmp 0x3af5
movq %rax, %rbx
leaq 0x178(%rsp), %rdi
callq 0x2f56
jmp 0x3b02
movq %rax, %rbx
leaq 0x1a8(%rsp), %rdi
callq 0x2f56
jmp 0x3b0f
jmp 0x3af2
movq %rax, %rbx
jmp 0x3b02
jmp 0x3ad9
jmp 0x3ae0
movq %rax, %rbx
jmp 0x3b0f
jmp 0x3af2
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x4af4
jmp 0x3af5
movq %rax, %rbx
leaq 0x190(%rsp), %rdi
callq 0x4af4
leaq 0x88(%rsp), %rdi
callq 0x4af4
leaq 0xf0(%rsp), %rdi
callq 0x2ede
leaq 0x1c0(%rsp), %rdi
callq 0x4ac8
leaq 0x248(%rsp), %rdi
callq 0x22a0
leaq 0x8(%rsp), %rdi
callq 0x2ede
leaq 0x58(%rsp), %rdi
callq 0x2ede
movq %rbx, %rdi
callq 0x2380