Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 3 : //* 4 : //* All rights reserved, see COPYRIGHT for full restrictions 5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT 6 : //* 7 : //* Licensed under LGPL 2.1, please see LICENSE for details 8 : //* https://www.gnu.org/licenses/lgpl-2.1.html 9 : 10 : #pragma once 11 : 12 : /// HashMap is an abstraction for dictionary data type, we make it thread-safe by locking inserts 13 : #include "libmesh/threads.h" 14 : 15 : #include <unordered_map> 16 : 17 : template <typename Key, typename T> 18 : class HashMap : public std::unordered_map<Key, T> 19 : { 20 : public: 21 24510036 : inline T & operator[](const Key & k) 22 : { 23 24510036 : libMesh::Threads::spin_mutex::scoped_lock lock(spin_mutex); 24 49020072 : return std::unordered_map<Key, T>::operator[](k); 25 24510036 : } 26 : 27 80311 : inline std::size_t erase(const Key & k) 28 : { 29 80311 : libMesh::Threads::spin_mutex::scoped_lock lock(spin_mutex); 30 160622 : return std::unordered_map<Key, T>::erase(k); 31 80311 : } 32 : 33 : using typename std::unordered_map<Key, T>::const_iterator; 34 : using typename std::unordered_map<Key, T>::iterator; 35 : 36 128836885 : inline iterator find(const Key & k) 37 : { 38 128836885 : libMesh::Threads::spin_mutex::scoped_lock lock(spin_mutex); 39 257673770 : return std::unordered_map<Key, T>::find(k); 40 128836885 : } 41 : 42 49271649 : inline const_iterator find(const Key & k) const 43 : { 44 49271649 : libMesh::Threads::spin_mutex::scoped_lock lock(spin_mutex); 45 98543298 : return std::unordered_map<Key, T>::find(k); 46 49271649 : } 47 : 48 : inline bool contains(const Key & key) const { return this->find(key) != this->end(); } 49 : 50 : private: 51 : mutable libMesh::Threads::spin_mutex spin_mutex; 52 : };