| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2010 Apple Inc. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 */ | 25 */ |
| 26 | 26 |
| 27 #ifndef WTF_NonCopyingSort_h | 27 #ifndef WTF_NonCopyingSort_h |
| 28 #define WTF_NonCopyingSort_h | 28 #define WTF_NonCopyingSort_h |
| 29 | 29 |
| 30 namespace WTF { | 30 namespace WTF { |
| 31 | 31 |
| 32 using std::swap; | 32 using std::swap; |
| 33 | 33 |
| 34 template <typename RandomAccessIterator, typename Predicate> | 34 template <typename RandomAccessIterator, typename Predicate> |
| 35 inline void siftDown(RandomAccessIterator array, ptrdiff_t start, ptrdiff_t end,
Predicate compareLess) | 35 inline void siftDown(RandomAccessIterator array, |
| 36 { | 36 ptrdiff_t start, |
| 37 ptrdiff_t root = start; | 37 ptrdiff_t end, |
| 38 Predicate compareLess) { |
| 39 ptrdiff_t root = start; |
| 38 | 40 |
| 39 while (root * 2 + 1 <= end) { | 41 while (root * 2 + 1 <= end) { |
| 40 ptrdiff_t child = root * 2 + 1; | 42 ptrdiff_t child = root * 2 + 1; |
| 41 if (child < end && compareLess(array[child], array[child + 1])) | 43 if (child < end && compareLess(array[child], array[child + 1])) |
| 42 child++; | 44 child++; |
| 43 | 45 |
| 44 if (compareLess(array[root], array[child])) { | 46 if (compareLess(array[root], array[child])) { |
| 45 swap(array[root], array[child]); | 47 swap(array[root], array[child]); |
| 46 root = child; | 48 root = child; |
| 47 } else { | 49 } else { |
| 48 return; | 50 return; |
| 49 } | |
| 50 } | 51 } |
| 52 } |
| 51 } | 53 } |
| 52 | 54 |
| 53 template <typename RandomAccessIterator, typename Predicate> | 55 template <typename RandomAccessIterator, typename Predicate> |
| 54 inline void heapify(RandomAccessIterator array, ptrdiff_t count, Predicate compa
reLess) | 56 inline void heapify(RandomAccessIterator array, |
| 55 { | 57 ptrdiff_t count, |
| 56 ptrdiff_t start = (count - 2) / 2; | 58 Predicate compareLess) { |
| 59 ptrdiff_t start = (count - 2) / 2; |
| 57 | 60 |
| 58 while (start >= 0) { | 61 while (start >= 0) { |
| 59 siftDown(array, start, count - 1, compareLess); | 62 siftDown(array, start, count - 1, compareLess); |
| 60 start--; | 63 start--; |
| 61 } | 64 } |
| 62 } | 65 } |
| 63 | 66 |
| 64 template <typename RandomAccessIterator, typename Predicate> | 67 template <typename RandomAccessIterator, typename Predicate> |
| 65 void heapSort(RandomAccessIterator start, RandomAccessIterator end, Predicate co
mpareLess) | 68 void heapSort(RandomAccessIterator start, |
| 66 { | 69 RandomAccessIterator end, |
| 67 ptrdiff_t count = end - start; | 70 Predicate compareLess) { |
| 68 heapify(start, count, compareLess); | 71 ptrdiff_t count = end - start; |
| 72 heapify(start, count, compareLess); |
| 69 | 73 |
| 70 ptrdiff_t endIndex = count - 1; | 74 ptrdiff_t endIndex = count - 1; |
| 71 while (endIndex > 0) { | 75 while (endIndex > 0) { |
| 72 swap(start[endIndex], start[0]); | 76 swap(start[endIndex], start[0]); |
| 73 siftDown(start, 0, endIndex - 1, compareLess); | 77 siftDown(start, 0, endIndex - 1, compareLess); |
| 74 endIndex--; | 78 endIndex--; |
| 75 } | 79 } |
| 76 } | 80 } |
| 77 | 81 |
| 78 template <typename RandomAccessIterator, typename Predicate> | 82 template <typename RandomAccessIterator, typename Predicate> |
| 79 inline void nonCopyingSort(RandomAccessIterator start, RandomAccessIterator end,
Predicate compareLess) | 83 inline void nonCopyingSort(RandomAccessIterator start, |
| 80 { | 84 RandomAccessIterator end, |
| 81 // heapsort happens to use only swaps, not copies, but the essential thing | 85 Predicate compareLess) { |
| 82 // about this function is the fact that it does not copy, not the specific | 86 // heapsort happens to use only swaps, not copies, but the essential thing |
| 83 // algorithm | 87 // about this function is the fact that it does not copy, not the specific |
| 84 heapSort(start, end, compareLess); | 88 // algorithm |
| 89 heapSort(start, end, compareLess); |
| 85 } | 90 } |
| 86 | 91 |
| 87 } // namespace WTF | 92 } // namespace WTF |
| 88 | 93 |
| 89 using WTF::nonCopyingSort; | 94 using WTF::nonCopyingSort; |
| 90 | 95 |
| 91 #endif // WTF_NonCopyingSort_h | 96 #endif // WTF_NonCopyingSort_h |
| OLD | NEW |