redis qsort源码阅读

举报
兔老大 发表于 2021/04/23 00:06:43 2021/04/23
【摘要】 /* The following is the NetBSD libc qsort implementation modified in order to * support partial sorting of ranges for Redis. * * Copyright(C) 2009-2012 Salvatore Sanfilippo. All rights rese...

  
  1. /* The following is the NetBSD libc qsort implementation modified in order to
  2. * support partial sorting of ranges for Redis.
  3. *
  4. * Copyright(C) 2009-2012 Salvatore Sanfilippo. All rights reserved.
  5. *
  6. * The original copyright notice follows. */
  7. /* $NetBSD: qsort.c,v 1.19 2009/01/30 23:38:44 lukem Exp $ */
  8. /*-
  9. * Copyright (c) 1992, 1993
  10. * The Regents of the University of California. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. */
  36. #include <sys/types.h>
  37. #include <errno.h>
  38. #include <stdlib.h>
  39. static inline char *med3 (char *, char *, char *,
  40. int (*)(const void *, const void *));
  41. static inline void swapfunc (char *, char *, size_t, int);
  42. #define min(a, b) (a) < (b) ? a : b
  43. /*
  44. * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
  45. */
  46. #define swapcode(TYPE, parmi, parmj, n) { \
  47. size_t i = (n) / sizeof (TYPE); \
  48. TYPE *pi = (TYPE *)(void *)(parmi); \
  49. TYPE *pj = (TYPE *)(void *)(parmj); \
  50. do { \
  51. TYPE t = *pi; \
  52. *pi++ = *pj; \
  53. *pj++ = t; \
  54. } while (--i > 0); \
  55. }
  56. #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
  57. es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
  58. static inline void
  59. swapfunc(char *a, char *b, size_t n, int swaptype)
  60. {
  61. if (swaptype <= 1)
  62. swapcode(long, a, b, n)
  63. else
  64. swapcode(char, a, b, n)
  65. }
  66. #define swap(a, b) \
  67. if (swaptype == 0) { \
  68. long t = *(long *)(void *)(a); \
  69. *(long *)(void *)(a) = *(long *)(void *)(b); \
  70. *(long *)(void *)(b) = t; \
  71. } else \
  72. swapfunc(a, b, es, swaptype)
  73. #define vecswap(a, b, n) if ((n) > 0) swapfunc((a), (b), (size_t)(n), swaptype)
  74. static inline char *
  75. med3(char *a, char *b, char *c,
  76. int (*cmp) (const void *, const void *))
  77. {
  78. return cmp(a, b) < 0 ?
  79. (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
  80. :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
  81. }
  82. static void
  83. _pqsort(void *a, size_t n, size_t es,
  84. int (*cmp) (const void *, const void *), void *lrange, void *rrange)
  85. {
  86. char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
  87. size_t d, r;
  88. int swaptype, cmp_result;
  89. loop: SWAPINIT(a, es);
  90. if (n < 7) {
  91. for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
  92. for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
  93. pl -= es)
  94. swap(pl, pl - es);
  95. return;
  96. }
  97. pm = (char *) a + (n / 2) * es;
  98. if (n > 7) {
  99. pl = (char *) a;
  100. pn = (char *) a + (n - 1) * es;
  101. if (n > 40) {
  102. d = (n / 8) * es;
  103. pl = med3(pl, pl + d, pl + 2 * d, cmp);
  104. pm = med3(pm - d, pm, pm + d, cmp);
  105. pn = med3(pn - 2 * d, pn - d, pn, cmp);
  106. }
  107. pm = med3(pl, pm, pn, cmp);
  108. }
  109. swap(a, pm);
  110. pa = pb = (char *) a + es;
  111. pc = pd = (char *) a + (n - 1) * es;
  112. for (;;) {
  113. while (pb <= pc && (cmp_result = cmp(pb, a)) <= 0) {
  114. if (cmp_result == 0) {
  115. swap(pa, pb);
  116. pa += es;
  117. }
  118. pb += es;
  119. }
  120. while (pb <= pc && (cmp_result = cmp(pc, a)) >= 0) {
  121. if (cmp_result == 0) {
  122. swap(pc, pd);
  123. pd -= es;
  124. }
  125. pc -= es;
  126. }
  127. if (pb > pc)
  128. break;
  129. swap(pb, pc);
  130. pb += es;
  131. pc -= es;
  132. }
  133. pn = (char *) a + n * es;
  134. r = min(pa - (char *) a, pb - pa);
  135. vecswap(a, pb - r, r);
  136. r = min((size_t)(pd - pc), pn - pd - es);
  137. vecswap(pb, pn - r, r);
  138. if ((r = pb - pa) > es) {
  139. void *_l = a, *_r = ((unsigned char*)a)+r-1;
  140. if (!((lrange < _l && rrange < _l) ||
  141. (lrange > _r && rrange > _r)))
  142. _pqsort(a, r / es, es, cmp, lrange, rrange);
  143. }
  144. if ((r = pd - pc) > es) {
  145. void *_l, *_r;
  146. /* Iterate rather than recurse to save stack space */
  147. a = pn - r;
  148. n = r / es;
  149. _l = a;
  150. _r = ((unsigned char*)a)+r-1;
  151. if (!((lrange < _l && rrange < _l) ||
  152. (lrange > _r && rrange > _r)))
  153. goto loop;
  154. }
  155. /* qsort(pn - r, r / es, es, cmp);*/
  156. }
  157. void
  158. pqsort(void *a, size_t n, size_t es,
  159. int (*cmp) (const void *, const void *), size_t lrange, size_t rrange)
  160. {
  161. _pqsort(a,n,es,cmp,((unsigned char*)a)+(lrange*es),
  162. ((unsigned char*)a)+((rrange+1)*es)-1);
  163. }

 

文章来源: fantianzuo.blog.csdn.net,作者:兔老大RabbitMQ,版权归原作者所有,如需转载,请联系作者。

原文链接:fantianzuo.blog.csdn.net/article/details/102470205

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。