text:
#include "autodataassociation.h"

#include "include/core/g2o_object.h"
#include "instance.h"

#include <Eigen/Core>

#include "DataStruct.h"

#include <algorithm>

using namespace Eigen;
using namespace std;

bool compare_func(pair<int,double>& p1, pair<int,double>& p2)
{
    return p1.second < p2.second;
}

void AutoDataAssociation::initialize(vector<Instance> &instances, Matrix3d& calib)
{

    mmCalib = calib;
    initializeInstances(instances);

}

void AutoDataAssociation::initializeInstances(vector<Instance> &instances)
{
    mvpEllipsoids.clear();
    int num = instances.size();
    for(int i=0;i<num;i++){
        auto ins = instances[i];
        g2o::ellipsoid* e = generateEllipsoid(ins);
        mvpEllipsoids.push_back(e);
    }

    return;

}

// 输入: 当前帧gt,
vector<Association> AutoDataAssociation::process(VectorXd &poseTwc, MatrixXd &detMat){
    int objNum = mvpEllipsoids.size();

    vector<ProjEllipse> projEllipses;
    // 遍历 instances
    for(int i=0;i<objNum;i++)
    {
        g2o::ellipsoid* e = mvpEllipsoids[i];

        g2o::SE3Quat Twc(poseTwc.tail(7));  // xyz q4
        Vector4d bbox = e->getBoundingBoxFromProjection(Twc.inverse(), mmCalib);

        // 定义一个投影体, 方便彼此进行数据关联.
        ProjEllipse proje;
        proje.label = e->miLabel;
        proje.bbox = bbox;
        proje.id = i;

        projEllipses.push_back(proje);
    }

    // 开始做关联
    vector<Association> associations = associateProjEllipseAndDetections(projEllipses, detMat);

    return associations;

}

g2o::ellipsoid* AutoDataAssociation::generateEllipsoid(Instance& ins){
    g2o::ellipsoid* e = new g2o::ellipsoid;
    Eigen::Matrix<double, 9, 1> v;
    for(int n=0;n<9;n++)
        v(n) = ins.param[n];
    e->fromMinimalVector(v);

    // 设置 label id.
    e->miLabel = ins.label;

    return e;
}

vector<Association> AutoDataAssociation::associateProjEllipseAndDetections(vector<ProjEllipse>& projEllipses, MatrixXd &detMat)
{
    double THRESH_DIS = 150; // 中心 x y差距和不超过多少像素
    // -------
    vector<Association> associations;
    vector<ProjEllipse> projEllipsesSelected; // 存储那些被选召的椭圆

    // 针对每个ob det 从候选椭球挑一个
    //  -> 依据: label一致(?错误检测, 很少的情况不考虑), 挑选距离最近, 而且距离满足阈值.
    int detNum = detMat.rows();

    vector<ProjEllipse> projEllipsesLeft = projEllipses;
    int objNum = projEllipsesLeft.size();
    vector<bool> projsChosen(objNum, false);
    for( int i=0; i< detNum; i++ )
    {
        Association asso;

        VectorXd det = detMat.row(i);
        // 挑选出所有label一致的.
        int label = det(DET_TABLE_LABEL);
        double x1 = det(DET_TABLE_X1);
        double y1 = det(DET_TABLE_Y1);
        double x2 = det(DET_TABLE_X2);
        double y2 = det(DET_TABLE_Y2);

        if(objNum > 0){
            vector<pair<int,double>> associationDistances;
            for(int n=0;n<objNum;n++)
            {
                if(projsChosen[n]) continue;        // 已经选中的不再考虑
                ProjEllipse proj = projEllipsesLeft[n];
                // if(proj.label!=label) continue;

                // 计算矩形框中间差值
                double dis = std::abs(x1+x2-proj.bbox(0)-proj.bbox(2))/2
                        + std::abs(y1+y2-proj.bbox(1)-proj.bbox(3))/2;
                associationDistances.push_back(make_pair(n, dis));
            }

            if(associationDistances.size() > 0){
                // 排序取最小的一个.
                sort(associationDistances.begin(), associationDistances.end(), compare_func);

                double mini_dis = associationDistances[0].second;
                int best_instance_id = associationDistances[0].first;
                if(mini_dis < THRESH_DIS )
                {
                    // ok
                    Association asso;
                    asso.instanceID = best_instance_id;
                    asso.detID = i;
                    asso.prob = 1;

                    associations.push_back(asso);

                    projsChosen[best_instance_id] = true;
                    projEllipsesSelected.push_back(projEllipses[best_instance_id]);

                }
            }
        }

    }
    // 依次循环得到关联结果.
    mvProjEllipses.clear();
    mvProjEllipses = projEllipsesSelected;

    return associations;
}

cv::Mat AutoDataAssociation::drawProjection(cv::Mat &in)
{
    cv::Mat out = in.clone();
    int num = mvProjEllipses.size();
    for(auto proj : mvProjEllipses )
    {
        Vector4d rect = proj.bbox;
        cv::rectangle(out, cv::Rect(cv::Point(rect[0],rect[1]),cv::Point(rect[2],rect[3])), cv::Scalar(0,0,255), 2);
    }

    return out.clone();
}


text:
/*
 * (C) Copyright 2015 Kurento (http://kurento.org/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <gst/gst.h>

#include "loadConfig.hpp"

#include <iostream>
#include <queue>
#include <list>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/info_parser.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <utility>

#define GST_CAT_DEFAULT kurento_load_config
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
#define GST_DEFAULT_NAME "KurentoLoadConfig"

namespace kurento
{

class ParseException : public virtual std::exception
{
public:
  ParseException(std::string message) : message(std::move(message)){};

  ~ParseException() override = default;

  const char *what() const noexcept override { return message.c_str(); };

  const std::string &getMessage() const
  {
    return message;
  };

private:
  std::string message;
};

static std::list<std::string>
split (const std::string &s, char delim)
{
  std::list<std::string> elems;
  std::stringstream ss (s);
  std::string item;

  while (std::getline (ss, item, delim) ) {
    elems.push_back (item);
  }

  return elems;
}

void
mergePropertyTrees (boost::property_tree::ptree &ptMerged,
                    const boost::property_tree::ptree &ptSecond, int level )
{
  // Value or object or array
  if (level > 0 && ptSecond.empty() ) {
    // Copy value
    ptMerged = ptSecond;
  } else if (level > 0 && ptSecond.count (std::string() ) == ptSecond.size() ) {
    // Copy array
    ptMerged = ptSecond;
  } else {
    auto it = ptSecond.begin();

    for (; it != ptSecond.end(); ++it) {
      boost::property_tree::ptree child = ptMerged.get_child (it->first.data(),
                                          boost::property_tree::ptree() );
      mergePropertyTrees (child, it->second, level + 1);

      ptMerged.erase (it->first.data() );
      ptMerged.add_child (it->first.data(), child);
    }
  }
}

static std::string
loadFile (boost::property_tree::ptree &config,
          const boost::filesystem::path &fspath)
{
  const std::string path = fspath.string();
  const std::string extension1 = fspath.stem().extension().string();
  const std::string extension2 = fspath.extension().string();

  boost::property_tree::ptree readConfig;

  if (extension1 == ".conf") {
    if (extension2 == ".json") {
      try {
        boost::property_tree::read_json (path, readConfig);
      } catch (boost::property_tree::json_parser_error &e) {
        throw ParseException (std::string("JSON parse error: ") + e.what());
      }
    } else if (extension2 == ".info") {
      try {
        boost::property_tree::read_info (path, readConfig);
      } catch (boost::property_tree::info_parser_error &e) {
        throw ParseException (std::string("INFO parse error: ") + e.what());
      }
    } else if (extension2 == ".ini") {
      try {
        boost::property_tree::read_ini (path, readConfig);
      } catch (boost::property_tree::ini_parser_error &e) {
        throw ParseException (std::string("INI parse error: ") + e.what());
      }
    } else if (extension2 == ".xml") {
      try {
        boost::property_tree::read_xml (path, readConfig);
      } catch (boost::property_tree::xml_parser_error &e) {
        throw ParseException (std::string("XML parse error: ") + e.what());
      }
    } else {
      throw ParseException ("Unknown conf format: " + extension2);
    }
  } else {
    throw ParseException ("Unknown file type: " + fspath.filename().string());
  }

  mergePropertyTrees (config, readConfig);

  config.put ("configPath", fspath.parent_path().string() );

  std::string fileName = fspath.filename().string();
  fileName = fileName.substr (0, fileName.size() - extension1.size()
      - extension2.size());

  return fileName;
}

static std::string
diffPathToKey (const boost::filesystem::path &path,
               const boost::filesystem::path &ancestorPath)
{
  boost::filesystem::path diffpath;

  boost::filesystem::path tmppath = path;

  while (tmppath != ancestorPath) {
    diffpath = tmppath.stem() / diffpath;
    tmppath = tmppath.parent_path();
  }

  tmppath = diffpath;

  boost::property_tree::ptree loadedConfig;
  std::string key;

  for (auto it = tmppath.begin(); it != tmppath.end(); it ++) {
    if (key.empty() ) {
      key = it->string();
    } else {
      key += "." + it->string();
    }
  }

  return key;
}

static void
loadModulesConfigFromDir (boost::property_tree::ptree &config,
                          const boost::filesystem::path &dir, const boost::filesystem::path &parentDir)
{
  GST_INFO ("Looking for config files in %s", dir.string().c_str() );

  if (!boost::filesystem::is_directory (dir) ) {
    GST_WARNING ("Unable to load config files from: %s, it is not a directory",
                 dir.string().c_str() );
    return;
  }

  boost::filesystem::directory_iterator end_itr;

  for ( boost::filesystem::directory_iterator itr ( dir ); itr != end_itr;
        ++itr ) {
    if (boost::filesystem::is_regular (*itr) ) {
      const std::string pathStr = itr->path().string();
      try {
        boost::property_tree::ptree moduleConfig;
        std::string fileName = loadFile (moduleConfig, itr->path() );
        std::string key = diffPathToKey (itr->path().parent_path(), parentDir);
        boost::property_tree::ptree loadedConfig;

        key = key.empty() ? "modules" :  "modules." + key;
        key += "." + fileName;

        loadedConfig.put_child (key, moduleConfig);
        mergePropertyTrees (config, loadedConfig);

        GST_INFO ("Loaded module config: %s", pathStr.c_str());
      } catch (ParseException &e) {
        GST_WARNING ("Error loading config: %s, %s", pathStr.c_str(), e.what());
        std::cerr << "Error loading config: " << pathStr << ", " << e.what()
            << std::endl;
      }
    } else if (boost::filesystem::is_directory (*itr) ) {
      loadModulesConfigFromDir (config, itr->path(), parentDir);
    }
  }
}

static void
loadModulesConfig (boost::property_tree::ptree &config,
                   const boost::filesystem::path &configFilePath, std::string modulesConfigPath)
{
  std::list <std::string> locations;

  if (modulesConfigPath.empty() ) {
    boost::filesystem::path modulesConfigDir = configFilePath.parent_path() /
        "modules";

    modulesConfigPath = modulesConfigDir.string();
  }

  locations = split (modulesConfigPath, ':');

  for (std::string location : locations) {
    boost::filesystem::path dir (location);
    dir.normalize();

    loadModulesConfigFromDir (config, dir, dir);
  }
}

void
loadConfig (boost::property_tree::ptree &config, const std::string &file_name,
            const std::string &modulesConfigPath)
{
  boost::filesystem::path configFilePath (file_name);
  GST_INFO ("Reading configuration from: %s", file_name.c_str () );

  try {
    loadFile (config, configFilePath);
  } catch (ParseException &e) {
    GST_ERROR ("Error reading configuration: %s", e.what() );
    std::cerr << "Error reading configuration: " << e.what() << std::endl;
    exit (1);
  } catch (boost::property_tree::ptree_error &e) {
    GST_ERROR ("Error reading configuration: %s", e.what() );
    std::cerr << "Error reading configuration: " << e.what() << std::endl;
    exit (1);
  }

  loadModulesConfig (config, configFilePath, modulesConfigPath);

  GST_INFO ("Configuration loaded successfully");

  std::ostringstream oss;
  boost::property_tree::write_json (oss, config, true);
  std::string infoConfig = oss.str();

  GST_INFO ("Loaded config in effect:\n%s", infoConfig.c_str() );
}

} /* kurento */

static void init_debug() __attribute__((constructor));

static void init_debug() {
  GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, GST_DEFAULT_NAME, 0,
                           GST_DEFAULT_NAME);
}

ext: cpp

max_stars_repo_name: dasJ/kurento-media-server

max_issues_repo_name: dasJ/kurento-media-server

max_forks_repo_name: dasJ/kurento-media-server


text:
#include "Widget_planeDetection.h"
#include <QSlider>
#include <QLabel>
#include <QVBoxLayout>
#include <QtInfoPanelTrafficLightBase.h>
#include <boost/tokenizer.hpp>
#include <boost/foreach.hpp>

Widget_planeDetection::Widget_planeDetection(
        QWidget *parent, Qt::WindowFlags f)
    : QtPluginWidgetBase(parent, f)
{
    mColorWithLevel = true;
    this->mWidgetLocation = WidgetLocation::top_right;
    mStreamTypes = ifind::InitialiseStreamTypeSetFromString("Standardplanedetection");
    mIsBuilt = false;

    mLabel = new QLabel("Text not set", this);
    mLabel->setStyleSheet(sQLabelStyle);

    //
    mSlider = new QSlider(Qt::Orientation::Horizontal);
    mSlider->setStyleSheet(QtPluginWidgetBase::sQSliderStyle);

    mSlider->setMaximum(100);
    mSlider->setMinimum(0);
    mSlider->setValue(100);
    mSlider->setAutoFillBackground(true);

    //
    mSliderTA = new QSlider(Qt::Orientation::Horizontal);
    mSliderTA->setStyleSheet(QtPluginWidgetBase::sQSliderStyle);

    mSliderTA->setMaximum(100);
    mSliderTA->setMinimum(0);
    mSliderTA->setValue(20);
    mSliderTA->setAutoFillBackground(true);

    auto vLayout = new QVBoxLayout(this);
    vLayout->setContentsMargins(0, 0, 0, 0);
    vLayout->setSpacing(0);
    this->setLayout(vLayout);

    vLayout->addWidget(mLabel);
    this->AddInputStreamComboboxToLayout(vLayout);
    vLayout->addWidget(mSlider);
    vLayout->addWidget(mSliderTA);

}

void Widget_planeDetection::Build(std::vector<std::string> &labelnames){

    QVBoxLayout * outmost_layout = reinterpret_cast<QVBoxLayout*>(this->layout());
    if (mWidgetOptions.show_bars == true) {
        //QVBoxLayout *vLayout = dynamic_cast<QVBoxLayout*>(outmost_layout);
        /// This will have bar graphs of the live scan plane values
        {
            QtInfoPanelTrafficLightBase::Configuration standardPlaneTrafficLightConfig;

            standardPlaneTrafficLightConfig.Mode =
                    QtInfoPanelTrafficLightBase::Modes::ImmediateBarAbsolute;
            standardPlaneTrafficLightConfig.LabelNames = labelnames;
            standardPlaneTrafficLightConfig.NGridColumns = 2;

            standardPlaneTrafficLightConfig.ValueColorsVector.push_back(
                        QtInfoPanelTrafficLightBase::ValueColors(
                            std::numeric_limits<double>::lowest(), // value
                            QColor("black"), // background colour
                            QColor("silver"))); // text colour

            standardPlaneTrafficLightConfig.MetadataLabelsKey = "Standardplanedetection_labels";
            standardPlaneTrafficLightConfig.MetadataValuesKey = "Standardplanedetection_confidences";
            standardPlaneTrafficLightConfig.MetadataSplitCharacter = ',';

            auto infoPanel = new QtInfoPanelTrafficLightBase(standardPlaneTrafficLightConfig, this);
            infoPanel->SetStreamTypesFromStr("Standardplanedetection");
            outmost_layout->addWidget(infoPanel, 1, Qt::AlignTop);

            QObject::connect(this, &QtPluginWidgetBase::ImageAvailable,
                             infoPanel, &QtInfoPanelBase::SendImageToWidget);
        }
        /// This will 'light up' as each scan plane is found
        if (mWidgetOptions.show_checklist == true) {
            QtInfoPanelTrafficLightBase::Configuration standardPlaneTrafficLightConfig;

            standardPlaneTrafficLightConfig.Mode =
                    QtInfoPanelTrafficLightBase::Modes::ImmediateLabelAbsolute;
            standardPlaneTrafficLightConfig.LabelNames ={ "3VV","4CH","Abdominal",
                                                          "Background","Brain (Cb.)","Brain (Tv.)", "Femur","Kidneys","Lips","LVOT",
                                                          "Profile","RVOT","Spine (cor.)","Spine (sag.)" };
            standardPlaneTrafficLightConfig.NGridColumns = 2;

            standardPlaneTrafficLightConfig.ValueColorsVector.push_back(
                        QtInfoPanelTrafficLightBase::ValueColors(
                            std::numeric_limits<double>::lowest(), // value
                            QColor("black"), // background colour
                            QColor("silver"))); // text colour

            standardPlaneTrafficLightConfig.ValueColorsVector.push_back(
                        QtInfoPanelTrafficLightBase::ValueColors(
                            5.0, // value
                            QColor(214, 123, 1), // background colour - like coral but darker and less red
                            QColor("white"))); // black"))); // text colour

            standardPlaneTrafficLightConfig.ValueColorsVector.push_back(
                        QtInfoPanelTrafficLightBase::ValueColors(
                            10.0, // value
                            QColor("green"), // background colour
                            QColor("white"))); // black"))); // text colour

            standardPlaneTrafficLightConfig.MetadataLabelsKey = "Autoreport_spd_labels";
            standardPlaneTrafficLightConfig.MetadataValuesKey = "Autoreport_Standardplane_clusterR";
            standardPlaneTrafficLightConfig.MetadataSplitCharacter = ',';

            auto infoPanel = new QtInfoPanelTrafficLightBase(
                        standardPlaneTrafficLightConfig, this);
            infoPanel->setColorWithLevel(mColorWithLevel);
            infoPanel->SetStreamTypesFromStr("AutoReport");
            outmost_layout->addWidget(infoPanel, 1, Qt::AlignTop);

            QObject::connect(
                        this, &QtPluginWidgetBase::ImageAvailable,
                        infoPanel, &QtInfoPanelBase::SendImageToWidget);
        }
    }
}

void Widget_planeDetection::SendImageToWidgetImpl(ifind::Image::Pointer image){

    if (mIsBuilt == false){
        mIsBuilt = true;
        std::string labels = image->GetMetaData<std::string>((this->pluginName() +"_labels").toStdString().c_str());

        boost::char_separator<char> sep(",");
        boost::tokenizer< boost::char_separator<char> > tokens(labels, sep);
        std::vector<std::string> labelnames;
        BOOST_FOREACH (const std::string& t, tokens) {
                labelnames.push_back(t);
            }
        this->Build(labelnames);
    }

    std::stringstream stream;
    stream << "==" << this->mPluginName.toStdString() << "=="<<std::endl;
    if (image->HasKey("Standardplanedetection_bckth")){
        stream << "(1) Background threshold: "<< image->GetMetaData<std::string>("Standardplanedetection_bckth")<< std::endl;
        stream << "(2) Averaged frames: "<< image->GetMetaData<std::string>("Standardplanedetection_tempAvg")<< std::endl;
    }
    //stream << "Receiving " << ifind::StreamTypeSetToString(this->mInputStreamTypes) << std::endl;
    stream << "Sending " << ifind::StreamTypeSetToString(this->mStreamTypes);

    mLabel->setText(stream.str().c_str());

    Q_EMIT this->ImageAvailable(image);
}

bool Widget_planeDetection::colorWithLevel() const
{
    return mColorWithLevel;
}

void Widget_planeDetection::setColorWithLevel(bool colorWithLevel)
{
    mColorWithLevel = colorWithLevel;
}

ext: cxx

max_stars_repo_name: gomezalberto/pretus

max_issues_repo_name: gomezalberto/pretus

max_forks_repo_name: gomezalberto/pretus


text:
#include "cliquetable_state.hpp"
#include <boost/dynamic_bitset.hpp>

State* CliqueTableState::transition(Problem* prob, int var, int val)
{
	assert(val == 0 || val == 1);

	if (val == 1 && !intset.contains(var)) {
		return NULL;
	}

	CliqueTableInstance* insti = dynamic_cast<CliqueTableInstance*>(prob->inst);
	if (insti == NULL) {
		cout << "Error: Using incompatible State and Instance" << endl;
		exit(1);
	}

	int nvars = insti->nvars;

	if (!insti->nonnegated_only) {
		if (val == 0 && !intset.contains(var + nvars)) {
			return NULL;
		}
	}

	CliqueTableState* new_state;
	new_state = new CliqueTableState(intset);

	if (insti->nonnegated_only) {

		// Remove vertex itself
		new_state->intset.remove(var);

		// Remove neighbors of vertex if added to graph
		if (val == 1) {
			new_state->intset.set &= insti->adj_mask_compl[var].set;
		}

	} else {

		// Remove variable itself (both non-negated and negated)
		new_state->intset.remove(var);
		new_state->intset.remove(var + nvars);

		// Remove neighbors of vertex if added to graph
		if (val == 1) {
			new_state->intset.set &= insti->adj_mask_compl[var].set;
		} else { // val == 0
			new_state->intset.set &= insti->adj_mask_compl[var+nvars].set;
		}

		// Check for early infeasibility: if any of the vertices removed from set has a counterpart also not in set, then infeasible
		// Note that we cannot simply check if neither of them are in the set because they may have been already branched upon

		// 1 if a set-to-zero variable (1 in first expression) has a corresponding negation set to zero (0 in second expression)
		boost::dynamic_bitset<>& new_set = new_state->intset.set;
		boost::dynamic_bitset<> infeas_check = (intset.set - new_set) - ((new_set << nvars) | (new_set >> nvars));
		infeas_check.reset(var); // ignore current var
		infeas_check.reset(var + nvars);
		if (infeas_check.any()) { // if any 1 is left (except for current variable), then infeasible
			// infeasible
			delete new_state;
			return NULL;
		}
	}

	return new_state;
}

max_stars_repo_name: ctjandra/ddopt-bounds

max_issues_repo_name: ctjandra/ddopt-bounds

max_forks_repo_name: ctjandra/ddopt-bounds


text:
/***************************************************************************************************************************
* formation_control.cpp
*
* Author: Qyp
*
* Update Time: 2020.4.2
*                           采用虚拟领机-从机结构。地面站为主节点，每一个飞机都为从机
***************************************************************************************************************************/
#include <ros/ros.h>
#include <iostream>
#include <command_to_mavros.h>
#include <geometry_msgs/Point.h>
#include <mavros_msgs/CommandBool.h>
#include <mavros_msgs/SetMode.h>
#include <mavros_msgs/State.h>
#include <nav_msgs/Odometry.h>
#include <mavros_msgs/PositionTarget.h>
#include <geometry_msgs/PoseStamped.h>
#include <geometry_msgs/TwistStamped.h>
#include <mavros_msgs/ActuatorControl.h>
#include <sensor_msgs/Imu.h>
#include <bitset>
#include <Eigen/Eigen>

using namespace std;
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>变量声明<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
string uav_name;
string neighbour_name1,neighbour_name2;
int num_neighbour = 2;
ros::Publisher setpoint_raw_local_pub;

mavros_msgs::State current_state;
Eigen::Vector3d pos_drone;
Eigen::Vector3d vel_drone;

Eigen::Vector3d pos_leader;
Eigen::Vector3d relative_pos_to_leader;

Eigen::Vector3d state_sp(0,0,0);
float yaw_sp = 0;
bool get_new_cmd = false;

float k_p;
float k_aij;
ros::Publisher vision_pub;
//mavros_msgs::State current_state_nei[2];
Eigen::Vector3d pos_nei[2];
Eigen::Vector3d vel_nei[2];
//2020.4.2 这里有个十分神奇的bug： 这个语句后面不要再放任何变量声明了，不然变量的值会各种变化。。
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>函数声明<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>回调函数<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
void pos_leader_cb(const geometry_msgs::Point::ConstPtr &msg)
{
    pos_leader = Eigen::Vector3d(msg->x,msg->y,msg->z);
    get_new_cmd = true;
}
void formation_cb(const geometry_msgs::Point::ConstPtr &msg)
{
    relative_pos_to_leader  = Eigen::Vector3d(msg->x,msg->y,msg->z);
}
void state_cb(const mavros_msgs::State::ConstPtr &msg)
{
    current_state = *msg;
}
void odom_cb(const nav_msgs::Odometry::ConstPtr &msg)
{
    pos_drone  = Eigen::Vector3d(msg->pose.pose.position.x,msg->pose.pose.position.y,msg->pose.pose.position.z);
    vel_drone  = Eigen::Vector3d(msg->twist.twist.linear.x,msg->twist.twist.linear.y,msg->twist.twist.linear.z);
}
void odom_nei_cb(const nav_msgs::Odometry::ConstPtr &msg, int nei_id)
{
    pos_nei[nei_id]  = Eigen::Vector3d(msg->pose.pose.position.x,msg->pose.pose.position.y,msg->pose.pose.position.z);
    vel_nei[nei_id]  = Eigen::Vector3d(msg->twist.twist.linear.x,msg->twist.twist.linear.y,msg->twist.twist.linear.z);
}
void gazebo_cb(const nav_msgs::Odometry::ConstPtr& msg)
{

    geometry_msgs::PoseStamped vision;

    vision.pose.position.x = msg->pose.pose.position.x;
    vision.pose.position.y = msg->pose.pose.position.y;
    vision.pose.position.z = msg->pose.pose.position.z;

    vision.pose.orientation.x = msg->pose.pose.orientation.x;
    vision.pose.orientation.y = msg->pose.pose.orientation.y;
    vision.pose.orientation.z = msg->pose.pose.orientation.z;
    vision.pose.orientation.w = msg->pose.pose.orientation.w;

    vision.header.stamp = ros::Time::now();
    vision_pub.publish(vision);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>主 函 数<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
int main(int argc, char **argv)
{
    ros::init(argc, argv, "formation_control");
    ros::NodeHandle nh("~");
    ros::Rate rate(20.0);

    //无人机编号 1号无人机则为1
    nh.param<string>("uav_name", uav_name, "/uav0");
    nh.param<float>("k_p", k_p, 0.95);
    nh.param<float>("k_aij", k_aij, 0.1);
    //可监听到的无人机编号，目前设定为可监听到两台无人机，后期考虑可通过数组传递参数，监听任意ID的无人机
    nh.param<string>("neighbour_name1", neighbour_name1, "/uav0");
    nh.param<string>("neighbour_name2", neighbour_name2, "/uav0");

    //订阅虚拟主机的位置，及阵型
    ros::Subscriber leader_sub = nh.subscribe<geometry_msgs::Point>("/prometheus/formation/leader_pos", 10, pos_leader_cb);
    ros::Subscriber formation_sub = nh.subscribe<geometry_msgs::Point>("/prometheus/formation" + uav_name, 10, formation_cb);

    // 【订阅】gazebo仿真真值
    ros::Subscriber gazebo_sub = nh.subscribe<nav_msgs::Odometry>("/prometheus/ground_truth"+ uav_name, 100, gazebo_cb);
    vision_pub = nh.advertise<geometry_msgs::PoseStamped>(uav_name + "/mavros/vision_pose/pose", 100);

    //订阅本台飞机的状态
    ros::Subscriber state_sub    = nh.subscribe<mavros_msgs::State>(uav_name + "/mavros/state", 10, state_cb);
    ros::Subscriber odom_sub = nh.subscribe<nav_msgs::Odometry>(uav_name + "/mavros/local_position/odom", 100, odom_cb);

    //订阅邻居飞机的状态信息
    ros::Subscriber odom_nei_sub_1 = nh.subscribe<nav_msgs::Odometry>(neighbour_name1 + "/mavros/local_position/odom", 100, boost::bind(&odom_nei_cb,_1, 0));
    ros::Subscriber odom_nei_sub_2 = nh.subscribe<nav_msgs::Odometry>(neighbour_name2 + "/mavros/local_position/odom", 100, boost::bind(&odom_nei_cb,_1, 1));

    // 用于与mavros通讯的类，通过mavros发送控制指令至飞控【本程序->mavros->飞控】
    command_to_mavros _command_to_mavros;

    //固定的浮点显示
    cout.setf(ios::fixed);
    //setprecision(n) 设显示小数精度为n位
    cout<<setprecision(2);
    //左对齐
    cout.setf(ios::left);
    // 强制显示小数点
    cout.setf(ios::showpoint);
    // 强制显示符号
    cout.setf(ios::showpos);

    // Waiting for input
    cout << "Waiting for start command......"<< endl;

    int start_flag = 0;
    while(!get_new_cmd)
    {
        _command_to_mavros.idle();
        ros::spinOnce();
        rate.sleep();
    }

    get_new_cmd = false;

    cout << "Switch to OFFBOARD and arm ..."<<endl;
    while(current_state.mode != "OFFBOARD")
    {
        _command_to_mavros.mode_cmd.request.custom_mode = "OFFBOARD";
        _command_to_mavros.set_mode_client.call(_command_to_mavros.mode_cmd);
        _command_to_mavros.idle();
        //执行回调函数
        ros::spinOnce();
        ros::Duration(0.5).sleep();

        if (_command_to_mavros.mode_cmd.response.mode_sent)
        {
            cout <<"[formation_flight, UAV_name: "<<uav_name<<"]: "<< "Set to OFFBOARD Mode Susscess! "<< endl;
        }
    }

    while(!current_state.armed)
    {
        _command_to_mavros.arm_cmd.request.value = true;
        _command_to_mavros.arming_client.call(_command_to_mavros.arm_cmd);
        _command_to_mavros.idle();
        //执行回调函数
        ros::spinOnce();
        ros::Duration(0.5).sleep();

        if (_command_to_mavros.arm_cmd.response.success)
        {
            cout <<"[formation_flight, UAV_name: "<<uav_name<<"]: "<< "Arm Susscess! "<< endl;
        }
    }

    //起飞位置为飞机所在位置上方1米
    cout<<"[formation_flight, UAV_name: "<<uav_name<<"]: "<<"Takeoff to predefined position."<<endl;
    Eigen::Vector3d takeoff_pos = pos_drone + Eigen::Vector3d(0,0,1);
    yaw_sp = 0.0;
    while(!get_new_cmd)
    {
        _command_to_mavros.send_pos_setpoint(takeoff_pos, yaw_sp);
        rate.sleep();
        ros::spinOnce();
    }

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>主  循  环<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    while(ros::ok())
    {
        ros::spinOnce();

        //formation control， 平面阵型，xy控制速度，z轴高度定高
        state_sp[0] = k_p * (pos_leader[0] + relative_pos_to_leader[0] - pos_drone[0]) + k_aij*(pos_nei[0][0] - pos_drone[0]) + k_aij*(pos_nei[1][0] - pos_drone[0]);
        state_sp[1] = k_p * (pos_leader[1] + relative_pos_to_leader[1] - pos_drone[1]) + k_aij*(pos_nei[0][1] - pos_drone[1]) + k_aij*(pos_nei[1][1] - pos_drone[1]);
        state_sp[2] = 1.0;
        yaw_sp = 0.0;
        _command_to_mavros.send_vel_xy_pos_z_setpoint(state_sp, yaw_sp);

        cout << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Formation Flight Mission<<<<<<<<<<<<<<<<<<<<<<<<<<< "<< endl;
        cout << "UAV_name : " << uav_name <<"   UAV_mode: "<< current_state.mode <<"   arm or not: "<< current_state.armed << endl;
        cout << "Position [X Y Z] : " << pos_drone[0] << " [ m ] "<< pos_drone[1]<<" [ m ] "<<pos_drone[2]<<" [ m ] "<<endl;
        cout << "Velocity [X Y Z] : " << vel_drone[0] << " [m/s] "<< vel_drone[1]<<" [m/s] "<<vel_drone[2]<<" [m/s] "<<endl;
        cout << "P_Leader [X Y Z] : " << pos_leader[0] << " [ m ] "<< pos_leader[1]<<" [ m ] "<<pos_leader[2]<<" [ m ] "<<endl;
        cout << "Pos_Nei1 [X Y Z] : " << pos_nei[0][0] << " [ m ] "<< pos_nei[0][1]<<" [ m ] "<<pos_nei[0][2]<<" [ m ] "<<endl;
        cout << "Pos_Nei2 [X Y Z] : " << pos_nei[1][0] << " [ m ] "<< pos_nei[1][1]<<" [ m ] "<<pos_nei[1][2]<<" [ m ] "<<endl;
        cout << "Relative [X Y Z] : " << relative_pos_to_leader[0] << " [ m ] "<< relative_pos_to_leader[1]<<" [ m ] "<<relative_pos_to_leader[2]<<" [ m ] "<<endl;
        cout << "state_sp [X Y Z] : " << state_sp[0]  << " [m/s] "<< state_sp[1] <<" [m/s] "<<state_sp[2] <<" [m/s] "<<endl;

        rate.sleep();
    }

}

max_stars_repo_name: zenglongGH/Prometheus

max_issues_repo_name: JcZou/Prometheus

max_forks_repo_name: JcZou/Prometheus


text:
#include <tradelayer/mdex.h>
#include <tradelayer/dex.h>
#include <tradelayer/tradelayer.h>
#include <tradelayer/uint256_extensions.h>
#include <test/test_bitcoin.h>
#include <boost/test/unit_test.hpp>
#include <stdint.h>

using namespace mastercore;

BOOST_FIXTURE_TEST_SUITE(tradelayer_mdex_functions_tests, BasicTestingSetup)

void literVolume(int64_t& amount, uint32_t propertyId, const int& fblock, const int& sblock, const std::map<int, std::map<uint32_t,int64_t>>& aMap)
{
    // BOOST_TEST_MESSAGE("iter, fblock:" << fblock);
    // BOOST_TEST_MESSAGE("iter, sblock:" << sblock);
    for(const auto &m : aMap)
    {
        const int& blk = m.first;
        // BOOST_TEST_MESSAGE("iter, block (after):" << blk);
        if(blk < fblock && fblock != 0){
            continue;
        } else if(sblock < blk){
            break;
        }

        // BOOST_TEST_MESSAGE("iter, block:" << blk);

        const auto &blockMap = m.second;
        auto itt = blockMap.find(propertyId);
        // BOOST_TEST_MESSAGE("iter, propertyId:" << propertyId);
        if (itt != blockMap.end()){

            const int64_t& newAmount = itt->second;

            // overflows?
            assert(!isOverflow(amount, newAmount));
            amount += newAmount;
            // BOOST_TEST_MESSAGE("iter, amount (after):" << amount);

        }

    }

}

int64_t lgetVWap(uint32_t propertyId, int aBlock, const std::map<uint32_t,std::map<int,std::vector<std::pair<int64_t,int64_t>>>>& aMap)
{
    int64_t volume = 0;
    arith_uint256 nvwap = 0;
    const int rollback = aBlock - 12;

    // BOOST_TEST_MESSAGE("rollback:" << rollback);

    auto it = aMap.find(propertyId);
    if (it != aMap.end())
    {
        auto &vmap = it->second;
        auto itt = (rollback > 0) ? find_if(vmap.begin(), vmap.end(), [&rollback] (const std::pair<int,std::vector<std::pair<int64_t,int64_t>>>& int_arith_pair) { return (int_arith_pair.first >= rollback);}) : vmap.begin();
        if (itt != vmap.end())
        {
            for ( ; itt != vmap.end(); ++itt)
            {
                auto v = itt->second;
                for_each(v.begin(),v.end(), [&nvwap](const std::pair<int64_t,int64_t>& num){ nvwap += ConvertTo256(num.first * (num.second / COIN));});
            }
        }

    }
    // calculating the volume
    // BOOST_TEST_MESSAGE("rollback:" << rollback);
    // BOOST_TEST_MESSAGE("aBlock:" << aBlock);
    literVolume(volume, propertyId, rollback, aBlock, MapLTCVolume);

    if (volume == 0) BOOST_TEST_MESSAGE("volume here is 0");

    // BOOST_TEST_MESSAGE("nvwap:" << ConvertTo64(nvwap));
    // BOOST_TEST_MESSAGE("volume:" << volume);
    return ((volume > 0) ? (COIN *(ConvertTo64(nvwap) / volume)) : 0);

}

int64_t lincreaseLTCVolume(uint32_t propertyId, uint32_t propertyDesired, int aBlock)
{
    int64_t total = 0, propertyAmount = 0, propertyDesiredAmount = 0;
    const int rollback = aBlock - 1000;
    iterVolume(propertyAmount, propertyId, rollback, aBlock, MapLTCVolume);
    iterVolume(propertyDesiredAmount, propertyDesired, rollback, aBlock, MapLTCVolume);

    // BOOST_TEST_MESSAGE("propertyAmount:" << propertyAmount);
    // BOOST_TEST_MESSAGE("propertyDesiredAmount:" << propertyDesiredAmount);
    // BOOST_TEST_MESSAGE("aBlock:" << aBlock);
    // BOOST_TEST_MESSAGE("rollback:" << rollback);

    if (1000 * COIN <=  propertyAmount && 1000 * COIN <=  propertyDesiredAmount)
    {

        // look up the 12-block VWAP of the denominator vs. LTC
        const int64_t vwap = getVWap(propertyDesired, aBlock, tokenvwap);
        const arith_uint256 aTotal = (ConvertTo256(propertyDesiredAmount) * ConvertTo256(vwap)) / ConvertTo256(COIN);
        total = ConvertTo64(aTotal);
        // BOOST_TEST_MESSAGE("vwap:" << vwap);
        // BOOST_TEST_MESSAGE("propertyDesiredAmount:" << propertyDesiredAmount);
        // BOOST_TEST_MESSAGE("total:" << total);

        // increment cumulative LTC volume by tokens traded * the 12-block VWAP
        MapLTCVolume[aBlock][propertyDesired] += total;

    }

    return total;

}

BOOST_AUTO_TEST_CASE(getvwap_function)
{
    const uint32_t propertyId = 3;
    // actual block
    const int aBlock = 10250;

    // adding  amount *  price
    tokenvwap[propertyId][aBlock - 1].push_back(std::make_pair(2000 * COIN, 1500 * COIN));
    tokenvwap[propertyId][aBlock - 2].push_back(std::make_pair(2000 * COIN, 1600 * COIN));
    tokenvwap[propertyId][aBlock - 3].push_back(std::make_pair(2000 * COIN, 1700 * COIN));
    tokenvwap[propertyId][aBlock - 3].push_back(std::make_pair(2000 * COIN, 1800 * COIN));
    tokenvwap[propertyId][aBlock - 4].push_back(std::make_pair(2000 * COIN, 1900 * COIN));

    // adding some volume
    MapLTCVolume[aBlock - 1][propertyId] += 2000 * COIN;
    MapLTCVolume[aBlock - 2][propertyId] += 1000 * COIN;
    MapLTCVolume[aBlock - 3][propertyId] += 3000 * COIN;
    MapLTCVolume[aBlock - 4][propertyId] += 4000 * COIN;

    // checking vwap:   17000000 / 10000 = 1700
    BOOST_CHECK_EQUAL(1700 * COIN, lgetVWap(propertyId, aBlock, tokenvwap));

    // cleaning maps
    tokenvwap.clear();
    MapLTCVolume.clear();
}

BOOST_AUTO_TEST_CASE(increase_ltc_volume_function)
{
    const uint32_t propertyId = 1;
    const uint32_t propertyDesired = 2;
    // actual block
    const int aBlock = 10250;

    // adding  amount *  price
    tokenvwap[propertyId][aBlock - 10].push_back(std::make_pair(1000 * COIN, 1500 * COIN));
    tokenvwap[propertyId][aBlock - 10].push_back(std::make_pair(3000 * COIN, 1500 * COIN));

    tokenvwap[propertyDesired][aBlock - 10].push_back(std::make_pair(1000 * COIN, 1500 * COIN));
    tokenvwap[propertyDesired][aBlock - 10].push_back(std::make_pair(3000 * COIN, 1500 * COIN));

    // adding some volume
    MapLTCVolume[aBlock - 10][propertyId] = 2000 * COIN;
    MapLTCVolume[aBlock - 10][propertyDesired] = 2000 * COIN;

    // 12-vwap * amountdesired = 3000 * 2000 = 6000000
    BOOST_CHECK_EQUAL(6000000 * COIN, lincreaseLTCVolume(propertyId, propertyDesired, aBlock));

}

BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: patrickdugan/BlockPo-to-Tradelayer

max_issues_repo_name: patrickdugan/BlockPo-to-Tradelayer

max_forks_repo_name: patrickdugan/BlockPo-to-Tradelayer


text:
/**********************************************************************
 *  Copyright (c) 2008-2016, Alliance for Sustainable Energy.
 *  All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 **********************************************************************/

#ifndef SHAREDGUICOMPONENTS_BUILDINGCOMPONENTDIALOGCENTRALWIDGET_HPP
#define SHAREDGUICOMPONENTS_BUILDINGCOMPONENTDIALOGCENTRALWIDGET_HPP

#include <QWidget>

#include <set>
#include <vector>

#include <boost/optional.hpp>

class QProgressBar;

namespace openstudio {

class BCLComponent;
class BCLMeasure;
class Component;
class ComponentList;
class CollapsibleComponentList;

class BuildingComponentDialogCentralWidget : public QWidget
{
  Q_OBJECT

public:
  BuildingComponentDialogCentralWidget(QWidget * parent = nullptr);
  BuildingComponentDialogCentralWidget(int tid, QWidget * parent = nullptr);
  virtual ~BuildingComponentDialogCentralWidget() {}
  int tid();
  void setTid(const std::string& filterType,
    int tids,
    int pageIdx,
    const QString & title,
    const QString & string = "");
  Component * checkedComponent() const;
  int pageIdx();
  bool showNewComponents();
  void setShowNewComponents(bool showNewComponents);

protected:

private:
  void createLayout();
  void init();
  void setTid();

  int m_tid;
  CollapsibleComponentList * m_collapsibleComponentList;
  ComponentList * m_componentList; // TODO cruft to be removed
  QProgressBar * m_progressBar;
  std::set<std::string> m_pendingDownloads;
  std::string m_filterType;
  int m_pageIdx;
  QString m_searchString;
  bool m_showNewComponents;

signals:
  void headerClicked(bool checked);
  void componentClicked(bool checked);
  void collapsibleComponentClicked(bool checked);
  void getComponentsByPage(int pageNum);
  void componentsReady();
  void noComponents();
  void requestComponents(const std::string& filterType, int tids, int pageIdx, const QString & searchString);

private slots:
  void upperPushButtonClicked();
  void lowerPushButtonClicked();
  void comboBoxIndexChanged(const QString & text);
  void on_headerClicked(bool checked);
  void componentDownloadComplete(const std::string& uid, const boost::optional<BCLComponent>& component);
  void measureDownloadComplete(const std::string& uid, const boost::optional<BCLMeasure>& measure);
  void on_componentClicked(bool checked);
  void on_collapsibleComponentClicked(bool checked);
  void on_getComponentsByPage(int pageIdx);

};

} // namespace openstudio

#endif // SHAREDGUICOMPONENTS_BUILDINGCOMPONENTDIALOGCENTRALWIDGET_HPP

ext: hpp

max_stars_repo_name: jasondegraw/OpenStudio

max_issues_repo_name: jasondegraw/OpenStudio

max_forks_repo_name: jasondegraw/OpenStudio


text:
/** mmap_perf.cc                                 -*- C++ -*-
    Rémi Attab, 25 Jan 2013
    Copyright (c) 2013 Datacratic.  All rights reserved.

    Performance tests for DasDB.

*/

#include "mmap_perf_utils.h"
#include "mmap/mmap_file.h"
#include "mmap/mmap_map.h"
#include "soa/utils/threaded_test.h"
#include "jml/utils/guard.h"
#include "jml/utils/filter_streams.h"
#include "jml/arch/timers.h"

#include <boost/program_options/cmdline.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/positional_options.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
#include <map>

using namespace std;
using namespace ML;
using namespace Datacratic;
using namespace Datacratic::MMap;

/******************************************************************************/
/* TEST FUNCTIONS                                                             */
/******************************************************************************/

template<typename Key, typename Value>
size_t findFn(
        unsigned id,
        unsigned iteration,
        size_t workingSet,
        Map<Key, Value>& map,
        Generator<Key>& keyGen,
        Generator<Value>& valueGen)
{
    const size_t ops = 1000;

    Partition<Key> keyPart(keyGen, id * workingSet, workingSet);
    Partition<Value> valuePart(valueGen, id * workingSet, workingSet);

    auto version = map.current();

    for (size_t i = 0; i < ops; ++i) {
        size_t index = iteration * ops + i;
        auto it = version.find(keyPart[index]);
        ExcAssert(it != version.end());
        ExcAssertEqual(it.value(), valuePart[index]);
    }

    version.forceUnlock();

    return ops;
}

template<typename Key, typename Value>
size_t writeFn(
        unsigned id,
        unsigned iteration,
        size_t workingSet,
        Map<Key, Value>& map,
        Generator<Key>& keyGen,
        Generator<Value>& valueGen)
{
    const size_t ops = 100;

    Partition<Key> keyPart(keyGen, id * workingSet, workingSet);
    Partition<Value> valuePart(valueGen, id * workingSet, workingSet);

    for (size_t i = 0; i < ops; ++i) {
        size_t index = iteration * ops + i;
        auto ret = map.insert(keyPart[index], valuePart[index]);
        ExcAssert(ret.second);
        ExcAssertEqual(ret.first.value(), valuePart[index]);
    }

    if (!iteration) return ops;

    for (size_t i = 0; i < ops; ++i) {
        size_t index = (iteration - 1) * ops + i;
        bool success = map.remove(keyPart[index]);
        ExcAssert(success);
    }

    map.forceUnlock();

    return ops * 2;
}

template<typename Key, typename Value>
size_t txWriteFn(
        unsigned id,
        unsigned iteration,
        size_t workingSet,
        size_t txSize,
        Map<Key, Value>& map,
        Generator<Key>& keyGen,
        Generator<Value>& valueGen,
        TransactionStats& txStats)
{
    static __thread size_t it;
    if (!iteration) it = 0;

    const size_t ops = txSize / 2;
    size_t totalOps = 0;

    Partition<Key> keyPart(keyGen, id * workingSet, workingSet);
    Partition<Value> valuePart(valueGen, id * workingSet, workingSet);

    auto tx = map.transaction();

    while (true) {
        for (size_t i = 0; i < ops; ++i) {
            size_t index = it * ops + i;
            auto ret = tx.insert(keyPart[index], valuePart[index]);
            ExcAssert(ret.second);
            ExcAssertEqual(ret.first.value(), valuePart[index]);
            totalOps++;
        }

        if (it) {
            for (size_t i = 0; i < ops; ++i) {
                size_t index = (it - 1) * ops + i;
                bool success = tx.remove(keyPart[index]);
                ExcAssert(success);
                totalOps++;
            }
        }

        it++;

        Timer tm;

        if (tx.tryCommit()) {
            txStats.success(tm.elapsed_wall());
            return totalOps;
        }

        txStats.fail(tm.elapsed_wall());
    }

    tx.forceUnlock();
}

/******************************************************************************/
/* SETUP                                                                      */
/******************************************************************************/

struct Test
{
    Test() :
        initSize(100000), workingSet(100000), sort(false),
        durationMs(1000), txSize(0), readThreads(1), writeThreads(1),
        ops("fi"), csvReport(false)
    {}

    string keyFile;
    size_t initSize;
    size_t workingSet;
    bool sort;

    unsigned durationMs;
    size_t txSize;
    size_t readThreads;
    size_t writeThreads;

    string ops; // r|f = find, w|i = insert/remove, r = replace

    bool csvReport;
    string variable;

    TransactionStats txStats;

    template<typename Key, typename Value>
    void run(bool verbose)
    {
        size_t dataset = (readThreads + writeThreads) * workingSet + initSize;

        if (verbose) cerr << "Generating values: " << fmtValue(dataset) << endl;

        Generator<Value> valueGen(dataset);
        valueGen.load();

        if (verbose) {
            cerr << "Generating keys: "
                << fmtValue(dataset) << ", " << keyFile << endl;
        }

        Generator<Key> keyGen(dataset);
        if (keyFile.empty()) keyGen.load();
        else {
            ML::filter_istream is(keyFile);
            keyGen.load(is);
        }

        if (verbose && sort) cerr << "Sorting keys" << endl;
        if (sort) keyGen.sort();

        MMapFile dasdb(RES_CREATE);
        ML::Call_Guard guard([&] { dasdb.unlink(); });

        Map<Key, Value> map(&dasdb, 1);

        ThreadedTimedTest test;
        std::map<unsigned, string> titles;
        unsigned id = 0;

        for (size_t i = 0; i < ops.size(); ++i) {
            switch(ops[i]) {

            case 'r': // read
            case 'f': // find
            {
                if (verbose) {
                    cerr << "find: "
                        << readThreads << ", " << fmtValue(workingSet)
                        << endl;
                }

                auto tx = map.transaction();
                for (size_t th = 0; th < readThreads; ++th, ++id) {
                    for (size_t j = 0; j < workingSet; ++j) {
                        int index = id * workingSet + j;
                        tx.insert(keyGen[index], valueGen[index]);
                    }
                }
                tx.commit();
                map.forceUnlock();

                auto fn = bind(
                        findFn<Key,Value>, _1, _2,  workingSet,
                        ref(map), ref(keyGen), ref(valueGen));
                int gr = test.add(fn, readThreads);
                titles[gr] = "find";
                break;
            }

            case 'w': // write
            case 'i': // insert
            {
                if (txSize) {
                    if (verbose) {
                        cerr << "tx-write: "
                            << writeThreads << ", "
                            << fmtValue(workingSet) << ", "
                            << fmtValue(txSize)
                            << endl;
                    }

                    auto fn = bind(
                            txWriteFn<Key,Value>, _1, _2, workingSet, txSize,
                            ref(map), ref(keyGen), ref(valueGen), ref(txStats));
                    int gr = test.add(fn, writeThreads);
                    titles[gr] = "tx-write";
                }
                else {
                    if (verbose) {
                        cerr << "write: "
                            << writeThreads << ", "
                            << fmtValue(workingSet) << ", "
                            << endl;
                    }

                    auto fn = bind(
                            writeFn<Key,Value>, _1, _2, workingSet,
                            ref(map), ref(keyGen), ref(valueGen));
                    int gr = test.add(fn, writeThreads);
                    titles[gr] = "write";
                }
                id += writeThreads;
                break;
            }

            default:
                cerr << "ERROR: Operand "
                    << ops[i] << " is not yet supported."
                    << endl;
            }
        }

        if (verbose) cerr << "init: " << fmtValue(initSize) << endl;
        Partition<Key> keyPart(keyGen, id * workingSet, initSize);
        Partition<Value> valuePart(valueGen, id * workingSet, initSize);

        auto tx = map.transaction();
        for (size_t i = 0; i < initSize; ++i) {
            auto ret = tx.insert(keyPart[i], valuePart[i]);
            ExcAssert(ret.second);
            ExcAssert(ret.first != tx.end());
        }
        tx.commit();
        map.forceUnlock();

        if (verbose) cerr << "running: " << fmtElapsed(durationMs / 1000.0) << endl;

        txStats.reset();

        test.run(durationMs);
        report(test, titles);
    }

    void reportHeader()
    {
        char v = variable[0];
        if ('w' != v) cerr << fmtValue(writeThreads) << " write threads" << endl;
        if ('r' != v) cerr << fmtValue(readThreads) << " read threads" << endl;
        if ('i' != v) cerr << fmtValue(initSize) << " init size" << endl;
        if ('z' != v) cerr << fmtValue(workingSet) << " working set" << endl;
        if ('x' != v && txSize)
            cerr << fmtValue(txSize) << " transaction size" << endl;

        printf("\n");

        if (!variable.empty()) {
            string title;

            switch(variable[0]) {

            case 'w': title = "writeTh"; break;
            case 'r': title = "readTh"; break;

            case 'i': title = "initSz"; break;
            case 'z': title = "WSet"; break;
            case 'x': title = "txSz"; break;

            }

            printf("%10s ", title.c_str());
        }

        for (int i = 0; i < ops.size(); ++i)
            printf("%10s %10s %10s ", "op", "sec/ops", "ops/sec");

        if (txSize) printf("%10s %10s", "cm rate", "sec/cm");

        printf("\n\n");
    }

    void report(
            ThreadedTimedTest& results,
            const std::map<unsigned, string>& titles)
    {
        if (!variable.empty()) {
            string val;

            switch(variable[0]) {

            case 'w': val = to_string(writeThreads); break;
            case 'r': val = to_string(readThreads); break;

            case 'i': val = fmtValue(initSize); break;
            case 'z': val = fmtValue(workingSet); break;
            case 'x': val = fmtValue(txSize); break;

            }

            printf("%10s ", val.c_str());
        }

        for(const auto& entry : titles) {
            unsigned id;
            string title;
            tie(id, title) = entry;
            auto dists = results.distributions(id);

            double latency = dists.first.mean();
            double throughput = accumulate(
                    dists.second.begin(), dists.second.end(), 0.0);

            printf("%10s %10s %10s ",
                    title.c_str(),
                    fmtElapsed(latency).c_str(),
                    fmtValue(throughput).c_str());
        }

        if (txSize) {
            printf("%10s %10s",
                    fmtPct(txStats.commitRate()).c_str(),
                    fmtElapsed(txStats.commitLatency()).c_str());
        }

        printf("\n");
    }

};

void runTest(Test& test, bool keyString, bool valueString, bool verbose)
{
    if (verbose) {
        cerr << "Test<"
            << (keyString ? "string" : "int") << ", "
            << (valueString ? "string" : "int") << ">"
            << endl;
    }

    if (keyString && valueString)
        test.run<string, string>(verbose);

    else if (!keyString && valueString)
        test.run<uint64_t, string>(verbose);

    else if (keyString && !valueString)
        test.run<string, uint64_t>(verbose);

    else test.run<uint64_t, uint64_t>(verbose);
}

void runAutoTest(
        Test& test, size_t start, size_t stop, size_t step,
        bool keyString, bool valueString, bool verbose)
{
    function<size_t(size_t)> stepFn;
    if (step)
        stepFn = [=] (size_t i) { return i + step; };
    else stepFn = [] (size_t i) { return i * 2; };

    for (int i = start; i <= stop; i = stepFn(i)) {
        switch(test.variable[0]) {

        case 'w': test.writeThreads = i; break;
        case 'r': test.readThreads = i; break;

        case 'i': test.initSize = i; break;
        case 'z': test.workingSet = i; break;
        case 'x': test.txSize = i; break;

        default: cerr << "ERROR: Unknown variable: " << test.variable[0] << endl;

        }

        runTest(test, keyString, valueString, verbose);
    }
}

/******************************************************************************/
/* MAIN                                                                       */
/******************************************************************************/

int main(int argc, char** argv)
{
    using namespace boost::program_options;

    Test test;

    options_description datasetOptions("Dataset");
    datasetOptions.add_options()
        ("string-keys,k", "Use string as keys")
        ("string-values,v", "Use string as values")

        ("working-set,z", value<size_t>(&test.workingSet),
                "Number of keys to modify by each thread during the test")

        ("init-size,s", value<size_t>(&test.initSize),
                "Initial size of the database")

        ("key-file,f", value<string>(&test.keyFile),
                "File containing the keys to be used in the test");

    string accessPattern = "rnd"; // || "seq"

    options_description testOptions("Manual Testing");
    testOptions.add_options()
        ("duration,d", value<unsigned>(&test.durationMs),
                "length of each tests in ms.")

        ("write-threads,w", value<size_t>(&test.writeThreads),
                "Number of write threads")

        ("read-threads,r", value<size_t>(&test.readThreads),
                "Number of read threads")

        ("tx-size,x", value<size_t>(&test.txSize),
                "Use transactions of this size during the test")

        ("ops,o", value<string>(&test.ops),
                "The ops to run for the test")

        ("access,a", value<string>(&accessPattern),
                "Data access pattern");

    // w = writeTh, r = readTh, i = initSize, w = workingSet, x = txSize
    size_t autoStart = 1;
    size_t autoStop = 32;
    size_t autoSteps = 0;

    options_description autoOptions("Automatic testing");
    autoOptions.add_options()
        ("var", value<string>(&test.variable),
                "The value to act as a variable for the test")
        ("start", value<size_t>(&autoStart), "Value to start scaling at")
        ("stop", value<size_t>(&autoStop), "Value to stop scaling at")
        ("step", value<size_t>(&autoSteps),
                "Value increments. If 0, exponential growth is used");

    options_description allOptions;
    allOptions
        .add(datasetOptions)
        .add(testOptions)
        .add(autoOptions)
        .add_options()
        ("help,h", "Print this message")
        ("verbose", "Dumps lots of debugging information");

    variables_map vm;
    store(command_line_parser(argc, argv).options(allOptions).run(), vm);
    notify(vm);

    if (vm.count("help")) {
        cerr << allOptions << endl;
        return 1;
    }

    if (accessPattern == "seq") test.sort = true;

    bool stringKeys = vm.count("string-keys");
    bool stringValues = vm.count("string-values");
    bool verbose = vm.count("verbose");

    test.reportHeader();

    Timer tm;

    if (test.variable.empty())
        runTest(test, stringKeys, stringValues, verbose);
    else
        runAutoTest(
                test, autoStart, autoStop, autoSteps,
                stringKeys, stringValues, verbose);

    printf("\n%10s elapsed\n", fmtElapsed(tm.elapsed_wall()).c_str());
}

ext: cc

max_stars_repo_name: datacratic/DasDB

max_issues_repo_name: datacratic/DasDB

max_forks_repo_name: datacratic/DasDB


text:
#ifndef BOOST_THREAD_PTHREAD_PTHREAD_HELPERS_HPP
#define BOOST_THREAD_PTHREAD_PTHREAD_HELPERS_HPP
// Copyright (C) 2017
// Vicente J. Botet Escriba
//
//  Distributed under the Boost Software License, Version 1.0. (See
//  accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)

#include <boost/thread/detail/config.hpp>
#include <boost/throw_exception.hpp>
#include <pthread.h>
#include <errno.h>

#include <boost/config/abi_prefix.hpp>

#ifndef BOOST_THREAD_HAS_NO_EINTR_BUG
#define BOOST_THREAD_HAS_EINTR_BUG
#endif

namespace boost
{
  namespace posix
  {
    BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
    int pthread_mutex_init(pthread_mutex_t* m, const pthread_mutexattr_t* attr = NULL)
    {
      return ::pthread_mutex_init(m, attr);
    }

    BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
    int pthread_cond_init(pthread_cond_t* c)
    {
#ifdef BOOST_THREAD_INTERNAL_CLOCK_IS_MONO
      pthread_condattr_t attr;
      int res = pthread_condattr_init(&attr);
      if (res)
      {
        return res;
      }
      BOOST_VERIFY(!pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
      res = ::pthread_cond_init(c, &attr);
      BOOST_VERIFY(!pthread_condattr_destroy(&attr));
      return res;
#else
      return ::pthread_cond_init(c, NULL);
#endif
    }

#ifdef BOOST_THREAD_HAS_EINTR_BUG
    BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
    int pthread_mutex_destroy(pthread_mutex_t* m)
    {
      int ret;
      do
      {
          ret = ::pthread_mutex_destroy(m);
      } while (ret == EINTR);
      return ret;
    }

    BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
    int pthread_cond_destroy(pthread_cond_t* c)
    {
      int ret;
      do
      {
          ret = ::pthread_cond_destroy(c);
      } while (ret == EINTR);
      return ret;
    }

    BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
    int pthread_mutex_lock(pthread_mutex_t* m)
    {
      int ret;
      do
      {
          ret = ::pthread_mutex_lock(m);
      } while (ret == EINTR);
      return ret;
    }

    BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
    int pthread_mutex_trylock(pthread_mutex_t* m)
    {
      int ret;
      do
      {
          ret = ::pthread_mutex_trylock(m);
      } while (ret == EINTR);
      return ret;
    }

    BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
    int pthread_mutex_unlock(pthread_mutex_t* m)
    {
      int ret;
      do
      {
          ret = ::pthread_mutex_unlock(m);
      } while (ret == EINTR);
      return ret;
    }

    BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
    int pthread_cond_wait(pthread_cond_t* c, pthread_mutex_t* m)
    {
      int ret;
      do
      {
          ret = ::pthread_cond_wait(c, m);
      } while (ret == EINTR);
      return ret;
    }

    BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
    int pthread_cond_timedwait(pthread_cond_t* c, pthread_mutex_t* m, const struct timespec* t)
    {
      int ret;
      do
      {
          ret = ::pthread_cond_timedwait(c, m, t);
      } while (ret == EINTR);
      return ret;
    }
#else
    BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
    int pthread_mutex_destroy(pthread_mutex_t* m)
    {
      return ::pthread_mutex_destroy(m);
    }

    BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
    int pthread_cond_destroy(pthread_cond_t* c)
    {
      return ::pthread_cond_destroy(c);
    }

    BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
    int pthread_mutex_lock(pthread_mutex_t* m)
    {
      return ::pthread_mutex_lock(m);
    }

    BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
    int pthread_mutex_trylock(pthread_mutex_t* m)
    {
      return ::pthread_mutex_trylock(m);
    }

    BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
    int pthread_mutex_unlock(pthread_mutex_t* m)
    {
      return ::pthread_mutex_unlock(m);
    }

    BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
    int pthread_cond_wait(pthread_cond_t* c, pthread_mutex_t* m)
    {
      return ::pthread_cond_wait(c, m);
    }

    BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
    int pthread_cond_timedwait(pthread_cond_t* c, pthread_mutex_t* m, const struct timespec* t)
    {
      return ::pthread_cond_timedwait(c, m, t);
    }
#endif

    BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
    int pthread_cond_signal(pthread_cond_t* c)
    {
      return ::pthread_cond_signal(c);
    }

    BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
    int pthread_cond_broadcast(pthread_cond_t* c)
    {
      return ::pthread_cond_broadcast(c);
    }
  }
}

#include <boost/config/abi_suffix.hpp>

#endif


text:
/*
    Copyright 2005-2007 Adobe Systems Incorporated

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

    See http://opensource.adobe.com/gil for most recent version including documentation.
*/

/*************************************************************************************************/

#ifndef GIL_TYPEDEFS_H
#define GIL_TYPEDEFS_H

////////////////////////////////////////////////////////////////////////////////////////
/// \file
/// \brief Useful typedefs
/// \author Lubomir Bourdev and Hailin Jin \n
///         Adobe Systems Incorporated
/// \date 2005-2007 \n Last updated on March 8, 2006
///
////////////////////////////////////////////////////////////////////////////////////////

#include "gil_config.hpp"
#include <boost/cstdint.hpp>
#include "gray.hpp"
#include "rgb.hpp"
#include "rgba.hpp"
#include "cmyk.hpp"
#include "device_n.hpp"
#include <memory>

// CS = 'bgr' LAYOUT='bgr_layout_t'
#define GIL_DEFINE_BASE_TYPEDEFS_INTERNAL(T,CS,LAYOUT)                                              \
    template <typename, typename>    struct pixel;                                                \
    template <typename, typename>    struct planar_pixel_reference;                                            \
    template <typename, typename>    struct planar_pixel_iterator;                                            \
    template <typename>                class memory_based_step_iterator;                                    \
    template <typename>                class point2;                                                \
    template <typename>                class memory_based_2d_locator;                                    \
    template <typename>                class image_view;                                            \
    template <typename, bool, typename>    class image;                                                \
    typedef pixel<bits##T, LAYOUT >                        CS##T##_pixel_t;        \
    typedef const pixel<bits##T, LAYOUT >                   CS##T##c_pixel_t;        \
    typedef pixel<bits##T, LAYOUT >&                      CS##T##_ref_t;            \
    typedef const pixel<bits##T, LAYOUT >&                CS##T##c_ref_t;            \
    typedef CS##T##_pixel_t*                                               CS##T##_ptr_t;            \
    typedef CS##T##c_pixel_t*                                               CS##T##c_ptr_t;            \
    typedef memory_based_step_iterator<CS##T##_ptr_t>                               CS##T##_step_ptr_t;        \
    typedef memory_based_step_iterator<CS##T##c_ptr_t>                               CS##T##c_step_ptr_t;    \
    typedef memory_based_2d_locator<memory_based_step_iterator<CS##T##_ptr_t> >       CS##T##_loc_t;            \
    typedef memory_based_2d_locator<memory_based_step_iterator<CS##T##c_ptr_t> >       CS##T##c_loc_t;            \
    typedef memory_based_2d_locator<memory_based_step_iterator<CS##T##_step_ptr_t> >  CS##T##_step_loc_t;        \
    typedef memory_based_2d_locator<memory_based_step_iterator<CS##T##c_step_ptr_t> > CS##T##c_step_loc_t;    \
    typedef image_view<CS##T##_loc_t>                                        CS##T##_view_t;            \
    typedef image_view<CS##T##c_loc_t>                                        CS##T##c_view_t;        \
    typedef image_view<CS##T##_step_loc_t>                                    CS##T##_step_view_t;    \
    typedef image_view<CS##T##c_step_loc_t>                                   CS##T##c_step_view_t;    \
    typedef image<CS##T##_pixel_t,false,std::allocator<unsigned char> >           CS##T##_image_t;

// CS = 'bgr' CS_FULL = 'rgb_t' LAYOUT='bgr_layout_t'
#define GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(T,CS,CS_FULL,LAYOUT)                                                                \
    GIL_DEFINE_BASE_TYPEDEFS_INTERNAL(T,CS,LAYOUT)                                                                    \
    typedef planar_pixel_reference<bits##T&,CS_FULL >                                          CS##T##_planar_ref_t;        \
    typedef planar_pixel_reference<const bits##T&,CS_FULL >                                      CS##T##c_planar_ref_t;        \
    typedef planar_pixel_iterator<bits##T*,CS_FULL >                                          CS##T##_planar_ptr_t;        \
    typedef planar_pixel_iterator<const bits##T*,CS_FULL >                                      CS##T##c_planar_ptr_t;        \
    typedef memory_based_step_iterator<CS##T##_planar_ptr_t>                              CS##T##_planar_step_ptr_t;    \
    typedef memory_based_step_iterator<CS##T##c_planar_ptr_t>                              CS##T##c_planar_step_ptr_t;    \
    typedef memory_based_2d_locator<memory_based_step_iterator<CS##T##_planar_ptr_t> >          CS##T##_planar_loc_t;        \
    typedef memory_based_2d_locator<memory_based_step_iterator<CS##T##c_planar_ptr_t> >      CS##T##c_planar_loc_t;        \
    typedef memory_based_2d_locator<memory_based_step_iterator<CS##T##_planar_step_ptr_t> >  CS##T##_planar_step_loc_t;    \
    typedef memory_based_2d_locator<memory_based_step_iterator<CS##T##c_planar_step_ptr_t> > CS##T##c_planar_step_loc_t;    \
    typedef image_view<CS##T##_planar_loc_t>                                      CS##T##_planar_view_t;        \
    typedef image_view<CS##T##c_planar_loc_t>                                      CS##T##c_planar_view_t;        \
    typedef image_view<CS##T##_planar_step_loc_t>                                  CS##T##_planar_step_view_t;    \
    typedef image_view<CS##T##c_planar_step_loc_t>                                  CS##T##c_planar_step_view_t;\
    typedef image<CS##T##_pixel_t,true,std::allocator<unsigned char> >              CS##T##_planar_image_t;

#define GIL_DEFINE_BASE_TYPEDEFS(T,CS)        \
    GIL_DEFINE_BASE_TYPEDEFS_INTERNAL(T,CS,CS##_layout_t)

#define GIL_DEFINE_ALL_TYPEDEFS(T,CS)         \
    GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(T,CS,CS##_t,CS##_layout_t)

namespace boost { namespace gil {

// forward declarations
template <typename B, typename Mn, typename Mx> struct scoped_channel_value;
struct float_zero;
struct float_one;
typedef scoped_channel_value<float,float_zero,float_one> bits32f;
typedef uint8_t  bits8;
typedef uint16_t bits16;
typedef uint32_t bits32;
typedef int8_t   bits8s;
typedef int16_t  bits16s;
typedef int32_t  bits32s;

GIL_DEFINE_BASE_TYPEDEFS(8,  gray)
GIL_DEFINE_BASE_TYPEDEFS(8s, gray)
GIL_DEFINE_BASE_TYPEDEFS(16, gray)
GIL_DEFINE_BASE_TYPEDEFS(16s,gray)
GIL_DEFINE_BASE_TYPEDEFS(32 ,gray)
GIL_DEFINE_BASE_TYPEDEFS(32s,gray)
GIL_DEFINE_BASE_TYPEDEFS(32f,gray)
GIL_DEFINE_BASE_TYPEDEFS(8,  bgr)
GIL_DEFINE_BASE_TYPEDEFS(8s, bgr)
GIL_DEFINE_BASE_TYPEDEFS(16, bgr)
GIL_DEFINE_BASE_TYPEDEFS(16s,bgr)
GIL_DEFINE_BASE_TYPEDEFS(32 ,bgr)
GIL_DEFINE_BASE_TYPEDEFS(32s,bgr)
GIL_DEFINE_BASE_TYPEDEFS(32f,bgr)
GIL_DEFINE_BASE_TYPEDEFS(8,  argb)
GIL_DEFINE_BASE_TYPEDEFS(8s, argb)
GIL_DEFINE_BASE_TYPEDEFS(16, argb)
GIL_DEFINE_BASE_TYPEDEFS(16s,argb)
GIL_DEFINE_BASE_TYPEDEFS(32, argb)
GIL_DEFINE_BASE_TYPEDEFS(32s,argb)
GIL_DEFINE_BASE_TYPEDEFS(32f,argb)
GIL_DEFINE_BASE_TYPEDEFS(8,  abgr)
GIL_DEFINE_BASE_TYPEDEFS(8s, abgr)
GIL_DEFINE_BASE_TYPEDEFS(16, abgr)
GIL_DEFINE_BASE_TYPEDEFS(16s,abgr)
GIL_DEFINE_BASE_TYPEDEFS(32 ,abgr)
GIL_DEFINE_BASE_TYPEDEFS(32s,abgr)
GIL_DEFINE_BASE_TYPEDEFS(32f,abgr)
GIL_DEFINE_BASE_TYPEDEFS(8,  bgra)
GIL_DEFINE_BASE_TYPEDEFS(8s, bgra)
GIL_DEFINE_BASE_TYPEDEFS(16, bgra)
GIL_DEFINE_BASE_TYPEDEFS(16s,bgra)
GIL_DEFINE_BASE_TYPEDEFS(32 ,bgra)
GIL_DEFINE_BASE_TYPEDEFS(32s,bgra)
GIL_DEFINE_BASE_TYPEDEFS(32f,bgra)

GIL_DEFINE_ALL_TYPEDEFS(8,  rgb)
GIL_DEFINE_ALL_TYPEDEFS(8s, rgb)
GIL_DEFINE_ALL_TYPEDEFS(16, rgb)
GIL_DEFINE_ALL_TYPEDEFS(16s,rgb)
GIL_DEFINE_ALL_TYPEDEFS(32 ,rgb)
GIL_DEFINE_ALL_TYPEDEFS(32s,rgb)
GIL_DEFINE_ALL_TYPEDEFS(32f,rgb)
GIL_DEFINE_ALL_TYPEDEFS(8,  rgba)
GIL_DEFINE_ALL_TYPEDEFS(8s, rgba)
GIL_DEFINE_ALL_TYPEDEFS(16, rgba)
GIL_DEFINE_ALL_TYPEDEFS(16s,rgba)
GIL_DEFINE_ALL_TYPEDEFS(32 ,rgba)
GIL_DEFINE_ALL_TYPEDEFS(32s,rgba)
GIL_DEFINE_ALL_TYPEDEFS(32f,rgba)
GIL_DEFINE_ALL_TYPEDEFS(8,  cmyk)
GIL_DEFINE_ALL_TYPEDEFS(8s, cmyk)
GIL_DEFINE_ALL_TYPEDEFS(16, cmyk)
GIL_DEFINE_ALL_TYPEDEFS(16s,cmyk)
GIL_DEFINE_ALL_TYPEDEFS(32 ,cmyk)
GIL_DEFINE_ALL_TYPEDEFS(32s,cmyk)
GIL_DEFINE_ALL_TYPEDEFS(32f,cmyk)

template <int N> struct devicen_t;
template <int N> struct devicen_layout_t;
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(8,  dev2n, devicen_t<2>, devicen_layout_t<2>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(8s, dev2n, devicen_t<2>, devicen_layout_t<2>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(16, dev2n, devicen_t<2>, devicen_layout_t<2>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(16s,dev2n, devicen_t<2>, devicen_layout_t<2>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(32 ,dev2n, devicen_t<2>, devicen_layout_t<2>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(32s,dev2n, devicen_t<2>, devicen_layout_t<2>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(32f,dev2n, devicen_t<2>, devicen_layout_t<2>)

GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(8,  dev3n, devicen_t<3>, devicen_layout_t<3>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(8s, dev3n, devicen_t<3>, devicen_layout_t<3>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(16, dev3n, devicen_t<3>, devicen_layout_t<3>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(16s,dev3n, devicen_t<3>, devicen_layout_t<3>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(32 ,dev3n, devicen_t<3>, devicen_layout_t<3>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(32s,dev3n, devicen_t<3>, devicen_layout_t<3>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(32f,dev3n, devicen_t<3>, devicen_layout_t<3>)

GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(8,  dev4n, devicen_t<4>, devicen_layout_t<4>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(8s, dev4n, devicen_t<4>, devicen_layout_t<4>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(16, dev4n, devicen_t<4>, devicen_layout_t<4>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(16s,dev4n, devicen_t<4>, devicen_layout_t<4>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(32 ,dev4n, devicen_t<4>, devicen_layout_t<4>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(32s,dev4n, devicen_t<4>, devicen_layout_t<4>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(32f,dev4n, devicen_t<4>, devicen_layout_t<4>)

GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(8,  dev5n, devicen_t<5>, devicen_layout_t<5>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(8s, dev5n, devicen_t<5>, devicen_layout_t<5>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(16, dev5n, devicen_t<5>, devicen_layout_t<5>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(16s,dev5n, devicen_t<5>, devicen_layout_t<5>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(32 ,dev5n, devicen_t<5>, devicen_layout_t<5>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(32s,dev5n, devicen_t<5>, devicen_layout_t<5>)
GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(32f,dev5n, devicen_t<5>, devicen_layout_t<5>)

} }  // namespace boost::gil

#endif

max_stars_repo_name: randolphwong/mcsema

max_issues_repo_name: randolphwong/mcsema

max_forks_repo_name: randolphwong/mcsema


text:
//=======================================================================
// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
//
// This file is part of the Boost Graph Library
//
// You should have received a copy of the License Agreement for the
// Boost Graph Library along with the software; see the file LICENSE.
// If not, contact Office of Research, Indiana University,
// Bloomington, IN 47405.
//
// Permission to modify the code and to distribute the code is
// granted, provided the text of this NOTICE is retained, a notice if
// the code was modified is included with the above COPYRIGHT NOTICE
// and with the COPYRIGHT NOTICE in the LICENSE file, and that the
// LICENSE file is distributed with the modified code.
//
// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.
// By way of example, but not limitation, Licensor MAKES NO
// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS
// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS
// OR OTHER RIGHTS.
//=======================================================================
#include <iostream>
#include <boost/graph/edge_list.hpp>
#include <boost/graph/bellman_ford_shortest_paths.hpp>

int
main()
{
  using namespace boost;
  // ID numbers for the routers (vertices).
  enum
  { A, B, C, D, E, F, G, H, n_vertices };
  const int n_edges = 11;
  typedef std::pair < int, int >Edge;

  // The list of connections between routers stored in an array.
  Edge edges[] = {
  Edge(A, B), Edge(A, C),
        Edge(B, D), Edge(B, E), Edge(C, E), Edge(C, F), Edge(D, H),
        Edge(D, E), Edge(E, H), Edge(F, G), Edge(G, H)
  };

  // Specify the graph type and declare a graph object
  typedef edge_list < Edge*, Edge, std::ptrdiff_t, std::random_access_iterator_tag> Graph;
  Graph g(edges, edges + n_edges);

  // The transmission delay values for each edge.
  float delay[] =
    {5.0, 1.0, 1.3, 3.0, 10.0, 2.0, 6.3, 0.4, 1.3, 1.2, 0.5};

  // Declare some storage for some "external" vertex properties.
  char name[] = "ABCDEFGH";
  int parent[n_vertices];
  for (int i = 0; i < n_vertices; ++i)
    parent[i] = i;
  float distance[n_vertices];
  std::fill(distance, distance + n_vertices, std::numeric_limits < float >::max());
  // Specify A as the source vertex
  distance[A] = 0;

  bool r = bellman_ford_shortest_paths(g, int (n_vertices),
                                       weight_map(make_iterator_property_map
                                                  (&delay[0],
                                                   get(edge_index, g),
                                                   delay[0])).
                                       distance_map(&distance[0]).
                                       predecessor_map(&parent[0]));

  if (r)
    for (int i = 0; i < n_vertices; ++i)
      std::cout << name[i] << ": " << distance[i]
        << " " << name[parent[i]] << std::endl;
  else
    std::cout << "negative cycle" << std::endl;

  return EXIT_SUCCESS;
}


text:
#include <marlin/beacon/DiscoveryServer.hpp>
#include <marlin/asyncio/core/EventLoop.hpp>
#include <cstring>
#include <structopt/app.hpp>
#include <cryptopp/scrypt.h>
#include <cryptopp/modes.h>
#include <cryptopp/keccak.h>
#include <cryptopp/hex.h>
#include <cryptopp/aes.h>
#include <rapidjson/document.h>
#include <boost/filesystem.hpp>

using namespace marlin;

struct BeaconDelegate {
	void new_reg(core::SocketAddress const& addr, beacon::PeerInfo const& peer_info) {
		SPDLOG_INFO(
			"New reg: {}: 0x{:spn}",
			addr.to_string(),
			spdlog::to_hex(peer_info.address.data(), peer_info.address.data()+20)
		);
	}
};

struct CliOptions {
	std::optional<std::string> keystore_path;
	std::optional<std::string> keystore_pass_path;
	std::optional<std::string> discovery_addr;
	std::optional<std::string> heartbeat_addr;
	std::optional<std::string> beacon_addr;
};
STRUCTOPT(CliOptions,  keystore_path, keystore_pass_path, discovery_addr, heartbeat_addr, beacon_addr);

std::string get_key(std::string keystore_path, std::string keystore_pass_path);

int main(int argc, char** argv) {
	try {
		auto options = structopt::app("beacon").parse<CliOptions>(argc, argv);
		std::string key;
		if(options.beacon_addr.has_value()) {
			if(options.keystore_path.has_value() && options.keystore_pass_path.has_value()) {
				key = get_key(options.keystore_path.value(), options.keystore_pass_path.value());
				if (key.empty()) {
					SPDLOG_ERROR("keystore file error");
					return 1;
				}
			} else {
				SPDLOG_ERROR("require keystore and password file");
				return 1;
			}
		}
		auto discovery_addr = core::SocketAddress::from_string(
			options.discovery_addr.value_or("127.0.0.1:8002")
		);
		auto heartbeat_addr = core::SocketAddress::from_string(
			options.heartbeat_addr.value_or("127.0.0.1:8003")
		);
		auto beacon_addr = options.beacon_addr.has_value() ? std::make_optional(core::SocketAddress::from_string(*options.beacon_addr)) : std::nullopt;

		SPDLOG_INFO(
			"Starting beacon with discovery: {}, heartbeat: {}",
			discovery_addr.to_string(),
			heartbeat_addr.to_string()
		);

		BeaconDelegate del;
		beacon::DiscoveryServer<BeaconDelegate> b(discovery_addr, heartbeat_addr, beacon_addr, key);
		b.delegate = &del;

		return asyncio::EventLoop::run();
	} catch (structopt::exception& e) {
		SPDLOG_ERROR("{}", e.what());
		SPDLOG_ERROR("{}", e.help());
	}

	return -1;
}

std::string string_to_hex(const std::string& input)
{
    std::string output;
	CryptoPP::StringSource ss2( input, true,
    new CryptoPP::HexEncoder(
        new CryptoPP::StringSink( output )
    )); // HexEncoder
    return output;
}

std::string hex_to_string(const std::string& input)
{
	std::string output;
	CryptoPP::StringSource ss2( input, true,
    new CryptoPP::HexDecoder(
        new CryptoPP::StringSink( output )
    )); // HexDecoder
    return output;
}

bool isvalid_cipherparams(const rapidjson::Value &cipherparams, const std::string &cipher) {
	if(cipher == "aes-128-ctr") {
		return cipherparams.HasMember("iv") && cipherparams["iv"].IsString();
	}
	return false;
}

bool isvalid_kdfparams(const rapidjson::Value &kdfparams, const std::string &kdf) {
	if(kdf == "scrypt") {
		return kdfparams.HasMember("dklen") && kdfparams["dklen"].IsUint64()
				&& kdfparams.HasMember("n") && kdfparams["n"].IsUint64()
				&& kdfparams.HasMember("p") && kdfparams["p"].IsUint64()
				&& kdfparams.HasMember("r") && kdfparams["r"].IsUint64()
				&& kdfparams.HasMember("salt") && kdfparams["salt"].IsString();
	}
	return false;
}

bool isvalid_keystore(rapidjson::Document &keystore) {
	if (keystore.HasMember("crypto") && keystore["crypto"].IsObject() ) {
		const rapidjson::Value &crypto = keystore["crypto"];
		const rapidjson::Value &cipherparams = crypto["cipherparams"];
		const rapidjson::Value &kdfparams = crypto["kdfparams"];
		return crypto.HasMember("cipher") && crypto["cipher"].IsString()
			&& crypto.HasMember("ciphertext") && crypto["ciphertext"].IsString()
			&& crypto.HasMember("kdf") && crypto["kdf"].IsString()
			&& crypto.HasMember("mac") && crypto["mac"].IsString()
			&& crypto.HasMember("cipherparams") && crypto["cipherparams"].IsObject()
			&& crypto.HasMember("kdfparams") && crypto["kdfparams"].IsObject()
			&& isvalid_cipherparams(cipherparams, crypto["cipher"].GetString())
			&& isvalid_kdfparams(kdfparams, crypto["kdf"].GetString());
	}
	return false;
}

void derivekey_scrypt(CryptoPP::SecByteBlock &derived, const rapidjson::Value &kdfparams, const std::string &pass) {

	CryptoPP::Scrypt pbkdf;
	derived = CryptoPP::SecByteBlock(kdfparams["dklen"].GetUint());
	std::string salt(hex_to_string(kdfparams["salt"].GetString()));
	pbkdf.DeriveKey(derived, derived.size(),
			CryptoPP::ConstBytePtr(pass), CryptoPP::BytePtrSize(pass),
			CryptoPP::ConstBytePtr(salt), CryptoPP::BytePtrSize(salt),
			CryptoPP::word64(kdfparams["n"].GetUint64()),
			CryptoPP::word64(kdfparams["r"].GetUint64()),
			CryptoPP::word64(kdfparams["p"].GetUint64()));
}

std::string decrypt_aes128ctr(const rapidjson::Value &cipherparams, std::string &ciphertext, CryptoPP::SecByteBlock &derived) {
	std::string iv = hex_to_string(cipherparams["iv"].GetString());

	CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption d;
	d.SetKeyWithIV(derived, 16, CryptoPP::ConstBytePtr(iv));

	CryptoPP::SecByteBlock decrypted(ciphertext.size());
	d.ProcessData(decrypted, CryptoPP::ConstBytePtr(ciphertext), CryptoPP::BytePtrSize(ciphertext));
	return std::string((const char*)decrypted.data(), decrypted.size());
}

bool check_mac(const std::string &mac, CryptoPP::SecByteBlock &derived, std::string &ciphertext) {
	CryptoPP::Keccak_256 hasher;
	auto hashinput = CryptoPP::SecByteBlock((derived.data()+16), 16) + CryptoPP::SecByteBlock((const CryptoPP::byte*)ciphertext.data(), ciphertext.size());

	CryptoPP::SecByteBlock hash(mac.size());
	return hasher.VerifyTruncatedDigest((const CryptoPP::byte*)mac.data(), mac.size(), hashinput, hashinput.size());
}

std::string get_key(std::string keystore_path, std::string keystore_pass_path) {
	std::string _pass;
	rapidjson::Document _keystore;

	// read password file
	if(boost::filesystem::exists(keystore_pass_path)) {
		std::ifstream fin(keystore_pass_path);
		std::getline(fin, _pass);
		fin.close();
	}
	if (_pass.empty()) {
		SPDLOG_ERROR("Invalid password file");
		return "";
	}

	// read keystore file
	if(boost::filesystem::exists(keystore_path)) {
		std::string s;
		boost::filesystem::load_string_file(keystore_path, s);
		rapidjson::StringStream ss(s.c_str());
		_keystore.ParseStream(ss);
	}
	if (!isvalid_keystore(_keystore)){
		SPDLOG_ERROR("Invalid keystore file");
		return "";
	}

	const rapidjson::Value &crypto = _keystore["crypto"];
	std::string ciphertext = hex_to_string(crypto["ciphertext"].GetString());
	CryptoPP::SecByteBlock derived;
	std::string decrypted;

	// get derived keycrypto
	if (crypto["kdf"] == "scrypt") {
		derivekey_scrypt(derived, crypto["kdfparams"], _pass);
	}
	if (derived.size() == 0) {
		SPDLOG_ERROR("kdf error");
		return "";
	}
	if (crypto["cipher"] == "aes-128-ctr") {
		decrypted  = decrypt_aes128ctr(crypto["cipherparams"], ciphertext, derived);
	}

	//validate mac
	if (!check_mac(hex_to_string(crypto["mac"].GetString()), derived, ciphertext)) {
		SPDLOG_ERROR("Invalid mac");
		return "";
	}
	SPDLOG_INFO("decrypted keystore");
	return decrypted;
}

max_stars_repo_name: marlinprotocol/OpenWeaver

max_issues_repo_name: marlinpro/openweaver

max_forks_repo_name: marlinpro/openweaver


text: #include <boost/mpl/shift_right.hpp>

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino