Browse Source

feat: load factor test of unordered_set for `backtrack_t`

legacy
Dnomd343 2 years ago
parent
commit
580d3a7ae4
  1. 74
      src/analyse/backtrack.cc
  2. 24
      src/fast_cal/cal_core.cc
  3. 56
      src/main.cc

74
src/analyse/backtrack.cc

@ -32,20 +32,12 @@ void Analyse::backtrack_demo(uint64_t code) {
// TODO: confirm code exist // TODO: confirm code exist
std::cout << cases[code].step << std::endl; std::cout << cases[code].step << std::endl;
std::string str("demo");
std::hash<std::string> str_hash;
std::cout << "hash str -> " << str_hash(str) << std::endl;
int i = 123;
std::hash<int> int_hash;
std::cout << "hash int -> " << int_hash(i) << std::endl;
// std::vector<std::unordered_set<backtrack_t>> dat; // std::vector<std::unordered_set<backtrack_t>> dat;
auto b_hash = [](const backtrack_t &b) { auto b_hash = [](const backtrack_t &b) {
std::cout << "get hash: " << b.code << std::endl; auto hash_dat = std::hash<uint64_t>()(b.code);
return (std::size_t)666; std::cout << "get hash: " << b.code << " -> " << hash_dat << std::endl;
return hash_dat;
}; };
auto b_eq = [](const backtrack_t &b1, const backtrack_t &b2) { auto b_eq = [](const backtrack_t &b1, const backtrack_t &b2) {
@ -53,29 +45,45 @@ void Analyse::backtrack_demo(uint64_t code) {
return b1.code == b2.code; return b1.code == b2.code;
}; };
std::unordered_set<backtrack_t, decltype(b_hash), decltype(b_eq)> test(10, b_hash, b_eq); std::unordered_set<backtrack_t, decltype(b_hash), decltype(b_eq)> test(0, b_hash, b_eq);
test.emplace(backtrack_t { // test.emplace(backtrack_t {
.code = 123, // .code = 123,
.layer_num = 0, // .layer_num = 0,
.last = std::list<backtrack_t*>{}, // .last = std::list<backtrack_t*>{},
.next = std::list<backtrack_t*>{}, // .next = std::list<backtrack_t*>{},
}); // });
//
test.emplace(backtrack_t { // test.emplace(backtrack_t {
.code = 233, // .code = 233,
.layer_num = 1, // .layer_num = 1,
.last = std::list<backtrack_t*>{}, // .last = std::list<backtrack_t*>{},
.next = std::list<backtrack_t*>{}, // .next = std::list<backtrack_t*>{},
}); // });
//
test.emplace(backtrack_t { // test.emplace(backtrack_t {
.code = 343, // .code = 343,
.layer_num = 2, // .layer_num = 2,
.last = std::list<backtrack_t*>{}, // .last = std::list<backtrack_t*>{},
.next = std::list<backtrack_t*>{}, // .next = std::list<backtrack_t*>{},
}); // });
for (uint32_t i = 0; i < 20; ++i) {
test.emplace(backtrack_t {
.code = i * 1000,
.layer_num = 1,
.last = std::list<backtrack_t*>{},
.next = std::list<backtrack_t*>{},
});
}
std::cout << "------------------------" << std::endl;
std::cout << "hashmap size: " << test.size() << std::endl;
std::cout << "bucket count: " << test.bucket_count() << std::endl;
std::cout << "load factor: " << test.load_factor() << std::endl;
for (uint32_t i = 0; i < test.bucket_count(); ++i) {
std::cout << "bucket " << i << " -> " << test.bucket_size(i) << std::endl;
}
} }

24
src/fast_cal/cal_core.cc

@ -44,35 +44,11 @@ void FastCal::build() {
} }
} }
#include <iostream>
/// found first matched target /// found first matched target
RawCode FastCal::target(const match_t &match) { RawCode FastCal::target(const match_t &match) {
auto core = init(root); auto core = init(root);
// std::cout << "-----------------------------------" << std::endl;
// std::cout << "start count: " << cases.bucket_count() << std::endl;
while (!cache.empty()) { while (!cache.empty()) {
if (match(cache.front()->code)) { if (match(cache.front()->code)) {
// std::cout << "hashmap size: " << cases.size() << std::endl;
// std::cout << "bucket count: " << cases.bucket_count() << std::endl;
// std::cout << "load factor: " << cases.load_factor() << std::endl;
// std::vector<uint32_t> temp;
// for (uint32_t i = 0; i < cases.bucket_count(); ++i) {
// uint32_t size = cases.bucket_size(i);
// if (size >= temp.size()) {
// temp.resize(size + 1);
// }
// ++temp.at(size);
// }
// for (uint32_t i = 0; i < temp.size(); ++i) {
// std::cout << "size " << i << " -> " << temp[i] << std::endl;
// }
return RawCode::unsafe_create(cache.front()->code); // match target return RawCode::unsafe_create(cache.front()->code); // match target
} }
core.next_cases(cache.front()->code, cache.front()->mask); core.next_cases(cache.front()->code, cache.front()->mask);

56
src/main.cc

@ -19,22 +19,22 @@ int main() {
// AllCases::build(); // AllCases::build();
std::vector<RawCode> test_cases; // std::vector<RawCode> test_cases;
{ // {
AllCases::build(); // AllCases::build();
std::vector<uint64_t> all_cases; // std::vector<uint64_t> all_cases;
for (uint64_t head = 0; head < 16; ++head) { // for (uint64_t head = 0; head < 16; ++head) {
for (const auto &range : AllCases::fetch()[head]) { // for (const auto &range : AllCases::fetch()[head]) {
all_cases.emplace_back(head << 32 | range); // all_cases.emplace_back(head << 32 | range);
} // }
} // }
for (uint32_t i = 0; i < 100; ++i) { // for (uint32_t i = 0; i < 100; ++i) {
test_cases.emplace_back( // test_cases.emplace_back(
RawCode::from_common_code(all_cases.at(i * 293344)) // RawCode::from_common_code(all_cases.at(i * 293344))
); // );
} // }
} // }
std::cout << "test size -> " << test_cases.size() << std::endl; // std::cout << "test size -> " << test_cases.size() << std::endl;
// std::cout << "wait 3s" << std::endl; // std::cout << "wait 3s" << std::endl;
@ -47,17 +47,17 @@ int main() {
// auto fc = FastCal(RawCode::from_common_code("1a9bf0c")); // auto fc = FastCal(RawCode::from_common_code("1a9bf0c"));
// fc.solve(); // fc.solve();
{ // {
auto fc = FastCal(RawCode::unsafe_create(0)); // auto fc = FastCal(RawCode::unsafe_create(0));
for (auto code : test_cases) { // for (auto code : test_cases) {
fc.set_root(code); // fc.set_root(code);
fc.solve(); // fc.solve();
} // }
} // }
// auto a = Analyse(RawCode::from_common_code("1a9bf0c")); auto a = Analyse(RawCode::from_common_code("1a9bf0c"));
// a.build(); a.build();
// auto ret = a.build_until([](uint64_t code) { // auto ret = a.build_until([](uint64_t code) {
// return ((code >> (3 * 0xD)) & 0b111) == B_2x2; // return ((code >> (3 * 0xD)) & 0b111) == B_2x2;
// }); // });
@ -65,7 +65,7 @@ int main() {
// std::cout << r << std::endl; // std::cout << r << std::endl;
// } // }
// a.backtrack_demo(0x7F87E0E5BFFF492); a.backtrack_demo(0x7F87E0E5BFFF492);
// a.backtrack_demo(0x1FB1E36F9FFF492); // a.backtrack_demo(0x1FB1E36F9FFF492);
// auto start_time = clock(); // auto start_time = clock();
@ -111,8 +111,8 @@ int main() {
// std::cerr << (clock() - start_time) / CLOCKS_PER_SEC << "s" << std::endl; // std::cerr << (clock() - start_time) / CLOCKS_PER_SEC << "s" << std::endl;
std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl; // std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl;
// std::cerr << (clock() - start_time) * 1000000 / CLOCKS_PER_SEC << "us" << std::endl; std::cerr << (clock() - start_time) * 1000000 / CLOCKS_PER_SEC << "us" << std::endl;
// std::cout << "complete benchmark" << std::endl; // std::cout << "complete benchmark" << std::endl;

Loading…
Cancel
Save