Back to home page

EIC code displayed by LXR

 
 

    


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

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 <utility>
0021 
0022 #include "arrow/result.h"
0023 
0024 namespace arrow {
0025 namespace internal {
0026 
0027 /// Helper providing single-lookup conditional insertion into std::map or
0028 /// std::unordered_map. If `key` exists in the container, an iterator to that pair
0029 /// will be returned. If `key` does not exist in the container, `gen(key)` will be
0030 /// invoked and its return value inserted.
0031 template <typename Map, typename Gen>
0032 auto GetOrInsertGenerated(Map* map, typename Map::key_type key, Gen&& gen)
0033     -> decltype(map->begin()->second = gen(map->begin()->first), map->begin()) {
0034   decltype(gen(map->begin()->first)) placeholder{};
0035 
0036   auto it_success = map->emplace(std::move(key), std::move(placeholder));
0037   if (it_success.second) {
0038     // insertion of placeholder succeeded, overwrite it with gen()
0039     const auto& inserted_key = it_success.first->first;
0040     auto* value = &it_success.first->second;
0041     *value = gen(inserted_key);
0042   }
0043   return it_success.first;
0044 }
0045 
0046 template <typename Map, typename Gen>
0047 auto GetOrInsertGenerated(Map* map, typename Map::key_type key, Gen&& gen)
0048     -> Result<decltype(map->begin()->second = gen(map->begin()->first).ValueOrDie(),
0049                        map->begin())> {
0050   decltype(gen(map->begin()->first).ValueOrDie()) placeholder{};
0051 
0052   auto it_success = map->emplace(std::move(key), std::move(placeholder));
0053   if (it_success.second) {
0054     // insertion of placeholder succeeded, overwrite it with gen()
0055     const auto& inserted_key = it_success.first->first;
0056     auto* value = &it_success.first->second;
0057     ARROW_ASSIGN_OR_RAISE(*value, gen(inserted_key));
0058   }
0059   return it_success.first;
0060 }
0061 
0062 }  // namespace internal
0063 }  // namespace arrow