GURLS++  2.0.00
C++ Implementation of GURLS Matlab Toolbox
optfunction.h
00001 /*
00002   * The GURLS Package in C++
00003   *
00004   * Copyright (C) 2011-1013, IIT@MIT Lab
00005   * All rights reserved.
00006   *
00007   * author:  M. Santoro
00008   * email:   msantoro@mit.edu
00009   * website: http://cbcl.mit.edu/IIT@MIT/IIT@MIT.html
00010   *
00011   * Redistribution and use in source and binary forms, with or without
00012   * modification, are permitted provided that the following conditions
00013   * are met:
00014   *
00015   *     * Redistributions of source code must retain the above
00016   *       copyright notice, this list of conditions and the following
00017   *       disclaimer.
00018   *     * Redistributions in binary form must reproduce the above
00019   *       copyright notice, this list of conditions and the following
00020   *       disclaimer in the documentation and/or other materials
00021   *       provided with the distribution.
00022   *     * Neither the name(s) of the copyright holders nor the names
00023   *       of its contributors or of the Massacusetts Institute of
00024   *       Technology or of the Italian Institute of Technology may be
00025   *       used to endorse or promote products derived from this software
00026   *       without specific prior written permission.
00027   *
00028   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00029   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00030   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00031   * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00032   * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00033   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00034   * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00035   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00036   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00037   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00038   * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00039   * POSSIBILITY OF SUCH DAMAGE.
00040   */
00041 
00042 #ifndef _GURLS_OPTFUNCTION_H_
00043 #define _GURLS_OPTFUNCTION_H_
00044 
00045 #include "gurls++/exports.h"
00046 #include "gurls++/options.h"
00047 #include "gurls++/exceptions.h"
00048 
00049 #include <string>
00050 
00051 namespace gurls
00052 {
00053 
00054 class GURLS_EXPORT Functor
00055 {
00056 public:
00057     virtual double operator()(const double *v, int n) = 0;
00058     virtual float operator()(const float *v, int n) = 0;
00059 };
00060 
00064 class GURLS_EXPORT Mean : public Functor
00065 {
00066 public:
00067 
00068     double operator()(const double *v, int n)
00069     {
00070         return mean<double>(v, n);
00071     }
00072 
00073     float operator()(const float *v, int n)
00074     {
00075         return mean<float>(v, n);
00076     }
00077 
00078 protected:
00079     template <typename T>
00080     T mean(const T *v, int n)
00081     {
00082         T m = (T)0.0;
00083 
00084         for (int i = 0; i < n; ++i, ++v)
00085             m += *v;
00086 
00087         return m/n;
00088     }
00089 };
00090 
00094 class GURLS_EXPORT Min : public Functor
00095 {
00096 public:
00097     double operator()(const double *v, int n)
00098     {
00099         return *std::min_element(v,v+n);
00100     }
00101 
00102     float operator()(const float *v, int n)
00103     {
00104         return *std::min_element(v,v+n);
00105     }
00106 };
00107 
00108 
00112 class GURLS_EXPORT Max : public Functor
00113 {
00114 public:
00115     double operator()(const double *v, int n)
00116     {
00117         return *std::max_element(v,v+n);
00118     }
00119 
00120     float operator()(const float *v, int n)
00121     {
00122         return *std::max_element(v,v+n);
00123     }
00124 };
00125 
00129 class GURLS_EXPORT Median : public Functor
00130 {
00131 public:
00132     double operator()(const double *v, int n)
00133     {
00134         return median(v,n);
00135     }
00136 
00137     float operator()(const float *v, int n)
00138     {
00139         return median(v,n);
00140     }
00141 
00142 protected:
00143     template <typename T>
00144     T median(const T *v, int n)
00145     {
00146         std::vector<T> vd(v, v+n);
00147         sort(vd.begin(), vd.end());
00148 
00149         if(n%2)
00150             return *(vd.begin()+vd.size()/2);
00151         else
00152             return (*(vd.begin()+vd.size()/2) + *((vd.begin()+vd.size()/2)-1) )/2;
00153     }
00154 };
00155 
00156 
00157 
00158 
00159 #ifdef _WIN32
00160 #pragma warning(push)
00161 #pragma warning(disable : 4251)
00162 #endif
00163 
00164 
00170 class GURLS_EXPORT OptFunction: public GurlsOption
00171 {
00172 protected:
00173     std::string name; 
00174     Functor *f;       
00175 
00176 public:
00177 
00181     OptFunction();
00182 
00186     OptFunction(std::string func_name);
00187 
00191     ~OptFunction();
00192 
00196     OptFunction& operator=(const OptFunction& other);
00197 
00201     std::string getName() const;
00202 
00206     template<typename T>
00207     T getValue(T* array, int n) const
00208     {
00209         return (*f)(array, n);
00210     }
00211 
00215     virtual bool isA(OptTypes id) const;
00216 
00220     static OptFunction* dynacast(GurlsOption* opt);
00221 
00225     static const OptFunction* dynacast(const GurlsOption* opt);
00226 
00230     virtual std::ostream& operator<<(std::ostream& os) const;
00231 
00232 
00233 protected:
00234 
00238     void setValue(std::string func_name);
00239 
00240 };
00241 
00242 
00243 #ifdef _WIN32
00244 #pragma warning(pop)
00245 #endif
00246 
00247 
00248 }
00249 
00250 #endif //_GURLS_OPTFUNCTION_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends