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> | 8 #include <vector> |
9 | 9 |
10 namespace pdfium { | 10 namespace pdfium { |
11 | 11 |
12 // To treat a possibly-empty vector as an array, use these functions. | 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() | 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. | 14 // directly, but that is undefined behaviour if |v| is empty. |
15 template <typename T> | 15 template <typename T> |
16 inline T* vector_as_array(std::vector<T>* v) { | 16 inline T* vector_as_array(std::vector<T>* v) { |
17 return v->empty() ? nullptr : &*v->begin(); | 17 return v->empty() ? nullptr : &*v->begin(); |
18 } | 18 } |
19 | 19 |
20 template <typename T> | 20 template <typename T> |
21 inline const T* vector_as_array(const std::vector<T>* v) { | 21 inline const T* vector_as_array(const std::vector<T>* v) { |
22 return v->empty() ? nullptr : &*v->begin(); | 22 return v->empty() ? nullptr : &*v->begin(); |
23 } | 23 } |
24 | 24 |
| 25 // 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. |
| 27 template <typename Collection, typename Key> |
| 28 bool ContainsKey(const Collection& collection, const Key& key) { |
| 29 return collection.find(key) != collection.end(); |
| 30 } |
| 31 |
| 32 // Test to see if a collection like a vector contains a particular value. |
| 33 // Returns true if the value is in the collection. |
| 34 template <typename Collection, typename Value> |
| 35 bool ContainsValue(const Collection& collection, const Value& value) { |
| 36 return std::find(collection.begin(), collection.end(), value) != |
| 37 collection.end(); |
| 38 } |
| 39 |
25 } // namespace pdfium | 40 } // namespace pdfium |
26 | 41 |
27 #endif // PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_ | 42 #endif // PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_ |
OLD | NEW |