22

私は現在、C++ 11 でいくつかの暗号化アルゴリズムをコーディングしていますが、これには多くの関数構成が必要です。私が対処しなければならない構成には2つのタイプがあります。

  1. それ自体で関数を可変回数構成します。数学的には、特定の関数 F について、F^n(x) = (F^{n-1} o F)(x) = F^{n-1}(F(x)) です。

  2. さまざまな機能を一緒に構成します。たとえば、同じタイプのいくつかの関数 f、g、h、i、j および k の場合、f(g(h(i(j(k(x)))))) があります。

私の場合、次の F の定義を使用しています。

const std::vector<uint8_t> F(const std::vector<uint8_t> &x);

この関数をそれ自体でn回構成したいと思います。私は正常に動作している単純な再帰的な方法で構成を実装しました:

const std::vector<uint8_t> compose(const uint8_t n, const std::vector<uint8_t> &x)
{
    if(n > 1)
       return compose(n-1, F(x));

    return F(x);
}

この場合、 c++11を使用して BOOST を使用せずにこのコンポジションを実装するためのより効率的な方法はありますか? もちろん可能であれば、このフォームを使用することは素晴らしいことです:

answer = compose<4>(F)(x); // Same as 'answer = F^4(x) = F(F(F(F(x))))'

2 番目のケースでは、可変数の関数の構成を実装したいと考えています。F と同じ定義を持つ関数 F0、F1、...、Fn の特定のセットについて、n が変数である場合にそれらを構成する効率的で適切な方法はありますか? ここでは可変個引数テンプレートが役立つと思いますが、その場合の使用方法がわかりません。

ご協力いただきありがとうございます。

4

7 に答える 7

17

おそらく(テストされていない)これらの線に沿ったもの:

template <typename F>
class Composer {
  int n_;
  F f_;
public:
  Composer(int n, F f) : n_(n), f_(f) {}

  template <typename T>
  T operator()(T x) const {
    int n = n_;
    while (n--) {
      x = f_(x);
    }
    return x;
  }
};

template <int N, typename F>
Composer<F> compose(F f) {
  return Composer<F>(N, f);
}

編集:そして2番目のケース(今回はテスト済み)の場合:

#include <iostream>

template <typename F0, typename... F>
class Composer2 {
    F0 f0_;
    Composer2<F...> tail_;
public:
    Composer2(F0 f0, F... f) : f0_(f0), tail_(f...) {}

    template <typename T>
    T operator() (const T& x) const {
        return f0_(tail_(x));
    }
};

template <typename F>
class Composer2<F> {
    F f_;
public:
    Composer2(F f) : f_(f) {}

    template <typename T>
    T operator() (const T& x) const {
        return f_(x);
    }
};

template <typename... F>
Composer2<F...> compose2(F... f) {
    return Composer2<F...>(f...);
}

int f(int x) { return x + 1; }
int g(int x) { return x * 2; }
int h(int x) { return x - 1; }

int main() {
  std::cout << compose2(f, g, h)(42);
  return 0;
}
于 2013-09-28T20:38:05.150 に答える
12

楽しい質問をありがとう、2013 年のガブリエル。ここに解決策があります。c++14 で動作します。

#include <functional>
#include <iostream>
using std::function;

// binary function composition for arbitrary types
template <class F, class G> auto compose(F f, G g) {
  return [f, g](auto x) { return f(g(x)); };
}

// for convienience
template <class F, class G> auto operator*(F f, G g) { return compose(f, g); }

// composition for n arguments
template <class F, typename... Fs> auto compose(F f, Fs &&... fs) {
  return f * compose(fs...);
}

// composition for n copies of f
template <int i, class F>
// must wrap chain in a struct to allow partial template specialization
struct multi {
  static F chain(F f) { return f * multi<i - 1, F>::chain(f); }
};
template <class F> struct multi<2, F> {
  static F chain(F f) { return f * f; }
};
template <int i, class F> F compose(F f) { return multi<i, F>::chain(f); }

int main(int argc, char const *argv[]) {

  function<double(int)> f = [](auto i) { return i + 3; };
  function<int(double)> g = [](auto i) { return i * 2; };
  function<int(int)> h = [](auto i) { return i + 1; };

  std::cout << '\n'
            << "9   == " << compose(f, g, f)(0) << '\n'
            << "5   == " << (f * g * h)(0) << '\n'
            << "100 == " << compose<100>(h)(0) << '\n';

  return 0;
}

定義できます

Matrix compose(Matrix f, Matrix g);

また

Rotation compose(Rotation f, Rotation g);

このコードをあらゆる種類のものに再利用します。

于 2016-04-01T02:47:06.617 に答える
2

非常に一般的な例 ( g++ -std=c++1y composition.cpp):

// ---------------------------------------------------------
// "test" part
// ---------------------------------------------------------
int f(int a) { return 2*a; }
double g(int a) { return a+2.5; }
double h(double a) { return 2.5*a; }
double i(double a) { return 2.5-a; }

class Functor {
  double x;
public:
  Functor (double x_) :  x(x_) { }
  double operator() (double a) { return a*x; }
};

// ---------------------------------------------------------
// ---------------------------------------------------------
int main () {

  auto l1 = [] (double a) { return a/3; };
  auto l2 = [] (double a) { return 3.5+a; };

  Functor fu {4.5};

  auto compos1 = compose (f, g, l1, g, h, h, l1, l2); 

  auto compos2 = compose (compos1, l1, l2, fu);

  auto x = compos2 (3);

  cout << x << endl;
  cout << compos2(3) << endl;

  cout << fu(l2(l1(l2(l1(h(h(g(l1(g(f(3))))))))))) << endl;

} // ()

ライブラリ部分:

// ---------------------------------------------------------
// "library" part
// ---------------------------------------------------------
template<typename F1, typename F2>
class Composite{
private:
  F1  f1;
  F2  f2;

public:
  Composite(F1  f1,  F2  f2) : f1(f1), f2(f2) { }

  template<typename IN>
  decltype(auto) operator() (IN i)   
  { 
    return f2 ( f1(i) ); 
  }
};

// ---------------------------------------------------------
// ---------------------------------------------------------
template<typename F1, typename F2>
decltype(auto) compose (F1 f, F2 g) {
  return Composite<F1, F2> {f,g};
}

// ---------------------------------------------------------
// ---------------------------------------------------------
template<typename F1, typename... Fs>
decltype(auto) compose (F1  f,  Fs  ... args) 
{
  return compose (f, compose(args...));
}

プログラム全体:

//  g++ -std=c++1y composition.cpp 

#include <iostream>

using namespace std;

// ---------------------------------------------------------
// "library" part
// ---------------------------------------------------------
template<typename F1, typename F2>
class Composite{
private:
  F1  f1;
  F2  f2;

public:
  Composite(F1  f1,  F2  f2) : f1(f1), f2(f2) { }

  template<typename IN>
  decltype(auto) operator() (IN i)   
  { 
    return f2 ( f1(i) ); 
  }
};

// ---------------------------------------------------------
// ---------------------------------------------------------
template<typename F1, typename F2>
decltype(auto) compose (F1 f, F2 g) {
  return Composite<F1, F2> {f,g};
}

// ---------------------------------------------------------
// ---------------------------------------------------------
template<typename F1, typename... Fs>
decltype(auto) compose (F1  f,  Fs  ... args) 
{
  return compose (f, compose(args...));
}

// ---------------------------------------------------------
// "test" part
// ---------------------------------------------------------
int f(int a) { return 2*a; }
double g(int a) { return a+2.5; }
double h(double a) { return 2.5*a; }
double i(double a) { return 2.5-a; }

class Functor {
  double x;
public:
  Functor (double x_) :  x(x_) { }
  double operator() (double a) { return a*x; }
};

// ---------------------------------------------------------
// ---------------------------------------------------------
int main () {

  auto l1 = [] (double a) { return a/3; };
  auto l2 = [] (double a) { return 3.5+a; };

  Functor fu {4.5};

  auto compos1 = compose (f, g, l1, g, h, h, l1, l2); 

  auto compos2 = compose (compos1, l1, l2, fu);

  auto x = compos2 (3);

  cout << x << endl;
  cout << compos2(3) << endl;

  cout << fu(l2(l1(l2(l1(h(h(g(l1(g(f(3))))))))))) << endl;

} // ()
于 2014-12-31T23:27:01.850 に答える
1

引数転送による関数反復の迅速な実装。関数テンプレートは部分的に特殊化できないため、残念ながらヘルパー型が必要です。

#include <functional>
#include <iostream>
using namespace std;

template<int n, typename A>
struct iterate_helper {
  function<A(A)> f;
  iterate_helper(function<A(A)> f) : f(f) {}
  A operator()(A&& x) {
    return f(iterate_helper<n - 1, A>(f)(forward<A>(x)));
  };
};

template<typename A>
struct iterate_helper<1, A> {
  function<A(A)> f;
  iterate_helper(function<A(A)> f) : f(f) {}
  A operator()(A&& x) {
    return f(forward<A>(x));
  };
};

template<int n, typename A>
function<A(A)> iterate(function<A(A)> f) {
  return iterate_helper<n, A>(f);
}

int succ(int x) {
  return x + 1;
}

int main() {
  auto add5 = iterate<5>(function<int(int)>(succ));
  cout << add5(10) << '\n';
}
于 2013-09-28T20:34:07.350 に答える
0

の本体は示していませんがF、入力を変更して出力を形成するように変更できる場合は、署名を次のように変更します。

void F(std::vector<uint8_t>& x);

その後、Fn を次のように実装できます。

void Fn(std::vector<uint8_t>& x, size_t n)
{
    for (size_t i = 0; i < n; i++)
        F(x);
}

より効率的な場合、コンパイラはループをアンロールしますが、そうでない場合でも、ローカル変数のインクリメント/比較は、F を呼び出すよりも桁違いに高速です。

実際にコピーを作成したい場合は、新しいベクトルを明示的にコピー構築できます。

vector<uint8_t> v1 = ...;
vector<uint8_t> v2 = v1; // explicitly take copy
Fn(v2,10);
于 2013-09-29T15:29:11.333 に答える
0

どうですか(未テスト):

template < typename Func, typename T >
T  compose_impl( Func &&, T &&x, std::integral_constant<std::size_t, 0> )
{ return std::forward<T>(x); }

template < typename Func, typename T, std::size_t N >
T compose_impl( Func &&f, T &&x, std::integral_constant<std::size_t, N> )
{
    return compose_impl( std::forward<Func>(f),
     std::forward<Func>(f)(std::forward<T>( x )),
     std::integral_constant<std::size_t, N-1>{} );
}

template < std::size_t Repeat = 1, typename Func, typename T >
T  compose( Func &&f, T &&x )
{
    return compose_impl( std::forward<Func>(f), std::forward<T>(x),
     std::integral_constant<std::size_t, Repeat>{} );
}

複数の関数に可変長関数テンプレートを使用できます (未テスト)。

template < typename Func, typename T >
constexpr  // C++14 only, due to std::forward not being constexpr in C++11
auto chain_compose( Func &&f, T &&x )
 noexcept( noexcept(std::forward<Func>( f )( std::forward<T>(x) )) )
 -> decltype( std::forward<Func>(f)(std::forward<T>( x )) )
{ return std::forward<Func>(f)(std::forward<T>( x )); }

template < typename Func1, typename Func2, typename Func3, typename ...RestAndT >
constexpr  // C++14 only, due to std::forward
auto  chain_compose( Func1 &&f, Func2 &&g, Func3 &&h, RestAndT &&...i_and_x )
 noexcept( CanAutoWorkHereOtherwiseDoItYourself )
 -> decltype( auto )  // C++14 only
{
    return chain_compose( std::forward<Func1>(f),
     chain_compose(std::forward<Func2>( g ), std::forward<Func3>( h ),
     std::forward<RestAndT>( i_and_x )...) );
}

次のdecltype(auto)コンストラクトは、インライン関数からの戻り値の型を自動的に計算します。同様の自動計算があるかどうかはわかりませんnoexcept

于 2013-09-29T16:30:56.223 に答える