具有不满足严格弱排序的值的 C++ 顺序范围
c++ order range with values that doesn't fulfill strict weak ordering
我需要对范围内的值进行排序,以便该范围表示一个链。
struct Link
{
int id;
int next;
};
Link::id
和Link::next
的值是任意的,并且它们本身不提供任何语义含义(无论如何都不用于排序算法(。
两个链接(排序后(之间的关系为:lhs.next
正好rhs.id
。
前提 条件
- 无序
range
保证保存可以精确排序到一个链中的值。 - 保证值集中没有递归(无循环(
例:
std::vector< Link> range{ { 4, 1}, { 1, 5}, { 3, 4}, { 2, 3}};
auto chain = some_algorithm( range);
// expect the ordering to be: { { 2, 3}, { 3, 4}, { 4, 1}, { 1, 5}};
我至少可以想到两种方法,但我怀疑这已经以一种惯用的方式解决了。所以,我的问题是:如何以惯用的方式解决这个问题?
我怀疑是否有惯用的方法,因为这不是常见的情况。
链接主要由指针/迭代器完成(例如std::list
(,实际的链接大多是在插入时完成的。
有趣的是找到第一个链接以及如何处理循环链接和错误情况。
这是我想到的:
"概念"R是某种行为类似于范围的类型,可能是容器,也可能是其他东西。
如果range
有缺口(链接方面(,将订购新的链。我不想有一些"断言输出"或抛出。我仍然保持我的目标,因为在我的用例中,我知道所有值都可以形成一个链。
template< typename R, typename ISP>
R chain( R range, ISP is_link_predicate)
{
auto first = std::begin( range);
auto current = first;
const auto last = std::end( range);
while( current != last)
{
const auto next = current + 1;
// try to find a value in [next, last) that can be linked with *current.
auto link = std::find_if( next, last, [current,is_link_predicate]( auto& value){
return is_link_predicate( *current, value);
});
if( link != last)
{
using std::swap;
swap( *next, *link);
current = next;
}
else
{
// we need to check if some value in [next, last) can be "inserted" in the
// beginning of the chain. That is, can form a link with *first.
auto new_first = std::find_if( next, last, [first,is_link_predicate]( auto& value){
return is_link_predicate( value, *first);
});
if( new_first != last)
{
// we got a new_first, we need to rotate it first
//
// C = current
// N = next (not ordered).
// - = ordered values
// = = not ordered values
// X = value to be "inserted" first, new_first
//
// -----CN====X=== we start with
// X-----C======== we end up with
//
std::rotate( first, new_first, new_first + 1);
current = next;
}
else
{
// no values in [next, last) is part of the current chain.
// we start building the next chain.
current = next;
first = current;
}
}
}
return range;
}
评论?
首先,你正在做的不是排序,所以要考虑排序。你永远无法判断A
是在B
之前还是之后,你可以只知道A
在B
之前是正好是1还是之后正好是1。所以排序在这里不能帮助你,甚至拓扑排序也没有。
一种算法:对于链A1..An
回溯所有可以与之链接的元素X
,例如A1..AnX
或XA1..An
,然后对新链重复此操作。起始链是空链,任何元素都可以与之链接。
相关文章:
- CMake-按正确顺序将项目与C运行时对象文件链接
- 函数调用中参数的顺序重要吗
- 为什么不;名字在地图上是按顺序排列的吗
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 数到第n个楼梯的路(顺序无关紧要)
- 优先顺序:智能指针和类析构函数
- 在循环中按顺序遍历成员变量
- 独立读取-修改-写入顺序
- QML按钮点击功能执行顺序
- C++中数据类型修饰符的顺序
- 当比特(而不是字节)的顺序至关重要时的持久性
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- 通过选项卡的文本设置QTabWidget顺序
- c++11评估顺序(未定义的行为)
- 如何在C++中递归地按相反顺序打印集合
- 给定顺序中的事件处理
- 具有包含其他对象的类的对象创建顺序
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- 遍历顺序由 std::文件系统directory_iterator给出
- 检查 2 棵树是否具有相同的顺序