Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 08:27:07

0001 // Licensed to the Apache Software Foundation (ASF) under one
0002 // or more contributor license agreements.  See the NOTICE file
0003 // distributed with this work for additional information
0004 // regarding copyright ownership.  The ASF licenses this file
0005 // to you under the Apache License, Version 2.0 (the
0006 // "License"); you may not use this file except in compliance
0007 // with the License.  You may obtain a copy of the License at
0008 //
0009 //   http://www.apache.org/licenses/LICENSE-2.0
0010 //
0011 // Unless required by applicable law or agreed to in writing,
0012 // software distributed under the License is distributed on an
0013 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0014 // KIND, either express or implied.  See the License for the
0015 // specific language governing permissions and limitations
0016 // under the License.
0017 
0018 #pragma once
0019 
0020 #include <unordered_map>
0021 #include <utility>
0022 
0023 #include "arrow/util/mutex.h"
0024 
0025 namespace arrow {
0026 namespace util {
0027 
0028 template <typename K, typename V>
0029 class ConcurrentMap {
0030  public:
0031   void Insert(const K& key, const V& value) {
0032     auto lock = mutex_.Lock();
0033     map_.insert({key, value});
0034   }
0035 
0036   template <typename ValueFunc>
0037   V GetOrInsert(const K& key, ValueFunc&& compute_value_func) {
0038     auto lock = mutex_.Lock();
0039     auto it = map_.find(key);
0040     if (it == map_.end()) {
0041       auto pair = map_.emplace(key, compute_value_func());
0042       it = pair.first;
0043     }
0044     return it->second;
0045   }
0046 
0047   void Erase(const K& key) {
0048     auto lock = mutex_.Lock();
0049     map_.erase(key);
0050   }
0051 
0052   void Clear() {
0053     auto lock = mutex_.Lock();
0054     map_.clear();
0055   }
0056 
0057   size_t size() const {
0058     auto lock = mutex_.Lock();
0059     return map_.size();
0060   }
0061 
0062  private:
0063   std::unordered_map<K, V> map_;
0064   mutable arrow::util::Mutex mutex_;
0065 };
0066 
0067 }  // namespace util
0068 }  // namespace arrow