]>
Commit | Line | Data |
---|---|---|
4983ca93 | 1 | /** @file\r |
2 | Library used for sorting routines.\r | |
3 | \r | |
5a0fe66e | 4 | Copyright (c) 2009 - 2011, 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 | |
4ff7e37b ED |
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 | |
4983ca93 | 44 | **/\r |
45 | VOID\r | |
46 | EFIAPI\r | |
47 | QuickSortWorker (\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 | |
5dcb5355 | 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 | |
4983ca93 | 120 | \r |
5dcb5355 | 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 | |
4983ca93 | 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 | |
4ff7e37b ED |
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 | |
4983ca93 | 148 | **/\r |
149 | VOID\r | |
150 | EFIAPI\r | |
151 | PerformQuickSort (\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 | |
125c2cf4 | 156 | )\r |
157 | {\r | |
4983ca93 | 158 | VOID *Buffer;\r |
159 | \r | |
160 | ASSERT(BufferToSort != NULL);\r | |
161 | ASSERT(CompareFunction != NULL);\r | |
162 | \r | |
5a0fe66e | 163 | Buffer = AllocateZeroPool(ElementSize);\r |
4983ca93 | 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 | |
125c2cf4 | 176 | \r |
177 | /**\r | |
178 | Not supported in Base version.\r | |
1e6e84c7 | 179 | \r |
a405b86d | 180 | @param[in] Buffer1 Ignored.\r |
181 | @param[in] Buffer2 Ignored.\r | |
182 | \r | |
125c2cf4 | 183 | ASSERT and return 0.\r |
184 | **/\r | |
185 | INTN\r | |
21ecdf15 | 186 | EFIAPI\r |
125c2cf4 | 187 | DevicePathCompare (\r |
b3011f40 | 188 | IN CONST VOID *Buffer1,\r |
189 | IN CONST VOID *Buffer2\r | |
125c2cf4 | 190 | )\r |
191 | {\r | |
192 | ASSERT(FALSE);\r | |
193 | return 0;\r | |
70328967 | 194 | }\r |
11d2decf | 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 | |
1e6e84c7 | 204 | @return > 0 Buffer1 is greater than Buffer2.\r |
11d2decf | 205 | **/\r |
206 | INTN\r | |
207 | EFIAPI\r | |
208 | StringNoCaseCompare (\r | |
b3011f40 | 209 | IN CONST VOID *Buffer1,\r |
210 | IN CONST VOID *Buffer2\r | |
11d2decf | 211 | )\r |
212 | {\r | |
213 | ASSERT(FALSE);\r | |
214 | return 0;\r | |
215 | }\r | |
216 | \r | |
217 | \r | |
a405b86d | 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 | |
228 | INTN\r | |
229 | EFIAPI\r | |
230 | StringCompare (\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 |