]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/BaseSortLib/BaseSortLib.c
updating headers from code review.
[mirror_edk2.git] / ShellPkg / Library / BaseSortLib / BaseSortLib.c
CommitLineData
4983ca93 1/** @file\r
2 Library used for sorting routines.\r
3\r
4Copyright (c) 2009, Intel Corporation\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT 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
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
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 QuickSortWorker(\r
113 BufferToSort, \r
114 NextSwapLocation, \r
115 ElementSize, \r
116 CompareFunction,\r
117 Buffer);\r
118\r
119 QuickSortWorker(\r
120 (UINT8 *)BufferToSort + (NextSwapLocation+1) * ElementSize,\r
121 Count - NextSwapLocation - 1, \r
122 ElementSize, \r
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
143 @param[in] CompareFunction The function to call to perform the comparison \r
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
176 \r
177 ASSERT and return 0.\r
178**/\r
179INTN\r
180DevicePathCompare (\r
181 IN VOID *Buffer1,\r
182 IN VOID *Buffer2\r
183 )\r
184{\r
185 ASSERT(FALSE);\r
186 return 0;\r
187}