Skip to main content
Tweeted twitter.com/StackCodeReview/status/1380943683743326215
Describe the purpose, not the concerns, in the title
Source Link
Toby Speight
  • 81.7k
  • 14
  • 101
  • 308

How to make this rust algorithm code cleaner? Rust implementation of next-permutation

template<class BidirIt>
bool next_permutation(BidirIt first, BidirIt last)
{
    if (first == last) return false;
    BidirIt i = last;
    if (first == --i) return false;
  
    while (true) {
        BidirIt i1, i2;
  
        i1 = i;
        if (*--i < *i1) {
            i2 = last;
            while (!(*i < *--i2))
                ;
            std::iter_swap(i, i2);
            std::reverse(i1, last);
            return true;
        }
        if (i == first) {
            std::reverse(first, last);
            return false;
        }
    }
}
template<class BidirIt>
bool next_permutation(BidirIt first, BidirIt last)
{
    if (first == last) return false;
    BidirIt i = last;
    if (first == --i) return false;
  
    while (true) {
        BidirIt i1, i2;
  
        i1 = i;
        if (*--i < *i1) {
            i2 = last;
            while (!(*i < *--i2))
                ;
            std::iter_swap(i, i2);
            std::reverse(i1, last);
            return true;
        }
        if (i == first) {
            std::reverse(first, last);
            return false;
        }
    }
}

Though it works, I think it's not very good-looking. I am a beginner in rust,Rust; any improvement will be welcome!

How to make this rust algorithm code cleaner?

template<class BidirIt>
bool next_permutation(BidirIt first, BidirIt last)
{
    if (first == last) return false;
    BidirIt i = last;
    if (first == --i) return false;
  
    while (true) {
        BidirIt i1, i2;
  
        i1 = i;
        if (*--i < *i1) {
            i2 = last;
            while (!(*i < *--i2))
                ;
            std::iter_swap(i, i2);
            std::reverse(i1, last);
            return true;
        }
        if (i == first) {
            std::reverse(first, last);
            return false;
        }
    }
}

Though it works, I think it's not very good-looking. I am a beginner in rust, any improvement will be welcome!

Rust implementation of next-permutation

template<class BidirIt>
bool next_permutation(BidirIt first, BidirIt last)
{
    if (first == last) return false;
    BidirIt i = last;
    if (first == --i) return false;
  
    while (true) {
        BidirIt i1, i2;
  
        i1 = i;
        if (*--i < *i1) {
            i2 = last;
            while (!(*i < *--i2))
                ;
            std::iter_swap(i, i2);
            std::reverse(i1, last);
            return true;
        }
        if (i == first) {
            std::reverse(first, last);
            return false;
        }
    }
}

Though it works, I think it's not very good-looking. I am a beginner in Rust; any improvement will be welcome!

added 25 characters in body
Source Link

Though it works, I think it's not very good-looking. AnyI am a beginner in rust, any improvement will be welcome!

Though it works, I think it's not very good-looking. Any improvement will be welcome!

Though it works, I think it's not very good-looking. I am a beginner in rust, any improvement will be welcome!

edited tags
Link
Malachi
  • 28.7k
  • 11
  • 86
  • 188
Post Migrated Here from codereview.meta.stackexchange.com (revisions)
Source Link
Loading