xref: /freebsd/contrib/llvm-project/libcxx/src/algorithm.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===----------------------- algorithm.cpp --------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "algorithm"
10*0b57cec5SDimitry Andric #include "random"
11*0b57cec5SDimitry Andric #ifndef _LIBCPP_HAS_NO_THREADS
12*0b57cec5SDimitry Andric #include "mutex"
13*0b57cec5SDimitry Andric #if defined(__unix__) &&  defined(__ELF__) && defined(_LIBCPP_HAS_COMMENT_LIB_PRAGMA)
14*0b57cec5SDimitry Andric #pragma comment(lib, "pthread")
15*0b57cec5SDimitry Andric #endif
16*0b57cec5SDimitry Andric #endif
17*0b57cec5SDimitry Andric 
18*0b57cec5SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
19*0b57cec5SDimitry Andric 
20*0b57cec5SDimitry Andric template void __sort<__less<char>&, char*>(char*, char*, __less<char>&);
21*0b57cec5SDimitry Andric template void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&);
22*0b57cec5SDimitry Andric template void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&);
23*0b57cec5SDimitry Andric template void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&);
24*0b57cec5SDimitry Andric template void __sort<__less<short>&, short*>(short*, short*, __less<short>&);
25*0b57cec5SDimitry Andric template void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&);
26*0b57cec5SDimitry Andric template void __sort<__less<int>&, int*>(int*, int*, __less<int>&);
27*0b57cec5SDimitry Andric template void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&);
28*0b57cec5SDimitry Andric template void __sort<__less<long>&, long*>(long*, long*, __less<long>&);
29*0b57cec5SDimitry Andric template void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&);
30*0b57cec5SDimitry Andric template void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&);
31*0b57cec5SDimitry Andric template void __sort<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&);
32*0b57cec5SDimitry Andric template void __sort<__less<float>&, float*>(float*, float*, __less<float>&);
33*0b57cec5SDimitry Andric template void __sort<__less<double>&, double*>(double*, double*, __less<double>&);
34*0b57cec5SDimitry Andric template void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&);
35*0b57cec5SDimitry Andric 
36*0b57cec5SDimitry Andric template bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&);
37*0b57cec5SDimitry Andric template bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&);
38*0b57cec5SDimitry Andric template bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&);
39*0b57cec5SDimitry Andric template bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&);
40*0b57cec5SDimitry Andric template bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&);
41*0b57cec5SDimitry Andric template bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&);
42*0b57cec5SDimitry Andric template bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&);
43*0b57cec5SDimitry Andric template bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&);
44*0b57cec5SDimitry Andric template bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&);
45*0b57cec5SDimitry Andric template bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&);
46*0b57cec5SDimitry Andric template bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&);
47*0b57cec5SDimitry Andric template bool __insertion_sort_incomplete<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&);
48*0b57cec5SDimitry Andric template bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&);
49*0b57cec5SDimitry Andric template bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&);
50*0b57cec5SDimitry Andric template bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&);
51*0b57cec5SDimitry Andric 
52*0b57cec5SDimitry Andric template unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&);
53*0b57cec5SDimitry Andric 
54*0b57cec5SDimitry Andric #ifndef _LIBCPP_HAS_NO_THREADS
55*0b57cec5SDimitry Andric _LIBCPP_SAFE_STATIC static __libcpp_mutex_t __rs_mut = _LIBCPP_MUTEX_INITIALIZER;
56*0b57cec5SDimitry Andric #endif
57*0b57cec5SDimitry Andric unsigned __rs_default::__c_ = 0;
58*0b57cec5SDimitry Andric 
59*0b57cec5SDimitry Andric __rs_default::__rs_default()
60*0b57cec5SDimitry Andric {
61*0b57cec5SDimitry Andric #ifndef _LIBCPP_HAS_NO_THREADS
62*0b57cec5SDimitry Andric     __libcpp_mutex_lock(&__rs_mut);
63*0b57cec5SDimitry Andric #endif
64*0b57cec5SDimitry Andric     __c_ = 1;
65*0b57cec5SDimitry Andric }
66*0b57cec5SDimitry Andric 
67*0b57cec5SDimitry Andric __rs_default::__rs_default(const __rs_default&)
68*0b57cec5SDimitry Andric {
69*0b57cec5SDimitry Andric     ++__c_;
70*0b57cec5SDimitry Andric }
71*0b57cec5SDimitry Andric 
72*0b57cec5SDimitry Andric __rs_default::~__rs_default()
73*0b57cec5SDimitry Andric {
74*0b57cec5SDimitry Andric #ifndef _LIBCPP_HAS_NO_THREADS
75*0b57cec5SDimitry Andric     if (--__c_ == 0)
76*0b57cec5SDimitry Andric        __libcpp_mutex_unlock(&__rs_mut);
77*0b57cec5SDimitry Andric #else
78*0b57cec5SDimitry Andric     --__c_;
79*0b57cec5SDimitry Andric #endif
80*0b57cec5SDimitry Andric }
81*0b57cec5SDimitry Andric 
82*0b57cec5SDimitry Andric __rs_default::result_type
83*0b57cec5SDimitry Andric __rs_default::operator()()
84*0b57cec5SDimitry Andric {
85*0b57cec5SDimitry Andric     static mt19937 __rs_g;
86*0b57cec5SDimitry Andric     return __rs_g();
87*0b57cec5SDimitry Andric }
88*0b57cec5SDimitry Andric 
89*0b57cec5SDimitry Andric __rs_default
90*0b57cec5SDimitry Andric __rs_get()
91*0b57cec5SDimitry Andric {
92*0b57cec5SDimitry Andric     return __rs_default();
93*0b57cec5SDimitry Andric }
94*0b57cec5SDimitry Andric 
95*0b57cec5SDimitry Andric _LIBCPP_END_NAMESPACE_STD
96