使用选择算法编译时递归排序
Compile Time Recursive Sort with Selection Algorithm
是否可以使用选择算法在c++中编写编译时递归排序函数?
我想按升序对数组x
从元素istart
到元素iend
进行排序。阵列x
具有N
元素。输入数组x
中的数据仅在运行时已知,因此数据只能在运行时排序。然而,我想生成C++代码,即在编译时对sort_asc()
的所有递归函数调用。此外,我想在CUDA设备功能中使用此代码。由于CUDA是C++的一个子集,有一些扩展,我认为这是一个正确的问题。不幸的是,我认为CUDA不支持constexpr
关键字,既不支持Boost也不支持STL。
我想出了以下按升序排序的代码。
// sort in ascending order.
template< int istart, int N, int iend, int iend_min_istart >
inline void sort_asc
(
float *x
)
{
int min_idx = istart;
float min_val = x[ min_idx ];
#pragma unroll
for( int i=istart+1; i<N; i++ ){
if( x[ i ] < min_val ){
min_idx = i;
min_val = x[ i ];
}
}
swap( x[ istart ], x[ min_idx ] );
sort_asc< istart+1, N, iend, iend-(istart+1) >( x );
}
其中CCD_ 9。如果iend_min_istart < 0
,那么递归已经完成,因此我们可以将终止条件写为:
// sort recursion termination condition.
template< int istart, int N, int iend >
inline void sort_asc< -1 >
(
float *x
)
{
// do nothing.
}
交换功能定义为:
void d_swap
(
float &a,
float &b
)
{
float c = a;
a = b;
b = c;
}
然后我会将排序函数调用为:
void main( int argc, char *argv[] )
{
float x[] = { 3, 4, 9, 2, 7 }; // 5 element array.
sort_asc< 0, 5, 2, 2-0 >( x ); // sort from the 1st till the 3th element.
float x_median = cost[ 2 ]; // the 3th element is the median
}
但是,由于c++不支持函数的部分模板专用化,因此此代码不进行编译。此外,我不知道如何在C++元编程中写这篇文章。有什么方法可以让这个代码正常工作吗?
这次使用选择排序。(使用合并排序的变量。)没有任何保证,但通过了一个简单的测试:
// generate a sequence of integers as non-type template arguments
// (a basic meta-programming tool)
template<int... Is> struct seq {};
template<int N, int... Is> struct gen_seq : gen_seq<N-1, N-1, Is...> {};
template<int... Is> struct gen_seq<0, Is...> : seq<Is...> {};
// an array type that can be returned from a function
// and has `constexpr` accessors (as opposed to C++11's `std::array`)
template<class T, int N>
struct c_array
{
T m[N];
constexpr T const& operator[](int p) const
{ return m[p]; }
constexpr T const* begin() const { return m+0; }
constexpr T const* end() const { return m+N; }
};
// return the index of the smallest element
template<class T, int Size>
constexpr int c_min_index(c_array<T, Size> const& arr, int offset, int cur)
{
return Size == offset ? cur :
c_min_index(arr, offset+1, arr[cur] < arr[offset] ? cur : offset);
}
我们的目标是能够在编译时进行排序。我们可以使用几种工具(宏、类模板、constexpr
函数),但在许多情况下,constexpr
函数往往是最简单的。
上面的函数c_min_index
是C++11对constexpr
函数的限制的一个例子:它只能由一个返回语句(加上static_assert
和using
)组成,并且至少对于一些可能的参数,它必须产生一个常量表达式。这意味着:没有循环,就没有赋值。因此,我们需要在这里使用递归,并将中间结果传递给下一个调用(cur
)。
// copy the array but with the elements at `index0` and `index1` swapped
template<class T, int Size, int... Is>
constexpr c_array<T, Size> c_swap(c_array<T, Size> const& arr,
int index0, int index1, seq<Is...>)
{
return {{arr[Is == index0 ? index1 : Is == index1 ? index0 : Is]...}};
}
由于constexpr
函数的限制,我们无法修改参数,因此我们需要返回整个数组的副本,以便交换两个元素。
函数期望Is...
是由gen_seq<Size>{}
(在其基类中)生成的整数0, 1, 2 .. Size-1
的序列。这些整数用于访问数组CCD_ 22的元素。一些像CCD_ 23这样的表达会产生CCD_。在这里,我们通过对当前整数应用条件来交换索引:a[0 == index0 ? index1 : 0 == index1 ? index0 : 0], ..
// the selection sort algorithm
template<class T, int Size>
constexpr c_array<T, Size> c_sel_sort(c_array<T, Size> const& arr, int cur = 0)
{
return cur == Size ? arr :
c_sel_sort( c_swap(arr, cur, c_min_index(arr, cur, cur),
gen_seq<Size>{}),
cur+1 );
}
同样,我们需要将循环替换为递归(cur
)。剩下的是一个直接的选择排序实现,语法很尴尬:从索引cur
开始,我们在剩下的数组中找到最小元素,并将其与cur
处的元素交换。然后,我们对剩余的未排序数组重新运行选择排序(通过递增cur
)。
#include <iostream>
#include <iterator>
int main()
{
// homework: write a wrapper so that C-style arrays can be passed
// to an overload of `c_sel_sort` ;)
constexpr c_array<float,10> f = {{4, 7, 9, 0, 6, 2, 3, 8, 1, 5}};
constexpr auto sorted = c_sel_sort(f);
for(auto const& e : sorted) std::cout << e << ", ";
}
我想出了下面的元代码,它很有效。
template < class T >
__forceinline void swap
(
T &a,
T &b
)
{
T c = a;
a = b;
b = c;
}
template< int istart, int N, int iend, int iend_min_istart >
class SORT
{
public:
static __forceinline void sort( float *x, int *idx )
{
// sort code.
int min_idx = istart;
float min_val = x[ min_idx ];
for( int i=istart+1; i<N; i++ ){
if( x[ i ] < min_val ){
min_idx = i;
min_val = x[ i ];
}
}
swap( x[ istart ], x[ min_idx ] );
swap( idx[ istart ], idx[ min_idx ] );
SORT<istart+1, N, iend, iend-(istart+1)>::sort( x, idx );
}
};
template< int istart, int N, int iend >
class SORT<istart,N,iend,-1>
{
public:
static __forceinline void sort( float *x, int *idx )
{
}
};
void main( int argc, char *argv[] )
{
float arr[] = {1,4,2,7,5};
int idx[] = {0,1,2,3,4};
SORT<0,5,3,2-0>::sort( arr, idx );
}
- 使用列表 STL 递归进行插入排序
- 在气泡排序中使用递归
- 运行合并排序递归算法时EXC_BAD_ACCESS错误
- 合并排序与递归
- 递归合并排序算法实现
- 从递归向后选择排序函数调用 max 和交换函数
- 递归在合并排序中行为异常
- 递归定义数组中的数据对齐和排序
- 为什么我的递归快速排序算法有如此不平衡的分区
- C :重复的变化的递归功能,按不同字母数量排序
- 递归代码快速排序
- 无法让我的代码正确排序用户输入的数字数组(使用递归)
- 合并排序中的递归
- C 不需要带有递归排序算法的数组长度
- C++中的无限递归快速排序
- 使用选择算法编译时递归排序
- 递归合并排序错误
- 随机输出与C++递归合并排序
- 选择在C++中递归排序
- C++ 递归排序通过分而治之和迭代排序