Rust dev, I enjoy reading and playing games, I also usually like to spend time with friends.

You can reach me on mastodon @sukhmel@mastodon.online or telegram @sukhmel@tg

  • 0 Posts
  • 2 Comments
Joined 3 years ago
cake
Cake day: July 3rd, 2023

help-circle
  • I haven’t looked at the code, so my two cents may be irrelevant, but it sounds like you could define a structure that borrows the vector, keeps a separate vector of indices into borrowed vector, and has a current element index.

    struct SortFacadeIterator<'a, T: Ord> {
        data: &'a Vec<T>,
        indices: Vec<usize>,
        position: usize,
    }
    

    Then in new you would need to sort the original vector but instead of mutating it you would store indices of a resulting sorted elements from the original, i.e. when passed [“b”, “a”, “c”] you would create index storage of [1, 0, 2]. After that you can iterate both ways, returning an element by index:

    impl<'a, T: Ord> SortFacadeIterator<'a, T> {
        fn current(&self) -> &T {
            self.data[self.indices[self.position]]
        }
    }
    

    I think, maybe sorting a vector could be done by enumerating original vector and sort_by a value, also I’m not sure you need a full ordering, but I don’t remember what a sort expects