添加了注释
This commit is contained in:
		
							parent
							
								
									2ecddfa22f
								
							
						
					
					
						commit
						1f8beaa44d
					
				
							
								
								
									
										28
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								src/main.cpp
									
									
									
									
									
								
							@ -15,27 +15,27 @@
 | 
			
		||||
#include <unordered_map>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// std::string reversedComplement(std::string DNAsequence);
 | 
			
		||||
// 这两个宏用来申请读入和读出流,实现反射并输出日志,获取申请流的变量名字
 | 
			
		||||
 | 
			
		||||
#define OPEN_IFS_AND_CHECK(file_path,value_name)std::ifstream value_name(file_path);if(value_name.is_open()==false){std::stringstream ss;ss<<"cannot open input file stream : "<<file_path.filename();throw std::runtime_error(ss.str());}else{zt::print("Open input file stream to value ["#value_name"] ok , from [",file_path.filename(),"]\n");}
 | 
			
		||||
 | 
			
		||||
#define OPEN_OFS_AND_CHECK(file_path,value_name)std::ofstream value_name(file_path);if(value_name.is_open()==false){std::stringstream ss;ss<<"cannot open output file stream : "<<file_path.filename();throw std::runtime_error(ss.str());}else{zt::print("Open output file stream to value ["#value_name"] ok , from [",file_path.filename(),"]\n");}
 | 
			
		||||
 | 
			
		||||
//最大DNA序列长度
 | 
			
		||||
const size_t MAX_SIZE = 5e4+5;
 | 
			
		||||
 | 
			
		||||
void reverseComplement(std::array<char, MAX_SIZE> &DNAsequence, const size_t buf_size) 
 | 
			
		||||
void reverseComplement(std::array<char, MAX_SIZE> &DNAsequence, const size_t buf_size) //注意这里使用引用DNA sequence,避免拷贝开销
 | 
			
		||||
{
 | 
			
		||||
    static const std::unordered_map<char, char> complement = {
 | 
			
		||||
    static const std::unordered_map<char, char> complement = { //这里使用查表的方式大大提高CPU速度,因为if分支CPU不容易命中缓存,需要使用查表加速
 | 
			
		||||
        {'A', 'T'}, {'a', 'T'},
 | 
			
		||||
        {'T', 'A'}, {'t', 'A'},
 | 
			
		||||
        {'C', 'G'}, {'c', 'G'},
 | 
			
		||||
        {'G', 'C'}, {'g', 'C'}
 | 
			
		||||
    };
 | 
			
		||||
    
 | 
			
		||||
    std::reverse(DNAsequence.begin(), DNAsequence.begin() + buf_size); 
 | 
			
		||||
    std::reverse(DNAsequence.begin(), DNAsequence.begin() + buf_size); //翻转DNA序列
 | 
			
		||||
    
 | 
			
		||||
    for (std::remove_const_t<decltype(buf_size)> i = 0; i < buf_size; ++i) { 
 | 
			
		||||
        auto it = complement.find(DNAsequence[i]);
 | 
			
		||||
        auto it = complement.find(DNAsequence[i]);//查表并替换
 | 
			
		||||
        if (it != complement.end()) {
 | 
			
		||||
            DNAsequence[i] = it->second;
 | 
			
		||||
        }
 | 
			
		||||
@ -43,7 +43,7 @@ void reverseComplement(std::array<char, MAX_SIZE> &DNAsequence, const size_t buf
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Spent{
 | 
			
		||||
class Spent{ // 使用RAII原理的自动计时器,计算主函数运行时间,析构时自动输出
 | 
			
		||||
private:
 | 
			
		||||
    const decltype(std::chrono::system_clock::now()) start;
 | 
			
		||||
    const std::string_view name;
 | 
			
		||||
@ -51,7 +51,7 @@ public:
 | 
			
		||||
    Spent(std::string_view name)noexcept:start(std::chrono::system_clock::now()),name(name){
 | 
			
		||||
        zt::print("[Timer: ",name,"]"," Start timing","\n");
 | 
			
		||||
    }
 | 
			
		||||
    ~Spent(){
 | 
			
		||||
    ~Spent()noexcept{
 | 
			
		||||
        const auto end = std::chrono::system_clock::now();
 | 
			
		||||
        const auto dur = std::chrono::duration_cast<std::chrono::milliseconds> (end-start);
 | 
			
		||||
        zt::print("[Timer: ",name,"]"," Stop timing , using ", dur,"\n");
 | 
			
		||||
@ -64,7 +64,7 @@ int main()
 | 
			
		||||
        std::ios_base::sync_with_stdio(false); //加了没效果 
 | 
			
		||||
        using namespace std;
 | 
			
		||||
 | 
			
		||||
        Spent all_spent("All spent");
 | 
			
		||||
        Spent all_spent("All spent"); //自动计时器,给主函数计时
 | 
			
		||||
        
 | 
			
		||||
        std::array<char,MAX_SIZE> buf;
 | 
			
		||||
        unsigned long long lines = 0;
 | 
			
		||||
@ -73,7 +73,7 @@ int main()
 | 
			
		||||
        
 | 
			
		||||
        // ifstream input_file_stream(input_path);
 | 
			
		||||
        // ofstream output_file_stream(output_path);
 | 
			
		||||
        OPEN_IFS_AND_CHECK(input_path, input_file_stream)
 | 
			
		||||
        OPEN_IFS_AND_CHECK(input_path, input_file_stream) //创建输入和输出流
 | 
			
		||||
        OPEN_OFS_AND_CHECK(output_path, output_file_stream)
 | 
			
		||||
 | 
			
		||||
        // string l = "";
 | 
			
		||||
@ -82,15 +82,15 @@ int main()
 | 
			
		||||
        {
 | 
			
		||||
            int m = lines%2;
 | 
			
		||||
            const auto buf_len = strlen(buf.data());
 | 
			
		||||
            const std::string_view suffix("\n");
 | 
			
		||||
            const std::string_view suffix("\n"); //设置一个每个DNA序列结尾的字符,这里是以\n换行来结尾
 | 
			
		||||
            if (m == 1){
 | 
			
		||||
                // output_file_stream << reverseComplement(buf) << endl;
 | 
			
		||||
                reverseComplement(buf,buf_len);
 | 
			
		||||
            }
 | 
			
		||||
            for(int i=0;i<suffix.size();i++){
 | 
			
		||||
                buf[buf_len+i]=suffix[i];
 | 
			
		||||
                buf[buf_len+i]=suffix[i]; // 将末尾增加字符追加到结尾。注意不能过长超过MAX_SIZE
 | 
			
		||||
            }
 | 
			
		||||
            output_file_stream.write(buf.data(), buf_len+suffix.size());
 | 
			
		||||
            output_file_stream.write(buf.data(), buf_len+suffix.size()); // 写入文件
 | 
			
		||||
            lines++;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
@ -99,7 +99,7 @@ int main()
 | 
			
		||||
        zt::print(
 | 
			
		||||
            "Caught an error because:\n",
 | 
			
		||||
            "\t",NAME_VALUE(e.what()),"\n"
 | 
			
		||||
            "closing\n"
 | 
			
		||||
            "Closing\n"
 | 
			
		||||
        );
 | 
			
		||||
    }catch(...){
 | 
			
		||||
        zt::print(
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user