File indexing completed on 2023-03-17 11:21:12
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef NOTBOOST_ALGORITHM_COMBINATION_HPP
0014 #define NOTBOOST_ALGORITHM_COMBINATION_HPP
0015
0016 #include <algorithm>
0017
0018 namespace notboost {
0019
0020 namespace detail {
0021
0022 template <class BidirectionalIterator>
0023 bool next_combination(BidirectionalIterator first1,
0024 BidirectionalIterator last1,
0025 BidirectionalIterator first2,
0026 BidirectionalIterator last2) {
0027 if ((first1 == last1) || (first2 == last2)) {
0028 return false;
0029 }
0030
0031 BidirectionalIterator m1 = last1;
0032 BidirectionalIterator m2 = last2;
0033 --m2;
0034
0035
0036
0037
0038 while (--m1 != first1 && !(*m1 < *m2)) {
0039 }
0040
0041
0042 bool result = (m1 == first1) && !(*first1 < *m2);
0043
0044 if (!result) {
0045
0046
0047 while (first2 != m2 && !(*m1 < *first2)) {
0048 ++first2;
0049 }
0050
0051 first1 = m1;
0052 std::iter_swap(first1, first2);
0053 ++first1;
0054 ++first2;
0055 }
0056
0057
0058
0059 if ((first1 != last1) && (first2 != last2)) {
0060 for (m1 = last1, m2 = first2; (m1 != first1) && (m2 != last2); ++m2) {
0061 std::iter_swap(--m1, m2);
0062 }
0063
0064 std::reverse(first1, m1);
0065 std::reverse(first1, last1);
0066
0067 std::reverse(m2, last2);
0068 std::reverse(first2, last2);
0069 }
0070
0071 return !result;
0072 }
0073
0074 }
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085 template <class BidirectionalIterator>
0086 bool next_combination(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last) {
0087 return detail::next_combination(first, middle, middle, last);
0088 }
0089
0090 }
0091
0092 #endif