zend_sort.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Xinchen Hui <laruence@php.net> |
  16. | Sterling Hughes <sterling@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "zend.h"
  20. #include "zend_sort.h"
  21. #include <limits.h>
  22. static inline void zend_sort_2(void *a, void *b, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
  23. if (cmp(a, b) > 0) {
  24. swp(a, b);
  25. }
  26. }
  27. /* }}} */
  28. static inline void zend_sort_3(void *a, void *b, void *c, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
  29. if (!(cmp(a, b) > 0)) {
  30. if (!(cmp(b, c) > 0)) {
  31. return;
  32. }
  33. swp(b, c);
  34. if (cmp(a, b) > 0) {
  35. swp(a, b);
  36. }
  37. return;
  38. }
  39. if (!(cmp(c, b) > 0)) {
  40. swp(a, c);
  41. return;
  42. }
  43. swp(a, b);
  44. if (cmp(b, c) > 0) {
  45. swp(b, c);
  46. }
  47. }
  48. /* }}} */
  49. static void zend_sort_4(void *a, void *b, void *c, void *d, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
  50. zend_sort_3(a, b, c, cmp, swp);
  51. if (cmp(c, d) > 0) {
  52. swp(c, d);
  53. if (cmp(b, c) > 0) {
  54. swp(b, c);
  55. if (cmp(a, b) > 0) {
  56. swp(a, b);
  57. }
  58. }
  59. }
  60. }
  61. /* }}} */
  62. static void zend_sort_5(void *a, void *b, void *c, void *d, void *e, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
  63. zend_sort_4(a, b, c, d, cmp, swp);
  64. if (cmp(d, e) > 0) {
  65. swp(d, e);
  66. if (cmp(c, d) > 0) {
  67. swp(c, d);
  68. if (cmp(b, c) > 0) {
  69. swp(b, c);
  70. if (cmp(a, b) > 0) {
  71. swp(a, b);
  72. }
  73. }
  74. }
  75. }
  76. }
  77. /* }}} */
  78. ZEND_API void zend_insert_sort(void *base, size_t nmemb, size_t siz, compare_func_t cmp, swap_func_t swp) /* {{{ */{
  79. switch (nmemb) {
  80. case 0:
  81. case 1:
  82. break;
  83. case 2:
  84. zend_sort_2(base, (char *)base + siz, cmp, swp);
  85. break;
  86. case 3:
  87. zend_sort_3(base, (char *)base + siz, (char *)base + siz + siz, cmp, swp);
  88. break;
  89. case 4:
  90. {
  91. size_t siz2 = siz + siz;
  92. zend_sort_4(base, (char *)base + siz, (char *)base + siz2, (char *)base + siz + siz2, cmp, swp);
  93. }
  94. break;
  95. case 5:
  96. {
  97. size_t siz2 = siz + siz;
  98. zend_sort_5(base, (char *)base + siz, (char *)base + siz2, (char *)base + siz + siz2, (char *)base + siz2 + siz2, cmp, swp);
  99. }
  100. break;
  101. default:
  102. {
  103. char *i, *j, *k;
  104. char *start = (char *)base;
  105. char *end = start + (nmemb * siz);
  106. size_t siz2= siz + siz;
  107. char *sentry = start + (6 * siz);
  108. for (i = start + siz; i < sentry; i += siz) {
  109. j = i - siz;
  110. if (!(cmp(j, i) > 0)) {
  111. continue;
  112. }
  113. while (j != start) {
  114. j -= siz;
  115. if (!(cmp(j, i) > 0)) {
  116. j += siz;
  117. break;
  118. }
  119. }
  120. for (k = i; k > j; k -= siz) {
  121. swp(k, k - siz);
  122. }
  123. }
  124. for (i = sentry; i < end; i += siz) {
  125. j = i - siz;
  126. if (!(cmp(j, i) > 0)) {
  127. continue;
  128. }
  129. do {
  130. j -= siz2;
  131. if (!(cmp(j, i) > 0)) {
  132. j += siz;
  133. if (!(cmp(j, i) > 0)) {
  134. j += siz;
  135. }
  136. break;
  137. }
  138. if (j == start) {
  139. break;
  140. }
  141. if (j == start + siz) {
  142. j -= siz;
  143. if (cmp(i, j) > 0) {
  144. j += siz;
  145. }
  146. break;
  147. }
  148. } while (1);
  149. for (k = i; k > j; k -= siz) {
  150. swp(k, k - siz);
  151. }
  152. }
  153. }
  154. break;
  155. }
  156. }
  157. /* }}} */
  158. /* {{{ ZEND_API void zend_sort(void *base, size_t nmemb, size_t siz, compare_func_t cmp, swap_func_t swp)
  159. *
  160. * Derived from LLVM's libc++ implementation of std::sort.
  161. *
  162. * ===========================================================================
  163. * libc++ License
  164. * ===========================================================================
  165. *
  166. * The libc++ library is dual licensed under both the University of Illinois
  167. * "BSD-Like" license and the MIT license. As a user of this code you may
  168. * choose to use it under either license. As a contributor, you agree to allow
  169. * your code to be used under both.
  170. *
  171. * Full text of the relevant licenses is included below.
  172. *
  173. * ===========================================================================
  174. *
  175. * University of Illinois/NCSA
  176. * Open Source License
  177. *
  178. * Copyright (c) 2009-2012 by the contributors listed at
  179. * http://llvm.org/svn/llvm-project/libcxx/trunk/CREDITS.TXT
  180. *
  181. * All rights reserved.
  182. *
  183. * Developed by:
  184. *
  185. * LLVM Team
  186. *
  187. * University of Illinois at Urbana-Champaign
  188. *
  189. * http://llvm.org
  190. *
  191. * Permission is hereby granted, free of charge, to any person obtaining a copy
  192. * of this software and associated documentation files (the "Software"), to
  193. * deal with the Software without restriction, including without limitation the
  194. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  195. * sell copies of the Software, and to permit persons to whom the Software is
  196. * furnished to do so, subject to the following conditions:
  197. *
  198. * * Redistributions of source code must retain the above copyright notice,
  199. * this list of conditions and the following disclaimers.
  200. *
  201. * * Redistributions in binary form must reproduce the above copyright
  202. * notice, this list of conditions and the following disclaimers in the
  203. * documentation and/or other materials provided with the distribution.
  204. *
  205. * * Neither the names of the LLVM Team, University of Illinois at
  206. * Urbana-Champaign, nor the names of its contributors may be used to
  207. * endorse or promote products derived from this Software without
  208. * specific prior written permission.
  209. *
  210. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  211. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  212. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  213. * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  214. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  215. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  216. * WITH THE SOFTWARE.
  217. *
  218. * ===========================================================================
  219. *
  220. * Copyright (c) 2009-2012 by the contributors listed at
  221. * http://llvm.org/svn/llvm-project/libcxx/trunk/CREDITS.TXT
  222. *
  223. * Permission is hereby granted, free of charge, to any person obtaining a copy
  224. * of this software and associated documentation files (the "Software"), to
  225. * deal in the Software without restriction, including without limitation the
  226. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  227. * sell copies of the Software, and to permit persons to whom the Software is
  228. * furnished to do so, subject to the following conditions:
  229. *
  230. * The above copyright notice and this permission notice shall be included in
  231. * all copies or substantial portions of the Software.
  232. *
  233. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  234. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  235. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  236. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  237. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  238. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  239. * IN THE SOFTWARE.
  240. */
  241. ZEND_API void zend_sort(void *base, size_t nmemb, size_t siz, compare_func_t cmp, swap_func_t swp)
  242. {
  243. while (1) {
  244. if (nmemb <= 16) {
  245. zend_insert_sort(base, nmemb, siz, cmp, swp);
  246. return;
  247. } else {
  248. char *i, *j;
  249. char *start = (char *)base;
  250. char *end = start + (nmemb * siz);
  251. size_t offset = (nmemb >> Z_L(1));
  252. char *pivot = start + (offset * siz);
  253. if ((nmemb >> Z_L(10))) {
  254. size_t delta = (offset >> Z_L(1)) * siz;
  255. zend_sort_5(start, start + delta, pivot, pivot + delta, end - siz, cmp, swp);
  256. } else {
  257. zend_sort_3(start, pivot, end - siz, cmp, swp);
  258. }
  259. swp(start + siz, pivot);
  260. pivot = start + siz;
  261. i = pivot + siz;
  262. j = end - siz;
  263. while (1) {
  264. while (cmp(pivot, i) > 0) {
  265. i += siz;
  266. if (UNEXPECTED(i == j)) {
  267. goto done;
  268. }
  269. }
  270. j -= siz;
  271. if (UNEXPECTED(j == i)) {
  272. goto done;
  273. }
  274. while (cmp(j, pivot) > 0) {
  275. j -= siz;
  276. if (UNEXPECTED(j == i)) {
  277. goto done;
  278. }
  279. }
  280. swp(i, j);
  281. i += siz;
  282. if (UNEXPECTED(i == j)) {
  283. goto done;
  284. }
  285. }
  286. done:
  287. swp(pivot, i - siz);
  288. if ((i - siz) - start < end - i) {
  289. zend_sort(start, (i - start)/siz - 1, siz, cmp, swp);
  290. base = i;
  291. nmemb = (end - i)/siz;
  292. } else {
  293. zend_sort(i, (end - i)/siz, siz, cmp, swp);
  294. nmemb = (i - start)/siz - 1;
  295. }
  296. }
  297. }
  298. }
  299. /* }}} */