[英]Efficient set intersection of a collection of sets in C++
我有一個std::set
。 我想以最快的方式找到該集合中所有集合的交集。 集合中的集合數量通常很小(〜5-10),每個集合中的元素數量通常少於1000,但偶爾可以增加到10000左右。但是我需要做這些交集成千上萬的時間,盡快。 我嘗試對幾種方法進行基准測試,如下所示:
std::set
對象中的就地交集,該對象最初復制第一組。 然后,對於后續集合,它會迭代其自身的所有元素以及集合的第i個集合,並根據需要從自身中刪除項目。 std::set_intersection
到臨時std::set
,將內容交換到當前集合,然后再次找到當前集合與下一個集合的交集,並插入到臨時集合中,依此類推。 vector
代替std::set
作為目標容器。 std::list
而不是vector
,懷疑list
會從中間提供更快的刪除速度。 std::unordered_set
)並檢查所有集中的所有項目。 事實證明,當每個集合中的元素數量較小時,使用vector
的速度略快,而對於更大的集合,使用list
的速度略快。 就地使用set
要比兩者都慢得多,其次是set_intersection
和哈希集。 是否有更快的算法/數據結構/技巧來實現這一目標? 如果需要,我可以發布代碼段。 謝謝!
您可能想嘗試std::set_intersection()
的概括:算法是對所有集合使用迭代器:
end()
,則操作完成。 因此,可以假定所有迭代器都是有效的。 x
。 std::find_if()
中移動至少與x
一樣大的元素。 x
則將其設為新的候選值,然后按迭代器順序再次搜索。 x
您找到了交集的元素:記錄該交集,增加所有迭代器,重新開始。 晚上是個好顧問,我想我可能有個主意;)
這就是為什么速度很重要的原因, vector
(或deque
)是如此出色的結構:它們在內存中發揮得很好。 因此,我絕對建議使用vector
作為我們的中介結構; 盡管只需要小心地從四肢插入/刪除四肢,以避免重新定位。
所以我想到了一個相當簡單的方法:
#include <cassert>
#include <algorithm>
#include <set>
#include <vector>
// Do not call this method if you have a single set...
// And the pointers better not be null either!
std::vector<int> intersect(std::vector< std::set<int> const* > const& sets) {
for (auto s: sets) { assert(s && "I said no null pointer"); }
std::vector<int> result; // only return this one, for NRVO to kick in
// 0. Check obvious cases
if (sets.empty()) { return result; }
if (sets.size() == 1) {
result.assign(sets.front()->begin(), sets.front()->end());
return result;
}
// 1. Merge first two sets in the result
std::set_intersection(sets[0]->begin(), sets[0]->end(),
sets[1]->begin(), sets[1]->end(),
std::back_inserter(result));
if (sets.size() == 2) { return result; }
// 2. Merge consecutive sets with result into buffer, then swap them around
// so that the "result" is always in result at the end of the loop.
std::vector<int> buffer; // outside the loop so that we reuse its memory
for (size_t i = 2; i < sets.size(); ++i) {
buffer.clear();
std::set_intersection(result.begin(), result.end(),
sets[i]->begin(), sets[i]->end(),
std::back_inserter(buffer));
swap(result, buffer);
}
return result;
}
看來是正確的 ,但是顯然我不能保證它的速度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.