Skip to content

Commit

Permalink
[coding-opt] list::radix_sort_back_fill 去模板参数优化
Browse files Browse the repository at this point in the history
  • Loading branch information
WentsingNee committed Jun 24, 2024
1 parent ee98e5a commit df93e12
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 67 deletions.
34 changes: 22 additions & 12 deletions include/kerbal/container/detail/list_base/list_base.decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ namespace kerbal

class list_type_unrelated
{
private:
template <typename T>
friend class list_type_only;

protected:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
Expand Down Expand Up @@ -181,6 +185,24 @@ namespace kerbal
KERBAL_CONSTEXPR14
static void k_splice(basic_const_iterator pos, basic_const_iterator first, basic_const_iterator last) KERBAL_NOEXCEPT;

KERBAL_CONSTEXPR14
static
void
k_radix_sort_back_fill(
kerbal::type_traits::false_type /*unsigned*/,
basic_const_iterator insert_pos,
list_type_unrelated buckets[], std::size_t BUCKETS_NUM
) KERBAL_NOEXCEPT;

KERBAL_CONSTEXPR14
static
void
k_radix_sort_back_fill(
kerbal::type_traits::true_type /*signed*/,
basic_const_iterator insert_pos,
list_type_unrelated buckets[], std::size_t BUCKETS_NUM
) KERBAL_NOEXCEPT;

//===================
// private

Expand Down Expand Up @@ -765,18 +787,6 @@ namespace kerbal

private:

template <bool is_radix_sort_acceptable_type>
KERBAL_CONSTEXPR20
typename kerbal::type_traits::enable_if<is_radix_sort_acceptable_type>::type
static k_radix_sort_back_fill(const_iterator insert_pos, kerbal::type_traits::false_type /*unsigned*/,
list_type_only buckets[], std::size_t BUCKETS_NUM) KERBAL_NOEXCEPT;

template <bool is_radix_sort_acceptable_type>
KERBAL_CONSTEXPR20
typename kerbal::type_traits::enable_if<is_radix_sort_acceptable_type>::type
static k_radix_sort_back_fill(const_iterator insert_pos, kerbal::type_traits::true_type /*signed*/,
list_type_only buckets[], std::size_t BUCKETS_NUM) KERBAL_NOEXCEPT;

template <std::size_t RADIX_BIT_WIDTH>
KERBAL_CONSTEXPR20
static void k_radix_sort(const_iterator first, const_iterator last, kerbal::type_traits::false_type asc,
Expand Down
118 changes: 63 additions & 55 deletions include/kerbal/container/detail/list_base/list_base.impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,39 @@ namespace kerbal
list_type_unrelated::k_hook_node(pos, chain.start, chain.back);
}

KERBAL_CONSTEXPR14
inline
void
list_type_unrelated::
k_radix_sort_back_fill(
kerbal::type_traits::false_type /*unsigned*/,
basic_const_iterator insert_pos,
list_type_unrelated buckets[], std::size_t BUCKETS_NUM
) KERBAL_NOEXCEPT
{
for (std::size_t i = 0; i < BUCKETS_NUM; ++i) {
k_splice(insert_pos, buckets[i]);
}
}

KERBAL_CONSTEXPR14
inline
void
list_type_unrelated::
k_radix_sort_back_fill(
kerbal::type_traits::true_type /*signed*/,
basic_const_iterator insert_pos,
list_type_unrelated buckets[], std::size_t BUCKETS_NUM
) KERBAL_NOEXCEPT
{
for (std::size_t i = BUCKETS_NUM / 2; i < BUCKETS_NUM; ++i) {
k_splice(insert_pos, buckets[i]);
}
for (std::size_t i = 0; i < BUCKETS_NUM / 2; ++i) {
k_splice(insert_pos, buckets[i]);
}
}


//===================
// private
Expand Down Expand Up @@ -1371,35 +1404,6 @@ namespace kerbal
k_merge_sort_n(first, kerbal::iterator::distance(first, last), cmp);
}

template <typename T>
template <bool is_radix_sort_acceptable_type>
KERBAL_CONSTEXPR20
typename kerbal::type_traits::enable_if<is_radix_sort_acceptable_type>::type
list_type_only<T>::k_radix_sort_back_fill(
const_iterator insert_pos, kerbal::type_traits::false_type /*unsigned*/,
list_type_only buckets[], std::size_t BUCKETS_NUM) KERBAL_NOEXCEPT
{
for (std::size_t i = 0; i < BUCKETS_NUM; ++i) {
k_splice(insert_pos, buckets[i]);
}
}

template <typename T>
template <bool is_radix_sort_acceptable_type>
KERBAL_CONSTEXPR20
typename kerbal::type_traits::enable_if<is_radix_sort_acceptable_type>::type
list_type_only<T>::k_radix_sort_back_fill(
const_iterator insert_pos, kerbal::type_traits::true_type /*signed*/,
list_type_only buckets[], std::size_t BUCKETS_NUM) KERBAL_NOEXCEPT
{
for (std::size_t i = BUCKETS_NUM / 2; i < BUCKETS_NUM; ++i) {
k_splice(insert_pos, buckets[i]);
}
for (std::size_t i = 0; i < BUCKETS_NUM / 2; ++i) {
k_splice(insert_pos, buckets[i]);
}
}

template <typename T>
template <std::size_t RADIX_BIT_WIDTH>
KERBAL_CONSTEXPR20
Expand All @@ -1408,7 +1412,7 @@ namespace kerbal
kerbal::type_traits::integral_constant<std::size_t, RADIX_BIT_WIDTH>) KERBAL_NOEXCEPT
{
typedef kerbal::type_traits::integral_constant<std::size_t, 1 << RADIX_BIT_WIDTH> BUCKETS_NUM;
list_type_only buckets[2][BUCKETS_NUM::value];
list_type_unrelated buckets[2][BUCKETS_NUM::value];

for (int i = 0; i < 2; ++i) {
for (std::size_t j = 0; j < BUCKETS_NUM::value; ++j) {
Expand All @@ -1422,29 +1426,31 @@ namespace kerbal
// first round
for (const_iterator it(first); it != last; ) {
int bucket_id = *it % BUCKETS_NUM::value;
list_type_only & bucket_in = buckets[0][bucket_id];
bucket_in.k_splice(bucket_in.cend(), it++);
list_type_unrelated & bucket_in = buckets[0][bucket_id];
bucket_in.k_splice(bucket_in.basic_cend(), it++);
}

for (std::size_t round = 1; round < ROUNDS::value; ++round) {
list_type_only (& buckets_from)[BUCKETS_NUM::value] = buckets[(round + 1) % 2];
list_type_only (& buckets_to)[BUCKETS_NUM::value] = buckets[round % 2];
list_type_unrelated (& buckets_from)[BUCKETS_NUM::value] = buckets[(round + 1) % 2];
list_type_unrelated (& buckets_to)[BUCKETS_NUM::value] = buckets[round % 2];

for (std::size_t i = 0; i < BUCKETS_NUM::value; ++i) {
list_type_only & bucket_out = buckets_from[i];
const_iterator it(bucket_out.begin());
const_iterator const cend(bucket_out.end());
list_type_unrelated & bucket_out = buckets_from[i];
const_iterator it(bucket_out.basic_begin());
const_iterator const cend(bucket_out.basic_end());
while (it != cend) {
int bucket_id = (*it >> (RADIX_BIT_WIDTH * round)) % BUCKETS_NUM::value;
list_type_only & bucket_in = buckets_to[bucket_id];
bucket_in.k_splice(bucket_in.cend(), it++);
list_type_unrelated & bucket_in = buckets_to[bucket_id];
bucket_in.k_splice(bucket_in.basic_cend(), it++);
}
}
}

k_radix_sort_back_fill<true>(
last, kerbal::type_traits::is_signed<value_type>(),
buckets[(ROUNDS::value + 1) % 2], BUCKETS_NUM::value);
k_radix_sort_back_fill(
kerbal::type_traits::is_signed<value_type>(),
last,
buckets[(ROUNDS::value + 1) % 2], BUCKETS_NUM::value
);
}

template <typename T>
Expand All @@ -1455,7 +1461,7 @@ namespace kerbal
kerbal::type_traits::integral_constant<std::size_t, RADIX_BIT_WIDTH>) KERBAL_NOEXCEPT
{
typedef kerbal::type_traits::integral_constant<std::size_t, 1 << RADIX_BIT_WIDTH> BUCKETS_NUM;
list_type_only buckets[2][BUCKETS_NUM::value];
list_type_unrelated buckets[2][BUCKETS_NUM::value];

for (int i = 0; i < 2; ++i) {
for (std::size_t j = 0; j < BUCKETS_NUM::value; ++j) {
Expand All @@ -1469,29 +1475,31 @@ namespace kerbal
// first round
for (const_iterator it(first); it != last; ) {
int bucket_id = BUCKETS_NUM::value - 1 - *it % BUCKETS_NUM::value;
list_type_only & bucket_in = buckets[0][bucket_id];
bucket_in.k_splice(bucket_in.cend(), it++);
list_type_unrelated & bucket_in = buckets[0][bucket_id];
bucket_in.k_splice(bucket_in.basic_cend(), it++);
}

for (std::size_t round = 1; round < ROUNDS::value; ++round) {
list_type_only (& buckets_from)[BUCKETS_NUM::value] = buckets[(round + 1) % 2];
list_type_only (& buckets_to)[BUCKETS_NUM::value] = buckets[round % 2];
list_type_unrelated (& buckets_from)[BUCKETS_NUM::value] = buckets[(round + 1) % 2];
list_type_unrelated (& buckets_to)[BUCKETS_NUM::value] = buckets[round % 2];

for (std::size_t i = 0; i < BUCKETS_NUM::value; ++i) {
list_type_only & bucket_out = buckets_from[i];
const_iterator it(bucket_out.begin());
const_iterator const cend(bucket_out.end());
list_type_unrelated & bucket_out = buckets_from[i];
const_iterator it(bucket_out.basic_begin());
const_iterator const cend(bucket_out.basic_end());
while (it != cend) {
int bucket_id = BUCKETS_NUM::value - 1 - (*it >> (RADIX_BIT_WIDTH * round)) % BUCKETS_NUM::value;
list_type_only & bucket_in = buckets_to[bucket_id];
bucket_in.k_splice(bucket_in.cend(), it++);
list_type_unrelated & bucket_in = buckets_to[bucket_id];
bucket_in.k_splice(bucket_in.basic_cend(), it++);
}
}
}

k_radix_sort_back_fill<true>(
last, kerbal::type_traits::is_signed<value_type>(),
buckets[(ROUNDS::value + 1) % 2], BUCKETS_NUM::value);
k_radix_sort_back_fill(
kerbal::type_traits::is_signed<value_type>(),
last,
buckets[(ROUNDS::value + 1) % 2], BUCKETS_NUM::value
);
}

template <typename T>
Expand Down

0 comments on commit df93e12

Please sign in to comment.