PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: QuickSort.h * 00003 * 00004 * Copyright (C) 2002-2011 The PixelLight Team (http://www.pixellight.org/) 00005 * 00006 * This file is part of PixelLight. 00007 * 00008 * PixelLight is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU Lesser General Public License as published by 00010 * the Free Software Foundation, either version 3 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * PixelLight is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public License 00019 * along with PixelLight. If not, see <http://www.gnu.org/licenses/>. 00020 \*********************************************************/ 00021 00022 00023 #ifndef __PLCORE_QUICKSORT_H__ 00024 #define __PLCORE_QUICKSORT_H__ 00025 #pragma once 00026 00027 00028 //[-------------------------------------------------------] 00029 //[ Includes ] 00030 //[-------------------------------------------------------] 00031 #include "PLCore/PLCore.h" 00032 00033 00034 //[-------------------------------------------------------] 00035 //[ Namespace ] 00036 //[-------------------------------------------------------] 00037 namespace PLCore { 00038 00039 00040 //[-------------------------------------------------------] 00041 //[ Classes ] 00042 //[-------------------------------------------------------] 00043 /** 00044 * @brief 00045 * Quick sort class 00046 * 00047 * @remarks 00048 * @verbatim 00049 * Usage example: 00050 * // Derive your own class from QuickSort to implement the compare function: 00051 * class QuickSortTest : public QuickSort { 00052 * private: 00053 * virtual char Compare(void *pElement1, void *pElement2) const { 00054 * if (*((int*)pElement1) < *((int*)pElement2)) return -1; 00055 * if (*((int*)pElement1) == *((int*)pElement2)) return 0; 00056 * else return 1; 00057 * }; 00058 * }; 00059 * // Sort example using the derived class above: 00060 * QuickSortTest cQuickSort; 00061 * int nTest[10] = {3, 6, 2, 6, 8, 4, 36, 22, 64, 99}; 00062 * cQuickSort.Sort(nTest, 10, sizeof(int)); 00063 * @endverbatim 00064 */ 00065 class QuickSort { 00066 00067 00068 //[-------------------------------------------------------] 00069 //[ Public functions ] 00070 //[-------------------------------------------------------] 00071 public: 00072 /** 00073 * @brief 00074 * Constructor 00075 */ 00076 PLCORE_API QuickSort(); 00077 00078 /** 00079 * @brief 00080 * Destructor 00081 */ 00082 PLCORE_API virtual ~QuickSort(); 00083 00084 /** 00085 * @brief 00086 * Sorts a set of data using the QuickSort algorithm 00087 * 00088 * @param[in, out] pData 00089 * Data to sort (MUST be valid!) 00090 * @param[in] nNumOfElements 00091 * Number of data elements to sort (MUST be valid!) 00092 * @param[in] nElementSize 00093 * Size of an element in bytes (MUST be valid!) 00094 * 00095 * @return 00096 * 'true' if all went fine, else 'false' (invalid data etc.) 00097 */ 00098 PLCORE_API bool Sort(void *pData, uint32 nNumOfElements, uint32 nElementSize); 00099 00100 00101 //[-------------------------------------------------------] 00102 //[ Private functions ] 00103 //[-------------------------------------------------------] 00104 private: 00105 /** 00106 * @brief 00107 * Copy constructor 00108 * 00109 * @param[in] cSource 00110 * Source to copy from 00111 */ 00112 QuickSort(const QuickSort &cSource); 00113 00114 /** 00115 * @brief 00116 * Copy operator 00117 * 00118 * @param[in] cSource 00119 * Source to copy from 00120 * 00121 * @return 00122 * Reference to this instance 00123 */ 00124 QuickSort &operator =(const QuickSort &cSource); 00125 00126 /** 00127 * @brief 00128 * Sorts a set of data recursive using the QuickSort algorithm 00129 * 00130 * @param[in] pLeftStart 00131 * Left start element (always valid!) 00132 * @param[in] pRightStart 00133 * Right start element (always valid!) 00134 */ 00135 void SortRec(uint8 *pLeftStart, uint8 *pRightStart); 00136 00137 00138 //[-------------------------------------------------------] 00139 //[ Private data ] 00140 //[-------------------------------------------------------] 00141 private: 00142 uint8 *m_pData; /**< Data to sort, can be a null pointer */ 00143 uint32 m_nElementSize; /**< Size of an element in bytes */ 00144 uint8 *m_pTemp; /**< Temp buffer for swapping, can be a null pointer */ 00145 00146 00147 //[-------------------------------------------------------] 00148 //[ Virtual private QuickSort functions ] 00149 //[-------------------------------------------------------] 00150 private: 00151 /** 00152 * @brief 00153 * Custom element compare function 00154 * 00155 * @param[in] pElement1 00156 * First element 00157 * @param[in] pElement2 00158 * Second element 00159 * 00160 * @return 00161 * < 0 pElement1 less than pElement2\n 00162 * = 0 pElement1 equivalent to pElement2\n 00163 * > 0 pElement1 greater than pElement2 00164 */ 00165 virtual char Compare(void *pElement1, void *pElement2) const = 0; 00166 00167 00168 }; 00169 00170 00171 //[-------------------------------------------------------] 00172 //[ Namespace ] 00173 //[-------------------------------------------------------] 00174 } // PLCore 00175 00176 00177 #endif // __PLCORE_QUICKSORT_H__
|