Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
- //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx")
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <algorithm>
- #include <set>
- #include <map>
- #include <queue>
- #include <random>
- #include <chrono>
- #include <cassert>
- #include <sstream>
- #include <fstream>
- #include <iomanip>
- #include <tuple>
- #include <array>
- #include <string_view>
- #include <list>
- #include <future>
- #include <mutex>
- #define fi first
- #define se second
- #define pb push_back
- #define ll long long
- #define ld long double
- #define hm unordered_map
- #define pii pair<int, int>
- #define sz(a) (int)a.size()
- #define all(a) a.begin(), a.end()
- #define cinv(v) for (auto& x: v) cin >> x
- #define fr(i, n) for (int i = 0; i < (n); ++i)
- #define fl(i, l, n) for (int i = (l); i < (n); ++i)
- // #define int ll
- template<typename T1, typename T2>
- inline bool chkmin(T1 &x, const T2 &y) {
- if (x > y) {
- x = y;
- return 1;
- }
- return 0;
- }
- template<typename T1, typename T2>
- inline bool chkmax(T1 &x, const T2 &y) {
- if (x < y) {
- x = y;
- return 1;
- }
- return 0;
- }
- using namespace std;
- #ifdef LOCAL
- #define dbg(x) cerr << #x << " : " << x << endl
- #else
- #define dbg(x)
- #endif
- //tg: @runningcherry
- template<typename Collection>
- string Join(const Collection &col) {
- bool first = true;
- stringstream ss;
- for (const auto &x: col) {
- if (!first) ss << ", ";
- first = false;
- ss << x;
- }
- return ss.str();
- }
- template<typename T1, typename T2>
- ostream &operator<<(ostream &out, const pair<T1, T2> &v) {
- return out << '{' << v.fi << ", " << v.se << '}';
- }
- template<typename T>
- ostream &operator<<(ostream &out, const vector<T> &v) {
- return out << '[' << Join(v) << ']';
- }
- template<typename T1, typename T2>
- ostream &operator<<(ostream &out, const map<T1, T2> &v) {
- return out << '{' << Join(v) << '}';
- }
- template<typename T>
- ostream &operator<<(ostream &out, const set<T> &v) {
- return out << '(' << Join(v) << ')';
- }
- template<typename T>
- ostream &operator<<(ostream &out, const multiset<T> &v) {
- return out << '(' << Join(v) << ')';
- }
- template<typename T1, typename T2>
- istream &operator>>(istream &in, pair<T1, T2> &a) {
- return in >> a.fi >> a.se;
- }
- const ll inf = (ll) 2e9;
- const int maxn = 1e5 + 20;
- #define ASSERT_EQUAL(a, b) dbg((bool)(a == b))
- #define ASSERT(a) dbg((bool)(a))
- template <typename K, typename V>
- class ConcurrentMap {
- public:
- static_assert(is_integral_v<K>, "ConcurrentMap supports only integer keys");
- struct Access {
- V& ref_to_value;
- };
- class SynchronizedMap {
- public:
- struct gay {
- map <K, Access>& ref;
- lock_guard<mutex> guard;
- };
- gay GetAccess() {
- return {a, lock_guard(m)};
- }
- private:
- mutex m;
- map <K, Access> a;
- };
- explicit ConcurrentMap(size_t bucket_count) : mod (bucket_count) {
- kek = vector<SynchronizedMap> (bucket_count);
- }
- Access operator[](const K& key) {
- int x = key % mod;
- auto ptt = kek[x].GetAccess ();
- return ptt.ref[key];
- }
- map<K, V> BuildOrdinaryMap() {
- map <K, V> res;
- fr (i, mod) {
- auto ptt = kek[i].GetAccess ().ref;
- for (auto& x: ptt) res[x.fi] = x.se.ref_to_value;
- }
- return res;
- }
- private:
- vector<SynchronizedMap> kek;
- int mod;
- };
- void RunConcurrentUpdates(
- ConcurrentMap<int, int>& cm, size_t thread_count, int key_count
- ) {
- auto kernel = [&cm, key_count](int seed) {
- vector<int> updates(key_count);
- iota(begin(updates), end(updates), -key_count / 2);
- shuffle(begin(updates), end(updates), default_random_engine(seed));
- for (int i = 0; i < 2; ++i) {
- for (auto key : updates) {
- dbg (key);
- cm[key].ref_to_value++;
- }
- }
- };
- vector<future<void>> futures;
- for (size_t i = 0; i < thread_count; ++i) {
- futures.push_back(async(kernel, i));
- }
- }
- void TestConcurrentUpdate() {
- const size_t thread_count = 3;
- const size_t key_count = 50000;
- ConcurrentMap<int, int> cm(thread_count);
- RunConcurrentUpdates(cm, thread_count, key_count);
- const auto result = cm.BuildOrdinaryMap();
- ASSERT_EQUAL(result.size(), key_count);
- for (auto& [k, v] : result) {
- ASSERT_EQUAL(v, 6);
- // AssertEqual(v, 6, "Key = " + to_string(k));
- }
- }
- int main () {
- ios_base::sync_with_stdio (0);
- cin.tie (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement