cpp-sort
cpp-sort copied to clipboard
Views, sort_copy, online algorithms...
cpp-sort currently only cares about in-place sorting algorithms: algorithms that mutate the passed collection and don't return anything (except when they do), which is pretty much enough for people most of the time. However, I believe that there is some value in having algorithms that actually copy a sorted equivalent of the passed collection into the library. It once again leads to maaaaany design decisions depending on how we want to implement that:
- Have a
cppsort::sort_copyfunction in the library taking a sorter (or defaulting todefault_sorter) and an additionalOutputIteratorparameter? - Provide an equivalent
cppsort::sort_movefunction? - Create a
cppsort::sortedfunction that takes an iterable and returns a new instance of the same iterable? Make it work with anOutputIteratorinstead? Have it take any sorter likecppsort::sort? - Add a
cppsort::sorted_viewreturning a range with sorted iterators which, when iterated, corresponds to a sorted view of the original collection? It's pretty much whatindirect_adapterdoes in the first place, but decoupling it from the adapter might also be interesting. - Have first-class support for online sorting algorithm? Adding an
is_onlineproperty incppsort::sorter_traitsis easy, but how to use it properly?
An interesting algorithm about incremental sort that might provide some idea and/or insights about online/incremental sorting: http://larshagencpp.github.io/blog/2016/04/23/fast-incremental-sort
For sort_copy algorithms, one interesting solution would be to take advantage of the fact that sorters are function objects to provide a copy or copy_to method:
cppsort::ska_sort.copy(collection, std::back_inserter(res));
I don't know whether it's a good idea. Like everything else I will have to balance the pros and cons.
What I called a sorted "view" in the original post can't be a View according to the standard concept semantics because views are supposed to have O(1) construction and linear traversal. A "sorted view" would either be sorted at construction in O(n log n) time or would use an online algorithm and have O(n log n) traversal. No matter what I do with this issue I should use another terminology.
A few updates regarding the concerns discussed in this issue:
- Issue #167 removes
cppsort::sortandcppsort::stable_sort, so adding a first-classcppsort::sortedmight seem like a poor idea. Though unlike the other two it would provide a feature that can't be covered by a simple sorter or sorter adapter. - The
sorted_viewidea is mostly covered by thesorted_iteratorsutility added as part of issue #200 (commit 8bd558cf9a6d993bf9aa51d3cd64f263c821f817).
The other points are still very much open design issues without obvious answers to this day. To keep the genericity of the collection/compare/projection design without getting too complicated, I'd probably need to invent a way to give a separate channel for output range parameters somehow.