我最近玩了一个std::unordered_set http://en.cppreference.com/w/cpp/container/unordered_set。我怀疑我的 STL 版本会跟踪某些 FILO 数据结构(看起来像列表)中的非空存储桶。我想这样做是为了提供O(n)
完整的时间穿越std::unordered_set
(where n
表示 a 中的元素数量unordered_set
with m
水桶和m
远大于n
)。这改进了对所有存储桶的简单遍历O(m)
time.
我已经测试过,确实可以遍历大量且非常稀疏的数据unordered_set
s (with begin
- end
)比简单地遍历所有桶要快得多。
Question:这个遍历运行时间有标准保证吗?或者这只是我特定标准库的一个功能?
这是我可以使用的测试代码:
#include <iostream>
#include <vector>
#include <numeric>
#include <unordered_set>
using namespace std;
void test(vector<int> data, int alloc_size) {
unordered_set<int> set(alloc_size);
for (auto i: data) {
set.insert(i);
}
for (size_t bidx = 0; bidx < set.bucket_count(); ++bidx) {
cout << "[B" << bidx << ":";
for (auto bit = set.begin(bidx); bit != set.end(bidx); ++bit) {
cout << " " << *bit;
}
cout << "] ";
}
cout << " {";
for (auto const & d: set) {
cout << d << " ";
}
cout << "}" << endl;
}
int main() {
test({1, 2, 0}, 3);
test({1, 2, 0, 7}, 3);
test({18, 6, 11, 3, 13, 4}, 20);
test({18, 6, 11, 3, 13, 4, 34}, 20);
}
哪个打印:
[B0: 0] [B1: 1] [B2: 2] [B3:] [B4:] {0 2 1 }
[B0: 0] [B1: 1] [B2: 7 2] [B3:] [B4:] {0 7 2 1 }
[B0:] [B1:] [B2:] [B3: 3] [B4: 4] [B5:] [B6: 6] [B7:] [B8:] [B9:] [B10:] [B11: 11] [B12:] [B13: 13] [B14:] [B15:] [B16:] [B17:] [B18: 18] [B19:] [B20:] [B21:] [B22:] {4 13 3 11 6 18 }
[B0:] [B1:] [B2:] [B3: 3] [B4: 4] [B5:] [B6: 6] [B7:] [B8:] [B9:] [B10:] [B11: 34 11] [B12:] [B13: 13] [B14:] [B15:] [B16:] [B17:] [B18: 18] [B19:] [B20:] [B21:] [B22:] {4 13 3 34 11 6 18 }
看来begin
- end
遍历以桶变为非空的相反顺序报告桶(参见第一行和第三行)。插入已经非空的桶中不会改变这个顺序(参见第二行和第四行)。