diff options
author | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:07:15 +0200 |
---|---|---|
committer | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:07:15 +0200 |
commit | 8318453bf9d4fd50b137ff6c6fc8d1fd22aa6395 (patch) | |
tree | 975c6e590c31e56007006a23e7b15d0245d75b08 /shared/globalFunctions.h | |
parent | 3.6 (diff) | |
download | FreeFileSync-8318453bf9d4fd50b137ff6c6fc8d1fd22aa6395.tar.gz FreeFileSync-8318453bf9d4fd50b137ff6c6fc8d1fd22aa6395.tar.bz2 FreeFileSync-8318453bf9d4fd50b137ff6c6fc8d1fd22aa6395.zip |
3.7
Diffstat (limited to 'shared/globalFunctions.h')
-rw-r--r-- | shared/globalFunctions.h | 183 |
1 files changed, 91 insertions, 92 deletions
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 <wx/longlong.h> #include <memory> #include <sstream> -#include <fstream> class wxStopWatch; @@ -39,27 +38,20 @@ T abs(const T& d) //absolute value } template <class T> -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 <class T> +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 <class T> T readNumber(std::ifstream& stream); -template <class T> void writeNumber(std::ofstream& stream, T number); +template <class T> T readNumber(std::istream& stream); +template <class T> 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 <class T> -void removeRowsFromVector(const std::set<unsigned int>& rowsToRemove, std::vector<T>& grid) -{ - if (rowsToRemove.empty()) - return; - - std::set<unsigned int>::const_iterator rowToSkipIndex = rowsToRemove.begin(); - unsigned int rowToSkip = *rowToSkipIndex; - - if (rowToSkip >= grid.size()) - return; - - typename std::vector<T>::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<size_t>& rowsToRemove, std::vector<T>& grid); //bubble sort using swap() instead of assignment: useful if assignment is very expensive template <class VectorData, typename CompareFct> -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 <class ForwardIterator, class T> -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 <class ForwardIterator, class T, typename Compare> -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<T>()); } @@ -199,43 +128,113 @@ wxString getCodeLocation(const wxString file, const int line); + + + + + + //---------------Inline Implementation--------------------------------------------------- template <class T> 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<char*>(&result), sizeof(T)); - return result; + std::ostringstream ss; + ss << number; + return ss.str(); } template <class T> 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<const char*>(&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 <class T> +void globalFunctions::removeRowsFromVector(const std::set<size_t>& rowsToRemove, std::vector<T>& grid) +{ + if (rowsToRemove.empty()) + return; + + std::set<size_t>::const_iterator rowToSkipIndex = rowsToRemove.begin(); + size_t rowToSkip = *rowToSkipIndex; + + if (rowToSkip >= grid.size()) + return; + + typename std::vector<T>::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 <class VectorData, typename CompareFct> +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 <class ForwardIterator, class T, typename Compare> 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 <class T> 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<char*>(&result), sizeof(T)); + return result; } +template <class T> 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<const char*>(&number), sizeof(T)); } |