sort.cc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * sort.cc - C++ sort functions
  3. * Copyright (C) 2019-2020 Yann Collet
  4. * GPL v2 License
  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; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * You can contact the author at:
  21. * - xxHash homepage: https://www.xxhash.com
  22. * - xxHash source repository: https://github.com/Cyan4973/xxHash
  23. */
  24. /*
  25. * C++ sort functions tend to run faster than C ones due to templates allowing
  26. * inline optimizations.
  27. * Also, glibc's qsort() seems to inflate memory usage, resulting in OOM
  28. * crashes on the test server.
  29. */
  30. #include <algorithm> // std::sort
  31. #define XXH_INLINE_ALL // XXH128_cmp
  32. #include <xxhash.h>
  33. #include "sort.hh"
  34. void sort64(uint64_t* table, size_t size)
  35. {
  36. std::sort(table, table + size);
  37. }
  38. #include <stdlib.h> // qsort
  39. void sort128(XXH128_hash_t* table, size_t size)
  40. {
  41. #if 0
  42. // C++ sort using a custom function object
  43. struct {
  44. bool operator()(XXH128_hash_t a, XXH128_hash_t b) const
  45. {
  46. return XXH128_cmp(&a, &b);
  47. }
  48. } customLess;
  49. std::sort(table, table + size, customLess);
  50. #else
  51. qsort(table, size, sizeof(*table), XXH128_cmp);
  52. #endif
  53. }