zend_sort.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2018 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. #define QSORT_STACK_SIZE (sizeof(size_t) * CHAR_BIT)
  23. ZEND_API void zend_qsort(void *base, size_t nmemb, size_t siz, compare_func_t compare, swap_func_t swp) /* {{{ */
  24. {
  25. void *begin_stack[QSORT_STACK_SIZE];
  26. void *end_stack[QSORT_STACK_SIZE];
  27. register char *begin;
  28. register char *end;
  29. register char *seg1;
  30. register char *seg2;
  31. register char *seg2p;
  32. register int loop;
  33. size_t offset;
  34. begin_stack[0] = (char *) base;
  35. end_stack[0] = (char *) base + ((nmemb - 1) * siz);
  36. for (loop = 0; loop >= 0; --loop) {
  37. begin = begin_stack[loop];
  38. end = end_stack[loop];
  39. while (begin < end) {
  40. offset = (end - begin) >> Z_L(1);
  41. swp(begin, begin + (offset - (offset % siz)));
  42. seg1 = begin + siz;
  43. seg2 = end;
  44. while (1) {
  45. for (; seg1 < seg2 && compare(begin, seg1) > 0;
  46. seg1 += siz);
  47. for (; seg2 >= seg1 && compare(seg2, begin) > 0;
  48. seg2 -= siz);
  49. if (seg1 >= seg2)
  50. break;
  51. swp(seg1, seg2);
  52. seg1 += siz;
  53. seg2 -= siz;
  54. }
  55. swp(begin, seg2);
  56. seg2p = seg2;
  57. if ((seg2p - begin) <= (end - seg2p)) {
  58. if ((seg2p + siz) < end) {
  59. begin_stack[loop] = seg2p + siz;
  60. end_stack[loop++] = end;
  61. }
  62. end = seg2p - siz;
  63. }
  64. else {
  65. if ((seg2p - siz) > begin) {
  66. begin_stack[loop] = begin;
  67. end_stack[loop++] = seg2p - siz;
  68. }
  69. begin = seg2p + siz;
  70. }
  71. }
  72. }
  73. }
  74. /* }}} */
  75. static inline void zend_sort_2(void *a, void *b, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
  76. if (cmp(a, b) > 0) {
  77. swp(a, b);
  78. }
  79. }
  80. /* }}} */
  81. static inline void zend_sort_3(void *a, void *b, void *c, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
  82. if (!(cmp(a, b) > 0)) {
  83. if (!(cmp(b, c) > 0)) {
  84. return;
  85. }
  86. swp(b, c);
  87. if (cmp(a, b) > 0) {
  88. swp(a, b);
  89. }
  90. return;
  91. }
  92. if (!(cmp(c, b) > 0)) {
  93. swp(a, c);
  94. return;
  95. }
  96. swp(a, b);
  97. if (cmp(b, c) > 0) {
  98. swp(b, c);
  99. }
  100. }
  101. /* }}} */
  102. static void zend_sort_4(void *a, void *b, void *c, void *d, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
  103. zend_sort_3(a, b, c, cmp, swp);
  104. if (cmp(c, d) > 0) {
  105. swp(c, d);
  106. if (cmp(b, c) > 0) {
  107. swp(b, c);
  108. if (cmp(a, b) > 0) {
  109. swp(a, b);
  110. }
  111. }
  112. }
  113. }
  114. /* }}} */
  115. static void zend_sort_5(void *a, void *b, void *c, void *d, void *e, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
  116. zend_sort_4(a, b, c, d, cmp, swp);
  117. if (cmp(d, e) > 0) {
  118. swp(d, e);
  119. if (cmp(c, d) > 0) {
  120. swp(c, d);
  121. if (cmp(b, c) > 0) {
  122. swp(b, c);
  123. if (cmp(a, b) > 0) {
  124. swp(a, b);
  125. }
  126. }
  127. }
  128. }
  129. }
  130. /* }}} */
  131. ZEND_API void zend_insert_sort(void *base, size_t nmemb, size_t siz, compare_func_t cmp, swap_func_t swp) /* {{{ */{
  132. switch (nmemb) {
  133. case 0:
  134. case 1:
  135. break;
  136. case 2:
  137. zend_sort_2(base, (char *)base + siz, cmp, swp);
  138. break;
  139. case 3:
  140. zend_sort_3(base, (char *)base + siz, (char *)base + siz + siz, cmp, swp);
  141. break;
  142. case 4:
  143. {
  144. size_t siz2 = siz + siz;
  145. zend_sort_4(base, (char *)base + siz, (char *)base + siz2, (char *)base + siz + siz2, cmp, swp);
  146. }
  147. break;
  148. case 5:
  149. {
  150. size_t siz2 = siz + siz;
  151. zend_sort_5(base, (char *)base + siz, (char *)base + siz2, (char *)base + siz + siz2, (char *)base + siz2 + siz2, cmp, swp);
  152. }
  153. break;
  154. default:
  155. {
  156. char *i, *j, *k;
  157. char *start = (char *)base;
  158. char *end = start + (nmemb * siz);
  159. size_t siz2= siz + siz;
  160. char *sentry = start + (6 * siz);
  161. for (i = start + siz; i < sentry; i += siz) {
  162. j = i - siz;
  163. if (!(cmp(j, i) > 0)) {
  164. continue;
  165. }
  166. while (j != start) {
  167. j -= siz;
  168. if (!(cmp(j, i) > 0)) {
  169. j += siz;
  170. break;
  171. }
  172. }
  173. for (k = i; k > j; k -= siz) {
  174. swp(k, k - siz);
  175. }
  176. }
  177. for (i = sentry; i < end; i += siz) {
  178. j = i - siz;
  179. if (!(cmp(j, i) > 0)) {
  180. continue;
  181. }
  182. do {
  183. j -= siz2;
  184. if (!(cmp(j, i) > 0)) {
  185. j += siz;
  186. if (!(cmp(j, i) > 0)) {
  187. j += siz;
  188. }
  189. break;
  190. }
  191. if (j == start) {
  192. break;
  193. }
  194. if (j == start + siz) {
  195. j -= siz;
  196. if (cmp(i, j) > 0) {
  197. j += siz;
  198. }
  199. break;
  200. }
  201. } while (1);
  202. for (k = i; k > j; k -= siz) {
  203. swp(k, k - siz);
  204. }
  205. }
  206. }
  207. break;
  208. }
  209. }
  210. /* }}} */
  211. /* {{{ ZEND_API void zend_sort(void *base, size_t nmemb, size_t siz, compare_func_t cmp, swap_func_t swp)
  212. *
  213. * Derived from LLVM's libc++ implementation of std::sort.
  214. *
  215. * ===========================================================================
  216. * libc++ License
  217. * ===========================================================================
  218. *
  219. * The libc++ library is dual licensed under both the University of Illinois
  220. * "BSD-Like" license and the MIT license. As a user of this code you may
  221. * choose to use it under either license. As a contributor, you agree to allow
  222. * your code to be used under both.
  223. *
  224. * Full text of the relevant licenses is included below.
  225. *
  226. * ===========================================================================
  227. *
  228. * University of Illinois/NCSA
  229. * Open Source License
  230. *
  231. * Copyright (c) 2009-2012 by the contributors listed at
  232. * http://llvm.org/svn/llvm-project/libcxx/trunk/CREDITS.TXT
  233. *
  234. * All rights reserved.
  235. *
  236. * Developed by:
  237. *
  238. * LLVM Team
  239. *
  240. * University of Illinois at Urbana-Champaign
  241. *
  242. * http://llvm.org
  243. *
  244. * Permission is hereby granted, free of charge, to any person obtaining a copy
  245. * of this software and associated documentation files (the "Software"), to
  246. * deal with the Software without restriction, including without limitation the
  247. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  248. * sell copies of the Software, and to permit persons to whom the Software is
  249. * furnished to do so, subject to the following conditions:
  250. *
  251. * * Redistributions of source code must retain the above copyright notice,
  252. * this list of conditions and the following disclaimers.
  253. *
  254. * * Redistributions in binary form must reproduce the above copyright
  255. * notice, this list of conditions and the following disclaimers in the
  256. * documentation and/or other materials provided with the distribution.
  257. *
  258. * * Neither the names of the LLVM Team, University of Illinois at
  259. * Urbana-Champaign, nor the names of its contributors may be used to
  260. * endorse or promote products derived from this Software without
  261. * specific prior written permission.
  262. *
  263. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  264. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  265. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  266. * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  267. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  268. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  269. * WITH THE SOFTWARE.
  270. *
  271. * ===========================================================================
  272. *
  273. * Copyright (c) 2009-2012 by the contributors listed at
  274. * http://llvm.org/svn/llvm-project/libcxx/trunk/CREDITS.TXT
  275. *
  276. * Permission is hereby granted, free of charge, to any person obtaining a copy
  277. * of this software and associated documentation files (the "Software"), to
  278. * deal in the Software without restriction, including without limitation the
  279. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  280. * sell copies of the Software, and to permit persons to whom the Software is
  281. * furnished to do so, subject to the following conditions:
  282. *
  283. * The above copyright notice and this permission notice shall be included in
  284. * all copies or substantial portions of the Software.
  285. *
  286. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  287. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  288. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  289. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  290. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  291. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  292. * IN THE SOFTWARE.
  293. */
  294. ZEND_API void zend_sort(void *base, size_t nmemb, size_t siz, compare_func_t cmp, swap_func_t swp)
  295. {
  296. while (1) {
  297. if (nmemb <= 16) {
  298. zend_insert_sort(base, nmemb, siz, cmp, swp);
  299. return;
  300. } else {
  301. char *i, *j;
  302. char *start = (char *)base;
  303. char *end = start + (nmemb * siz);
  304. size_t offset = (nmemb >> Z_L(1));
  305. char *pivot = start + (offset * siz);
  306. if ((nmemb >> Z_L(10))) {
  307. size_t delta = (offset >> Z_L(1)) * siz;
  308. zend_sort_5(start, start + delta, pivot, pivot + delta, end - siz, cmp, swp);
  309. } else {
  310. zend_sort_3(start, pivot, end - siz, cmp, swp);
  311. }
  312. swp(start + siz, pivot);
  313. pivot = start + siz;
  314. i = pivot + siz;
  315. j = end - siz;
  316. while (1) {
  317. while (cmp(pivot, i) > 0) {
  318. i += siz;
  319. if (UNEXPECTED(i == j)) {
  320. goto done;
  321. }
  322. }
  323. j -= siz;
  324. if (UNEXPECTED(j == i)) {
  325. goto done;
  326. }
  327. while (cmp(j, pivot) > 0) {
  328. j -= siz;
  329. if (UNEXPECTED(j == i)) {
  330. goto done;
  331. }
  332. }
  333. swp(i, j);
  334. i += siz;
  335. if (UNEXPECTED(i == j)) {
  336. goto done;
  337. }
  338. }
  339. done:
  340. swp(pivot, i - siz);
  341. if ((i - siz) - start < end - i) {
  342. zend_sort(start, (i - start)/siz - 1, siz, cmp, swp);
  343. base = i;
  344. nmemb = (end - i)/siz;
  345. } else {
  346. zend_sort(i, (end - i)/siz, siz, cmp, swp);
  347. nmemb = (i - start)/siz - 1;
  348. }
  349. }
  350. }
  351. }
  352. /* }}} */
  353. /*
  354. * Local Variables:
  355. * c-basic-offset: 4
  356. * tab-width: 4
  357. * End:
  358. * vim600: sw=4 ts=4 fdm=marker
  359. * vim<600: sw=4 ts=4
  360. */