OsmAnd
jni/protobuf/google/protobuf/stubs/map-util.h
Go to the documentation of this file.
00001 // Protocol Buffers - Google's data interchange format
00002 // Copyright 2008 Google Inc.  All rights reserved.
00003 // http://code.google.com/p/protobuf/
00004 //
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are
00007 // met:
00008 //
00009 //     * Redistributions of source code must retain the above copyright
00010 // notice, this list of conditions and the following disclaimer.
00011 //     * Redistributions in binary form must reproduce the above
00012 // copyright notice, this list of conditions and the following disclaimer
00013 // in the documentation and/or other materials provided with the
00014 // distribution.
00015 //     * Neither the name of Google Inc. nor the names of its
00016 // contributors may be used to endorse or promote products derived from
00017 // this software without specific prior written permission.
00018 //
00019 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00022 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00023 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00025 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00026 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00027 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00029 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 
00031 // from google3/util/gtl/map-util.h
00032 // Author: Anton Carver
00033 
00034 #ifndef GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__
00035 #define GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__
00036 
00037 #include <google/protobuf/stubs/common.h>
00038 
00039 namespace google {
00040 namespace protobuf {
00041 
00042 // Perform a lookup in a map or hash_map.
00043 // If the key is present in the map then the value associated with that
00044 // key is returned, otherwise the value passed as a default is returned.
00045 template <class Collection>
00046 const typename Collection::value_type::second_type&
00047 FindWithDefault(const Collection& collection,
00048                 const typename Collection::value_type::first_type& key,
00049                 const typename Collection::value_type::second_type& value) {
00050   typename Collection::const_iterator it = collection.find(key);
00051   if (it == collection.end()) {
00052     return value;
00053   }
00054   return it->second;
00055 }
00056 
00057 // Perform a lookup in a map or hash_map.
00058 // If the key is present a const pointer to the associated value is returned,
00059 // otherwise a NULL pointer is returned.
00060 template <class Collection>
00061 const typename Collection::value_type::second_type*
00062 FindOrNull(const Collection& collection,
00063            const typename Collection::value_type::first_type& key) {
00064   typename Collection::const_iterator it = collection.find(key);
00065   if (it == collection.end()) {
00066     return 0;
00067   }
00068   return &it->second;
00069 }
00070 
00071 // Perform a lookup in a map or hash_map whose values are pointers.
00072 // If the key is present a const pointer to the associated value is returned,
00073 // otherwise a NULL pointer is returned.
00074 // This function does not distinguish between a missing key and a key mapped
00075 // to a NULL value.
00076 template <class Collection>
00077 const typename Collection::value_type::second_type
00078 FindPtrOrNull(const Collection& collection,
00079               const typename Collection::value_type::first_type& key) {
00080   typename Collection::const_iterator it = collection.find(key);
00081   if (it == collection.end()) {
00082     return 0;
00083   }
00084   return it->second;
00085 }
00086 
00087 // Change the value associated with a particular key in a map or hash_map.
00088 // If the key is not present in the map the key and value are inserted,
00089 // otherwise the value is updated to be a copy of the value provided.
00090 // True indicates that an insert took place, false indicates an update.
00091 template <class Collection, class Key, class Value>
00092 bool InsertOrUpdate(Collection * const collection,
00093                    const Key& key, const Value& value) {
00094   pair<typename Collection::iterator, bool> ret =
00095     collection->insert(typename Collection::value_type(key, value));
00096   if (!ret.second) {
00097     // update
00098     ret.first->second = value;
00099     return false;
00100   }
00101   return true;
00102 }
00103 
00104 // Insert a new key and value into a map or hash_map.
00105 // If the key is not present in the map the key and value are
00106 // inserted, otherwise nothing happens. True indicates that an insert
00107 // took place, false indicates the key was already present.
00108 template <class Collection, class Key, class Value>
00109 bool InsertIfNotPresent(Collection * const collection,
00110                         const Key& key, const Value& value) {
00111   pair<typename Collection::iterator, bool> ret =
00112     collection->insert(typename Collection::value_type(key, value));
00113   return ret.second;
00114 }
00115 
00116 }  // namespace protobuf
00117 }  // namespace google
00118 
00119 #endif  // GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines