倒排列表求交集算法 包括baeza yates的交集算法
- #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的交集算法的更多相关文章
- wukong引擎源码分析之索引——part 1 倒排列表本质是有序数组存储
searcher.IndexDocument(0, types.DocumentIndexData{Content: "此次百度收购将成中国互联网最大并购"}) engine.go ...
- 图解Skip List——本质是空间换时间的数据结构,在lucene的倒排列表,bigtable,hbase,cassandra的memtable,redis中sorted set中均用到
Skip List的提出已有二十多年[Pugh, W. (1990)],却依旧应用广泛(Redis.LevelDB等).作为平衡树(AVL.红黑树.伸展树.树堆)的替代方案,虽然它性能不如平衡树稳定, ...
- 倒排列表压缩算法汇总——分区Elias-Fano编码貌似是最牛叉的啊!
来看看倒排索引压缩.压缩是拿CPU换IO的最重要手段之一,不论索引是放在硬盘还是内存中.索引压缩的算法有几十种,跟文本压缩不同,索引压缩算法不仅仅需要考虑压缩率,更要考虑压缩和解压性能,否则会解压太慢 ...
- ES里设置索引中倒排列表仅仅存文档ID——采用docs存储后可以降低pos文件和cfs文件大小
index_options The index_options parameter controls what information is added to the inverted index, ...
- Poseidon 系统是一个日志搜索平台——认证看链接ppt,本质是索引的倒排列表和原始日志数据都存在HDFS,而文档和倒排的元数据都在NOSQL里,同时针对单个filed都使用了独立索引,使用MR来索引和搜索
Poseidon 系统是一个日志搜索平台,可以在百万亿条.100PB 大小的日志数据中快速分析和检索.360 公司是一个安全公司,在追踪 APT(高级持续威胁)事件,经常需要在海量的历史日志数据中检索 ...
- 设计算法,求AB两个整数集合的交集
[本文链接] http://www.cnblogs.com/hellogiser/p/ab-set-intersection.html [分析] 思路1:排序法 对集合A和集合B进行排序(升序,用快排 ...
- GPU方法做倒排压缩和交集计算
之前一直想读这篇,今天读了一下,颇有收获: 1.对文档按相似term聚类之后,delta较小,能够提高压缩率(similarity graph) 1.GPU一般能够有几百个核,有shared memo ...
- 深入浅出搜索架构引擎、方案与细节 倒排 bitmap
深入浅出搜索架构引擎.方案与细节(上) 2017-02-14 23:55 58沈剑0 20 阅读 131 一.缘起 <100亿数据1万属性数据架构设计>文章发布后,不少朋友对58同城自 ...
- Lucene核心数据结构——FST存词典,跳表存倒排或者roarning bitmap 见另外一个文章
Lucene实现倒排表没有使用bitmap,为了效率,lucene使用了一些策略,具体如下:1. 使用FST保存词典,FST可以实现快速的Seek,这种结构在当查询可以表达成自动机时(PrefixQu ...
随机推荐
- Lucene 6.5.0 入门Demo
Lucene 6.5.0 要求jdk 1.8 1.目录结构: 2.数据库环境: private int id; private String name; private float price; pr ...
- jquery实现表单验证,所以的验证通过后方可提交
<html> <head> <meta http-equiv="content-type" content="tex ...
- BOJ 2773 第K个与m互质的数
算法是关键,得出1-m内的互质数,然后类推计算即可.下面有详细说明. #include<iostream> #include<cstring> using namespace ...
- css3 画半圆和1/4圆
半圆: #circle1 { width: 100px; height: 200px; background-color: #a72525; -webkit-border-radius: 100px ...
- HttpClient配置
ClientConfiguration.java 该类讲解了HttpClient的各方面的配置 package com.ydd.study.hello.httpclient; import java. ...
- tar [options] [list of file]
打包:zcvf 解压:zxvf -c 创建新档案文件 -x 从档案文件中解出文件(释放文件) -v (verbose)显示tar命令执行的详细过程 -f 指定目标为一个文件而不是一个设备 -z 调用g ...
- Linux防火墙iptables规则设置(转)
iptables命令是Linux上常用的防火墙软件,是netfilter项目的一部分.可以直接配置,也可以通过许多前端和图形界面配置. 一.语法 iptables(选项)(参数) 二.选项 -t< ...
- webuploader设置timeout
参考:http://www.codingwhy.com/view/841.html 备注下!
- vueSSR渲染原理
优点:利于搜索引擎,解决白屏问题,因为正常情况下在index.html文件中只有一个简单的标签,没有内容,不利于爬虫搜索 场景:交互少,数据多,例如新闻,博客,论坛类等 原理:相当于服务端前面加了一层 ...
- myBatis学习笔记(10)——使用拦截器实现分页查询
1. Page package com.sm.model; import java.util.List; public class Page<T> { public static fina ...