-
Notifications
You must be signed in to change notification settings - Fork 117
Add 'indirect_sort' #117
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
base: develop
Are you sure you want to change the base?
Add 'indirect_sort' #117
Changes from 1 commit
17c47e8
814f8a5
3ae9ee2
8be54b3
a7ae53d
25ab833
62922bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,71 @@ | ||||||
[/ File indirect_sort.qbk] | ||||||
|
||||||
[section:indirect_sort indirect_sort ] | ||||||
|
||||||
[/license | ||||||
Copyright (c) 2023 Marshall Clow | ||||||
|
||||||
Distributed under the Boost Software License, Version 1.0. | ||||||
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||||||
] | ||||||
|
||||||
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. | ||||||
|
||||||
Say you have a sequence `[first, last)` of 1000 items that are expensive to swap: | ||||||
|
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: |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,83 @@ | ||||||
/* | ||||||
Copyright (c) Marshall Clow 2023. | ||||||
|
||||||
Distributed under the Boost Software License, Version 1.0. (See accompanying | ||||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||||||
|
||||||
*/ | ||||||
|
||||||
/// \file indirect_sort.hpp | ||||||
/// \brief indirect sorting algorithms | ||||||
/// \author Marshall Clow | ||||||
/// | ||||||
|
||||||
#ifndef BOOST_ALGORITHM_IS_INDIRECT_SORT | ||||||
#define BOOST_ALGORITHM_IS_INDIRECT_SORT | ||||||
|
||||||
|
||||||
#include <algorithm> // for std::sort (and others) | ||||||
#include <functional> // for std::less | ||||||
#include <vector> // for std:;vector | ||||||
|
#include <vector> // for std:;vector | |
#include <vector> // for std::vector |
But is that comment really required?
Outdated
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, Pred pred) { | |
Permutation indirect_sort (RAIterator first, RAIterator last, Pred pred) { |
Outdated
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.
/// \fn indirect_sort (RAIterator first, RAIterator las ) | |
/// \fn indirect_sort (RAIterator first, RAIterator last) |
Outdated
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. |
Outdated
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) { |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,100 @@ | ||||||
/* | ||||||
Copyright (c) Marshall Clow 2011-2012. | ||||||
|
Copyright (c) Marshall Clow 2011-2012. | |
Copyright (c) Marshall Clow 2023. |
Outdated
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.
Outdated
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?
Outdated
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 ){
...
}
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:
Are the double-spaces after each sentence intended?