Initial import of v2.0.0beta
[protobuf.git] / src / google / protobuf / stubs / map-util.h
blobee8073fecf59081a12494e8ac72efdd7dcc466b5
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.
3 // http://code.google.com/p/protobuf/
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 // from google3/util/gtl/map-util.h
18 // Author: Anton Carver
20 #ifndef GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__
21 #define GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__
23 #include <google/protobuf/stubs/common.h>
25 namespace google {
26 namespace protobuf {
28 // Perform a lookup in a map or hash_map.
29 // If the key is present a const pointer to the associated value is returned,
30 // otherwise a NULL pointer is returned.
31 template <class Collection>
32 const typename Collection::value_type::second_type*
33 FindOrNull(const Collection& collection,
34 const typename Collection::value_type::first_type& key) {
35 typename Collection::const_iterator it = collection.find(key);
36 if (it == collection.end()) {
37 return 0;
39 return &it->second;
42 // Perform a lookup in a map or hash_map whose values are pointers.
43 // If the key is present a const pointer to the associated value is returned,
44 // otherwise a NULL pointer is returned.
45 // This function does not distinguish between a missing key and a key mapped
46 // to a NULL value.
47 template <class Collection>
48 const typename Collection::value_type::second_type
49 FindPtrOrNull(const Collection& collection,
50 const typename Collection::value_type::first_type& key) {
51 typename Collection::const_iterator it = collection.find(key);
52 if (it == collection.end()) {
53 return 0;
55 return it->second;
58 // Change the value associated with a particular key in a map or hash_map.
59 // If the key is not present in the map the key and value are inserted,
60 // otherwise the value is updated to be a copy of the value provided.
61 // True indicates that an insert took place, false indicates an update.
62 template <class Collection, class Key, class Value>
63 bool InsertOrUpdate(Collection * const collection,
64 const Key& key, const Value& value) {
65 pair<typename Collection::iterator, bool> ret =
66 collection->insert(typename Collection::value_type(key, value));
67 if (!ret.second) {
68 // update
69 ret.first->second = value;
70 return false;
72 return true;
75 // Insert a new key and value into a map or hash_map.
76 // If the key is not present in the map the key and value are
77 // inserted, otherwise nothing happens. True indicates that an insert
78 // took place, false indicates the key was already present.
79 template <class Collection, class Key, class Value>
80 bool InsertIfNotPresent(Collection * const collection,
81 const Key& key, const Value& value) {
82 pair<typename Collection::iterator, bool> ret =
83 collection->insert(typename Collection::value_type(key, value));
84 return ret.second;
87 } // namespace protobuf
88 } // namespace google
90 #endif // GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__