summaryrefslogtreecommitdiff
path: root/shared/globalFunctions.h
diff options
context:
space:
mode:
authorDaniel Wilhelm <daniel@wili.li>2014-04-18 17:00:50 +0200
committerDaniel Wilhelm <daniel@wili.li>2014-04-18 17:00:50 +0200
commit4ecfd41e36533d858c98d051ef70cab80e69e972 (patch)
treeca07d8745967d2c6a7123a5d32269cfbfaa7bd6c /shared/globalFunctions.h
parent2.2 (diff)
downloadFreeFileSync-4ecfd41e36533d858c98d051ef70cab80e69e972.tar.gz
FreeFileSync-4ecfd41e36533d858c98d051ef70cab80e69e972.tar.bz2
FreeFileSync-4ecfd41e36533d858c98d051ef70cab80e69e972.zip
2.3
Diffstat (limited to 'shared/globalFunctions.h')
-rw-r--r--shared/globalFunctions.h109
1 files changed, 65 insertions, 44 deletions
diff --git a/shared/globalFunctions.h b/shared/globalFunctions.h
index 00566b5f..b53ba596 100644
--- a/shared/globalFunctions.h
+++ b/shared/globalFunctions.h
@@ -18,17 +18,6 @@ class wxStopWatch;
namespace globalFunctions
{
//------------------------------------------------
-// GLOBALS
-//------------------------------------------------
-#ifdef FFS_WIN
- const wxChar FILE_NAME_SEPARATOR = '\\';
- static const wxChar* const LINE_BREAK = wxT("\r\n"); //internal linkage
-#elif defined FFS_LINUX
- const wxChar FILE_NAME_SEPARATOR = '/';
- static const wxChar* const LINE_BREAK = wxT("\n");
-#endif
-
-//------------------------------------------------
// FUNCTIONS
//------------------------------------------------
inline
@@ -78,32 +67,80 @@ namespace globalFunctions
}
- //Note: the following lines are a performance optimization for deleting elements from a vector. It is incredibly faster to create a new
-//vector and leave specific elements out than to delete row by row and force recopying of most elements for each single deletion (linear vs quadratic runtime)
+//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<int>& rowsToRemove, std::vector<T>& grid)
+ void removeRowsFromVector(const std::set<unsigned int>& rowsToRemove, std::vector<T>& grid)
{
- if (rowsToRemove.size() > 0)
- {
- std::vector<T> temp;
+ if (rowsToRemove.empty())
+ return;
+
+ std::set<unsigned int>::const_iterator rowToSkipIndex = rowsToRemove.begin();
+ unsigned int rowToSkip = *rowToSkipIndex;
- std::set<int>::const_iterator rowToSkipIndex = rowsToRemove.begin();
- int rowToSkip = *rowToSkipIndex;
+ if (rowToSkip >= grid.size())
+ return;
- for (int i = 0; i < int(grid.size()); ++i)
+ 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
{
- if (i != rowToSkip)
- temp.push_back(grid[i]);
- 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 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]))
{
- ++rowToSkipIndex;
- if (rowToSkipIndex != rowsToRemove.end())
- rowToSkip = *rowToSkipIndex;
+ folderCmp[j + 1].swap(folderCmp[j]);
+ swapped = true;
}
- }
- grid.swap(temp);
+
+ 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;
+ }
}
@@ -148,20 +185,4 @@ wxString getCodeLocation(const wxString file, const int line);
//speed alternative: wxLogDebug(wxT("text")) + DebugView
-//############################################################################
-class RuntimeException //Exception class used to notify of general runtime exceptions
-{
-public:
- RuntimeException(const wxString& txt) : errorMessage(txt) {}
-
- wxString show() const
- {
- return errorMessage;
- }
-
-private:
- wxString errorMessage;
-};
-
-
#endif // GLOBALFUNCTIONS_H_INCLUDED
bgstack15