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