我需要一个节省空间的概率数据结构来存储我已经计算过的值。对我来说,计算是便宜的但是空间不是 - 所以如果这个数据结构返回假阴性,我可以偶尔重做一些工作,但误报是不可接受的。所以我要找的是Bloom filter的反面。
答案 0 :(得分:7)
对于误报,您可以使用有损哈希表或LRUCache。 它是一种具有快速O(1)查找的数据结构,只会产生漏报。 如果你问“我是否运行测试X”,它会告诉你“是的,你肯定有”或“我不记得了”。
伪代码:
setup_test_table():
create test_table( some large number of entries )
clear each entry( test_table, NEVER )
return test_table
has_test_been_run_before( new_test_details, test_table ):
index = hash( test_details , test_table.length )
old_details = test_table[index].detail
// unconditionally overwrite old details with new details, LRU fashion.
// perhaps some other collision resolution technique might be better.
test_table[index].details = new_test_details
if ( old_details === test_details ) return YES
else if ( old_details === NEVER ) return NEVER
else return PERHAPS
main()
test_table = setup_test_table();
loop
test_details = generate_random_test()
status = has_test_been_run_before( test_details, test_table )
case status of
YES: do nothing;
NEVER: run test (test_details);
PERHAPS: if( rand()&1 ) run test (test_details);
next loop
end.
类似布隆过滤器的误报