qsort.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Assertions.h>
  27. #include <AK/QuickSort.h>
  28. #include <stdlib.h>
  29. #include <sys/types.h>
  30. class SizedObject {
  31. public:
  32. SizedObject() = delete;
  33. SizedObject(void* data, size_t size)
  34. : m_data(data)
  35. , m_size(size)
  36. {
  37. }
  38. void* data() const { return m_data; }
  39. size_t size() const { return m_size; }
  40. private:
  41. void* m_data;
  42. size_t m_size;
  43. };
  44. namespace AK {
  45. template<>
  46. inline void swap(const SizedObject& a, const SizedObject& b)
  47. {
  48. ASSERT(a.size() == b.size());
  49. const size_t size = a.size();
  50. const auto a_data = reinterpret_cast<char*>(a.data());
  51. const auto b_data = reinterpret_cast<char*>(b.data());
  52. for (auto i = 0u; i < size; ++i) {
  53. swap(a_data[i], b_data[i]);
  54. }
  55. }
  56. }
  57. class SizedObjectSlice {
  58. public:
  59. SizedObjectSlice() = delete;
  60. SizedObjectSlice(void* data, size_t element_size)
  61. : m_data(data)
  62. , m_element_size(element_size)
  63. {
  64. }
  65. const SizedObject operator[](size_t index)
  66. {
  67. return { static_cast<char*>(m_data) + index * m_element_size, m_element_size };
  68. }
  69. private:
  70. void* m_data;
  71. size_t m_element_size;
  72. };
  73. void qsort(void* bot, size_t nmemb, size_t size, int (*compar)(const void*, const void*))
  74. {
  75. if (nmemb <= 1) {
  76. return;
  77. }
  78. SizedObjectSlice slice { bot, size };
  79. AK::dual_pivot_quick_sort(slice, 0, nmemb - 1, [=](const SizedObject& a, const SizedObject& b) { return compar(a.data(), b.data()) < 0; });
  80. }
  81. void qsort_r(void* bot, size_t nmemb, size_t size, int (*compar)(const void*, const void*, void*), void* arg)
  82. {
  83. if (nmemb <= 1) {
  84. return;
  85. }
  86. SizedObjectSlice slice { bot, size };
  87. AK::dual_pivot_quick_sort(slice, 0, nmemb - 1, [=](const SizedObject& a, const SizedObject& b) { return compar(a.data(), b.data(), arg) < 0; });
  88. }