C++ Boost多索引类型识别

发布于 2024-09-11 23:36:29 字数 268 浏览 7 评论 0原文

在boost多索引中,我可以通过元编程验证特定索引类型是否是有序的吗? 有有序索引、哈希索引、序列索引等,可以通过元编程找出来吗?

假设有一个这样的索引:

 int main()
 {
    typedef multi_index_container<double> double_set;
    return 0;
 }

我想知道 double_set 索引是有序的、散列的还是有序的。当然,在这种情况下,它是被命令的。

In boost multi-index, can I verify whether a particular index type is ordered/not through meta programming?
There are the ordered indexes, hash indexes, sequence indexes etc. Can I find them out through meta programming?

Suppose there is a index like:

 int main()
 {
    typedef multi_index_container<double> double_set;
    return 0;
 }

I want to know whether the double_set index is ordered or hashed or sequenced. Of course in this case, it is ordered.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩物 2024-09-18 23:36:29

是的:

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/random_access_index.hpp>

#include <boost/mpl/bool.hpp>
#include <boost/mpl/or.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/not.hpp>
#include <boost/mpl/distance.hpp>
#include <boost/mpl/begin.hpp>

namespace mpl = boost::mpl;
namespace mi  = boost::multi_index;

//
// checking for ordered_unique:
//
template <typename MI>
struct is_nth_index_ordered_unique_helper : mpl::false_ {};

template <typename KeyFromValue, typename Compare>
struct is_nth_index_ordered_unique_helper<
    mi::ordered_unique<KeyFromValue,Compare>
> : mpl::true_ {};

template <typename TagList, typename KeyFromValue, typename Compare>
struct is_nth_index_ordered_unique_helper<
    mi::ordered_unique<TagList,KeyFromValue,Compare>
> : mpl::true_ {};

template <typename MI, int N>
struct is_nth_index_ordered_unique
    : is_nth_index_ordered_unique_helper<
         typename mpl::at_c< typename MI::index_specifier_type_list, N >::type
      > {};

//
// checking for ordered_non_unique:
//

template <typename MI>
struct is_nth_index_ordered_non_unique_helper : mpl::false_ {};

template <typename KeyFromValue, typename Compare>
struct is_nth_index_ordered_non_unique_helper<
    mi::ordered_non_unique<KeyFromValue,Compare>
> : mpl::true_ {};

template <typename TagList, typename KeyFromValue, typename Compare>
struct is_nth_index_ordered_non_unique_helper<
    mi::ordered_non_unique<TagList,KeyFromValue,Compare>
> : mpl::true_ {};

template <typename MI, int N>
struct is_nth_index_ordered_non_unique
    : is_nth_index_ordered_non_unique_helper<
         typename mpl::at_c< typename MI::index_specifier_type_list, N >::type
      > {};

//
// Combined (ordered_{non_,}unique):
//

template <typename MI, int N>
struct is_nth_index_ordered
    : mpl::or_<
         is_nth_index_ordered_unique<MI,N>,
         is_nth_index_ordered_non_unique<MI,N>
      > {};

//
// checking for sequenced:
//

template <typename MI>
struct is_nth_index_sequenced_helper : mpl::false_ {};

template <typename TagList>
struct is_nth_index_sequenced_helper<
    mi::sequenced<TagList>
> : mpl::true_ {};

template <typename MI, int N>
struct is_nth_index_sequenced
    : is_nth_index_sequenced_helper<
         typename mpl::at_c< typename MI::index_specifier_type_list, N >::type
      > {};

//
// test with example container:
//
typedef mi::multi_index_container<double> double_set_1;

BOOST_MPL_ASSERT(( is_nth_index_ordered<double_set_1,0> ));
BOOST_MPL_ASSERT(( mpl::not_< is_nth_index_sequenced<double_set_1,0> > ));
// or
BOOST_STATIC_ASSERT(( is_nth_index_ordered<double_set_1,0>::value ));
BOOST_STATIC_ASSERT(( mpl::not_< is_nth_index_sequenced<double_set_1,0> >::value ));

//
// And now with tag dispatch:
//

template <typename MI, typename Tag>
struct tag_to_n
    : mpl::distance<
          typename mpl::begin<typename MI::index_type_list>::type,
          typename MI::template index<Tag>::iter
      > {};

template <typename MI, typename Tag>
struct is_tagged_index_ordered_unique
    : is_nth_index_ordered_unique<MI,tag_to_n<MI,Tag>::value> {};

template <typename MI, typename Tag>
struct is_tagged_index_ordered_non_unique
    : is_nth_index_ordered_non_unique<MI,tag_to_n<MI,Tag>::value> {};

template <typename MI, typename Tag>
struct is_tagged_index_ordered
    : is_nth_index_ordered<MI,tag_to_n<MI,Tag>::value> {};

template <typename MI, typename Tag>
struct is_tagged_index_sequenced
    : is_nth_index_sequenced<MI,tag_to_n<MI,Tag>::value> {};


//
// test with another example container:
//

struct as_set {};
struct as_list {};

typedef mi::multi_index_container<
    double,
    mi::indexed_by<
        mi::sequenced< mi::tag<as_list> >,
        mi::ordered_non_unique< mi::tag<as_set>, mi::identity<double> >
    >
> double_set_2;

void fun() {
    double_set_2 ds2;
}

BOOST_MPL_ASSERT(( is_nth_index_sequenced<double_set_2,0> ));
BOOST_MPL_ASSERT(( is_nth_index_ordered<double_set_2,1> ));
BOOST_MPL_ASSERT(( mpl::not_< is_nth_index_ordered<double_set_2,0> > ));
BOOST_MPL_ASSERT(( mpl::not_< is_nth_index_sequenced<double_set_2,1> > ));

BOOST_MPL_ASSERT(( is_tagged_index_sequenced<double_set_2,as_list> ));
BOOST_MPL_ASSERT(( is_tagged_index_ordered<double_set_2,as_set> ));
BOOST_MPL_ASSERT(( mpl::not_< is_tagged_index_ordered<double_set_2,as_list> > ));
BOOST_MPL_ASSERT(( mpl::not_< is_tagged_index_sequenced<double_set_2,as_set> > ));

Yes:

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/random_access_index.hpp>

#include <boost/mpl/bool.hpp>
#include <boost/mpl/or.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/not.hpp>
#include <boost/mpl/distance.hpp>
#include <boost/mpl/begin.hpp>

namespace mpl = boost::mpl;
namespace mi  = boost::multi_index;

//
// checking for ordered_unique:
//
template <typename MI>
struct is_nth_index_ordered_unique_helper : mpl::false_ {};

template <typename KeyFromValue, typename Compare>
struct is_nth_index_ordered_unique_helper<
    mi::ordered_unique<KeyFromValue,Compare>
> : mpl::true_ {};

template <typename TagList, typename KeyFromValue, typename Compare>
struct is_nth_index_ordered_unique_helper<
    mi::ordered_unique<TagList,KeyFromValue,Compare>
> : mpl::true_ {};

template <typename MI, int N>
struct is_nth_index_ordered_unique
    : is_nth_index_ordered_unique_helper<
         typename mpl::at_c< typename MI::index_specifier_type_list, N >::type
      > {};

//
// checking for ordered_non_unique:
//

template <typename MI>
struct is_nth_index_ordered_non_unique_helper : mpl::false_ {};

template <typename KeyFromValue, typename Compare>
struct is_nth_index_ordered_non_unique_helper<
    mi::ordered_non_unique<KeyFromValue,Compare>
> : mpl::true_ {};

template <typename TagList, typename KeyFromValue, typename Compare>
struct is_nth_index_ordered_non_unique_helper<
    mi::ordered_non_unique<TagList,KeyFromValue,Compare>
> : mpl::true_ {};

template <typename MI, int N>
struct is_nth_index_ordered_non_unique
    : is_nth_index_ordered_non_unique_helper<
         typename mpl::at_c< typename MI::index_specifier_type_list, N >::type
      > {};

//
// Combined (ordered_{non_,}unique):
//

template <typename MI, int N>
struct is_nth_index_ordered
    : mpl::or_<
         is_nth_index_ordered_unique<MI,N>,
         is_nth_index_ordered_non_unique<MI,N>
      > {};

//
// checking for sequenced:
//

template <typename MI>
struct is_nth_index_sequenced_helper : mpl::false_ {};

template <typename TagList>
struct is_nth_index_sequenced_helper<
    mi::sequenced<TagList>
> : mpl::true_ {};

template <typename MI, int N>
struct is_nth_index_sequenced
    : is_nth_index_sequenced_helper<
         typename mpl::at_c< typename MI::index_specifier_type_list, N >::type
      > {};

//
// test with example container:
//
typedef mi::multi_index_container<double> double_set_1;

BOOST_MPL_ASSERT(( is_nth_index_ordered<double_set_1,0> ));
BOOST_MPL_ASSERT(( mpl::not_< is_nth_index_sequenced<double_set_1,0> > ));
// or
BOOST_STATIC_ASSERT(( is_nth_index_ordered<double_set_1,0>::value ));
BOOST_STATIC_ASSERT(( mpl::not_< is_nth_index_sequenced<double_set_1,0> >::value ));

//
// And now with tag dispatch:
//

template <typename MI, typename Tag>
struct tag_to_n
    : mpl::distance<
          typename mpl::begin<typename MI::index_type_list>::type,
          typename MI::template index<Tag>::iter
      > {};

template <typename MI, typename Tag>
struct is_tagged_index_ordered_unique
    : is_nth_index_ordered_unique<MI,tag_to_n<MI,Tag>::value> {};

template <typename MI, typename Tag>
struct is_tagged_index_ordered_non_unique
    : is_nth_index_ordered_non_unique<MI,tag_to_n<MI,Tag>::value> {};

template <typename MI, typename Tag>
struct is_tagged_index_ordered
    : is_nth_index_ordered<MI,tag_to_n<MI,Tag>::value> {};

template <typename MI, typename Tag>
struct is_tagged_index_sequenced
    : is_nth_index_sequenced<MI,tag_to_n<MI,Tag>::value> {};


//
// test with another example container:
//

struct as_set {};
struct as_list {};

typedef mi::multi_index_container<
    double,
    mi::indexed_by<
        mi::sequenced< mi::tag<as_list> >,
        mi::ordered_non_unique< mi::tag<as_set>, mi::identity<double> >
    >
> double_set_2;

void fun() {
    double_set_2 ds2;
}

BOOST_MPL_ASSERT(( is_nth_index_sequenced<double_set_2,0> ));
BOOST_MPL_ASSERT(( is_nth_index_ordered<double_set_2,1> ));
BOOST_MPL_ASSERT(( mpl::not_< is_nth_index_ordered<double_set_2,0> > ));
BOOST_MPL_ASSERT(( mpl::not_< is_nth_index_sequenced<double_set_2,1> > ));

BOOST_MPL_ASSERT(( is_tagged_index_sequenced<double_set_2,as_list> ));
BOOST_MPL_ASSERT(( is_tagged_index_ordered<double_set_2,as_set> ));
BOOST_MPL_ASSERT(( mpl::not_< is_tagged_index_ordered<double_set_2,as_list> > ));
BOOST_MPL_ASSERT(( mpl::not_< is_tagged_index_sequenced<double_set_2,as_set> > ));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文