boost multi_index

时间:2023-01-30 15:09:15
/**
boost 多索引容器的一般使用
这里使用google 的gmock 库来验证对boost 多索引模板的使用,并验证。
这里是手敲的,可能会有一些字符敲错的情况,编译错误的放,修改一下,同时链接gmock库就可以正常运行了。
当然还需要链接boost 的相关库
**/ #include <stdint.h> #include <gtest/gtest.h>
#include <gmock/gmock.h> #include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/global_fun.hpp> // 多索引的结构或者类
struct stru_AccountFeeIncre
{
int64_t nAccountID;
int32_t nGoodsID;
int32_t nGoodsGroupID;
int32_t nFeeMode;
int32_t nTradeMode;
double dFeeValue;
}; // 为索引的Key 定义一个标志类型,类似std::map 的Key
struct tagGoodsID {};
struct tagGoodsGroupID {}; // 声明多索引结构,主要绑定索引类型可以与指定成员的一一映射
using MultiIndexFeeIncre =
boost::multi_index::multi_index_container<
stru_AccountFeeIncre, // 数据结构,用于索引的对象
boost::multi_index::indexed_by< // 创建索引,可以是一个或者多个。分别以',' 分隔
// 创建唯一索引,不允许插入该字段的值相同的两个对象到此容器中。
boost::multi_index::ordered_unique<
boost::multi_index::tag<tagGoodsID>, // 索引标签
BOOST_MULTI_INDEX_MEMBER(struAccountFeeIncre, int32_t, nGoodsID) // 该索引绑定的成员
>,
// 创建非唯一索引,允许多个相同的值到此容器中
boost::multi_index::ordered_non_unique<
boost::multi_index::tag<tagGoodsGroupID>,
BOOST_MULTI_INDEX_MEMBER(stru_AccountFeeIncre, int32_t, nGoodsGroupID)
>
>
>; using C_ITER_FEE_INCRE = MultiIndexFeeIncre::const_iterator;
using PAIR_RANGE_FEE_INCRE = std::pair<C_ITER_FEE_INCRE, C_ITER_FEE_INCRE>;
using C_ITER_ID_FEE_INCRE = MultiIndexFeeIncre::index<tagGoodsID>::type::const_iterator;
using C_ITER_GROUP_FEE_INCRE = MultiIndexFeeIncre::index<tagGoodsGroupID>::type::const_iterator;
using PAIR_RANGE_GROUP_FEE_INCRE = std::pair<C_ITER_GROUP_FEE_INCRE, C_ITER_GROUP_FEE_INCRE>; // 定义多索引容器实例
static MultiIndexFeeIncre s_multiAccountFeeIncre;
// 定义容器的索引实例的引用 - 非const 版本
static MultiIndexFeeIncre::index<tagGoodsID>::type &INDEX_ID_FEE_INCRE = s_multiAccountFeeIncre.get<tagGoodsID>();
static MultiIndexFeeIncre::index<taggoodsGroupID>::type &INDEX_GROUP_FEE_INCRE = s_multiAccountFeeIncre.get<tagGoodsGroupID>();
// 定义容器的索引实例的引用 - const 版本
static const MultiIndexFeeIncre::index<tagGoodsID>::type &C_INDEX_ID_FEE_INCRE = s_multiAccountFeeIncre.get<tagGoodsID>();
static const MultiIndexFeeIncre::index<tagGoodsgroupID>::type &C_INDEX_GROUP_FEE_INCRE = s_multiAccountFeeIncre.get<tagGoodsGroupID>(); // template 版本
MultiIndexFeeIncre::template index<tagGoodsID>::type &TPLT_INDEX_ID = s_multiAccountFeeIncre.template get<taggoodsID>();
MultiIndexFeeIncre::template index<tagGoodsGroupID>::type &TPLT_INDEX_GROUP = s_multiAccountFeeIncre.template get<tagGoodsGroupID>(); TEST(TestBoostUse, MultiIndex)
{
const int32_t GROUP_ID_01 = ;
const int32_t GROUP_ID_02 = ;
int64_t nExpectSize = ; EXPECT_EQ(nExpectSize, s_multiAccountFeeIncre.size()); int64_t nGoodsID = ;
stru_AccountFeeIncre stFeeIncre;
stFeeIncre.nAccountID = ;
stFeeIncre.nGoodsID = nGoodsID;
stFeeIncre.nGoodsGroupID = GROUP_ID_01;
stFeeIncre.nFeeMode = ;
stFeeIncre.nTradeMode = ;
stFeeIncre.dFeeValue = 0.5; // 商品组1
int32_t nExpectSizeGroupID_1 = ;
// 插入一条
s_multiAccountFeeIncre.emplace(stFeeIncre);
++nExpectSize;
++nExpectSizeGroupID_1;
EXPECT_EQ(nExpectSize, s_multiAccountFeeIncre.size()); // 插入与唯一索引相同的值的对象,插入失败
s_multiAccountFeeIncre.emplace(stFeeIncre);
EXPECT_EQ(nExpectSize, s_multiAccountFeeIncre.size()); // 插入一条
++nGoodsID;
stFeeIncre.nGoodsID = nGoodsID;
stFeeIncre.nGoodsGroupID = GROUPID_01; s_multiAccountFeeIncre.emplace(stFeeIncre);
++nExpectSize;
++nExpectSizeGroupID_1;
EXPECT_EQ(nExpectSize, s_multiAccountfeeIncre.size()); // 插入一条
++nGoodsID;
stFeeIncre.nGoodsID = nGoodsID;
stFeeIncre.nGoodsGroupID = GROUP_ID_01; s_multiAccountFeeIncre.emplace(stFeeIncre);
++nExpectSize;
++nExpectSizeGroupID_1;
EXPECT_EQ(nExpectSize, s_multiAccountFeeIncre.size()); // 插入到另一个商品组
int32_t nExpectSizeGroupID_2 = ;
++nGoodsID;
stFeeIncre.nGoodsID = nGoodsID;
stFeeIncre.nGoodsGroupID = GROUP_ID_02; s_multiAccountFeeIncre.emplace(stFeeIncre);
++nExpectSize;
++nExpectSizeGroupID_2;
EXPECT_EQ(nExpectSize, s_multiAccountFeeIncre.size()); // 插入一条
++nGoodsID;
stFeeIncre.nGoodsID = nGoodsID;
stFeeIncre.nGoodsGroupID = GROUP_ID_02; s_multiAccountFeeIncre.emplace(stFeeIncre);
++nExpectSize;
++nExpectSizeGroupID_2;
EPXECT_EQ(nEpxectSize, s_multiAccountFeeIncre.size()); // 查询
do
{
// 唯一索引的查询,结果仅有一条记录
int nTempGoodsID = ;
C_ITER_ID_FEE_INCRE c_iter_id = INDEX_ID_FEE_INCRE.find(nTempGoodsID);
ASSERT_NE(c_iter_id, INDEX_ID_FEE_INCRE.end()); // 非唯一索引的查询,结果为一组记录
PAIR_RANGE_GROUP_FEE_INCRE range = TPLT_INDEX_GROUP.equal_range(GROUP_ID_01);
int nTempSize = ;
for (auto it = range.first; it != range.second; ++it)
{
EXPECT_EQ(GROUP_ID_01, it->nGoodsGroupID);
++nTempSize;
} EXPECT_EQ(nExpectSizeGroupID_1, nTempSize);
}
while (false); // 修改
do
{
// 拷贝副本
MultiIndexFeeIncre bak_multiAccountFeeIncre = s_multiAccountFeeIncre;
int32_t bakExpectSize = nExpectSize;
int32_t nTempGoosdID = ; C_ITER_ID_FEE_INCRE c_iter_id = bak_multiAccountFeeIncre.get<tagGoodsID>().find(nTempGoodsID);
EXPECT_NE(c_iter_id, bak_multiAccountFeeIncre.get<tagGoodsID>().end());
if (c_iter_id == bak_multiAccountFeeIncre.get<tagGoodsID>().end())
{
break;
} stru_AccountFeeIncre newObj; // 未设置值使用内存随机值即可
newObj.nGoodsID = c_iter_id->nGoodsID; // 唯一性主键不修改
newObj.nGoodsGroupID = nExpectSizeGroupID_2; // 商品组ID改到2 非唯一性主键修改
bool bResTemp = bak_multiAccountfeeIncre.replace(c_iter_id, newObj);
EXPECT_TRUE(bResTemp);
EXPECT_EQ(bakExpectSize, bak_multiAccountFeeIncre.size());
}
while (false); do
{
// 拷贝副本
MultiIndexFeeIncre bak_multiAccountFeeIncre = s_multiAccountFeeIncre;
int32_t bakExpectSize = nExpectSize;
int32_t nTempGoodsID = ; C_ITER_ID_FEE_INCRE c_iter_id = bak_multiAccountFeeIncre.get<tagGoodsID>().find(nTempGoodsID);
EXPECT_NE(c_iter_id, bak_multiAccountFeeIncre.get<tagGoodsID>().end());
if (c_iter_id == bak_multiAccountFeeIncre.get<tagGoodsID>().end())
{
break;
} stru_AccountFeeIncre newObj;
newObj.nGoodsID = ; // 修改唯一性主键
newObj.nGoodsGroupID = c_iter_id->nGoodsGroupID; // 非唯一性主键不修改
bool bResTemp = bak_multiAccountFeeIncre.replace(c_iter_id, newObj);
EXPECT_TRUE(bResTemp);
EXPECT_EQ(bakExpectSize, bak_multiAccountFeeIncre.size());
}
while (false); // 删除
do
{
// 拷贝副本
MultiIndexFeeIncre bak_multiAccountFeeIncre = s_multiAccountFeeIncre;
int32_t bakExpectSize = nExpectSize;
int32_t bakExpectSizeGroupID_1 = nExpectSizeGroupID_1; // 先删除商品组1 中的一个商品
int nTempGoodsID = ;
C_ITER_ID_FEE_INCRE c_iter_id = bak_multiAccountFeeIncre.get<tagGoodsID>().find(nTempGoodsID);
ASSERT_NE(c_iter_id, bak_multiAccountFeeIncre.get<taggoodsID>().end());
bak_multiAccountFeeIncre.erase(c_iter_id);
--bakExpectSize;
--bakExpectSizeGroupID_1;
EXPECT_EQ(bakExpectSize, bak_multiAccountFeeIncre.size()); // 删除商品组1 的元素
EXPECT_EQ(bakExpectSize, bak_multiAccountFeeIncre.get<tagGroupID>().size());
EXPECT_EQ(bakExpectSize, bak_multiAccountFeeIncre.get<tagGoodsGroupID>().size());
bak_multiAccountFeeIncre.template get<tagGoodsGroupID>().erase(GROUP_ID_01);
bakExpectSize -= bakExpectSizeGroupID_1;
bakExpectSizeGroupID_1 = ;
EXPECT_EQ(bakExpectSize, bak_multiAccountFeeIncre.size());
}
while (false); do
{
// 拷贝副本
MultiIndexFeeIncre bak_multiAccountFeeIncre = s_multiAccountFeeIncre;
int32_t bakExpectSize = nExpectSize;
int32_t bakExpectSizeGroupID_2 = nExpectSizeGroupID_2; // 删除商品组2 的元素
bak_multiAccountFeeIncre.template get<tagGoodsGroupID>().erase(GROUP_ID_02);
bakExpectSize -= bakExpectSizeGroupID_2;
bakExpectSizeGroupID_2 = ;
EXPECT_EQ(bakExpectSize, bak_multiAccountFeeIncre.size());
}
while (false); do
{
// 拷贝副本
MultiIndexFeeIncre bak_multiAccountFeeIncre = s_multiAccountFeeIncre;
int32_t bakGoodsID = nGoodsID;
int32_t bakExpectSize = nExpectSize; // 按唯一性主键: 商品ID 进行删除
do
{
bak_multiAccountFeeIncre.template get<tagGoodsID>().erase(bakGoodsID);
--bakExpectSize;
EXPECT_EQ(bakExpectSize, bak_multiAccountFeeIncre.size());
}
while (--bakGoodsID > );
}
while (false);
} int main(int argc, char *argv[])
{
testing::InitGoodsMock(&argc, argv); return RUN_ALL_TESTS();
}

boost multi_index的更多相关文章

  1. 使用boost&colon;&colon;multi&lowbar;index高速构建排行榜

    使用boost::multi_index高速构建排行榜 前几天在boost的maillist上看到boost::multi_index将要支持ranked_index(邮件内容见附件2),这实乃我等苦 ...

  2. 用 boost&colon;&colon;multi&lowbar;index 管理玩家

    用 boost::multi_index 管理玩家(金庆的专栏)网游服务器上的玩家集合需要多种索引:如用ID查找,角色名查找, 用登录时分配的会话ID查找.用boost::multi_index进行玩 ...

  3. boost&colon;&colon;multi&lowbar;index 多索引容器

    #include "stdafx.h" #include <string> #include <boost/multi_index_container.hpp&g ...

  4. boost&colon;&colon;multi&lowbar;index 提供一种千人在线即时排行榜的设计思路

    原文地址: http://www.limerence2017.com/2019/06/23/cpp01/ 做游戏或金融后台开发,经常会遇到设计开发排行榜的需求.比如玩家的充值排行,战力排行等等.而这种 ...

  5. boost multi&lowbar;index 插入返回值

    boost multi_index 对象插入函数emplace() 的返回值,是一个std::pair<iterator, bool>该pair 的first 是一个插入成功的位置,第二个 ...

  6. boost multi&lowbar;index简单了解

    #include <string> #include <iostream> #include <boost/multi_index_container.hpp> # ...

  7. Boost练习程序(multi&lowbar;index&lowbar;container)

    代码来自:http://blog.csdn.net/whuqin/article/details/8482547 该容器能实现多列索引,挺好. #include <string> #inc ...

  8. boost asio 异步实现tcp通讯

    ---恢复内容开始--- asioboost   目录(?)[-] 一前言 二实现思路 通讯包数据结构 连接对象 连接管理器 服务器端的实现 对象串行化   一.前言 boost asio可算是一个简 ...

  9. boost multi index

    Boost.MultiIndex makes it possible to define containers that support an arbitrary number of interfac ...

随机推荐

  1. C&num; 通过后台获取浏览器域名

    通过httpContext.获取当前地址当前主机域名 string url = HttpContext.Current.Request.Url.Host.ToString();

  2. Linux将Shelll输出写入到文件

    &>  以覆盖的方式,写入文件 &>> 将输出追加到文件 将命令的正确输出与错误输出都放入文件. /dev/null,垃圾箱. 将无用输出放入垃圾箱. 命令>& ...

  3. rabbitmq之amqp queue

    rabbitmq作为一个消息中间件,暂存信息的能力是必不可少的. 镜像队列

  4. poj3692 最大点权独立集&sol;最大独立集

    题意:有男孩和女孩,男孩之间全部认识,女孩之间全部认识,一部分男孩和女孩认识,现在希望选出一些孩子,这些孩子都相互认识. 方法:正的做不好做,观察他的补图,补图之间无关系的边就是原图有关系的.补图中的 ...

  5. 很棒的jQuery代码片段分享

    jQuery实现的内链接平滑滚动 不需要使用太复杂的插件,只要使用下载这段代码即可实现基于内部链接的平滑滚动 $('a[href^="#"]').bind('click.smoot ...

  6. LigerUI&plus;MVC的应用1

    [项目开发]LigerUI+MVC的应用(一) 近期因为稍微空闲有点时间,就晚上回家自己在随便写写代码,也就边写边记,中间主要采用了微软的MVC4.0框架.虽然目前公司也是使用的MVC的模式,但是因为 ...

  7. MySQL插入10万数据时间(结论:最快14&period;967s,每秒插入6681条)

    记录我的一次MySQL操作Demo: 存储过程: DROP PROCEDURE IF EXISTS my_insert; CREATE PROCEDURE my_insert() BEGIN ; lo ...

  8. RabbitMQ 消息队列 二

    一:查看MQ的用户角色 rabbitmqctl list_users 二:添加新的角色,并授予权限 rabbitmqctl add_user xiaoyao 123456 rabbitmqctl se ...

  9. vue 图片切换动态绑定

    <img :src="切换条件 ? require('xxx.png') : require('xxx.png')" />

  10. 自动化测试系列:如何实现Selenium自动化读取H5手机缓存

    更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6890675.html 前言: 由于Se ...