Home | History | Annotate | Download | only in gtl
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef LIBTEXTCLASSIFIER_UTIL_GTL_MAP_UTIL_H_
     18 #define LIBTEXTCLASSIFIER_UTIL_GTL_MAP_UTIL_H_
     19 
     20 namespace libtextclassifier2 {
     21 
     22 // Returns a const reference to the value associated with the given key if it
     23 // exists, otherwise returns a const reference to the provided default value.
     24 //
     25 // WARNING: If a temporary object is passed as the default "value,"
     26 // this function will return a reference to that temporary object,
     27 // which will be destroyed at the end of the statement. A common
     28 // example: if you have a map with string values, and you pass a char*
     29 // as the default "value," either use the returned value immediately
     30 // or store it in a string (not string&).
     31 template <class Collection>
     32 const typename Collection::value_type::second_type& FindWithDefault(
     33     const Collection& collection,
     34     const typename Collection::value_type::first_type& key,
     35     const typename Collection::value_type::second_type& value) {
     36   typename Collection::const_iterator it = collection.find(key);
     37   if (it == collection.end()) {
     38     return value;
     39   }
     40   return it->second;
     41 }
     42 
     43 // Inserts the given key and value into the given collection if and only if the
     44 // given key did NOT already exist in the collection. If the key previously
     45 // existed in the collection, the value is not changed. Returns true if the
     46 // key-value pair was inserted; returns false if the key was already present.
     47 template <class Collection>
     48 bool InsertIfNotPresent(Collection* const collection,
     49                         const typename Collection::value_type& vt) {
     50   return collection->insert(vt).second;
     51 }
     52 
     53 // Same as above except the key and value are passed separately.
     54 template <class Collection>
     55 bool InsertIfNotPresent(
     56     Collection* const collection,
     57     const typename Collection::value_type::first_type& key,
     58     const typename Collection::value_type::second_type& value) {
     59   return InsertIfNotPresent(collection,
     60                             typename Collection::value_type(key, value));
     61 }
     62 
     63 }  // namespace libtextclassifier2
     64 
     65 #endif  // LIBTEXTCLASSIFIER_UTIL_GTL_MAP_UTIL_H_
     66