Skip to content

Commit 10b6283

Browse files
committed
hashmap
1 parent cdc74e7 commit 10b6283

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
exclude: ["pdf/compose.py", "pdf/gen.py", "src/alfred/config/optimize-header.h"]
1+
exclude: ["src/alfred/config/optimize-header.h"]
22
title: Alfred's CP Library
33
theme: jekyll-theme-hacker
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
This cp library is powered by [competitive-verifier/competitive-verifier](https://github.com/competitive-verifier/competitive-verifier). Thanks for making this project possible.
22

3-
The library is mainly for OI competitions, as well as some online cp contests on codeforces or atcoder. It is devoted to the heighest efficiency and wide compatibility. Any issue can be reported on the issue page, but no fixes or changes is guaranteed to be made.
3+
The library is mainly for OI competitions, as well as some online cp contests on codeforces or atcoder. It is devoted to the highest efficiency and the widest compatibility. Any issue can be reported on the github issue page, but no bugfixes are guaranteed to be made.
44

5-
The jiangly and watashi code library are from [this link for jiangly](https://github.com/hh2048/XCPC/) and [this link for watashi](https://github.com/nju-icpc/code-library-legacy/). The part of my code is open source under the MIT License.
5+
Jiangly's library and watashi's library are from [this link for jiangly](https://github.com/hh2048/XCPC/) and [this link for watashi](https://github.com/nju-icpc/code-library-legacy/). My library is open source under the MIT License.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef AFDS_HASHMAP
2+
#define AFDS_HASHMAP
3+
4+
#include <utility>
5+
#include <vector>
6+
7+
template <class K, class V, size_t SIZE = 3301>
8+
struct HashMap {
9+
std::vector<std::pair<K, V>> vec[SIZE];
10+
HashMap(void) {}
11+
inline int hash_key(K key) { return key % SIZE; }
12+
inline auto find_it(int h, K key) {
13+
return std::find_if(vec[h].begin(), vec[h].end(), [&](auto kv) {
14+
return kv.first == key;
15+
});
16+
}
17+
inline void inc(K key) {
18+
int h = hash_key(key);
19+
auto it = find_it(h, key);
20+
if (it == vec[h].end()) {
21+
vec[h].emplace_back(key, 1);
22+
return;
23+
}
24+
it->second++;
25+
}
26+
inline void dec(K key) {
27+
int h = hash_key(key);
28+
auto it = find_it(h, key);
29+
if (it == vec[h].end()) {
30+
return;
31+
}
32+
it->second--;
33+
if (it->second == 0) {
34+
vec[h].erase(it);
35+
}
36+
}
37+
inline V get(K key) {
38+
int h = hash_key(key);
39+
auto it = find_it(h, key);
40+
if (it == vec[h].end()) {
41+
return 0;
42+
}
43+
return it->second;
44+
}
45+
}
46+
47+
#endif

0 commit comments

Comments
 (0)