summaryrefslogtreecommitdiff
path: root/shared/globalFunctions.h
blob: daf495b554dfdeb2e8418e9312246ba4e349b2ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) 2008-2010 ZenJu (zhnmju123 AT gmx.de)                    *
// **************************************************************************
//
#ifndef GLOBALFUNCTIONS_H_INCLUDED
#define GLOBALFUNCTIONS_H_INCLUDED

#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <wx/string.h>
#include <wx/longlong.h>
#include <memory>
#include <sstream>

class wxStopWatch;


namespace globalFunctions
{
//------------------------------------------------
//      FUNCTIONS
//------------------------------------------------
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>
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    wxStringToInt(   const wxString& number); //convert wxString to number
double wxStringToDouble(const wxString& number); //convert wxString to number

size_t getDigitCount(size_t number); //count number of digits

//read/write numbers: int, long, unsigned int ... ect
template <class T> T readNumber(std::istream& stream);
template <class T> void writeNumber(std::ostream& stream, T number);

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<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);

//enhanced binary search template: returns an iterator
template <class ForwardIterator, class T, typename Compare>
ForwardIterator custom_binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp = std::less<T>());
}


//############################################################################
class Performance
{
public:
    wxDEPRECATED(Performance()); //generate compiler warnings as a reminder to remove code after measurements
    ~Performance();
    void showResult();

private:
    bool resultWasShown;
    std::auto_ptr<wxStopWatch> timer;
};

//two macros for quick performance measurements
#define PERF_START Performance a;
#define PERF_STOP  a.showResult();


//############################################################################
class wxFile;
class DebugLog
{
public:
    wxDEPRECATED(DebugLog());
    ~DebugLog();
    void write(const wxString& logText);

private:
    wxString assembleFileName();
    wxString logfileName;
    int lineCount;
    wxFile* logFile; //logFile.close(); <- not needed
};
extern DebugLog logDebugInfo;
wxString getCodeLocation(const wxString file, const int line);

//small macro for writing debug information into a logfile
#define WRITE_DEBUG_LOG(x) logDebugInfo.write(getCodeLocation(__TFILE__, __LINE__) + x);
//speed alternative: wxLogDebug(wxT("text")) + DebugView





















//---------------Inline Implementation---------------------------------------------------
template <class T>
inline
std::string globalFunctions::numberToString(const T& number) //convert number to string the C++ way
{
    std::ostringstream ss;
    ss << number;
    return ss.str();
}


template <class T>
inline
void globalFunctions::stringToNumber(const std::string& input, T& number) //convert number to string the C++ way
{
    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
ForwardIterator globalFunctions::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;
}


template <class T>
inline
T globalFunctions::readNumber(std::istream& stream)
{
    T result = 0;
    stream.read(reinterpret_cast<char*>(&result), sizeof(T));
    return result;
}


template <class T>
inline
void globalFunctions::writeNumber(std::ostream& stream, T number)
{
    stream.write(reinterpret_cast<const char*>(&number), sizeof(T));
}


#endif // GLOBALFUNCTIONS_H_INCLUDED
bgstack15