Home | History | Annotate | Download | only in stubs
      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&
     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*
     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, assuming that the key exists.
     72 // Crash if it does not.
     73 //
     74 // This is intended as a replacement for operator[] as an rvalue (for reading)
     75 // when the key is guaranteed to exist.
     76 //
     77 // operator[] is discouraged for several reasons:
     78 //  * It has a side-effect of inserting missing keys
     79 //  * It is not thread-safe (even when it is not inserting, it can still
     80 //      choose to resize the underlying storage)
     81 //  * It invalidates iterators (when it chooses to resize)
     82 //  * It default constructs a value object even if it doesn't need to
     83 //
     84 // This version assumes the key is printable, and includes it in the fatal log
     85 // message.
     86 template <class Collection>
     87 const typename Collection::value_type::second_type&
     88 FindOrDie(const Collection& collection,
     89           const typename Collection::value_type::first_type& key) {
     90   typename Collection::const_iterator it = collection.find(key);
     91   GOOGLE_CHECK(it != collection.end()) << "Map key not found: " << key;
     92   return it->second;
     93 }
     94 
     95 // Perform a lookup in a map or hash_map whose values are pointers.
     96 // If the key is present a const pointer to the associated value is returned,
     97 // otherwise a NULL pointer is returned.
     98 // This function does not distinguish between a missing key and a key mapped
     99 // to a NULL value.
    100 template <class Collection>
    101 const typename Collection::value_type::second_type
    102 FindPtrOrNull(const Collection& collection,
    103               const typename Collection::value_type::first_type& key) {
    104   typename Collection::const_iterator it = collection.find(key);
    105   if (it == collection.end()) {
    106     return 0;
    107   }
    108   return it->second;
    109 }
    110 
    111 // Change the value associated with a particular key in a map or hash_map.
    112 // If the key is not present in the map the key and value are inserted,
    113 // otherwise the value is updated to be a copy of the value provided.
    114 // True indicates that an insert took place, false indicates an update.
    115 template <class Collection, class Key, class Value>
    116 bool InsertOrUpdate(Collection * const collection,
    117                    const Key& key, const Value& value) {
    118   pair<typename Collection::iterator, bool> ret =
    119     collection->insert(typename Collection::value_type(key, value));
    120   if (!ret.second) {
    121     // update
    122     ret.first->second = value;
    123     return false;
    124   }
    125   return true;
    126 }
    127 
    128 // Insert a new key and value into a map or hash_map.
    129 // If the key is not present in the map the key and value are
    130 // inserted, otherwise nothing happens. True indicates that an insert
    131 // took place, false indicates the key was already present.
    132 template <class Collection, class Key, class Value>
    133 bool InsertIfNotPresent(Collection * const collection,
    134                         const Key& key, const Value& value) {
    135   pair<typename Collection::iterator, bool> ret =
    136     collection->insert(typename Collection::value_type(key, value));
    137   return ret.second;
    138 }
    139 
    140 }  // namespace protobuf
    141 }  // namespace google
    142 
    143 #endif  // GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__
    144