text:
#include <ai.h>

#include <vector>
#include <map>
#include <iostream>
#include <fstream>
#include <cassert>

#include <boost/format.hpp>
#include <boost/multi_array.hpp>

#include <OpenEXR/ImathVec.h>

typedef boost::multi_array<Imath::V3f, 2> V3fSamplingArray2D;

//points
//{
// name test
// points 2 3 POINT
// # 2 points
// # 3 motion blur samples per point
// # First sample
// -10 0 0
// 10 0 0
// # Second sample
// -10 5 0
// 10 15 0
// # Third sample
// -15 5 0
// 15 10 0
// radius 1
// deform_time_samples 2 1 FLOAT
//-0.25 0.25
//}

int main(int argc, char **argv)
{
	V3fSamplingArray2D arnold_points_data_array;

	V3fSamplingArray2D::index i_motion_sample_count = 3;
	V3fSamplingArray2D::index i_current_interim_points_count = 2;
	arnold_points_data_array.resize(boost::extents[i_motion_sample_count][i_current_interim_points_count]);

	std::string i_arnold_filename("points.ass");
	std::string name("mypoints");

	// Sample 0
	arnold_points_data_array[0][0] = Imath::V3f(-10, 0, 0);
	arnold_points_data_array[0][1] = Imath::V3f( 10, 0, 0);

	// Sample 1
	arnold_points_data_array[1][0] = Imath::V3f(-10,  5, 0);
	arnold_points_data_array[1][1] = Imath::V3f( 10, 15, 0);

	// Sample 2
	arnold_points_data_array[2][0] = Imath::V3f(-15,  5, 0);
	arnold_points_data_array[2][1] = Imath::V3f( 15, 10, 0);

	// start an Arnold session
	AiBegin();

	// name
	AtNode *points = AiNode("points");
	AiNodeSetStr(points, "name", name.c_str());

	// points
	AiNodeSetArray(points, "points", AiArrayConvert(i_current_interim_points_count,i_motion_sample_count,AI_TYPE_POINT,arnold_points_data_array.data()));

	// write out an .ass file
	AiASSWrite(i_arnold_filename.c_str(), AI_NODE_ALL, false, false);

	// at this point we can shut down Arnold
	AiEnd();

	return 0;
}


text:
#include <boost/config.hpp>

class move_nothrow {
    // Some class class members go here.
    // ...

public:
    move_nothrow() BOOST_NOEXCEPT;
    move_nothrow(move_nothrow&&) BOOST_NOEXCEPT
        // Members initialization goes here.
        // ...
    {}

    move_nothrow& operator=(move_nothrow&&) BOOST_NOEXCEPT {
        // Implementation goes here.
        // ...
        return *this;
    }

    move_nothrow(const move_nothrow&);
    move_nothrow& operator=(const move_nothrow&);
};

// In header file.
int foo() BOOST_NOEXCEPT;

// In source file.
int foo() BOOST_NOEXCEPT {
    return 0;
}

#include <vector>

int main() {
    std::vector<move_nothrow> v(10);
    v.push_back(move_nothrow());

    foo();
}

// details:
move_nothrow::move_nothrow() BOOST_NOEXCEPT {}

ext: cpp

max_stars_repo_name: apolukhin/boost-cookbook

max_issues_repo_name: apolukhin/boost-cookbook

max_forks_repo_name: apolukhin/boost-cookbook


text:
////////////////////////////////////////////////////////////////////////////////
// DeformedCells_cli.cc
////////////////////////////////////////////////////////////////////////////////
/*! @file
//      Generates and homogenizes cells that have been deformed linearly.
*/
//  Author:  Julian Panetta (jpanetta), julian.panetta@gmail.com
//  Company:  New York University
//  Created:  05/12/2015 15:22:14
////////////////////////////////////////////////////////////////////////////////
#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
#include <MeshFEM/util.h>
#include <MeshFEM/Types.hh>
#include <MeshFEM/GlobalBenchmark.hh>
#include <MeshFEM/SymmetricMatrix.hh>
#include <MeshFEM/FEMMesh.hh>
#include <MeshFEM/MeshIO.hh>
#include <MeshFEM/LinearElasticity.hh>
#include <MeshFEM/Materials.hh>
#include <MeshFEM/PeriodicHomogenization.hh>
#include <MeshFEM/MSHFieldWriter.hh>
#include <MeshFEM/filters/remove_dangling_vertices.hh>

#include <boost/program_options.hpp>
#include <json.hpp>

namespace po = boost::program_options;
using json = nlohmann::json;
using namespace std;
using namespace PeriodicHomogenization;

[[ noreturn ]] void usage(int exitVal, const po::options_description &visible_opts) {
    cerr << "Usage: DeformedCells_cli [options] in.msh -j 'u_x,x u_x,y ...' out.msh" << endl;
    cerr << visible_opts << endl;
    exit(exitVal);
}

po::variables_map parseCmdLine(int argc, const char *argv[])
{
    po::options_description hidden_opts("Hidden Arguments");
    hidden_opts.add_options()
        ("mesh", po::value<string>(), "input mesh")
        ;
    po::positional_options_description p;
    p.add("mesh",    1);

    po::options_description visible_opts;
    visible_opts.add_options()("help", "Produce this help message")
        ("homogenize",                    "run homogenization")
        ("transformVersion",              "use transform version of homogenization")
        ("material,m", po::value<string>(), "base material")
        ("jacobian,j", po::value<string>(), "linear deformation jacobian")
        ("displacedMesh", po::value<string>(), "file prefix containing displaced mesh")
        ("displacementScale", po::value<double>(), "used to scale displacements obtained from constant plus periodic strain")
        ("parametrizedTransform,p",         "read a list of parameterized deformations from stdin")
        ("degree,d",   po::value<int>()->default_value(2), "degree of finite elements")
        ("tile,t",     po::value<string>(), "tilings 'nx ny nz' (default: 1)")
        ("out,o",      po::value<string>(), "output file of deformed geometry (and w_ij fields if homogenization is run)")
        ("dumpJson",   po::value<string>(), "dump info into a json file)")
        ;

    po::options_description cli_opts;
    cli_opts.add(visible_opts).add(hidden_opts);

    po::variables_map vm;
    try {
        po::store(po::command_line_parser(argc, argv).
                  options(cli_opts).positional(p).run(), vm);
        po::notify(vm);
    }
    catch (std::exception &e) {
        cerr << "Error: " << e.what() << endl << endl;
        usage(1, visible_opts);
    }

    bool fail = false;
    if (vm.count("mesh") == 0) {
        cerr << "Error: must specify input mesh" << endl;
        fail = true;
    }

    if (vm.count("tile") + vm.count("homogenize") == 2) {
        cerr << "Error: do not specify both tiling and homogenization" << endl;
        fail = true;
    }

    if (vm.count("out") + vm.count("homogenize") == 0) {
        cerr << "Error: no operation requested." << endl;
        fail = true;
    }

    if (vm.count("jacobian") + vm.count("parametrizedTransform") != 1) {
        cerr << "Error: must specify either deformation jacobian or parametrizedTransform" << endl;
        fail = true;
    }

    if (fail || vm.count("help"))
        usage(fail, visible_opts);

    return vm;
}

template<size_t _N>
using HMG = LinearElasticity::HomogenousMaterialGetter<Materials::Constant>::template Getter<_N>;

template<typename T>
void dumpJson(const T &EhDefo, const std::string &filename) {
    json data;
    data["elasticity_tensor"] = json::array();
    auto E = EhDefo.getCoefficients();
    for (auto x : E) {
        data["elasticity_tensor"].push_back(x);
    }
    data["homogenized_moduli"] = json::array();
    std::vector<double> P;
    EhDefo.getOrthotropicParameters(P);
    for (auto x : P) {
        data["homogenized_moduli"].push_back(x);
    }

    // Write json
    std::ofstream out(filename);
    out << data;
}

template<size_t _N, size_t _FEMDegree>
void execute(const po::variables_map &args,
             const vector<MeshIO::IOVertex> &inVertices,
             const vector<MeshIO::IOElement> &inElements)
{
    auto &mat = HMG<_N>::material;
    if (args.count("material")) mat.setFromFile(args["material"].as<string>());
    typedef LinearElasticity::Mesh<_N, _FEMDegree, HMG> Mesh;
    typedef LinearElasticity::Simulator<Mesh> Simulator;
    typedef typename Simulator::VField VField;
    Simulator sim(inElements, inVertices);
    auto &mesh = sim.mesh();

    cout << setprecision(16);

    if (args.count("parametrizedTransform")) {
        if (!args.count("homogenize") || args.count("tile"))
            throw runtime_error("parametrizedTransform only supports homogenization");
        if (args.count("transformVersion") == 0)
            cerr << "WARNING: running transformVersion" << endl;
        if (_N != 2)
            throw runtime_error("parametrizedTransform only supports 2D");
        string line;
        Real theta, lambda;

        auto EBase = mat.getTensor();

        while (getDataLine(cin, line)) {
            boost::trim(line);
            vector<string> lineComponents;
            boost::split(lineComponents, line, boost::is_any_of("\t "),
                    boost::token_compress_on);
            if (lineComponents.size() != 2)
                throw runtime_error("invalid input transformation: " + line);
            theta = stod(lineComponents[0]);
            lambda = stod(lineComponents[1]);

            Eigen::Matrix<Real, _N, _N> rot, stretch, jacobian;
            rot << cos(theta), -sin(theta),
                   sin(theta),  cos(theta);
            stretch << lambda, 0,
                       0,      1;
            jacobian = rot * stretch * rot.transpose();
            std::cout << jacobian << std::endl;
            mat.setTensor(EBase.transform(jacobian.inverse()));

            vector<VField> w_ij;
            solveCellProblems(w_ij, sim);
            auto EhDefo = homogenizedElasticityTensorDisplacementForm(w_ij, sim).transform(jacobian);
            auto ShDefo = EhDefo.inverse();

            cout << theta << '\t' << lambda << '\t'
                 << EhDefo.D(0, 0) << '\t' << EhDefo.D(0, 1) << '\t' << EhDefo.D(0, 2) << '\t'
                                           << EhDefo.D(1, 1) << '\t' << EhDefo.D(1, 2) << '\t'
                                                                     << EhDefo.D(2, 2) << '\t'
                 << ShDefo.D(0, 0) << '\t' << ShDefo.D(0, 1) << '\t' << ShDefo.D(0, 2) << '\t'
                                           << ShDefo.D(1, 1) << '\t' << ShDefo.D(1, 2) << '\t'
                                                                     << ShDefo.D(2, 2) << endl;
            if (args.count("dumpJson")) {
                dumpJson(EhDefo, args["dumpJson"].as<string>());
            }
        }
        // BENCHMARK_REPORT();
        return;
    }

    // Parse jacobian.
    Eigen::Matrix<Real, _N, _N> jacobian;

    vector<string> jacobianComponents;
    string jacobianString = args["jacobian"].as<string>();
    boost::trim(jacobianString);
    boost::split(jacobianComponents, jacobianString, boost::is_any_of("\t "),
                 boost::token_compress_on);
    if (jacobianComponents.size() != _N * _N)
        throw runtime_error("Invalid deformation jacobian");
    for (size_t i = 0; i < _N; ++i) {
        for (size_t j = 0; j < _N; ++j) {
            jacobian(i, j) = stod(jacobianComponents[_N * i + j]);
        }
    }

    auto bbox = mesh.boundingBox();
    VectorND<_N> center = 0.5 * (bbox.minCorner + bbox.maxCorner);
    vector<MeshIO::IOVertex> deformedVertices;

    for (size_t vi = 0; vi < mesh.numVertices(); ++vi) {
        VectorND<_N> p = mesh.vertex(vi).node()->p;
        deformedVertices.emplace_back((jacobian * (p - center)).eval());
    }

    Real deformedCellVolume = bbox.volume() * jacobian.determinant();

    if (args.count("homogenize") && args.count("transformVersion")) {
        vector<VField> w_ij;
        // Morteza's transformation formulas
        mat.setTensor(mat.getTensor().transform(jacobian.inverse()));
        solveCellProblems(w_ij, sim);
        auto EhDefo = homogenizedElasticityTensorDisplacementForm(w_ij, sim).transform(jacobian);
        cout << "Elasticity tensor:" << endl;
        cout << EhDefo << endl << endl;
        cout << "Homogenized Moduli: ";
        EhDefo.printOrthotropic(cout);
        if (args.count("dumpJson")) {
            dumpJson(EhDefo, args["dumpJson"].as<string>());
        }
    }
    else if (args.count("homogenize")) {
        sim.applyPeriodicConditions();
        sim.applyNoRigidMotionConstraint();
        sim.setUsePinNoRigidTranslationConstraint(true);
        sim.updateMeshNodePositions(deformedVertices);
        shared_ptr<MSHFieldWriter> writer;
        if (args.count("out"))
            writer = make_shared<MSHFieldWriter>(args["out"].as<string>(), mesh);
        vector<VField> w_ij;
        typedef typename Simulator::SMatrix SMatrix;
        constexpr size_t numStrains = SMatrix::flatSize();
        for (size_t i = 0; i < numStrains; ++i) {
            VField rhs(sim.constantStrainLoad(-SMatrix::CanonicalBasis(i)));
            w_ij.push_back(sim.solve(rhs));
            if (writer) {
                writer->addField("load_ij " + to_string(i), sim.dofToNodeField(rhs), DomainType::PER_NODE);
                writer->addField("w_ij" + to_string(i), w_ij.back(), DomainType::PER_NODE);
                writer->addField("strain w_ij " + to_string(i), sim.averageStrainField(w_ij[i]),
                                 DomainType::PER_ELEMENT);
            }
        }
        auto EhDefo = homogenizedElasticityTensorDisplacementForm(w_ij, sim, deformedCellVolume);
        cout << "Elasticity tensor:" << endl;
        cout << EhDefo << endl << endl;
        cout << "Homogenized Moduli: ";
        EhDefo.printOrthotropic(cout);

        if (args.count("displacedMesh")) {
            string out_mesh = args["displacedMesh"].as<string>();
            SymmetricMatrixValue<Real, _N> strain;
            auto displaced_center = mesh.boundingBox().center();

            vector<VField> cstrainDisp_ij;

            for (unsigned index = 0; index < 3; index++) {
                VField cstrainDisp(mesh.numNodes());
                strain = SMatrix::CanonicalBasis(index);
                for (auto n : mesh.nodes())
                    cstrainDisp(n.index()) = strain.contract(n->p - displaced_center);

                cstrainDisp_ij.push_back(cstrainDisp);
            }

            for (unsigned index = 0; index < 3; index++) {
                for (auto n : mesh.nodes())
                    cstrainDisp_ij[index](n.index()) += w_ij[index](n.index());

                writer->addField("u_cstrain_ij" + to_string(index), cstrainDisp_ij[index], DomainType::PER_NODE);
            }

            // Adding displacement to each node:
            for (unsigned index = 0; index < 3; index++) {
                vector<MeshIO::IOVertex> displacedVertices;
                VField cstrainDisp = cstrainDisp_ij[index];
                Eigen::Matrix<Real, _N, Eigen::Dynamic> cstrainDisp_v = sim.template nodeToVertexField<VField>(
                        cstrainDisp).data();
                assert(mesh.numVertices() == size_t(cstrainDisp_v.cols()));
                for (size_t vi = 0; vi < mesh.numVertices(); ++vi) {
                    VectorND<_N> p = mesh.vertex(vi).node()->p;
                    VectorND<_N> cstrainDisp_vi = cstrainDisp_v.col(vi);
                    double displacementScale = args.count("displacementScale") ? args["displacementScale"].as<double>() : 0.1;
                    displacedVertices.emplace_back((p + displacementScale * cstrainDisp_vi).eval());
                }

                sim.updateMeshNodePositions(displacedVertices);
                shared_ptr<MSHFieldWriter> writerDisplacedMesh;

                writerDisplacedMesh = make_shared<MSHFieldWriter>(out_mesh + to_string(index) + ".msh", mesh);
            }
        }

        if (args.count("dumpJson")) {
            dumpJson(EhDefo, args["dumpJson"].as<string>());
        }
    }
    else if (args.count("tile")) {
        mesh.setNodePositions(deformedVertices);
        string tileString = args["tile"].as<string>();
        boost::trim(tileString);
        vector<string> tileComponents;
        boost::split(tileComponents, tileString, boost::is_any_of("\t "),
                     boost::token_compress_on);
        if (tileComponents.size() != _N)
            throw runtime_error("Invalid number of tiling dimensions");
        vector<size_t> tilings;
        int numCells = 1;
        for (const auto &c : tileComponents) {
            int ci = stoi(c);
            if (ci <= 0) throw runtime_error("Invalid number of tilings");
            tilings.push_back((size_t) ci);
            numCells *= ci;
        }

        vector<MeshIO::IOVertex>  tiledVertices;
        vector<MeshIO::IOElement> tiledElements;
        tiledVertices.reserve(numCells * inVertices.size());
        tiledElements.reserve(numCells * inElements.size());
        size_t numCellVertices = inVertices.size();

        if (_N == 2) tilings.push_back(1);

        // Glue in (i, j, k) scanline order, attaching a copy of the base cell to the
        // existing structure by merging duplicated vertices. These duplicated
        // vertices for neighbors along dimension d are specified by the entries in
        // identifiedFacePairs[d]. In this scanline order, only neighbors
        // (i - 1, j, k), (i, j - 1, k), (i, j, k - 1) >= (0, 0, 0) exist.
        //      *-------*
        //     /   /   /|
        //    /---+---/ |
        //   /   /   /|/|     ^ j (y)
        //  *---+---* + *     |
        //  |   |   |/|/      |     i (x)
        //  |---+---| /       *----->
        //  |   |   |/       /
        //  *-------*       v k (z)
        // Gluing is done by overwriting tets' "tiled vertex indices" (i.e. index
        // before merging duplicates) with the corresponding "glued vertex indices"
        // (index after merging duplicates). For vertices that end up merging, the
        // final glued index is stored in a dictionary mapping the vertices'
        // original tiled vertex indices to the index they are merged to.
        //
        // Note: This scanline approach only works on geometry that is actually
        // triply periodic. For instance, it failed on James' zigzag shape that is
        // missing geometry in one of its corners.

        // Update a global tiled vertex index -> glued vertex index map.
        // Tiled vertex indieces are of the form:
        //   base_vertex_index + vtxOffset(i, j, k)
        //
        auto vtxIdxOffset = [numCellVertices, tilings](int i, int j, int k) -> size_t {
            return numCellVertices * (i * tilings[2] * tilings[1] + j * tilings[2] + k);
        };

        VectorND<_N> delta;
        for (size_t i = 0; i < tilings[0]; ++i) {
            delta[0] = i * bbox.dimensions()[0];
            for (size_t j = 0; j < tilings[1]; ++j) {
                delta[1] = j * bbox.dimensions()[1];
                for (size_t k = 0; k < tilings[2]; ++k) {
                    if (_N > 2) delta[2] = k * bbox.dimensions()[2];
                    auto vtxPosOffset = (jacobian * delta).eval();
                    for (const auto &v : deformedVertices)
                        tiledVertices.emplace_back((truncateFrom3D<VectorND<_N>>(v.point) + vtxPosOffset).eval());
                    for (auto e : inElements) {
                        for (size_t ei = 0; ei < e.size(); ++ei)
                            e[ei] = e[ei] + vtxIdxOffset(i, j, k);
                        tiledElements.push_back(e);
                    }
                }
            }
        }

        // TODO: merge duplicated vertices.
        remove_dangling_vertices(tiledVertices, tiledElements);
        save(args["out"].as<string>(), tiledVertices, tiledElements);
    }
}

////////////////////////////////////////////////////////////////////////////////
/*! Program entry point
//  @param[in]  argc    Number of arguments
//  @param[in]  argv    Argument strings
//  @return     status  (0 on success)
*///////////////////////////////////////////////////////////////////////////////
int main(int argc, const char *argv[])
{
    po::variables_map args = parseCmdLine(argc, argv);

    vector<MeshIO::IOVertex>  inVertices;
    vector<MeshIO::IOElement> inElements;
    auto type = load(args["mesh"].as<string>(), inVertices, inElements,
            MeshIO::FMT_GUESS, MeshIO::MESH_GUESS);

    // Infer dimension from mesh type.
    size_t dim;
    if      (type == MeshIO::MESH_TET) dim = 3;
    else if (type == MeshIO::MESH_TRI) dim = 2;
    else    throw std::runtime_error("Mesh must be triangle or tet.");

    // Look up and run appropriate instantiation.
    int deg = args["degree"].as<int>();
    auto exec = (dim == 3) ? ((deg == 2) ? execute<3, 2> : execute<3, 1>)
                           : ((deg == 2) ? execute<2, 2> : execute<2, 1>);
    exec(args, inVertices, inElements);
    return 0;
}

ext: cc

max_stars_repo_name: pbedenbaugh/MeshFEM

max_issues_repo_name: pbedenbaugh/MeshFEM

max_forks_repo_name: pbedenbaugh/MeshFEM


text:
//          Copyright Gavin Band 2008 - 2012.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

#ifndef QCTOOL_SNP_SUMMARY_COMPONENT_SEQUENCE_ANNOTATION_HPP
#define QCTOOL_SNP_SUMMARY_COMPONENT_SEQUENCE_ANNOTATION_HPP

#include <utility>
#include <map>
#include <deque>
#include <boost/function.hpp>
#include "components/SNPSummaryComponent/SNPSummaryComputation.hpp"
#include "genfile/Chromosome.hpp"
#include "genfile/VariantEntry.hpp"
#include "genfile/wildcard.hpp"
#include "GenomeSequence.hpp"

struct SequenceAnnotation: public SNPSummaryComputation
{
	typedef std::auto_ptr< SequenceAnnotation > UniquePtr ;

	typedef boost::function< void ( std::size_t, boost::optional< std::size_t > ) > ProgressCallback ;
	SequenceAnnotation( std::string const& annotation_name, std::string const& fasta_filename, ProgressCallback ) ;
	void operator()( VariantIdentifyingData const&, Genotypes const&, Ploidy const&, genfile::VariantDataReader&, ResultCallback ) ;

	std::string get_summary( std::string const& prefix = "", std::size_t column_width = 20 ) const ;

	void set_flanking( std::size_t left, std::size_t right ) ;
private:
	std::string const m_annotation_name ;
	std::string const m_fasta_filename ;
	std::vector< genfile::wildcard::FilenameMatch > const m_filenames ;

	GenomeSequence m_sequence ;
	std::pair< genfile::Position, genfile::Position > m_flanking ;
} ;

#endif

ext: hpp

max_stars_repo_name: CreRecombinase/qctool

max_issues_repo_name: CreRecombinase/qctool

max_forks_repo_name: CreRecombinase/qctool


text:
// --------------------------------------------------------------------------
//                   OpenMS -- Open-Source Mass Spectrometry
// --------------------------------------------------------------------------
// Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
// ETH Zurich, and Freie Universitaet Berlin 2002-2021.
//
// This software is released under a three-clause BSD license:
//  * 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 any author or any participating institution
//    may be used to endorse or promote products derived from this software
//    without specific prior written permission.
// For a full list of authors, refer to the file AUTHORS.
// --------------------------------------------------------------------------
// 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 ANY OF THE AUTHORS OR THE CONTRIBUTING
// INSTITUTIONS 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.
//
// --------------------------------------------------------------------------
// $Maintainer: Chris Bielow, Ahmed Khalil $
// $Authors: Andreas Bertsch, Chris Bielow $
// --------------------------------------------------------------------------
//

#include <OpenMS/CHEMISTRY/EmpiricalFormula.h>

#include <OpenMS/CHEMISTRY/Element.h>
#include <OpenMS/CHEMISTRY/ElementDB.h>
#include <OpenMS/CHEMISTRY/ISOTOPEDISTRIBUTION/IsotopePatternGenerator.h>
#include <OpenMS/CHEMISTRY/ISOTOPEDISTRIBUTION/IsotopeDistribution.h>
#include <OpenMS/CHEMISTRY/ISOTOPEDISTRIBUTION/CoarseIsotopePatternGenerator.h>
#include <OpenMS/CONCEPT/LogStream.h>
#include <OpenMS/CONCEPT/Constants.h>
#include <OpenMS/MATH/MISC/MathFunctions.h>

#include <boost/math/special_functions/binomial.hpp>

#include <iostream>
#include <algorithm>

using namespace std;

namespace OpenMS
{
  EmpiricalFormula::EmpiricalFormula() :
    charge_(0)
  {}

  EmpiricalFormula::EmpiricalFormula(const String& formula)
  {
    charge_ = parseFormula_(formula_, formula);
  }

  EmpiricalFormula::EmpiricalFormula(SignedSize number, const Element* element, SignedSize charge)
  {
    formula_[element] = number;
    charge_ = charge;
  }

  EmpiricalFormula::~EmpiricalFormula()
  {
  }

  double EmpiricalFormula::getMonoWeight() const
  {
    double weight = Constants::PROTON_MASS_U * charge_;
    for (const auto& it : formula_)
    {
      weight += it.first->getMonoWeight() * (double)it.second;
    }
    return weight;
  }

  double EmpiricalFormula::getAverageWeight() const
  {
    double weight = Constants::PROTON_MASS_U * charge_;
    for (const auto& it : formula_)
    {
      weight += it.first->getAverageWeight() * (double)it.second;
    }
    return weight;
  }

  double EmpiricalFormula::calculateTheoreticalIsotopesNumber() const
  {
    double total = 1;
    for (const auto& element : formula_)
    {
      UInt non_trace_isotopes = 0;
      const auto& distr = element.first->getIsotopeDistribution();
      for (const auto& isotope : distr)
      {
        if (isotope.getIntensity() != 0)
        {
          non_trace_isotopes++;
        }
      }
      if (non_trace_isotopes>1 && element.second!=1)
      {
        total *= boost::math::binomial_coefficient<double>(UInt(element.second), non_trace_isotopes);
      }
      else
      {
        total *= element.second*non_trace_isotopes;
      }
    }
    return total;
  }

  bool EmpiricalFormula::estimateFromWeightAndCompAndS(double average_weight, UInt S, double C, double H, double N, double O, double P)
  {
    const ElementDB* db = ElementDB::getInstance();

    double remaining_weight = average_weight - S * db->getElement("S")->getAverageWeight();

    // The number of sulfurs is set to 0 because we're explicitly specifying their count.
    // We propagate the return value to let the programmer know if the approximation succeeded
    // without requesting a negative number of hydrogens.
    bool ret = estimateFromWeightAndComp(remaining_weight, C, H, N, O, 0.0, P);

    formula_.at(db->getElement("S")) = S;

    return ret;
  }

  bool EmpiricalFormula::estimateFromWeightAndComp(double average_weight, double C, double H, double N, double O, double S, double P)
  {
    const ElementDB* db = ElementDB::getInstance();

    double avgTotal = (C * db->getElement("C")->getAverageWeight() +
                       H * db->getElement("H")->getAverageWeight() +
                       N * db->getElement("N")->getAverageWeight() +
                       O * db->getElement("O")->getAverageWeight() +
                       S * db->getElement("S")->getAverageWeight() +
                       P * db->getElement("P")->getAverageWeight());

    double factor = average_weight / avgTotal;

    formula_.clear();

    formula_.insert(make_pair(db->getElement("C"), (SignedSize) Math::round(C * factor)));
    formula_.insert(make_pair(db->getElement("N"), (SignedSize) Math::round(N * factor)));
    formula_.insert(make_pair(db->getElement("O"), (SignedSize) Math::round(O * factor)));
    formula_.insert(make_pair(db->getElement("S"), (SignedSize) Math::round(S * factor)));
    formula_.insert(make_pair(db->getElement("P"), (SignedSize) Math::round(P * factor)));

    double remaining_mass = average_weight-getAverageWeight();
    SignedSize adjusted_H = Math::round(remaining_mass / db->getElement("H")->getAverageWeight());

    // It's possible for a very small mass to get a negative value here.
    if (adjusted_H < 0)
    {
      // The approximation can still be useful, but we set the return flag to false to explicitly notify the programmer.
      return false;
    }

    // Only insert hydrogens if their number is not negative.
    formula_.insert(make_pair(db->getElement("H"), adjusted_H));
    // The approximation had no issues.
    return true;
  }

  IsotopeDistribution EmpiricalFormula::getIsotopeDistribution(const IsotopePatternGenerator& solver) const
  {
    return solver.run(*this);
  }

  IsotopeDistribution EmpiricalFormula::getConditionalFragmentIsotopeDist(const EmpiricalFormula& precursor,
                                                                          const std::set<UInt>& precursor_isotopes,
                                                                          const CoarseIsotopePatternGenerator& solver) const
  {
    // A fragment's isotopes can only be as high as the largest isolated precursor isotope.
    UInt max_depth = *std::max_element(precursor_isotopes.begin(), precursor_isotopes.end())+1;

    // Treat *this as the fragment molecule
    EmpiricalFormula complementary_fragment = precursor-*this;

    IsotopeDistribution fragment_isotope_dist = getIsotopeDistribution(CoarseIsotopePatternGenerator(max_depth));
    IsotopeDistribution comp_fragment_isotope_dist = complementary_fragment.getIsotopeDistribution(CoarseIsotopePatternGenerator(max_depth));

    IsotopeDistribution result = solver.calcFragmentIsotopeDist(fragment_isotope_dist, comp_fragment_isotope_dist, precursor_isotopes, getMonoWeight());

    // Renormalize to make these conditional probabilities (conditioned on the isolated precursor isotopes)
    result.renormalize();

    return result;
  }

  SignedSize EmpiricalFormula::getNumberOf(const Element* element) const
  {
    const auto& it  = formula_.find(element);
    if (it != formula_.end())
    {
      return it->second;
    }
    return 0;
  }

  SignedSize EmpiricalFormula::getNumberOfAtoms() const
  {
    SignedSize num_atoms(0);
    for (const auto& it : formula_) num_atoms += it.second;
    return num_atoms;
  }

  void EmpiricalFormula::setCharge(Int charge)
  {
    charge_ = charge;
  }

  Int EmpiricalFormula::getCharge() const
  {
    return charge_;
  }

  String EmpiricalFormula::toString() const
  {
    String formula;
    std::map<String, SignedSize> new_formula;

    for (const auto& it : formula_)
    {
      new_formula[it.first->getSymbol()] = it.second;
    }

    for (const auto& it : new_formula)
    {
      (formula += it.first) += String(it.second);
    }
    return formula;
  }

  std::map<std::string, int> EmpiricalFormula::toMap() const
  {
    std::map<std::string, int> new_formula;

    for (const auto & it : formula_)
    {
      new_formula[it.first->getSymbol()] = it.second;
    }
    return new_formula;
  }

  EmpiricalFormula EmpiricalFormula::operator*(const SignedSize& times) const
  {
    EmpiricalFormula ef(*this);
    for (const auto& it : formula_) ef.formula_[it.first] *= times;
    ef.charge_ *= times;
    ef.removeZeroedElements_();
    return ef;
  }

  EmpiricalFormula EmpiricalFormula::operator+(const EmpiricalFormula& formula) const
  {
    EmpiricalFormula ef;
    ef.formula_ = formula.formula_;
    for (const auto& it : formula_)
    {
      auto ef_it  = ef.formula_.find(it.first);
      if (ef_it != ef.formula_.end())
      {
        ef_it->second += it.second;
      }
      else
      {
        ef.formula_.insert(it);
      }
    }
    ef.charge_ = charge_ + formula.charge_;
    ef.removeZeroedElements_();
    return ef;
  }

  EmpiricalFormula& EmpiricalFormula::operator+=(const EmpiricalFormula& formula)
  {
    for (const auto& it : formula.formula_)
    {
      auto f_it  = formula_.find(it.first);
      if (f_it != formula_.end())
      {
        f_it->second += it.second;
      }
      else
      {
        formula_.insert(it);
      }
    }
    charge_ += formula.charge_;
    removeZeroedElements_();
    return *this;
  }

  EmpiricalFormula EmpiricalFormula::operator-(const EmpiricalFormula& formula) const
  {
    EmpiricalFormula ef(*this);
    for (const auto& it : formula.formula_)
    {
      const Element* e = it.first;
      SignedSize num = it.second;
      auto ef_it  = ef.formula_.find(e);
      if (ef_it != ef.formula_.end())
      {
        ef_it->second -= num;
      }
      else
      {
        ef.formula_[e] = -num;
      }
    }

    ef.charge_ = charge_ - formula.charge_;
    ef.removeZeroedElements_();
    return ef;
  }

  EmpiricalFormula& EmpiricalFormula::operator-=(const EmpiricalFormula& formula)
  {
    for (const auto& it : formula.formula_)
    {
      auto f_it  = formula_.find(it.first);
      if (f_it != formula_.end())
      {
        f_it->second -= it.second;
      }
      else
      {
        formula_[it.first] = -it.second;
      }
    }
    charge_ -= formula.charge_;
    removeZeroedElements_();
    return *this;
  }

  bool EmpiricalFormula::isCharged() const
  {
    return charge_ != 0;
  }

  bool EmpiricalFormula::isEmpty() const
  {
    return formula_.empty();
  }

  bool EmpiricalFormula::hasElement(const Element* element) const
  {
    return formula_.find(element) != formula_.end();
  }

  bool EmpiricalFormula::contains(const EmpiricalFormula& ef)
  {
    for (const auto& it : ef)
    {
      if (this->getNumberOf(it.first) < it.second)
      {
        return false;
      }
    }
    return true;
  }

  bool EmpiricalFormula::operator==(const EmpiricalFormula& formula) const
  {
    return formula_ == formula.formula_ && charge_ == formula.charge_;
  }

  bool EmpiricalFormula::operator!=(const EmpiricalFormula& formula) const
  {
    return formula_ != formula.formula_ || charge_ != formula.charge_;
  }

  ostream& operator<<(ostream& os, const EmpiricalFormula& formula)
  {
    std::map<String, SignedSize> new_formula;
    for (const auto& it : formula.formula_)
    {
      new_formula[it.first->getSymbol()] = it.second;
    }

    for (const auto& it : new_formula)
    {
      os << it.first;
      if (it.second > 1) os << it.second;
    }
    if (formula.charge_ == 0)
    {
      return os;
    }

    if (formula.charge_ > 0)
    {
      if (formula.charge_ == 1)
      {
        os << "+";
      }
      else
      {
        os << "+" << formula.charge_;
      }
    }
    else
    {
      if (formula.charge_ == -1)
      {
        os << "-";
      }
      else
      {
        os << "-" << formula.charge_;
      }
    }
    return os;
  }

  Int EmpiricalFormula::parseFormula_(std::map<const Element*, SignedSize>& ef, const String& input_formula) const
  {
    Int charge{0};
    String formula(input_formula);
    formula.trim();

    // we start with the charge part, read until the begin of the formula or a element symbol occurs
    String suffix;
    for (SignedSize reverse_i(formula.size() - 1); reverse_i >= 0; --reverse_i)
    {
      if (!isalpha(formula[reverse_i]))
      {
        suffix = formula[reverse_i] + suffix;
      }
      else
      {
        break;
      }
    }

    // determine charge
    if (!suffix.empty())
    {
      Size i = 1;
      for (; i < suffix.size(); ++i)
      {
        if (!isdigit(suffix[i]))
        {
          break;
        }
      }

      if (i != suffix.size())
      {
        // we found the charge part
        String charge_str;
        for (Size j = i + 1; j < suffix.size(); ++j)
        {
          charge_str += suffix[j];
        }

        Int tmp_charge = 1;
        if (!charge_str.empty())
        {
          tmp_charge = charge_str.toInt();
        }

        if (suffix[i] == '-')
        {
          charge = -1 * tmp_charge;
        }
        else
        {
          if (suffix[i] == '+')
          {
            charge = tmp_charge;
          }
          else
          {
            throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, formula, "Cannot parse charge part of formula!");
          }
        }

        // now remove the charge part from the formula
        formula.resize(formula.size() - charge_str.size() - 1);
      }
    }

    if (suffix.size() == 1 && suffix[0] == '+')
    {
      charge = 1;
      formula.resize(formula.size() - 1);
    }
    else if (suffix.size() == formula.size())
    {
      if (suffix.size() > 1)
      {
        if (suffix[0] == '-' || suffix[0] == '+')
        {
          charge = suffix.toInt();
          return charge;
        }
      }
      else
      {
        if (suffix == "-")
        {
          charge = -1;
          return charge;
        }
      }
    }

    // split the formula
    vector<String> splitter;
    if (formula.size() > 0)
    {
      if (!isdigit(formula[0]) || formula[0] == '(')
      {
        bool is_isotope(false), is_symbol(false);
        String split;
        for (Size i = 0; i < formula.size(); ++i)
        {
          if ((isupper(formula[i]) && (!is_isotope || is_symbol))
             || formula[i] == '(')
          {
            if (split != "")
            {
              splitter.push_back(split);
              is_isotope = false;
              is_symbol = false;
            }
            split = String(1, formula[i]);
          }
          else
          {
            split += String(1, formula[i]);
          }
          if (formula[i] == '(')
          {
            is_isotope = true;
          }
          if (isupper(formula[i]))
          {
            is_symbol = true;
          }
        }
        splitter.push_back(split);
      }
      else
      {
        throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, formula, "This formula does not begin with an element!");
      }
    }

    // add up the elements
    for (Size i = 0; i != splitter.size(); ++i)
    {
      String split = splitter[i];
      String number;
      String symbol;
      bool had_symbol(false);
      for (SignedSize j = split.size() - 1; j >= 0; --j)
      {
        if (!had_symbol && (isdigit(split[j]) || split[j] == '-'))
        {
          number = split[j] + number;
        }
        else
        {
          symbol = split[j] + symbol;
          had_symbol = true;
        }
      }

      SignedSize num(1);
      if (number != "")
      {
        num = number.toInt();
      }

      const ElementDB* db = ElementDB::getInstance();
      // support D and T for Deuterium and Tritium
      if (symbol == "D") // Deuterium is represented as (2)H in DB.
      {
        if (num != 0)
        {
          const Element* e = db->getElement("(2)H");
          if (auto it = ef.find(e); it != ef.end())
          {
            it->second += num;
          }
          else
          {
            ef.insert({e, num});
          }
        }
      }
      else if (symbol == "T") // Tritium is represented as (3)H in DB
      {
        if (num != 0)
        {
          const Element* e = db->getElement("(3)H");
          if (auto it = ef.find(e); it != ef.end())
          {
            it->second += num;
          }
          else
          {
            ef.insert({e, num});
          }
        }
      }
      else if (db->hasElement(symbol))
      {
        if (num != 0)
        {
          const Element* e = db->getElement(symbol);
          std::map<const Element*, SignedSize>::iterator it = ef.find(e);
          if (it != ef.end())
          {
            it->second += num;
          }
          else
          {
            ef.insert(std::make_pair(e, num));
          }
        }
      }
      else
      {
        throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Unknown element '" + split + "'", "'" + symbol + "' found.");
      }
    }

    // remove elements with 0 counts
    std::map<const Element*, SignedSize>::iterator it = ef.begin();
    while (it != ef.end())
    {
      if (it->second == 0)
      {
        ef.erase(it++);   // Note: post increment needed! Otherwise iterator is invalidated
      }
      else
      {
        ++it;
      }
    }

    return charge;
  }

  void EmpiricalFormula::removeZeroedElements_()
  {
    MapType_::iterator it = formula_.begin();
    while (it != formula_.end())
    {
      if (it->second == 0)
      {
        formula_.erase(it++);   // Note: post increment needed! Otherwise iterator is invalidated
      }
      else
      {
        ++it;
      }
    }
  }

  bool EmpiricalFormula::operator<(const EmpiricalFormula& rhs) const
  {
    if (formula_.size() != rhs.formula_.size())
    {
      return formula_.size() < rhs.formula_.size();
    }

    if (charge_ != rhs.charge_)
    {
      return charge_ < rhs.charge_;
    }

    return formula_ < rhs.formula_;
  }

} // namespace OpenMS

max_stars_repo_name: vmusch/OpenMS

max_issues_repo_name: vmusch/OpenMS

max_forks_repo_name: vmusch/OpenMS


text:
//
//  JdFiber.hpp
//
//  Created by Steven Massey on 5/30/12.
//  Copyright (c) 2012 Steven Massey. All rights reserved.
//

#ifndef __JdFiber_hpp__

#include <boost/context/detail/fcontext.hpp>
#include <list>
#include <iomanip>

#include "JdAssert.hpp"
#include "JdTable.hpp"

d_jdForwardInterface (IJdFiber);

enum EJdFiberStatus
{
	e_jdFiber_invalid,

	e_jdFiber_running,
	e_jdFiber_finished,
	e_jdFiber_terminated,
	e_jdFiber_aborted,

	// runnable states:
	e_jdFiber_initialized,
	e_jdFiber_paused,
	e_jdFiber_exiting,
	e_jdFiber_terminating
};

d_jdInterface (IJdFiberControl)
{
	virtual string				GetName				() = 0;
	virtual void				SetName				(stringRef_t i_name) = 0;
	virtual bool				IsRunnable			() = 0;
	virtual EJdFiberStatus		Run					(JdResult & o_result) = 0;
	virtual EJdFiberStatus		Run					() = 0;
	virtual void				Yield				(IJdFiber i_toFiber = nullptr) = 0;		// Yield (nullptr) switches to calling fiber
	virtual void				YieldTo				(IJdFiber i_toFiber) = 0;				// YieldTo (nullptr) switches to home
	virtual JdResult			Terminate			() = 0;

	virtual void				dump				() = 0;
};

class JdFibers;

d_jdInterface (IJdFiber)
{
	public: friend JdFibers;

	virtual						~ IIJdFiber 		() {}

	size_t						Run					(JdResult & o_result)
	{
		return m_controller->Run (o_result);
	}

	size_t						Run					()
	{
		return m_controller->Run ();
	}

	JdResult					Terminate			()
	{
		return m_controller->Terminate ();
	}

	bool						IsRunnable			()
	{
		return m_controller->IsRunnable ();
	}

	void						Yield				()
	{
		m_controller->Yield ();
	}

	string						GetName				()
	{
		return m_controller->GetName ();
	}

	void						dump				()
	{
		m_controller->dump ();
	}

	protected:

	virtual i64					RunFiber			() = 0;

	IJdFiberControl 			fibers				()
	{
		return m_controller;
	}

	private:

	IJdFiberControl				m_controller		= nullptr;
};

namespace c_jdFiber
{
	auto const 	invalid 			= e_jdFiber_invalid,
				initialized 		= e_jdFiber_initialized,
				paused				= e_jdFiber_paused,
				running				= e_jdFiber_running,
				terminating			= e_jdFiber_terminating,
				exiting				= e_jdFiber_exiting,
				finished			= e_jdFiber_finished,
				terminated			= e_jdFiber_terminated,
				aborted				= e_jdFiber_aborted;
};

class JdFiberTerminate {};

namespace boost_ctx = boost::context::detail;

class JdFibers
{
	public:
								JdFibers				()
								:
								m_homeFiber				(this, nullptr, nullptr, "home")
	{
	}

	virtual 					~ JdFibers				()
	{
		for (auto i : m_stacks)
			free (i.stack);

		//for (auto f : m_fibers)
		//	cout << "unreleased: " << f << endl;

		//cout << "fibers: " << m_fibers.size () << endl;
		d_jdAssert (m_fibers.size () == 0, "unreleased fibers");
	}

	template <typename T>
	T *							CreateFiber				(size_t i_stackSize, JdResult * o_result = nullptr, stringRef_t i_fiberName = "")
	{
		d_jdAssert (i_stackSize >= 8192, "stack is too small"); // 8192 might even be too small. termination throw seems to cause a stack overflow @ 4kB

		i_stackSize = (i_stackSize + 4095) & ~4095;

		// align objects to 16 bytes
		size_t controllerSize = sizeof (FiberController);
		controllerSize = (controllerSize + 15) & ~15;

		size_t sizeOfT = sizeof (T);
		sizeOfT = (sizeOfT + 15) &  ~15;

		size_t objectsSize = controllerSize + sizeOfT;
		d_jdAssert (i_stackSize > objectsSize, "fiber stack not large enough");

		auto stack = CreateStack (i_stackSize);
//		auto stack = (u8 *) malloc (i_stackSize);

//		cout << "stack: " << (voidptr_t) stack << endl;

		// create IJdFiber
		auto ptr = stack;
		ptr += i_stackSize;
		ptr -= sizeOfT;
		auto fiber = new (ptr) T;

		// create FiberController
		ptr -= controllerSize;
		auto fc = new (ptr) FiberController (this, stack, fiber, i_fiberName);
		fc->m_stackSize = i_stackSize;

		stack += i_stackSize;
		stack -= objectsSize;

//		cout << "stacksize: " << stackSize << endl;

		fc->m_context = boost_ctx::make_fcontext (stack, i_stackSize - objectsSize, FiberRunner);

//		cout << "free: " << (u8 *) fc->m_context - stack << endl;

		if (fc->m_context)
			fc->m_state = e_jdFiber_initialized;

		m_fibers.push_back (fiber);

		return fiber;
	}

	template <typename T = IIJdFiber>
	T *							GetActiveFiber			()
	{
		return static_cast <T *> (m_activeFiber->m_implementation);
	}

	IJdFiberControl				GetActiveController 	()
	{
		return m_activeFiber;
	}

	size_t						GetNumFibers			() const
	{
		return m_fibers.size ();
	}

	template <typename T>
	T *							GetFiber				(size_t i_index)
	{
		return static_cast <T *> (m_fibers [i_index]);
	}

	JdResult					ReleaseFiber			(IJdFiber i_fiber, bool i_forceQuit = false)
	{
		auto controller = static_cast <FiberController *> (i_fiber->m_controller);
		d_jdAssert (controller != m_activeFiber, "can't release the active fiber");

		if (controller->m_state == c_jdFiber::paused)
		{
			if (i_forceQuit)
			{
			}
			else return d_jdError ("can't release active fiber without i_forceQuit");
		}

		if (controller->m_state == c_jdFiber::exiting)
		{
			 // get fiber out of restart loop
			if (AtHome ())
				controller->Run ();
			else
				m_activeFiber->Yield (i_fiber);		// TODO: test this scenario of releasing a fiber from another fiber
		}

		auto i = find (m_fibers.begin (), m_fibers.end (), controller->m_implementation);
		m_fibers.erase (i);

		auto stack = controller->m_stack;
		m_stacks.push_back ({ stack, controller->m_stackSize });

		i_fiber->~IIJdFiber ();
		controller->~FiberController ();

		return c_jdNoErr;
	}

//	void						MoveFiber				(IJdFiber i_fiber)
//	{
//
//	}

	void						dump					()
	{
		if (m_fibers.size ())
		{
			JdTable t ({ "fiber", "L:name", "stack", "max-used", "runs", "L:state" });

			cstr_t states [] = { "invalid", "running", "finished", "terminated", "aborted", "inited", "paused", "exiting", "terminating" };

			for (auto i : m_fibers)
			{
				auto f = static_cast <FiberController *> (i->m_controller);

				t.AddRow (f, f->m_name, JdByteSize (f->m_stackSize), JdByteSize (measure_stack_height (f)), f->m_runs, states [f->m_state]);
			}
		}
	}

	protected:

	struct FiberController : IIJdFiberControl
	{
								FiberController			(JdFibers * i_home, void * i_stack, IJdFiber i_implementation, stringRef_t i_name)
								:
								m_stack					(i_stack),
								m_home					(i_home),
								m_implementation		(i_implementation),
								m_name					(i_name)
		{
			if (i_implementation)
				i_implementation->m_controller = this;
		}

		virtual string			GetName					()
		{
			return m_name;
		}

		virtual void			SetName					(stringRef_t i_name)
		{
			m_name = i_name;
		}

		virtual void			dump					()
		{
			m_home->dump ();
		}

		virtual bool			IsRunnable				()
		{
			return (m_state == c_jdFiber::initialized or m_state == c_jdFiber::paused);
		}

		virtual EJdFiberStatus	Run						(JdResult & o_result)
		{
			d_jdAssert (m_home->AtHome (), "fiber can only be run from home state");

			if (m_state >= c_jdFiber::initialized)
			{
//				++m_runs;
				boost_ctx::transfer_t from = boost_ctx::jump_fcontext (m_context, this);
				m_home->ComingHome (from.fctx);

				o_result = m_result;
			}
			else o_result = d_jdError ("invalid fiber state");

			return m_state;
		}

		virtual EJdFiberStatus	Run						()
		{
			if (m_state >= c_jdFiber::initialized)
			{
//				++m_runs;
				boost_ctx::transfer_t from = boost_ctx::jump_fcontext (m_context, this);
				m_home->ComingHome (from.fctx);
			}

			return m_state;
		}

		virtual void			YieldTo					(IJdFiber i_yieldTo)
		{
			if (i_yieldTo)
				Yield (i_yieldTo);
			else
			{
				++m_runs;
				m_home->ReturnHome (this);
			}
		}

		virtual void			Yield					(IJdFiber i_yieldTo)
		{
//			u8 stack; cout << "freestack: " << & stack - (u8*) m_stack << endl;

			++m_runs;

			d_jdAssert (m_state == c_jdFiber::running, "fiber can't yield; aint't runin");

			if (i_yieldTo)
			{
				auto fc = static_cast <FiberController *> (i_yieldTo->m_controller);
				boost_ctx::transfer_t from = boost_ctx::jump_fcontext (fc->m_context, fc);
				m_home->MakeTransfer (this, from.fctx);
			}
			else
			{
				boost_ctx::transfer_t from = boost_ctx::jump_fcontext (m_transferredFrom->m_context, nullptr);
				m_home->MakeTransfer (this, from.fctx);
			}

			if (m_state == c_jdFiber::terminating)
				throw JdFiberTerminate ();

//			++m_runs;
		}

		virtual JdResult		Terminate				()
		{
			if (m_state == c_jdFiber::running)
			{
				throw JdFiberTerminate ();
			}
			else
			{
				m_state = c_jdFiber::terminating;
				m_home->Terminate (this);
			}

			return c_jdNoErr;
		}

		void *					m_stack					= nullptr;
		size_t					m_stackSize				= 0;
		size_t					m_runs					= 0;
		JdFibers *				m_home					= nullptr;
		boost_ctx::fcontext_t	m_context				= nullptr;
		IJdFiber				m_implementation		= nullptr;
		string					m_name;
		EJdFiberStatus			m_state 				= e_jdFiber_invalid;
		FiberController *		m_transferredFrom		= nullptr;
		JdResult				m_result;
	};

	static void FiberRunner (boost_ctx::transfer_t i)
	{
		auto f = reinterpret_cast <FiberController *> (i.data);
		f->m_home->MakeTransfer (f, i.fctx);

		try
		{
//			while (f->m_state == c_jdFiber::running)
			{
				i64 result = f->m_implementation->RunFiber ();

				if (result == 0)
					f->m_state = c_jdFiber::exiting;

//				f->m_home->ReturnHome (f);
			}
		}
		catch (JdFiberTerminate & _exit)
		{
//			cout << "terminated\n";
			f->m_state = c_jdFiber::terminated;
		}
		catch (...)
		{
			f->m_state = c_jdFiber::aborted;
		}

		if (f->m_state == c_jdFiber::exiting)
			f->m_state = c_jdFiber::finished;

//		cout << "really exited\n";

		f->m_home->ReturnHome (f);
	}

	void 					ComingHome					(boost_ctx::fcontext_t i_previousContext)
	{
		MakeTransfer (& m_homeFiber, i_previousContext);
	}

	void 					ReturnHome					(FiberController * i_fromFiber)
	{
//		cout << "RETURN HOME\n";

		auto homeContext = m_homeFiber.m_context;
		if (homeContext)
		{
			boost_ctx::transfer_t from = boost_ctx::jump_fcontext (homeContext, nullptr);
			MakeTransfer (i_fromFiber, from.fctx);
		}
	}

	bool					AtHome					() const
	{
		return m_activeFiber == & m_homeFiber;
	}

	void					MakeTransfer			(FiberController * i_newFiber, boost_ctx::fcontext_t i_previousContext)
	{
		m_activeFiber->m_context = i_previousContext;

//		cout << m_activeFiber->GetName () << " (" << m_activeFiber->m_context << ")" << " --> " << i_newFiber->GetName () << " (" << i_newFiber->m_context << ")" << endl;

		i_newFiber->m_transferredFrom = m_activeFiber;

		if (i_newFiber->m_state != c_jdFiber::terminating)
			i_newFiber->m_state = c_jdFiber::running;

		if (m_activeFiber->m_state == c_jdFiber::running)
			m_activeFiber->m_state = c_jdFiber::paused;

		m_activeFiber = i_newFiber;
	}

	void					Terminate				(FiberController * i_terminatingFiber)
	{
		if (AtHome ())
			i_terminatingFiber->Run ();
		else
			m_activeFiber->Yield (i_terminatingFiber->m_implementation);
	}

	u8 *					CreateStack				(size_t i_stackSize)
	{
		auto i = m_stacks.begin (), e = m_stacks.end ();
		while (i != e)
		{
			if (i->size == i_stackSize)
			{
				auto stack = i->stack;
				m_stacks.erase (i);
				return (u8 *) stack;
			}
			++i;
		}

		auto stack = (u8 *) malloc (i_stackSize);
#		if DEBUG
			memset (stack, 0x0, i_stackSize);
#		endif

		return stack;
	}

	size_t						measure_stack_height	(FiberController * i_fiberController)
	{
		size_t numWords = i_fiberController->m_stackSize / sizeof (size_t);
		auto words = (size_t *) i_fiberController->m_stack;

		size_t t = 0;
		while (t < numWords)
		{
			if (words [t])
				break;
			++t;
		}

		numWords -= t;
		return numWords * sizeof (size_t);
	}

	FiberController					m_homeFiber;
	FiberController *				m_activeFiber			= & m_homeFiber;

	struct StackRecord
	{
		void *		stack;
		size_t		size;
	};

	list <StackRecord>				m_stacks;
	vector <IJdFiber>				m_fibers;

};

#define __JdFiber_hpp__
#endif

max_stars_repo_name: soundandform/epigram

max_issues_repo_name: soundandform/epigram

max_forks_repo_name: soundandform/epigram


text:
/*
   Copyright 2021 The Silkworm Authors

   Licensed 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 <filesystem>
#include <iostream>

#include <CLI/CLI.hpp>
#include <boost/endian/conversion.hpp>

#include <silkworm/stagedsync/stagedsync.hpp>
#include <silkworm/common/log.hpp>
#include <silkworm/db/stages.hpp>

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

int main(int argc, char* argv[]) {
    CLI::App app{"Generates Hashed state"};

    std::string db_path{db::default_path()};
    bool full{false};
    bool incrementally{false};
    bool reset{false};
    app.add_option("--chaindata", db_path, "Path to a database populated by Erigon", true)
        ->check(CLI::ExistingDirectory);

    app.add_flag("--full", full, "Start making lookups from block 0");
    app.add_flag("--increment", incrementally, "Use incremental method");
    app.add_flag("--reset", reset, "Reset HashState");
    CLI11_PARSE(app, argc, argv);

    // Check data.mdb exists in provided directory
    fs::path db_file{fs::path(db_path) / fs::path("data.mdb")};
    if (!fs::exists(db_file)) {
        SILKWORM_LOG(LogLevel::Error) << "Can't find a valid Erigon data file in " << db_path << std::endl;
        return -1;
    }
    fs::path datadir(db_path);
    fs::path etl_path(datadir.parent_path() / fs::path("etl-temp"));

    lmdb::DatabaseConfig db_config{db_path};
    db_config.set_readonly(false);
    std::shared_ptr<lmdb::Environment> env{lmdb::get_env(db_config)};
    std::unique_ptr<lmdb::Transaction> txn{env->begin_rw_transaction()};

    try {
        if (full || reset) {
            txn->open(db::table::kHashedAccounts)->clear();
            txn->open(db::table::kHashedStorage)->clear();
            txn->open(db::table::kContractCode)->clear();
            db::stages::set_stage_progress(*txn, db::stages::kHashStateKey, 0);
            if (reset) {
                SILKWORM_LOG(LogLevel::Info) << "Reset Complete!" << std::endl;
                lmdb::err_handler(txn->commit());
                return 0;
            }
        }
        SILKWORM_LOG(LogLevel::Info) << "Starting HashState" << std::endl;

        auto last_processed_block_number{db::stages::get_stage_progress(*txn, db::stages::kHashStateKey)};
        if (last_processed_block_number != 0 || incrementally) {
            SILKWORM_LOG(LogLevel::Info) << "Starting Account Hashing" << std::endl;
            stagedsync::hashstate_promote(txn.get(), stagedsync::HashstateOperation::HashAccount);
            SILKWORM_LOG(LogLevel::Info) << "Starting Storage Hashing" << std::endl;
            stagedsync::hashstate_promote(txn.get(), stagedsync::HashstateOperation::HashStorage);
            SILKWORM_LOG(LogLevel::Info) << "Hashing Code Keys" << std::endl;
            stagedsync::hashstate_promote(txn.get(), stagedsync::HashstateOperation::Code);
        } else {
            stagedsync::hashstate_promote_clean_state(txn.get(), etl_path.string());
            stagedsync::hashstate_promote_clean_code(txn.get(), etl_path.string());
        }
        // Update progress height with last processed block
        db::stages::set_stage_progress(*txn, db::stages::kHashStateKey,
                                       db::stages::get_stage_progress(*txn, db::stages::kExecutionKey));
        lmdb::err_handler(txn->commit());
        txn.reset();
        SILKWORM_LOG(LogLevel::Info) << "All Done!" << std::endl;
    } catch (const std::exception& ex) {
        SILKWORM_LOG(LogLevel::Error) << ex.what() << std::endl;
        return -5;
    }
}

max_stars_repo_name: flashbots/silkworm

max_issues_repo_name: flashbots/silkworm

max_forks_repo_name: flashbots/silkworm


text:
// copyright (c) 2012-2013 the moorecoin core developers
// distributed under the mit software license, see the accompanying
// file copying or http://www.opensource.org/licenses/mit-license.php.

#include "util.h"

#include "support/allocators/secure.h"
#include "test/test_moorecoin.h"

#include <boost/test/unit_test.hpp>

boost_fixture_test_suite(allocator_tests, basictestingsetup)

// dummy memory page locker for platform independent tests
static const void *last_lock_addr, *last_unlock_addr;
static size_t last_lock_len, last_unlock_len;
class testlocker
{
public:
    bool lock(const void *addr, size_t len)
    {
        last_lock_addr = addr;
        last_lock_len = len;
        return true;
    }
    bool unlock(const void *addr, size_t len)
    {
        last_unlock_addr = addr;
        last_unlock_len = len;
        return true;
    }
};

boost_auto_test_case(test_lockedpagemanagerbase)
{
    const size_t test_page_size = 4096;
    lockedpagemanagerbase<testlocker> lpm(test_page_size);
    size_t addr;
    last_lock_addr = last_unlock_addr = 0;
    last_lock_len = last_unlock_len = 0;

    /* try large number of small objects */
    addr = 0;
    for(int i=0; i<1000; ++i)
    {
        lpm.lockrange(reinterpret_cast<void*>(addr), 33);
        addr += 33;
    }
    /* try small number of page-sized objects, straddling two pages */
    addr = test_page_size*100 + 53;
    for(int i=0; i<100; ++i)
    {
        lpm.lockrange(reinterpret_cast<void*>(addr), test_page_size);
        addr += test_page_size;
    }
    /* try small number of page-sized objects aligned to exactly one page */
    addr = test_page_size*300;
    for(int i=0; i<100; ++i)
    {
        lpm.lockrange(reinterpret_cast<void*>(addr), test_page_size);
        addr += test_page_size;
    }
    /* one very large object, straddling pages */
    lpm.lockrange(reinterpret_cast<void*>(test_page_size*600+1), test_page_size*500);
    boost_check(last_lock_addr == reinterpret_cast<void*>(test_page_size*(600+500)));
    /* one very large object, page aligned */
    lpm.lockrange(reinterpret_cast<void*>(test_page_size*1200), test_page_size*500-1);
    boost_check(last_lock_addr == reinterpret_cast<void*>(test_page_size*(1200+500-1)));

    boost_check(lpm.getlockedpagecount() == (
        (1000*33+test_page_size-1)/test_page_size + // small objects
        101 + 100 +  // page-sized objects
        501 + 500)); // large objects
    boost_check((last_lock_len & (test_page_size-1)) == 0); // always lock entire pages
    boost_check(last_unlock_len == 0); // nothing unlocked yet

    /* and unlock again */
    addr = 0;
    for(int i=0; i<1000; ++i)
    {
        lpm.unlockrange(reinterpret_cast<void*>(addr), 33);
        addr += 33;
    }
    addr = test_page_size*100 + 53;
    for(int i=0; i<100; ++i)
    {
        lpm.unlockrange(reinterpret_cast<void*>(addr), test_page_size);
        addr += test_page_size;
    }
    addr = test_page_size*300;
    for(int i=0; i<100; ++i)
    {
        lpm.unlockrange(reinterpret_cast<void*>(addr), test_page_size);
        addr += test_page_size;
    }
    lpm.unlockrange(reinterpret_cast<void*>(test_page_size*600+1), test_page_size*500);
    lpm.unlockrange(reinterpret_cast<void*>(test_page_size*1200), test_page_size*500-1);

    /* check that everything is released */
    boost_check(lpm.getlockedpagecount() == 0);

    /* a few and unlocks of size zero (should have no effect) */
    addr = 0;
    for(int i=0; i<1000; ++i)
    {
        lpm.lockrange(reinterpret_cast<void*>(addr), 0);
        addr += 1;
    }
    boost_check(lpm.getlockedpagecount() == 0);
    addr = 0;
    for(int i=0; i<1000; ++i)
    {
        lpm.unlockrange(reinterpret_cast<void*>(addr), 0);
        addr += 1;
    }
    boost_check(lpm.getlockedpagecount() == 0);
    boost_check((last_unlock_len & (test_page_size-1)) == 0); // always unlock entire pages
}

boost_auto_test_suite_end()

max_stars_repo_name: moorecoin/MooreCoinMiningAlgorithm

max_issues_repo_name: moorecoin/MooreCoinMiningAlgorithm

max_forks_repo_name: moorecoin/MooreCoinMiningAlgorithm


text:
#include "dns.hpp"
#include "udpv4client.hpp"
#include "tcpv4client.hpp"
#include "rrgenerator.hpp"
#include "shufflebytes.hpp"
#include <algorithm>
#include <arpa/inet.h>
#include <boost/program_options.hpp>
#include <cstring>
#include <iostream>
#include <fstream>
#include <time.h>

const uint32_t DEFAULT_INTERVAL_MSEC = 10;

namespace po = boost::program_options;

int main( int argc, char **argv )
{
    std::string target_server;
    uint16_t    target_port;
    std::string source_data;
    uint32_t    interval;
    uint64_t    count = 0;

    po::options_description desc( "query generator" );
    desc.add_options()( "help,h", "print this message" )

        ( "server,s",
          po::value<std::string>( &target_server )->default_value( "127.0.0.1" ),
          "target server address" )
        ( "port,p",
          po::value<uint16_t>( &target_port )->default_value( 53 ),
          "target server port" )
        ( "source,d",
          po::value<std::string>( &source_data ),
          "output filename for recording query messages" )
	( "interval,i",
          po::value<uint32_t>( &interval )->default_value( DEFAULT_INTERVAL_MSEC ) );

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

    if ( vm.count( "help" ) ) {
        std::cerr << desc << "\n";
        return 1;
    }

    if ( source_data == "" ) {
        std::cerr << "queries source data file must be specified." << std::endl;
        return 1;
    }

    std::fstream fin;
    std::istream *in;
    if ( source_data == "-" )
        in = &std::cin;
    else {
        fin.open( source_data.c_str(), std::fstream::in );
        in = &fin;
    }

    std::vector< std::vector<uint8_t> > queries;
    while ( ! in->eof() ) {
        char buf[100000];
        in->getline( buf, sizeof(buf) );
        uint32_t line_size = std::strlen( buf );
        if ( line_size == 0 )
            continue;

        // YYYY-mm-dd HH:MM:SS,<BASE64-STRING>
        // 12345678890123456789
        uint32_t base64_offset = std::strlen( "YYYY-mm-dd HH:MM:SS," );
        uint32_t base64_size = line_size - base64_offset;
        uint32_t query_size  = base64_size / 4 * 3;
        std::vector<uint8_t> query( query_size );

        std::cerr << "line size: "     << line_size     << ", "
                  << "base64 offset: " << base64_offset << ", "
                  << "base64 size: "   << base64_size   << ", "
                  << "query_size: "    << query_size    << std::endl;

        decodeFromBase64( buf + base64_offset, buf + line_size, &query[0] );
        queries.push_back( query );

        try {
            dns::MessageInfo query_info = dns::parseDNSMessage( &query[0], &query[0] + query.size() );
            std::cout << query_info << std::endl;
        }
        catch ( std::runtime_error &e ) {
            std::cerr << "parse error" << std::endl;
            std::cerr << e.what() << std::endl;
        }
        catch ( ... ) {
            std::cerr << "unknown parse error" << std::endl;
        }
    }

    while ( true ) {
        if ( count % 10000 == 0 )
            std::cerr << "sent " << count << std::endl;

	wait_msec( interval );
        count++;

        for ( auto message : queries ) {
            try {
                if ( message.size() < 1500 ) {
                    udpv4::ClientParameters udp_param;
                    udp_param.mAddress = target_server;
                    udp_param.mPort    = target_port;
                    udpv4::Client udp( udp_param );
                    udp.sendPacket( message );
                }
                else {
                    tcpv4::ClientParameters tcp_param;
                    tcp_param.mAddress = target_server;
                    tcp_param.mPort    = target_port;
                    tcpv4::Client tcp( tcp_param );
                    tcp.openSocket();

                    uint16_t query_size_data2 = htons( message.size() );
                    tcp.send( reinterpret_cast<const uint8_t *>( &query_size_data2 ), 2 );
                    tcp.send( message );

                    tcpv4::ConnectionInfo response_size_data = tcp.receive_data( 2 );
                    if ( response_size_data.getLength() < 2 )
                        continue;
                    uint16_t response_size = ntohs( *( reinterpret_cast<const uint16_t *>( response_size_data.getData() ) ) );

                    PacketData response_data;
                    while ( response_data.size() < response_size ) {
                        tcpv4::ConnectionInfo received_data = tcp.receive_data( response_size - response_data.size() );

                        response_data.insert( response_data.end(), received_data.begin(), received_data.end() );
                    }
                }

            }
            catch ( std::runtime_error &e )  {
                std::cerr << e.what() << std::endl;
            }
            catch ( ... ) {
                std::cerr << "other error" << std::endl;
            }

            return 0;
        }
    }
    return 0;
}

max_stars_repo_name: sischkg/nxnsattack

max_issues_repo_name: sischkg/dns-fuzz-server

max_forks_repo_name: sischkg/dns-fuzz-server


text:
#include "searchchunkswidget.h"
#include "ui_searchchunkswidget.h"

#include "./chunkcache.h"
#include "./chunkmath.hpp"
#include "./prioritythreadpool.h"
#include "./range.h"

#include <QVariant>
#include <QTreeWidgetItem>
#include <boost/noncopyable.hpp>

SearchChunksWidget::SearchChunksWidget(const SearchEntityWidgetInputC& input)
  : QWidget(input.parent)
    , ui(new Ui::SearchChunksWidget)
    , m_input(input)
    , m_invoker()
    , m_threadPool(m_input.threadpool)
    , m_searchRunning(false)
    , cancellation()
{
  ui->setupUi(this);

  connect(m_input.cache.data(), SIGNAL(chunkLoaded(const QSharedPointer<Chunk>&,int,int)),
          this, SLOT(chunkLoaded(const QSharedPointer<Chunk>&,int,int)));

  auto layout = new QHBoxLayout(ui->plugin_context);
  ui->plugin_context->setLayout(layout);
  layout->setSizeConstraint(QLayout::SizeConstraint::SetMinimumSize);
  layout->addWidget(&m_input.searchPlugin->getWidget());

  qRegisterMetaType<QSharedPointer<SearchPluginI::ResultListT> >("QSharedPointer<SearchPluginI::ResultListT>");
}

SearchChunksWidget::~SearchChunksWidget()
{
  cancelSearch();

  ui->plugin_context->layout()->removeWidget(&m_input.searchPlugin->getWidget());

  delete ui;
}

void SearchChunksWidget::on_pb_search_clicked()
{
  if (cancellation)
  {
    cancelSearch();
    return;
  }

  ui->pb_search->setText("Cancel");

  cancellation = CancellationPtr::create();
  auto weakCancel = cancellation.getWeakToken();

  m_chunksRequestedToSearchList.clear();

  ui->resultList->clearResults();
  ui->resultList->setPointOfInterest(m_input.posOfInterestProvider());

  const int radius = 1 + (ui->sb_radius->value() / CHUNK_SIZE);
  ui->progressBar->reset();
  ui->progressBar->setMaximum(radius * radius * 4);

  const bool successfull_init = m_input.searchPlugin->initSearch();
  if (!successfull_init)
  {
    return;
  }

  const QVector2D poi = getChunkCoordinates(m_input.posOfInterestProvider());
  const QVector2D radius2d(radius, radius);

  QRect searchRange((poi - radius2d).toPoint(), (poi + radius2d).toPoint());

  size_t count = 0;
  for (RectangleInnerToOuterIterator it(searchRange); it != it.end(); ++it)
  {
    const ChunkID id(it->getX(), it->getZ());

    requestSearchingOfChunk(id);

    if ((count++ % 100) == 0)
      QApplication::processEvents();

    if (weakCancel.isCanceled())
    {
      return;
    }
  }
}

void SearchChunksWidget::requestSearchingOfChunk(ChunkID id)
{
  m_chunksRequestedToSearchList[id] = true;

  ChunkCache::Locker locked_cache(*m_input.cache);

  QSharedPointer<Chunk> chunk;
  if (locked_cache.isCached(id, chunk)) // can return true and nullptr in case of inexistend chunk
  {
    chunkLoaded(chunk, id.getX(), id.getZ());
    return;
  }

  m_threadPool->enqueueJob([this, id, cancelToken = cancellation.getToken()]()
  {
    if (!cancelToken.isCanceled())
    {
      auto chunk = m_input.cache->getChunkSynchronously(id);

      m_invoker.invoke([this, chunk, id](){
        chunkLoaded(chunk, id.getX(), id.getZ());
      });
    }
  });
}

void SearchChunksWidget::chunkLoaded(const QSharedPointer<Chunk>& chunk, int x, int z)
{
  if (!cancellation || cancellation->isCanceled())
  {
    return;
  }

  const ChunkID id(x, z);

  bool& isChunkToSearch = m_chunksRequestedToSearchList[id];
  if (!isChunkToSearch)
  {
    return; // already searched or not of interest
  }

  isChunkToSearch = false; // mark as done

  if (chunk)
  {
    searchLoadedChunk(chunk);
  }
  else
  {
    addOneToProgress(id);
  }
}

Range<float> helperRangeCreation(const QCheckBox& checkBox, const QSpinBox& sb1, const QSpinBox& sb2)
{
  if (!checkBox.isChecked())
  {
    return Range<float>::max();
  }
  else
  {
    return Range<float>::createFromUnorderedParams(sb1.value(), sb2.value());
  }
}

void SearchChunksWidget::searchLoadedChunk(const QSharedPointer<Chunk>& chunk)
{
  const Range<float> range_y = helperRangeCreation(*ui->check_range_y, *ui->sb_y_start, *ui->sb_y_end);

  auto job = [this, chunk, range_y, cancelToken = cancellation.getToken(), searchPlug = m_input.searchPlugin]()
  {
    if (cancelToken.isCanceled())
    {
      return;
    }

    ChunkID id(chunk->getChunkX(), chunk->getChunkZ());

    auto results_tmp = searchPlug->searchChunk(*chunk);
    auto results = QSharedPointer<SearchPluginI::ResultListT>::create();

    for (const auto& result: results_tmp)
    {
      if (range_y.isInsideRange(result.pos.y()))
      {
        results->push_back(result);
      }
    }

    m_invoker.invoke([this, cancelToken = cancelToken.toWeakToken(), results, id](){
      if (cancelToken.isCanceled())
      {
        return;
      }

      displayResults(results, id);
    });
  };

  m_threadPool->enqueueJob(job, PriorityThreadPool::JobPrio::high);
}

void SearchChunksWidget::displayResults(QSharedPointer<SearchPluginI::ResultListT> results,
                                        ChunkID id)
{
  for (const auto& result: *results)
  {
    ui->resultList->addResult(result);
  }

  addOneToProgress(id);
}

void SearchChunksWidget::addOneToProgress(ChunkID /* id */)
{
  ui->progressBar->setValue(ui->progressBar->value() + 1);

  if (ui->progressBar->maximum() == ui->progressBar->value())
  {
    cancelSearch();
  }
}

void SearchChunksWidget::cancelSearch()
{
  cancellation.cancelAndWait();

  ui->pb_search->setText("Search");

  ui->resultList->searchDone();
}

void SearchChunksWidget::on_resultList_jumpTo(const QVector3D &pos)
{
  emit jumpTo(pos);
}

void SearchChunksWidget::on_resultList_highlightEntities(QVector<QSharedPointer<OverlayItem> > item)
{
  emit highlightEntities(item);
}


text:
/*===================================================================

MSI applications for interactive analysis in MITK (M2aia)

Copyright (c) Jonas Cordes

All rights reserved.

This software is distributed WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.

See LICENSE.txt for details.

===================================================================*/

#include "mitkIOUtil.h"
#include <m2CoreCommon.h>
#include <mitkTestFixture.h>
#include <mitkTestingMacros.h>

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

class m2CoreMappingsTestSuite : public mitk::TestFixture
{
  CPPUNIT_TEST_SUITE(m2CoreMappingsTestSuite);
  MITK_TEST(CreateSwitches);

  CPPUNIT_TEST_SUITE_END();

private:
  void Switch(m2::SpectrumFormatType e)
  {
#pragma push
#ifdef _WIN32
#pragma warning(error : 4062)
#else
#pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wswitch"
#endif
    switch (e)
    {
      case m2::SpectrumFormatType::ContinuousCentroid:
      case m2::SpectrumFormatType::ContinuousProfile:
      case m2::SpectrumFormatType::ProcessedCentroid:
      case m2::SpectrumFormatType::ProcessedProfile:
      case m2::SpectrumFormatType::None:
        // add your new case here
        {
          //////////////////////////////////////////////////////////////////////
          /////////////////// ATTENTION ////////////////////////////////////////
          //////////////////////////////////////////////////////////////////////
          // It looks like you have added a new enum value to the list.
          // Ensure that all enum values are well defined in the m2::CORE_MAPPINGS.
          // m2::CORE_MAPPINGS is defined in <m2CoreCommon.h>.
          // m2::CORE_MAPPINGS is used to transform strings into unsigned int values.
          //////////////////////////////////////////////////////////////////////
          /////////////////// ATTENTION ////////////////////////////////////////
          //////////////////////////////////////////////////////////////////////
        }
    }
#pragma pop
#pragma GCC diagnostic pop
  }

  void Switch(m2::OverviewSpectrumType e)
  {
#pragma push
#ifdef _WIN32
#pragma warning(error : 4062)
#else
#pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wswitch"
#endif
    switch (e)
    {
      case m2::OverviewSpectrumType::None:
      case m2::OverviewSpectrumType::Maximum:
      case m2::OverviewSpectrumType::Mean:
      case m2::OverviewSpectrumType::Median:
      case m2::OverviewSpectrumType::PeakIndicators:
      case m2::OverviewSpectrumType::Sum:
      case m2::OverviewSpectrumType::Variance:
        // add your new case here
        {
          //////////////////////////////////////////////////////////////////////
          /////////////////// ATTENTION ////////////////////////////////////////
          //////////////////////////////////////////////////////////////////////
          // It looks like you have added a new enum value to the list.
          // Ensure that all enum values are well defined in the m2::CORE_MAPPINGS.
          // m2::CORE_MAPPINGS is defined in <m2CoreCommon.h>.
          // m2::CORE_MAPPINGS is used to transform strings into unsigned int values.
          //////////////////////////////////////////////////////////////////////
          /////////////////// ATTENTION ////////////////////////////////////////
          //////////////////////////////////////////////////////////////////////
        }
    }
#pragma pop
#pragma GCC diagnostic pop
  }

  void Switch(m2::NumericType e)
  {
#pragma push
#ifdef _WIN32
#pragma warning(error : 4062)
#else
#pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wswitch"
#endif
    switch (e)
    {
      case m2::NumericType::Float:
      case m2::NumericType::Double:

        // add your new case here
        {
          //////////////////////////////////////////////////////////////////////
          /////////////////// ATTENTION ////////////////////////////////////////
          //////////////////////////////////////////////////////////////////////
          // It looks like you have added a new enum value to the list.
          // Ensure that all enum values are well defined in the m2::CORE_MAPPINGS.
          // m2::CORE_MAPPINGS is defined in <m2CoreCommon.h>.
          // m2::CORE_MAPPINGS is used to transform strings into unsigned int values.
          //////////////////////////////////////////////////////////////////////
          /////////////////// ATTENTION ////////////////////////////////////////
          //////////////////////////////////////////////////////////////////////
        }
    }
#pragma pop
#pragma GCC diagnostic pop
  }

public:
  void CreateSwitches()
  {
    Switch(m2::SpectrumFormatType::None);
    Switch(m2::NumericType::Float);
    Switch(m2::OverviewSpectrumType::None);

  }
};

MITK_TEST_SUITE_REGISTRATION(m2CoreMappings)


text:
#include <boost/algorithm/string/replace.hpp>

#include <parselib/datastructure/operations/generalop.hpp>
#include <parselib/parsers/naiveparsers.hpp>
#include <parselib/utils/io.hpp>

#include "grammar.h"

using namespace std ;

namespace parselib {

Grammar::Grammar () {
	production_rules = ProductionRules () ;
	labels = LabelReplacementMap () ;
	keeper = KeepingList () ;
	unitrelation = UnitRelation() ;
	strnodes = StrList () ;
	tokens = TokenList () ;
}

void Grammar::merge (Grammar grammar) {
	// unitrelation is computed later
	for (Token token : grammar.tokens) {
		tokens.push_back (token) ;
	}

	// loop through production_rules and merge
	for (auto item : grammar.production_rules) {

		//get key and rules
		std::string key = item.first ;
		Rules rules = item.second ;

		// if key if map then merge vectors
		if (production_rules.find(key) != production_rules.end() ) {
			for (Rule rule : rules) {
				production_rules[key].push_back(rule);
			}
		} else { //add new map entry
			production_rules[key] = rules ;
		}
	}

	//labels merge
	for (auto item : grammar.labels) {
		std::string key = item.first ;
		LabelReplacement mylabels = item.second ;

		if (labels.find(key) != labels.end()) {
			for (auto & sublab : mylabels) {
				std::string subkey = sublab.first ;
				labels[key][subkey] = sublab.second ;
			}
		} else {
			labels[key] = mylabels ;
		}
	}

	//keeper merge
	for (auto item : grammar.keeper) {
		std::string key = item.first ;
		StrList kept = item.second ;

		if (keeper.find(key) != keeper.end()) {
			for (std::string str : kept) {
				if (std::find(keeper[key].begin(), keeper[key].end(), str) == keeper[key].end()) {
					keeper[key].push_back(str);
				}
			}
		} else {
			keeper[key] = kept ;
		}

		// merge "all" entries
		for (std::string entry : kept) {
			//if entry not in keeper(all) add it, otherwise ignore it, already added
			if (std::find (keeper["all"].begin(), keeper["all"].end(), entry) == keeper["all"].end()) {
				keeper["all"].push_back(entry);
			}
		}
	}

	for (std::string s : grammar.strnodes) {
		strnodes.push_back(s) ;
	}
}

/*!
 * \brief parses a list of tokens in a grammar
 * \param tokenizedgrammar : TokenList : list of tokens represented by the lexed grammar
 * \param grammartokens : TokenList : list of tokens representing the lexed grammar
 */
void Grammar::makegrammar (TokenList tokenizedgrammar, TokenList grammartokens) {
	//ngp for naive grammar parser
	parsers::SequentialParser ngp = parsers::SequentialParser (tokenizedgrammar, grammartokens) ;

	ngp.parse () ;

	production_rules = ngp.production_rules ;
	tokens = ngp.tokens ;
	labels = ngp.labels ;
	strnodes = ngp.strnodes ;
	keeper = ngp.keeper ;

	*this = grammaroperators::eliminatedoubles (*this) ;
	//gramtest = checkproductionrules(self.production_rules) #is fuckedup
	//return gramtest
	return ;
}

bool Grammar::inKeeperKeys(string toktype) {
	for (auto item : keeper) {
		if (item.first == toktype) {
			return true ;
		}
	}
	return std::find(keeper["all"].begin(), keeper["all"].end(), toktype) != keeper["all"].end() ;
}

bool Grammar::inLabelsKeys(string toktype) {
	for (auto item : labels) {
		string key = item.first ;
		if (toktype == key) { //element type in keeper.keys()
			return true ;
		}
	}
	return false ;
}

bool Grammar::keyIsStr(string toktype) {
	for (string token : strnodes) {
		if (token == toktype) {
			return true ;
		}
	}
	return false ;
}

bool Grammar::isTokenSavable(string parent, string child) {
	if (parent == "AXIOM" ||
		child == production_rules["AXIOM"][0][0].value()
	) {
		return true ;
	}
	if (!inKeeperKeys(parent)) {
		return false ;
	}
	return std::find(keeper[parent].begin(), keeper[parent].end(), child) != keeper[parent].end() ;
}

/*!
 * \brief generates dot graph from a grammar and stores it in filename.png
 */
void Grammar::exportToFile(std::string filename) {
	std::string ss = "digraph {\n" ;
	for (auto item : production_rules) {
		std::string key = item.first ;
		boost::replace_all (key, "-", "");
		boost::replace_all (key, "/", "_");
		boost::replace_all (key, "[", "_");
		boost::replace_all (key, "]", "");
		boost::replace_all (key, ".", "_tok");
		Rules rules = item.second ;
		for (Rule rule : rules) {
			StrList r ;
			for (Token op : rule) {
				std::string val = op.value() ;
				boost::replace_all (val, "-", "");
				boost::replace_all (val, ".", "");
				boost::replace_all (val, "\'\'", "eps");
				boost::replace_all (val, "\"\"", "eps");
				boost::replace_all (val, "/", "_");
				boost::replace_all (val, "[", "_");
				boost::replace_all (val, "]", "");
				r.push_back(val) ;
			}
			ss += "\t" + key + " -> " ;
			ss += utils::join(r, "->") ;
			ss += " ;\n" ;
		}
	}
	ss += "}" ;
	std::ofstream filestream (filename + ".dot") ;
	if (!filestream.is_open ()) {
		utils::Printer::showerr("(exportToFile) Can't open file " + filename + ".dot");
		return ;
	}
	filestream << ss ;
	filestream.close() ;
	std::string cmd = "dot -Tpng -o " + filename + ".png " + filename + ".dot" ;
	std::system (cmd.c_str()) ;
	cmd = "rm " + filename + ".dot" ;
	std::system (cmd.c_str()) ;
}

/// \brief Screaming results for debug resons or verbose
string Grammar::getstr () {
	string text_rule = "" ;

	for (auto item : production_rules) {
		string key = item.first ;
		Rules rules = item.second ;

		text_rule += "\nRULE " + key + " = [\n\t" ;

		StrList rule_in_a_line = StrList () ;

		for (Rule rule : rules) {
			StrList ruletxt = StrList () ;
			for (Token opr : rule) {
				ruletxt.push_back(opr.type()+"("+opr.value()+")");
			}
			string thisrule = utils::join(ruletxt, " ") ;
			rule_in_a_line.push_back(thisrule);
		}
		text_rule += utils::join(rule_in_a_line, "\n\t") + "]" ;
	}

	text_rule += "\n\n" ;

	text_rule += "LABELS = [\n" ;
	for (auto item : labels) {
		string key = item.first ;
		LabelReplacement labmap = item.second ;
		text_rule += key + " {\n" ;
		for (auto lab : labmap) {
			text_rule += "\t" + lab.first + " : " + lab.second + "\n" ;
		}
		text_rule += "}\n" ;
	}
	text_rule += "]\n" ;

	text_rule += "STRUCT = [\n" ;
	for (auto item : keeper) {
		string key = item.first ;
		StrList listkeep = item.second ;
		text_rule += "" + key + " {\n\t" ;
		text_rule += utils::join(listkeep, "\n\t") ;
		text_rule += "}\n" ;
	}
	text_rule += "\n]\n\n" ;

	text_rule += "STRNODE = [\n" + utils::join(strnodes, "") + "\n]\n\n" ;

	for (Token tok : tokens) {
		string label = tok.type() ;
		string regx = tok.value() ;
		text_rule += "TOKEN " + label + " = regex('" + regx + "')\n" ;
	}

	text_rule += "UNIT = [\n" ;
	for (auto item : unitrelation) {
		string key = item.first ;
		StrList listkeep = item.second ;
		text_rule += "" + key + " {\n\t" ;
		text_rule += utils::join(listkeep, "\n\t") ;
		text_rule += "}\n" ;
	}
	text_rule += "\n]\n\n" ;

	return text_rule ;
}

}

max_stars_repo_name: omarboukhris/parselibcpp

max_issues_repo_name: omarboukhris/parselibcpp

max_forks_repo_name: omarboukhris/parselibcpp


text:
/**
 * @file   moustique.hh
 * @author Matthieu Garrigues <matthieu.garrigues@gmail.com> <matthieu.garrigues@gmail.com>
 * @date   Sat Mar 31 23:52:51 2018
 *
 * @brief  moustique wrapper.
 *
 *
 */
#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <errno.h>

#include <thread>
#include <vector>
#include <boost/context/all.hpp>

/**
 * Open a socket on port \port and call \conn_handler(int client_fd, auto read, auto write)
 * to process each incomming connection. This handle takes 3 argments:
 *               - int client_fd: the file descriptor of the socket.
 *               - int read(buf, max_size_to_read):
 *                       The callback that conn_handler can use to read on the socket.
 *                       If data is available, copy it into \buf, otherwise suspend the handler until
 *                       there is something to read.
 *                       Returns the number of char actually read, returns 0 if the connection has been lost.
 *               - bool write(buf, buf_size): return true on success, false on error.
 *                       The callback that conn_handler can use to write on the socket.
 *                       If the socket is ready to write, write \buf, otherwise suspend the handler until
 *                       it is ready.
 *                       Returns true on sucess, false if connection is lost.
 *
 * @param port  The server port.
 * @param socktype The socket type, SOCK_STREAM for TCP, SOCK_DGRAM for UDP.
 * @param nthreads Number of threads.
 * @param conn_handler The connection handler
 * @return false on error, true on success.
 */
template <typename H>
int moustique_listen(int port,
                     int socktype,
                     int nthreads,
                     H conn_handler);

// Same as above but take an already opened socket \listen_fd.
template <typename H>
int moustique_listen_fd(int listen_fd,
                        int nthreads,
                        H conn_handler);

namespace moustique_impl
{
  static int create_and_bind(int port, int socktype)
  {
    struct addrinfo hints;
    struct addrinfo *result, *rp;
    int s, sfd;

    char port_str[20];
    snprintf(port_str, sizeof(port_str), "%d", port);
    memset (&hints, 0, sizeof (struct addrinfo));
    hints.ai_family = AF_UNSPEC;     /* Return IPv4 and IPv6 choices */
    hints.ai_socktype = socktype; /* We want a TCP socket */
    hints.ai_flags = AI_PASSIVE;     /* All interfaces */

    s = getaddrinfo (NULL, port_str, &hints, &result);
    if (s != 0)
    {
      fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (s));
      return -1;
    }

    for (rp = result; rp != NULL; rp = rp->ai_next)
    {
      sfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
      if (sfd == -1)
        continue;

      s = bind (sfd, rp->ai_addr, rp->ai_addrlen);
      if (s == 0)
      {
        /* We managed to bind successfully! */
        break;
      }

      close (sfd);
    }

    if (rp == NULL)
    {
      fprintf (stderr, "Could not bind\n");
      return -1;
    }

    freeaddrinfo (result);

    return sfd;
  }

}

#define MOUSTIQUE_CHECK_CALL(CALL)                                      \
  {                                                                     \
    int ret = CALL;                                                     \
    if (-1 == ret)                                                      \
    {                                                                   \
      fprintf(stderr, "Error at %s:%i  error is: %s", __PRETTY_FUNCTION__, __LINE__, strerror(ret)); \
      return false;                                                     \
    }                                                                   \
  }

template <typename H>
int moustique_listen(int port,
                     int socktype,
                     int nthreads,
                     H conn_handler)
{
  return moustique_listen_fd(moustique_impl::create_and_bind(port, socktype), nthreads, conn_handler);
}

template <typename H>
int moustique_listen_fd(int listen_fd,
                        int nthreads,
                        H conn_handler)
{
  namespace ctx = boost::context;

  if (listen_fd < 0) return 0;
  int flags = fcntl (listen_fd, F_GETFL, 0);
  MOUSTIQUE_CHECK_CALL(fcntl(listen_fd, F_SETFL, flags | O_NONBLOCK));
  MOUSTIQUE_CHECK_CALL(::listen(listen_fd, SOMAXCONN));

  auto event_loop_fn = [listen_fd, conn_handler] {

    int epoll_fd = epoll_create1(0);

    auto epoll_ctl = [epoll_fd] (int fd, uint32_t flags)
    {
      epoll_event event;
      event.data.fd = fd;
      event.events = flags;
      MOUSTIQUE_CHECK_CALL(::epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &event));
      return 1;
    };

    epoll_ctl(listen_fd, EPOLLIN | EPOLLET);

    const int MAXEVENTS = 64;
    std::vector<ctx::continuation> fibers;

    // Event loop.
    epoll_event events[MAXEVENTS];
    while (true)
    {
      int n_events = epoll_wait (epoll_fd, events, MAXEVENTS, -1);
      for (int i = 0; i < n_events; i++)
      {
        if ((events[i].events & EPOLLERR) ||
            (events[i].events & EPOLLHUP))
        {
          close (events[i].data.fd);
          fibers[events[i].data.fd] = fibers[events[i].data.fd].resume();
          continue;
        }
        else if (listen_fd == events[i].data.fd) // New connection.
        {
          while(true)
          {
            struct sockaddr in_addr;
            socklen_t in_len;
            int infd;
            char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];

            in_len = sizeof in_addr;
            infd = accept (listen_fd, &in_addr, &in_len);
            if (infd == -1)
              break;
            getnameinfo (&in_addr, in_len,
                         hbuf, sizeof(hbuf),
                         sbuf, sizeof(sbuf),
                         NI_NUMERICHOST | NI_NUMERICSERV);

            MOUSTIQUE_CHECK_CALL(fcntl(infd, F_SETFL, fcntl(infd, F_GETFL, 0) | O_NONBLOCK));

            epoll_ctl(infd, EPOLLIN | EPOLLOUT | EPOLLET);

            if (int(fibers.size()) < infd + 1)
              fibers.resize(infd + 10);

            struct end_of_file {};
            fibers[infd] = ctx::callcc([fd=infd, &conn_handler]
                                       (ctx::continuation&& sink) {
                                         auto read = [fd, &sink] (char* buf, int max_size) {
                                           ssize_t count = ::read(fd, buf, max_size);
                                           while (count <= 0)
                                           {
                                             if ((count < 0 and errno != EAGAIN) or count == 0)
                                               return ssize_t(0);
                                             sink = sink.resume();
                                             count = ::read(fd, buf, max_size);
                                           }
                                           return count;
                                         };

                                         auto write = [fd, &sink] (const char* buf, int size) {
                                           const char* end = buf + size;
                                           ssize_t count = ::write(fd, buf, end - buf);
                                           if (count > 0) buf += count;
                                           while (buf != end)
                                           {
                                             if ((count < 0 and errno != EAGAIN) or count == 0)
                                               return false;
                                             sink = sink.resume();
                                             count = ::write(fd, buf, end - buf);
                                             if (count > 0) buf += count;
                                           }
                                           return true;
                                         };

                                         conn_handler(fd, read, write);
                                         close(fd);
                                         return std::move(sink);
                                       });

          }

        }
        else // Data available on existing sockets. Wake up the fiber associated with events[i].data.fd.
          fibers[events[i].data.fd] = fibers[events[i].data.fd].resume();
      }
    }

  };

  std::vector<std::thread> ths;
  for (int i = 0; i < nthreads; i++)
    ths.push_back(std::thread([&] { event_loop_fn(); }));

  for (auto& t : ths)
    t.join();

  close(listen_fd);

  return true;
}

ext: hh

max_stars_repo_name: NanXiao/moustique

max_issues_repo_name: NanXiao/moustique

max_forks_repo_name: NanXiao/moustique