#ifndef __INTERSECT_HPP__
#define __INTERSECT_HPP__ #include "probe.hpp" namespace themas { /*
* like stl's set_intersect
*/
template<class InputIterator, class OutputIterator>
void linear_intersect(InputIterator begin1, InputIterator end1,
InputIterator begin2, InputIterator end2,
OutputIterator out)
{
if ( (end2 - begin2) > (end1 - begin1) )
{
// why in the world would i do this?
// hmmmmmmm.......... !
std::swap(begin1, begin2);
std::swap(end1, end2);
}
while (begin1 != end1 && begin2 != end2)
{
if (*begin1 < *begin2)
++begin1;
else if (*begin2 < *begin1)
++begin2;
else
{
*out++ = *begin1;
++begin1;
++begin2;
}
}
} /*
* this time with a comparator!
*/
template<class InputIterator, class OutputIterator, class Comparator >
void linear_intersect(InputIterator begin1, InputIterator end1,
InputIterator begin2, InputIterator end2,
OutputIterator out, Comparator cmp)
{
if ( (end2 - begin2) > (end1 - begin1) )
{
// why in the world would i do this?
// hmmmmmmm.......... !
std::swap(begin1, begin2);
std::swap(end1, end2);
}
while (begin1 != end1 && begin2 != end2)
{
if (cmp( *begin1, *begin2 ) )
++begin1;
else if ( cmp(*begin2, *begin1) )
++begin2;
else
{
*out++ = *begin1;
++begin1;
++begin2;
}
}
} /*
* baeza_intersect
*/
template< template <class, class> class Probe,
class RandomAccessIterator, class OutputIterator>
void baeza_intersect(RandomAccessIterator begin1, RandomAccessIterator end1,
RandomAccessIterator begin2, RandomAccessIterator end2,
OutputIterator out)
{
RandomAccessIterator probe1, probe2; if ( (end1 - begin1) < ( end2 - begin2 ) )
{
if ( begin1 == end1 )
return;
probe1 = begin1 + ( ( end1 - begin1 ) >> );
probe2 = lower_bound< Probe >( begin2, end2, *probe1 );
baeza_intersect< Probe >(begin1, probe1, begin2, probe2, out); // intersect left
if (! (probe2 == end2 || *probe1 < *probe2 ))
*out++ = *probe2++;
baeza_intersect< Probe >(++probe1, end1, probe2, end2, out); // intersect right
}
else
{
if ( begin2 == end2 )
return;
probe2 = begin2 + ( ( end2 - begin2 ) >> );
probe1 = lower_bound< Probe >( begin1, end1, *probe2 );
baeza_intersect< Probe >(begin1, probe1, begin2, probe2, out); // intersect left
if (! (probe1 == end1 || *probe2 < *probe1 ))
*out++ = *probe1++;
baeza_intersect< Probe >(probe1, end1, ++probe2, end2, out); // intersect right
}
} /*
* with a comparator
*/
template< template <class, class> class Probe,
class RandomAccessIterator, class OutputIterator, class Comparator >
void baeza_intersect(RandomAccessIterator begin1, RandomAccessIterator end1,
RandomAccessIterator begin2, RandomAccessIterator end2,
OutputIterator out, Comparator cmp)
{
RandomAccessIterator probe1, probe2; if ( (end1 - begin1) < ( end2 - begin2 ) )
{
if ( begin1 == end1 )
return;
probe1 = begin1 + ( ( end1 - begin1 ) >> );
probe2 = lower_bound< Probe >( begin2, end2, *probe1, cmp );
baeza_intersect< Probe >(begin1, probe1, begin2, probe2, out, cmp); // intersect left
if (! (probe2 == end2 || cmp( *probe1, *probe2 ) ))
*out++ = *probe2++;
baeza_intersect< Probe >(++probe1, end1, probe2, end2, out, cmp); // intersect right
}
else
{
if ( begin2 == end2 )
return;
probe2 = begin2 + ( ( end2 - begin2 ) >> );
probe1 = lower_bound< Probe >( begin1, end1, *probe2, cmp );
baeza_intersect< Probe >(begin1, probe1, begin2, probe2, out, cmp); // intersect left
if (! (probe1 == end1 || cmp( *probe2, *probe1 ) ))
*out++ = *probe1++;
baeza_intersect< Probe >(probe1, end1, ++probe2, end2, out, cmp); // intersect right
}
} } // themas #endif // __INTERSECT_HPP__

转自:https://github.com/erikfrey/themas/blob/master/src/set_intersection/intersect.hpp

#include <iostream>
#include <vector>
#include <set>
#include <ctime> #include <boost/random/mersenne_twister.hpp> #include "intersect.hpp" using namespace themas; int main(int argc, char * argv[])
{
std::set<int> nums1, nums2;
std::vector<int> result1, result2, result3; boost::mt19937 rng(time(NULL)); for ( unsigned int i = rng() % ; i != ; --i )
nums1.insert(rng());
for ( unsigned int i = rng() % ; i != ; --i )
nums2.insert(rng());
for ( unsigned int i = rng() % ; i != ; --i )
{
unsigned int j = rng();
nums1.insert(j);
nums2.insert(j);
}
std::vector<int> v1(nums1.begin(), nums1.end()), v2(nums2.begin(), nums2.end()); linear_intersect(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(result1));
baeza_intersect < binary_probe > (v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(result2));
baeza_intersect < interpolation_probe > (v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(result3)); if (result1 != result2 || result1 != result3)
std::cout << "FAIL!" << std::endl;
else
std::cout << "PASS!" << std::endl;
}

倒排列表求交集算法 包括baeza yates的交集算法的更多相关文章

  1. wukong引擎源码分析之索引——part 1 倒排列表本质是有序数组存储

    searcher.IndexDocument(0, types.DocumentIndexData{Content: "此次百度收购将成中国互联网最大并购"}) engine.go ...

  2. 图解Skip List——本质是空间换时间的数据结构,在lucene的倒排列表,bigtable,hbase,cassandra的memtable,redis中sorted set中均用到

    Skip List的提出已有二十多年[Pugh, W. (1990)],却依旧应用广泛(Redis.LevelDB等).作为平衡树(AVL.红黑树.伸展树.树堆)的替代方案,虽然它性能不如平衡树稳定, ...

  3. 倒排列表压缩算法汇总——分区Elias-Fano编码貌似是最牛叉的啊!

    来看看倒排索引压缩.压缩是拿CPU换IO的最重要手段之一,不论索引是放在硬盘还是内存中.索引压缩的算法有几十种,跟文本压缩不同,索引压缩算法不仅仅需要考虑压缩率,更要考虑压缩和解压性能,否则会解压太慢 ...

  4. ES里设置索引中倒排列表仅仅存文档ID——采用docs存储后可以降低pos文件和cfs文件大小

    index_options The index_options parameter controls what information is added to the inverted index, ...

  5. Poseidon 系统是一个日志搜索平台——认证看链接ppt,本质是索引的倒排列表和原始日志数据都存在HDFS,而文档和倒排的元数据都在NOSQL里,同时针对单个filed都使用了独立索引,使用MR来索引和搜索

    Poseidon 系统是一个日志搜索平台,可以在百万亿条.100PB 大小的日志数据中快速分析和检索.360 公司是一个安全公司,在追踪 APT(高级持续威胁)事件,经常需要在海量的历史日志数据中检索 ...

  6. 设计算法,求AB两个整数集合的交集

    [本文链接] http://www.cnblogs.com/hellogiser/p/ab-set-intersection.html [分析] 思路1:排序法 对集合A和集合B进行排序(升序,用快排 ...

  7. GPU方法做倒排压缩和交集计算

    之前一直想读这篇,今天读了一下,颇有收获: 1.对文档按相似term聚类之后,delta较小,能够提高压缩率(similarity graph) 1.GPU一般能够有几百个核,有shared memo ...

  8. 深入浅出搜索架构引擎、方案与细节 倒排 bitmap

    深入浅出搜索架构引擎.方案与细节(上) 2017-02-14 23:55 58沈剑0  20  阅读 131 一.缘起 <100亿数据1万属性数据架构设计>文章发布后,不少朋友对58同城自 ...

  9. Lucene核心数据结构——FST存词典,跳表存倒排或者roarning bitmap 见另外一个文章

    Lucene实现倒排表没有使用bitmap,为了效率,lucene使用了一些策略,具体如下:1. 使用FST保存词典,FST可以实现快速的Seek,这种结构在当查询可以表达成自动机时(PrefixQu ...

随机推荐

  1. hdu 1717

    小数化分数2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  2. R语言入门---杂记(一)---R的常用函数

    1.nchar():查看字符串长度. 2.rev(): 给你的数据翻个个 3.sort():给你数据排个序(默认从小到大依次排列) 4.runif():产生均匀分布的随机数 #runif

  3. PEP8 Python编码规范(转)

    一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车.3 类 ...

  4. Mybatis resultMap空值映射问题

    参考博客:https://www.oschina.net/question/1032714_224673 http://stackoverflow.com/questions/22852383/how ...

  5. SQL Server 命令行操作

    连接sqlcmd -S localhost -U SA -P '123456'; 执行脚本 sqlcmd -S localhost -U SA -P '123456' -i /root/dbo.sql ...

  6. 【.Net 学习系列】-- 利用Aspose转换Excel为PDF文件

    功能: 从数据库中查询出数据 利用Aspose.cell + Excel模板绑定数据源生成Excel文件 通过Aspose.pdf + 生成好的Excel生成PDF文件 实现: 查询数据,根据Exce ...

  7. iOS WKWebView添加网页加载进度条(转)

    一.效果展示 WKWebProgressViewDemo.gif 二.主要步骤 1.添加UIProgressView属性 @property (nonatomic, strong) WKWebView ...

  8. scrapy的自动限速(AutoThrottle)扩展

    该扩展能根据Scrapy服务器及您爬取的网站的负载自动限制爬取速度. 设计目标 更友好的对待网站,而不使用默认的下载延迟0. 自动调整scrapy来优化下载速度,使得用户不用调节下载延迟及并发请求数来 ...

  9. ImportError: No module named _curses;Color support is disabled, python-curses is not installed.解决办法

    linux系统默认安装了python2.6, 但是发现python2.7 import curses时 提示 找不到_curses 错误.  用pip(python2.7 )安装了curses-204 ...

  10. Delphi 的内存操作函数(1): 给字符指针分配内存

    马上能想到的函数有: GetMem AllocMem ReallocMem FreeMem GetMemory ReallocMemory FreeMemory New Dispose NewStr ...