tlx
cswap.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/sort/networks/cswap.hpp
3  *
4  * Conditional swap implementation used for sorting networks.
5  *
6  * Part of tlx - http://panthema.net/tlx
7  *
8  * Copyright (C) 2018-2020 Jasper Marianczuk <jasper.marianczuk@gmail.com>
9  * Copyright (C) 2020 Timo Bingmann <tb@panthema.net>
10  *
11  * All rights reserved. Published under the Boost Software License, Version 1.0
12  ******************************************************************************/
13 
14 #ifndef TLX_SORT_NETWORKS_CSWAP_HEADER
15 #define TLX_SORT_NETWORKS_CSWAP_HEADER
16 
17 #include <algorithm>
18 
19 namespace tlx {
20 
21 //! \addtogroup tlx_sort
22 //! \{
23 //! \name Implementations of Sorting Networks
24 //! \{
25 
26 //! Implementations of sorting networks for up to sixteen elements.
27 namespace sort_networks {
28 
29 //! Conditional swap implementation used for sorting networks: trivial portable
30 //! C++ implementation with custom comparison method and std::swap().
31 template <typename Comparator>
32 class CS_IfSwap
33 {
34 public:
35  CS_IfSwap(Comparator cmp) : cmp_(cmp) { }
36 
37  template <typename Type>
38  inline void operator () (Type& left, Type& right) {
39  if (cmp_(right, left)) { std::swap(left, right); }
40  }
41 
42 protected:
43  Comparator cmp_;
44 };
45 
46 /******************************************************************************/
47 
48 //! \}
49 //! \}
50 
51 } // namespace sort_networks
52 } // namespace tlx
53 
54 #endif // !TLX_SORT_NETWORKS_CSWAP_HEADER
55 
56 /******************************************************************************/
void operator()(Type &left, Type &right)
Definition: cswap.hpp:38
CS_IfSwap(Comparator cmp)
Definition: cswap.hpp:35
Conditional swap implementation used for sorting networks: trivial portable C++ implementation with c...
Definition: cswap.hpp:32
void swap(CountingPtr< A, D > &a1, CountingPtr< A, D > &a2) noexcept
swap enclosed object with another counting pointer (no reference counts need change) ...