#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<math.h>
#include<sys/time.h>
#include<functional>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<dirent.h>
#include<stdint.h>
#include<omp.h>
#include<unistd.h>
#include<random>
using namespace std;


//电路的parameter	Circuit_parameter
int			circuit_index	 		= 9999;		//电路编号
extern const int	parameter_input_bit_width 	= 64; 		//输入bit有几位
extern const int	parameter_output_bit_width	= 1;	 	//输出bit有几位
int			parameter_test_ios 		= 10000;	//测试要求多少样本

//算法的parameter 	Algorithm_parameter
extern const int 	parameter_num_threads		= 100;		//线程数
extern const int	parameter_multi_output_index	= 0; 		//BSD从第几层开始化简，前面若干层展开序确定
extern const int	parameter_early_stop_depth	= 100;		//BSD到第几层终止，输出此时的不准确BSD
extern const int	parameter_max_samples 		= 40000;		//BSD每一个节点最多进行多少次采样
extern const int	parameter_max_BDD_width		= 10000;	//BSD每一层最多多少个节点

//全局变量
int	GLOBAL_BDD_id_number; 
int	GLOBAL_BDD_nodes; 
int	GLOBAL_BDD_split_nodes; 
int	GLOBAL_train_time; 
int	GLOBAL_program_time; 

#include"cvt.h"
#include"io_generator_function.h"
#include"BSD.h"
#include"next_layer_bit.h"
#include"tool_function.h"
#include"print_circuit.h"



int	main(int argc,char* argv[]){


	omp_set_num_threads(parameter_num_threads);



	GLOBAL_BDD_id_number 	= 0;
	GLOBAL_BDD_nodes 	= 0;
	GLOBAL_BDD_split_nodes 	= 0;
	int i,j;
	int	which_demo_function;
	if(argc >= 2){
		circuit_index = atoi(argv[1]);
	}
	if(argc>=3){
		which_demo_function = atoi(argv[2]);
	}else{
		which_demo_function = 22;
	}
	DIR*	output_dir;
	output_dir	= opendir("rtl/");
	dirent	*ptr;
	vector<string>	files;	
	while((ptr=readdir(output_dir))!=NULL){
		files.push_back(ptr->d_name);
	}
	bool	this_bit_finished = 0;
	for (i=0;i<files.size();i++){
		if(files[i].substr(0,8) == "function"){
			int	this_output_bit = 0;
			for(j=0;j<10;j++){
				//cout<<files[i].substr(18+j,1);
				if(files[i].substr(18+j+1,1) == "."){
					break;
				}
			}
			//cout<<atoi(files[i].substr(9,9+j).c_str());
			if(atoi(files[i].substr(9,9+j).c_str()) == circuit_index){
				this_bit_finished = 1;
				cout<<"this bit finished:	"<<circuit_index<<endl;
				return 0;
				break;
			}
		}
	}
	closedir(output_dir);


	BDD_class BDD_class_main;
	BDD_class_main.start_depth 	= 0;	
	BDD_class_main.BDD_id 	= 0;
	GLOBAL_BDD_id_number += 1;
	BDD_class_main.which_demo_function 	= which_demo_function;
	BDD_class_main.which_demo_function 	= 22;
	BDD_class_main.how_many_start_nodes = parameter_output_bit_width;
	BDD_class_main.start_nodes = new BDD_node [BDD_class_main.how_many_start_nodes];
	for(int zi=0;zi<BDD_class_main.how_many_start_nodes;zi++){
		BDD_class_main.start_nodes[zi].which_bit_output 	= zi;
		BDD_class_main.start_nodes[zi].which_root_node  	= zi;
		BDD_class_main.start_nodes[zi].which_root_node_all[BDD_class_main.start_nodes[zi].which_root_node] = 1;
	}
	for(int zi=0;zi<parameter_input_bit_width+1;zi++){
		BDD_class_main.has_been_unfold[zi] = 0;
		BDD_class_main.most_influence[zi] = 0;
		BDD_class_main.BDD_width_each_layer[zi] = 0;
	}
	BDD_class_main.BDD_FULL_PROCESS();

};


