我想从约束列表中定义一个代表数学域的对象,但我不清楚如何去做。
例如,我从 IR 开始,我有以下约束:
- x > 0
- x 不在]3,5]
- x 不在 [7,12[
那么,我的域是 ]0,3] U ]5,7[ U [12,+oo .
我怎样才能很好地将它存储在 C++ 结构中?您以前做过吗?此外,我希望能够轻松检查域是否为空。
最佳答案
除非您想使用评论中提到的“第 3 方”工具,否则您必须编写自己的 Interval 类。 为此,您可以执行以下操作:
class Interval{
struct Range{
bool leftInclusive, rightInclusive;
double left, right;
bool operator<(Range other){return left<other.left;}
}
std::Set<Range> trueRanges;
void addTrueRange(Range r){
//check for overlaps
//merge if overlapping
//otherwise add to trueRanges
}
bool trueAt(double at){
//find the range with the highest left-bound lower than at
auto candidate = truethRanges.upper_bound(at);
if(candidate == trueRanged.end()) return false; // no range found
//on-point checking here
if(at <= candidate->left) return false;
if(at >= candidate->right) return false;
return true;
}
}
此处省略了点检查,因为您不能简单地说 doubleOne == doubleTwo
因为这可能会导致漏报。所以你必须说 ABS(doubleOne-doubleTwo) < tinyValue
.
要查找重叠,您可以查看 this .
关于c++ - 如何在 IR 中表示数学域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30574957/