-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add 'indirect_sort' #117
base: develop
Are you sure you want to change the base?
Add 'indirect_sort' #117
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks potentially useful.
Added some ideas for improving the description and pointed out a few places for improvement/typo-fixing.
doc/indirect_sort.qbk
Outdated
There are times that you want a sorted version of a sequence, but for some reason or another, you don't really want to sort them. Maybe the elements in the sequence are non-copyable (or non-movable), or the sequence is const, or they're just really expensive to move around. An example of this might be a sequence of records from a database. | ||
|
||
Nevertheless, you might want to sort them. That's where indirect sorting comes in. In a "normal" sort, the elements of the sequence to be sorted are shuffled in place. In indirect sorting, the elements are unchanged, but the sort algorithm returns to you a "permutation" of the elements that, when applied, will leave the elements in the sequence in a sorted order. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this a bit shorter wording especially avoiding to mention the need to sort twice:
There are times that you want a sorted version of a sequence, but for some reason or another, you don't really want to sort them. Maybe the elements in the sequence are non-copyable (or non-movable), or the sequence is const, or they're just really expensive to move around. An example of this might be a sequence of records from a database. | |
Nevertheless, you might want to sort them. That's where indirect sorting comes in. In a "normal" sort, the elements of the sequence to be sorted are shuffled in place. In indirect sorting, the elements are unchanged, but the sort algorithm returns to you a "permutation" of the elements that, when applied, will leave the elements in the sequence in a sorted order. | |
There are times that you want a sorted version of a sequence, but for some reason you don't want to modify it. Maybe the elements in the sequence can't be moved/copied, e.g. the sequence is const, or they're just really expensive to move around. An example of this might be a sequence of records from a database. | |
That's where indirect sorting comes in. In a "normal" sort, the elements of the sequence to be sorted are shuffled in place. In indirect sorting, the elements are unchanged, but the sort algorithm returns a "permutation" of the elements that, when applied, will put the elements in the sequence in a sorted order. |
Are the double-spaces after each sentence intended?
doc/indirect_sort.qbk
Outdated
|
||
Nevertheless, you might want to sort them. That's where indirect sorting comes in. In a "normal" sort, the elements of the sequence to be sorted are shuffled in place. In indirect sorting, the elements are unchanged, but the sort algorithm returns to you a "permutation" of the elements that, when applied, will leave the elements in the sequence in a sorted order. | ||
|
||
Say you have a sequence `[first, last)` of 1000 items that are expensive to swap: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Say you have a sequence `[first, last)` of 1000 items that are expensive to swap: | |
Assume a sequence `[first, last)` of 1000 items that are expensive to swap: |
|
||
#include <algorithm> // for std::sort (and others) | ||
#include <functional> // for std::less | ||
#include <vector> // for std:;vector |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo:
#include <vector> // for std:;vector | |
#include <vector> // for std::vector |
But is that comment really required?
/// | ||
|
||
#ifndef BOOST_ALGORITHM_IS_INDIRECT_SORT | ||
#define BOOST_ALGORITHM_IS_INDIRECT_SORT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unusual include guard. Why not BOOST_ALGORITHM_INDIRECT_SORT
?
/// \fn indirect_sort (RAIterator first, RAIterator las ) | ||
/// \returns a permutation of the elements in the range [first, last) | ||
/// such that when the permutation is applied to the sequence, | ||
/// the result is sorted according to the predicate pred. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// the result is sorted according to the predicate pred. | |
/// the result is sorted in non-descending order. |
/// \param last The end of the input sequence | ||
/// | ||
template <typename RAIterator> | ||
std::vector<size_t> indirect_sort (RAIterator first, RAIterator last) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::vector<size_t> indirect_sort (RAIterator first, RAIterator last) { | |
Permutation indirect_sort (RAIterator first, RAIterator last) { |
test/indirect_sort_test.cpp
Outdated
|
||
|
||
void test_sort () { | ||
BOOST_CXX14_CONSTEXPR int num[] = { 1,3,5,7,9, 2, 4, 6, 8, 10 }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BOOST_CXX14_CONSTEXPR int num[] = { 1,3,5,7,9, 2, 4, 6, 8, 10 }; | |
int num[] = { 1,3,5,7,9, 2, 4, 6, 8, 10 }; |
or int *first = &num[0];
is invalid isn't it?
test/indirect_sort_test.cpp
Outdated
|
||
// A permutation of size N is a sequence of values in the range [0..N) | ||
// such that no value appears more than once in the permutation. | ||
bool isa_permutation(Permutation p, size_t N) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bool isa_permutation(Permutation p, size_t N) { | |
bool is_a_permutation(Permutation p, size_t N) { |
is more readable.
test/indirect_sort_test.cpp
Outdated
test_one_sort(v.begin(), v.end(), std::greater<int>()); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE( test_main ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why that extra method and not using BOOST_AUTO_TEST_CASE(test_sort)
directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because I expect there to be more test cases in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the whole idea of BOOST_AUTO_TEST_CASE
is that you simply "decorate" each test case with that and NOT have a "main" function. By default it will run each such function sequentially even allowing you to filter test based on their name from the CLI.
-->
BOOST_AUTO_TEST_CASE( test_sort ){
...
}
BOOST_AUTO_TEST_CASE( test_indirect_stable_sort ){
...
}
test/indirect_sort_test.cpp
Outdated
@@ -0,0 +1,100 @@ | |||
/* | |||
Copyright (c) Marshall Clow 2011-2012. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copyright (c) Marshall Clow 2011-2012. | |
Copyright (c) Marshall Clow 2023. |
return ret; | ||
} | ||
|
||
/// \fn indirect_partial_sort (RAIterator first, RAIterator last) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C&P mistake in the signature inside this (and below) docstrings
/// \fn indirect_nth_element (RAIterator first, RAIterator last, Predicate p) | ||
/// \returns a permutation of the elements in the range [first, last) | ||
/// such that when the permutation is applied to the sequence, | ||
/// the result is sorted according to the predicate pred. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar C&P mistake in signature and description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch - thanks!
No description provided.