text:
#include <NTL/quad_float.h>
#include <NTL/RR.h>

#include <cfloat>

NTL_START_IMPL

#if (NTL_BITS_PER_LONG >= NTL_DOUBLE_PRECISION)

quad_float to_quad_float(long n)
{
   double xhi, xlo;

   xhi = TrueDouble(n);

   // Because we are assuming 2's compliment integer
   // arithmetic, the following prevents long(xhi) from overflowing.

   if (n > 0)
      xlo = TrueDouble(n+long(-xhi));
   else
      xlo = TrueDouble(n-long(xhi));

   // renormalize...just to be safe

   quad_float z;
   quad_float_normalize(z, xhi, xlo);
   return z;
}

quad_float to_quad_float(unsigned long n)
{
   double xhi, xlo, t;

   const double bnd = double(1L << (NTL_BITS_PER_LONG-2))*4.0;

   xhi = TrueDouble(n);

   if (xhi >= bnd)
      t = xhi - bnd;
   else
      t = xhi;

   // we use the "to_long" function here to be as portable as possible.
   long llo = to_long(n - (unsigned long)(t));
   xlo = TrueDouble(llo);

   quad_float z;
   quad_float_normalize(z, xhi, xlo);
   return z;
}
#endif

NTL_CHEAP_THREAD_LOCAL
long quad_float::oprec = 10;

void quad_float::SetOutputPrecision(long p)
{
   if (p < 1) p = 1;

   if (NTL_OVERFLOW(p, 1, 0))
      ResourceError("quad_float: output precision too big");

   oprec = p;
}

void power(quad_float& z, const quad_float& a, long e)
{
   quad_float res, u;
   unsigned long k;

   if (e < 0)
      k = -((unsigned long) e);
   else
      k = e;

   res = 1.0;
   u = a;

   while (k) {
      if (k & 1)
         res = res * u;

      k = k >> 1;
      if (k)
         u = u * u;
   }

   if (e < 0)
      z = 1.0/res;
   else
      z = res;
}

void power2(quad_float& z, long e)
{
   z.hi = _ntl_ldexp(1.0, e);
   z.lo = 0;
}

long to_long(const quad_float& x)
{
   double fhi, flo;

   fhi = floor(x.hi);

   if (fhi == x.hi)
      flo = floor(x.lo);
   else
      flo = 0;

   // the following code helps to prevent unnecessary integer overflow,
   // and guarantees that to_long(to_quad_float(a)) == a, for all long a,
   // provided long's are not too wide.

   if (fhi > 0)
      return long(flo) - long(-fhi);
   else
      return long(fhi) + long(flo);
}

// This version of ZZ to quad_float coversion relies on the
// precise rounding rules implemented by the ZZ to double conversion.

void conv(quad_float& z, const ZZ& a)
{
   double xhi, xlo;

   conv(xhi, a);

   if (!IsFinite(&xhi)) {
      z.hi = xhi;
      z.lo = 0;
      return;
   }

   NTL_ZZRegister(t);

   conv(t, xhi);
   sub(t, a, t);

   conv(xlo, t);

   quad_float_normalize(z, xhi, xlo);
}

void conv(ZZ& z, const quad_float& x)
{
   NTL_ZZRegister(t1);
   NTL_ZZRegister(t2);
   NTL_ZZRegister(t3);

   double fhi, flo;

   fhi = floor(x.hi);

   if (fhi == x.hi) {
      flo = floor(x.lo);

      conv(t1, fhi);
      conv(t2, flo);

      add(z, t1, t2);
   }
   else
      conv(z, fhi);
}

ostream& operator<<(ostream& s, const quad_float& a)
{
   quad_float aa = a;

   if (!IsFinite(&aa)) {
      s << "NaN";
      return s;
   }

   RRPush push;
   RROutputPush opush;

   RR::SetPrecision(long(3.33*quad_float::oprec) + 10);
   RR::SetOutputPrecision(quad_float::oprec);

   NTL_TLS_LOCAL(RR, t);

   conv(t, a);
   s << t;

   return s;
}

istream& operator>>(istream& s, quad_float& x)
{
   RRPush push;
   RR::SetPrecision(4*NTL_DOUBLE_PRECISION);

   NTL_TLS_LOCAL(RR, t);
   NTL_INPUT_CHECK_RET(s, s >> t);
   conv(x, t);

   return s;
}

void random(quad_float& x)
{
   RRPush push;
   RR::SetPrecision(4*NTL_DOUBLE_PRECISION);

   NTL_TLS_LOCAL(RR, t);
   random(t);
   conv(x, t);
}

quad_float random_quad_float()
{
   quad_float x;
   random(x);
   return x;
}

long IsFinite(quad_float *x)
{
   return IsFinite(&x->hi) && IsFinite(&x->lo);
}

quad_float floor(const quad_float& x)
{
   double fhi = floor(x.hi);

   if (fhi != x.hi)
      return quad_float(fhi, 0.0);
   else {
      double flo = floor(x.lo);
      quad_float z;
      quad_float_normalize(z, fhi, flo);
      return z;
   }
}

quad_float ceil(const quad_float& x) {
  return -floor(-x);
}

quad_float trunc(const quad_float& x) {
  if (x>=0.0) return floor(x); else return -floor(-x);
}

long compare(const quad_float& x, const quad_float& y)
{
   if (x.hi > y.hi)
      return 1;
   else if (x.hi < y.hi)
      return -1;
   else if (x.lo > y.lo)
      return 1;
   else if (x.lo < y.lo)
      return -1;
   else
      return 0;
}

quad_float fabs(const quad_float& x)
{ if (x.hi>=0.0) return x; else return -x; }

quad_float ldexp(const quad_float& x, long exp) { // x*2^exp
   double xhi, xlo;
   quad_float z;

   xhi = _ntl_ldexp(x.hi, exp);
   xlo = _ntl_ldexp(x.lo, exp);

   quad_float_normalize(z, xhi, xlo);
   return z;
}

quad_float exp(const quad_float& x) { // New version 97 Aug 05
/*
!  Calculate a quadruple-precision exponential
!  Method:
!   x    x.log2(e)    nint[x.log2(e)] + frac[x.log2(e)]
!  e  = 2          = 2
!
!                     iy    fy
!                  = 2   . 2
!  Then
!   fy    y.loge(2)
!  2   = e
!
!  Now y.loge(2) will be less than 0.3466 in absolute value.
!  This is halved and a Pade aproximation is used to approximate e^x over
!  the region (-0.1733, +0.1733).   This approximation is then squared.
*/
  if (x.hi<DBL_MIN_10_EXP*2.302585092994045684017991)
    return to_quad_float(0.0);
  if (x.hi>DBL_MAX_10_EXP*2.302585092994045684017991) {
    ResourceError("exp(quad_float): overflow");
  }

  static const quad_float Log2 = to_quad_float("0.6931471805599453094172321214581765680755");
  // GLOBAL (assumes C++11 thread-safe init)

  quad_float y,temp,ysq,sum1,sum2;
  long iy;
  y=x/Log2;
  temp = floor(y+0.5);
  iy = to_long(temp);
  y=(y-temp)*Log2;
  y=ldexp(y,-1L);
  ysq=y*y;
  sum1=y*((((ysq+3960.0)*ysq+2162160.0)*ysq+302702400.0)*ysq+8821612800.0);
  sum2=(((90.0*ysq+110880.0)*ysq+30270240.0)*ysq+2075673600.0)*ysq+17643225600.0;
/*
!                     sum2 + sum1         2.sum1
! Now approximation = ----------- = 1 + ----------- = 1 + 2.temp
!                     sum2 - sum1       sum2 - sum1
!
! Then (1 + 2.temp)^2 = 4.temp.(1 + temp) + 1
*/
  temp=sum1/(sum2-sum1);
  y=temp*(temp+1);
  y=ldexp(y,2L);
  return ldexp(y+1,iy);
}

quad_float log(const quad_float& t) { // Newton method. See Bailey, MPFUN
  if (t.hi <= 0.0) {
    ArithmeticError("log(quad_float): argument must be positive");
  }

  quad_float s = to_quad_float(log(t.hi));
  // NOTE: in case log yields excess precision, this assumes
  // that to_quad_float removes it

  quad_float e = exp(s);
  return s+(t-e)/e;  // Newton step
}

quad_float sqrt(const quad_float& y)
{
  if (y.hi < 0.0)
    ArithmeticError("quad_float: square root of negative number");
  if (y.hi == 0.0) return quad_float(0.0,0.0);

  double c = TrueDouble(sqrt(y.hi));
  // NOTE: we call TrueDouble, just in case sqrt yields excess precision

  quad_float yy = y;
  quad_float_in_place_sqrt(yy, c);
  return yy;
}

long operator> (const quad_float& x, const quad_float& y) {
   return (x.hi> y.hi) || (x.hi==y.hi && x.lo> y.lo); }
long operator>=(const quad_float& x, const quad_float& y) {
   return (x.hi>y.hi) || (x.hi==y.hi && x.lo>=y.lo); }
long operator< (const quad_float& x, const quad_float& y) {
   return (x.hi< y.hi) || (x.hi==y.hi && x.lo< y.lo); }
long operator<=(const quad_float& x, const quad_float& y) {
   return (x.hi<y.hi) || (x.hi==y.hi && x.lo<=y.lo); }
long operator==(const quad_float& x, const quad_float& y)
   { return x.hi==y.hi && x.lo==y.lo; }
long operator!=(const quad_float& x, const quad_float& y)
   { return x.hi!=y.hi || x.lo!=y.lo; }

NTL_END_IMPL


text:
#define BOOST_TEST_MODULE TreeTests

#include <boost/test/unit_test.hpp>
#include <Tree.ipp>
#include <string>

using namespace tree;

BOOST_AUTO_TEST_CASE( ConstructorTest )
{
  Tree<int> t;
  BOOST_CHECK_EQUAL(0, t.inOrder()->size());
}

BOOST_AUTO_TEST_CASE( AddOneElementTest )
{
  Tree<int> t;
  t.add(3);
  std::list<int> *listInOrder = t.inOrder();
  BOOST_CHECK_EQUAL(1, listInOrder->size());
  BOOST_CHECK_EQUAL(3, listInOrder->front());
  delete listInOrder;
}

BOOST_AUTO_TEST_CASE( InOrderTest )
{
  Tree<int> t;
  t.add(3);
  t.add(5);
  t.add(4);
  std::list<int> l;
  l.push_back(3);
  l.push_back(4);
  l.push_back(5);
  std::list<int> *listInOrder = t.inOrder();
  BOOST_CHECK_EQUAL(3, listInOrder->size());
  for (std::list<int>::iterator it = l.begin() ; it != l.end() ; ++it) {
    BOOST_CHECK_EQUAL(*it, listInOrder->front());
    listInOrder->pop_front();
  }
  delete listInOrder;
}

BOOST_AUTO_TEST_CASE( AddOneElementStringTest )
{
  Tree<std::string> t;
  t.add("there");
  t.add("hi");
  std::list<std::string> *listInOrder = t.inOrder();
  BOOST_CHECK_EQUAL(2, listInOrder->size());
  BOOST_CHECK_EQUAL("hi", listInOrder->front());
  listInOrder->pop_front();
  BOOST_CHECK_EQUAL("there", listInOrder->front());
  delete listInOrder;
}

ext: cpp

max_stars_repo_name: julitopower/cplusplusbaremetal

max_issues_repo_name: julitopower/cplusplusbaremetal

max_forks_repo_name: julitopower/cplusplusbaremetal


text:
/**
@file

Copyright John Reid 2006

*/
#ifdef _MSC_VER
# pragma warning( disable : 4503 )
#endif

#include "biopsy/custom_pssm.h"
#include "biopsy/sequence.h"
#include "biopsy/transfac.h"

#include <bio/biobase_filter.h>
#include <bio/cache.h>
#include <bio/singleton.h>
#include <bio/environment.h>

#include <boost/filesystem/convenience.hpp>
#include <boost/spirit/home/classic/phoenix/binders.hpp>
#include <boost/spirit/home/classic/core/composite/kleene_star.hpp>
#include <boost/spirit/home/classic/utility/lists.hpp>
#include <boost/spirit/include/classic_file_iterator.hpp>
#include <boost/spirit/include/classic_core.hpp>
#include <boost/spirit/include/classic_attribute.hpp>
#if BOOST_VERSION >= 103500
 using namespace BOOST_SPIRIT_CLASSIC_NS;
#endif

using namespace boost::spirit;
using namespace phoenix;
using namespace boost;
using namespace std;

#ifdef WIN32
# define DIR_SEP "\\"
#else //WIN32
# define DIR_SEP "/"
#endif //WIN32

namespace biopsy {

namespace detail {

struct CustomPssmFromName
    : std::unary_function< std::string, custom_pssm::ptr >
{
    custom_pssm::ptr operator()( const std::string & name ) const
    {
        return parse_custom_pssm_file( custom_pssm_filename( name ) ); //the name should be the PSSM id
    }
};

struct custom_pssm_cache
    : BIO_NS::Cache< CustomPssmFromName >
    , BIO_NS::Singleton< custom_pssm_cache >
{
};

struct custom_pssm_info
{
    string name;
    string id;
    string url;
    vector< unsigned > positions;
    vector< double > counts_a, counts_c, counts_g, counts_t;
    vector< string > iupac;
    unsigned width;

    custom_pssm::ptr create_pssm() const
    {
        //calculate pssm
        custom_pssm::ptr result( new custom_pssm );
        result->name = name;
        result->id = id;
        result->url = url;
        result->counts.resize( width );
        for( unsigned i = 0; width != i; ++i )
        {
            //const double sum = counts_a[i] + counts_c[i] + counts_g[i] + counts_t[i];
            result->counts[i].set( 0, counts_a[i] );
            result->counts[i].set( 1, counts_c[i] );
            result->counts[i].set( 2, counts_g[i] );
            result->counts[i].set( 3, counts_t[i] );
        }
        return result;
    }
};

struct custom_pssm_closure : BOOST_SPIRIT_CLASSIC_NS::closure< custom_pssm_closure, custom_pssm_info >
{
    member1 pssm;
};

struct push_back_impl {

    template< typename Arg1, typename Arg2 >
    struct result { typedef void type; };

    template< typename Arg1, typename Arg2 >
    void operator()( Arg1 & container, Arg2 value ) const
    {
        container.push_back(value);
    }
};

struct insert_impl {

    template< typename Arg1, typename Arg2 >
    struct result { typedef void type; };

    template< typename Arg1, typename Arg2 >
    void operator()( Arg1 & container, Arg2 value ) const
    {
        container.insert(value);
    }
};

struct custom_pssm_grammar : public grammar< custom_pssm_grammar, custom_pssm_closure::context_t >
{
    template< typename ScannerT >
    struct definition
    {
        typedef rule< ScannerT > rule_t;
        rule_t custom_pssm_rule, line_rule, counts_a_rule, counts_c_rule, counts_g_rule, counts_t_rule, position_rule, iupac_rule;
        definition( custom_pssm_grammar const& g )
        {
            using phoenix::function;

            member_var_ptr< string, custom_pssm_info > name = &custom_pssm_info::name;
            member_var_ptr< string, custom_pssm_info > id = &custom_pssm_info::id;
            member_var_ptr< string, custom_pssm_info > url = &custom_pssm_info::url;
            member_var_ptr< unsigned, custom_pssm_info > width = &custom_pssm_info::width;
            member_var_ptr< vector< double >, custom_pssm_info > counts_a = &custom_pssm_info::counts_a;
            member_var_ptr< vector< double >, custom_pssm_info > counts_c = &custom_pssm_info::counts_c;
            member_var_ptr< vector< double >, custom_pssm_info > counts_g = &custom_pssm_info::counts_g;
            member_var_ptr< vector< double >, custom_pssm_info > counts_t = &custom_pssm_info::counts_t;
            member_var_ptr< vector< unsigned >, custom_pssm_info > positions = &custom_pssm_info::positions;
            member_var_ptr< vector< string >, custom_pssm_info > iupac = &custom_pssm_info::iupac;

            function< push_back_impl > push_back;

            counts_a_rule = list_p.direct( real_p[ push_back( counts_a(g.pssm), arg1 ) ], eps_p );
            counts_c_rule = list_p.direct( real_p[ push_back( counts_c(g.pssm), arg1 ) ], eps_p );
            counts_g_rule = list_p.direct( real_p[ push_back( counts_g(g.pssm), arg1 ) ], eps_p );
            counts_t_rule = list_p.direct( real_p[ push_back( counts_t(g.pssm), arg1 ) ], eps_p );
            position_rule = list_p.direct( uint_p[ push_back( positions(g.pssm), arg1 ) ], eps_p );
            iupac_rule = list_p.direct( (+range_p('A','Z'))[ push_back( iupac(g.pssm), construct_< string >(arg1, arg2) ) ], eps_p );

            line_rule =
                ( ch_p('#') >> *(print_p-eol_p) )
                | ( str_p("NA  ") >> (+(print_p-eol_p))[ assign_a( name(g.pssm)() ) ] )
                | ( str_p("ID  ") >> (+(print_p-eol_p))[ assign_a( id(g.pssm)() ) ] )
                | ( str_p("WI  ") >> uint_p[ width(g.pssm) = arg1 ] )
                | ( str_p("CA  ") >> counts_a_rule )
                | ( str_p("CC  ") >> counts_c_rule )
                | ( str_p("CG  ") >> counts_g_rule )
                | ( str_p("CT  ") >> counts_t_rule )
                | ( str_p("PO  ") >> position_rule )
                | ( str_p("IU  ") >> iupac_rule )
                | ( str_p("UR  ") >> (+graph_p)[ assign_a( url(g.pssm)() ) ] )
                | eps_p
                ;

            custom_pssm_rule = list_p( line_rule, +eol_p ) ;
        }
        rule_t const& start() const { return custom_pssm_rule; }
    };
};

struct pssm_set_closure : BOOST_SPIRIT_CLASSIC_NS::closure< pssm_set_closure, pssm_set >
{
    member1 _set;
};

struct pssm_set_grammar : public grammar< pssm_set_grammar, pssm_set_closure::context_t >
{
    template< typename ScannerT >
    struct definition
    {
        typedef rule< ScannerT > rule_t;
        rule_t pssm_set_rule, line_rule;
        definition( pssm_set_grammar const& g )
        {
            using phoenix::function;

            function< insert_impl > insert;

            line_rule =
                ( ch_p('#') >> *(print_p-eol_p) ) //ignore comments
                | list_p.direct( (+graph_p)[ insert( g._set, construct_< std::string >( arg1, arg2 ) ) ], blank_p ) //whitespace separated list of strings
                | eps_p //empty line
                ;

            pssm_set_rule = list_p( line_rule, +eol_p ) ;
        }
        rule_t const& start() const { return pssm_set_rule; }
    };
};

} //namespace detail

custom_pssm::ptr parse_custom_pssm_file( const std::string & custom_pssm_filename )
{
    using namespace boost::spirit;
    using namespace std;

    //open the file
    file_iterator<> first( custom_pssm_filename.c_str() );
    if( !first ) throw std::logic_error(BIOPSY_MAKE_STRING( "Could not open: " << custom_pssm_filename ));
    file_iterator<> last = first.make_end();

    typedef char                     char_t;
    typedef file_iterator<char_t>    iterator_t;

    //parse the file
    detail::custom_pssm_grammar g;
    detail::custom_pssm_info pssm_info;
    parse_info<iterator_t> info = parse( first, last, g[ var(pssm_info)=arg1 ], blank_p );
    if( ! info.full )
    {
        const string stopped_at(info.stop, last);
        throw std::logic_error(BIOPSY_MAKE_STRING( "Could not parse entire file, stopped at: \"" << stopped_at << "\""));
    }

    //done parsing - check what we have makes sense
    if( pssm_info.width != pssm_info.counts_a.size()
        || pssm_info.width != pssm_info.counts_c.size()
        || pssm_info.width != pssm_info.counts_g.size()
        || pssm_info.width != pssm_info.counts_t.size()
        //|| pssm_info.counts_a.size() != pssm_info.positions.size()
        //|| pssm_info.counts_a.size() != pssm_info.iupac.size()
    ) throw std::logic_error( "Wrong number of entries in A, C, G, or T counts" );

    return pssm_info.create_pssm();
}

std::string custom_pssm_filename( const std::string & pssm_id )
{
    return BIOPSY_MAKE_STRING(
        BIO_NS::BioEnvironment::singleton().get_custom_pssm_dir()
        << DIR_SEP
        << pssm_id
        << ".pssm" )
        ;
}

custom_pssm::ptr get_custom_pssm( const std::string & pssm_id )
{
    return detail::custom_pssm_cache::singleton()( pssm_id );
}

pssm_set_ptr get_pssm_set( const std::string & name )
{
    namespace fs = boost::filesystem;
    const fs::path custom_pssm_dir = fs::system_complete( fs::path( BIO_NS::BioEnvironment::singleton().get_custom_pssm_dir() ) );
    const fs::path pssm_set_file = custom_pssm_dir / (name + ".pssm_set");

    //open the file
    file_iterator<> first( pssm_set_file.string() );
    if( !first ) throw std::logic_error(BIOPSY_MAKE_STRING( "Could not open: " << pssm_set_file.string() ));
    file_iterator<> last = first.make_end();

    typedef char                     char_t;
    typedef file_iterator<char_t>    iterator_t;

    //parse the file
    detail::pssm_set_grammar g;
    pssm_set _pssm_set;
    parse_info< iterator_t > info = parse( first, last, g[ var(_pssm_set) = arg1 ] );
    if( ! info.full )
    {
        const string stopped_at(info.stop, last);
        throw std::logic_error(BIOPSY_MAKE_STRING( "Could not parse entire file, stopped at: \"" << stopped_at << "\""));
    }

    pssm_set_ptr result( new pssm_set( _pssm_set ) );

    return result;
}

pssm_set_ptr all_custom_pssms()
{
    USING_BIO_NS;

    pssm_set_ptr result( new pssm_set );

    namespace fs = boost::filesystem;
    const std::string custom_pssm_dir = BioEnvironment::singleton().get_custom_pssm_dir();
    const fs::path full_path = fs::system_complete( fs::path( custom_pssm_dir ) );
    if ( !fs::exists( full_path ) ) throw std::logic_error( BIOPSY_MAKE_STRING( "Custom pssm dir does not exist: " << full_path.string() ) );
    if ( ! fs::is_directory( full_path ) ) throw std::logic_error( BIOPSY_MAKE_STRING( "Custom pssm dir is not a directory: " << full_path.string() ) );
    fs::directory_iterator end_iter;
    for( fs::directory_iterator dir_itr( full_path );
          dir_itr != end_iter;
          ++dir_itr )
    {
        if ( fs::extension( dir_itr->path() ) == ".pssm" )
        {
            result->insert( fs::basename( dir_itr->path() ) );
        }
    }

    return result;
}

std::vector< std::string > get_installed_custom_pssm_set_names()
{
    //implementation essentially same as all_custom_pssms()
    USING_BIO_NS;

    std::vector< std::string > result;

    namespace fs = boost::filesystem;
    const std::string custom_pssm_dir = BioEnvironment::singleton().get_custom_pssm_dir();
    const fs::path full_path = fs::system_complete( fs::path( custom_pssm_dir ) );
    if ( !fs::exists( full_path ) ) throw std::logic_error( BIOPSY_MAKE_STRING( "Custom pssm dir does not exist: " << full_path.string() ) );
    if ( ! fs::is_directory( full_path ) ) throw std::logic_error( BIOPSY_MAKE_STRING( "Custom pssm dir is not a directory: " << full_path.string() ) );
    fs::directory_iterator end_iter;
    for( fs::directory_iterator dir_itr( full_path );
          dir_itr != end_iter;
          ++dir_itr )
    {
        if ( fs::extension( dir_itr->path() ) == ".pssm_set" )
        {
            result.push_back( fs::basename( dir_itr->path() ) );
        }
    }

    return result;
}

void add_pssms_from_pssm_set(
    pssm_set & pssms,
    const std::string & pssm_set_name,
    bool use_transfac_consensus_sequences,
    const std::string & matrix_species,
    const std::string & matrix_name_match )
{
    if( pssm_set_name == "transfac" )
    {
        BIO_NS::BiobasePssmFilter filter(
            use_transfac_consensus_sequences,
            matrix_species,
            matrix_name_match );
        string_vec_ptr transfac_pssm_names = get_transfac_pssm_accessions( filter );
        std::copy( transfac_pssm_names->begin(), transfac_pssm_names->end(), inserter( pssms, pssms.end() ) );
    }
    else
    {
        pssm_set_ptr _pssm_set = pssm_set_name == "all-custom" ? all_custom_pssms() : get_pssm_set( pssm_set_name );
        std::copy( _pssm_set->begin(), _pssm_set->end(), inserter( pssms, pssms.end() ) );
    }
}

/**
Get the list of the custom pssms using boost python compatible type*/
string_vec_ptr
get_all_custom_pssms()
{
    pssm_set_ptr all_pssms = all_custom_pssms();
    string_vec_ptr result( new string_vec );
    BOOST_FOREACH(std::string pssm, *all_pssms)
        result->push_back(pssm);
    return result;
}

/**
Get the list of the custom pssms in boost python compatible type*/
string_vec_ptr
get_custom_pssms(const std::string & pssm_set_name)
{
    pssm_set_ptr all_pssms = get_pssm_set(pssm_set_name);
    string_vec_ptr result( new string_vec );
    BOOST_FOREACH(std::string pssm, *all_pssms)
        result->push_back(pssm);
    return result;
}

/**
Get the list of the custom pssms in boost python compatible type*/
string_vec_ptr
get_custom_pssm_set_names()
{
    std::vector< std::string > names = get_installed_custom_pssm_set_names();

    string_vec_ptr result( new string_vec );
    BOOST_FOREACH(std::string name,names)
    {
        result->push_back(name);
    }
    return result;
}

} //namespace biopsy

max_stars_repo_name: JohnReid/biopsy

max_issues_repo_name: JohnReid/biopsy

max_forks_repo_name: JohnReid/biopsy


text:
///////////////////////////////////////////////////////////////////////////////
// boost::statistics::detail::functor::range_visitor.hpp                     //
//                                                                           //
//  Copyright 2009 Erwann Rogard. Distributed under the Boost                //
//  Software License, Version 1.0. (See accompanying file                    //
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)         //
///////////////////////////////////////////////////////////////////////////////
#ifndef BOOST_STATISTICS_DETAIL_FUNCTOR_RANGE_VISITOR_HPP_ER_2009
#define BOOST_STATISTICS_DETAIL_FUNCTOR_RANGE_VISITOR_HPP_ER_2009
#include <iterator>
#include <boost/range.hpp>
#include <boost/utility.hpp>

namespace boost{
namespace statistics{
namespace detail{
namespace functor{

    // Models Generator
    //
    // Successively returns elements of a range
    template<typename R>
    class range_visitor{
        typedef typename range_iterator<R>::type it_t;
        typedef std::size_t size_type;

        public:
        range_visitor(){}
        range_visitor(const R& r):r_(r){ this-> reset(0); }
        range_visitor(const range_visitor& that)
            :r_(that.r_)
        {
            this->reset(
                std::distance(
                    boost::begin(that.r_),
                    that.it_
                )
            );
        }
        range_visitor& operator=(const range_visitor& that)
        {
            if(&that!=this){
                this->r_ = that.r_;
                this->reset(
                    std::distance(
                        boost::begin(that.r_),
                        that.it_
                    )
                );
            }
            return *this;
        }

        typedef typename range_value<R>::type result_type;

        result_type operator()(){
            typedef typename range_visitor<R>::result_type x_;
            return *(this->it_++);
        }

        void reset(size_type n)
        {
            this->it_ = boost::next(boost::begin(this->r_),n);
        }

        private:
        R r_;
        it_t it_;
    };

}// functor
}// detail
}// statistics
}// boost

#endif

ext: hpp


text:
/**
* \file
* The most simple example.
* install: `yaourt -S aur/libcurlpp`
* [curlpp examples](https://github.com/jpbarrette/curlpp/tree/master/examples)
* [curlpp download wnd write callback example](https://github.com/datacratic/curlpp/blob/master/examples/example11.cpp)
* [Link errors with curlpp](http://stackoverflow.com/questions/33420631/link-errors-with-curlpp)
*/

#include <iostream>
#include <sstream>
#include <string>
#include <regex>
#include <vector>
#include <queue> // for keeping directories list
#include <thread>
#include <mutex>
#include <condition_variable>

#include <boost/algorithm/string/predicate.hpp>

// curlpp specific headers
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>

// pugixml
#include <pugixml.hpp>

// [clean html with C or C++ tidy](http://www.cppfun.com/clean-html-with-c-or-cpp-tidy.htm)
// [tidy 5.2.0-1](https://www.archlinux.org/packages/extra/x86_64/tidy/)
// `pacman -S extra/tidy`

#include <tidy.h>
#include <buffio.h>

std::string cleanhtml(const std::string &html)
{
    // init a tidy document
    TidyDoc tidy_doc=tidyCreate();
    TidyBuffer output_buffer= {0};

    // configure tidy
    // the flags tell tidy to output xml and disable warnings
    bool config_success=tidyOptSetBool(tidy_doc,TidyXmlOut,yes)
                        && tidyOptSetBool(tidy_doc,TidyQuiet,yes)
                        && tidyOptSetBool(tidy_doc,TidyNumEntities,yes)
                        && tidyOptSetBool(tidy_doc,TidyShowWarnings,no);

    int tidy_rescode=-1;

    // parse input
    if(config_success)
        tidy_rescode=tidyParseString(tidy_doc,html.c_str());

    // process html
    if(tidy_rescode>=0)
        tidy_rescode=tidySaveBuffer(tidy_doc,&output_buffer);

    if(tidy_rescode<0)
        throw("tidy has a error: "+tidy_rescode);

    std::string result=(char *)output_buffer.bp;
    tidyBufFree(&output_buffer);
    tidyRelease(tidy_doc);

    return result;
}

using namespace curlpp::options;

bool is_ready(false);
std::mutex m;
std::condition_variable cv;

struct simple_walker: pugi::xml_tree_walker {
    virtual bool for_each(pugi::xml_node& node) {
        for (int i = 0; i < depth(); ++i) std::cout << "  "; // indentation
        std::cout << /* node_types[node.type()] << */ ": name='" << node.name() << "', value='" << node.value() << "'\n";
        return true; // continue traversal
    }
};

void
getWebPage(std::string const& url, std::string& page)
{
    try {
        // That's all that is needed to do cleanup of used resources (RAII style).
        curlpp::Cleanup myCleanup;

        // Our request to be sent.
        curlpp::Easy myRequest;

        // Set the URL.
        myRequest.setOpt(new curlpp::options::Url(url));

        // Send request and get a result.
        // By default the result goes to standard output.

        std::ostringstream os;
        curlpp::options::WriteStream ws(&os);
        myRequest.setOpt(ws);
        myRequest.perform();

        page = std::move(os.str());

    } catch(curlpp::RuntimeError & e) {
        std::cout << e.what() << std::endl;
    }

    catch(curlpp::LogicError & e) {
        std::cout << e.what() << std::endl;
    }
}

void
dirList(std::string const& html, std::vector<std::string>& attrs)
{
    std::string cleaned_xml{cleanhtml(html)};
    pugi::xml_document doc;
    pugi::xml_parse_result result = doc.load_string(cleaned_xml.c_str());
    auto table = doc.child("html").child("body").child("table");
    for (auto it = table.begin(); it != table.end(); ++it) {
        auto td = it->child("td").child("a");
        for (pugi::xml_attribute_iterator ait = td.attributes_begin(); ait != td.attributes_end(); ++ait) {
            attrs.emplace_back(ait->value());
        }
    }
}

void
parseManifest(std::string const& manifest_xml, std::vector<std::string>& attrs)
{
    pugi::xml_document doc;
    pugi::xml_parse_result result = doc.load_string(manifest_xml.c_str());

    if (result) {
        std::cout << "XML parsed without errors\n";
    } else {
        std::cout << "Error description: " << result.description() << "\n";
    }

    attrs.push_back(doc.child("metadata").child("version").child_value());
    attrs.push_back(doc.child("metadata").child("versioning").child("lastUpdated").child_value());
}

// convenience methods
bool
isDirectory(std::string const& path)
{
    return boost::ends_with(path, "/");
}

// is it ".." or "."
bool
isSpecialDir(std::string const& path)
{
    return boost::ends_with(path, "../")
        || boost::ends_with(path, "./");
}

bool
traverseDirectories(std::string const& url, std::vector<std::string>& flist)
{
    std::queue<std::string> directories;
    directories.push(url);
    while (!directories.empty()) {
        std::string dir = directories.front(), page;
        directories.pop();
        getWebPage(dir, std::ref(page));
        std::vector<std::string> dirlist;
        dirList(page, dirlist);
        for (std::string const& d : dirlist) {
            if (!isSpecialDir(d)) { // ignore ``..'' and ``.''
                if (isDirectory(d)) { // add to queue
                    directories.push(d);
                } else {
                    flist.push_back(d);
                }
            }
        }
    }
}

bool
extractManifestAttrs(std::string const& url, std::vector<std::string>& attrs)
{
    std::unique_lock<std::mutex> lk(m);
    std::vector<std::string> flist;

    // get file list after recursive directories traversal
    traverseDirectories(url, flist);
    std::for_each(flist.begin(), flist.end(),
            [](auto const& f){ std::cout << "f: " << f << std::endl; });

    // find manifest file from the file list
    std::regex manifest_exp(url+"(.+?)(metadata\\.xml)");
    auto manifest_it = std::find_if(flist.begin(), flist.end(),
            [=](std::string const& f)->bool{ return std::regex_match(f,manifest_exp); });

    // load manifest file and parse it
    std::string manifest;
    getWebPage(*manifest_it, std::ref(manifest));
    parseManifest(manifest, attrs);

    is_ready = true;
    cv.notify_one();
}

int main(int, char **)
{
    std::string url1 = "https://oss.sonatype.org/content/repositories/snapshots/edu/berkeley/cs/chisel3_2.11/";
    std::vector<std::string> manifest_ver_attrs;
    std::thread t1(extractManifestAttrs, url1, std::ref(manifest_ver_attrs));
    std::unique_lock<std::mutex> lk(m);
    cv.wait(lk, []{ return is_ready;} );
    t1.join();

    for (auto const& attr : manifest_ver_attrs) {
        std::cout << attr << std::endl;
    }

    return 0;
}

max_stars_repo_name: alsam/cpp-samples

max_issues_repo_name: alsam/cpp-samples

max_forks_repo_name: alsam/cpp-samples


text:
//==================================================================================================
/*
  Copyright 2016 NumScale SAS

  Distributed under the Boost Software License, Version 1.0.
  (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/
//==================================================================================================

#include <boost/simd/function/multiplies.hpp>
#include <boost/simd/function/plus.hpp>
#include <boost/simd/function/splat.hpp>
#include <boost/simd/pack.hpp>

//! [scalar-dot]
template <typename Value>
Value dot(Value* first1, Value* last1, Value* first2)
{
  Value v(0);

  for (; first1 < last1; ++first1, ++first2) {
    v += (*first1) * (*first2);
  }

  return v;
}
//! [scalar-dot]


text:
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)

// Copyright Paul A. Bristow 2015.
// Copyright Christopher Kormanyos 2015.
// Copyright Nikhar Agrawal 2015.

// This file is written to be included from a Quickbook .qbk document.
// It can be compiled by the C++ compiler, and run. Any output can
// also be added here as comment or included or pasted in elsewhere.
// Caution: this file contains Quickbook markup as well as code
// and comments: don't change any of the special comment markups!

// This file also includes Doxygen-style documentation about the function of the code.
// See http://www.doxygen.org for details.

//! \file

// Below are snippets of code that can be included into a Quickbook file.

#include <iostream>
#include <iomanip>
#include <exception>
#include <typeinfo>
#include <limits>

#include <boost/cstdint.hpp>

//[fixed_point_include_1
#include <boost/fixed_point/fixed_point.hpp>
//] [/fixed_point_include_1]

/*
! Show numeric_limits values for a type.
\tparam T floating-point, fixed-point type.
\param os std::ostream, default @c std::cout
*/

// static int Functions to access template parameters for fixed_point
// that are missing for fundamental integral and floating-point types.
// Version selected on whether is not arithmetic, is floating_point or is integral.

template <typename NumericalType,
          typename EnableType = void>
struct numerical_details
{
  static int get_range     () { return 0; }
  static int get_resolution() { return 0; }
};

/*! Deduce fixed-point if @c std::is_class (so exclude @c bool, @c int...) and not arithmetic (exclude @c float, @c double...).
*/
template <typename NumericalType>
struct numerical_details<NumericalType,
                         typename std::enable_if<   (std::is_arithmetic<NumericalType>::value == false)
                                                 && (std::is_class<NumericalType>::value      == true)>::type>
{
  static int get_range     () { return NumericalType::range; }
  static int get_resolution() { return NumericalType::resolution; }
};

/*! Deduce fundamental floating-point type @c float, @c double or @c long double.
*/

template <typename NumericalType>
struct numerical_details<NumericalType,
                         typename std::enable_if<std::is_floating_point<NumericalType>::value>::type>
{
  static int get_range     () { return 0; }
  static int get_resolution() { return std::numeric_limits<NumericalType>::digits; }
};

/*! Deduce fundamental integral type.
*/
template <typename NumericalType>
struct numerical_details<NumericalType,
                         typename std::enable_if<std::is_integral<NumericalType>::value>::type>
{
  static int get_range     () { return std::numeric_limits<NumericalType>::digits; }
  static int get_resolution() { return 0; }
};

template <typename T>
void show_fixed_point(std::ostream& os = std::cout)
{
  using boost::fixed_point::negatable;

  os.precision(std::numeric_limits<T>::max_digits10);

  os << "Numeric_limits of type: "
     << typeid(T).name()
     << "\n range        = " << numerical_details<T>::get_range() //
     << "\n resolution   = " << numerical_details<T>::get_resolution()
     << "\n radix        = " << std::numeric_limits<T>::radix  // Always 2 for fixed-point.
     << "\n digits       = " << std::numeric_limits<T>::digits; // Does not include any sign bit.

  if (std::is_signed<T>::value == true)
  {
    os << "\n signed "
       << "\n total bits =   " << std::numeric_limits<T>::digits + 1; // DOES include sign bit.
  }

  if (std::numeric_limits<T>::is_exact == false)
  { // epsilon has meaning.
    os << "\n epsilon      = " << std::numeric_limits<T>::epsilon();
  }
  else
  {
    os << "\n exact        = "  << std::numeric_limits<T>::is_exact;
  }

  // Avoid char values showing as a character or squiggle.
  BOOST_CONSTEXPR_OR_CONST bool is_any_character_type = (   std::is_same<T, signed char>::value
                                                         || std::is_same<T, unsigned char>::value
                                                         || std::is_same<T, char16_t>::value
                                                         || std::is_same<T, char32_t>::value);

  os << "\n lowest       = ";
  if(is_any_character_type)
  {
    os << static_cast<boost::int32_t>(std::numeric_limits<T>::lowest());
  }
  else
  {
    os << std::numeric_limits<T>::lowest();
  }

  BOOST_CONSTEXPR_OR_CONST bool is_8bit_character_type = (   std::is_same<T, signed char>::value
                                                          || std::is_same<T, unsigned char>::value);

  os << "\n min          = ";
  if(is_8bit_character_type)
  {
    os << static_cast<boost::int32_t>((std::numeric_limits<T>::min)());
  }
  else
  {
    os << (std::numeric_limits<T>::min)();
  }

  os << "\n max          = ";
  if(is_8bit_character_type)
  {
    os << static_cast<boost::int32_t>((std::numeric_limits<T>::max)());
  }
  else
  {
    os << (std::numeric_limits<T>::max)();
  }

  os << "\n max_exponent = " << std::numeric_limits<T>::max_exponent
     << "\n min_exponent = " << std::numeric_limits<T>::min_exponent
     << "\n digits10     = " << std::numeric_limits<T>::digits10
     << "\n max_digits10 = " << std::numeric_limits<T>::max_digits10
     << "\n"
     << std::endl;
} // template <typename T> void show_fixed_point

int main()
{
  using boost::fixed_point::negatable;

  typedef negatable<15,  -16> fixed_point_type_15m16;
  typedef negatable<11,  -20> fixed_point_type_11m20;
  typedef negatable< 0,  -31> fixed_point_type_0m31;
  typedef negatable<29,   -2> fixed_point_type_29m2;
  typedef negatable<0,  -168> fixed_point_type_0m168;
  typedef negatable<20, -148> fixed_point_type_20m148;

  try
  {
    std::cout.setf(std::ios_base::boolalpha | std::ios_base::showpoint); // Show any trailing zeros.
    std::cout << std::endl;

//[fixed_example_1

    // Show all the significant digits for this particular type.

    // Fundamental (built-in) integral types.
    show_fixed_point<bool>              ();
    show_fixed_point<signed char>       ();
    show_fixed_point<unsigned char>     ();
    show_fixed_point<char16_t>          (); // Shows as type unsigned short.
    show_fixed_point<char32_t>          (); // Shows as type unsigned int.
    show_fixed_point<short int>         ();
    show_fixed_point<unsigned short int>();
    show_fixed_point<int>               ();
    show_fixed_point<unsigned int>      ();

   // Fundamental (built-in) floating-point types.
    show_fixed_point<float>();
    // digits 24 (leaving 8 for decimal exponent).
    // epsilon 1.2e-7.

    show_fixed_point<double>();
    // digits 53 (leaving 10 for decimal exponent).
    // epsilon 2.2e-16.
    show_fixed_point<long double>();
    // Varies with compiler
    // Using MSVC double == long double

//] [/fixed_example_1]

// Some fixed_point types using only a single 8-bit byte (signed char).

//[fixed_point_15m16

    // Some fixed_point types using 32 bits, and more.
    show_fixed_point<fixed_point_type_15m16> (); // Even split bits between range and resolution.
    show_fixed_point<fixed_point_type_11m20> (); // More resolution than range.
    show_fixed_point<fixed_point_type_0m31>  (); // All bits used for resolution.
    show_fixed_point<fixed_point_type_29m2>  (); // Most bits used for range.
    show_fixed_point<fixed_point_type_0m168> ();
    show_fixed_point<fixed_point_type_20m148>();

    //std::cout << "fixed_point_type(123) / 100 = "
    //  << x // 1.22999573 is the nearest representation of decimal digit string 1.23.
    //  << std::endl;
  }
  catch (std::exception ex)
  {
    std::cout << ex.what() << std::endl;
  }
}

/*
//[fixed_point_type_examples_output_1

//] [/fixed_point_type_examples_output_1]
*/


text:
#include <iostream>
#include <vector>

#include <dlib/svm.h>
#include <dlib/statistics.h>
#include <dlib/global_optimization.h>

#include <sampml/data.hpp>
#include <sampml/random_forest.hpp>

#include "common.hpp"
#include "transform.hpp"

using sample_type = output_vector;

void train() {
    std::cout << "TRAINING:\n";

    sampml::data::reader<sample_type> data_positive_train(positive_train_data);
    sampml::data::reader<sample_type> data_negative_train(negative_train_data);

    sampml::trainer::random_forest<sample_type> model;
    model.set_samples(data_positive_train, data_negative_train);
    //model.cross_validate();
    model.train();

    model.serialize(std::string(rf_model.begin(), rf_model.end()));
    std::cout << "\n\n";
}

void test() {
    const float cutoff = 0.5;

    std::cout << "TESTING:\n";
    sampml::data::reader<sample_type> data_positive_test(positive_test_data);
    sampml::data::reader<sample_type> data_negative_test(negative_test_data);

    sampml::trainer::random_forest<sample_type> model;
    model.deserialize(std::string(rf_model.begin(), rf_model.end()));

    int true_positives = 0,
        false_positives = 0,
        true_negatives = 0,
        false_negatives = 0;
    dlib::running_stats<double> values;
    for(const auto& v : data_positive_test) {
        double prob = model.test(v);
        if(prob > cutoff)
            true_positives++;
        else
            false_negatives++;
        values.add(prob);
    }
    std::cout << "positive test set statistics: " << '\n';
    std::cout << "average: " << values.mean() << ", stddev: " << values.stddev() << '\n'
              << "min: " << values.min() << ", max: " << values.max()  << '\n'
              << "skewness: " << values.skewness() << ", excess kurtosis: " << values.ex_kurtosis() << '\n';
    values.clear();

    for(const auto& v : data_negative_test) {
        double prob = model.test(v);
        if(prob < cutoff)
            true_negatives++;
        else
            false_positives++;
        values.add(prob);
    }
    std::cout << "negative test set statistics: " << '\n';
    std::cout << "average: " << values.mean() << ", stddev: " << values.stddev() << '\n'
              << "min: " << values.min() << ", max: " << values.max()  << '\n'
              << "skewness: " << values.skewness() << ", excess kurtosis: " << values.ex_kurtosis() << '\n';

    int num_correct = true_positives + true_negatives,
        num_wrong = false_positives + false_negatives;
    std::cout << "\n\n";
    std::cout << "true positives: " << true_positives << ", false positives: " << false_positives << '\n';
    std::cout << "true negatives: " << true_negatives << ", false negatives: " << false_negatives << '\n';
    std::cout << "number of samples classified corretly: " << num_correct << '\n';
    std::cout << "number of samples classified incorrectly: " << num_wrong << '\n';
    std::cout << "accuracy: " << num_correct/float(num_correct + num_wrong) << '\n';
}

int main () {
    train();
    test();
    return 0;
}

max_stars_repo_name: YashasSamaga/sampml

max_issues_repo_name: YashasSamaga/sampml

max_forks_repo_name: YashasSamaga/sampml


text:
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/point_generators_3.h>
#include <CGAL/Real_timer.h>

#include <CGAL/compute_average_spacing.h>
#include <CGAL/grid_simplify_point_set.h>
#include <CGAL/jet_smooth_point_set.h>

#include <boost/lexical_cast.hpp>

#include <vector>
#include <fstream>

// Types
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::FT FT;
typedef Kernel::Point_3 Point;
typedef CGAL::Random_points_on_sphere_3<Point> Generator;

// Concurrency
#ifdef CGAL_LINKED_WITH_TBB
typedef CGAL::Parallel_tag Concurrency_tag;
#else
typedef CGAL::Sequential_tag Concurrency_tag;
#endif

// instance of std::function<bool(double)>
struct Progress_to_std_cerr_callback
{
  mutable std::size_t nb;
  CGAL::Real_timer timer;
  double t_start;
  mutable double t_latest;
  const std::string name;

  Progress_to_std_cerr_callback (const char* name)
    : name (name)
  {
    timer.start();
    t_start = timer.time();
    t_latest = t_start;
  }

  bool operator()(double advancement) const
  {
    // Avoid calling time() at every single iteration, which could
    // impact performances very badly
    ++ nb;
    if (advancement != 1 && nb % 100 != 0)
      return true;

    double t = timer.time();
    if (advancement == 1 || (t - t_latest) > 0.1) // Update every 1/10th of second
    {
      std::cerr << "\r" // Return at the beginning of same line and overwrite
                << name << ": " << int(advancement * 100) << "%";

      if (advancement == 1)
        std::cerr << std::endl;
      t_latest = t;
    }

    return true;
  }
};

int main (int argc, char* argv[])
{
  int N = (argc > 1) ? boost::lexical_cast<int>(argv[1]) : 1000;

  // Generate N points on a sphere of radius 100.
  std::vector<Point> points;
  points.reserve (N);
  Generator generator(100.);
  std::copy_n (generator, N, std::back_inserter(points));

  // Compute average spacing
  FT average_spacing = CGAL::compute_average_spacing<Concurrency_tag>
    (points, 6,
     CGAL::parameters::callback
     (Progress_to_std_cerr_callback("Computing average spacing")));

  // Simplify on a grid with a size of twice the average spacing
  points.erase(CGAL::grid_simplify_point_set
               (points, 2. * average_spacing,
                CGAL::parameters::callback
                (Progress_to_std_cerr_callback("Grid simplification"))),
               points.end());

  // Smooth simplified point set
  CGAL::jet_smooth_point_set<Concurrency_tag>
    (points, 6,
     CGAL::parameters::callback
     (Progress_to_std_cerr_callback("Jet smoothing")));

  return EXIT_SUCCESS;
}

max_stars_repo_name: josuehfa/DAASystem

max_issues_repo_name: josuehfa/DAASystem

max_forks_repo_name: josuehfa/DAASystem


text:
//
// Created by jiashuai on 17-11-7.
//

#include <thundersvm/kernel/kernelmatrix_kernel.h>
#include <Eigen/Dense>
#include <Eigen/Sparse>
namespace svm_kernel {
    void
    get_working_set_ins(const SyncArray<kernel_type> &val, const SyncArray<int> &col_ind, const SyncArray<int> &row_ptr,
                            const SyncArray<int> &data_row_idx, SyncArray<kernel_type> &data_rows, int m, int n) {
        const int *data_row_idx_data = data_row_idx.host_data();
        kernel_type *data_rows_data = data_rows.host_data();
        const int *row_ptr_data = row_ptr.host_data();
        const int *col_ind_data = col_ind.host_data();
        const kernel_type *val_data = val.host_data();
#pragma omp parallel for schedule(guided)
        for (int i = 0; i < m; i++) {
            int row = data_row_idx_data[i];
            for (int j = row_ptr_data[row]; j < row_ptr_data[row + 1]; ++j) {
                int col = col_ind_data[j];
                data_rows_data[i * n + col] = val_data[j]; //row major
            }
        }
    }

    void
    get_working_set_ins(const SyncArray<kernel_type> &val, const SyncArray<int> &col_ind, const SyncArray<int> &row_ptr,
                        const SyncArray<int> &data_row_idx, SyncArray <kernel_type>& ws_val,
                        SyncArray<int> &ws_col_ind, SyncArray<int> &ws_row_ptr, int m){
        const int *data_row_idx_data = data_row_idx.host_data();
        const int *row_ptr_data = row_ptr.host_data();
        const int *col_ind_data = col_ind.host_data();
        const kernel_type *val_data = val.host_data();
//        kernel_type *ws_val_data = ws_val.host_data();
//        int *ws_row_ptr_data = ws_row_ptr.host_data();
//        int *ws_col_ind_data = ws_col_ind.host_data();
        //three arrays for csr representation
        vector<kernel_type> csr_val;
        vector<int> csr_col_ind;//index of each value of all the instances
        vector<int> csr_row_ptr(1, 0);//the start positions of the instances
        //ws_row_ptr_data[0] = 0;
        for(int i = 0; i < m; i++){
            int row = data_row_idx_data[i];
            for(int j = row_ptr_data[row]; j < row_ptr_data[row+1]; ++j){
                csr_col_ind.push_back(col_ind_data[j]);
                csr_val.push_back(val_data[j]);
            }
            csr_row_ptr.push_back(csr_row_ptr.back() + row_ptr_data[row+1] - row_ptr_data[row]);
        }
        //three arrays (on GPU/CPU) for csr representation
        ws_val.resize(csr_val.size());
        ws_col_ind.resize(csr_col_ind.size());
        ws_row_ptr.resize(csr_row_ptr.size());
        //copy data to the three arrays
        ws_val.copy_from(csr_val.data(), ws_val.size());
        ws_col_ind.copy_from(csr_col_ind.data(), ws_col_ind.size());
        ws_row_ptr.copy_from(csr_row_ptr.data(), ws_row_ptr.size());
    }

    void
    RBF_kernel(const SyncArray<kernel_type> &self_dot0, const SyncArray<kernel_type> &self_dot1,
               SyncArray<kernel_type> &dot_product, int m,
               int n, kernel_type gamma) {
        kernel_type *dot_product_data = dot_product.host_data();
        const kernel_type *self_dot0_data = self_dot0.host_data();
        const kernel_type *self_dot1_data = self_dot1.host_data();
#pragma omp parallel for schedule(guided)
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; ++j) {
                dot_product_data[i * n + j] = expf(
                        -(self_dot0_data[i] + self_dot1_data[j] - dot_product_data[i * n + j] * 2) * gamma);
            }
        }
    }

    void
    RBF_kernel(const SyncArray<int> &self_dot0_idx, const SyncArray<kernel_type> &self_dot1,
               SyncArray<kernel_type> &dot_product, int m,
               int n, kernel_type gamma) {
        kernel_type *dot_product_data = dot_product.host_data();
        const int *self_dot0_idx_data = self_dot0_idx.host_data();
        const kernel_type *self_dot1_data = self_dot1.host_data();
#pragma omp parallel for schedule(guided)
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; ++j) {
                dot_product_data[i * n + j] = expf(
                        -(self_dot1_data[self_dot0_idx_data[i]] + self_dot1_data[j] - dot_product_data[i * n + j] * 2) *
                        gamma);
            }
        }
    }

    void poly_kernel(SyncArray<kernel_type> &dot_product, kernel_type gamma, kernel_type coef0, int degree, int mn) {
        kernel_type *dot_product_data = dot_product.host_data();
#pragma omp parallel for schedule(guided)
        for (int idx = 0; idx < mn; idx++) {
            dot_product_data[idx] = powf(gamma * dot_product_data[idx] + coef0, degree);
        }
    }

    void sigmoid_kernel(SyncArray<kernel_type> &dot_product, kernel_type gamma, kernel_type coef0, int mn) {
        kernel_type *dot_product_data = dot_product.host_data();
#pragma omp parallel for schedule(guided)
        for (int idx = 0; idx < mn; idx++) {
            dot_product_data[idx] = tanhf(gamma * dot_product_data[idx] + coef0);
        }
    }

    void sum_kernel_values(const SyncArray<float_type> &coef, int total_sv, const SyncArray<int> &sv_start,
                           const SyncArray<int> &sv_count, const SyncArray<float_type> &rho,
                           const SyncArray<kernel_type> &k_mat,
                           SyncArray<float_type> &dec_values, int n_classes, int n_instances) {
        const int *sv_start_data = sv_start.host_data();
        const int *sv_count_data = sv_count.host_data();
        const float_type *coef_data = coef.host_data();
        const kernel_type *k_mat_data = k_mat.host_data();
        float_type *dec_values_data = dec_values.host_data();
        const float_type* rho_data = rho.host_data();
#pragma omp parallel for schedule(guided)
        for (int idx = 0; idx < n_instances; idx++) {
            int k = 0;
            int n_binary_models = n_classes * (n_classes - 1) / 2;
            for (int i = 0; i < n_classes; ++i) {
                for (int j = i + 1; j < n_classes; ++j) {
                    int si = sv_start_data[i];
                    int sj = sv_start_data[j];
                    int ci = sv_count_data[i];
                    int cj = sv_count_data[j];
                    const float_type *coef1 = &coef_data[(j - 1) * total_sv];
                    const float_type *coef2 = &coef_data[i * total_sv];
                    const kernel_type *k_values = &k_mat_data[idx * total_sv];
                    double sum = 0;
#pragma omp parallel for reduction(+:sum)
                    for (int l = 0; l < ci; ++l) {
                        sum += coef1[si + l] * k_values[si + l];
                    }
#pragma omp parallel for reduction(+:sum)
                    for (int l = 0; l < cj; ++l) {
                        sum += coef2[sj + l] * k_values[sj + l];
                    }
                    dec_values_data[idx * n_binary_models + k] = sum - rho_data[k];
                    k++;
                }
            }
        }
    }

    void dns_csr_mul(int m, int n, int k, const SyncArray<kernel_type> &dense_mat, const SyncArray<kernel_type> &csr_val,
                     const SyncArray<int> &csr_row_ptr, const SyncArray<int> &csr_col_ind, int nnz,
                     SyncArray<kernel_type> &result) {
        Eigen::Map<const Eigen::Matrix<kernel_type, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>> denseMat(dense_mat.host_data(), k, n);
        Eigen::Map<const Eigen::SparseMatrix<kernel_type, Eigen::RowMajor>> sparseMat(m, k, nnz, csr_row_ptr.host_data(),
                                                                                csr_col_ind.host_data(),
                                                                                csr_val.host_data());
        Eigen::Matrix<kernel_type, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> retMat = sparseMat * denseMat;
        Eigen::Map<Eigen::Matrix<kernel_type, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> >(result.host_data(),
                                                                                retMat.rows(),
                                                                                retMat.cols()) = retMat;
    }

    void csr_csr_mul(int m, int n, int k, const SyncArray<kernel_type> &ws_val, const SyncArray<int> &ws_col_ind,
                     const SyncArray<int> &ws_row_ptr, const SyncArray<kernel_type> &csr_val,
                     const SyncArray<int> &csr_row_ptr, const SyncArray<int> &csr_col_ind, int nnz, int nnz2,
                     SyncArray<kernel_type> &result) {
        Eigen::Map<const Eigen::SparseMatrix<kernel_type, Eigen::RowMajor>> sparseMat1(m, k, nnz, csr_row_ptr.host_data(),
                                                                                csr_col_ind.host_data(),
                                                                                csr_val.host_data());
        Eigen::Map<const Eigen::SparseMatrix<kernel_type>> sparseMat2(k, n, nnz2, ws_row_ptr.host_data(),
                                                                      ws_col_ind.host_data(),
                                                                      ws_val.host_data());
        Eigen::Matrix<kernel_type, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> retMat = sparseMat1 * sparseMat2;
        Eigen::Map<Eigen::Matrix<kernel_type, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> >(result.host_data(),
                                                                                retMat.rows(),
                                                                                retMat.cols()) = retMat;
    }

    void dns_dns_mul(int m, int n, int k, const SyncArray<kernel_type> &dense_mat,
                     const SyncArray<kernel_type> &origin_dense, SyncArray<kernel_type> &result){
        Eigen::Map<const Eigen::Matrix<kernel_type, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>> denseMat(dense_mat.host_data(), k, n);
        Eigen::Map<const Eigen::Matrix<kernel_type, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>
                originDenseMat(origin_dense.host_data(), m, k);
        Eigen::Matrix<kernel_type, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> retMat = originDenseMat * denseMat;
        Eigen::Map<Eigen::Matrix<kernel_type, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> >(result.host_data(),
                                                                                retMat.rows(),
                                                                                retMat.cols()) = retMat;
    }
}

max_stars_repo_name: BioDataAnalysis/thundersvm

max_issues_repo_name: BioDataAnalysis/thundersvm

max_forks_repo_name: BioDataAnalysis/thundersvm


text:
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
#include <pybind11/eigen.h>

#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/PointIndices.h>
#include <pcl/Vertices.h>
#include <pcl/visualization/common/common.h>
#include <pcl/segmentation/supervoxel_clustering.h>

#include <Eigen/Geometry>

PYBIND11_MAKE_OPAQUE(std::vector<int>);
PYBIND11_MAKE_OPAQUE(std::vector<float>);
PYBIND11_MAKE_OPAQUE(std::vector<pcl::PointIndices>);
PYBIND11_MAKE_OPAQUE(std::vector<pcl::Vertices>);
PYBIND11_MAKE_OPAQUE(std::vector<pcl::visualization::Camera>);

PYBIND11_MAKE_OPAQUE(Eigen::VectorXf);
PYBIND11_MAKE_OPAQUE(Eigen::Quaternionf);

//all pcl point types
typedef std::vector<pcl::PointXYZ, Eigen::aligned_allocator<pcl::PointXYZ>> make_opaque_PointXYZ;
PYBIND11_MAKE_OPAQUE(make_opaque_PointXYZ);
typedef std::vector<pcl::PointXYZI, Eigen::aligned_allocator<pcl::PointXYZI>> make_opaque_PointXYZI;
PYBIND11_MAKE_OPAQUE(make_opaque_PointXYZI);
typedef std::vector<pcl::PointXYZL, Eigen::aligned_allocator<pcl::PointXYZL>> make_opaque_PointXYZL;
PYBIND11_MAKE_OPAQUE(make_opaque_PointXYZL);
typedef std::vector<pcl::Label, Eigen::aligned_allocator<pcl::Label>> make_opaque_Label;
PYBIND11_MAKE_OPAQUE(make_opaque_Label);
typedef std::vector<pcl::PointXYZRGBA, Eigen::aligned_allocator<pcl::PointXYZRGBA>> make_opaque_PointXYZRGBA;
PYBIND11_MAKE_OPAQUE(make_opaque_PointXYZRGBA);
typedef std::vector<pcl::PointXYZRGB, Eigen::aligned_allocator<pcl::PointXYZRGB>> make_opaque_PointXYZRGB;
PYBIND11_MAKE_OPAQUE(make_opaque_PointXYZRGB);
typedef std::vector<pcl::PointXYZRGBL, Eigen::aligned_allocator<pcl::PointXYZRGBL>> make_opaque_PointXYZRGBL;
PYBIND11_MAKE_OPAQUE(make_opaque_PointXYZRGBL);
typedef std::vector<pcl::PointXYZHSV, Eigen::aligned_allocator<pcl::PointXYZHSV>> make_opaque_PointXYZHSV;
PYBIND11_MAKE_OPAQUE(make_opaque_PointXYZHSV);
typedef std::vector<pcl::PointXY, Eigen::aligned_allocator<pcl::PointXY>> make_opaque_PointXY;
PYBIND11_MAKE_OPAQUE(make_opaque_PointXY);
typedef std::vector<pcl::InterestPoint, Eigen::aligned_allocator<pcl::InterestPoint>> make_opaque_InterestPoint;
PYBIND11_MAKE_OPAQUE(make_opaque_InterestPoint);
typedef std::vector<pcl::Axis, Eigen::aligned_allocator<pcl::Axis>> make_opaque_Axis;
PYBIND11_MAKE_OPAQUE(make_opaque_Axis);
typedef std::vector<pcl::Normal, Eigen::aligned_allocator<pcl::Normal>> make_opaque_Normal;
PYBIND11_MAKE_OPAQUE(make_opaque_Normal);
typedef std::vector<pcl::PointNormal, Eigen::aligned_allocator<pcl::PointNormal>> make_opaque_PointNormal;
PYBIND11_MAKE_OPAQUE(make_opaque_PointNormal);
typedef std::vector<pcl::PointXYZRGBNormal, Eigen::aligned_allocator<pcl::PointXYZRGBNormal>> make_opaque_PointXYZRGBNormal;
PYBIND11_MAKE_OPAQUE(make_opaque_PointXYZRGBNormal);
typedef std::vector<pcl::PointXYZINormal, Eigen::aligned_allocator<pcl::PointXYZINormal>> make_opaque_PointXYZINormal;
PYBIND11_MAKE_OPAQUE(make_opaque_PointXYZINormal);
typedef std::vector<pcl::PointXYZLNormal, Eigen::aligned_allocator<pcl::PointXYZLNormal>> make_opaque_PointXYZLNormal;
PYBIND11_MAKE_OPAQUE(make_opaque_PointXYZLNormal);
typedef std::vector<pcl::PointWithRange, Eigen::aligned_allocator<pcl::PointWithRange>> make_opaque_PointWithRange;
PYBIND11_MAKE_OPAQUE(make_opaque_PointWithRange);
typedef std::vector<pcl::PointWithViewpoint, Eigen::aligned_allocator<pcl::PointWithViewpoint>> make_opaque_PointWithViewpoint;
PYBIND11_MAKE_OPAQUE(make_opaque_PointWithViewpoint);
typedef std::vector<pcl::MomentInvariants, Eigen::aligned_allocator<pcl::MomentInvariants>> make_opaque_MomentInvariants;
PYBIND11_MAKE_OPAQUE(make_opaque_MomentInvariants);
typedef std::vector<pcl::PrincipalRadiiRSD, Eigen::aligned_allocator<pcl::PrincipalRadiiRSD>> make_opaque_PrincipalRadiiRSD;
PYBIND11_MAKE_OPAQUE(make_opaque_PrincipalRadiiRSD);
typedef std::vector<pcl::Boundary, Eigen::aligned_allocator<pcl::Boundary>> make_opaque_Boundary;
PYBIND11_MAKE_OPAQUE(make_opaque_Boundary);
typedef std::vector<pcl::PrincipalCurvatures, Eigen::aligned_allocator<pcl::PrincipalCurvatures>> make_opaque_PrincipalCurvatures;
PYBIND11_MAKE_OPAQUE(make_opaque_PrincipalCurvatures);
typedef std::vector<pcl::PFHSignature125, Eigen::aligned_allocator<pcl::PFHSignature125>> make_opaque_PFHSignature125;
PYBIND11_MAKE_OPAQUE(make_opaque_PFHSignature125);
typedef std::vector<pcl::PFHRGBSignature250, Eigen::aligned_allocator<pcl::PFHRGBSignature250>> make_opaque_PFHRGBSignature250;
PYBIND11_MAKE_OPAQUE(make_opaque_PFHRGBSignature250);
typedef std::vector<pcl::PPFSignature, Eigen::aligned_allocator<pcl::PPFSignature>> make_opaque_PPFSignature;
PYBIND11_MAKE_OPAQUE(make_opaque_PPFSignature);
typedef std::vector<pcl::CPPFSignature, Eigen::aligned_allocator<pcl::CPPFSignature>> make_opaque_CPPFSignature;
PYBIND11_MAKE_OPAQUE(make_opaque_CPPFSignature);
typedef std::vector<pcl::PPFRGBSignature, Eigen::aligned_allocator<pcl::PPFRGBSignature>> make_opaque_PPFRGBSignature;
PYBIND11_MAKE_OPAQUE(make_opaque_PPFRGBSignature);
typedef std::vector<pcl::NormalBasedSignature12, Eigen::aligned_allocator<pcl::NormalBasedSignature12>> make_opaque_NormalBasedSignature12;
PYBIND11_MAKE_OPAQUE(make_opaque_NormalBasedSignature12);
typedef std::vector<pcl::FPFHSignature33, Eigen::aligned_allocator<pcl::FPFHSignature33>> make_opaque_FPFHSignature33;
PYBIND11_MAKE_OPAQUE(make_opaque_FPFHSignature33);
typedef std::vector<pcl::VFHSignature308, Eigen::aligned_allocator<pcl::VFHSignature308>> make_opaque_VFHSignature308;
PYBIND11_MAKE_OPAQUE(make_opaque_VFHSignature308);
typedef std::vector<pcl::ESFSignature640, Eigen::aligned_allocator<pcl::ESFSignature640>> make_opaque_ESFSignature640;
PYBIND11_MAKE_OPAQUE(make_opaque_ESFSignature640);
typedef std::vector<pcl::BRISKSignature512, Eigen::aligned_allocator<pcl::BRISKSignature512>> make_opaque_BRISKSignature512;
PYBIND11_MAKE_OPAQUE(make_opaque_BRISKSignature512);
typedef std::vector<pcl::Narf36, Eigen::aligned_allocator<pcl::Narf36>> make_opaque_Narf36;
PYBIND11_MAKE_OPAQUE(make_opaque_Narf36);
typedef std::vector<pcl::IntensityGradient, Eigen::aligned_allocator<pcl::IntensityGradient>> make_opaque_IntensityGradient;
PYBIND11_MAKE_OPAQUE(make_opaque_IntensityGradient);
typedef std::vector<pcl::PointWithScale, Eigen::aligned_allocator<pcl::PointWithScale>> make_opaque_PointWithScale;
PYBIND11_MAKE_OPAQUE(make_opaque_PointWithScale);
typedef std::vector<pcl::PointSurfel, Eigen::aligned_allocator<pcl::PointSurfel>> make_opaque_PointSurfel;
PYBIND11_MAKE_OPAQUE(make_opaque_PointSurfel);
typedef std::vector<pcl::ShapeContext1980, Eigen::aligned_allocator<pcl::ShapeContext1980>> make_opaque_ShapeContext1980;
PYBIND11_MAKE_OPAQUE(make_opaque_ShapeContext1980);
typedef std::vector<pcl::UniqueShapeContext1960, Eigen::aligned_allocator<pcl::UniqueShapeContext1960>> make_opaque_UniqueShapeContext1960;
PYBIND11_MAKE_OPAQUE(make_opaque_UniqueShapeContext1960);
typedef std::vector<pcl::SHOT352, Eigen::aligned_allocator<pcl::SHOT352>> make_opaque_SHOT352;
PYBIND11_MAKE_OPAQUE(make_opaque_SHOT352);
typedef std::vector<pcl::SHOT1344, Eigen::aligned_allocator<pcl::SHOT1344>> make_opaque_SHOT1344;
PYBIND11_MAKE_OPAQUE(make_opaque_SHOT1344);
typedef std::vector<pcl::PointUV, Eigen::aligned_allocator<pcl::PointUV>> make_opaque_PointUV;
PYBIND11_MAKE_OPAQUE(make_opaque_PointUV);
typedef std::vector<pcl::ReferenceFrame, Eigen::aligned_allocator<pcl::ReferenceFrame>> make_opaque_ReferenceFrame;
PYBIND11_MAKE_OPAQUE(make_opaque_ReferenceFrame);
typedef std::vector<pcl::PointDEM, Eigen::aligned_allocator<pcl::PointDEM>> make_opaque_PointDEM;
PYBIND11_MAKE_OPAQUE(make_opaque_PointDEM);
//PYBIND11_MAKE_OPAQUE(std::vector<pcl::GRSDSignature21>);  // Linking error

//Supervoxel

typedef std::map<uint32_t, pcl::Supervoxel<pcl::PointXYZ>::Ptr> make_opaque_Supervoxel_PointXYZ;
PYBIND11_MAKE_OPAQUE(make_opaque_Supervoxel_PointXYZ);
typedef std::map<uint32_t, pcl::Supervoxel<pcl::PointXYZI>::Ptr> make_opaque_Supervoxel_PointXYZI;
PYBIND11_MAKE_OPAQUE(make_opaque_Supervoxel_PointXYZI);
typedef std::map<uint32_t, pcl::Supervoxel<pcl::PointXYZL>::Ptr> make_opaque_Supervoxel_PointXYZL;
PYBIND11_MAKE_OPAQUE(make_opaque_Supervoxel_PointXYZL);
typedef std::map<uint32_t, pcl::Supervoxel<pcl::PointXYZRGBA>::Ptr> make_opaque_Supervoxel_PointXYZRGBA;
PYBIND11_MAKE_OPAQUE(make_opaque_Supervoxel_PointXYZRGBA);
typedef std::map<uint32_t, pcl::Supervoxel<pcl::PointXYZRGB>::Ptr> make_opaque_Supervoxel_PointXYZRGB;
PYBIND11_MAKE_OPAQUE(make_opaque_Supervoxel_PointXYZRGB);
typedef std::map<uint32_t, pcl::Supervoxel<pcl::PointXYZRGBL>::Ptr> make_opaque_Supervoxel_PointXYZRGBL;
PYBIND11_MAKE_OPAQUE(make_opaque_Supervoxel_PointXYZRGBL);
typedef std::map<uint32_t, pcl::Supervoxel<pcl::PointXYZHSV>::Ptr> make_opaque_Supervoxel_PointXYZHSV;
PYBIND11_MAKE_OPAQUE(make_opaque_Supervoxel_PointXYZHSV);
typedef std::map<uint32_t, pcl::Supervoxel<pcl::InterestPoint>::Ptr> make_opaque_Supervoxel_InterestPoint;
PYBIND11_MAKE_OPAQUE(make_opaque_Supervoxel_InterestPoint);
typedef std::map<uint32_t, pcl::Supervoxel<pcl::PointNormal>::Ptr> make_opaque_Supervoxel_PointNormal;
PYBIND11_MAKE_OPAQUE(make_opaque_Supervoxel_PointNormal);
typedef std::map<uint32_t, pcl::Supervoxel<pcl::PointXYZRGBNormal>::Ptr> make_opaque_Supervoxel_PointXYZRGBNormal;
PYBIND11_MAKE_OPAQUE(make_opaque_Supervoxel_PointXYZRGBNormal);
typedef std::map<uint32_t, pcl::Supervoxel<pcl::PointXYZINormal>::Ptr> make_opaque_Supervoxel_PointXYZINormal;
PYBIND11_MAKE_OPAQUE(make_opaque_Supervoxel_PointXYZINormal);
typedef std::map<uint32_t, pcl::Supervoxel<pcl::PointXYZLNormal>::Ptr> make_opaque_Supervoxel_PointXYZLNormal;
PYBIND11_MAKE_OPAQUE(make_opaque_Supervoxel_PointXYZLNormal);
typedef std::map<uint32_t, pcl::Supervoxel<pcl::PointWithRange>::Ptr> make_opaque_Supervoxel_PointWithRange;
PYBIND11_MAKE_OPAQUE(make_opaque_Supervoxel_PointWithRange);
typedef std::map<uint32_t, pcl::Supervoxel<pcl::PointWithViewpoint>::Ptr> make_opaque_Supervoxel_PointWithViewpoint;
PYBIND11_MAKE_OPAQUE(make_opaque_Supervoxel_PointWithViewpoint);
typedef std::map<uint32_t, pcl::Supervoxel<pcl::PointWithScale>::Ptr> make_opaque_Supervoxel_PointWithScale;
PYBIND11_MAKE_OPAQUE(make_opaque_Supervoxel_PointWithScale);
typedef std::map<uint32_t, pcl::Supervoxel<pcl::PointSurfel>::Ptr> make_opaque_Supervoxel_PointSurfel;
PYBIND11_MAKE_OPAQUE(make_opaque_Supervoxel_PointSurfel);
typedef std::map<uint32_t, pcl::Supervoxel<pcl::PointDEM>::Ptr> make_opaque_Supervoxel_PointDEM;
PYBIND11_MAKE_OPAQUE(make_opaque_Supervoxel_PointDEM);

max_stars_repo_name: toinsson/pclpy

max_issues_repo_name: toinsson/pclpy

max_forks_repo_name: toinsson/pclpy


text:
#include <random>
#include <string>
#include <iostream>
#include <fstream>
#include <chrono>

#include <Eigen/Dense>

#include <cannon/logic/cnf.hpp>
#include <cannon/logic/dpll.hpp>
#include <cannon/logic/read_dimacs_cnf.hpp>

using namespace Eigen;

using namespace cannon::logic;

double calculate_moms(const CNFFormula& form, const Assignment& a, const
    Simplification& s, unsigned int prop) {
  unsigned int num_smallest_pos = 0;
  unsigned int num_smallest_neg = 0;
  unsigned int smallest_clause_size = form.get_smallest_clause_size(a, s);

  for (unsigned int i = 0; i < form.clauses_.size(); i++) {
    if (s[i])
      continue;

    if (form.clauses_[i].contains_prop(a, prop)) {
      if (form.clauses_[i].size(a) == smallest_clause_size) {
        if (form.clauses_[i].has_pos_literal(a, prop)) {
          num_smallest_pos += 1;
        } else {
          num_smallest_neg += 1;
        }
      }
    }
  }

  double first_part = (double)(num_smallest_pos + num_smallest_neg) * std::pow(2.0, 3.0);
  double second_part = num_smallest_pos * num_smallest_neg;

  return first_part + second_part;
}

unsigned int uniform_random_prop(const DPLLState& ds,
    const Assignment& a, const Simplification& s, std::vector<unsigned int>& props) {
  RandomComparator comp(ds);
  std::sort(props.begin(), props.end(), comp);

  return props[props.size() - 1];
}

unsigned int two_clause_prop(const DPLLState& ds,
    const Assignment& a, const Simplification& s, std::vector<unsigned int>& props) {
  RandomComparator rc(ds);
  TwoClauseComparator tc(ds, a, s, props, rc);

  std::sort(props.begin(), props.end(), tc);
  return props[props.size() - 1];
}

unsigned int vsids_prop(const DPLLState& ds, const Assignment& a, const Simplification& s,
    std::vector<unsigned int>& props) {
  RandomComparator rc(ds);
  VsidsComparator vc(ds, rc);
  TwoClauseComparator tc(ds, a, s, props, vc);

  std::sort(props.begin(), props.end(), tc);
  return props[props.size() - 1];
}

bool uniform_random_assign(const CNFFormula& form, const Assignment& a,
    const Simplification& s, unsigned int prop, const std::vector<std::vector<unsigned int>>& watched) {
  std::random_device rd;
  static std::default_random_engine gen(rd());
  std::uniform_real_distribution<double> d;

  double t = d(gen);
  return t < 0.5;
}

bool max_vote_assign(const CNFFormula& form, const Assignment& a, const
    Simplification& s, unsigned int prop, const std::vector<std::vector<unsigned int>>& watched) {
  std::random_device rd;
  static std::default_random_engine gen(rd());
  std::uniform_real_distribution<double> d;

  int num_pos = 0;
  int num_neg = 0;
  for (unsigned int c_num : watched[prop]) {
    if (s[c_num])
      continue;

    for (auto &l : form.clauses_[c_num].literals_) {
      if (l.prop_ == prop && l.negated_)
        num_neg += 1;

      if (l.prop_ == prop && !l.negated_)
        num_pos += 1;
    }
  }

  if (num_pos == num_neg) {
    double t = d(gen);
    return t < 0.5;
  } else {
    return num_pos > num_neg;
  }
}

bool min_vote_assign(const CNFFormula& form, const Assignment& a, const
    Simplification& s, unsigned int prop, const std::vector<std::vector<unsigned int>>& watched) {
  std::random_device rd;
  static std::default_random_engine gen(rd());
  std::uniform_real_distribution<double> d;

  int num_pos = 0;
  int num_neg = 0;
  for (unsigned int c_num : watched[prop]) {
    if (s[c_num])
      continue;

    for (auto &l : form.clauses_[c_num].literals_) {
      if (l.prop_ == prop && l.negated_)
        num_neg += 1;

      if (l.prop_ == prop && !l.negated_)
        num_pos += 1;
    }
  }

  if (num_pos == num_neg) {
    double t = d(gen);
    return t < 0.5;
  } else {
    return num_pos < num_neg;
  }
}

int main(int argc, char **argv) {
  DPLLResult r;
  Assignment a;
  int c;

  assert(argc >= 2);
  std::string filename(argv[1]);
  std::string filename_copy(filename);

  std::string report_filename =
    filename_copy.erase(filename_copy.find_first_of("."),
        filename_copy.size()-1);

  std::string yaml_report_filename(report_filename);
  report_filename += "_report.txt";
  yaml_report_filename += "_report.yaml";

  std::ofstream os(report_filename);
  std::ofstream y_os(yaml_report_filename); // Write to YAML as well

  y_os << "stats:" << std::endl;

  CNFFormula f = load_cnf(filename);
  std::cout << "Parsed passed CNF file as " << f << std::endl;
  os << "Parsed passed CNF file " << filename << " as " << std::endl << f << std::endl << std::endl;

  auto start = std::chrono::steady_clock::now();
  //std::tie(r, a, c) = dpll(f, uniform_random_prop, uniform_random_assign, std::chrono::seconds(1200)); // Random heuristic
  //std::tie(r, a, c) = dpll(f, two_clause_prop, uniform_random_assign); // 2-clause heuristic
  std::tie(r, a, c) = dpll(f, vsids_prop, min_vote_assign, std::chrono::seconds(1200)); // custom heuristic
  auto end = std::chrono::steady_clock::now();
  auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  y_os << "  time_micros: " << std::to_string(duration.count()) << std::endl;

  std::cout << "DPLL recursively called " << c << " times" << std::endl;
  os << "DPLL recursively called " << c << " times" << std::endl << std::endl;
  y_os << "  calls: " << std::to_string(c) << std::endl;

  std::cout << "DPLL on " << filename << " returned result " << r << " with assignment " << a << std::endl;
  os << "DPLL returned result " << r << " with assignment " << a << std::endl << std::endl;

  y_os << "  sat: ";
  if (r == DPLLResult::Satisfiable)
    y_os << "true" << std::endl;
  else
    y_os << "false" << std::endl;

  y_os << "  cutoff: ";
  if (r == DPLLResult::Unknown)
    y_os << "true" << std::endl;
  else
    y_os << "false" << std::endl;

  Simplification s = std::valarray<bool>(false, f.get_num_clauses());
  if (r == DPLLResult::Satisfiable) {
    std::cout << "Evaluation of formula on returned assignment is" << std::endl;
    std::cout << f.eval(a, s) << std::endl;
    assert(f.eval(a, s) == PropAssignment::True);

    os << "Evaluation of formula on returned assignment is " << f.eval(a, s);
    os.close();
  }
}

max_stars_repo_name: cannontwo/cannon

max_issues_repo_name: cannontwo/cannon

max_forks_repo_name: cannontwo/cannon


text:
//
// Copyright 2012 Christian Henning, Andreas Pokorny, Lubomir Bourdev
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
#ifndef BOOST_GIL_EXTENSION_TOOLBOX_METAFUNCTIONS_IS_BIT_ALIGNED_TYPE_HPP
#define BOOST_GIL_EXTENSION_TOOLBOX_METAFUNCTIONS_IS_BIT_ALIGNED_TYPE_HPP

#include <boost/gil/bit_aligned_pixel_reference.hpp>

namespace boost{ namespace gil {

/// is_bit_aligned metafunctions
/// \brief Determines whether the given type is bit_aligned.

template< typename PixelRef >
struct is_bit_aligned : mpl::false_{};

template <typename B, typename C, typename L, bool M>
struct is_bit_aligned<bit_aligned_pixel_reference<B,C,L,M> > : mpl::true_{};

template <typename B, typename C, typename L, bool M>
struct is_bit_aligned<const bit_aligned_pixel_reference<B,C,L,M> > : mpl::true_{};

template <typename B, typename C, typename L>
struct is_bit_aligned<packed_pixel<B,C,L> > : mpl::true_{};

template <typename B, typename C, typename L>
struct is_bit_aligned<const packed_pixel<B,C,L> > : mpl::true_{};

} // namespace gil
} // namespace boost

#endif


text:
// Licensed under the BSD license. See LICENSE.txt for more details.

#include "translator/kernel.h"

#include <utility>
#include <boost/foreach.hpp>

#include "translator/translation_context.h"
#include "translator/alias_analysis.h"

namespace si = SageInterface;

namespace physis {
namespace translator {

static void AnalyzeGridAccess(SgFunctionDeclaration *decl,
                              TranslationContext &tx,
                              const set<SgInitializedName*> &gvs,
                              GridSet &grid_set,
                              SgInitializedNamePtrSet &grid_var_set) {
  SgTypePtrList relevantTypes;
  BOOST_FOREACH(SgInitializedName *gv, gvs) {
    relevantTypes.push_back(gv->get_type());
    LOG_DEBUG() << "Relevant type: " << gv->get_type()->unparseToString() << "\n";
    LOG_DEBUG() << "Used grid: " << gv->unparseToString() << "\n";
    const GridSet *gs = tx.findGrid(gv);
    assert(gs);
    FOREACH(it, gs->begin(), gs->end()) {
      if (*it) {
        LOG_DEBUG() << "gobj: " << (*it)->toString() << "\n";
      } else {
        LOG_DEBUG() << "gobj: NULL\n";
      }
    }
    grid_set.insert(gs->begin(), gs->end());
  }

  LOG_DEBUG() << "Analyzing grid var aliases\n";
  AliasGraph ag(decl, relevantTypes);
  LOG_DEBUG() << ag;
  if (ag.HasMultipleOriginVar()) {
    LOG_ERROR() << "Grid variables must not have multiple assignments.\n";
    abort();
  }
  if (ag.HasNullInitVar()) {
    LOG_ERROR() << "Grid variables must not have NULL initialization.\n";
    abort();
  }

  BOOST_FOREACH(SgInitializedName *gv, gvs) {
    SgInitializedName* originalVar = ag.FindOriginalVar(gv);
    grid_var_set.insert(originalVar);
  }
}

void Kernel::AnalyzeGridWrites(TranslationContext &tx) {
  SgFunctionCallExpPtrList calls =
      tx.getGridEmitCalls(decl_->get_definition());
  set<SgInitializedName*> gvs;
  BOOST_FOREACH (SgFunctionCallExp *fc, calls) {
    gvs.insert(GridType::getGridVarUsedInFuncCall(fc));
  }
  AnalyzeGridAccess(decl_, tx, gvs, wGrids_, wGridVars_);
}

void Kernel::AnalyzeGridReads(TranslationContext &tx) {
  SgFunctionCallExpPtrList calls =
      tx.getGridGetCalls(decl_->get_definition());
  set<SgInitializedName*> gvs;
  BOOST_FOREACH (SgFunctionCallExp *fc, calls) {
    gvs.insert(GridType::getGridVarUsedInFuncCall(fc));
  }
  AnalyzeGridAccess(decl_, tx, gvs, rGrids_, rGridVars_);
  calls =
      tx.getGridGetPeriodicCalls(decl_->get_definition());
  gvs.clear();
  BOOST_FOREACH (SgFunctionCallExp *fc, calls) {
    gvs.insert(GridType::getGridVarUsedInFuncCall(fc));
  }
  AnalyzeGridAccess(decl_, tx, gvs, rGrids_, rGridVars_);
}

static void CollectionReadWriteGrids(SgFunctionDefinition *fdef,
                                     set<SgInitializedName*> &read_grids,
                                     set<SgInitializedName*> &written_grids) {
  vector<SgDotExp*> grid_reads =
      si::querySubTree<SgDotExp>(fdef);
  BOOST_FOREACH(SgDotExp *de, grid_reads) {
    SgVarRefExp *gr = isSgVarRefExp(de->get_lhs_operand());
    PSAssert(gr);
    SgInitializedName *gv = si::convertRefToInitializedName(gr);
    if (!rose_util::GetASTAttribute<GridType>(gv)) {
      // Not a grid
      continue;
    }
    LOG_DEBUG() << "Access to grid var found: "
                << de->unparseToString() << "\n";
    // write access if located as lhs of assign op
    if (isSgAssignOp(de->get_parent()) &&
        isSgAssignOp(de->get_parent())->get_lhs_operand() == de) {
      written_grids.insert(gv);
    } else {
      read_grids.insert(gv);
    }
  }
}

Kernel::Kernel(SgFunctionDeclaration *decl, TranslationContext *tx,
               Kernel *parent) : parent_(parent) {
  this->decl_ =
      isSgFunctionDeclaration(decl->get_definingDeclaration());
  assert(this->decl_);
  if (si::is_C_language() || si::is_Cxx_language()) {
    AnalyzeGridWrites(*tx);
    AnalyzeGridReads(*tx);
  } else if (si::is_Fortran_language()) {
    set<SgInitializedName*> rg, wg;
    CollectionReadWriteGrids(decl->get_definition(), rg, wg);
    AnalyzeGridAccess(decl, *tx, rg, rGrids_, rGridVars_);
    AnalyzeGridAccess(decl, *tx, wg, wGrids_, wGridVars_);
  }
}

Kernel::Kernel(const Kernel &k):
    decl_(k.decl_), rGrids_(k.rGrids_), rGridVars_(k.rGridVars_),
    wGrids_(k.wGrids_), wGridVars_(k.wGridVars_), parent_(k.parent_),
    calls_(k.calls_) {
}

const GridSet& Kernel::GetInGrids() const {
  return rGrids_;
}

const GridSet& Kernel::GetOutGrids() const {
  return wGrids_;
}

#ifdef UNUSED_CODE
bool Kernel::isModified(Grid *g) const {
  if (wGrids_.count(g)) return true;
  FOREACH(it, calls_.begin(), calls_.end()) {
    Kernel *child = it->second;
    if (child->isModified(g)) return true;
  }
  return false;
}

bool Kernel::isModifiedAny(GridSet *gs) const {
  FOREACH (it, gs->begin(), gs->end()) {
    if (isModified(*it)) return true;
  }
  return false;
}
#endif

bool Kernel::IsGridUnmodified(Grid *g) const {
  assert(g != NULL);
  // If NULL is contained, g may be modified.
  if (wGrids_.find(NULL) != wGrids_.end()) return false;
  if (wGrids_.find(g) != wGrids_.end()) return false;
  FOREACH(it, calls_.begin(), calls_.end()) {
    Kernel *child = it->second;
    if (!child->IsGridUnmodified(g)) return false;
  }
  return true;
}

bool Kernel::IsGridParamModified(SgInitializedName *v) const {
  return wGridVars_.find(v) != wGridVars_.end();
}

#ifdef UNUSED_CODE
bool Kernel::isRead(Grid *g) const {
  if (rGrids_.find(g) != rGrids_.end()) return true;
  FOREACH(it, calls.begin(), calls.end()) {
    Kernel *child = it->second;
    if (child->isRead(g)) return true;
  }
  return false;
}

bool Kernel::isReadAny(GridSet *gs) const {
  FOREACH (it, gs->begin(), gs->end()) {
    if (isRead(*it)) return true;
  }
  return false;
}
#endif

bool Kernel::IsGridUnread(Grid *g) const {
  if (rGrids_.find(NULL) != rGrids_.end()) return false;
  if (rGrids_.find(g) != rGrids_.end()) return false;
  FOREACH(it, calls_.begin(), calls_.end()) {
    Kernel *child = it->second;
    if (!child->IsGridUnread(g)) return false;
  }
  return true;
}

bool Kernel::IsGridParamRead(SgInitializedName *v) const {
  return rGridVars_.find(v) != rGridVars_.end();
}

void Kernel::AppendChild(SgFunctionCallExp *call, Kernel *child) {
  assert(calls_.insert(std::make_pair(call, child)).second);
}

const std::string RunKernelAttribute::name = "RunKernel";

const std::string Kernel::name = "Kernel";

const std::string KernelBody::name = "KernelBody";

} // namespace translator
} // namespace physis

ext: cc

max_stars_repo_name: naoyam/physis

max_issues_repo_name: naoyam/physis

max_forks_repo_name: naoyam/physis