Changeset 18145 for trunk/psLib/src/math/psSort.h
- Timestamp:
- Jun 16, 2008, 11:58:43 AM (18 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/math/psSort.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/math/psSort.h
r15713 r18145 30 30 31 31 32 // Use of the PSSORT() macro requiresadditional macros defined:32 // Use of the PSSORT() and PSSELECT() macros require additional macros defined: 33 33 // COMPAREEXPR(A,B): expression to compare element A with element B; true if element A is smaller than B. 34 34 // SWAPFUNC(TYPE,A,B): swap element A with element B; the type is provided so that a temporary variable can … … 74 74 75 75 76 77 // The following algorithm for selection was provided by http://en.wikipedia.org/wiki/Selection_algorithm 78 // (version as of 21 May 2008, at 17:38), and implemented below as PSSELECT(): 79 // 80 // function partition(list, left, right, pivotIndex) 81 // pivotValue := list[pivotIndex] 82 // swap list[pivotIndex] and list[right] // Move pivot to end 83 // storeIndex := left 84 // for i from left to right-1 85 // if list[i] < pivotValue 86 // swap list[storeIndex] and list[i] 87 // storeIndex := storeIndex + 1 88 // swap list[right] and list[storeIndex] // Move pivot to its final place 89 // return storeIndex 90 // 91 // function select(list, k, left, right) 92 // loop 93 // select a pivot value list[pivotIndex] 94 // pivotNewIndex := partition(list, left, right, pivotIndex) 95 // if k = pivotNewIndex 96 // return list[k] 97 // else if k < pivotNewIndex 98 // right := pivotNewIndex-1 99 // else 100 // left := pivotNewIndex+1 101 102 103 // Select the RANK-th element 104 // This macro reorders the array so that the RANK-th element is in the correct position 105 // SIZE: Size of the array 106 // RANK: RANK to move into the correct position 107 // COMPAREEXPR: Macro with expression for comparison 108 // SWAPFUNC: Macro to swap elements 109 // SWAPTYPE: Type for swapping, passed to SWAPFUNC 110 #define PSSELECT(SIZE, RANK, COMPAREEXPR, SWAPFUNC, SWAPTYPE) { \ 111 bool selectContinue = true; /* Continue swapping? */ \ 112 long selectMax = SIZE - 1; /* Maximum index */ \ 113 long selectMin = 0; /* Minimum index */ \ 114 while (selectContinue) { \ 115 long selectPivot = (selectMin + selectMax) >> 1; /* Pivot index */ \ 116 SWAPFUNC(SWAPTYPE, selectPivot, selectMax); /* Move pivot to end */ \ 117 long selectStore = selectMin; /* Index of interest */ \ 118 for (long i = selectMin; i < selectMax; i++) { \ 119 if (COMPAREEXPR(i, selectMax)) { /* Note: comparing with the original pivot */ \ 120 SWAPFUNC(SWAPTYPE, selectStore, i); \ 121 selectStore++; \ 122 } \ 123 } \ 124 SWAPFUNC(SWAPTYPE, selectMax, selectStore); /* Move pivot to its final place */ \ 125 if (selectStore == RANK) { \ 126 selectContinue = false; /* Done */ \ 127 } else if (selectStore > RANK) { \ 128 selectMax = selectStore - 1; \ 129 } else { \ 130 selectMin = selectStore + 1; \ 131 } \ 132 } \ 133 } 134 135 136 76 137 #endif
Note:
See TracChangeset
for help on using the changeset viewer.
