From daabc0fc189b26d827709978bbe118b85f01a88b Mon Sep 17 00:00:00 2001 From: MiserableLife Date: Sat, 24 Mar 2018 12:18:22 +0900 Subject: [PATCH] [feature] divide into modules * divide into modules. * refactoring into C++ form * add sequence analyze --- SequenceAnalyzer.cpp | 79 +++++++++++++++++++++ SequenceAnalyzer.h | 52 ++++++++++++++ SetAnalyzer.cpp | 94 +++++++++++++++++++++++++ SetAnalyzer.h | 31 +++++++++ main.cpp | 140 -------------------------------------- sequence_analyze_test.cpp | 48 +++++++++++++ set_analyze_test.cpp | 32 +++++++++ 7 files changed, 336 insertions(+), 140 deletions(-) create mode 100644 SequenceAnalyzer.cpp create mode 100644 SequenceAnalyzer.h create mode 100644 SetAnalyzer.cpp create mode 100644 SetAnalyzer.h delete mode 100644 main.cpp create mode 100644 sequence_analyze_test.cpp create mode 100644 set_analyze_test.cpp diff --git a/SequenceAnalyzer.cpp b/SequenceAnalyzer.cpp new file mode 100644 index 0000000..7f91420 --- /dev/null +++ b/SequenceAnalyzer.cpp @@ -0,0 +1,79 @@ +#include "SequenceAnalyzer.h" +#include +#include + + +sequence_table* SequenceAnalyzer::divide_by_thread(const char* filename) +{ + std::ifstream in; + char buffer[MAXBUFFER]; + struct contents tmp; + std::string str; + int num_line=0; + sequence_table* table = new sequence_table; + in.open(filename); + while(!in.eof()) + { + in.getline(buffer, MAXBUFFER); + str = buffer; + //delimeter process + //std::cout<first); + out.open(filename.c_str()); + std::vector& c = it->second; + for(std::vector::iterator it_v = c.begin(); it_v!= c.end(); it_v++) + out<< it_v->body <>> "<< s2[i].body<< std::endl; + return ; + } + } + + if(comp_size < 0 ) + std::cout<< "left sequence is subset of right sequence"<0) + std::cout<<"right sequence is subset of left sequence"< +#include +#include + +enum { + MAXBUFFER= 8192 +} ; + +struct contents +{ + int pos;//line number where the contents exist + std::string body; +}; + +typedef std::vector sequence; +typedef std::map sequence_table; +/* + * Analyzing format + * thread_number key(some parameters) any other things + * ex) + * 1 fopen("myfile.txt","rt") somebodyhelpme + * + * + ** + */ +class SequenceAnalyzer +{ +private: + std::string del; + + +public: + SequenceAnalyzer() : del(" \t") {} + inline void setdelimeter(const char* delimeter) + { + del = delimeter; + } + sequence_table* divide_by_thread(const char* filename); + void print_sequence_table_into_file(sequence_table& table, const char* prefix_filename ); + void compare(const sequence& s1, const sequence& s2); + + + +}; + + + + +#endif diff --git a/SetAnalyzer.cpp b/SetAnalyzer.cpp new file mode 100644 index 0000000..0eefeaf --- /dev/null +++ b/SetAnalyzer.cpp @@ -0,0 +1,94 @@ +#include "SetAnalyzer.h" +#include +#include +#include + + +int SetAnalyzer::read_with_delimeter(std::set& filtered_set, const char* filename, bool append) +{ + std::ifstream in; + char buffer[255]; + + if(!append) + filtered_set.clear(); + in.open(filename); + while(!in.eof()) + { + std::vector temp_storage; + in.getline(buffer, 255); + //delimeter process + std::string str = buffer; + std::size_t prepos=0, pos = str.find_first_of(del.c_str()); + while(pos != std::string::npos) + { + temp_storage.push_back(str.substr(prepos, pos-prepos)); + prepos = pos+1; + while( ( pos = str.find_first_of(del.c_str(),pos+1)) != std::string::npos ) + if( pos != prepos) + break; + else + prepos = pos+1; + } + temp_storage.push_back(str.substr(prepos, pos-prepos)); + str=""; + for(std::vector::iterator it= temp_storage.begin(); it!= temp_storage.end(); it++) + str += *it + " "; + //delimeter process end + + filtered_set.insert(str); + } + in.close(); + return 0; +} +std::set* SetAnalyzer::get_diffset(const std::set& set1, const std::set& set2) +{ + std::set* diff1_2 = new std::set; + //set1 - set2 + *diff1_2 = set1; + for(std::set::iterator it = set2.begin(); it!= set2.end(); it++) + diff1_2->erase(*it); + return diff1_2; +} + +void analyze_with_key(const std::set& diff1_2, const std::set& diff2_1)// developing.... +{ + std::map orghash; + for(std::set::iterator it = diff1_2.begin(); it!= diff1_2.end(); it++) + { + std::string str = *it; + std::size_t pos = str.find_first_of(" "); + orghash[str.substr(0,pos)] = str.substr(pos+1); + } + for(std::set::iterator it = diff2_1.begin(); it!= diff2_1.end(); it++) + { + std::string str = *it; + std::size_t pos = str.find_first_of(" "); + if(!orghash[str.substr(0,pos)].empty()) + { + std::string outputstr=""; + std::cout<< str.substr(0,pos)<< " " < +#include + + + +class SetAnalyzer +{ +private: +// std::map orghash; + std::string del; +public: + SetAnalyzer() : del("\t") + { + }; + inline void setdelimeter(const char* delimeter) + { + del = delimeter; + } + int read_with_delimeter(std::set& filtered_set, const char* filename, bool append=false); + std::set* SetAnalyzer::get_diffset(const std::set& set1, const std::set& set2); + + +}; + + + +#endif + diff --git a/main.cpp b/main.cpp deleted file mode 100644 index 398359b..0000000 --- a/main.cpp +++ /dev/null @@ -1,140 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - - -int main(int argc, char** argv) -{ - std::ifstream in; - if(argc <3 ) - return 1; - char* file1 = argv[1]; - char* file2 = argv[2]; - char buffer[255]; - std::set set1; - std::set set2; - std::set diff1_2; - std::set diff2_1; - std::map orghash; - in.open(file1); - while(!in.eof()) - { - std::vector temp_storage; - in.getline(buffer, 255); - //delimeter process - std::string str = buffer; - std::size_t prepos=0, pos = str.find_first_of(" \t"); - while(pos != std::string::npos) - { - temp_storage.push_back(str.substr(prepos, pos-prepos)); - prepos = pos+1; - while( ( pos = str.find_first_of(" \t",pos+1)) != std::string::npos ) - if( pos != prepos) - break; - else - prepos = pos+1; - } - temp_storage.push_back(str.substr(prepos, pos-prepos)); - str=""; - for(std::vector::iterator it= temp_storage.begin(); it!= temp_storage.end(); it++) - str += *it + " "; - //delimeter process end - - set1.insert(str); - } - in.close(); - in.open(file2); - while(!in.eof()) - { - std::vector temp_storage; - in.getline(buffer,255); - //delimeter process - std::string str = buffer; - std::size_t prepos=0, pos = str.find_first_of(" \t"); - while(pos != std::string::npos) - { - - temp_storage.push_back(str.substr(prepos, pos-prepos)); - prepos = pos+1; -// pos = str.find_first_of(" \t",pos+1); - while( ( pos = str.find_first_of(" \t",pos+1)) != std::string::npos ) - if( pos != prepos) - break; - else - prepos = pos+1; - } - temp_storage.push_back(str.substr(prepos, pos-prepos)); - str=""; - for(std::vector::iterator it= temp_storage.begin(); it!= temp_storage.end(); it++) - str += *it + " "; - //delimeter process end - - set2.insert(str); - } - in.close(); - - diff1_2 = set1; - diff2_1 = set2; - - for(std::set::iterator it = set1.begin(); it!= set1.end(); it++) - diff2_1.erase(*it); - for(std::set::iterator it = set2.begin(); it!= set2.end(); it++) - diff1_2.erase(*it); - - - for(std::set::iterator it = diff1_2.begin(); it!= diff1_2.end(); it++) - { - std::string str = *it; - std::size_t pos = str.find_first_of(" "); - orghash[str.substr(0,pos)] = str.substr(pos+1); - } - for(std::set::iterator it = diff2_1.begin(); it!= diff2_1.end(); it++) - { - std::string str = *it; - std::size_t pos = str.find_first_of(" "); - if(!orghash[str.substr(0,pos)].empty()) - { - std::string outputstr=""; - std::cout<< str.substr(0,pos)<< " " <::iterator it = diff1_2.begin(); it!= diff1_2.end(); it++) - std::cout<<*it<::iterator it = diff2_1.begin(); it!= diff2_1.end(); it++) - std::cout<<*it< +#include "SequenceAnalyzer.h" + +int main(int argc, char** argv) +{ + if(argc <3 ) + return 1; + char* file1 = argv[1]; + char* file2 = argv[2]; + sequence_table* table1;//thread , contents + sequence_table* table2;//thread , contents + + SequenceAnalyzer sa; + + table1 = sa.divide_by_thread(file1); + table2 = sa.divide_by_thread(file2); + + int cmd; + while(true) + { + std::cout<<"1. print out to files by thread number"< +#include "SetAnalyzer.h" + + + +int main(int argc, char** argv) +{ + if(argc <3 ) + return 1; + char* file1 = argv[1]; + char* file2 = argv[2]; + SetAnalyzer sa; + std::set set1, set2; + sa.read_with_delimeter(set1, file1); + sa.read_with_delimeter(set2, file2); + + std::set* diff1_2 = sa.get_diffset(set1, set2); + std::set* diff2_1 = sa.get_diffset(set2, set1); + std::cout<<"only on "<::iterator it = diff1_2->begin(); it!= diff1_2->end(); it++) + std::cout<<*it<::iterator it = diff2_1->begin(); it!= diff2_1->end(); it++) + std::cout<<*it<