00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef ABSL_ALGORITHM_ALGORITHM_H_
00023 #define ABSL_ALGORITHM_ALGORITHM_H_
00024
00025 #include <algorithm>
00026 #include <iterator>
00027 #include <type_traits>
00028
00029 namespace absl {
00030
00031 namespace algorithm_internal {
00032
00033
00034 struct EqualTo {
00035 template <typename T, typename U>
00036 bool operator()(const T& a, const U& b) const {
00037 return a == b;
00038 }
00039 };
00040
00041 template <typename InputIter1, typename InputIter2, typename Pred>
00042 bool EqualImpl(InputIter1 first1, InputIter1 last1, InputIter2 first2,
00043 InputIter2 last2, Pred pred, std::input_iterator_tag,
00044 std::input_iterator_tag) {
00045 while (true) {
00046 if (first1 == last1) return first2 == last2;
00047 if (first2 == last2) return false;
00048 if (!pred(*first1, *first2)) return false;
00049 ++first1;
00050 ++first2;
00051 }
00052 }
00053
00054 template <typename InputIter1, typename InputIter2, typename Pred>
00055 bool EqualImpl(InputIter1 first1, InputIter1 last1, InputIter2 first2,
00056 InputIter2 last2, Pred&& pred, std::random_access_iterator_tag,
00057 std::random_access_iterator_tag) {
00058 return (last1 - first1 == last2 - first2) &&
00059 std::equal(first1, last1, first2, std::forward<Pred>(pred));
00060 }
00061
00062
00063
00064
00065 template <typename InputIter1, typename InputIter2>
00066 bool EqualImpl(InputIter1 first1, InputIter1 last1, InputIter2 first2,
00067 InputIter2 last2, algorithm_internal::EqualTo ,
00068 std::random_access_iterator_tag,
00069 std::random_access_iterator_tag) {
00070 return (last1 - first1 == last2 - first2) &&
00071 std::equal(first1, last1, first2);
00072 }
00073
00074 template <typename It>
00075 It RotateImpl(It first, It middle, It last, std::true_type) {
00076 return std::rotate(first, middle, last);
00077 }
00078
00079 template <typename It>
00080 It RotateImpl(It first, It middle, It last, std::false_type) {
00081 std::rotate(first, middle, last);
00082 return std::next(first, std::distance(middle, last));
00083 }
00084
00085 }
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 template <typename InputIter1, typename InputIter2, typename Pred>
00099 bool equal(InputIter1 first1, InputIter1 last1, InputIter2 first2,
00100 InputIter2 last2, Pred&& pred) {
00101 return algorithm_internal::EqualImpl(
00102 first1, last1, first2, last2, std::forward<Pred>(pred),
00103 typename std::iterator_traits<InputIter1>::iterator_category{},
00104 typename std::iterator_traits<InputIter2>::iterator_category{});
00105 }
00106
00107
00108
00109 template <typename InputIter1, typename InputIter2>
00110 bool equal(InputIter1 first1, InputIter1 last1, InputIter2 first2,
00111 InputIter2 last2) {
00112 return absl::equal(first1, last1, first2, last2,
00113 algorithm_internal::EqualTo{});
00114 }
00115
00116
00117
00118
00119
00120
00121
00122
00123 template <typename InputIterator, typename EqualityComparable>
00124 bool linear_search(InputIterator first, InputIterator last,
00125 const EqualityComparable& value) {
00126 return std::find(first, last, value) != last;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 template <typename ForwardIterator>
00140 ForwardIterator rotate(ForwardIterator first, ForwardIterator middle,
00141 ForwardIterator last) {
00142 return algorithm_internal::RotateImpl(
00143 first, middle, last,
00144 std::is_same<decltype(std::rotate(first, middle, last)),
00145 ForwardIterator>());
00146 }
00147
00148 }
00149
00150 #endif // ABSL_ALGORITHM_ALGORITHM_H_