Browse files

Merge pull request #1 from gintenlabo/master

Fixed some bugs
  • Loading branch information...
2 parents 97f9f42 + 4d4b76a commit ec210fedbc36d3e63164ebea9256e86a34353d5f @gfx committed Oct 22, 2011
Showing with 30 additions and 5 deletions.
  1. +25 −0 test.cpp
  2. +5 −5 timsort.hpp
View
25 test.cpp
@@ -148,6 +148,31 @@ BOOST_AUTO_TEST_CASE( c_array ) {
BOOST_CHECK_EQUAL(a[4], 9);
}
+struct NonDefaultConstructible
+{
+ int i;
+
+ NonDefaultConstructible( int i_ )
+ : i( i_ ) {}
+
+ friend bool operator<( NonDefaultConstructible const& x, NonDefaultConstructible const& y ) {
+ return x.i < y.i;
+ }
+
+};
+
+BOOST_AUTO_TEST_CASE( non_default_constructible ) {
+ NonDefaultConstructible a[] = { 7, 1, 5, 3, 9 };
+
+ timsort(a, a + sizeof(a) / sizeof(a[0]), std::less<NonDefaultConstructible>());
+
+ BOOST_CHECK_EQUAL(a[0].i, 1);
+ BOOST_CHECK_EQUAL(a[1].i, 3);
+ BOOST_CHECK_EQUAL(a[2].i, 5);
+ BOOST_CHECK_EQUAL(a[3].i, 7);
+ BOOST_CHECK_EQUAL(a[4].i, 9);
+}
+
enum id { foo, bar, baz };
typedef std::pair<int, id> pair_t;
bool less_in_first(pair_t x, pair_t y) {
View
10 timsort.hpp
@@ -141,7 +141,7 @@ class TimSort {
++start;
}
for( ; start < hi; ++start ) {
- const value_t pivot = MOVE(*start);
+ /*const*/ value_t pivot = MOVE(*start);
iter_t left = lo;
iter_t right = start;
@@ -413,8 +413,8 @@ class TimSort {
void mergeLo(iter_t base1, diff_t len1, iter_t base2, diff_t len2) {
assert( len1 > 0 && len2 > 0 && base1 + len1 == base2 );
- tmp_.resize(len1);
- std::copy(base1, base1 + len1, tmp_.begin());
+ tmp_.clear(); tmp_.reserve(len1);
+ std::copy(base1, base1 + len1, std::back_inserter(tmp_));
tmp_iter_t cursor1 = tmp_.begin();
iter_t cursor2 = base2;
@@ -539,8 +539,8 @@ class TimSort {
void mergeHi(iter_t base1, diff_t len1, iter_t base2, diff_t len2) {
assert( len1 > 0 && len2 > 0 && base1 + len1 == base2 );
- tmp_.resize(len2);
- std::copy(base2, base2 + len2, tmp_.begin());
+ tmp_.clear(); tmp_.reserve(len2);
+ std::copy(base2, base2 + len2, std::back_inserter(tmp_));
iter_t cursor1 = base1 + (len1 - 1);
tmp_iter_t cursor2 = tmp_.begin() + (len2 - 1);

0 comments on commit ec210fe

Please sign in to comment.