text:
//  Copyright (c) 2014-2015 Thomas Heller
//
//  SPDX-License-Identifier: BSL-1.0
//  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 <hpx/hpx_init.hpp>
#include <hpx/hpx.hpp>

#include <hpx/format.hpp>
#include <hpx/include/parallel_algorithm.hpp>
#include <hpx/include/parallel_executors.hpp>
#include <hpx/include/parallel_numeric.hpp>
#include <hpx/include/serialization.hpp>
#include <hpx/topology/topology.hpp>
#include <hpx/util/from_string.hpp>

#include <hpx/compute/host/numa_allocator.hpp>

#include <boost/range/irange.hpp>

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <memory>
#include <numeric>
#include <string>
#include <utility>
#include <vector>

#define COL_SHIFT 1000.00           // Constant to shift column index
#define ROW_SHIFT 0.001             // Constant to shift row index

///////////////////////////////////////////////////////////////////////////////
hpx::threads::topology& retrieve_topology()
{
    static hpx::threads::topology& topo = hpx::threads::create_topology();
    return topo;
}

bool verbose = false;

char const* A_block_basename = "/transpose/block/A";
char const* B_block_basename = "/transpose/block/B";

struct sub_block
{
    enum mode {
        reference
      , owning
    };

    sub_block()
      : size_(0)
      , data_(nullptr)
      , mode_(reference)
    {}

    sub_block(double * data, std::uint64_t size)
      : size_(size)
      , data_(data)
      , mode_(reference)
    {}

    ~sub_block()
    {
        if(data_ && mode_ == owning)
        {
            delete[] data_;
        }
    }

    sub_block(sub_block && other)
      : size_(other.size_)
      , data_(other.data_)
      , mode_(other.mode_)
    {
        if(mode_ == owning) { other.data_ = nullptr; other.size_ = 0; }
    }

    sub_block & operator=(sub_block && other)
    {
        size_ = other.size_;
        data_ = other.data_;
        mode_ = other.mode_;
        if(mode_ == owning) { other.data_ = nullptr; other.size_ = 0; }

        return *this;
    }

    double operator[](std::size_t i) const
    {
        HPX_ASSERT(data_);
        return data_[i];
    }

    double & operator[](std::size_t i)
    {
        HPX_ASSERT(data_);
        HPX_ASSERT(mode_ == reference);
        return data_[i];
    }

    void load(hpx::serialization::input_archive & ar, unsigned version)
    {
        ar & size_;
        if(size_ > 0)
        {
            data_ = new double[size_];
            hpx::serialization::array<double> arr(data_, size_);
            ar >> arr;
            mode_ = owning;
        }
    }

    void save(hpx::serialization::output_archive & ar, unsigned version) const
    {
        ar & size_;
        if(size_ > 0)
        {
            hpx::serialization::array<double> arr(data_, size_);
            ar << arr;
        }
    }

    HPX_SERIALIZATION_SPLIT_MEMBER()

    std::uint64_t size_;
    double * data_;
    mode mode_;
};

///////////////////////////////////////////////////////////////////////////////
// dirty workaround to avoid serialization of executors
typedef
    hpx::parallel::execution::local_priority_queue_attached_executor
    executor_type;
typedef
    std::vector<executor_type>
    executors_vector;
typedef
    hpx::parallel::util::numa_allocator<double, executors_vector>
    allocator_type;

executors_vector execs;

struct block_component
  : hpx::components::component_base<block_component>
{
    block_component()
      : data_(0, 0.0, allocator_type(execs_, retrieve_topology()))
    {
        // This ctor should never be called.
        HPX_ASSERT(false);
    }

    block_component(std::uint64_t size, std::size_t numa_domain)
      : execs_(1, execs[numa_domain])
      , data_(size, 0.0, allocator_type(execs_, retrieve_topology()))
    {}

    sub_block get_sub_block(std::uint64_t offset, std::uint64_t size)
    {
        HPX_ASSERT(!data_.empty());
        return sub_block(&data_[offset], size);
    }

    HPX_DEFINE_COMPONENT_DIRECT_ACTION(block_component, get_sub_block);

    executors_vector execs_;
    std::vector<double, allocator_type> data_;
};

struct block
  : hpx::components::client_base<block, block_component>
{
    typedef hpx::components::client_base<block, block_component> base_type;

    block() {}

    block(hpx::future<hpx::id_type> id)
      : base_type(std::move(id))
    {
    }

    block(
        std::uint64_t id, std::uint64_t size, const char * base_name,
        std::size_t numa_domain)
      : base_type(hpx::new_<block_component>(hpx::find_here(), size, numa_domain))
    {
    }

    hpx::future<sub_block> get_sub_block(std::uint64_t offset, std::uint64_t size)
    {
        block_component::get_sub_block_action act;
        return hpx::async(act, get_id(), offset, size);
    }
};

// The macros below are necessary to generate the code required for exposing
// our partition type remotely.
//
// HPX_REGISTER_COMPONENT() exposes the component creation
// through hpx::new_<>().
typedef hpx::components::component<block_component> block_component_type;
HPX_REGISTER_COMPONENT(block_component_type, block_component);

// HPX_REGISTER_ACTION() exposes the component member function for remote
// invocation.
typedef block_component::get_sub_block_action get_sub_block_action;
HPX_REGISTER_ACTION(get_sub_block_action);

void transpose(hpx::future<sub_block> Af, hpx::future<sub_block> Bf,
    std::uint64_t block_order, std::uint64_t tile_size, std::uint64_t domain);
double test_results(std::uint64_t order, std::uint64_t block_order,
    std::vector<block> & trans, std::uint64_t blocks_start,
    std::uint64_t blocks_end, std::uint64_t domain);

///////////////////////////////////////////////////////////////////////////////
std::size_t get_num_numa_nodes(hpx::threads::topology const& topo,
    hpx::program_options::variables_map& vm)
{
    std::size_t numa_nodes = topo.get_number_of_numa_nodes();
    if (numa_nodes == 0)
        numa_nodes = topo.get_number_of_sockets();

    std::string num_numa_domains_str =
        vm["transpose-numa-domains"].as<std::string>();

    if (num_numa_domains_str != "all")
    {
        numa_nodes = hpx::util::from_string<std::size_t>(num_numa_domains_str);
    }

    return numa_nodes;
}

std::pair<std::size_t, std::size_t> get_num_numa_pus(
    hpx::threads::topology const& topo, std::size_t numa_nodes,
    hpx::program_options::variables_map& vm)
{
    std::size_t numa_pus = hpx::threads::hardware_concurrency() / numa_nodes;

    std::string num_threads_str = vm["transpose-threads"].as<std::string>();
    std::size_t pus = numa_pus;

    if(num_threads_str != "all")
    {
        pus = hpx::util::from_string<std::size_t>(num_threads_str);
    }

    return std::make_pair(numa_pus, pus);
}

///////////////////////////////////////////////////////////////////////////////
int hpx_main(hpx::program_options::variables_map& vm)
{
    {
        // extract hardware topology
        hpx::threads::topology const& topo = retrieve_topology();

        std::size_t numa_nodes = get_num_numa_nodes(topo, vm);
        std::pair<std::size_t, std::size_t> pus =
            get_num_numa_pus(topo, numa_nodes, vm);

        hpx::id_type here = hpx::find_here();
        bool root = here == hpx::find_root_locality();

        std::uint64_t num_localities = hpx::get_num_localities().get();

        std::uint64_t order = vm["matrix_size"].as<std::uint64_t>();
        std::uint64_t iterations = vm["iterations"].as<std::uint64_t>();
        std::uint64_t num_local_blocks = vm["num_blocks"].as<std::uint64_t>();
        std::uint64_t tile_size = order;

        if(vm.count("tile_size"))
            tile_size = vm["tile_size"].as<std::uint64_t>();

        verbose = vm.count("verbose") ? true : false;

        execs.reserve(numa_nodes);

        // creating our executors ....
        for (std::size_t i = 0; i != numa_nodes; ++i)
        {
            // create executor for this NUMA domain
            execs.emplace_back(i * pus.first, pus.second);
        }

        ///////////////////////////////////////////////////////////////////////
        std::uint64_t bytes =
            static_cast<std::uint64_t>(2.0 * sizeof(double) * order * order);

        std::uint64_t num_blocks = num_localities * num_local_blocks;
        std::uint64_t num_numa_blocks = num_local_blocks / numa_nodes;

        std::uint64_t block_order = order / num_blocks;
        std::uint64_t col_block_size = order * block_order;

        std::uint64_t id = hpx::get_locality_id();

        std::vector<block> A(num_blocks);
        std::vector<block> B(num_blocks);

        std::uint64_t blocks_start = id * num_local_blocks;
        std::uint64_t blocks_end = (id + 1) * num_local_blocks;

        std::vector<boost::integer_range<std::uint64_t> > numa_ranges;
        numa_ranges.reserve(numa_nodes);

        // Actually allocate the block components in AGAS
        std::uint64_t numa_block_begin = blocks_start;
        std::uint64_t block_numa_node = 0;
        std::uint64_t numa_blocks_allocated = 0;

        // Allocate our block components in AGAS
        for(std::uint64_t b = blocks_start; b != blocks_end; ++b)
        {
            A[b] = block(b, col_block_size, A_block_basename, block_numa_node);
            B[b] = block(b, col_block_size, B_block_basename, block_numa_node);
            ++numa_blocks_allocated;
            if(numa_blocks_allocated == num_numa_blocks)
            {
                std::cout << block_numa_node << ": "
                    << numa_block_begin << " " << b + 1 << "\n";
                numa_ranges.push_back(boost::irange(numa_block_begin, b + 1));
                numa_block_begin = b + 1;
                ++block_numa_node;
                numa_blocks_allocated = 0;
            }
        }

        // establish connection between localities, refer to all blocks from
        // everywhere
        std::vector<hpx::future<hpx::id_type> > A_ids
            = hpx::find_all_from_basename(A_block_basename, num_blocks);
        std::vector<hpx::future<hpx::id_type> > B_ids
            = hpx::find_all_from_basename(B_block_basename, num_blocks);

        if(root)
        {
            std::cout
                << "Serial Matrix transpose: B = A^T\n"
                << "Matrix order          = " << order << "\n"
                << "Matrix local columns  = " << block_order << "\n"
                << "Number of blocks      = " << num_blocks << "\n"
                << "Number of localities  = " << num_localities << "\n";
            if(tile_size < order)
                std::cout << "Tile size             = " << tile_size << "\n";
            else
                std::cout << "Untiled\n";
            std::cout
                << "Number of iterations  = " << iterations << "\n";
        }

        using hpx::parallel::for_each;
        using hpx::parallel::execution::par;

        // Fill the original matrix, set transpose to known garbage value.
        auto range = boost::irange(blocks_start, blocks_end);
        for_each(par, std::begin(range), std::end(range),
            [&](std::uint64_t b)
            {
                std::shared_ptr<block_component> A_ptr =
                    hpx::get_ptr<block_component>(A[b].get_id()).get();
                std::shared_ptr<block_component> B_ptr =
                    hpx::get_ptr<block_component>(B[b].get_id()).get();

                for(std::uint64_t i = 0; i != order; ++i)
                {
                    for(std::uint64_t j = 0; j != block_order; ++j)
                    {
                        double col_val = COL_SHIFT *
                            static_cast<double>(b * block_order + j);
                        A_ptr->data_[i * block_order + j] =
                            col_val + ROW_SHIFT * i;
                        B_ptr->data_[i * block_order + j] = -1.0;
                    }
                }

                // register the blocks for other localities to discover
                hpx::register_with_basename(A_block_basename, A[b].get_id(), b);
                hpx::register_with_basename(B_block_basename, B[b].get_id(), b);
            }
        );

        hpx::wait_all(A_ids);
        hpx::wait_all(B_ids);

        for(std::uint64_t b = 0; b != num_blocks; ++b)
        {
            // Convert id to our client
            if(b < blocks_start || b >= blocks_end)
            {
                A[b] = block(std::move(A_ids[b]));
                B[b] = block(std::move(B_ids[b]));
            }
        }

        double avgtime = 0.0;
        double maxtime = 0.0;
        double mintime = 366.0 * 24.0*3600.0; // set the minimum time to a large value;
                                              // one leap year should be enough

        hpx::lcos::local::barrier b(numa_ranges.size());

        // perform actual transpose
        std::vector<hpx::future<double> > numa_workers;
        numa_workers.reserve(numa_ranges.size());
        for(std::uint64_t domain = 0; domain < numa_ranges.size(); ++domain)
        {
            numa_workers.push_back(
                hpx::async(
                    execs[domain],
                    [&, domain]() -> double
                    {
                        double errsq = 0.0;
                        for(std::uint64_t iter = 0; iter < iterations; ++iter)
                        {
                            b.wait();
                            hpx::util::high_resolution_timer t;

                            auto range = numa_ranges[domain];

                            std::uint64_t block_start = *std::begin(range);
                            std::uint64_t block_end = *std::end(range);
                            std::uint64_t blocks_size = block_end - block_start;

                            std::vector<hpx::future<void> > block_futures;
                            block_futures.resize(blocks_size);

                            for_each(par.on(execs[domain]),
                                std::begin(range), std::end(range),
                                [
                                    domain, &block_futures, num_blocks,
                                    block_start, block_order, tile_size, &A, &B
                                ]
                                (std::uint64_t b)
                                {
                                    std::vector<hpx::future<void> > phase_futures;
                                    phase_futures.reserve(num_blocks);

                                    auto phase_range = boost::irange(
                                        static_cast<std::uint64_t>(0), num_blocks);
                                    for(std::uint64_t phase: phase_range)
                                    {
                                        const std::uint64_t block_size =
                                            block_order * block_order;
                                        const std::uint64_t from_block = phase;
                                        const std::uint64_t from_phase = b;
                                        const std::uint64_t A_offset =
                                            from_phase * block_size;
                                        const std::uint64_t B_offset =
                                            phase * block_size;

                                        phase_futures.push_back(
                                            hpx::dataflow(
                                                execs[domain]
                                              , &transpose
                                              , A[from_block].get_sub_block(
                                                    A_offset, block_size)
                                              , B[b].get_sub_block(B_offset, block_size)
                                              , block_order
                                              , tile_size
                                              , domain
                                            )
                                        );
                                    }

                                    block_futures[b - block_start] =
                                        hpx::when_all(phase_futures);
                                }
                            );

                            hpx::wait_all(block_futures);

                            b.wait();
                            double elapsed = t.elapsed();

                            if(domain == 0)
                            {
                                // Skip the first iteration
                                if(iter > 0 || iterations == 1)
                                {
                                    avgtime = avgtime + elapsed;
                                    maxtime = (std::max)(maxtime, elapsed);
                                    mintime = (std::min)(mintime, elapsed);
                                }
                            }

                            errsq += test_results(order, block_order, B,
                                block_start, block_end, domain);
                        }
                        return errsq;
                    }
                )
            );
        }
        std::vector<double> errsqs = hpx::util::unwrap(numa_workers);

        ///////////////////////////////////////////////////////////////////////
        // Analyze and output results
        if(root)
        {
            double errsq = std::accumulate(errsqs.begin(), errsqs.end(), 0.0);

            std::cout << "Solution validates\n";
            avgtime = avgtime/static_cast<double>(
                (std::max)(iterations-1, static_cast<std::uint64_t>(1)));
            std::cout
                << "Rate (MB/s): " << 1.e-6 * bytes/mintime << ", "
                << "Avg time (s): " << avgtime << ", "
                << "Min time (s): " << mintime << ", "
                << "Max time (s): " << maxtime << "\n";

            if(verbose)
                std::cout << "Squared errors: " << errsq << "\n";
        }

        {
            // unregister names...
            auto range = boost::irange(blocks_start, blocks_end);
            for (auto b: range)
            {
                hpx::unregister_with_basename(A_block_basename, b);
                hpx::unregister_with_basename(B_block_basename, b);
            }
        }
    }

    return hpx::finalize();
}

// Launch with something like:
//
// --transpose-numa-domains=2 --transpose-threads=6
//
// There shouldn't be any need for using  --hpx:threads or --hpx:bind, those
// are computed internally.
//
int main(int argc, char* argv[])
{
    using namespace hpx::program_options;

    options_description desc_commandline;
    desc_commandline.add_options()
        ("matrix_size", value<std::uint64_t>()->default_value(1024),
         "Matrix Size")
        ("iterations", value<std::uint64_t>()->default_value(10),
         "# iterations")
        ("tile_size", value<std::uint64_t>(),
         "Number of tiles to divide the individual matrix blocks for improved "
         "cache and TLB performance")
        ("num_blocks", value<std::uint64_t>()->default_value(1),
         "Number of blocks to divide the individual matrix blocks for "
         "improved cache and TLB performance")
        ( "verbose", "Verbose output")
        ("transpose-threads",
         hpx::program_options::value<std::string>()->default_value("all"),
         "number of threads per NUMA domain to use. (default: all)")
        ("transpose-numa-domains",
         hpx::program_options::value<std::string>()->default_value("all"),
         "number of NUMA domains to use. (default: all)")
    ;

    // parse command line here to extract the necessary settings for HPX
    parsed_options opts =
        command_line_parser(argc, argv)
            .allow_unregistered()
            .options(desc_commandline)
            .style(command_line_style::unix_style)
            .run();

    variables_map vm;
    store(opts, vm);

    hpx::threads::topology const& topo = retrieve_topology();
    std::size_t numa_nodes = get_num_numa_nodes(topo, vm);
    std::pair<std::size_t, std::size_t> pus =
        get_num_numa_pus(topo, numa_nodes, vm);
    //std::size_t num_cores = topo.get_number_of_numa_node_cores(0);

    // Initialize and run HPX, this example requires to run hpx_main on all
    // localities
    std::vector<std::string> cfg = {
        "hpx.run_hpx_main!=1",
        "hpx.numa_sensitive=2",  // no-cross NUMA stealing
        // block all cores of requested number of NUMA-domains
//        hpx::util::format("hpx.cores={}", numa_nodes * num_cores),
//        hpx::util::format("hpx.os_threads={}", numa_nodes * pus.second)
    };

    std::string node_name("numanode");
    if (topo.get_number_of_numa_nodes() == 0)
        node_name = "socket";

    std::string bind_desc("hpx.bind!=");
    for (std::size_t i = 0; i != numa_nodes; ++i)
    {
        if (i != 0)
            bind_desc += ";";

        std::size_t base_thread = i * pus.second;
        bind_desc += hpx::util::format(
            "thread:{}-{}={}:{}.core:0-{}.pu:0",
            base_thread, (base_thread+pus.second-1), // thread:%d-%d
            node_name, i,                            // %s:%d
            pus.second-1                             // core:0-%d
        );
    }
//    cfg.push_back(bind_desc);

//    return hpx::init();
    return hpx::init(desc_commandline, argc, argv, cfg);
}

void transpose(hpx::future<sub_block> Af, hpx::future<sub_block> Bf,
    std::uint64_t block_order, std::uint64_t tile_size,
    std::uint64_t domain)
{
    sub_block const A = Af.get();
    sub_block B = Bf.get();

    if(tile_size < block_order)
    {
        for(std::uint64_t i = 0; i < block_order; i += tile_size)
        {
            for(std::uint64_t j = 0; j < block_order; j += tile_size)
            {
                std::uint64_t max_i = (std::min)(block_order, i + tile_size);
                std::uint64_t max_j = (std::min)(block_order, j + tile_size);

                for(std::uint64_t it = i; it != max_i; ++it)
                {
                    for(std::uint64_t jt = j; jt != max_j; ++jt)
                    {
                        B[it + block_order * jt] = A[jt + block_order * it];
                    }
                }
            }
        }
    }
    else
    {
        for(std::uint64_t i = 0; i != block_order; ++i)
        {
            for(std::uint64_t j = 0; j != block_order; ++j)
            {
                B[i + block_order * j] = A[j + block_order * i];
            }
        }
    }
}

double test_results(std::uint64_t order, std::uint64_t block_order,
    std::vector<block> & trans, std::uint64_t blocks_start,
    std::uint64_t blocks_end, std::uint64_t domain)
{
    using hpx::parallel::transform_reduce;
    using hpx::parallel::execution::par;

    // Fill the original matrix, set transpose to known garbage value.
    auto range = boost::irange(blocks_start, blocks_end);
    double errsq =
        transform_reduce(
            par.on(execs[domain]),
            std::begin(range), std::end(range), 0.0,
            [](double lhs, double rhs) { return lhs + rhs; },
            [&](std::uint64_t b) -> double
            {
                sub_block trans_block =
                    trans[b].get_sub_block(0, order * block_order).get();
                double errsq = 0.0;
                for(std::uint64_t i = 0; i < order; ++i)
                {
                    double col_val = COL_SHIFT * i;
                    for(std::uint64_t j = 0; j < block_order; ++j)
                    {
                        double diff = trans_block[i * block_order + j] -
                            (col_val +
                                ROW_SHIFT *
                                    static_cast<double>(b * block_order + j));
                        errsq += diff * diff;
                    }
                }
                return errsq;
            }
        );

    if(verbose)
        std::cout << " Squared sum of differences: " << errsq << "\n";

    return errsq;
}

max_stars_repo_name: picanumber/hpx

max_issues_repo_name: picanumber/hpx

max_forks_repo_name: picanumber/hpx


text:
//==================================================================================================
/*!
  @file

    @copyright 2016 NumScale SAS

  Distributed under the Boost Software License, Version 1.0.
  (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/
//==================================================================================================
#ifndef BOOST_SIMD_FUNCTION_IS_FINITE_HPP_INCLUDED
#define BOOST_SIMD_FUNCTION_IS_FINITE_HPP_INCLUDED

#if defined(DOXYGEN_ONLY)
namespace boost { namespace simd
{

  /*!

    @ingroup group-predicates
    This function object returns @ref True if x is not inf, -inf or nan, else returns @ref False.

    @par Header <boost/simd/function/is_finite.hpp>

    @par Note

    Using `is_finite(x)` is similar to `x-x == Zero`

    @par Alias:
    is_not_invalid

    @par Example:

      @snippet is_finite.cpp is_finite

    @par Possible output:

      @snippet is_finite.txt is_finite

  **/
  as_logical_t<Value> is_finite(Value const& x);
} }
#endif

#include <boost/simd/function/scalar/is_finite.hpp>
#include <boost/simd/function/simd/is_finite.hpp>

#endif

ext: hpp

max_stars_repo_name: SylvainCorlay/pythran

max_issues_repo_name: SylvainCorlay/pythran

max_forks_repo_name: SylvainCorlay/pythran


text:
#include "token.hpp"
#include <stdexcept>
#include <boost/algorithm/string.hpp>

using std::logic_error;
using std::ostream;
using std::string;

using boost::is_any_of;
using boost::to_upper;
using boost::trim_right_if;

namespace motts { namespace lox {
    ostream& operator<<(ostream& os, Token_type token_type) {
        // IIFE so I can use return rather than assign-and-break
        string name {([&] () {
            switch (token_type) {
                #define X(name) \
                    case Token_type::name: \
                        return #name;
                MOTTS_LOX_TOKEN_TYPE_NAMES
                #undef X

                default:
                    throw logic_error{"Unexpected token type."};
            }
        })()};

        // Field names should print as uppercase without trailing underscores
        trim_right_if(name, is_any_of("_"));
        to_upper(name);

        os << name;

        return os;
    }

    ostream& operator<<(ostream& os, const Token& token) {
        os << token.type << " " << token.lexeme << " ";
        if (token.literal) {
            os << *(token.literal);
        } else {
            os << "null";
        }

        return os;
    }
}}

ext: cpp

max_stars_repo_name: Jeff-Mott-OR/cpplox

max_issues_repo_name: Jeff-Mott-OR/cpplox

max_forks_repo_name: Jeff-Mott-OR/cpplox


text: #include <boost/fusion/sequence/convert.hpp>

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text:
#include "ComponentWidget.hpp"

#include <QtGui>
#if QT_VERSION >= 0x050000
#include <QtWidgets>
#endif
#include <iostream>

#include <boost/lexical_cast.hpp>

ComponentWidget::ComponentWidget(DisCODe::ComponentProxy * proxy, DisCODe::SystemProxy * system, QWidget *parent) :
	QWidget(parent),
	m_proxy(proxy),
	m_system(system)
{

	QVBoxLayout * top_layout = new QVBoxLayout;

	// Log level indicator
	QSignalMapper * logmapper = new QSignalMapper(this);
	QGroupBox * log_box = new QGroupBox("Log level");
	QHBoxLayout * log_lay = new QHBoxLayout;
	QStringList levels;
	levels << "Trace" << "Debug" << "Info" << "Notice" << "Warning" << "Error" << "Critical" << "Fatal";
	int syslvl = system->getLogLvl();
	for (int i = 0; i < levels.count(); ++i) {
		QPushButton * btn = new QPushButton(levels[i]);
		btn->setCheckable(true);
		btn->setAutoExclusive(true);
		if (i == proxy->getBump()+syslvl) btn->setChecked(true);
		btn->setMinimumWidth(30);
		log_lay->addWidget(btn);
		logmapper->setMapping(btn, syslvl-i);
		connect(btn, SIGNAL(clicked()), logmapper, SLOT(map()), Qt::QueuedConnection);
	}
	log_box->setLayout(log_lay);
	connect(logmapper, SIGNAL(mapped(int)), this, SLOT(setBump(int)));
	top_layout->addWidget(log_box);

	// Properties
	QString group_separator = ".";
	int pc = proxy->countProperties();

	if (pc > 0) {
		QSignalMapper * signalMapper = new QSignalMapper(this);
		QGroupBox * group_box = new QGroupBox(QString(proxy->name().c_str()) + " properties");
		QGridLayout * layout = new QGridLayout;

		QMap<QString, QGroupBox*> boxes;
		QMap<QString, QGridLayout*> layouts;

		boxes["Ungrouped"] = group_box;
		layouts["Ungrouped"] = layout;

		for (int i = 0; i < pc; ++i) {
			QString ptype = proxy->getPropertyType(i).c_str();
			QString pname = proxy->getPropertyName(i).c_str();
			QString fullname = pname;
			QString pttip = proxy->getPropertyToolTip(i).c_str();
			QString group = "Ungrouped";

			QWidget * widget;
			QWidget * emitter = NULL;

			if (pname.contains(group_separator)) {
				group = pname.left(pname.indexOf(group_separator));
				pname.remove(0, pname.indexOf(group_separator)+1);
			}

			if (!boxes.contains(group)) {
				boxes[group] = new QGroupBox(group);

				//boxes[group]->setCheckable(true);
				//boxes[group]->setChecked(true);
				//connect(boxes[group], SIGNAL(toggled(bool)), boxes[group], SLOT(setVisible(bool)));

				layouts[group] = new QGridLayout;
			}

			layout = layouts[group];

			// boolean value rendered as CheckBox
			if (ptype == "b") {
				QCheckBox * check = new QCheckBox(pname);
				check->setChecked(boost::lexical_cast<bool>(proxy->getPropertyValue(i)));

				connect(check, SIGNAL(toggled(bool)), signalMapper, SLOT(map()));

				widget = check;
			} else
			// integer value rendered as SpinBox
			if (ptype == "i") {
				QSpinBox * spin = new QSpinBox;

				std::vector<std::string> vcon = proxy->getPropertyConstraints(i);

				int vmin = 0, vmax = 100;

				if (vcon.size() > 0)
					vmin = boost::lexical_cast<int>(vcon[0]);

				if (vcon.size() > 1)
					vmax = boost::lexical_cast<int>(vcon[1]);

				spin->setRange(vmin, vmax);
				spin->setValue(boost::lexical_cast<int>(proxy->getPropertyValue(i)));

				connect(spin, SIGNAL(valueChanged(int)), signalMapper, SLOT(map()));

				widget = spin;
			} else
			// choice out of fixed list of possibilities rendered as ComboBox
			if (ptype == "combo") {
				QComboBox * combo = new QComboBox;

				QStringList consts;
				std::vector<std::string> vcon = proxy->getPropertyConstraints(i);

				for (size_t cc = 0; cc < vcon.size(); ++cc) {
					std::cout << vcon[cc] << std::endl;
					consts.append(vcon[cc].c_str());
				}

				combo->addItems(consts);

				combo->setCurrentIndex(combo->findText(proxy->getPropertyValue(i).c_str()));

				connect(combo, SIGNAL(currentIndexChanged(int)), signalMapper, SLOT (map()));
				widget = combo;
			} else
			// integer value rendered as Slider+SpinBox
			if (ptype == "range") {
				QWidget * w = new QWidget;
				QHBoxLayout * lay = new QHBoxLayout;
				lay->setSpacing(3);
				lay->setMargin(0);

				QSlider * slider = new QSlider;
				slider->setOrientation(Qt::Horizontal);

				QSpinBox * spin = new QSpinBox;

				std::vector<std::string> vcon = proxy->getPropertyConstraints(i);

				if (vcon.size() > 0)
					slider->setMinimum(boost::lexical_cast<int>(vcon[0]));

				if (vcon.size() > 1)
					slider->setMaximum(boost::lexical_cast<int>(vcon[1]));

				lay->addWidget(new QLabel(QString::number(slider->minimum())));
				lay->addWidget(slider);
				lay->addWidget(new QLabel(QString::number(slider->maximum())));

				spin->setRange(slider->minimum(), slider->maximum());

				connect(slider, SIGNAL(valueChanged(int)), spin, SLOT(setValue(int)));
				connect(spin, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));

				slider->setValue(boost::lexical_cast<int>(proxy->getPropertyValue(i)));

				connect(slider, SIGNAL(valueChanged(int)), signalMapper, SLOT (map()));

				QFrame * line = new QFrame;
				line->setFrameShape(QFrame::VLine);
				line->setFrameStyle(QFrame::Sunken);
				lay->addWidget(line);
				lay->addWidget(spin);

				w->setLayout(lay);

				emitter = slider;
				widget = w;
			} else
			// default behavoir rendered as TextEdit
			{
				QLineEdit * edit = new QLineEdit(proxy->getPropertyValue(i).c_str());

				connect(edit, SIGNAL(editingFinished()), signalMapper, SLOT (map()));

				widget = edit;
			}

			if (!emitter)
				emitter = widget;

			emitter->setObjectName(fullname);
			emitter->setToolTip(pttip);
			emitter->setStatusTip(pttip);

			int pos = layout->rowCount();

			layout->addWidget(new QLabel(pname), pos, 0);
			layout->addWidget(widget, pos, 1);

			signalMapper->setMapping(emitter, emitter);
		}

		connect(signalMapper, SIGNAL(mapped(QWidget*)), this, SLOT(setProperty(QWidget*)));

		foreach (QString str, boxes.keys()) {
			if (str == "Ungrouped")
				continue;

			boxes[str]->setLayout(layouts[str]);
		}

		foreach (QString str, layouts.keys()) {
			if (str == "Ungrouped")
				continue;

			int pos = layouts["Ungrouped"]->rowCount();
			layouts["Ungrouped"]->addWidget(boxes[str], pos, 0, 1, 2);
		}

		boxes["Ungrouped"]->setLayout(layouts["Ungrouped"]);
		top_layout->addWidget(group_box);
	}

	int hc = proxy->countHandlers();

	if (hc > 0) {
		QSignalMapper * signalMapper = new QSignalMapper(this);
		QGroupBox * group_box = new QGroupBox(QString(proxy->name().c_str()) + " event handlers");
		QGridLayout * layout = new QGridLayout;

		for (int i = 0; i < hc; ++i) {
			QPushButton * button = new QPushButton(proxy->getHandler(i).c_str());
			layout->addWidget(button);
			signalMapper->setMapping(button, QString(proxy->getHandler(i).c_str()));
			connect(button, SIGNAL(clicked()), signalMapper, SLOT (map()));
		}

		connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(triggerHandler(QString)));

		group_box->setLayout(layout);
		top_layout->addWidget(group_box);
	}

	top_layout->addStretch();
	this->setLayout(top_layout);
}

ComponentWidget::~ComponentWidget()
{
}

void ComponentWidget::triggerHandler(const QString & name) {
	m_proxy->triggerHandler(name.toStdString());
}

void ComponentWidget::setProperty(QWidget * widget) {
	QString val;
	if (widget->inherits("QCheckBox")) {
		QCheckBox * check = qobject_cast<QCheckBox*>(widget);
		val = check->isChecked() ? "1" : "0";
	} else
	if (widget->inherits("QLineEdit")) {
		QLineEdit * edit = qobject_cast<QLineEdit*>(widget);
		val = edit->text();
	} else if (widget->inherits("QSpinBox")) {
		QSpinBox * spin = qobject_cast<QSpinBox*>(widget);
		val = spin->text();
	} else if (widget->inherits("QComboBox")) {
		QComboBox * combo = qobject_cast<QComboBox*>(widget);
		val = combo->itemText ( combo->currentIndex() );
	} else if (widget->inherits("QSlider")) {
		QSlider * slider = qobject_cast<QSlider*>(widget);
		val = boost::lexical_cast<std::string>(slider->value()).c_str();
	} else {
	}

	m_proxy->setPropertyValue(widget->objectName().toStdString(), val.toStdString());
	//std::cout << name.toStdString() << std::endl;
}

void ComponentWidget::setBump(int bump) {
	m_proxy->setBump(bump);
}

max_stars_repo_name: mlaszko/DisCODe

max_issues_repo_name: mlaszko/DisCODe

max_forks_repo_name: mlaszko/DisCODe


text:
// Boost.Geometry (aka GGL, Generic Geometry Library)
// Unit Test

// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
// Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
// Use, modification and distribution is subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef GEOMETRY_TEST_TEST_COMMON_TEST_POINT_HPP
#define GEOMETRY_TEST_TEST_COMMON_TEST_POINT_HPP

#include <boost/geometry/core/access.hpp>
#include <boost/geometry/core/coordinate_type.hpp>
#include <boost/geometry/core/coordinate_system.hpp>
#include <boost/geometry/core/coordinate_dimension.hpp>
#include <boost/geometry/core/cs.hpp>
#include <boost/geometry/core/tag.hpp>

// NOTE: since Boost 1.51 the Point type may always be a pointer.
// Therefore the traits class don't need to add a pointer.
// This obsoletes this whole test-point-type

namespace test
{

// Test point class

struct test_point
{
    float c1, c2, c3;
};

} // namespace test

namespace boost { namespace geometry { namespace traits {

template<>
struct tag<test::test_point> { typedef point_tag type; };

template<>
struct coordinate_type<test::test_point> { typedef float type; };

template<>
struct coordinate_system<test::test_point> { typedef cs::cartesian type; };

template<>
struct dimension<test::test_point>: boost::mpl::int_<3> {};

template<> struct access<test::test_point, 0>
{
    static inline const float& get(const test::test_point& p)
    {
        return p.c1;
    }

    static inline void set(test::test_point& p, const float& value)
    {
        p.c1 = value;
    }
};

template<> struct access<test::test_point, 1>
{
    static inline const float& get(const test::test_point& p)
    {
        return p.c2;
    }

    static inline void set(test::test_point& p, const float& value)
    {
        p.c2 = value;
    }
};

template<> struct access<test::test_point, 2>
{
    static inline const float& get(const test::test_point& p)
    {
        return p.c3;
    }

    static inline void set(test::test_point& p, const float& value)
    {
        p.c3 = value;
    }
};

}}} // namespace bg::traits

#endif // GEOMETRY_TEST_TEST_COMMON_TEST_POINT_HPP


text:
#pragma execution_character_set("utf-8")

#include "Discoverer.h"
#include <regex>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <future>
#include <boost/algorithm/string.hpp>

namespace new_words_discover
{

void Discoverer::process()
{
    start_sentence_parser();
    std::cout << "proccessing file " << filename_ + "...\n";
    if (parse_file() < 0) {
        return;
    }
    std::cout << "done.\n";

    std::cout << "calculating firmness...\n";
    remove_words_by_firmness();
    std::cout << "done.\n";
    remove_words_by_freq_and_word_len();

    std::cout << "calculating degree of freedom...\n";
    remove_words_by_degree_of_freedom();
    std::cout << "done.\n";
    print();
}

void Discoverer::start_sentence_parser()
{
    sentence_parser_ = std::thread([this] {
        while (!file_parse_done_) {
            std::unique_lock<std::mutex> lck(mutex_sentence_);
            cv_sentence_.wait(lck,
                              [this] { return !sentence_list_.empty() || file_parse_done_; });

            if (!sentence_list_.empty()) {
                std::wstring copy = sentence_list_.front();
                sentence_list_.pop();
                lck.unlock();
                parse_sentence(copy);
            }
        }

        while (!sentence_list_.empty()) {
            const std::wstring &sentence = sentence_list_.front();
            parse_sentence(sentence);
            sentence_list_.pop();
        }
    });
}

int Discoverer::parse_file()
{
    std::wifstream corpus(filename_);
    if (!corpus.is_open()) {
        std::cerr << "Failed to open " + filename_ + "!" << std::endl;
        return -1;
    }
    std::wstring paragraph;

#if defined(_MSC_VER)
    std::wregex re(L"\W+|[a-zA-Z0-9]+|\s+|\n+");
#else
    std::wregex re(L"\\W+|[a-zA-Z0-9]+|\\s+|\n+");
#endif
    std::cout << "calculating word frequency...\n";
    while (std::getline(corpus, paragraph)) {
        if (paragraph.empty()) {
            continue;
        }
        std::vector<std::wstring> para_vec;
        boost::algorithm::split(para_vec, paragraph,
                                boost::is_any_of(L"【】，。？《》！、（）……；：“”‘’"));
        for (auto &segment : para_vec) {
            std::wsregex_token_iterator it(segment.begin(), segment.end(), re, -1);
            std::wsregex_token_iterator end_it;
            while (it != end_it) {
                std::wstring &&cur_sentence = it->str();
                ++it;
                if (!cur_sentence.empty()) {
                    boost::trim(cur_sentence);
                    {
                        std::lock_guard<std::mutex> lck(mutex_sentence_);
                        sentence_list_.emplace(cur_sentence);
                    }
                    cv_sentence_.notify_one();
                }
            }
        }
    }
    file_parse_done_ = true;
    // notify to avoid the infinite wait.
    cv_sentence_.notify_one();
    std::cout << "done.\n";
    sentence_parser_.join();
    return 0;
}

void Discoverer::parse_sentence(const std::wstring &sentence)
{
    assert(!sentence.empty());
    for (size_t word_len = 1; word_len <= std::min(thresholds_.max_word_len, sentence.size());
         ++word_len) {
        parse_word(sentence, word_len);
    }
}

void Discoverer::parse_word(const std::wstring &sentence, size_t word_len)
{
    // Loop for the sentence to get all potential words.
    for (size_t j = 0; j + word_len <= sentence.size(); ++j) {
        auto word = sentence.substr(j, word_len);
        if (word.size() > 1) {
            if (j > 0) {
                // get the set of left adjacent Chinese character.
                const wchar_t &left_adja = sentence[j - 1];
                ++std::get<1>(words_[word])[left_adja];
            }
            if (j + word_len < sentence.size()) {
                // get the set of right adjacent Chinese character.
                const wchar_t &right_adja = sentence[j + word_len];
                ++std::get<2>(words_[word])[right_adja];
            }
        }
        ++std::get<frequency_t>(words_[word]);
        ++tot_frequency_;
    }
}

void Discoverer::remove_words_by_firmness()
{
    // Calculate firmness concurrently.
    int total_words = static_cast<int>(words_.size());
    if (total_words == 0) {
        return;
    }
    auto low = words_.begin();
    auto high = words_.begin();
    auto step = total_words >= 4 ? total_words >> 2 : 1;  // Seperate into 4 or 5 parts.
    std::advance(high, step);

    // high_int is to calculate the distance between high and words.end(),
    // avoid using std::distance for bidirectional iterators.
    int high_int = step;
    std::vector<std::future<void>> fu_vec;
    while (total_words - high_int >= 0) {
        // Go!
        fu_vec.push_back(
            std::async(std::launch::async, [this, low, high] { calculate_firmness(low, high); }));
        if (high_int == total_words) {
            break;
        }
        std::advance(low, step);
        if (total_words - high_int >= step) {
            high_int += step;
            std::advance(high, step);
        } else {
            std::advance(high, total_words - high_int);
            high_int += step;
        }
    }
    // Waiting for the future objects.
    for (auto &fu : fu_vec) {
        fu.get();
    }
    for (auto it = words_.begin(); it != words_.end();) {
        if (words_.size() > 1 && std::get<firmness_t>(it->second) < thresholds_.firmness_thr) {
            it = words_.erase(it);
        } else {
            ++it;
        }
    }
}

void Discoverer::calculate_firmness(std::pair<const std::wstring, word_t> &word)
{
    size_t p_word = std::get<frequency_t>(word.second);
    double min_firmness = std::numeric_limits<double>::max();

    for (size_t i = 0; i < word.first.size(); ++i) {
        const auto &left_part = words_[word.first.substr(0, i + 1)];
        const auto &right_part = words_[word.first.substr(i + 1, word.first.size() - i - 1)];
        auto ans = static_cast<double>(p_word) * tot_frequency_ /
                   (std::get<frequency_t>(left_part) * std::get<frequency_t>(right_part));
        if (ans < min_firmness) {
            min_firmness = ans;
        }
    }
    std::get<firmness_t>(word.second) = min_firmness;
}

void Discoverer::calculate_firmness(word_map_iter begin, word_map_iter end)
{
    for (auto it = begin; it != end; ++it) {
        if (it->first.size() > 1) {
            calculate_firmness(*it);
        }
    }
}

double Discoverer::calculate_degree_of_freedom(const std::pair<std::wstring, word_t> &word)
{
    const auto &left_adjas = std::get<1>(word.second);
    const auto &right_adjas = std::get<2>(word.second);
    return std::min(entropy(left_adjas), entropy(right_adjas));
}

double Discoverer::entropy(const std::unordered_map<wchar_t, frequency_t> &adjacents)
{
    size_t tot_freq = 0;
    double ret = 0;
    std::for_each(adjacents.begin(), adjacents.end(),
                  [&tot_freq](auto &val) { tot_freq += val.second; });
    std::for_each(adjacents.begin(), adjacents.end(), [&tot_freq, &ret](auto &val) {
        ret += (-std::log2(static_cast<double>(val.second) / tot_freq) *
                (static_cast<double>(val.second) / tot_freq));
    });
    return ret;
}

void Discoverer::remove_words_by_freq_and_word_len()
{
    for (auto it = words_.begin(); it != words_.end();) {
        if (it->first.size() < 2 || std::get<frequency_t>(it->second) < thresholds_.freq_thr) {
            it = words_.erase(it);
        } else {
            ++it;
        }
    }
}

void Discoverer::remove_words_by_degree_of_freedom()
{
    for (auto it = words_.begin(); it != words_.end();) {
        auto df = calculate_degree_of_freedom(*it);
        if (df < thresholds_.df_thr) {
            it = words_.erase(it);
        } else {
            ++it;
        }
    }
}

void Discoverer::print()
{
    std::vector<std::pair<std::wstring, word_t>> sorted(words_.begin(), words_.end());
    std::sort(sorted.begin(), sorted.end(), [](auto &x, auto &y) {
        return std::get<frequency_t>(x.second) < std::get<frequency_t>(y.second);
    });

    // Generating output filename by input filename, remove suffix if any.
    auto dot = filename_.find_last_of('.');
    std::string out_file;
    out_file = (dot != std::string::npos ? filename_.substr(0, dot) : filename_);
    out_file += "_out.txt";
    std::wofstream output(out_file);
    if (!output.is_open()) {
        std::cerr << "Failed to open " + out_file + "!" << std::endl;
        return;
    }
    output << L"Total words: " << sorted.size() << std::endl;
    for (const auto &s : sorted) {
        output << s.first << L" " << std::get<frequency_t>(s.second) << std::endl;
    }
    std::cout << "The results are stored in " + out_file << std::endl;
}
}  // namespace new_words_discover

max_stars_repo_name: zouyxdut/new-words-discoverer

max_issues_repo_name: zouyxdut/new-words-discoverer

max_forks_repo_name: zouyxdut/new-words-discoverer


text:
#include <glog/logging.h>
#include <gtest/gtest.h>

#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/vector.hpp>
#include <iostream>
#include <tuple>

#include "MemDevice.hpp"

namespace zlcook {

/*trivial class*/
class Person {
 public:
  Person(){};
  Person(int a, bool g, std::string n, float s)
      : age(a), gender(g), name(n), salary(s) {}

  void toString() {
    std::cout << "name: " << name << ", age: " << age << ", gender: " << gender
              << ", salary: " << salary << "\n";
  }

  friend bool operator==(const Person& l, const Person& r) {
    return l.age == r.age && l.gender == r.gender && l.name == r.name &&
           l.salary == r.salary;
  }

  friend bool operator!=(const Person& l, const Person& r) {
    return !(l.age == r.age && l.gender == r.gender && l.name == r.name &&
             l.salary == r.salary);
  }

 private:
  friend class boost::serialization::access;
  template <class Archive>
  void serialize(Archive& ar, const unsigned int version) {
    ar& age;
    ar& gender;
    ar& name;
    ar& salary;
  }

  int age;
  bool gender;
  std::string name;
  float salary;
};

/* No Trivial class */
class Company {
 public:
  Company() {}
  Company(std::string n, int y) : name(n), years(y) {}

  void addPerson(Person p) { persons.push_back(p); }

  void addSalary(int key, float money) { salarys.emplace(key, money); }

  friend bool operator==(const Company& l, const Company& r) {
    if (l.name != r.name) return false;
    if (l.years != r.years) return false;
    if (l.persons.size() != r.persons.size()) return false;

    for (size_t i = 0; i < l.persons.size(); i++) {
      if (l.persons[i] != r.persons[i]) return false;
    }
    if (l.salarys.size() != r.salarys.size()) return false;
    for (const auto& iter : l.salarys) {
      int key = iter.first;
      auto val = iter.second;
      if (r.salarys.find(key) == r.salarys.end()) return false;
      const float& r_val = r.salarys.at(key);
      if (val != r_val) return false;
    }
    return true;
  }

  void toString() {
    std::cout << "name: " << name << ", years: " << years << "\n";
  }

 private:
  friend class boost::serialization::access;

  template <class Archive>
  void serialize(Archive& ar, const unsigned int version) {
    ar& name;
    ar& years;
    ar& persons;
    ar& salarys;
  }

  std::string name;
  int years;
  std::vector<Person> persons;
  std::map<int, float> salarys;
};

TEST(ArchiveTest, Basic) {
  boost::iostreams::stream<MemSink> os(500);
  // serize
  {
    boost::archive::binary_oarchive oa(os);
    for (int i = 0; i < 10; i++) {
      oa& i;
    }
  }

  // deserize
  boost::iostreams::stream<MemSource> is(os->GetBuffer());
  {
    boost::archive::binary_iarchive ia(is);
    int number;
    for (int i = 0; i < 10; i++) {
      ia& number;
      ASSERT_EQ(i, number);
    }
  }
}

TEST(ArchiveTest, Trivial) {
  boost::iostreams::stream<MemSink> os(500);
  // serize
  {
    boost::archive::binary_oarchive oa(os);
    for (int i = 0; i < 10; i++) {
      Person p(i, true, std::to_string(i), i * 100.0);
      oa& p;
    }
  }

  // deserize
  boost::iostreams::stream<MemSource> is((*os).GetBuffer());
  {
    boost::archive::binary_iarchive ia(is);
    for (int i = 0; i < 10; i++) {
      Person ep(i, true, std::to_string(i), i * 100.0);
      Person p;
      ia& p;
      ASSERT_EQ(ep, p);
    }
  }
}

TEST(ArchiveTest, NoTrivial) {
  boost::iostreams::stream<MemSink> os(500);
  // serize
  {
    boost::archive::binary_oarchive oa(os);
    for (int i = 0; i < 10; i++) {
      Company cp(std::to_string(i), i);
      for (int j = 0; j < 3; j++) {
        Person p(i, true, std::to_string(i), i * 100.0);
        cp.addPerson(p);
        cp.addSalary(j, i * 100.0);
      }
      oa& cp;
    }
  }

  // deserize
  boost::iostreams::stream<MemSource> is((*os).GetBuffer());
  {
    boost::archive::binary_iarchive ia(is);
    for (int i = 0; i < 10; i++) {
      Company expect_cp(std::to_string(i), i);
      for (int j = 0; j < 3; j++) {
        Person p(i, true, std::to_string(i), i * 100.0);
        expect_cp.addPerson(p);
        expect_cp.addSalary(j, i * 100.0);
      }
      Company cp;
      ia& cp;
      // cp.toString();
      ASSERT_EQ(expect_cp, cp);
    }
  }
}
}  // namespace zlcook

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

max_stars_repo_name: zlcook/serialize

max_issues_repo_name: zlcook/serialize

max_forks_repo_name: zlcook/serialize


text:
/*

    Image Uploader -  free application for uploading images/files to the Internet

    Copyright 2007-2018 Sergey Svistunov (zenden2k@gmail.com)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/
#include "ImageDownloaderDlg.h"

#include <boost/format.hpp>

#include "Core/CommonDefs.h"
#include "Core/3rdpart/pcreplusplus.h"
#include "Core/Settings/WtlGuiSettings.h"
#include "Gui/GuiTools.h"
#include "Func/WinUtils.h"
#include "Core/ServiceLocator.h"
#include "Func/WebUtils.h"
#include "Func/IuCommonFunctions.h"
#include "Core/Utils/StringUtils.h"
#include "Core/AppParams.h"
#include "Core/Network/NetworkClientFactory.h"
#include "Core/DownloadTask.h"

namespace {

bool ExtractLinks(CString text, std::vector<CString>& result) {
    pcrepp::Pcre reg("((http|https|ftp)://[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*)", "imcu");
    std::string str = WCstringToUtf8(text);
    size_t pos = 0;
    while (pos <= str.length()) {
        if (reg.search(str, pos)) {
            pos = reg.get_match_end() + 1;
            CString temp = Utf8ToWstring(reg[1]).c_str();
            result.push_back(temp);
        } else
            break;
    }
    return true;
}

}

// CImageDownloaderDlg
CImageDownloaderDlg::CImageDownloaderDlg(CWizardDlg *wizardDlg, const CString &initialBuffer) {
    m_WizardDlg = wizardDlg;
    m_InitialBuffer = initialBuffer;
    m_retCode = 0;
    m_nFilesCount = 0;
    m_nFileDownloaded = 0;
    m_nSuccessfullDownloads = 0;
    isVistaOrLater_ = WinUtils::IsVistaOrLater();
    isRunning_ = false;
    ACCEL accels[] = {
        { FVIRTKEY|FCONTROL , VK_RETURN, IDOK },
        { FVIRTKEY , VK_ESCAPE, IDCANCEL },
    };
    accel_.CreateAcceleratorTable(accels, ARRAY_SIZE(accels));
}

LRESULT CImageDownloaderDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    CenterWindow(GetParent());
    auto* settings = ServiceLocator::instance()->settings<WtlGuiSettings>();

    AddClipboardFormatListener(m_hWnd);

    DlgResize_Init(false, true, 0); // resizable dialog without "griper"

    ::SetFocus(GetDlgItem(IDOK));
    SetWindowText(TR("Image Downloader"));
    CString addButtonCaption = TR("Add") + CString(_T(" (Ctrl+Enter)"));
    SetDlgItemText(IDOK, addButtonCaption);
    TRC(IDCANCEL, "Cancel");
    TRC(IDC_WATCHCLIPBOARD, "Watch Clipboard for URLs");
    TRC(IDC_IMAGEDOWNLOADERTIP, "Enter URLs (one http:// or ftp:// link per line)");
    ::ShowWindow(GetDlgItem(IDC_DOWNLOADFILESPROGRESS), SW_HIDE);
    SendDlgItemMessage(IDC_WATCHCLIPBOARD, BM_SETCHECK, settings->WatchClipboard?BST_CHECKED:BST_UNCHECKED);

    if(!m_InitialBuffer.IsEmpty())
    {
        ParseBuffer(m_InitialBuffer, false);
        BeginDownloading();
    } else {
        CString text;
        if (WinUtils::GetClipboardText(text, m_hWnd)) {
            if (WebUtils::DoesTextLookLikeUrl(text)) {
                SetDlgItemText(IDC_FILEINFOEDIT, text);
            }
        }
    }
    ::SetFocus(GetDlgItem(IDC_FILEINFOEDIT));
    return 0;
}

LRESULT CImageDownloaderDlg::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    RemoveClipboardFormatListener(m_hWnd);
    return 0;
}

LRESULT CImageDownloaderDlg::OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    BeginDownloading();
    return 0;
}

LRESULT CImageDownloaderDlg::OnClickedCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    if (isRunning_) {
        downloadTask_->cancel();
    } else
    {
        auto* settings = ServiceLocator::instance()->settings<WtlGuiSettings>();
        settings->WatchClipboard = SendDlgItemMessage(IDC_WATCHCLIPBOARD, BM_GETCHECK) != 0;
        EmulateEndDialog(wID);
    }
    return 0;
}

LRESULT CImageDownloaderDlg::OnClipboardUpdate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    clipboardUpdated();
    return 0;
}

BOOL CImageDownloaderDlg::PreTranslateMessage(MSG* pMsg) {
    if (accel_.TranslateAccelerator(m_hWnd, pMsg)) {
        return TRUE;
    }
    return FALSE;
}

bool CImageDownloaderDlg::OnFileFinished(bool ok, int statusCode, const DownloadTask::DownloadItem& it)
{
    if(ok)
    {
        CWizardDlg::AddImageStruct ais;
        ais.show =true;
        ais.RealFileName = Utf8ToWstring(it.fileName).c_str();
        ais.VirtualFileName =  Utf8ToWstring(it.displayName).c_str();
        if (ais.VirtualFileName.IsEmpty()) {
            ais.VirtualFileName = WinUtils::myExtractFileName(ais.RealFileName);
        }
        bool add = true;
        if(!IuCommonFunctions::IsImage(ais.RealFileName))
        {
            std::string mimeType = IuCoreUtils::GetFileMimeType(W2U(ais.RealFileName));
            if (mimeType.find("image/") != std::string::npos)
            {
                CString ext = U2W(IuCoreUtils::GetDefaultExtensionForMimeType(mimeType));
                if(!ext.IsEmpty())
                {
                    CString newFileName = ais.RealFileName + _T(".") + ext;
                    MoveFile(ais.RealFileName, newFileName);
                    ais.RealFileName = newFileName;
                    if (CString(WinUtils::GetFileExt(ais.VirtualFileName)).MakeLower() != ext) {
                        ais.VirtualFileName += _T(".") + ext;
                    }
                }
            }
            else
            {
                add = false;

                std::wstring url = IuCoreUtils::Utf8ToWstring(it.url);
                std::wstring mimeTypeW = IuCoreUtils::Utf8ToWstring(mimeType);
                std::wstring errorStr = str(boost::wformat(TR("File is not an image.\nUrl: %s\nMime-Type: %s")) % url % mimeTypeW);
                ServiceLocator::instance()->logger()->write(ILogger::logError, _T("Image Downloader"), errorStr.c_str());
            }
        }
        if (add) {
            if (m_WizardDlg) {
                SendMessage(m_WizardDlg->m_hWnd, WM_MY_ADDIMAGE, reinterpret_cast<WPARAM>(&ais), 0);
            } else {
                m_downloadedFiles.push_back(ais.RealFileName);
            }
            m_nSuccessfullDownloads++;

        }

    }
    m_nFileDownloaded++;
    SendDlgItemMessage(IDC_DOWNLOADFILESPROGRESS, PBM_SETPOS,  m_nFileDownloaded);
    return true;
}

void CImageDownloaderDlg::OnQueueFinished()
{
    if(!m_InitialBuffer.IsEmpty())
    {
        isRunning_ = false;
        EmulateEndDialog(0);
        WinUtils::CopyTextToClipboard("");
        return;
    }
    ::EnableWindow(GetDlgItem(IDOK),true);
    ::EnableWindow(GetDlgItem(IDC_FILEINFOEDIT),true);
    TRC(IDCANCEL, "Close");
    SetDlgItemText(IDC_FILEINFOEDIT, _T(""));
    ::ShowWindow(GetDlgItem(IDC_DOWNLOADFILESPROGRESS),SW_HIDE);
    ::EnableWindow(GetDlgItem(IDC_WATCHCLIPBOARD),true);
    isRunning_ = false;
}

bool CImageDownloaderDlg::BeginDownloading()
{
    std::string links = W2U(GuiTools::GetWindowText(GetDlgItem(IDC_FILEINFOEDIT)));
    std::vector<std::string> tokens;
    IuStringUtils::Split(links,"\n",tokens,-1);
    m_nFilesCount =0;
    m_nFileDownloaded = 0;
    std::vector<DownloadTask::DownloadItem> downloadItems;

    for(size_t i=0; i<tokens.size(); i++)
    {
        const std::string& token = tokens[i];
        if (token.empty() || IuStringUtils::Trim(token).empty()) {
            continue;
        }

        DownloadTask::DownloadItem di;
        di.url = IuStringUtils::Trim(token);
        di.id = reinterpret_cast<void*>(i);
        downloadItems.push_back(di);
        m_nFilesCount++;
    }
    if(m_nFilesCount)
    {
        TRC(IDCANCEL, "Cancel");
        ::EnableWindow(GetDlgItem(IDOK),false);
        ::EnableWindow(GetDlgItem(IDC_WATCHCLIPBOARD),false);
        ::EnableWindow(GetDlgItem(IDC_FILEINFOEDIT),false);
        ::ShowWindow(GetDlgItem(IDC_DOWNLOADFILESPROGRESS),SW_SHOW);
        SendDlgItemMessage(IDC_DOWNLOADFILESPROGRESS, PBM_SETRANGE, 0, MAKELPARAM(0, m_nFilesCount));
        SendDlgItemMessage(IDC_DOWNLOADFILESPROGRESS, PBM_SETPOS,  0);
        using namespace std::placeholders;

        auto networkClientFactory = ServiceLocator::instance()->networkClientFactory();
        downloadTask_ = std::make_shared<DownloadTask>(networkClientFactory, AppParams::instance()->tempDirectory(), downloadItems);
        downloadTask_->onFileFinished.connect(std::bind(&CImageDownloaderDlg::OnFileFinished, this, _1, _2, _3));
        downloadTask_->onTaskFinished.connect(std::bind(&CImageDownloaderDlg::OnQueueFinished, this));
        isRunning_ = true;
        ServiceLocator::instance()->taskDispatcher()->postTask(downloadTask_);
        return true;
    }
    return false;
}

bool CImageDownloaderDlg::LinksAvailableInText(const CString &text)
{
    std::vector<CString> links;
    ExtractLinks(text,links);
    return !links.empty();
}

void CImageDownloaderDlg::ParseBuffer(const CString& buffer,bool OnlyImages)
{
    std::vector<CString> links;
    ExtractLinks(buffer,links);
    CString text = GuiTools::GetWindowText(GetDlgItem(IDC_FILEINFOEDIT));
    for(size_t i=0; i<links.size(); i++)
    {
        CString fileName = WinUtils::myExtractFileName(links[i]);
        if( ((!OnlyImages && CString(WinUtils::GetFileExt(fileName)).IsEmpty()) || IuCommonFunctions::IsImage(fileName)) &&  text.Find(links[i]) == -1 ) {
            if (!text.IsEmpty() && text.Right(1) != _T("\n")) {
                text += "\r\n";
            }
            text+=links[i]+_T("\r\n");
        }
    }
    SetDlgItemText(IDC_FILEINFOEDIT, text);
}

void CImageDownloaderDlg::clipboardUpdated()
{
    bool IsClipboard = IsClipboardFormatAvailable(CF_UNICODETEXT) != 0;

    if (IsClipboard && SendDlgItemMessage(IDC_WATCHCLIPBOARD, BM_GETCHECK) == BST_CHECKED && !isRunning_)
    {
        CString str;
        if (WinUtils::GetClipboardText(str, m_hWnd, true))
        {
            ParseBuffer(str, false);
        }
    }
}

const std::vector<CString>& CImageDownloaderDlg::getDownloadedFiles() const {
    return m_downloadedFiles;
}

int CImageDownloaderDlg::EmulateModal(HWND hWndParent, LPARAM dwInitParam)
{
    ATLASSERT(!m_bModal);
    m_retCode = 0;
    ::EnableWindow(hWndParent, FALSE);
    Create(hWndParent, dwInitParam);
    ShowWindow(SW_SHOW);
    m_loop.AddMessageFilter(this);
    m_loop.Run();
    ::EnableWindow(hWndParent, TRUE);
    ShowWindow(SW_HIDE);
    ::SetActiveWindow(hWndParent);
    DestroyWindow();
    return m_retCode;
}

BOOL CImageDownloaderDlg::EmulateEndDialog(int nRetCode) {
    /*if (m_bModal) {
        EndDialog(nRetCode);
    }*/

    m_loop.RemoveMessageFilter(this);
    m_retCode = nRetCode;
    PostMessage(WM_QUIT);

    return TRUE;
}

int CImageDownloaderDlg::successfullDownloadsCount() const {
    return m_nSuccessfullDownloads;
}


text:
/*=============================================================================
Copyright 2018 Pranam Lashkari <plashkari628@gmail.com>

Distributed under the Boost Software License, Version 1.0. (See accompanying
file License.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/

#ifndef BOOST_ASTRONOMY_COORDINATE_FRAME_HPP
#define BOOST_ASTRONOMY_COORDINATE_FRAME_HPP

#include <boost/astronomy/coordinate/coord_sys/alt_az.hpp>
#include <boost/astronomy/coordinate/ref_frame/cirs.hpp>
#include <boost/astronomy/coordinate/coord_sys/galactic.hpp>
#include <boost/astronomy/coordinate/coord_sys/geocentric.hpp>
#include <boost/astronomy/coordinate/coord_sys/heliocentric.hpp>
#include <boost/astronomy/coordinate/ref_frame/icrs.hpp>
#include <boost/astronomy/coordinate/coord_sys/supergalactic.hpp>

#endif // !BOOST_ASTRONOMY_COORDINATE_FRAME_HPP


text: #include <boost/date_time/gregorian/conversion.hpp>


text:
/*
 * Copyright (c) 2021 jmjatlanta and contributors.
 *
 * The MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#pragma once

#include <graphene/protocol/htlc.hpp>
#include <graphene/db/generic_index.hpp>

#include <boost/multi_index/composite_key.hpp>

namespace graphene { namespace chain {
   using namespace protocol;

   /**
    * @brief database object to store HTLCs
    *
    * This object is stored in the database while an HTLC is active. The HTLC will
    * become inactive at expiration or when unlocked via the preimage.
    */
   class htlc_object : public graphene::db::abstract_object<htlc_object> {
      public:
         // uniquely identify this object in the database
         static constexpr uint8_t space_id = protocol_ids;
         static constexpr uint8_t type_id  = htlc_object_type;

         struct transfer_info {
            account_id_type from;
            account_id_type to;
            share_type amount;
            asset_id_type asset_id;
         } transfer;
         struct condition_info {
            struct hash_lock_info {
               htlc_hash preimage_hash;
               uint16_t preimage_size;
            } hash_lock;
            struct time_lock_info {
               fc::time_point_sec expiration;
            } time_lock;
         } conditions;

         fc::optional<memo_data> memo;

      /****
       * Index helper for timelock
       */
      struct timelock_extractor {
         typedef fc::time_point_sec result_type;
         const result_type& operator()(const htlc_object& o)const { return o.conditions.time_lock.expiration; }
      };

      /*****
       * Index helper for from
       */
      struct from_extractor {
         typedef account_id_type result_type;
         const result_type& operator()(const htlc_object& o)const { return o.transfer.from; }
      };

      /*****
       * Index helper for to
       */
      struct to_extractor {
         typedef account_id_type result_type;
         const result_type& operator()(const htlc_object& o)const { return o.transfer.to; }
      };
   };

   struct by_from_id;
   struct by_expiration;
   struct by_to_id;
   typedef multi_index_container<
         htlc_object,
         indexed_by<
            ordered_unique< tag< by_id >, member< object, object_id_type, &object::id > >,

            ordered_unique< tag< by_expiration >,
                  composite_key< htlc_object,
                  htlc_object::timelock_extractor,
                  member< object, object_id_type, &object::id > > >,

            ordered_unique< tag< by_from_id >,
                  composite_key< htlc_object,
                  htlc_object::from_extractor,
                  member< object, object_id_type, &object::id > > >,

            ordered_unique< tag< by_to_id >,
                  composite_key< htlc_object,
                  htlc_object::to_extractor,
                  member< object, object_id_type, &object::id > > >
      >

   > htlc_object_index_type;

   typedef generic_index< htlc_object, htlc_object_index_type > htlc_index;

} } // namespace graphene::chain

MAP_OBJECT_ID_TO_TYPE(graphene::chain::htlc_object)

FC_REFLECT_TYPENAME( graphene::chain::htlc_object::condition_info::hash_lock_info )
FC_REFLECT_TYPENAME( graphene::chain::htlc_object::condition_info::time_lock_info )
FC_REFLECT_TYPENAME( graphene::chain::htlc_object::condition_info )
FC_REFLECT_TYPENAME( graphene::chain::htlc_object )

GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::chain::htlc_object )

max_stars_repo_name: karthikskkk/core

max_issues_repo_name: karthikskkk/core

max_forks_repo_name: karthikskkk/core


text:
///////////////////////////////////////////////////////////////////////////////
// umessage_content_handler_base.cpp
//
// smart++ - Multipurpose C++ Library.
//
// 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)//
//
// 2009, (c) Dmitry Timoshenko.

#include "detail/complex_content_handler_base.hpp"

#include <smart/data/complex_except.hpp>
#include <smart/debug_out.hpp>

#include <xercesc/sax2/Attributes.hpp>

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

#include <stack>
#include <stdexcept>

#if defined (SMART_DEBUG) && !defined (SMART_COMPLEX_PARSER_VERBOSE) && defined (SMART_DEBUG_OUT)
# undef SMART_DEBUG_OUT
# define SMART_DEBUG_OUT(expr)
#endif // !defined NDEBUG && !defined UNI_PARSER_VERBOSE && defined SMART_DEBUG_OUT
//
using std::stack;
using std::runtime_error;
using std::string;
using std::make_pair;

using boost::shared_ptr;

using smart::data::detail::std_to_xml_string;
using smart::data::detail::xml_to_std_string;
using smart::data::detail::xml_string_type;

namespace smart
{

namespace data
{

namespace detail
{

//////////////////////////////////////////////////////////////////////////
// Auxiliary types
//
// Property Descriptor
//
// fixme: It's necessary to put resources (see mch_impl class) into PropertyDescriptors
// Resources should be available through pointers. If pointer is null, it means
// there is no requested resource in the descriptor

//////////////////////////////////////////////////////////////////////////
// Content Handler Implementation

struct mchb_impl
{
  mchb_impl(void): _chunks(false) { /*empty*/ }

  //-----------------------------------------------------------------------------
  const string& top_element(void) const
  {
    return _elements.top();
  }

  //-----------------------------------------------------------------------------
  void top_element(const string& element)
  {
    _elements.push(element);
  }

  //-----------------------------------------------------------------------------
  void pop_element(void)
  {
    _elements.pop();
  }

  //////////////////////////////////////////////////////////////////////////
  // data
  stack<string> _elements;
  //size_t _chunks;
  bool _chunks;
};

} // namespace detail

} // namespace data

} // namespace smart

using smart::data::detail::mchb_impl;

//////////////////////////////////////////////////////////////////////////
// message_content_handler class

smart::data::message_content_handler_base::message_content_handler_base(void):
  _impl(new mchb_impl)
{
  // empty
}

//-----------------------------------------------------------------------------
smart::data::message_content_handler_base::~message_content_handler_base(void)
{
  delete _impl;
}

//-----------------------------------------------------------------------------
const string& smart::data::message_content_handler_base::top_element(void) const
{
  return _impl->_elements.top();
}

//-----------------------------------------------------------------------------
smart::data::message_content_handler_base::tag_attributes_collection_type smart::data::message_content_handler_base::get_attribs(const xercesc::Attributes& attrs) const
{
  tag_attributes_collection_type attrs_res;

  for (unsigned int/*size_t*/ i = 0; i < attrs.getLength(); ++i)
  {
    const string attr_name           = xml_to_std_string(xml_string_type(attrs.getQName(i)));
    const xml_string_type attr_value = xml_string_type(attrs.getValue(i));
    attrs_res.insert(make_pair(attr_name, attr_value));
  }

  return attrs_res;
}

//-----------------------------------------------------------------------------
//void unicomm::message_content_handler::endDocument(void)
//{
//}
//
////-----------------------------------------------------------------------------
//void unicomm::message_content_handler::ignorableWhitespace(const XMLCh* const chars, const unsigned int length)
//{
//}
//
////-----------------------------------------------------------------------------
//void unicomm::message_content_handler::processingInstruction(const XMLCh* const target, const XMLCh* const data)
//{
//}
//
////-----------------------------------------------------------------------------
//void unicomm::message_content_handler::setDocumentLocator(const xercesc::Locator* const locator)
//{
//}
//
////-----------------------------------------------------------------------------
//void unicomm::message_content_handler::startDocument(void)
//{
//}

//-----------------------------------------------------------------------------
void smart::data::message_content_handler_base::error(const XERCES_CPP_NAMESPACE::SAXParseException& exc)
{
  throw parser_error("Unicomm xml parser error: " + xml_to_std_string(xml_string_type(exc.getMessage())));
}

//-----------------------------------------------------------------------------
void smart::data::message_content_handler_base::fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exc)
{
  throw parser_error("Unicomm xml parser error: " + xml_to_std_string(xml_string_type(exc.getMessage())));
}

//-----------------------------------------------------------------------------
void smart::data::message_content_handler_base::warning(const XERCES_CPP_NAMESPACE::SAXParseException& /*exc*/)
{
  //throw runtime_error(xml_to_std_string(xml_string_type(exc.getMessage())));
}

//-----------------------------------------------------------------------------
void smart::data::message_content_handler_base::startElement(const XMLCh* const /*uri*/, const XMLCh* const /*localname*/, const XMLCh* const qname, const xercesc::Attributes& attrs)
{
  // fixme: check tags invariant until xml-schema is not thought out

  const xml_string_type tmp = qname;
  const string tag          = xml_to_std_string(tmp);
  const tag_attributes_collection_type attribs = get_attribs(attrs);

  _impl->top_element(tag);
  _impl->_chunks = false;

  if (is_message_tag(tag))
  {
    // <message>
    SMART_DEBUG_OUT("[xml parser]: <message> started");

    on_message_start(tag, attribs);
  }
  else if (is_property_tag(tag))
  {
    // <property>
    SMART_DEBUG_OUT("[xml parser]: <property> started");

    on_property_start(tag, attribs);
  }
  else if (is_array_tag(tag))
  {
    // array <items>
    SMART_DEBUG_OUT("[xml parser]: <items> array started");

    on_array_start(tag, attribs);
  }
  else if (is_array_item_tag(tag))
  {
    // array <item>
    SMART_DEBUG_OUT("[xml parser]: <item> started");

    on_array_item_start(tag, attribs);
  }
}

//-----------------------------------------------------------------------------
void smart::data::message_content_handler_base::characters(const XMLCh* const chars, const unsigned int length)
{
  const string& tag       = top_element();
  const xml_string_type s = xml_string_type(chars, length);

  if (is_message_tag(tag))
  {
    // message
    SMART_DEBUG_OUT("[xml parser]: message characters");

    on_message_characters(s, _impl->_chunks);
  }
  else if (is_property_tag(tag))
  {
    // property
    SMART_DEBUG_OUT("[xml parser]: property characters");

    on_property_characters(s, _impl->_chunks);
  }
  else if (is_array_tag(tag))
  {
    // array
    SMART_DEBUG_OUT("[xml parser]: items characters");

    on_array_characters(s, _impl->_chunks);
  }
  else if (is_array_item_tag(tag))
  {
    // array item
    SMART_DEBUG_OUT("[xml parser]: item characters");

    on_array_item_characters(s, _impl->_chunks);
  }

  _impl->_chunks = true;
}

//-----------------------------------------------------------------------------
void smart::data::message_content_handler_base::endElement(const XMLCh* const /*uri*/, const XMLCh* const /*localname*/, const XMLCh* const qname)
{
  _impl->_chunks = false;

  const string& tag = top_element();

  BOOST_ASSERT(xml_to_std_string(xml_string_type(qname)) == tag && "Start and End tags should be identical");

  if (is_message_tag(tag))
  {
    // <message>
    SMART_DEBUG_OUT("[xml parser]: </message> finished");

    on_message_end(tag);
  }
  else if (/*uni_message::property_tag() == topelement()*/is_property_tag(tag))
  {
    // <property>
    SMART_DEBUG_OUT("[xml parser]: </property> finished");

    on_property_end(tag);
  }
  else if (/*uni_message::array_tag() == topelement()*/is_array_tag(tag))
  {
    // array
    SMART_DEBUG_OUT("[xml parser]: </items> array finished");

    on_array_end(tag);
  }
  else if (/*uni_message::array_item_tag() == topelement()*/is_array_item_tag(tag))
  {
    // array item
    SMART_DEBUG_OUT("[xml parser]: </item> finished");

    on_array_item_end(tag);
  }

  _impl->pop_element();
}

////-----------------------------------------------------------------------------
//void smart::data::message_content_handler::MessageStarted(void)
//{
//}
//
////-----------------------------------------------------------------------------
//void smart::data::message_content_handler::PropertyStarted(void)
//{
//}
//
////-----------------------------------------------------------------------------
//void smart::data::message_content_handler::ItemStarted(void)
//{
//
//}
//
////-----------------------------------------------------------------------------
//void smart::data::message_content_handler::MessageValue(void)
//{
//
//}
//
////-----------------------------------------------------------------------------
//void smart::data::message_content_handler::PropertyValue(void)
//{
//}
//
////-----------------------------------------------------------------------------
//void smart::data::message_content_handler::ItemValue(void)
//{
//}
//
////-----------------------------------------------------------------------------
//void smart::data::message_content_handler::MessageFinished(void)
//{
//}
//
////-----------------------------------------------------------------------------
//void smart::data::message_content_handler::PropertyFinished(void)
//{
//}
//
////-----------------------------------------------------------------------------
//void smart::data::message_content_handler::ItemFinished(void)
//{
//}

max_stars_repo_name: Chrizzly/libunicomm

max_issues_repo_name: Chrizzly/libunicomm

max_forks_repo_name: Chrizzly/libunicomm


text:
#ifndef HYPERTRIE_HYPERTRIE_TRAIT_HPP
#define HYPERTRIE_HYPERTRIE_TRAIT_HPP

#include <optional>
#include <vector>
#include <string>

#include <fmt/format.h>
#include <boost/type_index.hpp>

#include "Dice/hypertrie/internal/container/AllContainer.hpp"
#include "Dice/hypertrie/internal/util/Key.hpp"

namespace hypertrie {

	template<typename key_part_type_t = unsigned long,
			 typename value_type_t = bool,
			 template<typename, typename> class map_type_t = hypertrie::internal::container::tsl_sparse_map,
			 template<typename> class set_type_t = hypertrie::internal::container::tsl_sparse_set,
			 bool lsb_unused_v = false>
	struct Hypertrie_t {
		using key_part_type = key_part_type_t;
		using value_type = value_type_t;
		template<typename key, typename value>
		using map_type = map_type_t<key, value>;
		template<typename key>
		using set_type = set_type_t<key>;

		using SliceKey = ::hypertrie::SliceKey<key_part_type>;
		using Key = ::hypertrie::Key<key_part_type>;

		static constexpr const bool is_bool_valued = std::is_same_v<value_type, bool>;
		static constexpr const bool lsb_unused = lsb_unused_v;

		using IteratorEntry = std::conditional_t<(is_bool_valued), Key, std::pair<Key, value_type>>;

		struct iterator_entry{
			static Key &key(IteratorEntry &entry) noexcept {
				if constexpr (is_bool_valued) return entry;
				else
					return entry.first;
			}

			static auto value(IteratorEntry &entry) noexcept {
				if constexpr (is_bool_valued)
					return true;
				else
					return entry.second;
			}
		};

		using KeyPositions = std::vector<uint8_t>;

	private:
		template<typename T>
		static std::string nameOfType() {
			std::string string = boost::typeindex::type_id<T>().pretty_name();
			auto pos = string.find('<');
			return string.substr(0,pos);
		}

	public:
		static auto to_string() {
			return std::string{fmt::format(
					"< key_part = {}, value = {}, map = {}, set = {}, lsb_unused = {} >",
					nameOfType<key_part_type>(),
					nameOfType<value_type>(),
					nameOfType<map_type<key_part_type, value_type>>(),
					nameOfType<set_type<key_part_type>>(),
					lsb_unused)};
		}
	};

	namespace internal::hypertrie_trait {
		template<typename T, template<typename,
									  typename,
									  template<typename, typename> class,
									  template<typename> class,
									  bool>
							 typename U>
		struct is_instance_impl : public std::false_type {
		};

		template<template<typename,
						  typename,
						  template<typename, typename> class,
						  template<typename> class,
						  bool>
				 typename U,
				 typename key_part_type_t,
				 typename value_type_t,
				 template<typename, typename> class map_type_t,
				 template<typename> class set_type_t,
				bool lsb_unused_v>
		struct is_instance_impl<U<key_part_type_t, value_type_t, map_type_t, set_type_t, lsb_unused_v>, U> : public std::true_type {
		};

		template<typename T, template<typename,
									  typename,
									  template<typename, typename> class,
									  template<typename> class,
									  bool>
							 typename U>
		using is_instance = is_instance_impl<std::decay_t<T>, U>;
	}// namespace internal::hypertrie_trait

	template<class T>
	concept HypertrieTrait = internal::hypertrie_trait::is_instance<T, Hypertrie_t>::value;

	using default_bool_Hypertrie_t = Hypertrie_t<unsigned long,
												 bool,
												 hypertrie::internal::container::tsl_sparse_map ,
												 hypertrie::internal::container::tsl_sparse_set >;
	using lsbunused_bool_Hypertrie_t = Hypertrie_t<unsigned long,
												   bool,
												   hypertrie::internal::container::tsl_sparse_map,
												   hypertrie::internal::container::tsl_sparse_set,
												   true>;

	using default_long_Hypertrie_t = Hypertrie_t<unsigned long,
												 long,
												 hypertrie::internal::container::tsl_sparse_map,
												 hypertrie::internal::container::tsl_sparse_set>;

	using default_double_Hypertrie_t = Hypertrie_t<unsigned long,
												   double,
												   hypertrie::internal::container::tsl_sparse_map,
												   hypertrie::internal::container::tsl_sparse_set>;

};// namespace hypertrie

#endif//HYPERTRIE_HYPERTRIE_TRAIT_HPP

max_stars_repo_name: lennartaustenfeld/hypertrie

max_issues_repo_name: lennartaustenfeld/hypertrie

max_forks_repo_name: lennartaustenfeld/hypertrie


text:
#include "muxsvc.h"
#include <vector>
#include <cstring>
#include <boost/assert.hpp>

namespace muxsvc {

DataLenPair muxsvc(DataLenPair lower_layer, DataLenPair higher_layer, int32_t d_id_higher)
{
    BOOST_ASSERT(d_id_higher >= 1);

    // Use a vector to store the muxed data
    std::vector<char> muxed_data;

    // Reserve the size for the muxed data
    muxed_data.reserve(lower_layer.get<1>() + higher_layer.get<1>());

    const char* data_lower = lower_layer.get<0>().get();
    const char* data_higher = higher_layer.get<0>().get();

    // Position in the lower & higher layer data
    int32_t pos_lower  = 0;
    int32_t pos_higher = 0;

    StartSizePair next_higher_nal_pos(-1, -1);

    bool WORKAROUND_insert_double = false;

    // Loop through the lower layer's NAL unit
    while (true) {

        // Position of the next NAL unit in the stream
        StartSizePair next_nal_pos = next_nal(lower_layer, pos_lower);

        // We reached end of stream
        if (next_nal_pos.first == -1) {

            // We copy the rest of the higher data ..
            muxed_data.insert(muxed_data.end(),
                              &data_higher[pos_higher],
                              &data_higher[higher_layer.get<1>()]);

            // .. and exit
            break;
        }

        BOOST_ASSERT(next_nal_pos.second >= 5);

        // Find out what kind of NAL unit it is.
        nal_hdr hdr = parse_nal(lower_layer, next_nal_pos.first).first;

//        std::cout << "Type: " << (uint32_t)hdr.nal_unit_type << std::endl;

        bool insert_higher_nal = false;

        /*
         * If we are dealing with the dependency layer 1 we look for additional
         * NAL types.
         */
        if (d_id_higher == 1) {

            // After NAL types 1 and 5 we insert the higher layer NAL unit
            if ((uint32_t)hdr.nal_unit_type == NAL_T_CODED_SLICE_OF_AN_IDR_PICTURE ||
                (uint32_t)hdr.nal_unit_type == NAL_T_CODED_SLICE_OF_A_NON_IDR_PICTURE) {

                insert_higher_nal = true;

                // Check if we already got the next higher NAL unit
                if (next_higher_nal_pos.first == -1) {

                    next_higher_nal_pos = next_nal(higher_layer, pos_higher);
                    BOOST_ASSERT(next_higher_nal_pos.second != -1);

                    pos_higher += next_higher_nal_pos.second;
                }
            }
        } else {

            // After NAL type 20 we have to check if the NAL unit of type 20
            // belongs to the correct dependency layer.
            if ((uint32_t)hdr.nal_unit_type == NAL_T_SLICE_LAYER_EXTENSION_RBSP) {

                BOOST_ASSERT(next_nal_pos.second >= 9);

                // that means we have to parse the extension header to.
                nal_hdr_svc hdr_svc = parse_svc_nal(lower_layer, next_nal_pos.first).second;

//                std::cout << "SVC NAL Unit Lower: Dependency_id: " << (uint32_t)hdr_svc.dependency_id << std::endl;

                // Now we have to check if the temporal_id of the higher layer matches with the
                // lower one and we have to check of the dependency ids are in order of we
                // insert the higher one here.

                // Check if we already got the next higher NAL unit
                if (next_higher_nal_pos.first == -1) {

                    next_higher_nal_pos = next_nal(higher_layer, pos_higher);
                    BOOST_ASSERT(next_higher_nal_pos.second != -1);

                    pos_higher += next_higher_nal_pos.second;
                }

                // NAL Units of the higher layer have to consist of only NAL units
                // with the extension header.
                BOOST_ASSERT(next_higher_nal_pos.second >= 9);

                nal_hdr_svc hdr_svc_higher
                                = parse_svc_nal(higher_layer,
                                                next_higher_nal_pos.first
                                                    ).second;

//                std::cout << "SVC NAL Unit higher: Dependency_id: " << (uint32_t)hdr_svc_higher.dependency_id << std::endl;

                // We only insert the higher layer here if it matches
//TODO find out if this always works with the quality_ids that way
                if (((uint32_t)hdr_svc.dependency_id + 1) == (uint32_t)hdr_svc_higher.dependency_id &&
                    (uint32_t)hdr_svc.temporal_id == (uint32_t)hdr_svc_higher.temporal_id) {

                    insert_higher_nal = true;

                    WORKAROUND_insert_double = equals_next_nal_unit(higher_layer, next_higher_nal_pos.first + next_higher_nal_pos.second, hdr_svc_higher);
                }
            }
        }

        // First we insert the NAL from the lower layer
        muxed_data.insert(muxed_data.end(),
                          &data_lower[next_nal_pos.first],
                          &data_lower[next_nal_pos.first + next_nal_pos.second]);

        pos_lower += next_nal_pos.second;

        /*
         * We insert a NAL unit from the higher layer here.
         */
        if (insert_higher_nal) {

//            std::cout << "INSERTING HIGHER LAYER NAL, starting from " << pos_higher << " of " << higher_layer.second << std::endl;

            // This must not happen
            BOOST_ASSERT(next_higher_nal_pos.second != -1);

            // Insert the data
            muxed_data.insert(muxed_data.end(),
                            &data_higher[next_higher_nal_pos.first],
                            &data_higher[next_higher_nal_pos.first + next_higher_nal_pos.second]);

            next_higher_nal_pos = StartSizePair(-1, -1);

            //TODO WORKAROUND
            // Check if it was a splitted SVC NAL unit
            // i.e. if the next NAL unit is the same, we insert it to
            if (WORKAROUND_insert_double) {

                next_higher_nal_pos = next_nal(higher_layer, pos_higher);
                BOOST_ASSERT(next_higher_nal_pos.second != -1);

                pos_higher += next_higher_nal_pos.second;

                // Insert the data
                muxed_data.insert(muxed_data.end(),
                                &data_higher[next_higher_nal_pos.first],
                                &data_higher[next_higher_nal_pos.first + next_higher_nal_pos.second]);

                next_higher_nal_pos = StartSizePair(-1, -1);
            }
        }
    }

    // The size has to be correct
    BOOST_ASSERT(muxed_data.size() == (lower_layer.get<1>() + higher_layer.get<1>()));

    // Copy the data out of the vector and return it
    char* tmp = new char[muxed_data.size()];

    memcpy(tmp, &muxed_data[0], muxed_data.size());

    return DataLenPair(boost::shared_array<const char>(tmp), muxed_data.size());
}

DataLenPair muxsvc_vec(std::vector<DataLenPair> svc_vec)
{
    if (svc_vec.empty())
        return DataLenPair();

    if (svc_vec.size() == 1)
        return svc_vec[0];

    DataLenPair muxed_nal
        = muxsvc(svc_vec[0], svc_vec[1], 1);

    for (unsigned int i = 2; i < svc_vec.size(); i++) {

        muxed_nal = muxsvc(muxed_nal, svc_vec[i], i);
    }

    return muxed_nal;
}

NalHeaders parse_nal(DataLenPair search_data, int32_t start)
{
    nal_hdr hdr;
    memcpy(&hdr, &search_data.get<0>().get()[start + sizeof(nal_sep)], sizeof(nal_hdr));

    return NalHeaders(hdr, nal_hdr_svc());
}

NalHeaders parse_svc_nal(DataLenPair search_data, int32_t start)
{
    NalHeaders hdrs = parse_nal(search_data, start);

    BOOST_ASSERT((uint32_t)hdrs.first.nal_unit_type == NAL_T_SLICE_LAYER_EXTENSION_RBSP);

    const int32_t base = start + sizeof(nal_sep);

    char tmp[3];
    tmp[0] = search_data.get<0>().get()[base + 3];
    tmp[1] = search_data.get<0>().get()[base + 2];
    tmp[2] = search_data.get<0>().get()[base + 1];
    tmp[3] = '\0';

    nal_hdr_svc hdr_svc;

    memcpy(&hdr_svc, tmp, 4);

    hdrs.second = hdr_svc;

    return hdrs;
}

bool equals_next_nal_unit(DataLenPair search_data, int32_t start, nal_hdr_svc to_compare)
{
    StartSizePair next_nal_pair = next_nal(search_data, start);

    if (next_nal_pair.second == -1)
        return false;

    nal_hdr_svc hdr_svc = parse_svc_nal(search_data,start).second;

    if ((uint32_t)hdr_svc.dependency_id == (uint32_t)to_compare.dependency_id &&
        (uint32_t)hdr_svc.temporal_id == (uint32_t)to_compare.temporal_id &&
        (uint32_t)hdr_svc.quality_id == (uint32_t)to_compare.quality_id)
        return true;
    else
        return false;
}

StartSizePair next_nal(DataLenPair search_data, int32_t curr_pos)
{
    const char* data         = search_data.get<0>().get();
    const int32_t& data_len = search_data.get<1>();

    int32_t beginning = -1;

    while (curr_pos < data_len) {

        // Are we at the beginning of a NAL unit?
        if (memcmp(&data[curr_pos], &nal_sep[0], sizeof(nal_sep)) == 0) {

            // Are we looking for the beginning?
            if (beginning == -1) {

                beginning = curr_pos;
                curr_pos++;

            } else {

                return StartSizePair(beginning, curr_pos - beginning);
            }

        // If not forward the stream
        } else {

            curr_pos++;
        }
    }

    // That means there are no NAL units left in this stream
    if (beginning == -1) {

        return StartSizePair(-1, -1);

    // This means we reached the end of the stream
    } else {

        return StartSizePair(beginning, data_len - beginning);
    }
}

} // namespace muxsvc

max_stars_repo_name: csieber/svcDASHmuxer

max_issues_repo_name: csieber/svcDASHmuxer

max_forks_repo_name: csieber/svcDASHmuxer


text:
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2006-2007. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/interprocess/detail/config_begin.hpp>
//[doc_message_queueA
#include <boost/interprocess/ipc/message_queue.hpp>
#include <iostream>
#include <vector>

using namespace boost::interprocess;

int main ()
{
   try{
      //Erase previous message queue
      message_queue::remove("message_queue");

      //Create a message_queue.
      message_queue mq
         (create_only               //only create
         ,"message_queue"           //name
         ,100                       //max message number
         ,sizeof(int)               //max message size
         );

      //Send 100 numbers
      for(int i = 0; i < 100; ++i){
         mq.send(&i, sizeof(i), 0);
      }
   }
   catch(interprocess_exception &ex){
      std::cout << ex.what() << std::endl;
      return 1;
   }

   return 0;
}
//]
#include <boost/interprocess/detail/config_end.hpp>