~ubuntu-branches/ubuntu/utopic/libthrust/utopic

« back to all changes in this revision

Viewing changes to system/detail/internal/scalar/stable_primitive_sort.inl

  • Committer: Package Import Robot
  • Author(s): Andreas Beckmann
  • Date: 2013-07-10 12:57:33 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130710125733-my19jic71sqsabaj
Tags: 1.7.0-1
* New upstream release.  (Closes: #715362)
* Update watch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright 2008-2012 NVIDIA Corporation
 
3
 *
 
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
 
5
 *  you may not use this file except in compliance with the License.
 
6
 *  You may obtain a copy of the License at
 
7
 *
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 *  Unless required by applicable law or agreed to in writing, software
 
11
 *  distributed under the License is distributed on an "AS IS" BASIS,
 
12
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 *  See the License for the specific language governing permissions and
 
14
 *  limitations under the License.
 
15
 */
 
16
 
 
17
#pragma once
 
18
 
 
19
#include <thrust/detail/config.h>
 
20
#include <thrust/system/detail/internal/scalar/stable_primitive_sort.h>
 
21
#include <thrust/system/detail/internal/scalar/stable_radix_sort.h>
 
22
#include <thrust/functional.h>
 
23
#include <thrust/system/detail/internal/scalar/partition.h>
 
24
#include <thrust/iterator/zip_iterator.h>
 
25
#include <thrust/detail/type_traits.h>
 
26
 
 
27
namespace thrust
 
28
{
 
29
namespace system
 
30
{
 
31
namespace detail
 
32
{
 
33
namespace internal
 
34
{
 
35
namespace scalar
 
36
{
 
37
namespace stable_primitive_sort_detail
 
38
{
 
39
 
 
40
 
 
41
template<typename Iterator>
 
42
  struct enable_if_bool_sort
 
43
    : thrust::detail::enable_if<
 
44
        thrust::detail::is_same<
 
45
          bool,
 
46
          typename thrust::iterator_value<Iterator>::type
 
47
        >::value
 
48
      >
 
49
{};
 
50
 
 
51
 
 
52
template<typename Iterator>
 
53
  struct disable_if_bool_sort
 
54
    : thrust::detail::disable_if<
 
55
        thrust::detail::is_same<
 
56
          bool,
 
57
          typename thrust::iterator_value<Iterator>::type
 
58
        >::value
 
59
      >
 
60
{};
 
61
 
 
62
 
 
63
 
 
64
template<typename RandomAccessIterator>
 
65
  typename enable_if_bool_sort<RandomAccessIterator>::type
 
66
    stable_primitive_sort(RandomAccessIterator first, RandomAccessIterator last)
 
67
{
 
68
  // use stable_partition if we're sorting bool
 
69
  // stable_partition puts true values first, so we need to logical_not
 
70
  scalar::stable_partition(first, last, thrust::logical_not<bool>());
 
71
}
 
72
 
 
73
 
 
74
template<typename RandomAccessIterator>
 
75
  typename disable_if_bool_sort<RandomAccessIterator>::type
 
76
    stable_primitive_sort(RandomAccessIterator first, RandomAccessIterator last)
 
77
{
 
78
  // call stable_radix_sort
 
79
  scalar::stable_radix_sort(first,last);
 
80
}
 
81
 
 
82
 
 
83
struct logical_not_first
 
84
{
 
85
  template<typename Tuple>
 
86
  __host__ __device__
 
87
  bool operator()(Tuple t)
 
88
  {
 
89
    return !thrust::get<0>(t);
 
90
  }
 
91
};
 
92
 
 
93
 
 
94
template<typename RandomAccessIterator1, typename RandomAccessIterator2>
 
95
  typename enable_if_bool_sort<RandomAccessIterator1>::type
 
96
    stable_primitive_sort_by_key(RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last,
 
97
                                 RandomAccessIterator2 values_first)
 
98
{
 
99
  // use stable_partition if we're sorting bool
 
100
  // stable_partition puts true values first, so we need to logical_not
 
101
  scalar::stable_partition(thrust::make_zip_iterator(thrust::make_tuple(keys_first, values_first)),
 
102
                           thrust::make_zip_iterator(thrust::make_tuple(keys_last, values_first)),
 
103
                           logical_not_first());
 
104
}
 
105
 
 
106
 
 
107
template<typename RandomAccessIterator1, typename RandomAccessIterator2>
 
108
  typename disable_if_bool_sort<RandomAccessIterator1>::type
 
109
    stable_primitive_sort_by_key(RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last,
 
110
                                 RandomAccessIterator2 values_first)
 
111
{
 
112
  // call stable_radix_sort_by_key
 
113
  scalar::stable_radix_sort_by_key(keys_first, keys_last, values_first);
 
114
}
 
115
 
 
116
 
 
117
}
 
118
 
 
119
template<typename RandomAccessIterator>
 
120
void stable_primitive_sort(RandomAccessIterator first,
 
121
                           RandomAccessIterator last)
 
122
{
 
123
  scalar::stable_primitive_sort_detail::stable_primitive_sort(first,last);
 
124
}
 
125
 
 
126
template<typename RandomAccessIterator1,
 
127
         typename RandomAccessIterator2>
 
128
void stable_primitive_sort_by_key(RandomAccessIterator1 keys_first,
 
129
                                  RandomAccessIterator1 keys_last,
 
130
                                  RandomAccessIterator2 values_first)
 
131
{
 
132
  scalar::stable_primitive_sort_detail::stable_primitive_sort_by_key(keys_first, keys_last, values_first);
 
133
}
 
134
 
 
135
} // end namespace scalar
 
136
} // end namespace internal
 
137
} // end namespace detail
 
138
} // end namespace system
 
139
} // end namespace thrust
 
140
 
 
141
#include <thrust/system/detail/internal/scalar/stable_primitive_sort.inl>
 
142