MCPcopy Create free account
hub / github.com/ETLCPP/etl / prev_permutation

Function prev_permutation

include/etl/algorithm.h:1991–2032  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1989 //***************************************************************************
1990 template <typename TIterator, typename TCompare>
1991 ETL_CONSTEXPR14 bool prev_permutation(TIterator first, TIterator last, TCompare compare)
1992 {
1993 if (first == last)
1994 {
1995 return false;
1996 }
1997
1998 TIterator i = last;
1999 --i;
2000
2001 if (first == i)
2002 {
2003 return false;
2004 }
2005
2006 while (true)
2007 {
2008 TIterator i1 = i;
2009 --i;
2010
2011 if (compare(*i1, *i))
2012 {
2013 TIterator j = last;
2014 --j;
2015
2016 while (!compare(*j, *i))
2017 {
2018 --j;
2019 }
2020
2021 etl::iter_swap(i, j);
2022 etl::reverse(i1, last);
2023 return true;
2024 }
2025
2026 if (i == first)
2027 {
2028 etl::reverse(first, last);
2029 return false;
2030 }
2031 }
2032 }
2033
2034 //***************************************************************************
2035 /// prev_permutation

Callers 1

TESTFunction · 0.85

Calls 3

iter_swapFunction · 0.85
reverseClass · 0.85
compareClass · 0.70

Tested by 1

TESTFunction · 0.68