text:
/**
 * @brief Partial Least Squares Regression with Eigen
 *
 * Same source as LDA, \cite friedman2001elements
 *
 * @file pls-eigen.hpp
 * @author François-David Collin <Francois-David.Collin@umontpellier.fr>
 * @brief
 * @version 0.1
 * @date 2018-11-08
 *
 * @copyright Copyright (c) 2018
 *
 */
#pragma once

#include "various.hpp"
#include <Eigen/Dense>
#include <list>
#include <algorithm>
#include <range/v3/all.hpp>

using namespace Eigen;
using namespace std;
using namespace ranges;

template<class Derived, class OtherDerived>
VectorXd pls(const MatrixBase<Derived>& x,
         const MatrixBase<OtherDerived>& y,
         size_t ncomp,
         MatrixXd& Projection,
         RowVectorXd& mean,
         RowVectorXd& std,
         bool stopping = false)
{
    auto n = x.rows();
    auto p = x.cols();
    mean = x.colwise().mean();
    std = ((x.rowwise() - mean).array().square().colwise().sum() / (x.rows() - 1)).sqrt();;
    MatrixXd X = (x.rowwise() - mean).array().rowwise() / std.array();
    MatrixXd Xo = X;
    MatrixXd XX = X.transpose() * X;
    MatrixXd Y = MatrixXd::Zero(n, ncomp + 1);
    MatrixXd P(p,ncomp);
    MatrixXd R(p,ncomp);
    VectorXd XY(n);
    VectorXd w(p);
    VectorXd r(p);
    VectorXd t(p);
    VectorXd res(ncomp);
    size_t window_size = std::max(2_z,ncomp/10_z);

    std::list<unsigned char> stopping_criterium(window_size,0);
    // Y.col(0) = y.rowwise() - y.colwise().mean();
    double ymean = y.mean();
    Y.col(0).array() = ymean;
    double SSTO = (y.array() - ymean).array().square().sum();
    int m = 0;
    // for (auto m = 0; m < ncomp; m++)
    while (m < ncomp)
    {
        XY = X.transpose() * y;
        SelfAdjointEigenSolver<MatrixXd> es( XY.transpose() * XY );
        auto abs_eigenvalues = es.eigenvalues().array().abs();
        size_t max_eigenvalue_indice = ranges::distance(abs_eigenvalues.begin(),ranges::max_element(abs_eigenvalues));
        auto q = es.eigenvectors().col(max_eigenvalue_indice);
        w = XY * q;
        w /= sqrt((w.transpose()*w)(0,0));
        r=w;
        for (auto j=0; j<=m-1;j++)
        {
            r -= (P.col(j).transpose()*w)(0,0)*R.col(j);
        }
        R.col(m) = r;
        t = Xo * r;
        P.col(m) = XX *r/(t.transpose()*t)(0,0);
        VectorXd Zm = X * XY;
        double Znorm = Zm.array().square().sum();
        double Thetam = Zm.dot(y) / Znorm;
        Y.col(m + 1) = Y.col(m) + Thetam * Zm;
        X -= Zm.rowwise().replicate(p) * ((Zm/Znorm).transpose() * X).asDiagonal();
        res(m) = (Y.col(m + 1).array() - ymean).array().square().sum() / SSTO;
        if ((m >= 2) && stopping) {
            auto lastdiff = res(m) - res(m-1);
            auto lastmean = (res(m) + res(m-1))/2.0;
            size_t remains = ncomp - m;
            stopping_criterium.pop_front();
            stopping_criterium.push_back(lastmean >= 0.99 * remains * lastdiff);
            auto wcrit = ranges::accumulate(stopping_criterium,0);
            if (wcrit == window_size) break;
        }
        m++;
    }
    if (m < ncomp) {
        m--;
        res = res(seq(0,m)).eval();
    }
    Projection = R;
//    VectorXd res = (Y.block(0,1,n,ncomp).array() - ymean).array().square().colwise().sum() / SSTO;
    return res;
}

ext: hpp

max_stars_repo_name: vitorpavinato/abcranger

max_issues_repo_name: vitorpavinato/abcranger

max_forks_repo_name: vitorpavinato/abcranger


text:
#include <consolepp/console.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <cstdlib>

int main()
{
    using namespace consolepp::literals;

    boost::asio::io_context ctx;

    try
    {
        consolepp::console csl(ctx);

        auto const console_size = csl.size();
        std::cout << "console size is "
                  << console_size.width
                  << "x"
                  << console_size.height
                  << "\n";
    }
    catch (consolepp::invalid_console const &)
    {
        std::cout << "Invalid console\n";
    }

    return EXIT_SUCCESS;
}

ext: cpp

max_stars_repo_name: KazDragon/consolepp

max_issues_repo_name: KazDragon/consolepp

max_forks_repo_name: KazDragon/consolepp


text:
#include <boost/ut.hpp>// single header
// import boost.ut;        // single module (C++20)
#include "open_viii/compression/L4Z.hpp"
#include "open_viii/compression/LZSS.hpp"
#include "tl/random.hpp"
#include "tl/utility.hpp"
int
  main()
{
  using namespace boost::ut::literals;
  using namespace boost::ut::operators::terse;
  using namespace boost::ut;
  static constexpr tl::utility::sequence<1U, 100U> run{};
  [[maybe_unused]] suite                           tests = [] {
    "lzss compress and uncompress"_test = []() {
      const auto run_once = [&]<size_t i>() {
        const auto                  random_char = tl::random::iota<char, i>();
        [[maybe_unused]] const auto compressed_random_char =
          open_viii::compression::LZSS::compress(random_char);
        [[maybe_unused]] const auto decompressed_random_char =
          open_viii::compression::LZSS::decompress(compressed_random_char);
        expect(std::ranges::equal(random_char, decompressed_random_char));
      };
      run(run_once);
    };
    "lz4 compress and uncompress"_test = []() {
      const auto run_once = [&]<size_t i2>() {
        const auto                  random_char = tl::random::iota<char, i2>();
        [[maybe_unused]] const auto compressed_random_char =
          open_viii::compression::l4z::compress(random_char);
        [[maybe_unused]] const auto decompressed_random_char =
          open_viii::compression::l4z::decompress(compressed_random_char, i2);
        expect(std::ranges::equal(random_char, decompressed_random_char));
      };
      run(run_once);
    };
  };
}

max_stars_repo_name: Sebanisu/VIIIcppTest

max_issues_repo_name: Sebanisu/VIIIcppTest

max_forks_repo_name: Sebanisu/VIIIcppTest


text:
/*
// Copyright (c) 2000-2009, Texas Engineering Experiment Station (TEES), a
// component of the Texas A&M University System.

// All rights reserved.

// The information and source code contained herein is the exclusive
// property of TEES and may not be disclosed, examined or reproduced
// in whole or in part without explicit written authorization from TEES.
*/

#include <stapl/containers/array/array.hpp>
#include <stapl/algorithms/algorithm.hpp>
#include <stapl/views/array_view.hpp>
#include <map>
#include <boost/unordered_set.hpp>
#include "../../test_report.hpp"

using namespace stapl;

template<typename T>
struct state
{
  typedef std::map<size_t, T> request_type;
  request_type requests;

  void update_value_from_location(size_t const& p, T const& v)
  {
#ifdef DEBUG
    std::cout << "loc: " << get_location_id() << " | from " << p
              <<  " | last value: " << requests[p] << " | new value: "
              << v << std::endl;
#endif
    // check if it's the next value i'm expecting
    if (requests[p] != v - 1 && v != 0)
      stapl_assert(false, "request out of order");

    // special case for 0
    if (v == 0 && requests[p] != 0)
      stapl_assert(false, "zeroth request out of order");

    // set the value
    requests[p] = v;
  }

  void define_type(typer& t)
  {
    t.member(requests);
  }
};

namespace stapl {

template<typename T, typename Accessor>
struct proxy<state<T>, Accessor>
  : private Accessor
{
  friend class proxy_core_access;
  typedef state<T> target_t;

  typedef typename target_t::request_type request_type;

public:
  explicit proxy(Accessor acc)
    : Accessor(acc)
  { }

  void update_value_from_location(size_t const& p, T const& v)
  {
    Accessor::invoke(&target_t::update_value_from_location, p, v);
  }
};

} // namespace stapl

template<typename T>
struct setter
{
  T m_value;
  size_t m_gid;

  typedef void result_type;

  setter(T const& t, size_t gid) : m_value(t), m_gid(gid) { }

  template<typename U>
  void operator()(U& u) const
  {
    u.update_value_from_location(m_value.first, m_value.second);
  }

  template<typename A>
  void operator()(const proxy<T, A>& u) const
  {
#ifdef DEBUG
    std::cout << "gid: " << m_gid << " | ";
#endif
    u.update_value_from_location(m_value.first, m_value.second);
  }

  void define_type(typer& t)
  {
    t.member(m_value);
    t.member(m_gid);
  }
};

template<typename G, typename T>
std::vector<std::pair<G, T> >
generate_requests(size_t const& n, size_t const& values, size_t const& round,
                  boost::unordered_set<size_t> const& locs,
                  boost::unordered_map<G, T>& running_requests)
{
  std::vector<std::pair<G, T> > requests;

  // if this location is participating this round
  if (locs.find(get_location_id()) != locs.end())
  {
    typedef indexed_domain<size_t>          domain_type;
    typedef balanced_partition<domain_type> partition_type;

    partition_type p(domain_type(0, n-1), get_num_locations());
    domain_type d = p[(get_location_id() + round) % get_num_locations()];

    // generate values requests for each gid
    for (size_t i = d.first(); i <= d.last(); ++i)
      for (size_t j = 0; j < values; j++)
        requests.push_back(std::make_pair(i, j));

    // shuffle the order of messages for a single gid
    std::random_shuffle(requests.begin(), requests.end());

    // make sure that the values for a single gid are in sorted order
    for (typename
           std::vector<std::pair<size_t, T> >::iterator it = requests.begin();
         it != requests.end(); ++it)
      (*it).second = running_requests[(*it).first]++;

#ifdef DEBUG
    for (typename
           std::vector<std::pair<G, T> >::iterator it = requests.begin();
         it != requests.end(); ++it)
      std::cout << get_location_id() << ": (" << (*it).first << " -> "
                << (*it).second << ")" << std::endl;
#endif
  }

#ifdef DEBUG
  if (requests.empty())
    std::cout << get_location_id() << ": sending nothing" << std::endl;
#endif

  return requests;
}

boost::unordered_set<size_t> participating_locations(size_t const& locs,
                                                     size_t const& round)
{
  boost::unordered_set<size_t> s;

  for (size_t i = 0; i < locs; ++i)
    s.insert((i + round) % get_num_locations());

#ifdef DEBUG
  std::cout << "round " << round << " locs: ";
  for (boost::unordered_set<size_t>::iterator it = s.begin();
       it != s.end(); ++it)
    std::cout << *it << " ";
  std::cout << std::endl;
#endif

  return s;
}

stapl::exit_code stapl_main(int argc, char* argv[])
{
  if (argc < 5) {
    std::cerr << "usage: exe n participating_locations total_rounds num_values"
              << std::endl;
    exit(1);
  }

  const size_t n = atoi(argv[1]);
  size_t l = atoi(argv[2]);
  const size_t r = atoi(argv[3]);
  const size_t v = atoi(argv[4]);

  if (l > get_num_locations())
    l = get_num_locations();

  typedef int        value_type;
  typedef state<int> state_type;
  array<state_type> c(n);

  boost::unordered_map<size_t, value_type> next_requests;

  // for r rounds
  for (size_t i = 0; i < r; ++i)
  {
    // generate a set of participating locations for the ith round
    boost::unordered_set<size_t> locs = participating_locations(l, i);

    // generate a local schedule of requests for the ith round
    std::vector<std::pair<size_t, value_type> > requests =
      generate_requests<size_t, value_type>(n, v, i, locs, next_requests);

    // go through the schedule and perform the operations one at a time
    for (std::vector<std::pair<size_t, value_type> >::iterator it =
           requests.begin();
         it != requests.end(); ++it)
      c.apply_set((*it).first,
                  setter<std::pair<size_t, value_type> >(
                    std::make_pair(get_location_id(), (*it).second),
                    (*it).first));
  }

  STAPL_TEST_REPORT(true, "Testing container ordering");

  return EXIT_SUCCESS;
}

ext: cc


text:
#include <bacs/problem/utility.hpp>

#include <boost/optional.hpp>

#include <string>
#include <vector>

namespace bacs::problem::utilities {

class dot_net_solution : public utility {
 public:
  dot_net_solution(const boost::filesystem::path &location,
                   const boost::property_tree::ptree &config);

  bool make_package(const boost::filesystem::path &destination,
                    const bunsan::pm::entry &package,
                    const Revision &revision) const override;

 private:
  const boost::filesystem::path m_solution;
  const boost::optional<std::string> m_configuration;
  const std::vector<std::string> m_libs;
};

}  // namespace bacs::problem::utilities

max_stars_repo_name: bacsorg/bacs

max_issues_repo_name: bacsorg/bacs

max_forks_repo_name: bacsorg/bacs


text:
/*
Copyright 2018 Dennis Rohde

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#pragma once

#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>

#include <boost/tokenizer.hpp>

#include "relation.hpp"

template <typename T = double>
Relation<T> read_csv(std::string filename, const unsigned long d) {
    std::ifstream in(filename.c_str());
    Relation<T> result;
    std::vector<double> vec;
    std::string line;
    unsigned long i = 1;

    while (std::getline(in, line)) {
        if (line[0] == '#' or line[0] == ' ' or line[0] == '/') continue;
        vec.clear();
        boost::tokenizer<boost::escaped_list_separator<char>> tok(line);
        for (const auto &token: tok) {
            try {
                vec.push_back(std::stof(token));
            } catch (std::invalid_argument &e) {
                std::cerr << e.what() << std::endl;
            }
        }
        try {
            result.push_back(Tuple<T>(vec, d));
        } catch (std::invalid_argument) {
            std::cerr << "line " << i << " does not contain enough data!" << std::endl;
        }
        ++i;
    }
    std::cout << "Imported " << result.size() << " " << d << "-tuples." << std::endl;
    return result;
}


text:
// Boost.Geometry (aka GGL, Generic Geometry Library)

// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.

// Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
// (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.

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

#ifndef BOOST_GEOMETRY_STRATEGIES_AGNOSTIC_CONVEX_GRAHAM_ANDREW_HPP
#define BOOST_GEOMETRY_STRATEGIES_AGNOSTIC_CONVEX_GRAHAM_ANDREW_HPP

#include <cstddef>
#include <algorithm>
#include <vector>

#include <boost/range.hpp>

#include <boost/geometry/core/cs.hpp>
#include <boost/geometry/core/point_type.hpp>
#include <boost/geometry/strategies/convex_hull.hpp>

#include <boost/geometry/views/detail/range_type.hpp>

#include <boost/geometry/policies/compare.hpp>

#include <boost/geometry/algorithms/detail/for_each_range.hpp>
#include <boost/geometry/views/reversible_view.hpp>

namespace boost { namespace geometry
{

namespace strategy { namespace convex_hull
{

#ifndef DOXYGEN_NO_DETAIL
namespace detail
{

template
<
    typename InputRange,
    typename RangeIterator,
    typename StrategyLess,
    typename StrategyGreater
>
struct get_extremes
{
    typedef typename point_type<InputRange>::type point_type;

    point_type left, right;

    bool first;

    StrategyLess less;
    StrategyGreater greater;

    inline get_extremes()
        : first(true)
    {}

    inline void apply(InputRange const& range)
    {
        if (boost::size(range) == 0)
        {
            return;
        }

        // First iterate through this range
        // (this two-stage approach avoids many point copies,
        //  because iterators are kept in memory. Because iterators are
        //  not persistent (in MSVC) this approach is not applicable
        //  for more ranges together)

        RangeIterator left_it = boost::begin(range);
        RangeIterator right_it = boost::begin(range);

        for (RangeIterator it = boost::begin(range) + 1;
            it != boost::end(range);
            ++it)
        {
            if (less(*it, *left_it))
            {
                left_it = it;
            }

            if (greater(*it, *right_it))
            {
                right_it = it;
            }
        }

        // Then compare with earlier
        if (first)
        {
            // First time, assign left/right
            left = *left_it;
            right = *right_it;
            first = false;
        }
        else
        {
            // Next time, check if this range was left/right from
            // the extremes already collected
            if (less(*left_it, left))
            {
                left = *left_it;
            }

            if (greater(*right_it, right))
            {
                right = *right_it;
            }
        }
    }
};

template
<
    typename InputRange,
    typename RangeIterator,
    typename Container,
    typename SideStrategy
>
struct assign_range
{
    Container lower_points, upper_points;

    typedef typename point_type<InputRange>::type point_type;

    point_type const& most_left;
    point_type const& most_right;

    inline assign_range(point_type const& left, point_type const& right)
        : most_left(left)
        , most_right(right)
    {}

    inline void apply(InputRange const& range)
    {
        typedef SideStrategy side;

        // Put points in one of the two output sequences
        for (RangeIterator it = boost::begin(range);
            it != boost::end(range);
            ++it)
        {
            // check if it is lying most_left or most_right from the line

            int dir = side::apply(most_left, most_right, *it);
            switch(dir)
            {
                case 1 : // left side
                    upper_points.push_back(*it);
                    break;
                case -1 : // right side
                    lower_points.push_back(*it);
                    break;

                // 0: on line most_left-most_right,
                //    or most_left, or most_right,
                //    -> all never part of hull
            }
        }
    }
};

template <typename Range>
static inline void sort(Range& range)
{
    typedef typename boost::range_value<Range>::type point_type;
    typedef geometry::less<point_type> comparator;

    std::sort(boost::begin(range), boost::end(range), comparator());
}

} // namespace detail
#endif // DOXYGEN_NO_DETAIL

/*!
\brief Graham scan strategy to calculate convex hull
\ingroup strategies
\note Completely reworked version inspired on the sources listed below
\see http://www.ddj.com/architect/201806315
\see http://marknelson.us/2007/08/22/convex
 */
template <typename InputGeometry, typename OutputPoint>
class graham_andrew
{
public :
    typedef OutputPoint point_type;
    typedef InputGeometry geometry_type;

private:

    typedef typename cs_tag<point_type>::type cs_tag;

    typedef typename std::vector<point_type> container_type;
    typedef typename std::vector<point_type>::const_iterator iterator;
    typedef typename std::vector<point_type>::const_reverse_iterator rev_iterator;

    class partitions
    {
        friend class graham_andrew;

        container_type m_lower_hull;
        container_type m_upper_hull;
        container_type m_copied_input;
    };

public:
    typedef partitions state_type;

    inline void apply(InputGeometry const& geometry, partitions& state) const
    {
        // First pass.
        // Get min/max (in most cases left / right) points
        // This makes use of the geometry::less/greater predicates

        // For the left boundary it is important that multiple points
        // are sorted from bottom to top. Therefore the less predicate
        // does not take the x-only template parameter (this fixes ticket #6019.
        // For the right boundary it is not necessary (though also not harmful),
        // because points are sorted from bottom to top in a later stage.
        // For symmetry and to get often more balanced lower/upper halves
        // we keep it.

        typedef typename geometry::detail::range_type<InputGeometry>::type range_type;

        typedef typename boost::range_iterator
            <
                range_type const
            >::type range_iterator;

        detail::get_extremes
            <
                range_type,
                range_iterator,
                geometry::less<point_type>,
                geometry::greater<point_type>
            > extremes;
        geometry::detail::for_each_range(geometry, extremes);

        // Bounding left/right points
        // Second pass, now that extremes are found, assign all points
        // in either lower, either upper
        detail::assign_range
            <
                range_type,
                range_iterator,
                container_type,
                typename strategy::side::services::default_strategy<cs_tag>::type
            > assigner(extremes.left, extremes.right);

        geometry::detail::for_each_range(geometry, assigner);

        // Sort both collections, first on x(, then on y)
        detail::sort(assigner.lower_points);
        detail::sort(assigner.upper_points);

        //std::cout << boost::size(assigner.lower_points) << std::endl;
        //std::cout << boost::size(assigner.upper_points) << std::endl;

        // And decide which point should be in the final hull
        build_half_hull<-1>(assigner.lower_points, state.m_lower_hull,
                extremes.left, extremes.right);
        build_half_hull<1>(assigner.upper_points, state.m_upper_hull,
                extremes.left, extremes.right);
    }

    template <typename OutputIterator>
    inline void result(partitions const& state,
                    OutputIterator out, bool clockwise)  const
    {
        if (clockwise)
        {
            output_range<iterate_forward>(state.m_upper_hull, out, false);
            output_range<iterate_reverse>(state.m_lower_hull, out, true);
        }
        else
        {
            output_range<iterate_forward>(state.m_lower_hull, out, false);
            output_range<iterate_reverse>(state.m_upper_hull, out, true);
        }
    }

private:

    template <int Factor>
    static inline void build_half_hull(container_type const& input,
            container_type& output,
            point_type const& left, point_type const& right)
    {
        output.push_back(left);
        for(iterator it = input.begin(); it != input.end(); ++it)
        {
            add_to_hull<Factor>(*it, output);
        }
        add_to_hull<Factor>(right, output);
    }

    template <int Factor>
    static inline void add_to_hull(point_type const& p, container_type& output)
    {
        typedef typename strategy::side::services::default_strategy<cs_tag>::type side;

        output.push_back(p);
        std::size_t output_size = output.size();
        while (output_size >= 3)
        {
            rev_iterator rit = output.rbegin();
            point_type const last = *rit++;
            point_type const& last2 = *rit++;

            if (Factor * side::apply(*rit, last, last2) <= 0)
            {
                // Remove last two points from stack, and add last again
                // This is much faster then erasing the one but last.
                output.pop_back();
                output.pop_back();
                output.push_back(last);
                output_size--;
            }
            else
            {
                return;
            }
        }
    }

    template <iterate_direction Direction, typename OutputIterator>
    static inline void output_range(container_type const& range,
        OutputIterator out, bool skip_first)
    {
        typedef typename reversible_view<container_type const, Direction>::type view_type;
        view_type view(range);
        bool first = true;
        for (typename boost::range_iterator<view_type const>::type it = boost::begin(view);
            it != boost::end(view); ++it)
        {
            if (first && skip_first)
            {
                first = false;
            }
            else
            {
                *out = *it;
                ++out;
            }
        }
    }

};

}} // namespace strategy::convex_hull

#ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
template <typename InputGeometry, typename OutputPoint>
struct strategy_convex_hull<InputGeometry, OutputPoint, cartesian_tag>
{
    typedef strategy::convex_hull::graham_andrew<InputGeometry, OutputPoint> type;
};
#endif

}} // namespace boost::geometry

#endif // BOOST_GEOMETRY_STRATEGIES_AGNOSTIC_CONVEX_GRAHAM_ANDREW_HPP

max_stars_repo_name: cooparation/caffe-android

max_issues_repo_name: cooparation/caffe-android

max_forks_repo_name: cooparation/caffe-android


text:
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2014-2018, The Monero Project
// Copyright (c) 2018, The TurtleCoin Developers
//
// Please see the included LICENSE file for more information.

#include "FormatTools.h"
#include <cstdio>
#include <ctime>
#include <Xi/Config.h>
#include "CryptoNoteCore/Core.h"
#include "Rpc/CoreRpcServerCommandsDefinitions.h"

#include <Xi/ExternalIncludePush.h>
#include <boost/format.hpp>
#include <rang.hpp>
#include <yaml-cpp/yaml.h>
#include <Xi/ExternalIncludePop.h>

namespace Common {

//--------------------------------------------------------------------------------
std::string get_mining_speed(uint32_t hr) {
  if (hr > 1e9)
    return (boost::format("%.2f GH/s") % (hr / 1e9)).str();
  if (hr > 1e6)
    return (boost::format("%.2f MH/s") % (hr / 1e6)).str();
  if (hr > 1e3)
    return (boost::format("%.2f KH/s") % (hr / 1e3)).str();

  return (boost::format("%.0f H/s") % hr).str();
}

//--------------------------------------------------------------------------------
std::string get_sync_percentage(CryptoNote::BlockHeight height, CryptoNote::BlockHeight target_height) {
  /* Don't divide by zero */
  if (height.isNull() || target_height.isNull()) {
    return "0.00";
  }

  /* So we don't have > 100% */
  if (height > target_height) {
    height = target_height;
  }

  float pc = 100.0f * height.native() / target_height.native();

  if (height < target_height && pc > 99.99f) {
    pc = 99.99f;  // to avoid 100% when not fully synced
  }

  return (boost::format("%.2f") % pc).str();
}

enum ForkStatus { UpToDate, ForkLater, ForkSoonReady, ForkSoonNotReady, OutOfDate };

ForkStatus get_fork_status(CryptoNote::BlockHeight height, std::vector<CryptoNote::BlockHeight> upgrade_heights,
                           CryptoNote::BlockHeight supported_height, const CryptoNote::Currency& currency) {
  /* Allow fork heights to be empty */
  if (upgrade_heights.empty()) {
    return UpToDate;
  }

  CryptoNote::BlockHeight next_fork;

  for (auto upgrade : upgrade_heights) {
    /* We have hit an upgrade already that the user cannot support */
    if (height >= upgrade && supported_height < upgrade) {
      return OutOfDate;
    }

    /* Get the next fork height */
    if (upgrade > height) {
      next_fork = upgrade;
      break;
    }
  }

  auto days = (next_fork - height).native() / currency.coin().expectedBlocksPerDay();

  /* Next fork in < 30 days away */
  if (days < 30) {
    /* Software doesn't support the next fork yet */
    if (supported_height < next_fork) {
      return ForkSoonNotReady;
    } else {
      return ForkSoonReady;
    }
  }

  if (height > next_fork) {
    return UpToDate;
  }

  return ForkLater;
}

std::string get_fork_time(CryptoNote::BlockHeight height, const std::vector<CryptoNote::BlockHeight>& upgrade_heights,
                          const CryptoNote::Currency& currency) {
  CryptoNote::BlockHeight next_fork;

  for (auto upgrade : upgrade_heights) {
    /* Get the next fork height */
    if (upgrade > height) {
      next_fork = upgrade;
      break;
    }
  }

  auto heightOffset = next_fork - height;
  if (heightOffset.native() < 0) {
    return "";
  }
  auto days = heightOffset.native() / currency.coin().expectedBlocksPerDay();
  if (height == next_fork) {
    return " (forking now),";
  } else if (days < 1) {
    const auto hours = std::chrono::duration_cast<std::chrono::hours>(heightOffset.native() *
                                                                      std::chrono::seconds{currency.coin().blockTime()})
                           .count();
    return (boost::format(" (next fork in %.1f hours),") % hours).str();
  } else {
    return (boost::format(" (next fork in %.1f days),") % days).str();
  }
}

std::string get_update_status(ForkStatus forkStatus, CryptoNote::BlockHeight height,
                              const std::vector<CryptoNote::BlockHeight>& upgrade_heights,
                              const CryptoNote::Currency& currency) {
  switch (forkStatus) {
    case UpToDate:
    case ForkLater: {
      return " up to date";
    }
    case ForkSoonReady: {
      return get_fork_time(height, upgrade_heights, currency) + " up to date";
    }
    case ForkSoonNotReady: {
      return get_fork_time(height, upgrade_heights, currency) + " update needed";
    }
    case OutOfDate: {
      return " out of date, likely forked";
    }
    default: {
      throw std::runtime_error("Unexpected case unhandled");
    }
  }
}

//--------------------------------------------------------------------------------
std::string get_upgrade_info(CryptoNote::BlockHeight supported_height,
                             std::vector<CryptoNote::BlockHeight> upgrade_heights,
                             const CryptoNote::Currency& currency) {
  for (auto upgrade : upgrade_heights) {
    if (upgrade > supported_height) {
      return "The network forked at height " + std::to_string(upgrade.native()) +
             ", please update your software: " + currency.general().downloadUrl();
    }
  }

  /* This shouldnt happen */
  return std::string();
}

//--------------------------------------------------------------------------------
std::string get_status_string(const StatusInfo& iresp, const CryptoNote::Currency& currency) {
  std::stringstream ss;
  std::time_t uptime = std::time(nullptr) - iresp.start_time;
  auto forkStatus = get_fork_status(iresp.network_height, iresp.upgrade_heights, iresp.supported_height, currency);

  ss << "Height: " << iresp.height.native() << "/" << iresp.network_height.native() << " ("
     << get_sync_percentage(iresp.height, iresp.network_height) << "%) "
     << "on " << iresp.network << (iresp.synced ? " synced, " : " syncing, ") << "net hash "
     << get_mining_speed(iresp.hashrate) << ", "
     << "" << toString(iresp.version) << ","
     << get_update_status(forkStatus, iresp.network_height, iresp.upgrade_heights, currency) << ", "
     << iresp.outgoing_connections_count << "(out)+" << iresp.incoming_connections_count << "(in) connections, "
     << "uptime " << (unsigned int)floor(uptime / 60.0 / 60.0 / 24.0) << "d "
     << (unsigned int)floor(fmod((uptime / 60.0 / 60.0), 24.0)) << "h "
     << (unsigned int)floor(fmod((uptime / 60.0), 60.0)) << "m " << (unsigned int)fmod(uptime, 60.0) << "s";

  if (forkStatus == OutOfDate) {
    ss << std::endl << get_upgrade_info(iresp.supported_height, iresp.upgrade_heights, currency);
  }

  return ss.str();
}

void printStatus(const StatusInfo& iresp, const CryptoNote::Currency& currency, std::ostream& stream) {
  const auto currentTime = std::time(nullptr);
  const auto startTime = static_cast<std::time_t>(iresp.start_time);
  const auto upTime = currentTime - startTime;

  const auto upMinutes = upTime / 60;
  const auto upHours = upMinutes / 60;
  const auto upDays = upHours / 24;

  const auto syncPercentage = get_sync_percentage(iresp.height, iresp.network_height);
  const auto forkStatus =
      get_fork_status(iresp.network_height, iresp.upgrade_heights, iresp.supported_height, currency);

  YAML::Emitter emitter{stream};
  stream << rang::bg::reset << rang::fg::reset << rang::style::reset;
  emitter << YAML::BeginMap;
  {
    stream << rang::fg::green;
    emitter << YAML::Key << "height";

    std::stringstream builder{};
    builder << iresp.height.native() << "/" << iresp.network_height.native();
    stream << rang::fg::reset;
    emitter << YAML::Value << builder.str();

    if (iresp.height < iresp.network_height) {
      stream << rang::fg::yellow << rang::style::italic;
      stream << " (" << syncPercentage << "%) SYNCING";
      stream << rang::fg::reset << rang::style::reset;
    } else if (iresp.height > iresp.network_height.next(1)) {
      stream << rang::fg::red << rang::style::bold;
      stream << " DETACHED";
      stream << rang::fg::reset << rang::style::reset;
    } else {
      stream << rang::fg::green;
      stream << " SYNCED";
      stream << rang::fg::reset;
    }
  }
  {
    stream << rang::fg::green;
    emitter << YAML::Key << "network";
    stream << rang::fg::reset;
    emitter << YAML::Value << toString(iresp.network);
  }
  {
    stream << rang::fg::green;
    emitter << YAML::Key << "p2p";
    emitter << YAML::Value << YAML::BeginMap;
    emitter << YAML::Key << "outgoing connections";
    stream << rang::fg::reset;
    emitter << YAML::Value << iresp.outgoing_connections_count;
    stream << rang::fg::green;
    emitter << YAML::Key << "incoming connections";
    stream << rang::fg::reset;
    emitter << YAML::Value << iresp.incoming_connections_count;
    emitter << YAML::EndMap;
  }
  {
    stream << rang::fg::green;
    emitter << YAML::Key << "update status";
    stream << rang::fg::reset;
    if (forkStatus == ForkStatus::ForkLater) {
    } else if (forkStatus == ForkStatus::ForkSoonReady) {
      stream << rang::fg::green << rang::style::italic;
    } else if (forkStatus == ForkStatus::ForkSoonNotReady) {
      stream << rang::fg::red << rang::style::bold;
    } else if (forkStatus == ForkStatus::OutOfDate) {
      stream << rang::fg::red << rang::style::bold << rang::bg::yellow;

    } else if (forkStatus == ForkStatus::UpToDate) {
      stream << rang::fg::green << rang::style::underline;
    }
    emitter << YAML::Value
            << get_update_status(forkStatus, iresp.network_height, iresp.upgrade_heights, currency).substr(1);
    stream << rang::fg::reset << rang::style::reset;
  }
  {
    stream << rang::fg::green;
    emitter << YAML::Key << "hashrate";
    stream << rang::fg::reset;
    emitter << YAML::Value << get_mining_speed(iresp.hashrate);
  }
  {
    stream << rang::fg::green;
    emitter << YAML::Key << "uptime";
    stream << rang::fg::reset;
    std::stringstream builder{};
    builder << upDays << "d+" << upHours % 24 << ":" << upMinutes % 60;
    emitter << YAML::Value << builder.str();
  }
  emitter << YAML::EndMap;
  stream << rang::bg::reset << rang::fg::reset << rang::style::reset;
}

}  // namespace Common

max_stars_repo_name: ElSamaritan/blockchain-OLD

max_issues_repo_name: ElSamaritan/blockchain-OLD

max_forks_repo_name: ElSamaritan/blockchain-OLD


text:
//==================================================================================================
/*!
  @file

    @copyright 2016 NumScale SAS

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

#if defined(DOXYGEN_ONLY)
namespace boost { namespace simd
{

  /*!

    @ingroup group-hyperbolic
    This function object returns the hyperbolic cardinal sine: \f$\sinh(x)/x\f$.

    @par Header <boost/simd/function/sinhc.hpp>

    @see cosh, sinh

    @par Example:

      @snippet sinhc.cpp sinhc

    @par Possible output:

      @snippet sinhc.txt sinhc

  **/
  IEEEValue sinhc(IEEEValue const& x);
} }
#endif

#include <boost/simd/function/scalar/sinhc.hpp>
#include <boost/simd/function/simd/sinhc.hpp>

#endif

max_stars_repo_name: SylvainCorlay/pythran

max_issues_repo_name: SylvainCorlay/pythran

max_forks_repo_name: SylvainCorlay/pythran


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

#include <boost/hana/detail/variadic/foldr1.hpp>

#include <boost/hana/assert.hpp>
#include <boost/hana/config.hpp>
#include <boost/hana/type.hpp>
using namespace boost::hana;
namespace variadic = detail::variadic;

template <typename X, typename Y>
struct R;

template <typename X, typename Y>
struct F { using type = R<X, Y>; };

BOOST_HANA_CONSTEXPR_LAMBDA auto mf = metafunction<F>;
BOOST_HANA_CONSTEXPR_LAMBDA auto f = [](auto x, auto y) { return mf(x, y); };

template <typename ...Ts, typename R>
void test(R result) {
    static_assert(sizeof...(Ts) >= 1, "foldr1 requires at least 1 element");
    BOOST_HANA_CONSTANT_CHECK(equal(variadic::foldr1_t<Ts...>(mf), result));
    BOOST_HANA_CONSTANT_CHECK(equal(variadic::foldr1_t<Ts...>(f), result));
    BOOST_HANA_CONSTANT_CHECK(equal(variadic::foldr1(mf, type<Ts>...), result));
    BOOST_HANA_CONSTANT_CHECK(equal(variadic::foldr1(f, type<Ts>...), result));
}

struct x1; struct x2; struct x3; struct x4; struct x5; struct x6;

int main() {
    test<x1>(type<x1>);
    test<x1, x2>(type<R<x1, x2>>);
    test<x1, x2, x3>(type<R<x1, R<x2, x3>>>);
    test<x1, x2, x3, x4>(type<R<x1, R<x2, R<x3, x4>>>>);
    test<x1, x2, x3, x4, x5>(type<R<x1, R<x2, R<x3, R<x4, x5>>>>>);
    test<x1, x2, x3, x4, x5, x6>(type<R<x1, R<x2, R<x3, R<x4, R<x5, x6>>>>>>);

    (void)f;
}

max_stars_repo_name: josephwinston/hana

max_issues_repo_name: josephwinston/hana

max_forks_repo_name: josephwinston/hana


text:
//
// Copyright 2005-2007 Adobe Systems Incorporated
//
// 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 <boost/gil/extension/io/jpeg_dynamic_io.hpp>

// Example to demonstrate a way to compute gradients along x-axis

using namespace boost::gil;

template <typename Out>
struct halfdiff_cast_channels {
    template <typename T> Out operator()(const T& in1, const T& in2) const {
        return Out((in2-in1)/2);
    }
};

template <typename SrcView, typename DstView>
void x_gradient(const SrcView& src, const DstView& dst) {
    typedef typename channel_type<DstView>::type dst_channel_t;

    for (int y=0; y<src.height(); ++y) {
        typename SrcView::x_iterator src_it = src.row_begin(y);
        typename DstView::x_iterator dst_it = dst.row_begin(y);

        for (int x=1; x<src.width()-1; ++x) {
            static_transform(src_it[x-1], src_it[x+1], dst_it[x],
                             halfdiff_cast_channels<dst_channel_t>());
        }
    }
}

template <typename SrcView, typename DstView>
void x_luminosity_gradient(const SrcView& src, const DstView& dst) {
    typedef pixel<typename channel_type<SrcView>::type, gray_layout_t> gray_pixel_t;
    x_gradient(color_converted_view<gray_pixel_t>(src), dst);
}

int main() {
    rgb8_image_t img;
    jpeg_read_image("test.jpg",img);

    gray8s_image_t img_out(img.dimensions());
    fill_pixels(view(img_out),int8_t(0));

    x_luminosity_gradient(const_view(img), view(img_out));
    jpeg_write_view("out-x_gradient.jpg",color_converted_view<gray8_pixel_t>(const_view(img_out)));

    return 0;
}

max_stars_repo_name: alexhenrie/poedit

max_issues_repo_name: alexhenrie/poedit

max_forks_repo_name: alexhenrie/poedit


text:
#include "mex.h"
#include <iostream>
#include "drakeMexUtil.h"
#include "RigidBodyManipulator.h"
#include "math.h"
#include <Eigen/Sparse>

using namespace Eigen;
using namespace std;

/*
 * MATLAB signature: [n, D, dn, dD] = contactConstraintsmex(model_ptr, normals, idxA, idxB, xA, xB, d)
 *
 * Inputs:
 *  model_ptr = mex_model_ptr to the RBM
 *  normals = (3xm) contact normals (from B to A) for each of m possible contacts in world space
 *  idxA = 1-based body indexes of body A for m possible contacts
 *  idxB = 1-based body indexes of body B for m possible contacts
 *  xA = (3xm) matrix of contact positions in body A space
 *  xB = (3xm) matrix of contact positions in body B space
 *  d = {k}(3xm) surface tangent basis vectors for the friction cone approximation in world space
 *     **NOTE** k represents the half-count for basis vectors. (e.g. k = 2 means 4 basis vectors)
 * Outputs:
 *   n = m x (num_dof) dphi/dq normal vectors in joint coordinates
 *   D = {2k} m x (num_dof) dD/dq surface tangent basis vectors in joint coordinates
 *   dn = m*num_dof x num_dof dn/dq second derivative of contact distances with respect to state
 *   dD = {2k} m*num_dof x numdof dD/dq second derivate of basis vectors with respect to state
 */

inline void buildSparseMatrix(Matrix3Xd const & pts, SparseMatrix<double> & sparse)
{
	typedef SparseMatrix<double>::Index SparseIndex;
  const SparseIndex m = static_cast<SparseIndex>(pts.cols());
  const SparseIndex numNonZero = 3*m;

  sparse.resize(m, numNonZero);
  sparse.reserve(VectorXi::Constant(numNonZero, 1));

	SparseIndex j = 0;
  for (SparseIndex i = 0 ; i < m ; i++) {
    for (SparseIndex k = 0 ; k < 3 ; k++) {
      sparse.insert(i, j) =  pts(j);
      j++;
    }
  }
}

void mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] ) {

  if (nrhs < 8) {
    mexErrMsgIdAndTxt("Drake:contactConstraintsmex:NotEnoughInputs","Usage: \n[n, D] = contactConstraintsmex(model_ptr, cache_ptr, normal, idxA, idxB, xA, xB, d)\n[n, D, dn, dD] = contactConstraintsmex(model_ptr, normal, idxA, idxB, xA, xB, d)");
  }

  if (nrhs >= 6 && mxGetN(prhs[3]) != mxGetN(prhs[4])) {
    mexErrMsgIdAndTxt("Drake:contactConstraintsmex:InvalidBodyIndexes", "idxA and idxB must be the same size");
  }

  if (nrhs >= 8 && mxGetN(prhs[5]) != mxGetN(prhs[6])) {
    mexErrMsgIdAndTxt("Drake:contactConstraintsmex:InvalidBodyPoints", "xA and xB must be the same size");
  }

  if (nrhs >= 8 && (mxGetM(prhs[5]) != 3 || mxGetM(prhs[6]) != 3)) {
    mexErrMsgIdAndTxt("Drake:contactConstraintsmex:InvalidBodyPointsDimension", "body points xA and xB must be 3 dimensional");
  }

  if (nrhs >= 7 && mxGetN(prhs[2]) != mxGetN(prhs[5])) {
    mexErrMsgIdAndTxt("Drake:contactConstraintsmex:InvalidBodyPointsDimension", "normals must match the number of contact points");
  }

  int arg_num = 0;
  RigidBodyManipulator *model = static_cast<RigidBodyManipulator*>(getDrakeMexPointer(prhs[arg_num++]));
  KinematicsCache<double>* cache = static_cast<KinematicsCache<double>*>(getDrakeMexPointer(prhs[arg_num++]));

  const size_t numContactPairs = mxGetN(prhs[arg_num]);
  const Map<Matrix3Xd> normals(mxGetPrSafe(prhs[arg_num]), 3, numContactPairs); //contact normals in world space
  arg_num++;

  const bool compute_second_derivatives = nlhs > 3;

  if (model != NULL) {

    const Map<VectorXi> idxA((int*)mxGetData(prhs[arg_num++]), numContactPairs); //collision pairs index of body A
    const Map<VectorXi> idxB((int*)mxGetData(prhs[arg_num++]), numContactPairs); //collision pairs index of body B
    const Map<Matrix3Xd> xA(mxGetPrSafe(prhs[arg_num++]), 3, numContactPairs); //contact point in body A space
    const Map<Matrix3Xd> xB(mxGetPrSafe(prhs[arg_num++]), 3, numContactPairs); //contact point in body B space
    const size_t nq = model->num_positions;

    VectorXi idxA_zero_indexed = idxA.array() - 1;
    VectorXi idxB_zero_indexed = idxB.array() - 1;

    if (nlhs > 0) {
      MatrixXd J;
      MatrixXd dJ;
      vector<int> bodyInds;
      SparseMatrix<double> sparseNormals;
      model->computeContactJacobians(*cache, idxA_zero_indexed, idxB_zero_indexed, xA, xB, compute_second_derivatives, J, dJ);
      buildSparseMatrix(normals, sparseNormals);
      plhs[0] = mxCreateDoubleMatrix(numContactPairs, nq, mxREAL);
      Map<MatrixXd> n(mxGetPrSafe(plhs[0]), numContactPairs, nq);
      n = sparseNormals * J; //dphi/dq
      int d_arg_num = arg_num;
      const size_t* dims = mxGetDimensions(prhs[d_arg_num]);
      const mwSize num_tangent_vectors = dims[1];

      if (nlhs > 1) {
        const mwSize cellDims[] = {1, 2*num_tangent_vectors};
        plhs[1] = mxCreateCellArray(2, cellDims);
        if (nlhs > 3) {
          plhs[3] = mxCreateCellArray(2, cellDims);
        }
        for (int k = 0 ; k < num_tangent_vectors ; k++) { //for each friction cone basis vector
          Map<Matrix3Xd> dk(mxGetPrSafe(mxGetCell(prhs[d_arg_num], k)), 3, numContactPairs);
          SparseMatrix<double> sparseTangents;
          buildSparseMatrix(dk, sparseTangents);
          mxArray *D_cell = mxCreateDoubleMatrix(numContactPairs, nq, mxREAL);
          mxArray *D_cell_reflected = mxCreateDoubleMatrix(numContactPairs, nq, mxREAL);
          Map<MatrixXd> Dk(mxGetPrSafe(D_cell), numContactPairs, nq);
          Map<MatrixXd> Dk_reflected(mxGetPrSafe(D_cell_reflected), numContactPairs, nq);
          Dk = sparseTangents * J; //dd/dq
          Dk_reflected = -Dk;
          mxSetCell(plhs[1], k, D_cell);
          mxSetCell(plhs[1], k + num_tangent_vectors, D_cell_reflected);
          if (compute_second_derivatives) {
            if (nlhs > 3) {
              mxArray *dD_cell = mxCreateDoubleMatrix(numContactPairs * nq, nq, mxREAL);
              Map<MatrixXd> dD(mxGetPrSafe(dD_cell), numContactPairs , nq * nq);
              dD = sparseTangents * dJ;
              mxSetCell(plhs[3], k, dD_cell);
              mxArray *dD_cell_reflected = mxCreateDoubleMatrix(numContactPairs * nq, nq, mxREAL);
              Map<MatrixXd> dD_reflected(mxGetPrSafe(dD_cell_reflected), numContactPairs, nq*nq);
              dD_reflected = -dD;
              mxSetCell(plhs[3], k + num_tangent_vectors, dD_cell_reflected);
            }
          }
        }
      }

      if (nlhs > 2) {
       plhs[2] = mxCreateDoubleMatrix(numContactPairs * nq, nq, mxREAL);
       Map<MatrixXd> dn(mxGetPrSafe(plhs[2]), numContactPairs, nq*nq);
       dn = sparseNormals * dJ;
     }
   }
 }
}


text:
#include <vector>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;

template <typename Container, typename Iter>
struct srec_parser: qi::grammar<Iter, Container()> {
	srec_parser(): srec_parser::base_type(start) {
		start = qi::no_skip[lines];
		// _a: address length of this line
		// _b: data length of this line
		// _c: start address of this line
		// _d: data of this line
		// _e: line doesn't contain data
		lines = *('S'
			> qi::digit[
				phx::switch_(qi::_1) [
					phx::case_<'3'>(qi::_a = 4),
					phx::case_<'7'>(qi::_a = 4),
					phx::case_<'2'>(qi::_a = 3),
					phx::case_<'8'>(qi::_a = 3),
					phx::default_(qi::_a = 2)
				],
				phx::switch_(qi::_1) [
					phx::case_<'1'>(qi::_e = false),
					phx::case_<'2'>(qi::_e = false),
					phx::case_<'3'>(qi::_e = false),
					phx::default_(qi::_e = true)
				]
			  ]
			> hex_byte[qi::_b = qi::_1 - qi::_a - 1]
			> qi::eps[qi::_c = 0] > qi::repeat(qi::_a)[hex_byte[qi::_c = (qi::_c << 8) | qi::_1]]
			> qi::eps[phx::clear(qi::_d)] > qi::repeat(qi::_b)[hex_byte[phx::push_back(qi::_d, qi::_1)]]
			> hex_byte[
				qi::_pass = qi::_1 == (0xFF & ~phx::accumulate(qi::_d, (qi::_c>>24) + (qi::_c>>16) + (qi::_c>>8) + qi::_c + qi::_b + qi::_a + 1)),
				if_(!qi::_e) [
					phx::resize(qi::_val, qi::_b+qi::_c),
					phx::for_(qi::_b = 0, qi::_b < phx::size(qi::_d), ++qi::_b) [
						qi::_val[qi::_b+qi::_c] = qi::_d[qi::_b]
					]
				]
			  ]
			> +qi::eol);
		hex_byte = qi::uint_parser<unsigned, 16, 2, 2>();
		qi::on_error<qi::fail>
		(
            lines
          , std::cout
			<< phx::val("Error! Expecting ")
			<< qi::_4                               // what failed?
			<< phx::val(" here: \"")
			<< phx::construct<std::string>(qi::_3, qi::_2)   // iterators to error-pos, end
			<< phx::val("\"")
                << std::endl
        );	}
	qi::rule<Iter, Container()> start;
	qi::rule<Iter, Container(), qi::locals<unsigned, unsigned, std::size_t, std::vector<unsigned char>, bool> > lines;
	qi::rule<Iter, unsigned char()> hex_byte;
};

template <typename Container, typename Iter>
Container parse_srec(Iter &first, Iter last) {
	Container c;
	qi::parse(first, last, srec_parser<Container, Iter>(), c);
	return c;
}

template <typename Container, typename Input>
Container parse_srec(const Input &in) {
	typename Input::const_iterator ite = in.begin();
	typename Input::const_iterator end = in.end();
	return parse_srec<Container>(ite, end);
}

#include <boost/asio.hpp>

namespace asio = boost::asio;

#include <string>
#include <fstream>
#include <iterator>
#include <vector>
#include <deque>
#include <boost/exception/all.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/format.hpp>

using namespace std;
using boost::system::error_code;
using boost::format;

inline string get_option_arg(int &argc, char **&argv) {
	if (strlen(*argv)==2) {
		if (--argc) return string(*++argv);
		++argv;
		return string();
	}
	return string((*argv)+2);
}

inline void require(unsigned char val, unsigned char ex) {
	if (val == ex) return;
	cerr << (format("invalid response '0x%02X'(should be 0x%02X\n") % (unsigned)val % (unsigned)ex);
	throw runtime_error("unexpected respoonse");
}

void help() {
	cerr << "usage: yaki-h8 [opts...] {file}\n";
	cerr << "\n";
	cerr << "  file:        S-record file to transfer\n";
	cerr << " options:\n";
	cerr << "  -p PORT      specify serial port\n";
	cerr << "  -s BAUDRATE  specify initial hand-shake speed\n";
	cerr << "  -c CLOCK     specify target system clock freq\n";
	cerr << "  -t BAUDRATE  specify data transfer speed\n";
	cerr << "  -h           print this message\n";
}

#include "stub-h8tiny.h"

inline std::vector<unsigned char> read_file(const string &file) {
	std::ifstream f(file);
	f.seekg(0, std::ios_base::end);
	std::vector<unsigned char> r(f.tellg());
	f.seekg(0, std::ios_base::beg);
	f.read((char *)&r[0], r.size());
	return r;
}

int main(int argc, char **argv) {
	string mot_name;
	string port_name("COM1");
	int init_speed = 9600;
	int alt_speed = 0;
	double target_clock = 20.0;

	while (--argc) {
		string arg(*++argv);
		if (arg.empty()) continue;
		if (arg[0]=='-') {
			if (arg.length()==1) {
				if (--argc) mot_name = *++argv;
				else ++argc;
				continue;
			} else {
				switch (arg[1]) {
				case 'h': help(); continue;
				case 'p': port_name = get_option_arg(argc, argv); continue;
				case 's': init_speed = boost::lexical_cast<int>(get_option_arg(argc, argv)); continue;
				case 'c': target_clock = boost::lexical_cast<double>(get_option_arg(argc, argv)); continue;
				case 't': alt_speed = boost::lexical_cast<int>(get_option_arg(argc, argv)); continue;
				}
			}
			cerr << "unknown option: '" << arg << "'\n";
			continue;
		}
		if (mot_name.empty()) mot_name = std::move(arg);
		else cerr << "multiple target files. ignored.\n";
	}
	if (mot_name.empty()) {
		cerr << "target file must be set\n";
		return 1;
	}

	asio::io_service serv;
	asio::serial_port ser(serv, port_name);
	ser.set_option(asio::serial_port::baud_rate(init_speed));
	ser.set_option(asio::serial_port::flow_control());
	ser.set_option(asio::serial_port::parity());
	ser.set_option(asio::serial_port::stop_bits());
	ser.set_option(asio::serial_port::character_size());

	const auto sendb = [&ser](unsigned char b) {
		asio::write(ser, asio::buffer(&b, 1));
	};
	const auto sendw = [&ser](uint16_t w) {
		const unsigned char a[] = { (unsigned char)(w>>8), (unsigned char)w };
		asio::write(ser, asio::buffer(a));
	};
	const auto sendl = [&ser](uint32_t l) {
		const unsigned char a[] = { (unsigned char)(l>>24), (unsigned char)(l>>16), (unsigned char)(l>>8), (unsigned char)(l) };
		asio::write(ser, asio::buffer(a));
	};
	const auto recvb = [&ser]() -> unsigned char {
		unsigned char c;
		asio::read(ser, asio::buffer(&c, 1));
		return c;
	};

	auto romdata(parse_srec<deque<unsigned char>>(read_file(mot_name)));
	romdata.resize(((romdata.size()-1)|0x000000FF)+1, 0xFF);

	{
		cout << "synchronizing" << flush;
		unsigned char c;
		const array<unsigned char, 256> zeroes = {{}};
		bool synced = false;
		asio::async_read(ser, asio::buffer(&c, 1), [&synced](error_code ec, size_t) { synced = true; });
		for (unsigned n = 0; n < static_cast<unsigned>(-1) && !synced; ++n) {
			cout << '.' << flush;
			asio::async_write(ser, asio::buffer(zeroes), [](error_code ec, size_t) { });
			serv.run_one();
		}
		ser.cancel();
		if (!synced) {
			cerr << "no responce from target.\n";
			return 2;
		}
		require(c, 0x00);
		cout << " done." << endl;
	} {
		cout << "erasing..." << flush;
		sendb(0x55);
		unsigned char c;
		while ((c=recvb()) == 0x00);
		require(c, 0xAA);
		cout << " done." << endl;
	} {
		const vector<unsigned char> stub(stub_h8tiny, stub_h8tiny+sizeof(stub_h8tiny));
		cout << "sending stub (" << stub.size() << "B) " << flush;
		sendw(stub.size()&0xFFFF);
		cout << '.' << flush;
		recvb(); recvb();
		for (unsigned n=0; n<stub.size(); ++n) {
			if (n%16==0) cout << '.' << flush;
			sendb(stub[n]);
			recvb();
		}
		require(recvb(), 0xAA);
		cout << "done." << endl;
	} {
		cout << "writing " << mot_name << " (" << (romdata.size()/1024) << "KiB) "<< flush;
		require(recvb(), 'B');
		cout << '.' << flush;
		const int baud = alt_speed ? alt_speed : init_speed;
		const unsigned char spdchr = static_cast<unsigned char>(round(target_clock / 32 / baud * 1000000 - 1));
		sendb(spdchr);
		require(recvb(), spdchr);
		ser.set_option(asio::serial_port::baud_rate(baud));
		while (recvb() != 'S');
		do {
			sendb('s');
		} while (recvb() != 'T') ;
		sendb('t');
		cout << '.' << flush;
		const uint16_t m = static_cast<uint16_t>(romdata.size()>>8);
		sendw(m);
		for (uint32_t n = 0; n < m; ++n) {
			require(recvb(), 'A');
			cout << '.' << flush;
			asio::write(ser, asio::buffer(&romdata[n*256], 128));
			require(recvb(), 'A');
			asio::write(ser, asio::buffer(&romdata[n*256+128], 128));
		}
		require(recvb(), 'F');
		cout << "done." << endl;
	}
	cout << "finish." << endl;
	return 0;
}


text:
/*
 * Copyright 2014, 2015, 2016 Internet Corporation for Assigned Names and Numbers.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
 */

/*
 * Developed by Sinodun IT (www.sinodun.com)
 */

#include <iostream>
#include <string>
#include <set>
#include <map>

#define BOOST_FILESYSTEM_NO_DEPRECATED
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/unordered_map.hpp>
#include <boost/lexical_cast.hpp>

#include "dsc_types.h"
#include "DSCStrategy.h"

#include "pqxx/pqxx"

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

DSCStrategy::DSCStrategy(string server, string name, string keys[], int keylength) {

    if ( keylength != 0 ) keys_.insert (keys, keys+keylength);
    server_name_ = server;
    plot_name_ = name;

}

bool
DSCStrategy::is_valid_time(const string& time_string) {

    bool res = true;
    try {
        boost::lexical_cast<time_t>(time_string);
    if (time_string.size() != 10 ) res = false;
    }
    catch (boost::bad_lexical_cast &e) {
        res = false;
    }

    return(res);

}

void
DSCStrategy::elsify_unwanted_keys(DSCUMap &dscdata) {

    // elsify_unwanted_keys
    DSCUMap::iterator it=dscdata.begin();

    boost::unordered_map<string,int> tmp_value;
    boost::unordered_map<string,int>::iterator it1;
    while ( it != dscdata.end() ) {
        // skip if map key (second part) is else
        // skip if map key (second part) is in the list of valid
        // keys for this graph
        if ( ((*it).first.second == "else") || (keys_.find((*it).first.second) != keys_.end()) ) {
            it++;
            continue;
        }
        // Sum the values of the unknown keys
        // and then delete the unknown key.
        // Don't forget we are in an iterator and so must post increment it
        tmp_value[(*it).first.first] += (*it).second;
        dscdata.erase(it++);
    }

    it1 = tmp_value.begin();
    for(; it1 != tmp_value.end(); it1++) {
        DSCKey tmp_key1((*it1).first,"else");
        if ((*it1).second !=  0) {
            dscdata[tmp_key1] += (*it1).second;
        }
    }

}

int
DSCStrategy::accum1d_to_count(const DSCUMap &dscdata) {

    int count=0;
    DSCUMap::const_iterator it=dscdata.begin();
    for ( ; it!=dscdata.end() ; it++){
        if ((*it).first.second == "-:SKIPPED_SUM:-") {
            continue;
        } else if ((*it).first.second == "-:SKIPPED:-") {
            count += (*it).second;
        } else {
            count++;
        }
    }
    return count;

}

void
DSCStrategy::merge_accum1d(const DSCUMap &dscdata, DSCUMap &accum){

    DSCKey tmp_key;
    tmp_key.first = "All";
    DSCUMap::const_iterator it=dscdata.begin();
    for ( ; it!=dscdata.end() ; it++){
        tmp_key.second = (*it).first.second;
        accum[tmp_key] += (*it).second;
    }

}

// convert a "2D" accum-type dataset to a "count-type"
// ie, count all input->{*}{k2} into output->{k2}
void
DSCStrategy::accum2d_to_count(const DSCUMap &dscdata, DSCUMap &count) {

    DSCUMap::const_iterator it=dscdata.begin();
    DSCKey tmp_key;
    while ( it != dscdata.end() ) {
        if ( (*it).first.second == "-:SKIPPED_SUM:-" ) {

        } else if ( (*it).first.second == "-:SKIPPED:-" ) {
            tmp_key.first = "All";
            tmp_key.second = (*it).first.first;
            count[tmp_key] += (*it).second;
        } else {
            tmp_key.first = "All";
            tmp_key.second = (*it).first.first;
            count[tmp_key] += 1;
        }
        it++;
    }

}

// convert a "2D" accum-type dataset to a trace-type
// ie, sum all input->{*}{k2} into output->{k2}
void
DSCStrategy::accum2d_to_trace(const DSCUMap &dscdata, DSCUMap &trace){

    DSCUMap::const_iterator it=dscdata.begin();
    DSCKey tmp_key;
    while ( it != dscdata.end() ) {
        if ( (*it).first.second != "-:SKIPPED:-" ) {
            tmp_key.first = "All";
            tmp_key.second = (*it).first.first;
            trace[tmp_key] += (*it).second;
        }
        it++;

    }

}

void
DSCStrategy::swap_dimensions(const DSCUMap &dscdata, DSCUMap &swap) {

    DSCUMap::const_iterator it=dscdata.begin();
    DSCKey tmp_key;
    while ( it != dscdata.end() ) {
        tmp_key.first = (*it).first.second;
        tmp_key.second = (*it).first.first;
        swap[tmp_key] = (*it).second;
        it++;
    }
    if ( swap.size() != dscdata.size() ) cout << "Error swapping keys\n";

}

void
DSCStrategy::merge_accum2d(const DSCUMap &dscdata, DSCUMap &old) {

    DSCUMap::const_iterator it=dscdata.begin();
    for ( ; it != dscdata.end() ; it++ ) old[(*it).first] += (*it).second;

}

void
DSCStrategy::trim_accum2d(DSCUMap &dscdata) {

    // key order has been swapped before getting here
    // dscdata -> (TLD, QTYPE) -> count)
    multimap<int,string> r_map;
    multimap<int,string>::reverse_iterator ir;
    set<DSCKey> keys2delete;

    // iterate over allowed keys
    set<string>::iterator key = keys_.begin();
    for (; key != keys_.end() ; key++ ) {

        int n = 0;
        // iterate over dscdata and create multimap
        // r_map = count -> TLD (sorted))
        DSCUMap::iterator it=dscdata.begin();
        for (; it != dscdata.end() ; it++) {
            if ((*it).first.second != (*key) ) continue;
            // use multimap to sort by value
            r_map.insert(pair<int,string>((*it).second,(*it).first.first));
        }
        // iterate over r_map
        ir=r_map.rbegin();
        for (; ir != r_map.rend() ; ir++) {
            // Create key_pair
            // TLD,allowed key
            DSCKey key_pair ((*ir).second,*key);
            // continue if n <= 1000
            //  this means that we have seen 1000 or more (TLD,allowed key) pairs
            if ( ++n <= 1000 ) continue;
            // continue if key_pair is not found in dscdata
            if ( dscdata.find(key_pair) == dscdata.end() ) continue;
            // so now we have TLD,allowed key pairs above 1000
            // for each one add one to dscdata("-:SKIPPED:-", allowed key)  value
            DSCKey tmp_skip ("-:SKIPPED:-",*key);
            dscdata[tmp_skip]++;
            // and add the value to a dscdata("-:SKIPPED_SUM:-", allowed key)  value
            DSCKey tmp_skip_sum ("-:SKIPPED_SUM:-",*key);
            dscdata[tmp_skip_sum] += dscdata[key_pair];
            // Add the (TLD,allowed key pair) to a set of keys to be deleted later
            keys2delete.insert(key_pair);
        }
        r_map.clear();
        // and finally delete the dscdata entries in set keys2delete
        set<DSCKey>::iterator it1 = keys2delete.begin();
        for (; it1 != keys2delete.end(); it1++) {
            dscdata.erase((*it1));
        }
        keys2delete.clear();
    }
    return;

}

void
DSCStrategy::open_file(string dtime[], ios_base::openmode open_mode) {

    bfs::path p_tmp;
    bfs::path p_tmp_file;
    bfs::path p_unique;
    bfs::path p_data_root = ".";
    bfs::path p_storage = dtime[0];
    bfs::path p_data_storage(p_data_root/p_storage);
    bfs::path p_filename(plot_name_ + ".dat");
    bfs::path p_dat_file(p_data_storage/p_filename);

    if ( open_mode & ios_base::app || open_mode & ios_base::trunc || open_mode & ios_base::out) {
        p_unique = bfs::unique_path();
        p_tmp_file = plot_name_ + ".dat." + p_unique.string();
        p_tmp = p_data_storage/p_tmp_file;
    }

    try {

        if (! bfs::exists(p_data_storage)) bfs::create_directory(p_data_storage);
        if (! bfs::exists(p_data_storage)) {
            cerr <<  "FS Error: " << p_data_storage << " directory could not be created." << endl;
            return;
        }
        if (! bfs::is_directory(p_data_storage)) {
            cerr <<  "FS Error: " <<  p_data_storage << " exists but is not a directory." << endl;
            return;
        }

        if ( open_mode & ios_base::app || open_mode & ios_base::trunc || open_mode & ios_base::out) {
            if (bfs::exists(p_dat_file)) bfs::copy_file(p_dat_file, p_tmp);
            datafile_.filename=p_dat_file.string();
            datafile_.tmpfilename=p_tmp.string();
            datafile_.open_mode = open_mode;
            datafile_.filestream.open(p_tmp, open_mode);
            if (! datafile_.filestream.is_open()) {
                cerr <<  "FS Error: " << p_tmp << " temporary file did not open." << endl;
                return;
            }
        } else if ( open_mode & ios_base::in ) {
            datafile_.filename=p_dat_file.string();
            datafile_.open_mode = open_mode;
            datafile_.filestream.open(p_dat_file, open_mode);
            if (bfs::exists(p_dat_file) && (! datafile_.filestream.is_open()))  {
                cerr <<  "FS Error: " << p_dat_file << " dat file did not open." << endl;
                return;
            }
        } else {
            cerr << "FS Error: un-supported file open mode" << endl;
        }
    }
    catch (const bfs::filesystem_error& ex) {
        cerr << "FS Error: " << ex.what() << endl;
    }
    return;

}

void
DSCStrategy::close_file() {

    try {
        datafile_.filestream.close();
        if (datafile_.open_mode & ios_base::app || datafile_.open_mode & ios_base::trunc || datafile_.open_mode & ios_base::out) {
            bfs::rename(datafile_.tmpfilename, datafile_.filename);
        }
    }
    catch (const bfs::filesystem_error& ex) {
        cerr << "FS Error: " << ex.what() << endl;
    }

}

int
DSCStrategy::get_plot_id(const string& plot, pqxx::work& pg_db_trans) {

    stringstream sql;
    pqxx::result r;

    try {
        sql.clear();
        sql.str("");
        sql << "SELECT id FROM dsc.dataset WHERE name = '" << plot << "'" << endl;
        r = pg_db_trans.exec(sql.str());
        if ( r.size() != 1 ) {
            cerr << "Error: Expected 1 plot with name " << plot << ", "
                 << "but found " << r.size() << endl;
            exit( EXIT_FAILURE );
        }
    }
    catch( runtime_error & e )
    {
        cerr << "Runtime error: " << e.what() << endl;
        exit( EXIT_FAILURE );
    }
    catch( std::exception & e )
    {
        cerr << "Exception: " << e.what() << sql.str() << endl;
        exit( EXIT_FAILURE );
    }
    catch( ... )
    {
        cerr << "Unknown exception caught" << endl;
        exit( EXIT_FAILURE );
    }
    return r[0][0].as<int>();

}

max_stars_repo_name: japroc/hedgehog

max_issues_repo_name: japroc/hedgehog

max_forks_repo_name: sitedata/hedgehog


text:
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstring>

#include "BKTree.h"

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/regex.hpp>
#include <boost/program_options.hpp>

namespace po = boost::program_options;

class dict_builder{

	public:
	bool parse_args(int argc, char* argv[]);
	bool build_data();
	void save_data();
	std::string& get_type();
	void print_help();
	dict_builder();
	~dict_builder();

	private:
	BKTree<std::string> tree;
	std::string infile;
	std::string outfile;
	std::string ltype;
    po::options_description desc;

};

class my_exception : public std::exception {
    public:
    my_exception(const std::string& msg) : msg_(msg) {}
    const char* what(); // override what to return msg_;
    private:
    std::string msg_;
};

void dict_builder::print_help() {
	std::cout << desc << "\n";
    std::cout << "Usage: dict_builder -i <infile> -o <outfile>\n\n";
}

dict_builder::dict_builder() {
	tree = BKTree<std::string>();
}

std::string& dict_builder::get_type() {
	return ltype;
}

dict_builder::~dict_builder() {
	// Nothing yet
}

bool dict_builder::build_data() {
	std::ifstream words(infile);
	std::string lstr;
	boost::regex expr ("(\\w+)\\s+(\\w+)");
	boost::smatch what;
	if (!words.is_open()) {
    	std::cerr << "The infile cannot be open!\n";
		return false;
	} else {

		while (std::getline(words, lstr)) {
			bool res = boost::regex_search(lstr, what, expr);
			if (res) {
				std::string seq = what[1];
				std::string barcode = what[2];
				tree.insert(barcode);
			} else {
				throw my_exception("Problem in the parsing the barcode lines.\n");
			}
		}
	}
	std::cout<< "Loaded " <<tree.size()<< " entries" << std::endl;
	return true;
}

void dict_builder::save_data() {

	std::ofstream ofs(outfile);
    boost::archive::text_oarchive oa(ofs);
    oa << tree;
	ofs.close();
	return;
}

bool dict_builder::parse_args(int argc, char* argv[]) {
	 bool all_set = true;
	 bool none_set = true;

    desc.add_options()
        ("help,h", "produce help mesage")
        ("infile,i", po::value<std::string>(&infile), "Input file")
        ("outfile,o", po::value<std::string>(&outfile), "Output file")
    ;

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

    if (vm.count("help")) {
		//print_help();
        return 0;
    } else {
		//all_set =false;
	}

    if (!vm.count("infile")) {
        std::cout << "Infile not set.\n";
        all_set = false;

    } else {

        std::cout << "Infile is set to: " << infile << ".\n";
    }

    if (!vm.count("outfile")) {
        std::cout << "Outfile not set.\n";
        all_set = false;
    } else {
        std::cout << "Outfile is set to: " << outfile << ".\n";
    }

	return all_set;
}

int main(int argc, char* argv[]) {
	dict_builder ldict = dict_builder();
	bool all_set = true;

	po::options_description desc("Allowed options");
	try {

		all_set = ldict.parse_args(argc, argv);
	} catch(std::exception& e) {
       	std::cerr << "error: " << e.what() << "\n";
		//ldict.print_help();
       	return 1;
   	}
   	catch(...) {
       	std::cerr << "Exception of unknown type!\n";
		//ldict.print_help();
   	}

	if (!all_set) {
		ldict.print_help();
		return 0;
	}

	ldict.build_data();

	ldict.save_data();

    return 0;
}


text:
/*=============================================================================
    Copyright (c) 2001-2006 Joel de Guzman

    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)
==============================================================================*/
#if !defined(FUSION_AT_IMPL_07172005_0726)
#define FUSION_AT_IMPL_07172005_0726

#include <boost/fusion/support/detail/access.hpp>
#include <boost/type_traits/is_const.hpp>
#include <boost/type_traits/add_const.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/bool.hpp>

namespace boost { namespace fusion
{
    struct cons_tag;

    namespace extension
    {
        template <typename Tag>
        struct at_impl;

        template <>
        struct at_impl<cons_tag>
        {
            template <typename Sequence, typename N>
            struct apply
            {
                typedef typename
                    mpl::eval_if<
                        is_const<Sequence>
                      , add_const<typename Sequence::cdr_type>
                      , mpl::identity<typename Sequence::cdr_type>
                    >::type
                cdr_type;

                typedef typename
                    mpl::eval_if<
                        mpl::bool_<N::value == 0>
                      , mpl::identity<typename Sequence::car_type>
                      , apply<cdr_type, mpl::int_<N::value-1> >
                    >
                element;

                typedef typename
                    mpl::eval_if<
                        is_const<Sequence>
                      , detail::cref_result<element>
                      , detail::ref_result<element>
                    >::type
                type;

                template <typename Cons, int N2>
                static type
                call(Cons& s, mpl::int_<N2>)
                {
                    return call(s.cdr, mpl::int_<N2-1>());
                }

                template <typename Cons>
                static type
                call(Cons& s, mpl::int_<0>)
                {
                    return s.car;
                }

                static type
                call(Sequence& s)
                {
                    return call(s, mpl::int_<N::value>());
                }
            };
        };
    }
}}

#endif


text:
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------

#include "stdafx.h"

#include <boost/test/unit_test.hpp>
#include "Common/boost-taef.h"
#include "TStoreTestBase.h"

namespace TStoreTests
{
   using namespace ktl;

   class ConsolidationTest : public TStoreTestBase<int, int, IntComparer, TestStateSerializer<int>, TestStateSerializer<int>>
   {
   public:
      ConsolidationTest()
      {
         Setup(3);
      }

      ~ConsolidationTest()
      {
         Cleanup();
      }

      //CommonConfig config; // load the config object as its needed for the tracing to work

#pragma region test functions
    public:
        ktl::Awaitable<void> Add_ConsolidateRead_ShouldSucceed_Test()
       {
          {
             WriteTransaction<int, int>::SPtr tx1 = CreateWriteTransaction();
             for (int i = 0; i < 10; i++)
             {
                co_await Store->AddAsync(*tx1->StoreTransactionSPtr, i, i, DefaultTimeout, CancellationToken::None);
             }

             co_await tx1->CommitAsync();
          }

          // Checkpoint
          co_await CheckpointAsync();

          {
             for (ULONG m = 0; m < Stores->Count(); m++)
             {
                WriteTransaction<int, int>::SPtr tx2 = CreateWriteTransaction(*(*Stores)[m]);
                for (int i = 0; i < 10; i++)
                {
                   co_await VerifyKeyExistsAsync(*(*Stores)[m], *tx2->StoreTransactionSPtr, i, -1, i);
                }

                co_await tx2->AbortAsync();
             }
          }

          co_await CloseAndReOpenStoreAsync();

          for (int i = 0; i < 10; i++)
          {
             co_await VerifyKeyExistsInStoresAsync(i, -1, i);
          }
           co_return;
       }

        ktl::Awaitable<void> AddConsolidateRead_UpdateRead_ShouldSucceed_Test()
       {
          // 1. Add.
          {
             WriteTransaction<int, int>::SPtr tx1 = CreateWriteTransaction();

             for (int i = 0; i < 10; i++)
             {
                co_await Store->AddAsync(*tx1->StoreTransactionSPtr, i, i, DefaultTimeout, CancellationToken::None);
             }

             co_await tx1->CommitAsync();
          }

          // 2. Checkpoint.
          co_await CheckpointAsync();

          {
             for (ULONG m = 0; m < Stores->Count(); m++)
             {
                WriteTransaction<int, int>::SPtr tx2 = CreateWriteTransaction(*(*Stores)[m]);
                for (int i = 0; i < 10; i++)
                {
                   co_await VerifyKeyExistsAsync(*(*Stores)[m], *tx2->StoreTransactionSPtr, i, -1, i);
                }

                co_await tx2->AbortAsync();
             }
          }

          // 3. Update.
          {
             WriteTransaction<int, int>::SPtr tx3 = CreateWriteTransaction();
             for (int i = 0; i < 10; i++)
             {
                co_await Store->ConditionalUpdateAsync(*tx3->StoreTransactionSPtr, i, i + 10, DefaultTimeout, CancellationToken::None);
             }

             co_await tx3->CommitAsync();
          }

          {
             for (ULONG m = 0; m < Stores->Count(); m++)
             {
                WriteTransaction<int, int>::SPtr tx4 = CreateWriteTransaction(*(*Stores)[m]);
                for (int i = 0; i < 10; i++)
                {
                   co_await VerifyKeyExistsAsync(*(*Stores)[m], *tx4->StoreTransactionSPtr, i, -1, i + 10);
                }

                co_await tx4->AbortAsync();
             }
          }

          // 4. Checkpoint again.

          co_await CheckpointAsync();

          // 5. Reopen and verify.
          co_await CloseAndReOpenStoreAsync();

          for (int i = 0; i < 10; i++)
          {
             co_await VerifyKeyExistsInStoresAsync(i, -1, i + 10);
          }
           co_return;
       }

        ktl::Awaitable<void> AddConsolidateRead_UpdateConsolidateRead_ShouldSucceed_Test()
       {
          // 1. Add.
          {
             WriteTransaction<int, int>::SPtr tx1 = CreateWriteTransaction();

             for (int i = 0; i < 10; i++)
             {
                co_await Store->AddAsync(*tx1->StoreTransactionSPtr, i, i, DefaultTimeout, CancellationToken::None);
             }

             co_await tx1->CommitAsync();
          }

          // 2. Checkpoint.
          co_await CheckpointAsync();

          {
             for (ULONG m = 0; m < Stores->Count(); m++)
             {
                WriteTransaction<int, int>::SPtr tx2 = CreateWriteTransaction(*(*Stores)[m]);
                for (int i = 0; i < 10; i++)
                {
                   co_await VerifyKeyExistsAsync(*(*Stores)[m], *tx2->StoreTransactionSPtr, i, -1, i);
                }

                co_await tx2->AbortAsync();
             }
          }

          // 3. Update.
          {
             WriteTransaction<int, int>::SPtr tx3 = CreateWriteTransaction();
             for (int i = 0; i < 10; i++)
             {
                co_await Store->ConditionalUpdateAsync(*tx3->StoreTransactionSPtr, i, i + 10, DefaultTimeout, CancellationToken::None);
             }

             co_await tx3->CommitAsync();
          }

          // 5. Checkpoint.
          co_await CheckpointAsync();

          {
             for (ULONG m = 0; m < Stores->Count(); m++)
             {
                WriteTransaction<int, int>::SPtr tx4 = CreateWriteTransaction(*(*Stores)[m]);
                for (int i = 0; i < 10; i++)
                {
                   co_await VerifyKeyExistsAsync(*(*Stores)[m], *tx4->StoreTransactionSPtr, i, -1, i + 10);
                }

                co_await tx4->AbortAsync();
             }
          }

          // 6. Reopen and verify.
          co_await CloseAndReOpenStoreAsync();

          for (int i = 0; i < 10; i++)
          {
             co_await VerifyKeyExistsInStoresAsync(i, -1, i + 10);
          }
           co_return;
       }

        ktl::Awaitable<void> AddCheckpoint_ReOpenUpdate_ShouldSucceed_Test()
       {
          // 1. Add.
          {
             WriteTransaction<int, int>::SPtr tx1 = CreateWriteTransaction();
             for (int i = 0; i < 10; i++)
             {
                co_await Store->AddAsync(*tx1->StoreTransactionSPtr, i, i, DefaultTimeout, CancellationToken::None);
             }

             co_await tx1->CommitAsync();
          }

          // 2. Checkpoint.
          co_await CheckpointAsync();

          {
             for (ULONG m = 0; m < Stores->Count(); m++)
             {
                WriteTransaction<int, int>::SPtr tx2 = CreateWriteTransaction(*(*Stores)[m]);
                for (int i = 0; i < 10; i++)
                {
                   co_await VerifyKeyExistsAsync(*(*Stores)[m], *tx2->StoreTransactionSPtr, i, -1, i);
                }

                co_await tx2->AbortAsync();
             }
          }

          // 3. ReOpen.
          co_await CloseAndReOpenStoreAsync();

          // 4. Update.
          {
             WriteTransaction<int, int>::SPtr tx3 = CreateWriteTransaction();
             for (int i = 0; i < 10; i++)
             {
                co_await Store->ConditionalUpdateAsync(*tx3->StoreTransactionSPtr, i, i + 10, DefaultTimeout, CancellationToken::None);
             }

             co_await tx3->CommitAsync();
          }

          for (int i = 0; i < 10; i++)
          {
             co_await VerifyKeyExistsInStoresAsync(i, -1, i + 10);
          }
           co_return;
       }

        ktl::Awaitable<void> Add_Checkpoint_Commit_Test()
       {
          int key1 = 5;
          int key2 = 7;
          int value = 10;

          // 1. Add.
          WriteTransaction<int, int>::SPtr tx1 = CreateWriteTransaction();
          co_await Store->AddAsync(*tx1->StoreTransactionSPtr, key1, value, DefaultTimeout, CancellationToken::None);
          co_await Store->AddAsync(*tx1->StoreTransactionSPtr, key2, value, DefaultTimeout, CancellationToken::None);

          // 2. Checkpoint.
          co_await CheckpointAsync();

          // 3. Apply
          co_await tx1->CommitAsync();

          // 4. Dispose
          tx1 = nullptr;

          co_await VerifyKeyExistsInStoresAsync(key1, -1, value);
          co_await VerifyKeyExistsInStoresAsync(key2, -1, value);
           co_return;
       }

        Awaitable<void> EnumerateConsolidatedState_Test()
        {
            Store->EnableBackgroundConsolidation = false;
            Store->ConsolidationManagerSPtr->NumberOfDeltasToBeConsolidated = 3;

            LONG64 firstLSN = -1;

            int value = 17;

            {
                auto key1 = 1;
                auto key2 = 10;

                {
                    auto txn = CreateWriteTransaction();
                    co_await Store->AddAsync(*txn->StoreTransactionSPtr, key1, value, DefaultTimeout, CancellationToken::None);
                    co_await Store->AddAsync(*txn->StoreTransactionSPtr, key2, value, DefaultTimeout, CancellationToken::None);
                    firstLSN = co_await txn->CommitAsync();
                }

                co_await CheckpointAsync();
            }

            int key = 5; // Repeatedly update the same key

            // Add a key
            {
                {
                    auto txn = CreateWriteTransaction();
                    co_await Store->AddAsync(*txn->StoreTransactionSPtr, key, 0, DefaultTimeout, CancellationToken::None);
                    co_await txn->CommitAsync();
                }

                co_await CheckpointAsync();
            }

            // Update it 3 more times
            for (int i = 1; i < 4; i++)
            {
                {
                    auto txn = CreateWriteTransaction();
                    bool updated = co_await Store->ConditionalUpdateAsync(*txn->StoreTransactionSPtr, key, i, DefaultTimeout, CancellationToken::None);
                    CODING_ERROR_ASSERT(updated == true);
                    co_await txn->CommitAsync();
                }

                co_await CheckpointAsync();
            }

            // After consolidation: 1 consolidated state enumerator and 2 differential state enumerators

            auto enumerator = Store->ConsolidationManagerSPtr->GetAllKeysAndValuesEnumerator();

            CODING_ERROR_ASSERT(enumerator->MoveNext());
            CODING_ERROR_ASSERT(enumerator->Current().Key == 1);
            CODING_ERROR_ASSERT(enumerator->Current().Value->GetVersionSequenceNumber() == firstLSN);

            // Key 5 will show up in consolidated state and both differential states
            LONG64 previousLSN = LONG64_MAX;
            for (ULONG32 i = 0; i < 3; i++)
            {
                CODING_ERROR_ASSERT(enumerator->MoveNext());
                CODING_ERROR_ASSERT(enumerator->Current().Key == 5);

                LONG64 currentLSN = enumerator->Current().Value->GetVersionSequenceNumber();
                CODING_ERROR_ASSERT(currentLSN < previousLSN);
                previousLSN = currentLSN;
            }

            CODING_ERROR_ASSERT(enumerator->MoveNext());
            CODING_ERROR_ASSERT(enumerator->Current().Key == 10);
            CODING_ERROR_ASSERT(enumerator->Current().Value->GetVersionSequenceNumber() == firstLSN);

            CODING_ERROR_ASSERT(enumerator->MoveNext() == false);
        }
    #pragma endregion
   };

   BOOST_FIXTURE_TEST_SUITE(ConsolidationTestSuite, ConsolidationTest)

      BOOST_AUTO_TEST_CASE(Add_ConsolidateRead_ShouldSucceed)
   {
       SyncAwait(Add_ConsolidateRead_ShouldSucceed_Test());
   }

   BOOST_AUTO_TEST_CASE(AddConsolidateRead_UpdateRead_ShouldSucceed)
   {
       SyncAwait(AddConsolidateRead_UpdateRead_ShouldSucceed_Test());
   }

   BOOST_AUTO_TEST_CASE(AddConsolidateRead_UpdateConsolidateRead_ShouldSucceed)
   {
       SyncAwait(AddConsolidateRead_UpdateConsolidateRead_ShouldSucceed_Test());
   }

   BOOST_AUTO_TEST_CASE(AddCheckpoint_ReOpenUpdate_ShouldSucceed)
   {
       SyncAwait(AddCheckpoint_ReOpenUpdate_ShouldSucceed_Test());
   }

   BOOST_AUTO_TEST_CASE(Add_Checkpoint_Commit)
   {
       SyncAwait(Add_Checkpoint_Commit_Test());
   }

   BOOST_AUTO_TEST_CASE(Enumerate_ConsolidatedState)
   {
       SyncAwait(EnumerateConsolidatedState_Test());
   }

   BOOST_AUTO_TEST_SUITE_END()
}

max_stars_repo_name: gridgentoo/ServiceFabricAzure

max_issues_repo_name: gridgentoo/ServiceFabricAzure

max_forks_repo_name: gridgentoo/ServiceFabricAzure


text:
// Copyright (c) 2015-2018 The Bitcoin Core developers
// Copyright (c) 2017 The Raven Core developers
// Copyright (c) 2018 The Rito Core developers
// Copyright (c) 2020 The Ukcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "scheduler.h"

#include "random.h"
#include "reverselock.h"

#include <assert.h>
#include <boost/bind.hpp>
#include <utility>

CScheduler::CScheduler() : nThreadsServicingQueue(0), stopRequested(false), stopWhenEmpty(false)
{
}

CScheduler::~CScheduler()
{
    assert(nThreadsServicingQueue == 0);
}

#if BOOST_VERSION < 105000
static boost::system_time toPosixTime(const boost::chrono::system_clock::time_point& t)
{
    // Creating the posix_time using from_time_t loses sub-second precision. So rather than exporting the time_point to time_t,
    // start with a posix_time at the epoch (0) and add the milliseconds that have passed since then.
    return boost::posix_time::from_time_t(0) + boost::posix_time::milliseconds(boost::chrono::duration_cast<boost::chrono::milliseconds>(t.time_since_epoch()).count());
}
#endif

void CScheduler::serviceQueue()
{
    boost::unique_lock<boost::mutex> lock(newTaskMutex);
    ++nThreadsServicingQueue;

    // newTaskMutex is locked throughout this loop EXCEPT
    // when the thread is waiting or when the user's function
    // is called.
    while (!shouldStop()) {
        try {
            if (!shouldStop() && taskQueue.empty()) {
                reverse_lock<boost::unique_lock<boost::mutex> > rlock(lock);
                // Use this chance to get a tiny bit more entropy
                RandAddSeedSleep();
            }
            while (!shouldStop() && taskQueue.empty()) {
                // Wait until there is something to do.
                newTaskScheduled.wait(lock);
            }

            // Wait until either there is a new task, or until
            // the time of the first item on the queue:

// wait_until needs boost 1.50 or later; older versions have timed_wait:
#if BOOST_VERSION < 105000
            while (!shouldStop() && !taskQueue.empty() &&
                   newTaskScheduled.timed_wait(lock, toPosixTime(taskQueue.begin()->first))) {
                // Keep waiting until timeout
            }
#else
            // Some boost versions have a conflicting overload of wait_until that returns void.
            // Explicitly use a template here to avoid hitting that overload.
            while (!shouldStop() && !taskQueue.empty()) {
                boost::chrono::system_clock::time_point timeToWaitFor = taskQueue.begin()->first;
                if (newTaskScheduled.wait_until<>(lock, timeToWaitFor) == boost::cv_status::timeout)
                    break; // Exit loop after timeout, it means we reached the time of the event
            }
#endif
            // If there are multiple threads, the queue can empty while we're waiting (another
            // thread may service the task we were waiting on).
            if (shouldStop() || taskQueue.empty())
                continue;

            Function f = taskQueue.begin()->second;
            taskQueue.erase(taskQueue.begin());

            {
                // Unlock before calling f, so it can reschedule itself or another task
                // without deadlocking:
                reverse_lock<boost::unique_lock<boost::mutex> > rlock(lock);
                f();
            }
        } catch (...) {
            --nThreadsServicingQueue;
            throw;
        }
    }
    --nThreadsServicingQueue;
    newTaskScheduled.notify_one();
}

void CScheduler::stop(bool drain)
{
    {
        boost::unique_lock<boost::mutex> lock(newTaskMutex);
        if (drain)
            stopWhenEmpty = true;
        else
            stopRequested = true;
    }
    newTaskScheduled.notify_all();
}

void CScheduler::schedule(CScheduler::Function f, boost::chrono::system_clock::time_point t)
{
    {
        boost::unique_lock<boost::mutex> lock(newTaskMutex);
        taskQueue.insert(std::make_pair(t, f));
    }
    newTaskScheduled.notify_one();
}

void CScheduler::scheduleFromNow(CScheduler::Function f, int64_t deltaMilliSeconds)
{
    schedule(f, boost::chrono::system_clock::now() + boost::chrono::milliseconds(deltaMilliSeconds));
}

static void Repeat(CScheduler* s, CScheduler::Function f, int64_t deltaMilliSeconds)
{
    f();
    s->scheduleFromNow(boost::bind(&Repeat, s, f, deltaMilliSeconds), deltaMilliSeconds);
}

void CScheduler::scheduleEvery(CScheduler::Function f, int64_t deltaMilliSeconds)
{
    scheduleFromNow(boost::bind(&Repeat, this, f, deltaMilliSeconds), deltaMilliSeconds);
}

size_t CScheduler::getQueueInfo(boost::chrono::system_clock::time_point &first,
                             boost::chrono::system_clock::time_point &last) const
{
    boost::unique_lock<boost::mutex> lock(newTaskMutex);
    size_t result = taskQueue.size();
    if (!taskQueue.empty()) {
        first = taskQueue.begin()->first;
        last = taskQueue.rbegin()->first;
    }
    return result;
}

bool CScheduler::AreThreadsServicingQueue() const {
    boost::unique_lock<boost::mutex> lock(newTaskMutex);
    return nThreadsServicingQueue;
}

void SingleThreadedSchedulerClient::MaybeScheduleProcessQueue() {
    {
        LOCK(m_cs_callbacks_pending);
        // Try to avoid scheduling too many copies here, but if we
        // accidentally have two ProcessQueue's scheduled at once its
        // not a big deal.
        if (m_are_callbacks_running) return;
        if (m_callbacks_pending.empty()) return;
    }
    m_pscheduler->schedule(std::bind(&SingleThreadedSchedulerClient::ProcessQueue, this));
}

void SingleThreadedSchedulerClient::ProcessQueue() {
    std::function<void (void)> callback;
    {
        LOCK(m_cs_callbacks_pending);
        if (m_are_callbacks_running) return;
        if (m_callbacks_pending.empty()) return;
        m_are_callbacks_running = true;

        callback = std::move(m_callbacks_pending.front());
        m_callbacks_pending.pop_front();
    }

    // RAII the setting of fCallbacksRunning and calling MaybeScheduleProcessQueue
    // to ensure both happen safely even if callback() throws.
    struct RAIICallbacksRunning {
        SingleThreadedSchedulerClient* instance;
        explicit RAIICallbacksRunning(SingleThreadedSchedulerClient* _instance) : instance(_instance) {}
        ~RAIICallbacksRunning() {
            {
                LOCK(instance->m_cs_callbacks_pending);
                instance->m_are_callbacks_running = false;
            }
            instance->MaybeScheduleProcessQueue();
        }
    } raiicallbacksrunning(this);

    callback();
}

void SingleThreadedSchedulerClient::AddToProcessQueue(std::function<void (void)> func) {
    assert(m_pscheduler);

    {
        LOCK(m_cs_callbacks_pending);
        m_callbacks_pending.emplace_back(std::move(func));
    }
    MaybeScheduleProcessQueue();
}

void SingleThreadedSchedulerClient::EmptyQueue() {
    assert(!m_pscheduler->AreThreadsServicingQueue());
    bool should_continue = true;
    while (should_continue) {
        ProcessQueue();
        LOCK(m_cs_callbacks_pending);
        should_continue = !m_callbacks_pending.empty();
    }
}