text:
/* -*- c++ -*- */
/*
 * Copyright 2013 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gnuradio/filter/pfb_arb_resampler.h>
#include <gnuradio/logger.h>
#include <cstdio>
#include <stdexcept>
#include <boost/math/special_functions/round.hpp>

namespace gr {
  namespace filter {
    namespace kernel {

      pfb_arb_resampler_ccf::pfb_arb_resampler_ccf(float rate,
                                                   const std::vector<float> &taps,
                                                   unsigned int filter_size)
      {
        d_acc = 0; // start accumulator at 0

        /* The number of filters is specified by the user as the
           filter size; this is also the interpolation rate of the
           filter. We use it and the rate provided to determine the
           decimation rate. This acts as a rational resampler. The
           flt_rate is calculated as the residual between the integer
           decimation rate and the real decimation rate and will be
           used to determine to interpolation point of the resampling
           process.
        */
        d_int_rate = filter_size;
        set_rate(rate);

        d_last_filter = (taps.size()/2) % filter_size;

        d_filters = std::vector<fir_filter_ccf*>(d_int_rate);
        d_diff_filters = std::vector<fir_filter_ccf*>(d_int_rate);

        // Create an FIR filter for each channel and zero out the taps
        std::vector<float> vtaps(0, d_int_rate);
        for(unsigned int i = 0; i < d_int_rate; i++) {
          d_filters[i] = new fir_filter_ccf(1, vtaps);
          d_diff_filters[i] = new fir_filter_ccf(1, vtaps);
        }

        // Now, actually set the filters' taps
        set_taps(taps);

        // Delay is based on number of taps per filter arm. Round to
        // the nearest integer.
        float delay = rate * (taps_per_filter() - 1.0) / 2.0;
        d_delay = static_cast<int>(boost::math::iround(delay));

        // This calculation finds the phase offset induced by the
        // arbitrary resampling. It's based on which filter arm we are
        // at the filter's group delay plus the fractional offset
        // between the samples. Calculated here based on the rotation
        // around nfilts starting at start_filter.
        float accum = d_delay * d_flt_rate;
        int   accum_int = static_cast<int>(accum);
        float accum_frac = accum - accum_int;
        int end_filter = static_cast<int>
          (boost::math::iround(fmodf(d_last_filter + d_delay * d_dec_rate + accum_int, \
                                     static_cast<float>(d_int_rate))));

        d_est_phase_change = d_last_filter - (end_filter + accum_frac);
      }

      pfb_arb_resampler_ccf::~pfb_arb_resampler_ccf()
      {
        for(unsigned int i = 0; i < d_int_rate; i++) {
          delete d_filters[i];
          delete d_diff_filters[i];
        }
      }

      void
      pfb_arb_resampler_ccf::create_taps(const std::vector<float> &newtaps,
                                         std::vector< std::vector<float> > &ourtaps,
                                         std::vector<fir_filter_ccf*> &ourfilter)
      {
        unsigned int ntaps = newtaps.size();
        d_taps_per_filter = (unsigned int)ceil((double)ntaps/(double)d_int_rate);

        // Create d_numchan vectors to store each channel's taps
        ourtaps.resize(d_int_rate);

        // Make a vector of the taps plus fill it out with 0's to fill
        // each polyphase filter with exactly d_taps_per_filter
        std::vector<float> tmp_taps;
        tmp_taps = newtaps;
        while((float)(tmp_taps.size()) < d_int_rate*d_taps_per_filter) {
          tmp_taps.push_back(0.0);
        }

        for(unsigned int i = 0; i < d_int_rate; i++) {
          // Each channel uses all d_taps_per_filter with 0's if not enough taps to fill out
          ourtaps[i] = std::vector<float>(d_taps_per_filter, 0);
          for(unsigned int j = 0; j < d_taps_per_filter; j++) {
            ourtaps[i][j] = tmp_taps[i + j*d_int_rate];
          }

          // Build a filter for each channel and add it's taps to it
          ourfilter[i]->set_taps(ourtaps[i]);
        }
      }

      void
      pfb_arb_resampler_ccf::create_diff_taps(const std::vector<float> &newtaps,
                                              std::vector<float> &difftaps)
      {
        // Calculate the differential taps using a derivative filter
        std::vector<float> diff_filter(2);
        diff_filter[0] = -1;
        diff_filter[1] = 1;

        for(unsigned int i = 0; i < newtaps.size()-1; i++) {
          float tap = 0;
          for(unsigned int j = 0; j < diff_filter.size(); j++) {
            tap += diff_filter[j]*newtaps[i+j];
          }
          difftaps.push_back(tap);
        }
        difftaps.push_back(0);
      }

      void
      pfb_arb_resampler_ccf::set_taps(const std::vector<float> &taps)
      {
        std::vector<float> dtaps;
        create_diff_taps(taps, dtaps);
        create_taps(taps, d_taps, d_filters);
        create_taps(dtaps, d_dtaps, d_diff_filters);
      }

      std::vector<std::vector<float> >
      pfb_arb_resampler_ccf::taps() const
      {
        return d_taps;
      }

      void
      pfb_arb_resampler_ccf::print_taps()
      {
        unsigned int i, j;
        for(i = 0; i < d_int_rate; i++) {
          printf("filter[%d]: [", i);
          for(j = 0; j < d_taps_per_filter; j++) {
            printf(" %.4e", d_taps[i][j]);
          }
          printf("]\n");
        }
      }

      void
      pfb_arb_resampler_ccf::set_rate(float rate)
      {
        d_dec_rate = (unsigned int)floor(d_int_rate/rate);
        d_flt_rate = (d_int_rate/rate) - d_dec_rate;
      }

      void
      pfb_arb_resampler_ccf::set_phase(float ph)
      {
        if((ph < 0) || (ph >= 2.0*M_PI)) {
          throw std::runtime_error("pfb_arb_resampler_ccf: set_phase value out of bounds [0, 2pi).\n");
        }

        float ph_diff = 2.0*M_PI / (float)d_filters.size();
        d_last_filter = static_cast<int>(ph / ph_diff);
      }

      float
      pfb_arb_resampler_ccf::phase() const
      {
        float ph_diff = 2.0*M_PI / static_cast<float>(d_filters.size());
        return d_last_filter * ph_diff;
      }

      unsigned int
      pfb_arb_resampler_ccf::taps_per_filter() const
      {
        return d_taps_per_filter;
      }

      float
      pfb_arb_resampler_ccf::phase_offset(float freq, float fs)
      {
        float adj = (2.0*M_PI)*(freq/fs)/static_cast<float>(d_int_rate);
        return -adj * d_est_phase_change;
      }

      int
      pfb_arb_resampler_ccf::filter(gr_complex *output, gr_complex *input,
                                    int n_to_read, int &n_read)
      {
        int i_out = 0, i_in = 0;
        unsigned int j = d_last_filter;;
        gr_complex o0, o1;

        while(i_in < n_to_read) {
          // start j by wrapping around mod the number of channels
          while(j < d_int_rate) {
            // Take the current filter and derivative filter output
            o0 = d_filters[j]->filter(&input[i_in]);
            o1 = d_diff_filters[j]->filter(&input[i_in]);

            output[i_out] = o0 + o1*d_acc;     // linearly interpolate between samples
            i_out++;

            // Adjust accumulator and index into filterbank
            d_acc += d_flt_rate;
            j += d_dec_rate + (int)floor(d_acc);
            d_acc = fmodf(d_acc, 1.0);
          }
          i_in += (int)(j / d_int_rate);
          j = j % d_int_rate;
        }
        d_last_filter = j; // save last filter state for re-entry

        n_read = i_in;   // return how much we've actually read
        return i_out;    // return how much we've produced
      }

      /****************************************************************/

      pfb_arb_resampler_ccc::pfb_arb_resampler_ccc(float rate,
                                                   const std::vector<gr_complex> &taps,
                                                   unsigned int filter_size)
      {
        d_acc = 0; // start accumulator at 0

        /* The number of filters is specified by the user as the
           filter size; this is also the interpolation rate of the
           filter. We use it and the rate provided to determine the
           decimation rate. This acts as a rational resampler. The
           flt_rate is calculated as the residual between the integer
           decimation rate and the real decimation rate and will be
           used to determine to interpolation point of the resampling
           process.
        */
        d_int_rate = filter_size;
        set_rate(rate);

        d_last_filter = (taps.size()/2) % filter_size;

        d_filters = std::vector<fir_filter_ccc*>(d_int_rate);
        d_diff_filters = std::vector<fir_filter_ccc*>(d_int_rate);

        // Create an FIR filter for each channel and zero out the taps
        std::vector<gr_complex> vtaps(0, d_int_rate);
        for(unsigned int i = 0; i < d_int_rate; i++) {
          d_filters[i] = new fir_filter_ccc(1, vtaps);
          d_diff_filters[i] = new fir_filter_ccc(1, vtaps);
        }

        // Now, actually set the filters' taps
        set_taps(taps);

        // Delay is based on number of taps per filter arm. Round to
        // the nearest integer.
        float delay = rate * (taps_per_filter() - 1.0) / 2.0;
        d_delay = static_cast<int>(boost::math::iround(delay));

        // This calculation finds the phase offset induced by the
        // arbitrary resampling. It's based on which filter arm we are
        // at the filter's group delay plus the fractional offset
        // between the samples. Calculated here based on the rotation
        // around nfilts starting at start_filter.
        float accum = d_delay * d_flt_rate;
        int   accum_int = static_cast<int>(accum);
        float accum_frac = accum - accum_int;
        int end_filter = static_cast<int>
          (boost::math::iround(fmodf(d_last_filter + d_delay * d_dec_rate + accum_int, \
                                     static_cast<float>(d_int_rate))));

        d_est_phase_change = d_last_filter - (end_filter + accum_frac);
      }

      pfb_arb_resampler_ccc::~pfb_arb_resampler_ccc()
      {
        for(unsigned int i = 0; i < d_int_rate; i++) {
          delete d_filters[i];
          delete d_diff_filters[i];
        }
      }

      void
      pfb_arb_resampler_ccc::create_taps(const std::vector<gr_complex> &newtaps,
                                         std::vector< std::vector<gr_complex> > &ourtaps,
                                         std::vector<fir_filter_ccc*> &ourfilter)
      {
        unsigned int ntaps = newtaps.size();
        d_taps_per_filter = (unsigned int)ceil((double)ntaps/(double)d_int_rate);

        // Create d_numchan vectors to store each channel's taps
        ourtaps.resize(d_int_rate);

        // Make a vector of the taps plus fill it out with 0's to fill
        // each polyphase filter with exactly d_taps_per_filter
        std::vector<gr_complex> tmp_taps;
        tmp_taps = newtaps;
        while((float)(tmp_taps.size()) < d_int_rate*d_taps_per_filter) {
          tmp_taps.push_back(0.0);
        }

        for(unsigned int i = 0; i < d_int_rate; i++) {
          // Each channel uses all d_taps_per_filter with 0's if not enough taps to fill out
          ourtaps[i] = std::vector<gr_complex>(d_taps_per_filter, 0);
          for(unsigned int j = 0; j < d_taps_per_filter; j++) {
            ourtaps[i][j] = tmp_taps[i + j*d_int_rate];
          }

          // Build a filter for each channel and add it's taps to it
          ourfilter[i]->set_taps(ourtaps[i]);
        }
      }

      void
      pfb_arb_resampler_ccc::create_diff_taps(const std::vector<gr_complex> &newtaps,
                                              std::vector<gr_complex> &difftaps)
      {
        // Calculate the differential taps using a derivative filter
        std::vector<gr_complex> diff_filter(2);
        diff_filter[0] = -1;
        diff_filter[1] = 1;

        for(unsigned int i = 0; i < newtaps.size()-1; i++) {
          gr_complex tap = 0;
          for(unsigned int j = 0; j < diff_filter.size(); j++) {
            tap += diff_filter[j]*newtaps[i+j];
          }
          difftaps.push_back(tap);
        }
        difftaps.push_back(0);
      }

      void
      pfb_arb_resampler_ccc::set_taps(const std::vector<gr_complex> &taps)
      {
        std::vector<gr_complex> dtaps;
        create_diff_taps(taps, dtaps);
        create_taps(taps, d_taps, d_filters);
        create_taps(dtaps, d_dtaps, d_diff_filters);
      }

      std::vector<std::vector<gr_complex> >
      pfb_arb_resampler_ccc::taps() const
      {
        return d_taps;
      }

      void
      pfb_arb_resampler_ccc::print_taps()
      {
        unsigned int i, j;
        for(i = 0; i < d_int_rate; i++) {
          printf("filter[%d]: [", i);
          for(j = 0; j < d_taps_per_filter; j++) {
            printf(" %.4e + j%.4e", d_taps[i][j].real(), d_taps[i][j].imag());
          }
          printf("]\n");
        }
      }

      void
      pfb_arb_resampler_ccc::set_rate(float rate)
      {
        d_dec_rate = (unsigned int)floor(d_int_rate/rate);
        d_flt_rate = (d_int_rate/rate) - d_dec_rate;
      }

      void
      pfb_arb_resampler_ccc::set_phase(float ph)
      {
        if((ph < 0) || (ph >= 2.0*M_PI)) {
          throw std::runtime_error("pfb_arb_resampler_ccc: set_phase value out of bounds [0, 2pi).\n");
        }

        float ph_diff = 2.0*M_PI / (float)d_filters.size();
        d_last_filter = static_cast<int>(ph / ph_diff);
      }

      float
      pfb_arb_resampler_ccc::phase() const
      {
        float ph_diff = 2.0*M_PI / static_cast<float>(d_filters.size());
        return d_last_filter * ph_diff;
      }

      unsigned int
      pfb_arb_resampler_ccc::taps_per_filter() const
      {
        return d_taps_per_filter;
      }

      float
      pfb_arb_resampler_ccc::phase_offset(float freq, float fs)
      {
        float adj = (2.0*M_PI)*(freq/fs)/static_cast<float>(d_int_rate);
        return -adj * d_est_phase_change;
      }

      int
      pfb_arb_resampler_ccc::filter(gr_complex *output, gr_complex *input,
                                    int n_to_read, int &n_read)
      {
        int i_out = 0, i_in = 0;
        unsigned int j = d_last_filter;;
        gr_complex o0, o1;

        while(i_in < n_to_read) {
          // start j by wrapping around mod the number of channels
          while(j < d_int_rate) {
            // Take the current filter and derivative filter output
            o0 = d_filters[j]->filter(&input[i_in]);
            o1 = d_diff_filters[j]->filter(&input[i_in]);

            output[i_out] = o0 + o1*d_acc;     // linearly interpolate between samples
            i_out++;

            // Adjust accumulator and index into filterbank
            d_acc += d_flt_rate;
            j += d_dec_rate + (int)floor(d_acc);
            d_acc = fmodf(d_acc, 1.0);
          }
          i_in += (int)(j / d_int_rate);
          j = j % d_int_rate;
        }
        d_last_filter = j; // save last filter state for re-entry

        n_read = i_in;   // return how much we've actually read
        return i_out;    // return how much we've produced
      }

      /****************************************************************/

      pfb_arb_resampler_fff::pfb_arb_resampler_fff(float rate,
                                                   const std::vector<float> &taps,
                                                   unsigned int filter_size)
      {
        d_acc = 0; // start accumulator at 0

        /* The number of filters is specified by the user as the
           filter size; this is also the interpolation rate of the
           filter. We use it and the rate provided to determine the
           decimation rate. This acts as a rational resampler. The
           flt_rate is calculated as the residual between the integer
           decimation rate and the real decimation rate and will be
           used to determine to interpolation point of the resampling
           process.
        */
        d_int_rate = filter_size;
        set_rate(rate);

        d_last_filter = (taps.size()/2) % filter_size;

        d_filters = std::vector<fir_filter_fff*>(d_int_rate);
        d_diff_filters = std::vector<fir_filter_fff*>(d_int_rate);

        // Create an FIR filter for each channel and zero out the taps
        std::vector<float> vtaps(0, d_int_rate);
        for(unsigned int i = 0; i < d_int_rate; i++) {
          d_filters[i] = new fir_filter_fff(1, vtaps);
          d_diff_filters[i] = new fir_filter_fff(1, vtaps);
        }

        // Now, actually set the filters' taps
        set_taps(taps);

        // Delay is based on number of taps per filter arm. Round to
        // the nearest integer.
        float delay = rate * (taps_per_filter() - 1.0) / 2.0;
        d_delay = static_cast<int>(boost::math::iround(delay));

        // This calculation finds the phase offset induced by the
        // arbitrary resampling. It's based on which filter arm we are
        // at the filter's group delay plus the fractional offset
        // between the samples. Calculated here based on the rotation
        // around nfilts starting at start_filter.
        float accum = d_delay * d_flt_rate;
        int   accum_int = static_cast<int>(accum);
        float accum_frac = accum - accum_int;
        int end_filter = static_cast<int>
          (boost::math::iround(fmodf(d_last_filter + d_delay * d_dec_rate + accum_int, \
                                     static_cast<float>(d_int_rate))));

        d_est_phase_change = d_last_filter - (end_filter + accum_frac);
      }

      pfb_arb_resampler_fff::~pfb_arb_resampler_fff()
      {
        for(unsigned int i = 0; i < d_int_rate; i++) {
          delete d_filters[i];
          delete d_diff_filters[i];
        }
      }

      void
      pfb_arb_resampler_fff::create_taps(const std::vector<float> &newtaps,
                                         std::vector< std::vector<float> > &ourtaps,
                                         std::vector<fir_filter_fff*> &ourfilter)
      {
        unsigned int ntaps = newtaps.size();
        d_taps_per_filter = (unsigned int)ceil((double)ntaps/(double)d_int_rate);

        // Create d_numchan vectors to store each channel's taps
        ourtaps.resize(d_int_rate);

        // Make a vector of the taps plus fill it out with 0's to fill
        // each polyphase filter with exactly d_taps_per_filter
        std::vector<float> tmp_taps;
        tmp_taps = newtaps;
        while((float)(tmp_taps.size()) < d_int_rate*d_taps_per_filter) {
          tmp_taps.push_back(0.0);
        }

        for(unsigned int i = 0; i < d_int_rate; i++) {
          // Each channel uses all d_taps_per_filter with 0's if not enough taps to fill out
          ourtaps[i] = std::vector<float>(d_taps_per_filter, 0);
          for(unsigned int j = 0; j < d_taps_per_filter; j++) {
            ourtaps[i][j] = tmp_taps[i + j*d_int_rate];
          }

          // Build a filter for each channel and add it's taps to it
          ourfilter[i]->set_taps(ourtaps[i]);
        }
      }

      void
      pfb_arb_resampler_fff::create_diff_taps(const std::vector<float> &newtaps,
                                              std::vector<float> &difftaps)
      {
        // Calculate the differential taps using a derivative filter
        std::vector<float> diff_filter(2);
        diff_filter[0] = -1;
        diff_filter[1] = 1;

        for(unsigned int i = 0; i < newtaps.size()-1; i++) {
          float tap = 0;
          for(unsigned int j = 0; j < diff_filter.size(); j++) {
            tap += diff_filter[j]*newtaps[i+j];
          }
          difftaps.push_back(tap);
        }
        difftaps.push_back(0);
      }

      void
      pfb_arb_resampler_fff::set_taps(const std::vector<float> &taps)
      {
        std::vector<float> dtaps;
        create_diff_taps(taps, dtaps);
        create_taps(taps, d_taps, d_filters);
        create_taps(dtaps, d_dtaps, d_diff_filters);
      }

      std::vector<std::vector<float> >
      pfb_arb_resampler_fff::taps() const
      {
        return d_taps;
      }

      void
      pfb_arb_resampler_fff::print_taps()
      {
        unsigned int i, j;
        for(i = 0; i < d_int_rate; i++) {
          printf("filter[%d]: [", i);
          for(j = 0; j < d_taps_per_filter; j++) {
            printf(" %.4e", d_taps[i][j]);
          }
          printf("]\n");
        }
      }

      void
      pfb_arb_resampler_fff::set_rate(float rate)
      {
        d_dec_rate = (unsigned int)floor(d_int_rate/rate);
        d_flt_rate = (d_int_rate/rate) - d_dec_rate;
      }

      void
      pfb_arb_resampler_fff::set_phase(float ph)
      {
        if((ph < 0) || (ph >= 2.0*M_PI)) {
          throw std::runtime_error("pfb_arb_resampler_fff: set_phase value out of bounds [0, 2pi).\n");
        }

        float ph_diff = 2.0*M_PI / (float)d_filters.size();
        d_last_filter = static_cast<int>(ph / ph_diff);
      }

      float
      pfb_arb_resampler_fff::phase() const
      {
        float ph_diff = 2.0*M_PI / static_cast<float>(d_filters.size());
        return d_last_filter * ph_diff;
      }

      unsigned int
      pfb_arb_resampler_fff::taps_per_filter() const
      {
        return d_taps_per_filter;
      }

      float
      pfb_arb_resampler_fff::phase_offset(float freq, float fs)
      {
        float adj = (2.0*M_PI)*(freq/fs)/static_cast<float>(d_int_rate);
        return -adj * d_est_phase_change;
      }

      int
      pfb_arb_resampler_fff::filter(float *output, float *input,
                                    int n_to_read, int &n_read)
      {
        int i_out = 0, i_in = 0;
        unsigned int j = d_last_filter;;
        float o0, o1;

        while(i_in < n_to_read) {
          // start j by wrapping around mod the number of channels
          while(j < d_int_rate) {
            // Take the current filter and derivative filter output
            o0 = d_filters[j]->filter(&input[i_in]);
            o1 = d_diff_filters[j]->filter(&input[i_in]);

            output[i_out] = o0 + o1*d_acc;     // linearly interpolate between samples
            i_out++;

            // Adjust accumulator and index into filterbank
            d_acc += d_flt_rate;
            j += d_dec_rate + (int)floor(d_acc);
            d_acc = fmodf(d_acc, 1.0);
          }
          i_in += (int)(j / d_int_rate);
          j = j % d_int_rate;
        }
        d_last_filter = j; // save last filter state for re-entry

        n_read = i_in;   // return how much we've actually read
        return i_out;    // return how much we've produced
      }

    } /* namespace kernel */
  } /* namespace filter */
} /* namespace gr */

ext: cc


text:
/**
 *  @file
 *  @copyright defined in agr/LICENSE.txt
 */
#pragma once

#include <fc/optional.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/thread_pool.hpp>
#include <boost/asio/post.hpp>
#include <future>
#include <memory>

namespace agrio { namespace chain {

   /**
    * Wrapper class for boost asio thread pool and io_context run.
    * Also names threads so that tools like htop can see thread name.
    */
   class named_thread_pool {
   public:
      // name_prefix is name appended with -## of thread.
      // short name_prefix (6 chars or under) is recommended as console_appender uses 9 chars for thread name
      named_thread_pool( std::string name_prefix, size_t num_threads );

      // calls stop()
      ~named_thread_pool();

      boost::asio::io_context& get_executor() { return _ioc; }

      // destroy work guard, stop io_context, join thread_pool, and stop thread_pool
      void stop();

   private:
      using ioc_work_t = boost::asio::executor_work_guard<boost::asio::io_context::executor_type>;

      boost::asio::thread_pool       _thread_pool;
      boost::asio::io_context        _ioc;
      fc::optional<ioc_work_t>       _ioc_work;
   };

   // async on thread_pool and return future
   template<typename F>
   auto async_thread_pool( boost::asio::io_context& thread_pool, F&& f ) {
      auto task = std::make_shared<std::packaged_task<decltype( f() )()>>( std::forward<F>( f ) );
      boost::asio::post( thread_pool, [task]() { (*task)(); } );
      return task->get_future();
   }

} } // agrio::chain

ext: hpp

max_stars_repo_name: aggregion/agr

max_issues_repo_name: aggregion/agr

max_forks_repo_name: aggregion/agr


text:
/**
 * \file WhiteNoiseGeneratorFilter.cpp
 */

#include "WhiteNoiseGeneratorFilter.h"

#include <boost/math/constants/constants.hpp>

namespace ATK
{
  template<typename DataType_>
  WhiteNoiseGeneratorFilter<DataType_>::WhiteNoiseGeneratorFilter()
  :Parent(0, 1)
  {
  }

  template<typename DataType_>
  void WhiteNoiseGeneratorFilter<DataType_>::set_volume(DataType_ volume)
  {
    this->volume = volume;
    dist.param(typename boost::random::uniform_real_distribution<DataType_>::param_type(static_cast<DataType>(offset - volume), static_cast<DataType>(offset + volume)));
  }

  template<typename DataType_>
  DataType_ WhiteNoiseGeneratorFilter<DataType_>::get_volume() const
  {
    return volume;
  }

  template<typename DataType_>
  void WhiteNoiseGeneratorFilter<DataType_>::set_offset(DataType_ offset)
  {
    this->offset = offset;
    dist.param(typename boost::random::uniform_real_distribution<DataType_>::param_type(static_cast<DataType>(offset - volume), static_cast<DataType>(offset + volume)));
  }

  template<typename DataType_>
  DataType_ WhiteNoiseGeneratorFilter<DataType_>::get_offset() const
  {
    return offset;
  }

  template<typename DataType_>
  void WhiteNoiseGeneratorFilter<DataType_>::process_impl(gsl::index size) const
  {
    DataType* ATK_RESTRICT output = outputs[0];
    for(gsl::index i = 0; i < size; ++i)
    {
      output[i] = dist(gen);
    }
  }

#if ATK_ENABLE_INSTANTIATION
  template class WhiteNoiseGeneratorFilter<float>;
#endif
  template class WhiteNoiseGeneratorFilter<double>;
}

ext: cpp

max_stars_repo_name: AudioTK/AudioTK

max_issues_repo_name: AudioTK/AudioTK

max_forks_repo_name: AudioTK/AudioTK


text:
//          Copyright Oliver Kowalke 2009.
// 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)

#include <cstdlib>
#include <iostream>

#include <boost/coroutine/all.hpp>

#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>

int main()
{
    boost::coroutines::asymmetric_coroutine<int>::pull_type c(
        [](boost::coroutines::asymmetric_coroutine<int>::push_type& yield) {
            for (int i = 0; i < 5; ++i) {
                yield(i);
            }
        });
    auto crange = boost::make_iterator_range(boost::begin(c), boost::end(c));
    for (auto n : crange) {
        std::cout << n << " ";
    }

    std::cout << "\nDone" << std::endl;

    return EXIT_SUCCESS;
}


text:
//
// Created by arssivka on 11/30/15.
//

#include <boost/program_options.hpp>
#include <boost/make_shared.hpp>
#include <rd/hardware/Robot.h>
#include <rd/hardware/RemoteJoints.h>
#include <rd/hardware/RemoteLEDs.h>
#include <rd/hardware/RemoteCamera.h>
#include <rd/hardware/RemoteGyro.h>
#include <rd/hardware/RemoteAccelerometer.h>
#include <rd/hardware/RemoteKinematics.h>
#include <rd/hardware/RemoteAngle.h>
#include <rd/network/RPCServer.h>
#include <RemoteVision.h>
///testing
#include <nb/StepGenerator.h>
#include <rd/hardware/Locomotion.h>
#include <rd/hardware/RemoteLocomotion.h>

using namespace boost;
namespace po = boost::program_options;

int main(int argc, const char* const argv[]) {
    // Server parameter variables
    std::string ip;
    int port;
    std::string config_file;
    // Declare the supported options.
    po::options_description desc("Allowed options");
    desc.add_options()
            ("help,h", "produce help message")
            ("ip", po::value<std::string>(&ip)->default_value("127.0.0.1"), "set server ip")
            ("port", po::value<int>(&port)->default_value(5469), "set server port")
            ("config", po::value<std::string>(), "path to configuration file");

    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);

    if (vm.count("help")) {
        std::cout << desc << std::endl;
        return 1;
    }

    if (vm.count("config")) {
        config_file = vm["config"].as<std::string>();
    } else {
        std::cout << "Configuration file was not set" << std::endl;
        return EXIT_FAILURE;
    }

    boost::shared_ptr<rd::Robot> robot = boost::make_shared<rd::Robot>("NAO", ip, 9559, config_file);
    //testing

    boost::shared_ptr<rd::Kinematics> kinematics(
            make_shared<rd::Kinematics>(robot->getClock(), robot->getJoints(), robot->getConfig()));

    boost::shared_ptr<rd::Locomotion> locomotion(make_shared<rd::Locomotion>(robot));

    rd::RPCServer srv(port);
    srv.addModule(make_shared<rd::RemoteJoints>(robot->getJoints()));
    srv.addModule(make_shared<rd::RemoteCamera>(robot->getTopCamera(), robot->getBotCamera()));
    srv.addModule(make_shared<rd::RemoteLEDs>(robot->getLEDs()));
    srv.addModule(make_shared<rd::RemoteGyro>(robot->getGyro()));
    srv.addModule(make_shared<rd::RemoteAccelerometer>(robot->getAccelerometer()));
    srv.addModule(make_shared<rd::RemoteAngle>(robot->getAngle()));
    srv.addModule(make_shared<rd::RemoteKinematics>(kinematics));
    srv.addModule(make_shared<rd::RemoteLocomotion>(locomotion));
    srv.addModule(make_shared<rd::RemoteVision>(robot->getVision(), robot->getBotCamera()));
    srv.run();
    return EXIT_SUCCESS;
}

max_stars_repo_name: arssivka/naomech

max_issues_repo_name: arssivka/naomech

max_forks_repo_name: arssivka/naomech


text:
#pragma once

#include <list>
#include <future>
#include <memory>
#include <functional>

#include <boost/asio/io_context.hpp>
#include <boost/asio/local/stream_protocol.hpp>

namespace bamboo {
namespace concurrency {

class AsyncRun final {
 public:
  AsyncRun(boost::asio::io_context& io);
  ~AsyncRun();

  template <typename RETURN>
  void Run(std::function<RETURN()>&& call, std::function<void(RETURN)>&& cb) {
    auto f = std::async(std::launch::async, std::move(call));
    auto future = std::make_shared<std::future<RETURN>>(std::move(f));
    auto callcb = std::make_shared<std::function<void(RETURN)>>(std::move(cb));

    std::unique_ptr<AsyncInfo> info(new AsyncInfo);
    info->checker = [future]() -> bool {
      return future->wait_for(std::chrono::seconds(0)) == std::future_status::ready;
    };

    info->callback = [callcb, future]() {
      (*callcb)(future->get());
    };

    sendLock_.lock();
    sendList_.push_back(std::move(info));
    sendLock_.unlock();
  };

  template <typename RETURN>
  void Run(RETURN(*call)(), void(*cb)(RETURN)) {
    Run(std::function<RETURN()>(call), std::function<void(RETURN)>(cb));
  }

  template <typename RETURN>
  void Run(std::function<RETURN()>&& call, void(*cb)(RETURN)) {
    Run(std::move(call), std::function<void(RETURN)>(cb));
  }

  template <typename RETURN>
  void Run(RETURN(*call)(), std::function<void(RETURN)>&& cb) {
    Run(std::function<RETURN()>(call), std::move(cb));
  }

 private:
  struct AsyncInfo {
    std::function<bool()> checker;
    std::function<void()> callback;
  };

  void MasterThread();
  void HandleDone();
  void DoRead();

  using AsyncInfoPtr = std::unique_ptr<AsyncInfo>;

  std::atomic_bool isRunning_{true};
  std::thread masterThread_;
  boost::asio::local::stream_protocol::socket sender_;
  boost::asio::local::stream_protocol::socket receiver_;
  std::array<char, 4> buffer_;

  std::list<AsyncInfoPtr> sendList_;
  std::mutex sendLock_;
  std::list<AsyncInfoPtr> checkList_;

  std::unique_ptr<std::list<AsyncInfoPtr>> pendingDoneList_;
  std::unique_ptr<std::list<AsyncInfoPtr>> doneList_;
  std::mutex doneLock_;
};

}
}

max_stars_repo_name: difosschan/bamboo

max_issues_repo_name: difosschan/bamboo

max_forks_repo_name: difosschan/bamboo


text:
#ifndef BOOST_PP_IS_ITERATING
    ///////////////////////////////////////////////////////////////////////////////
    /// \file call.hpp
    /// Contains definition of the call<> transform.
    //
    //  Copyright 2008 Eric Niebler. 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_PROTO_TRANSFORM_CALL_HPP_EAN_11_02_2007
    #define BOOST_PROTO_TRANSFORM_CALL_HPP_EAN_11_02_2007

    #include <boost/xpressive/proto/detail/prefix.hpp>
    #include <boost/preprocessor/cat.hpp>
    #include <boost/preprocessor/iteration/iterate.hpp>
    #include <boost/preprocessor/repetition/enum.hpp>
    #include <boost/preprocessor/repetition/repeat.hpp>
    #include <boost/preprocessor/repetition/enum_params.hpp>
    #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
    #include <boost/utility/result_of.hpp>
    #include <boost/xpressive/proto/proto_fwd.hpp>
    #include <boost/xpressive/proto/traits.hpp>
    #include <boost/xpressive/proto/detail/dont_care.hpp>
    #include <boost/xpressive/proto/detail/as_lvalue.hpp>
    #include <boost/xpressive/proto/detail/suffix.hpp>

    namespace boost { namespace proto
    {

        namespace transform
        {
            namespace detail
            {
                using proto::detail::uncv;
                using proto::detail::as_lvalue;
                using proto::detail::dont_care;
                typedef char (&yes_type)[2];
                typedef char no_type;

                struct private_type_
                {
                    private_type_ const &operator ,(int) const;
                };

                template<typename T>
                yes_type check_fun_arity(T const &);

                no_type check_fun_arity(private_type_ const &);

                template<typename Fun>
                struct callable0_wrap : Fun
                {
                    callable0_wrap();
                    typedef private_type_ const &(*pfun0)();
                    operator pfun0() const;
                };

                template<typename Fun>
                struct callable1_wrap : Fun
                {
                    callable1_wrap();
                    typedef private_type_ const &(*pfun1)(dont_care);
                    operator pfun1() const;
                };

                template<typename Fun>
                struct callable2_wrap : Fun
                {
                    callable2_wrap();
                    typedef private_type_ const &(*pfun2)(dont_care, dont_care);
                    operator pfun2() const;
                };

                template<typename Fun>
                struct arity0
                {
                    static callable0_wrap<Fun> &fun;

                    static int const value =
                        sizeof(yes_type) == sizeof(check_fun_arity((fun(), 0)))
                      ? 0
                      : 3;
                };

                template<typename Fun, typename A0>
                struct arity1
                {
                    static callable1_wrap<Fun> &fun;
                    static A0 &a0;

                    static int const value =
                        sizeof(yes_type) == sizeof(check_fun_arity((fun(a0), 0)))
                      ? 1
                      : 3;
                };

                template<typename Fun, typename A0, typename A1>
                struct arity2
                {
                    static callable2_wrap<Fun> &fun;
                    static A0 &a0;
                    static A1 &a1;

                    static int const value =
                        sizeof(yes_type) == sizeof(check_fun_arity((fun(a0, a1), 0)))
                      ? 2
                      : 3;
                };

                template<typename Fun, typename Expr, typename State, typename Visitor>
                struct call3
                {
                    typedef typename boost::result_of<Fun(Expr, State, Visitor)>::type type;

                    template<typename Expr2, typename State2, typename Visitor2>
                    static type call(Expr2 &expr, State2 &state, Visitor2 &visitor)
                    {
                        Fun f;
                        return f(expr, state, visitor);
                    }
                };

                template<typename Fun, typename Expr, typename State, typename Visitor
                  , int Arity = arity0<Fun>::value>
                struct call0
                  : call3<Fun, Expr, State, Visitor>
                {};

                template<typename Fun, typename Expr, typename State, typename Visitor>
                struct call0<Fun, Expr, State, Visitor, 0>
                {
                    typedef typename boost::result_of<Fun()>::type type;

                    template<typename Expr2, typename State2, typename Visitor2>
                    static type call(Expr2 &, State2 &, Visitor2 &)
                    {
                        Fun f;
                        return f();
                    }
                };

                template<typename Fun, typename Expr, typename State, typename Visitor
                  , int Arity = arity1<Fun, Expr>::value>
                struct call1
                  : call3<Fun, Expr, State, Visitor>
                {};

                template<typename Fun, typename Expr, typename State, typename Visitor>
                struct call1<Fun, Expr, State, Visitor, 1>
                {
                    typedef typename boost::result_of<Fun(Expr)>::type type;

                    template<typename Expr2, typename State2, typename Visitor2>
                    static type call(Expr2 &expr, State2 &, Visitor2 &)
                    {
                        Fun f;
                        return f(expr);
                    }
                };

                template<typename Fun, typename Expr, typename State, typename Visitor
                  , int Arity = arity2<Fun, Expr, State>::value>
                struct call2
                  : call3<Fun, Expr, State, Visitor>
                {};

                template<typename Fun, typename Expr, typename State, typename Visitor>
                struct call2<Fun, Expr, State, Visitor, 2>
                {
                    typedef typename boost::result_of<Fun(Expr, State)>::type type;

                    template<typename Expr2, typename State2, typename Visitor2>
                    static type call(Expr2 &expr, State2 &state, Visitor2 &)
                    {
                        Fun f;
                        return f(expr, state);
                    }
                };
            } // namespace detail

            /// \brief Wrap \c PrimitiveTransform so that <tt>when\<\></tt> knows
            /// it is callable. Requires that the parameter is actually a
            /// PrimitiveTransform.
            ///
            /// This form of <tt>call\<\></tt> is useful for annotating an
            /// arbitrary PrimitiveTransform as callable when using it with
            /// <tt>when\<\></tt>. Consider the following transform, which
            /// is parameterized with another transform.
            ///
            /// \code
            /// template<typename Grammar>
            /// struct Foo
            ///   : when<
            ///         posit<Grammar>
            ///       , Grammar(_arg)   // May or may not work.
            ///     >
            /// {};
            /// \endcode
            ///
            /// The problem with the above is that <tt>when\<\></tt> may or
            /// may not recognize \c Grammar as callable, depending on how
            /// \c Grammar is implemented. (See <tt>is_callable\<\></tt> for
            /// a discussion of this issue.) The above code can guard against
            /// the issue by wrapping \c Grammar in <tt>call\<\></tt>, such
            /// as:
            ///
            /// \code
            /// template<typename Grammar>
            /// struct Foo
            ///   : when<
            ///         posit<Grammar>
            ///       , call<Grammar>(_arg)   // OK, this works
            ///     >
            /// {};
            /// \endcode
            ///
            /// The above could also have been written as:
            ///
            /// \code
            /// template<typename Grammar>
            /// struct Foo
            ///   : when<
            ///         posit<Grammar>
            ///       , call<Grammar(_arg)>   // OK, this works, too
            ///     >
            /// {};
            /// \endcode
            template<typename PrimitiveTransform>
            struct call : PrimitiveTransform
            {
                BOOST_PROTO_CALLABLE()
            };

            /// \brief Either call the PolymorphicFunctionObject with 0
            /// arguments, or invoke the PrimitiveTransform with 3
            /// arguments.
            template<typename Fun>
            struct call<Fun()> : proto::callable
            {
                template<typename Sig>
                struct result;

                template<typename This, typename Expr, typename State, typename Visitor>
                struct result<This(Expr, State, Visitor)>
                {
                    /// If \c Fun is a nullary PolymorphicFunctionObject, \c type is a typedef
                    /// for <tt>boost::result_of\<Fun()\>::::type</tt>. Otherwise, it is
                    /// a typedef for <tt>boost::result_of\<Fun(Expr, State, Visitor)\>::::type</tt>.
                    typedef
                        typename detail::call0<
                            Fun
                          , Expr
                          , State
                          , Visitor
                        >::type
                    type;
                };

                /// Either call the PolymorphicFunctionObject \c Fun with 0 arguments; or
                /// invoke the PrimitiveTransform \c Fun with 3 arguments: the current
                /// expression, state, and visitor.
                ///
                /// If \c Fun is a nullary PolymorphicFunctionObject, return <tt>Fun()()</tt>.
                /// Otherwise, return <tt>Fun()(expr, state, visitor)</tt>.
                ///
                /// \param expr The current expression
                /// \param state The current state
                /// \param visitor An arbitrary visitor
                template<typename Expr, typename State, typename Visitor>
                typename result<void(Expr, State, Visitor)>::type
                operator ()(Expr const &expr, State const &state, Visitor &visitor) const
                {
                    typedef
                        detail::call0<
                            Fun
                          , Expr
                          , State
                          , Visitor
                        >
                    impl;

                    return impl::call(expr, state, visitor);
                }
            };

            /// \brief Either call the PolymorphicFunctionObject with 1
            /// argument, or invoke the PrimitiveTransform with 3
            /// arguments.
            template<typename Fun, typename A0>
            struct call<Fun(A0)> : proto::callable
            {
                template<typename Sig>
                struct result;

                template<typename This, typename Expr, typename State, typename Visitor>
                struct result<This(Expr, State, Visitor)>
                {
                    /// Let \c x be <tt>when\<_, A0\>()(expr, state, visitor)</tt> and \c X
                    /// be the type of \c x.
                    /// If \c Fun is a unary PolymorphicFunctionObject that accepts \c x,
                    /// then \c type is a typedef for <tt>boost::result_of\<Fun(X)\>::::type</tt>.
                    /// Otherwise, it is a typedef for <tt>boost::result_of\<Fun(X, State, Visitor)\>::::type</tt>.
                    typedef
                        typename detail::call1<
                            Fun
                          , typename when<_, A0>::template result<void(Expr, State, Visitor)>::type
                          , State
                          , Visitor
                        >::type
                    type;
                };

                /// Either call the PolymorphicFunctionObject with 1 argument:
                /// the result of applying the \c A0 transform; or
                /// invoke the PrimitiveTransform with 3 arguments:
                /// result of applying the \c A0 transform, the state, and the
                /// visitor.
                ///
                /// Let \c x be <tt>when\<_, A0\>()(expr, state, visitor)</tt>.
                /// If \c Fun is a unary PolymorphicFunctionObject that accepts \c x,
                /// then return <tt>Fun()(x)</tt>. Otherwise, return
                /// <tt>Fun()(x, state, visitor)</tt>.
                ///
                /// \param expr The current expression
                /// \param state The current state
                /// \param visitor An arbitrary visitor
                template<typename Expr, typename State, typename Visitor>
                typename result<void(Expr, State, Visitor)>::type
                operator ()(Expr const &expr, State const &state, Visitor &visitor) const
                {
                    typedef
                        detail::call1<
                            Fun
                          , typename when<_, A0>::template result<void(Expr, State, Visitor)>::type
                          , State
                          , Visitor
                        >
                    impl;

                    return impl::call(
                        detail::as_lvalue(when<_, A0>()(expr, state, visitor))
                      , state
                      , visitor
                    );
                }
            };

            /// \brief Either call the PolymorphicFunctionObject with 2
            /// arguments, or invoke the PrimitiveTransform with 3
            /// arguments.
            template<typename Fun, typename A0, typename A1>
            struct call<Fun(A0, A1)> : proto::callable
            {
                template<typename Sig>
                struct result;

                template<typename This, typename Expr, typename State, typename Visitor>
                struct result<This(Expr, State, Visitor)>
                {
                    /// Let \c x be <tt>when\<_, A0\>()(expr, state, visitor)</tt> and \c X
                    /// be the type of \c x.
                    /// Let \c y be <tt>when\<_, A1\>()(expr, state, visitor)</tt> and \c Y
                    /// be the type of \c y.
                    /// If \c Fun is a binary PolymorphicFunction object that accepts \c x
                    /// and \c y, then \c type is a typedef for
                    /// <tt>boost::result_of\<Fun(X, Y)\>::::type</tt>. Otherwise, it is
                    /// a typedef for <tt>boost::result_of\<Fun(X, Y, Visitor)\>::::type</tt>.
                    typedef
                        typename detail::call2<
                            Fun
                          , typename when<_, A0>::template result<void(Expr, State, Visitor)>::type
                          , typename when<_, A1>::template result<void(Expr, State, Visitor)>::type
                          , Visitor
                        >::type
                    type;
                };

                /// Either call the PolymorphicFunctionObject with 2 arguments:
                /// the result of applying the \c A0 transform, and the
                /// result of applying the \c A1 transform; or invoke the
                /// PrimitiveTransform with 3 arguments: the result of applying
                /// the \c A0 transform, the result of applying the \c A1
                /// transform, and the visitor.
                ///
                /// Let \c x be <tt>when\<_, A0\>()(expr, state, visitor)</tt>.
                /// Let \c y be <tt>when\<_, A1\>()(expr, state, visitor)</tt>.
                /// If \c Fun is a binary PolymorphicFunction object that accepts \c x
                /// and \c y, return <tt>Fun()(x, y)</tt>. Otherwise, return
                /// <tt>Fun()(x, y, visitor)</tt>.
                ///
                /// \param expr The current expression
                /// \param state The current state
                /// \param visitor An arbitrary visitor
                template<typename Expr, typename State, typename Visitor>
                typename result<void(Expr, State, Visitor)>::type
                operator ()(Expr const &expr, State const &state, Visitor &visitor) const
                {
                    typedef
                        detail::call2<
                            Fun
                          , typename when<_, A0>::template result<void(Expr, State, Visitor)>::type
                          , typename when<_, A1>::template result<void(Expr, State, Visitor)>::type
                          , Visitor
                        >
                    impl;

                    return impl::call(
                        detail::as_lvalue(when<_, A0>()(expr, state, visitor))
                      , detail::as_lvalue(when<_, A1>()(expr, state, visitor))
                      , visitor
                    );
                }
            };

            /// \brief Call the PolymorphicFunctionObject or the
            /// PrimitiveTransform with the current expression, state
            /// and visitor, transformed according to \c A0, \c A1, and
            /// \c A2, respectively.
            template<typename Fun, typename A0, typename A1, typename A2>
            struct call<Fun(A0, A1, A2)> : proto::callable
            {
                template<typename Sig>
                struct result;

                template<typename This, typename Expr, typename State, typename Visitor>
                struct result<This(Expr, State, Visitor)>
                {
                    typedef typename when<_, A0>::template result<void(Expr, State, Visitor)>::type a0;
                    typedef typename when<_, A1>::template result<void(Expr, State, Visitor)>::type a1;
                    typedef typename when<_, A2>::template result<void(Expr, State, Visitor)>::type a2;
                    typedef typename boost::result_of<Fun(a0, a1, a2)>::type type;
                };

                /// Let \c x be <tt>when\<_, A0\>()(expr, state, visitor)</tt>.
                /// Let \c y be <tt>when\<_, A1\>()(expr, state, visitor)</tt>.
                /// Let \c z be <tt>when\<_, A2\>()(expr, state, visitor)</tt>.
                /// Return <tt>Fun()(x, y, z)</tt>.
                ///
                /// \param expr The current expression
                /// \param state The current state
                /// \param visitor An arbitrary visitor
                template<typename Expr, typename State, typename Visitor>
                typename result<void(Expr, State, Visitor)>::type
                operator ()(Expr const &expr, State const &state, Visitor &visitor) const
                {
                    Fun f;
                    return f(
                        detail::as_lvalue(when<_, A0>()(expr, state, visitor))
                      , detail::as_lvalue(when<_, A1>()(expr, state, visitor))
                      , detail::uncv(when<_, A2>()(expr, state, visitor)) // HACK
                    );
                }
            };

            #if BOOST_PROTO_MAX_ARITY > 3
            #define BOOST_PP_ITERATION_PARAMS_1 (3, (4, BOOST_PROTO_MAX_ARITY, <boost/xpressive/proto/transform/call.hpp>))
            #include BOOST_PP_ITERATE()
            #endif
        }

        /// INTERNAL ONLY
        ///
        template<typename Fun>
        struct is_callable<transform::call<Fun> >
          : mpl::true_
        {};

    }}

    #endif

#else

    #define N BOOST_PP_ITERATION()

        /// \brief Call the PolymorphicFunctionObject \c Fun with the
        /// current expression, state and visitor, transformed according
        /// to \c A0 through \c AN.
        template<typename Fun BOOST_PP_ENUM_TRAILING_PARAMS(N, typename A)>
        struct call<Fun(BOOST_PP_ENUM_PARAMS(N, A))> : proto::callable
        {
            template<typename Sig>
            struct result;

            template<typename This, typename Expr, typename State, typename Visitor>
            struct result<This(Expr, State, Visitor)>
            {
                #define TMP(Z, M, DATA)                                                             \
                    typedef                                                                         \
                        typename when<_, BOOST_PP_CAT(A, M)>                                        \
                            ::template result<void(Expr, State, Visitor)>                           \
                        ::type                                                                      \
                    BOOST_PP_CAT(a, M);                                                             \
                    /**/
                BOOST_PP_REPEAT(N, TMP, ~)
                #undef TMP

                typedef
                    typename boost::result_of<
                        Fun(BOOST_PP_ENUM_PARAMS(N, a))
                    >::type
                type;
            };

            /// Let \c ax be <tt>when\<_, Ax\>()(expr, state, visitor)</tt>
            /// for each \c x in <tt>[0,N]</tt>.
            /// Return <tt>Fun()(a0, a1,... aN)</tt>.
            ///
            /// \param expr The current expression
            /// \param state The current state
            /// \param visitor An arbitrary visitor
            template<typename Expr, typename State, typename Visitor>
            typename result<void(Expr, State, Visitor)>::type
            operator ()(Expr const &expr, State const &state, Visitor &visitor) const
            {
                Fun f;
                #define TMP(Z, M, DATA) when<_, BOOST_PP_CAT(A, M)>()(expr, state, visitor)
                return f(BOOST_PP_ENUM(N, TMP, ~));
                #undef TMP
            }
        };

    #undef N

#endif

max_issues_repo_name: mcanthony/madedit

max_forks_repo_name: mcanthony/madedit


text:
#include "blob_gc_job.h"
#include "blob_gc_picker.h"
#include "db_impl.h"
#include "delta_compression.h"
#include "rocksdb/options.h"
#include "rocksdb/statistics.h"
#include "test_util/testharness.h"
#include "titan/options.h"
#include "utilities/transactions/write_prepared_txn_db.h"

#include <bits/types/struct_timespec.h>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/operations.hpp>
#include <cstdint>
#include <gtest/gtest.h>
#include <string>

namespace rocksdb {
namespace titandb {

namespace fs = boost::filesystem;
using namespace fs;
using namespace std;

struct HumanReadable {
  uintmax_t size_;
  HumanReadable(size_t size = 0) : size_(size){};
  std::string ToString(bool with_byte = true) {
    int magnitude = 0;
    double mantissa = size_;
    while (mantissa >= 1024) {
      mantissa /= 1024.;
      ++magnitude;
    }

    mantissa = ceil(mantissa * 10.) / 10.;

    stringstream ss;
    ss << fixed << setprecision(2) << mantissa;
    string res = ss.str();
    res += "BKMGTPE"[magnitude];
    if (magnitude > 0) {
      res += 'B';
      if (with_byte)
        res += '(' + to_string(size_) + ')';
    }

    return res;
  }

private:
  friend ostream &operator<<(ostream &os, HumanReadable hr) {
    return os << hr.ToString();
  }
};

string exec(const char *cmd) {
  array<char, 128> buffer;
  string result;
  unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
  if (!pipe) {
    throw runtime_error("popen() failed!");
  }
  while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
    result += buffer.data();
  }
  return result;
}

size_t RunCmdReturnSize(string cmd) {
  string res = exec(cmd.c_str());
  size_t size;
  sscanf(res.c_str(), "%zu", &size);
  return size;
}

size_t GetDatabaseSize(string db_path) {
  string cmd = "du -s " + db_path;
  return RunCmdReturnSize(cmd) * 1024;
}

class DataReader {
public:
  DataReader(size_t expected_percentage = 100)
      : expected_percentage_(expected_percentage){};
  virtual ~DataReader(){};

  enum DataSetType : uint8_t {
    kWikipedia,
    kEnronMail,
    kStackOverFlow,
    kStackOverFlowComment
  };

  const path data_path = "/home/wht/tao-db/test-titan/dataset/DataSet/";
  const path wiki_directory = data_path / "wikipedia/article";
  const path enron_email_directory = data_path / "enronMail";
  const path stack_overflow_directory = data_path / "stackExchange";
  const path stack_overflow_comment_file = data_path / "Comments.xml";

  size_t CountWikipediaHtmls(void) {
    string cmd = "find " + wiki_directory.string() + " -name '*.html' | wc -l";
    return RunCmdReturnSize(cmd);
  }

  size_t CountEnronEmails(void) {
    string cmd = "find " + enron_email_directory.string() + " | wc -l";
    return RunCmdReturnSize(cmd);
  }

  size_t CountStackOverFlowXmlFiles() {
    string cmd = "find " + stack_overflow_directory.string() + " | wc -l";
    return RunCmdReturnSize(cmd);
  }

  size_t CountLinesOfStackOverFlowComment() {
    string cmd = "wc -l " + stack_overflow_comment_file.string();
    return RunCmdReturnSize(cmd);
  }

  void ReadDataPrepare(const DataSetType type) {
    printf("Scaning the number of files that can be Put into the "
           "database...\n");
    printf("Please wait, this may takes a few minutes...\n");
    switch (type) {
    case kWikipedia: {
      to_be_read_ = CountWikipediaHtmls();
      data_directory_ = wiki_directory;
      break;
    }
    case kEnronMail: {
      to_be_read_ = CountEnronEmails();
      data_directory_ = enron_email_directory;
      break;
    }
    case kStackOverFlow: {
      to_be_read_ = CountStackOverFlowXmlFiles();
      data_directory_ = stack_overflow_directory;
      break;
    }
    case kStackOverFlowComment: {
      to_be_read_ = CountLinesOfStackOverFlowComment();
      data_directory_ = stack_overflow_comment_file;
      break;
    }
    default:
      FAIL() << "wrong data set type!\n";
    }
    printf("%zu files can be put into the database\n", to_be_read_);
    printf("Data set path = %s\n", data_directory_.c_str());
  }

  virtual void GetSimilarRecords() = 0;

  void PrintFinishInfo() {
    printf("\n\n");
    printf("##################################################\n");
    cout << total_records_ << " records have been put into titan databse!\n";
    cout << put_key_value_size_ << " are the size of keys and values\n";
    printf("%zu (%.2f%%) is the number of similar records that can be delta "
           "compressed\n",
           max_similar_records_,
           (double)max_similar_records_ / total_records_ * 100);
    printf("##################################################\n");
    fflush(stdout);
  }

  void Finish() {
    GetSimilarRecords();
    PrintFinishInfo();
  }

  bool IsFinish() {
    has_been_read_++;
    completion_percentage_ = 100 * has_been_read_ / to_be_read_;
    if (completion_percentage_ - last_completion_ >= 1) {
      printf("\rExpect Put %2zu%%, complete %2zu%%", expected_percentage_,
             completion_percentage_);
      fflush(stdout);
      last_completion_ = completion_percentage_;
    }
    if (completion_percentage_ >= expected_percentage_) {
      printf("\rExpect Put %2zu%%, complete %2zu%%\n", expected_percentage_,
             completion_percentage_);
      return true;
    }
    return false;
  }

  void ParseWikipediaHtml(path path, string &key, string &value) {
    fs::ifstream fin(path);
    stringstream buffer;
    // key: file name
    // key example: Category~Cabarets_in_Paris_7ad0.html
    // value: html file content
    buffer << fin.rdbuf();
    string content = buffer.str();
    string file_name = path.filename().string();
    key = move(file_name);
    value = move(content);
  }

  void ParseEnronMail(path path, string &key, string &value) {
    fs::ifstream fin(path);
    stringstream buffer;
    // key: first line
    // key example: Message-ID: <18782981.1075855378110.JavaMail.evans@thyme>
    // value: remaining email lines
    string first_line;
    getline(fin, first_line);
    buffer << fin.rdbuf();
    string remain_lines = buffer.str();
    key = move(first_line);
    value = move(remain_lines);
  }

  virtual void ExecutePut(const string &key, const string &value) = 0;

  void Put(const string &key, const string &value) {
    ExecutePut(key, value);
    ++total_records_;
    put_key_value_size_.size_ += key.size() + value.size();
  }

  void ReadFilesUnderDirectoryThenPut(const DataSetType type) {
    for (recursive_directory_iterator f(data_directory_), file_end;
         f != file_end; ++f) {
      if (!is_directory(f->path())) {
        string key, value;
        if (type == kWikipedia) {
          ParseWikipediaHtml(f->path(), key, value);
        } else if (type == kEnronMail) {
          ParseEnronMail(f->path(), key, value);
        } else {
          FAIL() << "wrong data set type!\n";
        }
        Put(key, value);
      }
      if (IsFinish())
        break;
    }
  }

  void ReadParseStackOverFlowDataAndPut() {
    for (fs::recursive_directory_iterator file(stack_overflow_directory),
         file_end;
         file != file_end; ++file) {
      if (!is_directory(file->path())) {
        fs::ifstream fin(file->path());
        string line;
        while (getline(fin, line)) {
          if (line.size() < 3)
            continue;
          // find record start with <row
          size_t found = line.find("<row");
          if (found == string::npos) {
            continue;
          }

          size_t start = line.rfind(" Id=");
          //<row  ...  Id="12345" ... ></row>
          // start:    ^
          ASSERT_FALSE(start == string::npos)
              << line << "\ndoesn't find Id=\n\n";

          start += 5;
          //<row  ... Id="12345" ... ></row>
          // start:        ^

          size_t end = line.find('"', start);
          //<row  ... Id="12345" ... ></row>
          // end:               ^

          ASSERT_FALSE(end == string::npos)
              << line << "\ndoesn't find right quotation!\n\n";

          // key = Id = "12345"
          string key = line.substr(start, end - start);
          string &value = line;
          Put(key, value);
        }
      }
      // we count files as finish, not records this time.
      if (IsFinish())
        break;
    }
  }

  void ReadParseStackOverFlowCommentFileAndPut() {
    fs::ifstream fin(stack_overflow_comment_file);
    string line;
    const int kIdStartPosition = 11;
    while (getline(fin, line)) {
      // every record has this pattern:
      //  <row Id="12345" .../>
      // 01234567890
      // so we can find the Id through finding the right quotation after the
      // left quotation

      // some other lines, like start and end of the Comment.xml is not started
      // as "  <row"
      if (line.substr(0, 6) != "  <row") {
        // cout << line << '\n';
        // cout << "doesn't find <row" << "\n\n";
        continue;
      }

      // make sure <row follows the Id=
      ASSERT_STREQ(line.substr(7, 3).c_str(), "Id=") << "doesn't find Id=\n\n";

      // POS_ID_START=11, means the first number of the Id
      size_t pos_id_end = line.find('"', kIdStartPosition);
      ASSERT_NE(pos_id_end, string::npos)
          << line << "\ncan't find right quotation! \n\n";

      string key = line.substr(kIdStartPosition, pos_id_end - kIdStartPosition);
      string &value = line;
      Put(key, value);
      if (IsFinish())
        break;
    }
  }

  inline void PutWikipediaData() {
    ReadDataPrepare(kWikipedia);
    ReadFilesUnderDirectoryThenPut(kWikipedia);
    Finish();
  }
  inline void PutEnronMailData() {
    ReadDataPrepare(kEnronMail);
    ReadFilesUnderDirectoryThenPut(kEnronMail);
    Finish();
  }

  void PutStackOverFlowData() {
    ReadDataPrepare(kStackOverFlow);
    ReadParseStackOverFlowDataAndPut();
    Finish();
  }

  void PutStackOverFlowCommentData() {
    ReadDataPrepare(kStackOverFlowComment);
    ReadParseStackOverFlowCommentFileAndPut();
    Finish();
  }

  size_t to_be_read_;
  size_t has_been_read_ = 0;
  size_t total_records_ = 0;
  size_t completion_percentage_ = 0;
  size_t last_completion_ = 0;
  // expected_percentage_ range:[1-100]
  // once write data process percentage > expected percentage, write will stop
  size_t expected_percentage_;
  size_t max_similar_records_;
  struct HumanReadable put_key_value_size_;
  path data_directory_;
};

class DeltaCompressionTest : public testing::TestWithParam<DeltaCompressType>,
                             public DataReader {
public:
  string dbname_;
  TitanDB *db_;
  DBImpl *base_db_;
  TitanDBImpl *tdb_;
  BlobFileSet *blob_file_set_;
  TitanOptions options_;
  port::Mutex *mutex_;

  void Put(const string &key, const string &value) {
    Status s = db_->Put(WriteOptions(), key, value);
    ASSERT_TRUE(s.ok());
  }

  DeltaCompressionTest() : DataReader(), dbname_(test::TmpDir()) {
    options_.dirname = dbname_ + "/titandb";
    options_.create_if_missing = true;
    options_.disable_background_gc = true;
    options_.env->CreateDirIfMissing(dbname_);
    options_.env->CreateDirIfMissing(options_.dirname);

    // Optimize RocksDB. This is the easiest way to get RocksDB to perform well
    options_.IncreaseParallelism();
    options_.OptimizeLevelStyleCompaction();

    options_.blob_file_delta_compression = GetParam();

    // All value are stored in the blob, so they can be GC and delta compressed.
    options_.min_blob_size = 0;

    // make sure gc all records
    options_.min_gc_batch_size = 0;

    options_.statistics = CreateDBStatistics();
  }

  ~DeltaCompressionTest() { Close(); }

  weak_ptr<BlobStorage> GetBlobStorage(uint32_t cf_id) {
    MutexLock l(mutex_);
    return blob_file_set_->GetBlobStorage(cf_id);
  }

  void ClearDir() {
    vector<string> filenames;
    options_.env->GetChildren(options_.dirname, &filenames);
    for (auto &fname : filenames) {
      if (fname != "." && fname != "..") {
        ASSERT_OK(options_.env->DeleteFile(options_.dirname + "/" + fname));
      }
    }
    options_.env->DeleteDir(options_.dirname);
    filenames.clear();
    options_.env->GetChildren(dbname_, &filenames);
    for (auto &fname : filenames) {
      if (fname != "." && fname != "..") {
        options_.env->DeleteFile(dbname_ + "/" + fname);
      }
    }
  }

  void NewDB() {
    ClearDir();
    Open();
  }

  void Open() {
    ASSERT_OK(TitanDB::Open(options_, dbname_, &db_));
    tdb_ = reinterpret_cast<TitanDBImpl *>(db_);
    blob_file_set_ = tdb_->blob_file_set_.get();
    mutex_ = &tdb_->mutex_;
    base_db_ = reinterpret_cast<DBImpl *>(tdb_->GetRootDB());
  }

  void Flush() {
    FlushOptions fopts;
    fopts.wait = true;
    ASSERT_OK(db_->Flush(fopts));
  }

  void Close() {
    if (!db_)
      return;
    ASSERT_OK(db_->Close());
    delete db_;
    db_ = nullptr;
  }

  struct Statistics {
    uint64_t gc_number = 0;
    uint64_t compressed_number = 0;
    uint64_t gc_files = 0;
    HumanReadable delta_before_size{};
    HumanReadable delta_after_size{};
    string method = "unknown";
    timespec compressed_time{};
    uint64_t fail_number = 0;
    HumanReadable database_before_size{};
    HumanReadable database_after_size{};
    HumanReadable blob_files_size{};
    HumanReadable blob_files_after_size{};

    void AddTimespec(timespec &time, const timespec &addtime) {
      time.tv_sec += addtime.tv_sec;
      time.tv_nsec += addtime.tv_nsec;
      if (time.tv_nsec > 1000000000) {
        time.tv_nsec -= 1000000000;
        time.tv_sec++;
      }
    }

    double TimespecToSeconds(const timespec &time) {
      return time.tv_sec + time.tv_nsec / 1000000000.;
    }

    void Update(const BlobGCJob::Metrics &metrics) {
      gc_number += metrics.gc_num_processed_records;
      gc_files += metrics.gc_num_files;
      delta_before_size.size_ += metrics.gc_before_delta_compressed_size;
      delta_after_size.size_ += metrics.gc_after_delta_compressed_size;
      compressed_number += metrics.gc_delta_compressed_record;
      fail_number += metrics.gc_delta_compressed_failed_number;
      AddTimespec(compressed_time, metrics.gc_delta_compressed_time);
    }

    void GetTypeString(DeltaCompressType type) {
      for (auto &delta_compression_type :
           TitanOptionsHelper::delta_compression_type_string_map) {
        if (delta_compression_type.second == type) {
          method = delta_compression_type.first;
          break;
        }
      }
    }

    void Print() {
      double delta_ratio =
          (double)delta_before_size.size_ / delta_after_size.size_;
      double database_ratio =
          (double)database_before_size.size_ / database_after_size.size_;
      double time = TimespecToSeconds(compressed_time);
      double blob_file_ratio =
          (double)blob_files_size.size_ / blob_files_after_size.size_;
      putchar('\n');
      printf(
          "| method  | compress fail | compress success | delta size | delta "
          "after size | delta compress ratio | compress time |\n");
      printf("| ------- | ------------- | ---------------- | ---------- | "
             "---------------- | -------------------- | ------------- |\n");
      printf("| %s\t | %zu\t | %zu\t | %s\t | %s\t | %.2f\t | "
             "%.2fs\t |\n",
             method.c_str(), fail_number, compressed_number,
             delta_before_size.ToString(false).c_str(),
             delta_after_size.ToString(false).c_str(), delta_ratio, time);
      putchar('\n');
      printf("| method  | database size | database after size | database "
             "compress ratio | blob files size | blob files after size | blob "
             "file compress ratio |\n");
      printf("| ------- | ------------- | ------------------- | "
             "----------------------- | ----------------------- | "
             "----------------------- | ----------------------- |\n");
      printf("| %s\t | %s\t | %s\t | %.2f\t |  %s\t | %s\t | %.2f\t |\n",
             method.c_str(), database_before_size.ToString(false).c_str(),
             database_after_size.ToString(false).c_str(), database_ratio,
             blob_files_size.ToString(false).c_str(),
             blob_files_after_size.ToString(false).c_str(), blob_file_ratio);

      fflush(stdout);
    }
  };

  void PrintProcedure(const size_t gc_files, const size_t number_of_files) {
    double ratio = (double)gc_files / number_of_files;
    printf("\r%.2f%% GC complete", ratio * 100);
  }

  // TODO unifiy this and TitanDBImpl::TEST_StartGC
  void GCAllFiles() {
    Statistics stats;
    stats.database_before_size.size_ = GetDatabaseSize(dbname_);
    bool ok;
    ok = db_->GetIntProperty(TitanDB::Properties::kLiveBlobFileSize,
                             &stats.blob_files_size.size_);
    ASSERT_TRUE(ok);
    cout << "Start garbage collection! \nPlease wait several minutes..."
         << endl;
    MutexLock l(mutex_);
    Status s;
    auto *cfh = base_db_->DefaultColumnFamily();

    // Build BlobGC
    TitanDBOptions db_options = tdb_->db_options_;
    TitanCFOptions cf_options =
        TitanCFOptions(options_, tdb_->cf_info_[0].immutable_cf_options,
                       tdb_->cf_info_[0].mutable_cf_options);
    LogBuffer log_buffer(InfoLogLevel::INFO_LEVEL, db_options.info_log.get());

    size_t number_of_files;
    {
      auto blob_storage =
          blob_file_set_->GetBlobStorage(cfh->GetID()).lock().get();
      blob_storage->SetAllFilesLiveDataTo0();
      blob_storage->ComputeGCScore();
      number_of_files = blob_storage->gc_score().size();
    }

    unique_ptr<BlobGC> blob_gc;
    do {
      {
        shared_ptr<BlobGCPicker> blob_gc_picker =
            make_shared<BasicBlobGCPicker>(db_options, cf_options, nullptr);
        blob_gc = blob_gc_picker->PickBlobGC(
            blob_file_set_->GetBlobStorage(cfh->GetID()).lock().get());
      }

      if (blob_gc) {
        blob_gc->SetColumnFamily(cfh);

        BlobGCJob blob_gc_job(blob_gc.get(), base_db_, tdb_, mutex_,
                              tdb_->db_options_, GetParam(), tdb_->env_,
                              EnvOptions(options_), tdb_->blob_manager_.get(),
                              blob_file_set_, &log_buffer, nullptr, nullptr);

        s = blob_gc_job.Prepare();
        ASSERT_OK(s);

        {
          mutex_->Unlock();
          s = blob_gc_job.Run();
          ASSERT_OK(s);
          mutex_->Lock();
        }

        if (s.ok()) {
          s = blob_gc_job.Finish();
          ASSERT_OK(s);
        }
        blob_gc->ReleaseGcFiles();
        stats.Update(blob_gc_job.metrics_);
      }

      mutex_->Unlock();
      tdb_->PurgeObsoleteFiles();
      mutex_->Lock();
      PrintProcedure(stats.gc_files, number_of_files);
    } while (stats.gc_files < number_of_files);
    // } while (blob_gc != nullptr && blob_gc->trigger_next());
    stats.GetTypeString(options_.blob_file_delta_compression);
    stats.database_after_size.size_ = GetDatabaseSize(dbname_);
    ok = db_->GetIntProperty(TitanDB::Properties::kLiveBlobFileSize,
                             &stats.blob_files_after_size.size_);
    ASSERT_TRUE(ok);
    stats.Print();
  }

  void ExecutePut(const string &key, const string &value) override {
    Status s = db_->Put(WriteOptions(), key, value);
    ASSERT_OK(s);
  }

  void GetSimilarRecords() override {
    const auto &table =
        tdb_->cf_info_[0].immutable_cf_options.feature_index_table;
    max_similar_records_ = table->CountAllSimilarRecords();
  }

  void TestWikipedia() {
    NewDB();
    PutWikipediaData();
    Flush();
    GCAllFiles();
  }

  void TestEnronMail() {
    NewDB();
    PutEnronMailData();
    Flush();
    GCAllFiles();
  }

  void TestStackOverFlow() {
    NewDB();
    PutStackOverFlowData();
    Flush();
    GCAllFiles();
  }

  void TestStackOverFlowComment() {
    NewDB();
    PutStackOverFlowCommentData();
    Flush();
    GCAllFiles();
  }
};

class ResemblenceDetectionTest
    : public testing::TestWithParam<tuple<feature_t, size_t, size_t>>,
      public DataReader {
public:
  ResemblenceDetectionTest()
      : DataReader(100),
        table_(0, get<0>(GetParam()), get<1>(GetParam()), get<2>(GetParam())){};

  void ExecutePut(const string &key, const string &value) override {
    table_.Put(key, value);
  }

  void GetSimilarRecords() override {
    max_similar_records_ = table_.CountAllSimilarRecords();
  }

  void TestWikipedia() { PutWikipediaData(); }

  void TestEnronMail() { PutEnronMailData(); }

  void TestStackOverFlow() { PutStackOverFlowData(); }

  void TestStackOverFlowComment() { PutStackOverFlowCommentData(); }

private:
  FeatureIndexTable table_;
};

TEST_P(DeltaCompressionTest, EnronMail) { TestEnronMail(); }
TEST_P(DeltaCompressionTest, Wikipedia) { TestWikipedia(); }
TEST_P(DeltaCompressionTest, StackOverFlow) { TestStackOverFlow(); }
TEST_P(DeltaCompressionTest, StackOverFlowComment) {
  TestStackOverFlowComment();
}

INSTANTIATE_TEST_CASE_P(DeltaCompressionTestParameterized, DeltaCompressionTest,
                        ::testing::Values(kGDelta, kXDelta, kEDelta));

// INSTANTIATE_TEST_SUITE_P(DeltaCompressionTestParameterized,
// DeltaCompressionTest,
//                         ::testing::Values(kGDelta));

// TEST_P(ResemblenceDetectionTest, Wikipedia) { TestWikipedia(); }
// TEST_P(ResemblenceDetectionTest, EnronMail) { TestEnronMail(); }
// TEST_P(ResemblenceDetectionTest, StackOverFlow) { TestStackOverFlow(); }
// TEST_P(ResemblenceDetectionTest, StackOverFlowComment) {
//   TestStackOverFlowComment();
// }

typedef tuple<feature_t, size_t, size_t> TableParameters;

// INSTANTIATE_TEST_SUITE_P(
//     ResemblenceDetectionTestParameterized, ResemblenceDetectionTest,
//     ::testing::Values(TableParameters(k1_128RatioMask, 12, 3),
//                       TableParameters(k1_4RatioMask, 12, 3)
//                       TableParameters(k1_4RatioMask, 12, 12)
//                       ));

} // namespace titandb
} // namespace rocksdb

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

max_stars_repo_name: apple-ouyang/titan

max_issues_repo_name: apple-ouyang/titan

max_forks_repo_name: apple-ouyang/titan


text:
// Boost.Bimap
//
// Copyright (c) 2006-2007 Matias Capeletto
//
// 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)

//  VC++ 8.0 warns on usage of certain Standard Library and API functions that
//  can be cause buffer overruns or other possible security issues if misused.
//  See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
//  But the wording of the warning is misleading and unsettling, there are no
//  portable alternative functions, and VC++ 8.0's own libraries use the
//  functions in question. So turn off the warnings.
#define _CRT_SECURE_NO_DEPRECATE
#define _SCL_SECURE_NO_DEPRECATE

// Boost.Bimap Example
//-----------------------------------------------------------------------------

#include <boost/config.hpp>

#include <iostream>
#include <boost/tokenizer.hpp>

#include <boost/bimap/bimap.hpp>
#include <boost/bimap/unordered_set_of.hpp>
#include <boost/bimap/list_of.hpp>

using namespace boost::bimaps;

struct counter {
    counter() : c(0) {}
    counter& operator++() { ++c; return *this; }
    unsigned int operator++(int) { return c++; }
    operator const unsigned int() const { return c; }
    private:
    unsigned int c;
};

int main()
{
    //[ code_repetitions_counter

    typedef bimap
    <
        unordered_set_of< std::string >,
        list_of< counter > /*< `counter` is an integer that is initialized
                                in zero in the constructor >*/

    > word_counter;

    typedef boost::tokenizer<boost::char_separator<char> > text_tokenizer;

    std::string text=
        "Relations between data in the STL are represented with maps."
        "A map is a directed relation, by using it you are representing "
        "a mapping. In this directed relation, the first type is related to "
        "the second type but it is not true that the inverse relationship "
        "holds. This is useful in a lot of situations, but there are some "
        "relationships that are bidirectional by nature.";

    // feed the text into the container
    word_counter   wc;
    text_tokenizer tok(text,boost::char_separator<char>(" \t\n.,;:!?'\"-"));

    for( text_tokenizer::const_iterator it = tok.begin(), it_end = tok.end();
         it != it_end ; ++it )
    {
        /*<< Because the right collection type is `list_of`, the right data
             is not used a key and can be modified in the same way as with
             standard maps. >>*/
        ++ wc.left[*it];
    }

    // list words with counters by order of appearance
    /*<< When we insert the elements using the left map view, the element
         is inserted at the end of the list. >>*/
    for( word_counter::right_const_iterator
            wit = wc.right.begin(), wit_end = wc.right.end();

         wit != wit_end; ++wit )
    {
        std::cout << wit->second << ": " << wit->first;
    }
    //]

    return 0;
}

max_issues_repo_name: qingkouwei/mediaones


text:
#include "InputFileOperator.h"
#include "CommandLineHandlers/SingleCommandLineHandler.h"
#include <boost/regex.hpp>
#include "NumberOperator.h"
#include <boost/algorithm/string/trim.hpp>

USING_NAMESPACE(std);
USING_NAMESPACE(clu);

class KeyMerger : public InputFileOperator
{
public:
	KeyMerger(const string& dataDelimiter)
		: m_KeyDelimiter(','), m_DataDelimiter(dataDelimiter) {}
	virtual bool OnLineRead(string& input)
	{
		size_t delimPosition = input.find(m_KeyDelimiter);
		if (delimPosition != string::npos)
		{
			string key = input.substr(0, delimPosition);
			string value = input.substr(delimPosition + 1);
			if (m_Keys.find(key) != m_Keys.end())
			{
				m_Keys[key] = m_Keys[key] + m_DataDelimiter + value;
			}
			else
			{
				m_Keys[key] = value;
				m_OrderKeys.push_back(key);
			}
		}
		return true;
	}

	virtual void OnEnd()
	{
		for (list<string>::iterator iter = m_OrderKeys.begin();
		iter != m_OrderKeys.end();
			++iter)
		{
			m_OutputHandler->Output("[");
			m_OutputHandler->Output((*iter));
			m_OutputHandler->Output("] : [");
			m_OutputHandler->Output(m_Keys[(*iter)]);
			m_OutputHandler->Output("]\n");
		}

		m_Keys.clear();
		m_OrderKeys.clear();
	}

protected:
	map<string, string> m_Keys;
	list<string> m_OrderKeys;
	char m_KeyDelimiter;
	string m_DataDelimiter;
};

REGISTER_SINGLE_STRING("keymerge", KeyMerger)->SetHelp("Merge lines by the first element as key");

max_stars_repo_name: ShaiRoitman/clu

max_issues_repo_name: ShaiRoitman/clu

max_forks_repo_name: ShaiRoitman/clu


text:
//            Copyright Daniel Trebbien 2010.
// Distributed under the Boost Software License, Version 1.0.
//   (See accompanying file LICENSE_1_0.txt or the copy at
//         http://www.boost.org/LICENSE_1_0.txt)

#include <cassert>
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/one_bit_color_map.hpp>
#include <boost/graph/stoer_wagner_min_cut.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/typeof/typeof.hpp>

struct edge_t
{
  unsigned long first;
  unsigned long second;
};

// A graphic of the min-cut is available at <http://www.boost.org/doc/libs/release/libs/graph/doc/stoer_wagner_imgs/stoer_wagner.cpp.gif>
int main()
{
  using namespace std;

  typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
    boost::no_property, boost::property<boost::edge_weight_t, int> > undirected_graph;
  typedef boost::property_map<undirected_graph, boost::edge_weight_t>::type weight_map_type;
  typedef boost::property_traits<weight_map_type>::value_type weight_type;

  // define the 16 edges of the graph. {3, 4} means an undirected edge between vertices 3 and 4.
  edge_t edges[] = {{3, 4}, {3, 6}, {3, 5}, {0, 4}, {0, 1}, {0, 6}, {0, 7},
    {0, 5}, {0, 2}, {4, 1}, {1, 6}, {1, 5}, {6, 7}, {7, 5}, {5, 2}, {3, 4}};

  // for each of the 16 edges, define the associated edge weight. ws[i] is the weight for the edge
  // that is described by edges[i].
  weight_type ws[] = {0, 3, 1, 3, 1, 2, 6, 1, 8, 1, 1, 80, 2, 1, 1, 4};

  // construct the graph object. 8 is the number of vertices, which are numbered from 0
  // through 7, and 16 is the number of edges.
  undirected_graph g(edges, edges + 16, ws, 8, 16);

  // define a property map, `parities`, that will store a boolean value for each vertex.
  // Vertices that have the same parity after `stoer_wagner_min_cut` runs are on the same side of the min-cut.
  BOOST_AUTO(parities, boost::make_one_bit_color_map(num_vertices(g), get(boost::vertex_index, g)));

  // run the Stoer-Wagner algorithm to obtain the min-cut weight. `parities` is also filled in.
  int w = boost::stoer_wagner_min_cut(g, get(boost::edge_weight, g), boost::parity_map(parities));

  cout << "The min-cut weight of G is " << w << ".\n" << endl;
  assert(w == 7);

  cout << "One set of vertices consists of:" << endl;
  size_t i;
  for (i = 0; i < num_vertices(g); ++i) {
    if (get(parities, i))
      cout << i << endl;
  }
  cout << endl;

  cout << "The other set of vertices consists of:" << endl;
  for (i = 0; i < num_vertices(g); ++i) {
    if (!get(parities, i))
      cout << i << endl;
  }
  cout << endl;

  return EXIT_SUCCESS;
}


text:
/* ----------------------------------------------------------------------------
 * Copyright 2018, Ross Hartley <m.ross.hartley@gmail.com>
 * All Rights Reserved
 * See LICENSE for the license information
 * -------------------------------------------------------------------------- */

/**
 *  @file   kinematics.cpp
 *  @author Ross Hartley
 *  @brief  Example of invariant filtering for contact-aided inertial navigation
 *  @date   September 25, 2018
 **/

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <Eigen/Dense>
#include <boost/algorithm/string.hpp>
#include <vector>
#include "InEKF.h"

#define DT_MIN 1e-6
#define DT_MAX 1

using namespace std;
using namespace inekf;

double stod98(const std::string &s) {
    return atof(s.c_str());
}

int stoi98(const std::string &s) {
    return atoi(s.c_str());
}

int main() {
    //  ---- Initialize invariant extended Kalman filter ----- //
    RobotState initial_state;

    // Initialize state mean
    Eigen::Matrix3d R0;
    Eigen::Vector3d v0, p0, bg0, ba0;
    R0 << 1, 0, 0, // initial orientation
          0, -1, 0, // IMU frame is rotated 90deg about the x-axis
          0, 0, -1;
    v0 << 0,0,0; // initial velocity
    p0 << 0,0,0; // initial position
    bg0 << 0,0,0; // initial gyroscope bias
    ba0 << 0,0,0; // initial accelerometer bias
    initial_state.setRotation(R0);
    initial_state.setVelocity(v0);
    initial_state.setPosition(p0);
    initial_state.setGyroscopeBias(bg0);
    initial_state.setAccelerometerBias(ba0);

    // Initialize state covariance
    NoiseParams noise_params;
    noise_params.setGyroscopeNoise(0.01);
    noise_params.setAccelerometerNoise(0.1);
    noise_params.setGyroscopeBiasNoise(0.00001);
    noise_params.setAccelerometerBiasNoise(0.0001);
    noise_params.setContactNoise(0.01);

    // Initialize filter
    InEKF filter(initial_state, noise_params);
    cout << "Noise parameters are initialized to: \n";
    cout << filter.getNoiseParams() << endl;
    cout << "Robot's state is initialized to: \n";
    cout << filter.getState() << endl;

    // Open data file
    ifstream infile("../src/data/imu_kinematic_measurements.txt");
    string line;
    Eigen::Matrix<double,6,1> imu_measurement = Eigen::Matrix<double,6,1>::Zero();
    Eigen::Matrix<double,6,1> imu_measurement_prev = Eigen::Matrix<double,6,1>::Zero();
    double t = 0;
    double t_prev = 0;

    // ---- Loop through data file and read in measurements line by line ---- //
    while (getline(infile, line)){
        vector<string> measurement;
        boost::split(measurement,line,boost::is_any_of(" "));
        // // Handle measurements
        if (measurement[0].compare("IMU")==0){
            cout << "Received IMU Data, propagating state\n";
            assert((measurement.size()-2) == 6);
            t = atof(measurement[1].c_str());
            // Read in IMU data
            imu_measurement << stod98(measurement[2]),
                               stod98(measurement[3]),
                               stod98(measurement[4]),
                               stod98(measurement[5]),
                               stod98(measurement[6]),
                               stod98(measurement[7]);

            // Propagate using IMU data
            double dt = t - t_prev;
            if (dt > DT_MIN && dt < DT_MAX) {
                filter.Propagate(imu_measurement_prev, dt);
            }

        }
        else if (measurement[0].compare("CONTACT")==0){
            cout << "Received CONTACT Data, setting filter's contact state\n";
            assert((measurement.size()-2)%2 == 0);
            vector<pair<int,bool> > contacts;
            int id;
            bool indicator;
            t = stod98(measurement[1]);
            // Read in contact data
            for (int i=2; i<measurement.size(); i+=2) {
                id = stoi98(measurement[i]);
                indicator = bool(stod98(measurement[i+1]));
                contacts.push_back(pair<int,bool> (id, indicator));
            }
            // Set filter's contact state
            filter.setContacts(contacts);
        }
        else if (measurement[0].compare("KINEMATIC")==0){
            cout << "Received KINEMATIC observation, correcting state\n";
            assert((measurement.size()-2)%44 == 0);
            int id;
            Eigen::Quaternion<double> q;
            Eigen::Vector3d p;
            Eigen::Matrix4d pose = Eigen::Matrix4d::Identity();
            Eigen::Matrix<double,6,6> covariance;
            vectorKinematics measured_kinematics;
            t = stod98(measurement[1]);
            // Read in kinematic data
            for (int i=2; i<measurement.size(); i+=44) {
                id = stoi98(measurement[i]);
                q = Eigen::Quaternion<double> (stod98(measurement[i+1]),stod98(measurement[i+2]),stod98(measurement[i+3]),stod98(measurement[i+4]));
                q.normalize();
                p << stod98(measurement[i+5]),stod98(measurement[i+6]),stod98(measurement[i+7]);
                pose.block<3,3>(0,0) = q.toRotationMatrix();
                pose.block<3,1>(0,3) = p;
                for (int j=0; j<6; ++j) {
                    for (int k=0; k<6; ++k) {
                        covariance(j,k) = stod98(measurement[i+8 + j*6+k]);
                    }
                }
                Kinematics frame(id, pose, covariance);
                measured_kinematics.push_back(frame);
            }
            // Correct state using kinematic measurements
            filter.CorrectKinematics(measured_kinematics);
        }

        // Store previous timestamp
        t_prev = t;
        imu_measurement_prev = imu_measurement;
    }

    // Print final state
    cout << filter.getState() << endl;
    return 0;
}


text:
// Copyright (c) 2011-2016 The Cryptonote developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "gtest/gtest.h"

#include <boost/lexical_cast.hpp>

#include "Serialization/KVBinaryInputStreamSerializer.h"
#include "Serialization/KVBinaryOutputStreamSerializer.h"
#include "Serialization/SerializationOverloads.h"
#include "Serialization/SerializationTools.h"

#include <array>

using namespace CryptoNote;

namespace CryptoNote {

struct TestElement {
  std::string name;
  uint32_t nonce;
  std::array<uint8_t, 16> blob;
  std::vector<uint32_t> u32array;

  bool operator == (const TestElement& other) const {
    return
      name == other.name &&
      nonce == other.nonce &&
      blob == other.blob &&
      u32array == other.u32array;
  }

  void serialize(ISerializer& s) {
    s(name, "name");
    s(nonce, "nonce");
    s.binary(blob.data(), blob.size(), "blob");
    serializeAsBinary(u32array, "u32array", s);
  }
};

struct TestStruct {
  uint8_t u8;
  uint32_t u32;
  uint64_t u64;
  std::vector<TestElement> vec1;
  std::vector<TestElement> vec2;
  TestElement root;

  bool operator == (const TestStruct& other) const {
    return
      root == other.root &&
      u8 == other.u8 &&
      u32 == other.u32 &&
      u64 == other.u64 &&
      vec1 == other.vec1 &&
      vec2 == other.vec2;
  }

  void serialize(ISerializer& s) {
    s(root, "root");
    s(vec1, "vec1");
    s(vec2, "vec2");
    s(u8, "u8");
    s(u32, "u32");
    s(u64, "u64");
  }

};

}

#include <chrono>

typedef std::chrono::high_resolution_clock hclock;

class HiResTimer {
public:
  HiResTimer() :
    start(hclock::now()) {}

  std::chrono::duration<double> duration() {
    return hclock::now() - start;
  }

private:
  hclock::time_point start;
};

TEST(KVSerialize, Simple) {
  TestElement testData1, testData2;

  testData1.name = "hello";
  testData1.nonce = 12345;
  testData1.u32array.resize(128);

  testData2.name = "bye";
  testData2.nonce = 54321;

  std::string buf = CryptoNote::storeToBinaryKeyValue(testData1);
  ASSERT_TRUE(CryptoNote::loadFromBinaryKeyValue(testData2, buf));
  EXPECT_EQ(testData1, testData2);
}

TEST(KVSerialize, BigCollection) {
  TestStruct ts1;

  ts1.u8 = 100;
  ts1.u32 = 0xff0000;
  ts1.u64 = 1ULL << 60;
  ts1.root.name = "hello";

  TestElement sample;
  sample.nonce = 101;
  ts1.vec1.resize(0x10000 >> 2, sample);

  TestStruct ts2;

  std::string buf = CryptoNote::storeToBinaryKeyValue(ts1);
  ASSERT_TRUE(CryptoNote::loadFromBinaryKeyValue(ts2, buf));
  EXPECT_EQ(ts1, ts2);
}

max_stars_repo_name: AmericanCoinAMC/core

max_issues_repo_name: AmericanCoinAMC/core

max_forks_repo_name: AmericanCoinAMC/core


text:
#include "AdditionExpression.h"
#include "exceptions/IllegalAdditionOperationException.h"
#include "ParenthesesExpression.h"
#include <boost/format.hpp>

using namespace thewizardplusplus::wizard_basic_2::compiler::translator;
using namespace thewizardplusplus::wizard_basic_2::compiler::translator::
	exceptions;
using namespace boost;

AdditionExpression::AdditionExpression(Expression::Pointer operand1,
	AdditionType::Types addition_type, Expression::Pointer operand2)
:
	Expression(ValueType::NUMBER),
	operand1(operand1),
	addition_type(addition_type),
	operand2(operand2)
{
	if (operand1->getType() != operand2->getType() || operand1->getType() !=
		ValueType::NUMBER)
	{
		throw IllegalAdditionOperationException(addition_type, operand1->
			getType(), operand2->getType());
	}

	this->operand1 = Expression::Pointer(new ParenthesesExpression(operand1));
	this->operand2 = Expression::Pointer(new ParenthesesExpression(operand2));
}

std::string AdditionExpression::getCppCode(void) const {
	return (format("%1% %2% %3%") % operand1->getCppCode() % AdditionType::
		convertToCppCode(addition_type) % operand2->getCppCode()).str();
}