使用boost::multi_index高速构建排行榜
使用boost::multi_index高速构建排行榜
前几天在boost的maillist上看到boost::multi_index将要支持ranked_index(邮件内容见附件2),这实乃我等苦逼写排行榜的人的福音。大家再也不用去分析rank_tree里的内容了,故拿出来和大家一起分享。
ranked_index其内部实现和rank_tree是一样的。但其优点是集成在multi_index内部,使用上很方便,而且支持ranked_unique和ranked_non_unique两种索引。
话不多说,首先从http://tinyurl.com/kemwk8q下载下来multi_index的库文件,然后替换boost_1.58中的multi_index,有一个文件夹boost/multi_index和两个文件boost/multi_index_container.hpp,
boost/multi_index_container_fwd.hpp须要替换,替换之后,我们就能够把附件1里的代码拷贝到vs2008里执行啦,输出结果为:
245044518
1
0
了解过rank_tree组件的同学肯定知道是怎么回事了,ranked_index和rank_tree一样比普通的map多了两个函数。其相应关系是:
|
ranked_index |
rank_tree |
说明 |
|
nth |
find_by_rank |
名次到迭代器 |
|
rank |
rank |
迭代器到名次 |
附件1:
// test_rank.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/indexed_by.hpp>
#include <boost/multi_index/ranked_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <iostream>
namespace bmi = boost::multi_index;
typedef unsigned int uint32_t;
typedef int int32_t;
struct Data
{
int32_t key;
uint32_t uin;
};
struct tag_key{};
struct tag_uin{};
typedef boost::multi_index_container<
Data, bmi::indexed_by<
bmi::ranked_non_unique<
bmi::tag<tag_key>,
bmi::member<Data, int32_t, &Data::key>
>,
bmi::ordered_unique<
bmi::tag<tag_uin>,
bmi::member<Data, uint32_t, &Data::uin>
>
>
> container_t;
typedef container_t::index<tag_key>::type key_index_t;
typedef container_t::index<tag_uin>::type uin_index_t;
int _tmain(int argc, _TCHAR* argv[])
{
container_t c;
Data data;
data.key = 200;
data.uin = 245044518;
c.insert(data);
data.key = 100;
data.uin = 503063727;
c.insert(data);
key_index_t& key_index = boost::get<tag_key>(c);
uin_index_t& uin_index = boost::get<tag_uin>(c);
std::cout << key_index.nth(1)->uin << std::endl;
std::cout << key_index.rank(key_index.nth(1))
<< std::endl;
std::cout << key_index.rank(bmi::project<tag_key>(c, uin_index.find(503063727)))
<< std::endl;
return 0;
}
附件2:
Message: 2
Date: Wed, 22 Apr 2015 19:34:04 +0000 (UTC)
From: Joaquin M Lopez Munoz <joaquin@tid.es>
To: boost-users@lists.boost.org
Subject: [Boost-users] [multi_index] Announcing preview of ranked
indices
Message-ID: <loom.20150422T212903-565@post.gmane.org>
Content-Type: text/plain; charset=utf-8
Hi,
It's been 11 years since first proposed, but here it is at last. I'm
releasing a prereview of ranked indices for Boost.MultiIndex:
struct person
{
int age;
std::string name;
};
typedef multi_index_container<
person,
indexed_by<
ranked_non_unique<member<person,int,&person::age> >,
ordered_non_unique<member<person,std::string,&person::name>>>>
multi_t;
multi_t m={{40,"Joe"},{25,"Jill"},{30,"Kurt"},{32,"Sue"}};
auto it=m.emplace(31,"Maggie").first;
std::cout<<m.rank(it)<<" persons younger than "<<it->name<<"\n";
auto n=m.size()/2;
std::cout<<n<<" persons are less than "<<m.nth(n)->age<<" years old\n";
Ranked indices add a bunch of extra rank-related capabilities to the
interface of ordered indices. The rank of an element is its numerical
position in the index. nth(i) returns an iterator to the element with
rank i, whereas rank(it) is the inverse operation. Both execute in
log(n) time.
Additionally, the member functions
find_rank
lower_bound_rank
upper_bound_rank
equal_range_rank
range_rank
behave as their "_rank"-less counterparts except they return ranks rather
than iterators.
One drawback of ranked indices wrt ordered indices (other than their
slower perfomance and higher memory consumption) is that deletion of
elements is log(n) (in ordered indices it is constant time).
Download lib preview: http://tinyurl.com/kemwk8q
Tutorial: http://tinyurl.com/q36c9f7
Reference: http://tinyurl.com/pvoulgr
For people interested in this, I'd appreciate if you could download the
source, copy it on top of Boost 1.58 (1.57 should work as well), play a bit
with the new feature and report your results. Also, opinions on naming
are welcome. Target version for releasing is Boost 1.59, so we have
plenty of time to discuss the design before it is final.
Thank you,
Joaqu?n M L?
pez Mu?oz
Telef?
nica
------------------------------
使用boost::multi_index高速构建排行榜的更多相关文章
- boost::multi_index 提供一种千人在线即时排行榜的设计思路
原文地址: http://www.limerence2017.com/2019/06/23/cpp01/ 做游戏或金融后台开发,经常会遇到设计开发排行榜的需求.比如玩家的充值排行,战力排行等等.而这种 ...
- 玩转Windows服务系列——使用Boost.Application快速构建Windows服务
玩转Windows服务系列——创建Windows服务一文中,介绍了如何快速使用VS构建一个Windows服务.Debug.Release版本的注册和卸载,及其原理和服务运行.停止流程浅析分别介绍了Wi ...
- boost multi_index
/** boost 多索引容器的一般使用 这里使用google 的gmock 库来验证对boost 多索引模板的使用,并验证. 这里是手敲的,可能会有一些字符敲错的情况,编译错误的放,修改一下,同时链 ...
- JavaScript面向对象编程(9)高速构建继承关系之整合原型链
前面我们铺垫了非常多细节.是为了让大家更加明晰prototype的使用细节: 如今能够将前面的知识整合起来,写一个函数用于高速构建基于原型链的继承关系了: function extend(Child, ...
- 用 boost::multi_index 管理玩家
用 boost::multi_index 管理玩家(金庆的专栏)网游服务器上的玩家集合需要多种索引:如用ID查找,角色名查找, 用登录时分配的会话ID查找.用boost::multi_index进行玩 ...
- Yii2高速构建RESTful Web服务功能简单介绍
Yii2相比Yii1而言,一个重大的改进是内置了功能完备的RESTful支持. 其内置RESTful支持提供了例如以下功能: 使用ActiveRecord的通用接口来高速构建原型: 应答格式协商(缺省 ...
- boost::multi_index 多索引容器
#include "stdafx.h" #include <string> #include <boost/multi_index_container.hpp&g ...
- boost multi_index 插入返回值
boost multi_index 对象插入函数emplace() 的返回值,是一个std::pair<iterator, bool>该pair 的first 是一个插入成功的位置,第二个 ...
- boost multi_index简单了解
#include <string> #include <iostream> #include <boost/multi_index_container.hpp> # ...
随机推荐
- 基于python3.x,使用Tornado中的torndb模块操作数据库
目前Tornado中的torndb模块是不支持python3.x,所以需要修改部分torndb源码即可正常使用 1.开发环境介绍 操作系统:win8(64位),python版本:python3.6(3 ...
- 《项目架构那点儿事》——快速构建Junit用例
[前 言]按照惯例,在实际项目中我往往会对自己编写的程序进行测试,当测试通过后才能将其用于实战中,当然,编写单元测试是不可避免的,可以直接清晰的检验出 我们程序的可靠性.可只执行性,从中发现问题从而得 ...
- From missionary to firebrand--Eisle Tu [20160102]
From missionary to firebrand 杜叶锡恩(1913年(癸丑年)-2015年(乙未年),英文名字Elsie Hume Elliot Tu,丈夫是教育家杜学魁.她是香港著名的 ...
- Web登录敲门砖之sql注入
声明:文本原创,转载请说明出处,若因本文而产生任何违法违纪行为将与本人无关.在百度.博客园.oschina.github .SegmentFault.上面都关于sql注入的文章和工具.看过很多sql注 ...
- Java多线程之线程池详解
前言 在认识线程池之前,我们需要使用线程就去创建一个线程,但是我们会发现有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因 ...
- QuickTime视频解析问题
在QuickTime中可以解析出视频并播放视频,解析的格式后缀名为.mov,之后将该视频导入到Unity Project中,显示未解析到视频文件,本来应该会自动生成MovieTexture材质,但是并 ...
- dnsmasq服务的安装与配置
在ubuntu16.04上安装dnsmasq服务,在本地做泛域名解析 安装 $ apt-get install dnsmasq -y $ /etc/init.d/dnsmasq start 配置 Dn ...
- javaScript基础概念小知识点集
数据类型 typeof是一个操作符而不是函数,因此例子中圆括号尽管可以使用,但不是必须的. 只要在保存对象的变量还没有真正保存对象,就应该明确的让该变量保存null NaN是一个特殊的数值,与任何值都 ...
- WEB漏洞攻击之验证码绕过浅析
最近安全部门对WEB系统进行了一次漏洞整改,发现了某个系统存在验证码绕过风险. 根据安全部门提供的信息,该漏洞构造场景是通过一层中间代理(Burpsuite Proxy)拦截客户端与服务端的请求,通过 ...
- zzuli 1817: match number 模拟
1817: match number Time Limit: 1 Sec Memory Limit: 128 MB Submit: 199 Solved: 72 SubmitStatusWeb B ...