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

#include <gtest/gtest.h>

#include <boost/variant.hpp>
#include "outcome/outcome.hpp"
#include "common/blob.hpp"
#include "common/buffer.hpp"
#include "common/visitor.hpp"
#include "primitives/block.hpp"
#include "primitives/block_id.hpp"
#include "primitives/common.hpp"
#include "primitives/digest.hpp"
#include "primitives/extrinsic.hpp"
#include "primitives/inherent_data.hpp"
#include "primitives/parachain_host.hpp"
#include "primitives/scheduled_change.hpp"
#include "primitives/session_key.hpp"
#include "primitives/transaction_validity.hpp"
#include "primitives/version.hpp"
#include "scale/scale.hpp"
#include "scale/scale_error.hpp"
#include "testutil/literals.hpp"
#include "testutil/outcome.hpp"
#include "testutil/primitives/mp_utils.hpp"

using kagome::common::Blob;
using kagome::common::Buffer;
using kagome::common::Hash256;
using kagome::primitives::ApiId;
using kagome::primitives::AuthorityId;
using kagome::primitives::Block;
using kagome::primitives::BlockHeader;
using kagome::primitives::BlockId;
using kagome::primitives::Digest;
using kagome::primitives::Extrinsic;
using kagome::primitives::InherentData;
using kagome::primitives::InherentIdentifier;
using kagome::primitives::InvalidTransaction;
using kagome::primitives::PreRuntime;
using kagome::primitives::TransactionValidity;
using kagome::primitives::UnknownTransaction;
using kagome::primitives::ValidTransaction;
using kagome::primitives::Version;
using scale::ByteArray;
using scale::ScaleDecoderStream;
using scale::ScaleEncoderStream;

using scale::decode;
using scale::encode;

using testutil::createHash256;

/**
 * @class Primitives is a test fixture which contains useful data
 */
class Primitives : public testing::Test {
  void SetUp() override {
    block_id_hash_ = Hash256::fromHex(
                         "000102030405060708090A0B0C0D0E0F"
                         "101112131415161718191A1B1C1D1E1F")
                         .value();
  }

 protected:
  using array = std::array<uint8_t, 8u>;
  /// block header and corresponding scale representation
  BlockHeader block_header_{createHash256({0}),     // parent_hash
                            2,                      // number: number
                            createHash256({1}),     // state_root
                            createHash256({2}),     // extrinsic root
                            Digest{PreRuntime{}}};  // buffer: digest;
  /// Extrinsic instance and corresponding scale representation
  Extrinsic extrinsic_{{1, 2, 3}};
  /// block instance and corresponding scale representation
  Block block_{block_header_, {extrinsic_}};
  /// Version instance and corresponding scale representation
  Version version_{
      "qwe",  // spec name
      "asd",  // impl_name
      1,      // auth version
      42,     // spec version
      2,      // db version
      {{Blob{array{'1', '2', '3', '4', '5', '6', '7', '8'}}, 1},  // ApiId_1
       {Blob{array{'8', '7', '6', '5', '4', '3', '2', '1'}}, 2}}  // ApiId_2
  };
  /// block id variant number alternative and corresponding scale representation
  BlockId block_id_number_{1ull};
  /// block id variant hash alternative and corresponding scale representation
  BlockId block_id_hash_;

  /// TransactionValidity variant instance as Valid alternative and
  /// corresponding scale representation
  ValidTransaction valid_transaction_{.priority = 1,
                                      .requires = {{0, 1}, {2, 3}},
                                      .provides = {{4, 5}, {6, 7, 8}},
                                      .longevity = 2,
                                      .propagate = true};
};

/**
 * @given predefined block header
 * @when encodeBlockHeader is applied and result is decoded back
 * @then decoded block is equal to predefined block
 */
TEST_F(Primitives, EncodeBlockHeaderSuccess) {
  EXPECT_OUTCOME_TRUE(val, encode(block_header_));
  EXPECT_OUTCOME_TRUE(decoded_header, decode<BlockHeader>(val));
  ASSERT_EQ(block_header_, decoded_header);
}

/**
 * @given predefined extrinsic containing sequence {12, 1, 2, 3}
 * @when encodeExtrinsic is applied
 * @then the same expected buffer obtained {12, 1, 2, 3}
 */
TEST_F(Primitives, EncodeExtrinsicSuccess) {
  EXPECT_OUTCOME_TRUE(val, encode(extrinsic_));
  EXPECT_OUTCOME_TRUE(decoded_extrinsic, decode<Extrinsic>(val));
  ASSERT_EQ(extrinsic_, decoded_extrinsic);
}

/**
 * @given predefined instance of Block
 * @when encodeBlock is applied
 * @then expected result obtained
 */
TEST_F(Primitives, EncodeBlockSuccess) {
  EXPECT_OUTCOME_TRUE(res, encode(block_));
  EXPECT_OUTCOME_TRUE(decoded_block, decode<Block>(res));
  ASSERT_EQ(block_, decoded_block);
}

/// Version

/**
 * @given version instance and predefined match for encoded instance
 * @when codec_->encodeVersion() is applied
 * @then obtained result equal to predefined match
 */
TEST_F(Primitives, EncodeVersionSuccess) {
  EXPECT_OUTCOME_TRUE(val, encode(version_));
  EXPECT_OUTCOME_TRUE(decoded_version, decode<Version>(val));
  ASSERT_EQ(decoded_version, version_);
}

/// BlockId

/**
 * @given block id constructed as hash256 and predefined match
 * @when codec_->encodeBlockId() is applied
 * @then obtained result matches predefined value
 */
TEST_F(Primitives, EncodeBlockIdHash256Success) {
  EXPECT_OUTCOME_TRUE(val, encode(block_id_hash_))
  EXPECT_OUTCOME_TRUE(decoded_block_id, decode<BlockId>(val));
  ASSERT_EQ(decoded_block_id, block_id_hash_);
}

/**
 * @given block id constructed as BlockNumber and predefined match
 * @when codec_->encodeBlockId() is applied
 * @then obtained result matches predefined value
 */
TEST_F(Primitives, EncodeBlockIdBlockNumberSuccess) {
  EXPECT_OUTCOME_TRUE(val, encode(block_id_number_))
  EXPECT_OUTCOME_TRUE(decoded_block_id, decode<BlockId>(val));
  ASSERT_EQ(decoded_block_id, block_id_number_);
}

/// TransactionValidity

/**
 * @given TransactionValidity instance as Invalid and predefined match
 * @when codec_->encodeTransactionValidity() is applied
 * @then obtained result matches predefined value
 */
TEST_F(Primitives, EncodeTransactionValidityInvalidSuccess) {
  InvalidTransaction invalid{1};
  EXPECT_OUTCOME_TRUE(val, encode(invalid))
  EXPECT_OUTCOME_TRUE(decoded_validity, decode<InvalidTransaction>(val));
  ASSERT_EQ(decoded_validity, invalid);
}

/**
 * @given TransactionValidity instance as Unknown and predefined match
 * @when codec_->encodeTransactionValidity() is applied
 * @then obtained result matches predefined value
 */
TEST_F(Primitives, EncodeTransactionValidityUnknown) {
  UnknownTransaction unknown{2};
  EXPECT_OUTCOME_TRUE(val, encode(unknown))
  EXPECT_OUTCOME_TRUE(decoded_validity, decode<UnknownTransaction>(val));
  ASSERT_EQ(decoded_validity, unknown);
}

/**
 * @given TransactionValidity instance as Valid and predefined match
 * @when codec_->encodeTransactionValidity() is applied
 * @then obtained result matches predefined value
 */
TEST_F(Primitives, EncodeTransactionValiditySuccess) {
  ValidTransaction t = valid_transaction_;  // make it variant type
  EXPECT_OUTCOME_TRUE(val, encode(t))
  EXPECT_OUTCOME_TRUE(decoded_validity, decode<ValidTransaction>(val));
  ASSERT_EQ(decoded_validity, valid_transaction_);
}

/**
 * @given vector of authority ids
 * @when encode and decode this vector using scale codec
 * @then decoded vector of authority ids matches the original one
 */
TEST_F(Primitives, EncodeDecodeAuthorityIdsSuccess) {
  AuthorityId id1, id2;
  id1.id.fill(1u);
  id2.id.fill(2u);
  std::vector<AuthorityId> original{id1, id2};
  EXPECT_OUTCOME_TRUE(res, encode(original))

  EXPECT_OUTCOME_TRUE(decoded, decode<std::vector<AuthorityId>>(res))
  ASSERT_EQ(original, decoded);
}

TEST_F(Primitives, EncodeInherentSampleFromSubstrate) {
  std::string encoded_str = {'\b', 't',  'e',  's',    't',  'i',  'n',  'h',
                             '0',  '4',  '\f', '\x01', '\0', '\0', '\0', '\x02',
                             '\0', '\0', '\0', '\x03', '\0', '\0', '\0', 't',
                             'e',  's',  't',  'i',    'n',  'h',  '1',  '\x10',
                             '\a', '\0', '\0', '\0'};

  EXPECT_OUTCOME_TRUE(test_id1, InherentIdentifier::fromString("testinh0"));
  std::vector<uint32_t> data1{1, 2, 3};

  EXPECT_OUTCOME_TRUE(test_id2, InherentIdentifier::fromString("testinh1"));
  uint32_t data2 = 7;

  auto encoded_buf = Buffer().put(encoded_str);
  EXPECT_OUTCOME_TRUE(dec_data, decode<InherentData>(encoded_buf));

  EXPECT_OUTCOME_TRUE(dec_data1, dec_data.getData<decltype(data1)>(test_id1));
  EXPECT_EQ(data1, dec_data1);

  EXPECT_OUTCOME_TRUE(dec_data2, dec_data.getData<decltype(data2)>(test_id2));
  EXPECT_EQ(data2, dec_data2);
}

/**
 * @given inherent data
 * @when  encode and decode it
 * @then decoded result is exactly the original inherent data
 */
TEST_F(Primitives, EncodeInherentDataSuccess) {
  using array = std::array<uint8_t, 8u>;
  InherentData data;
  EXPECT_OUTCOME_TRUE(id1, InherentIdentifier::fromString("testinh0"));
  EXPECT_OUTCOME_TRUE(id2, InherentIdentifier::fromString("testinh1"));
  InherentIdentifier id3{array{3}};
  std::vector<uint32_t> data1{1, 2, 3};
  uint32_t data2 = 7;
  Buffer data3{1, 2, 3, 4};
  EXPECT_OUTCOME_TRUE_void(r1, data.putData(id1, data1));
  EXPECT_OUTCOME_TRUE_void(r2, data.putData(id2, data2));
  EXPECT_OUTCOME_TRUE_void(r3, data.putData(id3, data3));
  EXPECT_OUTCOME_FALSE_void(_, data.putData(id1, data2));

  ASSERT_EQ(data.getData<decltype(data1)>(id1).value(), data1);
  ASSERT_EQ(data.getData<decltype(data2)>(id2).value(), data2);
  ASSERT_EQ(data.getData<decltype(data3)>(id3).value(), data3);

  Buffer data4{1, 3, 5, 7};
  data.replaceData(id3, data4);
  ASSERT_EQ(data.getData<decltype(data4)>(id3).value(), data4);

  EXPECT_OUTCOME_TRUE(enc_data, encode(data))
  EXPECT_OUTCOME_TRUE(dec_data, decode<InherentData>(enc_data))

  ASSERT_EQ(dec_data.getData<decltype(data1)>(id1).value(), data1);

  EXPECT_EQ(data, dec_data);
}

max_stars_repo_name: soramitsu/kagome

max_issues_repo_name: soramitsu/kagome

max_forks_repo_name: soramitsu/kagome


text:
// Copyright (c) 2016
// Author: Chrono Law
#include <std.hpp>
using namespace std;

#include <boost/next_prior.hpp>
#include <boost/intrusive/slist.hpp>
using namespace boost::intrusive;

///////////////////////////////////////

void case1()
{
    typedef slist_node<void*>       node_t;
    typedef slist_node_traits<void*>    node_traits_t;
    typedef circular_slist_algorithms<node_traits_t> algo;

    node_t n1, n2;

    algo::init_header(&n1);
    assert(algo::count(&n1) == 1);

    algo::link_after(&n1, &n2);
    assert(algo::count(&n1) == 2);

    algo::unlink(&n1);
    assert(algo::count(&n2) == 1);

}

///////////////////////////////////////

int main()
{
    std::cout << "hello intrusive sample4" << std::endl;

    case1();
}

ext: cpp


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.
 *
 *************************************************************/

#ifndef INCLUDED_SLIDESHOW_VIEWLAYER_HXX
#define INCLUDED_SLIDESHOW_VIEWLAYER_HXX

#include <sal/config.h>
#include <boost/shared_ptr.hpp>

namespace basegfx
{
    class B1DRange;
    class B2DRange;
    class B2DVector;
    class B2DHomMatrix;
    class B2DPolyPolygon;
}
namespace cppcanvas
{
    class Canvas;
    class CustomSprite;
}

/* Definition of ViewLayer interface */

namespace slideshow
{
    namespace internal
    {
        class View;

        class ViewLayer
        {
        public:
            virtual ~ViewLayer() {}

            /** Query whether layer displays on given view.

                @return true, if this layer displays on the given
                view.
            */
            virtual bool isOnView(boost::shared_ptr<View> const& rView) const = 0;

            /** Get the associated canvas of this layer.

	        	The canvas returned by this method must not change, as
                long as this object is alive.
            */
            virtual boost::shared_ptr< cppcanvas::Canvas > getCanvas() const = 0;

            /** Clear the clipped view layer area

                This method clears the area inside the clip polygon,
                if none is set, the transformed unit rectangle of the
                view.
             */
            virtual void clear() const = 0;

            /** Clear the complete view

                This method clears the full view area (not only the
                transformed unit rectangle, or within the clip). If
                this ViewLayer represents the background layer, the
                whole XSlideShowView is cleared. If this ViewLayer is
                implemented using sprites (i.e. one of the upper
                layers), the sprite is cleared to fully transparent.
             */
            virtual void clearAll() const = 0;

            /** Create a sprite for this layer

	        	@param rSpriteSizePixel
                Sprite size in device pixel

                @param nPriority
                Sprite priority. This value determines the priority of
                this sprite, relative to all other sprites of this
                ViewLayer. The higher the priority, the closer to the
                foreground the sprite will be.

                @return the sprite, or NULL on failure (or if this
                canvas does not support sprites).
            */
            virtual boost::shared_ptr< cppcanvas::CustomSprite >
            createSprite( const basegfx::B2DVector& rSpriteSizePixel,
                          double                    nPriority ) const = 0;

            /** Set the layer priority range

	        	This method influences the relative priority of this
	        	layer, i.e. the z position in relation to other layers
	        	on the parent view. The higher the priority range, the
	        	further in front the layer resides.

                @param rRange
                Priority range, must be in the range [0,1]
            */
            virtual void setPriority( const basegfx::B1DRange& rRange ) = 0;

            /** Get the overall view transformation.

	        	This method should not simply return the
	        	underlying canvas' transformation, but rather provide
	        	a layer above that. This enables clients of the
	        	slideshow to set their own user space transformation
	        	at the canvas, whilst the slideshow adds their
	        	transformation on top of that. Concretely, this method
	        	returns the user transform (implicitely calculated
	        	from the setViewSize() method), combined with the view
	        	transformation.
            */
            virtual basegfx::B2DHomMatrix getTransformation() const = 0;

            /** Get the overall view transformation.

	        	Same transformation as with getTransformation(), only
	        	that you can safely use this one to position sprites
	        	on screen (no ViewLayer offsets included whatsoever).
            */
            virtual basegfx::B2DHomMatrix getSpriteTransformation() const = 0;

            /** Set clipping on this view layer.

            	@param rClip
                Clip poly-polygon to set. The polygon is interpreted
                in the user coordinate system, i.e. the view layer has
                the size as given by setViewSize() on its
                corresponding View.
             */
            virtual void setClip( const basegfx::B2DPolyPolygon& rClip ) = 0;

            /** Resize this view layer.

            	@param rArea
                New area to cover. The area is interpreted in the user
                coordinate system, i.e. relative to the size as given
                by setViewSize() on the corresponding View.

                @return true, if layer was actually resized (which
                invalidates its content)
             */
            virtual bool resize( const basegfx::B2DRange& rArea ) = 0;

        };

        typedef boost::shared_ptr< ViewLayer > ViewLayerSharedPtr;
    }
}

#endif /* INCLUDED_SLIDESHOW_VIEWLAYER_HXX */

ext: hxx

max_stars_repo_name: jimjag/openoffice

max_issues_repo_name: ackza/openoffice

max_forks_repo_name: ackza/openoffice


text:
#include <Eigen/Dense>
#include <Eigen/Sparse>
#include <memory>
#include <cmath>
#include <iostream>

#include "sparsetensor.h"
#include "bpmfutils.h"
#include "noisemodels.h"

using namespace Eigen;

////  AdaptiveGaussianNoise  ////
void AdaptiveGaussianNoise::init(MatrixData & matrixData) {
  double se = 0.0;
  double mean_value = matrixData.mean_value;

#pragma omp parallel for schedule(dynamic, 4) reduction(+:se)
  for (int k = 0; k < matrixData.Y.outerSize(); ++k) {
    for (SparseMatrix<double>::InnerIterator it(matrixData.Y, k); it; ++it) {
      se += square(it.value() - mean_value);
    }
  }

  var_total = se / matrixData.Y.nonZeros();
  if (var_total <= 0.0 || std::isnan(var_total)) {
    // if var cannot be computed using 1.0
    var_total = 1.0;
  }
  // Var(noise) = Var(total) / (SN + 1)
  alpha     = (sn_init + 1.0) / var_total;
  alpha_max = (sn_max + 1.0)  / var_total;
}

void AdaptiveGaussianNoise::init(TensorData & data) {
  double se = 0.0;
  double mean_value = data.mean_value;

  auto& sparseMode   = (*data.Y)[0];
  VectorXd & values  = sparseMode->values;

#pragma omp parallel for schedule(dynamic, 4) reduction(+:se)
  for (int i = 0; i < values.size(); i++) {
    se += square(values(i) - mean_value);
  }
  var_total = se / values.size();
  if (var_total <= 0.0 || std::isnan(var_total)) {
    var_total = 1.0;
  }
  // Var(noise) = Var(total) / (SN + 1)
  alpha     = (sn_init + 1.0) / var_total;
  alpha_max = (sn_max + 1.0)  / var_total;
}

void AdaptiveGaussianNoise::update(MatrixData & data, std::vector< std::unique_ptr<Eigen::MatrixXd> > & samples)
{
  double sumsq = 0.0;
  MatrixXd & U = *samples[0];
  MatrixXd & V = *samples[1];

  Eigen::SparseMatrix<double> & train = data.Y;
  double mean_value = data.mean_value;

#pragma omp parallel for schedule(dynamic, 4) reduction(+:sumsq)
  for (int j = 0; j < train.outerSize(); j++) {
    auto Vj = V.col(j);
    for (SparseMatrix<double>::InnerIterator it(train, j); it; ++it) {
      double Yhat = Vj.dot( U.col(it.row()) ) + mean_value;
      sumsq += square(Yhat - it.value());
    }
  }
  // (a0, b0) correspond to a prior of 1 sample of noise with full variance
  double a0 = 0.5;
  double b0 = 0.5 * var_total;
  double aN = a0 + train.nonZeros() / 2.0;
  double bN = b0 + sumsq / 2.0;
  alpha = rgamma(aN, 1.0 / bN);
  if (alpha > alpha_max) {
    alpha = alpha_max;
  }
}

void AdaptiveGaussianNoise::update(TensorData & data, std::vector< std::unique_ptr<Eigen::MatrixXd> > & samples)
{
  double sumsq = 0.0;
  double mean_value = data.mean_value;

  auto& sparseMode = (*data.Y)[0];
  auto& U = samples[0];

  const int nmodes = samples.size();
  const int num_latents = U->rows();

#pragma omp parallel for schedule(dynamic, 4) reduction(+:sumsq)
  for (int n = 0; n < data.dims(0); n++) {
    Eigen::VectorXd u = U->col(n);
    for (int j = sparseMode->row_ptr(n);
             j < sparseMode->row_ptr(n + 1);
             j++)
    {
      VectorXi idx = sparseMode->indices.row(j);
      // computing prediction from tensor
      double Yhat = mean_value;
      for (int d = 0; d < num_latents; d++) {
        double tmp = u(d);

        for (int m = 1; m < nmodes; m++) {
          tmp *= (*samples[m])(d, idx(m - 1));
        }
        Yhat += tmp;
      }
      sumsq += square(Yhat - sparseMode->values(j));
    }

  }
  double a0 = 0.5;
  double b0 = 0.5;
  double aN = a0 + sparseMode->values.size() / 2.0;
  double bN = b0 + sumsq / 2.0;
  alpha = rgamma(aN, 1.0 / bN);
  if (alpha > alpha_max) {
    alpha = alpha_max;
  }
}

inline double nCDF(double val) {return 0.5 * erfc(-val * M_SQRT1_2);}

/////  evalModel functions
void ProbitNoise::evalModel(MatrixData & data, const int n, Eigen::VectorXd & predictions, Eigen::VectorXd & predictions_var,
        std::vector< std::unique_ptr<Eigen::MatrixXd> > & samples) {
  const unsigned N = data.Ytest.nonZeros();
  Eigen::VectorXd pred(N);
  Eigen::VectorXd test(N);
  Eigen::MatrixXd & rows = *samples[0];
  Eigen::MatrixXd & cols = *samples[1];

// #pragma omp parallel for schedule(dynamic,8) reduction(+:se, se_avg) <- dark magic :)
  for (int k = 0; k < data.Ytest.outerSize(); ++k) {
    int idx = data.Ytest.outerIndexPtr()[k];
    for (Eigen::SparseMatrix<double>::InnerIterator it(data.Ytest,k); it; ++it) {
     pred[idx] = nCDF(cols.col(it.col()).dot(rows.col(it.row())));
     test[idx] = it.value();

      // https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm
      double pred_avg;
      if (n == 0) {
        pred_avg = pred[idx];
      } else {
        double delta = pred[idx] - predictions[idx];
        pred_avg = (predictions[idx] + delta / (n + 1));
        predictions_var[idx] += delta * (pred[idx] - pred_avg);
      }
      predictions[idx++] = pred_avg;

   }
  }
  auc_test_onesample = auc(pred,test);
  auc_test = auc(predictions, test);
}

void FixedGaussianNoise::evalModel(MatrixData & data, const int n, Eigen::VectorXd & predictions, Eigen::VectorXd & predictions_var,
        std::vector< std::unique_ptr<Eigen::MatrixXd> > & samples) {
   auto rmse = eval_rmse(data.Ytest, n, predictions, predictions_var, *samples[1], *samples[0], data.mean_value);
   rmse_test = rmse.second;
   rmse_test_onesample = rmse.first;
}

void AdaptiveGaussianNoise::evalModel(MatrixData & data, const int n, Eigen::VectorXd & predictions, Eigen::VectorXd & predictions_var,
        std::vector< std::unique_ptr<Eigen::MatrixXd> > & samples) {
   auto rmse = eval_rmse(data.Ytest, n, predictions, predictions_var, *samples[1], *samples[0], data.mean_value);
   rmse_test = rmse.second;
   rmse_test_onesample = rmse.first;
}

// evalModel for TensorData
void ProbitNoise::evalModel(TensorData & data, const int Nepoch, Eigen::VectorXd & predictions, Eigen::VectorXd & predictions_var,
        std::vector< std::unique_ptr<Eigen::MatrixXd> > & samples) {
  // TODO
  throw std::runtime_error("ProbitNoise::evalModel unimplemented.");
}

void FixedGaussianNoise::evalModel(TensorData & data, const int n, Eigen::VectorXd & predictions, Eigen::VectorXd & predictions_var,
        std::vector< std::unique_ptr<Eigen::MatrixXd> > & samples) {
  auto rmse = eval_rmse_tensor(data.Ytest, n, predictions, predictions_var, samples, data.mean_value);
  rmse_test = rmse.second;
  rmse_test_onesample = rmse.first;
}

void AdaptiveGaussianNoise::evalModel(TensorData & data, const int n, Eigen::VectorXd & predictions, Eigen::VectorXd & predictions_var,
        std::vector< std::unique_ptr<Eigen::MatrixXd> > & samples) {
  auto rmse = eval_rmse_tensor(data.Ytest, n, predictions, predictions_var, samples, data.mean_value);
  rmse_test = rmse.second;
  rmse_test_onesample = rmse.first;
}

//evalModel for Censored data
void FixedGaussianNoise::evalModel(MatrixDataCensored & data, const int n, Eigen::VectorXd & predictions, Eigen::VectorXd & predictions_var,
        std::vector< std::unique_ptr<Eigen::MatrixXd> > & samples) {
  auto rmse = eval_rmse(data.Ytest, n, predictions, predictions_var, *samples[1], *samples[0], data.mean_value);
  rmse_test = rmse.second;
  rmse_test_onesample = rmse.first;
}

void AdaptiveGaussianNoise::evalModel(MatrixDataCensored & data, const int n, Eigen::VectorXd & predictions, Eigen::VectorXd & predictions_var,
        std::vector< std::unique_ptr<Eigen::MatrixXd> > & samples) {
  //TODO
  throw std::runtime_error("evalModel not implemented for Censored Data and adaptive noise");
}

void ProbitNoise::evalModel(MatrixDataCensored & data, const int n, Eigen::VectorXd & predictions, Eigen::VectorXd & predictions_var,
        std::vector< std::unique_ptr<Eigen::MatrixXd> > & samples) {
//TODO
throw std::runtime_error("evalModel not implemented for Censored Data and probit noise");
}

max_stars_repo_name: edebrouwer/macau

max_issues_repo_name: edebrouwer/macau

max_forks_repo_name: edebrouwer/macau


text:
//
//   Project Name:        KratosSolidMechanicsApplication $
//   Created by:          $Author:            JMCarbonell $
//   Last modified by:    $Co-Author:                     $
//   Date:                $Date:                July 2013 $
//   Revision:            $Revision:                  0.0 $
//
//

// System includes
#include <pybind11/pybind11.h>
//#include <boost/python/suite/indexing/vector_indexing_suite.hpp>

// External includes

// Project includes
#include "includes/define.h"
#include "includes/constitutive_law.h"
#include "includes/node.h"
#include "includes/variables.h"
#include "includes/mesh.h"
#include "includes/element.h"
#include "includes/condition.h"
#include "includes/properties.h"

//#include "python/pointer_vector_set_python_interface.h"
#include "python/variable_indexing_python.h"
#include "python/add_mesh_to_python.h"

//Application includes
#include "custom_python/add_custom_constitutive_laws_to_python.h"

//constitutive laws
#include "custom_constitutive/zarate_law.hpp"
#include "custom_constitutive/fem_dem_elastic_law.hpp"

namespace Kratos
{
namespace Python
{
	void  AddCustomConstitutiveLawsToPython(pybind11::module& m)
	{
		py::class_<ZarateLaw, typename ZarateLaw::Pointer, ConstitutiveLaw >
			(m, "ZarateLaw").def(py::init<>() )
			;

		py::class_<FemDemElasticLaw, typename FemDemElasticLaw::Pointer, ConstitutiveLaw >
			(m, "FemDemElasticLaw").def(py::init<>() )
			;
	}
}  // namespace Python.
}  // namespace Kratos.


text:
// Copyright (C) 2006 The Trustees of Indiana University.

// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

//  Authors: Douglas Gregor
//           Andrew Lumsdaine

// This file contains the "unsafe_serialize" routine, which transforms
// types they may not be serializable (such as void*) into
// serializable equivalents.
#ifndef BOOST_PROPERTY_MAP_UNSAFE_SERIALIZE_HPP
#define BOOST_PROPERTY_MAP_UNSAFE_SERIALIZE_HPP

#include <boost/mpi/datatype.hpp>
#include <boost/serialization/is_bitwise_serializable.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/mpl/if.hpp>
#include <boost/cstdint.hpp>
#include <boost/static_assert.hpp>
#include <boost/type_traits.hpp>
#include <utility>

BOOST_IS_BITWISE_SERIALIZABLE(void*)
namespace boost { namespace mpi {
    template<> struct is_mpi_datatype<void*> : mpl::true_ { };
} } // end namespace boost::mpi

namespace boost {
  typedef mpl::if_c<(sizeof(int) == sizeof(void*)),
                    int,
                    mpl::if_c<(sizeof(long) == sizeof(void*)),
                              long,
                              mpl::if_c<(sizeof(void*) <= sizeof(boost::intmax_t)),
                                        boost::intmax_t,
                                        void>::type
                              >::type
                    >::type ptr_serialize_type;

  BOOST_STATIC_ASSERT ((!boost::is_void<ptr_serialize_type>::value));

  template<typename T> inline T& unsafe_serialize(T& x) { return x; }

  inline ptr_serialize_type& unsafe_serialize(void*& x)
  { return reinterpret_cast<ptr_serialize_type&>(x); }

  // Force Boost.MPI to serialize a void* like a ptr_serialize_type
  namespace mpi {
    template<> inline MPI_Datatype get_mpi_datatype<void*>(void* const& x)
    {
      return get_mpi_datatype<ptr_serialize_type>();
    }
  }

  template<typename T, typename U>
  struct unsafe_pair
  {
    unsafe_pair() { }
    unsafe_pair(const T& t, const U& u) : first(t), second(u) { }
    unsafe_pair(const std::pair<T, U>& p) : first(p.first), second(p.second) { }
    T first;
    U second;

    template<typename Archiver>
    void serialize(Archiver& ar, const unsigned /*version*/)
    {
      ar & unsafe_serialize(first) & unsafe_serialize(second);
    }
  };

  template<typename T, typename U>
  bool operator<(unsafe_pair<T,U> const& x, unsafe_pair<T,U> const& y)
  {
    return std::make_pair(x.first, x.second) <
      std::make_pair(y.first, y.second);
  }

} // end namespace boost

#endif // BOOST_PROPERTY_MAP_UNSAFE_SERIALIZE_HPP

ext: hpp


text:
#include <pxp-agent/results_storage.hpp>
#include <pxp-agent/action_response.hpp>
#include <pxp-agent/configuration.hpp>
#include <pxp-agent/time.hpp>

#include <leatherman/file_util/file.hpp>
#include <leatherman/file_util/directory.hpp>

#include <leatherman/locale/locale.hpp>

#define LEATHERMAN_LOGGING_NAMESPACE "puppetlabs.pxp_agent.results_storage"
#include <leatherman/logging/logging.hpp>

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

#include <algorithm>  // std::find

namespace PXPAgent {

namespace fs = boost::filesystem;
namespace lth_jc   = leatherman::json_container;
namespace lth_file = leatherman::file_util;
namespace lth_loc  = leatherman::locale;

static const std::string METADATA { "metadata" };
static const std::string STDOUT { "stdout" };
static const std::string STDERR { "stderr" };
static const std::string EXITCODE { "exitcode" };
static const std::string PID { "pid" };

ResultsStorage::ResultsStorage(std::string spool_dir, std::string spool_dir_ttl)
        : Purgeable { std::move(spool_dir_ttl) }, spool_dir_path_ { std::move(spool_dir) }
{
}

bool ResultsStorage::find(const std::string& transaction_id)
{
    auto p = spool_dir_path_ / transaction_id;
    return fs::exists(p) && fs::is_directory(p);
}

static void writeMetadata(const lth_jc::JsonContainer& metadata, const std::string& file_path) {
    // Redact "request_params" key in case parameters are sensitive.
    lth_jc::JsonContainer metadata_ { metadata };
    metadata_.set<std::string>("request_params", "{}");
    std::string txt = metadata_.toString() + "\n";
    try {
        lth_file::atomic_write_to_file(txt, file_path, NIX_FILE_PERMS, std::ios::binary);
    } catch (const std::exception& e) {
        throw ResultsStorage::Error {
            lth_loc::format("failed to write metadata: {1}", e.what()) };
    }
}

void ResultsStorage::initializeMetadataFile(const std::string& transaction_id,
                                            const lth_jc::JsonContainer& metadata)
{
    auto results_path = spool_dir_path_ / transaction_id;

    if (!fs::exists(results_path)) {
        LOG_DEBUG("Creating results directory for the  transaction {1} in '{2}'",
                  transaction_id, results_path.string());
        try {
            fs::create_directories(results_path);
            fs::permissions(results_path, NIX_DIR_PERMS);
        } catch (const fs::filesystem_error& e) {
            throw ResultsStorage::Error {
                lth_loc::format("failed to create results directory '{1}'",
                                e.what()) };
        }
    }

    auto metadata_file = (results_path / METADATA).string();
    writeMetadata(metadata, metadata_file);
}

void ResultsStorage::updateMetadataFile(const std::string& transaction_id,
                                        const lth_jc::JsonContainer& metadata)
{
    if (!find(transaction_id))
        throw Error {
            lth_loc::format("no results directory for the transaction {1}",
                            transaction_id) };

    auto metadata_file = (spool_dir_path_ / transaction_id / METADATA).string();
    writeMetadata(metadata, metadata_file);
}

lth_jc::JsonContainer
ResultsStorage::getActionMetadata(const std::string& transaction_id)
{
    auto metadata_file = (spool_dir_path_ / transaction_id / METADATA).string();
    std::string metadata_txt {};

    if (!fs::exists(metadata_file))
        throw Error {
            lth_loc::format("metadata file of the transaction {1} does not exist",
                            transaction_id) };

    if (!lth_file::read(metadata_file, metadata_txt))
        throw Error {
            lth_loc::format("failed to read metadata file of the transaction {1}",
                            transaction_id) };

    try {
        lth_jc::JsonContainer metadata { metadata_txt };

        if (!ActionResponse::isValidActionMetadata(metadata)) {
            LOG_DEBUG("The file '{1}' contains invalid action metadata:\n{2}",
                      metadata_file, metadata.toString());
            throw Error  {
                lth_loc::format("invalid action metadata of the transaction {1}",
                                transaction_id) };
        }

        return metadata;
    } catch (const lth_jc::data_parse_error& e) {
        LOG_DEBUG("The metadata file '{1}' is not valid JSON: {2}",
                  metadata_file, e.what());
        throw Error {
            lth_loc::format("invalid JSON in metadata file of the transaction {1}",
                            transaction_id) };
    }
}

bool ResultsStorage::pidFileExists(const std::string& transaction_id)
{
    return fs::exists(spool_dir_path_ / transaction_id / PID);
}

static int readIntegerFromFile(const std::string& file_path)
{
    std::string number_txt {};

    if (!fs::exists(file_path) || !lth_file::read(file_path, number_txt))
        throw ResultsStorage::Error {
            lth_loc::format("failed to read file '{1}'", file_path) };

    try {
        return std::stoi(number_txt);
    } catch (const std::invalid_argument& e) {
        throw ResultsStorage::Error {
            lth_loc::format("invalid value stored in file '{1}'{2}",
                            file_path,
                            (number_txt.empty() ? "" : ": " + number_txt)) };
    }
}

int ResultsStorage::getPID(const std::string& transaction_id)
{
    return readIntegerFromFile((spool_dir_path_ / transaction_id / PID).string());
}

bool ResultsStorage::outputIsReady(const std::string& transaction_id)
{
    return fs::exists(spool_dir_path_ / transaction_id / EXITCODE);
}

ActionOutput ResultsStorage::getOutput_(const std::string& transaction_id,
                                        bool get_exitcode)
{
    auto results_path = (spool_dir_path_ / transaction_id);

    ActionOutput output {};

    if (get_exitcode) {
        std::string exitcode_txt {};
        auto exitcode_file = (results_path / EXITCODE).string();
        output.exitcode = readIntegerFromFile(exitcode_file);
    }

    auto stderr_file = (results_path / STDERR).string();
    auto stdout_file = (results_path / STDOUT).string();

    if (fs::exists(stderr_file)) {
        if (!lth_file::read(stderr_file, output.std_err)) {
            LOG_ERROR("Failed to read error file '{1}'; this failure will be ignored",
                      stderr_file);
        } else {
            LOG_TRACE("Successfully read error file '{1}'", stderr_file);
        }
    }

    if (!fs::exists(stdout_file)) {
        LOG_DEBUG("Output file '{1}' does not exist", stdout_file);
    } else if (!lth_file::read(stdout_file, output.std_out)) {
        throw Error { lth_loc::format("failed to read '{1}'", stdout_file) };
    } else if (output.std_out.empty()) {
        LOG_TRACE("Output file '{1}' is empty", stdout_file);
    } else {
        LOG_TRACE("Successfully read output file '{1}'", stdout_file);
    }

    return output;
}

ActionOutput ResultsStorage::getOutput(const std::string& transaction_id)
{
    return getOutput_(transaction_id, true);
}

ActionOutput ResultsStorage::getOutput(const std::string& transaction_id,
                                       int exitcode)
{
    auto output = getOutput_(transaction_id, false);
    output.exitcode = exitcode;
    return output;
}

unsigned int ResultsStorage::purge(
                const std::string& ttl,
                std::vector<std::string> ongoing_transactions,
                std::function<void(const std::string& dir_path)> purge_callback)
{
    unsigned int num_purged_dirs { 0 };
    Timestamp ts { ttl };
    if (purge_callback == nullptr)
        purge_callback = &Purgeable::defaultDirPurgeCallback;

    LOG_INFO("About to purge the results directories from '{1}'; TTL = {2}",
             spool_dir_path_.string(), ttl);

    lth_file::each_subdirectory(
        spool_dir_path_.string(),
        [&](std::string const& s) -> bool {
            fs::path dir_path { s };
            auto transaction_id = dir_path.filename().string();
            LOG_TRACE("Inspecting '{1}' for purging", s);

            if (!ongoing_transactions.empty()
                    && std::find(ongoing_transactions.begin(),
                                 ongoing_transactions.end(),
                                 transaction_id) != ongoing_transactions.end())
                return true;

            try {
                auto md = getActionMetadata(transaction_id);

                if (md.get<std::string>("status") == "running") {
                    LOG_TRACE("Skipping '{1}' as the action status is 'running'", s);
                } else if (ts.isNewerThan(md.get<std::string>("start"))) {
                    LOG_TRACE("Removing '{1}'", s);

                    try {
                        purge_callback(dir_path.string());
                        num_purged_dirs++;
                    } catch (const std::exception& e) {
                        LOG_ERROR("Failed to remove '{1}': {2}", s, e.what());
                    }
                }
            } catch (const Error& e) {
                LOG_WARNING("Failed to retrieve the metadata for the transaction {1} "
                            "(the results directory will not be removed): {2}",
                            transaction_id, e.what());
            } catch (const Timestamp::Error& e) {
                LOG_WARNING("Failed to process the metadata for the transaction {1} "
                            "(the results directory will not be removed): {2}",
                            transaction_id, e.what());
            }

            return true;
        });

    LOG_INFO(lth_loc::format_n(
        // LOCALE: info
        "Removed {1} directory from '{2}'",
        "Removed {1} directories from '{2}'",
        num_purged_dirs, num_purged_dirs, spool_dir_path_.string()));
    return num_purged_dirs;
}

}  // namespace PXPAgent

ext: cc

max_stars_repo_name: Dorin-Pleava/pxp-agent

max_issues_repo_name: Dorin-Pleava/pxp-agent

max_forks_repo_name: Dorin-Pleava/pxp-agent


text:
#pragma once

#include "check_equal.hpp"

#include <boost/core/lightweight_test.hpp>
#include <coruja/support/type_traits.hpp>
#include <coruja/support/signal/any_connection.hpp>
#include <range/v3/range_traits.hpp>

#include <initializer_list>
#include <string>
#include <vector>

template<typename C>
struct F_ {
    C operator()(C& v)
    { return std::move(v); }
};

template<typename T, typename = void>
struct is_sequence_container : std::false_type {};

template<typename T>
struct is_sequence_container<T, coruja::void_t<
decltype(std::declval<T>().emplace_back(typename T::value_type{}))>>
    : std::true_type
{};

template<typename C>
inline
typename std::enable_if<is_sequence_container<C>::value>::type
emplace(C& c, typename C::value_type e)
{ c.emplace_back(std::move(e)); }

template<typename C>
inline
typename std::enable_if<!is_sequence_container<C>::value>::type
emplace(C& c, typename C::value_type e)
{ c.emplace(std::move(e)); }

template<typename V, typename F = F_<V>, typename E = typename V::observed_t>
void run(V pvec, E for_each_expected,
          F&& f = F_<V>{},
          E expected = E{},
          E erased_expected = E{})
{
    if(expected.size() == 0) expected = for_each_expected;
    bool before_erase_called{false};
    bool before_erase_by_ref_called{false};
    {
        auto vec = std::move(pvec);
        bool for_each_called{false};
        bool for_each_by_ref_called{false};
        E ires, rres, ieres, reres;
        vec.for_each
            ([&for_each_called, &ires](V& pvec, typename V::iterator it)
             {
                 emplace(ires, *it);
                 for_each_called = true;
             });
        vec.for_each
            ([&for_each_by_ref_called, &rres](const typename V::value_type& v)
             {
                 emplace(rres, v);
                 for_each_by_ref_called = true;
             });
        vec.before_erase
            ([&before_erase_called, &ieres](V& pvec, typename V::iterator it)
             {
                 emplace(ieres, *it);
                 before_erase_called = true;
             });
        vec.before_erase
            ([&before_erase_by_ref_called, &reres](const typename V::value_type& v)
             {
                 emplace(reres, v);
                 before_erase_by_ref_called = true;
             });
        auto rvec = f(vec);
        BOOST_TEST(for_each_called);
        BOOST_TEST(for_each_by_ref_called);
        BOOST_TEST(ires == for_each_expected);
        BOOST_TEST(rres == for_each_expected);
        if(!erased_expected.empty())
        {
            BOOST_TEST(ieres == erased_expected);
            BOOST_TEST(reres == erased_expected);
        }
        BOOST_TEST(rvec.observed() == expected);
    }
    BOOST_TEST(before_erase_called);
    BOOST_TEST(before_erase_by_ref_called);
}

template<typename E>
struct Fn_ref
{
    template<typename T>
    void operator()(const T& t)
    { emplace(res, t); }

    E& res;
};
template<typename E>
struct Fn_it
{
    template<typename V, typename It>
    void operator()(V&, It it)
    { emplace(res, *it); }

    E& res;
};

template<typename View, typename F, typename E >
void run_view(View pvec, E for_each_expected, F&& f, E erased_expected)
{
    using conn = coruja::scoped_any_connection;
    auto vec = std::move(pvec);
    using iterator = ranges::range_iterator_t<View>;
    E rres, reres;
    E rres_it, reres_it;
    E rres_t, reres_t;
    E rres_it_t, reres_it_t;
    //Lambda
    conn feconn = vec.for_each([&rres](const typename View::value_type& v)
    { emplace(rres, v); });
    conn feconn_it = vec.for_each([&rres_it](View& view, iterator it)
    { emplace(rres_it, *it); });
    conn beccon = vec.before_erase([&reres](const typename View::value_type& v)
    { emplace(reres, v); });
    conn beccon_it = vec.before_erase([&reres_it](View& view, iterator it)
    { emplace(reres_it, *it); });
    //Template
    conn feconn_t = vec.for_each(Fn_ref<E>{rres_t});
    conn feconn_it_t = vec.for_each(Fn_it<E>{rres_it_t});
    conn beccon_t = vec.before_erase(Fn_ref<E>{reres_t});
    conn beccon_it_t = vec.before_erase(Fn_it<E>{reres_it_t});

    auto rvec = f(vec);
    BOOST_TEST(rres == for_each_expected);
    BOOST_TEST(rres_it == for_each_expected);
    BOOST_TEST(rres_t == for_each_expected);
    BOOST_TEST(rres_it_t == for_each_expected);
    if(!erased_expected.empty())
    {
        BOOST_TEST(reres == erased_expected);
        BOOST_TEST(reres_it == erased_expected);
        BOOST_TEST(reres_t == erased_expected);
        BOOST_TEST(reres_it_t == erased_expected);
    }
}

template<typename View, typename F = F_<View>, typename E >
void run_view(View pvec, E for_each_expected, F&& f = F_<View>{})
{ run_view(std::move(pvec), std::move(for_each_expected), std::move(f), E{}, E{}); }

max_stars_repo_name: romariorios/coruja

max_issues_repo_name: romariorios/coruja

max_forks_repo_name: romariorios/coruja


text:
// Copyright Ricardo Calheiros de Miranda Cosme 2018.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE or copy at
//  http://www.boost.org/LICENSE_1_0.txt)

#include <coruja/object/object.hpp>
#include <coruja/object/for_each.hpp>
#include <coruja/object/view/transform.hpp>

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

using namespace coruja;

struct F {
    void operator()(bool o) const
    { b1 = true; }
    void operator()(std::string o) const
    { b2 = true; }
    bool &b1, &b2;
};

int main() {
    {
        object<bool> o1{false}, o2{true};
        object<std::string> o3{"abc"};
        bool b1{false}, b2{false};
        auto c = for_each(F{b1, b2}, o1, o2, o3);
        BOOST_TEST(b1);
        BOOST_TEST(b2);
        b1 = b2 = false;
        o1 = true;
        BOOST_TEST(b1);
        b1 = false;
        o2 = true;
        BOOST_TEST(b1);
        b1 = false;
        o3 = "def";
        BOOST_TEST(b2);
        b2 = false;

        c.disconnect();

        o1 = true;
        BOOST_TEST(!b1);
        o2 = true;
        BOOST_TEST(!b1);
        o3 = "ghi";
        BOOST_TEST(!b2);
    }

    //ObservableObject as an rvalue
    {
        object<bool> o1{false};
        bool b1{false}, b2{false};
        for_each(F{b1, b2},
                 o1,
                 view::transform(o1, [](bool v) -> std::string
                          {return v ? "true" : "false";}));
        BOOST_TEST(b1);
        BOOST_TEST(b2);
        b1 = b2 = false;
        o1 = true;
        BOOST_TEST(b2);
        b2 = false;
    }
}


text:
/*********************************************************************
* Software License Agreement (BSD License)
*
*  Copyright (c) 2012, Rice University
*  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 Rice University 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 OWNER 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.
*********************************************************************/

/* Author: Matt Maly, Keliang He */

#include "ompl/control/planners/ltl/Automaton.h"
#include "ompl/control/planners/ltl/World.h"
#if OMPL_HAVE_SPOT
#include <spot/tl/parse.hh>
#include <spot/tl/print.hh>
#include <spot/twaalgos/translate.hh>
#include <spot/twa/bddprint.hh>
#include <spot/misc/minato.hh>
#include <spot/twa/formula2bdd.hh>
#include <typeinfo>
#endif
#include <boost/range/irange.hpp>
#include <unordered_map>
#include <unordered_set>
#include <boost/dynamic_bitset.hpp>
#include <ostream>
#include <limits>
#include <queue>
#include <vector>

int ompl::control::Automaton::TransitionMap::eval(const World &w) const
{
    const auto d = entries.find(w);
    if (d != entries.end())
        return d->second;
    for (const auto &entry : entries)
    {
        if (w.satisfies(entry.first))
        {
            // Since w satisfies another world that leads to d->second,
            // we can add an edge directly from w to d->second.
            entries[w] = entry.second;
            return entry.second;
        }
    }
    return -1;
}

ompl::control::Automaton::Automaton(unsigned int numProps, unsigned int numStates)
  : numProps_(numProps)
  , numStates_(numStates)
  , accepting_(numStates_, false)
  , transitions_(numStates_)
  , distances_(numStates_, std::numeric_limits<unsigned int>::max())
{
}

#if OMPL_HAVE_SPOT
ompl::control::Automaton::Automaton(unsigned numProps, std::string formula, bool isCosafe)
  : Automaton::Automaton(numProps)
{
    if (!isCosafe)
        formula = "! (" + formula + ")";

    spot::formula f = spot::parse_formula(formula);
    spot::translator trans;

    // We want deterministic output (dfa)
    trans.set_pref(spot::postprocessor::Deterministic);
    // Apply all optimizations - will be slowest
    trans.set_level(spot::postprocessor::High);
    trans.set_type(spot::postprocessor::BA);
    spot::twa_graph_ptr au = trans.run(f);

    const auto &dict = au->get_dict();
    unsigned int n = au->num_states();
    for (unsigned int s = 0; s < n; ++s)
        addState(false);
    for (unsigned int s = 0; s < n; ++s)
    {
        // The out(s) method returns a fake container that can be
        // iterated over as if the contents was the edges going
        // out of s.  Each of these edge is a quadruplet
        // (src,dst,cond,acc).  Note that because this returns
        // a reference, the edge can also be modified.
        for (auto &t : au->out(s))
        {
            if (t.acc)
                setAccepting(s, true);

            // Parse the formula
            spot::minato_isop isop(t.cond);
            bdd clause = isop.next();
            if (clause == bddfalse)
                addTransition(s, numProps, t.dst);
            else
            {
                while (clause != bddfalse)
                {
                    ompl::control::World edgeLabel(numProps);
                    while (clause != bddtrue)
                    {
                        int var = bdd_var(clause);
                        const spot::bdd_dict::bdd_info &i = dict->bdd_map[var];
                        assert(i.type == spot::bdd_dict::var);
                        auto index = std::stoul(i.f.ap_name().substr(1));
                        bdd high = bdd_high(clause);
                        assert(index < numProps);
                        if (high == bddfalse)
                        {
                            edgeLabel[index] = false;
                            clause = bdd_low(clause);
                        }
                        else
                        {
                            assert(bdd_low(clause) == bddfalse);
                            edgeLabel[index] = true;
                            clause = high;
                        }
                    }
                    addTransition(s, edgeLabel, t.dst);

                    clause = isop.next();
                }
            }
        }
    }

    setStartState(au->get_init_state_number());

    if (!isCosafe)
        accepting_.flip();
}
#endif

unsigned int ompl::control::Automaton::addState(bool accepting)
{
    ++numStates_;
    accepting_.resize(numStates_);
    accepting_[numStates_ - 1] = accepting;
    transitions_.resize(numStates_);
    distances_.resize(numStates_, std::numeric_limits<unsigned int>::max());
    return numStates_ - 1;
}

void ompl::control::Automaton::setAccepting(unsigned int s, bool a)
{
    accepting_[s] = a;
}

bool ompl::control::Automaton::isAccepting(unsigned int s) const
{
    return accepting_[s];
}

void ompl::control::Automaton::setStartState(unsigned int s)
{
    startState_ = s;
}

int ompl::control::Automaton::getStartState() const
{
    return startState_;
}

void ompl::control::Automaton::addTransition(unsigned int src, const World &w, unsigned int dest)
{
    TransitionMap &map = transitions_[src];
    map.entries[w] = dest;
}

bool ompl::control::Automaton::run(const std::vector<World> &trace) const
{
    int current = startState_;
    for (const auto &w : trace)
    {
        current = step(current, w);
        if (current == -1)
            return false;
    }
    return true;
}

int ompl::control::Automaton::step(int state, const World &w) const
{
    if (state == -1)
        return -1;
    return transitions_[state].eval(w);
}

ompl::control::Automaton::TransitionMap &ompl::control::Automaton::getTransitions(unsigned int src)
{
    return transitions_[src];
}

unsigned int ompl::control::Automaton::numStates() const
{
    return numStates_;
}

unsigned int ompl::control::Automaton::numTransitions() const
{
    unsigned int ntrans = 0;
    for (const auto &transition : transitions_)
        ntrans += transition.entries.size();
    return ntrans;
}

unsigned int ompl::control::Automaton::numProps() const
{
    return numProps_;
}

void ompl::control::Automaton::print(std::ostream &out) const
{
    out << "digraph automaton {" << std::endl;
    out << "rankdir=LR" << std::endl;
    for (unsigned int i = 0; i < numStates_; ++i)
    {
        out << i << R"( [label=")" << i << R"(",shape=)";
        out << (accepting_[i] ? "doublecircle" : "circle") << "]" << std::endl;

        for (const auto &e : transitions_[i].entries)
        {
            const World &w = e.first;
            unsigned int dest = e.second;
            const std::string formula = w.formula();
            out << i << " -> " << dest << R"( [label=")" << formula << R"("])" << std::endl;
        }
    }
    out << "}" << std::endl;
}

unsigned int ompl::control::Automaton::distFromAccepting(unsigned int s, unsigned int maxDist) const
{
    if (distances_[s] < std::numeric_limits<unsigned int>::max())
        return distances_[s];
    if (accepting_[s])
        return 0;
    std::queue<unsigned int> q;
    std::unordered_set<unsigned int> processed;
    std::unordered_map<unsigned int, unsigned int> distance;

    q.push(s);
    distance[s] = 0;
    processed.insert(s);

    while (!q.empty())
    {
        unsigned int current = q.front();
        q.pop();
        if (accepting_[current])
        {
            distances_[s] = distance[current];
            return distance[current];
        }
        for (const auto &e : transitions_[current].entries)
        {
            unsigned int neighbor = e.second;
            if (processed.count(neighbor) > 0)
                continue;
            q.push(neighbor);
            processed.insert(neighbor);
            distance[neighbor] = distance[current] + 1;
        }
    }
    return std::numeric_limits<unsigned int>::max();
}

ompl::control::AutomatonPtr ompl::control::Automaton::AcceptingAutomaton(unsigned int numProps)
{
    auto phi(std::make_shared<Automaton>(numProps, 1));
    World trivial(numProps);
    phi->addTransition(0, trivial, 0);
    phi->setStartState(0);
    phi->setAccepting(0, true);
    return phi;
}

ompl::control::AutomatonPtr ompl::control::Automaton::CoverageAutomaton(unsigned int numProps,
                                                                        const std::vector<unsigned int> &covProps)
{
    auto phi(std::make_shared<Automaton>(numProps, 1 << covProps.size()));
    for (unsigned int src = 0; src < phi->numStates(); ++src)
    {
        const boost::dynamic_bitset<> state(covProps.size(), src);
        World loop(numProps);
        // each value of p is an index of a proposition in covProps
        for (unsigned int p = 0; p < covProps.size(); ++p)
        {
            // if proposition covProps[p] has already been covered at state src, skip it
            if (state[p])
                continue;
            // for each proposition covProps[p] that has not yet been
            // covered at state src, construct a transition from src to (src|p)
            // on formula (covProps[p]==true)
            boost::dynamic_bitset<> target(state);
            target[p] = true;
            World nextProp(numProps);
            nextProp[covProps[p]] = true;
            phi->addTransition(src, nextProp, target.to_ulong());
            // also build a loop from src to src on formula with conjunct (covProps[p]==false)
            loop[covProps[p]] = false;
        }
        // now we add a loop from src to src on conjunction of (covProps[p]==false)
        // for every p such that the pth bit of src is 1
        phi->addTransition(src, loop, src);
    }
    phi->setAccepting(phi->numStates() - 1, true);
    phi->setStartState(0);
    return phi;
}

ompl::control::AutomatonPtr ompl::control::Automaton::SequenceAutomaton(unsigned int numProps,
                                                                        const std::vector<unsigned int> &seqProps)
{
    auto seq(std::make_shared<Automaton>(numProps, seqProps.size() + 1));
    for (unsigned int state = 0; state < seqProps.size(); ++state)
    {
        // loop when next proposition in sequence is not satisfied
        World loop(numProps);
        loop[seqProps[state]] = false;
        seq->addTransition(state, loop, state);

        // progress forward when next proposition in sequence is satisfied
        World progress(numProps);
        progress[seqProps[state]] = true;
        seq->addTransition(state, progress, state + 1);
    }
    // loop on all input when in accepting state
    seq->addTransition(seqProps.size(), World(numProps), seqProps.size());
    seq->setAccepting(seqProps.size(), true);
    seq->setStartState(0);
    return seq;
}

ompl::control::AutomatonPtr ompl::control::Automaton::DisjunctionAutomaton(unsigned int numProps,
                                                                           const std::vector<unsigned int> &disjProps)
{
    auto disj(std::make_shared<Automaton>(numProps, 2));
    World loop(numProps);
    for (unsigned int disjProp : disjProps)
    {
        World satisfy(numProps);
        satisfy[disjProp] = true;
        loop[disjProp] = false;
        disj->addTransition(0, satisfy, 1);
    }
    disj->addTransition(0, loop, 0);
    disj->addTransition(1, World(numProps), 1);
    disj->setAccepting(1, true);
    disj->setStartState(0);
    return disj;
}

ompl::control::AutomatonPtr ompl::control::Automaton::AvoidanceAutomaton(unsigned int numProps,
                                                                         const std::vector<unsigned int> &avoidProps)
{
    /* An avoidance automaton is simply a disjunction automaton with its acceptance condition flipped. */
    AutomatonPtr avoid = DisjunctionAutomaton(numProps, avoidProps);
    avoid->setAccepting(0, true);
    avoid->setAccepting(1, false);
    return avoid;
}

ompl::control::AutomatonPtr ompl::control::Automaton::CoverageAutomaton(unsigned int numProps)
{
    const boost::integer_range<unsigned int> props = boost::irange(0u, numProps);
    return CoverageAutomaton(numProps, std::vector<unsigned int>(props.begin(), props.end()));
}

ompl::control::AutomatonPtr ompl::control::Automaton::SequenceAutomaton(unsigned int numProps)
{
    const boost::integer_range<unsigned int> props = boost::irange(0u, numProps);
    return SequenceAutomaton(numProps, std::vector<unsigned int>(props.begin(), props.end()));
}

ompl::control::AutomatonPtr ompl::control::Automaton::DisjunctionAutomaton(unsigned int numProps)
{
    const boost::integer_range<unsigned int> props = boost::irange(0u, numProps);
    return DisjunctionAutomaton(numProps, std::vector<unsigned int>(props.begin(), props.end()));
}

max_stars_repo_name: jingxixu/ompl

max_issues_repo_name: jingxixu/ompl

max_forks_repo_name: jingxixu/ompl


text:
/*! \file	sparse_flow.hpp
 *  \brief	Declarations for sparse optical flow.
*/

#ifndef _THERMALVIS_SPARSE_FLOW_H_
#define _THERMALVIS_SPARSE_FLOW_H_

#define MINIMUM_MATCHING_RANK	10
#define DEFAULT_DESCRIPTOR_AREA	3.0

#include "core/tools.hpp"
#include "core/program.hpp"

#include "core/general_resources.hpp"
#include "core/ros_resources.hpp"

#include "core/launch.hpp"
#include "core/improc.hpp"
#include "core/features.hpp"
#include "core/tracks.hpp"
#include "core/initialization.hpp"

#ifdef _AVLIBS_AVAILABLE_
#include "streamer/video.hpp"
#endif

#ifdef _USE_BOOST_
#include <boost/filesystem.hpp>
#include <boost/filesystem/path.hpp>
#endif

#ifdef _BUILD_FOR_ROS_
#include "feature_tracks.h"
#include "flowConfig.h"
#endif

#include "flow_config.hpp"

#include "opencv2/core.hpp"

#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <vector>
#include <iostream>

using namespace cv;
using namespace std;

/// \brief		Stores configuration information for the sparse optical flow routine
class trackerData : public flowSharedData, public commonData, public flowLaunchOnlyData {
	friend class xmlParameters;
	friend class featureTrackerNode;
#ifndef _BUILD_FOR_ROS_
	friend class flowConfig;
#endif

protected:

	string tracksOutputTopic;

	bool outputTrackCount, outputFeatureMotion, outputDebugImages;
	bool outputDetectedFeatures, outputTrackedFeatures;

	bool normalizeFeatureVelocities;

	int multiplier[MAX_HISTORY_FRAMES];
	int numDetectors;

	string detector[MAX_DETECTORS], descriptor[MAX_DETECTORS];
	double sensitivity[MAX_DETECTORS];
	string method[MAX_DETECTORS];
	bool method_match[MAX_DETECTORS];

public:
	trackerData();

	bool assignFromXml(xmlParameters& xP);

#ifdef _BUILD_FOR_ROS_
	bool obtainStartingData(ros::NodeHandle& nh);
#endif

    bool initializeDetectors(cv::Ptr<cv::FeatureDetector> *det, cv::Ptr<cv::FeatureDetector> *hom);
    void initializeDescriptors(cv::Ptr<cv::DescriptorExtractor> *desc, cv::Ptr<cv::DescriptorExtractor> *hom);

};

#ifndef _BUILD_FOR_ROS_
/// \brief		Substitute for ROS live configuration adjustments
class flowConfig : public flowRealtimeData {
public:
	flowConfig() { }
	bool assignStartingData(trackerData& startupData);

	void setDetector1(int detector_1 = DETECTOR_FAST) {};
	void setDetector2(int detector_2 = DETECTOR_FAST) {};
	void setDetector3(int detector_3 = DETECTOR_FAST) {};

	int getDetector1() { return detector_1; }
	int getDetector2() { return detector_2; }
	int getDetector3() { return detector_3; }

};
#endif

/// \brief		Manages the optical flow procedure
class featureTrackerNode : public GenericOptions {
private:

#ifdef _USE_QT_
        QImage myImage;
        QLabel myLabel;
#endif

#ifdef _BUILD_FOR_ROS_

	ros::NodeHandle private_node_handle;

	dynamic_reconfigure::Server<thermalvis::flowConfig> server;
	dynamic_reconfigure::Server<thermalvis::flowConfig>::CallbackType f;

	ros::Publisher tracks_pub;
	ros::Subscriber info_sub;

	image_transport::ImageTransport *it;
	image_transport::CameraPublisher debug_pub, pub_debug;
	image_transport::CameraSubscriber camera_sub;
	image_transport::Subscriber image_sub;

	sensor_msgs::CameraInfo debug_camera_info;
	sensor_msgs::Image msg_debug;

	cv_bridge::CvImagePtr cv_ptr;

	ros::Timer timer, features_timer;

#else
	const cv::Mat *bridgeReplacement;
#endif

	ros::Time lastCycleFrameTime;
	ros::Time olderTimes[MAX_HISTORY_FRAMES];
	ros::Time info_time, image_time, previous_time, original_time, dodgeTime;

	trackerData configData;

	bool previousTimeInitialized;

	ofstream trackCountStream, featureMotionStream, detectedFeaturesStream, trackedFeaturesStream;

	string optical_frame;

	bool cycleFlag, undergoingDelay, handleDelays;
	unsigned int cycleCount, previousTrackedPointsPeak, frameCount, readyFrame;

	std::vector<unsigned int> activeTrackIndices, lostTrackIndices;
	int previousIndex, currentIndex, skippedFrameCount, capturedFrameCount, referenceFrame;
	long int lastAllocatedTrackIndex, lastAllocatedDisplayTrackIndex;

	bool debugInitialized, lowPointsWarning, infoProcessed, infoSent, freezeNextOutput;

	int newlyDetectedFeatures, successfullyTrackedFeatures, discardedNewFeatures, newlyRecoveredFeatures, lostTrackCount;

	cv::Size ksize;
	double blurSigma, factor, minResponse, featuresVelocity, averageTrackLength;

	cv::Mat H12, dispMat;
	cv::Mat normalizedMat, blownImage, displayImage2, subscribedImage, drawImage2;
	cv::Mat blurredImage, newImage, grayImage, realImage, mappedImage, lastImage, olderImage;

	struct timeval cycle_timer, test_timer, skip_timer;
	double elapsedTime, testTime, skipTime;

	char *debug_pub_name, *tracks_pub_name, *nodeName;

	cv::Mat olderImages[MAX_HISTORY_FRAMES], grayImageBuffer[2], displayImage, drawImage, drawImage_resized;

	unsigned int numHistoryFrames, bufferIndices[2], olderIndices[MAX_HISTORY_FRAMES];

	std::vector<featureTrack> displayTracks;

	std::vector<std::string> predetectedFeatureFiles;

	cv::Ptr<cv::FeatureDetector> homographyDetector, keypointDetector[MAX_DETECTORS];
    vector<cv::KeyPoint> homogPoints[2], currPoints[MAX_DETECTORS];
	vector<cv::Point2f> candidates[MAX_DETECTORS];
    cv::Ptr<cv::DescriptorExtractor> homographyExtractor, descriptorExtractor;
    cv::Mat homogDescriptors[2];

    vector<cv::Point2f> globalStartingPoints, globalFinishingPoints, allRecoveredPoints, preservedRecoveredPoints, unmatchedPoints, matchedPoints, newlySensedFeatures, matchedFeatures;

    int distanceConstraint, peakTracks;

public:

	std::vector<featureTrack> featureTrackVector;

	///brief	Processes online changes in node configuration and applies them.
#ifdef _BUILD_FOR_ROS_
	void serverCallback(thermalvis::flowConfig &config, uint32_t level);
#else
	void serverCallback(flowConfig &config);
#endif

	///brief	Constructor. Must receive configuration data.
#ifdef _BUILD_FOR_ROS_
	featureTrackerNode(ros::NodeHandle& nh, trackerData startupData);
#else
	featureTrackerNode(trackerData startupData);
#endif

	///brief	Periodically checks for delays in data transmission, for detection of NUCs.
#ifdef _BUILD_FOR_ROS_
	void timed_loop(const ros::TimerEvent& event);
#else
	void timed_loop();
#endif

	///brief	Main function for attempting feature tracking.
#ifdef _BUILD_FOR_ROS_
	void features_loop(const ros::TimerEvent& event);
#else
	void features_loop();
#endif

	///brief	Processes provided configuration data to determine other calibration parameters etc.
#ifdef _BUILD_FOR_ROS_
	void process_info(const sensor_msgs::CameraInfoConstPtr& info_msg);
#else
	void process_info(sensor_msgs::CameraInfo *info_msg);
#endif

	///brief	Initial receipt of an image.
#ifdef _BUILD_FOR_ROS_
	void handle_camera(const sensor_msgs::ImageConstPtr& msg_ptr, const sensor_msgs::CameraInfoConstPtr& info_msg);
#else
	void handle_camera(cv::Mat& inputImage, sensor_msgs::CameraInfo *info_msg);
#endif

	///brief	Broadcasts tracking information.
#ifdef _BUILD_FOR_ROS_
	int publish_tracks(ros::Publisher *pub_message, unsigned int latestIndex);
#else
	int publish_tracks(unsigned int latestIndex);
#endif

	///brief	Calculates the average motion of all features successfully tracked from the previous frame.
	int calculateFeatureMotion(unsigned int idx, double& mx, double &my);

	///brief	Loads predetected keypoints from a file as an alternative to detecting them from the image
	void prepareKeypointFilelist();

	///brief	Loads predetected keypoints from a file as an alternative to detecting them from the image
	void loadKeypointsFromFile(vector<cv::KeyPoint>& pts_vec);

	///brief	Handles the situation where a duplicate frame is received (implying a NUC interruption).
	void handle_delay();

	///brief	Handles the situation where the first new frame after a NUC interruption arrives.
	void handle_very_new();

	///brief	Clear all values stored in the candidates arrays
	void clearCandidates();

	///brief	Reduces the size of the feature track vector by eliminating unlikely-to-be-recovered tracks.
	void trimFeatureTrackVector();

	///brief	Reduces the size of the debug/display feature track vector by eliminating unlikely-to-be-recovered tracks.
	void trimDisplayTrackVectors();

	///brief	Updates the search distance radius, possibly based on factors such as feature velocity as well as user settings.
	void updateDistanceConstraint();

	///brief	Attempts to track features from preceding frame.
	void attemptTracking();

	///brief	Attempts to track features from historically recorded frames.
	void attemptHistoricalTracking();

	///brief	Attempts to detect new features on current frame.
	void detectNewFeatures();

	///brief	Uses new detection and matching results to update the feature vectors.
	void updateTrackingVectors();

	///brief	Publishes all data relating to a completely processed frame, including for debugging.
	void publishRoutine();

	///brief	Attempts to match newly detected features with existing untracked features.
	void matchWithExistingTracks();

	///brief	Initial categorization (e.g. NUC/non-NUC) of an image.
	void act_on_image();

	///brief	Updates tracker configuration data, potentially based on first received image which contains image size etc.
	void updateTrackerData(trackerData newTrackerData) { configData = newTrackerData; }

	///brief	Updates the history/loop closure parameters, potentially based on user input.
	void updateHistoryParameters();

	///brief	Sets the debug mode - if true, images and data for visualization will be displayed to the user.
	void setDebugMode(bool val) {
		debugMode = val;
		configData.debugMode = val;
	}

	///brief	Prepares node for termination.
	void prepareForTermination();

	///brief	Displays latest debug frame
	void displayCurrentFrame();

};

#endif

max_stars_repo_name: ida-zrt/thermalvis

max_issues_repo_name: adaniy/thermalvis

max_forks_repo_name: adaniy/thermalvis


text:
// Copyright (c) 2020-2021 Franz Alt
// This code is licensed under MIT license (see LICENSE.txt for details).

#ifndef LIBCVPG_IMAGEPROC_SCRIPTING_IMAGE_PROCESSOR_HPP
#define LIBCVPG_IMAGEPROC_SCRIPTING_IMAGE_PROCESSOR_HPP

#include <any>
#include <chrono>
#include <cstdint>
#include <functional>
#include <map>
#include <memory>
#include <string>

#include <boost/asynchronous/servant_proxy.hpp>
#include <boost/asynchronous/trackable_servant.hpp>

#include <libcvpg/core/image.hpp>
#include <libcvpg/imageproc/scripting/algorithm_set.hpp>
#include <libcvpg/imageproc/scripting/item.hpp>
#include <libcvpg/imageproc/scripting/detail/compiler.hpp>
#include <libcvpg/imageproc/scripting/diagnostics/typedefs.hpp>

namespace cvpg::imageproc::scripting {

class processing_context;

class image_processor : public boost::asynchronous::trackable_servant<diagnostics::servant_job, diagnostics::servant_job>
                      , public std::enable_shared_from_this<image_processor>
{
public:
    using parameters_type = std::map<std::string, std::any>;

    image_processor(boost::asynchronous::any_weak_scheduler<diagnostics::servant_job> scheduler,
                    boost::asynchronous::any_shared_scheduler_proxy<diagnostics::servant_job> pool,
                    algorithm_set algorithms = algorithm_set());

    image_processor(image_processor const &) = delete;
    image_processor(image_processor &&) = default;

    image_processor & operator=(image_processor const &) = delete;
    image_processor & operator=(image_processor &&) = default;

    // compile an expression and return if compile was successfull and the compile ID
    void compile(std::string expression, std::function<void(std::size_t)> successful_callback, std::function<void(std::size_t, std::string)> failed_callback);

    // evaluate an input image
    void evaluate(std::size_t compile_id, cvpg::image_gray_8bit && image, std::function<void(item)> callback);
    void evaluate(std::size_t compile_id, cvpg::image_rgb_8bit && image, std::function<void(item)> callback);

    // evaluate an input image and convert the result if it is not the same as the input type
    void evaluate_convert_if(std::size_t compile_id, cvpg::image_gray_8bit && image, std::function<void(cvpg::image_gray_8bit)> callback, std::function<void(std::size_t, std::string)> failed_callback);
    void evaluate_convert_if(std::size_t compile_id, cvpg::image_rgb_8bit && image, std::function<void(cvpg::image_rgb_8bit)> callback, std::function<void(std::size_t, std::string)> failed_callback);

    // evaluate two input images
    void evaluate(std::size_t compile_id, cvpg::image_gray_8bit && image1, cvpg::image_gray_8bit && image2, std::function<void(item)> callback);
    void evaluate(std::size_t compile_id, cvpg::image_rgb_8bit && image1, cvpg::image_rgb_8bit && image2, std::function<void(item)> callback);

    // evaluate two input images and convert the result if it is not the same as the input type
    void evaluate_convert_if(std::size_t compile_id, cvpg::image_gray_8bit && image1, cvpg::image_gray_8bit &&image2, std::function<void(cvpg::image_gray_8bit)> callback, std::function<void(std::size_t, std::string)> failed_callback);
    void evaluate_convert_if(std::size_t compile_id, cvpg::image_rgb_8bit && image1, cvpg::image_rgb_8bit && image2, std::function<void(cvpg::image_rgb_8bit)> callback, std::function<void(std::size_t, std::string)> failed_callback);

    // add a parameter for filters
    void add_param(std::string key, std::any value);

    // get all parameters
    void parameters(std::function<void(parameters_type)> callback) const;

private:
    algorithm_set m_algorithms;

    std::map<std::size_t, detail::compiler::result> m_compiled;
    std::map<std::size_t, std::size_t> m_compiled_hashes;

    std::map<std::size_t, std::shared_ptr<processing_context> > m_context;

    std::size_t m_context_counter;

    parameters_type m_params;
};

struct image_processor_proxy : public boost::asynchronous::servant_proxy<image_processor_proxy, image_processor, diagnostics::servant_job>
{
    template<typename... Args>
    image_processor_proxy(Args... args)
        : boost::asynchronous::servant_proxy<image_processor_proxy, image_processor, diagnostics::servant_job>(std::forward<Args>(args)...)
    {}

    BOOST_ASYNC_POST_MEMBER_LOG(compile, "compile", 1)

    BOOST_ASYNC_POST_MEMBER_LOG(evaluate, "evaluate", 1)
    BOOST_ASYNC_POST_MEMBER_LOG(evaluate_convert_if, "evaluate_convert_if", 1)

    BOOST_ASYNC_POST_MEMBER_LOG(add_param, "add_param", 1)
    BOOST_ASYNC_POST_MEMBER_LOG(parameters, "parameters", 1)
};

} // namespace cvpg::imageproc::scripting

#endif // LIBCVPG_IMAGEPROC_SCRIPTING_IMAGE_PROCESSOR_HPP

max_stars_repo_name: franz-alt/cv-playground

max_issues_repo_name: franz-alt/cv-playground

max_forks_repo_name: franz-alt/cv-playground


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

// Verify that the headers can be included, even if their contents
// aren't neccessarily usable by broken compilers.

#ifdef TEST_STD_HEADERS

#include <memory>
#include <array>
#include <complex>
#include <functional>
#include <random>
#include <regex>
#include <tuple>
#include <utility>
#include <type_traits>

#else

#include <boost/tr1/array.hpp>
#include <boost/tr1/complex.hpp>
#include <boost/tr1/functional.hpp>
#include <boost/tr1/memory.hpp>
#include <boost/tr1/random.hpp>
#include <boost/tr1/regex.hpp>
#include <boost/tr1/tuple.hpp>
#include <boost/tr1/type_traits.hpp>
#include <boost/tr1/utility.hpp>

#endif

max_stars_repo_name: randolphwong/mcsema

max_issues_repo_name: randolphwong/mcsema

max_forks_repo_name: randolphwong/mcsema


text:
#pragma once

/*

File        : Server.hpp
Author      : Mustafa K. GILOR
Description :

| ASIO Server Wrapper |

A simple wrapper for Boost ASIO library to handle asynchronous connections.
Currently only supports TCP connections.

Usage :

boost::asio::io_service io;
Server server(io,3333);
io.run();

* Replace '3333' with desired listen port.

Author		: Mustafa K. GILOR
Last rev.	: 14/03/2015

*/

#pragma region Includes

#include "Session.hpp"
#include "IOServicePool.hpp"
#include "Server_Configuration.hpp"
#include "../ConcurrentStack.hpp"

#ifdef REPORT_MEMORY_USAGE
	#include "../Misc/MemUsage.h"
#endif

#if MAXIMUM_ALLOWED_CONNECTION > 0
	#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#endif

#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost\asio.hpp>
#include <boost/thread/recursive_mutex.hpp>
#include <boost/noncopyable.hpp>

#include <boost/range/algorithm/for_each.hpp>
#include <boost/range/adaptor/map.hpp>
#include <boost/mem_fn.hpp>
#include <boost/foreach.hpp>
#include <boost/atomic.hpp>

#include <map>
#include <set>

using boost::asio::ip::tcp;
using std::map;
using std::set;

typedef boost::atomic<unsigned long long> atomic_ulonglong;

#pragma endregion

#pragma region Declaration

template <class T>
class Server : boost::noncopyable
{
public:
	typedef std::map<unsigned long long, boost::shared_ptr<T>> SessionMap;
	typedef std::set<boost::shared_ptr<T>> SessionSet;

	T * operator[] (unsigned long long id)
	{

		boost::recursive_mutex::scoped_lock lock(sessionArrayLock);
		auto itr = arrSessions.find(id);
		if (itr != arrSessions.end())
			return itr->second;
		return nullptr;
	}

	/*
		Constructor
	*/
	Server(const unsigned short sPort);
	/*
		Generic destructor
	*/
	~Server();
	/*
		Start the server.
	*/
	void Run();
	/*
		Removes the given session from the session list.
		This is intended to be invoked by individual session.
	*/
	void OnSessionDisconnect(boost::shared_ptr<T> dConnection);
	/*
		Declared for keep alive service usage.
	*/
	void OnBrokenSession(boost::shared_ptr<T> dConnection);
	/*
		Updates the amount of total received bytes by given amount.
	*/
	void OnReceive(const size_t amount)
	{
		aulTotalRecv.store(aulTotalRecv.load() + amount);
		if (GetTickCount64() - ul_recvtime >= 1000)
		{
			ul_recvtime = GetTickCount64();
			fl_downrate.store((double)(aulTotalRecv - aulPrevTotalRecv));
			aulPrevTotalRecv.store(aulTotalRecv.load());
		}

	};
	/*
		Updates the amount of total sent bytes by given amount.
	*/
	void OnSend(const size_t amount)
	{
		aulTotalSend += amount;

		if (GetTickCount64() - ul_sendtime >= 1000)
		{
			ul_sendtime = GetTickCount64();
			fl_uprate.store((double)(aulTotalSend - aulPrevTotalSend));
			aulPrevTotalSend.store(aulTotalSend.load());
		}
	};

	/*
		The number of active session(s).
	*/
	size_t GetSessionCount() const { return arrSessions.size(); }

	/*
		Returns receive amount in bytes-per-second form.
	*/
	double GetCurrentReceiveAmount() const { return fl_downrate; }
	/*
		Returns send amount in bytes-per-second form.
	*/
	double GetCurrentSendAmount()    const{ return fl_uprate; }

	/*
	Acquires the session array lock.
	CAUTION : Every lock acquire operation should be followed by ReleaseSessionLock.
	Otherwise the program will be deadlocked!
	*/
	void AcquireSessionLock(){ sessionArrayLock.lock(); }
	void ReleaseSessionLock(){ sessionArrayLock.unlock(); }
	SessionMap & GetSessionMap(){ return arrSessions; }

private:

	/*
		The IO service pool for server.
		Default initialization value depends on CPU core amount.
	*/
	io_service_pool io_service_pool_;

	/*
		The listening port.
	*/
	unsigned __int16 m_sPort;

	#if MAXIMUM_ALLOWED_CONNECTION > 0
		boost::interprocess::interprocess_semaphore s_maxConnectionEnforcer;
	#endif

	ConcurrentStack<unsigned __int16> ClientID;

	/*
		The timer(s) used to check the socket keep alive and update internal server stats.
	*/
	boost::asio::deadline_timer t_keepalive, t_statupdate;

	/*
		The last receive / send update time. (for internal stats)
	*/
	unsigned long long ul_recvtime, ul_sendtime;

	/*
		Total received bytes since server started.
		(prev is for stat calculation)
	*/
	atomic_ulonglong aulTotalRecv, aulPrevTotalRecv;

	/*
		Total received sent since server started.
		(prev is for stat calculation)
	*/
	atomic_ulonglong aulTotalSend, aulPrevTotalSend;

	/*
		Amount of bytes being received & sent by server (per second)
	*/
	boost::atomic<double> fl_downrate, fl_uprate;

	/*
		The session map.
		Map key is session key (which is generated according to IP & Port
		Map value is session.
	*/
	SessionMap arrSessions;

	/*
		Session map mutex.

		This is here for preventing any race conditions
		between threads.

		NOTE : It must be acquired first before make any read / write operations
		on session map. Otherwise session map might be get corrupt.
	*/
	mutable boost::recursive_mutex sessionArrayLock;

	/*
		The temporary storage declaration for broken sessions that
		are detected by keep alive service.

		The session(s) that exist in this set will be automatically
		removed from the session map on next iteration.
	*/
	SessionSet arrSessionCleanup;

	/*
		Acceptor for incoming connections
	*/
	tcp::acceptor acceptor_;

	#ifdef REPORT_MEMORY_USAGE
		MemUsage mem_usage;
	#endif

	/*
		Server starts accepting connection(s) when called
	*/
	void StartAccepting();

	/*
		Invoked when an accept operation is completed
	*/
	void OnSessionConnect(boost::shared_ptr<T>, const boost::system::error_code&);

	/*
		Dispatches the keep alive service
	*/
	void DispatchKeepAlive();
	void OnKeepAlive();

	/*
		Dispatches the stat update service
	*/
	void DispatchStatUpdate();
	void OnStatUpdate();

};

#pragma endregion

#pragma region Implementation

/*
CTOR
*/
template <class T>
Server<T>::Server(unsigned short sPort)
#pragma region Initialize

try :
io_service_pool_(8),
acceptor_(io_service_pool_.get_io_service(), tcp::endpoint(tcp::v4(), sPort), false),
t_keepalive(io_service_pool_.get_io_service()),
t_statupdate(io_service_pool_.get_io_service()),
m_sPort(sPort),
ul_recvtime(0), ul_sendtime(0),
fl_downrate(0.0), fl_uprate(0.0), s_maxConnectionEnforcer(MAXIMUM_ALLOWED_CONNECTION)

#pragma endregion
{
	printf("libtcp_win ver 0.1\n");
	#pragma region Print debug information
	#ifdef _DEBUG
		printf("server_config_report (debug)\n");
		printf("	KEEP_ALIVE_INTERVAL		[%d]\n", KEEP_ALIVE_INTERVAL);
		printf("	STAT_UPDATE_INTERVAL		[%d]\n", STAT_UPDATE_INTERVAL);
		printf("	MAXIMUM_PENDING_CONNECTIONS	[%d]\n", MAXIMUM_PENDING_CONNECTIONS);
		printf("	MAXIMUM_ALLOWED_CONNECTION	[%d]\n", MAXIMUM_ALLOWED_CONNECTION);
	#endif
	#pragma endregion
	/* Initialize client id stack */
	for (int i = 65534; i > 0; i--){ ClientID.push(i); }

	StartAccepting();
	/*
		Dispatch the keep alive service in order to
		track disconnected or somehow broken session(s)
		*/
	DispatchKeepAlive();
	/*
		Stat update service, which keeps track of
		internal counters for send/recv operations.
		*/
	DispatchStatUpdate();
} catch (...) { throw; }

/*
DTOR
*/
template <class T>
Server<T>::~Server()
{
	/* Stop accepting new connections */
	acceptor_.close();

	{
		boost::recursive_mutex::scoped_lock lock(sessionArrayLock);
		// Disconnect all session(s)
		BOOST_FOREACH(SessionMap::value_type &ptr, arrSessions)
		{
			ptr.second->Disconnect();
		}

		arrSessions.clear();
	}
	/* Stop running io service(s) */
	io_service_pool_.stop();
}

template <class T>
void Server<T>::Run()
{
	io_service_pool_.run();
}

template <class T>
void Server<T>::StartAccepting()
{
	boost::shared_ptr<T> new_connection = T::create(io_service_pool_.get_io_service(), this);
	acceptor_.async_accept
		(
		/* Socket to copy connected client */
		new_connection->socket(),
		/* After-connection stuff */
		boost::bind
		(
		/* Function to call after connection */
		&Server::OnSessionConnect,
		/* Server */
		this,
		/* Client */
		new_connection,
		/* */
		boost::asio::placeholders::error
		)
		);
}

#pragma region Session connect / disconnect handlers

template <class T>
void Server<T>::OnSessionConnect(boost::shared_ptr<T> new_connection, const boost::system::error_code& error)
{
	/* As we're using shared pointers for Sessions,
	we don't need to worry about dropped connections
	or connection errors.

	(broken session will be automatically deleted
	when this function goes out of the scope
	as the last reference exist in this function).
	*/

	if (error)
		/* TODO : Determine the error and print a detailed output message to log file. */
		printf("> session_connect  \n	failed.\n<\n");
	else
	{
		/* Set session ID */
		((boost::shared_ptr<Session>)new_connection)->SetSessionID(ClientID.pop());
		/* Initialize the new connection */
		((boost::shared_ptr<Session>)new_connection)->Initialize();
		printf("> session_connect  \n	(S) ip : %s, port : %hu \n	(R) ip : %s, port : %hu, sid : %llu \n<\n", new_connection->GetLocalIPAddress().c_str(), new_connection->GetLocalPort(), new_connection->GetRemoteIPAddress().c_str(), new_connection->GetRemotePort(), new_connection->GetSessionID());
		/* Inform session that it's connected properly */
		((boost::shared_ptr<Session>)new_connection)->OnConnect();
		{
			/* Acquire the session map mutex */
			boost::recursive_mutex::scoped_lock lock(sessionArrayLock);
			if (new_connection->GetSessionID() == 0)
			{
				/* NOTE: This should never happen.
				If it happens, we need to investigate why.
				*/
				ASSERT(1);
				return;
			}
			/* Insert the session to the session map */
			arrSessions.insert(std::make_pair(new_connection->GetSessionID(), new_connection));
		}

	}

	#if MAXIMUM_ALLOWED_CONNECTION > 0
		s_maxConnectionEnforcer.wait();
	#endif
	/* Re-invoke accept operation */
	StartAccepting();
}

template <class T>
void Server<T>::OnSessionDisconnect(boost::shared_ptr<T> dConnection)
{
	printf("> session_disconnect \n	(R) ip : %s, port : %hu, sid %llu\n<\n", dConnection->GetRemoteIPAddress().c_str(), dConnection->GetRemotePort(), dConnection->GetSessionID());
	{
		/* Acquire the session map mutex (as it can occur concurrently) */
		boost::recursive_mutex::scoped_lock lock(sessionArrayLock);
		#if MAXIMUM_ALLOWED_CONNECTION > 0
			s_maxConnectionEnforcer.post();
		#endif
		/* Check if we have a session exist with that session key in the session map */
		if (arrSessions.find(dConnection->GetSessionID()) != arrSessions.end())
		{
			/*
			If it exist, we should erase it
			Otherwise, either it's deleted before or never existed.
			(both cases should not occur normally)
			*/

			arrSessions.erase(dConnection->GetSessionID());
			ClientID.push((unsigned short)dConnection->GetSessionID());
		}
		else
			printf("> session_disconnect \n	(R) ip : %s, port : %hu, sid %llu\n	failed,key does not exist.\n<\n", dConnection->GetRemoteIPAddress().c_str(), dConnection->GetRemotePort(), dConnection->GetSessionID());

	}
}

template <class T>
void Server<T>::OnBrokenSession(boost::shared_ptr<T> dConnection)
{
	arrSessionCleanup.insert(dConnection);
}

#pragma endregion

#pragma region Deadline timer dispatchers
template <class T>
void Server<T>::DispatchKeepAlive()
{
	/* Set timer expiration time */
	t_keepalive.expires_from_now(boost::posix_time::seconds(KEEP_ALIVE_INTERVAL));
	/* Wait until time expires (asynchronously), then call Server::OnKeepAlive */
	t_keepalive.async_wait(boost::bind(&Server::OnKeepAlive, this));
}

template <class T>
void Server<T>::DispatchStatUpdate()
{
	/* Set timer expiration time */
	t_statupdate.expires_from_now(boost::posix_time::seconds(STAT_UPDATE_INTERVAL));
	/* Wait until time expires (asynchronously), then call Server::OnStatUpdate */
	t_statupdate.async_wait(boost::bind(&Server::OnStatUpdate, this));
}

#pragma endregion

#pragma region Deadline timer callbacks

template <class T>
void Server<T>::OnKeepAlive()
{
	printf("< keep_alive\n	checking for dead connections..\n");
	/* Acquire the session map mutex (to avoid concurrent modifications) */
	{
		boost::recursive_mutex::scoped_lock lock(sessionArrayLock);
		boost::for_each(arrSessions | boost::adaptors::map_values, boost::mem_fn(&T::CheckAlive));
		{
			BOOST_FOREACH(boost::shared_ptr<T> ptr, arrSessionCleanup)
			{
				arrSessions.erase(ptr->GetSessionID());
			}
		}
	}
	printf("	removed [%d] idle session(s)\n<\n", arrSessionCleanup.size());
	arrSessionCleanup.clear();
	DispatchKeepAlive();
}
template <class T>
void Server<T>::OnStatUpdate()
{
	static const int gbyte_limit = 1074790400;
	static const int mbyte_limit = 1049600;
	static const int kbyte_limit = 1024;
	static const char * szFormat = "service::{%d}::sess(%d) <-> ul[%.2f kb/s] / dn[%.2f %s/s] / mem [%.2f MB]";
	char buf[1024];
	std::string ind = "bytes";
	double dr = fl_downrate.load();

	if (fl_downrate > gbyte_limit)
	{
		ind = "GB";
		dr /= gbyte_limit;
	}
	else if (fl_downrate > mbyte_limit)
	{
		ind = "MB";
		dr /= mbyte_limit;
	}
	else if (fl_downrate > kbyte_limit)
	{

		ind = "KB";
		dr /= kbyte_limit;
	}
#ifdef REPORT_MEMORY_USAGE
	sprintf_s(buf, szFormat, m_sPort, GetSessionCount(), fl_uprate.load(), dr, ind.c_str(), (double)(mem_usage.getCurrentRSS() / mbyte_limit));
#else
	sprintf_s(buf, szFormat, m_sPort, GetSessionCount(), fl_uprate.load(), dr, ind.c_str(), 0);
#endif
	SetConsoleTitleA(buf);
	DispatchStatUpdate();
}

#pragma endregion

#pragma endregion


text:
#pragma once

#include "request.hxx"
#include "response.hxx"

#include <boost/signals2/signal.hpp>

#include <string>
#include <functional>
#include <unordered_map>

namespace usagi::json::picojson::rpc::jsonrpc20
{
  class server_type
  {
    public:
      using method_signature_type = auto ( const value_type& params ) -> value_type;
      using method_functor_type = std::function< method_signature_type >;
      using method_signal_type = boost::signals2::signal< method_signature_type >;
      using method_slot_type = method_signal_type::slot_type;
    protected:
      std::unordered_map< std::string, method_signal_type > mapper;
    public:
      auto connect( const std::string& method, const method_functor_type& functor )
      { return mapper[ method ].connect( functor ); }

      template < typename T >
      auto connect( const std::string& method, const method_functor_type& functor, const T& track_target )
      { return mapper[ method ].connect( method_slot_type( functor ).track_foreign( track_target ) ); }

      /// @brief request を受けて処理し result または error の value_type を返す
      auto operator()( const value_type& request )
      {
        try
        {
          //const auto& [ method, params ] = extract_request( request );
          auto x = get_method( request );
          const auto& method_params = extract_request( request );
          const auto& method = std::get< 0 >( method_params );
          const auto& params = std::get< 1 >( method_params );
          auto optional_result = mapper.at( method )( params );
          if ( not optional_result )
            throw exception_type( error_code_type::method_not_found, "sigal is empty." );
          return make_result_from_request( request, *optional_result );
        }
        catch ( const exception_type& e )
        { return make_error_from_request( request, e ); }
        catch ( const std::out_of_range& e )
        { return make_error_from_request( request, exception_type( error_code_type::method_not_found ) ); }
        catch ( const std::exception& e )
        { return make_error_from_request( request, exception_type( error_code_type::internal_error, e.what() ) ); }
        catch ( ... )
        { return make_error_from_request( request, exception_type( error_code_type::internal_error ) ); }
      }

      auto operator()( const std::string& request_string )
      {
        value_type request;
        const auto& e = ::picojson::parse( request, request_string );
        if ( not e.empty() )
          return make_error_from_request( request, exception_type( error_code_type::parse_error ) );
        return operator()( request );
      }
  };
}

max_stars_repo_name: usagi/usagi

max_issues_repo_name: usagi/usagi

max_forks_repo_name: usagi/usagi


text:
#include <boost/lockfree/spsc_queue.hpp> // ring buffer

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>

namespace bip = boost::interprocess;
namespace shm
{
    typedef bip::allocator<char, bip::managed_shared_memory::segment_manager> char_alloc;
    typedef bip::basic_string<char, std::char_traits<char>, char_alloc >      shared_string;

    typedef boost::lockfree::spsc_queue<
        shared_string,
        boost::lockfree::capacity<200>
    > ring_buffer;
}

#include <iostream>

int main()
{
    // create segment and corresponding allocator
    bip::managed_shared_memory segment(bip::open_or_create, "new_momory", 65536);
    shm::char_alloc char_alloc(segment.get_segment_manager());

    shm::ring_buffer *queue = segment.find_or_construct<shm::ring_buffer>("queue")();

    while (true)
    {
        shm::shared_string v(char_alloc);
        if (queue->pop(v))
            std::cout << "Processed: '" << v << "'\n";
            // std::cout<<v.find(',')<<std::endl;

    }
}

max_stars_repo_name: xiaoqiyu/CTPTrader

max_issues_repo_name: xiaoqiyu/DERIQT

max_forks_repo_name: xiaoqiyu/DERIQT