Skip to content

Commit

Permalink
improve the performance of sort<>
Browse files Browse the repository at this point in the history
  • Loading branch information
brunocodutra committed Aug 18, 2016
1 parent f67e991 commit a0e9ac5
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 342 deletions.
2 changes: 0 additions & 2 deletions include/metal/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@
#include <metal/list/insert.hpp>
#include <metal/list/join.hpp>
#include <metal/list/list.hpp>
#include <metal/list/merge.hpp>
#include <metal/list/none.hpp>
#include <metal/list/partition.hpp>
#include <metal/list/pop_back.hpp>
#include <metal/list/pop_front.hpp>
#include <metal/list/push_back.hpp>
#include <metal/list/push_front.hpp>
#include <metal/list/range.hpp>
#include <metal/list/reduce.hpp>
#include <metal/list/remove.hpp>
#include <metal/list/remove_if.hpp>
#include <metal/list/replace.hpp>
Expand Down
110 changes: 0 additions & 110 deletions include/metal/list/merge.hpp

This file was deleted.

85 changes: 0 additions & 85 deletions include/metal/list/reduce.hpp

This file was deleted.

109 changes: 100 additions & 9 deletions include/metal/list/sort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,113 @@
#ifndef METAL_LIST_SORT_HPP
#define METAL_LIST_SORT_HPP

#include <metal/list/list.hpp>
#include <metal/list/merge.hpp>
#include <metal/list/transpose.hpp>
#include <metal/lambda/apply.hpp>
#include <metal/lambda/lambda.hpp>
#include <metal/lambda/partial.hpp>
#include <metal/number/if.hpp>

namespace metal
{
namespace detail
{
template<typename seq, typename lbd>
struct _sort;
}

/// \ingroup list
/// ...
template<typename seq, typename lbd>
using sort = metal::apply<
metal::partial<metal::lambda<metal::merge>, lbd, metal::list<>>,
metal::transpose<metal::list<seq>>
>;
using sort = typename detail::_sort<seq, if_<is_lambda<lbd>, lbd>>::type;
}

#include <metal/list/list.hpp>
#include <metal/list/size.hpp>
#include <metal/list/join.hpp>
#include <metal/list/range.hpp>
#include <metal/list/copy_if.hpp>
#include <metal/list/remove_if.hpp>
#include <metal/lambda/invoke.hpp>
#include <metal/lambda/partial.hpp>
#include <metal/number/number.hpp>
#include <metal/number/div.hpp>

namespace metal
{
namespace detail
{
template<typename lbd, typename x, typename y>
struct _merge;

template<typename lbd, typename x, typename y>
using merge = typename _merge<lbd, x, y>::type;

template<typename lbd, typename x, typename xh, typename y, typename yh>
using merge_impl = join<
remove_if<x, partial<lbd, yh>>,
remove_if<y, partial<lbd, xh>>,
merge<
lbd,
copy_if<x, partial<lbd, yh>>,
copy_if<y, partial<lbd, xh>>
>
>;

template<typename lbd, typename x, typename y>
struct _merge
{};

template<
typename lbd,
typename xh, typename... xt,
typename yh, typename... yt
>
struct _merge<lbd, list<xh, xt...>, list<yh, yt...>> :
_invoke<
lambda<merge_impl>,
lbd, list<xh, xt...>, xh, list<yh, yt...>, yh
>
{};

template<typename lbd, typename seq>
struct _merge<lbd, seq, list<>>
{
using type = seq;
};

template<typename lbd, typename seq>
struct _merge<lbd, list<>, seq>
{
using type = seq;
};

template<typename lbd>
struct _merge<lbd, list<>, list<>>
{
using type = list<>;
};

template<typename seq, typename lbd>
using sort_impl = merge<
lbd,
sort<range<seq, number<0>, div<size<seq>, number<2>>>, lbd>,
sort<range<seq, div<size<seq>, number<2>>, size<seq>>, lbd>
>;

template<typename seq, typename lbd>
struct _sort :
_invoke<lambda<sort_impl>, seq, lbd>
{};

template<typename val, typename lbd>
struct _sort<list<val>, lbd>
{
using type = list<val>;
};

template<typename lbd>
struct _sort<list<>, lbd>
{
using type = list<>;
};
}
}

#endif
63 changes: 0 additions & 63 deletions test/src/metal/list/merge.cpp

This file was deleted.

Loading

0 comments on commit a0e9ac5

Please sign in to comment.