1 #ifndef MSP_CORE_ALGORITHM_H_
2 #define MSP_CORE_ALGORITHM_H_
9 template<typename Container, typename T>
10 inline typename Container::iterator find(Container &cont, const T &value)
12 return std::find(cont.begin(), cont.end(), value);
15 template<typename Container, typename T>
16 inline typename Container::const_iterator find(const Container &cont, const T &value)
18 return std::find(cont.begin(), cont.end(), value);
21 template<typename Container, typename Predicate>
22 inline typename Container::iterator find_if(Container &cont, Predicate pred)
24 return std::find_if(cont.begin(), cont.end(), pred);
27 template<typename Container, typename Predicate>
28 inline typename Container::const_iterator find_if(const Container &cont, Predicate pred)
30 return std::find_if(cont.begin(), cont.end(), pred);
38 bool operator()(const T &v) { return v==value; }
41 template<typename Container, typename T>
42 inline bool any_equals(Container &cont, const T &value)
44 return std::any_of(cont.begin(), cont.end(), ValueMatch<T>{value});
47 template<typename Container, typename T>
48 inline typename Container::iterator lower_bound(Container &cont, const T &value)
50 return std::lower_bound(cont.begin(), cont.end(), value);
53 template<typename Container, typename T>
54 inline typename Container::const_iterator lower_bound(const Container &cont, const T &value)
56 return std::lower_bound(cont.begin(), cont.end(), value);
59 template<typename Container, typename T, typename Predicate>
60 inline typename Container::iterator lower_bound(Container &cont, const T &value, Predicate pred)
62 return std::lower_bound(cont.begin(), cont.end(), value, pred);
65 template<typename Container, typename T, typename Predicate>
66 inline typename Container::const_iterator lower_bound(const Container &cont, const T &value, Predicate pred)
68 return std::lower_bound(cont.begin(), cont.end(), value, pred);
71 template<typename Container, typename T>
72 inline typename Container::iterator upper_bound(Container &cont, const T &value)
74 return std::upper_bound(cont.begin(), cont.end(), value);
77 template<typename Container, typename T>
78 inline typename Container::const_iterator upper_bound(const Container &cont, const T &value)
80 return std::upper_bound(cont.begin(), cont.end(), value);
83 template<typename Container, typename T, typename Predicate>
84 inline typename Container::iterator upper_bound(Container &cont, const T &value, Predicate pred)
86 return std::upper_bound(cont.begin(), cont.end(), value, pred);
89 template<typename Container, typename T, typename Predicate>
90 inline typename Container::const_iterator upper_bound(const Container &cont, const T &value, Predicate pred)
92 return std::upper_bound(cont.begin(), cont.end(), value, pred);
95 template<typename Container>
96 inline void sort(Container &cont)
98 std::sort(cont.begin(), cont.end());
101 template<typename Container, typename Predicate>
102 inline void sort(Container &cont, Predicate pred)
104 std::sort(cont.begin(), cont.end(), pred);
107 template<typename Container>
108 inline void stable_sort(Container &cont)
110 std::stable_sort(cont.begin(), cont.end());
113 template<typename Container, typename Predicate>
114 inline void stable_sort(Container &cont, Predicate pred)
116 std::stable_sort(cont.begin(), cont.end(), pred);
119 template<typename C, typename T>
125 bool operator()(const C &obj) { return obj.*mem_ptr==value; }
128 template<typename Container, typename T>
129 inline typename Container::iterator find_member(Container &cont, const T &value, T Container::value_type::*mp)
131 return find_if(cont, MemberMatch<typename Container::value_type, T>{ value, mp });
134 template<typename Container, typename T>
135 inline typename Container::const_iterator find_member(const Container &cont, const T &value, T Container::value_type::*mp)
137 return find_if(cont, MemberMatch<typename Container::value_type, T>{ value, mp });
140 template<typename C, typename T, typename P=std::less<T>>
146 MemberCompare(T C::*p): mem_ptr(p) { }
148 bool operator()(const C &obj, const T &v) { return pred(obj.*mem_ptr, v); }
149 bool operator()(const T &v, const C &obj) { return pred(v, obj.*mem_ptr); }
150 bool operator()(const C &obj1, const C &obj2) { return pred(obj1.*mem_ptr, obj2.*mem_ptr); }
153 template<typename Container, typename T>
154 inline typename Container::iterator lower_bound_member(Container &cont, const T &value, T Container::value_type::*mp)
156 return lower_bound(cont, value, MemberCompare<typename Container::value_type, T>(mp));
159 template<typename Container, typename T>
160 inline typename Container::const_iterator lower_bound_member(const Container &cont, const T &value, T Container::value_type::*mp)
162 return lower_bound(cont, value, MemberCompare<typename Container::value_type, T>(mp));
165 template<typename Container, typename T>
166 inline typename Container::iterator upper_bound_member(Container &cont, const T &value, T Container::value_type::*mp)
168 return upper_bound(cont, value, MemberCompare<typename Container::value_type, T>(mp));
171 template<typename Container, typename T>
172 inline typename Container::const_iterator upper_bound_member(const Container &cont, const T &value, T Container::value_type::*mp)
174 return upper_bound(cont, value, MemberCompare<typename Container::value_type, T>(mp));
177 template<typename Container, typename T>
178 inline void sort_member(Container &cont, T Container::value_type::*mp)
180 sort(cont, MemberCompare<typename Container::value_type, T>(mp));
183 template<typename Container, typename T>
184 inline void stable_sort_member(Container &cont, T Container::value_type::*mp)
186 stable_sort(cont, MemberCompare<typename Container::value_type, T>(mp));
189 template<typename Container, typename Predicate>
190 inline void transform(Container &cont, Predicate pred)
192 transform(cont.begin(), cont.end(), cont.begin(), pred);