Skip to content

VecDeque has no sort_by method (should possibly have one to match Vec).聽#27322

Closed

Description

@mitchmindtree

I just came across a use case for this while working on a visit order for the UI graph in conrod - its absence took me by surprise 馃樃

Is there a particular reason this hasn't been implemented yet? Or has it just not popped up yet (pun intended)?

Activity

  1. tamird commented on Jul 27, 2015

    @tamird
    Contributor

    How come either method has to be inherent? Seems like this could be a trait that requires sort and provides sort_by.

  2. abonander commented on Oct 28, 2015

    @abonander
    Contributor

    sort and sort_by are methods provided by [T]. Since the sorting algorithm requires 2 * n extra space, I don't see a too much problem with copying the data from the disjoint slices into one allocation to sort and then putting it back in-order. It's mostly just a question of semantics.

  3. retep998 commented on Oct 30, 2015

    @retep998
    Contributor

    Wouldn't it have to be sort_by which is required and sort have a default implementation?
    I'd prefer if VecDeque had an optimal in place sorting algorithm though.

  4. Gankra commented on Oct 30, 2015

    @Gankra
    Contributor

    @retep998 the thing is std has no optimal or inplace sorting! All we have is naive merge sort (because it was argued that our sort should be stable). Our sort support is a bit trash imo. Quicksort (unstable) or wikisort (stable but complicated) would be good to have here. Maybe even expose insertionsort!

    Even then, it's genuinely unclear to me what would be optimal here. Seems like linearizing the array to make it contiguous and avoid modulo logic could yield huge benefits.

  5. petrochenkov commented on Oct 30, 2015

    @petrochenkov
    Contributor

    sort is stable? o_O
    At least a good thing is that it's not documented, it's still possible to fix it and add stable_sort as a separate function.

  6. Gankra commented on Oct 30, 2015

    @Gankra
    Contributor

    Changing that may be an unacceptable break.

  7. abonander commented on Oct 31, 2015

    @abonander
    Contributor

    It'd be cool, if a little overkill, to have a crate with a bunch of different sorting functions. That might make a good crate for contain-rs.

  8. bluss commented on Oct 31, 2015

    @bluss
    Contributor

    Oh no, we haven't documented that it's a stable sort. That's a strange oversight.

  9. petrochenkov commented on Oct 31, 2015

    @petrochenkov
    Contributor

    @bluss

    Oh no, we haven't documented that it's a stable sort.

    "Oh yes" you mean?
    Why should sort be stable by default? Stability is not usually needed. "Don't pay for what you don't use" and everything like this.

  10. Gankra commented on Oct 31, 2015

    @Gankra
    Contributor

    The current situation was definitely an active decision. I'm having trouble finding it, though.

    I basically agree that it's a weird cost, but there's possibly an argument to be had that it's the "safer" default, and unstable_sort could easily be the opt-in to less reliable behavior.

  11. bluss commented on Oct 31, 2015

    @bluss
    Contributor

    @petrochenkov It should be stable by default because a stable sort is more versatile. Reordering equally ordered elements in sort is a potential gotcha, too, and Rust wants to be a nice language. The third party crate space is open to find other sorting algorithms with different tradeoffs.

  12. Gankra commented on Oct 31, 2015

    @Gankra
    Contributor

    Some discussion:

    #11064

  13. added 3 commits that reference this issue on Nov 4, 2015
    a118aa2
    dd3778d
    05cbfa4
  14. nico-abram commented on Feb 22, 2020

    @nico-abram
    Contributor

    Why was this closed? The PR linked by bors has nothing to do with VecDeque as far as I can see, and I cannot find a way to sort a VecDeque optimally in std. Someone (@hyeonu in the rust discord) mentioned From VecDeque for Vec and back guarantees to not allocate (In the docs), but it (1) requires ownership of the VecDeque (So can't use it to sort &mut VecDeque afaik) and (2) requires moving some data around before beginning the sort(Which shouldn't be necessary unless I'm missing something).

    I'm not sure if there are other scenarios that it would make sense to sort that provide random access but are not contiguous memory from first to last (Which the current sort in std requires because it uses slices), but there might be.

  15. Mark-Simulacrum commented on Feb 22, 2020

    @Mark-Simulacrum
    Member

    It seems like this should be possible, at least. A slightly non-optimal implementation would be to sort the front and back slices separately and then swap elements back and forth as needed (second step of merge sort, basically).

  16. added
    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
    T-libs-api[DEPRECATED; DO NOT USE]
    on Mar 25, 2020
  17. retep998 commented on Apr 1, 2020

    @retep998
    Contributor

    Closed because of #69425 I assume

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-collectionsArea: `std::collections`C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions