LCOV - code coverage report
Current view: top level - atermpp/include/mcrl2/atermpp/detail - function_symbol_hash.h (source / functions) Hit Total Coverage
Test: mcrl2_coverage.info.cleaned Lines: 12 12 100.0 %
Date: 2024-04-26 03:18:02 Functions: 5 5 100.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_ATERMPP_DETAIL_FUNCTION_SYMBOL_HASH_H_
      11             : #define MCRL2_ATERMPP_DETAIL_FUNCTION_SYMBOL_HASH_H_
      12             : 
      13             : #include "mcrl2/atermpp/function_symbol.h"
      14             : 
      15             : #include <cstdint>
      16             : 
      17             : namespace std
      18             : {
      19             :  
      20             : /// \brief Specialisation of the standard hash function for function_symbol.
      21             : template<>
      22             : struct hash<atermpp::function_symbol>
      23             : {
      24   160362437 :   std::size_t operator()(const atermpp::function_symbol& f) const
      25             :   {
      26             :     // Function symbols take 48 bytes in memory, so when they are packed there
      27             :     // are at least 32 bits that do not distinguish two function symbols. As
      28             :     // such these can be removed.
      29   160362437 :     return reinterpret_cast<std::uint64_t>(f.m_function_symbol.get()) >> 5;
      30             :   }
      31             : };
      32             : 
      33             : /// \brief Specialisation of the standard hash function for _function_symbol.
      34             : template<>
      35             : struct hash<atermpp::detail::_function_symbol>
      36             : {
      37      126461 :   std::size_t operator()(const atermpp::detail::_function_symbol& f) const
      38             :   {
      39             :     std::hash<std::string> string_hasher;
      40      126461 :     std::size_t h = string_hasher(f.name());
      41      126461 :     return (h ^ f.arity());
      42             :   }
      43             : };
      44             : 
      45             : } // namespace std
      46             : 
      47             : namespace atermpp
      48             : {
      49             : namespace detail
      50             : {
      51             : 
      52             : /// \brief Computes the hash for given function symbol objects and for the
      53             : ///        function_symbol_key.
      54             : struct function_symbol_hasher
      55             : {
      56             :   using is_transparent = void;
      57             : 
      58             :   inline std::size_t operator() (const _function_symbol& symbol) const noexcept;
      59             :   inline std::size_t operator() (const std::string& name, std::size_t arity) const noexcept;
      60             : };
      61             : 
      62             : /// \brief True iff the given function symbols are equal to eachother or to
      63             : ///        the given key.
      64             : struct function_symbol_equals
      65             : {
      66             :   using is_transparent = void;
      67             : 
      68             :   inline bool operator() (const _function_symbol& first, const _function_symbol& second) const noexcept;
      69             :   inline bool operator()(const _function_symbol& symbol, const std::string& name, std::size_t arity) const noexcept;
      70             : };
      71             : 
      72      126461 : std::size_t function_symbol_hasher::operator() (const _function_symbol& symbol) const noexcept
      73             : {
      74             :   const std::hash<_function_symbol> function_symbol_hasher;
      75      126461 :   return function_symbol_hasher(symbol);
      76             : }
      77             : 
      78     1179406 : std::size_t function_symbol_hasher::operator() (const std::string& name, std::size_t arity) const noexcept
      79             : {
      80             :   std::hash<std::string> string_hasher;
      81     1179406 :   std::size_t h = string_hasher(name);
      82     1179406 :   return (h ^ arity);
      83             : }
      84             : 
      85             : bool function_symbol_equals::operator() (const _function_symbol& first, const _function_symbol& second) const noexcept
      86             : {
      87             :   return first == second;
      88             : }
      89             : 
      90    57022773 : bool function_symbol_equals::operator()(const _function_symbol& symbol, const std::string& name, std::size_t arity) const noexcept
      91             : {
      92    57022773 :   return (symbol.name() == name) && (symbol.arity() == arity);
      93             : }
      94             : 
      95             : } // namespace detail
      96             : } // namespace atermpp
      97             : 
      98             : #endif // MCRL2_ATERMPP_DETAIL_FUNCTION_SYMBOL_HASH_H_

Generated by: LCOV version 1.14