You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否借助type_traits将任意结构体作为空间数组访问以构建KD-Tree?

问题:为空间节点类实现KD-Tree适配的特性模板

背景与现有实现

我有一个包含空间成员(x、y、z 或 lat、lon)及其他属性成员的类,希望使用该类的数组创建可搜索的KD-Tree。目前已通过以下代码实现对std::array<Node, N>、std::vector<Node>或Node[5]这类容器的处理:

template <typename SPATIAL_CONTAINER,
          int NUM_DIMENSIONS>
struct Array_traits
{
    /* Dimension handling */
    static constexpr int DIM = NUM_DIMENSIONS;
    static_assert(DIM > 1, "KD-Trees require DIM of at least 2.");

    /* Spatial container handling */
    using Array = SPATIAL_CONTAINER;
    // Get the type used for each spatial element.  Decay<> removes any const/Ref
    typedef typename std::decay<decltype(*std::begin(Array()))>::type Node;
};

当前困惑与设想接口

我当前的困惑在于,是否应该为Node创建一个特性模板来访问其特定空间成员,以及该如何实现。以下是我设想的接口思路(无法运行,仅为示意):

// Doesn't work, just thoughts on the intended interface
template <int NUM_DIMENSIONS>
struct Node_traits
{
    /* Dimension handling */
    static constexpr int DIM = NUM_DIMENSIONS;
    static_assert(DIM > 1, "KD-Trees require DIM of at least 2.");

    CoordType get<0> { return x; } // or { return lat; }
    CoordType get<1> { return y; } // or { return lon; }
    constexpr if (DIM >= 2) {
        CoordType get<2>() { return z; }
    }
};

请问该思路是否可行?若可行,能否提供创建此类包装器/特性类以实现特定类型专属接口的相关提示?

内容的提问来源于stack exchange,提问作者Brett Stottlemyer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 08:01:01