From 8318453bf9d4fd50b137ff6c6fc8d1fd22aa6395 Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Fri, 18 Apr 2014 17:07:15 +0200 Subject: 3.7 --- shared/globalFunctions.h | 183 +++++++++++++++++++++++------------------------ 1 file changed, 91 insertions(+), 92 deletions(-) (limited to 'shared/globalFunctions.h') diff --git a/shared/globalFunctions.h b/shared/globalFunctions.h index f4f2caa4..daf495b5 100644 --- a/shared/globalFunctions.h +++ b/shared/globalFunctions.h @@ -15,7 +15,6 @@ #include #include #include -#include class wxStopWatch; @@ -39,27 +38,20 @@ T abs(const T& d) //absolute value } template -inline -std::string numberToString(const T& number) //convert number to string the C++ way -{ - std::stringstream ss; - ss << number; - return ss.str(); -} +std::string numberToString(const T& number); //convert number to string the C++ way +template +void stringToNumber(const std::string& input, T& number); //convert string to number the C++ way -int stringToInt( const std::string& number); //convert String to number -long stringToLong( const std::string& number); //convert String to number -double stringToDouble(const std::string& number); //convert String to number int wxStringToInt( const wxString& number); //convert wxString to number double wxStringToDouble(const wxString& number); //convert wxString to number -unsigned int getDigitCount(const unsigned int number); //count number of digits +size_t getDigitCount(size_t number); //count number of digits //read/write numbers: int, long, unsigned int ... ect -template T readNumber(std::ifstream& stream); -template void writeNumber(std::ofstream& stream, T number); +template T readNumber(std::istream& stream); +template void writeNumber(std::ostream& stream, T number); inline wxLongLong convertToSigned(const wxULongLong number) @@ -70,78 +62,15 @@ wxLongLong convertToSigned(const wxULongLong number) //Note: the following lines are a performance optimization for deleting elements from a vector: linear runtime at most! template -void removeRowsFromVector(const std::set& rowsToRemove, std::vector& grid) -{ - if (rowsToRemove.empty()) - return; - - std::set::const_iterator rowToSkipIndex = rowsToRemove.begin(); - unsigned int rowToSkip = *rowToSkipIndex; - - if (rowToSkip >= grid.size()) - return; - - typename std::vector::iterator insertPos = grid.begin() + rowToSkip; - - for (unsigned int i = rowToSkip; i < grid.size(); ++i) - { - if (i != rowToSkip) - { - *insertPos = grid[i]; - ++insertPos; - } - else - { - ++rowToSkipIndex; - if (rowToSkipIndex != rowsToRemove.end()) - rowToSkip = *rowToSkipIndex; - } - } - grid.erase(insertPos, grid.end()); -} +void removeRowsFromVector(const std::set& rowsToRemove, std::vector& grid); //bubble sort using swap() instead of assignment: useful if assignment is very expensive template -void bubbleSwapSort(VectorData& folderCmp, CompareFct compare) -{ - for (int i = folderCmp.size() - 2; i >= 0; --i) - { - bool swapped = false; - for (int j = 0; j <= i; ++j) - if (compare(folderCmp[j + 1], folderCmp[j])) - { - folderCmp[j + 1].swap(folderCmp[j]); - swapped = true; - } - - if (!swapped) - return; - } -} - -//enhanced binary search template: returns an iterator -template -inline -ForwardIterator custom_binary_search(ForwardIterator first, ForwardIterator last, const T& value) -{ - first = lower_bound(first, last, value); - if (first != last && !(value < *first)) - return first; - else - return last; -} +void bubbleSwapSort(VectorData& folderCmp, CompareFct compare); //enhanced binary search template: returns an iterator template -inline -ForwardIterator custom_binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp) -{ - first = lower_bound(first, last, value, comp); - if (first != last && !comp(value, *first)) - return first; - else - return last; -} +ForwardIterator custom_binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp = std::less()); } @@ -198,44 +127,114 @@ wxString getCodeLocation(const wxString file, const int line); + + + + + + //---------------Inline Implementation--------------------------------------------------- template inline -T globalFunctions::readNumber(std::ifstream& stream) +std::string globalFunctions::numberToString(const T& number) //convert number to string the C++ way { - T result = 0; - stream.read(reinterpret_cast(&result), sizeof(T)); - return result; + std::ostringstream ss; + ss << number; + return ss.str(); } template inline -void globalFunctions::writeNumber(std::ofstream& stream, T number) +void globalFunctions::stringToNumber(const std::string& input, T& number) //convert number to string the C++ way { - stream.write(reinterpret_cast(&number), sizeof(T)); + std::istringstream ss(input); + ss >> number; } +//Note: the following lines are a performance optimization for deleting elements from a vector: linear runtime at most! +template +void globalFunctions::removeRowsFromVector(const std::set& rowsToRemove, std::vector& grid) +{ + if (rowsToRemove.empty()) + return; + + std::set::const_iterator rowToSkipIndex = rowsToRemove.begin(); + size_t rowToSkip = *rowToSkipIndex; + + if (rowToSkip >= grid.size()) + return; + + typename std::vector::iterator insertPos = grid.begin() + rowToSkip; + + for (size_t i = rowToSkip; i < grid.size(); ++i) + { + if (i != rowToSkip) + { + *insertPos = grid[i]; + ++insertPos; + } + else + { + ++rowToSkipIndex; + if (rowToSkipIndex != rowsToRemove.end()) + rowToSkip = *rowToSkipIndex; + } + } + grid.erase(insertPos, grid.end()); +} + + +//bubble sort using swap() instead of assignment: useful if assignment is very expensive +template +void globalFunctions::bubbleSwapSort(VectorData& folderCmp, CompareFct compare) +{ + for (int i = folderCmp.size() - 2; i >= 0; --i) + { + bool swapped = false; + for (int j = 0; j <= i; ++j) + if (compare(folderCmp[j + 1], folderCmp[j])) + { + folderCmp[j + 1].swap(folderCmp[j]); + swapped = true; + } + + if (!swapped) + return; + } +} + + +//enhanced binary search template: returns an iterator +template inline -int globalFunctions::stringToInt(const std::string& number) +ForwardIterator globalFunctions::custom_binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp) { - return atoi(number.c_str()); + first = lower_bound(first, last, value, comp); + if (first != last && !comp(value, *first)) + return first; + else + return last; } +template inline -long globalFunctions::stringToLong(const std::string& number) +T globalFunctions::readNumber(std::istream& stream) { - return atol(number.c_str()); + T result = 0; + stream.read(reinterpret_cast(&result), sizeof(T)); + return result; } +template inline -double globalFunctions::stringToDouble(const std::string& number) +void globalFunctions::writeNumber(std::ostream& stream, T number) { - return atof(number.c_str()); + stream.write(reinterpret_cast(&number), sizeof(T)); } -- cgit