Taz bisounours-codeur | Code :
- // next_permutation and prev_permutation, with and without an explicitly
- // supplied comparison function.
- template <class _BidirectionalIter>
- bool next_permutation(_BidirectionalIter __first, _BidirectionalIter __last) {
- __STL_REQUIRES(_BidirectionalIter, _BidirectionalIterator);
- __STL_REQUIRES(typename iterator_traits<_BidirectionalIter>::value_type,
- _LessThanComparable);
- if (__first == __last)
- return false;
- _BidirectionalIter __i = __first;
- ++__i;
- if (__i == __last)
- return false;
- __i = __last;
- --__i;
- for(;;) {
- _BidirectionalIter __ii = __i;
- --__i;
- if (*__i < *__ii) {
- _BidirectionalIter __j = __last;
- while (!(*__i < *--__j))
- {}
- iter_swap(__i, __j);
- reverse(__ii, __last);
- return true;
- }
- if (__i == __first) {
- reverse(__first, __last);
- return false;
- }
- }
- }
- template <class _BidirectionalIter, class _Compare>
- bool next_permutation(_BidirectionalIter __first, _BidirectionalIter __last,
- _Compare __comp) {
- __STL_REQUIRES(_BidirectionalIter, _BidirectionalIterator);
- __STL_BINARY_FUNCTION_CHECK(_Compare, bool,
- typename iterator_traits<_BidirectionalIter>::value_type,
- typename iterator_traits<_BidirectionalIter>::value_type);
- if (__first == __last)
- return false;
- _BidirectionalIter __i = __first;
- ++__i;
- if (__i == __last)
- return false;
- __i = __last;
- --__i;
- for(;;) {
- _BidirectionalIter __ii = __i;
- --__i;
- if (__comp(*__i, *__ii)) {
- _BidirectionalIter __j = __last;
- while (!__comp(*__i, *--__j))
- {}
- iter_swap(__i, __j);
- reverse(__ii, __last);
- return true;
- }
- if (__i == __first) {
- reverse(__first, __last);
- return false;
- }
- }
- }
- template <class _BidirectionalIter>
- bool prev_permutation(_BidirectionalIter __first, _BidirectionalIter __last) {
- __STL_REQUIRES(_BidirectionalIter, _BidirectionalIterator);
- __STL_REQUIRES(typename iterator_traits<_BidirectionalIter>::value_type,
- _LessThanComparable);
- if (__first == __last)
- return false;
- _BidirectionalIter __i = __first;
- ++__i;
- if (__i == __last)
- return false;
- __i = __last;
- --__i;
- for(;;) {
- _BidirectionalIter __ii = __i;
- --__i;
- if (*__ii < *__i) {
- _BidirectionalIter __j = __last;
- while (!(*--__j < *__i))
- {}
- iter_swap(__i, __j);
- reverse(__ii, __last);
- return true;
- }
- if (__i == __first) {
- reverse(__first, __last);
- return false;
- }
- }
- }
- template <class _BidirectionalIter, class _Compare>
- bool prev_permutation(_BidirectionalIter __first, _BidirectionalIter __last,
- _Compare __comp) {
- __STL_REQUIRES(_BidirectionalIter, _BidirectionalIterator);
- __STL_BINARY_FUNCTION_CHECK(_Compare, bool,
- typename iterator_traits<_BidirectionalIter>::value_type,
- typename iterator_traits<_BidirectionalIter>::value_type);
- if (__first == __last)
- return false;
- _BidirectionalIter __i = __first;
- ++__i;
- if (__i == __last)
- return false;
- __i = __last;
- --__i;
- for(;;) {
- _BidirectionalIter __ii = __i;
- --__i;
- if (__comp(*__ii, *__i)) {
- _BidirectionalIter __j = __last;
- while (!__comp(*--__j, *__i))
- {}
- iter_swap(__i, __j);
- reverse(__ii, __last);
- return true;
- }
- if (__i == __first) {
- reverse(__first, __last);
- return false;
- }
- }
- }
|
|