Line data Source code
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 : 14 : #include "mcrl2/utilities/unordered_map.h" 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 : 19 : namespace mcrl2::utilities 20 : { 21 : 22 : MCRL2_UNORDERED_MAP_TEMPLATES 23 : template<typename ...Args> 24 2 : auto MCRL2_UNORDERED_MAP_CLASS::try_emplace(const key_type& key, Args&&... args) -> std::pair<iterator, bool> 25 : { 26 2 : m_set.rehash_if_needed(); 27 : 28 2 : 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 2 : iterator it = m_set.find_impl(bucket, key); 31 2 : if (it != end()) 32 : { 33 1 : return std::make_pair(it, false); 34 : } 35 : else 36 : { 37 1 : auto [x, y] = m_set.emplace_impl(bucket, key, std::forward<Args>(args)...); 38 1 : return std::make_pair(iterator(x), y); 39 : } 40 : } 41 : 42 : MCRL2_UNORDERED_MAP_TEMPLATES 43 2224 : T& 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 2224 : auto pair = m_set.emplace(std::make_pair(key, mapped_type())); 48 : 49 : // The second element of the pair is mutable. 50 2224 : return const_cast<mapped_type&>((*pair.first).second); 51 : } 52 : 53 : MCRL2_UNORDERED_MAP_TEMPLATES 54 3739 : const T& MCRL2_UNORDERED_MAP_CLASS::at(const key_type& key) const 55 : { 56 3739 : auto it = m_set.find(key); 57 3739 : assert(it != m_set.end()); 58 3739 : 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