Skip to content

Commit e39ca95

Browse files
mattreecebentleygfx
authored andcommitted
C++03/move compliance, better support for compiler/library feature detection (#22)
C++03/move compliance, better support for compiler/library feature detection
1 parent 2c1a5da commit e39ca95

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

timsort.hpp

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* - http://svn.python.org/projects/python/trunk/Objects/listobject.c
66
* - http://cr.openjdk.java.net/~martin/webrevs/openjdk7/timsort/raw_files/new/src/share/classes/java/util/TimSort.java
77
*
8-
* Copyright (c) 2011 Fuji, Goro (gfx) <gfuji@cpan.org>.
8+
* Copyright (c) 2011 Fuji, Goro (gfx) <gfuji@cpan.org>. C++03/move-compliance modifications by Matt Bentley 2017 (mattreecebentley@gmail.com)
99
*
1010
* Permission is hereby granted, free of charge, to any person obtaining a copy
1111
* of this software and associated documentation files (the "Software"), to
@@ -31,9 +31,8 @@
3131

3232
#include <vector>
3333
#include <cassert>
34-
#include <iterator>
35-
#include <algorithm>
36-
#include <utility>
34+
#include <algorithm> // std::copy
35+
#include <functional> // std::less
3736

3837
#ifdef ENABLE_TIMSORT_LOG
3938
#include <iostream>
@@ -42,20 +41,38 @@
4241
#define GFX_TIMSORT_LOG(expr) ((void)0)
4342
#endif
4443

45-
#if __cplusplus >= 201103L && !DISABLE_STD_MOVE
46-
#define ENABLE_STD_MOVE 1
47-
#endif
48-
49-
#if ENABLE_STD_MOVE
50-
#define GFX_TIMSORT_MOVE(x) std::move(x)
51-
#define GFX_TIMSORT_MOVE_RANGE(in1, in2, out) std::move((in1), (in2), (out))
52-
#define GFX_TIMSORT_MOVE_BACKWARD(in1, in2, out) std::move_backward((in1), (in2), (out))
44+
// If compiler supports both type traits and move semantics - will cover most but not all compilers/std libraries:
45+
#if (defined(_MSC_VER) && _MSC_VER >= 1700) || ((defined(__cplusplus) && __cplusplus >= 201103L && !defined(_LIBCPP_VERSION)) && ((!defined(__GNUC__) || __GNUC__ >= 5)) && (!defined(__GLIBCXX__) || __GLIBCXX__ >= 20150422))
46+
#include <iterator> // iterator_traits
47+
#include <utility> // std::move
48+
49+
#define GFX_TIMSORT_MOVE(x) (std::is_move_constructible<value_t>::value && std::is_move_assignable<value_t>::value) ? std::move(x) : (x)
50+
#define GFX_TIMSORT_MOVE_RANGE(in1, in2, out) \
51+
if (std::is_move_constructible<value_t>::value && std::is_move_assignable<value_t>::value) \
52+
{ \
53+
std::move((in1), (in2), (out)); \
54+
} \
55+
else \
56+
{ \
57+
std::copy((in1), (in2), (out)); \
58+
}
59+
#define GFX_TIMSORT_MOVE_BACKWARD(in1, in2, out) \
60+
if (std::is_move_constructible<value_t>::value && std::is_move_assignable<value_t>::value) \
61+
{ \
62+
std::move_backward((in1), (in2), (out)); \
63+
} \
64+
else \
65+
{ \
66+
std::copy_backward((in1), (in2), (out)); \
67+
}
5368
#else
54-
#define GFX_TIMSORT_MOVE(x) (x)
55-
#define GFX_TIMSORT_MOVE_RANGE(in1, in2, out) std::copy((in1), (in2), (out))
56-
#define GFX_TIMSORT_MOVE_BACKWARD(in1, in2, out) std::copy_backward((in1), (in2), (out))
69+
#define GFX_TIMSORT_MOVE(x) (x)
70+
#define GFX_TIMSORT_MOVE_RANGE(in1, in2, out) std::copy((in1), (in2), (out));
71+
#define GFX_TIMSORT_MOVE_BACKWARD(in1, in2, out) std::copy_backward((in1), (in2), (out));
5772
#endif
5873

74+
75+
5976
namespace gfx {
6077

6178
// ---------------------------------------

0 commit comments

Comments
 (0)