ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
OrderKey.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File OrderKey.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
32/*--------------------------------------------------------------------------*/
33/*----------------------------- DEFINITIONS --------------------------------*/
34/*--------------------------------------------------------------------------*/
35
36#ifndef __ADS_OrderKey
37 #define __ADS_OrderKey
38/* self-identification: #endif constant */
39
40/*--------------------------------------------------------------------------*/
41/*------------------------------ INCLUDES ----------------------------------*/
42/*--------------------------------------------------------------------------*/
43
44#include "ADS/sort/RadixSort.h"
45
46#include <cstdint>
47#include <functional>
48#include <type_traits>
49#include <utility>
50
51/*--------------------------------------------------------------------------*/
52/*--------------------------- NAMESPACE ------------------------------------*/
53/*--------------------------------------------------------------------------*/
54
55namespace ADS::detail
56{
57
59template< class T >
60struct IsNumberPair : std::false_type
61{ };
62
63template< class A , class B >
64struct IsNumberPair< std::pair< A , B > >
65 : std::bool_constant< RadixKey< A > && RadixKey< B > &&
66 ( sizeof( A ) + sizeof( B ) <= 8 ) >
67{ };
68
70template< class T >
71concept OrderKeyed = RadixKey< T > || IsNumberPair< T >::value;
72
74template< class T , class C >
75inline constexpr bool kAscending =
76 std::is_same_v< C , std::less<> > || std::is_same_v< C , std::less< T > >;
77
78template< class T , class C >
79inline constexpr bool kDescending = std::is_same_v< C , std::greater<> > ||
80 std::is_same_v< C , std::greater< T > >;
81
83template< class T , class C >
84inline constexpr bool kKeyed =
85 OrderKeyed< T > && ( kAscending< T , C > || kDescending< T , C > );
86
88template< OrderKeyed T >
89[[nodiscard]] inline auto orderKey( const T & x )
90{
91 if constexpr( RadixKey< T > )
92 return( radixKey( x ) );
93 else {
94 using A = typename T::first_type;
95 using B = typename T::second_type;
96 using U = std::conditional_t< sizeof( A ) + sizeof( B ) <= 4 ,
97 std::uint32_t ,
98 std::uint64_t >;
99 return( U( ( U( radixKey( x.first ) ) << ( 8 * sizeof( B ) ) ) |
100 U( radixKey( x.second ) ) ) );
101 }
102 }
103
105template< class C , class T >
106 requires kKeyed< T , C >
107[[nodiscard]] inline auto orderKeyBy( const T & x )
108{
109 const auto k = orderKey( x );
110 if constexpr( kDescending< T , C > )
111 return( decltype( k )( ~k ) );
112 else
113 return( k );
114 }
115
116} // end( namespace ADS::detail )
117
118/*--------------------------------------------------------------------------*/
119
120#endif /* OrderKey.h included */
121
122/*--------------------------------------------------------------------------*/
123/*-------------------------- End File OrderKey.h ---------------------------*/
124/*--------------------------------------------------------------------------*/
The sorts by the digits of the keys, beyond the book (which has Counting Sort and Pigeonhole Sort,...