mCRL2
Loading...
Searching...
No Matches
unordered_map_implementation.h
Go to the documentation of this file.
1// Author(s): Maurice Laveaux
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8
9
10#ifndef MCRL2_UTILITIES_UNORDERED_MAP_IMPLEMENTATION_H
11#define MCRL2_UTILITIES_UNORDERED_MAP_IMPLEMENTATION_H
12#pragma once
13
15
16#define MCRL2_UNORDERED_MAP_TEMPLATES template<typename Key, typename T, typename Hash, typename Equals, typename Allocator, bool ThreadSafe, bool Resize>
17#define MCRL2_UNORDERED_MAP_CLASS unordered_map<Key, T, Hash, Equals, Allocator, ThreadSafe, Resize>
18
19namespace mcrl2::utilities
20{
21
23template<typename ...Args>
24auto MCRL2_UNORDERED_MAP_CLASS::try_emplace(const key_type& key, Args&&... args) -> std::pair<iterator, bool>
25{
26 m_set.rehash_if_needed();
27
28 std::size_t bucket = m_set.find_bucket_index(key);
29 // If the key can be found, then return it and otherwise add it in the same bucket.
30 iterator it = m_set.find_impl(bucket, key);
31 if (it != end())
32 {
33 return std::make_pair(it, false);
34 }
35 else
36 {
37 auto [x, y] = m_set.emplace_impl(bucket, key, std::forward<Args>(args)...);
38 return std::make_pair(iterator(x), y);
39 }
40}
41
43T& MCRL2_UNORDERED_MAP_CLASS::operator[](const key_type& key)
44{
45 // Insert a new object and return a reference to it;
46 // auto pair = m_set.emplace(std::make_pair<const Key, T>(key, mapped_type()));
47 auto pair = m_set.emplace(std::make_pair(key, mapped_type()));
48
49 // The second element of the pair is mutable.
50 return const_cast<mapped_type&>((*pair.first).second);
51}
52
54const T& MCRL2_UNORDERED_MAP_CLASS::at(const key_type& key) const
55{
56 auto it = m_set.find(key);
57 assert(it != m_set.end());
58 return (*it).second;
59}
60
61#undef MCRL2_UNORDERED_MAP_TEMPLATES
62#undef MCRL2_UNORDERED_MAP_CLASS
63
64} // namespace mcrl2::utilities
65
66#endif // MCRL2_UTILITIES_UNORDERED_MAP_IMPLEMENTATION_H
#define MCRL2_UNORDERED_MAP_TEMPLATES