Skip to content

Commit

Permalink
Add is_sorted_until
Browse files Browse the repository at this point in the history
Implement is_sorted with is_sorted_until
Move is_sorted to thrust/sort.h
Deprecate thrust/is_sorted.h
Replace #include <thrust/is_sorted.h> with #include <thrust/sort.h>

Fixes issue 214
  • Loading branch information
jaredhoberock committed Sep 24, 2010
1 parent 6472386 commit ad8e187
Show file tree
Hide file tree
Showing 11 changed files with 346 additions and 186 deletions.
2 changes: 1 addition & 1 deletion examples/cpp_integration/host.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <thrust/host_vector.h>
#include <thrust/generate.h>
#include <thrust/is_sorted.h>
#include <thrust/sort.h>
#include <cstdlib>
#include <iostream>
#include <iterator>
Expand Down
1 change: 0 additions & 1 deletion performance/host_sort.test
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
PREAMBLE = \
"""
#include <thrust/sort.h>
#include <thrust/is_sorted.h>
#include <algorithm>
"""

Expand Down
1 change: 0 additions & 1 deletion performance/host_sort_by_key.test
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
PREAMBLE = \
"""
#include <thrust/sort.h>
#include <thrust/is_sorted.h>
"""

INITIALIZE = \
Expand Down
22 changes: 21 additions & 1 deletion testing/is_sorted.cu
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <unittest/unittest.h>
#include <thrust/is_sorted.h>
#include <thrust/sort.h>

template <class Vector>
Expand Down Expand Up @@ -33,6 +32,26 @@ void TestIsSortedSimple(void)
}
DECLARE_VECTOR_UNITTEST(TestIsSortedSimple);

template <class Vector>
void TestIsSortedRepeatedElements(void)
{
Vector v(10);

v[0] = 0;
v[1] = 1;
v[2] = 1;
v[3] = 2;
v[4] = 3;
v[5] = 4;
v[6] = 5;
v[7] = 5;
v[8] = 5;
v[9] = 6;

ASSERT_EQUAL(true, thrust::is_sorted(v.begin(), v.end()));
}
DECLARE_VECTOR_UNITTEST(TestIsSortedRepeatedElements);


template <class Vector>
void TestIsSorted(void)
Expand All @@ -53,3 +72,4 @@ void TestIsSorted(void)
ASSERT_EQUAL(thrust::is_sorted(v.begin(), v.end()), true);
}
DECLARE_VECTOR_UNITTEST(TestIsSorted);

97 changes: 97 additions & 0 deletions testing/is_sorted_until.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <unittest/unittest.h>
#include <thrust/sort.h>

template<typename Vector>
void TestIsSortedUntilSimple(void)
{
typedef typename Vector::value_type T;
typedef typename Vector::iterator Iterator;

Vector v(4);
v[0] = 0; v[1] = 5; v[2] = 8; v[3] = 0;

Iterator first = v.begin();

Iterator last = v.begin() + 0;
Iterator ref = last;
ASSERT_EQUAL_QUIET(ref, thrust::is_sorted_until(first, last));

last = v.begin() + 1;
ref = last;
ASSERT_EQUAL_QUIET(ref, thrust::is_sorted_until(first, last));

last = v.begin() + 2;
ref = last;
ASSERT_EQUAL_QUIET(ref, thrust::is_sorted_until(first, last));

last = v.begin() + 3;
ref = v.begin() + 3;
ASSERT_EQUAL_QUIET(ref, thrust::is_sorted_until(first, last));

last = v.begin() + 4;
ref = v.begin() + 3;
ASSERT_EQUAL_QUIET(ref, thrust::is_sorted_until(first, last));

last = v.begin() + 3;
ref = v.begin() + 3;
ASSERT_EQUAL_QUIET(ref, thrust::is_sorted_until(first, last, thrust::less<T>()));

last = v.begin() + 4;
ref = v.begin() + 3;
ASSERT_EQUAL_QUIET(ref, thrust::is_sorted_until(first, last, thrust::less<T>()));

last = v.begin() + 1;
ref = v.begin() + 1;
ASSERT_EQUAL_QUIET(ref, thrust::is_sorted_until(first, last, thrust::greater<T>()));

last = v.begin() + 4;
ref = v.begin() + 1;
ASSERT_EQUAL_QUIET(ref, thrust::is_sorted_until(first, last, thrust::greater<T>()));

first = v.begin() + 2;
last = v.begin() + 4;
ref = v.begin() + 4;
ASSERT_EQUAL_QUIET(ref, thrust::is_sorted_until(first, last, thrust::greater<T>()));
}
DECLARE_VECTOR_UNITTEST(TestIsSortedUntilSimple);

template<typename Vector>
void TestIsSortedUntilRepeatedElements(void)
{
Vector v(10);

v[0] = 0;
v[1] = 1;
v[2] = 1;
v[3] = 2;
v[4] = 3;
v[5] = 4;
v[6] = 5;
v[7] = 5;
v[8] = 5;
v[9] = 6;

ASSERT_EQUAL_QUIET(v.end(), thrust::is_sorted_until(v.begin(), v.end()));
}
DECLARE_VECTOR_UNITTEST(TestIsSortedUntilRepeatedElements);

template <class Vector>
void TestIsSortedUntil(void)
{
typedef typename Vector::value_type T;

const size_t n = (1 << 16) + 13;

Vector v = unittest::random_integers<T>(n);

v[0] = 1;
v[1] = 0;

ASSERT_EQUAL_QUIET(v.begin() + 1, thrust::is_sorted_until(v.begin(), v.end()));

thrust::sort(v.begin(), v.end());

ASSERT_EQUAL_QUIET(v.end(), thrust::is_sorted_until(v.begin(), v.end()));
}
DECLARE_VECTOR_UNITTEST(TestIsSortedUntil);

8 changes: 4 additions & 4 deletions thrust/detail/internal_functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ struct equal_to_value
};

template <typename Predicate>
struct tuple_equal_to
struct tuple_binary_predicate
{
typedef bool result_type;

__host__ __device__
tuple_equal_to(const Predicate& p) : pred(p) {}
tuple_binary_predicate(const Predicate& p) : pred(p) {}

template<typename Tuple>
__host__ __device__
Expand All @@ -147,12 +147,12 @@ struct tuple_equal_to
};

template <typename Predicate>
struct tuple_not_equal_to
struct tuple_not_binary_predicate
{
typedef bool result_type;

__host__ __device__
tuple_not_equal_to(const Predicate& p) : pred(p) {}
tuple_not_binary_predicate(const Predicate& p) : pred(p) {}

template<typename Tuple>
__host__ __device__
Expand Down
52 changes: 0 additions & 52 deletions thrust/detail/is_sorted.inl

This file was deleted.

2 changes: 1 addition & 1 deletion thrust/detail/mismatch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ thrust::pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1,
ZipIterator zipped_first = thrust::make_zip_iterator(thrust::make_tuple(first1,first2));
ZipIterator zipped_last = thrust::make_zip_iterator(thrust::make_tuple(last1, first2));

ZipIterator result = thrust::find_if(zipped_first, zipped_last, thrust::detail::tuple_not_equal_to<BinaryPredicate>(pred));
ZipIterator result = thrust::find_if_not(zipped_first, zipped_last, thrust::detail::tuple_binary_predicate<BinaryPredicate>(pred));

return thrust::make_pair(thrust::get<0>(result.get_iterator_tuple()),
thrust::get<1>(result.get_iterator_tuple()));
Expand Down
46 changes: 46 additions & 0 deletions thrust/detail/sort.inl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <thrust/sort.h>
#include <thrust/iterator/iterator_traits.h>
#include <thrust/distance.h>
#include <thrust/functional.h>
#include <thrust/detail/dispatch/sort.h>

Expand Down Expand Up @@ -131,5 +132,50 @@ template<typename RandomAccessKeyIterator,
typename thrust::iterator_space<RandomAccessValueIterator>::type());
}

template<typename ForwardIterator>
bool is_sorted(ForwardIterator first,
ForwardIterator last)
{
return thrust::is_sorted_until(first, last) == last;
} // end is_sorted()

template<typename ForwardIterator,
typename Compare>
bool is_sorted(ForwardIterator first,
ForwardIterator last,
Compare comp)
{
return thrust::is_sorted_until(first, last, comp) == last;
} // end is_sorted()

template<typename ForwardIterator>
ForwardIterator is_sorted_until(ForwardIterator first,
ForwardIterator last)
{
typedef typename thrust::iterator_value<ForwardIterator>::type InputType;

return thrust::is_sorted_until(first, last, thrust::less<InputType>());
} // end is_sorted_until()

template<typename ForwardIterator,
typename Compare>
ForwardIterator is_sorted_until(ForwardIterator first,
ForwardIterator last,
Compare comp)
{
if(thrust::distance(first,last) < 2) return last;

typedef thrust::tuple<ForwardIterator,ForwardIterator> IteratorTuple;
typedef thrust::zip_iterator<IteratorTuple> ZipIterator;

ForwardIterator first_plus_one = first;
thrust::advance(first_plus_one, 1);

ZipIterator zipped_first = thrust::make_zip_iterator(thrust::make_tuple(first_plus_one, first));
ZipIterator zipped_last = thrust::make_zip_iterator(thrust::make_tuple(last, first));

return thrust::get<0>(thrust::find_if(zipped_first, zipped_last, thrust::detail::tuple_binary_predicate<Compare>(comp)).get_iterator_tuple());
} // end is_sorted_until()

} // end namespace thrust

Loading

0 comments on commit ad8e187

Please sign in to comment.