#ifndef INTERFACE_HPP #define INTERFACE_HPP #include #include #include #include #include #include "type.hpp" //can't be here std::vector chunker(std::vector file_arg, size_t block_size); std::vector sorter(std::vector chunks_arg, std::string delim); std::string merge(std::vector list_to_merge); template std::vector chunker(std::vector file_arg, size_t block_size){ std::vector chunk_filenames_list; typename std::vector::const_iterator fileIterator; for(fileIterator=file_arg.begin();fileIterator!=file_arg.end();fileIterator++){ int chunk_number=0; saga::filesystem::file f(saga::url(*fileIterator), saga::filesystem::Read); boost::iostreams::stream in (*fileIterator); while(in.good()){ size_t size_total=0; T elem; in >> elem; if(in.fail()) break; std::string chunk_name(*fileIterator); std::stringstream convert; convert << chunk_number; chunk_name.append(".chunk"); chunk_name.append(convert.str()); saga::filesystem::file fout (saga::url(chunk_name), saga::filesystem::Write | saga::filesystem::Create); boost::iostreams::stream out (chunk_name); out << " "; out << elem; size_total+=sizeof(elem); size_total+=sizeof(" "); while(size_total> elem; if(in.fail()) break; out << " "; //ask how to make this more generic out << elem; size_total+=sizeof(elem); size_total+=sizeof(" "); } fout.close(); chunk_number++; chunk_filenames_list.push_back(chunk_name); } } return chunk_filenames_list; } template std::vector sorter(std::vector chunks_arg,std::string delim){ typename std::vector::const_iterator fileIterator; std::vector sorted_filenames_list; for(fileIterator=chunks_arg.begin();fileIterator!=chunks_arg.end();fileIterator++){ T elem; saga::filesystem::file f(saga::url(*fileIterator), saga::filesystem::Read); boost::iostreams::stream in (*fileIterator); if(!in.good()) continue; std::vector values; std::string sorted_chunk_name(*fileIterator); sorted_chunk_name.append(".sorted"); saga::filesystem::file fout (saga::url(sorted_chunk_name), saga::filesystem::Write | saga::filesystem::Create); boost::iostreams::stream out (sorted_chunk_name); while(in >> elem){ values.push_back(elem); } std::sort(values.begin(),values.end()); for(typename std::vector::size_type x=0;x std::vector private_merge(std::vector filename_list, size_t buf_size){ static int counter=0; std::vector retval; if(filename_list.size()%2!=0) retval.push_back(filename_list.back()); for(unsigned int x = 1;x in1 (f1); //Open file1 & associate stream saga::filesystem::file f2(saga::url(filename_list[x]), saga::filesystem::Read); boost::iostreams::stream in2 (f2); //Open file2 & associate stream std::string filename("file.merged"); std::stringstream convert; convert << counter; filename.append(convert.str()); saga::filesystem::file fout (saga::url(filename), saga::filesystem::Write | saga::filesystem::Create); boost::iostreams::stream out (filename); //Create file where all merges to std::vector leftover1; std::vector leftover2; //A variable to catch what was greater than the greatest stored in list2 while(1) { bool l1success = false; bool l2success = false; int end = -1; std::vector list1(leftover1),list2(leftover2); leftover1.clear(); leftover2.clear(); //Create 2 vectors while(list1.size() < buf_size) { T elem; if(!(in1 >> elem)) { l1success = true; break; } list1.push_back(elem); } //Read as much as possible and note if got all while(list2.size() < buf_size) { T elem; if(!(in2 >> elem)){ l2success = true; break; } if(elem > list1.back() && end!=-1) { end = list2.size(); } list2.push_back(elem); } if(end == -1){ end = list2.size(); } //Read as much as possible and not when became greater than the largest in list1 std::vector output; while(1) { T min; if(list1.front() < list2.front()){ min = list1.front(); list1.erase(list1.begin()); } else { min = list2.front(); list2.erase(list2.begin()); } output.push_back(min); if(list1.size()==0 || list2.size()==0) { leftover1 = list1; leftover2 = list2; //CREATE LEFTOVERS break; } } //merge as until greater than last element in list1 for(unsigned int y=0;y //The list of files you want to merge, buffer_size is the largest vector/array/list you can have std::string merge(std::vector list_to_merge,size_t buf_siz){ int x=0; while(list_to_merge.size()>1){ list_to_merge = private_merge(list_to_merge,buf_siz); x++; } return list_to_merge[0]; } #endif