From 580d3a7ae43ce88a9a2e9d2892cfddfe4c490d36 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 20 Jan 2023 02:08:36 +0800 Subject: [PATCH] feat: load factor test of unordered_set for `backtrack_t` --- src/analyse/backtrack.cc | 74 ++++++++++++++++++++++------------------ src/fast_cal/cal_core.cc | 24 ------------- src/main.cc | 56 +++++++++++++++--------------- 3 files changed, 69 insertions(+), 85 deletions(-) diff --git a/src/analyse/backtrack.cc b/src/analyse/backtrack.cc index cadaae8..5b6c133 100644 --- a/src/analyse/backtrack.cc +++ b/src/analyse/backtrack.cc @@ -32,20 +32,12 @@ void Analyse::backtrack_demo(uint64_t code) { // TODO: confirm code exist std::cout << cases[code].step << std::endl; - - std::string str("demo"); - std::hash str_hash; - std::cout << "hash str -> " << str_hash(str) << std::endl; - - int i = 123; - std::hash int_hash; - std::cout << "hash int -> " << int_hash(i) << std::endl; - // std::vector> dat; auto b_hash = [](const backtrack_t &b) { - std::cout << "get hash: " << b.code << std::endl; - return (std::size_t)666; + auto hash_dat = std::hash()(b.code); + std::cout << "get hash: " << b.code << " -> " << hash_dat << std::endl; + return hash_dat; }; 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; }; - std::unordered_set test(10, b_hash, b_eq); - - test.emplace(backtrack_t { - .code = 123, - .layer_num = 0, - .last = std::list{}, - .next = std::list{}, - }); - - test.emplace(backtrack_t { - .code = 233, - .layer_num = 1, - .last = std::list{}, - .next = std::list{}, - }); - - test.emplace(backtrack_t { - .code = 343, - .layer_num = 2, - .last = std::list{}, - .next = std::list{}, - }); + std::unordered_set test(0, b_hash, b_eq); + +// test.emplace(backtrack_t { +// .code = 123, +// .layer_num = 0, +// .last = std::list{}, +// .next = std::list{}, +// }); +// +// test.emplace(backtrack_t { +// .code = 233, +// .layer_num = 1, +// .last = std::list{}, +// .next = std::list{}, +// }); +// +// test.emplace(backtrack_t { +// .code = 343, +// .layer_num = 2, +// .last = std::list{}, +// .next = std::list{}, +// }); + + for (uint32_t i = 0; i < 20; ++i) { + test.emplace(backtrack_t { + .code = i * 1000, + .layer_num = 1, + .last = std::list{}, + .next = std::list{}, + }); + } + 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; + } } diff --git a/src/fast_cal/cal_core.cc b/src/fast_cal/cal_core.cc index 00b59c8..1480282 100644 --- a/src/fast_cal/cal_core.cc +++ b/src/fast_cal/cal_core.cc @@ -44,35 +44,11 @@ void FastCal::build() { } } -#include - /// found first matched target RawCode FastCal::target(const match_t &match) { auto core = init(root); - -// std::cout << "-----------------------------------" << std::endl; -// std::cout << "start count: " << cases.bucket_count() << std::endl; - while (!cache.empty()) { 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 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 } core.next_cases(cache.front()->code, cache.front()->mask); diff --git a/src/main.cc b/src/main.cc index 818fc2a..2a1d9c9 100644 --- a/src/main.cc +++ b/src/main.cc @@ -19,22 +19,22 @@ int main() { // AllCases::build(); - std::vector test_cases; - { - AllCases::build(); - std::vector all_cases; - for (uint64_t head = 0; head < 16; ++head) { - for (const auto &range : AllCases::fetch()[head]) { - all_cases.emplace_back(head << 32 | range); - } - } - for (uint32_t i = 0; i < 100; ++i) { - test_cases.emplace_back( - RawCode::from_common_code(all_cases.at(i * 293344)) - ); - } - } - std::cout << "test size -> " << test_cases.size() << std::endl; +// std::vector test_cases; +// { +// AllCases::build(); +// std::vector all_cases; +// for (uint64_t head = 0; head < 16; ++head) { +// for (const auto &range : AllCases::fetch()[head]) { +// all_cases.emplace_back(head << 32 | range); +// } +// } +// for (uint32_t i = 0; i < 100; ++i) { +// test_cases.emplace_back( +// RawCode::from_common_code(all_cases.at(i * 293344)) +// ); +// } +// } +// std::cout << "test size -> " << test_cases.size() << std::endl; // std::cout << "wait 3s" << std::endl; @@ -47,17 +47,17 @@ int main() { // auto fc = FastCal(RawCode::from_common_code("1a9bf0c")); // fc.solve(); - { - auto fc = FastCal(RawCode::unsafe_create(0)); - for (auto code : test_cases) { - fc.set_root(code); - fc.solve(); - } - } +// { +// auto fc = FastCal(RawCode::unsafe_create(0)); +// for (auto code : test_cases) { +// fc.set_root(code); +// fc.solve(); +// } +// } -// auto a = Analyse(RawCode::from_common_code("1a9bf0c")); -// a.build(); + auto a = Analyse(RawCode::from_common_code("1a9bf0c")); + a.build(); // auto ret = a.build_until([](uint64_t code) { // return ((code >> (3 * 0xD)) & 0b111) == B_2x2; // }); @@ -65,7 +65,7 @@ int main() { // std::cout << r << std::endl; // } -// a.backtrack_demo(0x7F87E0E5BFFF492); + a.backtrack_demo(0x7F87E0E5BFFF492); // a.backtrack_demo(0x1FB1E36F9FFF492); // 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) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl; -// std::cerr << (clock() - start_time) * 1000000 / CLOCKS_PER_SEC << "us" << 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::cout << "complete benchmark" << std::endl;