Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
container-util.h
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
16 #ifndef IMPALA_UTIL_CONTAINER_UTIL_H
17 #define IMPALA_UTIL_CONTAINER_UTIL_H
18 
19 #include <map>
20 #include <boost/unordered_map.hpp>
21 
22 #include "util/hash-util.h"
23 #include "gen-cpp/Types_types.h"
24 
25 namespace impala {
26 
29 inline std::size_t hash_value(const TNetworkAddress& host_port) {
30  uint32_t hash =
31  HashUtil::Hash(host_port.hostname.c_str(), host_port.hostname.length(), 0);
32  return HashUtil::Hash(&host_port.port, sizeof(host_port.port), hash);
33 }
34 
35 struct HashTNetworkAddressPtr : public std::unary_function<TNetworkAddress*, size_t> {
36  size_t operator()(const TNetworkAddress* const& p) const { return hash_value(*p); }
37 };
38 
39 struct TNetworkAddressPtrEquals : public std::unary_function<TNetworkAddress*, bool> {
40  bool operator()(const TNetworkAddress* const& p1,
41  const TNetworkAddress* const& p2) const {
42  return p1->hostname == p2->hostname && p1->port == p2->port;
43  }
44 };
45 
46 
49 
50 template <typename K, typename V>
51 V* FindOrInsert(std::map<K,V>* m, const K& key, const V& default_val) {
52  typename std::map<K,V>::iterator it = m->find(key);
53  if (it == m->end()) {
54  it = m->insert(std::make_pair(key, default_val)).first;
55  }
56  return &it->second;
57 }
58 
59 template <typename K, typename V>
60 V* FindOrInsert(boost::unordered_map<K,V>* m, const K& key, const V& default_val) {
61  typename boost::unordered_map<K,V>::iterator it = m->find(key);
62  if (it == m->end()) {
63  it = m->insert(std::make_pair(key, default_val)).first;
64  }
65  return &it->second;
66 }
67 
68 
71 
72 template <typename K, typename V>
73 const V& FindWithDefault(const std::map<K, V>& m, const K& key, const V& default_val) {
74  typename std::map<K,V>::const_iterator it = m.find(key);
75  if (it == m.end()) return default_val;
76  return it->second;
77 }
78 
79 template <typename K, typename V>
80 const V& FindWithDefault(const boost::unordered_map<K, V>& m, const K& key,
81  const V& default_val) {
82  typename boost::unordered_map<K,V>::const_iterator it = m.find(key);
83  if (it == m.end()) return default_val;
84  return it->second;
85 }
86 
89 template<typename K, typename V>
90 void MergeMapValues(const std::map<K, V>& src, std::map<K, V>* dst) {
91  for (typename std::map<K, V>::const_iterator src_it = src.begin();
92  src_it != src.end(); ++src_it) {
93  typename std::map<K, V>::iterator dst_it = dst->find(src_it->first);
94  if (dst_it == dst->end()) {
95  (*dst)[src_it->first] = src_it->second;
96  } else {
97  dst_it->second += src_it->second;
98  }
99  }
100 }
101 
102 }
103 
104 #endif
const StringSearch UrlParser::hash_search & hash
Definition: url-parser.cc:41
std::size_t hash_value(const Decimal4Value &v)
This function must be called 'hash_value' to be picked up by boost.
V * FindOrInsert(std::map< K, V > *m, const K &key, const V &default_val)
static uint32_t Hash(const void *data, int32_t bytes, uint32_t seed)
Definition: hash-util.h:135
const V & FindWithDefault(const std::map< K, V > &m, const K &key, const V &default_val)
size_t operator()(const TNetworkAddress *const &p) const
bool operator()(const TNetworkAddress *const &p1, const TNetworkAddress *const &p2) const
void MergeMapValues(const std::map< K, V > &src, std::map< K, V > *dst)