LCOV - code coverage report
Current view: top level - utilities/include/mcrl2/utilities/detail - unordered_map_implementation.h (source / functions) Hit Total Coverage
Test: mcrl2_coverage.info.cleaned Lines: 15 15 100.0 %
Date: 2020-09-22 00:46:14 Functions: 3 4 75.0 %
Legend: Lines: hit not hit

          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>
      17             : #define MCRL2_UNORDERED_MAP_CLASS unordered_map<Key, T, Hash, Equals, Allocator, ThreadSafe>
      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        2625 : T& MCRL2_UNORDERED_MAP_CLASS::operator[](const key_type& key)
      44             : {
      45             :   // Insert a new object and return a reference to it;
      46        2625 :   auto pair = m_set.emplace(std::make_pair(key, mapped_type()));
      47             : 
      48             :   // The second element of the pair is mutable.
      49        2625 :   return const_cast<mapped_type&>((*pair.first).second);
      50             : }
      51             : 
      52             : MCRL2_UNORDERED_MAP_TEMPLATES
      53        4434 : const T& MCRL2_UNORDERED_MAP_CLASS::at(const key_type& key) const
      54             : {
      55        4434 :   auto it = m_set.find(key);
      56        4434 :   assert(it != m_set.end());
      57        4434 :   return (*it).second;
      58             : }
      59             : 
      60             : #undef MCRL2_UNORDERED_MAP_TEMPLATES
      61             : #undef MCRL2_UNORDERED_MAP_CLASS
      62             : 
      63             : } // namespace mcrl2::utilities
      64             : 
      65             : #endif // MCRL2_UTILITIES_UNORDERED_MAP_IMPLEMENTATION_H

Generated by: LCOV version 1.13