-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSortWithIndex.hpp
More file actions
59 lines (49 loc) · 1.87 KB
/
Copy pathSortWithIndex.hpp
File metadata and controls
59 lines (49 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef ASU_SORTWITHINDEX
#define ASU_SORTWITHINDEX
#include<iterator>
#include<vector>
#include<algorithm>
#include<ReorderUseIndex.hpp>
/*************************************************
* This C++ template takes the same input as sort
* in the standard library. Returns the index of
* sorted elements of their original position.
*
* Note: must pass cmp. (C++ Can't have default callables?)
* original array is sorted in-place.
*
* intput(s):
* vector<T1>::iterator Begin ---- Sort begin position.
* vector<T1>::iterator End ---- Sort end position.
* T2 cmp ---- callable for comparison.
* const bool &ActualSortIt ---- default: true. If false, input is not sorted.
*
* return(s):
* vector<std::size_t> ans ---- Original index for each element in the sorted array.
* Original[ans[i]]=Sorted[i].
*
* Shule Yu
* Dec 28 2017
*
* Key words: sort, index
*************************************************/
template <typename T1, typename T2=std::less<typename std::iterator_traits<T1>::value_type> >
std::vector<std::size_t> SortWithIndex(T1 Begin, T1 End, T2 cmp=T2(), const bool &ActualSortIt=true) {
// Initialize original index locations.
std::vector<std::size_t> idx;
std::size_t index=0;
for (auto it=Begin;it!=End;++it) idx.push_back(index++);
// Sort indexes based on comparing values.
auto cmp2=[&cmp,&Begin](const std::size_t &i1, const std::size_t &i2) {
return cmp(*(Begin+i1),*(Begin+i2));
};
sort(idx.begin(),idx.end(),cmp2);
// Actually sort the input array.
if (ActualSortIt) ReorderUseIndex(Begin,End,idx);
return idx;
}
template <typename T>
std::vector<std::size_t> SortWithIndex(T Begin, T End, const bool &ActualSortIt) {
return SortWithIndex(Begin, End, std::less<typename std::iterator_traits<T>::value_type> (), ActualSortIt);
}
#endif