]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - ShellPkg/Library/BaseSortLib/BaseSortLib.c
connect - add comments and add input verification
[mirror_edk2.git] / ShellPkg / Library / BaseSortLib / BaseSortLib.c
... / ...
CommitLineData
1/** @file\r
2 Library used for sorting routines.\r
3\r
4 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved. <BR>\r
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
9\r
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
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
21#include <Library/SortLib.h>\r
22\r
23/**\r
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
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
41 @param[in] CompareFunction The function to call to perform the comparison\r
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
53 )\r
54{\r
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
63 if ( Count < 2\r
64 || ElementSize < 1\r
65 ){\r
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
81 ; LoopCount < Count -1\r
82 ; LoopCount++\r
83 ){\r
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
89 // swap\r
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
97 //\r
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
109 // Now recurse on 2 paritial lists. neither of these will have the 'pivot' element\r
110 // IE list is sorted left half, pivot element, sorted right half...\r
111 //\r
112 if (NextSwapLocation >= 2) {\r
113 QuickSortWorker(\r
114 BufferToSort,\r
115 NextSwapLocation,\r
116 ElementSize,\r
117 CompareFunction,\r
118 Buffer);\r
119 }\r
120\r
121 if ((Count - NextSwapLocation - 1) >= 2) {\r
122 QuickSortWorker(\r
123 (UINT8 *)BufferToSort + (NextSwapLocation+1) * ElementSize,\r
124 Count - NextSwapLocation - 1,\r
125 ElementSize,\r
126 CompareFunction,\r
127 Buffer);\r
128 }\r
129 return;\r
130}\r
131/**\r
132 Function to perform a Quick Sort alogrithm on a buffer of comparable elements.\r
133\r
134 Each element must be equal sized.\r
135\r
136 if BufferToSort is NULL, then ASSERT.\r
137 if CompareFunction is NULL, then ASSERT.\r
138\r
139 if Count is < 2 then perform no action.\r
140 if Size is < 1 then perform no action.\r
141\r
142 @param[in,out] BufferToSort on call a Buffer of (possibly sorted) elements\r
143 on return a buffer of sorted elements\r
144 @param[in] Count the number of elements in the buffer to sort\r
145 @param[in] ElementSize Size of an element in bytes\r
146 @param[in] CompareFunction The function to call to perform the comparison\r
147 of any 2 elements\r
148**/\r
149VOID\r
150EFIAPI\r
151PerformQuickSort (\r
152 IN OUT VOID *BufferToSort,\r
153 IN CONST UINTN Count,\r
154 IN CONST UINTN ElementSize,\r
155 IN SORT_COMPARE CompareFunction\r
156 )\r
157{\r
158 VOID *Buffer;\r
159\r
160 ASSERT(BufferToSort != NULL);\r
161 ASSERT(CompareFunction != NULL);\r
162\r
163 Buffer = AllocatePool(ElementSize);\r
164 ASSERT(Buffer != NULL);\r
165\r
166 QuickSortWorker(\r
167 BufferToSort,\r
168 Count,\r
169 ElementSize,\r
170 CompareFunction,\r
171 Buffer);\r
172\r
173 FreePool(Buffer);\r
174 return;\r
175}\r
176\r
177/**\r
178 Not supported in Base version.\r
179\r
180 @param[in] Buffer1 Ignored.\r
181 @param[in] Buffer2 Ignored.\r
182\r
183 ASSERT and return 0.\r
184**/\r
185INTN\r
186EFIAPI\r
187DevicePathCompare (\r
188 IN CONST VOID *Buffer1,\r
189 IN CONST VOID *Buffer2\r
190 )\r
191{\r
192 ASSERT(FALSE);\r
193 return 0;\r
194}\r
195\r
196/**\r
197 Function to compare 2 strings without regard to case of the characters.\r
198\r
199 @param[in] Buffer1 Pointer to String to compare.\r
200 @param[in] Buffer2 Pointer to second String to compare.\r
201\r
202 @retval 0 Buffer1 equal to Buffer2.\r
203 @return < 0 Buffer1 is less than Buffer2.\r
204 @return > 0 Buffer1 is greater than Buffer2.\r
205**/\r
206INTN\r
207EFIAPI\r
208StringNoCaseCompare (\r
209 IN CONST VOID *Buffer1,\r
210 IN CONST VOID *Buffer2\r
211 )\r
212{\r
213 ASSERT(FALSE);\r
214 return 0;\r
215}\r
216\r
217\r
218/**\r
219 Function to compare 2 strings.\r
220\r
221 @param[in] Buffer1 Pointer to String to compare (CHAR16**).\r
222 @param[in] Buffer2 Pointer to second String to compare (CHAR16**).\r
223\r
224 @retval 0 Buffer1 equal to Buffer2.\r
225 @return < 0 Buffer1 is less than Buffer2.\r
226 @return > 0 Buffer1 is greater than Buffer2.\r
227**/\r
228INTN\r
229EFIAPI\r
230StringCompare (\r
231 IN CONST VOID *Buffer1,\r
232 IN CONST VOID *Buffer2\r
233 )\r
234{\r
235 ASSERT(FALSE);\r
236 return 0;\r
237}\r
238\r
239\r