jjzjj

C++ Boost 多索引类型识别

coder 2024-02-20 原文

在 boost multi-index 中,我可以通过元编程验证特定索引类型是否有序吗? 有有序索引、散列索引、序列索引等,我可以通过元编程找到它们吗?

假设有一个像这样的索引:

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

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

最佳答案

是的:

#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> > ));

关于C++ Boost 多索引类型识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3370656/

有关C++ Boost 多索引类型识别的更多相关文章

  1. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  2. ruby - Infinity 和 NaN 的类型是什么? - 2

    我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串

  3. ruby - 检查方法参数的类型 - 2

    我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)

  4. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  5. ruby - 查找字符串中的内容类型(数字、日期、时间、字符串等) - 2

    我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s

  6. ruby-on-rails - 在 Rails 开发环境中为 .ogv 文件设置 Mime 类型 - 2

    我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain

  7. 报告回顾丨模型进化狂飙,DetectGPT能否识别最新模型生成结果? - 2

    导读语言模型给我们的生产生活带来了极大便利,但同时不少人也利用他们从事作弊工作。如何规避这些难辨真伪的文字所产生的负面影响也成为一大难题。在3月9日智源Live第33期活动「DetectGPT:判断文本是否为机器生成的工具」中,主讲人Eric为我们讲解了DetectGPT工作背后的思路——一种基于概率曲率检测的用于检测模型生成文本的工具,它可以帮助我们更好地分辨文章的来源和可信度,对保护信息真实、防止欺诈等方面具有重要意义。本次报告主要围绕其功能,实现和效果等展开。(文末点击“阅读原文”,查看活动回放。)Ericmitchell斯坦福大学计算机系四年级博士生,由ChelseaFinn和Chri

  8. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

  9. [Vuforia]二.3D物体识别 - 2

    之前说过10之后的版本没有3dScan了,所以还是9.8的版本或者之前更早的版本。 3d物体扫描需要先下载扫描的APK进行扫面。首先要在手机上装一个扫描程序,扫描现实中的三维物体,然后上传高通官网,在下载成UnityPackage类型让Unity能够使用这个扫描程序可以从高通官网上进行下载,是一个安卓程序。点到Tools往下滑,找到VuforiaObjectScanner下载后解压数据线连接手机,将apk文件拷入手机安装然后刚才解压文件中的Media文件夹打开,两个PDF图打印第一张A4-ObjectScanningTarget.pdf,主要是用来辅助扫描的。好了,接下来就是扫描三维物体。将瓶

  10. ruby-on-rails - 在 heroku 的 .fonts 文件夹中包含自定义字体,似乎无法识别它们 - 2

    Heroku支持人员告诉我,为了在我的Web应用程序中使用自定义字体(未安装在系统中,您可以在bash控制台中使用fc-list查看已安装的字体)我必须部署一个包含所有字体的.fonts文件夹里面的字体。问题是我不知道该怎么做。我的意思是,我不知道文件名是否必须遵循heroku的任何特殊模式,或者我必须在我的代码中做一些事情来考虑这种字体,或者如果我将它包含在文件夹中它是自动的......事实是,我尝试以不同的方式更改字体的文件名,但根本没有使用该字体。为了提供更多详细信息,我们使用字体的过程是将PDF转换为图像,更具体地说,使用rghostgem。并且最终图像根本不使用自定义字体。在

随机推荐