-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_algorithms.h
221 lines (172 loc) · 5.21 KB
/
benchmark_algorithms.h
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
Copyright 2007-2008 Adobe Systems Incorporated
Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
or a copy at http://stlab.adobe.com/licenses.html)
Shared source file for algorithms used in multiple benchmark files
*/
namespace benchmark {
/******************************************************************************/
template <typename Iterator>
bool is_sorted(Iterator first, Iterator last) {
Iterator prev = first;
first++;
while (first != last) {
if ( *first++ < *prev++)
return false;
}
return true;
}
/******************************************************************************/
template <typename Iterator, typename T>
void fill(Iterator first, Iterator last, T value) {
while (first != last) *first++ = value;
}
/******************************************************************************/
template <typename Iterator, typename T>
void fill_random(Iterator first, Iterator last) {
while (first != last) {
*first++ = static_cast<T>( rand() );
}
}
/******************************************************************************/
template <class Iterator, class T>
void fill_descending(Iterator first, Iterator last, unsigned count) {
while (first != last) {
*first++ = static_cast<T>( --count );
}
}
/******************************************************************************/
template <typename Iterator1, typename Iterator2>
void copy(Iterator1 firstSource, Iterator1 lastSource, Iterator2 firstDest) {
while (firstSource != lastSource) *(firstDest++) = *(firstSource++);
}
/******************************************************************************/
template <class Iterator, class Swapper>
void reverse(Iterator begin, Iterator end, Swapper doswap)
{
while (begin != end)
{
--end;
if (begin == end)
break;
doswap(begin, end);
++begin;
}
}
/******************************************************************************/
// our accumulator function template, using iterators or pointers
template <typename Iterator, typename Number>
Number accumulate(Iterator first, Iterator last, Number result) {
while (first != last) result = result + *first++;
return result;
}
/******************************************************************************/
template <typename Iterator, typename T>
void insertionSort( Iterator begin, Iterator end )
{
Iterator p = begin;
p++;
while ( p != end ) {
T tmp = *p;
Iterator j = p;
Iterator prev = j;
for ( ; j != begin && tmp < *--prev; --j ) {
*j = *prev;
}
*j = tmp;
p++;
}
}
/******************************************************************************/
template<typename Iterator, typename T>
void quicksort(Iterator begin, Iterator end)
{
if ( (end - begin) > 1 ) {
T middleValue = *begin;
Iterator left = begin;
Iterator right = end;
for(;;) {
while ( middleValue < *(--right) );
if ( !(left < right ) ) break;
while ( *(left) < middleValue )
++left;
if ( !(left < right ) ) break;
// swap
T temp = *right;
*right = *left;
*left = temp;
}
quicksort<Iterator,T>( begin, right + 1 );
quicksort<Iterator,T>( right + 1, end );
}
}
/******************************************************************************/
template<typename Iterator, typename T, class Swapper>
void quicksort(Iterator begin, Iterator end, Swapper doswap)
{
if ( (end - begin) > 1 ) {
T middleValue = *begin;
Iterator left = begin;
Iterator right = end;
for(;;) {
while ( middleValue < *(--right) );
if ( !(left < right ) ) break;
while ( *(left) < middleValue )
++left;
if ( !(left < right ) ) break;
// swap
doswap( right, left );
}
quicksort<Iterator,T, Swapper>( begin, right + 1, doswap );
quicksort<Iterator,T, Swapper>( right + 1, end, doswap );
}
}
/******************************************************************************/
template<typename Iterator, typename T>
void sift_in(ptrdiff_t count, Iterator begin, ptrdiff_t free_in, T next)
{
ptrdiff_t i;
ptrdiff_t free = free_in;
// sift up the free node
for ( i = 2*(free+1); i < count; i += i) {
if ( *(begin+(i-1)) < *(begin+i))
i++;
*(begin + free) = *(begin+(i-1));
free = i-1;
}
// special case in sift up if the last inner node has only 1 child
if (i == count) {
*(begin + free) = *(begin+(i-1));
free = i-1;
}
// sift down the new item next
i = (free-1)/2;
while( (free > free_in) && *(begin+i) < next) {
*(begin + free) = *(begin+i);
free = i;
i = (free-1)/2;
}
*(begin + free) = next;
}
template<typename Iterator, typename T>
void heapsort(Iterator begin, Iterator end)
{
ptrdiff_t j;
ptrdiff_t count = end - begin;
// build the heap structure
for( j = (count / 2) - 1; j >= 0; --j) {
T next = *(begin+j);
sift_in< Iterator, T>(count, begin, j, next);
}
// search next by next remaining extremal element
for( j = count - 1; j >= 1; --j) {
T next = *(begin+j);
*(begin+j) = *(begin);
sift_in< Iterator, T>(j, begin, 0, next);
}
}
} // end namespace benchmark
using namespace benchmark;
/******************************************************************************/
/******************************************************************************/
/******************************************************************************/