CXXR (C++ R)
UnaryFunction.hpp
Go to the documentation of this file.
1 /*CXXR $Id: UnaryFunction.hpp 1351 2013-03-08 15:12:28Z arr $
2  *CXXR
3  *CXXR This file is part of CXXR, a project to refactor the R interpreter
4  *CXXR into C++. It may consist in whole or in part of program code and
5  *CXXR documentation taken from the R project itself, incorporated into
6  *CXXR CXXR (and possibly MODIFIED) under the terms of the GNU General Public
7  *CXXR Licence.
8  *CXXR
9  *CXXR CXXR is Copyright (C) 2008-13 Andrew R. Runnalls, subject to such other
10  *CXXR copyrights and copyright restrictions as may be stated below.
11  *CXXR
12  *CXXR CXXR is not part of the R project, and bugs and other issues should
13  *CXXR not be reported via r-bugs or other R project channels; instead refer
14  *CXXR to the CXXR website.
15  *CXXR */
16 
17 /*
18  * R : A Computer Language for Statistical Data Analysis
19  *
20  * This program is free software; you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation; either version 2.1 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  * GNU Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program; if not, a copy is available at
32  * http://www.r-project.org/Licenses/
33  */
34 
40 #ifndef UNARYFUNCTION_HPP
41 #define UNARYFUNCTION_HPP 1
42 
43 #include <algorithm>
44 #include <boost/lambda/bind.hpp>
45 #include <boost/lambda/lambda.hpp>
46 #include "CXXR/VectorBase.h"
47 #include "CXXR/errors.h"
48 
49 namespace CXXR {
56  namespace VectorOps {
76  static void copyAttributes(VectorBase* to, const VectorBase* from)
77  {
78  to->copyAttributes(from, true);
79  }
80  };
100  static void copyAttributes(VectorBase* to, const VectorBase* from);
101  };
102 
103 
116  static void copyAttributes(VectorBase*, const VectorBase*)
117  {}
118  };
119 
153  template <typename argument_type,
154  typename result_type,
155  typename Functor>
157  : public std::unary_function<argument_type, result_type> {
158  public:
165  UnaryNAPropagator(const Functor& f)
166  : m_func(f)
167  {}
168 
185  result_type operator()(const argument_type& in) const
186  {
187  return (isNA(in)
188  ? NA<result_type>()
189  : result_type((m_func)(ElementTraits::data(in))));
190  }
191 
208  void warnings()
209  {}
210  private:
211  Functor m_func;
212  };
213 
236  template <class Functor>
238  : public std::unary_function<typename Functor::argument_type,
239  typename Functor::result_type> {
240  public:
241  // See para. 3 of ISO14882:2003 Sec. 14.6.2 for why these
242  // typedefs aren't inherited from std::unary_function:
243  typedef typename Functor::argument_type argument_type;
244  typedef typename Functor::result_type result_type;
245 
252  NullUnaryFunctorWrapper(const Functor& f)
253  : m_func(f)
254  {}
255 
273  result_type operator()(const argument_type& in) const
274  {
275  return (m_func)(in);
276  }
277 
294  void warnings()
295  {}
296  private:
297  Functor m_func;
298  };
299 
331  template <typename Functor, class AttributeCopier,
332  template <typename, typename, typename> class FunctorWrapper
333  = UnaryNAPropagator>
335  public:
343  UnaryFunction(const Functor& f = Functor())
344  : m_f(f)
345  {}
346 
364  template <class Vout, class Vin>
365  Vout* apply(const Vin* v) const;
366  private:
367  Functor m_f;
368  };
369 
397  template <class AttributeCopier,
398  template <typename,
399  typename, typename> class FunctorWrapper,
400  typename Functor>
401  static UnaryFunction<Functor, AttributeCopier, FunctorWrapper>
402  makeUnaryFunction(Functor f)
403  {
404  return UnaryFunction<AttributeCopier, Functor, FunctorWrapper>(f);
405  }
406 
429  template <class AttributeCopier, typename Functor>
430  static UnaryFunction<Functor, AttributeCopier>
431  makeUnaryFunction(Functor f)
432  {
433  return UnaryFunction<Functor, AttributeCopier>(f);
434  }
435  } // namespace VectorOps
436 } // namespace CXXR
437 
438 
439 // ***** Implementations of non-inlined templated functions. *****
440 
441 template <typename Functor, class AttributeCopier,
442  template <typename, typename, typename> class FunctorWrapper>
443 template <class Vout, class Vin>
444 Vout* CXXR::VectorOps::UnaryFunction<Functor,
445  AttributeCopier,
446  FunctorWrapper>::apply(const Vin* v) const
447 {
448  using namespace boost::lambda;
449  typedef typename Vin::value_type Inelt;
450  typedef typename Vout::value_type Outelt;
451  GCStackRoot<Vout> ans(CXXR_NEW(Vout(v->size())));
452  FunctorWrapper<Inelt, Outelt, Functor> fwrapper(m_f);
453  std::transform(v->begin(), v->end(), ans->begin(),
454  bind<Outelt>(var(fwrapper), _1));
455  fwrapper.warnings();
456  AttributeCopier::copyAttributes(ans, v);
457  return ans;
458 }
459 
460 #endif // UNARYFUNCTION_HPP