1. #ifndef __INTERSECT_HPP__
  2. #define __INTERSECT_HPP__
  3.  
  4. #include "probe.hpp"
  5.  
  6. namespace themas {
  7.  
  8. /*
  9. * like stl's set_intersect
  10. */
  11. template<class InputIterator, class OutputIterator>
  12. void linear_intersect(InputIterator begin1, InputIterator end1,
  13. InputIterator begin2, InputIterator end2,
  14. OutputIterator out)
  15. {
  16. if ( (end2 - begin2) > (end1 - begin1) )
  17. {
  18. // why in the world would i do this?
  19. // hmmmmmmm.......... !
  20. std::swap(begin1, begin2);
  21. std::swap(end1, end2);
  22. }
  23. while (begin1 != end1 && begin2 != end2)
  24. {
  25. if (*begin1 < *begin2)
  26. ++begin1;
  27. else if (*begin2 < *begin1)
  28. ++begin2;
  29. else
  30. {
  31. *out++ = *begin1;
  32. ++begin1;
  33. ++begin2;
  34. }
  35. }
  36. }
  37.  
  38. /*
  39. * this time with a comparator!
  40. */
  41. template<class InputIterator, class OutputIterator, class Comparator >
  42. void linear_intersect(InputIterator begin1, InputIterator end1,
  43. InputIterator begin2, InputIterator end2,
  44. OutputIterator out, Comparator cmp)
  45. {
  46. if ( (end2 - begin2) > (end1 - begin1) )
  47. {
  48. // why in the world would i do this?
  49. // hmmmmmmm.......... !
  50. std::swap(begin1, begin2);
  51. std::swap(end1, end2);
  52. }
  53. while (begin1 != end1 && begin2 != end2)
  54. {
  55. if (cmp( *begin1, *begin2 ) )
  56. ++begin1;
  57. else if ( cmp(*begin2, *begin1) )
  58. ++begin2;
  59. else
  60. {
  61. *out++ = *begin1;
  62. ++begin1;
  63. ++begin2;
  64. }
  65. }
  66. }
  67.  
  68. /*
  69. * baeza_intersect
  70. */
  71. template< template <class, class> class Probe,
  72. class RandomAccessIterator, class OutputIterator>
  73. void baeza_intersect(RandomAccessIterator begin1, RandomAccessIterator end1,
  74. RandomAccessIterator begin2, RandomAccessIterator end2,
  75. OutputIterator out)
  76. {
  77. RandomAccessIterator probe1, probe2;
  78.  
  79. if ( (end1 - begin1) < ( end2 - begin2 ) )
  80. {
  81. if ( begin1 == end1 )
  82. return;
  83. probe1 = begin1 + ( ( end1 - begin1 ) >> );
  84. probe2 = lower_bound< Probe >( begin2, end2, *probe1 );
  85. baeza_intersect< Probe >(begin1, probe1, begin2, probe2, out); // intersect left
  86. if (! (probe2 == end2 || *probe1 < *probe2 ))
  87. *out++ = *probe2++;
  88. baeza_intersect< Probe >(++probe1, end1, probe2, end2, out); // intersect right
  89. }
  90. else
  91. {
  92. if ( begin2 == end2 )
  93. return;
  94. probe2 = begin2 + ( ( end2 - begin2 ) >> );
  95. probe1 = lower_bound< Probe >( begin1, end1, *probe2 );
  96. baeza_intersect< Probe >(begin1, probe1, begin2, probe2, out); // intersect left
  97. if (! (probe1 == end1 || *probe2 < *probe1 ))
  98. *out++ = *probe1++;
  99. baeza_intersect< Probe >(probe1, end1, ++probe2, end2, out); // intersect right
  100. }
  101. }
  102.  
  103. /*
  104. * with a comparator
  105. */
  106. template< template <class, class> class Probe,
  107. class RandomAccessIterator, class OutputIterator, class Comparator >
  108. void baeza_intersect(RandomAccessIterator begin1, RandomAccessIterator end1,
  109. RandomAccessIterator begin2, RandomAccessIterator end2,
  110. OutputIterator out, Comparator cmp)
  111. {
  112. RandomAccessIterator probe1, probe2;
  113.  
  114. if ( (end1 - begin1) < ( end2 - begin2 ) )
  115. {
  116. if ( begin1 == end1 )
  117. return;
  118. probe1 = begin1 + ( ( end1 - begin1 ) >> );
  119. probe2 = lower_bound< Probe >( begin2, end2, *probe1, cmp );
  120. baeza_intersect< Probe >(begin1, probe1, begin2, probe2, out, cmp); // intersect left
  121. if (! (probe2 == end2 || cmp( *probe1, *probe2 ) ))
  122. *out++ = *probe2++;
  123. baeza_intersect< Probe >(++probe1, end1, probe2, end2, out, cmp); // intersect right
  124. }
  125. else
  126. {
  127. if ( begin2 == end2 )
  128. return;
  129. probe2 = begin2 + ( ( end2 - begin2 ) >> );
  130. probe1 = lower_bound< Probe >( begin1, end1, *probe2, cmp );
  131. baeza_intersect< Probe >(begin1, probe1, begin2, probe2, out, cmp); // intersect left
  132. if (! (probe1 == end1 || cmp( *probe2, *probe1 ) ))
  133. *out++ = *probe1++;
  134. baeza_intersect< Probe >(probe1, end1, ++probe2, end2, out, cmp); // intersect right
  135. }
  136. }
  137.  
  138. } // themas
  139.  
  140. #endif // __INTERSECT_HPP__

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

  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <ctime>
  5.  
  6. #include <boost/random/mersenne_twister.hpp>
  7.  
  8. #include "intersect.hpp"
  9.  
  10. using namespace themas;
  11.  
  12. int main(int argc, char * argv[])
  13. {
  14. std::set<int> nums1, nums2;
  15. std::vector<int> result1, result2, result3;
  16.  
  17. boost::mt19937 rng(time(NULL));
  18.  
  19. for ( unsigned int i = rng() % ; i != ; --i )
  20. nums1.insert(rng());
  21. for ( unsigned int i = rng() % ; i != ; --i )
  22. nums2.insert(rng());
  23. for ( unsigned int i = rng() % ; i != ; --i )
  24. {
  25. unsigned int j = rng();
  26. nums1.insert(j);
  27. nums2.insert(j);
  28. }
  29. std::vector<int> v1(nums1.begin(), nums1.end()), v2(nums2.begin(), nums2.end());
  30.  
  31. linear_intersect(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(result1));
  32. baeza_intersect < binary_probe > (v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(result2));
  33. baeza_intersect < interpolation_probe > (v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(result3));
  34.  
  35. if (result1 != result2 || result1 != result3)
  36. std::cout << "FAIL!" << std::endl;
  37. else
  38. std::cout << "PASS!" << std::endl;
  39. }

倒排列表求交集算法 包括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. Lucene 6.5.0 入门Demo

    Lucene 6.5.0 要求jdk 1.8 1.目录结构: 2.数据库环境: private int id; private String name; private float price; pr ...

  2. jquery实现表单验证,所以的验证通过后方可提交

    <html>     <head>         <meta http-equiv="content-type" content="tex ...

  3. BOJ 2773 第K个与m互质的数

    算法是关键,得出1-m内的互质数,然后类推计算即可.下面有详细说明. #include<iostream> #include<cstring> using namespace ...

  4. css3 画半圆和1/4圆

    半圆: #circle1 { width: 100px; height: 200px; background-color: #a72525; -webkit-border-radius: 100px ...

  5. HttpClient配置

    ClientConfiguration.java 该类讲解了HttpClient的各方面的配置 package com.ydd.study.hello.httpclient; import java. ...

  6. tar [options] [list of file]

    打包:zcvf 解压:zxvf -c 创建新档案文件 -x 从档案文件中解出文件(释放文件) -v (verbose)显示tar命令执行的详细过程 -f 指定目标为一个文件而不是一个设备 -z 调用g ...

  7. Linux防火墙iptables规则设置(转)

    iptables命令是Linux上常用的防火墙软件,是netfilter项目的一部分.可以直接配置,也可以通过许多前端和图形界面配置. 一.语法 iptables(选项)(参数) 二.选项 -t< ...

  8. webuploader设置timeout

    参考:http://www.codingwhy.com/view/841.html 备注下!

  9. vueSSR渲染原理

    优点:利于搜索引擎,解决白屏问题,因为正常情况下在index.html文件中只有一个简单的标签,没有内容,不利于爬虫搜索 场景:交互少,数据多,例如新闻,博客,论坛类等 原理:相当于服务端前面加了一层 ...

  10. myBatis学习笔记(10)——使用拦截器实现分页查询

    1. Page package com.sm.model; import java.util.List; public class Page<T> { public static fina ...