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 /*
26  * C++ sort functions tend to run faster than C ones due to templates allowing
27  * inline optimizations.
28  * Also, glibc's qsort() seems to inflate memory usage, resulting in OOM
29  * crashes on the test server.
30  */
31 
32 #include <algorithm>  // std::sort
33 #define XXH_INLINE_ALL  // XXH128_cmp
34 #include <xxhash.h>
35 
36 #include "sort.hh"
37 
sort64(uint64_t * table,size_t size)38 void sort64(uint64_t* table, size_t size)
39 {
40     std::sort(table, table + size);
41 }
42 
43 #include <stdlib.h>  // qsort
44 
sort128(XXH128_hash_t * table,size_t size)45 void sort128(XXH128_hash_t* table, size_t size)
46 {
47 #if 0
48     // C++ sort using a custom function object
49     struct {
50         bool operator()(XXH128_hash_t a, XXH128_hash_t b) const
51         {
52             return XXH128_cmp(&a, &b);
53         }
54     } customLess;
55     std::sort(table, table + size, customLess);
56 #else
57     qsort(table, size, sizeof(*table), XXH128_cmp);
58 #endif
59 }
60