Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

131 строка
2.9 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net>
  5. // © 2013—2015 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  6. //
  7. // Lol Engine is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #pragma once
  14. #include <lol/base/array.h>
  15. #include <lol/math/rand.h>
  16. #include <../legacy/lol/math/functions.h> // lol::max
  17. namespace lol
  18. {
  19. /*
  20. * Shuffle an array.
  21. */
  22. template<typename T, typename ARRAY>
  23. void array_base<T, ARRAY>::shuffle()
  24. {
  25. auto n = count();
  26. auto ni = n;
  27. while (n > 0)
  28. {
  29. ni = lol::rand(n--) | 0;
  30. this->swap(ni, n);
  31. }
  32. }
  33. /*
  34. * Sort an array
  35. */
  36. template<typename T, typename ARRAY>
  37. static void quick_swap_sort(array_base<T, ARRAY> &a,
  38. ptrdiff_t start, ptrdiff_t stop);
  39. template<typename T, typename ARRAY>
  40. void array_base<T, ARRAY>::sort(SortAlgorithm algorithm)
  41. {
  42. #if !SORT_WORKS // yeah cause it's shite.
  43. algorithm = SortAlgorithm::Bubble;
  44. #endif
  45. // Classic bubble
  46. if (algorithm == SortAlgorithm::Bubble)
  47. {
  48. int d = 1;
  49. for (ptrdiff_t i = 0; i < count_s() - 1; i = lol::max(i + d, (ptrdiff_t)0))
  50. {
  51. if (i <= 0 || m_data[i] < m_data[i + 1])
  52. d = 1;
  53. if (m_data[i + 1] < m_data[i])
  54. {
  55. this->swap(i, i + 1);
  56. d = -1;
  57. }
  58. }
  59. }
  60. // Quick sort with swap
  61. else if (algorithm == SortAlgorithm::QuickSwap)
  62. {
  63. quick_swap_sort(*this, 0, count_s());
  64. }
  65. }
  66. template<typename T, typename ARRAY>
  67. static void quick_swap_sort(array_base<T, ARRAY> &a,
  68. ptrdiff_t start, ptrdiff_t stop)
  69. {
  70. ptrdiff_t m[3] =
  71. {
  72. rand(start, stop),
  73. rand(start, stop),
  74. rand(start, stop)
  75. };
  76. for (int i = 0; i < 2; )
  77. {
  78. if (a[m[i+1]] < a[m[i]])
  79. {
  80. ptrdiff_t mt = m[i+1];
  81. m[i+1] = m[i];
  82. m[i] = mt;
  83. i = 0;
  84. }
  85. else
  86. i++;
  87. }
  88. // actual stuff
  89. T median = a[m[1]];
  90. ptrdiff_t i0 = start, i1 = stop - 1;
  91. bool b_swap = false;
  92. while (i0 < i1)
  93. {
  94. if (!(a[i0] < median) && a[i1] < median)
  95. {
  96. a.swap(i0, i1);
  97. i0++;
  98. i1--;
  99. b_swap = true;
  100. }
  101. else
  102. {
  103. if (a[i0] < median)
  104. i0++;
  105. if (!(a[i1] < median))
  106. i1--;
  107. }
  108. }
  109. if (stop - start == 1 || !b_swap)
  110. return;
  111. if (start < i0)
  112. quick_swap_sort(a, start, i0);
  113. if (i0 < stop)
  114. quick_swap_sort(a, i0, stop);
  115. }
  116. } /* namespace lol */