JACK tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.7KB

  1. /* -*- Mode: C ; c-basic-offset: 2 -*- */
  2. /*****************************************************************************
  3. *
  4. * list_sort() adapted from linux kernel.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. *****************************************************************************/
  20. #include <assert.h>
  21. #include "list.h"
  22. /* list sort from Mark J Roberts (mjr@znex.org) */
  23. void
  24. __list_sort(
  25. struct list_head *head,
  26. int member_offset,
  27. int (*cmp)(void * a, void * b))
  28. {
  29. struct list_head *p, *q, *e, *list, *tail, *oldhead;
  30. int insize, nmerges, psize, qsize, i;
  31. list = head->next;
  32. list_del(head);
  33. insize = 1;
  34. for (;;) {
  35. p = oldhead = list;
  36. list = tail = NULL;
  37. nmerges = 0;
  38. while (p) {
  39. nmerges++;
  40. q = p;
  41. psize = 0;
  42. for (i = 0; i < insize; i++) {
  43. psize++;
  44. q = q->next == oldhead ? NULL : q->next;
  45. if (!q)
  46. break;
  47. }
  48. qsize = insize;
  49. while (psize > 0 || (qsize > 0 && q)) {
  50. if (!psize) {
  51. e = q;
  52. q = q->next;
  53. qsize--;
  54. if (q == oldhead)
  55. q = NULL;
  56. } else if (!qsize || !q) {
  57. e = p;
  58. p = p->next;
  59. psize--;
  60. if (p == oldhead)
  61. p = NULL;
  62. } else if (cmp((void *)p - member_offset, (void *)q - member_offset) <= 0) {
  63. e = p;
  64. p = p->next;
  65. psize--;
  66. if (p == oldhead)
  67. p = NULL;
  68. } else {
  69. e = q;
  70. q = q->next;
  71. qsize--;
  72. if (q == oldhead)
  73. q = NULL;
  74. }
  75. if (tail)
  76. tail->next = e;
  77. else
  78. list = e;
  79. e->prev = tail;
  80. tail = e;
  81. }
  82. p = q;
  83. }
  84. tail->next = list;
  85. list->prev = tail;
  86. if (nmerges <= 1)
  87. break;
  88. insize *= 2;
  89. }
  90. head->next = list;
  91. head->prev = list->prev;
  92. list->prev->next = head;
  93. list->prev = head;
  94. }
  95. struct test_list_el {
  96. int value;
  97. struct list_head test_list_node;
  98. };
  99. int test_list_sort_comparator(struct test_list_el * e1, struct test_list_el * e2)
  100. {
  101. return e1->value - e2->value;
  102. }
  103. void test_list_sort(void)
  104. {
  105. struct list_head test_list;
  106. struct test_list_el *el, *next;
  107. struct test_list_el te1 = {.value = 1};
  108. struct test_list_el te2 = {.value = 2};
  109. struct test_list_el te3 = {.value = 3};
  110. struct test_list_el te4 = {.value = 4};
  111. struct test_list_el te5 = {.value = 5};
  112. struct test_list_el te6 = {.value = 6};
  113. struct test_list_el te7 = {.value = 7};
  114. const int expected[] = {1, 2, 3, 4, 5, 6, 7};
  115. int i;
  116. INIT_LIST_HEAD(&test_list);
  117. list_add_tail(&te2.test_list_node, &test_list);
  118. list_add_tail(&te6.test_list_node, &test_list);
  119. list_add_tail(&te4.test_list_node, &test_list);
  120. list_add_tail(&te5.test_list_node, &test_list);
  121. list_add_tail(&te7.test_list_node, &test_list);
  122. list_add_tail(&te1.test_list_node, &test_list);
  123. list_add_tail(&te3.test_list_node, &test_list);
  124. list_sort(&test_list, struct test_list_el, test_list_node, test_list_sort_comparator);
  125. i = 0;
  126. list_for_each_entry_safe(el, next, &test_list, test_list_node) {
  127. assert(el->value == expected[i]);
  128. i++;
  129. }
  130. }