2 Library used for sorting routines.
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved. <BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/BaseLib.h>
11 #include <Library/BaseMemoryLib.h>
12 #include <Library/DebugLib.h>
13 #include <Library/MemoryAllocationLib.h>
14 #include <Library/SortLib.h>
17 Worker function for QuickSorting. This function is identical to PerformQuickSort,
18 except that is uses the pre-allocated buffer so the in place sorting does not need to
19 allocate and free buffers constantly.
21 Each element must be equal sized.
23 if BufferToSort is NULL, then ASSERT.
24 if CompareFunction is NULL, then ASSERT.
25 if Buffer is NULL, then ASSERT.
27 if Count is < 2 then perform no action.
28 if Size is < 1 then perform no action.
30 @param[in, out] BufferToSort on call a Buffer of (possibly sorted) elements
31 on return a buffer of sorted elements
32 @param[in] Count the number of elements in the buffer to sort
33 @param[in] ElementSize Size of an element in bytes
34 @param[in] CompareFunction The function to call to perform the comparison
36 @param[in] Buffer Buffer of size ElementSize for use in swapping
41 IN OUT VOID
*BufferToSort
,
43 IN CONST UINTN ElementSize
,
44 IN SORT_COMPARE CompareFunction
,
50 UINTN NextSwapLocation
;
52 ASSERT(BufferToSort
!= NULL
);
53 ASSERT(CompareFunction
!= NULL
);
54 ASSERT(Buffer
!= NULL
);
65 // pick a pivot (we choose last element)
67 Pivot
= ((UINT8
*)BufferToSort
+((Count
-1)*ElementSize
));
70 // Now get the pivot such that all on "left" are below it
71 // and everything "right" are above it
74 ; LoopCount
< Count
-1
78 // if the element is less than the pivot
80 if (CompareFunction((VOID
*)((UINT8
*)BufferToSort
+((LoopCount
)*ElementSize
)),Pivot
) <= 0){
84 CopyMem (Buffer
, (UINT8
*)BufferToSort
+(NextSwapLocation
*ElementSize
), ElementSize
);
85 CopyMem ((UINT8
*)BufferToSort
+(NextSwapLocation
*ElementSize
), (UINT8
*)BufferToSort
+((LoopCount
)*ElementSize
), ElementSize
);
86 CopyMem ((UINT8
*)BufferToSort
+((LoopCount
)*ElementSize
), Buffer
, ElementSize
);
89 // increment NextSwapLocation
95 // swap pivot to it's final position (NextSwapLocaiton)
97 CopyMem (Buffer
, Pivot
, ElementSize
);
98 CopyMem (Pivot
, (UINT8
*)BufferToSort
+(NextSwapLocation
*ElementSize
), ElementSize
);
99 CopyMem ((UINT8
*)BufferToSort
+(NextSwapLocation
*ElementSize
), Buffer
, ElementSize
);
102 // Now recurse on 2 paritial lists. neither of these will have the 'pivot' element
103 // IE list is sorted left half, pivot element, sorted right half...
105 if (NextSwapLocation
>= 2) {
114 if ((Count
- NextSwapLocation
- 1) >= 2) {
116 (UINT8
*)BufferToSort
+ (NextSwapLocation
+1) * ElementSize
,
117 Count
- NextSwapLocation
- 1,
125 Function to perform a Quick Sort alogrithm on a buffer of comparable elements.
127 Each element must be equal sized.
129 if BufferToSort is NULL, then ASSERT.
130 if CompareFunction is NULL, then ASSERT.
132 if Count is < 2 then perform no action.
133 if Size is < 1 then perform no action.
135 @param[in, out] BufferToSort on call a Buffer of (possibly sorted) elements
136 on return a buffer of sorted elements
137 @param[in] Count the number of elements in the buffer to sort
138 @param[in] ElementSize Size of an element in bytes
139 @param[in] CompareFunction The function to call to perform the comparison
145 IN OUT VOID
*BufferToSort
,
146 IN CONST UINTN Count
,
147 IN CONST UINTN ElementSize
,
148 IN SORT_COMPARE CompareFunction
153 ASSERT(BufferToSort
!= NULL
);
154 ASSERT(CompareFunction
!= NULL
);
156 Buffer
= AllocateZeroPool(ElementSize
);
157 ASSERT(Buffer
!= NULL
);
171 Not supported in Base version.
173 @param[in] Buffer1 Ignored.
174 @param[in] Buffer2 Ignored.
181 IN CONST VOID
*Buffer1
,
182 IN CONST VOID
*Buffer2
190 Not supported in Base version.
192 @param[in] Buffer1 Ignored.
193 @param[in] Buffer2 Ignored.
199 StringNoCaseCompare (
200 IN CONST VOID
*Buffer1
,
201 IN CONST VOID
*Buffer2
210 Not supported in Base version.
212 @param[in] Buffer1 Ignored.
213 @param[in] Buffer2 Ignored.
220 IN CONST VOID
*Buffer1
,
221 IN CONST VOID
*Buffer2