Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions data_structure/fast_hash_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ template <typename K, typename V, std::size_t N> struct HashMap {
return V{};
}

bool contains(K key) const noexcept {
std::size_t pos = hash(key);
for (std::size_t step = 0; step < N; ++step) {
if (versions[pos] != version) return false;
if (keys[pos] == key) return true;
pos = (pos + 1) & (N - 1);
}
return false;
}

std::size_t size() const noexcept { return count; }
bool empty() const noexcept { return count == 0; }

Expand Down
3 changes: 2 additions & 1 deletion data_structure/fast_hash_map.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ HashMap<long long, long long, 1 << 20> mp;

mp.set(10, 20); // key 10 に value 20 を設定
mp.get(10); // 20(存在しないキーに対しては V{})
mp.contains(10); // true(キーの存在判定)
mp.size(); // 1
mp.empty(); // false
mp.clear(); // 全要素を O(1) で削除
```

キーは 64 bit 以下の整数型でなければならない.計算量は `set()` / `get()` が平均 $O(1)$,`clear()` / `reset()` / `size()` / `empty()` が $O(1)$.格納する相異なるキー数が `N` を超えないようにする必要がある.
キーは 64 bit 以下の整数型でなければならない.計算量は `set()` / `get()` / `contains()` が平均 $O(1)$,`clear()` / `reset()` / `size()` / `empty()` が $O(1)$.格納する相異なるキー数が `N` を超えないようにする必要がある.

## 問題例

Expand Down
Loading