src/util/unordered_stl_hacker/lib.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "../alias/others/lib.hpp" | ||
| 4 | #include "../rand/lib.hpp" | ||
| 5 | |||
| 6 | namespace tifa_libs { | ||
| 7 | |||
| 8 | // Just run it on the same compiler and same options as the solution you want to hack. | ||
| 9 | // Works for integral types, and strn. Slow for strn. | ||
| 10 | template <class T> | ||
| 11 | 5 | inline vec<T> unordered_stl_hacker(usz n) NE { | |
| 12 | 5 | auto get_bucket_counts = [n]() NE { | |
| 13 | vecz ans; | ||
| 14 | std::unordered_set<int> s; | ||
| 15 | rand_gen<int> gen; | ||
| 16 | while (s.size() < n) { | ||
| 17 | if (ans.empty() || ans.back() != s.bucket_count()) ans.push_back(s.bucket_count()); | ||
| 18 | s.insert(gen()); | ||
| 19 | } | ||
| 20 | return ans; | ||
| 21 | }; | ||
| 22 | |||
| 23 | 5 | vecz bc = get_bucket_counts(); | |
| 24 | |||
| 25 | if CEXP (std::is_same_v<strn, T>) { | ||
| 26 | // Edit these if need | ||
| 27 | 1 | const usz len = 15; | |
| 28 | 3 | const strn pref = "", alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"; | |
| 29 | 1 | rand_gen<usz> gen(0, alphabet.size() - 1); | |
| 30 | 1 | auto gen_str_with_divisible_hash = [&](usz mod) NE -> strn { | |
| 31 | strn s = pref; | ||
| 32 | while (true) { | ||
| 33 | s.resize(pref.size()); | ||
| 34 | while (s.size() < len) s += alphabet[gen()]; | ||
| 35 | if (!(std::hash<strn>{}(s) % mod)) return s; | ||
| 36 | } | ||
| 37 | return ""; | ||
| 38 | }; | ||
| 39 | 1 | vec<T> ans1; | |
| 40 | 1 | i64 op1 = 0; | |
| 41 |
2/2✓ Branch 2 taken 1000 times.
✓ Branch 3 taken 1 time.
|
1001 | flt_ (usz, i, 0, n, psb = 0, cnt = 0, lst = bc[0]) { |
| 42 | 1000 | const T nw = gen_str_with_divisible_hash(lst); | |
| 43 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 2 taken 993 times.
|
1000 | if (ans1.push_back(nw), op1 += cnt; i >= lst) lst = bc[++psb], cnt = 1; |
| 44 | 993 | else ++cnt; | |
| 45 | } | ||
| 46 | 1 | vec<T> ans2; | |
| 47 | 1 | i64 op2 = 0; | |
| 48 |
3/4✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 6 taken 1000 times.
✓ Branch 7 taken 1 time.
|
1001 | flt_ (usz, i, 0, n, cnt = 0, pr = bc.size() > 1 ? bc[bc.size() - 2] : 0, lst = bc.back()) { |
| 49 | 1000 | const T nw = gen_str_with_divisible_hash(lst); | |
| 50 |
2/2✓ Branch 1 taken 459 times.
✓ Branch 2 taken 541 times.
|
1000 | ans2.push_back(nw), op2 += i < pr ? 1 : cnt, ++cnt; |
| 51 | } | ||
| 52 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
1 | retif_((op1 > op2), ans1, ans2); |
| 53 | 1 | } else if CEXP (std::is_integral_v<T>) { | |
| 54 | 4 | vec<T> ans1; | |
| 55 | 4 | i64 op1 = 0; | |
| 56 |
8/8std::vector<int, std::allocator<int> > tifa_libs::unordered_stl_hacker<int>(unsigned long):
✓ Branch 1 taken 1000 times.
✓ Branch 2 taken 1 time.
std::vector<long, std::allocator<long> > tifa_libs::unordered_stl_hacker<long>(unsigned long):
✓ Branch 1 taken 1000 times.
✓ Branch 2 taken 1 time.
std::vector<unsigned int, std::allocator<unsigned int> > tifa_libs::unordered_stl_hacker<unsigned int>(unsigned long):
✓ Branch 1 taken 1000 times.
✓ Branch 2 taken 1 time.
std::vector<unsigned long, std::allocator<unsigned long> > tifa_libs::unordered_stl_hacker<unsigned long>(unsigned long):
✓ Branch 1 taken 1000 times.
✓ Branch 2 taken 1 time.
|
4004 | flt_ (usz, i, 0, n, psb = 0, cnt = 0, lst = bc[0]) |
| 57 |
8/8std::vector<int, std::allocator<int> > tifa_libs::unordered_stl_hacker<int>(unsigned long):
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 993 times.
std::vector<long, std::allocator<long> > tifa_libs::unordered_stl_hacker<long>(unsigned long):
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 993 times.
std::vector<unsigned int, std::allocator<unsigned int> > tifa_libs::unordered_stl_hacker<unsigned int>(unsigned long):
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 993 times.
std::vector<unsigned long, std::allocator<unsigned long> > tifa_libs::unordered_stl_hacker<unsigned long>(unsigned long):
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 993 times.
|
4000 | if (ans1.push_back(T(lst * cnt)), op1 += cnt; i >= lst) lst = bc[++psb], cnt = 1; |
| 58 | 3972 | else ++cnt; | |
| 59 | 4 | vec<T> ans2; | |
| 60 | 4 | i64 op2 = 0; | |
| 61 |
12/16std::vector<int, std::allocator<int> > tifa_libs::unordered_stl_hacker<int>(unsigned long):
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 6 taken 1000 times.
✓ Branch 7 taken 1 time.
std::vector<long, std::allocator<long> > tifa_libs::unordered_stl_hacker<long>(unsigned long):
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 6 taken 1000 times.
✓ Branch 7 taken 1 time.
std::vector<unsigned int, std::allocator<unsigned int> > tifa_libs::unordered_stl_hacker<unsigned int>(unsigned long):
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 6 taken 1000 times.
✓ Branch 7 taken 1 time.
std::vector<unsigned long, std::allocator<unsigned long> > tifa_libs::unordered_stl_hacker<unsigned long>(unsigned long):
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 6 taken 1000 times.
✓ Branch 7 taken 1 time.
|
4004 | flt_ (usz, i, 0, n, cnt = 0, pr = bc.size() > 1 ? bc[bc.size() - 2] : 0, lst = bc.back()) { |
| 62 | 4000 | ans2.push_back(T(lst * cnt)); | |
| 63 |
8/8std::vector<int, std::allocator<int> > tifa_libs::unordered_stl_hacker<int>(unsigned long):
✓ Branch 0 taken 459 times.
✓ Branch 1 taken 541 times.
std::vector<long, std::allocator<long> > tifa_libs::unordered_stl_hacker<long>(unsigned long):
✓ Branch 0 taken 459 times.
✓ Branch 1 taken 541 times.
std::vector<unsigned int, std::allocator<unsigned int> > tifa_libs::unordered_stl_hacker<unsigned int>(unsigned long):
✓ Branch 0 taken 459 times.
✓ Branch 1 taken 541 times.
std::vector<unsigned long, std::allocator<unsigned long> > tifa_libs::unordered_stl_hacker<unsigned long>(unsigned long):
✓ Branch 0 taken 459 times.
✓ Branch 1 taken 541 times.
|
4000 | op2 += i < pr ? 1 : cnt; |
| 64 | 4000 | ++cnt; | |
| 65 | } | ||
| 66 |
4/8std::vector<int, std::allocator<int> > tifa_libs::unordered_stl_hacker<int>(unsigned long):
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
std::vector<long, std::allocator<long> > tifa_libs::unordered_stl_hacker<long>(unsigned long):
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
std::vector<unsigned int, std::allocator<unsigned int> > tifa_libs::unordered_stl_hacker<unsigned int>(unsigned long):
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
std::vector<unsigned long, std::allocator<unsigned long> > tifa_libs::unordered_stl_hacker<unsigned long>(unsigned long):
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
4 | retif_((op1 > op2), ans1, ans2); |
| 67 | 4 | } | |
| 68 | 5 | } | |
| 69 | |||
| 70 | } // namespace tifa_libs | ||
| 71 |