FILENAME 3/4 - delete the old get_fun_path etc.
[hiphop-php.git] / hphp / util / safesort.h
blob0bca187cb360d572ba3a1f50d0abc9377c823381
1 /**
2 * ===========================================================================
3 * libc++ License
4 * ===========================================================================
6 * The libc++ library is dual licensed under both the University of Illinois
7 * "BSD-Like" license and the MIT license. As a user of this code you may
8 * choose to use it under either license. As a contributor, you agree to allow
9 * your code to be used under both.
11 * Full text of the relevant licenses is included below.
13 * ===========================================================================
15 * University of Illinois/NCSA
16 * Open Source License
18 * Copyright (c) 2009-2012 by the contributors listed at
19 * http://llvm.org/svn/llvm-project/libcxx/trunk/CREDITS.TXT
21 * All rights reserved.
23 * Developed by:
25 * LLVM Team
27 * University of Illinois at Urbana-Champaign
29 * http://llvm.org
31 * Permission is hereby granted, free of charge, to any person obtaining a copy
32 * of this software and associated documentation files (the "Software"), to
33 * deal with the Software without restriction, including without limitation the
34 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
35 * sell copies of the Software, and to permit persons to whom the Software is
36 * furnished to do so, subject to the following conditions:
38 * * Redistributions of source code must retain the above copyright notice,
39 * this list of conditions and the following disclaimers.
41 * * Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimers in the
43 * documentation and/or other materials provided with the distribution.
45 * * Neither the names of the LLVM Team, University of Illinois at
46 * Urbana-Champaign, nor the names of its contributors may be used to
47 * endorse or promote products derived from this Software without
48 * specific prior written permission.
50 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
51 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
52 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
53 * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
54 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
55 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
56 * WITH THE SOFTWARE.
58 * ===========================================================================
60 * Copyright (c) 2009-2012 by the contributors listed at
61 * http://llvm.org/svn/llvm-project/libcxx/trunk/CREDITS.TXT
63 * Permission is hereby granted, free of charge, to any person obtaining a copy
64 * of this software and associated documentation files (the "Software"), to
65 * deal in the Software without restriction, including without limitation the
66 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
67 * sell copies of the Software, and to permit persons to whom the Software is
68 * furnished to do so, subject to the following conditions:
70 * The above copyright notice and this permission notice shall be included in
71 * all copies or substantial portions of the Software.
73 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
74 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
75 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
76 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
77 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
78 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
79 * IN THE SOFTWARE.
82 /**
83 * == Safesort ==
85 * The safesort algorithm below is based on LLVM's libc++ implementation
86 * of std::sort.
88 * One key difference is that safesort is safe to use with a comparator
89 * that does not impose a strict weak ordering on the elements (whereas
90 * std::sort may crash or go into infinite loops for such comparators).
91 * Safesoft is also "exception safe", leaving the array in a consistent
92 * state in the event that the comparator throws. This is important for
93 * HHVM for several reasons. Some of the builtin comparators in PHP do
94 * not impose a strict weak ordereding (ex. SORT_REGULAR over strings).
95 * Also, user code can supply comparators that behave inconsistently or
96 * throw exceptions.
98 * In cases where the comparator does not impose a strict weak ordering
99 * or the comparator throws, no guarantees are made about the order of
100 * the elements produced the sort algorithm, though the algorithm still
101 * upholds a weaker guarantee that the result will be some permutation
102 * of the input.
104 * Another import difference is that safesort assumes the comparator
105 * returns true if the left argument is GREATER than the right argument.
106 * This is the opposite of what the STL's sort implementation does, and
107 * we do it because it helps HHVM be more compatible with existing PHP
108 * programs that (inadvertently) depend on unspecified behavior of the
109 * PHP5 implementation.
112 #pragma once
114 #include <algorithm>
116 namespace HPHP {
117 namespace Sort {
119 template <class GtCompT, class IterT>
120 void sort3(IterT x, IterT y, IterT z, GtCompT gt) {
121 using std::swap;
122 if (!gt(*x, *y)) {
123 if (!gt(*y, *z))
124 return;
125 swap(*y, *z);
126 if (gt(*x, *y)) {
127 swap(*x, *y);
129 return;
131 if (gt(*y, *z)) {
132 swap(*x, *z);
133 return;
135 swap(*x, *y);
136 if (gt(*y, *z)) {
137 swap(*y, *z);
141 template <class GtCompT, class IterT>
142 void sort4(IterT x1, IterT x2, IterT x3, IterT x4, GtCompT gt) {
143 using std::swap;
144 sort3<GtCompT>(x1, x2, x3, gt);
145 if (gt(*x3, *x4)) {
146 swap(*x3, *x4);
147 if (gt(*x2, *x3)) {
148 swap(*x2, *x3);
149 if (gt(*x1, *x2)) {
150 swap(*x1, *x2);
156 template <class GtCompT, class IterT>
157 void sort5(IterT x1, IterT x2, IterT x3, IterT x4, IterT x5, GtCompT gt) {
158 using std::swap;
159 sort4<GtCompT>(x1, x2, x3, x4, gt);
160 if (gt(*x4, *x5)) {
161 swap(*x4, *x5);
162 if (gt(*x3, *x4)) {
163 swap(*x3, *x4);
164 if (gt(*x2, *x3)) {
165 swap(*x2, *x3);
166 if (gt(*x1, *x2)) {
167 swap(*x1, *x2);
174 template <class GtCompT, class IterT>
175 void insertion_sort(IterT first, IterT last, GtCompT gt) {
176 typedef typename std::iterator_traits<IterT>::value_type value_type;
177 typedef typename std::iterator_traits<IterT>::difference_type
178 difference_type;
179 difference_type len = last - first;
180 if (len < 2) {
181 // If there aren't at least 2 elements, we're done
182 return;
184 // Loop over the first six elements
185 IterT i = first;
186 ++i;
187 IterT l = (len < 6) ? last : first+6;
188 for (; i != l; ++i) {
189 IterT j = i;
190 --j;
191 // If this element is not less than the element
192 // immediately before it, then we can leave this
193 // element where it is for now
194 if (!gt(*j, *i))
195 continue;
196 // Scan backward one element at a time looking
197 // for the earliest element that *i is less than
198 for (;;) {
199 if (j == first) {
200 break;
202 IterT k = j;
203 --k;
204 if (!gt(*k, *i)) {
205 break;
207 j = k;
209 value_type t(*i);
210 for (IterT k = i; k != j; --k) {
211 *k = *(k-1);
213 *j = t;
215 // Loop over the remaining elements
216 IterT second = first;
217 ++second;
218 for (; i != last; ++i) {
219 IterT j = i;
220 --j;
221 // If this element is not less than the element
222 // immediately before it, then we can leave this
223 // element where it is for now
224 if (!gt(*j, *i))
225 continue;
226 // Scan backward two elements at a time looking
227 // for the earliest element that *i is less than
228 for (;;) {
229 // Invariant: j >= first && *i < *j
230 if (j <= second) {
231 // j points to first or second, so we have
232 // reached the end of the loop
233 if (j == second) {
234 // If j points to second, we need to test
235 // if *i is less than *first
236 IterT m = j;
237 --m;
238 if (gt(*m, *i)) {
239 j = m;
242 break;
244 // Move backward by two
245 IterT k = j-2;
246 if (!gt(*k, *i)) {
247 // If (*i < *k) is false, we know that *(k+1) or
248 // *(k+2) is the element we are looking for.
249 IterT m = k;
250 ++m;
251 if (gt(*m, *i)) {
252 j = m;
254 break;
256 j = k;
258 // Move *i to temporary t, move the elements in the
259 // range [j,i) over to the right one position, and
260 // then move t to *j
261 value_type t(*i);
262 for (IterT m = i; m != j; --m) {
263 *m = *(m-1);
265 *j = t;
269 template <class GtCompT, class IterT>
270 void sort(IterT first, IterT last, GtCompT gt) {
271 typedef typename std::iterator_traits<IterT>::difference_type
272 difference_type;
273 using std::swap;
274 while (true) {
275 difference_type len = last - first;
276 // For small numbers of elements, use insertion sort
277 if (len <= 16) {
278 insertion_sort<GtCompT>(first, last, gt);
279 return;
281 // Find a pivot
282 IterT pivot;
284 IterT lm1 = last-1;
285 difference_type delta = len/2;
286 pivot = first + delta;
287 if (len >= 1000) {
288 // Compute the median of 5
289 delta /= 2;
290 sort5<GtCompT>(first, first + delta, pivot, pivot+delta, lm1, gt);
291 } else {
292 // Compute the median of 3
293 sort3<GtCompT>(first, pivot, lm1, gt);
295 // Temporarily move the pivot to the second position
296 swap(*(first+1), *pivot);
297 pivot = first+1;
299 // Split the elements into two partitions (excluding the pivot);
300 // we don't have to inspect the first element and last element
301 // because they've already been put in the right place by the
302 // call to sort3/sort5 above
303 IterT i = first+2;
304 IterT j = last-1;
305 for (;;) {
306 while (gt(*pivot, *i)) {
307 ++i;
308 if (UNLIKELY(i == j)) {
309 goto done;
312 --j;
313 if (UNLIKELY(i == j)) {
314 goto done;
316 while (gt(*j, *pivot)) {
317 --j;
318 if (UNLIKELY(i == j)) {
319 goto done;
322 swap(*i, *j);
323 ++i;
324 if (UNLIKELY(i == j)) {
325 goto done;
328 done:
329 // Put the pivot in between the left partition and right partition
330 swap(*pivot, *(i-1));
331 // We now have the left partition in [first,i-1) and we have the
332 // right parition in [i,last). Sort smaller partition with recursive
333 // call and sort the larger partition with tail recursion elimination
334 if ((i-1) - first < last - i) {
335 sort<GtCompT>(first, i-1, gt);
336 first = i;
337 } else {
338 sort<GtCompT>(i, last, gt);
339 last = i-1;