OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 TOOLS_GN_LABEL_PTR_H_ | 5 #ifndef TOOLS_GN_LABEL_PTR_H_ |
6 #define TOOLS_GN_LABEL_PTR_H_ | 6 #define TOOLS_GN_LABEL_PTR_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <functional> | 10 #include <functional> |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 typedef LabelPtrPair<Target> LabelTargetPair; | 49 typedef LabelPtrPair<Target> LabelTargetPair; |
50 | 50 |
51 typedef std::vector<LabelConfigPair> LabelConfigVector; | 51 typedef std::vector<LabelConfigPair> LabelConfigVector; |
52 typedef std::vector<LabelTargetPair> LabelTargetVector; | 52 typedef std::vector<LabelTargetPair> LabelTargetVector; |
53 | 53 |
54 // Comparison and search functions --------------------------------------------- | 54 // Comparison and search functions --------------------------------------------- |
55 | 55 |
56 // To do a brute-force search by label: | 56 // To do a brute-force search by label: |
57 // std::find_if(vect.begin(), vect.end(), LabelPtrLabelEquals<Config>(label)); | 57 // std::find_if(vect.begin(), vect.end(), LabelPtrLabelEquals<Config>(label)); |
58 template<typename T> | 58 template<typename T> |
59 struct LabelPtrLabelEquals : public std::unary_function<Label, bool> { | 59 struct LabelPtrLabelEquals { |
60 explicit LabelPtrLabelEquals(const Label& l) : label(l) {} | 60 explicit LabelPtrLabelEquals(const Label& l) : label(l) {} |
61 | 61 |
62 bool operator()(const LabelPtrPair<T>& arg) const { | 62 bool operator()(const LabelPtrPair<T>& arg) const { |
63 return arg.label == label; | 63 return arg.label == label; |
64 } | 64 } |
65 | 65 |
66 const Label& label; | 66 const Label& label; |
67 }; | 67 }; |
68 | 68 |
69 // To do a brute-force search by object pointer: | 69 // To do a brute-force search by object pointer: |
70 // std::find_if(vect.begin(), vect.end(), LabelPtrPtrEquals<Config>(config)); | 70 // std::find_if(vect.begin(), vect.end(), LabelPtrPtrEquals<Config>(config)); |
71 template<typename T> | 71 template<typename T> |
72 struct LabelPtrPtrEquals : public std::unary_function<T, bool> { | 72 struct LabelPtrPtrEquals { |
73 explicit LabelPtrPtrEquals(const T* p) : ptr(p) {} | 73 explicit LabelPtrPtrEquals(const T* p) : ptr(p) {} |
74 | 74 |
75 bool operator()(const LabelPtrPair<T>& arg) const { | 75 bool operator()(const LabelPtrPair<T>& arg) const { |
76 return arg.ptr == ptr; | 76 return arg.ptr == ptr; |
77 } | 77 } |
78 | 78 |
79 const T* ptr; | 79 const T* ptr; |
80 }; | 80 }; |
81 | 81 |
82 // To sort by label: | 82 // To sort by label: |
83 // std::sort(vect.begin(), vect.end(), LabelPtrLabelLess<Config>()); | 83 // std::sort(vect.begin(), vect.end(), LabelPtrLabelLess<Config>()); |
84 template<typename T> | 84 template<typename T> |
85 struct LabelPtrLabelLess : public std::binary_function<LabelPtrPair<T>, | 85 struct LabelPtrLabelLess { |
86 LabelPtrPair<T>, | |
87 bool> { | |
88 bool operator()(const LabelPtrPair<T>& a, const LabelPtrPair<T>& b) const { | 86 bool operator()(const LabelPtrPair<T>& a, const LabelPtrPair<T>& b) const { |
89 return a.label < b.label; | 87 return a.label < b.label; |
90 } | 88 } |
91 }; | 89 }; |
92 | 90 |
93 // Default comparison operators ----------------------------------------------- | 91 // Default comparison operators ----------------------------------------------- |
94 // | 92 // |
95 // The default hash and comparison operators operate on the label, which should | 93 // The default hash and comparison operators operate on the label, which should |
96 // always be valid, whereas the pointer is sometimes null. | 94 // always be valid, whereas the pointer is sometimes null. |
97 | 95 |
(...skipping 12 matching lines...) Expand all Loading... |
110 template<typename T> struct hash< LabelPtrPair<T> > { | 108 template<typename T> struct hash< LabelPtrPair<T> > { |
111 std::size_t operator()(const LabelPtrPair<T>& v) const { | 109 std::size_t operator()(const LabelPtrPair<T>& v) const { |
112 BASE_HASH_NAMESPACE::hash<Label> h; | 110 BASE_HASH_NAMESPACE::hash<Label> h; |
113 return h(v.label); | 111 return h(v.label); |
114 } | 112 } |
115 }; | 113 }; |
116 | 114 |
117 } // namespace BASE_HASH_NAMESPACE | 115 } // namespace BASE_HASH_NAMESPACE |
118 | 116 |
119 #endif // TOOLS_GN_LABEL_PTR_H_ | 117 #endif // TOOLS_GN_LABEL_PTR_H_ |
OLD | NEW |