Skip to content

Commit ca26c03

Browse files
committed
cleanup
1 parent 6197068 commit ca26c03

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

timsort.hpp

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,40 @@
3737

3838
#ifdef ENABLE_TIMSORT_LOG
3939
#include <iostream>
40-
#define LOG(expr) (std::clog << "# " << __func__ << ": " << expr << std::endl)
40+
#define GFX_TIMSORT_LOG(expr) (std::clog << "# " << __func__ << ": " << expr << std::endl)
4141
#else
42-
#define LOG(expr) ((void)0)
42+
#define GFX_TIMSORT_LOG(expr) ((void)0)
4343
#endif
4444

4545
#if ENABLE_STD_MOVE && __cplusplus >= 201103L
46-
#define MOVE(x) std::move(x)
46+
#define GFX_TIMSORT_MOVE(x) std::move(x)
4747
#else
48-
#define MOVE(x) (x)
48+
#define GFX_TIMSORT_MOVE(x) (x)
4949
#endif
5050

5151
namespace gfx {
5252

53+
// ---------------------------------------
54+
// Declaration
55+
// ---------------------------------------
56+
57+
/**
58+
* Same as std::stable_sort(first, last).
59+
*/
60+
template<typename RandomAccessIterator>
61+
inline void timsort(RandomAccessIterator const first, RandomAccessIterator const last);
62+
63+
/**
64+
* Same as std::stable_sort(first, last, c).
65+
*/
66+
template<typename RandomAccessIterator, typename LessFunction>
67+
inline void timsort(RandomAccessIterator const first, RandomAccessIterator const last, LessFunction compare);
68+
69+
70+
// ---------------------------------------
71+
// Implementation
72+
// ---------------------------------------
73+
5374
template <typename Value, typename LessFunction> class Compare {
5475
public:
5576
typedef Value value_type;
@@ -118,7 +139,7 @@ class TimSort {
118139

119140
if(nRemaining < MIN_MERGE) {
120141
diff_t const initRunLen = countRunAndMakeAscending(lo, hi, c);
121-
LOG("initRunLen: " << initRunLen);
142+
GFX_TIMSORT_LOG("initRunLen: " << initRunLen);
122143
binarySort(lo, hi, lo + initRunLen, c);
123144
return;
124145
}
@@ -146,7 +167,7 @@ class TimSort {
146167
ts.mergeForceCollapse();
147168
assert( ts.pending_.size() == 1 );
148169

149-
LOG("size: " << (hi - lo) << " tmp_.size(): " << ts.tmp_.size() << " pending_.size(): " << ts.pending_.size());
170+
GFX_TIMSORT_LOG("size: " << (hi - lo) << " tmp_.size(): " << ts.tmp_.size() << " pending_.size(): " << ts.pending_.size());
150171
} // sort()
151172

152173
static
@@ -157,13 +178,13 @@ class TimSort {
157178
}
158179
for( ; start < hi; ++start ) {
159180
assert(lo <= start);
160-
/*const*/ value_t pivot = MOVE(*start);
181+
/*const*/ value_t pivot = GFX_TIMSORT_MOVE(*start);
161182

162183
iter_t const pos = std::upper_bound(lo, start, pivot, compare.less_function());
163184
for(iter_t p = start; p > pos; --p) {
164-
*p = MOVE(*(p - 1));
185+
*p = GFX_TIMSORT_MOVE(*(p - 1));
165186
}
166-
*pos = MOVE(pivot);
187+
*pos = GFX_TIMSORT_MOVE(pivot);
167188
}
168189
}
169190

@@ -634,20 +655,20 @@ class TimSort {
634655
friend void timsort(IterT first, IterT last, LessT c);
635656
};
636657

637-
template<typename RandomAccessIterator, typename LessFunction>
638-
inline void timsort(RandomAccessIterator const first, RandomAccessIterator const last, LessFunction c) {
639-
TimSort<RandomAccessIterator, LessFunction>::sort(first, last, c);
640-
}
641-
642658
template<typename RandomAccessIterator>
643659
inline void timsort(RandomAccessIterator const first, RandomAccessIterator const last) {
644660
typedef typename std::iterator_traits<RandomAccessIterator>::value_type value_type;
645661
timsort(first, last, std::less<value_type>());
646662
}
647663

664+
template<typename RandomAccessIterator, typename LessFunction>
665+
inline void timsort(RandomAccessIterator const first, RandomAccessIterator const last, LessFunction compare) {
666+
TimSort<RandomAccessIterator, LessFunction>::sort(first, last, compare);
667+
}
668+
648669
} // namespace gfx
649670

650-
#undef LOG
651-
#undef MOVE
671+
#undef GFX_TIMSORT_LOG
672+
#undef GFX_TIMSORT_MOVE
652673
#endif // GFX_TIMSORT_HPP
653674

0 commit comments

Comments
 (0)