text:
//
// $Id$
//
//
// Original author: Matt Chambers <matt.chambers42 .@. gmail.com>
//
// Copyright 2017 Matt Chambers - Nashville, TN 37221
//
// 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.
//

#ifndef _TIMSDATA_HPP_
#define _TIMSDATA_HPP_

#include "pwiz/utility/misc/Export.hpp"
#include "CompassData.hpp"
#include <boost/optional.hpp>
#include <boost/container/flat_map.hpp>
#include "timsdata_cpp_pwiz.h" // Derived from timsdata_cpp.h, has light changes to help with single-scan access efficiency

using boost::optional;
typedef timsdata::TimsData TimsBinaryData;

namespace pwiz {
namespace vendor_api {
namespace Bruker {

typedef boost::shared_ptr<TimsBinaryData> TimsBinaryDataPtr;
struct TimsSpectrum;
struct TimsDataImpl;

struct PasefPrecursorInfo
{
    int scanBegin, scanEnd; // scan end is inclusive
    double avgScanNumber; // average value from Precursors table
    double isolationMz;
    double isolationWidth;
    double collisionEnergy;
    double monoisotopicMz;
    int charge;
    double intensity;
};
typedef boost::shared_ptr<PasefPrecursorInfo> PasefPrecursorInfoPtr;

struct PWIZ_API_DECL TimsFrame
{

    TimsFrame(TimsDataImpl& timsDataImpl, int64_t frameId,
              int msLevel, double rt,
              double startMz, double endMz,
              double tic, double bpi,
              IonPolarity polarity, int scanMode, int numScans,
              const optional<uint64_t>& parentId,
              const optional<double>& precursorMz,
              const optional<double>& isolationWidth,
              const optional<int>& precursorCharge);

    int64_t frameId() const { return frameId_; }
    int numScans() const { return numScans_; }

    private:
    friend struct TimsSpectrum;
    friend struct TimsDataImpl;
    int64_t frameId_;
    int msLevel_;
    double rt_;
    optional<uint64_t> parentId_;
    double tic_;
    double bpi_;
    int numScans_;

    IonPolarity polarity_;

    std::pair<double, double> scanRange_;
    optional<int> chargeState_;
    optional<double> isolationWidth_;

    optional<double> precursorMz_;
    int scanMode_;

    map<int, size_t> scanIndexByScanNumber_; // for frame/scan -> index calculation with support for missing scans (e.g. allowMsMsWithoutPrecursor == false)

    vector<PasefPrecursorInfoPtr> pasef_precursor_info_;

    TimsDataImpl & timsDataImpl_;
    vector<double> oneOverK0_; // access by (scan number - 1)
};

typedef boost::shared_ptr<TimsFrame> TimsFramePtr;

struct PWIZ_API_DECL TimsSpectrum : public MSSpectrum
{
protected:
    TimsSpectrum(const TimsFramePtr& framePtr, int scanBegin) :
        frame_(*framePtr), scanBegin_(scanBegin) {}
public:
    virtual ~TimsSpectrum() {}

    virtual bool hasLineData() const;
    virtual bool hasProfileData() const;
    virtual size_t getLineDataSize() const;
    virtual size_t getProfileDataSize() const;
    virtual void getLineData(automation_vector<double>& mz, automation_vector<double>& intensities) const;
    virtual void getProfileData(automation_vector<double>& mz, automation_vector<double>& intensities) const;

    virtual double getTIC() const { return frame_.tic_; }
    virtual double getBPI() const { return frame_.bpi_; }

    virtual int getMSMSStage() const;
    virtual double getRetentionTime() const;
    virtual void getIsolationData(std::vector<double>& isolatedMZs, std::vector<IsolationMode>& isolationModes) const;
    virtual void getFragmentationData(std::vector<double>& fragmentedMZs, std::vector<FragmentationMode>& fragmentationModes) const;
    virtual IonPolarity getPolarity() const;

    virtual std::pair<double, double> getScanRange() const;
    virtual int getChargeState() const;
    virtual double getIsolationWidth() const;

    virtual bool isIonMobilitySpectrum() const { return oneOverK0() > 0; }
    virtual double oneOverK0() const;

    void getCombinedSpectrumData(pwiz::util::BinaryData<double>& mz, pwiz::util::BinaryData<double>& intensities, pwiz::util::BinaryData<double>& mobilities) const;
    size_t getCombinedSpectrumDataSize() const;
    virtual pwiz::util::IntegerSet getMergedScanNumbers() const;

    virtual bool HasPasefPrecursorInfo() const { return false; }
    virtual const PasefPrecursorInfo& GetPasefPrecursorInfo() const { return empty_; }

    int scanBegin() const { return scanBegin_; }
    virtual int scanEnd() const = 0;
    virtual bool isCombinedScans() const = 0;

    virtual MSSpectrumParameterListPtr parameters() const;

    protected:
    friend struct TimsDataImpl;

    const TimsFrame& frame_;
    const int scanBegin_;
    const static PasefPrecursorInfo empty_;

};

struct TimsSpectrumNonPASEF : public TimsSpectrum
{
    TimsSpectrumNonPASEF(const TimsFramePtr& framePtr, int scanBegin) :
        TimsSpectrum(framePtr, scanBegin) {}
    virtual ~TimsSpectrumNonPASEF() {}
    virtual int scanEnd() const { return scanBegin_; }
    virtual bool isCombinedScans() const { return false; }
};

struct TimsSpectrumCombinedNonPASEF : public TimsSpectrumNonPASEF
{
    TimsSpectrumCombinedNonPASEF(const TimsFramePtr& framePtr, int scanBegin, int scanEnd) :
        TimsSpectrumNonPASEF(framePtr, scanBegin), scanEnd_(scanEnd) {}
    virtual ~TimsSpectrumCombinedNonPASEF() {}
    virtual int scanEnd() const { return scanEnd_; }
    virtual bool isCombinedScans() const { return scanEnd_ != scanBegin_; }
private:
    int scanEnd_; // 0-based index, scanEnd is inclusive (so for unmerged spectrum, begin==end)
};

struct TimsSpectrumPASEF : public TimsSpectrum
{
    TimsSpectrumPASEF(const TimsFramePtr& framePtr, int scanBegin, const PasefPrecursorInfo& pasefPrecursorInfo) :
        TimsSpectrum(framePtr, scanBegin), pasefPrecursorInfo_(pasefPrecursorInfo) {}
    virtual ~TimsSpectrumPASEF() {}
    virtual int scanEnd() const { return scanBegin_; }
    virtual bool isCombinedScans() const { return false; }
    virtual bool HasPasefPrecursorInfo() const { return &pasefPrecursorInfo_ != &empty_; };
    virtual const PasefPrecursorInfo& GetPasefPrecursorInfo() const { return pasefPrecursorInfo_; }
private:
    const PasefPrecursorInfo& pasefPrecursorInfo_;
};

struct TimsSpectrumCombinedPASEF : public TimsSpectrumPASEF
{
    TimsSpectrumCombinedPASEF(const TimsFramePtr& framePtr, int scanBegin, int scanEnd, const PasefPrecursorInfo& pasefPrecursorInfo) :
        TimsSpectrumPASEF(framePtr, scanBegin, pasefPrecursorInfo), scanEnd_(scanEnd) {}

    virtual ~TimsSpectrumCombinedPASEF() {}
    virtual int scanEnd() const { return scanEnd_; }
    virtual bool isCombinedScans() const { return scanEnd_ != scanBegin_; }
private:
    int scanEnd_; // 0-based index, scanEnd is inclusive (so for unmerged spectrum, begin==end)
};

typedef boost::shared_ptr<TimsSpectrum> TimsSpectrumPtr;

struct PWIZ_API_DECL TimsDataImpl : public CompassData
{
    TimsDataImpl(const std::string& rawpath, bool combineIonMobilitySpectra, int preferOnlyMsLevel = 0, bool allowMsMsWithoutPrecursor = true);
    virtual ~TimsDataImpl() {}

    /// returns true if the source has MS spectra
    virtual bool hasMSData() const;

    /// returns true if the source has LC spectra or traces
    virtual bool hasLCData() const;

    /// returns true if the source is TIMS PASEF data
    virtual bool hasPASEFData() const;

    /// returns the number of spectra available from the MS source
    virtual size_t getMSSpectrumCount() const;

    /// returns a spectrum from the MS source
    virtual MSSpectrumPtr getMSSpectrum(int scan, DetailLevel detailLevel = DetailLevel_FullMetadata) const;

    virtual std::pair<size_t, size_t> getFrameScanPair(int scan) const;
    virtual size_t getSpectrumIndex(int frame, int scan) const;

    /// returns the number of sources available from the LC system
    virtual size_t getLCSourceCount() const;

    /// returns the number of spectra available from the specified LC source
    virtual size_t getLCSpectrumCount(int source) const;

    /// returns a source from the LC system
    virtual LCSpectrumSourcePtr getLCSource(int source) const;

    /// returns a spectrum from the specified LC source
    virtual LCSpectrumPtr getLCSpectrum(int source, int scan) const;

    virtual std::string getOperatorName() const;
    virtual std::string getAnalysisName() const ;
    virtual boost::local_time::local_date_time getAnalysisDateTime() const;
    virtual std::string getSampleName() const;
    virtual std::string getMethodName() const;
    virtual InstrumentFamily getInstrumentFamily() const;
    virtual int getInstrumentRevision() const;
    virtual std::string getInstrumentDescription() const;
    virtual InstrumentSource getInstrumentSource() const;
    virtual std::string getAcquisitionSoftware() const;
    virtual std::string getAcquisitionSoftwareVersion() const;

    private:
    std::string tdfFilepath_;
    TimsBinaryDataPtr tdfStoragePtr_;
    boost::container::flat_map<size_t, TimsFramePtr> frames_;
    std::vector<TimsSpectrumPtr> spectra_;
    std::string acquisitionSoftware_;
    std::string acquisitionSoftwareVersion_;
    InstrumentFamily instrumentFamily_;
    int instrumentRevision_;
    InstrumentSource instrumentSource_;
    std::string acquisitionDateTime_;
    std::string operatorName_;
    bool combineSpectra_;
    bool hasPASEFData_;
    int preferOnlyMsLevel_; // when nonzero, caller only wants spectra at this ms level
    bool allowMsMsWithoutPrecursor_; // when false, PASEF MS2 specta without precursor info will be excluded

    int64_t currentFrameId_; // used for cacheing frame contents

    protected:
    friend struct TimsFrame;
    friend struct TimsSpectrum;
    TimsBinaryData& tdfStorage_;

    ///
    /// cache entire frames while dealing with single spectrum access
    ///

    const timsdata::FrameProxy& readFrame(
        int64_t frame_id);     //< frame index

};

} // namespace Bruker
} // namespace vendor_api
} // namespace pwiz

#endif // _TIMSDATA_HPP_

max_stars_repo_name: austinkeller/pwiz

max_issues_repo_name: austinkeller/pwiz

max_forks_repo_name: austinkeller/pwiz


text:
#include <catch.hpp>
#include <facter/util/directory.hpp>
#include <boost/filesystem.hpp>
#include "../fixtures.hpp"

using namespace std;
using namespace facter::util;
using namespace facter::testing;

SCENARIO("listing files in a directory") {
    vector<string> files;
    GIVEN("no pattern") {
        directory::each_file(LIBFACTER_TESTS_DIRECTORY "/fixtures/execution/ls", [&](string const& file) {
            files.push_back(boost::filesystem::path(file).filename().string());
            return true;
        });
        sort(files.begin(), files.end());
        THEN("all files are returned") {
            REQUIRE(files.size() == 4);
            REQUIRE(files[0] == "file1.txt");
            REQUIRE(files[1] == "file2.txt");
            REQUIRE(files[2] == "file3.txt");
            REQUIRE(files[3] == "file4.txt");
        }
    }
    GIVEN("a file pattern") {
        directory::each_file(LIBFACTER_TESTS_DIRECTORY "/fixtures/execution/ls", [&](string const& file) {
            files.push_back(boost::filesystem::path(file).filename().string());
            return true;
        }, "^file[23].txt$");
        sort(files.begin(), files.end());
        THEN("only the matching files are returned") {
            REQUIRE(files.size() == 2);
            REQUIRE(files[0] == "file2.txt");
            REQUIRE(files[1] == "file3.txt");
        }
    }
    GIVEN("a callback that returns false") {
        directory::each_file(LIBFACTER_TESTS_DIRECTORY "/fixtures/execution/ls", [&](string const& file) {
            files.push_back(boost::filesystem::path(file).filename().string());
            return false;
        });
        THEN("only one file is returned") {
            REQUIRE(files.size() == 1);
        }
    }
}

SCENARIO("listing directories in a directory") {
    vector<string> subdirectories;
    GIVEN("no pattern") {
        directory::each_subdirectory(LIBFACTER_TESTS_DIRECTORY "/fixtures/facts/external", [&](string const& directory) {
            subdirectories.push_back(boost::filesystem::path(directory).filename().string());
            return true;
        });
        sort(subdirectories.begin(), subdirectories.end());
        THEN("all directories are returned") {
            REQUIRE(subdirectories.size() == 5);
            REQUIRE(subdirectories[0] == "json");
            REQUIRE(subdirectories[1] == "posix");
            REQUIRE(subdirectories[2] == "text");
            REQUIRE(subdirectories[3] == "windows");
            REQUIRE(subdirectories[4] == "yaml");
        }
    }
    GIVEN("a directory pattern") {
        directory::each_subdirectory(LIBFACTER_TESTS_DIRECTORY "/fixtures/facts/external", [&](string const& directory) {
            subdirectories.push_back(boost::filesystem::path(directory).filename().string());
            return true;
        }, "^[jy].*$");
        sort(subdirectories.begin(), subdirectories.end());
        THEN("only the matching directories are returned") {
            REQUIRE(subdirectories.size() == 2);
            REQUIRE(subdirectories[0] == "json");
            REQUIRE(subdirectories[1] == "yaml");
        }
    }
    GIVEN("a callback that returns false") {
        directory::each_subdirectory(LIBFACTER_TESTS_DIRECTORY "/fixtures/facts/external", [&](string const& directory) {
            subdirectories.push_back(boost::filesystem::path(directory).filename().string());
            return false;
        });
        THEN("only one directory is returned") {
            REQUIRE(subdirectories.size() == 1);
        }
    }
}

ext: cc

max_stars_repo_name: MikaelSmith/cfacter

max_issues_repo_name: MikaelSmith/cfacter

max_forks_repo_name: MikaelSmith/cfacter


text:
#ifndef PARAMETRIZER_H_
#define PARAMETRIZER_H_
#include <atomic>
#include <condition_variable>
#ifdef WITH_TBB
#include <tbb/tbb.h>
#endif

#include <list>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <map>
#include <set>
#include <unordered_set>
#include "adjacent-matrix.hpp"
#include "field-math.hpp"
#include "hierarchy.hpp"
#include "post-solver.hpp"
#include "serialize.hpp"
#include "disajoint-tree.hpp"
using namespace Eigen;

typedef std::pair<unsigned int, unsigned int> Edge;
typedef std::map<int, std::pair<int, int> > SingDictionary;

struct ExpandInfo
{
	ExpandInfo()
	{}
	int current_v;
	int singularity;
	int step;
	int edge_id;
	int prev;
};

class Parametrizer
{
public:
    Parametrizer()
    : flag_preserve_sharp(0), flag_adaptive_scale(0)
    {}
    // Mesh Initialization
	void Load(const char* filename);
    void ComputeMeshStatus();
    void ComputeSmoothNormal();
    void ComputeSharpEdges();
    void ComputeSharpO();
    void ComputeVertexArea();
	void Initialize(int faces);
    void ExtractGroups(std::vector<std::pair<std::pair<int, double>, Eigen::Vector2d> >& groups,  std::vector<int>& min_id, std::vector<Eigen::Vector3d>& positions, std::vector<std::pair<int, int> >& conv_indices, int approx_dis);

	// Singularity and Mesh property
    void AnalyzeValence();
	void ComputeOrientationSingularities();
	void ComputePositionSingularities();
    void Downsample(std::vector<std::pair<Vector4i, Vector4i> >& entries, std::vector<Vector3d>& textiles_positions,
        std::vector<std::pair<Vector4i, Vector4i> >& new_entries, std::vector<Vector3d>& new_textiles_positions,
        std::vector<int>& groups, std::vector<int>& groups_orient_diff, std::vector<int>& seeds);
    void QuantizeTextiles();

	// Integer Grid Map Pipeline
	void ComputeIndexMap(int with_scale = 0);
	void BuildEdgeInfo();
    void ComputeMaxFlow();
    void MarkInteger();
	void BuildIntegerConstraints();

    // Fix Flip
    void FixFlipHierarchy();
    void FixFlipSat();
    void FixHoles();
    void FixHoles(std::vector<int>& loop_vertices);
    void FixValence();
    double QuadEnergy(std::vector<int>& loop_vertices, std::vector<Vector4i>& res_quads, int level);

    // Quadmesh and IO
    void AdvancedExtractQuad();
    void BuildTriangleManifold(DisajointTree& disajoint_tree, std::vector<int>& edge, std::vector<int>& face, std::vector<DEdge>& edge_values, std::vector<Vector3i>& F2E, std::vector<Vector2i>& E2F, std::vector<Vector2i>& EdgeDiff, std::vector<Vector3i>& FQ);
	void OutputMesh(const char* obj_name);

    std::map<int, int> singularities;  // map faceid to valence (1 (valence=3) or 3(valence=5))
	std::map<int, Vector2i> pos_sing;
    MatrixXi pos_rank; // pos_rank(i, j) i \in [0, 3) jth face ith vertex  rotate by its value so that all thress vertices are in the same orientation
    MatrixXi pos_index; // pos_index(i x 2 + dim, j) i \in [0, 6) jth face ith vertex's (t_ij-t_ji)'s dim's dimenstion in the paper
	// input mesh
	MatrixXd V;
	MatrixXd N;
	MatrixXd Nf;
	MatrixXd FS;
	MatrixXd FQ;
	MatrixXi F;

    double normalize_scale;
    Vector3d normalize_offset;

	// data structures
    VectorXd rho;
	VectorXi V2E;
	VectorXi E2E;
	VectorXi boundary;
    VectorXi nonManifold; // nonManifold vertices, in boolean
	AdjacentMatrix adj;
	Hierarchy hierarchy;

	// Mesh Status;
	double surface_area;
	double scale;
	double average_edge_length;
	double max_edge_length;
	VectorXd A;

	// target mesh
	int num_vertices;
	int num_faces;

	//just for test
	DisajointTree disajoint_tree;

	int compact_num_v;
    std::vector<std::vector<int> > Vset;
	std::vector<Vector3d> O_compact;
    std::vector<Vector3d> Q_compact;
    std::vector<Vector3d> N_compact;
	std::vector<Vector4i> F_compact;
    std::set<std::pair<int, int> > Quad_edges;
    std::vector<int> V2E_compact;
    std::vector<int> E2E_compact;
    VectorXi boundary_compact;
    VectorXi nonManifold_compact;

	std::vector<int> bad_vertices;
	std::vector<double> counter;
    std::vector<int> sharp_edges;   // sharp_edges[deid]: whether deid is a sharp edge that should be preserved
    std::vector<int> allow_changes; // allow_changes[variable_id]: whether var can be changed based on sharp edges
    std::vector<Vector2i> edge_diff;  // edge_diff[edgeIds[i](j)]:  t_ij+t_ji under edge_values[edgeIds[i](j)].x's Q value
    std::vector<DEdge> edge_values;  // see above
    std::vector<Vector3i> face_edgeIds; // face_edgeIds[i](j): ith face jth edge's "undirected edge ID"

    // face_edgeOrients[i](j): Rotate from edge_diff space
    //    (a) initially, to F(0, i)'s Q space
    //    (b) later on, to a global Q space where some edges are fixed
    std::vector<Vector3i> face_edgeOrients;

    // variable[i].first: indices of the two equations corresponding to variable i
    // variable[i].second: number of positive minus negative of variables' occurances
	std::vector<std::pair<Vector2i, int> > variables;

    struct QuadInfo
    {
        QuadInfo()
        : patchId(-1), coordinate(0x10000000,0x10000000), singular(0), edge(0)
        {}
        int patchId;
        Vector2i coordinate;
        int singular;
        int edge;
    };
    std::vector<QuadInfo> quad_info;

    // scale
    void ComputeInverseAffine();
    void EstimateSlope();
    std::vector<MatrixXd> triangle_space;

    // flag
    int flag_preserve_sharp;
    int flag_adaptive_scale;
    int gen_normalize;
    float unit;
    int num_original_v;
    bool gen_textiles, gen_pool, gen_dialation, gen_entry, gen_frames;
    int num_levels, kernel_size, rosy;
    std::string indices_file, position_file, entry_file, frames_file;
};

extern void generate_adjacency_matrix_uniform(const MatrixXi& F, const VectorXi& V2E,
                                              const VectorXi& E2E, const VectorXi& nonManifold,
                                              AdjacentMatrix& adj);

#endif

ext: hpp

max_stars_repo_name: hjwdzh/TextureNet

max_issues_repo_name: jtpils/TextureNet

max_forks_repo_name: jtpils/TextureNet


text:
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include <boost/test/unit_test.hpp>
#include "armnnTfParser/ITfParser.hpp"
#include "ParserPrototxtFixture.hpp"

BOOST_AUTO_TEST_SUITE(TensorflowParser)

struct Pooling2dFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser>
{
    explicit Pooling2dFixture(const char* poolingtype, std::string dataLayout, std::string paddingOption)
    {
        m_Prototext =  "node {\n"
            "  name: \"Placeholder\"\n"
            "  op: \"Placeholder\"\n"
            "  attr {\n"
            "    key: \"dtype\"\n"
            "    value {\n"
            "      type: DT_FLOAT\n"
            "    }\n"
            "  }\n"
            "  attr {\n"
            "    key: \"value\"\n"
            "    value {\n"
            "      tensor {\n"
            "        dtype: DT_FLOAT\n"
            "        tensor_shape {\n"
            "        }\n"
            "      }\n"
            "    }\n"
            "   }\n"
            "  }\n"
            "node {\n"
            "  name: \"";
        m_Prototext.append(poolingtype);
        m_Prototext.append("\"\n"
                               "  op: \"");
        m_Prototext.append(poolingtype);
        m_Prototext.append("\"\n"
                               "  input: \"Placeholder\"\n"
                               "  attr {\n"
                               "    key: \"T\"\n"
                               "    value {\n"
                               "      type: DT_FLOAT\n"
                               "    }\n"
                               "  }\n"
                               "  attr {\n"
                               "    key: \"data_format\"\n"
                               "    value {\n"
                               "      s: \"");
        m_Prototext.append(dataLayout);
        m_Prototext.append("\"\n"
                               "    }\n"
                               "  }\n"
                               "  attr {\n"
                               "    key: \"ksize\"\n"
                               "    value {\n"
                               "      list {\n"

                               "        i: 1\n");
        if(dataLayout == "NHWC")
        {
            m_Prototext.append("        i: 2\n"
                               "        i: 2\n"
                               "        i: 1\n");
        }
        else
        {
            m_Prototext.append("        i: 1\n"
                               "        i: 2\n"
                               "        i: 2\n");
        }
        m_Prototext.append(
                               "      }\n"
                               "    }\n"
                               "  }\n"
                               "  attr {\n"
                               "    key: \"padding\"\n"
                               "    value {\n"
                               "      s: \"");
        m_Prototext.append(paddingOption);
        m_Prototext.append(
                               "\"\n"
                               "    }\n"
                               "  }\n"
                               "  attr {\n"
                               "    key: \"strides\"\n"
                               "    value {\n"
                               "      list {\n"
                               "        i: 1\n"
                               "        i: 1\n"
                               "        i: 1\n"
                               "        i: 1\n"
                               "      }\n"
                               "    }\n"
                               "  }\n"
                               "}\n");

        if(dataLayout == "NHWC")
        {
            SetupSingleInputSingleOutput({ 1, 2, 2, 1 }, "Placeholder", poolingtype);
        }
        else
        {
            SetupSingleInputSingleOutput({ 1, 1, 2, 2 }, "Placeholder", poolingtype);
        }
    }
};

struct MaxPoolFixtureNhwcValid : Pooling2dFixture
{
    MaxPoolFixtureNhwcValid() : Pooling2dFixture("MaxPool", "NHWC", "VALID") {}
};
BOOST_FIXTURE_TEST_CASE(ParseMaxPoolNhwcValid, MaxPoolFixtureNhwcValid)
{
    RunTest<4>({1.0f, 2.0f, 3.0f, -4.0f}, {3.0f});
}

struct MaxPoolFixtureNchwValid : Pooling2dFixture
{
    MaxPoolFixtureNchwValid() : Pooling2dFixture("MaxPool", "NCHW", "VALID") {}
};
BOOST_FIXTURE_TEST_CASE(ParseMaxPoolNchwValid, MaxPoolFixtureNchwValid)
{
    RunTest<4>({1.0f, 2.0f, 3.0f, -4.0f}, {3.0f});
}

struct MaxPoolFixtureNhwcSame : Pooling2dFixture
{
    MaxPoolFixtureNhwcSame() : Pooling2dFixture("MaxPool", "NHWC", "SAME") {}
};
BOOST_FIXTURE_TEST_CASE(ParseMaxPoolNhwcSame, MaxPoolFixtureNhwcSame)
{
    RunTest<4>({1.0f, 2.0f, 3.0f, -4.0f}, {3.0f, 2.0f, 3.0f, -4.0f});
}

struct MaxPoolFixtureNchwSame : Pooling2dFixture
{
    MaxPoolFixtureNchwSame() : Pooling2dFixture("MaxPool", "NCHW", "SAME") {}
};
BOOST_FIXTURE_TEST_CASE(ParseMaxPoolNchwSame, MaxPoolFixtureNchwSame)
{
    RunTest<4>({1.0f, 2.0f, 3.0f, -4.0f}, {3.0f, 2.0f, 3.0f, -4.0f});
}

struct AvgPoolFixtureNhwcValid : Pooling2dFixture
{
    AvgPoolFixtureNhwcValid() : Pooling2dFixture("AvgPool", "NHWC", "VALID") {}
};
BOOST_FIXTURE_TEST_CASE(ParseAvgPoolNhwcValid, AvgPoolFixtureNhwcValid)
{
    RunTest<4>({1.0f, 2.0f, 3.0f, 4.0f}, {2.5f});
}

struct AvgPoolFixtureNchwValid : Pooling2dFixture
{
    AvgPoolFixtureNchwValid() : Pooling2dFixture("AvgPool", "NCHW", "VALID") {}
};
BOOST_FIXTURE_TEST_CASE(ParseAvgPoolNchwValid, AvgPoolFixtureNchwValid)
{
    RunTest<4>({1.0f, 2.0f, 3.0f, 4.0f}, {2.5f});
}

struct AvgPoolFixtureNhwcSame : Pooling2dFixture
{
    AvgPoolFixtureNhwcSame() : Pooling2dFixture("AvgPool", "NHWC", "SAME") {}
};
BOOST_FIXTURE_TEST_CASE(ParseAvgPoolNhwcSame, AvgPoolFixtureNhwcSame)
{
    RunTest<4>({1.0f, 2.0f, 3.0f, 4.0f}, {2.5f, 3.0f, 3.5f, 4.0f});
}

struct AvgPoolFixtureNchwSame : Pooling2dFixture
{
    AvgPoolFixtureNchwSame() : Pooling2dFixture("AvgPool", "NCHW", "SAME") {}
};
BOOST_FIXTURE_TEST_CASE(ParseAvgPoolNchwSame, AvgPoolFixtureNchwSame)
{
    RunTest<4>({1.0f, 2.0f, 3.0f, 4.0f}, {2.5f, 3.0f, 3.5f, 4.0f});
}

BOOST_AUTO_TEST_SUITE_END()

ext: cpp


text: #include <boost/fusion/functional/generation.hpp>

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text:
/**
 * @file
 *
 * 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
 */

#define TENUM_USE_SHORTCUTS 1
#include "tenum.hpp"

#define BOOST_TEST_MODULE tenum_dynamic_enum_tests
#include <boost/test/unit_test.hpp>

lte_dynamic_enum(dynamic_enum,
    lte_ev(value1,2)
    lte_evn(value2,5,"test_value2")
    lte_e(value3),
    value3+10,
    ::boost::uint16_t
)

/**
 * @defgroup dynamic_enum_tests Dynamic enums test cases
 */BOOST_AUTO_TEST_SUITE(dynamic_enum_tests)

/**
 * @class test_dynamic_enum_from_offset_serialization
 * @brief Tests dynamic enum construction from computed constant and serialization.
 * @ingroup dynamic_enum_tests
 */
BOOST_AUTO_TEST_CASE(test_dynamic_enum_from_offset_serialization) {
  dynamic_enum_t enum_value = static_cast< dynamic_enum_t > (dynamic_enum::value2 - 1);
  ::std::string string_value;
  ::std::stringstream stream;
  stream << enum_value;
  stream >> string_value;
  // (value2 - 1) is 4, which is (value1 + 2)
  BOOST_CHECK_EQUAL(string_value,"value1#2");
}

/**
 * @class test_dynamic_enum_serialization_explicit
 * @brief Tests dynamic enum serialization with an explicit string equivalent.
 * @ingroup dynamic_enum_tests
 */
BOOST_AUTO_TEST_CASE(test_dynamic_enum_serialization_explicit) {
  dynamic_enum_t enum_value = static_cast< dynamic_enum_t > (dynamic_enum::value2);
  ::std::string string_value;
  ::std::stringstream stream;
  stream << enum_value;
  stream >> string_value;
  BOOST_CHECK_EQUAL(string_value,"test_value2");
}

/**
 * @class test_dynamic_enum_from_integer_serialization
 * @brief Tests dynamic enum serialization from an integer value.
 * @ingroup dynamic_enum_tests
 */
BOOST_AUTO_TEST_CASE(test_dynamic_enum_from_integer_serialization) {
  dynamic_enum_t enum_value = static_cast< dynamic_enum_t > (3);
  ::std::string string_value;
  ::std::stringstream stream;
  stream << enum_value;
  stream >> string_value;
  BOOST_CHECK_EQUAL(string_value,"value1#1");
}

/**
 * @class test_dynamic_enum_deserialization
 * @brief Tests dynamic enum deserialization with offset and comparison to integer and enum values.
 * @ingroup dynamic_enum_tests
 */
BOOST_AUTO_TEST_CASE(test_dynamic_enum_deserialization) {
  dynamic_enum_t enum_value;
  ::std::string string_value = "value3#4";
  ::std::stringstream stream;
  stream << string_value;
  stream >> enum_value;
  BOOST_CHECK_EQUAL(enum_value,static_cast< dynamic_enum_t > (dynamic_enum::value3 + 4));
  BOOST_CHECK_EQUAL(enum_value,static_cast< dynamic_enum_t > (10));
}

/**
 * @class test_dynamic_enum_unknown_serialization
 * @brief Tests dynamic enum unknown value serialization.
 * @ingroup dynamic_enum_tests
 */
BOOST_AUTO_TEST_CASE(test_dynamic_enum_unknown_serialization) {
  dynamic_enum_t enum_value = static_cast< dynamic_enum_t > (dynamic_enum::lte_unknown);
  ::std::string string_value;
  ::std::stringstream stream;
  stream << enum_value;
  stream >> string_value;
  BOOST_CHECK_EQUAL(string_value,"lte_unknown");
}

/**
 * @class test_dynamic_enum_out_of_range_serialization
 * @brief Tests dynamic enum out of range value serialization to unknown.
 * @ingroup dynamic_enum_tests
 */
BOOST_AUTO_TEST_CASE(test_dynamic_enum_out_of_range_serialization) {
  dynamic_enum_t enum_value = static_cast< dynamic_enum_t > (0);
  ::std::string string_value;
  ::std::stringstream stream;
  stream << enum_value;
  stream >> string_value;
  BOOST_CHECK_EQUAL(string_value,"lte_unknown");
}

/**
 * @class test_dynamic_enum_out_of_range_deserialization
 * @brief Tests dynamic enum out of range value deserialization to unknown.
 * @ingroup dynamic_enum_tests
 */
BOOST_AUTO_TEST_CASE(test_dynamic_enum_out_of_range_deserialization) {
  dynamic_enum_t enum_value;
  ::std::string string_value = "value3#123456789";
  ::std::stringstream stream;
  stream << string_value;
  stream >> enum_value;
  BOOST_CHECK_EQUAL(enum_value,static_cast< dynamic_enum_t > (dynamic_enum::lte_unknown));
  BOOST_CHECK_EQUAL(enum_value,static_cast< dynamic_enum_t > (static_cast< int >(dynamic_enum::value3+10)));
}

/**
 * @class test_dynamic_enum_empty_deserialization
 * @brief Tests dynamic enum empty string deserialization to unknown constant.
 * @ingroup dynamic_enum_tests
 */
BOOST_AUTO_TEST_CASE(test_dynamic_enum_empty_deserialization) {
  dynamic_enum_t enum_value;
  ::std::string string_value = "";
  ::std::stringstream stream;
  stream << string_value;
  stream >> enum_value;
  BOOST_CHECK_EQUAL(enum_value,static_cast< dynamic_enum_t > (dynamic_enum::lte_unknown));
  BOOST_CHECK_EQUAL(enum_value,static_cast< dynamic_enum_t > (static_cast< int >(dynamic_enum::value3+10)));
}

/**
 * @class test_dynamic_enum_correct_deserialization
 * @brief Tests dynamic enum value deserialization and serialization to correct value.
 * @ingroup dynamic_enum_tests
 */
BOOST_AUTO_TEST_CASE(test_dynamic_enum_correct_deserialization) {
  dynamic_enum_t enum_value;
  ::std::string string_value = "value1#3";
  ::std::stringstream stream;
  stream << string_value;
  stream >> enum_value;

  ::std::stringstream outstream;
  outstream << enum_value;
  outstream >> string_value;
  BOOST_CHECK_EQUAL(string_value,"test_value2");
}

/**
 * @class test_dynamic_enum_integers_eq
 * @brief Tests dynamic enum integer equality operator.
 * @ingroup dynamic_enum_tests
 */
BOOST_AUTO_TEST_CASE(test_dynamic_enum_integers_eq) {
  dynamic_enum_t enum_value = dynamic_enum::value1;
  ::boost::uint64_t integer_value = 2;
  BOOST_CHECK(integer_value == enum_value);
  BOOST_CHECK(enum_value == integer_value);
}

/**
 * @class test_dynamic_enum_integers_neq
 * @brief Tests dynamic enum integer inequality operator.
 * @ingroup dynamic_enum_tests
 */
BOOST_AUTO_TEST_CASE(test_dynamic_enum_integers_neq) {
  dynamic_enum_t enum_value = dynamic_enum::value1;
  ::boost::uint64_t integer_value = 0;
  BOOST_CHECK(integer_value != enum_value);
  BOOST_CHECK(enum_value != integer_value);
}

/**
 * @class test_dynamic_enum_integers_bitwise_and
 * @brief Tests integer bitwise and operator with dynamic enum.
 * @ingroup dynamic_enum_tests
 */
BOOST_AUTO_TEST_CASE(test_dynamic_enum_integers_bitwise_and) {
  dynamic_enum_t enum_value = dynamic_enum::value1;
  ::boost::uint64_t integer_value1 = 0b011 & enum_value;
  BOOST_CHECK(integer_value1 == 0b010);
  ::boost::uint64_t integer_value2 = enum_value & 0b011;
  BOOST_CHECK(integer_value2 == 0b010);
}

/**
 * @class test_dynamic_enum_integers_bitwise_or
 * @brief Tests integer bitwise or operator with dynamic enum.
 * @ingroup dynamic_enum_tests
 */
BOOST_AUTO_TEST_CASE(test_dynamic_enum_integers_bitwise_or) {
  dynamic_enum_t enum_value = dynamic_enum::value1;
  ::boost::uint64_t integer_value1 = 0b001 | enum_value;
  BOOST_CHECK(integer_value1 == 0b011);
  ::boost::uint64_t integer_value2 = enum_value | 0b001;
  BOOST_CHECK(integer_value2 == 0b011);
}

/**
 * @class test_dynamic_enum_integers_bitwise_xor
 * @brief Tests integer bitwise xor operator with dynamic enum.
 * @ingroup dynamic_enum_tests
 */
BOOST_AUTO_TEST_CASE(test_dynamic_enum_integers_bitwise_xor) {
  dynamic_enum_t enum_value = dynamic_enum::value1;
  ::boost::uint64_t integer_value1 = 0b011 ^ enum_value;
  BOOST_CHECK(integer_value1 == 0b001);
  ::boost::uint64_t integer_value2 = enum_value ^ 0b011;
  BOOST_CHECK(integer_value2 == 0b001);
}

/**
 * @class test_dynamic_enum_integers_bitwise_and_assign
 * @brief Tests integer bitwise and assignation operator with dynamic enum.
 * @ingroup dynamic_enum_tests
 */
BOOST_AUTO_TEST_CASE(test_dynamic_enum_integers_bitwise_and_assign) {
  dynamic_enum_t enum_value = dynamic_enum::value1;
  ::boost::uint64_t integer_value = 0b011;
  integer_value &= enum_value;
  BOOST_CHECK(integer_value == 0b010);
}

/**
 * @class test_dynamic_enum_integers_bitwise_or_assign
 * @brief Tests integer bitwise or assignation operator with dynamic enum.
 * @ingroup dynamic_enum_tests
 */
BOOST_AUTO_TEST_CASE(test_dynamic_enum_integers_bitwise_or_assign) {
  dynamic_enum_t enum_value = dynamic_enum::value1;
  ::boost::uint64_t integer_value = 0b001;
  integer_value |= enum_value;
  BOOST_CHECK(integer_value == 0b011);
}

/**
 * @class test_dynamic_enum_integers_bitwise_xor_assign
 * @brief Tests integer bitwise xor assignation operator with dynamic enum.
 * @ingroup dynamic_enum_tests
 */
BOOST_AUTO_TEST_CASE(test_dynamic_enum_integers_bitwise_xor_assign) {
  dynamic_enum_t enum_value = dynamic_enum::value1;
  ::boost::uint64_t integer_value = 0b011;
  integer_value ^= enum_value;
  BOOST_CHECK(integer_value == 0b001);
}

BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: berenm/libtenum

max_issues_repo_name: berenm/libtenum

max_forks_repo_name: berenm/libtenum


text:
#include <pybind11/pybind11.h>
#include <pybind11/eigen.h>
#include <pybind11/stl.h>
#include <ceres/ceres.h>
#include <Eigen/Core>
#include <SO3.h>
#include <SE3.h>

namespace py = pybind11;

// AutoDiff local parameterization for the SO3 rotation [q] object.
// The boxplus operator informs Ceres how the manifold evolves and also
// allows for the calculation of derivatives.
struct SO3Plus {
  // boxplus operator for both doubles and jets
  template<typename T>
  bool operator()(const T* x, const T* delta, T* x_plus_delta) const
  {
    SO3<T> X(x);
    Eigen::Map<const Eigen::Matrix<T, 3, 1>> dX(delta);

    SO3<T> Y = X + dX;

    Eigen::Map<Eigen::Matrix<T, 4, 1>> Yvec(x_plus_delta);
    Yvec << Y.array();

    return true;
  }

  // local parameterization generator--ONLY FOR PYTHON WRAPPER
  static ceres::LocalParameterization *Create() {
    return new ceres::AutoDiffLocalParameterization<SO3Plus,
                                                    4,
                                                    3>(new SO3Plus);
  }
};

// AutoDiff local parameterization for the compact SE3 pose [t q] object.
// The boxplus operator informs Ceres how the manifold evolves and also
// allows for the calculation of derivatives.
struct SE3Plus {
  // boxplus operator for both doubles and jets
  template<typename T>
  bool operator()(const T* x, const T* delta, T* x_plus_delta) const
  {
    SE3<T> X(x);
    Eigen::Map<const Eigen::Matrix<T, 6, 1>> dX(delta);

    SE3<T> Y = X + dX;

    Eigen::Map<Eigen::Matrix<T, 7, 1>> Yvec(x_plus_delta);
    Yvec << Y.array();

    return true;
  }

  // local parameterization generator--ONLY FOR PYTHON WRAPPER
  static ceres::LocalParameterization *Create() {
    return new ceres::AutoDiffLocalParameterization<SE3Plus,
                                                    7,
                                                    6>(new SE3Plus);
  }
};

// AutoDiff cost function (factor) for the difference between two rotations.
// Weighted by measurement covariance, Qij_.
class SO3Functor
{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  // store measured relative pose and inverted covariance matrix
  SO3Functor(Eigen::Matrix<double,4,1> &qi_vec, Eigen::Matrix<double,3,3> &Qij)
  : q_(qi_vec)
  {
    Qij_inv_ = Qij.inverse();
  }

  // templated residual definition for both doubles and jets
  // basically a weighted implementation of boxminus using Eigen templated types
  template<typename T>
  bool operator()(const T* _q_hat, T* _res) const
  {
    SO3<T> q_hat(_q_hat);
    Eigen::Map<Eigen::Matrix<T,3,1>> r(_res);

    r = Qij_inv_.cast<T>() * (q_hat - q_.cast<T>());

    return true;
  }

  // cost function generator--ONLY FOR PYTHON WRAPPER
  static ceres::CostFunction *Create(Eigen::Matrix<double,4,1> &qi, Eigen::Matrix<double,3,3> &Qij) {
    return new ceres::AutoDiffCostFunction<SO3Functor,
                                           3,
                                           4>(new SO3Functor(qi, Qij));
  }

private:
  SO3d q_;
  Eigen::Matrix<double,3,3> Qij_inv_;
};

// AutoDiff cost function (factor) for the difference between a measured 3D
// relative transform, Xij = (tij_, qij_), and the relative transform between two
// estimated poses, Xi_hat and Xj_hat. Weighted by measurement covariance, Qij_.
class SE3Functor
{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  // store measured relative pose and inverted covariance matrix
  SE3Functor(Eigen::Matrix<double,7,1> &Xij_vec, Eigen::Matrix<double,6,6> &Qij) : Xij_(Xij_vec)
  {
    Qij_inv_ = Qij.inverse();
  }

  // templated residual definition for both doubles and jets
  // basically a weighted implementation of boxminus using Eigen templated types
  template<typename T>
  bool operator()(const T* _Xi_hat, const T* _Xj_hat, T* _res) const
  {
    SE3<T> Xi_hat(_Xi_hat);
    SE3<T> Xj_hat(_Xj_hat);
    Eigen::Map<Eigen::Matrix<T,6,1>> r(_res);

    r = Qij_inv_.cast<T>() * (Xi_hat.inverse() * Xj_hat - Xij_.cast<T>());

    return true;
  }

  // cost function generator--ONLY FOR PYTHON WRAPPER
  static ceres::CostFunction *Create(Eigen::Matrix<double,7,1> &Xij, Eigen::Matrix<double,6,6> &Qij) {
    return new ceres::AutoDiffCostFunction<SE3Functor,
                                           6,
                                           7,
                                           7>(new SE3Functor(Xij, Qij));
  }

private:
  SE3d Xij_;
  Eigen::Matrix<double,6,6> Qij_inv_;
};

void add_custom_cost_functions(py::module &m) {

  // Use pybind11 code to wrap your own cost function which is defined in C++

  // SO3 Factors
  m.def("SO3Parameterization", &SO3Plus::Create);
  m.def("SO3Factor", &SO3Functor::Create);

  // SE3 Factors
  m.def("SE3Parameterization", &SE3Plus::Create);
  m.def("SE3Factor", &SE3Functor::Create);

}

max_stars_repo_name: goromal/accel

max_issues_repo_name: goromal/accel

max_forks_repo_name: goromal/accel


text:
#include <iostream>                // std::cout/endl
#include <stdexcept>               // std::invalid_argument
#include <string>                  // std::string/stoi
#include <boost/lexical_cast.hpp>  // boost::lexical_cast/bad_lexical_cast

using namespace std;
using boost::bad_lexical_cast;
using boost::lexical_cast;

int main()
{
    int d = 42;
    auto d_str = lexical_cast<string>(d);
    cout << d_str << endl;

    auto f = lexical_cast<float>(d_str) / 4.0;
    cout << f << endl;

    try {
        int t = lexical_cast<int>("x");
        cout << t << endl;
    }
    catch (bad_lexical_cast& e) {
        cout << e.what() << endl;
    }

    try {
        int t = std::stoi("x");
        cout << t << endl;
    }
    catch (invalid_argument& e) {
        cout << e.what() << endl;
    }
}


text:
#include <boost/python/def.hpp>
#include <boost/python/module.hpp>
#include <boost/python/args.hpp>

using namespace boost::python;

double aaa(int x, int y)
{
    return 0.1;
}

BOOST_PYTHON_MODULE(numpypp)
{
    def("aaa", aaa, args("x", "y"), "foo's docstring");
}

max_stars_repo_name: ysraell/examples

max_issues_repo_name: ysraell/examples

max_forks_repo_name: ysraell/examples


text:
/**
 * Copyright Soramitsu Co., Ltd. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

#include "framework/integration_framework/iroha_instance.hpp"

#include <boost/filesystem.hpp>
#include <cstdlib>
#include <sstream>

#include "ametsuchi/impl/postgres_options.hpp"
#include "ametsuchi/storage.hpp"
#include "cryptography/keypair.hpp"
#include "framework/config_helper.hpp"
#include "framework/integration_framework/test_irohad.hpp"
#include "logger/logger.hpp"
#include "main/impl/pg_connection_init.hpp"

using namespace std::chrono_literals;

static constexpr std::chrono::milliseconds kMstEmissionPeriod = 100ms;

namespace integration_framework {

  IrohaInstance::IrohaInstance(
      const IrohadConfig &config,
      const std::string &listen_ip,
      logger::LoggerManagerTreePtr irohad_log_manager,
      logger::LoggerPtr log,
      iroha::StartupWsvDataPolicy startup_wsv_data_policy,
      const boost::optional<std::string> &dbname)
      : config_(config),
        working_dbname_(dbname.value_or(getRandomDbName())),
        listen_ip_(listen_ip),
        opt_mst_gossip_params_(boost::make_optional(
            config_.mst_support,
            [] {
              iroha::GossipPropagationStrategyParams params;
              params.emission_period = kMstEmissionPeriod;
              return params;
            }())),
        irohad_log_manager_(std::move(irohad_log_manager)),
        log_(std::move(log)),
        startup_wsv_data_policy_(startup_wsv_data_policy) {}

  void IrohaInstance::init() {
    auto init_result = instance_->init();
    if (auto error =
            boost::get<iroha::expected::Error<std::string>>(&init_result)) {
      std::string error_msg("Irohad startup failed: ");
      error_msg.append(error->error);
      log_->critical("{}", error_msg);
      throw(std::runtime_error(error_msg));
    }
  }

  void IrohaInstance::makeGenesis(
      std::shared_ptr<const shared_model::interface::Block> block) {
    if (auto e =
            iroha::expected::resultToOptionalError(instance_->dropStorage())) {
      throw std::runtime_error(e.value());
    }
    rawInsertBlock(block);
  }

  void IrohaInstance::rawInsertBlock(
      std::shared_ptr<const shared_model::interface::Block> block) {
    if (auto e = iroha::expected::resultToOptionalError(
            instance_->storage->insertBlock(block))) {
      log_->warn("Could not insert block {}: {}", block->height(), e.value());
    }
  }

  void IrohaInstance::setMstGossipParams(
      std::chrono::milliseconds mst_gossip_emitting_period,
      uint32_t mst_gossip_amount_per_once) {
    BOOST_ASSERT_MSG(
        not instance_,
        "Gossip propagation params must be set before Irohad is started!");
    iroha::GossipPropagationStrategyParams gossip_params;
    gossip_params.emission_period = mst_gossip_emitting_period;
    gossip_params.amount_per_once = mst_gossip_amount_per_once;
    opt_mst_gossip_params_ = gossip_params;
  }

  void IrohaInstance::initPipeline(
      const shared_model::crypto::Keypair &key_pair, size_t max_proposal_size) {
    config_.max_proposal_size = max_proposal_size;
    instance_ = std::make_shared<TestIrohad>(
        config_,
        std::make_unique<iroha::ametsuchi::PostgresOptions>(
            getPostgresCredsOrDefault(), working_dbname_, log_),
        listen_ip_,
        key_pair,
        irohad_log_manager_,
        log_,
        startup_wsv_data_policy_,
        opt_mst_gossip_params_);
  }

  void IrohaInstance::run() {
    instance_->run().match(
        [](const auto &) {},
        [](const auto &error) {
          BOOST_THROW_EXCEPTION(std::runtime_error(error.error));
        });
  }

  std::shared_ptr<TestIrohad> &IrohaInstance::getIrohaInstance() {
    return instance_;
  }

  void IrohaInstance::terminateAndCleanup() {
    if (not instance_ or not instance_->storage) {
      log_->warn("Iroha instance or its storage are not initialized");
      return;
    }
    const auto pg_opt = *instance_->pg_opt_;
    log_->info("stopping irohad");
    instance_.reset();
    log_->info("removing storage");
    iroha::ametsuchi::PgConnectionInit::dropWorkingDatabase(pg_opt);
    if (config_.block_store_path) {
      boost::filesystem::remove_all(config_.block_store_path.value());
    }
  }

}  // namespace integration_framework

max_stars_repo_name: Revertron/iroha

max_issues_repo_name: Revertron/iroha

max_forks_repo_name: Revertron/iroha


text:
#include "MeshConverter.h"
#include "MeshCriteria.h"

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/make_mesh_3.h>
#include <CGAL/Mesh_3/config.h>
#include <CGAL/Mesh_complex_3_in_triangulation_3.h>
#include <CGAL/Mesh_criteria_3.h>
#include <CGAL/Mesh_triangulation_3.h>
#include <CGAL/Mesh_polyhedron_3.h>
#include <CGAL/Polyhedral_mesh_domain_3.h>
#include <CGAL/Polyhedral_mesh_domain_with_features_3.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/refine_mesh_3.h>
#include <CGAL/utility.h>

// IO
#include <iostream>
#include <fstream>

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

// Eigen
#include <Eigen/Dense>

// std
#include <set>
#include <vector>

// CGAL feature detection
#include <CGAL/Polygon_mesh_processing/detect_features.h>

// concurrency tags
#ifdef CGAL_CONCURRENT_MESH_3
typedef CGAL::Parallel_tag Concurrency_tag;
#else
typedef CGAL::Sequential_tag Concurrency_tag;
#endif

// Polyhedron
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Mesh_polyhedron_3<K>::type Polyhedron;

// Domain
typedef CGAL::Polyhedral_mesh_domain_with_features_3<K> Mesh_domain_features;
typedef CGAL::Polyhedral_mesh_domain_3<Polyhedron, K> Mesh_domain;

// Triangulation
typedef CGAL::Mesh_triangulation_3<Mesh_domain, CGAL::Default, Concurrency_tag>::type Tr;
typedef CGAL::Mesh_triangulation_3<Mesh_domain_features, CGAL::Default, Concurrency_tag>::type Tr_features;

typedef CGAL::Mesh_complex_3_in_triangulation_3<Tr> C3t3;
typedef CGAL::Mesh_complex_3_in_triangulation_3<Tr> C3t3_features;

// Criteria
typedef CGAL::Mesh_criteria_3<Tr> Mesh_criteria;

typedef Polyhedron::HalfedgeDS HalfedgeDS;

MeshConverter* MeshConverter::m_instance = new MeshConverter();

MeshConverter::MeshConverter()
{

}

// Helper function
template<typename T, unsigned int n>
void add_if_not_contains(std::vector<std::array<T, n>>& v, std::array<T, n> e)
{
    for (unsigned int i = 0; i < v.size(); ++i)
    {
        bool equal = true;
        for (unsigned int j = 0; j < n; ++j)
        {
            if (std::abs(v[i][j] - e[j]) > 0.0001)
                equal = false;
        }
        if (equal)
            return;
    }
    v.push_back(e);
}

template<typename Polyhedron>
void reset_sharp_edges(Polyhedron* pMesh)
{
    typename boost::property_map<Polyhedron, CGAL::edge_is_feature_t>::type if_pm =
            get(CGAL::edge_is_feature, *pMesh);
    for(typename boost::graph_traits<Polyhedron>::edge_descriptor ed : edges(*pMesh))
    {
        put(if_pm,ed,false);
    }
}
template<typename Polyhedron>
void detect_sharp_edges(Polyhedron* pMesh, const double angle)
{
    reset_sharp_edges(pMesh);

    // Detect edges in current polyhedron
    typename boost::property_map<Polyhedron, CGAL::edge_is_feature_t>::type eif =
            get(CGAL::edge_is_feature, *pMesh);

    CGAL::Polygon_mesh_processing::detect_sharp_edges(*pMesh, angle, eif);
}

// Creates a CGAL Polyhedron from the given vertices and triangles.
Polyhedron createPolyhedron(
        const Vectors& vertices,
        const Faces& facets)
{
    Polyhedron p;

    CGAL::Polyhedron_incremental_builder_3<HalfedgeDS> builder( p.hds(), true);
    typedef typename HalfedgeDS::Vertex   Vertex;
    typedef typename Vertex::Point Point;

    builder.begin_surface(vertices.size(), facets.size(), 0);

    for (const Eigen::Vector3d& v : vertices)
    {
        builder.add_vertex(Point(v(0), v(1), v(2)));
    }

    for (const std::array<unsigned int, 3>& f : facets)
    {
        builder.begin_facet();
        builder.add_vertex_to_facet(f[0]);
        builder.add_vertex_to_facet(f[1]);
        builder.add_vertex_to_facet(f[2]);
        builder.end_facet();
    }

    builder.end_surface();

    return p;
}

template <typename Mesh_domain>
bool generateMeshFromCGALPolyhedron(
        Mesh_domain& domain,
        Vectors& vertices_out,
        Faces& outer_facets_out,
        Faces& facets_out,
        Cells& cells_out,
        const MeshCriteria& meshCriteria)
{

    // Triangulation
    typedef typename CGAL::Mesh_triangulation_3<Mesh_domain, CGAL::Default, Concurrency_tag>::type MeshTr;
    typedef CGAL::Mesh_complex_3_in_triangulation_3<MeshTr> C3t3;
    typedef CGAL::Mesh_criteria_3<MeshTr> Mesh_criteria;

    Mesh_criteria criteria(
                CGAL::parameters::cell_radius_edge_ratio=meshCriteria.getCellRadiusEdgeRatio(),
                CGAL::parameters::cell_size=meshCriteria.getCellSize(),
                CGAL::parameters::facet_angle=meshCriteria.getFacetAngle(),
                CGAL::parameters::facet_size=meshCriteria.getFacetSize(),
                CGAL::parameters::facet_distance=meshCriteria.getFaceDistance());

//    Mesh_criteria criteria;

    // Mesh generation
    C3t3 c3t3 = CGAL::make_mesh_3<C3t3>(
                domain, criteria,
                CGAL::parameters::no_perturb(),
                CGAL::parameters::no_exude());

    // CONVERT FROM CGAL TO vectors
    typedef typename C3t3::Triangulation Tr;
    typedef typename C3t3::Facets_in_complex_iterator Facet_iterator;
    typedef typename C3t3::Cells_in_complex_iterator Cell_iterator;

    typedef typename Tr::Finite_vertices_iterator Finite_vertices_iterator;
    typedef typename Tr::Vertex_handle Vertex_handle;
    typedef typename Tr::Point Point_3;

    const Tr& tr = c3t3.triangulation();

    std::cout << "\nAfter Triangulation: \n" << "Vertices: "
            << tr.number_of_vertices() << "\n"
            << "Facets/Triangles: " << tr.number_of_facets() << "\n"
            << "Cells/Tetrahedra: " << tr.number_of_cells() << "\n";

    //-------------------------------------------------------
    // Vertices
    //-------------------------------------------------------

    boost::unordered_map<Vertex_handle, int> V;
    int inum = 0;
    for (Finite_vertices_iterator vit = tr.finite_vertices_begin();
            vit != tr.finite_vertices_end(); ++vit) {
        V[vit] = inum++;
        Point_3 p = vit->point();
        vertices_out.push_back(Eigen::Vector3d((float)CGAL::to_double(p.x()),
                (float)CGAL::to_double(p.y()), (float)CGAL::to_double(p.z())));
    }

    //-------------------------------------------------------
    // Facets
    //-------------------------------------------------------
    typename C3t3::size_type number_of_triangles =
            c3t3.number_of_facets_in_complex();

    for (Facet_iterator fit = c3t3.facets_in_complex_begin();
            fit != c3t3.facets_in_complex_end(); ++fit) {
        typename C3t3::Subdomain_index cell_sd=c3t3.subdomain_index(fit->first);
        typename C3t3::Subdomain_index opp_sd=c3t3.subdomain_index(fit->first->neighbor(fit->second));

        int j = -1;
        std::array<unsigned int, 3> vertex;

        for (int i = 0; i < 4; i++) {
            if (i != fit->second) {
                const Vertex_handle& vh = (*fit).first->vertex(i);
                vertex[++j] = V[vh];
            }
        }

        //facets.push_back(vertex);
        // Only for outer Facets true (that one that lie on the boundary)
        if (!(cell_sd != 0 && opp_sd != 0))
            outer_facets_out.push_back(vertex);
    }

    //-------------------------------------------------------
    // Tetrahedra
    //-------------------------------------------------------

    auto sortedFace = [](std::array<unsigned int, 3> f)
    {
        std::sort(f.begin(), f.end());
        return f;
    };

    std::set<std::array<unsigned int, 3>> addedSortedFacets;
    auto addFaceIfNotContains = [&sortedFace, &facets_out, &addedSortedFacets](
            std::array<unsigned int, 3> f)
    {
        std::array<unsigned int, 3> sortedF = sortedFace(f);
        if (addedSortedFacets.find(sortedF) == addedSortedFacets.end())
        {
            addedSortedFacets.insert(sortedF);
            facets_out.push_back(f);
        }
    };

    for (Cell_iterator cit = c3t3.cells_in_complex_begin();
            cit != c3t3.cells_in_complex_end(); ++cit)
    {
        std::array<unsigned int, 4> f;
        for (unsigned int i = 0; i < 4; i++)
            f[i] = static_cast<unsigned int>(V[cit->vertex(static_cast<int>(i))]);

        cells_out.push_back(f);

        addFaceIfNotContains({f[0], f[1], f[2]});
        addFaceIfNotContains({f[0], f[1], f[3]});
        addFaceIfNotContains({f[0], f[2], f[3]});
        addFaceIfNotContains({f[1], f[2], f[3]});
    }

    // correct the triangle indices
    // The 3 vertex indices of a triangles must be ordered in a way
    // that the cross product of the first two vertices:
    // v1.cross(v2) points on the outside.
    for (size_t i = 0; i < outer_facets_out.size(); ++i) {
        std::array<unsigned int, 3>& facet = outer_facets_out[i];

        // =====================================================================
        // Disclaimer:
        // This code part is responsible for fixing the vertex index order of
        // each outer triangle so that the normal that points outside the
        // polygon can be calculated in a consistent way. The same is already
        // done in the constructor of Polygon3D so this part may be removed.
        // It is left here for the case that one doesn't want to create a
        // Polygon3D afterwards. It could be removed for the future to slightly
        // improve the performance.

        // find the outer facet (tetrahedron) that contains this triangle
        // note, that since this is an outer triangle, there is only a single tetrahedron
        // that containts it.
        bool found = false;
        unsigned int other_vertex_id = 0;
        for (size_t j = 0; j < cells_out.size(); ++j)
        {
            Cell& c = cells_out[j];
            if (std::find(c.begin(), c.end(), facet[0]) != c.end() &&
                std::find(c.begin(), c.end(), facet[1]) != c.end()&&
                std::find(c.begin(), c.end(), facet[2]) != c.end())
            {
                found = true;

                // find the other vertex of the cell that is not part of the triangle
                for (size_t k = 0; k < 4; ++k)
                {
                    if (std::find(facet.begin(), facet.end(), c[k]) == facet.end())
                    {
                        other_vertex_id = c[k];
                        break;
                    }
                }

                break;
            }
        }

        bool reverted = false;
        if (!found)
        {
            std::cout << "Did not find cell for outer triangle.\n";
        }
        else
        {
            Eigen::Vector3d r_1 = vertices_out[facet[1]] - vertices_out[facet[0]];
            Eigen::Vector3d r_2 = vertices_out[facet[2]] - vertices_out[facet[0]];
            Eigen::Vector3d r_3 = vertices_out[other_vertex_id] - vertices_out[facet[0]];
//            std::cout << "other_vertex_id = " << other_vertex_id << "\n";

            if (r_1.cross(r_2).normalized().dot(r_3) > 0)
            {
                unsigned int temp = facet[1];
                facet[1] = facet[2];
                facet[2] = temp;
                reverted = true;
            }
        }

        // =====================================================================

        // Check if this face is part of all faces. If so, revert the
        // corresponding global facet if the outer facet was also reverted.
        // Polygon3D doesn't necessarily require this to be done because
        // it does it on its own as well, see
        // Polygon3D::synchronizeTriangleIndexOrder()).
        bool found2 = false;
        for (size_t j = 0; j < facets_out.size(); ++j)
        {
            if (sortedFace(facets_out[j]) == sortedFace(facet))
            {
                found2 = true;
                // revert the same face in facets_out as it was done in
                // outer_facets_out
                if (reverted)
                {
                    std::array<unsigned int, 3>& facet = facets_out[j];
                    unsigned int temp = facet[1];
                    facet[1] = facet[2];
                    facet[2] = temp;
                }
                break;
            }
        }
        if (!found2)
        {
            std::cout << "Outer face is not part of all faces.\n";
        }
    }

    std::cout << "\nIn Output File: \n" << "Vertices: "
            << vertices_out.size() << "\n"
            << "Facets/Triangles: " << facets_out.size() << "\n"
            << "Outer Facets/Triangles: " << outer_facets_out.size() << "\n"
            << "Cells/Tethraedra: " << cells_out.size() << "\n";

    return true;
}

// The same as the other generateMesh() but uses a CGAL Polyhedron.
// Calling createPolyhedron() and this method is equal to the other
// generateMesh().
bool generateMeshFromCGALPolyhedron(
        Polyhedron& p,
        Vectors& vertices_out,
        Faces& outer_facets_out,
        Faces& facets_out,
        Cells& cells_out,
        const MeshCriteria& meshCriteria)
{
    if (meshCriteria.isEdgesAsFeatures())
    {
        typedef CGAL::Polyhedral_mesh_domain_with_features_3<K> Mesh_domain;

        // Create domain
        Mesh_domain domain(p, p);
        domain.detect_features(meshCriteria.getMinFeatureEdgeAngleDeg());

        return generateMeshFromCGALPolyhedron(
                    domain, vertices_out, outer_facets_out, facets_out, cells_out,
                    meshCriteria);
    }
    else
    {
        typedef CGAL::Polyhedral_mesh_domain_3<Polyhedron, K> Mesh_domain;

        // Create domain
        Mesh_domain domain(p);

        return generateMeshFromCGALPolyhedron(
                    domain, vertices_out, outer_facets_out, facets_out, cells_out,
                    meshCriteria);
    }

}

bool MeshConverter::generateMesh(
        const Vectors& vertices,
        const Faces& facets,
        Vectors& vertices_out,
        Faces& outer_facets_out,
        Faces& facets_out,
        Cells& cells_out,
        const MeshCriteria& meshCriteria)
{
    std::cout << "Input: \n "
              << "Vertices: " << vertices.size() << "\n"
              << "Facets/Triangles: " << facets.size() << "\n";

    vertices_out.clear();
    outer_facets_out.clear();
    facets_out.clear();
    cells_out.clear();

    Polyhedron p = createPolyhedron(vertices, facets);

    std::cout << "\nAfter Conversion to Polyhedron: \n"
              << "Vertices: " << p.size_of_vertices() << "\n"
              << "Facets/Triangles: " << p.size_of_facets() << "\n";

    return generateMeshFromCGALPolyhedron(
                p, vertices_out, outer_facets_out, facets_out, cells_out, meshCriteria);

}


text: #include <boost/typeof/std/istream.hpp>


text:
// MIT License
//
// Copyright (c) 2018 Francisco Ruiz (francisco.ruiz.rayo@gmail.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

#include <gtest/gtest.h>
#include <boost/filesystem.hpp>

#include <coffee/basis/DataBlock.hpp>

#include <coffee/xml/Document.hpp>
#include <coffee/xml/Node.hpp>
#include <coffee/xml/Attribute.hpp>
#include <coffee/xml/Compiler.hpp>

using namespace coffee;
using namespace coffee::xml;

TEST(XmlWrittingDocumentTest,create_nodes)
{
   std::shared_ptr<xml::Node> root = std::make_shared<xml::Node>("the root");

   std::shared_ptr<xml::Node> level00 = root->createChild("level0-0");
   level00->createChild("level1-0");
   level00->createChild("level1-1");
   level00->createChild("level1-2");
   level00->createChild("level1-3")->createChild("2-0");
   ASSERT_EQ(4, level00->children_size());

   root->createChild("level0-1");
   root->createChild("level0-2");

   ASSERT_EQ(3, root->children_size());

   ASSERT_EQ(0, root->lookupChild("level0-0")->lookupChild("level1-0")->children_size());
   ASSERT_EQ(1, root->lookupChild("level0-0")->lookupChild("level1-3")->children_size());
}

TEST(XmlWrittingDocumentTest,create_text)
{
   std::shared_ptr<xml::Node> root = std::make_shared<xml::Node>("the root");

   std::shared_ptr<xml::Node> level00 = root->createChild("level0-0");
   level00->createText("0-0");
   level00->createChild("level1-3")->createText("2-0");
   ASSERT_EQ(1, level00->children_size());

   ASSERT_EQ("0-0", level00->getText());
   ASSERT_EQ("2-0", level00->lookupChild("level1-3")->getText());
}

TEST(XmlWrittingDocumentTest,create_attribute)
{
   std::shared_ptr<xml::Node> root = std::make_shared<xml::Node>("the root");

   root->createAttribute("attr_char", "This is the char");
   root->createAttribute("attr_int", 1024);
   root->createAttribute("attr_second", std::chrono::seconds(59));

   ASSERT_EQ(3, root->attributes_size());

   ASSERT_EQ("This is the char", root->lookupAttribute("attr_char")->getValue());
   ASSERT_EQ("1024", root->lookupAttribute("attr_int")->getValue());
   ASSERT_EQ("59 sec", root->lookupAttribute("attr_second")->getValue());
}

TEST(XmlWrittingDocumentTest,change_attribute)
{
   std::shared_ptr<xml::Node> root = std::make_shared<xml::Node>("the root");

   root->createAttribute("attr_char", "This is the char");

   ASSERT_EQ(1, root->attributes_size());

   ASSERT_EQ("This is the char", root->lookupAttribute("attr_char")->getValue());

   {
      std::string innerScope = "zzz";
      root->searchAttribute("attr_char")->setValue(innerScope);
   }

   int ii;

   ASSERT_EQ("zzz", root->lookupAttribute("attr_char")->getValue());
}

TEST(XmlWrittingDocumentTest,compile_text)
{
   std::shared_ptr<xml::Node> root = std::make_shared<xml::Node>("the_root");

   std::shared_ptr<xml::Node> level00 = root->createChild("level0-0");
   level00->createText("aaaa-bbbb");

   xml::Compiler compiler;

   std::string str = compiler.apply(root);

   ASSERT_EQ("<the_root><level0-0>aaaa-bbbb</level0-0></the_root>", str);
}

TEST(XmlWrittingDocumentTest,compile_tree)
{
   std::shared_ptr<xml::Node> root = std::make_shared<xml::Node>("the_root");

   std::shared_ptr<xml::Node> level00 = root->createChild("level0");

   level00->createAttribute("MyAttr", "It works");
   std::shared_ptr<xml::Node> level1 = level00->createChild("level1");
   level1->createAttribute("ZZ", "zz");

   level00->createChild("level2");
   std::shared_ptr<xml::Node> last = level00->createChild("level3")->createChild("LevelA");

   last->createAttribute("first", "111");
   last->createAttribute("second", 222);

   xml::Compiler compiler;
   std::string str = compiler.apply(root);

   ASSERT_EQ("<the_root><level0 MyAttr=\"It works\"><level1 ZZ=\"zz\"/><level2/><level3><LevelA first=\"111\" second=\"222\"/></level3></level0></the_root>", str);
}

TEST(XmlWrittingDocumentTest,compile_iso)
{
   std::shared_ptr<xml::Node> root = std::make_shared<xml::Node>("the_root");

   std::shared_ptr<xml::Node> level00 = root->createChild("level00");
   level00->createAttribute("Name", "Jörg1");
   level00->createAttribute("Other", "Büörgä");

   std::shared_ptr<xml::Node> level01 = root->createChild("level01");
   level01->createText("Jörg2");

   xml::Compiler compiler;
   compiler.setEncoding("ISO-8859-1");
   std::string str = compiler.apply(root);

   ASSERT_EQ("<the_root><level00 Name=\"Jörg1\" Other=\"Büörgä\"/><level01>Jörg2</level01></the_root>", str);
}

TEST(XmlWrittingDocumentTest,compare_attribute)
{
   std::shared_ptr<xml::Node> root = std::make_shared<xml::Node>("the root");

   auto attr00 = root->createAttribute("attr00", "This is the char");
   auto attr01 = root->createAttribute("attr01", 1024);

   ASSERT_TRUE(*attr00 < *attr01);
}

max_stars_repo_name: ciscoruiz/wepa

max_issues_repo_name: ciscoruiz/wepa

max_forks_repo_name: ciscoruiz/wepa


text:
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/punctuation/comma.hpp>
#include <boost/preprocessor/punctuation/comma_if.hpp>
#include <boost/preprocessor/arithmetic/inc.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>

template <int Val>
constexpr int find()
{
    return -1;
}

#define FIND_TEMPLATE_PARAMETERS(z, n, text) BOOST_PP_COMMA_IF(n) BOOST_PP_CAT(text, n)

#define FIND_TEMPLATE_ARGUMENTS(z, n, text) BOOST_PP_COMMA() BOOST_PP_CAT(text, n)

#define FIND_TEMPLATE(z, n, _) \
    template <int Val, BOOST_PP_REPEAT_ ## z(BOOST_PP_INC(n), FIND_TEMPLATE_PARAMETERS, int Arg)> \
    constexpr int find() \
    { \
        return Val == BOOST_PP_CAT(Arg, n) ? n : find<Val BOOST_PP_REPEAT_ ## z(n, FIND_TEMPLATE_ARGUMENTS, Arg)>(); \
    }

BOOST_PP_REPEAT(5, FIND_TEMPLATE, nil)

int main()
{
    constexpr int r1{ find<3, 5, 2, 3, 1, 4>() };
    static_assert(r1 == 2);

    constexpr int r2{ find<6, 5, 2, 3>() };
    static_assert(r2 == -1);
}

max_stars_repo_name: BorisSchaeling/boost-meta-programming

max_issues_repo_name: BorisSchaeling/boost-meta-programming

max_forks_repo_name: BorisSchaeling/boost-meta-programming


text:
//---------------------------------------------------------- -*- Mode: C++ -*-
// $Id: imerger_main.cc 3372 2011-11-28 22:13:52Z sriramr $
//
// Created 2010/11/14
//
// Copyright 2010 Yahoo Corporation.  All rights reserved.
// This file is part of the Sailfish project.
//
// 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.
//
// \brief imerger: merges records from chunks of an I-file and write to stdout.
//----------------------------------------------------------------------------

#include "common/log.h"
#include "imerger.h"
#include "kappender.h"

#include <iostream>
#include <sstream>
#include <boost/program_options.hpp>
#include <string>
#include <sys/types.h>
#include <unistd.h>

#if defined(KFS_OS_NAME_LINUX)
#include <sys/prctl.h>
#include <signal.h>
#endif

namespace po = boost::program_options;

using namespace KFS;
using std::string;
using std::ostringstream;

int main(int argc, char **argv)
{
    ServerLocation metaLoc;
    ServerLocation workbuilderLoc;
    string basedir;
    int partition;
    string logLevel;
    string jobId;
    // set it at 256M
    uint64_t mergeHeapSize = 256 * 1024 * 1024;

    // pass in the memory size for each sorted run file...DWIM

    // Declare the supported options.
    po::options_description desc("Allowed options");
    desc.add_options()
        ("help,h", "produce help message")
        ("metahost,M", po::value<string>(&metaLoc.hostname)->default_value("usualfall.greatamerica.corp.yahoo.com"), "KFS metaserver hostname")
        ("metaport,P", po::value<int>(&metaLoc.port)->default_value(20000), "KFS metaserver port")
        ("wbhost,w", po::value<string>(&workbuilderLoc.hostname)->default_value("localhost"), "Sailfish workbuilder hostname")
        ("wbport,x", po::value<int>(&workbuilderLoc.port)->default_value(12345), "Sailfish workbuilder port")
        ("jobid,J", po::value<string>(&jobId)->default_value(""), "Hadoop Job id")
        ("basedir,B", po::value<string>(&basedir)->default_value("/jobs"), "Base dir for storing I-files in KFS")
        ("loglevel,L", po::value<string>(&logLevel)->default_value("INFO"), "Log level (INFO or DEBUG)")
        ("partition,i",po::value<int>(&partition)->default_value(1), "partition")
        ("mergeHeapSize,m",po::value<uint64_t>(&mergeHeapSize)->default_value(256 * 1024 * 1024), "Heap size for merge (default = 256MB)")
        ;

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

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

#if defined(KFS_OS_NAME_LINUX)
    // if we are created by a fork, we exit when parent dies
    prctl(PR_SET_PDEATHSIG, SIGHUP);
#endif

    // assume you got the args right

    // Initialize the msg-logger
    MsgLogger::Init(NULL, str2LogLevel(logLevel));

    std::cerr << "Our pid is: " << getpid() << std::endl;

    KfsClientPtr kfsClient = getKfsClientFactory()->GetClient(metaLoc.hostname, metaLoc.port);
    if (!kfsClient) {
        std::cerr << "Unable to initialize kfsClient.  Exiting!" << std::endl;
        exit(-1);
    }

    string wbHostPort = workbuilderLoc.hostname + ":" + boost::lexical_cast<string>(workbuilderLoc.port);

    IMerger imerger(kfsClient, partition, jobId, wbHostPort,
        basedir.c_str());

    // kgroupBy.LoadData(1, basedir + "/deadmappers");

    // send output to stdout
    imerger.Start(1, basedir + "/deadmappers", mergeHeapSize);

    MsgLogger::Stop();

    std::cerr << "IMerger returned...we are all done" << std::endl;
}

max_stars_repo_name: sriramsrao/kfs

max_issues_repo_name: sriramsrao/kfs

max_forks_repo_name: sriramsrao/kfs


text:
/**
 * Copyright Soramitsu Co., Ltd. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

#include "runtime/binaryen/wasm_executor.hpp"

#include <binaryen/shell-interface.h>
#include <gtest/gtest.h>

#include <boost/filesystem.hpp>
#include <fstream>

#include "crypto/bip39/impl/bip39_provider_impl.hpp"
#include "crypto/crypto_store/crypto_store_impl.hpp"
#include "crypto/ed25519/ed25519_provider_impl.hpp"
#include "crypto/hasher/hasher_impl.hpp"
#include "crypto/pbkdf2/impl/pbkdf2_provider_impl.hpp"
#include "crypto/random_generator/boost_generator.hpp"
#include "crypto/secp256k1/secp256k1_provider_impl.hpp"
#include "crypto/sr25519/sr25519_provider_impl.hpp"
#include "extensions/impl/extension_factory_impl.hpp"
#include "mock/core/storage/changes_trie/changes_tracker_mock.hpp"
#include "mock/core/blockchain/block_header_repository_mock.hpp"
#include "runtime/binaryen/module/wasm_module_factory_impl.hpp"
#include "runtime/binaryen/runtime_manager.hpp"
#include "runtime/common/trie_storage_provider_impl.hpp"
#include "runtime/binaryen/runtime_api/core_factory_impl.hpp"
#include "storage/in_memory/in_memory_storage.hpp"
#include "storage/trie/impl/trie_storage_backend_impl.hpp"
#include "storage/trie/impl/trie_storage_impl.hpp"
#include "storage/trie/polkadot_trie/polkadot_trie_factory_impl.hpp"
#include "storage/trie/serialization/polkadot_codec.hpp"
#include "storage/trie/serialization/trie_serializer_impl.hpp"
#include "testutil/outcome.hpp"
#include "testutil/runtime/common/basic_wasm_provider.hpp"

using kagome::api::Session;
using kagome::common::Buffer;
using kagome::crypto::Bip39ProviderImpl;
using kagome::crypto::BoostRandomGenerator;
using kagome::crypto::CryptoStoreImpl;
using kagome::crypto::ED25519ProviderImpl;
using kagome::crypto::HasherImpl;
using kagome::crypto::Pbkdf2ProviderImpl;
using kagome::crypto::Secp256k1ProviderImpl;
using kagome::crypto::SR25519ProviderImpl;
using kagome::primitives::BlockHash;
using kagome::runtime::TrieStorageProvider;
using kagome::runtime::TrieStorageProviderImpl;
using kagome::runtime::WasmProvider;
using kagome::runtime::binaryen::CoreFactoryImpl;
using kagome::runtime::binaryen::RuntimeManager;
using kagome::runtime::binaryen::WasmExecutor;
using kagome::storage::changes_trie::ChangesTrackerMock;
using kagome::storage::trie::PolkadotCodec;
using kagome::storage::trie::PolkadotTrieFactoryImpl;
using kagome::storage::trie::PolkadotTrieImpl;
using kagome::storage::trie::TrieSerializerImpl;
using kagome::storage::trie::TrieStorage;
using kagome::storage::trie::TrieStorageImpl;

namespace fs = boost::filesystem;

class WasmExecutorTest : public ::testing::Test {
 public:
  void SetUp() override {
    // path to a file with wasm code in wasm/ subfolder
    auto wasm_path =
        fs::path(__FILE__).parent_path().string() + "/wasm/sumtwo.wasm";
    wasm_provider_ =
        std::make_shared<kagome::runtime::BasicWasmProvider>(wasm_path);

    auto backend =
        std::make_shared<kagome::storage::trie::TrieStorageBackendImpl>(
            std::make_shared<kagome::storage::InMemoryStorage>(),
            kagome::common::Buffer{});

    auto trie_factory = std::make_shared<PolkadotTrieFactoryImpl>();
    auto codec = std::make_shared<PolkadotCodec>();
    auto serializer =
        std::make_shared<TrieSerializerImpl>(trie_factory, codec, backend);

    auto trieDb = kagome::storage::trie::TrieStorageImpl::createEmpty(
                      trie_factory, codec, serializer, boost::none)
                      .value();

    storage_provider_ =
        std::make_shared<TrieStorageProviderImpl>(std::move(trieDb));

    auto random_generator = std::make_shared<BoostRandomGenerator>();
    auto sr25519_provider =
        std::make_shared<SR25519ProviderImpl>(random_generator);
    auto ed25519_provider = std::make_shared<ED25519ProviderImpl>();
    auto secp256k1_provider = std::make_shared<Secp256k1ProviderImpl>();
    auto hasher = std::make_shared<HasherImpl>();
    auto pbkdf2_provider = std::make_shared<Pbkdf2ProviderImpl>();
    auto bip39_provider = std::make_shared<Bip39ProviderImpl>(pbkdf2_provider);
    auto crypto_store = std::make_shared<CryptoStoreImpl>(ed25519_provider,
                                                          sr25519_provider,
                                                          secp256k1_provider,
                                                          bip39_provider,
                                                          random_generator);
    auto changes_tracker =
        std::make_shared<kagome::storage::changes_trie::ChangesTrackerMock>();
    auto extension_factory =
        std::make_shared<kagome::extensions::ExtensionFactoryImpl>(
            std::make_shared<ChangesTrackerMock>(),
            sr25519_provider,
            ed25519_provider,
            secp256k1_provider,
            hasher,
            crypto_store,
            bip39_provider, [this, changes_tracker](
    std::shared_ptr<kagome::runtime::WasmProvider> wasm_provider) {
      kagome::runtime::binaryen::CoreFactoryImpl factory(
          runtime_manager_,
          changes_tracker,
          std::make_shared<
              kagome::blockchain::BlockHeaderRepositoryMock>());
      return factory.createWithCode(std::move(wasm_provider));
    });

    auto module_factory =
        std::make_shared<kagome::runtime::binaryen::WasmModuleFactoryImpl>();

    runtime_manager_ =
        std::make_shared<RuntimeManager>(std::move(extension_factory),
                                         std::move(module_factory),
                                         storage_provider_,
                                         std::move(hasher));

    executor_ = std::make_shared<WasmExecutor>();
  }

 protected:
  std::shared_ptr<WasmExecutor> executor_;
  std::shared_ptr<RuntimeManager> runtime_manager_;
  std::shared_ptr<TrieStorageProvider> storage_provider_;
  std::shared_ptr<WasmProvider> wasm_provider_;
};

/**
 * @given wasm executor
 * @when call is invoked with wasm code with addTwo function
 * @then proper result is returned
 */
TEST_F(WasmExecutorTest, ExecuteCode) {
  EXPECT_OUTCOME_TRUE(environment,
                      runtime_manager_->createEphemeralRuntimeEnvironment(
                          wasm_provider_->getStateCode()));
  auto &&[module, memory, opt_batch] = std::move(environment);

  auto res = executor_->call(
      *module, "addTwo", wasm::LiteralList{wasm::Literal(1), wasm::Literal(2)});

  ASSERT_TRUE(res) << res.error().message();
  ASSERT_EQ(res.value().geti32(), 3);
}

max_stars_repo_name: FlorianFranzen/kagome

max_issues_repo_name: FlorianFranzen/kagome

max_forks_repo_name: FlorianFranzen/kagome


text:
//
// Created by X-ray on 6/3/2021.
//
#pragma once
#include <boost/beast.hpp>
#include "../utility/fail.hpp"

namespace web_server {
    namespace session {
        namespace beast = boost::beast;
        namespace http = beast::http;
        namespace websocket = beast::websocket;

        template<class Derived>
        class websocket_session
        {
            // Access the derived class, this is part of
            // the Curiously Recurring Template Pattern idiom.
            Derived& derived()
            {
                return static_cast<Derived&>(*this);
            }

            beast::flat_buffer buffer_;

            // Start the asynchronous operation
            template<class Body, class Allocator>
            void
            do_accept(http::request<Body, http::basic_fields<Allocator>> req)
            {
                // Set suggested timeout settings for the websocket
                derived().ws().set_option(websocket::stream_base::timeout::suggested(beast::role_type::server));

                // Set a decorator to change the Server of the handshake
                derived().ws().set_option(websocket::stream_base::decorator([](websocket::response_type& res)
                {
                    res.set(http::field::server,std::string(BOOST_BEAST_VERSION_STRING));
                }));

                // Accept the websocket handshake
                derived().ws().async_accept(req, beast::bind_front_handler(&websocket_session::on_accept,derived().shared_from_this()));
            }

            void on_accept(beast::error_code ec)
            {
                if(ec)
                    return utility::fail(ec, "accept");

                // Read a message
                do_read();
            }

            void do_read()
            {
                // Read a message into our buffer
                derived().ws().async_read(buffer_, beast::bind_front_handler(&websocket_session::on_read, derived().shared_from_this()));
            }

            void on_read(beast::error_code ec, std::size_t bytes_transferred)
            {
                boost::ignore_unused(bytes_transferred);

                // This indicates that the websocket_session was closed
                if(ec == websocket::error::closed)
                    return;

                if(ec)
                    utility::fail(ec, "read");

                // Echo the message
                derived().ws().text(derived().ws().got_text());
                derived().ws().async_write(buffer_.data(), beast::bind_front_handler(&websocket_session::on_write, derived().shared_from_this()));
            }

            void on_write(beast::error_code ec, std::size_t bytes_transferred)
            {
                boost::ignore_unused(bytes_transferred);

                if(ec)
                    return utility::fail(ec, "write");

                // Clear the buffer
                buffer_.consume(buffer_.size());

                // Do another read
                do_read();
            }

        public:
            // Start the asynchronous operation
            template<class Body, class Allocator>
            void run(http::request<Body, http::basic_fields<Allocator>> req)
            {
                // Accept the WebSocket upgrade request
                do_accept(std::move(req));
            }
        };

//------------------------------------------------------------------------------

// Handles a plain WebSocket connection
        class plain_websocket_session : public websocket_session<plain_websocket_session>, public std::enable_shared_from_this<plain_websocket_session>
        {
            websocket::stream<beast::tcp_stream> ws_;

        public:
            // Create the session
            explicit plain_websocket_session(beast::tcp_stream&& stream) : ws_(std::move(stream))
            {
            }

            // Called by the base class
            websocket::stream<beast::tcp_stream>& ws()
            {
                return ws_;
            }
        };

//------------------------------------------------------------------------------

// Handles an SSL WebSocket connection
        class ssl_websocket_session : public websocket_session<ssl_websocket_session>, public std::enable_shared_from_this<ssl_websocket_session>
        {
            websocket::stream<
                    beast::ssl_stream<beast::tcp_stream>> ws_;

        public:
            // Create the ssl_websocket_session
            explicit ssl_websocket_session(beast::ssl_stream<beast::tcp_stream>&& stream) : ws_(std::move(stream))
            {
            }

            // Called by the base class
            websocket::stream<beast::ssl_stream<beast::tcp_stream>>& ws()
            {
                return ws_;
            }
        };

//------------------------------------------------------------------------------

        template<class Body, class Allocator>
        void make_websocket_session(beast::tcp_stream stream, http::request<Body, http::basic_fields<Allocator>> req)
        {
            std::make_shared<plain_websocket_session>(std::move(stream))->run(std::move(req));
        }

        template<class Body, class Allocator>
        void make_websocket_session(beast::ssl_stream<beast::tcp_stream> stream, http::request<Body, http::basic_fields<Allocator>> req)
        {
            std::make_shared<ssl_websocket_session>(std::move(stream))->run(std::move(req));
        }
    } // session
} // web_server


text:
// Copyright 2004-present Facebook. All Rights Reserved.

#include <algorithm>
#include <map>
#include <string>
#include <unordered_set>
#include <map>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <sys/types.h>
#include <sys/sysctl.h>
#include <libproc.h>
#include <stdlib.h>

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/lexical_cast.hpp>

#include <glog/logging.h>

#include "osquery/core.h"
#include "osquery/database.h"
#include "osquery/filesystem.h"

#define IPv6_2_IPv4(v6) (((uint8_t *)((struct in6_addr *)v6)->s6_addr)+12)

namespace osquery {
namespace tables {

std::unordered_set<int> getProcList() {
  std::unordered_set<int> pidlist;
  int bufsize = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);
  if (bufsize <= 0) {
    LOG(ERROR) << "An error occured retrieving the process list";
    return pidlist;
  }

  // arbitrarily create a list with 2x capacity in case more processes have
  // been loaded since the last proc_listpids was executed
  pid_t pids[2 * bufsize/sizeof(pid_t)];

  // now that we've allocated "pids", let's overwrite num_pids with the actual
  // amount of data that was returned for proc_listpids when we populate the
  // pids data structure
  bufsize = proc_listpids(PROC_ALL_PIDS, 0, pids, sizeof(pids));
  if (bufsize <= 0) {
    LOG(ERROR) << "An error occured retrieving the process list";
    return pidlist;
  }

  int num_pids = bufsize / sizeof(pid_t);
  for (int i = 0; i < num_pids; ++i) {
    // if the pid is negative or 0, it doesn't represent a real process so
    // continue the iterations so that we don't add it to the results set
    if (pids[i] <= 0) {
      continue;
    }
    pidlist.insert(pids[i]);
  }

  return pidlist;
}

std::unordered_map<int, int> getParentMap(std::unordered_set<int> & pidlist) {
  std::unordered_map<int, int> pidmap;
  auto num_pids = pidlist.size();
  pid_t children[num_pids];

  // Find children for each pid, and mark that pid as their parent
  for (auto& pid : pidlist) {
    int num_children = proc_listchildpids(pid, children, sizeof(children));
    for (int i = 0; i < num_children; ++i) {
      pidmap[children[i]] = pid;
    }
  }

  return pidmap;
}

std::string getProcName(int pid) {
  char name[1024] = "\0";
  int bufsize = proc_name(pid, name, sizeof(name));
  if (bufsize <= 0) {
    name[0] = '\0';
  }

  return std::string(name);
}

std::string getProcPath(int pid) {
  char path[PROC_PIDPATHINFO_MAXSIZE] = "\0";
  int bufsize = proc_pidpath(pid, path, sizeof(path));
  if (bufsize <= 0) {
    path[0] = '\0';
  }

  return std::string(path);
}

// Get the max args space
int genMaxArgs() {
  int mib[2] = {CTL_KERN, KERN_ARGMAX};

  int argmax = 0;
  size_t size = sizeof(argmax);
  if (sysctl(mib, 2, &argmax, &size, NULL, 0) == -1) {
    LOG(ERROR) << "An error occured retrieving the max arg size";
    return 0;
  }

  return argmax;
}

std::unordered_map<std::string, std::string> getProcEnv(int pid, size_t argmax) {
  std::unordered_map<std::string, std::string> env;
  std::vector<std::string> args;

  char procargs[argmax];
  const char* cp = procargs;
  int mib[3] = {CTL_KERN, KERN_PROCARGS2, pid};

  if (sysctl(mib, 3, &procargs, &argmax, NULL, 0) == -1) {
    LOG(ERROR) << "An error occured retrieving the env for " << pid;
    return env;
  }

  // Here we make the assertion that we are interested in all non-empty strings
  // in the proc args+env
  do {
    std::string s = std::string(cp);
    if (s.length() > 0) {
      args.push_back(s);
    }
    cp += args.back().size() + 1;
  } while (cp < procargs + argmax);

  // Since we know that all envs will have an = sign and are at the end of the
  // list, we iterate from the end forward until we stop seeing = signs.
  // According to the // ps source, there is no programmatic way to know where
  // args stop and env begins, so args at the end of a command string which
  // contain "=" may erroneously appear as env vars.
  for (auto itr = args.rbegin(); itr < args.rend(); ++itr) {
    size_t idx = itr->find_first_of("=");
    if (idx == std::string::npos) {
      break;
    }
    std::string key = itr->substr(0, idx);
    std::string value = itr->substr(idx + 1);
    env[key] = value;
  }

  return env;
}

struct OpenFile {
  std::string local_path;
  std::string file_type;
  std::string remote_host;
  std::string remote_port;
  std::string local_host;
  std::string local_port;
};

std::vector<OpenFile> getOpenFiles(int pid) {
  std::vector<OpenFile> open_files;
  int sz;
  int bufsize = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, 0, 0);
  if (bufsize == -1) {
    LOG(ERROR) << "An error occured retrieving the open files " << pid;
    return open_files;
  }

  proc_fdinfo fd_infos[bufsize / PROC_PIDLISTFD_SIZE];

  int num_fds = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, fd_infos, sizeof(fd_infos));
  struct vnode_fdinfowithpath vnode_info;
  struct socket_fdinfo socket_info;
  void * la = NULL, * fa = NULL;
  int lp, fp, v4mapped;
  char buf[1024];

  for (int i = 0; i < num_fds; ++i) {
    OpenFile row;
    auto fd_info = fd_infos[i];
    switch (fd_info.proc_fdtype) {
      case PROX_FDTYPE_VNODE:
        row.file_type = "file";
        sz = proc_pidfdinfo(pid, fd_info.proc_fd, PROC_PIDFDVNODEPATHINFO, &vnode_info, PROC_PIDFDVNODEPATHINFO_SIZE);
        if (sz > 0) {
          row.local_path = std::string(vnode_info.pvip.vip_path);
        }
        break;
      case PROX_FDTYPE_SOCKET:
        // Its a socket
        sz = proc_pidfdinfo(pid, fd_info.proc_fd, PROC_PIDFDSOCKETINFO, &socket_info, PROC_PIDFDSOCKETINFO_SIZE);

        if (sz > 0) {
          switch (socket_info.psi.soi_family) {
            case AF_INET:
              if (socket_info.psi.soi_kind == SOCKINFO_TCP) {
                row.file_type = "TCP";

                la = &socket_info.psi.soi_proto.pri_tcp.tcpsi_ini.insi_laddr.ina_46.i46a_addr4;
                lp = ntohs(socket_info.psi.soi_proto.pri_tcp.tcpsi_ini.insi_lport);
                fa = &socket_info.psi.soi_proto.pri_tcp.tcpsi_ini.insi_faddr.ina_46.i46a_addr4;
                fp = ntohs(socket_info.psi.soi_proto.pri_tcp.tcpsi_ini.insi_fport);

              } else {
                row.file_type = "UDP";
                la = &socket_info.psi.soi_proto.pri_in.insi_laddr.ina_46.i46a_addr4;
                lp = ntohs(socket_info.psi.soi_proto.pri_in.insi_lport);
                fa = &socket_info.psi.soi_proto.pri_in.insi_faddr.ina_46.i46a_addr4;
                fp = ntohs(socket_info.psi.soi_proto.pri_in.insi_fport);
              }

              row.local_host = std::string(inet_ntop(AF_INET, &(((struct sockaddr_in *)la)->sin_addr), buf, sizeof(buf)));
              row.local_port  = boost::lexical_cast<std::string>(lp);
              row.remote_host = std::string(inet_ntop(AF_INET, &(((struct sockaddr_in *)fa)->sin_addr), buf, sizeof(buf)));
              row.remote_port = boost::lexical_cast<std::string>(fp);

              break;
            case AF_INET6:
              if (socket_info.psi.soi_kind == SOCKINFO_TCP) {
                row.file_type = "TCP6";

                la = &socket_info.psi.soi_proto.pri_tcp.tcpsi_ini.insi_laddr.ina_6;
                lp = ntohs(socket_info.psi.soi_proto.pri_tcp.tcpsi_ini.insi_lport);
                fa = &socket_info.psi.soi_proto.pri_tcp.tcpsi_ini.insi_faddr.ina_6;
                fp = ntohs(socket_info.psi.soi_proto.pri_tcp.tcpsi_ini.insi_fport);

                if ((socket_info.psi.soi_proto.pri_tcp.tcpsi_ini.insi_vflag & INI_IPV4) != 0) {
                  v4mapped = 1;
                }
              } else {
                row.file_type = "UDP6";

                la = &socket_info.psi.soi_proto.pri_in.insi_laddr.ina_6;
                lp = ntohs(socket_info.psi.soi_proto.pri_in.insi_lport);
                fa = &socket_info.psi.soi_proto.pri_in.insi_faddr.ina_6;
                fp = ntohs(socket_info.psi.soi_proto.pri_in.insi_fport);

                if ((socket_info.psi.soi_proto.pri_in.insi_vflag & INI_IPV4) != 0) {
                  v4mapped = 1;
                }
              }

              if (v4mapped) {
                // Adjust IPv4 addresses mapped in IPv6 addresses.
                if (la) {
                  la = (struct sockaddr *)IPv6_2_IPv4(la);
                }
                if (fa) {
                  fa = (struct sockaddr *)IPv6_2_IPv4(fa);
                }
              }

              row.local_host = std::string(inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)la)->sin6_addr), buf, sizeof(buf)));
              row.local_port  = boost::lexical_cast<std::string>(lp);
              row.remote_host = std::string(inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)fa)->sin6_addr), buf, sizeof(buf)));
              row.remote_port = boost::lexical_cast<std::string>(fp);
              break;
            default:
              break;
          }
        }

        break;
      default:
        break;
    }

    open_files.push_back(row);
  }
  return open_files;
}

QueryData genProcesses() {
  QueryData results;
  auto pidlist = getProcList();
  auto parent_pid = getParentMap(pidlist);

  for (auto& pid : pidlist) {
    Row r;
    r["pid"] = boost::lexical_cast<std::string>(pid);
    r["name"] = getProcName(pid);
    r["path"] = getProcPath(pid);

    const auto parent_it = parent_pid.find(pid);
    if (parent_it != parent_pid.end()) {
      r["parent"] = boost::lexical_cast<std::string>(parent_it->second);
    } else {
      r["parent"] = "-1";
    }

    // if the path of the executable that started the process is available and
    // the path exists on disk, set on_disk to 1.  if the path is not
    // available, set on_disk to -1.  if, and only if, the path of the
    // executable is available and the file does not exist on disk, set on_disk
    // to 0.
    r["on_disk"] = osquery::pathExists(r["path"]).toString();

    // systems usage and time information
    struct rusage_info_v2 rusage_info_data;
    int rusage_status = proc_pid_rusage(
        pid, RUSAGE_INFO_V2, (rusage_info_t*)&rusage_info_data);
    // proc_pid_rusage returns -1 if it was unable to gather information
    if (rusage_status == 0) {
      // size information
      r["wired_size"] =
          boost::lexical_cast<std::string>(rusage_info_data.ri_wired_size);
      r["resident_size"] =
          boost::lexical_cast<std::string>(rusage_info_data.ri_resident_size);
      r["phys_footprint"] =
          boost::lexical_cast<std::string>(rusage_info_data.ri_phys_footprint);

      // time information
      r["user_time"] =
          boost::lexical_cast<std::string>(rusage_info_data.ri_user_time);
      r["system_time"] =
          boost::lexical_cast<std::string>(rusage_info_data.ri_system_time);
      r["start_time"] = boost::lexical_cast<std::string>(
          rusage_info_data.ri_proc_start_abstime);
    }

    // save the results
    results.push_back(r);
  }

  return results;
}

QueryData genProcessEnvs() {
  QueryData results;
  auto pidlist = getProcList();
  int argmax = genMaxArgs();

  for (auto& pid : pidlist) {
    auto env = getProcEnv(pid, argmax);
    for (auto env_itr = env.begin(); env_itr != env.end(); ++env_itr) {
      Row r;

      r["pid"] = boost::lexical_cast<std::string>(pid);
      r["name"] = getProcName(pid);
      r["path"] = getProcPath(pid);
      r["key"] = env_itr->first;
      r["value"] = env_itr->second;

      results.push_back(r);
    }
  }

  return results;
}

QueryData genProcessOpenFiles() {
  QueryData results;
  auto pidlist = getProcList();

  for (auto& pid : pidlist) {
    auto open_files = getOpenFiles(pid);
    for (auto& open_file : open_files) {
      Row r;

      r["pid"] = boost::lexical_cast<std::string>(pid);
      r["name"] = getProcName(pid);
      r["path"] = getProcPath(pid);
      r["file_type"] = open_file.file_type;
      r["local_path"] = open_file.local_path;
      r["local_host"] = open_file.local_host;
      r["local_port"] = open_file.local_port;
      r["remote_host"] = open_file.remote_host;
      r["remote_port"] = open_file.remote_port;

      results.push_back(r);
    }
  }

  return results;
}
}
}

max_stars_repo_name: bowlofstew/osquery

max_issues_repo_name: bowlofstew/osquery

max_forks_repo_name: bowlofstew/osquery