text:
#pragma once
#include <memory>
#include <boost/asio/ip/udp.hpp>
#include "Const.h"
#include "UDPSessionListener.h"
#include "comm/Context.h"

namespace cys {
	namespace comm {
		namespace app {
			using udp = boost::asio::ip::udp;
			class UDPSession {
			public:
				UDPSession(cys::comm::Context* ctx, std::size_t channel)
					: m_context(ctx)
					, m_channel(0)
					, m_mutex()
					, m_listeners()
					, m_isCreated(false)
					, m_socket()
					, m_buffer()
				{}
				~UDPSession() = default;
				UDPSession(const UDPSession& session) = delete;
				UDPSession(UDPSession&& session) noexcept
					: m_context(std::move(session.m_context))
					, m_channel(std::move(session.m_channel))
					, m_mutex()
					, m_listeners(std::move(session.m_listeners))
					, m_isCreated(std::move(session.m_isCreated))
					, m_socket(std::move(session.m_socket))
					, m_buffer(std::move(session.m_buffer))
				{}
				UDPSession& operator=(const UDPSession& session) = delete;
				UDPSession& operator=(UDPSession&& session) noexcept
				{
					if (this != &session) {
						std::unique_lock<std::mutex> lhsLock(m_mutex, std::defer_lock);
						std::unique_lock<std::mutex> rhsLock(session.m_mutex, std::defer_lock);
						std::lock(lhsLock, rhsLock);
						m_context = std::move(session.m_context);
						m_channel = std::move(session.m_channel);
						m_listeners = std::move(session.m_listeners);
						m_isCreated = std::move(session.m_isCreated);
						m_socket = std::move(session.m_socket);
						m_buffer = std::move(session.m_buffer);
					}

					return *this;
				}

			public:
				inline bool addListener(UDPSessionListener* listener) {
					if (std::find(m_listeners.begin(), m_listeners.end(), listener) != m_listeners.end()) return false;
					m_listeners.emplace_back(listener);
					return true;
				}

				inline bool deleteListener(UDPSessionListener* listener) {
					if (std::find(m_listeners.begin(), m_listeners.end(), listener) == m_listeners.end()) return false;
					m_listeners.erase(std::remove(m_listeners.begin(), m_listeners.end(), listener), m_listeners.end());
					return true;
				}

			public:
				bool create() {
					std::unique_lock<std::mutex> m_mutex;
					if (m_isCreated) return false;
					m_socket = std::make_unique<udp::socket>(m_context->getContext());
					m_isCreated = true;
					return true;
				}

				bool start() {
					std::unique_lock<std::mutex> m_mutex;
					if (!m_isCreated) return false;
					if (!startReceiveAsync()) return false;
					return true;
				}

				bool stop() {
					std::unique_lock<std::mutex> m_mutex;
					return true;
				}

				bool destroy() {
					std::unique_lock<std::mutex> m_mutex;
					if (!m_isCreated) return true;
					closeSocket();

					for (auto& listener : m_listeners)
						if (listener != nullptr) listener->onUDPSessionUnBind(m_channel);

					m_isCreated = false;
					return true;
				}

			public:
				udp::socket& getSocket() { return *m_socket.get(); }

			private:
				bool startReceiveAsync() {
					if (m_socket.get() != nullptr) {
						m_socket->async_receive(boost::asio::buffer(m_buffer),
							[this](const boost::system::error_code& err, std::size_t transferred) {
								if (!err) {
									for (auto& listener : m_listeners) {
										if (listener != nullptr)
											listener->onUDPSessionReceived(m_channel, err, m_buffer);
									}
									startReceiveAsync();
								}
								else {
									// closeSocket();
								}
							});
					}

					return true;
				}

				bool closeSocket() {
					if (m_socket.get() == nullptr) return true;
					boost::system::error_code ec;
					m_socket->shutdown(udp::socket::shutdown_both, ec);
					m_socket->close();
					// m_socket.reset();

					return true;
				}

			private:
				cys::comm::Context* m_context;
				std::size_t m_channel;
				mutable std::mutex m_mutex;
				std::vector<UDPSessionListener*> m_listeners;
				bool m_isCreated;
				std::unique_ptr<udp::socket> m_socket;
				std::array<uint8_t, MAX_BUFFER_NUM> m_buffer;
			};
		}
	}
}

max_stars_repo_name: systembug/comm

max_issues_repo_name: systembug/comm

max_forks_repo_name: systembug/comm


text:
#include <am/matrix/matrix_db.h>

#include <boost/test/unit_test.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/random.hpp>

#include <string>
#include <time.h>
#include <math.h>
#include <fstream>
#include <iostream>

using namespace izenelib::am;
using namespace std;

namespace bfs = boost::filesystem;

const char* MATRIX_TEST_DIR_STR = "matrix";

BOOST_AUTO_TEST_SUITE(matrix_test)

BOOST_AUTO_TEST_CASE(update_elem)
{
    bfs::path matrix_path(MATRIX_TEST_DIR_STR);
    boost::filesystem::remove_all(matrix_path);
    bfs::create_directories(matrix_path);
    std::string matrix_path_str = matrix_path.string();

    MatrixDB<uint32_t, uint32_t> matrix(1024*2, matrix_path_str+"/test.db");
    size_t colNum = 100;
    size_t rowNum = 100;
    boost::mt19937 engine ;
    boost::uniform_int<> distribution(1, 10000) ;
    boost::variate_generator<boost::mt19937, boost::uniform_int<> > random(engine, distribution);

    uint32_t v = 0;
    for(size_t i = 0; i < rowNum; ++i)
        for(size_t j = 0; j < colNum; ++j)
            matrix.update_elem(i, j, v++);

    BOOST_TEST_MESSAGE(matrix);

    v = 0;
    for(size_t i = 0; i < rowNum; ++i)
        for(size_t j = 0; j < colNum; ++j)
        {
            uint32_t e = matrix.elem(i, j);
            BOOST_CHECK_EQUAL(e	,v++);
        }
}

BOOST_AUTO_TEST_CASE(dump)
{
    bfs::path matrix_path(MATRIX_TEST_DIR_STR);
    std::string matrix_path_str = matrix_path.string();

    MatrixDB<uint32_t, uint32_t> matrix(1024, matrix_path_str+"/test.db");
    size_t colNum = 100;
    size_t rowNum = 100;

    uint32_t v = 0;
    for(size_t i = 0; i < rowNum; ++i)
        for(size_t j = 0; j < colNum; ++j)
        {
            uint32_t e = matrix.elem(i, j);
            BOOST_CHECK_EQUAL(e	,v++);
        }

    BOOST_TEST_MESSAGE(matrix);
}

BOOST_AUTO_TEST_SUITE_END()

ext: cpp

max_stars_repo_name: izenecloud/izenelib

max_issues_repo_name: izenecloud/izenelib

max_forks_repo_name: izenecloud/izenelib


text:
/******************************************************************************
 * Copyright (C) 2013 by Jerome Maye                                          *
 * jerome.maye@gmail.com                                                      *
 *                                                                            *
 * This program is free software; you can redistribute it and/or modify       *
 * it under the terms of the Lesser GNU General Public License as published by*
 * the Free Software Foundation; either version 3 of the License, or          *
 * (at your option) any later version.                                        *
 *                                                                            *
 * This program is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * Lesser GNU General Public License for more details.                        *
 *                                                                            *
 * You should have received a copy of the Lesser GNU General Public License   *
 * along with this program. If not, see <http://www.gnu.org/licenses/>.       *
 ******************************************************************************/

#include "aslam/calibration/algorithms/marginalize.h"

#include <cmath>

#include <Eigen/Dense>

#include <aslam/backend/CompressedColumnMatrix.hpp>

#include "aslam/calibration/exceptions/InvalidOperationException.h"
#include "aslam/calibration/exceptions/OutOfBoundException.h"

namespace aslam {
namespace calibration {

/******************************************************************************/
/* Methods                                                                    */
/******************************************************************************/

double colNorm(cholmod_sparse* A, size_t j) {
    if (j >= A->ncol)
        throw OutOfBoundException<size_t>(j, "colNorm(): index must be lower than the number of columns", __FILE__,
                                          __LINE__);
    const std::ptrdiff_t* col_ptr = reinterpret_cast<const std::ptrdiff_t*>(A->p);
    const double* values = reinterpret_cast<const double*>(A->x);
    const std::ptrdiff_t p = col_ptr[j];
    const std::ptrdiff_t numElements = col_ptr[j + 1] - p;
    double norm = 0;
    for (std::ptrdiff_t i = 0; i < numElements; ++i) norm += values[p + i] * values[p + i];
    return std::sqrt(norm);
}

Eigen::MatrixXd marginalJacobian(cholmod_sparse* J_x, cholmod_sparse* J_thetat, cholmod_common* cholmod) {
    // compute the QR factorization of J_x
    SuiteSparseQR_factorization<double>* QR =
        SuiteSparseQR_factorize<double>(SPQR_ORDERING_BEST, SPQR_DEFAULT_TOL, J_x, cholmod);
    if (QR == NULL)
        throw InvalidOperationException(
            "marginalJacobian(): "
            "SuiteSparseQR_factorize failed");

    // compute the Jacobian of the reduced system
    cholmod_sparse* J_thetatQFull = SuiteSparseQR_qmult<double>(SPQR_XQ, QR, J_thetat, cholmod);
    if (J_thetatQFull == NULL) {
        SuiteSparseQR_free(&QR, cholmod);
        throw InvalidOperationException(
            "marginalJacobian(): "
            "SuiteSparseQR_qmult failed");
    }
    std::ptrdiff_t* colIndices = new std::ptrdiff_t[J_x->ncol];
    for (size_t i = 0; i < J_x->ncol; ++i) colIndices[i] = i;
    cholmod_sparse* J_thetatQ = cholmod_l_submatrix(J_thetatQFull, NULL, -1, colIndices, J_x->ncol, 1, 0, cholmod);
    delete[] colIndices;
    if (J_thetatQ == NULL) {
        SuiteSparseQR_free(&QR, cholmod);
        cholmod_l_free_sparse(&J_thetatQFull, cholmod);
        throw InvalidOperationException(
            "marginalJacobian(): "
            "cholmod_l_submatrix failed");
    }
    cholmod_sparse* J_thetat2 = cholmod_l_aat(J_thetat, NULL, 0, 1, cholmod);
    if (J_thetat2 == NULL) {
        SuiteSparseQR_free(&QR, cholmod);
        cholmod_l_free_sparse(&J_thetatQFull, cholmod);
        cholmod_l_free_sparse(&J_thetatQ, cholmod);
        throw InvalidOperationException(
            "marginalJacobian(): "
            "cholmod_l_aat failed");
    }
    cholmod_sparse* J_thetatQ2 = cholmod_l_aat(J_thetatQ, NULL, 0, 1, cholmod);
    if (J_thetatQ2 == NULL) {
        SuiteSparseQR_free(&QR, cholmod);
        cholmod_l_free_sparse(&J_thetatQFull, cholmod);
        cholmod_l_free_sparse(&J_thetatQ, cholmod);
        cholmod_l_free_sparse(&J_thetat2, cholmod);
        throw InvalidOperationException(
            "marginalJacobian(): "
            "cholmod_l_aat failed");
    }
    double alpha[2];
    alpha[0] = 1;
    double beta[2];
    beta[0] = -1;
    cholmod_sparse* Omega = cholmod_l_add(J_thetat2, J_thetatQ2, alpha, beta, 1, 0, cholmod);
    if (Omega == NULL) {
        SuiteSparseQR_free(&QR, cholmod);
        cholmod_l_free_sparse(&J_thetatQFull, cholmod);
        cholmod_l_free_sparse(&J_thetatQ, cholmod);
        cholmod_l_free_sparse(&J_thetat2, cholmod);
        cholmod_l_free_sparse(&J_thetatQ2, cholmod);
        throw InvalidOperationException(
            "marginalJacobian(): "
            "cholmod_l_add failed");
    }
    aslam::backend::CompressedColumnMatrix<std::ptrdiff_t> OmegaCCM;
    OmegaCCM.fromCholmodSparse(Omega);
    Eigen::MatrixXd OmegaDense(Omega->nrow, Omega->ncol);
    OmegaCCM.toDenseInto(OmegaDense);

    // clean allocated memory
    SuiteSparseQR_free(&QR, cholmod);
    cholmod_l_free_sparse(&J_thetatQFull, cholmod);
    cholmod_l_free_sparse(&J_thetatQ, cholmod);
    cholmod_l_free_sparse(&J_thetat2, cholmod);
    cholmod_l_free_sparse(&J_thetatQ2, cholmod);
    cholmod_l_free_sparse(&Omega, cholmod);

    return OmegaDense;
}

double marginalize(const aslam::backend::CompressedColumnMatrix<std::ptrdiff_t>& Jt, size_t j, Eigen::MatrixXd& NS,
                   Eigen::MatrixXd& CS, Eigen::MatrixXd& Sigma, Eigen::MatrixXd& SigmaP, Eigen::MatrixXd& Omega,
                   double normTol, double epsTol) {
    // init cholmod
    cholmod_common cholmod;
    cholmod_l_start(&cholmod);

    // convert to cholmod_sparse
    cholmod_sparse JtCs;
    const_cast<aslam::backend::CompressedColumnMatrix<std::ptrdiff_t>&>(Jt).getView(&JtCs);
    cholmod_sparse* J = cholmod_l_transpose(&JtCs, 1, &cholmod);
    if (J == NULL) {
        cholmod_l_finish(&cholmod);
        throw InvalidOperationException(
            "marginalize(): "
            "cholmod_l_transpose failed");
    }

    // extract the part corresponding to the state/landmarks/...
    std::ptrdiff_t* colIndices = new std::ptrdiff_t[j];
    for (size_t i = 0; i < j; ++i) colIndices[i] = i;
    cholmod_sparse* J_x = cholmod_l_submatrix(J, NULL, -1, colIndices, j, 1, 0, &cholmod);
    delete[] colIndices;
    if (J_x == NULL) {
        cholmod_l_free_sparse(&J, &cholmod);
        cholmod_l_finish(&cholmod);
        throw InvalidOperationException(
            "marginalize(): "
            "cholmod_l_submatrix failed");
    }

    // extract the part corresponding to the calibration parameters
    colIndices = new std::ptrdiff_t[J->ncol - j];
    for (size_t i = j; i < J->ncol; ++i) colIndices[i - j] = i;
    cholmod_sparse* J_theta = cholmod_l_submatrix(J, NULL, -1, colIndices, J->ncol - j, 1, 0, &cholmod);
    delete[] colIndices;
    if (J_theta == NULL) {
        cholmod_l_free_sparse(&J, &cholmod);
        cholmod_l_free_sparse(&J_x, &cholmod);
        cholmod_l_finish(&cholmod);
        throw InvalidOperationException(
            "marginalize(): "
            "cholmod_l_submatrix failed");
    }
    cholmod_sparse* J_thetat = cholmod_l_transpose(J_theta, 1, &cholmod);
    if (J_thetat == NULL) {
        cholmod_l_free_sparse(&J, &cholmod);
        cholmod_l_free_sparse(&J_x, &cholmod);
        cholmod_l_free_sparse(&J_theta, &cholmod);
        cholmod_l_finish(&cholmod);
        throw InvalidOperationException(
            "marginalize(): "
            "cholmod_l_transpose failed");
    }

    // compute the marginal Jacobian
    try {
        Omega = marginalJacobian(J_x, J_thetat, &cholmod);
    } catch (const InvalidOperationException& e) {
        cholmod_l_free_sparse(&J, &cholmod);
        cholmod_l_free_sparse(&J_x, &cholmod);
        cholmod_l_free_sparse(&J_theta, &cholmod);
        cholmod_l_free_sparse(&J_thetat, &cholmod);
        cholmod_l_finish(&cholmod);
        throw;
    }

    // scale J_x
    cholmod_dense* G_x = cholmod_l_allocate_dense(J_x->ncol, 1, J_x->ncol, CHOLMOD_REAL, &cholmod);
    if (G_x == NULL) {
        cholmod_l_free_sparse(&J, &cholmod);
        cholmod_l_free_sparse(&J_x, &cholmod);
        cholmod_l_free_sparse(&J_theta, &cholmod);
        cholmod_l_free_sparse(&J_thetat, &cholmod);
        cholmod_l_finish(&cholmod);
        throw InvalidOperationException(
            "marginalize(): "
            "cholmod_l_allocate_dense failed");
    }
    try {
        double* values = reinterpret_cast<double*>(G_x->x);
        for (size_t j = 0; j < J_x->ncol; ++j) {
            const double normCol = colNorm(J_x, j);
            if (normCol < normTol)
                values[j] = 0.0;
            else
                values[j] = 1.0 / normCol;
        }
    } catch (const OutOfBoundException<size_t>& e) {
        cholmod_l_free_sparse(&J, &cholmod);
        cholmod_l_free_sparse(&J_x, &cholmod);
        cholmod_l_free_sparse(&J_theta, &cholmod);
        cholmod_l_free_sparse(&J_thetat, &cholmod);
        cholmod_l_free_dense(&G_x, &cholmod);
        cholmod_l_finish(&cholmod);
        throw;
    }
    if (cholmod_l_scale(G_x, CHOLMOD_COL, J_x, &cholmod) == 0) {
        cholmod_l_free_sparse(&J, &cholmod);
        cholmod_l_free_sparse(&J_x, &cholmod);
        cholmod_l_free_sparse(&J_theta, &cholmod);
        cholmod_l_free_sparse(&J_thetat, &cholmod);
        cholmod_l_free_dense(&G_x, &cholmod);
        cholmod_l_finish(&cholmod);
        throw InvalidOperationException(
            "marginalize(): "
            "cholmod_l_scale failed");
    }
    cholmod_l_free_dense(&G_x, &cholmod);

    // scale J_thetat
    cholmod_dense* G_theta = cholmod_l_allocate_dense(J_theta->ncol, 1, J_theta->ncol, CHOLMOD_REAL, &cholmod);
    if (G_theta == NULL) {
        cholmod_l_free_sparse(&J, &cholmod);
        cholmod_l_free_sparse(&J_x, &cholmod);
        cholmod_l_free_sparse(&J_theta, &cholmod);
        cholmod_l_free_sparse(&J_thetat, &cholmod);
        cholmod_l_finish(&cholmod);
        throw InvalidOperationException(
            "marginalize(): "
            "cholmod_l_allocate_dense failed");
    }
    try {
        double* values = reinterpret_cast<double*>(G_theta->x);
        for (size_t j = 0; j < J_theta->ncol; ++j) {
            const double normCol = colNorm(J_theta, j);
            if (normCol < normTol)
                values[j] = 0.0;
            else
                values[j] = 1.0 / normCol;
        }
    } catch (const OutOfBoundException<size_t>& e) {
        cholmod_l_free_sparse(&J, &cholmod);
        cholmod_l_free_sparse(&J_x, &cholmod);
        cholmod_l_free_sparse(&J_theta, &cholmod);
        cholmod_l_free_sparse(&J_thetat, &cholmod);
        cholmod_l_free_dense(&G_theta, &cholmod);
        cholmod_l_finish(&cholmod);
        throw;
    }
    if (cholmod_l_scale(G_theta, CHOLMOD_ROW, J_thetat, &cholmod) == 0) {
        cholmod_l_free_sparse(&J, &cholmod);
        cholmod_l_free_sparse(&J_x, &cholmod);
        cholmod_l_free_sparse(&J_theta, &cholmod);
        cholmod_l_free_sparse(&J_thetat, &cholmod);
        cholmod_l_free_dense(&G_theta, &cholmod);
        cholmod_l_finish(&cholmod);
        throw InvalidOperationException(
            "marginalize(): "
            "cholmod_l_scale failed");
    }
    cholmod_l_free_dense(&G_theta, &cholmod);

    // compute the scaled marginal Jacobian
    Eigen::MatrixXd OmegaScaled;
    try {
        OmegaScaled = marginalJacobian(J_x, J_thetat, &cholmod);
    } catch (const InvalidOperationException& e) {
        cholmod_l_free_sparse(&J, &cholmod);
        cholmod_l_free_sparse(&J_x, &cholmod);
        cholmod_l_free_sparse(&J_theta, &cholmod);
        cholmod_l_free_sparse(&J_thetat, &cholmod);
        cholmod_l_finish(&cholmod);
        throw;
    }

    // clean cholmod
    cholmod_l_free_sparse(&J, &cholmod);
    cholmod_l_free_sparse(&J_x, &cholmod);
    cholmod_l_free_sparse(&J_theta, &cholmod);
    cholmod_l_free_sparse(&J_thetat, &cholmod);
    cholmod_l_finish(&cholmod);

    // compute the thin SVD of OmegaScaled
    const Eigen::JacobiSVD<Eigen::MatrixXd> svdScaled(OmegaScaled, Eigen::ComputeThinU | Eigen::ComputeThinV);

    // compute the numerical rank
    size_t nrank = OmegaScaled.cols();
    const Eigen::VectorXd& SScaled = svdScaled.singularValues();
    const double tol = OmegaScaled.rows() * SScaled(0) * epsTol;
    for (std::ptrdiff_t i = OmegaScaled.cols() - 1; i > 0; --i) {
        if (SScaled(i) > tol)
            break;
        else
            nrank--;
    }

    // compute the thin SVD of Omega
    const Eigen::JacobiSVD<Eigen::MatrixXd> svd(Omega, Eigen::ComputeThinU | Eigen::ComputeThinV);
    const Eigen::MatrixXd& V = svd.matrixV();

    // compute the numerical column space
    CS = V.block(0, 0, V.rows(), nrank);

    // compute the numerical null space
    NS = V.block(0, nrank, V.rows(), Omega.cols() - nrank);

    // compute the projected covariance matrix
    Eigen::MatrixXd invS(Eigen::MatrixXd::Zero(Omega.cols(), Omega.cols()));
    SigmaP = Eigen::MatrixXd::Zero(nrank, nrank);
    double svLogSum = 0;
    const Eigen::VectorXd& S = svd.singularValues();
    for (size_t i = 0; i < nrank; ++i) {
        SigmaP(i, i) = 1.0 / S(i);
        svLogSum = svLogSum + log2(S(i));
        invS(i, i) = SigmaP(i, i);
    }

    // compute the covariance matrix
    Sigma = V * invS * V.transpose();
    return svLogSum;
}

}  // namespace calibration
}  // namespace aslam

max_stars_repo_name: chengfzy/kalibr

max_issues_repo_name: chengfzy/kalibr

max_forks_repo_name: chengfzy/kalibr


text:
// Copyright Abel Sinkovics (abel@sinkovics.hu) 2010.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

#include <boost/metaparse/util/is_lcase_letter.hpp>

#include "common.hpp"

#include <boost/mpl/apply_wrap.hpp>
#include <boost/mpl/not.hpp>
#include <boost/mpl/assert.hpp>

#include "test_case.hpp"

BOOST_METAPARSE_TEST_CASE(util_is_lcase_letter)
{
  using boost::metaparse::util::is_lcase_letter;

  using boost::mpl::apply_wrap1;
  using boost::mpl::not_;

  // test_letter
  BOOST_MPL_ASSERT((apply_wrap1<is_lcase_letter<>, char_k>));

  // test_non_letter
  BOOST_MPL_ASSERT((not_<apply_wrap1<is_lcase_letter<>, char_K> >));
}


text:
#define BOOST_TEST_MODULE Test InfluxDB Factory
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

#include "../include/InfluxDBFactory.h"
#include "../src/InfluxDBException.h"

namespace influxdb {
namespace test {

//BOOST_AUTO_TEST_CASE(unrecognisedBackend)
//{
//  BOOST_CHECK_THROW(influxdb::InfluxDBFactory::Get("httpz://localhost:8086?db=test"), InfluxDBException);
//}
//
//BOOST_AUTO_TEST_CASE(missformatedUrl)
//{
//  BOOST_CHECK_THROW(influxdb::InfluxDBFactory::Get("localhost:8086?db=test"), InfluxDBException);
//}
//
//BOOST_AUTO_TEST_CASE(missingDb)
//{
//  BOOST_CHECK_THROW(influxdb::InfluxDBFactory::Get("http://localhost:8086"), InfluxDBException);
//}

} // namespace test
} // namespace influxdb

ext: cxx


text:
// Copyright (c) 2021-2022 The MetabaseNet developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "addressdb.h"

#include <boost/range/adaptor/reversed.hpp>

#include "leveldbeng.h"

using namespace std;
using namespace hcode;

namespace metabasenet
{
namespace storage
{

#define DB_ADDRESS_KEY_NAME_DEST string("dest")
#define DB_ADDRESS_KEY_NAME_TRIEROOT string("trieroot")
#define DB_ADDRESS_KEY_NAME_PREVROOT string("prevroot")

//////////////////////////////
// CListContractAddressTrieDBWalker

bool CListContractAddressTrieDBWalker::Walk(const bytes& btKey, const bytes& btValue, const uint32 nDepth, bool& fWalkOver)
{
    if (btKey.size() == 0 || btValue.size() == 0)
    {
        StdError("CListContractAddressTrieDBWalker", "btKey.size() = %ld, btValue.size() = %ld", btKey.size(), btValue.size());
        return false;
    }

    try
    {
        hcode::CBufStream ssKey(btKey);
        string strName;
        ssKey >> strName;
        if (strName == DB_ADDRESS_KEY_NAME_DEST)
        {
            uint256 dest;
            CAddressContext ctxtAddress;
            hcode::CBufStream ssValue(btValue);
            ssKey >> dest;
            ssValue >> ctxtAddress;
            if (ctxtAddress.IsContract())
            {
                CContractAddressContext ctxtContract;
                if (!ctxtAddress.GetContractAddressContext(ctxtContract))
                {
                    StdError("CListContractAddressTrieDBWalker", "GetContractAddressContext fail");
                    return false;
                }
                mapContractAddress.insert(std::make_pair(dest, ctxtContract));
            }
        }
    }
    catch (std::exception& e)
    {
        hcode::StdError(__PRETTY_FUNCTION__, e.what());
        return false;
    }
    return true;
}

//////////////////////////////
// CForkAddressDB

CForkAddressDB::CForkAddressDB(const bool fCacheIn)
{
    fCache = fCacheIn;
}

CForkAddressDB::~CForkAddressDB()
{
    dbTrie.Deinitialize();
}

bool CForkAddressDB::Initialize(const uint256& hashForkIn, const boost::filesystem::path& pathData)
{
    if (!dbTrie.Initialize(pathData))
    {
        StdLog("CForkAddressDB", "Initialize: Initialize fail, fork: %s", hashForkIn.GetHex().c_str());
        return false;
    }
    hashFork = hashForkIn;
    return true;
}

void CForkAddressDB::Deinitialize()
{
    dbTrie.Deinitialize();
}

bool CForkAddressDB::RemoveAll()
{
    dbTrie.RemoveAll();
    return true;
}

bool CForkAddressDB::AddAddressContext(const uint256& hashPrevBlock, const uint256& hashBlock, const std::map<uint256, CAddressContext>& mapAddress, uint256& hashNewRoot)
{
    uint256 hashPrevRoot;
    if (hashBlock != hashFork)
    {
        if (!ReadTrieRoot(hashPrevBlock, hashPrevRoot))
        {
            StdLog("CForkAddressDB", "Add Address Context: Read trie root fail, hashPrevBlock: %s, hashBlock: %s",
                   hashPrevBlock.GetHex().c_str(), hashBlock.GetHex().c_str());
            return false;
        }
    }

    bytesmap mapKv;
    for (const auto& kv : mapAddress)
    {
        hcode::CBufStream ssKey, ssValue;
        bytes btKey, btValue;

        ssKey << DB_ADDRESS_KEY_NAME_DEST << kv.first;
        ssKey.GetData(btKey);

        ssValue << kv.second;
        ssValue.GetData(btValue);

        mapKv.insert(make_pair(btKey, btValue));
    }
    AddPrevRoot(hashPrevRoot, hashBlock, mapKv);

    if (!dbTrie.AddNewTrie(hashPrevRoot, mapKv, hashNewRoot))
    {
        StdLog("CForkAddressDB", "Add Address Context: Add new trie fail, hashPrevBlock: %s, hashBlock: %s",
               hashPrevBlock.GetHex().c_str(), hashBlock.GetHex().c_str());
        return false;
    }

    if (!WriteTrieRoot(hashBlock, hashNewRoot))
    {
        StdLog("CForkAddressDB", "Add Address Context: Write trie root fail, hashPrevBlock: %s, hashBlock: %s",
               hashPrevBlock.GetHex().c_str(), hashBlock.GetHex().c_str());
        return false;
    }
    return true;
}

bool CForkAddressDB::RetrieveAddressContext(const uint256& hashBlock, const uint256& dest, CAddressContext& ctxtAddress)
{
    uint256 hashRoot;
    if (!ReadTrieRoot(hashBlock, hashRoot))
    {
        StdLog("CForkAddressDB", "Retrieve address context: Read trie root fail, block: %s", hashBlock.GetHex().c_str());
        return false;
    }
    hcode::CBufStream ssKey, ssValue;
    bytes btKey, btValue;
    ssKey << DB_ADDRESS_KEY_NAME_DEST << dest;
    ssKey.GetData(btKey);
    if (!dbTrie.Retrieve(hashRoot, btKey, btValue))
    {
        StdLog("CForkAddressDB", "Retrieve address context: Trie retrieve kv fail, root: %s, block: %s",
               hashRoot.GetHex().c_str(), hashBlock.GetHex().c_str());
        return false;
    }
    try
    {
        ssValue.Write((char*)(btValue.data()), btValue.size());
        ssValue >> ctxtAddress;
    }
    catch (std::exception& e)
    {
        hcode::StdError(__PRETTY_FUNCTION__, e.what());
        return false;
    }
    return true;
}

bool CForkAddressDB::ListContractAddress(const uint256& hashBlock, std::map<uint256, CContractAddressContext>& mapContractAddress)
{
    uint256 hashRoot;
    if (!ReadTrieRoot(hashBlock, hashRoot))
    {
        StdLog("CForkAddressDB", "List contract address: Read trie root fail, block: %s", hashBlock.GetHex().c_str());
        return false;
    }
    CListContractAddressTrieDBWalker walker(mapContractAddress);
    if (!dbTrie.WalkThroughTrie(hashRoot, walker))
    {
        StdLog("CForkAddressDB", "List contract address: Walk through trie fail, block: %s", hashBlock.GetHex().c_str());
        return false;
    }
    return true;
}

bool CForkAddressDB::CheckAddressContext(const std::vector<uint256>& vCheckBlock)
{
    if (vCheckBlock.size() > 0)
    {
        std::vector<uint256> vCheckRoot;
        vCheckRoot.reserve(vCheckBlock.size());
        for (size_t i = 0; i < vCheckBlock.size(); i++)
        {
            if (!ReadTrieRoot(vCheckBlock[i], vCheckRoot[i]))
            {
                StdLog("CForkAddressDB", "Check address context: Read trie root fail, block: %s", vCheckBlock[i].GetHex().c_str());
                return false;
            }
        }
        if (!dbTrie.CheckTrie(vCheckRoot))
        {
            StdLog("CForkAddressDB", "Check address context: Check trie fail");
            return false;
        }
    }
    return true;
}

bool CForkAddressDB::CheckAddressContext(const uint256& hashLastBlock, const uint256& hashLastRoot)
{
    uint256 hashLastRootRead;
    if (!ReadTrieRoot(hashLastBlock, hashLastRootRead))
    {
        return false;
    }
    if (hashLastRootRead != hashLastRoot)
    {
        return false;
    }

    uint256 hashBlockLocal;
    uint256 hashRoot = hashLastRoot;
    std::map<uint256, CTrieValue> mapCacheNode;
    do
    {
        if (!dbTrie.CheckTrieNode(hashRoot, mapCacheNode))
        {
            return false;
        }
        uint256 hashRootPrev;
        if (!GetPrevRoot(hashRoot, hashRootPrev, hashBlockLocal))
        {
            return false;
        }
        uint256 hashRootLocal;
        if (!ReadTrieRoot(hashBlockLocal, hashRootLocal))
        {
            return false;
        }
        if (hashRootLocal != hashRoot)
        {
            return false;
        }
        hashRoot = hashRootPrev;
    } while (hashBlockLocal != hashFork);

    return true;
}

bool CForkAddressDB::VerifyAddressContext(const uint256& hashPrevBlock, const uint256& hashBlock, uint256& hashRoot, const bool fVerifyAllNode)
{
    if (!ReadTrieRoot(hashBlock, hashRoot))
    {
        StdLog("CForkAddressDB", "Verify address context: Read trie root fail, block: %s", hashBlock.GetHex().c_str());
        return false;
    }

    if (fVerifyAllNode)
    {
        std::map<uint256, CTrieValue> mapCacheNode;
        if (!dbTrie.CheckTrieNode(hashRoot, mapCacheNode))
        {
            StdLog("CForkAddressDB", "Verify address context: Check trie node fail, root: %s, block: %s", hashRoot.GetHex().c_str(), hashBlock.GetHex().c_str());
            return false;
        }
    }

    uint256 hashPrevRoot;
    if (hashBlock != hashFork)
    {
        if (!ReadTrieRoot(hashPrevBlock, hashPrevRoot))
        {
            StdLog("CForkAddressDB", "Verify address context: Read prev trie root fail, prev block: %s", hashPrevBlock.GetHex().c_str());
            return false;
        }
    }

    uint256 hashRootPrevDb;
    uint256 hashBlockLocalDb;
    if (!GetPrevRoot(hashRoot, hashRootPrevDb, hashBlockLocalDb))
    {
        StdLog("CForkAddressDB", "Verify address context: Get prev root fail, hashRoot: %s, hashPrevRoot: %s, hashBlock: %s",
               hashRoot.GetHex().c_str(), hashPrevRoot.GetHex().c_str(), hashBlock.GetHex().c_str());
        return false;
    }
    if (hashRootPrevDb != hashPrevRoot || hashBlockLocalDb != hashBlock)
    {
        StdLog("CForkAddressDB", "Verify address context: Root error, hashRootPrevDb: %s, hashPrevRoot: %s, hashBlockLocalDb: %s, hashBlock: %s",
               hashRootPrevDb.GetHex().c_str(), hashPrevRoot.GetHex().c_str(),
               hashBlockLocalDb.GetHex().c_str(), hashBlock.GetHex().c_str());
        return false;
    }
    return true;
}

///////////////////////////////////
bool CForkAddressDB::WriteTrieRoot(const uint256& hashBlock, const uint256& hashTrieRoot)
{
    hcode::CBufStream ss;
    ss << DB_ADDRESS_KEY_NAME_TRIEROOT << hashBlock;
    uint256 hashKey = metabasenet::crypto::CryptoHash(ss.GetData(), ss.GetSize());

    bytes btValue(hashTrieRoot.begin(), hashTrieRoot.end());
    if (!dbTrie.SetValueNode(hashKey, btValue))
    {
        return false;
    }
    return true;
}

bool CForkAddressDB::ReadTrieRoot(const uint256& hashBlock, uint256& hashTrieRoot)
{
    if (hashBlock == 0)
    {
        hashTrieRoot = 0;
        return true;
    }

    hcode::CBufStream ss;
    ss << DB_ADDRESS_KEY_NAME_TRIEROOT << hashBlock;
    uint256 hashKey = metabasenet::crypto::CryptoHash(ss.GetData(), ss.GetSize());

    bytes btValue;
    if (!dbTrie.GetValueNode(hashKey, btValue))
    {
        return false;
    }
    hashTrieRoot = uint256(btValue);
    return true;
}

void CForkAddressDB::AddPrevRoot(const uint256& hashPrevRoot, const uint256& hashBlock, bytesmap& mapKv)
{
    hcode::CBufStream ssKey, ssValue;
    bytes btKey, btValue;

    ssKey << DB_ADDRESS_KEY_NAME_PREVROOT;
    ssKey.GetData(btKey);

    ssValue << hashPrevRoot << hashBlock;
    ssValue.GetData(btValue);

    mapKv.insert(make_pair(btKey, btValue));
}

bool CForkAddressDB::GetPrevRoot(const uint256& hashRoot, uint256& hashPrevRoot, uint256& hashBlock)
{
    hcode::CBufStream ssKey, ssValue;
    bytes btKey, btValue;
    ssKey << DB_ADDRESS_KEY_NAME_PREVROOT;
    ssKey.GetData(btKey);
    if (!dbTrie.Retrieve(hashRoot, btKey, btValue))
    {
        return false;
    }
    try
    {
        ssValue.Write((char*)(btValue.data()), btValue.size());
        ssValue >> hashPrevRoot >> hashBlock;
    }
    catch (std::exception& e)
    {
        hcode::StdError(__PRETTY_FUNCTION__, e.what());
        return false;
    }
    return true;
}

//////////////////////////////
// CAddressDB

bool CAddressDB::Initialize(const boost::filesystem::path& pathData)
{
    pathAddress = pathData / "address";

    if (!boost::filesystem::exists(pathAddress))
    {
        boost::filesystem::create_directories(pathAddress);
    }

    if (!boost::filesystem::is_directory(pathAddress))
    {
        return false;
    }
    return true;
}

void CAddressDB::Deinitialize()
{
    CWriteLock wlock(rwAccess);
    mapAddressDB.clear();
}

bool CAddressDB::ExistFork(const uint256& hashFork)
{
    CReadLock rlock(rwAccess);
    return (mapAddressDB.find(hashFork) != mapAddressDB.end());
}

bool CAddressDB::LoadFork(const uint256& hashFork)
{
    CWriteLock wlock(rwAccess);

    auto it = mapAddressDB.find(hashFork);
    if (it != mapAddressDB.end())
    {
        return true;
    }

    std::shared_ptr<CForkAddressDB> spAddress(new CForkAddressDB());
    if (spAddress == nullptr)
    {
        return false;
    }
    if (!spAddress->Initialize(hashFork, pathAddress / hashFork.GetHex()))
    {
        return false;
    }
    mapAddressDB.insert(make_pair(hashFork, spAddress));
    return true;
}

void CAddressDB::RemoveFork(const uint256& hashFork)
{
    CWriteLock wlock(rwAccess);

    auto it = mapAddressDB.find(hashFork);
    if (it != mapAddressDB.end())
    {
        it->second->RemoveAll();
        mapAddressDB.erase(it);
    }

    boost::filesystem::path forkPath = pathAddress / hashFork.GetHex();
    if (boost::filesystem::exists(forkPath))
    {
        boost::filesystem::remove_all(forkPath);
    }
}

bool CAddressDB::AddNewFork(const uint256& hashFork)
{
    RemoveFork(hashFork);
    return LoadFork(hashFork);
}

void CAddressDB::Clear()
{
    CWriteLock wlock(rwAccess);

    auto it = mapAddressDB.begin();
    while (it != mapAddressDB.end())
    {
        it->second->RemoveAll();
        mapAddressDB.erase(it++);
    }
}

bool CAddressDB::AddAddressContext(const uint256& hashFork, const uint256& hashPrevBlock, const uint256& hashBlock, const std::map<uint256, CAddressContext>& mapAddress, uint256& hashNewRoot)
{
    CReadLock rlock(rwAccess);

    auto it = mapAddressDB.find(hashFork);
    if (it != mapAddressDB.end())
    {
        return it->second->AddAddressContext(hashPrevBlock, hashBlock, mapAddress, hashNewRoot);
    }
    return false;
}

bool CAddressDB::RetrieveAddressContext(const uint256& hashFork, const uint256& hashBlock, const uint256& dest, CAddressContext& ctxtAddress)
{
    CReadLock rlock(rwAccess);

    auto it = mapAddressDB.find(hashFork);
    if (it != mapAddressDB.end())
    {
        return it->second->RetrieveAddressContext(hashBlock, dest, ctxtAddress);
    }
    return false;
}

bool CAddressDB::ListContractAddress(const uint256& hashFork, const uint256& hashBlock, std::map<uint256, CContractAddressContext>& mapContractAddress)
{
    CReadLock rlock(rwAccess);

    auto it = mapAddressDB.find(hashFork);
    if (it != mapAddressDB.end())
    {
        return it->second->ListContractAddress(hashBlock, mapContractAddress);
    }
    return false;
}

bool CAddressDB::CheckAddressContext(const uint256& hashFork, const std::vector<uint256>& vCheckBlock)
{
    CReadLock rlock(rwAccess);

    auto it = mapAddressDB.find(hashFork);
    if (it != mapAddressDB.end())
    {
        return it->second->CheckAddressContext(vCheckBlock);
    }
    return false;
}

bool CAddressDB::CheckAddressContext(const uint256& hashFork, const uint256& hashLastBlock, const uint256& hashLastRoot)
{
    CReadLock rlock(rwAccess);

    auto it = mapAddressDB.find(hashFork);
    if (it != mapAddressDB.end())
    {
        return it->second->CheckAddressContext(hashLastBlock, hashLastRoot);
    }
    return false;
}

bool CAddressDB::VerifyAddressContext(const uint256& hashFork, const uint256& hashPrevBlock, const uint256& hashBlock, uint256& hashRoot, const bool fVerifyAllNode)
{
    CReadLock rlock(rwAccess);

    auto it = mapAddressDB.find(hashFork);
    if (it != mapAddressDB.end())
    {
        return it->second->VerifyAddressContext(hashPrevBlock, hashBlock, hashRoot, fVerifyAllNode);
    }
    return false;
}

} // namespace storage
} // namespace metabasenet

max_stars_repo_name: metabasenet/metabasenet

max_issues_repo_name: metabasenet/metabasenet

max_forks_repo_name: metabasenet/metabasenet


text:
//
// Created by tudom on 2019-12-28.
//

#pragma once

#include <boost/preprocessor/stringize.hpp>

#define LIBLUCENT_INCLUDE_HEADER(header) \
    BOOST_PP_STRINGIZE(../include/liblucent/header)

#define LIBLUCENT_INCLUDE_HEADER_F(header) \
    BOOST_PP_STRINGIZE(../../include/liblucent/header)

ext: hpp

max_stars_repo_name: isbodand/InfoDynamite

max_issues_repo_name: isbodand/InfoDynamite

max_forks_repo_name: isbodand/InfoDynamite


text:
/// \file   environment.cpp
///
/// \brief
///
/// \authors    Maarten P. Scholl
/// \date       2018-11-24
/// \copyright  Copyright 2017-2019 The Institute for New Economic Thinking,
///             Oxford Martin School, University of Oxford
///
///             Licensed under the Apache License, Version 2.0 (the "License");
///             you may not use this file except in compliance with the License.
///             You may obtain a copy of the License at
///
///                 http://www.apache.org/licenses/LICENSE-2.0
///
///             Unless required by applicable law or agreed to in writing,
///             software distributed under the License is distributed on an "AS
///             IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
///             express or implied. See the License for the specific language
///             governing permissions and limitations under the License.
///
///             You may obtain instructions to fulfill the attribution
///             requirements in CITATION.cff
///
#include <sstream>
#include <fstream>
#include <chrono>
using std::chrono::high_resolution_clock;

#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/vector.hpp>

#if BOOST_VERSION >= 106500
#include <boost/serialization/unordered_map.hpp>
#endif

#include <boost/core/demangle.hpp>

#include <esl/agent.hpp>
#include <esl/computation/environment.hpp>
#include <esl/simulation/model.hpp>
using esl::simulation::time_point;
#include <esl/economics/price.hpp>
using esl::economics::price;
#include <esl/data/serialization.hpp>
#include <esl/data/log.hpp>

namespace esl::computation {

    environment::environment()
    {

    }

    ///
    /// agents are activated when they are newly created or when they migrate
    /// from one computational process to another
    ///
    size_t environment::activate()
    {
        size_t result_ = 0;
        for(const auto &a: activated_) {
            activate_agent(a);
            ++result_;
        }
        activated_.clear();
        return result_;
    }

    ///
    /// \brief  Agents are deactivated when they are deleted
    ///
    size_t environment::deactivate()
    {
        size_t result_ = 0;
        for(const auto &a: deactivated_) {
            deactivate_agent(a);
            ++result_;
        }
        return result_;
    }

    ///
    /// \brief
    ///
    void environment::before_step()
    {

    }

    void environment::after_step(simulation::model &simulation)
    {
        (void) simulation;
    }

    void environment::after_run(simulation::model &simulation)
    {
        return; /// TODO
        for(auto &[i, a]: simulation.agents.local_agents_) {
            for(const auto &o: a->outputs){
                std::stringstream filename_;
                filename_ << i << '_' << o.first << ".xml";

                try{
                    std::ofstream ofs(filename_.str());
                    LOG(trace) << "output " << o.second->name << " to file "
                               << filename_.str() << std::endl;
                    assert(ofs.good());
                    boost::archive::xml_oarchive oa(ofs);
                    oa.template register_type<price>();
                    oa.template register_type<std::vector<price>>();
                    typedef std::tuple<time_point, std::vector<price>>
                        tuple_time_point_price_vector;
                    oa.template register_type<tuple_time_point_price_vector>();
                    typedef std::vector<
                        std::tuple<time_point, std::vector<price>>>
                        time_series_price_vector;
                    oa.template register_type<time_series_price_vector>();
                    oa.template register_type<
                        data::output<std::vector<price>>>();
                    data::output_base *ptr = o.second.get();
                    oa << boost::serialization::make_nvp("output", ptr);
                }catch(std::exception &e){
                    (void)e;
                }
            }
        }
    }

    ///
    /// Send and receive messages, to all nodes all at the same time
    ///
    size_t environment::send_messages(simulation::model &simulation)
    {
        size_t messages_ = 0;

        // agent locality refers to memory locality. In the multi-threaded
        // setting, we are still able to observe all agents from one thread
        for(auto &[i, a] :simulation.agents.local_agents_){
            (void)i;
            for(const auto &m :a->outbox){
                auto iterator_ = simulation.agents.local_agents_.find(m->recipient);
                if(simulation.agents.local_agents_.end() == iterator_) {
                    // We are not in distributed mode, and no local agent matching recipient.
                    // The only possible explanation is that the recipient identity is incorrect
                    throw esl::exception("message recipient agent not found " + m->recipient.representation());
                }
                // insert into inbox
                iterator_->second->inbox.insert({m->received, m});

                // now that the recipient has a new message, make sure that they wake up on time to do something with it
                auto recipient_iterator_ = simulation.wake_up_times.find(m->recipient);
                if(simulation.wake_up_times.end() == recipient_iterator_){
                    simulation.wake_up_times[m->recipient] = m->received;
                }else{
                    (*recipient_iterator_).second = std::min((*recipient_iterator_).second, m->received);
                }

                ++messages_;
            }

            a->outbox.clear();          // clear the outbox, so that messages are only sent once
            a->outbox.shrink_to_fit();  // this is aggressively releasing the capacity, so that periods after intense
                                        // messaging do not use too much memory
        }
        return messages_;
    }

    ///
    /// \param a
    void environment::activate_agent(const identity<agent> &a)
    {
        activated_.push_back(a);
    }

    ///
    /// \param a
    void environment::deactivate_agent(const identity<agent> &a)
    {
        // agent_action_time_.erase(a);
        deactivated_.push_back(a);
    }

    ///
    /// \param simulation
    void environment::step(simulation::model &simulation)
    {
        simulation::time_interval step_ = {simulation.time, simulation.end};
        if(simulation.time < simulation.end) {
            simulation.step(step_);
        }

        size_t changes_ = 0;
        changes_ += activate();
        changes_ += deactivate();
    }

    ///
    /// \param simulation
    void environment::run(simulation::model &simulation)
    {
        auto timer_start_run_ = high_resolution_clock::now();
        simulation.initialize();
        auto timer_initialization_ = high_resolution_clock::now() - timer_start_run_;

        simulation::time_interval step_ = {simulation.start, simulation.end};
        do {
            size_t changes_ = 0;
            changes_ += activate();
            changes_ += deactivate();
            step_.lower = simulation.step(step_);
        } while(step_.lower < simulation.end);
        auto timer_simulation_ = high_resolution_clock::now() - timer_start_run_;

        LOG(notice) << "simulation took "
                    << (double(timer_simulation_.count()) / 1e+9)
                    <<  " seconds" << std::endl;

        simulation.terminate();
        auto timer_termination_ = high_resolution_clock::now() - timer_simulation_;

        after_run(simulation);
        auto timer_processing_after_ = high_resolution_clock::now() - timer_termination_;
        auto timer_total_ = high_resolution_clock::now() - timer_start_run_;

        LOG(notice) << "running simulation in " << boost::core::demangle(typeid(decltype(*this)).name())
                    << " took " << (double(timer_total_.count()) / 1e+9)
                    << " seconds" << std::endl;
    }
}// namespace esl::computation

max_stars_repo_name: vishalbelsare/ESL

max_issues_repo_name: vishalbelsare/ESL

max_forks_repo_name: vishalbelsare/ESL


text:
// Copyright 2020-2022 The Defold Foundation
// Copyright 2014-2020 King
// Copyright 2009-2014 Ragnar Svensson, Christian Murray
// Licensed under the Defold License version 1.0 (the "License"); you may not use
// this file except in compliance with the License.
//
// You may obtain a copy of the License, together with FAQs at
// https://www.defold.com/license
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

#include "profiler.h"

#include <dlib/dlib.h>
#include <dlib/profile.h>
#include <dlib/log.h>
#include <extension/extension.h>
#include <render/render.h>
#include <dmsdk/vectormath/cpp/vectormath_aos.h>

#include "profiler_private.h"
#include "profile_render.h"

namespace dmProfiler
{

/*# Profiler API documentation
 *
 * Functions for getting profiling data in runtime.
 * More detailed [profiling](https://www.defold.com/manuals/profiling/) and [debugging](http://www.defold.com/manuals/debugging/) information available in the manuals.
 *
 * @document
 * @name Profiler
 * @namespace profiler
 */

static bool g_TrackCpuUsage = false;
static dmProfileRender::HRenderProfile gRenderProfile = 0;
static uint32_t gUpdateFrequency = 60;

void SetUpdateFrequency(uint32_t update_frequency)
{
    gUpdateFrequency = update_frequency;
}

void ToggleProfiler()
{
    if (gRenderProfile)
    {
        dmProfileRender::DeleteRenderProfile(gRenderProfile);
        gRenderProfile = 0;
    }
    else
    {
        gRenderProfile = dmProfileRender::NewRenderProfile(gUpdateFrequency);
    }
}

void RenderProfiler(dmProfile::HProfile profile, dmGraphics::HContext graphics_context, dmRender::HRenderContext render_context, dmRender::HFontMap system_font_map)
{
    if(gRenderProfile)
    {
        DM_PROFILE(Profile, "Draw");
        dmProfile::Pause(true);

        dmProfileRender::UpdateRenderProfile(gRenderProfile, profile);
        dmRender::RenderListBegin(render_context);
        dmProfileRender::Draw(gRenderProfile, render_context, system_font_map);
        dmRender::RenderListEnd(render_context);
        dmRender::SetViewMatrix(render_context, dmVMath::Matrix4::identity());
        dmRender::SetProjectionMatrix(render_context, dmVMath::Matrix4::orthographic(0.0f, dmGraphics::GetWindowWidth(graphics_context), 0.0f, dmGraphics::GetWindowHeight(graphics_context), 1.0f, -1.0f));
        dmRender::DrawRenderList(render_context, 0, 0, 0);
        dmRender::ClearRenderObjects(render_context);

        dmProfile::Pause(false);
    }
}

/*# get current memory usage for app reported by OS
 * Get the amount of memory used (resident/working set) by the application in bytes, as reported by the OS.
 *
 * [icon:attention] This function is not available on [icon:html5] HTML5.
 *
 * The values are gathered from internal OS functions which correspond to the following;
 *
 * OS                                | Value
 * ----------------------------------|------------------
 * [icon:ios] iOS
[icon:macos] MacOS
[icon:android]
Androd
[icon:linux] Linux | [Resident memory](https://en.wikipedia.org/wiki/Resident_set_size)
 * [icon:windows] Windows            | [Working set](https://en.wikipedia.org/wiki/Working_set)
 * [icon:html5] HTML5                | [icon:attention] Not available
 *
 * @name profiler.get_memory_usage
 * @return bytes [type:number] used by the application
 * @examples
 *
 * Get memory usage before and after loading a collection:
 *
 * ```lua
 * print(profiler.get_memory_usage())
 * msg.post("#collectionproxy", "load")
 * ...
 * print(profiler.get_memory_usage()) -- will report a higher number than the initial call
 * ```
 */
static int MemoryUsage(lua_State* L)
{
    DM_LUA_STACK_CHECK(L, 1)
    lua_pushnumber(L, dmProfilerExt::GetMemoryUsage());
    return 1;
}

/*# get current CPU usage for app reported by OS
 * Get the percent of CPU usage by the application, as reported by the OS.
 *
 * [icon:attention] This function is not available on [icon:html5] HTML5.
 *
 * For some platforms ([icon:android] Android, [icon:linux] Linux and [icon:windows] Windows), this information is only available
 * by default in the debug version of the engine. It can be enabled in release version as well
 * by checking `track_cpu` under `profiler` in the `game.project` file.
 * (This means that the engine will sample the CPU usage in intervalls during execution even in release mode.)
 *
 * @name profiler.get_cpu_usage
 * @return percent [type:number] of CPU used by the application
 */
static int CPUUsage(lua_State* L)
{
    DM_LUA_STACK_CHECK(L, 1)
    lua_pushnumber(L, dmProfilerExt::GetCpuUsage());
    return 1;
}

/*# enables or disables the on-screen profiler ui
 * Creates and shows or hides and destroys the on-sceen profiler ui
 *
 * The profiler is a real-time tool that shows the numbers of milliseconds spent
 * in each scope per frame as well as counters. The profiler is very useful for
 * tracking down performance and resource problems.
 *
 * @name profiler.enable_ui
 * @param enabled [type:boolean] true to enable, false to disable
 *
 * @examples
 * ```lua
 * -- Show the profiler UI
 * profiler.enable_ui(true)
 * ```
 */
static int EnableProfilerUI(lua_State* L)
{
    DM_LUA_STACK_CHECK(L, 0)

    if (!lua_isboolean(L, 1))
    {
        return DM_LUA_ERROR("Invalid parameter, expected a boolean but got a %s", lua_typename(L, lua_type(L, 1)))
    }

    bool enabled = lua_toboolean(L, 1);

    if (enabled && !gRenderProfile)
    {
        gRenderProfile = dmProfileRender::NewRenderProfile(gUpdateFrequency);
    }
    else if (!enabled && gRenderProfile)
    {
        dmProfileRender::DeleteRenderProfile(gRenderProfile);
        gRenderProfile = 0;
    }

    return 0;
}

/*# sets the the on-screen profiler ui mode
 * Set the on-screen profile mode - run, pause, record or show peak frame
 *
 * @name profiler.set_ui_mode
 * @param mode [type:constant] the mode to set the ui profiler in
 *
 * - `profiler.MODE_RUN` This is default mode that continously shows the last frame
 * - `profiler.MODE_PAUSE` Pauses on the currently displayed frame
 * - `profiler.MODE_SHOW_PEAK_FRAME` Pauses on the currently displayed frame but shows a new frame if that frame is slower
 * - `profiler.MODE_RECORD` Records all incoming frames to the recording buffer
 *
 * To stop recording, switch to a different mode such as `MODE_PAUSE` or `MODE_RUN`.
 * You can also use the `view_recorded_frame` function to display a recorded frame. Doing so stops the recording as well.
 *
 * Every time you switch to recording mode the recording buffer is cleared.
 * The recording buffer is also cleared when setting the `MODE_SHOW_PEAK_FRAME` mode.
 *
 * @examples
 * ```lua
 * function start_recording()
 *      profiler.set_ui_mode(profiler.MODE_RECORD)
 * end
 *
 * function stop_recording()
 *      profiler.set_ui_mode(profiler.MODE_PAUSE)
 * end
 * ```
 */
static int SetProfileUIMode(lua_State* L)
{
    DM_LUA_STACK_CHECK(L, 0)

    if (!gRenderProfile)
    {
        return 0;
    }

    uint32_t mode = luaL_checknumber(L, 1);

    dmProfileRender::SetMode(gRenderProfile, (dmProfileRender::ProfilerMode)mode);

    return 0;
}

/*# sets the the on-screen profiler ui view mode
 * Set the on-screen profile view mode - minimized or expanded
 *
 * @name profiler.set_ui_view_mode
 * @param mode [type:constant] the view mode to set the ui profiler in
 *
 * - `profiler.VIEW_MODE_FULL` The default mode which displays all the ui profiler details
 * - `profiler.VIEW_MODE_MINIMIZED` Minimized mode which only shows the top header (fps counters and ui profiler mode)
 *
 *
 * @examples
 * ```lua
 * -- Minimize the profiler view
 * profiler.set_ui_view_mode(profiler.VIEW_MODE_MINIMIZED)
 * ```
 */
static int SetProfilerUIViewMode(lua_State* L)
{
    DM_LUA_STACK_CHECK(L, 0)

    if (!gRenderProfile)
    {
        return 0;
    }

    uint32_t mode = luaL_checknumber(L, 1);

    dmProfileRender::SetViewMode(gRenderProfile, (dmProfileRender::ProfilerViewMode)mode);

    return 0;
}

/*# Shows or hides the vsync wait time in the on-screen profiler ui
 * Shows or hides the time the engine waits for vsync in the on-screen profiler
 *
 * Each frame the engine waits for vsync and depending on your vsync settings and how much time
 * your game logic takes this time can dwarf the time in the game logic making it hard to
 * see details in the on-screen profiler graph and lists.
 *
 * Also, by hiding this the FPS times in the header show the time spent each time excuding the
 * time spent waiting for vsync. This shows you how long time your game is spending actively
 * working each frame.
 *
 * This setting also effects the display of recorded frames but does not affect the actual
 * recorded frames so it is possible to toggle this on and off when viewing recorded frames.
 *
 * By default the vsync wait times is displayed in the profiler.
 *
 * @name profiler.set_ui_vsync_wait_visible
 * @param visible [type:boolean] true to include it in the display, false to hide it.
 *
 * @examples
 * ```lua
 * -- Exclude frame wait time form the profiler ui
 * profiler.set_ui_vsync_wait_visible(false)
 * ```
 */
static int SetProfileUIVSyncWaitVisible(lua_State* L)
{
    DM_LUA_STACK_CHECK(L, 0)

    if (!gRenderProfile)
    {
        return 0;
    }

    if (!lua_isboolean(L, 1))
    {
        return DM_LUA_ERROR("Invalid parameter, expected a boolean but got a %s", lua_typename(L, lua_type(L, 1)))
    }

    bool visible = lua_toboolean(L, 1);

    dmProfileRender::SetWaitTime(gRenderProfile, visible);

    return 0;
}

/*# get the number of recorded frames in the on-screen profiler ui
 * Get the number of recorded frames in the on-screen profiler ui recording buffer
 *
 * @name profiler.recorded_frame_count
 * @return frame_count [type:number] the number of recorded frames, zero if on-screen profiler is disabled
 *
 * @examples
 * ```lua
 * -- Show the last recorded frame
 * local recorded_frame_count = profiler.recorded_frame_count()
 * profiler.view_recorded_frame(recorded_frame_count)
 * ```
 */
static int ProfilerUIRecordedFrameCount(lua_State* L)
{
    DM_LUA_STACK_CHECK(L, 1)

    if (!gRenderProfile)
    {
        lua_pushnumber(L, 0);
    }
    else
    {
        int frame_count = dmProfileRender::GetRecordedFrameCount(gRenderProfile);
        lua_pushnumber(L, frame_count);
    }

    return 1;
}

/*# displays a previously recorded frame in the on-screen profiler ui
 * Pauses and displays a frame from the recording buffer in the on-screen profiler ui
 *
 * The frame to show can either be an absolute frame or a relative frame to the current frame.
 *
 * @name profiler.view_recorded_frame
 * @param frame_index [type:table] a table where you specify one of the following parameters:
 *
 * - `distance` The offset from the currently displayed frame (this is truncated between zero and the number of recorded frames)
 * - `frame` The frame index in the recording buffer (1 is first recorded frame)
 *
 * @examples
 * ```lua
 * -- Go back one frame
 * profiler.view_recorded_frame({distance = -1})
 * ```
 */
static int ProfilerUIViewRecordedFrame(lua_State* L)
{
    DM_LUA_STACK_CHECK(L, 0)

    if (!gRenderProfile)
    {
        return 0;
    }

    luaL_checktype(L, 1, LUA_TTABLE);

    lua_getfield(L, -1, "distance");
    int distance = lua_isnil(L, -1) ? 0 : luaL_checkinteger(L, -1);
    lua_pop(L, 1);

    lua_getfield(L, -1, "frame");
    int frame = lua_isnil(L, -1) ? -1 : luaL_checkinteger(L, -1);
    lua_pop(L, 1);

    if (distance != 0)
    {
        dmProfileRender::AdjustShownFrame(gRenderProfile, distance);
    }
    else if (frame != -1)
    {
        int recorded_frame_count = dmProfileRender::GetRecordedFrameCount(gRenderProfile);
        if (recorded_frame_count == 0)
        {
            return DM_LUA_ERROR("The profiler recording buffer is empty");
        }
        if (frame < 1 || frame > recorded_frame_count)
        {
            return DM_LUA_ERROR("Frame index is out of range, valid range is %d to %d", 1, recorded_frame_count);
        }
        dmProfileRender::ShowRecordedFrame(gRenderProfile, frame - 1);
    }
    else
    {
        return DM_LUA_ERROR("'distance' or 'frame' must be given in properties table");
    }
    return 0;
}

/*# continously show latest frame
*
* @name profiler.MODE_RUN
* @variable
*/
/*# pause on current frame
*
* @name profiler.MODE_PAUSE
* @variable
*/
/*# pause at peak frame
*
* @name profiler.MODE_SHOW_PEAK_FRAME
* @variable
*/
/*# start recording
*
* @name profiler.MODE_RECORD
* @variable
*/
/*# show full profiler ui
*
* @name profiler.VIEW_MODE_FULL
* @variable
*/
/*# show mimimal profiler ui
*
* @name profiler.VIEW_MODE_MINIMIZED
* @variable
*/

} // dmProfiler

static dmExtension::Result InitializeProfiler(dmExtension::Params* params)
{
    // Should CPU sample tracking be run each step?
    // This is enabled by default in debug, but can be turned on in release via project config.
    dmProfiler::g_TrackCpuUsage = dLib::IsDebugMode();
    if (dmConfigFile::GetInt(params->m_ConfigFile, "profiler.track_cpu", 0) == 1)
    {
        dmProfiler::g_TrackCpuUsage = true;
    }

    static const luaL_reg Module_methods[] =
    {
        {"get_memory_usage", dmProfiler::MemoryUsage},
        {"get_cpu_usage", dmProfiler::CPUUsage},
        {"enable_ui", dmProfiler::EnableProfilerUI},
        {"set_ui_mode", dmProfiler::SetProfileUIMode},
        {"set_ui_view_mode", dmProfiler::SetProfilerUIViewMode},
        {"set_ui_vsync_wait_visible", dmProfiler::SetProfileUIVSyncWaitVisible},
        {"recorded_frame_count", dmProfiler::ProfilerUIRecordedFrameCount},
        {"view_recorded_frame", dmProfiler::ProfilerUIViewRecordedFrame},
        {0, 0}
    };

    luaL_register(params->m_L, "profiler", Module_methods);

    lua_pushnumber(params->m_L, (lua_Number) dmProfileRender::PROFILER_MODE_RUN);
    lua_setfield(params->m_L, -2, "MODE_RUN");
    lua_pushnumber(params->m_L, (lua_Number) dmProfileRender::PROFILER_MODE_PAUSE);
    lua_setfield(params->m_L, -2, "MODE_PAUSE");
    lua_pushnumber(params->m_L, (lua_Number) dmProfileRender::PROFILER_MODE_SHOW_PEAK_FRAME);
    lua_setfield(params->m_L, -2, "MODE_SHOW_PEAK_FRAME");
    lua_pushnumber(params->m_L, (lua_Number) dmProfileRender::PROFILER_MODE_RECORD);
    lua_setfield(params->m_L, -2, "MODE_RECORD");

    lua_pushnumber(params->m_L, (lua_Number) dmProfileRender::PROFILER_VIEW_MODE_FULL);
    lua_setfield(params->m_L, -2, "VIEW_MODE_FULL");
    lua_pushnumber(params->m_L, (lua_Number) dmProfileRender::PROFILER_VIEW_MODE_MINIMIZED);
    lua_setfield(params->m_L, -2, "VIEW_MODE_MINIMIZED");

    lua_pop(params->m_L, 1);

    return dmExtension::RESULT_OK;
}

static dmExtension::Result UpdateProfiler(dmExtension::Params* params)
{
    if (dmProfiler::g_TrackCpuUsage)
    {
        dmProfilerExt::SampleCpuUsage();
    }

    if (dLib::IsDebugMode()) {
        DM_COUNTER("CPU Usage", dmProfilerExt::GetCpuUsage()*100.0);
        DM_COUNTER("Mem Usage (Kb)", dmProfilerExt::GetMemoryUsage() / 1024u);
    }

    return dmExtension::RESULT_OK;
}

static dmExtension::Result FinalizeProfiler(dmExtension::Params* params)
{
    if (dmProfiler::gRenderProfile)
    {
        dmProfileRender::DeleteRenderProfile(dmProfiler::gRenderProfile);
        dmProfiler::gRenderProfile = 0;
    }
    return dmExtension::RESULT_OK;
}

static dmExtension::Result AppInitializeProfiler(dmExtension::AppParams* params)
{
    return dmExtension::RESULT_OK;
}

static dmExtension::Result AppFinalizeProfiler(dmExtension::AppParams* params)
{
    return dmExtension::RESULT_OK;
}

DM_DECLARE_EXTENSION(ProfilerExt, "Profiler", AppInitializeProfiler, AppFinalizeProfiler, InitializeProfiler, UpdateProfiler, 0, FinalizeProfiler)

max_stars_repo_name: cmarincia/defold

max_issues_repo_name: cmarincia/defold

max_forks_repo_name: cmarincia/defold


text:
#include "Reader.h"
#include <algorithm>
#include <boost/algorithm/string.hpp>
#include <fstream>
#include <iostream>
#include <unordered_map>
#include <sstream>

using namespace std;

void Reader::read_word_vector(string folder, unordered_map<string,vector<double>> &word_embeddings){
  ifstream glove_file;
  glove_file.open(folder);
  if(!glove_file){
    cout << "unable to word vector file" << '\n';
    exit(0);
  }
  cout << "Reading Word Embeddings" << endl;
  string word;
  string str;
  while(getline( glove_file,str)){
    istringstream ss(str);
    ss >> word;
    vector<double> embed;
    double elem;
    for(int s = 0 ; s < 300 ; s++) {
      ss >> elem;
      embed.push_back(elem);
    }
    word_embeddings[word] = embed;
  }
}

void Reader::calculate_word_vector(const unordered_map<string,vector<double>> &word_embeddings,const string& word,
                                   vector<double> &v_g_word_vector)
{

  vector<string> each_word;
  boost::split(each_word, word, boost::is_any_of(" "));
  unsigned int cnt = 0;
  for(const string& token : each_word){
    if(!token.empty()){
      if(word_embeddings.find(token) != word_embeddings.end()){
        cnt++;
        const vector<double>& each_word_vector = word_embeddings.at(token);
        if(!v_g_word_vector.empty())
          std::transform (v_g_word_vector.begin(), v_g_word_vector.end(),
                          each_word_vector.begin(), v_g_word_vector.begin(),
                          std::plus<double>());
        else
          v_g_word_vector = each_word_vector;
      }
    }
  }
  return;
  /*if(v_g_word_vector.empty()) return;
  cnt = each_word.size();
  transform(v_g_word_vector.begin(), v_g_word_vector.end(), v_g_word_vector.begin(), [cnt](double &c){ return c/cnt; });*/

  //if (v_g_word_vector.empty()) return;
  /*if (each_word.size() != cnt){
    vector<double> rand_vector (word_embeddings.begin()->second.size(),0);
    srand(time(0));
    generate(rand_vector.begin(), rand_vector.end(), rand);
    std::transform(rand_vector.begin(), rand_vector.end(), rand_vector.begin(),
                   [](double d) -> double { return d / RAND_MAX; });

    std::transform (v_g_word_vector.begin(), v_g_word_vector.end(),
                    rand_vector.begin(), v_g_word_vector.begin(),
                    std::plus<double>());

    int size = each_word.size();
    transform(v_g_word_vector.begin(), v_g_word_vector.end(), v_g_word_vector.begin(), [size](double &c){ return c/size; });
  }else{
    transform(v_g_word_vector.begin(), v_g_word_vector.end(), v_g_word_vector.begin(), [cnt](double &c){ return c/cnt; });
  }*/
}

max_stars_repo_name: rsltgy/libgrape-lite

max_issues_repo_name: rsltgy/libgrape-lite

max_forks_repo_name: rsltgy/libgrape-lite


text:
//  Copyright 2018 Stephan Menzel. Distributed under the Boost
//  Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include "AsioHelpers.hpp"

#include <boost/asio.hpp>

namespace moose {
namespace tools {

asio_streambuf_input_device::asio_streambuf_input_device(boost::asio::streambuf &n_streambuf, const std::streamsize n_limit)
		: m_streambuf(n_streambuf)
		, m_bytes_remaining(n_limit) {

}

std::streamsize asio_streambuf_input_device::read(char_type *const n_buffer, const std::streamsize n_buffer_size) {

	// Determine max amount of bytes to copy.
	const std::streamsize bytes_to_copy =
		std::min(m_bytes_remaining, std::min(
			static_cast<std::streamsize>(m_streambuf.size()), n_buffer_size));

	// If there is no more data to be read, indicate end-of-sequence per
	// Source concept.
	if (0 == bytes_to_copy) {
		return -1; // Indicate end-of-sequence, per Source concept.
	}

	// Copy from the streambuf into the provided buffer.
	std::copy_n(boost::asio::buffers_begin(m_streambuf.data()), bytes_to_copy, n_buffer);

	// Update bytes remaining.
	m_bytes_remaining -= bytes_to_copy;

	// Consume from the streambuf.
	m_streambuf.consume(bytes_to_copy);

	return bytes_to_copy;
}

}
}


text:
//  Copyright (c) 2001-2009 Hartmut Kaiser
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#if !defined(BOOST_SPIRIT_FORMAT_MANIP_MAY_05_2007_1203PM)
#define BOOST_SPIRIT_FORMAT_MANIP_MAY_05_2007_1203PM

#include <boost/spirit/home/qi/parse.hpp>
#include <boost/spirit/home/support/unused.hpp>

#include <iterator>
#include <string>

///////////////////////////////////////////////////////////////////////////////
namespace boost { namespace spirit { namespace qi { namespace detail
{
    ///////////////////////////////////////////////////////////////////////////
    template <
        typename Expr,
        typename Attribute = unused_type const,
        typename Skipper = unused_type
    >
    struct match_manip
    {
        match_manip(Expr const& xpr, Attribute& a, Skipper const& s)
          : expr(xpr), attr(a), skipper(s)
        {}

        Expr const& expr;
        Attribute& attr;
        Skipper const& skipper;
    };

    ///////////////////////////////////////////////////////////////////////////
    template<typename Char, typename Traits, typename Expr>
    inline std::basic_istream<Char, Traits> &
    operator>> (std::basic_istream<Char, Traits> &is,
        match_manip<Expr> const& fm)
    {
        typedef std::istream_iterator<Char, Char, Traits> input_iterator;
        input_iterator f(is);
        input_iterator l;
        if (!qi::parse (f, l, fm.expr))
        {
            is.setstate(std::ios_base::failbit);
        }
        return is;
    }

    ///////////////////////////////////////////////////////////////////////////
    template<typename Char, typename Traits, typename Expr, typename Attribute>
    inline std::basic_istream<Char, Traits> &
    operator>> (std::basic_istream<Char, Traits> &is,
        match_manip<Expr, Attribute> const& fm)
    {
        typedef std::istream_iterator<Char, Char, Traits> input_iterator;
        input_iterator f(is);
        input_iterator l;
        if (!qi::parse(f, l, fm.expr, fm.attr))
        {
            is.setstate(std::ios_base::failbit);
        }
        return is;
    }

    template<typename Char, typename Traits, typename Expr, typename Skipper>
    inline std::basic_istream<Char, Traits> &
    operator>> (std::basic_istream<Char, Traits> &is,
        match_manip<Expr, unused_type, Skipper> const& fm)
    {
        typedef std::istream_iterator<Char, Char, Traits> input_iterator;
        input_iterator f(is);
        input_iterator l;
        if (!qi::phrase_parse(f, l, fm.expr, fm.skipper))
        {
            is.setstate(std::ios_base::failbit);
        }
        return is;
    }

    ///////////////////////////////////////////////////////////////////////////
    template<
        typename Char, typename Traits,
        typename Expr, typename Attribute, typename Skipper
    >
    inline std::basic_istream<Char, Traits> &
    operator>> (
        std::basic_istream<Char, Traits> &is,
        match_manip<Expr, Attribute, Skipper> const& fm)
    {
        typedef std::istream_iterator<Char, Char, Traits> input_iterator;
        input_iterator f(is);
        input_iterator l;
        if (!qi::phrase_parse(f, l, fm.expr, fm.attr, fm.skipper))
        {
            is.setstate(std::ios_base::failbit);
        }
        return is;
    }

}}}}

#endif


text:
//
// Created by calebcintary on 3/17/22.
//

#include <boost/test/unit_test.hpp>

#include "pyplot_cpp/Bar.hpp"
#include "pyplot_cpp/plt/Properties.hpp"

BOOST_AUTO_TEST_SUITE(Bar_Test)

    BOOST_AUTO_TEST_CASE(Bar_SimpleSave_Test) {
        pyplot_cpp::Bar bar;
        bar.appendData("C", 20);
        bar.appendData("C++", 15);
        bar.appendData("Java", 30);
        bar.appendData("Python", 35);
        bar.save("../../examples/Bar_SimpleShow.png");
    }

    BOOST_AUTO_TEST_CASE(Bar_SimpleShow_Test_WithArguments) {
        pyplot_cpp::Bar bar;
        bar.appendData("C", 20);
        bar.appendData("C++", 15);
        bar.appendData("Java", 30);
        bar.appendData("Python", 35);
        bar.addProperty(pyplot_cpp::plt::Color("maroon"));
        bar.show();
    }

BOOST_AUTO_TEST_SUITE_END()


text:
// clang: MatousFormat

// Include the LKF header
#include <mrs_lib/geometry/cyclic.h>
#include <iostream>
#include <iomanip>
#include <chrono>
#include <complex>
#include <Eigen/Dense>

using namespace mrs_lib::geometry;
using cx = std::complex<double>;

double wrap(double r)
{
  return std::arg(std::polar<double>(1, r));
}

double diff(const double a, const double b)
{
  return std::arg(std::polar<double>(1, a) * std::conj(std::polar<double>(1, b)));
}

static double dist(const double a, const double b)
{
  return std::abs(diff(a, b));
}

/* interpolateAngles() //{ */

double interpolateAngles(const double& a1, const double& a2, const double& coeff)
{

  // interpolate the yaw
  Eigen::Vector3d axis = Eigen::Vector3d(0, 0, 1);

  Eigen::Quaterniond quat1 = Eigen::Quaterniond(Eigen::AngleAxis<double>(a1, axis));
  Eigen::Quaterniond quat2 = Eigen::Quaterniond(Eigen::AngleAxis<double>(a2, axis));

  Eigen::Quaterniond new_quat = quat1.slerp(coeff, quat2);

  Eigen::Vector3d vecx = new_quat * Eigen::Vector3d(1, 0, 0);

  return atan2(vecx[1], vecx[0]);
}

//}

int main()
{
  std::function correct(interpolateAngles);
  auto totest = std::bind<double(double, double,double)>(sradians::interpUnwrapped, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);

  const std::vector<double> tst {-0.0, 0.0, 1, -1, M_PI, -M_PI, 2*M_PI, -2*M_PI};
  std::cout <<
    "┌────────────┬────────────┬────────────┐\n"
    "│  correct   │  mrs_lib   │ difference │\n"
    "├────────────┼────────────┼────────────┤\n"
    ;
  for (const auto& a : tst)
  {
    for (const auto& b : tst)
    {
      for (const auto& c : tst)
      {
        const auto cor = correct(a, b, c);
        const auto res = totest(a, b, c);
        const auto dif = dist(cor, res);
        const bool ok = dif < 1e-9;
        if (!ok)
          std::cout << "\033[1;31m";
        std::cout << std::left << std::showpos << std::setprecision(4)
        << "│ " << std::setw(10) << cor << " │ " << std::setw(10) << res << " │ " << std::setw(10) << dif << " |";
        if (!ok)
          std::cout << "\tfor values: " << std::left << std::showpos << std::setprecision(4) << a << "\tand\t" << b << "\tand\t" << c << "\033[0m";
        std::cout << std::endl;
      }
    }
  }
  std::cout <<
  "└────────────┴────────────┴────────────┘" << std::endl;

  constexpr int N = 1e4;
  const auto start1 = std::chrono::high_resolution_clock::now();
  for (int it = 0; it < N; it++)
  {
    for (const auto& a : tst)
    {
      for (const auto& b : tst)
      {
        for (const auto& c : tst)
        {
          [[maybe_unused]] volatile auto cor = correct(a, b, c);
        }
      }
    }
  }
  const auto stop1 = std::chrono::high_resolution_clock::now();

  const auto start2 = std::chrono::high_resolution_clock::now();
  for (int it = 0; it < N; it++)
  {
    for (const auto& a : tst)
    {
      for (const auto& b : tst)
      {
        for (const auto& c : tst)
        {
          [[maybe_unused]] volatile auto cor = totest(a, b, c);
        }
      }
    }
  }
  const auto stop2 = std::chrono::high_resolution_clock::now();

  std::cout << "dur1: " << std::chrono::duration_cast<std::chrono::microseconds>(stop1-start1).count()/double(N) << "us" << std::endl;
  std::cout << "dur2: " << std::chrono::duration_cast<std::chrono::microseconds>(stop2-start2).count()/double(N) << "us" << std::endl;

  return 0;
}


text:
#define BOOST_TEST_MODULE "root_name"
// #define BOOST_TEST_MAIN

#include "CalibratorSample.h"
#include "CalibratorReset.h"

#include <boost/test/unit_test.hpp>
#include <boost/shared_ptr.hpp>

#include <random>
#include <string>
#include <iostream>
#include <chrono>

//* The test files for this come from Alessandro Marras at Desy 16/1/20. They are available at:
/*
/* DLS: see pathToTestFiles below
**/

static const float smallPercent = 1.0f;
static const int rows = 1484;
static const int cols = 1440;

BOOST_AUTO_TEST_CASE(LoadFromH5uint16Frame1)
{
    std::string pathToTestFiles = "/dls/detectors/Percival/test_data/LATcorrectionExample/";
    MemBlockI16 block;
    block.init(rows, cols);
    block.loadFromH5(pathToTestFiles + "dataElaboration_example2_step1_2Img_DLSraw.h5", "/reset", 0);

    BOOST_CHECK_EQUAL(block.at(0,36), 3602);

    block.loadFromH5(pathToTestFiles + "dataElaboration_example2_step1_2Img_DLSraw.h5", "/reset", 1);

    BOOST_CHECK_EQUAL(block.at(0,36), 3538);
}

BOOST_AUTO_TEST_CASE(LoadFromH5float)
{
    std::string pathToTestFiles("/dls/detectors/Percival/test_data/CalibRTests/");
    MemBlockF block;
    block.init(rows, cols);
    block.loadFromH5(pathToTestFiles + "BSI04_Tminus20_dmuxSELsw_2019.11.20_ADCcor.h5", "/sample/coarse/offset", 0);

    BOOST_CHECK_CLOSE(block.at(0,36), 51.089, smallPercent);
    BOOST_CHECK_CLOSE(block.at(1483,1435), 52.1033, smallPercent);

    block.loadFromH5(pathToTestFiles + "example_1_1Img_ADU.h5", "/sample", 0);

    BOOST_CHECK_CLOSE(block.at(0,34), -1535.9, smallPercent);
    BOOST_CHECK_CLOSE(block.at(1483,1432), -287.8, smallPercent);
}

BOOST_AUTO_TEST_CASE(LoadFromH5uint16)
{
    std::string pathToTestFiles("/dls/detectors/Percival/test_data/CalibRTests/");
    MemBlockI16 block;
    block.init(rows, cols);
    block.loadFromH5(pathToTestFiles + "example_1_1Img_DLSraw.h5", "/sample", 0);

    BOOST_CHECK_EQUAL(block.at(1313,1313), 3057);
    BOOST_CHECK_EQUAL(block.at(0,50), 12201);
    BOOST_CHECK_EQUAL(block.at(1313,1426), 4339);

}

BOOST_AUTO_TEST_CASE(calibrationADC)
{
    std::string pathToTestFiles("/dls/detectors/Percival/test_data/CalibRTests/");
    CalibratorSample calibratorS(rows, cols);

    calibratorS.loadADCGain(pathToTestFiles + "BSI04_Tminus20_dmuxSELsw_2019.11.20_ADCcor.h5");

    // we're not doing the cda stage at this point, so by setting 1.0f we make it nilpotent
    calibratorS.m_Gain3 = 1.0f;

    MemBlockI16 input;
    MemBlockF output, correct;
    input.init(rows,cols);
    correct.init(rows,cols);
    output.init(rows,cols);
    input.loadFromH5(pathToTestFiles + "example_1_1Img_DLSraw.h5", "/sample", 0);
    correct.loadFromH5(pathToTestFiles + "example_1_1Img_ADU.h5", "/sample", 0);

    calibratorS.processFrame(input, output);
    for(int r=0;r<rows;++r)
    {
       for(int c=0;c<cols;++c)
       {
           if(isnan(output.at(r,c)))
           {
              BOOST_CHECK(isnan(correct.at(r,c)));
           }
           else if( 0.01f < fabs( output.at(r,c)-correct.at(r,c) ) )
           {
                // I found that using boost_check_close failed for small values of output, so I did this instead.
                std::cout << "at r,c: " << r << " " << c;
                std::cout << " calc: " << output.at(r,c) << " correct: " << correct.at(r,c) << std::endl;
                BOOST_CHECK(false);
           }
       }
    }
}

BOOST_AUTO_TEST_CASE(sampleCMACDA)
{
    std::string pathToTestFiles("/dls/detectors/Percival/test_data/CalibRTests/");
    CalibratorReset calibratorR(rows, cols);
    CalibratorSample calibratorS(rows,cols);

    calibratorR.loadADCGain(pathToTestFiles + "BSI04_Tminus20_dmuxSELsw_2019.11.20_ADCcor.h5");
    calibratorS.loadADCGain(pathToTestFiles + "BSI04_Tminus20_dmuxSELsw_2019.11.20_ADCcor.h5");

    MemBlockI16 input;
    MemBlockF reset, output, correct;
    input.init(rows,cols);
    correct.init(rows,cols);
    reset.init(rows,cols);
    output.init(rows,cols);
    input.loadFromH5(pathToTestFiles + "example_1_1Img_DLSraw.h5", "/reset", 0);
    calibratorR.processFrame(input, reset);

    input.loadFromH5(pathToTestFiles + "example_1_1Img_DLSraw.h5", "/sample", 0);
    correct.loadFromH5(pathToTestFiles + "example_1_1Img_CMACDS.h5", "/sample", 0);
    calibratorS.setCMA(true, 1024);
    calibratorS.m_Gain3 = 1.0f;
    calibratorS.setResetFrame(reset);
    calibratorS.processFrame(input, output);

    for(int r=0;r<rows;++r)
    {
       for(int c=0;c<cols;++c)
       {
           if(isnan(output.at(r,c)))
           {
              if(!isnan(correct.at(r,c)))
              {
                 std::cout << "nan mismatch at " << r << "," << c << " output nan, correct is " << correct.at(r,c) << std::endl;
                 BOOST_CHECK(false);
              }
           }
           else if( 0.01f < fabs( output.at(r,c)-correct.at(r,c) ) )
           {
                // I found that using boost_check_close failed for small values of output, so I did this instead.
                std::cout << "at r,c: " << r << " " << c;
                std::cout << " calc: " << output.at(r,c) << " correct: " << correct.at(r,c) << std::endl;
                BOOST_CHECK(false);
           }
       }
   }
}

BOOST_AUTO_TEST_CASE(sampleCDA_LAT)
{
    std::string pathToTestFiles = "/dls/detectors/Percival/test_data/LATcorrectionExample/";
    CalibratorReset calibratorR(rows, cols);
    CalibratorSample calibratorS(rows,cols);

    calibratorR.loadADCGain(pathToTestFiles + "BSI04_Tminus20_dmuxSELsw_2019.11.20_ADCcor.h5");
    calibratorS.loadADCGain(pathToTestFiles + "BSI04_Tminus20_dmuxSELsw_2019.11.20_ADCcor.h5");
    calibratorS.loadLatGain(pathToTestFiles + "latgainfile.h5");

    MemBlockI16 input;
    MemBlockF reset, output, correct;
    input.init(rows,cols);
    correct.init(rows,cols);
    reset.init(rows,cols);
    output.init(rows,cols);
    input.loadFromH5(pathToTestFiles + "dataElaboration_example2_step1_2Img_DLSraw.h5", "/reset", 0);
    calibratorR.processFrame(input, reset);

    input.loadFromH5(pathToTestFiles + "dataElaboration_example2_step1_2Img_DLSraw.h5", "/data", 1);
    correct.loadFromH5(pathToTestFiles + "dataElaboration_example2_step4_1Img_e.h5", "/data/data", 0);

    calibratorS.setResetFrame(reset);
    calibratorS.processFrame(input, output);

    bool oneRowHasAllG0 = false;
    for(int r=0;r<rows;++r)
    {
       int countNumPixelsG0 = 0;
       for(int c=0;c<cols;++c)
       {
           if(isnan(output.at(r,c)))
           {
              if(!isnan(correct.at(r,c)))
              {
                 std::cout << "nan mismatch at " << r << "," << c << " output nan, correct is " << correct.at(r,c) << std::endl;
                 BOOST_CHECK(false);
              }
           }
           else
           {
               float diff = fabs( output.at(r,c)-correct.at(r,c) );
               if(input.at(r,c) == 0)
                  ++countNumPixelsG0;

               if( 1.0f < diff && 0.001 < fabs(diff / output.at(r,c))  )
               {
                    // I found that using boost_check_close failed for small values of output, so I did this instead.
                    std::cout << "at r,c: " << r << " " << c;
                    std::cout << " calc: " << output.at(r,c) << " correct: " << correct.at(r,c) << std::endl;
                    BOOST_CHECK(false);
               }
           }
       }

       if(countNumPixelsG0 == 1408)
           oneRowHasAllG0 = true;
    }

    // this little check verifies that the cma vals are not all NAN on one row, ie that our cma value is correct and real.
    BOOST_CHECK(oneRowHasAllG0);
}

BOOST_AUTO_TEST_CASE(sampleCMA_CDA_LAT)
{
    std::string pathToTestFiles = "/dls/detectors/Percival/test_data/LATcorrectionExample/";
    CalibratorReset calibratorR(rows, cols);
    CalibratorSample calibratorS(rows,cols);
    calibratorS.setCMA(true, 1152);

    calibratorR.loadADCGain(pathToTestFiles + "BSI04_Tminus20_dmuxSELsw_2019.11.20_ADCcor.h5");
    calibratorS.loadADCGain(pathToTestFiles + "BSI04_Tminus20_dmuxSELsw_2019.11.20_ADCcor.h5");
    calibratorS.loadLatGain(pathToTestFiles + "latgainfile.h5");

    MemBlockI16 input;
    MemBlockF reset, output, correct;
    input.init(rows,cols);
    correct.init(rows,cols);
    reset.init(rows,cols);
    output.init(rows,cols);
    input.loadFromH5(pathToTestFiles + "dataElaboration_example2_step1_2Img_DLSraw.h5", "/reset", 0);
    calibratorR.processFrame(input, reset);

    input.loadFromH5(pathToTestFiles + "dataElaboration_example2_step1_2Img_DLSraw.h5", "/data", 1);
    correct.loadFromH5(pathToTestFiles + "dataElaboration_example2_step4_1Img_cma_e.h5", "/data/data", 0);

    calibratorS.setResetFrame(reset);
    calibratorS.processFrame(input, output);

    bool oneRowHasAllG0 = false;
    for(int r=0;r<rows;++r)
    {
       int countNumPixelsG0 = 0;
       for(int c=0;c<cols;++c)
       {
           if(isnan(output.at(r,c)))
           {
              if(!isnan(correct.at(r,c)))
              {
                 std::cout << "nan mismatch at " << r << "," << c << " output nan, correct is " << correct.at(r,c) << std::endl;
                 BOOST_CHECK(false);
              }
           }
           else
           {
               float diff = fabs( output.at(r,c)-correct.at(r,c) );
               if(input.at(r,c) == 0)
                  ++countNumPixelsG0;

               if( 1.0f < diff && 0.001 < fabs(diff / output.at(r,c))  )
               {
                    // I found that using boost_check_close failed for small values of output, so I did this instead.
                    std::cout << "at r,c: " << r << " " << c;
                    std::cout << " calc: " << output.at(r,c) << " correct: " << correct.at(r,c) << std::endl;
                    BOOST_CHECK(false);
               }
           }
       }

       if(countNumPixelsG0 == 1408)
           oneRowHasAllG0 = true;
    }

    // this little check verifies that the cma vals are not all NAN on one row, ie that our cma value is correct and real.
    BOOST_CHECK(oneRowHasAllG0);

    // now do the same with a parallel computer and see how long it takes.
    input.loadFromH5(pathToTestFiles + "dataElaboration_example2_step1_2Img_DLSraw.h5", "/data", 1);
    output.setAll(0.0f);
    BOOST_CHECK_EQUAL(output.at(0,0),0.0f);

    auto t1 = std::chrono::high_resolution_clock::now();
    calibratorS.processFrameP(input, output);
    auto t2 = std::chrono::high_resolution_clock::now();

    auto d = std::chrono::duration_cast<std::chrono::microseconds>(t2-t1);
    std::cout << "S parallel process took " << d.count() << "us" << std::endl;

    for(int r=0;r<rows;++r)
    {
       for(int c=0;c<cols;++c)
       {
           if(isnan(output.at(r,c)))
           {
              if(!isnan(correct.at(r,c)))
              {
                 std::cout << "nan mismatch at " << r << "," << c << " output nan, correct is " << correct.at(r,c) << std::endl;
                 BOOST_CHECK(false);
              }
           }
           else
           {
               float diff = fabs( output.at(r,c)-correct.at(r,c) );

               if( 1.0f < diff && 0.001 < fabs(diff / output.at(r,c))  )
               {
                    // I found that using boost_check_close failed for small values of output, so I did this instead.
                    std::cout << "at r,c: " << r << " " << c;
                    std::cout << " calc: " << output.at(r,c) << " correct: " << correct.at(r,c) << std::endl;
                    BOOST_CHECK(false);
               }
           }
       }
   }
}

max_stars_repo_name: karmagh/percival-detector

max_issues_repo_name: karmagh/percival-detector

max_forks_repo_name: karmagh/percival-detector


text:
/*
 * StoreTarget.cpp
 *
 *  Created on: 19 Jan 2016
 *      Author: hieu
 */
#include <boost/foreach.hpp>
#include "StoreTarget.h"
#include "line_splitter.h"
#include "probing_hash_utils.h"
#include "OutputFileStream.h"
#include "moses2/legacy/Util2.h"

using namespace std;

namespace probingpt
{

StoreTarget::StoreTarget(const std::string &basepath)
  :m_basePath(basepath)
  ,m_vocab(basepath + "/TargetVocab.dat")
{
  std::string path = basepath + "/TargetColl.dat";
  m_fileTargetColl.open(path.c_str(),
                        std::ios::out | std::ios::binary | std::ios::ate | std::ios::trunc);
  if (!m_fileTargetColl.is_open()) {
    throw "can't create file ";
  }

}

StoreTarget::~StoreTarget()
{
  assert(m_coll.empty());
  m_fileTargetColl.close();

  // vocab
  m_vocab.Save();
}

uint64_t StoreTarget::Save()
{
  uint64_t ret = m_fileTargetColl.tellp();

  // save to disk
  uint64_t numTP = m_coll.size();
  m_fileTargetColl.write((char*) &numTP, sizeof(uint64_t));

  for (size_t i = 0; i < m_coll.size(); ++i) {
    Save(*m_coll[i]);
  }

  // clear coll
  Moses2::RemoveAllInColl(m_coll);
  m_coll.clear();

  // starting position of coll
  return ret;
}

void StoreTarget::Save(const target_text &rule)
{
  // metadata for each tp
  TargetPhraseInfo tpInfo;
  tpInfo.alignTerm = GetAlignId(rule.word_align_term);
  tpInfo.alignNonTerm = GetAlignId(rule.word_align_non_term);
  tpInfo.numWords = rule.target_phrase.size();
  tpInfo.propLength = rule.property.size();

  //cerr << "TPInfo=" << sizeof(TPInfo);
  m_fileTargetColl.write((char*) &tpInfo, sizeof(TargetPhraseInfo));

  // scores
  for (size_t i = 0; i < rule.prob.size(); ++i) {
    float prob = rule.prob[i];
    m_fileTargetColl.write((char*) &prob, sizeof(prob));
  }

  // tp
  for (size_t i = 0; i < rule.target_phrase.size(); ++i) {
    uint32_t vocabId = rule.target_phrase[i];
    m_fileTargetColl.write((char*) &vocabId, sizeof(vocabId));
  }

  // prop TODO

}

void StoreTarget::SaveAlignment()
{
  std::string path = m_basePath + "/Alignments.dat";
  probingpt::OutputFileStream file(path);

  BOOST_FOREACH(Alignments::value_type &valPair, m_aligns) {
    file << valPair.second << "\t";

    const std::vector<size_t> &aligns = valPair.first;
    BOOST_FOREACH(size_t align, aligns) {
      file << align << " ";
    }
    file << endl;
  }

}

void StoreTarget::Append(const line_text &line, bool log_prob, bool scfg)
{
  target_text *rule = new target_text;
  //cerr << "line.target_phrase=" << line.target_phrase << endl;

  // target_phrase
  vector<bool> nonTerms;
  util::TokenIter<util::SingleCharacter> it;
  it = util::TokenIter<util::SingleCharacter>(line.target_phrase,
       util::SingleCharacter(' '));
  while (it) {
    StringPiece word = *it;
    //cerr << "word=" << word << endl;

    bool nonTerm = false;
    if (scfg) {
      // not really sure how to handle factored SCFG and NT
      if (scfg && word[0] == '[' && word[word.size() - 1] == ']') {
        //cerr << "NON-TERM=" << tok << " " << nonTerms.size() << endl;
        nonTerm = true;
      }
      nonTerms.push_back(nonTerm);
    }

    util::TokenIter<util::SingleCharacter> itFactor;
    itFactor = util::TokenIter<util::SingleCharacter>(word,
               util::SingleCharacter('|'));
    while (itFactor) {
      StringPiece factor = *itFactor;

      string factorStr = factor.as_string();
      uint32_t vocabId = m_vocab.GetVocabId(factorStr);

      rule->target_phrase.push_back(vocabId);

      itFactor++;
    }

    it++;
  }

  // probs
  it = util::TokenIter<util::SingleCharacter>(line.prob,
       util::SingleCharacter(' '));
  while (it) {
    string tok = it->as_string();
    float prob = Moses2::Scan<float>(tok);

    if (log_prob) {
      prob = Moses2::FloorScore(log(prob));
      if (prob == 0.0f) prob = 0.0000000001;
    }

    rule->prob.push_back(prob);
    it++;
  }

  /*
  cerr << "nonTerms=";
  for (size_t i = 0; i < nonTerms.size(); ++i) {
    cerr << nonTerms[i] << " ";
  }
  cerr << endl;
  */

  // alignment
  it = util::TokenIter<util::SingleCharacter>(line.word_align,
       util::SingleCharacter(' '));
  while (it) {
    string tokPair = Moses2::Trim(it->as_string());
    if (tokPair.empty()) {
      break;
    }

    vector<size_t> alignPair = Moses2::Tokenize<size_t>(tokPair, "-");
    assert(alignPair.size() == 2);

    bool nonTerm = false;
    size_t sourcePos = alignPair[0];
    size_t targetPos = alignPair[1];
    if (scfg) {
      nonTerm = nonTerms[targetPos];
    }

    //cerr << targetPos << "=" << nonTerm << endl;

    if (nonTerm) {
      rule->word_align_non_term.push_back(sourcePos);
      rule->word_align_non_term.push_back(targetPos);
      //cerr << (int) rule->word_all1.back() << " ";
    } else {
      rule->word_align_term.push_back(sourcePos);
      rule->word_align_term.push_back(targetPos);
    }

    it++;
  }

  // extra scores
  string prop = line.property.as_string();
  AppendLexRO(prop, rule->prob, log_prob);

  //cerr << "line.property=" << line.property << endl;
  //cerr << "prop=" << prop << endl;

  // properties
  /*
   for (size_t i = 0; i < prop.size(); ++i) {
   rule->property.push_back(prop[i]);
   }
   */
  m_coll.push_back(rule);
}

uint32_t StoreTarget::GetAlignId(const std::vector<size_t> &align)
{
  boost::unordered_map<std::vector<size_t>, uint32_t>::iterator iter =
    m_aligns.find(align);
  if (iter == m_aligns.end()) {
    uint32_t ind = m_aligns.size();
    m_aligns[align] = ind;
    return ind;
  } else {
    return iter->second;
  }
}

void StoreTarget::AppendLexRO(std::string &prop, std::vector<float> &retvector,
                              bool log_prob) const
{
  size_t startPos = prop.find("{{LexRO ");

  if (startPos != string::npos) {
    size_t endPos = prop.find("}}", startPos + 8);
    string lexProb = prop.substr(startPos + 8, endPos - startPos - 8);
    //cerr << "lexProb=" << lexProb << endl;

    // append lex probs to pt probs
    vector<float> scores = Moses2::Tokenize<float>(lexProb);

    if (log_prob) {
      for (size_t i = 0; i < scores.size(); ++i) {
        scores[i] = Moses2::FloorScore(log(scores[i]));
        if (scores[i] == 0.0f) scores[i] = 0.0000000001;
      }
    }

    for (size_t i = 0; i < scores.size(); ++i) {
      retvector.push_back(scores[i]);
    }

    // exclude LexRO property from property column
    prop = prop.substr(0, startPos)
           + prop.substr(endPos + 2, prop.size() - endPos - 2);
    //cerr << "line.property_to_be_binarized=" << line.property_to_be_binarized << "AAAA" << endl;
  }
}

} /* namespace Moses2 */


text:
/*
 * Copyright 2020 Adobe. All rights reserved.
 * This file is licensed 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 REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
#include <lagrange/common.h>
#include <lagrange/compute_facet_area.h>
#include <lagrange/io/load_mesh.impl.h>
#include <lagrange/quad_to_tri.h>
#include <Eigen/Core>
#include <lagrange/testing/common.h>

namespace lagrange_test_internal {

template <typename MeshType1, typename MeshType2>
void assert_same_area(MeshType1& mesh1, MeshType2& mesh2)
{
    compute_facet_area(mesh1);
    compute_facet_area(mesh2);

    const auto& area1 = mesh1.get_facet_attribute("area");
    const auto& area2 = mesh2.get_facet_attribute("area");

    REQUIRE(area1.sum() == Approx(area2.sum()));

    if (mesh1.is_uv_initialized() && mesh2.is_uv_initialized()) {
        const auto uv1 = mesh1.get_uv_mesh();
        const auto uv2 = mesh2.get_uv_mesh();

        compute_facet_area(*uv1);
        compute_facet_area(*uv2);
        const auto& uv_area1 = uv1->get_facet_attribute("area");
        const auto& uv_area2 = uv2->get_facet_attribute("area");

        REQUIRE(uv_area1.sum() == Approx(uv_area2.sum()));
    }
}

} // namespace lagrange_test_internal

TEST_CASE("quad_to_tri juicebox", "[mesh][quad_to_tri][juicebox]" LA_SLOW_FLAG)
{
    using namespace lagrange;

    auto mesh = lagrange::testing::load_mesh<QuadMesh3D>("corp/core/juicebox.obj");
    REQUIRE(mesh->get_vertex_per_facet() == 4);
    auto tri_mesh = quad_to_tri(*mesh);
    REQUIRE(tri_mesh->get_vertex_per_facet() == 3);

    mesh->initialize_components();
    tri_mesh->initialize_components();
    REQUIRE(mesh->get_num_components() == tri_mesh->get_num_components());

    lagrange_test_internal::assert_same_area(*mesh, *tri_mesh);
}

TEST_CASE("quad_to_tri banner_single", "[mesh][quad_to_tri][banner_single]" LA_SLOW_FLAG)
{
    using namespace lagrange;

    auto mesh = lagrange::testing::load_mesh<QuadMesh3D>("corp/core/banner_single.obj");
    REQUIRE(mesh->get_vertex_per_facet() == 4);
    auto tri_mesh = quad_to_tri(*mesh);
    REQUIRE(tri_mesh->get_vertex_per_facet() == 3);

    mesh->initialize_components();
    tri_mesh->initialize_components();
    REQUIRE(mesh->get_num_components() == tri_mesh->get_num_components());

    lagrange_test_internal::assert_same_area(*mesh, *tri_mesh);
}

TEST_CASE("quad_to_tri attribute", "[mesh][quad_to_tri][attribute]")
{
    using namespace lagrange;
    Eigen::Matrix<float, Eigen::Dynamic, 3> vertices(8, 3);
    vertices << 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0,
        1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0;

    Eigen::Matrix<int, Eigen::Dynamic, 4> facets(6, 4);
    facets << 3, 2, 1, 0, 4, 5, 6, 7, 0, 1, 5, 4, 1, 2, 6, 5, 7, 6, 2, 3, 4, 7, 3, 0;

    auto cube = wrap_with_mesh(vertices, facets);

    using MeshType = decltype(cube)::element_type;
    using AttributeArray = MeshType::AttributeArray;
    using Scalar = typename MeshType::Scalar;
    using Index = typename MeshType::Index;

    AttributeArray vertex_indices(8, 1);
    AttributeArray facet_indices(6, 1);
    AttributeArray corner_indices(24, 1);
    for (int i = 0; i < 8; i++) {
        vertex_indices(i, 0) = safe_cast<Scalar>(i);
    }
    for (int i = 0; i < 6; i++) {
        facet_indices(i, 0) = safe_cast<Scalar>(i);
    }
    for (int i = 0; i < 24; i++) {
        corner_indices(i, 0) = safe_cast<Scalar>(i);
    }

    cube->add_vertex_attribute("index");
    cube->set_vertex_attribute("index", vertex_indices);
    cube->add_facet_attribute("index");
    cube->set_facet_attribute("index", facet_indices);
    cube->add_corner_attribute("index");
    cube->set_corner_attribute("index", corner_indices);

    auto tri_cube = quad_to_tri(*cube);

    SECTION("vertex_attribute")
    {
        REQUIRE(tri_cube->has_vertex_attribute("index"));
        const auto& index = tri_cube->get_vertex_attribute("index");
        for (auto i : range(tri_cube->get_num_vertices())) {
            const auto& tri_vi = tri_cube->get_vertices().row(i);
            const auto v_idx = safe_cast<Index>(index(i, 0));
            const auto& cube_vi = cube->get_vertices().row(v_idx);
            REQUIRE((tri_vi - cube_vi).norm() == Approx(0.0));
        }
    }

    SECTION("facet_index")
    {
        REQUIRE(tri_cube->has_facet_attribute("index"));
        const auto& index = tri_cube->get_facet_attribute("index");
        std::vector<int> counter(6, 0);
        for (auto i : range(tri_cube->get_num_facets())) {
            counter[safe_cast<int>(index(i, 0))]++;
        }
        for (int i = 0; i < 6; i++) {
            REQUIRE(counter[i] == 2);
        }
    }

    SECTION("corner_index")
    {
        REQUIRE(tri_cube->has_corner_attribute("index"));
        const auto& index = tri_cube->get_corner_attribute("index").cast<int>();
        const auto& tri_facets = tri_cube->get_facets();
        for (int i = 0; i < 36; i++) {
            const auto& tri_vi = tri_cube->get_vertices().row(tri_facets(i / 3, i % 3));
            const auto& cube_vi =
                cube->get_vertices().row(facets(index(i, 0) / 4, index(i, 0) % 4));
            REQUIRE((tri_vi - cube_vi).norm() == Approx(0.0));
        }
    }
}


text: #include <boost/graph/directed_graph.hpp>

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino