| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_ | 5 #ifndef PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_ |
| 6 #define PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_ | 6 #define PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_ |
| 7 | 7 |
| 8 #include <vector> | |
| 9 | |
| 10 namespace pdfium { | 8 namespace pdfium { |
| 11 | 9 |
| 12 // To treat a possibly-empty vector as an array, use these functions. | |
| 13 // If you know the array will never be empty, you can use &*v.begin() | |
| 14 // directly, but that is undefined behaviour if |v| is empty. | |
| 15 template <typename T> | |
| 16 inline T* vector_as_array(std::vector<T>* v) { | |
| 17 return v->empty() ? nullptr : &*v->begin(); | |
| 18 } | |
| 19 | |
| 20 template <typename T> | |
| 21 inline const T* vector_as_array(const std::vector<T>* v) { | |
| 22 return v->empty() ? nullptr : &*v->begin(); | |
| 23 } | |
| 24 | |
| 25 // Test to see if a set, map, hash_set or hash_map contains a particular key. | 10 // Test to see if a set, map, hash_set or hash_map contains a particular key. |
| 26 // Returns true if the key is in the collection. | 11 // Returns true if the key is in the collection. |
| 27 template <typename Collection, typename Key> | 12 template <typename Collection, typename Key> |
| 28 bool ContainsKey(const Collection& collection, const Key& key) { | 13 bool ContainsKey(const Collection& collection, const Key& key) { |
| 29 return collection.find(key) != collection.end(); | 14 return collection.find(key) != collection.end(); |
| 30 } | 15 } |
| 31 | 16 |
| 32 // Test to see if a collection like a vector contains a particular value. | 17 // Test to see if a collection like a vector contains a particular value. |
| 33 // Returns true if the value is in the collection. | 18 // Returns true if the value is in the collection. |
| 34 template <typename Collection, typename Value> | 19 template <typename Collection, typename Value> |
| 35 bool ContainsValue(const Collection& collection, const Value& value) { | 20 bool ContainsValue(const Collection& collection, const Value& value) { |
| 36 return std::find(collection.begin(), collection.end(), value) != | 21 return std::find(collection.begin(), collection.end(), value) != |
| 37 collection.end(); | 22 collection.end(); |
| 38 } | 23 } |
| 39 | 24 |
| 40 } // namespace pdfium | 25 } // namespace pdfium |
| 41 | 26 |
| 42 #endif // PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_ | 27 #endif // PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_ |
| OLD | NEW |