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