]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/BaseSortLib/BaseSortLib.c
Fix incorrect copyright format
[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
53 ){\r
54 VOID *Pivot;\r
55 UINTN LoopCount;\r
56 UINTN NextSwapLocation;\r
57\r
58 ASSERT(BufferToSort != NULL);\r
59 ASSERT(CompareFunction != NULL);\r
60 ASSERT(Buffer != NULL);\r
61\r
62 if ( Count < 2 \r
63 || ElementSize < 1\r
64 ){\r
65 return;\r
66 }\r
67\r
68 NextSwapLocation = 0;\r
69\r
70 //\r
71 // pick a pivot (we choose last element)\r
72 //\r
73 Pivot = ((UINT8*)BufferToSort+((Count-1)*ElementSize));\r
74\r
75 //\r
76 // Now get the pivot such that all on "left" are below it\r
77 // and everything "right" are above it\r
78 //\r
79 for ( LoopCount = 0\r
80 ; LoopCount < Count -1 \r
81 ; LoopCount++\r
82 ){\r
83 //\r
84 // if the element is less than the pivot\r
85 //\r
86 if (CompareFunction((VOID*)((UINT8*)BufferToSort+((LoopCount)*ElementSize)),Pivot) <= 0){\r
87 //\r
88 // swap \r
89 //\r
90 CopyMem (Buffer, (UINT8*)BufferToSort+(NextSwapLocation*ElementSize), ElementSize);\r
91 CopyMem ((UINT8*)BufferToSort+(NextSwapLocation*ElementSize), (UINT8*)BufferToSort+((LoopCount)*ElementSize), ElementSize);\r
92 CopyMem ((UINT8*)BufferToSort+((LoopCount)*ElementSize), Buffer, ElementSize);\r
93\r
94 //\r
95 // increment NextSwapLocation\r
96 // \r
97 NextSwapLocation++;\r
98 }\r
99 }\r
100 //\r
101 // swap pivot to it's final position (NextSwapLocaiton)\r
102 //\r
103 CopyMem (Buffer, Pivot, ElementSize);\r
104 CopyMem (Pivot, (UINT8*)BufferToSort+(NextSwapLocation*ElementSize), ElementSize);\r
105 CopyMem ((UINT8*)BufferToSort+(NextSwapLocation*ElementSize), Buffer, ElementSize);\r
106\r
107 //\r
108 // Now recurse on 2 paritial lists. neither of these will have the 'pivot' element \r
109 // IE list is sorted left half, pivot element, sorted right half...\r
110 //\r
111 QuickSortWorker(\r
112 BufferToSort, \r
113 NextSwapLocation, \r
114 ElementSize, \r
115 CompareFunction,\r
116 Buffer);\r
117\r
118 QuickSortWorker(\r
119 (UINT8 *)BufferToSort + (NextSwapLocation+1) * ElementSize,\r
120 Count - NextSwapLocation - 1, \r
121 ElementSize, \r
122 CompareFunction,\r
123 Buffer);\r
124\r
125 return;\r
126}\r
127/**\r
128 Function to perform a Quick Sort alogrithm on a buffer of comparable elements.\r
129\r
130 Each element must be equal sized.\r
131\r
132 if BufferToSort is NULL, then ASSERT.\r
133 if CompareFunction is NULL, then ASSERT.\r
134\r
135 if Count is < 2 then perform no action.\r
136 if Size is < 1 then perform no action.\r
137\r
138 @param[in,out] BufferToSort on call a Buffer of (possibly sorted) elements\r
139 on return a buffer of sorted elements\r
140 @param[in] Count the number of elements in the buffer to sort\r
141 @param[in] ElementSize Size of an element in bytes\r
142 @param[in] CompareFunction The function to call to perform the comparison \r
143 of any 2 elements\r
144**/\r
145VOID\r
146EFIAPI\r
147PerformQuickSort (\r
148 IN OUT VOID *BufferToSort,\r
149 IN CONST UINTN Count,\r
150 IN CONST UINTN ElementSize,\r
151 IN SORT_COMPARE CompareFunction\r
152 ){\r
153 VOID *Buffer;\r
154\r
155 ASSERT(BufferToSort != NULL);\r
156 ASSERT(CompareFunction != NULL);\r
157\r
158 Buffer = AllocatePool(ElementSize);\r
159 ASSERT(Buffer != NULL);\r
160\r
161 QuickSortWorker(\r
162 BufferToSort,\r
163 Count,\r
164 ElementSize,\r
165 CompareFunction,\r
166 Buffer);\r
167\r
168 FreePool(Buffer);\r
169 return;\r
170}\r