gim_radixsort.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. #ifndef GIM_RADIXSORT_H_INCLUDED
  2. #define GIM_RADIXSORT_H_INCLUDED
  3. /*! \file gim_radixsort.h
  4. \author Francisco Leon Najera.
  5. Based on the work of Michael Herf : "fast floating-point radix sort"
  6. Avaliable on http://www.stereopsis.com/radix.html
  7. */
  8. /*
  9. -----------------------------------------------------------------------------
  10. This source file is part of GIMPACT Library.
  11. For the latest info, see http://gimpact.sourceforge.net/
  12. Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
  13. email: projectileman@yahoo.com
  14. This library is free software; you can redistribute it and/or
  15. modify it under the terms of EITHER:
  16. (1) The GNU Lesser General Public License as published by the Free
  17. Software Foundation; either version 2.1 of the License, or (at
  18. your option) any later version. The text of the GNU Lesser
  19. General Public License is included with this library in the
  20. file GIMPACT-LICENSE-LGPL.TXT.
  21. (2) The BSD-style license that is included with this library in
  22. the file GIMPACT-LICENSE-BSD.TXT.
  23. (3) The zlib/libpng license that is included with this library in
  24. the file GIMPACT-LICENSE-ZLIB.TXT.
  25. This library is distributed in the hope that it will be useful,
  26. but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
  28. GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
  29. -----------------------------------------------------------------------------
  30. */
  31. #include "gim_memory.h"
  32. ///Macros for sorting.
  33. //! Prototype for comparators
  34. class less_comparator
  35. {
  36. public:
  37. template<class T,class Z>
  38. inline int operator() ( const T& a, const Z& b )
  39. {
  40. return ( a<b?-1:(a>b?1:0));
  41. }
  42. };
  43. //! Prototype for comparators
  44. class integer_comparator
  45. {
  46. public:
  47. template<class T>
  48. inline int operator() ( const T& a, const T& b )
  49. {
  50. return (int)(a-b);
  51. }
  52. };
  53. //!Prototype for getting the integer representation of an object
  54. class uint_key_func
  55. {
  56. public:
  57. template<class T>
  58. inline GUINT operator()( const T& a)
  59. {
  60. return (GUINT)a;
  61. }
  62. };
  63. //!Prototype for copying elements
  64. class copy_elements_func
  65. {
  66. public:
  67. template<class T>
  68. inline void operator()(T& a,T& b)
  69. {
  70. a = b;
  71. }
  72. };
  73. //!Prototype for copying elements
  74. class memcopy_elements_func
  75. {
  76. public:
  77. template<class T>
  78. inline void operator()(T& a,T& b)
  79. {
  80. gim_simd_memcpy(&a,&b,sizeof(T));
  81. }
  82. };
  83. //! @{
  84. struct GIM_RSORT_TOKEN
  85. {
  86. GUINT m_key;
  87. GUINT m_value;
  88. GIM_RSORT_TOKEN()
  89. {
  90. }
  91. GIM_RSORT_TOKEN(const GIM_RSORT_TOKEN& rtoken)
  92. {
  93. m_key = rtoken.m_key;
  94. m_value = rtoken.m_value;
  95. }
  96. inline bool operator <(const GIM_RSORT_TOKEN& other) const
  97. {
  98. return (m_key < other.m_key);
  99. }
  100. inline bool operator >(const GIM_RSORT_TOKEN& other) const
  101. {
  102. return (m_key > other.m_key);
  103. }
  104. };
  105. //! Prototype for comparators
  106. class GIM_RSORT_TOKEN_COMPARATOR
  107. {
  108. public:
  109. inline int operator()( const GIM_RSORT_TOKEN& a, const GIM_RSORT_TOKEN& b )
  110. {
  111. return (int)((a.m_key) - (b.m_key));
  112. }
  113. };
  114. #define kHist 2048
  115. // ---- utils for accessing 11-bit quantities
  116. #define D11_0(x) (x & 0x7FF)
  117. #define D11_1(x) (x >> 11 & 0x7FF)
  118. #define D11_2(x) (x >> 22 )
  119. ///Radix sort for unsigned integer keys
  120. inline void gim_radix_sort_rtokens(
  121. GIM_RSORT_TOKEN * array,
  122. GIM_RSORT_TOKEN * sorted, GUINT element_count)
  123. {
  124. GUINT i;
  125. GUINT b0[kHist * 3];
  126. GUINT *b1 = b0 + kHist;
  127. GUINT *b2 = b1 + kHist;
  128. for (i = 0; i < kHist * 3; ++i)
  129. {
  130. b0[i] = 0;
  131. }
  132. GUINT fi;
  133. GUINT pos;
  134. for (i = 0; i < element_count; ++i)
  135. {
  136. fi = array[i].m_key;
  137. b0[D11_0(fi)] ++;
  138. b1[D11_1(fi)] ++;
  139. b2[D11_2(fi)] ++;
  140. }
  141. {
  142. GUINT sum0 = 0, sum1 = 0, sum2 = 0;
  143. GUINT tsum;
  144. for (i = 0; i < kHist; ++i)
  145. {
  146. tsum = b0[i] + sum0;
  147. b0[i] = sum0 - 1;
  148. sum0 = tsum;
  149. tsum = b1[i] + sum1;
  150. b1[i] = sum1 - 1;
  151. sum1 = tsum;
  152. tsum = b2[i] + sum2;
  153. b2[i] = sum2 - 1;
  154. sum2 = tsum;
  155. }
  156. }
  157. for (i = 0; i < element_count; ++i)
  158. {
  159. fi = array[i].m_key;
  160. pos = D11_0(fi);
  161. pos = ++b0[pos];
  162. sorted[pos].m_key = array[i].m_key;
  163. sorted[pos].m_value = array[i].m_value;
  164. }
  165. for (i = 0; i < element_count; ++i)
  166. {
  167. fi = sorted[i].m_key;
  168. pos = D11_1(fi);
  169. pos = ++b1[pos];
  170. array[pos].m_key = sorted[i].m_key;
  171. array[pos].m_value = sorted[i].m_value;
  172. }
  173. for (i = 0; i < element_count; ++i)
  174. {
  175. fi = array[i].m_key;
  176. pos = D11_2(fi);
  177. pos = ++b2[pos];
  178. sorted[pos].m_key = array[i].m_key;
  179. sorted[pos].m_value = array[i].m_value;
  180. }
  181. }
  182. /// Get the sorted tokens from an array. For generic use. Tokens are IRR_RSORT_TOKEN
  183. /*!
  184. *\param array Array of elements to sort
  185. *\param sorted_tokens Tokens of sorted elements
  186. *\param element_count element count
  187. *\param uintkey_macro Functor which retrieves the integer representation of an array element
  188. */
  189. template<typename T, class GETKEY_CLASS>
  190. void gim_radix_sort_array_tokens(
  191. T* array ,
  192. GIM_RSORT_TOKEN * sorted_tokens,
  193. GUINT element_count,GETKEY_CLASS uintkey_macro)
  194. {
  195. GIM_RSORT_TOKEN * _unsorted = (GIM_RSORT_TOKEN *) gim_alloc(sizeof(GIM_RSORT_TOKEN)*element_count);
  196. for (GUINT _i=0;_i<element_count;++_i)
  197. {
  198. _unsorted[_i].m_key = uintkey_macro(array[_i]);
  199. _unsorted[_i].m_value = _i;
  200. }
  201. gim_radix_sort_rtokens(_unsorted,sorted_tokens,element_count);
  202. gim_free(_unsorted);
  203. gim_free(_unsorted);
  204. }
  205. /// Sorts array in place. For generic use
  206. /*!
  207. \param type Type of the array
  208. \param array
  209. \param element_count
  210. \param get_uintkey_macro Macro for extract the Integer value of the element. Similar to SIMPLE_GET_UINTKEY
  211. \param copy_elements_macro Macro for copy elements, similar to SIMPLE_COPY_ELEMENTS
  212. */
  213. template<typename T, class GETKEY_CLASS, class COPY_CLASS>
  214. void gim_radix_sort(
  215. T * array, GUINT element_count,
  216. GETKEY_CLASS get_uintkey_macro, COPY_CLASS copy_elements_macro)
  217. {
  218. GIM_RSORT_TOKEN * _sorted = (GIM_RSORT_TOKEN *) gim_alloc(sizeof(GIM_RSORT_TOKEN)*element_count);
  219. gim_radix_sort_array_tokens(array,_sorted,element_count,get_uintkey_macro);
  220. T * _original_array = (T *) gim_alloc(sizeof(T)*element_count);
  221. gim_simd_memcpy(_original_array,array,sizeof(T)*element_count);
  222. for (GUINT _i=0;_i<element_count;++_i)
  223. {
  224. copy_elements_macro(array[_i],_original_array[_sorted[_i].m_value]);
  225. }
  226. gim_free(_original_array);
  227. gim_free(_sorted);
  228. }
  229. //! Failsafe Iterative binary search,
  230. /*!
  231. If the element is not found, it returns the nearest upper element position, may be the further position after the last element.
  232. \param _array
  233. \param _start_i the beginning of the array
  234. \param _end_i the ending index of the array
  235. \param _search_key Value to find
  236. \param _comp_macro macro for comparing elements
  237. \param _found If true the value has found. Boolean
  238. \param _result_index the index of the found element, or if not found then it will get the index of the closest bigger value
  239. */
  240. template<class T, typename KEYCLASS, typename COMP_CLASS>
  241. bool gim_binary_search_ex(
  242. const T* _array, GUINT _start_i,
  243. GUINT _end_i,GUINT & _result_index,
  244. const KEYCLASS & _search_key,
  245. COMP_CLASS _comp_macro)
  246. {
  247. GUINT _k;
  248. int _comp_result;
  249. GUINT _i = _start_i;
  250. GUINT _j = _end_i+1;
  251. while (_i < _j)
  252. {
  253. _k = (_j+_i-1)/2;
  254. _comp_result = _comp_macro(_array[_k], _search_key);
  255. if (_comp_result == 0)
  256. {
  257. _result_index = _k;
  258. return true;
  259. }
  260. else if (_comp_result < 0)
  261. {
  262. _i = _k+1;
  263. }
  264. else
  265. {
  266. _j = _k;
  267. }
  268. }
  269. _result_index = _i;
  270. return false;
  271. }
  272. //! Failsafe Iterative binary search,Template version
  273. /*!
  274. If the element is not found, it returns the nearest upper element position, may be the further position after the last element.
  275. \param _array
  276. \param _start_i the beginning of the array
  277. \param _end_i the ending index of the array
  278. \param _search_key Value to find
  279. \param _result_index the index of the found element, or if not found then it will get the index of the closest bigger value
  280. \return true if found, else false
  281. */
  282. template<class T>
  283. bool gim_binary_search(
  284. const T*_array,GUINT _start_i,
  285. GUINT _end_i,const T & _search_key,
  286. GUINT & _result_index)
  287. {
  288. GUINT _i = _start_i;
  289. GUINT _j = _end_i+1;
  290. GUINT _k;
  291. while(_i < _j)
  292. {
  293. _k = (_j+_i-1)/2;
  294. if(_array[_k]==_search_key)
  295. {
  296. _result_index = _k;
  297. return true;
  298. }
  299. else if (_array[_k]<_search_key)
  300. {
  301. _i = _k+1;
  302. }
  303. else
  304. {
  305. _j = _k;
  306. }
  307. }
  308. _result_index = _i;
  309. return false;
  310. }
  311. ///heap sort from http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Sort/Heap/
  312. template <typename T, typename COMP_CLASS>
  313. void gim_down_heap(T *pArr, GUINT k, GUINT n,COMP_CLASS CompareFunc)
  314. {
  315. /* PRE: a[k+1..N] is a heap */
  316. /* POST: a[k..N] is a heap */
  317. T temp = pArr[k - 1];
  318. /* k has child(s) */
  319. while (k <= n/2)
  320. {
  321. int child = 2*k;
  322. if ((child < (int)n) && CompareFunc(pArr[child - 1] , pArr[child])<0)
  323. {
  324. child++;
  325. }
  326. /* pick larger child */
  327. if (CompareFunc(temp , pArr[child - 1])<0)
  328. {
  329. /* move child up */
  330. pArr[k - 1] = pArr[child - 1];
  331. k = child;
  332. }
  333. else
  334. {
  335. break;
  336. }
  337. }
  338. pArr[k - 1] = temp;
  339. } /*downHeap*/
  340. template <typename T, typename COMP_CLASS>
  341. void gim_heap_sort(T *pArr, GUINT element_count, COMP_CLASS CompareFunc)
  342. {
  343. /* sort a[0..N-1], N.B. 0 to N-1 */
  344. GUINT k;
  345. GUINT n = element_count;
  346. for (k = n/2; k > 0; k--)
  347. {
  348. gim_down_heap(pArr, k, n, CompareFunc);
  349. }
  350. /* a[1..N] is now a heap */
  351. while ( n>=2 )
  352. {
  353. gim_swap_elements(pArr,0,n-1); /* largest of a[0..n-1] */
  354. --n;
  355. /* restore a[1..i-1] heap */
  356. gim_down_heap(pArr, 1, n, CompareFunc);
  357. }
  358. }
  359. #endif // GIM_RADIXSORT_H_INCLUDED