/root/doris/be/src/util/container_util.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // This file is copied from |
18 | | // https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/container-util.hpp |
19 | | // and modified by Doris |
20 | | |
21 | | #pragma once |
22 | | |
23 | | #include <gen_cpp/Types_types.h> |
24 | | |
25 | | #include <map> |
26 | | #include <unordered_map> |
27 | | |
28 | | #include "util/hash_util.hpp" |
29 | | |
30 | | namespace doris { |
31 | | |
32 | | // Hash function for TNetworkAddress. This function must be called hash_value to be picked |
33 | | // up properly by boost. |
34 | 0 | inline std::size_t hash_value(const TNetworkAddress& host_port) { |
35 | 0 | uint32_t hash = HashUtil::hash(host_port.hostname.c_str(), host_port.hostname.length(), 0); |
36 | 0 | return HashUtil::hash(&host_port.port, sizeof(host_port.port), hash); |
37 | 0 | } |
38 | | |
39 | | struct HashTNetworkAddressPtr { |
40 | 0 | size_t operator()(const TNetworkAddress* const& p) const { return hash_value(*p); } |
41 | | }; |
42 | | |
43 | | struct TNetworkAddressPtrEquals { |
44 | 0 | bool operator()(const TNetworkAddress* const& p1, const TNetworkAddress* const& p2) const { |
45 | 0 | return p1->hostname == p2->hostname && p1->port == p2->port; |
46 | 0 | } |
47 | | }; |
48 | | |
49 | | // find_or_insert(): if the key is present, return the value; if the key is not present, |
50 | | // create a new entry (key, default_val) and return default_val. |
51 | | |
52 | | template <typename K, typename V> |
53 | 1.05k | V* find_or_insert(std::map<K, V>* m, const K& key, const V& default_val) { |
54 | 1.05k | typename std::map<K, V>::iterator it = m->find(key); |
55 | | |
56 | 1.05k | if (it == m->end()) { |
57 | 212 | it = m->insert(make_pair(key, default_val)).first; |
58 | 212 | } |
59 | | |
60 | 1.05k | return &it->second; |
61 | 1.05k | } |
62 | | |
63 | | template <typename K, typename V> |
64 | | V* find_or_insert(std::unordered_map<K, V>* m, const K& key, const V& default_val) { |
65 | | typename std::unordered_map<K, V>::iterator it = m->find(key); |
66 | | |
67 | | if (it == m->end()) { |
68 | | it = m->insert(make_pair(key, default_val)).first; |
69 | | } |
70 | | |
71 | | return &it->second; |
72 | | } |
73 | | |
74 | | // find_with_default: if the key is present, return the corresponding value; if the key |
75 | | // is not present, return the supplied default value |
76 | | |
77 | | template <typename K, typename V> |
78 | | std::reference_wrapper<const V> find_with_default(const std::map<K, V>& m, const K& key, |
79 | 0 | const V& default_val) { |
80 | 0 | typename std::map<K, V>::const_iterator it = m.find(key); |
81 | |
|
82 | 0 | if (it == m.end()) { |
83 | 0 | return default_val; |
84 | 0 | } |
85 | | |
86 | 0 | return it->second; |
87 | 0 | } Unexecuted instantiation: _ZN5doris17find_with_defaultIiiEESt17reference_wrapperIKT0_ERKSt3mapIT_S2_St4lessIS6_ESaISt4pairIKS6_S2_EEERSA_RS3_ Unexecuted instantiation: _ZN5doris17find_with_defaultIiSt6vectorINS_16TScanRangeParamsESaIS2_EEEESt17reference_wrapperIKT0_ERKSt3mapIT_S6_St4lessISA_ESaISt4pairIKSA_S6_EEERSE_RS7_ Unexecuted instantiation: _ZN5doris17find_with_defaultIibEESt17reference_wrapperIKT0_ERKSt3mapIT_S2_St4lessIS6_ESaISt4pairIKS6_S2_EEERSA_RS3_ |
88 | | |
89 | | template <typename K, typename V> |
90 | | std::reference_wrapper<const V> find_with_default(const std::unordered_map<K, V>& m, const K& key, |
91 | | const V& default_val) { |
92 | | typename std::unordered_map<K, V>::const_iterator it = m.find(key); |
93 | | |
94 | | if (it == m.end()) { |
95 | | return default_val; |
96 | | } |
97 | | |
98 | | return it->second; |
99 | | } |
100 | | |
101 | | } // namespace doris |