text:
#include <iostream>

#include "matplotlibcpp.h"

#include <Eigen/Dense>
#include "../src/rk_implementer.hpp"
#include "../src/rk_solvers.hpp"

namespace plt = matplotlibcpp;

void lotkaVolterra(){

  /**
   *  Setting global variables, the time we are integrating over
   *  and the number of integration steps we take in this time.
   *
   *  We also define the function f representing our ODE
   *  and the initial conditiond y0.
   */

  unsigned int steps = 1000;
  unsigned int time = 13;

  auto f = [] (Eigen::VectorXd y) {
    Eigen::VectorXd df(2);
    df << y(0)*(4-4.0/3*y(1)) , -y(1)*(0.8-0.4*y(0));

    return df;
  };

  Eigen::VectorXd y0(2);
  y0 << 6,3;

  /**
   * Solve using built in solver:
   */

  std::vector<Eigen::VectorXd> result = ExplicitRKSolvers::classical4thOrderRuleIntegrator(f, time,y0, steps);

  std::vector<double> t(steps);
  std::vector<double> prey(steps);
  std::vector<double> predator(steps);

    for(size_t i = 0; i < t.size(); i++) {
        t[i] = i*(time / (double) steps);
        prey[i] = result[i](0);
        predator[i] = result[i](1);
    }
    plt::title("Lotka-Volterra Integration Example");

    plt::named_plot("Prey Population",t, prey);
    plt::named_plot("Predator Population",t,predator);

    plt::xlabel("Time");
    plt::ylabel("Population");
    plt::legend();

    plt::save("lotkaVolterraSolved.png");

}

void lorenzAttractor(){

  /**
   *  Setting global variables, the time we are integrating over
   *  and the number of integration steps we take in this time.
   *
   *  We also define the function f representing our ODE
   *  and the initial conditiond y0.
   */

  unsigned int steps = 10000;
  unsigned int time = 50;
  double sigma = 10;
  double rho = 28;
  double beta = 8.0/3;

  auto f = [beta,sigma,rho] (Eigen::VectorXd y) {
    Eigen::VectorXd df(3);
    df << sigma*(y(1) - y(0)),
          y(0)*(rho-y(2)) - y(1),
          y(0)*y(1) - beta*y(2);

    return df;
  };

  Eigen::VectorXd y0(3);
  y0 << 1,1,1;

  /**
   * Solve using built in solver:
   */

  std::vector<Eigen::VectorXd> result = ExplicitRKSolvers::classical4thOrderRuleIntegrator(f, time,y0, steps);

  std::vector<double> x(steps);
  std::vector<double> y(steps);
  std::vector<double> z(steps);

    for(size_t i = 0; i < x.size(); i++) {
        x[i] = result[i](0);
        y[i] = result[i](1);
        z[i] = result[i](2);
    }
    plt::plot3(x, y, z);

    plt::xlabel("x");
    plt::ylabel("y");
    plt::set_zlabel("z"); // set_zlabel rather than just zlabel, in accordance with the Axes3D method

    // plt::title needs to be called after plot3(), otherwise title won't show up
    plt::title("Lorenz Attractor Integration Example");
    plt::save("lorenzAttractorSolved.png");

}

int main(void) {
  lotkaVolterra();
  lorenzAttractor();
  plt::show();
}

max_stars_repo_name: davidrzs/Runge-Kutta-ODE-Solver

max_issues_repo_name: davidrzs/Runge-Kutta-ODE-Solver

max_forks_repo_name: davidrzs/Runge-Kutta-ODE-Solver


text:
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/regex.hpp>
int main()
{
    boost::asio::ip::tcp::iostream s("tycho.usno.navy.mil", "http");
    if(!s)
        std::cout << "Could not connect to tycho.usno.navy.mil\n";
    s  << "GET /cgi-bin/timer.pl HTTP/1.0\r\n"
       << "Host: tycho.usno.navy.mil\r\n"
       << "Accept: */*\r\n"
       << "Connection: close\r\n\r\n" ;
    for(std::string line; getline(s, line); )
    {
        boost::smatch matches;
        if(regex_search(line, matches, boost::regex("<BR>(.+\\s+UTC)") ) )
        {
            std::cout << matches[1] << '\n';
            break;
        }
    }
}

ext: cpp

max_stars_repo_name: ethansaxenian/RosettaDecode

max_issues_repo_name: ethansaxenian/RosettaDecode

max_forks_repo_name: ethansaxenian/RosettaDecode


text:
/**
 ** Copyright (c) 2014 Illumina, Inc.
 **
 ** This file is part of Illumina's Enhanced Artificial Genome Engine (EAGLE),
 ** covered by the "BSD 2-Clause License" (see accompanying LICENSE file)
 **
 ** \description Fast fasta reader, which we keep until the mainstream reader is faster than this one
 **
 ** \author Lilian Janin
 **/

#ifndef TMP_FASTA_READER_HH
#define TMP_FASTA_READER_HH

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

#include "genome/Reference.hh"

using namespace std;

namespace eagle
{
namespace genome
{

struct TmpFastaFileInfo
{
    ifstream *file;
    unsigned long globalPosMin, globalPosMax;
    unsigned int headerLength;
    unsigned int basesPerLine;
    unsigned long baseCount;
    string contigName;
    unsigned long fileSize;
};

class TmpFastaReader
{
public:
    TmpFastaReader( const boost::filesystem::path &refDir );
    const vector<string> allContigNames() { return fastaRef_.allContigNames(); }
    const vector<unsigned long> allContigLengths() { return fastaRef_.allContigLengths(); }
    char get( const unsigned long globalPos, const unsigned long offset, bool& overlapContigBoundary );
    void convertFromGlobalPos( const unsigned long globalPos, int& refId, unsigned long& posInContig );

private:
    MultiFastaReference fastaRef_;
    vector<TmpFastaFileInfo> fileInfos_;
};

} // namespace genome
} // namespace eagle

#endif

ext: hh

max_stars_repo_name: sequencing/EAGLE

max_issues_repo_name: sequencing/EAGLE

max_forks_repo_name: sequencing/EAGLE


text:
#include <iostream> // for standard I/O
#include <iomanip>  // for controlling float print precision
#include <opencv2/core.hpp>     // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/imgproc.hpp>  // Gaussian Blur
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>  // OpenCV window I/O
#include <map>
#include <random>
#include <opencv2/text.hpp>
#include <boost/filesystem.hpp>
#include "TextDetection/TextDetection.h"
#include "SubtitleLoader.h"
#include "PicClean.h"

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

int same_thres_per = 1500;
int id = 0;
cv::Size SUBTITLESIZE(850, 35);

Mat process_imgs(vector<Mat> &imgs, string out_path) {
    auto path = fs::system_complete(out_path);
    auto out = PicClean::keep_common(imgs);
//    imshow("out", out);
    imwrite(path.string()+ "/" + to_string(id++) + ".jpg", out);
//    imshow("out", out);
//    waitKey();
    // 每输出10张报告一次
    if (id % 10 == 0) {
        cout << id << " completed" << endl;
    }
    imgs.clear();
    return out;
}

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        cerr << "usage: input_video output_dir_name" << endl;
        exit(-1);
    }
    string input_video = argv[1],
        dir_name = argv[2];

    fs::create_directory(dir_name);

    SubtitleLoader lder(input_video, 0.2);
    lder.set_frame_id(0);
    vector<Mat> imgs;
    try {
        while (true) {
            auto frame = lder.load();
//            resize
            cv::resize(frame, frame, SUBTITLESIZE);

            frame = PicClean::keep_white(frame);
//            imshow("frame", frame);
//            waitKey();
            auto bound = PicClean::locate_subtitle(frame);
            if (abs(bound.first - bound.second) < 5) {
                if (!imgs.empty()) {
                    process_imgs(imgs, dir_name);
                }
                else {
                    continue;
                }
            }
            else {
                if (imgs.empty()) {
                    imgs.push_back(frame);
                }
                else {
                    if (PicClean::same_subtitle(imgs.back(), frame, same_thres_per*abs(bound.second-bound.first), bound.first, bound.second)) {
                        imgs.push_back(frame);
                    }
                    else {
                        process_imgs(imgs, dir_name);
                    }
                }
            }

        }
    }
    catch (OutOfFrames e) {
    }

    return 0;
}

max_stars_repo_name: xinnjie/extract-subtitle

max_issues_repo_name: xinnjie/extract-subtitle

max_forks_repo_name: xinnjie/extract-subtitle


text:
#include <memory>

#include <Eigen/Dense>
#include <gtest/gtest.h>

#include <aikido/constraint/DifferentiableIntersection.hpp>
#include <aikido/constraint/dart/TSR.hpp>
#include <aikido/statespace/Rn.hpp>

#include "PolynomialConstraint.hpp"

using aikido::constraint::DifferentiableIntersection;
using aikido::constraint::DifferentiablePtr;
using aikido::constraint::dart::TSR;

using aikido::statespace::R1;
using aikido::statespace::StateSpace;
using aikido::statespace::StateSpacePtr;

TEST(DifferentiableIntersection, InvalidConstructor)
{
  std::vector<DifferentiablePtr> constraints;
  std::shared_ptr<R1> rvss(new R1());

  // empty constraints
  EXPECT_THROW(
      DifferentiableIntersection(constraints, rvss), std::invalid_argument);

  // null statespace
  StateSpacePtr space;
  constraints.push_back(std::make_shared<PolynomialConstraint<1>>(
      Eigen::Vector3d(1, 2, 3), rvss));
  EXPECT_THROW(
      DifferentiableIntersection(constraints, space), std::invalid_argument);

  // constraints have different space
  constraints.push_back(std::make_shared<PolynomialConstraint<1>>(
      Eigen::Vector3d(1, 2, 3), rvss));
  constraints.push_back(dart::common::make_aligned_shared<TSR>());
  EXPECT_THROW(
      DifferentiableIntersection(constraints, rvss), std::invalid_argument);
}

TEST(DifferentiableIntersection, getValue)
{
  std::vector<DifferentiablePtr> constraints;
  std::shared_ptr<R1> rvss(new R1());

  // constraint1: 1 + 2x + 3x^2
  constraints.push_back(std::make_shared<PolynomialConstraint<1>>(
      Eigen::Vector3d(1, 2, 3), rvss));

  // constraint2: 4 + 5x
  constraints.push_back(
      std::make_shared<PolynomialConstraint<1>>(Eigen::Vector2d(4, 5), rvss));

  Eigen::VectorXd v(1);
  v(0) = -2;

  auto s1 = rvss->createState();
  s1.setValue(v);

  DifferentiableIntersection stacked(constraints, rvss);

  Eigen::Vector2d expected(9, -6);
  Eigen::VectorXd out;
  stacked.getValue(s1, out);

  EXPECT_TRUE(out.isApprox(expected));
}

TEST(DifferentiableIntersection, getJacobian)
{
  std::vector<DifferentiablePtr> constraints;
  std::shared_ptr<R1> rvss(new R1());

  // constraint1: 1 + 2x + 3x^2
  constraints.push_back(std::make_shared<PolynomialConstraint<1>>(
      Eigen::Vector3d(1, 2, 3), rvss));

  // constraint2: 4 + 5x
  constraints.push_back(
      std::make_shared<PolynomialConstraint<1>>(Eigen::Vector2d(4, 5), rvss));

  Eigen::VectorXd v(1);
  v(0) = -2;

  auto s1 = rvss->createState();
  s1.setValue(v);

  DifferentiableIntersection stacked(constraints, rvss);
  Eigen::MatrixXd expected(2, 1);
  expected(0, 0) = -10;
  expected(1, 0) = 5;

  Eigen::MatrixXd out;
  stacked.getJacobian(s1, out);

  EXPECT_TRUE(out.isApprox(expected));
}

/// Compare returned values with getValue and getJacobian
TEST(DifferentiableIntersection, GetValueAndJacobianMatchValueAndJacobian)
{
  std::vector<DifferentiablePtr> constraints;
  std::shared_ptr<R1> rvss(new R1());

  // constraint1: 1 + 2x + 3x^2
  constraints.push_back(std::make_shared<PolynomialConstraint<1>>(
      Eigen::Vector3d(1, 2, 3), rvss));

  // constraint2: 4 + 5x
  constraints.push_back(
      std::make_shared<PolynomialConstraint<1>>(Eigen::Vector2d(4, 5), rvss));

  auto s1 = rvss->createState();
  Eigen::VectorXd v(1);
  v(0) = -2;
  s1.setValue(v);

  DifferentiableIntersection stacked(constraints, rvss);
  Eigen::VectorXd val;
  Eigen::MatrixXd jac;
  stacked.getValueAndJacobian(s1, val, jac);

  Eigen::VectorXd value;
  Eigen::MatrixXd jacobian;

  stacked.getValue(s1, value);
  stacked.getJacobian(s1, jacobian);

  EXPECT_TRUE(val.isApprox(value));
  EXPECT_TRUE(jac.isApprox(jacobian));
}

TEST(DifferentiableIntersection, GetConstraintTypes)
{
  std::vector<DifferentiablePtr> constraints;
  std::shared_ptr<R1> rvss(new R1());

  // constraint1: 1 + 2x + 3x^2
  constraints.push_back(std::make_shared<PolynomialConstraint<1>>(
      Eigen::Vector3d(1, 2, 3), rvss));

  // constraint2: 4 + 5x
  constraints.push_back(
      std::make_shared<PolynomialConstraint<1>>(Eigen::Vector2d(4, 5), rvss));

  DifferentiableIntersection stacked(constraints, rvss);
  auto constraintTypes = stacked.getConstraintTypes();

  // Check the size of ConstraintTypes
  EXPECT_EQ(constraintTypes.size(), 2);

  // Check the types
  for (auto cType : constraintTypes)
  {
    EXPECT_EQ(cType, aikido::constraint::ConstraintType::EQUALITY);
  }
}

TEST(DifferentiableIntersection, GetStateSpace)
{
  std::vector<DifferentiablePtr> constraints;
  std::shared_ptr<R1> rvss(new R1());

  // constraint1: 1 + 2x + 3x^2
  constraints.push_back(std::make_shared<PolynomialConstraint<1>>(
      Eigen::Vector3d(1, 2, 3), rvss));

  // constraint2: 4 + 5x
  constraints.push_back(
      std::make_shared<PolynomialConstraint<1>>(Eigen::Vector2d(4, 5), rvss));

  DifferentiableIntersection stacked(constraints, rvss);
  auto space = stacked.getStateSpace();

  EXPECT_EQ(space, rvss);
}


text:
#include "bpt.h"
#include "../engine/stdafx.h"
#include "../engine/util.h"
#include <boost/property_tree/xml_parser.hpp>

BPT* BPT::_inst;
BPT* BPT::inst()
{
	if (_inst == NULL) _inst = new BPT();
	return _inst;
}

BPT::BPT()
{
	using boost::property_tree::ptree;
	std::stringstream ss;
	IOManager::readFileToBuffer("data/lang.xml", ss);
	//read_xml("data/pd/lang.xml", pt);
	read_xml(ss, pt);
}

BPT::~BPT()
{
}

std::string BPT::getText(const std::string& key)
{
	return inst()->pt.get<std::string>(key);
}

max_stars_repo_name: mtribiere/Pixel-dungeon-CPP

max_issues_repo_name: mtribiere/Pixel-dungeon-CPP

max_forks_repo_name: mtribiere/Pixel-dungeon-CPP


text:
/* for_each_line_test.cc
   Wolfgang Sourdeau, 28 August 2014
   Copyright (c) 2014 mldb.ai inc.  All rights reserved.
   This file is part of MLDB. Copyright 2015 mldb.ai inc. All rights reserved.

   Test for_each_line
*/

#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK

#include <atomic>
#include <sstream>
#include <string>
#include <vector>
#include <boost/test/unit_test.hpp>
#include "mldb/arch/exception.h"
#include "mldb/utils/string_functions.h"
#include "mldb/utils/vector_utils.h"
#include "mldb/utils/for_each_line.h"

using namespace std;
using namespace MLDB;

namespace {

vector<string> dataStrings{"line1", "line2", "", "line forty 2"};

}

BOOST_AUTO_TEST_CASE( test_forEachLine_data )
{
    vector<string> expected(dataStrings);
    expected.emplace_back("");

    string data;
    for (const auto & line: dataStrings) {
        data += line + "\n";
    }
    istringstream stream(data);

    vector<string> result;
    auto processLine = [&] (const char * data, size_t dataSize, int64_t lineNum) {
        result.emplace_back(data, dataSize);
    };

    auto logger = getMldbLog("test");
    forEachLine(stream, processLine, logger);
    BOOST_CHECK_EQUAL(result, expected);
}

BOOST_AUTO_TEST_CASE( test_forEachLineStr_data )
{
    vector<string> expected(dataStrings);
    expected.emplace_back("");

    string data;
    for (const auto & line: dataStrings) {
        data += line + "\n";
    }
    istringstream stream(data);

    vector<string> result;
    auto processLine = [&] (const string & data, int64_t lineNum) {
        result.emplace_back(data);
    };

    auto logger = getMldbLog("test");
    forEachLineStr(stream, processLine, logger);
    BOOST_CHECK_EQUAL(result, expected);
}

BOOST_AUTO_TEST_CASE( test_forEachLine_throw )
{
    string data;
    for (int i = 0; i < 1000; i++) {
        data += to_string(i) + "\n";
    }
    istringstream stream(data);

    atomic<int> count(0);
    auto processLine = [&] (const string & data, int64_t lineNum) {
        if (count.fetch_add(1) > 500) {
            throw MLDB::Exception("thrown");
        }
    };

    auto logger = getMldbLog("test");
    BOOST_CHECK_THROW(forEachLineStr(stream, processLine, logger), MLDB::Exception);
}

ext: cc

max_stars_repo_name: kstepanmpmg/mldb

max_issues_repo_name: Davletvm/mldb

max_forks_repo_name: matebestek/mldb


text:
/**
 * Copyright (C) 2020 Xilinx, Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License"). You may
 * not use this file except in compliance with the License. A copy of the
 * License is located 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.
 */

// ------ I N C L U D E   F I L E S -------------------------------------------
// Local - Include Files
#include "ReportPlatform.h"
#include "flash/flasher.h"
#include "flash/firmware_image.h"
#include "core/common/query_requests.h"

// Utilities
#include "tools/common/XBUtilities.h"
namespace XBU = XBUtilities;

// 3rd Party Library - Include Files
#include <boost/format.hpp>

static boost::format fmtBasic("  %-20s : %s\n");

void
ReportPlatform::getPropertyTreeInternal( const xrt_core::device * device,
                                         boost::property_tree::ptree &pt) const
{
  // Defer to the 20202 format.  If we ever need to update JSON data,
  // Then update this method to do so.
  getPropertyTree20202(device, pt);
}

/*
 * helper function for getPropertyTree20202()
 */
static bool
same_shell(const std::string& vbnv, const std::string& id,
            const DSAInfo& installed)
{
  if (!vbnv.empty()) {
    bool same_dsa = ((installed.name == vbnv) &&
      (installed.matchId(id)));
    return same_dsa;
  }
  return false;
}

/*
 * helper function for getPropertyTree20202()
 */
static bool
same_sc(const std::string& sc, const DSAInfo& installed)
{
  return ((sc.empty()) || (installed.bmcVer == sc) || (sc.find("FIXED") != std::string::npos));
}

/*
 * scan for plps installed on the system
 */
static boost::property_tree::ptree
get_installed_partitions(std::string interface_uuid)
{
  auto availableDSAs = firmwareImage::getIntalledDSAs();
  boost::property_tree::ptree pt_plps;
  for(unsigned int i = 0; i < availableDSAs.size(); i++) {
    boost::property_tree::ptree pt_plp;
    DSAInfo installedDSA = availableDSAs[i];
    if(installedDSA.hasFlashImage || installedDSA.uuids.empty())
      continue;
    pt_plp.put("vbnv", installedDSA.name);
    //the first UUID is always the logic UUID
    pt_plp.put("logic-uuid", XBU::string_to_UUID(installedDSA.uuids[0]));

    // Find the UUID that it exposes for other partitions
    for(unsigned int j = 1; j < installedDSA.uuids.size(); j++){
      //check if the interface UUID is resolution of BLP
      if(interface_uuid.compare(installedDSA.uuids[j]) != 0)
        continue;
      pt_plp.put("interface-uuid", XBU::string_to_UUID(installedDSA.uuids[j]));
    }
    pt_plp.put("file", installedDSA.file);

    //if the partition doesn't resolve the passed in BLP, don't add it to the list
    if(!pt_plp.get<std::string>("interface-uuid", "").empty())
      pt_plps.push_back( std::make_pair("", pt_plp) );
  }
  return pt_plps;
}

void
ReportPlatform::getPropertyTree20202( const xrt_core::device * device,
                                      boost::property_tree::ptree &pt) const
{
  boost::property_tree::ptree pt_platform;

  Flasher f(device->get_device_id());

  BoardInfo info;
  f.getBoardInfo(info);
  //create information tree for a device
  pt_platform.put("bdf", xrt_core::query::pcie_bdf::to_string(xrt_core::device_query<xrt_core::query::pcie_bdf>(device)));
  pt_platform.put("flash_type", xrt_core::device_query<xrt_core::query::flash_type>(device));
  pt_platform.put("hardware.serial_num", info.mSerialNum);

  //Flashable partition running on FPGA

  std::vector<std::string> logic_uuids, interface_uuids;
  // the vectors are being populated by empty strings which need to be removed
  try {
    logic_uuids = xrt_core::device_query<xrt_core::query::logic_uuids>(device);
    logic_uuids.erase(
      std::remove_if(logic_uuids.begin(), logic_uuids.end(),
                      [](const std::string& s) { return s.empty(); }), logic_uuids.end());
  } catch (...) {}
  try {
    interface_uuids = xrt_core::device_query<xrt_core::query::interface_uuids>(device);
    interface_uuids.erase(
      std::remove_if(interface_uuids.begin(), interface_uuids.end(),
                  [](const std::string& s) { return s.empty(); }), interface_uuids.end());
  } catch (...) {}

  boost::property_tree::ptree pt_current_shell;
  if(xrt_core::device_query<xrt_core::query::is_mfg>(device)) { // golden
    std::string board_name = xrt_core::device_query<xrt_core::query::board_name>(device);
    std::string vbnv = "xilinx_" + board_name + "_GOLDEN";
    pt_current_shell.put("vbnv", vbnv);
  } else if(!logic_uuids.empty() && !interface_uuids.empty()) { // 2RP
    try {
      DSAInfo part("", NULL_TIMESTAMP, logic_uuids[0], "");
      pt_current_shell.put("vbnv", part.name);
      pt_current_shell.put("logic-uuid", XBU::string_to_UUID(logic_uuids[0]));
      pt_current_shell.put("interface-uuid", XBU::string_to_UUID(interface_uuids[0]));
      pt_current_shell.put("id", (boost::format("0x%x") % part.timestamp));
    } catch (...) {
      //safe to ignore exceptions. We print out whatever information that is available to us
    }

      boost::property_tree::ptree pt_plps;
      for(unsigned int i = 1; i < logic_uuids.size(); i++) {
        boost::property_tree::ptree pt_plp;
        DSAInfo partition("", NULL_TIMESTAMP, logic_uuids[i], "");
        pt_plp.put("vbnv", partition.name);
        pt_plp.put("logic-uuid", XBU::string_to_UUID(logic_uuids[i]));
        pt_plp.put("interface-uuid", XBU::string_to_UUID(interface_uuids[i]));
        pt_plps.push_back( std::make_pair("", pt_plp) );
      }
      pt_platform.put_child("current_partitions", pt_plps);
  } else { //1RP
    pt_current_shell.put("vbnv", xrt_core::device_query<xrt_core::query::rom_vbnv>(device));
    pt_current_shell.put("id", (boost::format("0x%x") % xrt_core::device_query<xrt_core::query::rom_time_since_epoch>(device)));
  }

  std::string sc_ver;
  try {
    sc_ver = xrt_core::device_query<xrt_core::query::xmc_sc_version>(device);
  } catch (...) {}
  if(sc_ver.empty())
    sc_ver = info.mBMCVer;
  pt_current_shell.put("sc_version", sc_ver);
  pt_platform.add_child("current_shell", pt_current_shell);

  //Flashable partitions installed in system
  std::vector<DSAInfo> availableDSAs = f.getInstalledDSA();
  boost::property_tree::ptree pt_available_shells;
  for(unsigned int i = 0; i < availableDSAs.size(); i++) {
    boost::property_tree::ptree pt_available_shell;
    DSAInfo installedDSA = availableDSAs[i];
    pt_available_shell.put("vbnv", installedDSA.name);
    pt_available_shell.put("sc_version", installedDSA.bmcVer);
    pt_available_shell.put("id", (boost::format("0x%x") % installedDSA.timestamp));
    //the first UUID in the list is logic-uuid
    pt_available_shell.put("logic-uuid", XBU::string_to_UUID(installedDSA.uuids[0]));
    pt_available_shell.put("file", installedDSA.file);

    boost::property_tree::ptree pt_status;
    pt_status.put("shell", same_shell( pt_current_shell.get<std::string>("vbnv", ""),
              pt_current_shell.get<std::string>("id", ""), installedDSA));
    pt_status.put("sc", same_sc( pt_current_shell.get<std::string>("sc_version", ""), installedDSA));
    pt_platform.add_child("status", pt_status);

    pt_available_shells.push_back( std::make_pair("", pt_available_shell) );
  }
  pt_platform.put_child("available_shells", pt_available_shells);

  if (!interface_uuids.empty()) {
    auto pt_available_partitions = get_installed_partitions(interface_uuids[0]);
    pt_platform.put_child("available_partitions", pt_available_partitions);
  }

  // There can only be 1 root node
  pt.add_child("platform", pt_platform);
}

static const std::string
shell_status(bool shell_status, bool sc_status, int num_dsa_shells)
{
  if(num_dsa_shells == 0)
    return boost::str(boost::format("%-8s : %s\n") % "WARNING" % "No shell is installed on the system.");
  if(num_dsa_shells > 1)
    return boost::str(boost::format("%-8s : %s\n") % "WARNING" % "Multiple shells are installed on the system.");
  if(!shell_status)
    return boost::str(boost::format("%-8s : %s\n") % "WARNING" % "Device is not up-to-date.");
  if(!sc_status)
    return boost::str(boost::format("%-8s : %s\n") % "WARNING" % "SC image on the device is not up-to-date.");
  return "";
}

void
ReportPlatform::writeReport( const xrt_core::device * device,
                             const std::vector<std::string> & /*_elementsFilter*/,
                             std::iostream & output) const
{
  boost::property_tree::ptree pt;
  getPropertyTreeInternal(device, pt);

  output << "Flash properties\n";
  output << fmtBasic % "Type" % pt.get<std::string>("platform.flash_type", "N/A");
  output << fmtBasic % "Serial Number" % pt.get<std::string>("platform.hardware.serial_num", "N/A");
  output << std::endl;

  output << "Flashable partitions running on FPGA\n";
  output << fmtBasic % "Platform" % pt.get<std::string>("platform.current_shell.vbnv", "N/A");
  output << fmtBasic % "SC Version" % pt.get<std::string>("platform.current_shell.sc_version", "N/A");

  // print platform ID, for 2RP, platform ID = logic UUID
  auto logic_uuid = pt.get<std::string>("platform.current_shell.logic-uuid", "");
  auto interface_uuid = pt.get<std::string>("platform.current_shell.interface-uuid", "");
  if (!logic_uuid.empty() && !interface_uuid.empty()) {
    output << fmtBasic % "Platform UUID" % logic_uuid;
    output << fmtBasic % "Interface UUID" % interface_uuid;
  } else {
    output << fmtBasic % "Platform ID" % pt.get<std::string>("platform.current_shell.id", "N/A");
  }
  output << std::endl;

  // List PLP running on the system
  boost::property_tree::ptree pt_empty;
  boost::property_tree::ptree& plps = pt.get_child("platform.current_partitions", pt_empty);
  for(auto& kv : plps) {
    boost::property_tree::ptree& plp = kv.second;
    output << fmtBasic % "Platform" % plp.get<std::string>("vbnv", "N/A");
    output << fmtBasic % "Logic UUID" % plp.get<std::string>("logic-uuid", "N/A");
    output << fmtBasic % "Interface UUID" % plp.get<std::string>("interface-uuid", "N/A");
    output << std::endl;
  }

  output << "Flashable partitions installed in system\n";
  boost::property_tree::ptree& available_shells = pt.get_child("platform.available_shells");

  if(available_shells.empty())
    output << boost::format("  %-20s\n") % "None" << std::endl;

  for(auto& kv : available_shells) {
    boost::property_tree::ptree& available_shell = kv.second;
    output << fmtBasic % "Platform" % available_shell.get<std::string>("vbnv", "N/A");
    output << fmtBasic % "SC Version" % available_shell.get<std::string>("sc_version", "N/A");
    // print platform ID, for 2RP, platform ID = logic UUID
    auto platform_uuid = available_shell.get<std::string>("logic-uuid", "");
    if (!platform_uuid.empty()) {
      output << fmtBasic % "Platform UUID" % platform_uuid;
    } else {
      output << fmtBasic % "Platform ID" % available_shell.get<std::string>("id", "N/A");
    }
      output << std::endl;
  }

  //PLPs installed on the system
  boost::property_tree::ptree& available_plps = pt.get_child("platform.available_partitions", pt_empty);
  for(auto& kv : available_plps) {
    boost::property_tree::ptree& plp = kv.second;
    output << fmtBasic % "Platform" % plp.get<std::string>("vbnv", "N/A");
    output << fmtBasic % "Logic UUID" % plp.get<std::string>("logic-uuid", "N/A");
    output << fmtBasic % "Interface UUID" % plp.get<std::string>("interface-uuid", "N/A");
    output << std::endl;
  }

  output << "----------------------------------------------------\n"
         << shell_status(pt.get<bool>("platform.status.shell", false),
                         pt.get<bool>("platform.status.sc", false),  static_cast<int>(available_shells.size()));
}

max_stars_repo_name: ccasexlnx/XRT

max_issues_repo_name: ccasexlnx/XRT

max_forks_repo_name: ccasexlnx/XRT


text:
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2012 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "db.h"

#ifndef WIN32
#include <sys/stat.h>
#endif

#include <boost/filesystem.hpp>
#include <boost/version.hpp>
#include <openssl/rand.h>
#include <boost/thread.hpp>
#ifdef WIN32
#include <ShlObj.h>
#endif
#include <fc/log/logger.hpp>

using namespace std;
using namespace boost;

unsigned int nWalletDBUpdated;

#ifdef WIN32
#define MSG_NOSIGNAL        0
#define MSG_DONTWAIT        0

#ifndef S_IRUSR
#define S_IRUSR             0400
#define S_IWUSR             0200
#endif
#else
#define MAX_PATH            1024
#endif

//
// CDB
//

inline void MilliSleep(int64_t n)
{
#if BOOST_VERSION >= 105000
	boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
#else
	boost::this_thread::sleep(boost::posix_time::milliseconds(n));
#endif
}
inline int64_t GetTimeMillis()
{
	return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
		boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))).total_milliseconds();
}

#ifdef WIN32
boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
{
	namespace fs = boost::filesystem;

	char pszPath[MAX_PATH] = "";

	if (SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
	{
		return fs::path(pszPath);
	}

	dlog("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
	return fs::path("");
}
#endif
boost::filesystem::path GetDefaultDataDir()
{
	namespace fs = boost::filesystem;
	// Windows < Vista: C:\Documents and Settings\Username\Application Data\HShare
	// Windows >= Vista: C:\Users\Username\AppData\Roaming\HShare
	// Mac: ~/Library/Application Support/HShare
	// Unix: ~/.hshare
#ifdef WIN32
	// Windows
	return GetSpecialFolderPath(CSIDL_APPDATA,true) / "HShare";
#else
	fs::path pathRet;
	char* pszHome = getenv("HOME");
	if (pszHome == NULL || strlen(pszHome) == 0)
		pathRet = fs::path("/");
	else
		pathRet = fs::path(pszHome);
#ifdef MAC_OSX
	// Mac
	pathRet /= "Library/Application Support";
	fs::create_directory(pathRet);
	return pathRet / "HShare";
#else
	// Unix
	return pathRet / ".hshare";
#endif
#endif
}

boost::filesystem::path GetDataDir()
{
	namespace fs = boost::filesystem;

	fs::path path;
	// This can be called during exceptions by dlogf(), so we cache the
	// value so we don't have to do memory allocations after that.

	path = GetDefaultDataDir();
	if(!fs::exists(path))
		fs::create_directory(path);
	path /= "hshareoldwallet";
	fs::create_directory(path);

	return path;
}

static const signed char phexdigit[256] =
{ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
-1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };

vector<unsigned char> ParseHex(const char* psz)
{
	// convert hex dump to vector
	vector<unsigned char> vch;
	while (true)
	{
		while (isspace(*psz))
			psz++;
		signed char c = phexdigit[(unsigned char)*psz++];
		if (c == (signed char)-1)
			break;
		unsigned char n = (c << 4);
		c = phexdigit[(unsigned char)*psz++];
		if (c == (signed char)-1)
			break;
		n |= c;
		vch.push_back(n);
	}
	return vch;
}

vector<unsigned char> ParseHex(const string& str)
{
	return ParseHex(str.c_str());
}

CDBEnv bitdb;

void CDBEnv::EnvShutdown()
{
	if (!fDbEnvInit)
		return;

	fDbEnvInit = false;
	int ret = dbenv.close(0);
	if (ret != 0)
		ulog("EnvShutdown exception: ${p} ${q}\n", (("p",DbEnv::strerror(ret)),("q", ret)));
	if (!fMockDb)
		DbEnv(0).remove(strPath.c_str(), 0);
}

CDBEnv::CDBEnv() : dbenv(DB_CXX_NO_EXCEPTIONS)
{
	fDbEnvInit = false;
	fMockDb = false;
}

CDBEnv::~CDBEnv()
{
	EnvShutdown();
}

void CDBEnv::Close()
{
	EnvShutdown();
}

bool CDBEnv::Open(boost::filesystem::path pathEnv_)
{
	if (fDbEnvInit)
		return true;

	boost::this_thread::interruption_point();

	pathEnv = pathEnv_;
	filesystem::path pathDataDir = pathEnv;
	strPath = pathDataDir.string();
	filesystem::path pathLogDir = pathDataDir / "database";
	filesystem::create_directory(pathLogDir);
	filesystem::path pathErrorFile = pathDataDir / "db.log";
	dlog("dbenv.open LogDir=%s ErrorFile=%s\n",( pathLogDir.string(), pathErrorFile.string()));

	unsigned int nEnvFlags = 0;
	nEnvFlags |= DB_PRIVATE;

	int nDbCache = 25;
	dbenv.set_lg_dir(pathLogDir.string().c_str());
	dbenv.set_cachesize(nDbCache / 1024, (nDbCache % 1024) * 1048576, 1);
	dbenv.set_lg_bsize(1048576);
	dbenv.set_lg_max(10485760);
	/*dbenv.set_lk_max_locks(10000);
	dbenv.set_lk_max_objects(10000);*/
	dbenv.set_errfile(fopen(pathErrorFile.string().c_str(), "a")); /// debug
	dbenv.set_flags(DB_AUTO_COMMIT, 1);
	dbenv.set_flags(DB_TXN_WRITE_NOSYNC, 1);
#ifdef DB_LOG_AUTO_REMOVE
	dbenv.log_set_config(DB_LOG_AUTO_REMOVE, 1);
#endif
	int ret = dbenv.open(strPath.c_str(),
		DB_CREATE |
		DB_INIT_LOCK |
		DB_INIT_LOG |
		DB_INIT_MPOOL |
		DB_INIT_TXN |
		DB_THREAD |
		DB_RECOVER |
		nEnvFlags,
		S_IRUSR | S_IWUSR);
	if (ret != 0)
	{
		dlog("CDB() : error %s (%d) opening database environment", (DbEnv::strerror(ret), ret));
		return false;
	}

	fDbEnvInit = true;
	fMockDb = false;

	return true;
}

void CDBEnv::MakeMock()
{
	if (fDbEnvInit)
		throw runtime_error("CDBEnv::MakeMock(): already initialized");

	boost::this_thread::interruption_point();

	dlog("CDBEnv::MakeMock()\n");

	dbenv.set_cachesize(1, 0, 1);
	dbenv.set_lg_bsize(10485760 * 4);
	dbenv.set_lg_max(10485760);
	dbenv.set_lk_max_locks(10000);
	dbenv.set_lk_max_objects(10000);
	dbenv.set_flags(DB_AUTO_COMMIT, 1);
#ifdef DB_LOG_IN_MEMORY
	dbenv.log_set_config(DB_LOG_IN_MEMORY, 1);
#endif
	int ret = dbenv.open(NULL,
		DB_CREATE |
		DB_INIT_LOCK |
		DB_INIT_LOG |
		DB_INIT_MPOOL |
		DB_INIT_TXN |
		DB_THREAD |
		DB_PRIVATE,
		S_IRUSR | S_IWUSR);
	if (ret > 0)
	{
		std::string error_message = "CDBEnv::MakeMock(): error " + to_string(ret) + " opening database environment";
		throw runtime_error(error_message);
	}

	fDbEnvInit = true;
	fMockDb = true;
}

CDBEnv::VerifyResult CDBEnv::Verify(std::string strFile, bool(*recoverFunc)(CDBEnv& dbenv, std::string strFile))
{
	LOCK(cs_db);
	assert(mapFileUseCount.count(strFile) == 0);

	Db db(&dbenv, 0);
	int result = db.verify(strFile.c_str(), NULL, NULL, 0);
	if (result == 0)
		return VERIFY_OK;
	else if (recoverFunc == NULL)
		return RECOVER_FAIL;

	// Try to recover:
	bool fRecovered = (*recoverFunc)(*this, strFile);
	return (fRecovered ? RECOVER_OK : RECOVER_FAIL);
}

bool CDBEnv::Salvage(std::string strFile, bool fAggressive,
	std::vector<CDBEnv::KeyValPair >& vResult)
{
	LOCK(cs_db);
	assert(mapFileUseCount.count(strFile) == 0);

	u_int32_t flags = DB_SALVAGE;
	if (fAggressive) flags |= DB_AGGRESSIVE;

	stringstream strDump;

	Db db(&dbenv, 0);
	int result = db.verify(strFile.c_str(), NULL, &strDump, flags);
	if (result == DB_VERIFY_BAD)
	{
		dlog("Error: Salvage found errors, all data may not be recoverable.\n");
		if (!fAggressive)
		{
			dlog("Error: Rerun with aggressive mode to ignore errors and continue.\n");
			return false;
		}
	}
	if (result != 0 && result != DB_VERIFY_BAD)
	{
		dlog("ERROR: db salvage failed: ${p}\n",("p", result));
		return false;
	}

	// Format of bdb dump is ascii lines:
	// header lines...
	// HEADER=END
	// hexadecimal key
	// hexadecimal value
	// ... repeated
	// DATA=END

	string strLine;
	while (!strDump.eof() && strLine != "HEADER=END")
		getline(strDump, strLine); // Skip past header

	std::string keyHex, valueHex;
	while (!strDump.eof() && keyHex != "DATA=END")
	{
		getline(strDump, keyHex);
		if (keyHex != "DATA=END")
		{
			getline(strDump, valueHex);
			vResult.push_back(make_pair(ParseHex(keyHex), ParseHex(valueHex)));
		}
	}

	return (result == 0);
}

void CDBEnv::CheckpointLSN(const std::string& strFile)
{
	dbenv.txn_checkpoint(0, 0, 0);
	if (fMockDb)
		return;
	dbenv.lsn_reset(strFile.c_str(), 0);
}

CDB::CDB(const std::string& strFilename, const char* pszMode) :
	pdb(NULL), activeTxn(NULL)
{
	int ret;
	fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
	if (strFilename.empty())
		return;

	bool fCreate = strchr(pszMode, 'c');
	unsigned int nFlags = DB_THREAD;
	if (fCreate)
		nFlags |= DB_CREATE;

	{
		LOCK(bitdb.cs_db);
		if (!bitdb.Open(GetDataDir()))
			throw runtime_error("env open failed");

		strFile = strFilename;
		++bitdb.mapFileUseCount[strFile];
		pdb = bitdb.mapDb[strFile];
		if (pdb == NULL)
		{
			pdb = new Db(&bitdb.dbenv, 0);

			bool fMockDb = bitdb.IsMock();
			if (fMockDb)
			{
				DbMpoolFile*mpf = pdb->get_mpf();
				ret = mpf->set_flags(DB_MPOOL_NOFILE, 1);
				if (ret != 0)
					throw runtime_error("CDB : Failed to configure for no temp file backing for database "+strFile);
			}

			ret = pdb->open(NULL, // Txn pointer
				fMockDb ? NULL : strFile.c_str(), // Filename
				fMockDb ? strFile.c_str() : "main", // Logical db name
				DB_BTREE, // Database type
				nFlags, // Flags
				0);

			if (ret != 0)
			{
				delete pdb;
				pdb = NULL;
				--bitdb.mapFileUseCount[strFile];
				strFile = "";
					std::string error_message = "CDB : Error" + to_string(ret) + ", can't open database " + strFile ;
					throw runtime_error(error_message);
			}

			if (fCreate && !Exists(string("version")))
			{
				bool fTmp = fReadOnly;
				fReadOnly = false;
				WriteVersion(CLIENT_VERSION);
				fReadOnly = fTmp;
			}

			bitdb.mapDb[strFile] = pdb;
		}
	}
}

void CDB::Close()
{
	if (!pdb)
		return;
	if (activeTxn)
		activeTxn->abort();
	activeTxn = NULL;
	pdb = NULL;

	// Flush database activity from memory pool to disk log
	unsigned int nMinutes = 0;
	if (fReadOnly)
		nMinutes = 1;

	bitdb.dbenv.txn_checkpoint(nMinutes ? 100 * 1024 : 0, nMinutes, 0);

	{
		LOCK(bitdb.cs_db);
		--bitdb.mapFileUseCount[strFile];
	}
}

void CDBEnv::CloseDb(const string& strFile)
{
	{
		LOCK(cs_db);
		if (mapDb[strFile] != NULL)
		{
			// Close the database handle
			Db* pdb = mapDb[strFile];
			pdb->close(0);
			delete pdb;
			mapDb[strFile] = NULL;
		}
	}
}

bool CDBEnv::RemoveDb(const string& strFile)
{
	this->CloseDb(strFile);

	LOCK(cs_db);
	int rc = dbenv.dbremove(NULL, strFile.c_str(), NULL, DB_AUTO_COMMIT);
	return (rc == 0);
}

bool CDB::Rewrite(const string& strFile, const char* pszSkip)
{
	while (true)
	{
		{
			LOCK(bitdb.cs_db);
			if (!bitdb.mapFileUseCount.count(strFile) || bitdb.mapFileUseCount[strFile] == 0)
			{
				// Flush log data to the dat file
				bitdb.CloseDb(strFile);
				bitdb.CheckpointLSN(strFile);
				bitdb.mapFileUseCount.erase(strFile);

				bool fSuccess = true;
				dlog("Rewriting ${p}...\n", ("p",strFile));
				string strFileRes = strFile + ".rewrite";
				{ // surround usage of db with extra {}
					CDB db(strFile.c_str(), "r");
					Db* pdbCopy = new Db(&bitdb.dbenv, 0);

					int ret = pdbCopy->open(NULL,                 // Txn pointer
						strFileRes.c_str(),   // Filename
						"main",    // Logical db name
						DB_BTREE,  // Database type
						DB_CREATE,    // Flags
						0);
					if (ret > 0)
					{
						dlog("Cannot create database file ${p} \n", ("p", strFileRes));
						fSuccess = false;
					}

					Dbc* pcursor = db.GetCursor();
					if (pcursor)
						while (fSuccess)
						{
							CDataStream ssKey(SER_DISK, CLIENT_VERSION);
							CDataStream ssValue(SER_DISK, CLIENT_VERSION);
							int ret = db.ReadAtCursor(pcursor, ssKey, ssValue, DB_NEXT);
							if (ret == DB_NOTFOUND)
							{
								pcursor->close();
								break;
							}
							else if (ret != 0)
							{
								pcursor->close();
								fSuccess = false;
								break;
							}
							if (pszSkip &&
								strncmp(&ssKey[0], pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0)
								continue;
							if (strncmp(&ssKey[0], "\x07version", 8) == 0)
							{
								// Update version:
								ssValue.clear();
								ssValue << CLIENT_VERSION;
							}
							Dbt datKey(&ssKey[0], ssKey.size());
							Dbt datValue(&ssValue[0], ssValue.size());
							int ret2 = pdbCopy->put(NULL, &datKey, &datValue, DB_NOOVERWRITE);
							if (ret2 > 0)
								fSuccess = false;
						}
					if (fSuccess)
					{
						db.Close();
						bitdb.CloseDb(strFile);
						if (pdbCopy->close(0))
							fSuccess = false;
						delete pdbCopy;
					}
				}
				if (fSuccess)
				{
					Db dbA(&bitdb.dbenv, 0);
					if (dbA.remove(strFile.c_str(), NULL, 0))
						fSuccess = false;
					Db dbB(&bitdb.dbenv, 0);
					if (dbB.rename(strFileRes.c_str(), NULL, strFile.c_str(), 0))
						fSuccess = false;
				}
				if (!fSuccess)
					dlog("Rewriting of ${p} FAILED!\n", ("p",strFileRes));
				return fSuccess;
			}
		}
		MilliSleep(100);
	}
	return false;
}

void CDBEnv::Flush(bool fShutdown)
{
	int64_t nStart = GetTimeMillis();
	// Flush log data to the actual data file
	//  on all files that are not in use
	//dlog( "Flush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started");
	if (!fDbEnvInit)
		return;
	{
		LOCK(cs_db);
		map<string, int>::iterator mi = mapFileUseCount.begin();
		while (mi != mapFileUseCount.end())
		{
			string strFile = (*mi).first;
			int nRefCount = (*mi).second;
			dlog( "${p} refcount=${q}\n",(("p", strFile),("q", nRefCount)));
			if (nRefCount == 0)
			{
				// Move log data to the dat file
				CloseDb(strFile);
				dlog( "${p} checkpoint\n", ("p", strFile));
				dbenv.txn_checkpoint(0, 0, 0);
				dlog( "${p} detach\n", ("p", strFile));
				if (!fMockDb)
					dbenv.lsn_reset(strFile.c_str(), 0);
				dlog( "${p} closed\n", ("p", strFile));
				mapFileUseCount.erase(mi++);
			}
			else
				mi++;
		}
		if (fShutdown)
		{
			char** listp;
			if (mapFileUseCount.empty())
			{
				dbenv.log_archive(&listp, DB_ARCH_REMOVE);
				Close();
			}
		}
	}
}


text:
/*
 * SolutionHistory.cpp
 *
 *  Created on: 2014/07/24
 *      Author: kryozahiro
 */

#include "SolutionHistory.h"

#include <algorithm>
#include <boost/log/sources/record_ostream.hpp>
using namespace std;

SolutionHistory::SolutionHistory(int historySize) : historySize(historySize) {
}

void SolutionHistory::start() {
	timer.start();
	summaryLogger.add_attribute("Summary", generationAttr);
	solutionLogger.add_attribute("Solution", generationAttr);
	programLogger.add_attribute("Program", generationAttr);
}

double SolutionHistory::getElapsedTime() const {
	return static_cast<double>(timer.elapsed()) / 1000.0;
}

void SolutionHistory::addSolution(shared_ptr<Solution>& solution) {
	int generation = solution->getGeneration();
	generationAttr.set(generation);

	solution->setTime(timer.elapsed());
	BOOST_LOG(solutionLogger) << solution->toString();
	BOOST_LOG(programLogger) << *solution->getProgram();
	records[solution->getGeneration()].push_back(solution);

	sort(records[generation].begin(), records[generation].end(), [](shared_ptr<Solution>& lhs, shared_ptr<Solution>& rhs) {
		return lhs->getFitness() < rhs->getFitness();
	});
	while (records.size() > (unsigned int)historySize) {
		records.erase(records.begin());
	}
}

void SolutionHistory::addGeneration(Generation& generation) {
	int generationNum = generation.front()->getGeneration();
	generationAttr.set(generationNum);

	for (shared_ptr<Solution>& solution : generation) {
		solution->setTime(timer.elapsed());
		BOOST_LOG(solutionLogger) << solution->toString();
		BOOST_LOG(programLogger) << *solution->getProgram();
	}
	records[generationNum].insert(records[generationNum].end(), generation.begin(), generation.end());

	sort(records[generationNum].begin(), records[generationNum].end(), [](shared_ptr<Solution>& lhs, shared_ptr<Solution>& rhs) {
		return lhs->getFitness() < rhs->getFitness();
	});
	while (records.size() > (unsigned int)historySize) {
		records.erase(records.begin());
	}
	BOOST_LOG(summaryLogger) << showSummary(records[generationNum]);
}

int SolutionHistory::getLastGenerationNum() const {
	if (records.empty()) {
		return -1;
	}
	return records.rbegin()->second.front()->getGeneration();
}

const SolutionHistory::Generation& SolutionHistory::getGeneration(int generation) const {
	int generationSize = getLastGenerationNum() + 1;
	assert(getLastGenerationNum() >= 0 and generation >= -generationSize);
	return records.at((generation + generationSize) % generationSize);
}

string SolutionHistory::toString() const {
	string ret;
	for (auto itr = records.begin(); itr != records.end(); ++itr) {
		for (const shared_ptr<Solution>& record : itr->second) {
			ret += record->toString() + "\n";
		}
	}
	return ret;
}

string SolutionHistory::showRelation() const {
	string ret;
	for (auto itr = records.begin(); itr != records.end(); ++itr) {
		for (const shared_ptr<Solution>& record : itr->second) {
			ret += record->showRelation();
		}
	}
	return ret;
}

double SolutionHistory::getMeanFitness(const Generation& generation) {
	double sum = accumulate(generation.begin(), generation.end(), 0.0, [](double s, const shared_ptr<Solution>& solution) {
		return s + solution->getFitness();
	});
	return sum / static_cast<double>(generation.size());
}

std::string SolutionHistory::showSummary(const Generation& generation) {
	//世代数
	int generationNum = generation.front()->getGeneration();

	//最小
	double best = generation.front()->getFitness();

	//最大
	double worst = generation.back()->getFitness();

	//平均
	double average = getMeanFitness(generation);

	//不偏分散
	double var = accumulate(generation.begin(), generation.end(), 0.0, [&](double sum, shared_ptr<Solution> solution) {
		return sum + pow(solution->getFitness() - average, 2.0);
	}) / static_cast<double>(generation.size() - 1);

	//標準偏差
	double sd = sqrt(var);

	return to_string(generationNum) + "\t" +
			to_string(best) + "\t" +
			to_string(worst) + "\t" +
			to_string(average) + "\t" +
			to_string(sd);
}

max_stars_repo_name: kryozahiro/gamesolver

max_issues_repo_name: kryozahiro/gamesolver

max_forks_repo_name: kryozahiro/gamesolver


text:
//  (C) Copyright Eric Niebler 2004.
//  Use, modification and distribution are subject to the
//  Boost Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

/*
 Revision history:
   25 August 2005 : Initial version.
*/

#include <boost/core/lightweight_test.hpp>
#include <boost/foreach.hpp>

///////////////////////////////////////////////////////////////////////////////
// define the container types, used by utility.hpp to generate the helper functions
typedef int foreach_container_type[5];
typedef int const foreach_const_container_type[5];
typedef int foreach_value_type;
typedef int &foreach_reference_type;
typedef int const &foreach_const_reference_type;

#include "./utility.hpp"

///////////////////////////////////////////////////////////////////////////////
// define some containers
//
int my_array[5] = { 1,2,3,4,5 };
int const (&my_const_array)[5] = my_array;

///////////////////////////////////////////////////////////////////////////////
// test_main
//
int main()
{
    boost::mpl::false_ *p = BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(my_array);
    (void)p;
    // non-const containers by value
    BOOST_TEST(sequence_equal_byval_n(my_array, "\1\2\3\4\5"));

    // const containers by value
    BOOST_TEST(sequence_equal_byval_c(my_const_array, "\1\2\3\4\5"));

    return boost::report_errors();
}


text:
// http://turtle.sourceforge.net
//
// Copyright Mathieu Champlon 2009
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef MOCK_TYPE_NAME_HPP_INCLUDED
#define MOCK_TYPE_NAME_HPP_INCLUDED

#include "../config.hpp"
#include <boost/test/utils/basic_cstring/io.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/erase.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/version.hpp>
#if BOOST_VERSION >= 107000
#   include <boost/core/typeinfo.hpp>
#   define MOCK_TYPEID( t ) BOOST_CORE_TYPEID(t)
#   define MOCK_TYPEINFO boost::core::typeinfo
#else
#   include <boost/detail/sp_typeinfo.hpp>
#   define MOCK_TYPEID( t ) BOOST_SP_TYPEID(t)
#   define MOCK_TYPEINFO boost::detail::sp_typeinfo
#endif
#include <boost/shared_ptr.hpp>
#include <stdexcept>
#include <typeinfo>
#include <ostream>
#ifdef __GNUC__
#include <cxxabi.h>
#include <cstdlib>
#endif

#define MOCK_TYPE_NAME( t ) mock::detail::type_name( MOCK_TYPEID(t) )

namespace mock
{
namespace detail
{
    class type_name
    {
    public:
        explicit type_name( const MOCK_TYPEINFO& info )
            : info_( &info )
        {}
        friend std::ostream& operator<<( std::ostream& s, const type_name& t )
        {
            t.serialize( s, *t.info_ );
            return s;
        }
    private:
        void serialize( std::ostream& s,
            const MOCK_TYPEINFO& info ) const
        {
            const char* name = info.name();
#ifdef __GNUC__
            int status = 0;
            boost::shared_ptr< char > demangled(
                abi::__cxa_demangle( name, 0, 0, &status ),
                &std::free );
            if( ! status && demangled )
                serialize( s, demangled.get() );
            else
#endif
                serialize( s, name );
        }

        typedef std::string::size_type size_type;

        void serialize( std::ostream& s, std::string name ) const
        {
            const size_type nm = rfind( name, ':' ) + 1;
            const size_type tpl = name.find( '<', nm );
            s << clean( name.substr( nm, tpl - nm ) );
            if( tpl == std::string::npos )
                return;
            s << '<';
            list( s, name.substr( tpl + 1, name.rfind( '>' ) - tpl - 1 ) );
            s << '>';
        }
        void list( std::ostream& s, const std::string& name ) const
        {
            const size_type comma = rfind( name, ',' );
            if( comma != std::string::npos )
            {
                list( s, name.substr( 0, comma ) );
                s << ", ";
            }
            serialize( s, name.substr( comma + 1 ) );
        }
        std::string clean( std::string name ) const
        {
            boost::algorithm::trim( name );
            boost::algorithm::erase_all( name, "class " );
            boost::algorithm::erase_all( name, "struct " );
            boost::algorithm::erase_all( name, "__ptr64" );
            boost::algorithm::replace_all( name, " &", "&" );
            boost::algorithm::replace_all( name, "& ", "&" );
            boost::algorithm::replace_all( name, " *", "*" );
            boost::algorithm::replace_all( name, "* ", "*" );
            return name;
        }
        size_type rfind( const std::string& name, char c ) const
        {
            size_type count = 0;
            for( size_type i = name.size() - 1; i > 0; --i )
            {
                if( name[ i ] == '>' )
                    ++count;
                else if( name[ i ] == '<' )
                    --count;
                if( name[ i ] == c && count == 0 )
                    return i;
            }
            return std::string::npos;
        }

        const MOCK_TYPEINFO* info_;
    };
}
} // mock

#endif // MOCK_TYPE_NAME_HPP_INCLUDED

ext: hpp

max_stars_repo_name: jonesmz/turtle

max_issues_repo_name: jonesmz/turtle

max_forks_repo_name: jonesmz/turtle


text:
/* Copyright (C) 2010-2019, The Regents of The University of Michigan.
 All rights reserved.

 This software was developed as part of the The Vulcan project in the Intelligent Robotics Lab
 under the direction of Benjamin Kuipers, kuipers@umich.edu. Use of this code is governed by an
 MIT-style License that can be found at "https://github.com/h2ssh/Vulcan".
*/

/**
 * \file     isovist_gradients.cpp
 * \author   Collin Johnson
 *
 * Definition of VoronoiIsovistGradients.
 */

#include "hssh/local_topological/area_detection/gateways/isovist_gradients.h"
#include "hssh/local_topological/area_detection/local_topo_isovist_field.h"
#include "math/savitzky_golay.h"
#include <boost/range/algorithm_ext.hpp>
#include <boost/range/as_array.hpp>
#include <cmath>
#include <numeric>

namespace vulcan
{
namespace hssh
{

VoronoiIsovistGradients::VoronoiIsovistGradients(const VoronoiEdges& edges) : edges_(edges)
{
    for (const auto& e : edges_) {
        boost::push_back(isovistCells_, boost::as_array(e));
    }

    gradients_.resize(isovistCells_.size());
    initializeCellToIndex();
}

void VoronoiIsovistGradients::calculateGradients(utils::Isovist::Scalar scalar, const VoronoiIsovistField& field)
{
    for (const auto& edgeCells : edges_) {
        calculateGradientsForEdge(edgeCells, scalar, field);
    }
}

void VoronoiIsovistGradients::initializeCellToIndex(void)
{
    cellToIndex_.reserve(isovistCells_.size());
    int index = 0;
    for (auto& cell : isovistCells_) {
        cellToIndex_[cell] = index++;
    }
}

void VoronoiIsovistGradients::calculateGradientsForEdge(const CellVector& cells,
                                                        utils::Isovist::Scalar scalar,
                                                        const VoronoiIsovistField& field)
{
    edgeScalars_.clear();

    std::transform(cells.begin(), cells.end(), std::back_inserter(edgeScalars_), [&](cell_t cell) {
        return field.at(cell).scalar(scalar);
    });

    edgeDerivs_.resize(edgeScalars_.size());

    if (edgeDerivs_.size() >= 7) {
        math::savitzky_golay_first_deriv(edgeScalars_.begin(),
                                         edgeScalars_.end(),
                                         edgeDerivs_.begin(),
                                         math::SGWindowRadius::three);
    } else if (edgeDerivs_.size() >= 5) {
        math::savitzky_golay_first_deriv(edgeScalars_.begin(),
                                         edgeScalars_.end(),
                                         edgeDerivs_.begin(),
                                         math::SGWindowRadius::two);
    } else {
        std::adjacent_difference(edgeScalars_.begin(), edgeScalars_.end(), edgeDerivs_.begin());
        edgeDerivs_[0] = 0.0;
    }

    for (std::size_t n = 0; n < cells.size(); ++n) {
        int cellIndex = cellToIndex_[cells[n]];

        // Take the absolute value of the derivative to account for it being a function of the direction
        // of iteration along the edge. If we iterated the other direction, then it would have the opposite
        // sign -- thus causing some otherwise obvious gateways to "hide"
        gradients_[cellIndex] = position_value_t(cells[n], std::abs(edgeDerivs_[n]), edgeScalars_[n]);

        if ((scalar == utils::Isovist::kWeightedOrientation) || (scalar == utils::Isovist::kOrientation)) {
            gradients_[cellIndex].value = std::abs(wrap_to_pi_2(edgeDerivs_[n]));
        }
    }
}

}   // namespace hssh
}   // namespace vulcan

max_stars_repo_name: anuranbaka/Vulcan

max_issues_repo_name: anuranbaka/Vulcan

max_forks_repo_name: anuranbaka/Vulcan


text:
#include <Eigen/Core>
#include <Eigen/Sparse>
#include <algorithm>
#include <atomic>
#include <cstddef>
#include <future>
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>

#include <pybind11/eigen.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "argcheck.hpp"

namespace irspack {

using CountVector = Eigen::Matrix<std::int64_t, Eigen::Dynamic, 1>;
struct Metrics {
  // This isn't necessary, but MSVC complains Metric is not default
  // constructible.
  inline Metrics() : Metrics(0) {}
  inline Metrics(size_t n_item) : n_item(n_item), item_cnt(n_item) {
    item_cnt.array() = 0;
  }

  inline void merge(const Metrics &other) {
    hit += other.hit;
    recall += other.recall;
    ndcg += other.ndcg;
    total_user += other.total_user;
    valid_user += other.valid_user;
    item_cnt += other.item_cnt;
    precision += other.precision;
    map += other.map;
  }

  std::map<std::string, double> as_dict() const {
    std::map<std::string, double> result;
    CountVector count_local(item_cnt);
    double total_item = item_cnt.sum();
    std::sort(count_local.data(), count_local.data() + item_cnt.rows());
    double apperere_item = 0;
    double entropy = 0;
    double gini = 0;

    for (int i = 0; i < item_cnt.rows(); i++) {
      int64_t cnt = count_local(i);
      if (cnt == 0) {
        continue;
      }
      double p = cnt / total_item;
      apperere_item++;
      entropy += -std::log(p) * p;
      gini += (2 * i - item_cnt.rows() + 1) * cnt;
    }
    gini /= (item_cnt.rows() * total_item);

    size_t denominator = valid_user > 0u ? valid_user : 1;
    result["total_user"] = total_user;
    result["valid_user"] = valid_user;
    result["n_items"] = n_item;
    result["hit"] = hit / denominator;
    result["ndcg"] = ndcg / denominator;
    result["recall"] = recall / denominator;
    result["map"] = map / denominator;
    result["precision"] = precision / denominator;
    result["appeared_item"] = apperere_item;
    result["entropy"] = entropy;
    result["gini_index"] = gini;
    return result;
  }

  size_t valid_user = 0;
  size_t total_user = 0;
  double hit = 0;
  double recall = 0;
  double ndcg = 0;
  double precision = 0;
  double map = 0;
  size_t n_item;
  CountVector item_cnt;
};

struct EvaluatorCore {
  using Real = double;
  using SparseMatrix = Eigen::SparseMatrix<Real, Eigen::RowMajor>;

  template <typename ScoreFloatType>
  using DenseMatrix = Eigen::Matrix<ScoreFloatType, Eigen::Dynamic,
                                    Eigen::Dynamic, Eigen::RowMajor>;

  EvaluatorCore(const SparseMatrix &X,
                const std::vector<std::vector<size_t>> &recommendable_items)
      : X_(X), n_users(X.rows()), n_items(X.cols()),
        recommendable_items(recommendable_items) {
    check_arg(recommendable_items.empty() ||
                  (recommendable_items.size() == 1) ||
                  static_cast<size_t>(X.rows()) == recommendable_items.size(),
              "recommendable.size.() must be in {0, 1, ground_truth.size()}");
    X_.makeCompressed();
    // sort & bound check
    for (auto &urec : this->recommendable_items) {
      std::sort(urec.begin(), urec.end());
      if (!urec.empty()) {
        check_arg(urec.back() < static_cast<size_t>(X_.cols()),
                  "recommendable items contain a index >= n_items.");
        auto prev = urec[0];
        for (size_t i = 1; i < urec.size(); i++) {
          auto current = urec[i];
          check_arg(current > prev, "duplicate recommendable items.");
          prev = current;
        }
      }
    }
  }

  inline void cache_X_map(size_t n_threads) {
    check_arg(n_threads > 0, "n_threads must be strictly positive.");
    if (!this->X_as_set.empty()) {
      return;
    }

    const int64_t n_rows = this->X_.rows();
    this->X_as_set.resize(this->X_.rows());
    std::atomic<std::int64_t> cursor(0);
    std::vector<std::future<void>> workers;
    for (size_t th = 0; th < n_threads; th++) {
      workers.emplace_back(std::async([this, &cursor, n_rows]() {
        while (true) {
          int64_t current = cursor.fetch_add(1);
          if (current >= n_rows) {
            break;
          }
          auto &target = this->X_as_set[current];
          for (SparseMatrix::InnerIterator iter(this->X_, current); iter;
               ++iter) {
            target.insert(iter.col());
          }
        }
      }));
    }
  }

  template <typename ScoreFloatType>
  inline Metrics
  get_metrics(const Eigen::Ref<DenseMatrix<ScoreFloatType>> &scores,
              size_t cutoff, size_t offset, size_t n_threads,
              bool recall_with_cutoff = false) {
    this->cache_X_map(n_threads);
    Metrics overall(n_items);
    check_arg(n_threads > 0, "n_threads == 0");
    check_arg(n_users > offset, "got offset >= n_users");
    check_arg(static_cast<size_t>(offset + scores.rows()) <= n_users,
              "offset + scores.shape[0] exceeds n_users");
    check_arg(cutoff > 0, "cutoff must be strictly greather than 0.");
    check_arg(cutoff <= n_items, "cutoff must not exeeed the number of items.");
    std::atomic<int64_t> current_index(0);

    std::vector<std::future<Metrics>> workers;
    for (size_t th = 0; th < n_threads; th++) {
      workers.emplace_back(std::async(
          std::launch::async, [this, &current_index, &scores, cutoff,
                               offset, recall_with_cutoff]() {
            return this->get_metrics_local(scores, current_index, cutoff,
                                           offset, recall_with_cutoff);
          }));
    }
    for (auto &metric : workers) {
      overall.merge(metric.get());
    }
    return overall;
  }

  inline SparseMatrix get_ground_truth() const { return this->X_; }
  inline std::vector<std::vector<size_t>> get_recommendable_items() const {
    return this->recommendable_items;
  }

private:
  template <typename ScoreFloatType>
  inline Metrics
  get_metrics_local(const Eigen::Ref<DenseMatrix<ScoreFloatType>> &scores,
                    std::atomic<int64_t> &current_index, size_t cutoff,
                    size_t offset, bool recall_with_cutoff = false) const {
    using StorageIndex = typename SparseMatrix::StorageIndex;

    Metrics metrics_local(n_items);

    const ScoreFloatType *score_begin = scores.data();
    std::vector<std::pair<ScoreFloatType, StorageIndex>> score_and_index;
    score_and_index.reserve(n_items);
    std::vector<StorageIndex> intersection(cutoff);
    std::vector<double> dcg_discount(cutoff);
    for (size_t i = 0; i < cutoff; i++) {
      dcg_discount[i] = 1 / std::log2(2 + i);
    }

    size_t n_recommendable_items = std::min(cutoff, n_items);
    while (true) {
      auto u = current_index.fetch_add(1);
      if (u >= scores.rows()) {
        break;
      }
      auto buffer = score_begin + n_items * u;
      int u_orig = u + offset;

      const auto &gt_indices = this->X_as_set.at(u_orig);
      metrics_local.total_user += 1;
      score_and_index.clear();

      size_t n_gt = 0;
      const auto n_items_signed = static_cast<StorageIndex>(n_items);
      if (this->recommendable_items.empty()) {
        auto score_loc = buffer;
        for (StorageIndex _ = 0; _ < n_items_signed; _++) {
          score_and_index.emplace_back(-*(score_loc++), _);
        }
        n_gt = gt_indices.size();
      } else if (this->recommendable_items.size() == 1u) {
        const auto &rec_items_global = recommendable_items[0];
        for (auto i : rec_items_global) {
          score_and_index.emplace_back(-buffer[i], i);
          if (gt_indices.find(i) != gt_indices.cend()) {
            n_gt++;
          }
        }
        n_recommendable_items = std::min(cutoff, recommendable_items[0].size());
      } else {
        const auto &item_local = this->recommendable_items[u_orig];
        for (auto i : item_local) {
          score_and_index.emplace_back(-buffer[i], i);
          if (gt_indices.find(i) != gt_indices.cend()) {
            n_gt++;
          }
        }
        n_recommendable_items = std::min(cutoff, item_local.size());
      }

      if ((n_gt == 0) || (n_recommendable_items == 0)) {
        continue;
      }

      std::partial_sort(score_and_index.begin(),
                        score_and_index.begin() + n_recommendable_items,
                        score_and_index.end());

      metrics_local.valid_user += 1;
      double dcg = 0;
      double idcg = std::accumulate(
          dcg_discount.begin(),
          dcg_discount.begin() + std::min(n_gt, n_recommendable_items), 0.);
      double average_precision = 0;

      size_t cum_hit = 0;
      for (size_t i = 0; i < n_recommendable_items; i++) {
        auto rec_index = score_and_index[i].second;
        metrics_local.item_cnt(rec_index) += 1;
        if (gt_indices.find(rec_index) == gt_indices.cend()) {
        } else {
          dcg += dcg_discount[i];
          cum_hit++;
          average_precision += (static_cast<Real>(cum_hit) / (i + 1));
        }
      }
      if (cum_hit > 0) {
        metrics_local.hit += 1;
      }
      metrics_local.precision +=
          cum_hit / static_cast<double>(n_recommendable_items);
      metrics_local.recall +=
          cum_hit / static_cast<double>(recall_with_cutoff
                                            ? (n_gt > cutoff ? cutoff : n_gt)
                                            : n_gt);
      metrics_local.ndcg += (dcg / idcg);
      metrics_local.map += average_precision / n_gt;
    }
    return metrics_local;
  }

  SparseMatrix X_;
  const size_t n_users;
  const size_t n_items;
  std::vector<std::vector<size_t>> recommendable_items;
  std::vector<std::unordered_set<int64_t>> X_as_set;
}; // namespace irspack
} // namespace irspack

namespace py = pybind11;
using namespace irspack;

PYBIND11_MODULE(_core, m) {
  py::class_<Metrics>(m, "Metrics")
      .def(py::init<size_t>())
      .def("merge",
           [](Metrics &this_, const Metrics &other) { this_.merge(other); })
      .def("as_dict", &Metrics::as_dict);

  py::class_<EvaluatorCore>(m, "EvaluatorCore")
      .def(py::init<const typename EvaluatorCore::SparseMatrix &,
                    const std::vector<std::vector<size_t>> &>(),
           py::arg("grount_truth"), py::arg("recommendable"))
      .def("get_metrics_f64",
           static_cast<Metrics (EvaluatorCore::*)(
               const Eigen::Ref<EvaluatorCore::DenseMatrix<double>> &, size_t,
               size_t, size_t, bool)>(&EvaluatorCore::get_metrics<double>),
           py::arg("score_array"), py::arg("cutoff"), py::arg("offset"),
           py::arg("n_threads"), py::arg("recall_with_cutoff") = false)
      .def("get_metrics_f32",
           static_cast<Metrics (EvaluatorCore::*)(
               const Eigen::Ref<EvaluatorCore::DenseMatrix<float>> &, size_t,
               size_t, size_t, bool)>(&EvaluatorCore::get_metrics<float>),
           py::arg("score_array"), py::arg("cutoff"), py::arg("offset"),
           py::arg("n_threads"), py::arg("recall_with_cutoff") = false)
      .def("get_ground_truth", &EvaluatorCore::get_ground_truth)
      .def("cache_X_as_set", &EvaluatorCore::cache_X_map)
      .def(py::pickle(
          [](const EvaluatorCore &evaluator) {
            return py::make_tuple(evaluator.get_ground_truth(),
                                  evaluator.get_recommendable_items());
          },
          [](py::tuple t) {
            if (t.size() != 2)
              throw std::runtime_error("invalid state");
            return EvaluatorCore(t[0].cast<EvaluatorCore::SparseMatrix>(),
                                 t[1].cast<std::vector<std::vector<size_t>>>());
          }));

  ;
}

max_stars_repo_name: tohtsky/irspack

max_issues_repo_name: tohtsky/irspack

max_forks_repo_name: tohtsky/irspack


text:
#include <Eigen/Dense>
#include <iostream>

using namespace Eigen;
using namespace std;

int main(int, char**)
{
  cout.precision(3);
  MatrixXd ones = MatrixXd::Ones(3,3);
cout << "The operator norm of the 3x3 matrix of ones is "
     << ones.selfadjointView<Lower>().operatorNorm() << endl;

  return 0;
}

max_issues_repo_name: TANHAIYU/planecalib

max_forks_repo_name: TANHAIYU/planecalib


text:
// Copyright 2015 Maurice Fallon

// Synchronized Stereo Translator
//
// TODO(tbd):
// rgb bgr conversion
// grey compression
// disparity/depth conversion from device

// rosparam set /compressed_listener/image_transport compressed;
// rosparam set /ros2lcm/stereo hand; rosrun translators  ros2lcm_stereo  __name:=ros2lcm_hand
//
// rosparam set /ros2lcm_head/image_transport compressed;
// rosparam set /ros2lcm/stereo head;  rosrun translators ros2lcm_stereo  __name:=ros2lcm_head
//
// rosparam set /compressed_listener/image_transport compressed
// rosrun translators  my_subscriber __name:=compressed_listener

#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>

#include <ros/ros.h>
#include <ros/console.h>
#include <cstdlib>
#include <sys/time.h>
#include <time.h>
#include <iostream>
#include <string>
#include <vector>

#include <message_filters/subscriber.h>
#include <message_filters/time_synchronizer.h>
#include <message_filters/synchronizer.h>
#include <message_filters/sync_policies/approximate_time.h>
#include <image_transport/image_transport.h>
#include <image_transport/subscriber_filter.h>
#include <image_geometry/stereo_camera_model.h>
#include <sensor_msgs/image_encodings.h>
#include <cv_bridge/cv_bridge.h>

#include <sensor_msgs/Image.h>
#include <sensor_msgs/CameraInfo.h>
#include <sensor_msgs/Imu.h>
#include <std_msgs/Float64.h>

#include <lcm/lcm-cpp.hpp>
#include <lcmtypes/bot_core.hpp>
#include <opencv2/opencv.hpp>

class App
{
public:
  explicit App(ros::NodeHandle node_in);
  ~App();

private:
  lcm::LCM lcm_publish_;
  ros::NodeHandle node_;

  // Combined Stereo Image:
  bot_core::images_t images_msg_out_;
  bot_core::image_t image_a_lcm_;
  bot_core::image_t image_b_lcm_;
  void publishStereo(const sensor_msgs::ImageConstPtr& image_a_ros, const sensor_msgs::CameraInfoConstPtr& info_a_ros,
                     const sensor_msgs::ImageConstPtr& image_ros_b, const sensor_msgs::CameraInfoConstPtr& info_b_ros,
                     std::string camera_out);
  image_transport::ImageTransport it_;

  ///////////////////////////////////////////////////////////////////////////////
  void head_stereo_cb(const sensor_msgs::ImageConstPtr& image_a_ros, const sensor_msgs::CameraInfoConstPtr& info_cam_a,
                      const sensor_msgs::ImageConstPtr& image_ros_b, const sensor_msgs::CameraInfoConstPtr& info_cam_b);
  image_transport::SubscriberFilter image_a_ros_sub_, image_b_ros_sub_;
  message_filters::Subscriber<sensor_msgs::CameraInfo> info_a_ros_sub_, info_b_ros_sub_;
  message_filters::TimeSynchronizer<sensor_msgs::Image, sensor_msgs::CameraInfo, sensor_msgs::Image,
      sensor_msgs::CameraInfo> sync_;

  bool do_jpeg_compress_;
  int jpeg_quality_;
  bool do_zlib_compress_;
  int depth_compress_buf_size_;
  uint8_t* depth_compress_buf_;

  void prepImage(bot_core::image_t& lcm_image, const sensor_msgs::ImageConstPtr& ros_image);
};

App::App(ros::NodeHandle node_in) :
    node_(node_in), it_(node_in), sync_(10)
{
  if (!lcm_publish_.good())
  {
    std::cerr << "ERROR: lcm is not good()" << std::endl;
  }

  std::string image_a_string, info_a_string, image_b_string, info_b_string;
  std::string head_stereo_root = "";

  image_a_string = head_stereo_root + "/left/image_rect_color";
  info_a_string = image_a_string + "/camera_info";
  // rim_string = head_stereo_root + "/right/image_rect_color";
  image_b_string = head_stereo_root + "/right/image_rect";
  info_b_string = image_b_string + "/camera_info";
  std::cout << image_a_string << " is the image_a topic subscription [for stereo]\n";
  image_a_ros_sub_.subscribe(it_, ros::names::resolve(image_a_string), 30);
  info_a_ros_sub_.subscribe(node_, ros::names::resolve(info_a_string), 30);
  image_b_ros_sub_.subscribe(it_, ros::names::resolve(image_b_string), 30);
  info_b_ros_sub_.subscribe(node_, ros::names::resolve(info_b_string), 30);
  sync_.connectInput(image_a_ros_sub_, info_a_ros_sub_, image_b_ros_sub_, info_b_ros_sub_);
  sync_.registerCallback(boost::bind(&App::head_stereo_cb, this, _1, _2, _3, _4));

  images_msg_out_.images.push_back(image_a_lcm_);
  images_msg_out_.images.push_back(image_b_lcm_);
  images_msg_out_.image_types.push_back(0);
  images_msg_out_.image_types.push_back(2);
  // bot_core::images_t::LEFT
  // 0 left, 1 right, 2 DISPARITY, 3 maskzipped, 4 depth mm, 5 DISPARITY_ZIPPED, 6 DEPTH_MM_ZIPPED

  // allocate space for zlib compressing depth data

  depth_compress_buf_size_ = 800 * 800 * sizeof(int8_t) * 10;
  depth_compress_buf_ = (uint8_t*)malloc(depth_compress_buf_size_);
  do_jpeg_compress_ = true;
  jpeg_quality_ = 95;  // 95 is opencv default
  do_zlib_compress_ = true;
}

App::~App()
{
}

int stereo_counter = 0;
void App::head_stereo_cb(const sensor_msgs::ImageConstPtr& image_a_ros,
                         const sensor_msgs::CameraInfoConstPtr& info_a_ros,
                         const sensor_msgs::ImageConstPtr& image_b_ros,
                         const sensor_msgs::CameraInfoConstPtr& info_b_ros)
{
  int64_t current_utime = (int64_t)floor(image_a_ros->header.stamp.toNSec() / 1000);
  publishStereo(image_a_ros, info_a_ros, image_b_ros, info_b_ros, "CAMERA");

  if (stereo_counter % 30 == 0)
  {
    ROS_ERROR("HDCAM [%d]", stereo_counter);
  }
  stereo_counter++;
}

void App::publishStereo(const sensor_msgs::ImageConstPtr& image_a_ros,
                        const sensor_msgs::CameraInfoConstPtr& info_a_ros,
                        const sensor_msgs::ImageConstPtr& image_ros_b,
                        const sensor_msgs::CameraInfoConstPtr& info_b_ros, std::string camera_out)
{
  prepImage(image_a_lcm_, image_a_ros);
  prepImage(image_b_lcm_, image_ros_b);

  images_msg_out_.images[0] = image_a_lcm_;
  images_msg_out_.images[1] = image_b_lcm_;
  images_msg_out_.image_types[0] = 0;
  images_msg_out_.image_types[1] = 1;

  images_msg_out_.n_images = images_msg_out_.images.size();
  images_msg_out_.utime = (int64_t)floor(image_a_ros->header.stamp.toNSec() / 1000);
  lcm_publish_.publish("CAMERA", &images_msg_out_);
  return;
}

void App::prepImage(bot_core::image_t& lcm_image, const sensor_msgs::ImageConstPtr& ros_image)
{
  int64_t current_utime = (int64_t)floor(ros_image->header.stamp.toNSec() / 1000);
  lcm_image.utime = current_utime;
  int isize = ros_image->width * ros_image->height;
  int n_colors;

  if ((ros_image->encoding.compare("mono8") == 0)
      || ((ros_image->encoding.compare("rgb8") == 0) || (ros_image->encoding.compare("bgr8") == 0)))
  {
    if (ros_image->encoding.compare("mono8") == 0)
    {
      n_colors = 1;
    }
    else if (ros_image->encoding.compare("rgb8") == 0)
    {
      n_colors = 3;
    }
    else if (ros_image->encoding.compare("bgr8") == 0)
    {
      n_colors = 3;
    }
    else
    {
      std::cout << "Encoding [" << ros_image->encoding << "] not supported\n";
      exit(-1);
    }

    void* bytes = const_cast<void*>(static_cast<const void*>(ros_image->data.data()));
    cv::Mat mat;
    if (n_colors == 1)
    {
      mat = cv::Mat(ros_image->height, ros_image->width, CV_8UC1, bytes, n_colors * ros_image->width);
    }
    else if (n_colors == 3)
    {
      mat = cv::Mat(ros_image->height, ros_image->width, CV_8UC3, bytes, n_colors * ros_image->width);
    }
    else
    {
      std::cout << "Number of colors [" << n_colors << "] not supported\n";
      exit(-1);
    }

    if (do_jpeg_compress_)
    {
      if (ros_image->encoding.compare("rgb8") == 0)  // non intuative color flip needed here
        cv::cvtColor(mat, mat, CV_BGR2RGB);

      std::vector<int> params;
      params.push_back(cv::IMWRITE_JPEG_QUALITY);
      params.push_back(jpeg_quality_);
      cv::imencode(".jpg", mat, lcm_image.data, params);
      lcm_image.size = lcm_image.data.size();
      lcm_image.pixelformat = bot_core::image_t::PIXEL_FORMAT_MJPEG;
    }
    else
    {
      if (ros_image->encoding.compare("bgr8") == 0)
        cv::cvtColor(mat, mat, CV_BGR2RGB);

      lcm_image.data.resize(n_colors * isize);
      memcpy(&lcm_image.data[0], mat.data, n_colors * isize);
      lcm_image.size = n_colors * isize;
      if (n_colors == 1)
      {
        lcm_image.pixelformat = bot_core::image_t::PIXEL_FORMAT_GRAY;
      }
      else if (n_colors == 3)
      {
        lcm_image.pixelformat = bot_core::image_t::PIXEL_FORMAT_RGB;
      }
      else
      {
        std::cout << "Number of colors [" << n_colors << "] not supported\n";
        exit(-1);
      }
    }
  }
  else if (1 == 2)
  {
    std::cout << ros_image->encoding << " | encoded not fully working - FIXME\n";
    exit(-1);
    return;

    n_colors = 2;  // 2 bytes per pixel

    /*
     int uncompressed_size = isize;
     // Insert proper compression here if needed:
     unsigned long compressed_size = depth_compress_buf_size_;
     compress2( depth_compress_buf_, &compressed_size, (const Bytef*) imageDataP, uncompressed_size,
     Z_BEST_SPEED);
     lcm_image.data.resize(compressed_size);
     */
    unsigned long zlib_compressed_size = 1000;  // fake compressed size
    lcm_image.data.resize(zlib_compressed_size);
    lcm_image.size = zlib_compressed_size;
    // images_msg_out_.image_types[1] = 5;// bot_core::images_t::DISPARITY_ZIPPED );
  }
  else
  {
    std::cout << ros_image->encoding << " | encoded not fully working - FIXME\n";
    exit(-1);
    return;

    n_colors = 2;  // 2 bytes per pixel

    lcm_image.data.resize(2 * isize);
    lcm_image.size = 2 * isize;
    // images_msg_out_.image_types[1] = 2;// bot_core::images_t::DISPARITY );
  }

  lcm_image.width = ros_image->width;
  lcm_image.height = ros_image->height;
  lcm_image.nmetadata = 0;
  lcm_image.row_stride = n_colors * ros_image->width;
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "ros2lcm_stereo");
  std::string which_camera = "head";

  ros::NodeHandle nh;
  // Disabled, originally used for jpeg-in-jpeg out:
  /*
   std::string  which_transport;
   which_transport = "/ros2lcm_head";
   std::string transport;
   if (nh.getParam(std::string( which_transport + "/image_transport"), transport)) {
   std::cout << "transport is " << transport << "\n";
   }
   ROS_ERROR("Stereo Camera Translator: [%s] [%s]", which_camera.c_str() , transport.c_str());
   */
  ROS_ERROR("Stereo Camera Translator: [%s]", which_camera.c_str());

  new App(nh);
  std::cout << "ros2lcm translator ready\n";
  ros::spin();
  return 0;
}

max_stars_repo_name: liangfok/oh-distro

max_issues_repo_name: liangfok/oh-distro

max_forks_repo_name: liangfok/oh-distro


text:
//*****************************************************************
// Copyright 2015 MIT Lincoln Laboratory
// Project:            SPAR
// Authors:            OMD
// Description:        Unit tests for AggregatingFuture
//
// Modifications:
// Date          Name           Modification
// ----          ----           ------------
// 17 Nov 2012   omd            Original Version
//*****************************************************************

#define BOOST_TEST_MODULE AggregatingFutureTest
#include "test-init.h"

#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <string>

#include "aggregating-future.h"
#include "check.h"
#include "conditions.h"
#include "string-algo.h"

using std::string;

// This is called in a different thread so it can wait for the future and then
// check it's value. The SimpleCondition waititng is set to true just before
// Wait is called on the future. This lets the test thread know that it can fire
// the furture and actually be testing that it wakes the thread.
template<class ResultT>
void WaitAndExpect(Future<ResultT> f, SimpleCondition<bool>* waiting,
                   ResultT expected, bool* called) {
  CHECK(*called == false);
  waiting->Set(true);
  f.Wait();
  BOOST_CHECK_EQUAL(f.Value(), expected);
  *called = true;
}

template<class ResultT>
void CheckExpectedCallback(ResultT expected, ResultT received, bool* called) {
  CHECK(*called == false);
  BOOST_CHECK_EQUAL(received, expected);
  *called = true;
}

// Simple test aggregator that keeps a running sum of all the partial results.
// When Finalize is called it converts the sum to a string and returns that.
class IntegerSumToString : public FutureAggregator<string, int> {
 public:
  IntegerSumToString() : sum_(0) {}
  virtual ~IntegerSumToString() {}

  virtual void AddPartialResult(const int& partial_result) {
    sum_ += partial_result;
  }

 protected:
  virtual string Finalize() {
    return itoa(sum_);
  }

 private:
  int sum_;
};

BOOST_AUTO_TEST_CASE(AggregationWorks) {
  IntegerSumToString agg;
  Future<string> f = agg.GetFuture();

  agg.AddPartialResult(10);
  agg.AddPartialResult(2);
  agg.AddPartialResult(8);
  agg.AddPartialResult(2);

  agg.Done();

  BOOST_CHECK_EQUAL(f.Value(), "22");
}

// A little bit of everything! Here we use a Future in aggregating mode. We set
// a callback, and we have another thread waiting on the Future to fire. We
// check that the callback and the waiting thread receive the expected data.
BOOST_AUTO_TEST_CASE(AggregationWaitAndCallbackWork) {
  IntegerSumToString agg;
  Future<string> f = agg.GetFuture();

  agg.AddPartialResult(10);

  bool wait_finished = false;
  SimpleCondition<bool> thread_waiting(false);
  boost::thread t(&WaitAndExpect<string>, f, &thread_waiting,
                  "100", &wait_finished);

  bool cb1_called = false;
  f.AddCallback(
      boost::bind(&CheckExpectedCallback<string>, "100", _1, &cb1_called));

  agg.AddPartialResult(80);
  agg.AddPartialResult(10);

  // Make sure the thread is running and waiting on this future.
  thread_waiting.Wait(true);

  agg.Done();

  BOOST_CHECK_EQUAL(cb1_called, true);
  t.join();
  BOOST_CHECK_EQUAL(wait_finished, true);

  // Add another callback. Since the Future already fired this should be called
  // immediately.
  bool cb2_called = false;
  f.AddCallback(
      boost::bind(&CheckExpectedCallback<string>, "100", _1, &cb2_called));
  BOOST_CHECK_EQUAL(cb2_called, true);
}

max_stars_repo_name: nathanawmk/SPARTA

max_issues_repo_name: nathanawmk/SPARTA

max_forks_repo_name: nathanawmk/SPARTA


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

#if !defined(OCTOPUS_95F7B0D1_2246_4B89_9B80_BAC4C4F22C0E)
#define OCTOPUS_95F7B0D1_2246_4B89_9B80_BAC4C4F22C0E

//#include <hpx/runtime/actions/guid_initialization.hpp>
//#include <hpx/util/detail/serialization_registration.hpp>

#include <octopus/octree/octree_server.hpp>

#include <boost/cstdint.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/version.hpp>

#define OCTOPUS_REFINEMENT_CRITERIA_BASE_VERSION 0x01

namespace octopus
{

struct OCTOPUS_EXPORT refinement_criteria_base
{
    virtual ~refinement_criteria_base() {}

    virtual bool refine(octree_server& e, child_index idx) = 0;

    virtual bool unrefine(octree_server& e, child_index idx) = 0;

    virtual refinement_criteria_base* clone() const = 0;

    template <typename Archive>
    void serialize(Archive& ar, const unsigned int) {}
};

struct refinement_criteria
{
  private:
    boost::shared_ptr<refinement_criteria_base> ptr_;

  public:
    refinement_criteria() : ptr_() {}

    refinement_criteria(refinement_criteria const& other)
      : ptr_(other.ptr_)
    {}

    refinement_criteria& operator=(refinement_criteria const& other)
    {
        ptr_ = other.ptr_;
        return *this;
    }

    refinement_criteria& operator=(refinement_criteria_base const& other)
    {
        ptr_.reset(other.clone());
        return *this;
    }

    bool refine(octree_server& e, child_index idx) const
    {
        return ptr_->refine(e, idx);
    }

    bool unrefine(octree_server& e, child_index idx) const
    {
        return ptr_->unrefine(e, idx);
    }

    template <typename Derived>
    Derived* cast() const
    {
        return dynamic_cast<Derived*>(ptr_.get());
    }

    template<class Archive>
    void save(Archive & ar, const unsigned int version) const
    {
        bool has_ptr = ptr_;

        ar & has_ptr;

        if (has_ptr)
        {
            refinement_criteria_base const* r = ptr_.get();
            ar & r;
        }
    }

    template<class Archive>
    void load(Archive & ar, const unsigned int version)
    {
        bool has_ptr = false;

        ar & has_ptr;

        if (has_ptr)
        {
            refinement_criteria_base* r = 0;
            ar & r;
            ptr_.reset(r);
        }
    }

    BOOST_SERIALIZATION_SPLIT_MEMBER();
};

}

BOOST_CLASS_VERSION(octopus::refinement_criteria_base
                  , OCTOPUS_REFINEMENT_CRITERIA_BASE_VERSION)
BOOST_CLASS_TRACKING(octopus::refinement_criteria_base
                   , boost::serialization::track_never)

#endif // OCTOPUS_95F7B0D1_2246_4B89_9B80_BAC4C4F22C0E

max_stars_repo_name: STEllAR-GROUP/octopus

max_issues_repo_name: STEllAR-GROUP/octopus

max_forks_repo_name: STEllAR-GROUP/octopus


text:
#include "utils.hpp"
#include <boost/asio/io_context.hpp>
#include <boost/test/unit_test.hpp>
#include <sodium/utils.h>
#include <string.h>

using namespace std;

namespace pichi::unit_test {

static boost::asio::io_context io;
static boost::asio::detail::Pull* pPull = nullptr;
static boost::asio::detail::Push* pPush = nullptr;
static boost::asio::detail::YieldState* pState = nullptr;
boost::asio::yield_context gYield = {io.get_executor(), *pState, *pPush, *pPull};

vector<uint8_t> str2vec(string_view s) { return {cbegin(s), cend(s)}; }

vector<uint8_t> hex2bin(string_view hex)
{
  auto v = vector<uint8_t>(hex.size() / 2, 0);
  sodium_hex2bin(v.data(), v.size(), hex.data(), hex.size(), nullptr, nullptr, nullptr);
  return v;
}

}  // namespace pichi::unit_test

max_stars_repo_name: imuzi/pichi

max_issues_repo_name: imuzi/pichi

max_forks_repo_name: imuzi/pichi