summaryrefslogtreecommitdiff
path: root/shared/globalFunctions.h
diff options
context:
space:
mode:
authorDaniel Wilhelm <daniel@wili.li>2014-04-18 17:01:29 +0200
committerDaniel Wilhelm <daniel@wili.li>2014-04-18 17:01:29 +0200
commit9a2a524f1e311853d08050be2dcdddc09ac7759a (patch)
treed8e4a24169fce88c2d89931d58514889a0bcb0ea /shared/globalFunctions.h
parent2.3 (diff)
downloadFreeFileSync-9a2a524f1e311853d08050be2dcdddc09ac7759a.tar.gz
FreeFileSync-9a2a524f1e311853d08050be2dcdddc09ac7759a.tar.bz2
FreeFileSync-9a2a524f1e311853d08050be2dcdddc09ac7759a.zip
3.0
Diffstat (limited to 'shared/globalFunctions.h')
-rw-r--r--shared/globalFunctions.h288
1 files changed, 192 insertions, 96 deletions
diff --git a/shared/globalFunctions.h b/shared/globalFunctions.h
index b53ba596..74387959 100644
--- a/shared/globalFunctions.h
+++ b/shared/globalFunctions.h
@@ -9,6 +9,8 @@
#include <wx/longlong.h>
#include <memory>
#include <sstream>
+#include <fstream>
+#include <wx/stream.h>
class wxInputStream;
class wxOutputStream;
@@ -20,129 +22,131 @@ namespace globalFunctions
//------------------------------------------------
// FUNCTIONS
//------------------------------------------------
- inline
- int round(double d) //little rounding function
- {
- return static_cast<int>(d < 0 ? d - .5 : d + .5);
- }
+inline
+int round(double d) //little rounding function
+{
+ return static_cast<int>(d < 0 ? d - .5 : d + .5);
+}
- template <class T>
- inline
- T abs(const T& d) //absolute value
- {
- return(d < 0 ? -d : d);
- }
+template <class T>
+inline
+T abs(const T& d) //absolute value
+{
+ return(d < 0 ? -d : d);
+}
- 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();
- }
+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();
+}
- wxString numberToWxString(const unsigned int number); //convert number to wxString
- wxString numberToWxString(const int number); //convert number to wxString
- wxString numberToWxString(const float number); //convert number to wxString
+wxString numberToWxString(const unsigned int number); //convert number to wxString
+wxString numberToWxString(const int number); //convert number to wxString
+wxString numberToWxString(const float number); //convert number to wxString
- 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 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
+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
+unsigned int getDigitCount(const unsigned int number); //count number of digits
- int readInt(std::ifstream& stream); //read int from file stream
- void writeInt(std::ofstream& stream, const int number); //write int to filestream
+//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);
- int readInt(wxInputStream& stream); //read int from file stream
- void writeInt(wxOutputStream& stream, const int number); //write int to filestream
+template <class T> T readNumber(wxInputStream& stream);
+template <class T> void writeNumber(wxOutputStream& stream, T number);
- inline
- wxLongLong convertToSigned(const wxULongLong number)
- {
- return wxLongLong(number.GetHi(), number.GetLo());
- }
+inline
+wxLongLong convertToSigned(const wxULongLong number)
+{
+ return wxLongLong(number.GetHi(), number.GetLo());
+}
//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;
+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;
+ std::set<unsigned int>::const_iterator rowToSkipIndex = rowsToRemove.begin();
+ unsigned int rowToSkip = *rowToSkipIndex;
- if (rowToSkip >= grid.size())
- return;
+ if (rowToSkip >= grid.size())
+ return;
- typename std::vector<T>::iterator insertPos = grid.begin() + rowToSkip;
+ typename std::vector<T>::iterator insertPos = grid.begin() + rowToSkip;
- for (unsigned int i = rowToSkip; i < grid.size(); ++i)
+ for (unsigned int i = rowToSkip; i < grid.size(); ++i)
+ {
+ if (i != rowToSkip)
{
- if (i != rowToSkip)
- {
- *insertPos = grid[i];
- ++insertPos;
- }
- else
- {
- ++rowToSkipIndex;
- if (rowToSkipIndex != rowsToRemove.end())
- rowToSkip = *rowToSkipIndex;
- }
+ *insertPos = grid[i];
+ ++insertPos;
}
- grid.erase(insertPos, grid.end());
- }
-
- //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)
+ else
{
- 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;
+ ++rowToSkipIndex;
+ if (rowToSkipIndex != rowsToRemove.end())
+ rowToSkip = *rowToSkipIndex;
}
}
+ grid.erase(insertPos, grid.end());
+}
- //enhanced binary search template: returns an iterator
- template <class ForwardIterator, class T>
- inline
- ForwardIterator custom_binary_search(ForwardIterator first, ForwardIterator last, const T& value)
+//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)
{
- first = lower_bound(first, last, value);
- if (first != last && !(value < *first))
- return first;
- else
- return last;
- }
+ 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;
+ }
- //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;
+ 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;
+}
+
+//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;
+}
+}
+
//############################################################################
class Performance
@@ -185,4 +189,96 @@ wxString getCodeLocation(const wxString file, const int line);
//speed alternative: wxLogDebug(wxT("text")) + DebugView
+
+
+
+
+
+
+
+
+
+
+
+
+
+//---------------Inline Implementation---------------------------------------------------
+template <class T>
+inline
+T globalFunctions::readNumber(std::ifstream& stream)
+{
+ T result = 0;
+ stream.read(reinterpret_cast<char*>(&result), sizeof(T));
+ return result;
+}
+
+
+template <class T>
+inline
+void globalFunctions::writeNumber(std::ofstream& stream, T number)
+{
+ stream.write(reinterpret_cast<const char*>(&number), sizeof(T));
+}
+
+
+template <class T>
+inline
+T globalFunctions::readNumber(wxInputStream& stream)
+{
+ T result = 0;
+ stream.Read(&result, sizeof(T));
+ return result;
+}
+
+
+template <class T>
+inline
+void globalFunctions::writeNumber(wxOutputStream& stream, T number)
+{
+ stream.Write(&number, sizeof(T));
+}
+
+
+inline
+wxString globalFunctions::numberToWxString(const unsigned int number)
+{
+ return wxString::Format(wxT("%u"), number);
+}
+
+
+inline
+wxString globalFunctions::numberToWxString(const int number)
+{
+ return wxString::Format(wxT("%i"), number);
+}
+
+
+inline
+wxString globalFunctions::numberToWxString(const float number)
+{
+ return wxString::Format(wxT("%f"), number);
+}
+
+
+inline
+int globalFunctions::stringToInt(const std::string& number)
+{
+ return atoi(number.c_str());
+}
+
+
+inline
+long globalFunctions::stringToLong(const std::string& number)
+{
+ return atol(number.c_str());
+}
+
+
+inline
+double globalFunctions::stringToDouble(const std::string& number)
+{
+ return atof(number.c_str());
+}
+
+
#endif // GLOBALFUNCTIONS_H_INCLUDED
bgstack15