1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // http://code.google.com/p/protobuf/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // from google3/util/gtl/map-util.h
32 // Author: Anton Carver
33 
34 #ifndef GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__
35 #define GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__
36 
37 #include <google/protobuf/stubs/common.h>
38 
39 namespace google {
40 namespace protobuf {
41 
42 // Perform a lookup in a map or hash_map.
43 // If the key is present in the map then the value associated with that
44 // key is returned, otherwise the value passed as a default is returned.
45 template <class Collection>
46 const typename Collection::value_type::second_type&
FindWithDefault(const Collection & collection,const typename Collection::value_type::first_type & key,const typename Collection::value_type::second_type & value)47 FindWithDefault(const Collection& collection,
48                 const typename Collection::value_type::first_type& key,
49                 const typename Collection::value_type::second_type& value) {
50   typename Collection::const_iterator it = collection.find(key);
51   if (it == collection.end()) {
52     return value;
53   }
54   return it->second;
55 }
56 
57 // Perform a lookup in a map or hash_map.
58 // If the key is present a const pointer to the associated value is returned,
59 // otherwise a NULL pointer is returned.
60 template <class Collection>
61 const typename Collection::value_type::second_type*
FindOrNull(const Collection & collection,const typename Collection::value_type::first_type & key)62 FindOrNull(const Collection& collection,
63            const typename Collection::value_type::first_type& key) {
64   typename Collection::const_iterator it = collection.find(key);
65   if (it == collection.end()) {
66     return 0;
67   }
68   return &it->second;
69 }
70 
71 // Perform a lookup in a map or hash_map whose values are pointers.
72 // If the key is present a const pointer to the associated value is returned,
73 // otherwise a NULL pointer is returned.
74 // This function does not distinguish between a missing key and a key mapped
75 // to a NULL value.
76 template <class Collection>
77 const typename Collection::value_type::second_type
FindPtrOrNull(const Collection & collection,const typename Collection::value_type::first_type & key)78 FindPtrOrNull(const Collection& collection,
79               const typename Collection::value_type::first_type& key) {
80   typename Collection::const_iterator it = collection.find(key);
81   if (it == collection.end()) {
82     return 0;
83   }
84   return it->second;
85 }
86 
87 // Change the value associated with a particular key in a map or hash_map.
88 // If the key is not present in the map the key and value are inserted,
89 // otherwise the value is updated to be a copy of the value provided.
90 // True indicates that an insert took place, false indicates an update.
91 template <class Collection, class Key, class Value>
InsertOrUpdate(Collection * const collection,const Key & key,const Value & value)92 bool InsertOrUpdate(Collection * const collection,
93                    const Key& key, const Value& value) {
94   pair<typename Collection::iterator, bool> ret =
95     collection->insert(typename Collection::value_type(key, value));
96   if (!ret.second) {
97     // update
98     ret.first->second = value;
99     return false;
100   }
101   return true;
102 }
103 
104 // Insert a new key and value into a map or hash_map.
105 // If the key is not present in the map the key and value are
106 // inserted, otherwise nothing happens. True indicates that an insert
107 // took place, false indicates the key was already present.
108 template <class Collection, class Key, class Value>
InsertIfNotPresent(Collection * const collection,const Key & key,const Value & value)109 bool InsertIfNotPresent(Collection * const collection,
110                         const Key& key, const Value& value) {
111   pair<typename Collection::iterator, bool> ret =
112     collection->insert(typename Collection::value_type(key, value));
113   return ret.second;
114 }
115 
116 }  // namespace protobuf
117 }  // namespace google
118 
119 #endif  // GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__
120