text:
#include <utility>

/**
 * @file
 * @copyright This code is licensed under the 3-clause BSD license.\n
 *            Copyright ETH Zurich, Laboratory for Physical Chemistry, Reiher Group.\n
 *            See LICENSE.txt for details.
 */
#include "GenericMethodWrapper.h"
#include "MoldenFileGenerator.h"
#include <Sparrow/Implementations/Nddo/Pm6/Wrapper/PM6MethodWrapper.h>
#include <Sparrow/Implementations/Nddo/Utils/NDDOInitializer.h>
#include <Utils/IO/NativeFilenames.h>
#include <Utils/Scf/MethodInterfaces/LcaoMethod.h>
#include <Eigen/Eigenvalues>
#include <fstream>
#include <iomanip>
#include <iostream>
namespace Scine {
namespace Sparrow {
using namespace nddo::GeneralTypes;

std::map<int, nddo::GeneralTypes::orb_t> MoldenFileGenerator::indexMap_ = {
    // s
    {0, orb_t::s},
    // px, py, pz
    {1, orb_t::x},
    {2, orb_t::y},
    {3, orb_t::z},
    // z2, xz, yz, x2-y2, xy
    {7, orb_t::x2y2},
    {5, orb_t::xz},
    {4, orb_t::z2},
    {6, orb_t::yz},
    {8, orb_t::xy}};

MoldenFileGenerator::MoldenFileGenerator(const GenericMethodWrapper& calculator) : calculator_(calculator) {
}

void MoldenFileGenerator::generateWavefunctionInformation(std::ostream& out) const {
  out << "[Molden Format]" << std::endl;
  out << "Written by Sparrow, the semiempirical library "
         "of the SCINE software package"
      << std::endl;
  generateAtomBlock(out);
  generateGTOBlock(out);
  generateMolecularOrbitalsBlock(out);
}

std::vector<Utils::AtomicGtos> MoldenFileGenerator::getStoNGExpansion() const {
  auto filename = calculator_.getStoNGExpansionPath();
  auto elements = calculator_.getStructure()->getElements();
  std::vector<Utils::AtomicGtos> atomicGtosVector;

  for (Utils::ElementType el : elements) {
    std::ifstream basisFile(filename);
    if (!basisFile.is_open()) {
      throw std::runtime_error("Not able to open basis set file '" + filename + "'.");
    }
    // TODO: parse basis set file and give it back as is.
    std::string line;
    while (std::getline(basisFile, line)) {
      std::string symbol = Utils::ElementInfo::symbol(el);
      std::transform(line.begin(), line.end(), line.begin(), ::tolower);
      std::transform(symbol.begin(), symbol.end(), symbol.begin(), ::tolower);
      if (line.substr(0, symbol.size() + 1) == symbol + " ") {
        Utils::AtomicGtos atomicGtos;
        // found element
        std::string gtoLine;
        std::stringstream gtoStream;
        // skip asterisk
        std::getline(basisFile, gtoLine);

        while (std::getline(basisFile, gtoLine)) {
          if (gtoLine.find('*') != std::string::npos)
            break;
          // get first line
          int numberOfPrimitives = 0;
          std::string label;
          // read GTO contraction infos
          gtoStream = std::stringstream(gtoLine);
          gtoStream >> numberOfPrimitives >> label;

          int angularMomentum;
          if (label == "s")
            angularMomentum = 0;
          else if (label == "p")
            angularMomentum = 1;
          else if (label == "d")
            angularMomentum = 2;
          else
            throw std::runtime_error("Angular momentum not available in semiempirical methods.");
          Utils::GtoExpansion gto(numberOfPrimitives);
          gto.setAngularMomentum(angularMomentum);
          for (int primitive = 0; primitive < numberOfPrimitives; ++primitive) {
            double coefficient = 0.0, exponent = 0.0;
            std::getline(basisFile, gtoLine);
            gtoStream = std::stringstream(gtoLine);
            gtoStream >> exponent >> coefficient;
            gto.setGTF(primitive, exponent, coefficient);
          }
          if (angularMomentum == 0) {
            atomicGtos.setS(std::move(gto));
          }
          else if (angularMomentum == 1) {
            atomicGtos.setP(std::move(gto));
          }
          else {
            atomicGtos.setD(std::move(gto));
          }
        }
        atomicGtosVector.emplace_back(std::move(atomicGtos));
      }
    }
  }

  return atomicGtosVector;
}

void MoldenFileGenerator::generateAtomBlock(std::ostream& out) const {
  auto structure = *calculator_.getStructure();
  const auto& elements = structure.getElements();
  const auto& positions = structure.getPositions();
  out << "[Atoms] (AU)" << std::fixed << std::setprecision(10) << std::endl;
  for (int atom = 0; atom < structure.size(); ++atom) {
    const auto e = elements[atom];
    const auto& p = positions.row(atom);
    out << std::left << std::setw(5) << Utils::ElementInfo::symbol(e) << std::setw(10) << atom + 1 << std::setw(10)
        << Utils::ElementInfo::Z(e) << std::right << " " << std::setw(18) << p.x() << " " << std::setw(18) << p.y()
        << " " << std::setw(18) << p.z() << std::endl;
  }
}

void MoldenFileGenerator::generateGTOBlock(std::ostream& out) const {
  const auto& gtoExpansion = getStoNGExpansion();
  auto smallBasisMethod = calculator_.name() == "MNDO" || calculator_.name() == "AM1" || calculator_.name() == "PM3" ||
                          calculator_.name() == "RM1";
  out << std::right << "[GTO]" << std::endl;
  for (int atom = 0; atom < calculator_.getStructure()->size(); ++atom) {
    out << std::left << atom + 1 << " " << 0 << std::endl;
    if (gtoExpansion[atom].hasS()) {
      const auto& s = gtoExpansion[atom].s();
      out << std::left << std::setw(5) << "s" << std::setw(5) << s.size() << std::setw(5) << "1.00" << std::endl;
      for (int primitive = 0; primitive < s.size(); ++primitive) {
        out << std::right << std::setw(18) << s.getExponent(primitive) << " " << std::setw(18)
            << s.getCoefficient(primitive) << std::endl;
      }
    }
    if (gtoExpansion[atom].hasP()) {
      const auto& p = gtoExpansion[atom].p();
      out << std::left << std::setw(5) << "p" << std::setw(5) << p.size() << std::setw(5) << "1.00" << std::endl;
      for (int primitive = 0; primitive < p.size(); ++primitive) {
        out << std::right << std::setw(18) << p.getExponent(primitive) << " " << std::setw(18)
            << p.getCoefficient(primitive) << std::endl;
      }
    }
    if (gtoExpansion[atom].hasD() && !smallBasisMethod) {
      const auto& d = gtoExpansion[atom].d();
      out << std::left << std::setw(5) << "d" << std::setw(5) << d.size() << std::setw(5) << "1.00" << std::endl;
      for (int primitive = 0; primitive < d.size(); ++primitive) {
        out << std::right << std::setw(18) << d.getExponent(primitive) << " " << std::setw(18)
            << d.getCoefficient(primitive) << std::endl;
      }
    }
    out << std::endl;
  }
}

void MoldenFileGenerator::generateMolecularOrbitalsBlock(std::ostream& out) const {
  const auto& method = calculator_.getLcaoMethod();
  const auto& moEnergies = method.getSingleParticleEnergies();
  const auto& occupation = method.getElectronicOccupation();
  out << "[5D]" << std::endl;
  out << "[7F]" << std::endl;
  out << "[9G]" << std::endl;
  out << "[MO]" << std::endl;
  if (method.unrestrictedCalculationRunning()) {
    writeMOBlock(out, method.getMolecularOrbitals().alphaMatrix(), occupation.getFilledAlphaOrbitals(),
                 moEnergies.getAlphaEnergies(), "Alpha");
    writeMOBlock(out, method.getMolecularOrbitals().betaMatrix(), occupation.getFilledBetaOrbitals(),
                 moEnergies.getBetaEnergies(), "Beta");
  }
  else {
    writeMOBlock(out, method.getMolecularOrbitals().restrictedMatrix(), occupation.getFilledRestrictedOrbitals(),
                 moEnergies.getRestrictedEnergies(), "Alpha");
  }
}

void MoldenFileGenerator::writeMOBlock(std::ostream& out, Eigen::MatrixXd moMatrix, const std::vector<int>& filledOrb,
                                       const std::vector<double>& moEnergies, const std::string& spin) const {
  const auto& method = calculator_.getLcaoMethod();
  std::string occupancy = method.unrestrictedCalculationRunning() ? "1.0" : "2.0";
  for (int orb = 0; orb < moMatrix.cols(); ++orb) {
    const auto& mo = moMatrix.col(orb);
    bool isOccupied = std::find(filledOrb.begin(), filledOrb.end(), orb) != filledOrb.end();
    out << std::left << std::setw(10) << "Sym=" << std::setw(10) << "A1" << std::endl;
    out << std::left << std::setw(10) << "Ene=" << std::setw(10) << moEnergies[orb] << std::endl;
    out << std::left << std::setw(10) << "Spin=" << std::setw(10) << spin << std::endl;
    out << std::left << std::setw(10) << "Occup=" << std::setw(10) << (isOccupied ? occupancy : "0.0") << std::endl;
    for (int atom = 0; atom < method.getAtomsOrbitalsIndexesHolder().getNAtoms(); ++atom) {
      int firstAtom = method.getAtomsOrbitalsIndexesHolder().getFirstOrbitalIndex(atom);
      int nAOs = method.getAtomsOrbitalsIndexesHolder().getNOrbitals(atom);
      for (int ao = 0; ao < nAOs; ++ao) {
        out << std::right << std::setw(4) << firstAtom + ao + 1 << std::setw(18)
            << mo(firstAtom + indexMap_[static_cast<orb_t>(ao)]) << std::endl;
      }
    }
  }
}

} // namespace Sparrow
} // namespace Scine

max_stars_repo_name: DockBio/sparrow

max_issues_repo_name: DockBio/sparrow

max_forks_repo_name: DockBio/sparrow


text:
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
// Zen Enterprise Framework
//
// Copyright (C) 2001 - 2016 Raymond A. Richards
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
#ifndef ZEN_ENTERPRISE_I_NUMERIC_TYPE_MESSAGE_REGISTRY_HPP_INCLUDED
#define ZEN_ENTERPRISE_I_NUMERIC_TYPE_MESSAGE_REGISTRY_HPP_INCLUDED

#include "Configuration.hpp"

#include <memory>
#include <memory>

#include <Zen/Enterprise/I_MessageRegistry.hpp>
#include <Zen/Enterprise/I_MessageType.hpp>

#include <boost/cstdint.hpp>

//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
namespace Zen {
namespace Enterprise {
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~

/// Message Registry that uses I_MessageType with a numeric
/// uint32_t implementation.
class ENTERPRISE_DLL_LINK I_NumericTypeMessageRegistry
:   public I_MessageRegistry
{
    /// @name Types
    /// @{
public:
    typedef std::shared_ptr<I_MessageType>         pMessageType_type;
    typedef Zen::Memory::managed_weak_ptr<I_MessageType>    wpMessageType_type;
    typedef std::shared_ptr<I_MessageRegistry>     pMessageRegistry_type;
    /// @}

    /// @name I_NumericTypeMessageRegistry interface.
    /// @{
public:
    virtual pMessageType_type getMessageType(boost::uint64_t _type) = 0;
    /// @}

    /// @name Inner types
    /// @{
public:
    class I_NumericType
    :   public I_MessageType
    {
    public:
        virtual boost::uint64_t getType() const = 0;
    };  // class NumericType

    /// @}
    /// @name 'Structors
    /// @{
protected:
             I_NumericTypeMessageRegistry();
    virtual ~I_NumericTypeMessageRegistry();
    /// @}

};  // interface I_NumericTypeMessageRegistry

//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
}   // namespace Enterprise
}   // namespace Zen
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~

#endif // ZEN_ENTERPRISE_I_NUMERIC_TYPE_MESSAGE_REGISTRY_HPP_INCLUDED

ext: hpp

max_stars_repo_name: indie-zen/zen-enterprise

max_issues_repo_name: indie-zen/zen-enterprise

max_forks_repo_name: indie-zen/zen-enterprise


text:
//          Copyright Rein Halbersma 2014-2021.
// 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 <xstd/cstdlib.hpp>             // div, floored_div, euclidean_div
#include <boost/test/unit_test.hpp>     // BOOST_AUTO_TEST_SUITE, BOOST_AUTO_TEST_SUITE_END, BOOST_AUTO_TEST_CASE, BOOST_CHECK_EQUAL, BOOST_CHECK_EQUAL_COLLECTIONS
#include <algorithm>                    // transform
#include <array>                        // array
#include <cstdlib>                      // div, div_t
#include <iterator>                     // back_inserter
#include <sstream>                      // stringstream
#include <utility>                      // pair
#include <vector>                       // vector

BOOST_AUTO_TEST_SUITE(CStdLib)

BOOST_AUTO_TEST_CASE(Abs)
{
        BOOST_CHECK_EQUAL(xstd::abs(-2), 2);
        BOOST_CHECK_EQUAL(xstd::abs(-1), 1);
        BOOST_CHECK_EQUAL(xstd::abs( 0), 0);
        BOOST_CHECK_EQUAL(xstd::abs(+1), 1);
        BOOST_CHECK_EQUAL(xstd::abs(+2), 2);
}

BOOST_AUTO_TEST_CASE(Sign)
{
        BOOST_CHECK_EQUAL(xstd::sign(-2), -1);
        BOOST_CHECK_EQUAL(xstd::sign(-1), -1);
        BOOST_CHECK_EQUAL(xstd::sign( 0),  0);
        BOOST_CHECK_EQUAL(xstd::sign(+1), +1);
        BOOST_CHECK_EQUAL(xstd::sign(+2), +1);
}

// http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf

auto const input = std::array<std::pair<int, int>, 8>
{{
        {+8, +3}, {+8, -3}, {-8, +3}, {-8, -3},
        {+1, +2}, {+1, -2}, {-1, +2}, {-1, -2}
}};

BOOST_AUTO_TEST_CASE(StdDiv)
{
        auto const std_div = std::vector<xstd::div_t>
        {
                {+2, +2}, {-2, +2}, {-2, -2}, {+2, -2},
                { 0, +1}, { 0, +1}, { 0, -1}, { 0, -1}
        };

        std::vector<xstd::div_t> std_res;
        std::transform(input.begin(), input.end(), std::back_inserter(std_res), [](auto const& p)
                -> xstd::div_t {
                auto const d = std::div(p.first, p.second);
                return { d.quot, d.rem };
        });

        BOOST_CHECK_EQUAL_COLLECTIONS(
                std_res.begin(), std_res.end(),
                std_div.begin(), std_div.end()
        );
}

BOOST_AUTO_TEST_CASE(TruncatedDiv)
{
        auto const div = std::vector<xstd::div_t>
        {
                {+2, +2}, {-2, +2}, {-2, -2}, {+2, -2},
                { 0, +1}, { 0, +1}, { 0, -1}, { 0, -1}
        };

        std::vector<xstd::div_t> truncated_res;
        std::transform(input.begin(), input.end(), std::back_inserter(truncated_res), [](auto const& p) {
                return xstd::div(p.first, p.second);
        });

        BOOST_CHECK_EQUAL_COLLECTIONS(
                truncated_res.begin(), truncated_res.end(),
                div.begin(), div.end()
        );
}

BOOST_AUTO_TEST_CASE(EuclideanDiv)
{
        auto const euclidean_div = std::vector<xstd::div_t>
        {
                {+2, +2}, {-2, +2}, {-3, +1}, {+3, +1},
                { 0, +1}, { 0, +1}, {-1, +1}, {+1, +1}
        };

        std::vector<xstd::div_t> euclidean_res;
        std::transform(input.begin(), input.end(), std::back_inserter(euclidean_res), [](auto const& p) {
                return xstd::euclidean_div(p.first, p.second);
        });

        BOOST_CHECK_EQUAL_COLLECTIONS(
                euclidean_res.begin(), euclidean_res.end(),
                euclidean_div.begin(), euclidean_div.end()
        );
}

BOOST_AUTO_TEST_CASE(FlooredDiv)
{
        auto const floored_div = std::vector<xstd::div_t>
        {
                {+2, +2}, {-3, -1}, {-3, +1}, {+2, -2},
                { 0, +1}, {-1, -1}, {-1, +1}, { 0, -1}
        };

        std::vector<xstd::div_t> floored_res;
        std::transform(input.begin(), input.end(), std::back_inserter(floored_res), [](auto const& p) {
                return xstd::floored_div(p.first, p.second);
        });

        BOOST_CHECK_EQUAL_COLLECTIONS(
                floored_res.begin(), floored_res.end(),
                floored_div.begin(), floored_div.end()
        );
}

BOOST_AUTO_TEST_CASE(IOStreamsOperators)
{
        xstd::div_t const a { 1, 1 };
        xstd::div_t b;
        std::stringstream sstr;
        sstr << a;
        sstr >> b;
        BOOST_CHECK_EQUAL(a, b);
}

BOOST_AUTO_TEST_SUITE_END()

ext: cpp

max_stars_repo_name: rhalbersma/xstd

max_issues_repo_name: rhalbersma/xstd

max_forks_repo_name: rhalbersma/xstd


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 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

#include <boost/config.hpp>

// Boost.Test
#include <boost/test/minimal.hpp>

#include <boost/static_assert.hpp>
#include <boost/type_traits/is_same.hpp>

// Boost.Bimap
#include <boost/bimap/support/lambda.hpp>
#include <boost/bimap/bimap.hpp>
#include <boost/bimap/list_of.hpp>

// Support metafunctions
#include <boost/bimap/support/data_type_by.hpp>
#include <boost/bimap/support/key_type_by.hpp>
#include <boost/bimap/support/map_type_by.hpp>
#include <boost/bimap/support/value_type_by.hpp>
#include <boost/bimap/support/iterator_type_by.hpp>

using namespace boost::bimaps;
using namespace boost::bimaps::support;

typedef bimap<int, unconstrained_set_of<double> > bm_type;

namespace support_metafunctions_test {

    typedef boost::is_same
    <
        data_type_by< member_at::left , bm_type >::type,
        key_type_by < member_at::right, bm_type >::type

    >::type test_metafunction_1;
    BOOST_STATIC_ASSERT(test_metafunction_1::value);

    typedef boost::is_same
    <
        data_type_by< member_at::right, bm_type >::type,
        key_type_by < member_at::left , bm_type >::type

    >::type test_metafunction_2;
    BOOST_STATIC_ASSERT(test_metafunction_2::value);

    typedef boost::is_same
    <
        map_type_by  < member_at::left , bm_type >::type::value_type,
        value_type_by< member_at::left , bm_type >::type

    >::type test_metafunction_3;
    BOOST_STATIC_ASSERT(test_metafunction_3::value);

} // namespace support_metafunctions_test

void test_bimap_extra()
{
    // extra tests
    // ---------------------------------------------------------------
    // This section test small things... when a group of this checks
    // can be related it is moved to a separate unit test file.

}

int test_main( int, char* [] )
{
    test_bimap_extra();
    return 0;
}

max_stars_repo_name: randolphwong/mcsema

max_issues_repo_name: randolphwong/mcsema

max_forks_repo_name: randolphwong/mcsema


text:
#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK

#include <boost/test/unit_test.hpp>

#include <httpparser/httprequestparser.h>
#include <httpparser/request.h>

#include "common.h"

BOOST_AUTO_TEST_SUITE(Simple)

using httpparser::HttpRequestParser;
using httpparser::Request;

struct RequestFixture
{
    Request parse(const std::string& text)
    {
        Request request;
        HttpRequestParser parser;

        HttpRequestParser::ParseResult res = parser.parse(request, text.c_str(), text.c_str() + text.size());

        if (res != HttpRequestParser::ParsingCompleted)
            return Request();
        else
            return request;
    }
};

BOOST_FIXTURE_TEST_CASE(get_http_09, RequestFixture)
{
    const char* text = "GET /uri\r\n";
    Request result   = parse(text);
    Request should   = RequestDsl().method("GET").uri("/uri").version(0, 9);

    BOOST_CHECK_EQUAL(result.inspect(), should.inspect());
}

BOOST_FIXTURE_TEST_CASE(get_http_09_uri_with_query_string, RequestFixture)
{
    const char* text = "GET /uri?arg1=test;arg1=%20%21;arg3=test\r\n";
    Request result   = parse(text);
    Request should   = RequestDsl().method("GET").uri("/uri?arg1=test;arg1=%20%21;arg3=test").version(0, 9);

    BOOST_CHECK_EQUAL(result.inspect(), should.inspect());
}

BOOST_FIXTURE_TEST_CASE(get_http_10, RequestFixture)
{
    const char* text = "GET /uri HTTP/1.0\r\n\r\n";
    Request result   = parse(text);
    Request should   = RequestDsl().method("GET").uri("/uri").version(1, 0).keepAlive(false);

    BOOST_CHECK_EQUAL(result.inspect(), should.inspect());
}

BOOST_FIXTURE_TEST_CASE(get_http_11, RequestFixture)
{
    const char* text = "GET /uri HTTP/1.1\r\n\r\n";
    Request result   = parse(text);
    Request should   = RequestDsl().method("GET").uri("/uri").version(1, 1).keepAlive(true);

    BOOST_CHECK_EQUAL(result.inspect(), should.inspect());
}

BOOST_FIXTURE_TEST_CASE(post_http_11, RequestFixture)
{
    const char* text = "POST /uri HTTP/1.1\r\n\r\n";
    Request result   = parse(text);
    Request should   = RequestDsl().method("POST").uri("/uri").version(1, 1).keepAlive(true);

    BOOST_CHECK_EQUAL(result.inspect(), should.inspect());
}

BOOST_FIXTURE_TEST_CASE(post_http_11_with_header_field, RequestFixture)
{
    const char* text = "POST /uri HTTP/1.1\r\n"
                       "X-Custom-Header: header value\r\n"
                       "\r\n";
    Request result = parse(text);
    Request should =
        RequestDsl().method("POST").uri("/uri").version(1, 1).header("X-Custom-Header", "header value").keepAlive(true);

    BOOST_CHECK_EQUAL(result.inspect(), should.inspect());
}

BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: maywine/httpparser

max_issues_repo_name: maywine/httpparser

max_forks_repo_name: maywine/httpparser


text:
//  This file is distributed under the BSD 3-Clause License. See LICENSE for details.
#ifndef INOU_SAT_H_
#define INOU_SAT_H_

#include "core/options.hpp"
#include <boost/program_options.hpp>

class Sat_pass_pack {

public:
  // input related options
  std::string sat_lgdb;
  Sat_pass_pack(int argc, const char **argv);
};

#endif

max_stars_repo_name: zebramints/lgraph

max_issues_repo_name: zebramints/lgraph

max_forks_repo_name: zebramints/lgraph


text:
/*
Copyright (C) 2012 Mathias Eitz and Ronald Richter.
All rights reserved.

This file is part of the imdb library and is made available under
the terms of the BSD license (see the LICENSE file).
*/

#ifndef CMDLINE_HPP
#define CMDLINE_HPP

#include <string>
#include <vector>
#include <iostream>

#include <boost/lexical_cast.hpp>

namespace imdb
{

/**
 * @ingroup io
 * @brief Defines the structure of a short commandline option.
 *
 * In this implementation a short command line option starts with a
 * '-' and is then followed by exactly one alphabetic character, e.g. -t
 */
inline bool is_short_option(const std::string& s)
{
	std::locale loc;
    return (s.length() == 2 && s[0] == '-' && std::isalpha(s[1], loc));
}

/**
 * @ingroup io
 * @brief Defines the structure of a long commandline option.
 *
 * In this implementation a long command line option starts with '--'
 * and is then followed by at least one alphabetic character, followed
 * by arbitrary characters, e.g. --parameters
 */
inline bool is_long_option(const std::string& s)
{
	std::locale loc;
    return (s.length() > 2 && s[0] == '-' && s[1] == '-' && std::isalpha(s[2], loc));
}

std::vector<std::string> argv_to_strings(int argc, char* argv[])
{
    std::vector<std::string> strings;
    for (int i = 0; i < argc; i++) strings.push_back(argv[i]);
    return strings;
}

/**
 * @ingroup io
 * @brief Encodes a single commandline option including a brief description
 *
 * A CmdOption encodes a single commandline option that a user can provide to a command as well as the parameters that
 * are supported by this option.
 *
 * Here is an example: assume we have a command that supports a single option
 * '--sigma' which has a single floating point parameter, i.e. a use would use --sigma 0.5
 */
class CmdOption
{
    public:

    /**
     * @brief Constructs a command option, defined by a long and short option and a description
     * @param long_option Single word that describes the command option reasonably, e.g. sigma
     * @param short_option Typically a single letter, e.g. s
     * @param description One ore more sentences describing the command
     */
    CmdOption(const std::string& long_option, const std::string& short_option, const std::string& description)
        : _long_option(long_option)
        , _short_option(short_option)
        , _description(description)
    {}

    /**
     * @brief Parse a single parameter from the commandline option.
     *
     * If the user provided --sigma 0.5 you would use T=float and would get back the value 0.5
     */
    template <class T>
    bool parse_single(const std::vector<std::string>& args, T& value)
    {
        bool found = false;

        for (int i = 0; i < (int)args.size() - 1; i++)
        {
            if (match(args[i]))
            {
                if (is_short_option(args[i + 1]) || is_long_option(args[i + 1])) continue;

                try
                {
                    value = boost::lexical_cast<T>(args[i + 1]);
                    found = true;
                }
                catch (boost::bad_lexical_cast&)
                {
                    std::cerr << "bad parameter value: " << args[i + 1] << std::endl;
                }
            }
        }

        return found;
    }

    /**
     * @brief Parse multiple parameters from a commandline option.
     *
     * If the user provided --input file1.txt file2.txt you would use T=std::string and would get back a
     * vector<std::string> containing "file1.txt" and "file2.txt"
     */
    template <class T>
    bool parse_multiple(const std::vector<std::string>& args, std::vector<T>& values)
    {
        bool found = false;

        for (int i = 0; i < (int)args.size() - 1; i++)
        {
            if (match(args[i]))
            {
                for (size_t k = i+1; k < args.size(); k++ )
                {
                    if (is_short_option(args[k]) || is_long_option(args[k])) break;

                    try
                    {
                        values.push_back(boost::lexical_cast<T>(args[k]));
                        found = true;
                    }
                    catch (boost::bad_lexical_cast&)
                    {
                        std::cerr << "bad parameter value: " << args[k] << std::endl;
                    }
                }
            }
        }

        return found;
    }

    bool match(const std::string& arg)
    {
        return ((is_short_option(arg) && arg.compare(1, arg.length()-1, _short_option) == 0) ||
                (is_long_option(arg) && arg.compare(2, arg.length()-2, _long_option) == 0));
    }

    const std::string& long_option() const { return _long_option; }
    const std::string& short_option() const { return _short_option; }
    const std::string& description() const { return _description; }

    private:

    std::string _long_option;
    std::string _short_option;
    std::string _description;
};

/**
 * @ingroup io
 * @brief Base class for a commandline options parser.
 *
 * Derive from this class and add the desired CmdOption instances to define the behaviour of your
 * specific commandline parser. Derived classes need to implement run(). Inside run(), you extract
 * the commandline parameters as passed in by the user and pass those to your actual program.
 */
class Command
{
    public:

    Command(const std::string& usage = "") : _usage(usage)
    {}

    /**
     * @brief Add as many CmdOption instances as desired.
     */
    void add(const CmdOption& option)
    {
        _options.push_back(option);
    }

    /**
     * @brief Check for undefined commandline options passed in by the user.
     *
     * Undefined commandline options are those that are not supported by any of the CmdOption
     * instances add to this Command.
     */
    std::vector<std::string> check_for_unknown_option(const std::vector<std::string>& args)
    {
        std::vector<std::string> unknown;

        for (size_t i = 0; i < args.size(); i++)
        {
            if (!is_short_option(args[i]) && !is_long_option(args[i])) continue;

            bool found = false;

            for (size_t k = 0; k < _options.size() && !found; k++)
            {
                if (_options[k].match(args[i])) found = true;
            }

            if (!found) unknown.push_back(args[i]);
        }

        return unknown;
    }

    void warn_for_unknown_option(const std::vector<std::string>& args)
    {
        std::vector<std::string> unknown = check_for_unknown_option(args);
        for (size_t i = 0; i < unknown.size(); i++)
        {
            std::cerr << "WARNING: unknown option: " << unknown[i] << std::endl;
        }
    }

    void print() const
    {
        static const int c0 = 30;

        std::cout << _usage << std::endl;

        if (_options.size() > 0) std::cout << "options:" << std::endl;

        for (size_t i = 0; i < _options.size(); i++)
        {
            std::string line = "  --" + _options[i].long_option() + ", -" + _options[i].short_option();
            for (int k = line.length(); k < c0; k++) line += ' ';
            std::cout << line << _options[i].description() << std::endl;
        }
    }

    virtual bool run(const std::vector<std::string>& /*args*/)
    {
        return false;
    }

    private:

    std::vector<CmdOption> _options;
    std::string            _usage;
};

} // namespace imdb

#endif // CMDLINE_HPP


text:
#ifndef FILTERNOLOOPPLUGIN_HH
#define FILTERNOLOOPPLUGIN_HH

/*
 *  Copyright (c) CERN 2014
 *
 *  Licensed under the Apache License, Version 2.0
 *  See the LICENSE file for further information
 *
 */

/**
 * @file   filter_noloop_plugin.hh
 * @brief  Exotic filter plugin that avoid redirection loops
 * @author Devresse Adrien
 * @date   Feb 2014
 */

#include "PluginInterface.hh"
#include <boost/asio.hpp>
#include <boost/thread.hpp>

enum AddrType{
     AddrIPv4,
     AddrIpv6
};

struct HostAddr{

};

class FilterNoLoopPlugin : public FilterPlugin
{
public:
    FilterNoLoopPlugin( UgrConnector & c, std::vector<std::string> & parms);
    virtual ~FilterNoLoopPlugin();

    virtual int applyFilterOnReplicaList(UgrReplicaVec&replica, const UgrClientInfo &cli_info);

private:
};

#endif // FILTERNOLOOPPLUGIN_HH

ext: hh

max_stars_repo_name: indigo-dc/dynafed

max_issues_repo_name: indigo-dc/dynafed

max_forks_repo_name: indigo-dc/dynafed


text:
#include "sea_dsa/support/NameValues.hh"

#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"

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

using namespace llvm;

namespace sea_dsa {

char NameValues::ID = 0;

bool NameValues::runOnModule(Module &M) {
  bool change = false;
  for (Module::iterator FI = M.begin(), E = M.end(); FI != E; ++FI)
    change |= runOnFunction(*FI);
  return change;
}

bool NameValues::runOnFunction(Function &F) {
  bool change = false;

  // -- print to string
  std::string funcAsm;
  raw_string_ostream out(funcAsm);
  out << F;
  out.flush();

  typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
  boost::char_separator<char> nl_sep("\n");
  boost::char_separator<char> sp_sep(" :\t%@");

  tokenizer lines(funcAsm, nl_sep);
  tokenizer::iterator line_iter = lines.begin();

  // -- skip function attributes
  if (boost::starts_with(*line_iter, "; Function Attrs:"))
    ++line_iter;

  unsigned ArgIdx = 0;
  for (Argument &arg : F.args()) {
    ++ArgIdx;
    if (!arg.hasName())
      arg.setName("_arg" + std::to_string(ArgIdx));
  }

  // -- skip function definition line
  ++line_iter;

  for (Function::iterator BI = F.begin(), BE = F.end();
       BI != BE && line_iter != lines.end(); ++BI) {
    BasicBlock &BB = *BI;

    if (!BB.hasName()) {
      std::string bb_line = *line_iter;
      tokenizer names(bb_line, sp_sep);
      std::string bb_name = *names.begin();
      if (bb_name == ";")
        bb_name = "bb";
      BB.setName("_" + bb_name);
      change = true;
    }
    ++line_iter;

    for (BasicBlock::iterator II = BB.begin(), IE = BB.end();
         II != IE && line_iter != lines.end(); ++II) {
      Instruction &I = *II;
      if (!I.hasName() && !(I.getType()->isVoidTy())) {
        std::string inst_line = *line_iter;
        tokenizer names(inst_line, sp_sep);
        std::string inst_name = *names.begin();
        I.setName("_" + inst_name);
        change = true;
      }
      ++line_iter;
    }
  }
  return change;
}
} // namespace sea_dsa

static llvm::RegisterPass<sea_dsa::NameValues> X("seadsa-name-values",
                                                 "Names all unnamed values");

ext: cc

max_stars_repo_name: igcontreras/sea-dsa

max_issues_repo_name: igcontreras/sea-dsa

max_forks_repo_name: igcontreras/sea-dsa


text:
// SPDX-FileCopyrightText: 2018-2020 Smart Robotics Lab, Imperial College London
// SPDX-FileCopyrightText: 2016 Emanuele Vespa, ETH Zürich
// SPDX-FileCopyrightText: 2019-2020 Nils Funk, Imperial College London
// SPDX-License-Identifier: BSD-3-Clause

#ifndef __OCTREE_IO_HPP
#define __OCTREE_IO_HPP

#include <string>

#include <Eigen/Dense>

#include "se/octree.hpp"

namespace se {

  /**
   * \brief Save a 3D slice of the octree values as a VTK file.
   *
   * \param[in] octree        The octree to be sliced.
   * \param[in] filename      The output filename.
   * \param[in] lower_coord   The lower, left, front coordinates of the 3D slice.
   * \param[in] upper_coord   The upper, right, back coordinates of the 3D slice.
   * \param[in] select_value  lambda function selecting the node and voxel value from the octant data to be saved.
   * \param[in] min_scale     The minimum scale to select the data from.
   * \return 0 on success, nonzero on error.
   */
  template <typename VoxelT, typename ValueSelector>
  int save_3d_value_slice_vtk(const se::Octree<VoxelT>& octree,
                              const std::string         filename,
                              const Eigen::Vector3i&    lower_coord,
                              const Eigen::Vector3i&    upper_coord,
                              ValueSelector             select_value,
                              const int                 min_scale = 0);

  /**
   * \brief Save a 3D slice of the octree values as a VTK file.
   *
   * \param[in] octree             The octree to be sliced.
   * \param[in] filename           The output filename.
   * \param[in] lower_coord        The lower, left, front coordinates of the 3D slice.
   * \param[in] upper_coord        The upper, right, back coordinates of the 3D slice.
   * \param[in] select_node_value  Lambda function to select the node value to store in the slice.
   * \param[in] select_voxel_value Lambda function to select the voxel value to store in the slice.
   * \param[in] min_scale          The minimum scale to select the data from.
   * \return 0 on success, nonzero on error.
   */
  template <typename VoxelT, typename NodeValueSelector, typename VoxelValueSelector>
  int save_3d_value_slice_vtk(const se::Octree<VoxelT>& octree,
                              const std::string         filename,
                              const Eigen::Vector3i&    lower_coord,
                              const Eigen::Vector3i&    upper_coord,
                              NodeValueSelector         select_node_value,
                              VoxelValueSelector        select_voxel_value,
                              const int                 min_scale = 0);

  template <typename VoxelT>
  int save_3d_scale_slice_vtk(const se::Octree<VoxelT>& octree,
                              const std::string         filename,
                              const Eigen::Vector3i&    lower_coord,
                              const Eigen::Vector3i&    upper_coord,
                              const int                 min_scale = 0);

  template <typename ValueGetter>
  int save_3d_slice_vtk(const std::string         filename,
                        const Eigen::Vector3i&    lower_coord,
                        const Eigen::Vector3i&    upper_coord,
                        ValueGetter               get_value,
                        const int                 min_scale);

  /**
   * \brief Save the octree structure as a PLY file.
   *
   * Documentation for the PLY polygon file format available here
   * https://web.archive.org/web/20161204152348/http://www.dcs.ed.ac.uk/teaching/cs4/www/graphics/Web/ply.html.
   *
   * \note Aggregated voxel blocks are written as a single octant of size block_size^3.
   *
   * \param[in] octree   The octree providing the structure to be saved.
   * \param[in] filename The output filename.
   * \return 0 on success, nonzero on error.
   */
  template <typename VoxelT>
  int save_octree_structure_ply(const se::Octree<VoxelT>& octree,
                                const std::string         filename);

}

#include "octree_io_impl.hpp"

#endif // __OCTREE_IO_HPP

max_stars_repo_name: hexagon-geo-surv/supereight-public

max_issues_repo_name: hexagon-geo-surv/supereight-public

max_forks_repo_name: hexagon-geo-surv/supereight-public


text:
//  Unit test for boost::lexical_cast for https://svn.boost.org/trac/boost/ticket/11669.
//
//  See http://www.boost.org for most recent version, including documentation.
//
//  Copyright Antony Polukhin, 2015-2020.
//
//  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 <string>
#define BOOST_LCAST_NO_COMPILE_TIME_PRECISION
#include <boost/lexical_cast.hpp>

#include <boost/test/unit_test.hpp>

void main_check() {
    BOOST_CHECK(!boost::lexical_cast<std::string>(2.12345).empty());
    BOOST_CHECK(!boost::lexical_cast<std::string>(2.12345678).empty());
}

boost::unit_test::test_suite *init_unit_test_suite(int, char *[])
{
    boost::unit_test::test_suite *suite =
        BOOST_TEST_SUITE("lexical_cast no-compile-time-precision check");
    suite->add(BOOST_TEST_CASE(main_check));
    return suite;
}

max_issues_repo_name: qingkouwei/mediaones

max_forks_repo_name: qingkouwei/mediaones


text:
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE aros_reedsolomonfec
#include <boost/test/unit_test.hpp>

max_stars_repo_name: hrjonashansen/ccfec

max_issues_repo_name: hrjonashansen/ccfec

max_forks_repo_name: hrjonashansen/ccfec


text:
/*

Copyright (c) 2012, Arvid Norberg
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

*/

#include "libtorrent/file.hpp"
#include "libtorrent/io.hpp"
#include <cstring>
#include <boost/bind.hpp>
#include <stdlib.h>
#include <map>
#include <vector>

using namespace libtorrent;
using namespace libtorrent::detail; // for write_* and read_*

void print_usage()
{
	fprintf(stderr, "usage: parse_request_log log-file\n");
	exit(1);
}

// the event format in the log is:
// uint64_t timestamp (microseconds)
// uint64_t info-hash prefix
// uint32_t peer identifier
// uint32_t piece
// uint32_t start offset
// uint32_t length

struct request
{
	boost::uint64_t timestamp;
	boost::uint64_t infohash;
	boost::uint32_t peer;
	boost::uint32_t piece;
	boost::uint32_t start;
	boost::uint32_t length;
};

struct object_entry
{
	int hits;
	int cache_hits;
	bool operator<(object_entry const& rhs) const
	{
		return hits < rhs.hits;
	}
};

struct average_time
{
	average_time() : first_request(0), num_requests(0), last_peer(0) {}

	void sample(boost::uint32_t peer, boost::uint64_t timestamp)
	{
//		if (peer == last_peer) return;
//		last_peer = peer;
		++num_requests;
		if (first_request == 0)
		{
			first_request = timestamp;
			last_request = timestamp;
		}
		else
		{
			assert(timestamp >= last_request);
			last_request = timestamp;
		}
	}

	float request_rate() const
	{
		if (num_requests <= 8) return 0.f;
		return float(num_requests) / float(last_request - first_request) * 1000000.f;
	}
	boost::uint64_t first_request;
	boost::uint64_t last_request;
	int num_requests;
	boost::uint32_t last_peer;
};

struct cache
{
	virtual bool incoming_request(request const& r) = 0;
	virtual ~cache() {}
};

struct noop_cache : cache
{
	virtual bool incoming_request(request const& r) { return false; }
};

struct lru_cache : cache
{
	lru_cache(int size) : m_size(size) {}
	virtual bool incoming_request(request const& r)
	{
		boost::uint64_t piece = (r.infohash & 0xffffffff00000000LL) | r.piece;
		cache_t::iterator i = m_cache.find(piece);
		if (i != m_cache.end())
		{
			i->second = r.timestamp;
			return true;
		}

		// cache miss, insert this piece
		if (m_cache.size() == m_size)
		{
			// need to evict the least recently used. This is
			// a stupid expensive implementation (but simple)

			i = std::min_element(m_cache.begin(), m_cache.end()
				, boost::bind(&cache_t::value_type::second, _1)
				< boost::bind(&cache_t::value_type::second, _2));

			m_cache.erase(i);
		}
		m_cache[piece] = r.timestamp;
		return false;
	}
	int m_size;
	// maps piece -> timestamp
	typedef std::map<boost::uint64_t, boost::uint64_t> cache_t;
	cache_t m_cache;
};

int main(int argc, char* argv[])
{
	if (argc != 2 && argc != 4) print_usage();

	cache* disk_cache = 0;

	if (argc == 4)
	{
		int size = atoi(argv[2]);
		if (strcmp(argv[1], "lru") == 0)
		{
			disk_cache = new lru_cache(size);
		}

		argv += 2;
	}

	if (disk_cache == 0)
	{
		disk_cache = new noop_cache;
	}

	FILE* log_file = fopen(argv[1], "r");
	if (log_file == 0)
	{
		fprintf(stderr, "failed to open logfile: %s\n%d: %s\n"
			, argv[1], errno, strerror(errno));
		return 1;
	}

	FILE* expand_file = fopen("expanded_requests.log", "w+");
	std::map<boost::uint64_t, object_entry> torrent_map;
	std::map<boost::uint64_t, object_entry> piece_map;
	std::map<boost::uint64_t, average_time> piece_frequency_map;
	std::map<boost::uint32_t, object_entry> peer_map;

	boost::uint64_t first_timestamp = 0;

	for (;;)
	{
		char entry[sizeof(request)];
		char* ptr = entry;
		int ret = fread(&entry, 1, sizeof(entry), log_file);
		if (ret != sizeof(entry)) break;

		request r;
		r.timestamp = read_uint64(ptr);
		r.infohash = read_uint64(ptr);
		r.peer = read_uint32(ptr);
		r.piece = read_uint32(ptr);
		r.start = read_uint32(ptr);
		r.length = read_uint32(ptr);

		if (first_timestamp == 0) first_timestamp = r.timestamp;

		fprintf(expand_file, "%" PRIu64 "\t%" PRIu64 "\t%" PRIu32 "\t%" PRIu32 "\t%" PRIu32 "\t%" PRIu32 "\n"
			, r.timestamp, r.infohash, r.peer, r.piece, r.start, r.length);

		bool hit = disk_cache->incoming_request(r);

		boost::uint64_t piece = (r.infohash & 0xffffffff00000000LL) | r.piece;
		torrent_map[r.infohash].hits += 1;
		peer_map[r.peer].hits += 1;
		piece_map[piece].hits += 1;
		piece_frequency_map[piece].sample(r.peer, r.timestamp);
		if (hit)
		{
			torrent_map[r.infohash].cache_hits += 1;
			peer_map[r.peer].cache_hits += 1;
			piece_map[piece].cache_hits += 1;
		}
	}

	fclose(expand_file);

	// === torrents ===
	FILE* file = fopen("torrent_dist.log", "w+");
	std::vector<std::pair<object_entry, boost::uint64_t> > histogram;
	for (std::map<boost::uint64_t, object_entry>::iterator i = torrent_map.begin()
		, end(torrent_map.end()); i != end; ++i)
	{
		histogram.push_back(std::pair<object_entry, boost::uint64_t>(i->second, i->first));
	}
	std::sort(histogram.begin(), histogram.end());

	int count = 0;
	for (std::vector<std::pair<object_entry, boost::uint64_t> >::iterator i = histogram.begin()
		, end(histogram.end()); i != end; ++i, ++count)
	{
		fprintf(file, "%d\t%d\t%d\n", count, i->first.hits, i->first.cache_hits);
	}
	fclose(file);

	// === peers ===
	file = fopen("peer_dist.log", "w+");
	histogram.clear();
	for (std::map<boost::uint32_t, object_entry>::iterator i = peer_map.begin()
		, end(peer_map.end()); i != end; ++i)
	{
		histogram.push_back(std::pair<object_entry, boost::uint64_t>(i->second, i->first));
	}
	std::sort(histogram.begin(), histogram.end());

	count = 0;
	for (std::vector<std::pair<object_entry, boost::uint64_t> >::iterator i = histogram.begin()
		, end(histogram.end()); i != end; ++i, ++count)
	{
		fprintf(file, "%d\t%d\t%d\n", count, i->first.hits, i->first.cache_hits);
	}
	fclose(file);

	// === pieces ===
	file = fopen("piece_dist.log", "w+");
	histogram.clear();
	for (std::map<boost::uint64_t, object_entry>::iterator i = piece_map.begin()
		, end(piece_map.end()); i != end; ++i)
	{
		histogram.push_back(std::pair<object_entry, boost::uint64_t>(i->second, i->first));
	}
	std::sort(histogram.begin(), histogram.end());

	count = 0;
	for (std::vector<std::pair<object_entry, boost::uint64_t> >::iterator i = histogram.begin()
		, end(histogram.end()); i != end; ++i, ++count)
	{
		fprintf(file, "%d\t%d\t%d\n", count, i->first.hits, i->first.cache_hits);
	}
	fclose(file);

	// === piece frequency ===
	file = fopen("piece_frequency_dist.log", "w+");
	// just update the histogram to get the piece mapping the same

	count = 0;
	for (std::vector<std::pair<object_entry, boost::uint64_t> >::iterator i = histogram.begin()
		, end(histogram.end()); i != end; ++i, ++count)
	{
		fprintf(file, "%d\t%f\n", count, piece_frequency_map[i->second].request_rate());
	}
	fclose(file);

	FILE* gnuplot = fopen("requests.gnuplot", "w+");

	char const* gnuplot_file =
		"set term png size 1400,1024\n"
		"set output \"requests-torrent-histogram.png\"\n"
		"set xlabel \"torrent\"\n"
		"set ylabel \"number of requests\"\n"
		"plot \"torrent_dist.log\" using 1:2 title \"torrent request\" with boxes, "
		"\"torrent_dist.log\" using 1:3 title \"torrent cache hits\" with boxes\n"

		"set output \"requests-peer-histogram.png\"\n"
		"set xlabel \"peer\"\n"
		"set ylabel \"number of requests\"\n"
		"plot \"peer_dist.log\" using 1:2 title \"peer request\" with boxes, "
		"\"peer_dist.log\" using 1:3 title \"peer cache hits\" with boxes\n"

		"set output \"requests-piece-histogram.png\"\n"
		"set xlabel \"piece\"\n"
		"set ylabel \"number of requests\"\n"
		"plot \"piece_dist.log\" using 1:2 title \"piece requests\" with boxes, "
		"\"piece_dist.log\" using 1:3 title \"piece cache hits\" with boxes\n"

		"set output \"requests-piece-frequency-histogram.png\"\n"
		"set xlabel \"piece\"\n"
		"set ylabel \"average requests per second\"\n"
		"set yrange [0: 0.02]\n"
		"plot \"piece_frequency_dist.log\" using 1:2 title \"piece request frequency\" with boxes\n"
		;

	fwrite(gnuplot_file, strlen(gnuplot_file), 1, gnuplot);
	fclose(gnuplot);

	system("gnuplot requests.gnuplot");

	return 0;
}


text:
#ifndef LIB_FLAT_STATIC_MULTIMAP_HPP
#define LIB_FLAT_STATIC_MULTIMAP_HPP

#include "flat_multimap.hpp"
#include <boost/container/static_vector.hpp>

namespace fc
{

template<typename Key, typename Mapped, std::size_t N, typename... Args>
using static_multimap = flat_map<
    ::boost::container::static_vector<std::pair<Key, Mapped>, N>, Args...>;

}

#endif

max_stars_repo_name: JasonDictos/flat

max_issues_repo_name: JasonDictos/flat

max_forks_repo_name: JasonDictos/flat


text:
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

#include "kudu/rpc/reactor.h"

#include <arpa/inet.h>
#include <boost/intrusive/list.hpp>
#include <ev++.h>
#include <glog/logging.h>
#include <mutex>
#include <netinet/in.h>
#include <stdlib.h>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#include "kudu/gutil/ref_counted.h"
#include "kudu/gutil/stringprintf.h"
#include "kudu/rpc/connection.h"
#include "kudu/rpc/messenger.h"
#include "kudu/rpc/negotiation.h"
#include "kudu/rpc/rpc_controller.h"
#include "kudu/rpc/rpc_introspection.pb.h"
#include "kudu/rpc/sasl_client.h"
#include "kudu/rpc/sasl_server.h"
#include "kudu/rpc/transfer.h"
#include "kudu/util/countdown_latch.h"
#include "kudu/util/errno.h"
#include "kudu/util/flag_tags.h"
#include "kudu/util/monotime.h"
#include "kudu/util/thread.h"
#include "kudu/util/threadpool.h"
#include "kudu/util/thread_restrictions.h"
#include "kudu/util/trace.h"
#include "kudu/util/status.h"
#include "kudu/util/net/socket.h"

// When compiling on Mac OS X, use 'kqueue' instead of the default, 'select', for the event loop.
// Otherwise we run into problems because 'select' can't handle connections when more than 1024
// file descriptors are open by the process.
#if defined(__APPLE__)
static const int kDefaultLibEvFlags = ev::KQUEUE;
#else
static const int kDefaultLibEvFlags = ev::AUTO;
#endif

using std::string;
using std::shared_ptr;

DEFINE_int64(rpc_negotiation_timeout_ms, 3000,
             "Timeout for negotiating an RPC connection.");
TAG_FLAG(rpc_negotiation_timeout_ms, advanced);
TAG_FLAG(rpc_negotiation_timeout_ms, runtime);

namespace kudu {
namespace rpc {

namespace {
Status ShutdownError(bool aborted) {
  const char* msg = "reactor is shutting down";
  return aborted ?
      Status::Aborted(msg, "", ESHUTDOWN) :
      Status::ServiceUnavailable(msg, "", ESHUTDOWN);
}
} // anonymous namespace

ReactorThread::ReactorThread(Reactor *reactor, const MessengerBuilder &bld)
  : loop_(kDefaultLibEvFlags),
    cur_time_(MonoTime::Now()),
    last_unused_tcp_scan_(cur_time_),
    reactor_(reactor),
    connection_keepalive_time_(bld.connection_keepalive_time_),
    coarse_timer_granularity_(bld.coarse_timer_granularity_) {
}

Status ReactorThread::Init() {
  DCHECK(thread_.get() == nullptr) << "Already started";
  DVLOG(6) << "Called ReactorThread::Init()";
  // Register to get async notifications in our epoll loop.
  async_.set(loop_);
  async_.set<ReactorThread, &ReactorThread::AsyncHandler>(this);
  async_.start();

  // Register the timer watcher.
  // The timer is used for closing old TCP connections and applying
  // backpressure.
  timer_.set(loop_);
  timer_.set<ReactorThread, &ReactorThread::TimerHandler>(this); // NOLINT(*)
  timer_.start(coarse_timer_granularity_.ToSeconds(),
               coarse_timer_granularity_.ToSeconds());

  // Create Reactor thread.
  return kudu::Thread::Create("reactor", "rpc reactor", &ReactorThread::RunThread, this, &thread_);
}

void ReactorThread::Shutdown() {
  CHECK(reactor_->closing()) << "Should be called after setting closing_ flag";

  VLOG(1) << name() << ": shutting down Reactor thread.";
  WakeThread();
}

void ReactorThread::ShutdownInternal() {
  DCHECK(IsCurrentThread());

  // Tear down any outbound TCP connections.
  Status service_unavailable = ShutdownError(false);
  VLOG(1) << name() << ": tearing down outbound TCP connections...";
  for (auto c = client_conns_.begin(); c != client_conns_.end();
       c = client_conns_.begin()) {
    const scoped_refptr<Connection>& conn = (*c).second;
    VLOG(1) << name() << ": shutting down " << conn->ToString();
    conn->Shutdown(service_unavailable);
    client_conns_.erase(c);
  }

  // Tear down any inbound TCP connections.
  VLOG(1) << name() << ": tearing down inbound TCP connections...";
  for (const scoped_refptr<Connection>& conn : server_conns_) {
    VLOG(1) << name() << ": shutting down " << conn->ToString();
    conn->Shutdown(service_unavailable);
  }
  server_conns_.clear();

  // Abort any scheduled tasks.
  //
  // These won't be found in the ReactorThread's list of pending tasks
  // because they've been "run" (that is, they've been scheduled).
  Status aborted = ShutdownError(true); // aborted
  for (DelayedTask* task : scheduled_tasks_) {
    task->Abort(aborted); // should also free the task.
  }
  scheduled_tasks_.clear();
}

ReactorTask::ReactorTask() {
}
ReactorTask::~ReactorTask() {
}

Status ReactorThread::GetMetrics(ReactorMetrics *metrics) {
  DCHECK(IsCurrentThread());
  metrics->num_client_connections_ = client_conns_.size();
  metrics->num_server_connections_ = server_conns_.size();
  return Status::OK();
}

Status ReactorThread::DumpRunningRpcs(const DumpRunningRpcsRequestPB& req,
                                      DumpRunningRpcsResponsePB* resp) {
  DCHECK(IsCurrentThread());
  for (const scoped_refptr<Connection>& conn : server_conns_) {
    RETURN_NOT_OK(conn->DumpPB(req, resp->add_inbound_connections()));
  }
  for (const conn_map_t::value_type& entry : client_conns_) {
    Connection* conn = entry.second.get();
    RETURN_NOT_OK(conn->DumpPB(req, resp->add_outbound_connections()));
  }
  return Status::OK();
}

void ReactorThread::WakeThread() {
  async_.send();
}

// Handle async events.  These events are sent to the reactor by other
// threads that want to bring something to our attention, like the fact that
// we're shutting down, or the fact that there is a new outbound Transfer
// ready to send.
void ReactorThread::AsyncHandler(ev::async &watcher, int revents) {
  DCHECK(IsCurrentThread());

  if (PREDICT_FALSE(reactor_->closing())) {
    ShutdownInternal();
    loop_.break_loop(); // break the epoll loop and terminate the thread
    return;
  }

  boost::intrusive::list<ReactorTask> tasks;
  reactor_->DrainTaskQueue(&tasks);

  while (!tasks.empty()) {
    ReactorTask &task = tasks.front();
    tasks.pop_front();
    task.Run(this);
  }
}

void ReactorThread::RegisterConnection(const scoped_refptr<Connection>& conn) {
  DCHECK(IsCurrentThread());

  Status s = StartConnectionNegotiation(conn);
  if (!s.ok()) {
    LOG(ERROR) << "Server connection negotiation failed: " << s.ToString();
    DestroyConnection(conn.get(), s);
  }
  server_conns_.push_back(conn);
}

void ReactorThread::AssignOutboundCall(const shared_ptr<OutboundCall> &call) {
  DCHECK(IsCurrentThread());
  scoped_refptr<Connection> conn;

  Status s = FindOrStartConnection(call->conn_id(), &conn);
  if (PREDICT_FALSE(!s.ok())) {
    call->SetFailed(s);
    return;
  }

  conn->QueueOutboundCall(call);
}

//
// Handles timer events.  The periodic timer:
//
// 1. updates Reactor::cur_time_
// 2. every tcp_conn_timeo_ seconds, close down connections older than
//    tcp_conn_timeo_ seconds.
//
void ReactorThread::TimerHandler(ev::timer &watcher, int revents) {
  DCHECK(IsCurrentThread());
  if (EV_ERROR & revents) {
    LOG(WARNING) << "Reactor " << name() << " got an error in "
      "the timer handler.";
    return;
  }
  MonoTime now(MonoTime::Now());
  VLOG(4) << name() << ": timer tick at " << now.ToString();
  cur_time_ = now;

  ScanIdleConnections();
}

void ReactorThread::RegisterTimeout(ev::timer *watcher) {
  watcher->set(loop_);
}

void ReactorThread::ScanIdleConnections() {
  DCHECK(IsCurrentThread());
  // enforce TCP connection timeouts
  auto c = server_conns_.begin();
  auto c_end = server_conns_.end();
  uint64_t timed_out = 0;
  for (; c != c_end; ) {
    const scoped_refptr<Connection>& conn = *c;
    if (!conn->Idle()) {
      VLOG(3) << "Connection " << conn->ToString() << " not idle";
      ++c; // TODO: clean up this loop
      continue;
    }

    MonoDelta connection_delta(cur_time_ - conn->last_activity_time());
    if (connection_delta > connection_keepalive_time_) {
      conn->Shutdown(Status::NetworkError(
                       StringPrintf("connection timed out after %s seconds",
                                    connection_keepalive_time_.ToString().c_str())));
      VLOG(1) << "Timing out connection " << conn->ToString() << " - it has been idle for "
              << connection_delta.ToSeconds() << "s";
      server_conns_.erase(c++);
      ++timed_out;
    } else {
      ++c;
    }
  }

  // TODO: above only times out on the server side.
  // Clients may want to set their keepalive timeout as well.

  VLOG_IF(1, timed_out > 0) << name() << ": timed out " << timed_out << " TCP connections.";
}

const std::string &ReactorThread::name() const {
  return reactor_->name();
}

MonoTime ReactorThread::cur_time() const {
  return cur_time_;
}

Reactor *ReactorThread::reactor() {
  return reactor_;
}

bool ReactorThread::IsCurrentThread() const {
  return thread_.get() == kudu::Thread::current_thread();
}

void ReactorThread::RunThread() {
  ThreadRestrictions::SetWaitAllowed(false);
  ThreadRestrictions::SetIOAllowed(false);
  DVLOG(6) << "Calling ReactorThread::RunThread()...";
  loop_.run(0);
  VLOG(1) << name() << " thread exiting.";

  // No longer need the messenger. This causes the messenger to
  // get deleted when all the reactors exit.
  reactor_->messenger_.reset();
}

Status ReactorThread::FindOrStartConnection(const ConnectionId &conn_id,
                                            scoped_refptr<Connection>* conn) {
  DCHECK(IsCurrentThread());
  conn_map_t::const_iterator c = client_conns_.find(conn_id);
  if (c != client_conns_.end()) {
    *conn = (*c).second;
    return Status::OK();
  }

  // No connection to this remote. Need to create one.
  VLOG(2) << name() << " FindOrStartConnection: creating "
          << "new connection for " << conn_id.remote().ToString();

  // Create a new socket and start connecting to the remote.
  Socket sock;
  RETURN_NOT_OK(CreateClientSocket(&sock));
  bool connect_in_progress;
  RETURN_NOT_OK(StartConnect(&sock, conn_id.remote(), &connect_in_progress));

  // Register the new connection in our map.
  *conn = new Connection(this, conn_id.remote(), sock.Release(), Connection::CLIENT);
  (*conn)->set_user_credentials(conn_id.user_credentials());

  // Kick off blocking client connection negotiation.
  Status s = StartConnectionNegotiation(*conn);
  if (s.IsIllegalState()) {
    // Return a nicer error message to the user indicating -- if we just
    // forward the status we'd get something generic like "ThreadPool is closing".
    return Status::ServiceUnavailable("Client RPC Messenger shutting down");
  }
  // Propagate any other errors as-is.
  RETURN_NOT_OK_PREPEND(s, "Unable to start connection negotiation thread");

  // Insert into the client connection map to avoid duplicate connection requests.
  client_conns_.insert(conn_map_t::value_type(conn_id, *conn));
  return Status::OK();
}

Status ReactorThread::StartConnectionNegotiation(const scoped_refptr<Connection>& conn) {
  DCHECK(IsCurrentThread());

  // Set a limit on how long the server will negotiate with a new client.
  MonoTime deadline = MonoTime::Now() +
      MonoDelta::FromMilliseconds(FLAGS_rpc_negotiation_timeout_ms);

  scoped_refptr<Trace> trace(new Trace());
  ADOPT_TRACE(trace.get());
  TRACE("Submitting negotiation task for $0", conn->ToString());
  RETURN_NOT_OK(reactor()->messenger()->negotiation_pool()->SubmitClosure(
      Bind(&Negotiation::RunNegotiation, conn, deadline)));
  return Status::OK();
}

void ReactorThread::CompleteConnectionNegotiation(const scoped_refptr<Connection>& conn,
      const Status &status) {
  DCHECK(IsCurrentThread());
  if (PREDICT_FALSE(!status.ok())) {
    DestroyConnection(conn.get(), status);
    return;
  }

  // Switch the socket back to non-blocking mode after negotiation.
  Status s = conn->SetNonBlocking(true);
  if (PREDICT_FALSE(!s.ok())) {
    LOG(DFATAL) << "Unable to set connection to non-blocking mode: " << s.ToString();
    DestroyConnection(conn.get(), s);
    return;
  }
  conn->MarkNegotiationComplete();
  conn->EpollRegister(loop_);
}

Status ReactorThread::CreateClientSocket(Socket *sock) {
  Status ret = sock->Init(Socket::FLAG_NONBLOCKING);
  if (ret.ok()) {
    ret = sock->SetNoDelay(true);
  }
  LOG_IF(WARNING, !ret.ok()) << "failed to create an "
    "outbound connection because a new socket could not "
    "be created: " << ret.ToString();
  return ret;
}

Status ReactorThread::StartConnect(Socket *sock, const Sockaddr &remote, bool *in_progress) {
  Status ret = sock->Connect(remote);
  if (ret.ok()) {
    VLOG(3) << "StartConnect: connect finished immediately for " << remote.ToString();
    *in_progress = false; // connect() finished immediately.
    return ret;
  }

  int posix_code = ret.posix_code();
  if (Socket::IsTemporarySocketError(posix_code) || (posix_code == EINPROGRESS)) {
    // The connect operation is in progress.
    *in_progress = true;
    VLOG(3) << "StartConnect: connect in progress for " << remote.ToString();
    return Status::OK();
  } else {
    LOG(WARNING) << "failed to create an outbound connection to " << remote.ToString()
                 << " because connect failed: " << ret.ToString();
    return ret;
  }
}

void ReactorThread::DestroyConnection(Connection *conn,
                                      const Status &conn_status) {
  DCHECK(IsCurrentThread());

  conn->Shutdown(conn_status);

  // Unlink connection from lists.
  if (conn->direction() == Connection::CLIENT) {
    ConnectionId conn_id(conn->remote(), conn->user_credentials());
    auto it = client_conns_.find(conn_id);
    CHECK(it != client_conns_.end()) << "Couldn't find connection " << conn->ToString();
    client_conns_.erase(it);
  } else if (conn->direction() == Connection::SERVER) {
    auto it = server_conns_.begin();
    while (it != server_conns_.end()) {
      if ((*it).get() == conn) {
        server_conns_.erase(it);
        break;
      }
      ++it;
    }
  }
}

DelayedTask::DelayedTask(boost::function<void(const Status &)> func,
                         MonoDelta when)
    : func_(std::move(func)), when_(std::move(when)), thread_(nullptr) {}

void DelayedTask::Run(ReactorThread* thread) {
  DCHECK(thread_ == nullptr) << "Task has already been scheduled";
  DCHECK(thread->IsCurrentThread());

  // Schedule the task to run later.
  thread_ = thread;
  timer_.set(thread->loop_);
  timer_.set<DelayedTask, &DelayedTask::TimerHandler>(this);
  timer_.start(when_.ToSeconds(), // after
               0);                // repeat
  thread_->scheduled_tasks_.insert(this);
}

void DelayedTask::Abort(const Status& abort_status) {
  func_(abort_status);
  delete this;
}

void DelayedTask::TimerHandler(ev::timer& watcher, int revents) {
  // We will free this task's memory.
  thread_->scheduled_tasks_.erase(this);

  if (EV_ERROR & revents) {
    string msg = "Delayed task got an error in its timer handler";
    LOG(WARNING) << msg;
    Abort(Status::Aborted(msg)); // Will delete 'this'.
  } else {
    func_(Status::OK());
    delete this;
  }
}

Reactor::Reactor(const shared_ptr<Messenger>& messenger,
                 int index, const MessengerBuilder &bld)
  : messenger_(messenger),
    name_(StringPrintf("%s_R%03d", messenger->name().c_str(), index)),
    closing_(false),
    thread_(this, bld) {
}

Status Reactor::Init() {
  DVLOG(6) << "Called Reactor::Init()";
  return thread_.Init();
}

void Reactor::Shutdown() {
  {
    std::lock_guard<LockType> l(lock_);
    if (closing_) {
      return;
    }
    closing_ = true;
  }

  thread_.Shutdown();

  // Abort all pending tasks. No new tasks can get scheduled after this
  // because ScheduleReactorTask() tests the closing_ flag set above.
  Status aborted = ShutdownError(true);
  while (!pending_tasks_.empty()) {
    ReactorTask& task = pending_tasks_.front();
    pending_tasks_.pop_front();
    task.Abort(aborted);
  }
}

Reactor::~Reactor() {
  Shutdown();
}

const std::string &Reactor::name() const {
  return name_;
}

bool Reactor::closing() const {
  std::lock_guard<LockType> l(lock_);
  return closing_;
}

// Task to call an arbitrary function within the reactor thread.
class RunFunctionTask : public ReactorTask {
 public:
  explicit RunFunctionTask(boost::function<Status()> f)
      : function_(std::move(f)), latch_(1) {}

  virtual void Run(ReactorThread *reactor) OVERRIDE {
    status_ = function_();
    latch_.CountDown();
  }
  virtual void Abort(const Status &status) OVERRIDE {
    status_ = status;
    latch_.CountDown();
  }

  // Wait until the function has completed, and return the Status
  // returned by the function.
  Status Wait() {
    latch_.Wait();
    return status_;
  }

 private:
  boost::function<Status()> function_;
  Status status_;
  CountDownLatch latch_;
};

Status Reactor::GetMetrics(ReactorMetrics *metrics) {
  return RunOnReactorThread(boost::bind(&ReactorThread::GetMetrics, &thread_, metrics));
}

Status Reactor::RunOnReactorThread(const boost::function<Status()>& f) {
  RunFunctionTask task(f);
  ScheduleReactorTask(&task);
  return task.Wait();
}

Status Reactor::DumpRunningRpcs(const DumpRunningRpcsRequestPB& req,
                                DumpRunningRpcsResponsePB* resp) {
  return RunOnReactorThread(boost::bind(&ReactorThread::DumpRunningRpcs, &thread_,
                                        boost::ref(req), resp));
}

class RegisterConnectionTask : public ReactorTask {
 public:
  explicit RegisterConnectionTask(const scoped_refptr<Connection>& conn) :
    conn_(conn)
  {}

  virtual void Run(ReactorThread *thread) OVERRIDE {
    thread->RegisterConnection(conn_);
    delete this;
  }

  virtual void Abort(const Status &status) OVERRIDE {
    // We don't need to Shutdown the connection since it was never registered.
    // This is only used for inbound connections, and inbound connections will
    // never have any calls added to them until they've been registered.
    delete this;
  }

 private:
  scoped_refptr<Connection> conn_;
};

void Reactor::RegisterInboundSocket(Socket *socket, const Sockaddr &remote) {
  VLOG(3) << name_ << ": new inbound connection to " << remote.ToString();
  scoped_refptr<Connection> conn(
    new Connection(&thread_, remote, socket->Release(), Connection::SERVER));
  auto task = new RegisterConnectionTask(conn);
  ScheduleReactorTask(task);
}

// Task which runs in the reactor thread to assign an outbound call
// to a connection.
class AssignOutboundCallTask : public ReactorTask {
 public:
  explicit AssignOutboundCallTask(shared_ptr<OutboundCall> call)
      : call_(std::move(call)) {}

  virtual void Run(ReactorThread *reactor) OVERRIDE {
    reactor->AssignOutboundCall(call_);
    delete this;
  }

  virtual void Abort(const Status &status) OVERRIDE {
    call_->SetFailed(status);
    delete this;
  }

 private:
  shared_ptr<OutboundCall> call_;
};

void Reactor::QueueOutboundCall(const shared_ptr<OutboundCall> &call) {
  DVLOG(3) << name_ << ": queueing outbound call "
           << call->ToString() << " to remote " << call->conn_id().remote().ToString();
  AssignOutboundCallTask *task = new AssignOutboundCallTask(call);
  ScheduleReactorTask(task);
}

void Reactor::ScheduleReactorTask(ReactorTask *task) {
  {
    std::unique_lock<LockType> l(lock_);
    if (closing_) {
      // We guarantee the reactor lock is not taken when calling Abort().
      l.unlock();
      task->Abort(ShutdownError(false));
      return;
    }
    pending_tasks_.push_back(*task);
  }
  thread_.WakeThread();
}

bool Reactor::DrainTaskQueue(boost::intrusive::list<ReactorTask> *tasks) { // NOLINT(*)
  std::lock_guard<LockType> l(lock_);
  if (closing_) {
    return false;
  }
  tasks->swap(pending_tasks_);
  return true;
}

} // namespace rpc
} // namespace kudu

max_stars_repo_name: sarahjelinek/kudu

max_issues_repo_name: sarahjelinek/kudu

max_forks_repo_name: sarahjelinek/kudu


text:
//
// Created by Daniel Schnell on 19.05.20.
//

#include "RewriteTesterUtils.h"
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/locale.hpp>
#include <fst/arc.h>
#include <fst/flags.h>
#include <fst/fst.h>
#include <fst/string.h>
#include <fst/symbol-table.h>
#include <fst/vector-fst.h>
#include <thrax/grm-manager.h>
#include <thrax/algo/paths.h>
#include <thrax/symbols.h>

using ::fst::StdArc;
using ::fst::StdVectorFst;
using ::fst::StringCompiler;
using ::fst::StringTokenType;
using ::fst::SymbolTable;
using ::thrax::FstToStrings;
using ::thrax::GetGeneratedSymbolTable;
using ::thrax::RuleTriple;

#define HISTORY_FILE ".rewrite-tester-history"

DEFINE_string(far, "g2p.far", "Path to the FAR.");
DEFINE_string(rules, "G2P", "Names of the rewrite rules.");
DEFINE_string(input_mode, "utf8", "Either \"byte\", \"utf8\", or the path to a "
                                  "symbol table for input parsing.");
DEFINE_string(output_mode, "utf8", "Either \"byte\", \"utf8\", or the path to "
                                   "a symbol table for input parsing.");
DEFINE_string(history_file, HISTORY_FILE,
              "Location of history file");
DEFINE_int64(noutput, 1, "Maximum number of output strings for each input.");
DEFINE_bool(show_details, false, "Show the output of each individual rule when"
                                 " multiple rules are specified.");
DEFINE_string(field_separator, " ",
              "Field separator for strings of symbols from a symbol table.");

DEFINE_string(word_file, "",
              "File with newline separated fields that should be used for input");

namespace thrax {
    namespace {

        using ::fst::kNoStateId;
        using ::fst::LabelsToUTF8String;
        using ::fst::PathIterator;
        using ::fst::Project;
        using ::fst::PROJECT_OUTPUT;
        using ::fst::RmEpsilon;
        using ::fst::ShortestPath;
        using ::fst::StdArc;
        using ::fst::StdVectorFst;
        using ::fst::SymbolTable;

        using Label = StdArc::Label;

        inline bool AppendLabel(Label label, TokenType type,
                                const SymbolTable *generated_symtab,
                                SymbolTable *symtab, std::string *path) {
            if (label != 0) {
                // Check first to see if this label is in the generated symbol set. Note
                // that this should not conflict with a user-provided symbol table since
                // the parser used by GrmCompiler doesn't generate extra labels if a
                // string is parsed using a user-provided symbol table.
                if (generated_symtab && !generated_symtab->Find(label).empty()) {
                    const auto &sym = generated_symtab->Find(label);
                    *path += "[" + sym + "]";
                } else if (type == SYMBOL) {
                    const auto &sym = symtab->Find(label);
                    if (sym.empty()) {
                        LOG(ERROR) << "Missing symbol in symbol table for id: " << label;
                        return false;
                    }
                    // For non-byte, non-UTF8 symbols, one overwhelmingly wants these to be
                    // space-separated.
                    if (!path->empty()) *path += FLAGS_field_separator;
                    *path += sym;
                } else if (type == BYTE) {
                    path->push_back(label);
                } else if (type == UTF8) {
                    std::string utf8_string;
                    std::vector<Label> labels;
                    labels.push_back(label);
                    if (!LabelsToUTF8String(labels, &utf8_string)) {
                        LOG(ERROR) << "LabelsToUTF8String: Bad code point: " << label;
                        return false;
                    }
                    *path += utf8_string;
                }
            }
            return true;
        }

    }  // namespace

    bool FstToStrings(const StdVectorFst &fst,
                      std::vector<std::pair<std::string, float>> *strings,
                      const SymbolTable *generated_symtab, TokenType type,
                      SymbolTable *symtab, size_t n) {
        StdVectorFst shortest_path;
        if (n == 1) {
            ShortestPath(fst, &shortest_path, n);
        } else {
            // The uniqueness feature of ShortestPath requires us to have an acceptor,
            // so we project and remove epsilon arcs.
            StdVectorFst temp(fst);
            Project(&temp, PROJECT_OUTPUT);
            RmEpsilon(&temp);
            ShortestPath(temp, &shortest_path, n, /*unique=*/true);
        }
        if (shortest_path.Start() == kNoStateId) return false;
        for (PathIterator<StdArc> iter(shortest_path, /*check_acyclic=*/false);
             !iter.Done(); iter.Next()) {
            std::string path;
            for (const auto label : iter.OLabels()) {
                if (!AppendLabel(label, type, generated_symtab, symtab, &path)) {
                    return false;
                }
            }
            strings->emplace_back(std::move(path), iter.Weight().Value());
        }
        return true;
    }

    const SymbolTable *GetGeneratedSymbolTable(GrmManagerSpec<StdArc> *grm) {
        const auto *symbolfst = grm->GetFst("*StringFstSymbolTable");
        return symbolfst ? symbolfst->InputSymbols()->Copy() : nullptr;
    }

}  // namespace thrax

RewriteTesterUtils::RewriteTesterUtils() :
        compiler_(nullptr),
        byte_symtab_(nullptr),
        utf8_symtab_(nullptr),
        input_symtab_(nullptr),
        output_symtab_(nullptr)
{
    if (!FLAGS_word_file.empty())
    {
        namespace FS=boost::filesystem;
        if (!FS::exists(FLAGS_word_file))
        {
            auto errorMsg = std::string("No such file: ") + FLAGS_word_file;
            throw std::runtime_error(errorMsg);
        }
    }
}

RewriteTesterUtils::~RewriteTesterUtils() {
    delete compiler_;
    delete input_symtab_;
    delete output_symtab_;
    delete byte_symtab_;
    delete utf8_symtab_;
}

void RewriteTesterUtils::Initialize() {
    boost::locale::generator gen;
    std::locale::global(gen("is_IS.UTF-8"));

    CHECK(grm_.LoadArchive(FLAGS_far));
    rules_ = ::fst::StringSplit(FLAGS_rules, ',');
    byte_symtab_ = nullptr;
    utf8_symtab_ = nullptr;
    if (rules_.empty()) LOG(FATAL) << "--rules must be specified";
    for (size_t i = 0; i < rules_.size(); ++i) {
        RuleTriple triple(rules_[i]);
        const auto *fst = grm_.GetFst(triple.main_rule);
        if (!fst) {
            LOG(FATAL) << "grm.GetFst() must be non nullptr for rule: "
                       << triple.main_rule;
        }
        StdVectorFst vfst(*fst);
        // If the input transducers in the FAR have symbol tables then we need to
        // add the appropriate symbol table(s) to the input strings, according to
        // the parse mode.
        if (vfst.InputSymbols()) {
            if (!byte_symtab_ &&
                vfst.InputSymbols()->Name() ==
                ::thrax::function::kByteSymbolTableName) {
                byte_symtab_ = vfst.InputSymbols()->Copy();
            } else if (!utf8_symtab_ &&
                       vfst.InputSymbols()->Name() ==
                       ::thrax::function::kUtf8SymbolTableName) {
                utf8_symtab_ = vfst.InputSymbols()->Copy();
            }
        }
        if (!triple.pdt_parens_rule.empty()) {
            fst = grm_.GetFst(triple.pdt_parens_rule);
            if (!fst) {
                LOG(FATAL) << "grm.GetFst() must be non nullptr for rule: "
                           << triple.pdt_parens_rule;
            }
        }
        if (!triple.mpdt_assignments_rule.empty()) {
            fst = grm_.GetFst(triple.mpdt_assignments_rule);
            if (!fst) {
                LOG(FATAL) << "grm.GetFst() must be non nullptr for rule: "
                           << triple.mpdt_assignments_rule;
            }
        }
    }
    generated_symtab_ = GetGeneratedSymbolTable(&grm_);
    if (FLAGS_input_mode == "byte") {
        compiler_ = new StringCompiler<StdArc>(StringTokenType::BYTE);
    } else if (FLAGS_input_mode == "utf8") {
        compiler_ = new StringCompiler<StdArc>(StringTokenType::UTF8);
    } else {
        input_symtab_ = SymbolTable::ReadText(FLAGS_input_mode);
        if (!input_symtab_) {
            LOG(FATAL) << "Invalid mode or symbol table path.";
        }
        compiler_ =
                new StringCompiler<StdArc>(StringTokenType::SYMBOL, input_symtab_);
    }
    output_symtab_ = nullptr;
    if (FLAGS_output_mode == "byte") {
        type_ = BYTE;
    } else if (FLAGS_output_mode == "utf8") {
        type_ = UTF8;
    } else {
        type_ = SYMBOL;
        output_symtab_ = SymbolTable::ReadText(FLAGS_output_mode);
        if (!output_symtab_) {
            LOG(FATAL) << "Invalid mode or symbol table path.";
        }
    }
}

const std::string RewriteTesterUtils::ProcessInput(const std::string& input,
                                                   bool prepend_output) {
    StdVectorFst input_fst;
    StdVectorFst output_fst;
    if (!compiler_->operator()(input, &input_fst)) {
        return "Unable to parse input string.";
    }
    std::ostringstream sstrm;
    // Set symbols for the input, if appropriate
    if (byte_symtab_ && type_ == BYTE) {
        input_fst.SetInputSymbols(byte_symtab_);
        input_fst.SetOutputSymbols(byte_symtab_);
    } else if (utf8_symtab_ && type_ == UTF8) {
        input_fst.SetInputSymbols(utf8_symtab_);
        input_fst.SetOutputSymbols(utf8_symtab_);
    } else if (input_symtab_ && type_ == SYMBOL) {
        input_fst.SetInputSymbols(input_symtab_);
        input_fst.SetOutputSymbols(input_symtab_);
    }
    bool succeeded = true;
    for (size_t i = 0; i < rules_.size(); ++i) {
        RuleTriple triple(rules_[i]);
        if (grm_.Rewrite(triple.main_rule, input_fst, &output_fst,
                         triple.pdt_parens_rule, triple.mpdt_assignments_rule)) {
            if (FLAGS_show_details && rules_.size() > 1) {
                std::vector<std::pair<std::string, float>> tmp;
                FstToStrings(output_fst, &tmp, generated_symtab_, type_,
                             output_symtab_, FLAGS_noutput);
                for (const auto& one_result : tmp) {
                    sstrm << "output of rule[" << triple.main_rule
                          << "] is: " << one_result.first << '\n';
                }
            }
            input_fst = output_fst;
        } else {
            succeeded = false;
            break;
        }
    }
    std::vector<std::pair<std::string, float>> strings;
    std::set<std::string> seen;
    if (succeeded && FstToStrings(output_fst, &strings,
                                  generated_symtab_, type_,
                                  output_symtab_, FLAGS_noutput)) {
        for (auto it = strings.cbegin(); it != strings.cend(); ++it) {
            const auto sx = seen.find(it->first);
            if (sx != seen.end()) continue;
            if (prepend_output) {
                sstrm << "Output string: " << it->first;
            } else {
                sstrm << it->first;
            }
            if (FLAGS_noutput != 1 && it->second != 0) {
                sstrm << " <cost=" << it->second << '>';
            }
            seen.insert(it->first);
            if (it + 1 != strings.cend()) sstrm << '\n';
        }
        return sstrm.str();
    } else {
        return "Rewrite failed.";
    }
}

// Run() for interactive/file based mode.
void RewriteTesterUtils::Run()
{
    // if input word file has been given, we are not using interactive mode,
    // but each line of the word_file corresponds to a word
    if (!FLAGS_word_file.empty())
    {
        processFile(FLAGS_word_file);
    }
    else
    {
        std::string input;
        while (ReadInput(&input))
            std::cout << ProcessInput(boost::locale::to_lower(input)) << std::endl;
    }
}

void RewriteTesterUtils::processFile(const std::string& filename)
{
    std::ifstream file(filename);
    std::string word;
    while(std::getline(file, word))
    {
        std::cout << word << "\t" << processWord(word) << std::endl;
    }
}

std::string RewriteTesterUtils::processWord(const std::string &word)
{
    // Words need to be in lowercase, so that our model size doesn't explode. Icelandic words are in UTF-8
    // encoding, accordingly std::tolower would not help, therefore use boost::locale
    auto processedWord = ProcessInput(boost::locale::to_lower(word), false);
    boost::trim(processedWord);
    return processedWord;
}

bool RewriteTesterUtils::ReadInput(std::string* s)
{
    std::cout << "Input string: ";
    return static_cast<bool>(getline(std::cin, *s));
}


text:
#pragma once

#include <Geometry2d/Point.hpp>
#include <boost/optional.hpp>
#include "planning/MotionInstant.hpp"
#include "Utils.hpp"

namespace Planning {

/*
 * This is a superclass for different MotionCommands.
 * Currently implemented are PathTarget, WorldVel, Pivot, DirectPathtarget, None
 */
class MotionCommand {
public:
    enum CommandType { PathTarget, WorldVel, Pivot, DirectPathTarget, None };
    virtual ~MotionCommand() = default;
    CommandType getCommandType() const { return commandType; }
    virtual std::unique_ptr<Planning::MotionCommand> clone() const = 0;

protected:
    MotionCommand(const MotionCommand& that) = default;
    MotionCommand(CommandType command) : commandType(command) {}

private:
    // The type of command
    const CommandType commandType;
};

struct EmptyCommand : public MotionCommand {
    EmptyCommand() : MotionCommand(MotionCommand::None){};

    virtual std::unique_ptr<Planning::MotionCommand> clone() const override {
        return std::make_unique<EmptyCommand>();
    }
};

struct PathTargetCommand : public MotionCommand {
    virtual std::unique_ptr<Planning::MotionCommand> clone() const override {
        return std::make_unique<PathTargetCommand>(*this);
    }
    explicit PathTargetCommand(const MotionInstant& goal)
        : MotionCommand(MotionCommand::PathTarget), pathGoal(goal){};
    MotionInstant pathGoal;
};

struct WorldVelTargetCommand : public MotionCommand {
    explicit WorldVelTargetCommand(Geometry2d::Point vel)
        : MotionCommand(MotionCommand::WorldVel), worldVel(vel){};
    virtual std::unique_ptr<Planning::MotionCommand> clone() const override {
        return std::make_unique<WorldVelTargetCommand>(*this);
    }
    Geometry2d::Point worldVel;
};
struct PivotCommand : public MotionCommand {
    explicit PivotCommand(Geometry2d::Point pivotPoint,
                          Geometry2d::Point target, float radius)
        : MotionCommand(MotionCommand::Pivot),
          pivotPoint(pivotPoint),
          pivotTarget(target),
          radius(radius) {}

    virtual std::unique_ptr<Planning::MotionCommand> clone() const override {
        return std::make_unique<PivotCommand>(*this);
    }

    Geometry2d::Point pivotTarget;
    Geometry2d::Point pivotPoint;
    float radius;
};

struct DirectPathTargetCommand : public MotionCommand {
    virtual std::unique_ptr<Planning::MotionCommand> clone() const override {
        return std::make_unique<DirectPathTargetCommand>(*this);
    }
    explicit DirectPathTargetCommand(const MotionInstant& goal)
        : MotionCommand(MotionCommand::DirectPathTarget), pathGoal(goal){};
    MotionInstant pathGoal;
};
}  // namespace Planning

max_stars_repo_name: justbuchanan/robocup-software

max_issues_repo_name: justbuchanan/robocup-software

max_forks_repo_name: justbuchanan/robocup-software


text:
#include <iostream>
#include <fstream>
#include <string>
#include <Eigen/Dense>
#include <vector>
#include <type_traits>

using namespace std;
using namespace Eigen;

MatrixXd open(string fileToOpen)
{
    vector<double> MtxEntries;
    ifstream MtxDataFile(fileToOpen);
    string MtxRowString;
    string MtxEntry;

    int MtxRowNumber = 0;

    while (getline(MtxDataFile, MtxRowString))
    {
        stringstream MtxRowStringStream(MtxRowString);

        while (getline(MtxRowStringStream, MtxEntry, ','))
        {
            MtxEntries.push_back(stod(MtxEntry));
        }
        MtxRowNumber++;
    }

    return Map<Matrix<double, Dynamic, Dynamic, RowMajor>>(matrixEntries.data(), matrixRowNumber, matrixEntries.size() / matrixRowNumber);
 }

void save(string fileName, MatrixXd  matrix)
{
    const static IOFormat CSVFormat(FullPrecision, DontAlignCols, " ", "\n");
    ofstream file(fileName);
    if (file.is_open())
    {
        file << matrix.format(CSVFormat);
        file.close();
    }
}

void Print(MatrixXd a, int n, int m)
{
     for (int i = 0; i < n; i++)
{
	 for (int j = n; j < m; j++)
{
            printf("%.3f  ", a(i, j));
        }
        fprintf(output_file,"\n");
    }

     return;
}

void GaussJordan(MatrixXd mtx){

   MatrixXd I_ = MatrixXd::Identity(mtx.rows(),mtx.cols());

   MatrixXd C(mtx.rows(), mtx.cols()+I_.cols());

   C << mtx, I_;

   C.row(0).swap(C.row(mtx.rows() - 1));

   for (int i = 0; i < mtx.rows(); i++) {

        for (int j = 0; j < mtx.rows(); j++)
{
            if (j != i) {

                float temp = C(j,i) / C(i,i);
                for (int k = 0; k < 2*mtx.rows(); k++)
{
                    C(j,k) -= C(i,k)*temp;
                }
            }
        }
    }

    for (int i = 0; i < mtx.rows(); i++)
{
        float temp_ = C(i,i);

        for (int j = 0; j < 2 * mtx.rows(); j++)
{
            C(i,j) = C(i,j)/temp_;
        }
      }

    Print(C, mtx.cols(), 2*mtx.cols());
}


text:
#ifndef InitCondLV_CC_
#define InitCondLV_CC_
/**
 * @file initcondlv.cc
 * @brief NPDE homework InitCondLV code
 * @author lfilippo, tille, jgacon, dcasati
 * @copyright Developed at ETH Zurich
 */

#include <Eigen/Core>
#include <iostream>
#include <utility>

#include "../../../lecturecodes/Ode45/ode45.h"

namespace InitCondLV {

/* Compute the maps Phi(t,y0) and W(t,y0) at final time T.
 * Use initial data given by u0 and v0. */
/* SAM_LISTING_BEGIN_1 */
std::pair<Eigen::Vector2d, Eigen::Matrix2d> PhiAndW(double u0, double v0,
                                                    double T) {
  // Save the values of Phi and W at time T in PaW.first and PaW.second resp.
  std::pair<Eigen::Vector2d, Eigen::Matrix2d> PaW;

  //====================
  // Your code goes here
  //====================
  return PaW;
}
/* SAM_LISTING_END_1 */

}  // namespace InitCondLV

#endif  // #define InitCondLV_CC_