基于区间的数据结构(类似于boost icl)

时间:2015-04-14 13:39:06

标签: c++ algorithm boost intervals discretization

我的目标是将一个不均匀离散的3D空间表示为一个箱子。

bin包含任意类型的元素(确定类型) 当添加一个bin(或bin所在的区间)并且它与之前添加的bin相交时,两者都应该合并。

现在我只添加二进制文件(区间),之后我正在迭代以获取属于右侧bin的元素,但是将来我可能需要同时更改元素/间隔和访问元素。
使用这种数据结构的算法应该是时间有效的。

到目前为止,我倾向于尽可能使用现有的库和数据结构 Boost :: ICL接缝很方便,但问题可能是合并。

现在我用包装器做这件事。 F.E.只有两个维度(Y,Z)和一个设置为bin:

class Set_wrapper : public boost::enable_shared_from_this<Set_wrapper>
{
public:
    Set_wrapper() :
        mValue(new boost::shared_ptr<std::set<int> >(new std::set<int>()))
    {
    }
    Set_wrapper(const std::set<int> & s)
    {
        mValue.reset(new boost::shared_ptr<std::set<int> >(new std::set<int>(s)));
    }
    void operator+=(const Set_wrapper &s)
    {
        Set_wrapper* sp = (Set_wrapper*) &s;
        (*sp->mValue)->insert((*mValue)->begin(), (*mValue)->end());
        *mValue = *(sp->mValue);
    }
    bool operator==(const Set_wrapper &s) const
    {
        return *s.mValue == *mValue;
    }

    boost::shared_ptr<boost::shared_ptr<std::set<int>>> mValue;

};

typedef interval_map<double, Set_wrapper> ZMap;

class Z_map_wrapper : public boost::enable_shared_from_this<Z_map_wrapper>
{
public:
    Z_map_wrapper() :
        mValue(new boost::shared_ptr<ZMap>(new ZMap()))
    {
    }
    Z_map_wrapper(const ZMap & s)
    {
        mValue.reset(new boost::shared_ptr<ZMap>(new ZMap(s)));
    }

    void operator+=(const Z_map_wrapper &s)
    {
        Z_map_wrapper* sp = (Z_map_wrapper*) &s;
        for(auto it = (*mValue)->begin(); it != (*mValue)->end(); ++it)
        {
          *(*sp->mValue) += std::make_pair(it->first, it->second);
        }
        *mValue = *(sp->mValue);
    }
    bool operator==(const Z_map_wrapper &s) const
    {
        return *s.mValue == *mValue;
    }

    boost::shared_ptr<boost::shared_ptr<ZMap>> mValue;

};

typedef interval_map<double, Z_map_wrapper> YMap;

这对我来说有点hacky: - )

另一种选择是使用b树或间隔树(来自cgal的fe)编写我自己的数据结构。 或者调整或扩展boost :: icl。但我不是最先进的程序员。

所以我的问题是:

尽管我的方法很丑陋......这会起作用还是会出现某些问题?

是否有更适合的数据结构?

在实施自己的数据结构时,我需要考虑什么?

非常感谢你的帮助

1 个答案:

答案 0 :(得分:1)

  

是否有更适合的数据结构?

也许您正在寻找Boost几何Spatial Index containers(例如rtree)

这可能意味着您必须进行合并,但至少您可以免费获得可扩展性要求:查找速度快,空间查询可用且迭代是一项功能。