Skip to content

Commit b666a60

Browse files
authored
Merge pull request #29 from byronhe/mutualPartitioningIntersect
add mutualPartitioningIntersect
2 parents b4d62c3 + 0a7901c commit b666a60

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

src/intersection.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,37 @@ size_t onesidedgallopingintersection(const uint32_t *smallset,
9292
return out - initout;
9393
}
9494

95+
// from: http://didawiki.di.unipi.it/doku.php/magistraleinformaticanetworking/ae/ae2019/start#books_notes_etc
96+
// "The magic of Algorithms! "
97+
// Chap. 6, Algorithm 6.1 Intersection based on Mutual Partitioning
98+
//
99+
size_t mutualPartitioningIntersect(const uint32_t* small_set, size_t small_length,
100+
const uint32_t* large_set, size_t large_length,
101+
uint32_t * result) {
102+
if ((small_length <= 0) || (large_length <= 0)) {
103+
return 0;
104+
}
105+
if (small_length > large_length) {
106+
return mutualPartitioningIntersect(large_set, large_length, small_set, small_length, result);
107+
}
108+
int mid_index = small_length / 2;
109+
const auto mid_val = small_set[mid_index];
110+
auto it = std::lower_bound(large_set, large_set + large_length, mid_val);
111+
size_t out_num = mutualPartitioningIntersect(small_set, mid_index, large_set, it - large_set, result);
112+
if (it == large_set + large_length) {
113+
return out_num;
114+
}
115+
result += out_num;
116+
if (*it == mid_val) {
117+
*result++ = mid_val;
118+
++it;
119+
++out_num;
120+
}
121+
++mid_index;
122+
return out_num + mutualPartitioningIntersect(small_set + mid_index, small_length - mid_index,
123+
it, large_set + large_length - it, result);
124+
}
125+
95126
/**
96127
* Fast scalar scheme designed by N. Kurz.
97128
*/
@@ -1269,6 +1300,7 @@ initializeintersectionfactory() {
12691300
std::map<std::string, intersectionfunction> schemes;
12701301
schemes["simd"] = SIMDintersection;
12711302
schemes["galloping"] = onesidedgallopingintersection;
1303+
schemes["mut_part"] = mutualPartitioningIntersect;
12721304
schemes["scalar"] = scalar;
12731305
schemes["v1"] = v1;
12741306
schemes["v3"] = v3;

0 commit comments

Comments
 (0)