/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef LIBTEXTCLASSIFIER_UTIL_GTL_MAP_UTIL_H_ #define LIBTEXTCLASSIFIER_UTIL_GTL_MAP_UTIL_H_ namespace libtextclassifier { // Returns a const reference to the value associated with the given key if it // exists, otherwise returns a const reference to the provided default value. // // WARNING: If a temporary object is passed as the default "value," // this function will return a reference to that temporary object, // which will be destroyed at the end of the statement. A common // example: if you have a map with string values, and you pass a char* // as the default "value," either use the returned value immediately // or store it in a string (not string&). template const typename Collection::value_type::second_type& FindWithDefault( const Collection& collection, const typename Collection::value_type::first_type& key, const typename Collection::value_type::second_type& value) { typename Collection::const_iterator it = collection.find(key); if (it == collection.end()) { return value; } return it->second; } // Inserts the given key and value into the given collection if and only if the // given key did NOT already exist in the collection. If the key previously // existed in the collection, the value is not changed. Returns true if the // key-value pair was inserted; returns false if the key was already present. template bool InsertIfNotPresent(Collection* const collection, const typename Collection::value_type& vt) { return collection->insert(vt).second; } // Same as above except the key and value are passed separately. template bool InsertIfNotPresent( Collection* const collection, const typename Collection::value_type::first_type& key, const typename Collection::value_type::second_type& value) { return InsertIfNotPresent(collection, typename Collection::value_type(key, value)); } } // namespace libtextclassifier #endif // LIBTEXTCLASSIFIER_UTIL_GTL_MAP_UTIL_H_