question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make `Iterators.mergeSorted()` stable

See original GitHub issue

I’ve noticed that Iterators.mergeSorted() is unstable.

So for example if I have arrays: [A_1, B_1, C_1] [A_2, B_2, C_2] (where the comparator looks only at the letter), I’d like the result to iterate [A_1, A_2, B_1, B_2, C_1, C_2]. The current implementation doesn’t guarantee this.

Might it be possible to add a stable version?

I confess it’s not obvious what to do if the arrays are something like [A_1, A_2, B_1], [A_3] - to me it would make sense to empty the first iterator of A_n elements before moving on to the second, so [A_1, A_2, A_3, B_1].

[I appreciate I’m being a little lazy with my terminology here - I can provide a fully fleshed example if it’s not clear]

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
u6885077commented, Oct 26, 2022

Hi, I am new to open source contribution projects. I see that this issue is still open. I was hoping that I could work on it if it has not already been implemented?

0reactions
msmerccommented, May 3, 2022

@chaoren / google folk: I’ve had a go at solving this, see https://gist.github.com/msmerc/fff91f4049dd4d8e39f3614bb0c58acb

My solution is to generate a comparable such that objects are equal (using the original comparator) it will compare the position in the initial array. Is this something you’d want in the main library? Let me know.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Merge Sort using iterators in C++ - Stack Overflow
Problem is here merge(result, vector, vector.begin(), mid); merge(result, vector, mid, vector.end());. That should be
Read more >
c++ - Merge Sort implemented using iterators
This seems like a straightforward top-down implementation of mergesort. However, there are some issues: Stability.
Read more >
https://www.cs.uaf.edu/2009/fall/cs311/docs/merge_...
Merge is done in a stable manner. // Requirements on Types: // FDIter is a forward iterator type. // operator< is a total...
Read more >
Google | Onsite | Merge K Sorted Iterators - LeetCode Discuss
Standard PQ solution for merging K sorted lists, clean. constructor - O(K*logK) - where K - number of lists(iterators) hasNext() - O(1) speed...
Read more >
stable_sort() in C++ STL - GeeksforGeeks
first: iterator pointing to the first element in the range. last: iterator pointing to ... C++ program to demonstrate stable sort() in STL....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found