-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an explicit stable_adapter<verge_sorter> specializations
An likewise for stable_adapter<verge_adapter>. Instead of simply relying on make_stable, the new specializations use a variant of vergesort that detects strictly descending runs instead of non-ascending ones, and wraps the fallback sorter in stable_t. The technique of detecting non-descending and strictly descending runs is a trick borrowed from timsort in order to preserve stability in natural mergesort algorithms: descending runs are reversed in-place, so equivalent elements wouldn't retain their original order when reversed in such runs. While verge_adapter does not handle bidirectional iterators, the underlying code was changed so that it will be easier to do in the future, but currently this involves some dirty tricks with a stripped down implementation of a sized iterator. This will make the transition to C++20 ranges and standard sized iterators and sentinels easier in the long term. Meanwhile it is just an implementation detail. This commit addresses external issue Morwenn/vergesort#11 and somehow Morwenn/vergesort#7 too. cpp-sort is a better recipient for those variations on vergesort than the standalone project, but some cross-project documentation will be needed anyway.
- Loading branch information
Showing
9 changed files
with
351 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright (c) 2020 Morwenn | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
#ifndef CPPSORT_DETAIL_SIZED_ITERATOR_H_ | ||
#define CPPSORT_DETAIL_SIZED_ITERATOR_H_ | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Headers | ||
//////////////////////////////////////////////////////////// | ||
|
||
namespace cppsort | ||
{ | ||
namespace detail | ||
{ | ||
//////////////////////////////////////////////////////////// | ||
// Mostly a hack to avoid some gratuitous performance loss | ||
// by passing bidirectional iterators + size to a function | ||
// accepting a pair of iterators. It is worse than the | ||
// equivalent C++20 features, but should be good enough for | ||
// the internal use we make of it. | ||
// | ||
// NOTE: the full iterator features are not provided, this | ||
// is intentional to avoid unintentional uses of the | ||
// class in the library's internals. | ||
|
||
template<typename Iterator> | ||
class sized_iterator | ||
{ | ||
public: | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Public types | ||
|
||
using iterator_category = iterator_category_t<Iterator>; | ||
using iterator_type = Iterator; | ||
using value_type = value_type_t<Iterator>; | ||
using difference_type = difference_type_t<Iterator>; | ||
using pointer = pointer_t<Iterator>; | ||
using reference = reference_t<Iterator>; | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Constructors | ||
|
||
sized_iterator() = default; | ||
|
||
constexpr sized_iterator(Iterator it, difference_type size): | ||
_it(std::move(it)), | ||
_size(size) | ||
{} | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Members access | ||
|
||
auto base() const | ||
-> iterator_type | ||
{ | ||
return _it; | ||
} | ||
|
||
auto size() const | ||
-> difference_type | ||
{ | ||
return _size; | ||
} | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Element access | ||
|
||
auto operator*() const | ||
-> reference | ||
{ | ||
return *_it; | ||
} | ||
|
||
auto operator->() const | ||
-> pointer | ||
{ | ||
return &(operator*()); | ||
} | ||
|
||
private: | ||
|
||
Iterator _it; | ||
difference_type _size; | ||
}; | ||
|
||
// Alternative to std::distance meant to be picked up by ADL in | ||
// specific places, uses the size of the *second* iterator | ||
template<typename Iterator> | ||
constexpr auto distance(sized_iterator<Iterator>, sized_iterator<Iterator> last) | ||
-> difference_type_t<Iterator> | ||
{ | ||
return last.size(); | ||
} | ||
|
||
template<typename Iterator> | ||
auto make_sized_iterator(Iterator it, difference_type_t<Iterator> size) | ||
-> sized_iterator<Iterator> | ||
{ | ||
return { it, size }; | ||
} | ||
|
||
}} | ||
|
||
#endif // CPPSORT_DETAIL_SIZED_ITERATOR_H_ |
Oops, something went wrong.