diff --git a/src/filepattern/cpp/external/external_filepattern.cpp b/src/filepattern/cpp/external/external_filepattern.cpp index 0a79f32..57a8c27 100644 --- a/src/filepattern/cpp/external/external_filepattern.cpp +++ b/src/filepattern/cpp/external/external_filepattern.cpp @@ -1,8 +1,6 @@ #include "external_filepattern.hpp" -using namespace std; - -ExternalFilePattern::ExternalFilePattern(const string& path, const string& filePattern, const string& block_size, bool recursive, bool suppressWarnings, bool sorted): +ExternalFilePattern::ExternalFilePattern(const std::string& path, const std::string& filePattern, const std::string& block_size, bool recursive, bool suppressWarnings, bool sorted): ExternalPattern(path, block_size, recursive) { this->setSuppressWarnings(suppressWarnings); @@ -53,7 +51,7 @@ ExternalFilePattern::~ExternalFilePattern(){ void ExternalFilePattern::printFiles(){ - vector files; + std::vector files; while(true){ files = this->stream_.getValidFilesBlock(); @@ -62,12 +60,12 @@ void ExternalFilePattern::printFiles(){ if(std::get<0>(file).size() < this->stream_.map_size_) continue; for(const auto& element: std::get<0>(file)){ - cout << element.first << ":" << s::to_string(element.second) << endl; + std::cout << element.first << ":" << s::to_string(element.second) << std::endl; } for(const auto& element: std::get<1>(file)){ - cout << "file: " << element << endl; + std::cout << "file: " << element << std::endl; } - cout << endl; + std::cout << std::endl; } if (this->stream_.endOfValidFiles()) break; @@ -90,11 +88,11 @@ void ExternalFilePattern::matchFiles() { } void ExternalFilePattern::matchFilesOneDir(){ - vector block; + std::vector block; - regex pattern_regex = regex(this->getRegexFilePattern()); - string file; - smatch sm; + std::regex pattern_regex = std::regex(this->getRegexFilePattern()); + std::string file; + std::smatch sm; // iterate over files while(!this->stream_.isEmpty()){ diff --git a/src/filepattern/cpp/external/external_pattern.cpp b/src/filepattern/cpp/external/external_pattern.cpp index 743ee37..65ede5b 100644 --- a/src/filepattern/cpp/external/external_pattern.cpp +++ b/src/filepattern/cpp/external/external_pattern.cpp @@ -1,8 +1,6 @@ #include "external_pattern.hpp" -using namespace std; - -ExternalPattern::ExternalPattern(const string& path, const string& block_size, bool recursive): +ExternalPattern::ExternalPattern(const std::string& path, const std::string& block_size, bool recursive): stream_(FilesystemStream(path, recursive, block_size)){ this->valid_files_path_ = this->stream_.getValidFilesPath(); // Store path to valid files txt file this->tmp_directories_.push_back(this->valid_files_path_); @@ -15,17 +13,17 @@ ExternalPattern::~ExternalPattern() { this->infile_.close(); this->group_stream_.close(); } - -void ExternalPattern::getMatchingLoop(ifstream& infile, - ofstream& outfile, - const string& variable, - const vector& values, + +void ExternalPattern::getMatchingLoop(std::ifstream& infile, + std::ofstream& outfile, + const std::string& variable, + const std::vector& values, Types& temp, Tuple& temp_map){ // while infile still has a map: // read map and add to outfile if value matches while(m::getMap(infile, temp_map, this->map_size_)){ - temp = get<0>(temp_map)[variable]; + temp = std::get<0>(temp_map)[variable]; for(const auto& value: values){ if(s::to_string(temp) == s::to_string(value)){ @@ -36,23 +34,23 @@ void ExternalPattern::getMatchingLoop(ifstream& infile, } -void ExternalPattern::getMatchingHelper(const tuple>& variable_map, const string& matching){ - string variable = get<0>(variable_map); // get key from argument - vector values = get<1>(variable_map); // get value from argument +void ExternalPattern::getMatchingHelper(const std::tuple>& variable_map, const std::string& matching){ + std::string variable = std::get<0>(variable_map); // get key from argument + std::vector values = std::get<1>(variable_map); // get value from argument // throw error if argument variable is not in the pattern - if(find(begin(this->variables_), end(this->variables_), variable) == end(this->variables_)) { - throw invalid_argument("\"" + variable + "\" is not a variable. Use a variable that is contained in the pattern."); + if(std::find(std::begin(this->variables_), std::end(this->variables_), variable) == std::end(this->variables_)) { + throw std::invalid_argument("\"" + variable + "\" is not a variable. Use a variable that is contained in the pattern."); } Types temp; - vector iter; + std::vector iter; Tuple temp_map; // if first or only variable to match, iterate over valid files if(!(fs::exists(matching))) { - ifstream valid_files(this->valid_files_path_); - ofstream outfile(matching); + std::ifstream valid_files(this->valid_files_path_); + std::ofstream outfile(matching); this->getMatchingLoop(valid_files, outfile, variable, values, temp, temp_map); @@ -75,13 +73,13 @@ void ExternalPattern::getMatchingHelper(const tuple>& vari ofs.close(); - ifstream in(this->matching_copy_); - ofstream out(matching); + std::ifstream in(this->matching_copy_); + std::ofstream out(matching); this->getMatchingLoop(in, out, variable, values, temp, temp_map); } } -void ExternalPattern::getMatchingInit(const vector>>& variables) { +void ExternalPattern::getMatchingInit(const std::vector>>& variables) { // construct temporary directory path this->fp_tmpdir_ = fs::temp_directory_path().string(); if (s::endsWith(this->fp_tmpdir_, "\\")) this->fp_tmpdir_.pop_back(); @@ -120,25 +118,25 @@ void ExternalPattern::getMatchingInit(const vector>> this->matching_init_ = true; } -vector ExternalPattern::getMatching(const vector>>& variables){ +std::vector ExternalPattern::getMatching(const std::vector>>& variables){ if (!matching_init_) { this->getMatchingInit(variables); - vector vec; + std::vector vec; return vec; } return this->getMatchingBlock(); } -vector ExternalPattern::getMatchingBlock(){ +std::vector ExternalPattern::getMatchingBlock(){ - long size = sizeof(vector); // store amount of memory begin used by this method + long size = sizeof(std::vector); // store amount of memory begin used by this method // throw error if block_size is too small to fit the empty vector - if(size > this->block_size_) throw runtime_error("The block size is smaller than the size of a vector. The block size must be increased"); + if(size > this->block_size_) throw std::runtime_error("The block size is smaller than the size of a vector. The block size must be increased"); Tuple temp; - vector vec; + std::vector vec; bool more_files; // If there are no more files to return, return an empty vector @@ -169,9 +167,9 @@ void ExternalPattern::groupByHelper(){ std::vector> , std::vector>> temp_group; - vector temp_vec; - vector> grouped_variables; - string group_by; + std::vector temp_vec; + std::vector> grouped_variables; + std::string group_by; //for(const auto& group_by: this->group){ for(unsigned int j = 1; j < this->group_.size(); ++j){ @@ -188,12 +186,12 @@ void ExternalPattern::groupByHelper(){ if (isSorted()) { sort(vec.second.begin(), vec.second.end(), [&group_by = as_const(group_by)](Tuple& p1, Tuple& p2){ - return get<0>(p1)[group_by] < get<0>(p2)[group_by]; + return std::get<0>(p1)[group_by] < std::get<0>(p2)[group_by]; }); } - Types current_value = get<0>(vec.second[0])[group_by]; // get the value of variable - vector empty_vec; + Types current_value = std::get<0>(vec.second[0])[group_by]; // get the value of variable + std::vector empty_vec; unsigned int i = 0; int group_ptr = 0; @@ -213,13 +211,13 @@ void ExternalPattern::groupByHelper(){ if (isSorted()) { sort(temp_group[group_ptr].second.begin(), temp_group[group_ptr].second.end(), [](Tuple& m1, Tuple& m2){ - return get<1>(m1)[0] < get<1>(m2)[0]; + return std::get<1>(m1)[0] < std::get<1>(m2)[0]; }); } temp_vec.clear(); if (i < vec.second.size()){ - current_value = get<0>(vec.second[i])[group_by]; + current_value = std::get<0>(vec.second[i])[group_by]; grouped_variables.pop_back(); } ++group_ptr; @@ -239,23 +237,23 @@ void ExternalPattern::nextGroup(){ if(this->first_call_) this->groupBy(this->group_); this->current_group_.clear(); - string str; - vector empty; - vector> grouped_variables; + std::string str; + std::vector empty; + std::vector> grouped_variables; - this->current_group_.push_back(make_pair(grouped_variables, empty)); + this->current_group_.push_back(std::make_pair(grouped_variables, empty)); - if(get<0>(this->temp_).size() != 0) this->current_group_[0].second.push_back(this->temp_); + if(std::get<0>(this->temp_).size() != 0) this->current_group_[0].second.push_back(this->temp_); // add mapping from previous call to return block if(!this->first_call_){ this->current_group_[0].second.clear(); - if(get<0>(this->temp_).size() != 0) this->current_group_[0].second.push_back(this->temp_); + if(std::get<0>(this->temp_).size() != 0) this->current_group_[0].second.push_back(this->temp_); } // check if end of file - streampos ptr = this->group_stream_.tellg(); + std::streampos ptr = this->group_stream_.tellg(); if(!(this->group_stream_ >> str)){ // reset variables in case of another call @@ -266,7 +264,7 @@ void ExternalPattern::nextGroup(){ this->groupByHelper(); return; } - this->group_stream_.seekg(ptr, ios::beg); + this->group_stream_.seekg(ptr, std::ios::beg); // iterate over valid files temp file while the group variable is constant bool value_added = false; @@ -276,7 +274,7 @@ void ExternalPattern::nextGroup(){ // if method has not been called, initialize data structures if(this->first_call_) { - this->current_value_ = get<0>(this->temp_)[this->group_[0]]; + this->current_value_ = std::get<0>(this->temp_)[this->group_[0]]; this->current_group_.resize(1); //grouped_variables.push_back(make_pair(group[0], current_value)); this->current_group_[0] = make_pair(grouped_variables, empty); @@ -290,7 +288,7 @@ void ExternalPattern::nextGroup(){ value_added = true; } - if(get<0>(this->temp_)[this->group_[0]] == this->current_value_) { + if(std::get<0>(this->temp_)[this->group_[0]] == this->current_value_) { this->current_group_[0].second.push_back(this->temp_); } else { @@ -299,10 +297,10 @@ void ExternalPattern::nextGroup(){ // sort block by basename if (isSorted()) { sort(this->current_group_[0].second.begin(), this->current_group_[0].second.end(), [](Tuple& m1, Tuple& m2){ - return get<1>(m1)[0] < get<1>(m2)[0]; + return std::get<1>(m1)[0] < std::get<1>(m2)[0]; }); } - this->current_value_ = get<0>(this->temp_)[this->group_[0]]; + this->current_value_ = std::get<0>(this->temp_)[this->group_[0]]; value_added = false; this->groupByHelper(); @@ -313,7 +311,7 @@ void ExternalPattern::nextGroup(){ if (isSorted()) { sort(this->current_group_[0].second.begin(), this->current_group_[0].second.end(), [](Tuple& m1, Tuple& m2){ - return get<1>(m1)[0] < get<1>(m2)[0]; + return std::get<1>(m1)[0] < std::get<1>(m2)[0]; }); } this->groupByHelper(); @@ -340,10 +338,10 @@ std::vector ExternalPattern::getValidFilesBlock(){ } -void ExternalPattern::groupBy(vector& group_by) { +void ExternalPattern::groupBy(std::vector& group_by) { this->setGroup(group_by); // sort valid files externally - string path = this->stream_.getValidFilesPath(); + std::string path = this->stream_.getValidFilesPath(); this->tmp_directories_.push_back(path); if (isSorted()) { @@ -356,15 +354,15 @@ void ExternalPattern::groupBy(vector& group_by) { } } -string ExternalPattern::externalOutPutName(){ - string output_name = this->getFilePattern(); // store a copy of the filePattern to modify - regex pattern_regex(this->getRegexFilePattern()); // regex version of filePattern +std::string ExternalPattern::externalOutPutName(){ + std::string output_name = this->getFilePattern(); // store a copy of the filePattern to modify + std::regex pattern_regex(this->getRegexFilePattern()); // regex version of filePattern std::ifstream infile; Tuple temp, min, max; int idx = 0; - string tempStr; + std::string tempStr; // iterate over every variable in mapping for(const auto& var: this->variables_){ infile.open(this->valid_files_path_); // open matched files @@ -375,8 +373,8 @@ string ExternalPattern::externalOutPutName(){ // find min and max values while(m::getMap(infile, temp, this->map_size_)){ - if(get<0>(temp)[var] < get<0>(min)[var]) min = temp; - if(get<0>(temp)[var] > get<0>(max)[var]) max = temp; + if(std::get<0>(temp)[var] < std::get<0>(min)[var]) min = temp; + if(std::get<0>(temp)[var] > std::get<0>(max)[var]) max = temp; } // update output name @@ -389,19 +387,19 @@ string ExternalPattern::externalOutPutName(){ return output_name; } -string ExternalPattern::outputName(vector& vec){ +std::string ExternalPattern::outputName(std::vector& vec){ // if vector is passed, process using in memory version if(vec.size() != 0) return this->outputNameHelper(vec); else return this->externalOutPutName(); // if no vector provided, iterate over all matched files } -string ExternalPattern::inferPattern(vector& vec, string& variables){ +std::string ExternalPattern::inferPattern(std::vector& vec, std::string& variables){ auto pattern = inferPatternInternal(vec, variables); return s::escape_regex_characters(pattern); } -string ExternalPattern::inferPattern(const string& path, string& variables, const string& block_size){ +std::string ExternalPattern::inferPattern(const std::string& path, std::string& variables, const std::string& block_size){ if (!fs::exists(path)) { throw std::invalid_argument("Path \"" + path + "\" does not exist."); @@ -409,9 +407,9 @@ string ExternalPattern::inferPattern(const string& path, string& variables, cons FilesystemStream stream = FilesystemStream(path, true, block_size, true); // create a stream from directory - vector vec = stream.getBlock(); + std::vector vec = stream.getBlock(); for(auto& str: vec) str = s::getBaseName(str); // Get basename of each file - string pattern = inferPatternInternal(vec, variables); // Get pattern from first block + std::string pattern = inferPatternInternal(vec, variables); // Get pattern from first block // while the stream is nonempty: // process each block using the in memory version with the current pattern @@ -440,27 +438,27 @@ void ExternalPattern::sortFiles(){ Tuple ExternalPattern::getItem(unsigned int key){ if(key < 0) { - if(this->stream_.getValidFilesSize() + key < 0) throw out_of_range("Index " + std::to_string(key) + " is out of range."); + if(this->stream_.getValidFilesSize() + key < 0) throw std::out_of_range("Index " + std::to_string(key) + " is out of range."); return this->stream_.getFileByIndex(this->stream_.getValidFilesSize()+key); } - if(key >= this->stream_.getValidFilesSize()) throw out_of_range("Index " + std::to_string(key) + " is out of range."); + if(key >= this->stream_.getValidFilesSize()) throw std::out_of_range("Index " + std::to_string(key) + " is out of range."); return this->stream_.getFileByIndex(key); } -vector ExternalPattern::getItemList(vector& key){ +std::vector ExternalPattern::getItemList(std::vector& key){ - vector vec; + std::vector vec; int valid_files_size = this->stream_.getValidFilesSize(); for(const auto& index: key){ if(index < 0) { - if(valid_files_size + index < 0) throw out_of_range("Index " + std::to_string(index) + " is out of range."); + if(valid_files_size + index < 0) throw std::out_of_range("Index " + std::to_string(index) + " is out of range."); vec.push_back(this->stream_.getFileByIndex(valid_files_size+index)); } else { - if(index > valid_files_size) throw out_of_range("Index " + std::to_string(index) + " is out of range."); + if(index > valid_files_size) throw std::out_of_range("Index " + std::to_string(index) + " is out of range."); vec.push_back(this->stream_.getFileByIndex(index)); } } @@ -468,16 +466,16 @@ vector ExternalPattern::getItemList(vector& key){ return vec; } -vector ExternalPattern::getSlice(vector& key){ +std::vector ExternalPattern::getSlice(std::vector& key){ - string key0 = s::to_string(key[0]); - string key1 = s::to_string(key[1]); - string key2 = s::to_string(key[2]); + std::string key0 = s::to_string(key[0]); + std::string key1 = s::to_string(key[1]); + std::string key2 = s::to_string(key[2]); if(s::is_number(key0) && key1 == "None" && key2 == "None"){ unsigned int i = stoi(key0); - if(i >= this->stream_.getValidFilesSize()) throw out_of_range("Index " + std::to_string(i) + " is out of range."); + if(i >= this->stream_.getValidFilesSize()) throw std::out_of_range("Index " + std::to_string(i) + " is out of range."); unsigned int j = this->stream_.getValidFilesSize(); unsigned int step = 1; return this->stream_.getValidFilesSlice(i, j, step); @@ -488,9 +486,9 @@ vector ExternalPattern::getSlice(vector& key){ unsigned int i = stoi(key0); unsigned int j = stoi(key1); - if(i > this->stream_.getValidFilesSize()) throw out_of_range("Index " + std::to_string(i) + " is out of range."); - if(j > this->stream_.getValidFilesSize()) throw out_of_range("Index " + std::to_string(j) + " is out of range."); - if(j >= 0 && i > j) throw out_of_range("Invalid range."); + if(i > this->stream_.getValidFilesSize()) throw std::out_of_range("Index " + std::to_string(i) + " is out of range."); + if(j > this->stream_.getValidFilesSize()) throw std::out_of_range("Index " + std::to_string(j) + " is out of range."); + if(j >= 0 && i > j) throw std::out_of_range("Invalid range."); if(j < 0) j += this->stream_.getValidFilesSize() + 1; @@ -502,8 +500,8 @@ vector ExternalPattern::getSlice(vector& key){ unsigned int i = stoi(key0); unsigned int j = stoi(key1); - if(i > this->stream_.getValidFilesSize()) throw out_of_range("Index " + std::to_string(i) + " is out of range."); - if(j > this->stream_.getValidFilesSize()) throw out_of_range("Index " + std::to_string(j) + " is out of range."); + if(i > this->stream_.getValidFilesSize()) throw std::out_of_range("Index " + std::to_string(i) + " is out of range."); + if(j > this->stream_.getValidFilesSize()) throw std::out_of_range("Index " + std::to_string(j) + " is out of range."); int step = stoi(key2); return this->stream_.getValidFilesSlice(i, j, step); @@ -511,14 +509,14 @@ vector ExternalPattern::getSlice(vector& key){ if(s::is_number(key0) && key1 == "None" && s::is_number(key2)){ unsigned int i = stoi(key0); - if(i > this->stream_.getValidFilesSize()) throw out_of_range("Index " + std::to_string(i) + " is out of range."); + if(i > this->stream_.getValidFilesSize()) throw std::out_of_range("Index " + std::to_string(i) + " is out of range."); unsigned int j = this->stream_.getValidFilesSize(); int step = stoi(key2); return this->stream_.getValidFilesSlice(i, j, step); } - vector empty; + std::vector empty; return empty; } diff --git a/src/filepattern/cpp/external/external_stringpattern.cpp b/src/filepattern/cpp/external/external_stringpattern.cpp index 7a6c2e7..0eead93 100644 --- a/src/filepattern/cpp/external/external_stringpattern.cpp +++ b/src/filepattern/cpp/external/external_stringpattern.cpp @@ -1,8 +1,6 @@ #include "external_stringpattern.hpp" -using namespace std; - -ExternalStringPattern::ExternalStringPattern(const string& path, const string& file_pattern, const string& block_size, bool suppress_warnings, bool sorted): +ExternalStringPattern::ExternalStringPattern(const std::string& path, const std::string& file_pattern, const std::string& block_size, bool suppress_warnings, bool sorted): ExternalPattern(path, block_size, false) { this->setSuppressWarnings(suppress_warnings); this->setPath(path); // store path to target directory @@ -43,11 +41,11 @@ void ExternalStringPattern::matchFiles(){ this->setMapSize(this->variables_.size()); - vector block; + std::vector block; - regex pattern_regex = regex(this->getRegexFilePattern()); - string file; - smatch sm; + std::regex pattern_regex = std::regex(this->getRegexFilePattern()); + std::string file; + std::smatch sm; // iterate over files while(!this->stream_.isEmpty()){ diff --git a/src/filepattern/cpp/external/external_vectorpattern.cpp b/src/filepattern/cpp/external/external_vectorpattern.cpp index a50e7e0..4094627 100644 --- a/src/filepattern/cpp/external/external_vectorpattern.cpp +++ b/src/filepattern/cpp/external/external_vectorpattern.cpp @@ -1,12 +1,10 @@ #include "external_vectorpattern.hpp" -using namespace std; - const std::regex ExternalVectorPattern::STITCH_REGEX_ = std::regex("(corr): (.*); (position): \\((.*), (.*)\\); (grid): \\((.*), (.*)\\);"); // regex of a stitching vector line const std::vector ExternalVectorPattern::STITCH_REGEX_VECTOR_ = {std::regex("(corr):\\s*(.*?);"), std::regex("(position):\\s*\\((.*?),\\s*(.*?)\\);"), std::regex("(grid):\\s*\\((.*),\\s*(.*)\\);")}; const std::vector ExternalVectorPattern::STITCH_VARIABLES_ = {"correlation","posX","posY","gridX","gridY"}; // stitching vector variables -ExternalVectorPattern::ExternalVectorPattern(const string& path, const string& file_pattern, const string& block_size, bool suppress_warnings, bool sorted): +ExternalVectorPattern::ExternalVectorPattern(const std::string& path, const std::string& file_pattern, const std::string& block_size, bool suppress_warnings, bool sorted): ExternalPattern(path, block_size, false){ this->setSuppressWarnings(suppress_warnings); this->path_ = path; // store path to target directory @@ -15,7 +13,7 @@ ExternalPattern(path, block_size, false){ this->setBlockSize(Block::parseblockSize(block_size)); this->vector_reader_.open(path); - if(!this->vector_reader_.is_open()) throw invalid_argument("Invalid path \"" + path + "\"."); + if(!this->vector_reader_.is_open()) throw std::invalid_argument("Invalid path \"" + path + "\"."); this->setFilePattern(file_pattern); // cast input string to regex this->setRegexFilePattern(""); // Regex version of pattern @@ -45,13 +43,13 @@ void ExternalVectorPattern::matchFiles(){ this->setMapSize(this->variables_.size() + this->STITCH_VARIABLES_.size()); // Change the size of the map to include stitching variables - this->setRegexExpression(regex(this->getRegexFilePattern())); + this->setRegexExpression(std::regex(this->getRegexFilePattern())); - string line, file; + std::string line, file; Tuple temp; - smatch sm; + std::smatch sm; - while(getline(this->vector_reader_, line)){ + while(std::getline(this->vector_reader_, line)){ file = VectorParser::getFileName(line); if(regex_match(file, sm, this->getRegexExpression())){ temp = getVariableMap(file, sm); @@ -62,17 +60,17 @@ void ExternalVectorPattern::matchFiles(){ this->vector_reader_.close(); } -string ExternalVectorPattern::inferPattern(const string& path, string& variables, const string& block_size){ +std::string ExternalVectorPattern::inferPattern(const std::string& path, std::string& variables, const std::string& block_size){ long block = Block::parseblockSize(block_size); // parse string - vector files; - string file; - ifstream infile(path); + std::vector files; + std::string file; + std::ifstream infile(path); - long size = sizeof(vector) + sizeof(vector>); // memory footprint + long size = sizeof(std::vector) + sizeof(std::vector>); // memory footprint - string pattern = ""; + std::string pattern = ""; // while the stitching vector contains another line: // get block of file from stitching vector and call internal memory version of infer pattern on the block while(getline(infile, file)){ @@ -81,11 +79,11 @@ string ExternalVectorPattern::inferPattern(const string& path, string& variables files.push_back(s::escape_regex_characters(file)); - size += sizeof(string) + file.length() + sizeof(int)*2*file.length(); // account for size of filenames + size += sizeof(std::string) + file.length() + sizeof(int)*2*file.length(); // account for size of filenames if(size >= block) { pattern = inferPatternInternal(files, variables, pattern); - size = sizeof(vector) + sizeof(vector>); + size = sizeof(std::vector) + sizeof(std::vector>); files.clear(); } } diff --git a/src/filepattern/cpp/internal/filepattern.cpp b/src/filepattern/cpp/internal/filepattern.cpp index 42268d7..d299042 100644 --- a/src/filepattern/cpp/internal/filepattern.cpp +++ b/src/filepattern/cpp/internal/filepattern.cpp @@ -2,9 +2,7 @@ #include "../util/util.hpp" #include -using namespace std; - -FilePatternObject::FilePatternObject(const string& path, const string& file_pattern, bool recursive, bool suppress_warnings, bool sorted) { +FilePatternObject::FilePatternObject(const std::string& path, const std::string& file_pattern, bool recursive, bool suppress_warnings, bool sorted) { this->setSuppressWarnings(suppress_warnings); @@ -17,7 +15,7 @@ FilePatternObject::FilePatternObject(const string& path, const string& file_patt this->recursive_ = true; this->setJustPath(true); } catch (const std::runtime_error& e) { - string error = "No directory found. Invalid path \"" + path + "\"."; + std::string error = "No directory found. Invalid path \"" + path + "\"."; throw std::runtime_error(error); } @@ -46,7 +44,7 @@ FilePatternObject::FilePatternObject(const string& path, const string& file_patt this->iterator_ = fs::directory_iterator(fs::path(this->getPath())); // store iterator for target directory } } catch (const std::runtime_error& e) { - string error = "No directory found. Invalid path \"" + path + "\"."; + std::string error = "No directory found. Invalid path \"" + path + "\"."; throw std::runtime_error(error); } } @@ -64,10 +62,10 @@ FilePatternObject::FilePatternObject(const string& path, const string& file_patt void FilePatternObject::matchFilesOneDir(){ Map mapping; - vector parsed_regex; + std::vector parsed_regex; - string s; - string file, file_path; + std::string s; + std::string file, file_path; Tuple member; // Iterate over every file in directory @@ -76,11 +74,11 @@ void FilePatternObject::matchFilesOneDir(){ auto start = this->getRegexFilePattern().find('{'); auto end = this->getRegexFilePattern().find('}'); auto length = end - start; - throw invalid_argument("Invalid pattern found in bracket expressions in filepattern: \"" + this->getRegexFilePattern().substr(start, length+1) + "\""); + throw std::invalid_argument("Invalid pattern found in bracket expressions in filepattern: \"" + this->getRegexFilePattern().substr(start, length+1) + "\""); } - regex pattern_regex = regex(this->getRegexFilePattern()); - smatch sm; + std::regex pattern_regex = std::regex(this->getRegexFilePattern()); + std::smatch sm; for (const auto& entry : this->iterator_) { // Get the current file @@ -96,11 +94,11 @@ void FilePatternObject::matchFilesOneDir(){ void FilePatternObject::matchFilesMultDir(){ - regex pattern_regex = regex(this->getRegexFilePattern()); + std::regex pattern_regex = std::regex(this->getRegexFilePattern()); Tuple tup; - smatch sm; - string file, file_path; + std::smatch sm; + std::string file, file_path; bool is_pushed = false; @@ -125,7 +123,7 @@ void FilePatternObject::matchFilesMultDir(){ tup = getVariableMapMultDir(file_path, sm); } - if(get<0>(tup).size() > 0){ + if(std::get<0>(tup).size() > 0){ this->valid_files_.push_back(tup); is_pushed = true; } else { @@ -134,7 +132,7 @@ void FilePatternObject::matchFilesMultDir(){ } } - if (!is_pushed && get<1>(tup).size() > 0) { + if (!is_pushed && std::get<1>(tup).size() > 0) { this->valid_files_.push_back(tup); } } diff --git a/src/filepattern/cpp/internal/internal_pattern.cpp b/src/filepattern/cpp/internal/internal_pattern.cpp index c3e7f72..111c852 100644 --- a/src/filepattern/cpp/internal/internal_pattern.cpp +++ b/src/filepattern/cpp/internal/internal_pattern.cpp @@ -1,17 +1,15 @@ #include "internal_pattern.hpp" #include "../util/alphanum.hpp" -using namespace std; - void InternalPattern::next() {} void InternalPattern::nextGroup() {} -void InternalPattern::groupByHelper(const vector& groups){ +void InternalPattern::groupByHelper(const std::vector& groups){ std::vector> , std::vector>> temp; - vector temp_vec; - vector> grouped_variables; + std::vector temp_vec; + std::vector> grouped_variables; for(const auto& group_by: groups){ @@ -23,13 +21,13 @@ void InternalPattern::groupByHelper(const vector& groups){ // Sort the matched files by the group_by parameter if (isSorted()) { - sort(vec.second.begin(), vec.second.end(), [&group_by = as_const(group_by)](Tuple& p1, Tuple& p2){ - return get<0>(p1)[group_by] < get<0>(p2)[group_by]; + sort(vec.second.begin(), vec.second.end(), [&group_by = std::as_const(group_by)](Tuple& p1, Tuple& p2){ + return std::get<0>(p1)[group_by] < std::get<0>(p2)[group_by]; }); } - Types current_value = get<0>(vec.second[0])[group_by]; // get the value of variable - vector empty_vec; + Types current_value = std::get<0>(vec.second[0])[group_by]; // get the value of variable + std::vector empty_vec; unsigned int i = 0; int group_ptr = 0; @@ -48,12 +46,12 @@ void InternalPattern::groupByHelper(const vector& groups){ grouped_variables.push_back(make_pair(group_by, current_value)); temp.push_back(make_pair(grouped_variables, temp_vec)); sort(temp[group_ptr].second.begin(), temp[group_ptr].second.end(), [](Tuple& m1, Tuple& m2){ - return get<1>(m1)[0] < get<1>(m2)[0]; + return std::get<1>(m1)[0] < std::get<1>(m2)[0]; }); temp_vec.clear(); if (i < vec.second.size()){ - current_value = get<0>(vec.second[i])[group_by]; + current_value = std::get<0>(vec.second[i])[group_by]; grouped_variables.pop_back(); } ++group_ptr; @@ -67,35 +65,35 @@ void InternalPattern::groupByHelper(const vector& groups){ } -void InternalPattern::groupBy(vector& groups) { +void InternalPattern::groupBy(std::vector& groups) { // Cannot group empty files so return if (valid_files_.size() == 0) return; - vector> grouped_variables; + std::vector> grouped_variables; this->setGroup(groups); this->valid_grouped_files_.clear(); Tuple member; if(groups.size() == 1 && groups[0] == "*__all__*") { - vector> empty; + std::vector> empty; this->valid_grouped_files_.push_back(make_pair(empty, this->valid_files_)); return; } - string group_by = groups[0]; + std::string group_by = groups[0]; // Sort the matched files by the group_by parameter if (isSorted()) { sort(this->valid_files_.begin(), this->valid_files_.end(), [&group_by = as_const(group_by)](Tuple& p1, Tuple& p2){ - return get<0>(p1)[group_by] < get<0>(p2)[group_by]; + return std::get<0>(p1)[group_by] < std::get<0>(p2)[group_by]; }); } - Types current_value = get<0>(this->valid_files_[0])[group_by]; // get the value of variable + Types current_value = std::get<0>(this->valid_files_[0])[group_by]; // get the value of variable - vector empty_vec; + std::vector empty_vec; unsigned int i = 0; int group_ptr = 0; @@ -111,7 +109,7 @@ void InternalPattern::groupBy(vector& groups) { // sort group of variables if (isSorted()) { sort(this->valid_grouped_files_[group_ptr].second.begin(), this->valid_grouped_files_[group_ptr].second.end(), [](Tuple& m1, Tuple& m2){ - return get<1>(m1)[0] < get<1>(m2)[0]; + return std::get<1>(m1)[0] < std::get<1>(m2)[0]; }); } @@ -119,7 +117,7 @@ void InternalPattern::groupBy(vector& groups) { if (i >= this->valid_files_.size()) break; } - if (i < this->valid_files_.size()) current_value = get<0>(this->valid_files_[i])[group_by]; + if (i < this->valid_files_.size()) current_value = std::get<0>(this->valid_files_[i])[group_by]; ++group_ptr; } @@ -133,12 +131,12 @@ std::vector InternalPattern::getMatchingBlock() { return vec; } -void InternalPattern::getMatchingLoop(vector& iter, - const string& variable, - const vector& values, +void InternalPattern::getMatchingLoop(std::vector& iter, + const std::string& variable, + const std::vector& values, Types& temp){ for(auto& file: iter){ - temp = get<0>(file)[variable]; + temp = std::get<0>(file)[variable]; for(const auto& value: values){ if(temp == value){ this->matching_.push_back(file); @@ -147,17 +145,17 @@ void InternalPattern::getMatchingLoop(vector& iter, } } -void InternalPattern::getMatchingHelper(const tuple>& variableMap){ - string variable = get<0>(variableMap); // get key from argument - vector values = get<1>(variableMap); // get value from argument +void InternalPattern::getMatchingHelper(const std::tuple>& variableMap){ + std::string variable = std::get<0>(variableMap); // get key from argument + std::vector values = std::get<1>(variableMap); // get value from argument // throw error if argument variable is not in the pattern if(find(begin(this->variables_), end(this->variables_), variable) == end(this->variables_)) { - throw invalid_argument("\"" + variable + "\" is not a variable. Use a variable that is contained in the pattern."); + throw std::invalid_argument("\"" + variable + "\" is not a variable. Use a variable that is contained in the pattern."); } Types temp; - vector iter; + std::vector iter; // if first or only variable to match, iterate over valid files if(this->matching_.size() == 0) { this->getMatchingLoop(this->valid_files_, variable, values, temp); @@ -168,7 +166,7 @@ void InternalPattern::getMatchingHelper(const tuple>& vari } } -vector InternalPattern::getMatching(const vector>>& variables){ +std::vector InternalPattern::getMatching(const std::vector>>& variables){ // clear the vector that stores matching files this->matching_.erase(this->matching_.begin(), this->matching_.end()); @@ -181,12 +179,12 @@ vector InternalPattern::getMatching(const vectormatching_; } -string InternalPattern::outputName(vector& vec){ +std::string InternalPattern::outputName(std::vector& vec){ return this->outputNameHelper(vec); } -string InternalPattern::inferPattern(const string& path, string& variables, const string& block_size){ - vector vec; +std::string InternalPattern::inferPattern(const std::string& path, std::string& variables, const std::string& block_size){ + std::vector vec; if (!fs::exists(path)) { throw std::invalid_argument("Path \"" + path + "\" does not exist."); @@ -194,10 +192,10 @@ string InternalPattern::inferPattern(const string& path, string& variables, cons if(s::endsWith(path, ".txt")){ - ifstream infile(path); + std::ifstream infile(path); - string str; - while(getline(infile, str)) { + std::string str; + while(std::getline(infile, str)) { vec.push_back(s::escape_regex_characters(str)); } @@ -213,7 +211,7 @@ string InternalPattern::inferPattern(const string& path, string& variables, cons return inferPatternInternal(vec, variables); } -string InternalPattern::inferPattern(vector& vec, string& variables){ +std::string InternalPattern::inferPattern(std::vector& vec, std::string& variables){ for (auto& file: vec) { file = s::escape_regex_characters(file); } @@ -229,36 +227,36 @@ void InternalPattern::sortFiles(){ sort(this->valid_files_.begin(), this->valid_files_.end(), [comparator](Tuple& m1, Tuple& m2){ #ifdef JAVA_BINDING - return comparator(get<1>(m1)[0], get<1>(m2)[0]); + return comparator(std::get<1>(m1)[0], std::get<1>(m2)[0]); #else - return comparator(get<1>(m1)[0].u8string(), get<1>(m2)[0].u8string()); + return comparator(std::get<1>(m1)[0].u8string(), std::get<1>(m2)[0].u8string()); #endif }); } Tuple InternalPattern::getItem(unsigned int key){ if(key < 0) { - if(this->valid_files_.size() + key < 0) throw out_of_range("Index " + std::to_string(key) + " is out of range."); + if(this->valid_files_.size() + key < 0) throw std::out_of_range("Index " + std::to_string(key) + " is out of range."); return this->valid_files_[this->valid_files_.size()+key]; } - if(key >= this->valid_files_.size()) throw out_of_range("Index " + std::to_string(key) + " is out of range."); + if(key >= this->valid_files_.size()) throw std::out_of_range("Index " + std::to_string(key) + " is out of range."); return this->valid_files_[key]; } -vector InternalPattern::getItemList(vector& key){ +std::vector InternalPattern::getItemList(std::vector& key){ - vector vec; + std::vector vec; int valid_files_size = this->valid_files_.size(); for(const auto& index: key){ if(index < 0) { - if(valid_files_size + index < 0) throw out_of_range("Index " + std::to_string(index) + " is out of range."); + if(valid_files_size + index < 0) throw std::out_of_range("Index " + std::to_string(index) + " is out of range."); vec.push_back(this->valid_files_[valid_files_size+index]); } else { - if(index > valid_files_size) throw invalid_argument("Index " + std::to_string(index) + " is out of range."); + if(index > valid_files_size) throw std::invalid_argument("Index " + std::to_string(index) + " is out of range."); vec.push_back(this->valid_files_[index]); } } @@ -266,17 +264,17 @@ vector InternalPattern::getItemList(vector& key){ return vec; } -vector InternalPattern::getSlice(vector& key){ +std::vector InternalPattern::getSlice(std::vector& key){ - string key0 = s::to_string(key[0]); - string key1 = s::to_string(key[1]); - string key2 = s::to_string(key[2]); + std::string key0 = s::to_string(key[0]); + std::string key1 = s::to_string(key[1]); + std::string key2 = s::to_string(key[2]); int valid_files_size = this->valid_files_.size(); if(s::is_number(key0) && key1 == "None" && key2 == "None"){ int i = stoi(key0); - if(i >= valid_files_size) throw out_of_range("Index " + std::to_string(i) + " is out of range."); + if(i >= valid_files_size) throw std::out_of_range("Index " + std::to_string(i) + " is out of range."); int j = valid_files_size; int step = 1; @@ -289,9 +287,9 @@ vector InternalPattern::getSlice(vector& key){ int i = stoi(key0); int j = stoi(key1); - if(i > valid_files_size) throw out_of_range("Index " + std::to_string(i) + " is out of range."); - if(j > valid_files_size) throw out_of_range("Index " + std::to_string(j) + " is out of range."); - if(j >= 0 && i > j) throw out_of_range("Invalid range."); + if(i > valid_files_size) throw std::out_of_range("Index " + std::to_string(i) + " is out of range."); + if(j > valid_files_size) throw std::out_of_range("Index " + std::to_string(j) + " is out of range."); + if(j >= 0 && i > j) throw std::out_of_range("Invalid range."); if(j < 0) j += valid_files_size + 1; @@ -303,8 +301,8 @@ vector InternalPattern::getSlice(vector& key){ int i = stoi(key0); int j = stoi(key1); - if(i > valid_files_size) throw out_of_range("Index " + std::to_string(i) + " is out of range."); - if(j > valid_files_size) throw out_of_range("Index " + std::to_string(j) + " is out of range."); + if(i > valid_files_size) throw std::out_of_range("Index " + std::to_string(i) + " is out of range."); + if(j > valid_files_size) throw std::out_of_range("Index " + std::to_string(j) + " is out of range."); int step = stoi(key2); return v::sliceVector(this->valid_files_, i, j, step); @@ -312,7 +310,7 @@ vector InternalPattern::getSlice(vector& key){ if(s::is_number(key0) && key1 == "None" && s::is_number(key2)){ int i = stoi(key0); - if(i > valid_files_size) throw out_of_range("Index " + std::to_string(i) + " is out of range."); + if(i > valid_files_size) throw std::out_of_range("Index " + std::to_string(i) + " is out of range."); int j = valid_files_size; int step = stoi(key2); @@ -320,7 +318,7 @@ vector InternalPattern::getSlice(vector& key){ return v::sliceVector(this->valid_files_, i, j, step); } - vector empty; + std::vector empty; return empty; } diff --git a/src/filepattern/cpp/internal/stringpattern.cpp b/src/filepattern/cpp/internal/stringpattern.cpp index 2f869dd..b574295 100644 --- a/src/filepattern/cpp/internal/stringpattern.cpp +++ b/src/filepattern/cpp/internal/stringpattern.cpp @@ -1,9 +1,7 @@ #include "stringpattern.hpp" #include -using namespace std; - -StringPattern::StringPattern(const string& file_name, const string& file_pattern, bool suppress_warnings, bool sorted) { +StringPattern::StringPattern(const std::string& file_name, const std::string& file_pattern, bool suppress_warnings, bool sorted) { if (!fs::exists(file_name)) { throw std::invalid_argument("Path \"" + file_name + "\" does not exist."); @@ -24,13 +22,13 @@ StringPattern::StringPattern(const string& file_name, const string& file_pattern } void StringPattern::readFile(){ - string str; - ifstream in(this->file_name_); + std::string str; + std::ifstream in(this->file_name_); if(!in.is_open()) { - throw runtime_error("File \"" + this->file_name_ + "\" not found."); + throw std::runtime_error("File \"" + this->file_name_ + "\" not found."); } // read filenames into memory - while(getline(in, str)){ + while(std::getline(in, str)){ if(str.size()) this->files_.push_back(str); } @@ -40,11 +38,11 @@ void StringPattern::matchFiles(){ filePatternToRegex(); // get regex equivalent of filepattern //string file_path; - regex pattern_regex = regex(this->getRegexFilePattern()); // convert to regex - smatch sm; // store matching groups + std::regex pattern_regex = std::regex(this->getRegexFilePattern()); // convert to regex + std::smatch sm; // store matching groups for (const auto& file_path : this->files_) { // Get the current file - if(regex_match(file_path, sm, pattern_regex)){ + if(std::regex_match(file_path, sm, pattern_regex)){ this->valid_files_.push_back(getVariableMap(file_path, sm)); // write to txt file } } diff --git a/src/filepattern/cpp/internal/vectorpattern.cpp b/src/filepattern/cpp/internal/vectorpattern.cpp index fe90694..93738ba 100644 --- a/src/filepattern/cpp/internal/vectorpattern.cpp +++ b/src/filepattern/cpp/internal/vectorpattern.cpp @@ -1,7 +1,5 @@ #include "vectorpattern.hpp" -using namespace std; - const std::regex VectorPattern::STITCH_REGEX_ = std::regex("(corr): (.*); (position): \\((.*), (.*)\\); (grid): \\((.*), (.*)\\);"); // regex of a stitching vector line const std::vector VectorPattern::STITCH_REGEX_VECTOR_ = {std::regex("(corr):\\s*(.*?);"), std::regex("(position):\\s*\\((.*?),\\s*(.*?)\\);"), std::regex("(grid):\\s*\\((.*),\\s*(.*)\\);")}; const std::vector VectorPattern::STITCH_VARIABLES_ = {"correlation","posX","posY","gridX","gridY"}; // stitching vector variables @@ -15,7 +13,7 @@ VectorPattern::VectorPattern(const std::string& path, const std::string& filePat this->path_ = path; // store path to target directory this->infile_.open(path); - if(!this->infile_.is_open()) throw invalid_argument("Invalid path \"" + this->path_ + "\"."); + if(!this->infile_.is_open()) throw std::invalid_argument("Invalid path \"" + this->path_ + "\"."); this->setFilePattern(filePattern); // cast input string to regex this->setRegexFilePattern(""); // Regex version of pattern @@ -33,14 +31,14 @@ void VectorPattern::matchFiles(){ this->filePatternToRegex(); // get regex version of the pattern - this->setRegexExpression(regex(this->getRegexFilePattern())); // cast pattern to regex + this->setRegexExpression(std::regex(this->getRegexFilePattern())); // cast pattern to regex std::string line, file; Tuple temp; - smatch sm; + std::smatch sm; while(getline(this->infile_, line)){ file = VectorParser::getFileName(line); // get filename from line of stitching vector - if(regex_match(file, sm, this->getRegexExpression())){ // match to regex. groups are in sm + if(std::regex_match(file, sm, this->getRegexExpression())){ // match to regex. groups are in sm temp = getVariableMap(file, sm); // get the variable map for the filename //VectorParser::parseVectorLine(temp, line, this->STITCH_VARIABLES_, this->STITCH_REGEX_, this->variables_); // parse the vector line VectorParser::parseVectorLine(temp, line, this->STITCH_VARIABLES_, this->STITCH_REGEX_VECTOR_, this->variables_); // parse the vector line @@ -57,11 +55,11 @@ std::string VectorPattern::inferPattern(const std::string& path, std::string& va } std::string file; - ifstream infile(path); + std::ifstream infile(path); std::vector files; // get filenames from stitching vector - while(getline(infile, file)){ + while(std::getline(infile, file)){ file = VectorParser::getFileName(file); files.push_back(s::escape_regex_characters(file)); } diff --git a/src/filepattern/cpp/pattern.cpp b/src/filepattern/cpp/pattern.cpp index 22a71bb..7ff6c2d 100644 --- a/src/filepattern/cpp/pattern.cpp +++ b/src/filepattern/cpp/pattern.cpp @@ -1,8 +1,6 @@ #include "pattern.hpp" -using namespace std; - -void Pattern::getPathFromPattern(const string& path){ +void Pattern::getPathFromPattern(const std::string& path){ this->path_ = path; this->file_pattern_ = path; @@ -13,13 +11,13 @@ void Pattern::getPathFromPattern(const string& path){ auto firstGroup = customGroupFirst ? firstCustomGroup : firstBracketGroup; - if(firstGroup == string::npos) return; // return if none found + if(firstGroup == std::string::npos) return; // return if none found // find first slash before named group while(path[firstGroup] != '/'){ --firstGroup; if(firstGroup == 0) { - throw invalid_argument("Invalid path. Atleast one directory without a named group must be provided."); + throw std::invalid_argument("Invalid path. Atleast one directory without a named group must be provided."); } } ++firstGroup; @@ -29,7 +27,7 @@ void Pattern::getPathFromPattern(const string& path){ } -void Pattern::setGroup(const vector& groups){ +void Pattern::setGroup(const std::vector& groups){ if (groups.size() == 1 && groups[0] == "*__all__*") { this->group_ = groups; @@ -42,7 +40,7 @@ void Pattern::setGroup(const vector& groups){ continue; //this->group = group; } else if(group != "") { - throw invalid_argument("Group by variable must be contained in the pattern."); + throw std::invalid_argument("Group by variable must be contained in the pattern."); } } @@ -51,7 +49,7 @@ void Pattern::setGroup(const vector& groups){ void Pattern::setIsSorted(bool sorted) {this->sorted_ = sorted;} -vector Pattern::getVariables(){ +std::vector Pattern::getVariables(){ return this->variables_; } @@ -59,30 +57,30 @@ void Pattern::filePatternToRegex(){ replace(path_.begin(), path_.end(), '\\', '/'); - tuple vars = getRegex(this->file_pattern_, this->suppress_warnings_); + std::tuple vars = getRegex(this->file_pattern_, this->suppress_warnings_); - this->regex_file_pattern_ = get<0>(vars); - this->variables_ = get<1>(vars); - this->named_groups_ = get<2>(vars); + this->regex_file_pattern_ = std::get<0>(vars); + this->variables_ = std::get<1>(vars); + this->named_groups_ = std::get<2>(vars); } -tuple, vector> Pattern::getRegex(string& pattern, bool suppressWarning){ +std::tuple, std::vector> Pattern::getRegex(std::string& pattern, bool suppressWarning){ getNewNaming(pattern, suppressWarning); // regex to match variables std::string e_str = "(\\{(\\w+):([0\\.dcf+]+)\\})|(\\(\\?P<(\\w+)>(.+)\\))"; - std::regex e(e_str, regex_constants::ECMAScript); // check for bracket expressions or named groups + std::regex e(e_str, std::regex_constants::ECMAScript); // check for bracket expressions or named groups std::string group_str = "\\?P<(\\w+)>(.+)"; - std::regex group(group_str, regex_constants::ECMAScript); // check for regex named groups + std::regex group(group_str, std::regex_constants::ECMAScript); // check for regex named groups std::string var_str = "\\{(\\w+):([0\\.dcf+]+)\\}"; - std::regex var(var_str, regex_constants::ECMAScript); // pattern style of groups (e.g {r:ddd}) + std::regex var(var_str, std::regex_constants::ECMAScript); // pattern style of groups (e.g {r:ddd}) - map patternMap; // map of variable types to regex equivalent + std::map patternMap; // map of variable types to regex equivalent patternMap['d'] = "[0-9]"; // integer matchingß patternMap['c'] = "[a-zA-Z]"; // character matching patternMap['f'] = "[0-9\\.]"; // match any floating point @@ -90,13 +88,13 @@ tuple, vector> Pattern::getRegex(string& pattern, patternMap['0'] = "0"; // 0 for decimal patterns patternMap['+'] = "+"; // arbitrary length matching - string str, rgx; // temp string and regex - vector> matches; // map between bracket expression and regex - vector variables; // store variable names - string patternCopy = pattern; // get a copy of pattern since regex_search is inplace + std::string str, rgx; // temp string and regex + std::vector> matches; // map between bracket expression and regex + std::vector variables; // store variable names + std::string patternCopy = pattern; // get a copy of pattern since regex_search is inplace std::smatch sm, m; // regex matches - string temp; + std::string temp; // extract bracket expressions from pattern and store regex while (regex_search(patternCopy, m, e)){ temp = m[0]; @@ -135,8 +133,8 @@ tuple, vector> Pattern::getRegex(string& pattern, } - string regexFilePattern = pattern; - vector namedGroups; + std::string regexFilePattern = pattern; + std::vector namedGroups; // Replace bracket groups with regex capture groups for(const auto& match: matches){ @@ -152,23 +150,23 @@ tuple, vector> Pattern::getRegex(string& pattern, } -Tuple Pattern::getVariableMapMultDir(const string& filePath, const smatch& sm){ +Tuple Pattern::getVariableMapMultDir(const std::string& filePath, const std::smatch& sm){ Tuple tup; bool matched = false; - string basename; - string file = s::getBaseName(filePath); + std::string basename; + std::string file = s::getBaseName(filePath); // iterate over matched files, checking if filename already exists for (auto& valid_file: this->valid_files_) { #ifdef JAVA_BINDING - basename = s::getBaseName(s::to_string(get<1>(valid_file)[0])); // store the basename + basename = s::getBaseName(s::to_string(std::get<1>(valid_file)[0])); // store the basename #else - basename = s::getBaseName(get<1>(valid_file)[0].string()); // store the basename + basename = s::getBaseName(std::get<1>(valid_file)[0].string()); // store the basename #endif // if the filename is found, add the filepath to the vector in the second member of the tuple if(basename == file){ matched = true; - get<1>(valid_file).push_back(filePath); // Add path to existing mapping + std::get<1>(valid_file).push_back(filePath); // Add path to existing mapping break; } } @@ -181,13 +179,13 @@ Tuple Pattern::getVariableMapMultDir(const string& filePath, const smatch& sm){ return tup; } -Tuple Pattern::getVariableMap(const string& filePath, const smatch& sm){ +Tuple Pattern::getVariableMap(const std::string& filePath, const std::smatch& sm){ Tuple tup; // filename matches the pattern std::get<1>(tup).push_back(filePath); - string str; + std::string str; // Extract capture groups from filename and store in mapping for(unsigned int i = 1; i < sm.size(); ++i){ @@ -196,38 +194,38 @@ Tuple Pattern::getVariableMap(const string& filePath, const smatch& sm){ // conserve variable type if (s::is_number(str)) { if (s::is_integer(str)) { - get<0>(tup)[variables_[i-1]] = std::stoi(str); + std::get<0>(tup)[variables_[i-1]] = std::stoi(str); } else { - get<0>(tup)[variables_[i-1]] = std::stod(str); + std::get<0>(tup)[variables_[i-1]] = std::stod(str); } } else { - get<0>(tup)[variables_[i-1]] = str; + std::get<0>(tup)[variables_[i-1]] = str; } - this->variable_occurrences_[this->variables_[i-1]][get<0>(tup)[this->variables_[i-1]]] += 1; // update count of the variable occurrence - this->unique_values_[this->variables_[i-1]].insert(get<0>(tup)[this->variables_[i-1]]); // update the unique values for the variable + this->variable_occurrences_[this->variables_[i-1]][std::get<0>(tup)[this->variables_[i-1]]] += 1; // update count of the variable occurrence + this->unique_values_[this->variables_[i-1]].insert(std::get<0>(tup)[this->variables_[i-1]]); // update the unique values for the variable } return tup; } -std::map> Pattern::getOccurrences(const vector>>& mapping){ +std::map> Pattern::getOccurrences(const std::vector>>& mapping){ // if no variables request, return all variables if(mapping.size() == 0){ return this->variable_occurrences_; } std::map temp; - std::map> occurrences; - string variable; + std::map> occurrences; + std::string variable; // loop over vector passed in that contains the variable mapped to value(s) for(const auto& tup: mapping){ - if(get<1>(tup).size() == 0){ - occurrences[get<0>(tup)] = this->variable_occurrences_[get<0>(tup)]; + if(std::get<1>(tup).size() == 0){ + occurrences[std::get<0>(tup)] = this->variable_occurrences_[std::get<0>(tup)]; } else { - for(const auto& value: get<1>(tup)){ - variable = get<0>(tup); - temp[value] = this->variable_occurrences_[get<0>(tup)][value]; + for(const auto& value: std::get<1>(tup)){ + variable = std::get<0>(tup); + temp[value] = this->variable_occurrences_[std::get<0>(tup)][value]; } occurrences[variable] = temp; } @@ -236,36 +234,36 @@ std::map> Pattern::getOccurrences(const vectorfile_pattern_; } -void Pattern::setPattern(const string& pattern){ +void Pattern::setPattern(const std::string& pattern){ this->file_pattern_ = pattern; } -string Pattern::getRegexPattern(){ +std::string Pattern::getRegexPattern(){ return this->regex_file_pattern_; } void Pattern::printVariables(){ int i = 0; - cout << "The variables are: "; + std::cout << "The variables are: "; int size = this->getVariables().size(); for(const auto& var: this->getVariables()){ - cout << var; + std::cout << var; if(i> Pattern::getUniqueValues(const vector& vec){ +std::map> Pattern::getUniqueValues(const std::vector& vec){ if(vec.size() == 0) return this->unique_values_; // if no variables are passed, return all variables - map> temp; + std::map> temp; // return variables that were requested for(const auto& str: vec){ temp[str] = unique_values_[str]; @@ -274,18 +272,18 @@ map> Pattern::getUniqueValues(const vector& vec){ } -void Pattern::getNewNaming(string& pattern, bool suppressWarnings){ +void Pattern::getNewNaming(std::string& pattern, bool suppressWarnings){ if(pattern == "") return; - string vars = "\\{([rtczyxp+]+)\\}"; // check for old naming style or named grouped + std::string vars = "\\{([rtczyxp+]+)\\}"; // check for old naming style or named grouped std::regex e(vars); - string str; // temp string + std::string str; // temp string - vector> matches; // map between bracket expression and regex + std::vector> matches; // map between bracket expression and regex - string patternCopy = pattern; // get a copy of pattern since regex_search is inplace + std::string patternCopy = pattern; // get a copy of pattern since regex_search is inplace std::smatch m; // regex matches @@ -320,21 +318,21 @@ void Pattern::getNewNaming(string& pattern, bool suppressWarnings){ } } if(replaced && !suppressWarnings){ - cout << "WARNING: The old style of pattern was used. This style may become deprecated in future releases." << endl; - cout << "The recommended pattern to use is: " << pattern << - ". See the documentation for details about the new style." << endl; + std::cout << "WARNING: The old style of pattern was used. This style may become deprecated in future releases." << std::endl; + std::cout << "The recommended pattern to use is: " << pattern << + ". See the documentation for details about the new style." << std::endl; } } -void Pattern::replaceOutputName(Tuple& min, Tuple& max, const string& var, string& outputName, const int idx, string& temp, const regex& patternRegex){ - string file; - smatch sm; - if(get<0>(min)[var] == get<0>(max)[var]){ +void Pattern::replaceOutputName(Tuple& min, Tuple& max, const std::string& var, std::string& outputName, const int idx, std::string& temp, const std::regex& patternRegex){ + std::string file; + std::smatch sm; + if(std::get<0>(min)[var] == std::get<0>(max)[var]){ #ifdef JAVA_BINDING - file = s::getBaseName(get<1>(min)[0]); // get basename of filepath + file = s::getBaseName(std::get<1>(min)[0]); // get basename of filepath #else - file = s::getBaseName((get<1>(min)[0]).string()); // get basename of filepath + file = s::getBaseName((std::get<1>(min)[0]).string()); // get basename of filepath #endif regex_match(file, sm, patternRegex); @@ -345,9 +343,9 @@ void Pattern::replaceOutputName(Tuple& min, Tuple& max, const string& var, strin temp = "("; #ifdef JAVA_BINDING - file = s::getBaseName(get<1>(min)[0]); // get basename of filepath + file = s::getBaseName(std::get<1>(min)[0]); // get basename of filepath #else - file = s::getBaseName((get<1>(min)[0]).string()); // get basename of filepath + file = s::getBaseName((std::get<1>(min)[0]).string()); // get basename of filepath #endif regex_match(file, sm, patternRegex); // find variables @@ -355,9 +353,9 @@ void Pattern::replaceOutputName(Tuple& min, Tuple& max, const string& var, strin temp += "-"; #ifdef JAVA_BINDING - file = s::getBaseName(get<1>(max)[0]); // get basename of filepath + file = s::getBaseName(std::get<1>(max)[0]); // get basename of filepath #else - file = s::getBaseName((get<1>(max)[0]).string()); // get basename of filepath + file = s::getBaseName((std::get<1>(max)[0]).string()); // get basename of filepath #endif regex_match(file, sm, patternRegex); @@ -368,19 +366,19 @@ void Pattern::replaceOutputName(Tuple& min, Tuple& max, const string& var, strin } } -string Pattern::outputNameHelper(vector& vec){ +std::string Pattern::outputNameHelper(std::vector& vec){ if(vec.size() == 0){ vec = this->valid_files_; } - string outputName = this->file_pattern_; + std::string outputName = this->file_pattern_; int idx = 0; int min, max; - smatch sm; - string temp, file; + std::smatch sm; + std::string temp, file; - regex patternRegex(this->regex_file_pattern_); + std::regex patternRegex(this->regex_file_pattern_); for(auto& var: this->variables_){ @@ -397,11 +395,11 @@ string Pattern::outputNameHelper(vector& vec){ } -string Pattern::inferPatternInternal(vector& files, string& variables, const string& startingPattern){ +std::string Pattern::inferPatternInternal(std::vector& files, std::string& variables, const std::string& startingPattern){ variables += "rtczyxp"; // extra variables incase not enough were given - string pattern; + std::string pattern; if(startingPattern == "") { pattern = files[0]; @@ -409,9 +407,9 @@ string Pattern::inferPatternInternal(vector& files, string& variables, c pattern = startingPattern; } - string regexStr; - string patternCpy = pattern; - regex rgx = regex(get<0>(getRegex(patternCpy))); + std::string regexStr; + std::string patternCpy = pattern; + std::regex rgx = std::regex(std::get<0>(getRegex(patternCpy))); for(auto& file : files){ @@ -421,8 +419,8 @@ string Pattern::inferPatternInternal(vector& files, string& variables, c patternCpy = pattern; - regexStr = get<0>(getRegex(patternCpy)); - rgx = regex(regexStr); + regexStr = std::get<0>(getRegex(patternCpy)); + rgx = std::regex(regexStr); } } @@ -433,13 +431,13 @@ string Pattern::inferPatternInternal(vector& files, string& variables, c return pattern; } -string Pattern::swSearch(string& pattern, string& filename, const string& variables){ - string numbers = "0123456789<>"; // numeric values, '<' -> d and '>' -> d+ - string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@$"; // alphabetic values, '@' -> c+ and '$' -> c - smatch sm; // store regex group matches - string rgxStr; // string version of rgx - regex rgx; - string temp, smStr; +std::string Pattern::swSearch(std::string& pattern, std::string& filename, const std::string& variables){ + std::string numbers = "0123456789<>"; // numeric values, '<' -> d and '>' -> d+ + std::string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@$"; // alphabetic values, '@' -> c+ and '$' -> c + std::smatch sm; // store regex group matches + std::string rgxStr; // string version of rgx + std::regex rgx; + std::string temp, smStr; bool match; //vlist = [(0,0,"", "")] @@ -450,7 +448,7 @@ string Pattern::swSearch(string& pattern, string& filename, const string& variab rgxStr += v; rgxStr += "):([dc+]+)\\}"; - rgx = regex(rgxStr); // regex to match + rgx = std::regex(rgxStr); // regex to match match = false; // match if found while(regex_search(pattern, sm, rgx)){ @@ -480,7 +478,7 @@ string Pattern::swSearch(string& pattern, string& filename, const string& variab } // scoring function - map> sab = { + std::map> sab = { {"numeric", { {"match", 2}, {"penalty", 1} @@ -498,7 +496,7 @@ string Pattern::swSearch(string& pattern, string& filename, const string& variab // scoring matrix creation const int m = pattern.length(); const int n = filename.length(); - vector> matrix(m+1, vector(n+1, 0));//[m+1][n+1]; // switch this to linear indexing in future + std::vector> matrix(m+1, std::vector(n+1, 0));//[m+1][n+1]; // switch this to linear indexing in future //fill(matrix[0], matrix[0] + (m+1) * (n+1), 0); @@ -506,7 +504,7 @@ string Pattern::swSearch(string& pattern, string& filename, const string& variab int f_idx = 0; bool pIsNumeric, fIsNumeric; bool pIsCharacter, fIsCharacter; - vector scores; + std::vector scores; int s, wi, wj; // populate scoring matrix @@ -596,7 +594,7 @@ string Pattern::swSearch(string& pattern, string& filename, const string& variab } // traceback, building a new pattern template - string patternTemplate = ""; //(1, filename[col-1]); + std::string patternTemplate = ""; //(1, filename[col-1]); patternTemplate.push_back(filename[col-1]); int lastRow = row; int lastCol = col; @@ -646,7 +644,7 @@ string Pattern::swSearch(string& pattern, string& filename, const string& variab patternTemplate = "@" + patternTemplate; } } else { - throw runtime_error("Non-numeric, non-alphabetic characters found that do not match"); + throw std::runtime_error("Non-numeric, non-alphabetic characters found that do not match"); } } else if(lastCol != col && lastRow != row){ // progrsssion was made so add a placeholder @@ -668,27 +666,27 @@ string Pattern::swSearch(string& pattern, string& filename, const string& variab pattern = patternTemplate; int vi = 0; rgx = "[<>\\$@]+"; - string vdef; + std::string vdef; // Construct a new filepattern - for (auto i = sregex_iterator(patternTemplate.begin(), patternTemplate.end(), rgx); i != sregex_iterator(); ++i) { + for (auto i = std::sregex_iterator(patternTemplate.begin(), patternTemplate.end(), rgx); i != std::sregex_iterator(); ++i) { temp = (*i).str(); - if(temp.find('>') != string::npos){ + if(temp.find('>') != std::string::npos){ vdef = "{"; vdef += variables[vi]; vdef += ":d+}"; - } else if (temp.find('<') != string::npos){ + } else if (temp.find('<') != std::string::npos){ vdef = "{"; vdef += variables[vi]; vdef += ":"; vdef.insert(vdef.length(), temp.length(), 'd'); vdef += "}"; - } else if (temp.find('@') != string::npos) { + } else if (temp.find('@') != std::string::npos) { vdef = "{"; vdef += variables[vi]; vdef += ":c+}"; - } else if (temp.find('$') != string::npos){ + } else if (temp.find('$') != std::string::npos){ vdef = "{"; vdef += variables[vi]; vdef += ":"; @@ -704,7 +702,7 @@ string Pattern::swSearch(string& pattern, string& filename, const string& variab return pattern; } -vector Pattern::getTmpDirs(){ +std::vector Pattern::getTmpDirs(){ return this->tmp_directories_; } diff --git a/src/filepattern/cpp/util/fs_stream.cpp b/src/filepattern/cpp/util/fs_stream.cpp index 9acfa25..fc9ff53 100644 --- a/src/filepattern/cpp/util/fs_stream.cpp +++ b/src/filepattern/cpp/util/fs_stream.cpp @@ -1,8 +1,6 @@ #include "fs_stream.hpp" -using namespace std; - -FilesystemStream::FilesystemStream(const string& path, bool recursive, const string& block_size, const bool is_infer) { +FilesystemStream::FilesystemStream(const std::string& path, bool recursive, const std::string& block_size, const bool is_infer) { this->valid_files_size_ = 0; this->is_infer_ = is_infer; // Is a call from inferPattern (handles memory footprint calculation different if true) this->tmpdir_ = fs::temp_directory_path().string(); // temp directory to store txt files @@ -18,7 +16,7 @@ FilesystemStream::FilesystemStream(const string& path, bool recursive, const str bool created = fs::create_directory(this->tmpdir_); // create temp directory //throw error if temp directory cannot be created if (!created) { - throw runtime_error("Could not create temporary file."); + throw std::runtime_error("Could not create temporary file."); } fs::permissions(this->tmpdir_, fs::perms::all); @@ -34,7 +32,7 @@ FilesystemStream::FilesystemStream(const string& path, bool recursive, const str this->txt_input_ = true; this->inputfile_.open(path); if (!this->inputfile_.is_open()) { - throw invalid_argument("Invalid path \"" + path + "\"."); + throw std::invalid_argument("Invalid path \"" + path + "\"."); } } else { @@ -46,7 +44,7 @@ FilesystemStream::FilesystemStream(const string& path, bool recursive, const str } } catch (const std::runtime_error& e) { - string error = "No directory found. Invalid path \"" + path + "\"."; + std::string error = "No directory found. Invalid path \"" + path + "\"."; throw std::runtime_error(error); } } @@ -56,7 +54,7 @@ FilesystemStream::~FilesystemStream() { d::remove_dir(this->tmpdir_); } -vector FilesystemStream::getBlock(){ +std::vector FilesystemStream::getBlock(){ if(this->txt_input_){ return this->getBlockTxt(); } else { @@ -64,27 +62,27 @@ vector FilesystemStream::getBlock(){ } } -void FilesystemStream::updateSize(long& size, const string& current){ +void FilesystemStream::updateSize(long& size, const std::string& current){ if(this->is_infer_){ - size += sizeof(vector>) + current.length()*sizeof(vector) + 2*current.length()*sizeof(int); + size += sizeof(std::vector>) + current.length()*sizeof(std::vector) + 2*current.length()*sizeof(int); } - if(size > this->block_size_) throw runtime_error("The block size is too small. Block size must be increased."); + if(size > this->block_size_) throw std::runtime_error("The block size is too small. Block size must be increased."); } -vector FilesystemStream::getBlockIterator(){ +std::vector FilesystemStream::getBlockIterator(){ - vector vec; // files to return - long previous_size = sizeof(vector); //memory used + std::vector vec; // files to return + long previous_size = sizeof(std::vector); //memory used - string current; + std::string current; if(this->recursive_){ try { current = (*this->recursive_directory_iterator_).path().string(); - } catch (exception& e){ - cout << e.what() << endl; + } catch (std::exception& e){ + std::cout << e.what() << std::endl; } this->updateSize(previous_size, current); @@ -102,8 +100,8 @@ vector FilesystemStream::getBlockIterator(){ try{ current = (*this->recursive_directory_iterator_).path().string(); - } catch (exception& e){ - cout << e.what() << endl; + } catch (std::exception& e){ + std::cout << e.what() << std::endl; } } } @@ -111,8 +109,8 @@ vector FilesystemStream::getBlockIterator(){ try { current = (*this->directory_iterator_).path().string(); - } catch (exception& e){ - cout << e.what() << endl; + } catch (std::exception& e){ + std::cout << e.what() << std::endl; } this->updateSize(previous_size, current); @@ -130,8 +128,8 @@ vector FilesystemStream::getBlockIterator(){ try{ current = (*this->directory_iterator_).path().string(); - } catch (exception& e){ - cout << e.what() << endl; + } catch (std::exception& e){ + std::cout << e.what() << std::endl; } } } @@ -141,11 +139,11 @@ vector FilesystemStream::getBlockIterator(){ return vec; } -vector FilesystemStream::getBlockTxt(){ +std::vector FilesystemStream::getBlockTxt(){ - vector vec; - long size = sizeof(vector); - string str; + std::vector vec; + long size = sizeof(std::vector); + std::string str; // get string while less than block size this->inputfile_ >> str; this->updateSize(size, str); @@ -160,22 +158,22 @@ vector FilesystemStream::getBlockTxt(){ } //check if end of file - streampos ptr = this->inputfile_.tellg(); + std::streampos ptr = this->inputfile_.tellg(); if(!(this->inputfile_ >> str)){ this->empty_ = true; } - this->inputfile_.seekg(ptr, ios::beg); + this->inputfile_.seekg(ptr, std::ios::beg); return vec; } -string FilesystemStream::getTmpPath(){ +std::string FilesystemStream::getTmpPath(){ return this->tmpdir_; } -void FilesystemStream::writeBlock(const vector& vec){ - ofstream file(this->out_name_, ios_base::app); +void FilesystemStream::writeBlock(const std::vector& vec){ + std::ofstream file(this->out_name_, std::ios_base::app); for(const auto& element: vec){ file << '\n' << element; @@ -185,13 +183,13 @@ void FilesystemStream::writeBlock(const vector& vec){ void FilesystemStream::writeValidFiles(const Tuple& mapping){ this->counter_++; - ofstream file(this->valid_files_, ios_base::app); + std::ofstream file(this->valid_files_, std::ios_base::app); - for(const auto& element: get<0>(mapping)){ + for(const auto& element: std::get<0>(mapping)){ file << element.first << ":" << s::to_string(element.second) << '\n'; } - for(const auto& element: get<1>(mapping)){ + for(const auto& element: std::get<1>(mapping)){ #ifdef JAVA_BINDING file << element << "," << '\n'; #else @@ -203,27 +201,27 @@ void FilesystemStream::writeValidFiles(const Tuple& mapping){ file.close(); this->valid_files_empty_ = false; if(this->counter_ == 1){ - this->map_size_ = get<0>(mapping).size(); + this->map_size_ = std::get<0>(mapping).size(); this->infile_.open(this->valid_files_); } } -vector FilesystemStream::getValidFilesBlock(){ +std::vector FilesystemStream::getValidFilesBlock(){ if(this->valid_files_empty_){ - vector empty; + std::vector empty; return empty; } - vector vec; + std::vector vec; Tuple member; - long size = sizeof(vector); + long size = sizeof(std::vector); Map map; - string str; - string key, value; + std::string str; + std::string key, value; int value_length; size_t pos; Types result; @@ -232,21 +230,21 @@ vector FilesystemStream::getValidFilesBlock(){ while(size < this->block_size_ && this->infile_ >> str){ if (map.size() == (this->map_size_)) { - size += sizeof(map) + sizeof(vector); + size += sizeof(map) + sizeof(std::vector); //sizeof(Tuple) + for(const auto& item : map){ size += item.first.length() + s::size(item.second); } - get<0>(member) = map; + std::get<0>(member) = map; str.pop_back(); // remove trailing comma - get<1>(member).push_back(str); + std::get<1>(member).push_back(str); vec.push_back(member); map.clear(); - get<1>(member).clear(); + std::get<1>(member).clear(); this->infile_ >> str; } @@ -266,19 +264,19 @@ vector FilesystemStream::getValidFilesBlock(){ size += value_length + pos; } - streampos ptr = this->infile_.tellg(); + std::streampos ptr = this->infile_.tellg(); if(!(this->infile_ >> str)){ this->valid_files_empty_ = true; this->infile_.close(); } //ptr +=1; - this->infile_.seekg(ptr, ios::beg); + this->infile_.seekg(ptr, std::ios::beg); this->temp_map_ = map; return vec; } long double FilesystemStream::currentSize(const int& stringSize, const long double& previous_size){ - return sizeof(string) + stringSize + previous_size; + return sizeof(std::string) + stringSize + previous_size; } bool FilesystemStream::isEmpty() { @@ -289,15 +287,15 @@ bool FilesystemStream::endOfValidFiles(){ return this->valid_files_empty_; } -string FilesystemStream::getFilePath(){ +std::string FilesystemStream::getFilePath(){ return this->out_name_; } -string FilesystemStream::getValidFilesPath(){ +std::string FilesystemStream::getValidFilesPath(){ return this->valid_files_; } -string FilesystemStream::getBlockSizeStr(){ +std::string FilesystemStream::getBlockSizeStr(){ return this->block_size_str_; } @@ -305,12 +303,12 @@ string FilesystemStream::getBlockSizeStr(){ Tuple FilesystemStream::getFileByIndex(int i) { int file_index = (this->map_size_ + 1)*i + 1; //cout << "file: " << file_index << endl; - ifstream in = ifstream(this->valid_files_); + std::ifstream in = std::ifstream(this->valid_files_); - string str; + std::string str; Map map; - string key, value; + std::string key, value; int value_length; size_t pos; Types result; @@ -337,9 +335,9 @@ Tuple FilesystemStream::getFileByIndex(int i) { in >> str; Tuple temp; - get<0>(temp) = map; + std::get<0>(temp) = map; str.pop_back(); // remove trailing comma - get<1>(temp).push_back(str); + std::get<1>(temp).push_back(str); return temp; } @@ -348,8 +346,8 @@ unsigned int FilesystemStream::getValidFilesSize(){ return this->valid_files_size_; } -vector FilesystemStream::getValidFilesSlice(int i, int j, int step){ - vector vec; +std::vector FilesystemStream::getValidFilesSlice(int i, int j, int step){ + std::vector vec; for(int index = i; index < j; index += step){ vec.push_back(getFileByIndex(index)); diff --git a/src/filepattern/cpp/util/sort.cpp b/src/filepattern/cpp/util/sort.cpp index 3663564..9f03379 100644 --- a/src/filepattern/cpp/util/sort.cpp +++ b/src/filepattern/cpp/util/sort.cpp @@ -1,12 +1,10 @@ #include "sort.hpp" -using namespace std; - ExternalMergeSort::ExternalMergeSort(const Structure& structure, - const string& input_file_name, - const string& output_file_name, - const string& block_size, - const string& variable="", + const std::string& input_file_name, + const std::string& output_file_name, + const std::string& block_size, + const std::string& variable="", const int map_size=0) { this->input_file_name_ = input_file_name; @@ -25,7 +23,7 @@ ExternalMergeSort::ExternalMergeSort(const Structure& structure, // Create temp directory bool created = fs::create_directory(tmpdir_); if(!created) { - throw invalid_argument("Error creating tmp directory"); + throw std::invalid_argument("Error creating tmp directory"); } // Call function based on data type @@ -41,22 +39,22 @@ ExternalMergeSort::ExternalMergeSort(const Structure& structure, long ExternalMergeSort::currentSize(const int string_size, const long& previous_size){ - return sizeof(string) + string_size + previous_size; // update amount of memory being used + return sizeof(std::string) + string_size + previous_size; // update amount of memory being used } void ExternalMergeSort::sortMapFile(){ int vector_capacity = 0; - int string_size = sizeof(string); + int string_size = sizeof(std::string); int block_num = 0; - string block_name; + std::string block_name; - ifstream infile(this->input_file_name_); + std::ifstream infile(this->input_file_name_); if (infile.peek() == std::ifstream::traits_type::eof()) return; - vector vec; + std::vector vec; // Get blocks of files and sort while not end of file while (true) { @@ -67,18 +65,18 @@ void ExternalMergeSort::sortMapFile(){ // sort block if(this->sort_variable_ == ""){ sort(vec.begin(), vec.end(), [](Tuple& a, Tuple& b) { - return get<1>(a)[0] < get<1>(b)[0]; + return std::get<1>(a)[0] < std::get<1>(b)[0]; }); } else { sort(vec.begin(), vec.end(), [&sort_variable_ = as_const(sort_variable_)](Tuple& a, Tuple& b) { - return get<0>(a)[sort_variable_] < get<0>(b)[sort_variable_]; + return std::get<0>(a)[sort_variable_] < std::get<0>(b)[sort_variable_]; }); } //write to file - block_name = this->tmpdir_ + to_string(block_num) + ".txt";; - ofstream out(block_name, ios_base::app); + block_name = this->tmpdir_ + std::to_string(block_num) + ".txt";; + std::ofstream out(block_name, std::ios_base::app); this->writeMapTmpFile(out, vec); this->files_to_merge_.push_back(block_name); @@ -93,15 +91,15 @@ void ExternalMergeSort::sortMapFile(){ } /*This function could be made cleaner by writing each map to a single line in the text file*/ -bool ExternalMergeSort::getMapBlock(ifstream& infile, vector& vec){ +bool ExternalMergeSort::getMapBlock(std::ifstream& infile, std::vector& vec){ Tuple member; - long size = sizeof(vector); + long size = sizeof(std::vector); Map map; - string str; - string key, value; + std::string str; + std::string key, value; int value_length; size_t pos; Types result; @@ -111,17 +109,17 @@ bool ExternalMergeSort::getMapBlock(ifstream& infile, vector& vec){ // if map is full, add to block if (map.size() == (this->map_size_)) { - size += sizeof(map) + sizeof(vector); + size += sizeof(map) + sizeof(std::vector); for(const auto& item : map){ size += item.first.length() + s::size(item.second); } - get<0>(member) = map; + std::get<0>(member) = map; str.pop_back(); // remove trailing comma - get<1>(member).push_back(str); + std::get<1>(member).push_back(str); vec.push_back(member); map.clear(); - get<1>(member).clear(); + std::get<1>(member).clear(); infile >> str; } // split line on : (line is in the form "key:value") @@ -144,11 +142,11 @@ bool ExternalMergeSort::getMapBlock(ifstream& infile, vector& vec){ this->temp_map_ = map; // check if end of file - streampos ptr = infile.tellg(); + std::streampos ptr = infile.tellg(); if(!(infile >> str)){ return false; } else { - infile.seekg(ptr, ios::beg); + infile.seekg(ptr, std::ios::beg); return true; } @@ -156,14 +154,14 @@ bool ExternalMergeSort::getMapBlock(ifstream& infile, vector& vec){ void ExternalMergeSort::sortFile(){ - string str; + std::string str; int block_num = 0; - string block_name; - double previous_size = sizeof(vector); + std::string block_name; + double previous_size = sizeof(std::vector); - ifstream file(this->input_file_name_); + std::ifstream file(this->input_file_name_); if(!file.is_open()){ - throw invalid_argument("Cannot find file \"" + this->input_file_name_ + "\"."); + throw std::invalid_argument("Cannot find file \"" + this->input_file_name_ + "\"."); } if (file.peek() == std::ifstream::traits_type::eof()) return; @@ -185,10 +183,10 @@ void ExternalMergeSort::sortFile(){ // sort the block sort(this->vec_.begin(), this->vec_.end(), - [](const string& a, const string& b) {return a < b;}); + [](const std::string& a, const std::string& b) {return a < b;}); //write to file - block_name = to_string(block_num) + ".txt";; + block_name = std::to_string(block_num) + ".txt";; this->writeTmpFile(block_name); // add txt file name to vector to be merged @@ -196,7 +194,7 @@ void ExternalMergeSort::sortFile(){ //clear vec and continue reading file this->vec_.clear(); - previous_size = sizeof(vector); + previous_size = sizeof(std::vector); block_num++; } @@ -204,22 +202,22 @@ void ExternalMergeSort::sortFile(){ this->merge(); } -void ExternalMergeSort::writeTmpFile(string& output){ +void ExternalMergeSort::writeTmpFile(std::string& output){ output = this->tmpdir_ + output; - ofstream out(output); + std::ofstream out(output); for (const auto& element: this->vec_) out << element << "\n"; } -void ExternalMergeSort::writeMapTmpFile(ofstream& file, - const vector& vec){ +void ExternalMergeSort::writeMapTmpFile(std::ofstream& file, + const std::vector& vec){ //loop over map, writing the key value pair in the form "key:value\n" for(const auto& mapping: vec){ - for(const auto& element: get<0>(mapping)){ + for(const auto& element: std::get<0>(mapping)){ file << element.first << ":" << s::to_string(element.second) << '\n'; } - for(const auto& element: get<1>(mapping)){ + for(const auto& element: std::get<1>(mapping)){ #ifdef JAVA_BINDING file << element << "," << '\n'; #else @@ -229,16 +227,16 @@ void ExternalMergeSort::writeMapTmpFile(ofstream& file, } } -void ExternalMergeSort::writeFile(const string& output){ +void ExternalMergeSort::writeFile(const std::string& output){ // write string to output file - ofstream out(output); + std::ofstream out(output); for (const auto& element: this->vec_) out << element << "\n"; } void ExternalMergeSort::merge(){ - string str1, str2; - string out_file; + std::string str1, str2; + std::string out_file; int length; int count; int iter = 0; @@ -248,22 +246,22 @@ void ExternalMergeSort::merge(){ length = files_to_merge_.size(); for(int i = 0; i < (length - (length%2)); i+=2){ - ifstream file1(files_to_merge_[i]); - ifstream file2(files_to_merge_[i+1]); + std::ifstream file1(files_to_merge_[i]); + std::ifstream file2(files_to_merge_[i+1]); if(!file1.is_open()) { - string error = "Cannot open file " + files_to_merge_[i]; - throw runtime_error(error); + std::string error = "Cannot open file " + files_to_merge_[i]; + throw std::runtime_error(error); } if(!file2.is_open()) { - string error = "Cannot open file " + files_to_merge_[i+1]; - throw runtime_error(error); + std::string error = "Cannot open file " + files_to_merge_[i+1]; + throw std::runtime_error(error); } - out_file = this->tmpdir_ + to_string(iter) + "_" + to_string(i) + "_" + to_string(i+1) + ".txt"; + out_file = this->tmpdir_ + std::to_string(iter) + "_" + std::to_string(i) + "_" + std::to_string(i+1) + ".txt"; this->new_files_.push_back(out_file); - ofstream outfile(out_file, ios_base::app); + std::ofstream outfile(out_file, std::ios_base::app); count = 0; while(true){ @@ -330,10 +328,10 @@ void ExternalMergeSort::merge(){ new_files_.clear(); } - ifstream file(this->files_to_merge_[0]); + std::ifstream file(this->files_to_merge_[0]); if(!file.is_open()){ - string error = "Cannot open file " + files_to_merge_[0]; - throw runtime_error(error); + std::string error = "Cannot open file " + files_to_merge_[0]; + throw std::runtime_error(error); } this->vec_.clear(); @@ -347,7 +345,7 @@ void ExternalMergeSort::merge(){ void ExternalMergeSort::mergeMaps(){ - string out_file; + std::string out_file; int length; int iter = 0; @@ -355,13 +353,13 @@ void ExternalMergeSort::mergeMaps(){ length = files_to_merge_.size(); for(int i = 0; i < (length - (length%2)); i+=2){ - out_file = this->tmpdir_ + to_string(iter) + "_" + to_string(i) + "_" + to_string(i+1) + ".txt"; + out_file = this->tmpdir_ + std::to_string(iter) + "_" + std::to_string(i) + "_" + std::to_string(i+1) + ".txt"; this->twoWayMergeMaps(files_to_merge_[i], files_to_merge_[i+1], out_file); this->new_files_.push_back(out_file); } if(length%2 == 1){ - out_file = out_file = this->tmpdir_ + to_string(iter) + "_" + to_string(length-1) + "_" + to_string(length-1) + ".txt"; + out_file = out_file = this->tmpdir_ + std::to_string(iter) + "_" + std::to_string(length-1) + "_" + std::to_string(length-1) + ".txt"; this->twoWayMergeMaps(files_to_merge_[files_to_merge_.size()-1], new_files_[0], out_file); new_files_[0] = out_file; } @@ -371,38 +369,38 @@ void ExternalMergeSort::mergeMaps(){ new_files_.clear(); } - ifstream file(files_to_merge_[0]); + std::ifstream file(files_to_merge_[0]); if(!file.is_open()){ - string error = "Cannot open file " + files_to_merge_[0]; - throw runtime_error(error); + std::string error = "Cannot open file " + files_to_merge_[0]; + throw std::runtime_error(error); } Tuple map1, map2; - ofstream outfile(this->output_file_name_); + std::ofstream outfile(this->output_file_name_); while(m::getMap(file, map1, this->map_size_)){ m::writeMap(outfile, map1); - get<1>(map1).clear(); + std::get<1>(map1).clear(); } file.close(); outfile.close(); } -void ExternalMergeSort::twoWayMergeMaps(const string& fileName1, const string& fileName2, const string& out_file){ +void ExternalMergeSort::twoWayMergeMaps(const std::string& fileName1, const std::string& fileName2, const std::string& out_file){ - ifstream file1(fileName1); - ifstream file2(fileName2); + std::ifstream file1(fileName1); + std::ifstream file2(fileName2); if(!file1.is_open()) { - string error = "Cannot open file " + fileName1; - throw runtime_error(error); + std::string error = "Cannot open file " + fileName1; + throw std::runtime_error(error); } if(!file2.is_open()) { - string error = "Cannot open file " + fileName2; - throw runtime_error(error); + std::string error = "Cannot open file " + fileName2; + throw std::runtime_error(error); } - ofstream outfile(out_file); + std::ofstream outfile(out_file); int count = 0; Tuple map1, map2; @@ -451,7 +449,7 @@ void ExternalMergeSort::twoWayMergeMaps(const string& fileName1, const string& f break; } else if(this->sort_variable_ == ""){ - if(get<1>(map1)[0] <= get<1>(map2)[0]) { + if(std::get<1>(map1)[0] <= std::get<1>(map2)[0]) { //write str1 to output m::writeMap(outfile, map1); m::getMap(file1, map1, this->map_size_); @@ -460,7 +458,7 @@ void ExternalMergeSort::twoWayMergeMaps(const string& fileName1, const string& f m::writeMap(outfile, map2); m::getMap(file2, map2, this->map_size_); } - } else if(get<0>(map1)[this->sort_variable_] <= get<0>(map2)[this->sort_variable_]){ + } else if(std::get<0>(map1)[this->sort_variable_] <= std::get<0>(map2)[this->sort_variable_]){ //write str1 to output m::writeMap(outfile, map1); m::getMap(file1, map1, this->map_size_); diff --git a/src/filepattern/cpp/util/stream.cpp b/src/filepattern/cpp/util/stream.cpp index d570ac6..b037070 100644 --- a/src/filepattern/cpp/util/stream.cpp +++ b/src/filepattern/cpp/util/stream.cpp @@ -1,8 +1,6 @@ #include "stream.hpp" -using namespace std; - -Stream::Stream(const string& block_size, const bool is_infer) { +Stream::Stream(const std::string& block_size, const bool is_infer) { this->is_infer = is_infer; this->tmpdir = fs::temp_directory_path().string(); this->tmpdir += "/fs_stream_tmp_" + s::getTimeString() + "/"; @@ -19,15 +17,15 @@ Stream::Stream(const string& block_size, const bool is_infer) { bool created = fs::create_directory(tmpdir); if (!created) { - throw runtime_error("Could not create temporary file."); + throw std::runtime_error("Could not create temporary file."); } this->out_name = tmpdir + "/temp.txt"; this->infile.open(valid_files); } -void Stream::writeBlock(const vector& vec){ - ofstream file(this->out_name, ios_base::app); +void Stream::writeBlock(const std::vector& vec){ + std::ofstream file(this->out_name, std::ios_base::app); for(const auto& element: vec){ file << '\n' << element; @@ -37,24 +35,24 @@ void Stream::writeBlock(const vector& vec){ void Stream::writeValidFiles(const Tuple& mapping){ counter++; - ofstream file(valid_files, ios_base::app); + std::ofstream file(valid_files, ios_base::app); } -vector Stream::getValidFilesBlock(){ +std::vector Stream::getValidFilesBlock(){ if(this->valid_files_empty){ - vector empty; + std::vector empty; return empty; } - vector vec; + std::vector vec; Tuple member; - long size = sizeof(vector); + long size = sizeof(std::vector); Map map; - string str; - string key, value; + std::string str; + std::string key, value; int value_length; size_t pos; Types result; @@ -63,21 +61,21 @@ vector Stream::getValidFilesBlock(){ while(size < block_size && this->infile >> str){ if (map.size() == (this->map_size)) { - size += sizeof(map) + sizeof(vector); + size += sizeof(map) + sizeof(std::vector); //sizeof(Tuple) + for(const auto& item : map){ size += item.first.length() + s::size(item.second); } - get<0>(member) = map; + std::get<0>(member) = map; str.pop_back(); // remove trailing comma - get<1>(member).push_back(str); + std::get<1>(member).push_back(str); vec.push_back(member); map.clear(); - get<1>(member).clear(); + std::get<1>(member).clear(); infile >> str; } @@ -97,18 +95,18 @@ vector Stream::getValidFilesBlock(){ size += value_length + pos; } - streampos ptr = infile.tellg(); + std::streampos ptr = infile.tellg(); if(!(this->infile >> str)){ valid_files_empty = true; } //ptr +=1; - infile.seekg(ptr, ios::beg); + infile.seekg(ptr, std::ios::beg); this->temp_map = map; return vec; } -long double Stream::currentSize(const int stringSize, const long double& previousSize){ - return sizeof(string) + stringSize + previousSize; +long double Stream::currentSize(const int stringSize, const long double& previousSize){ + return sizeof(std::string) + stringSize + previousSize; } bool Stream::isEmpty() { @@ -119,14 +117,14 @@ bool Stream::endOfValidFiles(){ return valid_files_empty; } -string Stream::getFilePath(){ +std::string Stream::getFilePath(){ return out_name; } -string Stream::getValidFilesPath(){ +std::string Stream::getValidFilesPath(){ return valid_files; } -string Stream::getBlockSizeStr(){ +std::string Stream::getBlockSizeStr(){ return this->block_size_str; } diff --git a/src/filepattern/cpp/util/vector_parser.cpp b/src/filepattern/cpp/util/vector_parser.cpp index 6ee7924..c2eede3 100644 --- a/src/filepattern/cpp/util/vector_parser.cpp +++ b/src/filepattern/cpp/util/vector_parser.cpp @@ -1,15 +1,13 @@ #include "vector_parser.hpp" #include "../internal/vectorpattern.hpp" -using namespace std; - std::string VectorParser::getFileName(const std::string& stitching_vector_line){ std::regex pattern =std::regex("file:\\s*(.*?);"); - smatch sm; + std::smatch sm; - if (regex_search(stitching_vector_line, sm, pattern)){ + if (std::regex_search(stitching_vector_line, sm, pattern)){ return sm[1]; } else { throw std::runtime_error("Filename not found in the line: " + stitching_vector_line); @@ -42,16 +40,16 @@ bool VectorParser::isStitchingVector(const std::string& line) { void VectorParser::parseVectorLine(Tuple& tup, const std::string& stitching_vector_line, const std::vector& stitch_variables, - const std::vector& stitch_regex, + const std::vector& stitch_regex, std::vector variables) { - smatch sm; + std::smatch sm; std::unordered_map vars; for (const auto& rgx: stitch_regex) { // if the line from the stitching vector matching the stitching vector regex pattern - if (regex_search(stitching_vector_line, sm, rgx)){ + if (std::regex_search(stitching_vector_line, sm, rgx)){ if (sm[1] == "grid") { @@ -111,12 +109,12 @@ void VectorParser::parseVectorLine(Tuple& tup, // maintain datatype of variable if(s::is_number(variable.second)){ if (s::is_integer(variable.second)) { - get<0>(tup)[variable.first] = stoi(variable.second); + std::get<0>(tup)[variable.first] = stoi(variable.second); } else { - get<0>(tup)[variable.first] = stod(variable.second); + std::get<0>(tup)[variable.first] = stod(variable.second); } } else { - get<0>(tup)[variable.first] = variable.second; + std::get<0>(tup)[variable.first] = variable.second; } }