text:
/*
* Software License Agreement (BSD License)
*
*  Copyright (c) 2014-2016, Timm Linder, Social Robotics Lab, University of Freiburg
*  All rights reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions are met:
*
*  * Redistributions of source code must retain the above copyright notice, this
*    list of conditions and the following disclaimer.
*  * Redistributions in binary form must reproduce the above copyright notice,
*    this list of conditions and the following disclaimer in the documentation
*    and/or other materials provided with the distribution.
*  * Neither the name of the copyright holder nor the names of its contributors
*    may be used to endorse or promote products derived from this software
*    without specific prior written permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
*  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
*  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
*  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
*  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
*  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
*  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
*  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
*  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <srl_nearest_neighbor_tracker/ros/geometry_utils.h>
#include <srl_nearest_neighbor_tracker/ros/params.h>

#include <eigen_conversions/eigen_msg.h>
#include <tf_conversions/tf_eigen.h>
#include <tf/transform_datatypes.h>

#include <srl_nearest_neighbor_tracker/base/stl_helpers.h>
#include <Eigen/Eigenvalues>

#include <boost/algorithm/string.hpp>

namespace srl_nnt
{
// Static variable definition
GeometryUtils* GeometryUtils::s_instance = NULL;

GeometryUtils::GeometryUtils() : m_transformListener()
{
    ROS_ASSERT_MSG(s_instance == NULL, "Cannot create multiple instances of GeometryUtils singleton!");
    s_instance = this;
}

std::string GeometryUtils::getWorldFrame()
{
    return srl_nnt::Params::get<std::string>("world_frame", "odom");
}

bool GeometryUtils::lookupTransformIntoWorldFrame(ros::Time stamp, const string& sourceFrame, Eigen::Affine3d& resultingTransform)
{
    tf::StampedTransform tfTransform;
    const string targetFrame = getWorldFrame();

    try {
        m_transformListener.waitForTransform(targetFrame, sourceFrame, stamp, ros::Duration(srl_nnt::Params::get<double>("transform_timeout", 0.2) ));
        m_transformListener.lookupTransform(targetFrame, sourceFrame, stamp, tfTransform);
    }
    catch(const tf::TransformException& ex) {
        ROS_ERROR_STREAM_THROTTLE(2.0, "Could not determine transform from observation frame \"" << sourceFrame << "\" into fixed world frame \"" << targetFrame << "\", "
                << "which may lead to observations being dropped. This message will re-appear every 2 seconds. Reason: " << ex.what());
        return false;
    }

    tf::transformTFToEigen(tfTransform, resultingTransform);
    return true;
}

void GeometryUtils::convertDetectedPersonsToObservations(const spencer_tracking_msgs::DetectedPersons::ConstPtr& detectedPersons, Observations& observations)
{
    // Ignore if pointer is not set
    if(!detectedPersons) return;

    // We need to convert all coordinates into our fixed world reference frame
    double currentTime = detectedPersons->header.stamp.toSec();
    Eigen::Affine3d transformIntoWorldFrame;
    if(!lookupTransformIntoWorldFrame(detectedPersons->header.stamp, detectedPersons->header.frame_id, transformIntoWorldFrame)) return;

    // In case we are overriding the detector's measurement noise (see below)
    bool overrideMeasurementNoise = srl_nnt::Params::get<bool>("overwrite_measurement_noise", false);
    const double measurementNoise = srl_nnt::Params::get<double>("measurement_noise", 0.01);
    ObsMatrix newMeasurementNoiseMatrix = ObsMatrix::Identity() * measurementNoise;

    // Convert DetectedPerson instances (ROS messages) into Observation instances (our own format).
    foreach(const spencer_tracking_msgs::DetectedPerson& detectedPerson, detectedPersons->detections)
    {
        // Heuristic sanity check for detected person poses (if anything in the detector goes wrong or groundtruth annotations are invalid)
        if(!posePassesSanityCheck(detectedPerson.pose, false)) {
            ROS_WARN_STREAM("Pose of DetectedPerson " << detectedPerson.detection_id << " does not pass sanity check, will ignore this detection: " << detectedPerson.pose.pose);
            continue;
        }

        Observation::Ptr observation(new Observation);
        observation->id = detectedPerson.detection_id;
        observation->createdAt = currentTime;
        observation->confidence = detectedPerson.confidence;

        vector<std::string> modalities;
        boost::split(modalities, detectedPerson.modality, boost::is_any_of(","));

        foreach(std::string modality, modalities) {
            boost::trim(modality, std::locale(""));
            observation->modalities.insert(modality);
        }

        poseToMeanAndCovariance(detectedPerson.pose, observation->z, observation->R, transformIntoWorldFrame);

        // Overwrite measurement noise if activated
        if(overrideMeasurementNoise) observation->R = newMeasurementNoiseMatrix;

        observations.push_back(observation);

        ROS_DEBUG("Received observation from ROS (ID=%d) at x=%.2f, y=%.2f", (unsigned int) detectedPerson.detection_id, observation->z(0), observation->z(1));
    }

    //ROS_INFO_STREAM("Received " << observations.size() << " observation(s) from ROS!");
}

void GeometryUtils::convertObservationsToDetectedPersons(const double currentTime, const Observations& observations, spencer_tracking_msgs::DetectedPersons& detectedPersons)
{
    detectedPersons.header.stamp = ros::Time(currentTime);
    detectedPersons.header.frame_id = getWorldFrame();

    foreach(Observation::Ptr observation, observations) {
        spencer_tracking_msgs::DetectedPerson detectedPerson;
        detectedPerson.detection_id = observation->id;
        detectedPerson.confidence = observation->confidence;

        StateVector z;
        z(0) = observation->z(0);
        z(1) = observation->z(1);
        for(int i = 2; i < STATE_VISUALIZATION_DIM; i++) z(i) = 0;

        StateMatrix R;
        for(int r = 0; r < STATE_VISUALIZATION_DIM; r++) {
            for(int c = 0; c < STATE_VISUALIZATION_DIM; c++) {
                if(r < 2 && c < 2) R(r,c) = observation->R(r,c);
                else R(r,c) = 0;
            }
        }

        geometry_msgs::TwistWithCovariance unusedTwist;
        meanAndCovarianceToPoseAndTwist(z, R, detectedPerson.pose, unusedTwist);
        detectedPersons.detections.push_back(detectedPerson);
    }
}

void GeometryUtils::meanAndCovarianceToPoseAndTwist(const StateVector& x, const StateMatrix& C, geometry_msgs::PoseWithCovariance& pose, geometry_msgs::TwistWithCovariance& twist)
{
    // Some constants for determining the pose
    const double AVERAGE_ROTATION_VARIANCE = pow(10.0 / 180 * M_PI, 2); // FIXME: determine from vx, vy?
    const double INFINITE_VARIANCE = 9999999; // should not really use infinity here because then the covariance matrix cannot be rotated (singularities!)

    StateVisVector xVis = x.head(STATE_VISUALIZATION_DIM);
    StateVisMatrix CVis = C.block<STATE_VISUALIZATION_DIM, STATE_VISUALIZATION_DIM>(0,0);

    assert(xVis.size() % 2 == 0);
    const int numAxes = xVis.size() / 2; // either 2 or 3
    const double vx = xVis(numAxes+0), vy = xVis(numAxes+1);

    // Set pose (=position + orientation)
    pose.pose.position.x = xVis(0);
    pose.pose.position.y = xVis(1);
    pose.pose.position.z = numAxes > 2 ? xVis(2) : 0.0;
    pose.pose.orientation = tf::createQuaternionMsgFromYaw(atan2(vy, vx)); // determine orientation from current velocity estimate

    pose.covariance.fill(0.0);
    pose.covariance[2 * ROS_COV_DIM + 2] = INFINITE_VARIANCE; // default variance of z position, might get overwritten below if numAxes > 2

    for(int row = 0; row < numAxes; row++) {
       for(int col = 0; col < numAxes; col++) {
           pose.covariance[row * ROS_COV_DIM + col] = CVis(row, col); // copy position covariances
       }
    }

    pose.covariance[3 * ROS_COV_DIM + 3] = INFINITE_VARIANCE; // variance of x rotation
    pose.covariance[4 * ROS_COV_DIM + 4] = INFINITE_VARIANCE; // variance of y rotation
    pose.covariance[5 * ROS_COV_DIM + 5] = AVERAGE_ROTATION_VARIANCE; // variance of z rotation

    // Set twist (=velocities)
    twist.twist.linear.x = xVis(numAxes + 0);
    twist.twist.linear.y = xVis(numAxes + 1);
    twist.twist.linear.z = numAxes > 2 ? xVis(numAxes + 2) : 0.0;

    twist.covariance.fill(0.0);
    twist.covariance[2 * ROS_COV_DIM + 2] = INFINITE_VARIANCE; // default variance of z linear velocity, might get overwritten below if numAxes > 2

    for(int row = 0; row < numAxes; row++) {
       for(int col = 0; col < numAxes; col++) {
           twist.covariance[row * ROS_COV_DIM + col] = CVis(numAxes + row, numAxes + col); // copy velocity covariances
       }
    }

    twist.covariance[3 * ROS_COV_DIM + 3] = INFINITE_VARIANCE; // variance of x angular velocity
    twist.covariance[4 * ROS_COV_DIM + 4] = INFINITE_VARIANCE; // variance of y angular velocity
    twist.covariance[5 * ROS_COV_DIM + 5] = INFINITE_VARIANCE; // variance of z angular velocity
}

void GeometryUtils::poseToMeanAndCovariance(const geometry_msgs::PoseWithCovariance& pose, ObsVector& x, ObsMatrix& C, const Eigen::Affine3d& transformToApply)
{
    // ROS Pose is a 3D position + rotation relative to the message's coordinate frame
    Eigen::Affine3d poseInDetectionFrame;
    tf::poseMsgToEigen(pose.pose, poseInDetectionFrame);

    // ROS Covariance is a 6x6 matrix (xyz + xyz rotation) relative to the message's coordinate frame
    // We are not interested in pose rotation, so only take first 3 rows and columns
    Eigen::Matrix3d covInDetectionFrame;
    for(int row = 0; row < ROS_COV_DIM / 2; row++) {
        for(int col = 0; col < ROS_COV_DIM / 2; col++) {
            covInDetectionFrame(row, col) = pose.covariance[row * ROS_COV_DIM + col];
        }
    }

    // Transform pose and covariance into our reference frame, still in 3D
    Eigen::Affine3d poseInOurFrame = transformToApply * poseInDetectionFrame;
    Eigen::Vector3d positionInOurFrame = poseInOurFrame.translation();

    // For covariance, only the coordinate frame rotation is relevant (invariant w.r.t. translation)
    Eigen::Matrix3d detectionFrameToOurFrameRotation = transformToApply.linear().matrix();
    Eigen::Matrix3d covInOurFrame = detectionFrameToOurFrameRotation * covInDetectionFrame * detectionFrameToOurFrameRotation.transpose();

    // Convert from 3D to 2D by simply dropping the Z coordinate
    assert(OBS_DIM == 2);
    x = positionInOurFrame.head(OBS_DIM);
    C = covInOurFrame.topLeftCorner(OBS_DIM, OBS_DIM);
}

bool GeometryUtils::posePassesSanityCheck(const geometry_msgs::PoseWithCovariance& poseWithCovariance, bool checkOrientation)
{
    const geometry_msgs::Pose& pose = poseWithCovariance.pose;

    // Position
    const float MAX_REASONABLE_DISTANCE_FROM_ORIGIN = 1000.0f; // in meters
    float positionValues[] = { pose.position.x, pose.position.y, pose.position.z };
    for(size_t i = 0; i < sizeof(positionValues) / sizeof(positionValues[0]); i++) {
        if(!isfinite(positionValues[i]) || abs(positionValues[i]) > MAX_REASONABLE_DISTANCE_FROM_ORIGIN) {
            ROS_WARN("Suspicious coordinate value(s) in pose position encountered!");
            return false;
        }
    }

    // Orientation
    if(checkOrientation) {
        float squaredSum = 0;
        float orientationValues[] = { pose.orientation.x, pose.orientation.y, pose.orientation.z, pose.orientation.w };
        for(size_t i = 0; i < sizeof(orientationValues) / sizeof(orientationValues[0]); i++) {
            if(!isfinite(orientationValues[i])) {
                ROS_WARN("Non-finite pose orientation value(s) encountered!");
                return false;
            }
            squaredSum += orientationValues[i] * orientationValues[i];
        }

        if(abs(squaredSum - 1.0) > 0.05) {
            ROS_WARN("Pose orientation quaternion is not a unit quaternion!");
            return false;
        }
    }

    // Positive semi-definiteness of covariance matrix (x, y coordinates only)
    Eigen::Matrix2d cov;
    for(int row = 0; row < 2; row++)
        for(int col = 0; col < 2; col++)
            cov(row, col) = poseWithCovariance.covariance[row * ROS_COV_DIM + col];

    Eigen::Vector2cd eigenvals = cov.eigenvalues();
    bool positiveSemiDefinite = eigenvals(0).real() > 0 && eigenvals(1).real() > 0;
    if(!positiveSemiDefinite) {
        ROS_WARN_STREAM("Pose covariance matrix X,Y part is not positive semi-definite: " << std::endl << cov);
        return false;
    }

    return true;
}

} // end of namespace srl

ext: cpp

max_stars_repo_name: CodeToPoem/HumanAwareRobotNavigation

max_issues_repo_name: dmr-goncalves/HumanAwareRobotNavigation

max_forks_repo_name: dmr-goncalves/HumanAwareRobotNavigation


text:
#ifndef BOOST_MPL_AUX_SINGLE_ELEMENT_ITER_HPP_INCLUDED
#define BOOST_MPL_AUX_SINGLE_ELEMENT_ITER_HPP_INCLUDED

// Copyright Aleksey Gurtovoy 2000-2004
//
// 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)
//
// See http://www.boost.org/libs/mpl for documentation.

// $Source$
// $Date: 2004-11-27 20:58:27 -0500 (Sat, 27 Nov 2004) $
// $Revision: 26325 $

#include <boost/mpl/iterator_tags.hpp>
#include <boost/mpl/advance_fwd.hpp>
#include <boost/mpl/distance_fwd.hpp>
#include <boost/mpl/next_prior.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/aux_/nttp_decl.hpp>
#include <boost/mpl/aux_/value_wknd.hpp>
#include <boost/mpl/aux_/config/ctps.hpp>

namespace boost { namespace mpl {

#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)

namespace aux {

template< typename T, BOOST_MPL_AUX_NTTP_DECL(int, is_last_) >
struct sel_iter;

template< typename T >
struct sel_iter<T,0>
{
    typedef random_access_iterator_tag category;
    typedef sel_iter<T,1> next;
    typedef T type;
};

template< typename T >
struct sel_iter<T,1>
{
    typedef random_access_iterator_tag category;
    typedef sel_iter<T,0> prior;
};

} // namespace aux

template< typename T, BOOST_MPL_AUX_NTTP_DECL(int, is_last_), typename Distance >
struct advance< aux::sel_iter<T,is_last_>,Distance>
{
    typedef aux::sel_iter<
          T
        , ( is_last_ + BOOST_MPL_AUX_NESTED_VALUE_WKND(int, Distance) )
        > type;
};

template<
      typename T
    , BOOST_MPL_AUX_NTTP_DECL(int, l1)
    , BOOST_MPL_AUX_NTTP_DECL(int, l2)
    >
struct distance< aux::sel_iter<T,l1>, aux::sel_iter<T,l2> >
    : int_<( l2 - l1 )>
{
};

#else

namespace aux {

struct sel_iter_tag;

template< typename T, BOOST_MPL_AUX_NTTP_DECL(int, is_last_) >
struct sel_iter
{
    enum { pos_ = is_last_ };
    typedef aux::sel_iter_tag tag;
    typedef random_access_iterator_tag category;

    typedef sel_iter<T,(is_last_ + 1)> next;
    typedef sel_iter<T,(is_last_ - 1)> prior;
    typedef T type;
};

} // namespace aux

template<> struct advance_impl<aux::sel_iter_tag>
{
    template< typename Iterator, typename N > struct apply
    {
        enum { pos_ = Iterator::pos_, n_ = N::value };
        typedef aux::sel_iter<
              typename Iterator::type
            , (pos_ + n_)
            > type;
    };
};

template<> struct distance_impl<aux::sel_iter_tag>
{
    template< typename Iter1, typename Iter2 > struct apply
    {
        enum { pos1_ = Iter1::pos_, pos2_ = Iter2::pos_ };
        typedef int_<( pos2_ - pos1_ )> type;
        BOOST_STATIC_CONSTANT(int, value = ( pos2_ - pos1_ ));
    };
};

#endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION

}}

#endif // BOOST_MPL_AUX_SINGLE_ELEMENT_ITER_HPP_INCLUDED

ext: hpp

max_stars_repo_name: jaredhoberock/gotham

max_issues_repo_name: foxostro/CheeseTesseract

max_forks_repo_name: foxostro/CheeseTesseract


text: #include <boost/spirit/home/classic/utility/chset.hpp>

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text:
#include <iostream>
#include <boost/asio.hpp>

#define USE_LISTENER_HELPER

#if !defined(USE_LISTENER_HELPER)
#include <memory>
#endif

#define USE_WEBSOCKET_PLAIN
#define USE_WEBSOCKET_SSL

#ifdef USE_WEBSOCKET_SSL
#include "example/common/server_certificate.hpp"
#endif

#include "my_async.hpp"

#define ADDRESS		"0.0.0.0"
#define PORT		8080

template<bool UseSSL>
using Simple_Echo = My_Async::Websocket::Session_Static_Cb<UseSSL, std::string>;

template<bool UseSSL>
static void on_open(std::shared_ptr<Simple_Echo<UseSSL>>){
	std::cout << "Open\n";
}

template<bool UseSSL>
static void on_close(std::shared_ptr<Simple_Echo<UseSSL>>,
		boost::system::error_code ec){
	std::cout << "Close: " << ec.message() << "\n";
}

template<bool UseSSL>
static void on_message(std::shared_ptr<Simple_Echo<UseSSL>> ws,
		std::string const& data){
	std::cout << "data: " << data << "\n";
	ws->write(data);
}

template<bool UseSSL>
static void on_error(std::shared_ptr<Simple_Echo<UseSSL>>,
		boost::system::error_code ec,
		char const* what){
	std::cerr << what << ": " << ec.message() << "\n";
}

int main(){
	// The io_context is required for all I/O
	boost::asio::io_context ioc;

#ifdef USE_WEBSOCKET_PLAIN
	Simple_Echo<false>::on_open(on_open<false>);
	Simple_Echo<false>::on_close(on_close<false>);
	Simple_Echo<false>::on_message(on_message<false>);
	Simple_Echo<false>::on_error(on_error<false>);
#endif

#ifdef USE_WEBSOCKET_SSL
	Simple_Echo<true>::on_open(on_open<true>);
	Simple_Echo<true>::on_close(on_close<true>);
	Simple_Echo<true>::on_message(on_message<true>);
	Simple_Echo<true>::on_error(on_error<true>);
#endif

	auto const address = boost::asio::ip::make_address(ADDRESS);
	const unsigned int port = PORT;

#ifdef USE_WEBSOCKET_SSL
	boost::asio::ssl::context ctx{boost::asio::ssl::context::tlsv12};
	load_server_certificate(ctx);
#endif

//Plain
#if defined(USE_WEBSOCKET_PLAIN) && !defined(USE_WEBSOCKET_SSL)
#ifdef USE_LISTENER_HELPER
	My_Async::Websocket::
			make_listener<Simple_Echo<false>>(
				ioc,
				address, port)->run();
#else/* USE_LISTENER_HELPER */
    std::make_shared<My_Async::Websocket::
    		Listener_Base<Simple_Echo<false>>>
    				(ioc,
    				boost::asio::ip::tcp::endpoint{address, port})->run();
#endif/* USE_LISTENER_HELPER */
#endif

//SSL
#if !defined(USE_WEBSOCKET_PLAIN) && defined(USE_WEBSOCKET_SSL)
#ifdef USE_LISTENER_HELPER
    My_Async::Websocket::
    		make_listener<Simple_Echo<true>>(
    			ioc,
				ctx,
    			address, port)->run();
#else/* USE_LISTENER_HELPER */
	std::make_shared<My_Async::Websocket::
		Listener_Base<Simple_Echo<true>>>
				(ioc,
				ctx,
				boost::asio::ip::tcp::endpoint{address, port})->run();
#endif/* USE_LISTENER_HELPER */
#endif

//Plain and SSL
#if defined(USE_WEBSOCKET_PLAIN) && defined(USE_WEBSOCKET_SSL)
#ifdef USE_LISTENER_HELPER
    My_Async::Websocket::
		make_listener<Simple_Echo<false>, Simple_Echo<true>>(
			ioc,
			ctx,
			address, port)->run();
#else/* USE_LISTENER_HELPER */
	    std::make_shared<My_Async::Websocket::
		Listener_Dual<Simple_Echo<false>, Simple_Echo<true>>>
				(ioc,
				ctx,
				boost::asio::ip::tcp::endpoint{address, port})->run();

#endif/* USE_LISTENER_HELPER */
#endif
	// Create and launch a listening port
	boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
	signals.async_wait(
		[&ioc](boost::system::error_code const&, int){
			ioc.stop();
		});

	ioc.run();
	return EXIT_SUCCESS;
}


text:
// --------------------------------------------------------------------------
// Copyright (C) Karlsruhe Institute of Technology, 2018
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// --------------------------------------------------------------------------

#include "src/bid_set.h"
#include <boost/numeric/ublas/matrix_proxy.hpp>

BidSet::BidSet(const std::vector<double> &v_v,
               const boost::numeric::ublas::matrix<int> &m_q)
    : values(v_v), quantities(m_q) {}

BidSet::BidSet(const BidSet &copy)
    : values(copy.values), quantities(copy.quantities) {}

BidSet BidSet::fromYAML(YAML::Node bidset) {
  auto values = bidset["values"].as<std::vector<double>>();

  std::vector<unsigned int> v_q;
  for (auto row : bidset["quantities"]) {
    auto v_row = row.as<std::vector<unsigned int>>();
    v_q.insert(v_q.end(), v_row.begin(), v_row.end());
  }

  auto nrows = bidset["quantities"].size();
  auto ncols = v_q.size() / nrows;

  boost::numeric::ublas::matrix<unsigned int> quantities(nrows, ncols);
  std::copy(v_q.begin(), v_q.end(), quantities.data().begin());

  return BidSet(values, quantities);
}

BidSet BidSet::sample(double sampling_ratio) {
  unsigned int sample_n = (int)(N() * sampling_ratio);

  std::vector<double> sample_values;
  sample_values.resize(sample_n);
  std::copy_n(values.begin(), sample_n, sample_values.begin());

  auto sample_quantities =
      boost::numeric::ublas::subslice(quantities, 0, 1, sample_n, 0, 1, L());

  return BidSet(sample_values, sample_quantities);
}

boost::unordered_map<unsigned int, double> BidSet::computeAvgPrices() const {
  boost::unordered_map<unsigned int, double> avg_price;
  for (unsigned int i = 0; i < N(); ++i) {
    unsigned int q_i = 0;
    for (unsigned int k = 0; k < L(); ++k) {
      q_i += quantities(i, k);
    }
    avg_price[i] = values[i] / q_i;
  }
  return avg_price;
}

boost::unordered_map<unsigned int, double> BidSet::computeDensities() const {
  return computeDensities(std::vector<double>(L(), 1.));
}

boost::unordered_map<unsigned int, double> BidSet::computeDensities(
    std::vector<double> f) const {
  boost::unordered_map<unsigned int, double> density;
  for (unsigned int i = 0; i < N(); ++i) {
    double m_i = 0;
    for (unsigned int k = 0; k < L(); ++k) {
      m_i += quantities(i, k) * f[k];
    }
    density[i] = values[i] / std::sqrt(m_i);
  }
  return density;
}

std::vector<unsigned int> BidSet::computeQPerResource() const {
  std::vector<unsigned int> qpr(L(), 0);
  for (unsigned int i = 0; i < N(); ++i) {
    for (unsigned int k = 0; k < L(); ++k) {
      qpr[k] += quantities(i, k);
    }
  }
  return qpr;
}

max_stars_repo_name: dianagudu/ca-portfolio

max_issues_repo_name: dianagudu/ca-portfolio

max_forks_repo_name: dianagudu/ca-portfolio


text:
/******************************************************************
 *
 * uEcho for C
 *
 * Copyright (C) Satoshi Konno 2015
 *
 * This is licensed under BSD-style license, see file COPYING.
 *
 ******************************************************************/

#include <boost/test/unit_test.hpp>

#include <stdlib.h>

#include <uecho/_object.h>

const int U_ECHO_TEST_OBJECT_CODE_MAX = uEchoObjectCodeMax % 100;

BOOST_AUTO_TEST_CASE(ObjectListAdd)
{
  uEchoObjectList* objs = uecho_objectlist_new();

  BOOST_CHECK_EQUAL(uecho_objectlist_size(objs), 0);

  for (size_t n = uEchoObjectCodeMin; n <= U_ECHO_TEST_OBJECT_CODE_MAX; n++) {
    uEchoObject* obj = uecho_object_new();
    uecho_object_setcode(obj, (uEchoObjectCode)n);
    BOOST_CHECK(uecho_objectlist_add(objs, obj));
  }

  BOOST_CHECK_EQUAL(uecho_objectlist_size(objs), (U_ECHO_TEST_OBJECT_CODE_MAX - uEchoObjectCodeMin + 1));

  for (size_t n = uEchoObjectCodeMin; n <= U_ECHO_TEST_OBJECT_CODE_MAX; n++) {
    uEchoObject* obj = uecho_objectlist_getbycode(objs, (uEchoObjectCode)n);
    BOOST_CHECK(obj);
    BOOST_CHECK_EQUAL(uecho_object_getcode(obj), n);
  }

  BOOST_CHECK_EQUAL(uecho_objectlist_size(objs), (U_ECHO_TEST_OBJECT_CODE_MAX - uEchoObjectCodeMin + 1));

  BOOST_CHECK(uecho_objectlist_clear(objs));

  BOOST_CHECK_EQUAL(uecho_objectlist_size(objs), 0);

  uecho_objectlist_delete(objs);
}

BOOST_AUTO_TEST_CASE(ObjectListSet)
{
  uEchoObjectList* objs = uecho_objectlist_new();

  BOOST_CHECK_EQUAL(uecho_objectlist_size(objs), 0);

  for (size_t n = uEchoObjectCodeMin; n <= U_ECHO_TEST_OBJECT_CODE_MAX; n++) {
    BOOST_CHECK(uecho_objectlist_set(objs, (uEchoObjectCode)n));
  }

  BOOST_CHECK_EQUAL(uecho_objectlist_size(objs), (U_ECHO_TEST_OBJECT_CODE_MAX - uEchoObjectCodeMin + 1));

  for (size_t n = uEchoObjectCodeMin; n <= U_ECHO_TEST_OBJECT_CODE_MAX; n++) {
    uEchoObject* obj = uecho_objectlist_getbycode(objs, (uEchoObjectCode)n);
    BOOST_CHECK(obj);
    BOOST_CHECK_EQUAL(uecho_object_getcode(obj), n);
  }

  BOOST_CHECK_EQUAL(uecho_objectlist_size(objs), (U_ECHO_TEST_OBJECT_CODE_MAX - uEchoObjectCodeMin + 1));

  BOOST_CHECK(uecho_objectlist_clear(objs));

  BOOST_CHECK_EQUAL(uecho_objectlist_size(objs), 0);

  uecho_objectlist_delete(objs);
}


text:
#include "solver/eigenvalue/krylov_schur_eigenvalue_solver.hpp"

#include <deal.II/lac/petsc_full_matrix.h>

#include "test_helpers/gmock_wrapper.h"
#include "solver/eigenvalue/tests/spectral_radius_mock.hpp"

namespace  {

using namespace bart;
using ::testing::ContainerEq;

class SolverEigenvalueKrylovSchurEigenvalueSolverTest : public ::testing::Test {
 public:
  using Matrix = dealii::PETScWrappers::FullMatrix;
  Matrix test_matrix_;
  auto SetUp() -> void override;
};

auto SolverEigenvalueKrylovSchurEigenvalueSolverTest::SetUp() -> void {
  const std::vector<std::vector<double>> matrix_values{ {3, 2, 4}, {2, 0, 2}, {4, 2, 3}};
  test_matrix_.reinit(3,3);
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      test_matrix_.set(i, j, matrix_values.at(i).at(j));
    }
  }
  test_matrix_.compress(dealii::VectorOperation::insert);
}

TEST_F(SolverEigenvalueKrylovSchurEigenvalueSolverTest, Dummy) {
  std::vector<double> expected_eigenvector{ 2.0/3.0, 1.0/3.0, 2.0/3.0};
  const double expected_spectral_radius{ 8 };
  solver::eigenvalue::KrylovSchurEigenvalueSolver solver;
  const auto [spectral_radius, eigenvector] = solver.SpectralRadius(test_matrix_);
  EXPECT_NEAR(spectral_radius, expected_spectral_radius, 1e-6);
  ASSERT_EQ(eigenvector.size(), 3);
  for (int i = 0; i < 3; ++i) {
    EXPECT_NEAR(std::abs(eigenvector.at(i)), expected_eigenvector.at(i), 1e-6);
  }

}

} // namespace

max_stars_repo_name: SlaybaughLab/Transport

max_issues_repo_name: SlaybaughLab/Transport

max_forks_repo_name: SlaybaughLab/Transport


text:
//===--------------------------------------------------------------------------------*- C++ -*-===//
//                         _____                        _
//                        / ____|                      (_)
//                       | (___   ___  __ _ _   _  ___  _  __ _
//                        \___ \ / _ \/ _` | | | |/ _ \| |/ _` |
//                        ____) |  __/ (_| | |_| | (_) | | (_| |
//                       |_____/ \___|\__, |\__,_|\___/|_|\__,_| - Game Engine (2016-2017)
//                                       | |
//                                       |_|
//
// This file is distributed under the MIT License (MIT).
// See LICENSE.txt for details.
//
//===------------------------------------------------------------------------------------------===//

#include "sequoia-engine/Core/Logging.h"
#include "sequoia-engine/Render/Exception.h"
#include "sequoia-engine/Render/GL/GL.h"
#include "sequoia-engine/Render/GL/GLShader.h"
#include "sequoia-engine/Render/GL/GLShaderManager.h"
#include <boost/lexical_cast.hpp>
#include <fstream>
#include <sstream>

#include <iostream>

namespace sequoia {

namespace render {

/// @brief Extract a single row from `source`
static std::string getRow(const std::string& source, int rowIdx) {
  int row = 1;
  std::size_t start = 0, end = std::string::npos;
  for(std::size_t i = 0; i < source.size(); ++i) {
    if(source[i] == '\n') {
      end = i;
      if(row == rowIdx)
        return source.substr(start, end - start);
      start = end + 1;
      ++row;
    }
  }
  return std::string();
}

GLShaderManager::~GLShaderManager() {}

void GLShaderManager::makeValid(GLShader* shader) {
  SEQUOIA_ASSERT_MSG(!shader->isValid(), "shader already initialized");

  Log::debug("Loading shader from disk \"{}\"", shader->filename_);

  // Get the shader source
  if(shader->source_.empty())
    SEQUOIA_THROW(RenderSystemException, "empty shader source: '{}'", shader->filename_);

  // Register the shader within OpenGL
  shader->id_ = glCreateShader(GLShader::getGLShaderType(shader->getType()));
  if(shader->id_ == 0)
    SEQUOIA_THROW(RenderSystemException, "cannot create shader: '{}'", shader->filename_);

  Log::debug("Created shader (ID={}) from source \"{}\"", shader->id_, shader->filename_);

  Log::debug("Compiling shader (ID={}) ...", shader->id_);

  // Compile shader
  const char* code = shader->source_.c_str();
  GLint length = shader->source_.size();
  glShaderSource(shader->id_, 1, &code, &length);
  glCompileShader(shader->id_);

  // Check compilation
  GLint status = 0;
  glGetShaderiv(shader->id_, GL_COMPILE_STATUS, &status);
  if(!status) {
    GLint infoLogLength = 0;
    glGetShaderiv(shader->id_, GL_INFO_LOG_LENGTH, &infoLogLength);

    std::vector<char> infoLog(infoLogLength + 1);
    glGetShaderInfoLog(shader->id_, infoLogLength, NULL, &infoLog[0]);

    std::string reason(infoLog.data(), infoLog.size());

    // Extract 'Y(X)' where 'X' is the line number and 'Y' is the column number
    int lbrace = reason.find_first_of('('), rbrace = reason.find_first_of(')');
    try {
      int row = boost::lexical_cast<int>(reason.substr(lbrace + 1, rbrace - lbrace - 1));
      std::string line = getRow(shader->source_, row);

      if(!line.empty())
        reason += "\n  " + line + "\n";

    } catch(const boost::bad_lexical_cast&) {
    }

    SEQUOIA_THROW(RenderSystemException, "failed to compile shader (ID={}):\n{}", shader->id_, reason);
  }

  Log::debug("Successfully compiled shader (ID={})", shader->id_);
}

std::shared_ptr<GLShader> GLShaderManager::create(GLShader::ShaderType type,
                                                  const std::string& filename,
                                                  const std::string& source) {
  SEQUOIA_LOCK_GUARD(mutex_);

  auto it = filenameLookupMap_.find(filename);

  if(it != filenameLookupMap_.end())
    return shaderList_[it->second];

  shaderList_.emplace_back(std::make_shared<GLShader>(type, filename, source));
  filenameLookupMap_[filename] = shaderList_.size() - 1;
  return shaderList_.back();
}

void GLShaderManager::remove(const std::shared_ptr<GLShader>& shader) noexcept {
  SEQUOIA_LOCK_GUARD(mutex_);
  shaderList_.erase(std::remove(shaderList_.begin(), shaderList_.end(), shader), shaderList_.end());
  filenameLookupMap_.erase(shader->getFilename());
}

} // namespace render

} // namespace sequoia

max_stars_repo_name: thfabian/sequoia

max_issues_repo_name: thfabian/sequoia

max_forks_repo_name: thfabian/sequoia


text:
#include <search/KeyValueFileList.hpp>
#include <city.h>
#include <fstream>
#include <iostream>
#include <thread>
#include <mutex>
#include <cstring>
#include <boost/endian/conversion.hpp>

// for ram
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif

#define makeStringView(str) BytesView(str.data(), str.size())

namespace Search {

	#define BulkReserve 1'000'000  // 1Mb

	const uint64_t KeyValueFileList::Version;

	KeyValueFileList::KeyValueFileList(const fs::path& path)
		: path_(path), locked_(false), buffer_(nullptr), importing_(nullptr)
	{
		if( !path.empty() ) {
			if( !fs::is_regular_file(path_) ) {
				// create empty file
				createFile(path);
			}
			openFile();
		}
	}

	KeyValueFileList::~KeyValueFileList()
	{
		if( importing_ ) {
			delete importing_;
			importing_ = nullptr;
		}
	}

	void KeyValueFileList::createFile(const fs::path& path, uint64_t tabSize, uint64_t contentSize)
	{
		// create
		{ std::ofstream output(path.string()); }
		fs::resize_file(path, 100+tabSize*sizeof(uint64_t)+contentSize);

		boost::iostreams::mapped_file file;
		file.open(path);
		if( !file.is_open() )
			throw std::runtime_error("Cant open file2");
		assert(file.data());

		/*auto ptr2 = reinterpret_cast<uint64_t*>(file.data());
		ptr2[0] = 1;  // version
		ptr2[1] = tabSize;  // number of slots in hash table
		ptr2[2] = 0;  // number of vasted bytes
		ptr2[3] = 100+tabSize*sizeof(uint64_t);  // next data offset
		ptr2[4] = 0;  // num items
		ptr2[5] = 0;  // num keys*/

		uint64_t numWasted = 0;
		uint64_t nextDataOffset2 = 100+tabSize*sizeof(uint64_t);
		uint64_t numItems = 0;
		uint64_t numKeys = 0;

		const auto v = boost::endian::native_to_little<uint64_t>(Version);
		std::memcpy(file.data()+0*sizeof(uint64_t), &v, sizeof(uint64_t));
		std::memcpy(file.data()+1*sizeof(uint64_t), &tabSize, sizeof(uint64_t));  // number of slots in hash table
		std::memcpy(file.data()+2*sizeof(uint64_t), &numWasted, sizeof(uint64_t));  // number of wasted bytes
		std::memcpy(file.data()+3*sizeof(uint64_t), &nextDataOffset2, sizeof(uint64_t));  // next data offset
		std::memcpy(file.data()+4*sizeof(uint64_t), &numItems, sizeof(uint64_t));  // num items
		std::memcpy(file.data()+5*sizeof(uint64_t), &numKeys, sizeof(uint64_t));  // num keys

		file.close();
	}

	size_t KeyValueFileList::fileSize() const
	{
		return file_.size();
	}

	bool KeyValueFileList::isFileVersionOk(const fs::path& pth)
	{
		if(!fs::is_regular_file(pth))
			return true;
		if(fs::file_size(pth) == 0)
			return false;
		boost::iostreams::mapped_file f;
		f.open(pth);
		if( !f.is_open() || f.data() == nullptr )
			throw std::runtime_error("Cant open file");

		if(f.size() < 100) {
			f.close();
			return false;
		}

		uint64_t ver;
		std::memcpy(&ver, f.data()+0*sizeof(uint64_t), sizeof(uint64_t));  // version
		ver = boost::endian::little_to_native<uint64_t>(ver);
		bool ok = ver == Version;
		f.close();
		return ok;
	}

	void KeyValueFileList::set(const std::string& key, const Bytes& value)
	{
		set(std::string_view(key), makeStringView(value));
	}

	void KeyValueFileList::set(std::string_view key, BytesView value)
	{
		ensureTableSize(1);
		auto bucket = calcBucket(key);
		auto itemSize = ItemKey::calcSize(key) + ItemValue::calcSize(value);
		ensureFreeSpace(itemSize);
		setInternal(bucket, key, value);
		ensureOptimalWaste();
	}

	void KeyValueFileList::set(const KeyValueFileList& db2)
	{
		ensureTableSize(db2.numKeys());

		auto num1 = numBuckets();
		auto num2 = db2.numBuckets();
		for( uint64_t i=0; i<num2; ++i ) {
			auto itKey = db2.firstKey(i);
			ensureFreeSpace(itKey.calcSize());

			while( itKey.valid() ) {
				uint64_t bucket;
				if( num1 == num2 ) {
					bucket = i;
				}
				else {
					bucket = calcBucket(itKey.key());
				}

				auto itValue = itKey.value();
				while(itValue.valid()) {
					ensureFreeSpace(itValue.calcSize());
					setInternal(bucket, itKey.key(), itValue.value());
					itValue = itValue.next();
				}
				itKey = itKey.next();
			}
		}

		ensureOptimalWaste();
	}

	std::vector<BytesView> KeyValueFileList::get(std::string_view key) const
	{
		return getWithBucket(calcBucket(key), key);
	}

	std::vector<BytesView> KeyValueFileList::getWithBucket(uint64_t bucket, std::string_view key) const
	{
		auto itKey = firstKey(bucket);
		while(itKey.valid()) {
			if( itKey.key() == key ) {
				std::vector<BytesView> arr;
				auto itValue = itKey.value();
				while(itValue.valid()) {
					arr.push_back(itValue.value());
					itValue = itValue.next();
				}
				return arr;
			}
			itKey = itKey.next();
		}
		return {};
	}

	std::vector<BytesView> KeyValueFileList::get(const std::string& key2) const
	{
		return get(std::string_view(key2));
	}

	bool KeyValueFileList::exists(std::string_view key, BytesView value) const
	{
		return existsWithBucket(calcBucket(key), key, value);
	}

	bool KeyValueFileList::existsWithBucket(uint64_t bucket, std::string_view key, BytesView value) const
	{
		auto itKey = firstKey(bucket);
		while(itKey.valid()) {
			if( itKey.key() == key ) {
				auto itValue = itKey.value();
				while(itValue.valid()) {
					if( itValue.value() == value ) {
						return true;
					}
					itValue = itValue.next();
				}
				return false;
			}
			itKey = itKey.next();
		}
		return false;
	}

	void KeyValueFileList::remove(std::string_view key, BytesView value)
	{
		auto bucket = calcBucket(key);
		removeInternal(bucket, key, value);
		ensureOptimalWaste();
	}

	void KeyValueFileList::remove(const std::string& key2, const Bytes& value2)
	{
		auto key = std::string_view(key2);
		auto value = makeStringView(value2);
		return remove(key, value);
	}

	void KeyValueFileList::setInternal(uint64_t bucket, std::string_view key, BytesView value)
	{
		uint64_t keyOffset = 0;
		bool valueExists = false;
		auto it = firstKey(bucket);
		while(it.valid()) {
			if( it.key() == key ) {
				keyOffset = it.offset();
				auto it2 = it.value();
				while(it2.valid()) {
					if( it2.value() == value ) {
						valueExists = true;
						break;
					}
					it2 = it2.next();
				}
				break;
			}
			it = it.next();
		}
		if(valueExists) {
			return;
		}

		uint64_t nextValueOffset = keyOffset != 0 ? it.valueOffset() : 0;

		// add value
		auto valueOffset = nextDataOffset();
		auto dt = data() + valueOffset;
		ItemValue::write(dt, nextValueOffset, value);
		setNextDataOffset(dt-data());
		setNumItems(numItems()+1);

		if( keyOffset != 0 ) {
			// key exists
			it.setValueOffset(valueOffset);
		}
		else {
			// key doesnt exist
			keyOffset = nextDataOffset();
			dt = data() + keyOffset;
			ItemKey::write(dt, tableOffset(bucket), key, valueOffset);
			setNextDataOffset(dt-data());
			setNumKeys(numKeys()+1);
			setTableOffset(bucket, keyOffset);
		}
	}

	void KeyValueFileList::removeInternal(uint64_t bucket, std::string_view key, BytesView value)
	{
		// find key
		size_t prevKeyOffset = 0;
		auto itKey = firstKey(bucket);
		while( true ) {
			if( !itKey.valid() ) {
				return;
			}
			if( itKey.key() == key ) {
				break;
			}
			prevKeyOffset = itKey.offset();
			itKey = itKey.next();
		}

		// find value
		auto prevValueOffset = 0;
		auto itValue = itKey.value();
		while( true ) {
			if( !itValue.valid() ) {
				return;
			}
			if( itValue.value() == value ) {
				break;
			}
			prevValueOffset = itValue.offset();
			itValue = itValue.next();
		}

		// remove value
		if( prevValueOffset != 0 ) {
			ItemValue itPrev(data(), prevValueOffset);
			itPrev.setNextOffset(itValue.nextOffset());
		}
		else {
			itKey.setValueOffset(itValue.nextOffset());
		}
		setWasted(wasted() + itValue.calcSize());
		setNumItems(numItems()-1);

		// does key have any more values?
		if( itKey.value().valid() ) {
			return;
		}

		// remove key
		if( prevKeyOffset == 0 ) {
			setTableOffset(bucket, itKey.nextOffset());
		}
		else {
			ItemKey it2(data(), prevKeyOffset);
			it2.setNextOffset(itKey.nextOffset());
		}
		setWasted(wasted() + itKey.calcSize());
		setNumKeys(numKeys()-1);
	}

	KeyValueFileList::ItemKey KeyValueFileList::firstKey(uint64_t bucket) const
	{
		//assert(bucket < numBuckets());
		auto offset = tableOffset(bucket);
		return ItemKey(data(), offset);
	}

	void KeyValueFileList::bulkWrite(std::ofstream& out, std::string_view key, BytesView value)
	{
		uint64_t hash = calcHash(key);
		out.write((const char*)&hash, sizeof(hash));

		// key len
		auto len = ItemKey::calcSize(key);
		auto len2 = writeSizeString(len);
		out.write((const char*)len2.data(), len2.size());

		// key
		Bytes buff(len, (std::byte)'\0');
		std::byte* buff2 = &buff[0];
		ItemKey::write(buff2, 0, key, 0);
		out.write((const char*)buff.data(), buff.size());

		// value len
		len = ItemValue::calcSize(value);
		len2 = writeSizeString(len);
		out.write((const char*)len2.data(), len2.size());

		// value
		buff = Bytes(len, (std::byte)'\0');
		buff2 = &buff[0];
		ItemValue::write(buff2, 0, value);
		out.write((const char*)buff.data(), buff.size());
	}

	std::tuple<uint64_t, std::string_view, BytesView> KeyValueFileList::bulkRead(const std::byte*& dt)
	{
		uint64_t hash;
		std::memcpy(&hash, dt, sizeof(hash));
		dt += sizeof(hash);

		auto len = readSize(dt);
		std::byte* dt2 = (std::byte*)dt;
		ItemKey itKey(dt2, 0);
		dt += len;

		len = readSize(dt);
		dt2 = (std::byte*)dt;
		ItemValue itValue(dt2, 0);
		dt += len;

		return {hash, itKey.key(), itValue.value()};
	}

	void KeyValueFileList::bulkStart(size_t numThreads)
	{
		assert(!importing_);
		std::unique_ptr<ImportingData> dt(new ImportingData());
		dt->numItems = numItems();
		dt->numKeys = numKeys();
		dt->wasted = wasted();
		dt->dataRange.resize(numThreads, {0,0});
		importing_ = dt.release();
	}
	void KeyValueFileList::bulkStop()
	{
		assert(importing_);

		for(auto& pair : importing_->dataRange) {
			auto diff = pair.second - pair.first;
			importing_->wasted += diff;
		}

		std::unique_ptr<ImportingData> dt(importing_);
		importing_ = nullptr;
		setNumItems(dt->numItems);
		setNumKeys(dt->numKeys);
		setWasted(dt->wasted);
	}

	void KeyValueFileList::bulkInsertEnlarge(size_t nthThread, size_t numThreads)
	{
		// must be locked by caller
		//std::unique_lock<std::shared_mutex> lock2(importing_->mutex_);

		auto diff = importing_->dataRange[nthThread].second - importing_->dataRange[nthThread].first;
		importing_->wasted += diff;
		importing_->dataRange[nthThread].first = 0;
		importing_->dataRange[nthThread].second = 0;

		setWasted(importing_->wasted);
		setNumItems(importing_->numItems);
		setNumKeys(importing_->numKeys);
		if( wasted() > 100'000'000 ) {
			for(auto& pair : importing_->dataRange) {
				auto diff = pair.second - pair.first;
				if(diff > 0) {
					setWasted(wasted() + diff);
				}
				pair.first = 0;
				pair.second = 0;
			}

			uint64_t contentSize = nextDataOffset() - 100 - numBuckets() * sizeof(uint64_t) - wasted();
			changeTable(numBuckets(), contentSize);
			importing_->wasted = wasted();
			assert(importing_->numItems == numItems());
			assert(importing_->numKeys == numKeys());
		}

		// ensure free space
		ensureFreeSpace(BulkReserve);
		importing_->dataRange[nthThread].first = nextDataOffset();
		importing_->dataRange[nthThread].second = importing_->dataRange[nthThread].first + BulkReserve;
		setNextDataOffset(importing_->dataRange[nthThread].second);
	}

	void KeyValueFileList::bulkInsert(uint64_t bucket, std::string_view key, BytesView value, size_t nthThread, size_t numThreads)
	{
		auto itemSize = ItemKey::calcSize(key) + ItemValue::calcSize(value);
		if( itemSize > BulkReserve ) {
			throw std::runtime_error("Too big");
		}

		std::lock_guard lock1(importing_->mutex_);
		if( itemSize > importing_->dataRange[nthThread].second - importing_->dataRange[nthThread].first ) {
			bulkInsertEnlarge(nthThread, numThreads);
		}

		uint64_t keyOffset = 0;
		bool valueExists = false;
		auto it = firstKey(bucket);
		while(it.valid()) {
			if( it.key() == key ) {
				keyOffset = it.offset();
				/*auto it2 = it.value();
				while(it2.valid()) {
					if( it2.value() == value ) {
						valueExists = true;
						break;
					}
					it2 = it2.next();
				}*/
				break;
			}
			it = it.next();
		}
		if(valueExists) {
			return;
		}

		uint64_t nextValueOffset = keyOffset != 0 ? it.valueOffset() : 0;

		// add value
		auto valueOffset = importing_->dataRange[nthThread].first;
		auto dt = data() + valueOffset;
		ItemValue::write(dt, nextValueOffset, value);
		importing_->dataRange[nthThread].first += dt - data() - valueOffset;
		importing_->numItems++;

		if( keyOffset != 0 ) {
			// key exists
			it.setValueOffset(valueOffset);
		}
		else {
			// key doesnt exist
			keyOffset = importing_->dataRange[nthThread].first;
			dt = data() + keyOffset;
			ItemKey::write(dt, tableOffset(bucket), key, valueOffset);
			importing_->dataRange[nthThread].first += dt - data() - keyOffset;
			importing_->numKeys++;
			setTableOffset(bucket, keyOffset);
		}
	}

	void KeyValueFileList::bulkRemove(uint64_t bucket, std::string_view key, BytesView value, size_t nthThread, size_t numThreads)
	{
		std::lock_guard lock1(importing_->mutex_);

		// find key
		size_t prevKeyOffset = 0;
		auto itKey = firstKey(bucket);
		while( true ) {
			if( !itKey.valid() ) {
				return;
			}
			if( itKey.key() == key ) {
				break;
			}
			prevKeyOffset = itKey.offset();
			itKey = itKey.next();
		}

		// find value
		auto prevValueOffset = 0;
		auto itValue = itKey.value();
		while( true ) {
			if( !itValue.valid() ) {
				return;
			}
			if( itValue.value() == value ) {
				break;
			}
			prevValueOffset = itValue.offset();
			itValue = itValue.next();
		}

		// remove value
		if( prevValueOffset != 0 ) {
			ItemValue itPrev(data(), prevValueOffset);
			itPrev.setNextOffset(itValue.nextOffset());
		}
		else {
			itKey.setValueOffset(itValue.nextOffset());
		}
		importing_->wasted += itValue.calcSize();
		importing_->numItems--;

		// does key have any more values?
		if( itKey.value().valid() ) {
			return;
		}

		// remove key
		if( prevKeyOffset == 0 ) {
			setTableOffset(bucket, itKey.nextOffset());
		}
		else {
			ItemKey it2(data(), prevKeyOffset);
			it2.setNextOffset(itKey.nextOffset());
		}
		importing_->wasted += itKey.calcSize();
		importing_->numKeys--;

		/*size_t offset = 0;
		size_t prevOffset = 0;
		bool otherKeys = false;
		{
			auto it = firstKey(bucket);
			while(it.valid()) {
				if( it.key() == key ) {
					if(it.value() == value) {
						offset = it.offset();
					}
					else {
						otherKeys = true;
					}
					if( offset != 0 && otherKeys ) {
						break;
					}
				}
				prevOffset = it.offset();
				it = it.next();
			}
		}

		if(offset == 0) {
			return;
		}

		Item it3(data(), offset);
		if( prevOffset == 0 ) {
			setTableOffset(bucket, it3.nextOffset());
		}
		else {
			Item it2(data(), prevOffset);
			it2.setNextOffset(it3.nextOffset());
		}
		importing_->numItems--;
		importing_->wasted += it3.calcSize();*/
	}

	bool KeyValueFileList::bulkIsInThread(uint64_t bucket, size_t nthThread, size_t numThreads, uint64_t numBuckets2)
	{
		if( numThreads == 1 )
			return true;
		size_t perThread = numBuckets2 / numThreads;
		size_t start = nthThread * perThread;
		if( bucket < start )
			return false;
		if( bucket < start + perThread )
			return true;
		if( nthThread+1 == numThreads ) {
			return true;
		}
		return false;
	}

	uint64_t KeyValueFileList::calcHash(std::string_view key)
	{
		return CityHash64((const char*)key.data(), key.size());
	}

	uint64_t KeyValueFileList::calcBucketFromHash(uint64_t hash, uint64_t numBuckets2)
	{
		return hash % numBuckets2;
	}

	uint64_t KeyValueFileList::calcBucket(std::string_view key) const
	{
		return calcBucketFromHash(calcHash(key), numBuckets());
	}

	/*std::vector<uint64_t> KeyValueFileList::calcBuckets(const std::vector<std::pair<std::string_view, BytesView>>& arr) const
	{
		std::vector<uint64_t> buckets(arr.size());
		for( size_t i=0; i<arr.size(); ++i ) {
			buckets[i] = calcBucket(arr[i].first);
		}
		return buckets;
	}*/

	std::byte* KeyValueFileList::data() const
	{
		//assert(file_.is_open());
		if( buffer_ )
			return buffer_;
		std::byte* dt = (std::byte*)file_.data();
		//assert(dt != nullptr);
		return dt;
	}

	uint64_t KeyValueFileList::tableOffset(uint64_t bucket) const
	{
		std::byte* dt = data();
		dt += 100;
		dt += bucket * sizeof(uint64_t);
		//return *reinterpret_cast<uint64_t*>(dt);
		uint64_t n; std::memcpy(&n, dt, sizeof(n)); return n;
	}
	void KeyValueFileList::setTableOffset(uint64_t bucket, uint64_t offset) const
	{
		std::byte* dt = data();
		dt += 100;
		dt += bucket * sizeof(uint64_t);
		//*reinterpret_cast<uint64_t*>(dt) = offset;
		std::memcpy(dt, &offset, sizeof(uint64_t));
	}

	uint64_t KeyValueFileList::numBuckets() const
	{
		//auto ptr2 = reinterpret_cast<uint64_t*>(data());
		//return ptr2[0];
		uint64_t n; std::memcpy(&n, data()+1*sizeof(uint64_t), sizeof(n)); return n;
	}

	uint64_t KeyValueFileList::nextDataOffset() const
	{
		//auto ptr2 = reinterpret_cast<uint64_t*>(data());
		//return ptr2[2];
		uint64_t n; std::memcpy(&n, data()+3*sizeof(uint64_t), sizeof(n)); return n;
	}

	void KeyValueFileList::setNextDataOffset(uint64_t off)
	{
		//auto ptr2 = reinterpret_cast<uint64_t*>(data());
		//ptr2[2] = off;
		std::memcpy(data()+3*sizeof(uint64_t), &off, sizeof(uint64_t));
	}

	uint64_t KeyValueFileList::numItems() const
	{
		//auto ptr2 = reinterpret_cast<uint64_t*>(data());
		//return ptr2[3];
		uint64_t n; std::memcpy(&n, data()+4*sizeof(uint64_t), sizeof(n)); return n;
	}

	void KeyValueFileList::setNumItems(uint64_t n)
	{
		//auto ptr2 = reinterpret_cast<uint64_t*>(data());
		//ptr2[3] = n;
		std::memcpy(data()+4*sizeof(uint64_t), &n, sizeof(uint64_t));
	}

	uint64_t KeyValueFileList::numKeys() const
	{
		//auto ptr2 = reinterpret_cast<uint64_t*>(data());
		//return ptr2[4];
		uint64_t n; std::memcpy(&n, data()+5*sizeof(uint64_t), sizeof(n)); return n;
	}

	void KeyValueFileList::setNumKeys(uint64_t n)
	{
		//auto ptr2 = reinterpret_cast<uint64_t*>(data());
		//ptr2[4] = n;
		std::memcpy(data()+5*sizeof(uint64_t), &n, sizeof(uint64_t));
	}

	uint64_t KeyValueFileList::wasted() const
	{
		//auto ptr2 = reinterpret_cast<uint64_t*>(data());
		//return ptr2[1];
		uint64_t n; std::memcpy(&n, data()+2*sizeof(uint64_t), sizeof(n)); return n;
	}

	void KeyValueFileList::setWasted(uint64_t w)
	{
		//auto ptr2 = reinterpret_cast<uint64_t*>(data());
		//ptr2[1] = w;
		std::memcpy(data()+2*sizeof(uint64_t), &w, sizeof(uint64_t));
	}

	void KeyValueFileList::optimize()
	{
		locked_ = false;

		double fact = (double)numKeys() / (double)numBuckets();
		if( fact > 1.05 || fact < 0.6 ) {
			uint64_t tabSize = findTabSizePrime(numKeys()/0.8);
			uint64_t contentSize = nextDataOffset() - 100 - numBuckets() * sizeof(uint64_t) - wasted();
			changeTable(tabSize, contentSize);
			return;
		}

		if( wasted() > 500000 ) {
			uint64_t contentSize = nextDataOffset() - 100 - numBuckets() * sizeof(uint64_t) - wasted();
			changeTable(numBuckets(), contentSize);
			return;
		}

		// optimize content
		auto s = nextDataOffset();
		file_.close();
		fs::resize_file(path_, s);
		openFile();
	}

	void KeyValueFileList::lockTableForNumKeys(uint64_t n)
	{
		locked_ = true;

		double fact = (double)n / (double)numBuckets();
		if( fact < 0.9 && fact > 0.6 ) {
			return;
		}

		auto pth = tmpFilePath();
		uint64_t tabSize = findTabSizePrime(n/0.8);
		uint64_t contentSize = file_.size() - 100 - numBuckets() * sizeof(uint64_t) - wasted();
		changeTable(tabSize, contentSize);
	}

	void KeyValueFileList::unlockTable()
	{
		locked_ = false;
	}

	std::vector<std::pair<std::string_view,BytesView>> KeyValueFileList::allDocuments() const
	{
		std::vector<std::pair<std::string_view,BytesView>> arr;
		arr.reserve(numItems());
		auto numB = numBuckets();
		for( uint64_t i=0; i<numB; ++i ) {
			auto itKey = firstKey(i);
			while( itKey.valid() ) {
				auto itValue = itKey.value();
				while(itValue.valid()) {
					arr.push_back({itKey.key(), itValue.value()});
					itValue = itValue.next();
				}
				itKey = itKey.next();
			}
		}
		return arr;
	}

	void KeyValueFileList::ensureFreeSpace(size_t additional)
	{
		if( buffer_ ) {
			return;
			//throw std::runtime_error("cant ensureFreeSpace() when using buffer");
		}

		auto s = file_.size();
		auto minS = nextDataOffset() + additional;
		if( minS <= s ) {
			return;
		}

		file_.close();

		if( s < 3000000 ) {
			s += 700000;
		}
		else {
			s += 5000000;
		}
		if( minS > s ) {
			s = minS + additional * 0.1;
		}

		fs::resize_file(path_, s);
		openFile();
	}

	void KeyValueFileList::ensureTableSize(int64_t additional)
	{
		if( locked_ )
			return;

		uint64_t num = numKeys() + additional;
		double fact = (double)num / (double)numBuckets();
		if( fact <= 1.4 && fact >= 0.3 ) {
			return;
		}
		if( fact < 1 && numBuckets() <= 101 ) {
			return;
		}

		auto pth = tmpFilePath();
		uint64_t tabSize;
		if( fact > 1 ) {
			// incresing
			tabSize = findTabSizePrimeDouble(num*1.8);
		}
		else {
			// decreasing
			tabSize = findTabSizePrimeDouble(num);
		}
		if( tabSize == numBuckets() ) {
			return;
		}
		auto contentSize = file_.size() - numBuckets() * sizeof(uint64_t);

		changeTable(tabSize, contentSize);
	}

	void KeyValueFileList::ensureOptimalWaste()
	{
		if( locked_ )
			return;
		if( wasted() < 30'000'000 )
			return;

		auto pth = tmpFilePath();
		uint64_t contentSize = file_.size() - 100 - numBuckets() * sizeof(uint64_t);
		changeTable(numBuckets(), contentSize);
	}

	fs::path KeyValueFileList::tmpFilePath() const
	{
		auto pth = path_;
		pth += ".tmp";
		return pth;
	}

	void KeyValueFileList::openFile()
	{
		file_.open(path_);
		if( !file_.is_open() || file_.data() == nullptr )
			throw std::runtime_error("Cant open file");

		uint64_t ver;
		std::memcpy(&ver, file_.data()+0*sizeof(uint64_t), sizeof(uint64_t));  // version
		ver = boost::endian::little_to_native<uint64_t>(ver);
		if(ver != Version) {
			file_.close();
			throw std::runtime_error("KeyValueFileList::openFile() Different version");
		}
	}

	void KeyValueFileList::changeTable(uint64_t tabSize, uint64_t contentSize)
	{
		if( buffer_ ) {
			throw std::runtime_error("cant change table when using buffer");
		}

		// for buffer only use absolutely necesary   -freeSpace -waste
		size_t newSizeContent = nextDataOffset() - 100 - numBuckets()*sizeof(uint64_t) - wasted();
		auto newSize = 100+tabSize*sizeof(uint64_t)+newSizeContent;
		bool isEnoughRam = newSize < (availableMemory()-100000000)*0.9;

		std::unique_ptr<std::byte[]> buffer2;
		if( isEnoughRam ) {
			buffer2.reset(new (std::nothrow) std::byte[newSize]);
		}

		// check if new failed - use disk
		if( !buffer2 ) {
			auto pth = tmpFilePath();
			createFile(pth, tabSize, contentSize);
			{
				KeyValueFileList tmp(pth);
				tmp.locked_ = true;
				tmp.set(*this);
			}
			file_.close();
			fs::rename(pth, path_);
			openFile();
			return;
		}

		{
			std::memset(buffer2.get(), 0, 100+tabSize*sizeof(uint64_t));
			/*auto ptr2 = reinterpret_cast<uint64_t*>(buffer2.get());
			ptr2[0] = 1;  // version
			ptr2[1] = tabSize;  // number of slots in hash table
			ptr2[2] = 0;  // number of wasted bytes
			ptr2[3] = 100+tabSize*sizeof(uint64_t);  // next data offset
			ptr2[4] = 0;  // num items
			ptr2[5] = 0;  // num keys*/

			uint64_t numWasted = 0;
			uint64_t nextDataOffset2 = 100+tabSize*sizeof(uint64_t);
			uint64_t numItems = 0;
			uint64_t numKeys = 0;

			const auto v = boost::endian::native_to_little<uint64_t>(Version);
			std::memcpy(buffer2.get()+0*sizeof(uint64_t), &v, sizeof(uint64_t));
			std::memcpy(buffer2.get()+1*sizeof(uint64_t), &tabSize, sizeof(uint64_t));  // number of slots in hash table
			std::memcpy(buffer2.get()+2*sizeof(uint64_t), &numWasted, sizeof(uint64_t));  // number of wasted bytes
			std::memcpy(buffer2.get()+3*sizeof(uint64_t), &nextDataOffset2, sizeof(uint64_t));  // next data offset
			std::memcpy(buffer2.get()+4*sizeof(uint64_t), &numItems, sizeof(uint64_t));  // num items
			std::memcpy(buffer2.get()+5*sizeof(uint64_t), &numKeys, sizeof(uint64_t));  // num keys
		}

		// dont optimize read from disk - no difference

		{
			fs::path p2;
			KeyValueFileList tmp(p2);
			tmp.locked_ = true;
			tmp.buffer_ = buffer2.get();
			tmp.set(*this);
		}

		file_.close();

		auto newSize2 = 100+tabSize*sizeof(uint64_t)+contentSize;
		fs::resize_file(path_, newSize2);

		{
			boost::iostreams::mapped_file file;
			file.open(path_);
			if( !file.is_open() )
				throw std::runtime_error("Cant open file2");
			assert(file.data());
			std::memcpy(file.data(), buffer2.get(), newSize);
			file.close();
		}

		openFile();
	}

	void KeyValueFileList::clear()
	{
		file_.close();
		createFile(path_);
		openFile();
	}

/*
#include <primesieve.hpp>
#include <iostream>

// brew install primesieve
// clang++ primes.cc -lprimesieve
// ./a.out

int main()
{
	// https://github.com/kimwalisch/primesieve#c-api

	primesieve::iterator it;
	uint64_t prime = it.next_prime();

	// iterate over the primes below 10^6
	size_t last = 1;
	for (; prime < 7000000000; prime = it.next_prime())
	{
		if( prime > 100 && (double)prime/last > 1.85 )
		{
			last = prime;
			std::cout << prime << ",";
		}
	}

	return 0;
}*/
	const size_t primesDoubleForTabSize[] = { 101,191,359,673,1249,2311,4283,7927,14669,27143,50221,92921,171917,318077,588463,1088657,2014027,3725951,6893011,12752071,23591333,43644023,80741447,149371709,276337673,511224709,945765721,1749666587,3236883239,5988234011 };
	const size_t primesDoubleForTabSizeNum = sizeof(primesDoubleForTabSize)/sizeof(size_t);
	const size_t primesForTabSize[] = {
		101,113,127,149,167,191,211,233,257,283,313,347,383,431,479,541,599,659,727,809,907,1009,1117,1229,1361,1499,1657,1823,2011,2213,2437,2683,2953,3251,3581,3943,4339,4783,5273,5801,6389,7039,7753,8537,9391,10331,11369,12511,13763,15149,16673,18341,20177,22229,24469,26921,29629,32603,35869,39461,43411,47777,52561,57829,63617,69991,76991,84691,93169,102497,112757,124067,136481,150131,165161,181693,199873,219871,241861,266051,292661,321947,354143,389561,428531,471389,518533,570389,627433,690187,759223,835207,918733,1010617,1111687,1222889,1345207,1479733,1627723,1790501,1969567,2166529,2383219,2621551,2883733,3172123,3489347,3838283,4222117,4644329,5108767,5619667,6181639,6799811,7479803,8227787,9050599,9955697,10951273,12046403,13251047,14576161,16033799,17637203,19400929,21341053,23475161,25822679,28404989,31245491,34370053,37807061,41587807,45746593,50321261,55353391,60888739,66977621,73675391,81042947,89147249,98061979,107868203,118655027,130520531,143572609,157929907,173722907,191095213,210204763,231225257,254347801,279782593,307760897,338536987,372390691,409629809,450592801,495652109,545217341,599739083,659713007,725684317,798252779,878078057,965885863,1062474559,1168722059,1285594279,1414153729,1555569107,1711126033,1882238639,2070462533,2277508787,2505259681,2755785653,3031364227,3334500667,3667950739,4034745863,4438220467,4882042547,5370246803,5907271567,6497998733
	};
	const size_t primesForTabSizeNum = sizeof(primesForTabSize)/sizeof(size_t);
	inline size_t KeyValueFileList::findTabSizePrime(size_t minNum)
	{
		for( size_t i=0; i<primesForTabSizeNum; ++i )
		{
			if( primesForTabSize[i] > minNum )
				return primesForTabSize[i];
		}
		throw std::runtime_error("KeyValueFileList findTabSizePrime() too big table");
	}
	inline size_t KeyValueFileList::findTabSizePrimeDouble(size_t minNum)
	{
		for( size_t i=0; i<primesDoubleForTabSizeNum; ++i )
		{
			if( primesDoubleForTabSize[i] > minNum ) {
				return primesDoubleForTabSize[i];
			}
		}
		throw std::runtime_error("KeyValueFileList findTabSizePrimeDouble() too big table");
	}

	uint64_t KeyValueFileList::availableMemory()
	{
		static uint64_t mem = 0;
		if( mem == 0 ) {
			// for determining ram size
			// https://stackoverflow.com/a/2513561
			#ifdef _WIN32
				MEMORYSTATUSEX status;
				status.dwLength = sizeof(status);
				GlobalMemoryStatusEx(&status);
				mem = status.ullTotalPhys;
			#else
				auto pages = sysconf(_SC_PHYS_PAGES);
				auto page_size = sysconf(_SC_PAGE_SIZE);
				mem = pages * page_size;
			#endif
			if( mem == 0 )
				throw std::runtime_error("Cant figure out how much ram is installed");
		}
		return mem;
	}

}

max_stars_repo_name: ibanic/search

max_issues_repo_name: ibanic/search

max_forks_repo_name: ibanic/search


text:
/**
 * @file eventManagerProxy.cc
 * @author Krzysztof Trzepla
 * @copyright (C) 2016 ACK CYFRONET AGH
 * @copyright This software is released under the MIT license cited in
 * 'LICENSE.txt'
 */

#include "communication/communicator.h"
#include "context.h"
#include "events/events.h"
#include "messages/fuse/fileAttr.h"
#include "messages/fuse/fileLocation.h"
#include "scheduler.h"

#include "messages.pb.h"

#include <boost/make_shared.hpp>
#include <boost/python.hpp>
#include <tbb/concurrent_hash_map.h>

#include <atomic>
#include <chrono>
#include <memory>
#include <unordered_map>

using namespace one;
using namespace one::client;
using namespace one::client::events;
using namespace one::communication;
using namespace boost::python;

class ManagerProxy {
public:
    ManagerProxy(std::shared_ptr<Context> context)
        : m_context{std::move(context)}
        , m_manager{m_context}
    {
    }

    ~ManagerProxy() { stop(); }

    void emitFileRead(std::string fileUuid, off_t offset, size_t size)
    {
        m_manager.emit<FileRead>(std::move(fileUuid), offset, size);
    }

    void emitFileWritten(std::string fileUuid, off_t offset, size_t size)
    {
        m_manager.emit<FileWritten>(std::move(fileUuid), offset, size);
    }

    void emitFileTruncated(std::string fileUuid, off_t fileSize)
    {
        m_manager.emit<FileTruncated>(std::move(fileUuid), fileSize);
    }

    std::int64_t subscribeFileRead(
        std::int64_t counterThr, std::int64_t timeThr)
    {
        return subscribeIOEvents<clproto::FileReadSubscription,
            FileReadSubscription>(counterThr, timeThr);
    }

    std::int64_t subscribeFileWritten(
        std::int64_t counterThr, std::int64_t timeThr)
    {
        return subscribeIOEvents<clproto::FileWrittenSubscription,
            FileWrittenSubscription>(counterThr, timeThr);
    }

    std::int64_t subscribeFileAttrChanged(
        std::string fileUuid, std::int64_t timeThr)
    {
        FileAttrChangedSubscription sub{std::move(fileUuid),
            std::chrono::milliseconds{timeThr},
            [this](auto) { this->incCounter("file_attr_changed"); }};
        return m_manager.subscribe(sub);
    }

    std::int64_t subscribeFileLocationChanged(
        std::string fileUuid, std::int64_t timeThr)
    {
        FileLocationChangedSubscription sub{std::move(fileUuid),
            std::chrono::milliseconds{timeThr},
            [this](auto) { this->incCounter("file_location_changed"); }};
        return m_manager.subscribe(sub);
    }

    std::int64_t subscribeFilePermChanged(std::string fileUuid)
    {
        FilePermChangedSubscription sub{std::move(fileUuid),
            [this](auto) { this->incCounter("file_perm_changed"); }};
        return m_manager.subscribe(sub);
    }

    std::int64_t subscribeFileRenamed(std::string fileUuid)
    {
        FileRenamedSubscription sub{std::move(fileUuid),
            [this](auto) { this->incCounter("file_renamed"); }};
        return m_manager.subscribe(sub);
    }

    std::int64_t subscribeFileRemoved(std::string fileUuid)
    {
        FileRemovedSubscription sub{std::move(fileUuid),
            [this](auto) { this->incCounter("file_removed"); }};
        return m_manager.subscribe(sub);
    }

    std::int64_t subscribeQuotaExceeded()
    {
        QuotaExceededSubscription sub{
            [this](auto) { this->incCounter("quota_exceeded"); }};
        return m_manager.subscribe(sub);
    }

    bool unsubscribe(std::int64_t subscriptionId)
    {
        return m_manager.unsubscribe(subscriptionId);
    }

    std::int64_t getHandlerCallsNumber(const std::string &handler)
    {
        decltype(m_counters)::const_accessor acc;
        if (m_counters.find(acc, handler)) {
            return acc->second;
        }
        return 0;
    }

    bool existsSubscription(std::int64_t subscriptionId, bool expected)
    {
        return m_manager.existsSubscription(subscriptionId) == expected;
    }

    void stop()
    {
        if (!m_stopped.test_and_set()) {
            m_context->communicator()->stop();
        }
    }

private:
    void incCounter(const std::string &handler)
    {
        decltype(m_counters)::accessor acc;
        if (m_counters.insert(acc, handler)) {
            acc->second = 1;
        }
        else {
            acc->second += 1;
        }
    }

    template <class ProtoSub, class Sub>
    std::int64_t subscribeIOEvents(
        std::int64_t counterThr, std::int64_t timeThr)
    {
        ProtoSub msg{};
        if (counterThr > 0)
            msg.set_counter_threshold(counterThr);
        if (timeThr > 0)
            msg.set_time_threshold(timeThr);

        return m_manager.subscribe(Sub{msg});
    }

    tbb::concurrent_hash_map<std::string, std::int64_t> m_counters;
    std::shared_ptr<Context> m_context;
    Manager m_manager;

    using CounterAcc = typename decltype(m_counters)::accessor;
    using ConstCounterAcc = typename decltype(m_counters)::const_accessor;

    std::atomic_flag m_stopped = ATOMIC_FLAG_INIT;
};

namespace {
boost::shared_ptr<ManagerProxy> create(
    std::string ip, const unsigned short port)
{
    FLAGS_minloglevel = 1;

    auto communicator = std::make_shared<Communicator>(/*connections*/ 10,
        /*threads*/ 1, std::move(ip), port,
        /*verifyServerCertificate*/ false, /*upgrade to clproto*/ true,
        /*perform handshake*/ false);

    auto context = std::make_shared<Context>();
    context->setScheduler(std::make_shared<Scheduler>(1));
    context->setCommunicator(communicator);
    communicator->setScheduler(context->scheduler());
    communicator->connect();

    return boost::make_shared<ManagerProxy>(context);
}
}

BOOST_PYTHON_MODULE(events)
{
    class_<ManagerProxy, boost::noncopyable>("Manager", no_init)
        .def("__init__", make_constructor(create))
        .def("stop", &ManagerProxy::stop)
        .def("emitFileRead", &ManagerProxy::emitFileRead)
        .def("emitFileWritten", &ManagerProxy::emitFileWritten)
        .def("emitFileTruncated", &ManagerProxy::emitFileTruncated)
        .def("subscribeFileRead", &ManagerProxy::subscribeFileRead)
        .def("subscribeFileWritten", &ManagerProxy::subscribeFileWritten)
        .def(
            "subscribeFileAttrChanged", &ManagerProxy::subscribeFileAttrChanged)
        .def("subscribeFileLocationChanged",
            &ManagerProxy::subscribeFileLocationChanged)
        .def(
            "subscribeFilePermChanged", &ManagerProxy::subscribeFilePermChanged)
        .def("subscribeFileRenamed", &ManagerProxy::subscribeFileRenamed)
        .def("subscribeFileRemoved", &ManagerProxy::subscribeFileRemoved)
        .def("subscribeQuotaExceeded", &ManagerProxy::subscribeQuotaExceeded)
        .def("unsubscribe", &ManagerProxy::unsubscribe)
        .def("getHandlerCallsNumber", &ManagerProxy::getHandlerCallsNumber)
        .def("existsSubscription", &ManagerProxy::existsSubscription);
}

ext: cc

max_stars_repo_name: onedata/oneclient

max_issues_repo_name: onedata/oneclient

max_forks_repo_name: onedata/oneclient


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

#include "Cluster.h"

#include <future>

#include <boost/filesystem.hpp>

Locator::Locator(Cluster &cluster, std::vector<Locator> &locators,
                 std::string name, uint16_t jmxManagerPort, bool useIPv6)
    : cluster_(cluster),
      name_(std::move(name)),
      locators_(locators),
      jmxManagerPort_(jmxManagerPort) {
  auto hostname = "localhost";
  if (useIPv6) {
    hostname = "ip6-localhost";
  }

  auto port = Framework::getAvailablePort();

  locatorAddress_ = LocatorAddress{hostname, port};
}

Locator::Locator(Cluster &cluster, std::vector<Locator> &locators,
                 std::string name, uint16_t jmxManagerPort, bool useIPv6,
                 uint16_t port, std::vector<uint16_t> &remotePorts,
                 uint16_t distributedSystemId)
    : cluster_(cluster),
      name_(std::move(name)),
      locators_(locators),
      jmxManagerPort_(jmxManagerPort),
      distributedSystemId_(distributedSystemId) {
  auto hostname = "localhost";
  if (useIPv6) {
    hostname = "ip6-localhost";
  }
  locatorAddress_ = LocatorAddress{hostname, port};

  for (uint16_t remotePort : remotePorts) {
    remoteLocatorsPorts_.push_back(remotePort);
  }
}

Locator::~Locator() {
  try {
    if (started_) {
      stop();
    }
  } catch (...) {
  }
}

Locator::Locator(Locator &&move)
    : cluster_(move.cluster_),
      name_(move.name_),
      locators_(move.locators_),
      locatorAddress_(move.locatorAddress_),
      remoteLocatorsPorts_(move.remoteLocatorsPorts_),
      jmxManagerPort_(move.jmxManagerPort_),
      started_(move.started_),
      distributedSystemId_(move.distributedSystemId_) {
  move.started_ = false;
}

const LocatorAddress &Locator::getAddress() const { return locatorAddress_; }

void Locator::start() {
  if (started_) return;

  auto safeName = name_;
  std::replace(safeName.begin(), safeName.end(), '/', '_');

  if (boost::filesystem::is_regular_file(name_ + "/vf.gf.locator.pid")) {
    cluster_.getGfsh().stop().locator().withDir(name_).execute();
  }

  auto locator = cluster_.getGfsh()
                     .start()
                     .locator()
                     .withLogLevel("INFO")
                     .withDir(name_)
                     .withName(safeName)
                     .withBindAddress(locatorAddress_.address)
                     .withPort(locatorAddress_.port)
                     .withRemoteLocators(remoteLocatorsPorts_)
                     .withDistributedSystemId(distributedSystemId_)
                     .withMaxHeap("256m")
                     .withJmxManagerPort(jmxManagerPort_)
                     .withHttpServicePort(0)
                     .withClasspath(cluster_.getClasspath())
                     .withSecurityManager(cluster_.getSecurityManager())
                     .withPreferIPv6(cluster_.getUseIPv6())
                     .withJmxManagerStart(true);

  if (cluster_.useSsl()) {
    locator.withConnect(false)
        .withSslEnabledComponents("all")
        .withSslRequireAuthentication(cluster_.requireSslAuthentication())
        .withSslKeystore(cluster_.keystore())
        .withSslTruststore(cluster_.truststore())
        .withSslKeystorePassword(cluster_.keystorePassword())
        .withSslTruststorePassword(cluster_.truststorePassword());
  }

  locator.execute(cluster_.getUser(), cluster_.getPassword(),
                  cluster_.keystore(), cluster_.truststore(),
                  cluster_.keystorePassword(), cluster_.truststorePassword());

  auto connect =
      cluster_.getGfsh().connect().withJmxManager(cluster_.getJmxManager());

  if (!cluster_.getUser().empty()) {
    connect.withUser(cluster_.getUser()).withPassword(cluster_.getPassword());
  }

  if (cluster_.useSsl()) {
    connect.withUseSsl(true)
        .withKeystore(cluster_.keystore())
        .withTruststore(cluster_.truststore())
        .withKeystorePassword(cluster_.keystorePassword())
        .withTruststorePassword(cluster_.truststorePassword());
  }

  connect.execute();

  started_ = true;
}

void Locator::stop() {
  cluster_.getGfsh().stop().locator().withDir(name_).execute();

  started_ = false;
}

const ServerAddress &Server::getAddress() const { return serverAddress_; }

Server::Server(Cluster &cluster, std::vector<Locator> &locators,
               std::string name, std::string xmlFile, bool useIPv6,
               uint16_t port)
    : cluster_(cluster),
      locators_(locators),
      name_(std::move(name)),
      xmlFile_(xmlFile) {
  auto hostname = "localhost";
  if (useIPv6) {
    hostname = "ip6-localhost";
  }

  serverAddress_ = ServerAddress{hostname, port};
}

std::string Server::getCacheXMLFile() { return xmlFile_; }

Server::~Server() {
  try {
    if (started_) {
      stop();
    }
  } catch (...) {
  }
}

Server::Server(Server &&move)
    : cluster_(move.cluster_),
      locators_(move.locators_),
      serverAddress_(move.serverAddress_),
      started_(move.started_),
      name_(move.name_),
      xmlFile_(move.xmlFile_) {
  move.started_ = false;
}

void Server::start() {
  auto safeName = name_;
  std::replace(safeName.begin(), safeName.end(), '/', '_');

  auto server =
      cluster_.getGfsh()
          .start()
          .server()
          .withDir(name_)
          .withName(safeName)
          .withBindAddress(serverAddress_.address)
          .withPort(serverAddress_.port)
          .withMaxHeap("1g")
          .withLocators(locators_.front().getAddress().address + "[" +
                        std::to_string(locators_.front().getAddress().port) +
                        "]")
          .withClasspath(cluster_.getClasspath())
          .withSecurityManager(cluster_.getSecurityManager())
          .withCacheXMLFile(getCacheXMLFile())
          .withConserveSockets(cluster_.getConserveSockets())
          .withPreferIPv6(cluster_.getUseIPv6());

  if (!cluster_.getUser().empty()) {
    server.withUser(cluster_.getUser()).withPassword(cluster_.getPassword());
  }

  if (cluster_.useSsl()) {
    server.withSslEnabledComponents("all")
        .withSslRquireAuthentication(cluster_.requireSslAuthentication())
        .withSslKeystore(cluster_.keystore())
        .withSslTruststore(cluster_.truststore())
        .withSslKeystorePassword(cluster_.keystorePassword())
        .withSslTruststorePassword(cluster_.truststorePassword());
  }

  server.execute();

  started_ = true;
}

void Server::stop() {
  cluster_.getGfsh().stop().server().withDir(name_).execute();

  started_ = false;
}

Cluster::Cluster(LocatorCount initialLocators, ServerCount initialServers,
                 std::vector<uint16_t> &locatorPorts,
                 std::vector<uint16_t> &remoteLocatorPort,
                 uint16_t distributedSystemId)
    : Cluster(
          Name(std::string(::testing::UnitTest::GetInstance()
                               ->current_test_info()
                               ->test_suite_name()) +
               "/DS" + std::to_string(distributedSystemId) + "/" +
               ::testing::UnitTest::GetInstance()->current_test_info()->name()),
          Classpath(""), SecurityManager(""), User(""), Password(""),
          initialLocators, initialServers, CacheXMLFiles({}), locatorPorts,
          remoteLocatorPort, distributedSystemId) {}

Cluster::Cluster(Name name, Classpath classpath,
                 SecurityManager securityManager, User user, Password password,
                 LocatorCount initialLocators, ServerCount initialServers,
                 CacheXMLFiles cacheXMLFiles,
                 std::vector<uint16_t> &locatorPorts,
                 std::vector<uint16_t> &remoteLocatorPort,
                 uint16_t distributedSystemId)
    : name_(name.get()),
      classpath_(classpath.get()),
      securityManager_(securityManager.get()),
      user_(user.get()),
      password_(password.get()),
      initialLocators_(initialLocators.get()),
      initialServers_(initialServers.get()),
      jmxManagerPort_(Framework::getAvailablePort()),
      distributedSystemId_(distributedSystemId) {
  cacheXMLFiles_ = cacheXMLFiles.get();
  useIPv6_ = false;

  for (uint16_t port : locatorPorts) {
    locatorsPorts_.push_back(port);
  }
  for (uint16_t port : remoteLocatorPort) {
    remoteLocatorsPorts_.push_back(port);
  }
  removeServerDirectory();
}

Cluster::Cluster(LocatorCount initialLocators, ServerCount initialServers,
                 UseIpv6 useIPv6)
    : Cluster(
          Name(std::string(::testing::UnitTest::GetInstance()
                               ->current_test_info()
                               ->test_suite_name()) +
               "/" +
               ::testing::UnitTest::GetInstance()->current_test_info()->name()),
          initialLocators, initialServers, useIPv6) {}

Cluster::Cluster(LocatorCount initialLocators, ServerCount initialServers)
    : Cluster(initialLocators, initialServers, UseIpv6(false)) {}

Cluster::Cluster(LocatorCount initialLocators, ServerCount initialServers,
                 CacheXMLFiles cacheXMLFiles)
    : name_(std::string(::testing::UnitTest::GetInstance()
                            ->current_test_info()
                            ->test_suite_name()) +
            "/" +
            ::testing::UnitTest::GetInstance()->current_test_info()->name()),
      initialLocators_(initialLocators.get()),
      initialServers_(initialServers.get()),
      jmxManagerPort_(Framework::getAvailablePort()) {
  removeServerDirectory();
  cacheXMLFiles_ = cacheXMLFiles.get();
}

Cluster::Cluster(LocatorCount initialLocators, ServerCount initialServers,
                 ConserveSockets conserveSockets, CacheXMLFiles cacheXMLFiles)
    : name_(std::string(::testing::UnitTest::GetInstance()
                            ->current_test_info()
                            ->test_suite_name()) +
            "/" +
            ::testing::UnitTest::GetInstance()->current_test_info()->name()),
      initialLocators_(initialLocators.get()),
      initialServers_(initialServers.get()) {
  jmxManagerPort_ = Framework::getAvailablePort();
  cacheXMLFiles_ = cacheXMLFiles.get();
  conserveSockets_ = conserveSockets.get();
}

Cluster::Cluster(LocatorCount initialLocators, ServerCount initialServers,
                 std::vector<uint16_t> &serverPorts)
    : name_(std::string(::testing::UnitTest::GetInstance()
                            ->current_test_info()
                            ->test_suite_name()) +
            "/" +
            ::testing::UnitTest::GetInstance()->current_test_info()->name()),
      initialLocators_(initialLocators.get()),
      initialServers_(initialServers.get()),
      jmxManagerPort_(Framework::getAvailablePort()) {
  for (uint16_t port : serverPorts) {
    serverPorts_.push_back(port);
  }
  removeServerDirectory();
}

Cluster::Cluster(Name name, LocatorCount initialLocators,
                 ServerCount initialServers, UseIpv6 useIPv6)
    : Cluster(Name(name.get()), Classpath(""), SecurityManager(""), User(""),
              Password(""), initialLocators, initialServers, CacheXMLFiles({}),
              useIPv6) {}

Cluster::Cluster(Name name, LocatorCount initialLocators,
                 ServerCount initialServers)
    : Cluster(Name(name.get()), Classpath(""), SecurityManager(""), User(""),
              Password(""), initialLocators, initialServers, CacheXMLFiles({}),
              UseIpv6(false)) {}

Cluster::Cluster(Name name, Classpath classpath,
                 SecurityManager securityManager, User user, Password password,
                 LocatorCount initialLocators, ServerCount initialServers,
                 CacheXMLFiles cacheXMLFiles, UseIpv6 useIPv6)
    : name_(name.get()),
      classpath_(classpath.get()),
      securityManager_(securityManager.get()),
      user_(user.get()),
      password_(password.get()),
      initialLocators_(initialLocators.get()),
      initialServers_(initialServers.get()) {
  jmxManagerPort_ = Framework::getAvailablePort();
  cacheXMLFiles_ = cacheXMLFiles.get();
  useIPv6_ = useIPv6.get();

  removeServerDirectory();
}

Cluster::Cluster(Name name, Classpath classpath,
                 SecurityManager securityManager, User user, Password password,
                 LocatorCount initialLocators, ServerCount initialServers)
    : name_(name.get()),
      classpath_(classpath.get()),
      securityManager_(securityManager.get()),
      user_(user.get()),
      password_(password.get()),
      initialLocators_(initialLocators.get()),
      initialServers_(initialServers.get()) {
  jmxManagerPort_ = Framework::getAvailablePort();

  removeServerDirectory();
}

Cluster::~Cluster() {
  try {
    if (started_) {
      stop();
    }
  } catch (...) {
  }
}

void Cluster::removeServerDirectory() {
  boost::filesystem::path serverDir = boost::filesystem::relative(name_);
  boost::filesystem::remove_all(serverDir);
}

apache::geode::client::Cache Cluster::createCache() { return createCache({}); }

apache::geode::client::Cache Cluster::createCache(
    const std::unordered_map<std::string, std::string> &properties) {
  return createCache(properties, SubscriptionState::Disabled);
}

apache::geode::client::Cache Cluster::createCache(
    const std::unordered_map<std::string, std::string> &properties,
    SubscriptionState state) {
  using apache::geode::client::CacheFactory;

  CacheFactory cacheFactory;

  cacheFactory.set("log-level", "none")
      .set("statistic-sampling-enabled", "false");

  for (auto &&property : properties) {
    cacheFactory.set(property.first, property.second);
  }

  auto cache = cacheFactory.create();

  auto poolFactory =
      cache.getPoolManager().createFactory().setSubscriptionEnabled(
          state == SubscriptionState::Enabled);
  applyLocators(poolFactory);
  poolFactory.create("default");

  return cache;
}

void Cluster::applyLocators(apache::geode::client::PoolFactory &poolFactory) {
  for (const auto &locator : locators_) {
    poolFactory.addLocator(locator.getAddress().address,
                           locator.getAddress().port);
  }
}

void Cluster::applyServer(apache::geode::client::PoolFactory &poolFactory,
                          ServerAddress oneServer) {
  poolFactory.addServer(oneServer.address, oneServer.port);
}

Gfsh &Cluster::getGfsh() { return gfsh_; }

std::vector<Server> &Cluster::getServers() { return servers_; }

std::vector<Locator> &Cluster::getLocators() { return locators_; }

std::string &Cluster::getClasspath() { return classpath_; }

std::string &Cluster::getSecurityManager() { return securityManager_; }

std::string &Cluster::getUser() { return user_; }

std::string &Cluster::getPassword() { return password_; }

std::vector<std::string> &Cluster::getCacheXMLFiles() { return cacheXMLFiles_; }

bool Cluster::getUseIPv6() { return useIPv6_; }

bool Cluster::getConserveSockets() { return conserveSockets_; }

void Cluster::start() { start(std::function<void()>()); }

void Cluster::start(std::function<void()> extraGfshCommands) {
  locators_.reserve(initialLocators_);
  for (size_t i = 0; i < initialLocators_; i++) {
    uint16_t port;
    if (locatorsPorts_.empty()) {
      port = Framework::getAvailablePort();
    } else {
      port = locatorsPorts_.at(i);
    }

    locators_.push_back({*this, locators_,
                         name_ + "/locator/" + std::to_string(i),
                         jmxManagerPort_, getUseIPv6(), port,
                         remoteLocatorsPorts_, distributedSystemId_});
  }

  servers_.reserve(initialServers_);
  std::string xmlFile;
  for (size_t i = 0; i < initialServers_; i++) {
    xmlFile = (cacheXMLFiles_.size() == 0) ? ""
              : cacheXMLFiles_.size() == 1 ? cacheXMLFiles_[0]
                                           : cacheXMLFiles_[i];

    uint16_t serverPort;
    if (serverPorts_.empty()) {
      serverPort = static_cast<uint16_t>(0);
    } else {
      serverPort = serverPorts_.at(i);
    }

    servers_.push_back({*this, locators_,
                        name_ + "/server/" + std::to_string(i), xmlFile,
                        getUseIPv6(), serverPort});
  }

  startLocators();

  if (extraGfshCommands) {
    extraGfshCommands();
  }

  startServers();

  started_ = true;
}

std::string Cluster::getJmxManager() {
  return locators_.begin()->getAddress().address + "[" +
         std::to_string(jmxManagerPort_) + "]";
}

uint16_t Cluster::getLocatorPort() {
  return locators_.begin()->getAddress().port;
}

void Cluster::startServers() {
  std::vector<std::future<void>> futures;

  for (auto &server : this->servers_) {
    futures.push_back(std::async(std::launch::async, [&] { server.start(); }));
  }

  for (auto &future : futures) {
    future.get();
  }
}

void Cluster::startLocators() {
  std::vector<std::future<void>> futures;

  for (auto &locator : locators_) {
    futures.push_back(std::async(std::launch::async, [&] { locator.start(); }));
  }

  // TODO hack until there is a way to either tell servers to retry or wait
  // for single future.
  for (auto &future : futures) {
    future.get();
  }
}

void Cluster::stop() {
  std::vector<std::future<void>> futures;
  for (auto &server : servers_) {
    futures.push_back(std::async(std::launch::async, [&] { server.stop(); }));
  }

  for (auto &locator : locators_) {
    futures.push_back(std::async(std::launch::async, [&] { locator.stop(); }));
  }

  for (auto &future : futures) {
    future.wait();
  }

  started_ = false;
}

void Cluster::useSsl(const bool requireSslAuthentication,
                     const std::string keystore, const std::string truststore,
                     const std::string keystorePassword,
                     const std::string truststorePassword) {
  useSsl_ = true;
  requireSslAuthentication_ = requireSslAuthentication;
  keystore_ = keystore;
  truststore_ = truststore;
  keystorePassword_ = keystorePassword;
  truststorePassword_ = truststorePassword;
}

bool Cluster::useSsl() { return useSsl_; }

void Cluster::usePropertiesFile(const std::string propertiesFile) {
  usePropertiesFile_ = true;
  propertiesFile_ = propertiesFile;
}

void Cluster::useSecurityPropertiesFile(
    const std::string securityPropertiesFile) {
  useSecurityPropertiesFile_ = true;
  securityPropertiesFile_ = securityPropertiesFile;
}

void Cluster::useHostNameForClients(const std::string hostName) {
  usePropertiesFile_ = true;
  hostName_ = hostName;
}

bool Cluster::usePropertiesFile() { return usePropertiesFile_; }
bool Cluster::useSecurityPropertiesFile() { return useSecurityPropertiesFile_; }
bool Cluster::useHostNameForClients() { return useHostNameForClients_; }

bool Cluster::requireSslAuthentication() { return requireSslAuthentication_; }

std::string Cluster::keystore() { return keystore_; }

std::string Cluster::truststore() { return truststore_; }

std::string Cluster::keystorePassword() { return keystorePassword_; }

std::string Cluster::truststorePassword() { return truststorePassword_; }

max_stars_repo_name: jupiternightingale/geode-native

max_issues_repo_name: jupiternightingale/geode-native

max_forks_repo_name: moleske/geode-native


text:
/*
 ******************************************************************************
 Project:      OWA EPANET
 Version:      2.2
 Module:       test_report.cpp
 Description:  Tests EPANET toolkit api functions
 Authors:      see AUTHORS
 Copyright:    see AUTHORS
 License:      see LICENSE
 Last Updated: 03/21/2019
 ******************************************************************************
*/

#include <boost/test/unit_test.hpp>

#include "test_toolkit.hpp"

BOOST_AUTO_TEST_SUITE (test_report)

BOOST_FIXTURE_TEST_CASE(test_rprt_anlysstats, FixtureOpenClose)
{
    int i;

    std::vector<double> test(5);
    double *array = test.data();

	std::vector<double> ref = {3.0, 7.0799498320679432e-06, 1.6680242187483429e-08,
        0.0089173150106518495, 0.99999998187144024};

    error = EN_solveH(ph);
    BOOST_REQUIRE(error == 0);

    error = EN_solveQ(ph);
    BOOST_REQUIRE(error == 0);

    for (i=EN_ITERATIONS; i<=EN_MASSBALANCE; i++) {
        error = EN_getstatistic(ph, i, array++);
        BOOST_REQUIRE(error == 0);
    }
    BOOST_CHECK(check_cdd_double(test, ref, 3));

    double temp;
    error = EN_getstatistic(ph, 8, &temp);
    BOOST_CHECK(error == 251);
}

BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: ajholguin/EPANET

max_issues_repo_name: michaeltryby/epanet-solver

max_forks_repo_name: michaeltryby/epanet-solver


text:
#pragma once

#include <boost/filesystem/path.hpp>
#include <string>

struct Config {
    unsigned int port;
    unsigned int webPort;
    unsigned int workers;
    std::string postgres;
    boost::filesystem::path dataDirectory;
};

max_stars_repo_name: svendcsvendsen/judoassistant

max_issues_repo_name: svendcsvendsen/judoassistant

max_forks_repo_name: judoassistant/judoassistant


text:
#pragma once
#include <boost/container/flat_map.hpp>
#include <boost/any.hpp>

#include <scorum/chain/dba/dba.hpp>

namespace scorum {
namespace chain {

class database;

namespace dba {

template <typename TObject> class db_accessor;

struct db_accessor_factory
{
    db_accessor_factory(db_index& db);

    template <typename TObject> db_accessor<TObject>& get_dba() const;

    mutable boost::container::flat_map<boost::typeindex::type_index, boost::any> _db_accessors;
    db_index& _db;
};

} // dba
} // chain
} // scorum

max_stars_repo_name: scorum/scorum

max_issues_repo_name: Scorum/Scorum

max_forks_repo_name: Scorum/Scorum


text:
#ifndef BOOST_MPL_MAP_AUX_AT_IMPL_HPP_INCLUDED
#define BOOST_MPL_MAP_AUX_AT_IMPL_HPP_INCLUDED

// Copyright Aleksey Gurtovoy 2003-2004
// Copyright David Abrahams 2003-2004
//
// 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)
//
// See http://www.boost.org/libs/mpl for documentation.

// $Id: at_impl.hpp 49267 2008-10-11 06:19:02Z agurtovoy $
// $Date: 2008-10-11 02:19:02 -0400 (Sat, 11 Oct 2008) $
// $Revision: 49267 $

#include <boost/mpl/at_fwd.hpp>
#include <boost/mpl/long.hpp>
#include <boost/mpl/map/aux_/tag.hpp>
#include <boost/mpl/aux_/order_impl.hpp>
#include <boost/mpl/aux_/overload_names.hpp>
#include <boost/mpl/aux_/type_wrapper.hpp>
#include <boost/mpl/aux_/ptr_to_ref.hpp>
#include <boost/mpl/aux_/static_cast.hpp>
#include <boost/mpl/aux_/config/typeof.hpp>
#include <boost/mpl/aux_/config/ctps.hpp>

#if !defined(BOOST_MPL_CFG_TYPEOF_BASED_SEQUENCES)
#   include <boost/mpl/eval_if.hpp>
#   include <boost/mpl/pair.hpp>
#   include <boost/mpl/void.hpp>
#   include <boost/mpl/aux_/config/static_constant.hpp>
#endif

namespace boost { namespace mpl {

#if defined(BOOST_MPL_CFG_TYPEOF_BASED_SEQUENCES)

template< typename Map, typename Key >
struct m_at
{
    typedef aux::type_wrapper<Key> key_;
    typedef __typeof__( BOOST_MPL_AUX_OVERLOAD_CALL_VALUE_BY_KEY(
          Map
        , BOOST_MPL_AUX_STATIC_CAST(key_*, 0)
        ) ) type;
};

template<>
struct at_impl< aux::map_tag >
{
    template< typename Map, typename Key > struct apply
        : aux::wrapped_type< typename m_at<
              Map
            , Key
            >::type >
    {
    };
};

// agurt 31/jan/04: two-step implementation for the sake of GCC 3.x
template< typename Map, long order >
struct item_by_order_impl
{
    typedef __typeof__( BOOST_MPL_AUX_OVERLOAD_CALL_ITEM_BY_ORDER(
          Map
        , BOOST_MPL_AUX_STATIC_CAST(long_<order>*, 0)
        ) ) type;
};

template< typename Map, long order >
struct item_by_order
    : aux::wrapped_type<
          typename item_by_order_impl<Map,order>::type
        >
{
};

#else // BOOST_MPL_CFG_TYPEOF_BASED_SEQUENCES

#   if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)

template< typename Map, long n > struct m_at
{
    typedef void_ type;
};

#   else

template< long n > struct m_at_impl
{
    template< typename Map > struct result_
    {
        typedef void_ type;
    };
};

template< typename Map, long n > struct m_at
{
    typedef typename m_at_impl<n>::result_<Map>::type type;
};

#   endif

template<>
struct at_impl< aux::map_tag >
{
    template< typename Map, typename Key > struct apply
    {
        typedef typename m_at< Map, (x_order_impl<Map,Key>::value - 2) >::type item_;
        typedef typename eval_if<
              is_void_<item_>
            , void_
            , second<item_>
            >::type type;
    };
};

template< typename Map, long order > struct is_item_masked
{
    BOOST_STATIC_CONSTANT(bool, value =
          sizeof( BOOST_MPL_AUX_OVERLOAD_CALL_IS_MASKED(
              Map
            , BOOST_MPL_AUX_STATIC_CAST(long_<order>*, 0)
            ) ) == sizeof(aux::yes_tag)
        );
};

template< typename Map, long order > struct item_by_order
{
    typedef typename eval_if_c<
          is_item_masked<Map,order>::value
        , void_
        , m_at<Map,(order - 2)>
        >::type type;
};

#endif

}}

#endif // BOOST_MPL_SET_AUX_AT_IMPL_HPP_INCLUDED

max_stars_repo_name: MIPT-ILab/mdsp

max_issues_repo_name: MIPT-ILab/mdsp

max_forks_repo_name: MIPT-ILab/mdsp


text:
#include "StringParserImpl.hpp"

#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
//#include <boost/spirit/home/phoenix/container.hpp>
//#include <boost/spirit/home/phoenix/bind/bind_member_function.hpp>
//#include <boost/spirit/home/phoenix/statement/sequence.hpp>
#include "../../parsing/StringTerminal.hpp"
#include "../../parsing/EmptyNode.hpp"

namespace bs = boost::spirit;
namespace asli = axis::services::language::iterators;
namespace aslp = axis::services::language::parsing;
namespace aslppi = axis::services::language::primitives::impl;

aslppi::StringParserImpl::StringParserImpl( bool escapedString )
{
	// initialize grammar
	if (escapedString)
	{
		_char_rule = bs::qi::lexeme[_T("\\\\")][bs::qi::_val = _T("\\")] |
                 bs::qi::lexeme[_T("\\\"")][bs::qi::_val = _T("\"")] |
                 (bs::qi::char_ - _T("\"") - _T("\\"))[bs::qi::_val = bs::qi::_1];
	}
	else
	{
		_char_rule = (bs::qi::char_ - _T("\""));
	}
	_string_rule = (bs::qi::lexeme[_T("\"")] >>
                 *_char_rule[bs::qi::_val += bs::qi::_1] >>
                 bs::qi::lexeme[_T("\"")]);
}

aslppi::StringParserImpl::~StringParserImpl( void )
{
	/* nothing to do here */
}

aslp::ParseResult aslppi::StringParserImpl::DoParse( const asli::InputIterator& begin,
                                                     const asli::InputIterator& end,
                                                     bool trimSpaces /*= true*/ ) const
{
	asli::InputIterator it(begin);
	String value;
	// remove whitespaces before, if asked
	if(trimSpaces) bs::qi::parse(it, end, *bs::qi::blank);
	bool ret = bs::qi::parse(it, end, _string_rule, value);
	// remove whitespaces at the end, if asked
	if(trimSpaces) bs::qi::parse(it, end, *bs::qi::blank);
	if (ret)
	{
		return aslp::ParseResult(aslp::ParseResult::MatchOk, *new aslp::StringTerminal(value), it);
	}
	return aslp::ParseResult(aslp::ParseResult::FailedMatch, *new aslp::EmptyNode(), it);
}

max_stars_repo_name: renato-yuzup/axis-fem

max_issues_repo_name: renato-yuzup/axis-fem

max_forks_repo_name: renato-yuzup/axis-fem


text:
//==============================================================================
//         Copyright 2003 - 2011 LASMEA UMR 6602 CNRS/Univ. Clermont II
//         Copyright 2009 - 2011 LRI    UMR 8623 CNRS/Univ Paris Sud XI
//
//          Distributed under the Boost Software License, Version 1.0.
//                 See accompanying file LICENSE.txt or copy at
//                     http://www.boost.org/LICENSE_1_0.txt
//==============================================================================
#ifndef BOOST_SIMD_TOOLBOX_ARITHMETIC_FUNCTIONS_SIMD_COMMON_ABS_HPP_INCLUDED
#define BOOST_SIMD_TOOLBOX_ARITHMETIC_FUNCTIONS_SIMD_COMMON_ABS_HPP_INCLUDED
#include <boost/simd/toolbox/arithmetic/functions/abs.hpp>
#include <boost/dispatch/meta/as_integer.hpp>
#include <boost/simd/include/constants/mzero.hpp>
#include <boost/simd/include/constants/maxleftshift.hpp>
#include <boost/simd/include/functions/simd/bitwise_notand.hpp>
#include <boost/simd/include/functions/simd/shri.hpp>
#include <boost/simd/include/functions/simd/negate.hpp>

namespace boost { namespace simd { namespace ext
{
  BOOST_SIMD_FUNCTOR_IMPLEMENTATION( boost::simd::tag::abs_, tag::cpu_
                            , (A0)(X)
                            , ((simd_<signed_<A0>,X>))
                            )
  {
    typedef A0 result_type;
    BOOST_SIMD_FUNCTOR_CALL(1)
    {
      typedef typename dispatch::meta::as_integer<result_type, signed>::type int_type;
      typedef typename meta::scalar_of<int_type>::type                      sint_type;
      result_type const s = shri(a0, Maxleftshift<sint_type>());
      return (a0-s)^(-s);
    }
  };

  BOOST_SIMD_FUNCTOR_IMPLEMENTATION( boost::simd::tag::abs_, tag::cpu_
                            , (A0)(X)
                            , ((simd_<unsigned_<A0>,X>))
                            )
  {
    typedef A0 result_type;
    BOOST_SIMD_FUNCTOR_CALL(1) { return a0; }
  };

  BOOST_SIMD_FUNCTOR_IMPLEMENTATION( boost::simd::tag::abs_, tag::cpu_
                            , (A0)(X)
                            , ((simd_<floating_<A0>,X>))
                            )
  {
    typedef A0 result_type;
    BOOST_SIMD_FUNCTOR_CALL(1)
    {
      return bitwise_notand(Mzero<result_type>(),a0);
    }
  };
} } }

#endif