Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
ogs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
wenqing
ogs
Commits
ad77893c
Commit
ad77893c
authored
6 years ago
by
Dmitri Naumov
Committed by
Tom Fischer
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
[BL] Generalize quicksort to use a give comparator
parent
70d3573b
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
BaseLib/quicksort.h
+16
-7
16 additions, 7 deletions
BaseLib/quicksort.h
with
16 additions
and
7 deletions
BaseLib/quicksort.h
+
16
−
7
View file @
ad77893c
...
...
@@ -19,11 +19,10 @@
namespace
BaseLib
{
/// @pre {first1 <= last1 and the second iterator can be incremented
/// distance(first1, last1) times}
template
<
typename
It1
,
typename
It2
>
void
quicksort
(
It1
first1
,
It1
last1
,
It2
first2
)
template
<
typename
It1
,
typename
It2
,
typename
Comparator
>
void
quicksort
(
It1
first1
,
It1
last1
,
It2
first2
,
Comparator
compare
)
{
using
T1
=
typename
std
::
iterator_traits
<
It1
>::
value_type
;
using
T2
=
typename
std
::
iterator_traits
<
It2
>::
value_type
;
...
...
@@ -35,10 +34,7 @@ void quicksort(It1 first1, It1 last1, It2 first2)
[](
T1
const
&
t1
,
T2
const
&
t2
)
{
return
std
::
make_pair
(
t1
,
t2
);
});
// Sort data using first element of the pair.
std
::
sort
(
begin
(
data
),
end
(
data
),
[](
std
::
pair
<
T1
,
T2
>
const
&
a
,
std
::
pair
<
T1
,
T2
>
const
&
b
)
{
return
(
a
.
first
<
b
.
first
);
});
std
::
sort
(
begin
(
data
),
end
(
data
),
compare
);
// Unzip sorted data.
for
(
auto
const
&
pair
:
data
)
...
...
@@ -49,6 +45,19 @@ void quicksort(It1 first1, It1 last1, It2 first2)
++
first2
;
}
}
/// @pre {first1 <= last1 and the second iterator can be incremented
/// distance(first1, last1) times}
template
<
typename
It1
,
typename
It2
>
void
quicksort
(
It1
first1
,
It1
last1
,
It2
first2
)
{
using
T1
=
typename
std
::
iterator_traits
<
It1
>::
value_type
;
using
T2
=
typename
std
::
iterator_traits
<
It2
>::
value_type
;
quicksort
(
first1
,
last1
,
first2
,
[](
std
::
pair
<
T1
,
T2
>
const
&
a
,
std
::
pair
<
T1
,
T2
>
const
&
b
)
{
return
a
.
first
<
b
.
first
;
});
}
/// @pre {end<=array.size() and perm.size()==array.size()}
template
<
typename
T1
,
typename
T2
=
std
::
size_t
>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment