]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/BaseSortLib/BaseSortLib.c
udk2010.up2.shell initial release.
[mirror_edk2.git] / ShellPkg / Library / BaseSortLib / BaseSortLib.c
CommitLineData
4983ca93 1/** @file\r
2 Library used for sorting routines.\r
3\r
a405b86d 4 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved. <BR>\r
1e6e84c7 5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
4983ca93 9\r
1e6e84c7 10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
4983ca93 12\r
13**/\r
14\r
15#include <Uefi.h>\r
16\r
17#include <Library/BaseLib.h>\r
18#include <Library/BaseMemoryLib.h>\r
19#include <Library/DebugLib.h>\r
20#include <Library/MemoryAllocationLib.h>\r
1e6e84c7 21#include <Library/SortLib.h>\r
4983ca93 22\r
23/**\r
1e6e84c7 24 Worker function for QuickSorting. This function is identical to PerformQuickSort,\r
25 except that is uses the pre-allocated buffer so the in place sorting does not need to\r
4983ca93 26 allocate and free buffers constantly.\r
27\r
28 Each element must be equal sized.\r
29\r
30 if BufferToSort is NULL, then ASSERT.\r
31 if CompareFunction is NULL, then ASSERT.\r
32 if Buffer is NULL, then ASSERT.\r
33\r
34 if Count is < 2 then perform no action.\r
35 if Size is < 1 then perform no action.\r
36\r
37 @param[in,out] BufferToSort on call a Buffer of (possibly sorted) elements\r
38 on return a buffer of sorted elements\r
39 @param[in] Count the number of elements in the buffer to sort\r
40 @param[in] ElementSize Size of an element in bytes\r
1e6e84c7 41 @param[in] CompareFunction The function to call to perform the comparison\r
4983ca93 42 of any 2 elements\r
43 @param[in] Buffer Buffer of size ElementSize for use in swapping\r
44**/\r
45VOID\r
46EFIAPI\r
47QuickSortWorker (\r
48 IN OUT VOID *BufferToSort,\r
49 IN CONST UINTN Count,\r
50 IN CONST UINTN ElementSize,\r
51 IN SORT_COMPARE CompareFunction,\r
52 IN VOID *Buffer\r
125c2cf4 53 )\r
54{\r
4983ca93 55 VOID *Pivot;\r
56 UINTN LoopCount;\r
57 UINTN NextSwapLocation;\r
58\r
59 ASSERT(BufferToSort != NULL);\r
60 ASSERT(CompareFunction != NULL);\r
61 ASSERT(Buffer != NULL);\r
62\r
1e6e84c7 63 if ( Count < 2\r
4983ca93 64 || ElementSize < 1\r
a405b86d 65 ){\r
4983ca93 66 return;\r
67 }\r
68\r
69 NextSwapLocation = 0;\r
70\r
71 //\r
72 // pick a pivot (we choose last element)\r
73 //\r
74 Pivot = ((UINT8*)BufferToSort+((Count-1)*ElementSize));\r
75\r
76 //\r
77 // Now get the pivot such that all on "left" are below it\r
78 // and everything "right" are above it\r
79 //\r
80 for ( LoopCount = 0\r
1e6e84c7 81 ; LoopCount < Count -1\r
4983ca93 82 ; LoopCount++\r
a405b86d 83 ){\r
4983ca93 84 //\r
85 // if the element is less than the pivot\r
86 //\r
87 if (CompareFunction((VOID*)((UINT8*)BufferToSort+((LoopCount)*ElementSize)),Pivot) <= 0){\r
88 //\r
1e6e84c7 89 // swap\r
4983ca93 90 //\r
91 CopyMem (Buffer, (UINT8*)BufferToSort+(NextSwapLocation*ElementSize), ElementSize);\r
92 CopyMem ((UINT8*)BufferToSort+(NextSwapLocation*ElementSize), (UINT8*)BufferToSort+((LoopCount)*ElementSize), ElementSize);\r
93 CopyMem ((UINT8*)BufferToSort+((LoopCount)*ElementSize), Buffer, ElementSize);\r
94\r
95 //\r
96 // increment NextSwapLocation\r
1e6e84c7 97 //\r
4983ca93 98 NextSwapLocation++;\r
99 }\r
100 }\r
101 //\r
102 // swap pivot to it's final position (NextSwapLocaiton)\r
103 //\r
104 CopyMem (Buffer, Pivot, ElementSize);\r
105 CopyMem (Pivot, (UINT8*)BufferToSort+(NextSwapLocation*ElementSize), ElementSize);\r
106 CopyMem ((UINT8*)BufferToSort+(NextSwapLocation*ElementSize), Buffer, ElementSize);\r
107\r
108 //\r
1e6e84c7 109 // Now recurse on 2 paritial lists. neither of these will have the 'pivot' element\r
4983ca93 110 // IE list is sorted left half, pivot element, sorted right half...\r
111 //\r
112 QuickSortWorker(\r
1e6e84c7 113 BufferToSort,\r
114 NextSwapLocation,\r
115 ElementSize,\r
4983ca93 116 CompareFunction,\r
117 Buffer);\r
118\r
119 QuickSortWorker(\r
120 (UINT8 *)BufferToSort + (NextSwapLocation+1) * ElementSize,\r
1e6e84c7 121 Count - NextSwapLocation - 1,\r
122 ElementSize,\r
4983ca93 123 CompareFunction,\r
124 Buffer);\r
125\r
126 return;\r
127}\r
128/**\r
129 Function to perform a Quick Sort alogrithm on a buffer of comparable elements.\r
130\r
131 Each element must be equal sized.\r
132\r
133 if BufferToSort is NULL, then ASSERT.\r
134 if CompareFunction is NULL, then ASSERT.\r
135\r
136 if Count is < 2 then perform no action.\r
137 if Size is < 1 then perform no action.\r
138\r
139 @param[in,out] BufferToSort on call a Buffer of (possibly sorted) elements\r
140 on return a buffer of sorted elements\r
141 @param[in] Count the number of elements in the buffer to sort\r
142 @param[in] ElementSize Size of an element in bytes\r
1e6e84c7 143 @param[in] CompareFunction The function to call to perform the comparison\r
4983ca93 144 of any 2 elements\r
145**/\r
146VOID\r
147EFIAPI\r
148PerformQuickSort (\r
149 IN OUT VOID *BufferToSort,\r
150 IN CONST UINTN Count,\r
151 IN CONST UINTN ElementSize,\r
152 IN SORT_COMPARE CompareFunction\r
125c2cf4 153 )\r
154{\r
4983ca93 155 VOID *Buffer;\r
156\r
157 ASSERT(BufferToSort != NULL);\r
158 ASSERT(CompareFunction != NULL);\r
159\r
160 Buffer = AllocatePool(ElementSize);\r
161 ASSERT(Buffer != NULL);\r
162\r
163 QuickSortWorker(\r
164 BufferToSort,\r
165 Count,\r
166 ElementSize,\r
167 CompareFunction,\r
168 Buffer);\r
169\r
170 FreePool(Buffer);\r
171 return;\r
172}\r
125c2cf4 173\r
174/**\r
175 Not supported in Base version.\r
1e6e84c7 176\r
a405b86d 177 @param[in] Buffer1 Ignored.\r
178 @param[in] Buffer2 Ignored.\r
179\r
125c2cf4 180 ASSERT and return 0.\r
181**/\r
182INTN\r
21ecdf15 183EFIAPI\r
125c2cf4 184DevicePathCompare (\r
b3011f40 185 IN CONST VOID *Buffer1,\r
186 IN CONST VOID *Buffer2\r
125c2cf4 187 )\r
188{\r
189 ASSERT(FALSE);\r
190 return 0;\r
70328967 191}\r
11d2decf 192\r
193/**\r
194 Function to compare 2 strings without regard to case of the characters.\r
195\r
196 @param[in] Buffer1 Pointer to String to compare.\r
197 @param[in] Buffer2 Pointer to second String to compare.\r
198\r
199 @retval 0 Buffer1 equal to Buffer2.\r
200 @return < 0 Buffer1 is less than Buffer2.\r
1e6e84c7 201 @return > 0 Buffer1 is greater than Buffer2.\r
11d2decf 202**/\r
203INTN\r
204EFIAPI\r
205StringNoCaseCompare (\r
b3011f40 206 IN CONST VOID *Buffer1,\r
207 IN CONST VOID *Buffer2\r
11d2decf 208 )\r
209{\r
210 ASSERT(FALSE);\r
211 return 0;\r
212}\r
213\r
214\r
a405b86d 215/**\r
216 Function to compare 2 strings.\r
217\r
218 @param[in] Buffer1 Pointer to String to compare (CHAR16**).\r
219 @param[in] Buffer2 Pointer to second String to compare (CHAR16**).\r
220\r
221 @retval 0 Buffer1 equal to Buffer2.\r
222 @return < 0 Buffer1 is less than Buffer2.\r
223 @return > 0 Buffer1 is greater than Buffer2.\r
224**/\r
225INTN\r
226EFIAPI\r
227StringCompare (\r
228 IN CONST VOID *Buffer1,\r
229 IN CONST VOID *Buffer2\r
230 )\r
231{\r
232 ASSERT(FALSE);\r
233 return 0;\r
234}\r
235\r
236\r