-
Notifications
You must be signed in to change notification settings - Fork 17
/
algos.hpp
188 lines (168 loc) · 5.01 KB
/
algos.hpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Copyright 2015 Georgia Institute of Technology
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file algos.hpp
* @author Nagakishore Jammula <[email protected]>
* @author Patrick Flick <[email protected]>
* @brief Implements some common sequential algorithms
*
*/
#ifndef HPC_ALGOS_H
#define HPC_ALGOS_H
#include <cstdlib>
#include <iterator>
/**
* @brief Calculates the inclusive prefix sum of the given input range.
*
* @param begin An iterator to the beginning of the sequence.
* @param end An iterator to the end of the sequence.
*/
template <typename Iterator>
void prefix_sum(Iterator begin, const Iterator end)
{
// set the total sum to zero
typename std::iterator_traits<Iterator>::value_type sum = 0;
// calculate the inclusive prefix sum
while (begin != end)
{
sum += *begin;
*begin = sum;
++begin;
}
}
/**
* @brief Calculates the exclusive prefix sum of the given input range.
*
* @param begin An iterator to the beginning of the sequence.
* @param end An iterator to the end of the sequence.
*/
template <typename Iterator>
void excl_prefix_sum(Iterator begin, const Iterator end)
{
// set the total sum to zero
typename std::iterator_traits<Iterator>::value_type sum = 0;
typename std::iterator_traits<Iterator>::value_type tmp;
// calculate the exclusive prefix sum
while (begin != end)
{
tmp = sum;
sum += *begin;
*begin = tmp;
++begin;
}
}
/**
* @brief Returns whether the given input range is in sorted order.
*
* @param begin An iterator to the begin of the sequence.
* @param end An iterator to the end of the sequence.
*
* @return Whether the input sequence is sorted.
*/
template <typename Iterator>
bool is_sorted(Iterator begin, Iterator end)
{
if (begin == end)
return true;
typename std::iterator_traits<Iterator>::value_type last = *(begin++);
while (begin != end)
{
if (last > *begin)
return false;
last = *(begin++);
}
return true;
}
/**
* @brief Performs a balanced partitioning.
*
* This function internally does 3-way partitioning (i.e. partitions the
* input sequence into elements of the three classes given by: <, ==, >
*
* After partioning, the elements that are in the `==` class are put into the
* class of `<` or `>`.
*
* This procedure ensures that if the pivot is part of the sequence, then
* the returned left (`<`) and right (`>`) sequences are both at least one
* element in length, i.e. neither is of length zero.
*
* @param begin An iterator to the beginning of the sequence.
* @param end An iterator to the end of the sequence.
* @param pivot A value to be used as pivot.
*
* @return An iterator pointing to the first element of the right (`>`)
* sequence.
*/
template<typename Iterator, typename T>
Iterator balanced_partitioning(Iterator begin, Iterator end, T pivot)
{
if (begin == end)
return begin;
// do 3-way partitioning and then balance the result
Iterator eq_it = begin;
Iterator le_it = begin;
Iterator ge_it = end - 1;
while (true)
{
while (le_it < ge_it && *le_it < pivot) ++le_it;
while (le_it < ge_it && *ge_it > pivot) --ge_it;
if (le_it == ge_it)
{
if (*le_it == pivot)
{
std::swap(*(le_it++), *(eq_it++));
}
else if (*le_it < pivot)
{
le_it++;
}
break;
}
if (le_it > ge_it)
break;
if (*le_it == pivot)
{
std::swap(*(le_it++), *(eq_it++));
} else if (*ge_it == pivot)
{
std::swap(*(ge_it--), *(le_it));
std::swap(*(le_it++), *(eq_it++));
}
else
{
std::swap(*(le_it++), *(ge_it--));
}
}
// if there are pivots, put them to the smaller side
if (eq_it != begin)
{
typedef typename std::iterator_traits<Iterator>::difference_type diff_t;
diff_t left_size = le_it - eq_it;
diff_t right_size = end - le_it;
// if left is bigger, put to right, otherwise just leave where they are
if (left_size > right_size)
{
// put elements to right
while (eq_it > begin)
{
std::swap(*(--le_it), *(--eq_it));
}
}
}
// no pivots, therefore simply return the partitioned sequence
return le_it;
}
#endif // HPC_ALGOS_H