text:
// Copyright (c) 2015-2019 Daniel Cooke
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.

#include "variant_call_filter.hpp"

#include <utility>
#include <unordered_map>
#include <map>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <limits>
#include <cmath>
#include <thread>

#include <boost/range/combine.hpp>
#include <boost/multiprecision/gmp.hpp>

#include "config/common.hpp"
#include "containers/mappable_flat_set.hpp"
#include "containers/mappable_map.hpp"
#include "core/types/variant.hpp"
#include "core/models/haplotype_likelihood_array.hpp"
#include "basics/aligned_read.hpp"
#include "utils/read_stats.hpp"
#include "utils/maths.hpp"
#include "utils/string_utils.hpp"
#include "utils/genotype_reader.hpp"
#include "utils/append.hpp"
#include "utils/parallel_transform.hpp"
#include "io/variant/vcf_writer.hpp"
#include "io/variant/vcf_spec.hpp"

namespace octopus { namespace csr {

namespace {

unsigned get_pool_size(VariantCallFilter::ConcurrencyPolicy policy)
{
    const auto num_cores = std::thread::hardware_concurrency();
    if (policy.max_threads) {
        if (*policy.max_threads > 1) {
            return num_cores > 0 ? std::min(*policy.max_threads, num_cores) : *policy.max_threads;
        } else {
            return 0;
        }
    } else {
        return num_cores > 0 ? num_cores : 8;
    }
}

bool contains(const std::vector<MeasureWrapper>& measures, const std::string& name)
{
    return std::find_if(std::cbegin(measures), std::cend(measures),
                        [&] (const auto& measure) { return measure.name() == name; }) != std::cend(measures);
}

} // namespace

// public methods

VariantCallFilter::VariantCallFilter(FacetFactory facet_factory,
                                     std::vector<MeasureWrapper> measures,
                                     OutputOptions output_config,
                                     ConcurrencyPolicy threading)
: measures_ {std::move(measures)}
, debug_log_ {logging::get_debug_log()}
, facet_factory_ {std::move(facet_factory)}
, facet_names_ {get_all_requirements(measures_)}
, output_config_ {output_config}
, duplicate_measures_ {}
, workers_ {get_pool_size(threading)}
{
    std::unordered_map<MeasureWrapper, int> measure_counts {};
    measure_counts.reserve(measures_.size());
    for (const auto& m : measures_) {
        ++measure_counts[m];
        if (measure_counts[m] == 2) {
            duplicate_measures_.push_back(m);
        }
    }
    duplicate_measures_.shrink_to_fit();
    logging::WarningLogger warn_log {};
    for (const auto& annotation : output_config_.annotations) {
        if (!contains(measures_, annotation)) {
            stream(warn_log) << "The measure " << annotation << " is not active and will not be reported";
        }
    }
}

inline VcfHeader read_header(const VcfWriter& writer)
{
    assert(writer.is_header_written());
    assert(writer.path());
    VcfReader tmp {*writer.path()};
    return tmp.fetch_header();
}

std::string VariantCallFilter::name() const
{
    return do_name();
}

void VariantCallFilter::filter(const VcfReader& source, VcfWriter& dest) const
{
    if (dest.is_header_written()) {
        const auto header = read_header(dest);
        filter(source, dest, header);
    } else {
        const auto header = make_header(source);
        dest << header;
        filter(source, dest, header);
    }
}

// protected methods

namespace {
template <typename Range, typename BinaryPredicate>
bool all_equal(const Range& values, BinaryPredicate pred)
{
    const auto not_pred = [&](const auto& lhs, const auto& rhs) { return !pred(lhs, rhs); };
    return std::adjacent_find(std::cbegin(values), std::cend(values), not_pred) == std::cend(values);
}
} // namespace

VariantCallFilter::Classification
VariantCallFilter::merge(const ClassificationList& sample_classifications, const MeasureVector& measures) const
{
    assert(!sample_classifications.empty());
    if (sample_classifications.size() == 1) {
        return sample_classifications.front();
    }
    Classification result {};
    result.quality = compute_joint_quality(sample_classifications, measures);
    if (!result.quality && all_equal(sample_classifications, [] (const auto& lhs, const auto& rhs) { return lhs.category == rhs.category; })) {
        result.category = sample_classifications.front().category;
    } else if (is_soft_filtered(sample_classifications, result.quality, measures, result.reasons)) {
        result.category = Classification::Category::soft_filtered;
    } else {
        result.category = Classification::Category::unfiltered;
    }
    if (result.category != Classification::Category::unfiltered && result.reasons.empty()) {
        result.reasons = compute_reason_union(sample_classifications);
    }
    return result;
}

VariantCallFilter::Classification VariantCallFilter::merge(const ClassificationList& sample_classifications) const
{
    return this->merge(sample_classifications, {});
}

bool VariantCallFilter::can_measure_single_call() const noexcept
{
    return facet_names_.empty();
}

bool VariantCallFilter::can_measure_multiple_blocks() const noexcept
{
    return is_multithreaded();
}

namespace {

GenomicRegion get_phase_set(const VcfRecord& record, const SampleName& sample)
{
    auto result = get_phase_region(record, sample);
    return result ? *result : mapped_region(record);
}

std::vector<GenomicRegion> get_phase_sets(const VcfRecord& record, const std::vector<SampleName>& samples)
{
    std::vector<GenomicRegion> result{};
    result.reserve(samples.size());
    std::transform(std::cbegin(samples), std::cend(samples), std::back_inserter(result),
                   [&record](const auto& sample) { return get_phase_set(record, sample); });
    return result;
}

GenomicRegion get_phase_region(const VcfRecord& record, const std::vector<SampleName>& samples)
{
    return encompassing_region(get_phase_sets(record, samples));
}

template<typename T, typename _>
std::vector<T> copy_each_first(const std::vector<std::pair<T, _>>& items)
{
    std::vector<T> result {};
    result.reserve(items.size());
    std::transform(std::cbegin(items), std::cend(items), std::back_inserter(result),
                   [] (const auto& p) { return p.first; });
    return result;
}

template <typename Range>
bool can_add_to_phase_block(const VcfRecord& call, const GenomicRegion& call_phase_region, const Range& block)
{
    if (block.empty()) return true;
    if (overlaps(block.back().second, call_phase_region)) return true;
    return is_refcall(call) && is_same_contig(call, block.back().first);
}

} // namespace

VariantCallFilter::CallBlock
VariantCallFilter::read_next_block(VcfIterator& first, const VcfIterator& last, const SampleList& samples) const
{
    std::vector<std::pair<VcfRecord, GenomicRegion>> block {};
    for (; first != last; ++first) {
        const VcfRecord& call {*first};
        auto call_phase_region = get_phase_region(call, samples);
        if (!can_add_to_phase_block(call, call_phase_region, block)) {
            return copy_each_first(block);
        }
        block.emplace_back(call, std::move(call_phase_region));
    }
    return copy_each_first(block);
}

std::vector<VariantCallFilter::CallBlock>
VariantCallFilter::read_next_blocks(VcfIterator& first, const VcfIterator& last, const SampleList& samples) const
{
    std::vector<VariantCallFilter::CallBlock> result {};
    if (can_measure_multiple_blocks()) {
        const auto max_blocks = max_concurrent_blocks();
        result.reserve(max_blocks);
        while (result.size() < max_blocks) {
            result.push_back(read_next_block(first, last, samples));
            if (first == last || !is_same_contig(*first, result.back().front())) break;
        }
    } else {
        result.push_back(read_next_block(first, last, samples));
    }
    return result;
}

VariantCallFilter::MeasureVector VariantCallFilter::measure(const VcfRecord& call) const
{
    MeasureVector result(measures_.size());
    if (duplicate_measures_.empty()) {
        std::transform(std::cbegin(measures_), std::cend(measures_), std::begin(result),
                       [&call] (const MeasureWrapper& m) { return m(call); });
    } else {
        std::unordered_map<MeasureWrapper, Measure::ResultType> result_buffer {};
        result_buffer.reserve(duplicate_measures_.size());
        for (const auto& m : duplicate_measures_) {
            result_buffer.emplace(m, m(call));
        }
        std::transform(std::cbegin(measures_), std::cend(measures_), std::begin(result),
                       [&call, &result_buffer] (const MeasureWrapper& m) -> Measure::ResultType {
                           auto itr = result_buffer.find(m);
                           if (itr != std::cend(result_buffer)) {
                               return itr->second;
                           } else {
                               return m(call);
                           }
                       });
    }
    return result;
}

VariantCallFilter::MeasureBlock VariantCallFilter::measure(const CallBlock& block) const
{
    const auto facets = compute_facets(block);
    auto result = measure(block, facets);
    return result;
}

std::vector<VariantCallFilter::MeasureBlock> VariantCallFilter::measure(const std::vector<CallBlock>& blocks) const
{
    std::vector<MeasureBlock> result {};
    result.reserve(blocks.size());
    if (is_multithreaded()) {
        const auto facets = compute_facets(blocks);
        if (debug_log_) {
            stream(*debug_log_) << "Measuring " << blocks.size() << " blocks with " << workers_.size() << " threads";
        }
        transform(std::cbegin(blocks), std::cend(blocks), std::cbegin(facets), std::back_inserter(result),
                  [this] (const auto& block, const auto& block_facets) {
                      return this->measure(block, block_facets);
                  }, workers_);
    } else {
        for (const CallBlock& block : blocks) {
            result.push_back(measure(block));
        }
    }
    return result;
}

void VariantCallFilter::write(const VcfRecord& call, const Classification& classification, VcfWriter& dest) const
{
    if (!is_hard_filtered(classification)) {
        auto filtered_call = construct_template(call);
        annotate(filtered_call, classification);
        dest << filtered_call.build_once();
    }
}

void VariantCallFilter::write(const VcfRecord& call, const Classification& classification,
                              const SampleList& samples, const ClassificationList& sample_classifications,
                              VcfWriter& dest) const
{
    if (!is_hard_filtered(classification)) {
        auto filtered_call = construct_template(call);
        annotate(filtered_call, classification);
        annotate(filtered_call, samples, sample_classifications);
        dest << filtered_call.build_once();
    }
}

bool VariantCallFilter::measure_annotations_requested() const noexcept
{
    return output_config_.annotate_all_active_measures || !output_config_.annotations.empty();
}

void VariantCallFilter::annotate(VcfRecord::Builder& call, const MeasureVector& measures, const VcfHeader& header) const
{
    if (output_config_.clear_info) {
        call.clear_info();
    }
    for (auto p : boost::combine(measures_, measures)) {
        const MeasureWrapper& measure {p.get<0>()};
        if (is_requested_annotation(measure)) {
            const Measure::ResultType& measured_value {p.get<1>()};
            measure.annotate(call, measured_value, header);
        }
    }
}

// private methods

boost::optional<Phred<double>>
VariantCallFilter::compute_joint_quality(const ClassificationList& sample_classifications, const MeasureVector& measures) const
{
    std::vector<Phred<double>> sample_classification_qualities {};
    sample_classification_qualities.reserve(sample_classifications.size());
    for (const auto& sample_classification : sample_classifications) {
        if (sample_classification.quality) {
            sample_classification_qualities.push_back(*sample_classification.quality);
        }
    }
    if (!sample_classification_qualities.empty()) {
        return compute_joint_quality(sample_classification_qualities);
    }
    return boost::none;
}

bool
VariantCallFilter::is_soft_filtered(const ClassificationList& sample_classifications,
                                    boost::optional<Phred<double>> joint_quality,
                                    const MeasureVector& measures,
                                    std::vector<std::string>& reasons) const
{
    return std::all_of(std::cbegin(sample_classifications), std::cend(sample_classifications),
                       [] (const auto& c) { return c.category != Classification::Category::unfiltered; });
}

Phred<double> ln_probability_true_to_phred(const double ln_prob_true)
{
    assert(ln_prob_true < 0);
    if (ln_prob_true <= -1e-10) {
        return Phred<double> {maths::probability_true_to_phred<double>(std::exp(ln_prob_true))};
    } else {
        using BigFloat = boost::multiprecision::mpf_float_500;
        return octopus::ln_probability_true_to_phred<double>(BigFloat {ln_prob_true});
    }
}

Phred<double> VariantCallFilter::compute_joint_quality(const std::vector<Phred<double>>& qualities) const
{
    assert(!qualities.empty());
    if (qualities.size() == 1) return qualities.front();
    if (std::any_of(std::cbegin(qualities), std::cend(qualities), [] (auto p) { return p.score() <= 0; })) {
        return Phred<double> {0.0};
    }
    std::vector<double> log_probs(qualities.size());
    std::transform(std::cbegin(qualities), std::cend(qualities), std::begin(log_probs),
                   [] (auto p) { return std::log(p.probability_true()); });
    const auto ln_prob_all_good = std::accumulate(std::cbegin(log_probs), std::cend(log_probs), 0.0);
    return ln_probability_true_to_phred(ln_prob_all_good / log_probs.size());
}

std::vector<std::string> VariantCallFilter::compute_reason_union(const ClassificationList& sample_classifications) const
{
    std::vector<std::string> result {};
    for (const auto& sample_classification : sample_classifications) {
        utils::append(sample_classification.reasons, result);
    }
    std::sort(std::begin(result), std::end(result));
    result.erase(std::unique(std::begin(result), std::end(result)), std::end(result));
    return result;
}

VcfHeader VariantCallFilter::make_header(const VcfReader& source) const
{
    VcfHeader::Builder builder {source.fetch_header()};
    if (output_config_.clear_info) {
        builder.clear_info();
    }
    if (measure_annotations_requested()) {
        for (const auto& measure : measures_) {
            if (is_requested_annotation(measure)) {
                measure.annotate(builder);
            }
        }
    }
    if (output_config_.emit_sites_only) {
        builder.clear_format();
    }
    annotate(builder);
    return builder.build_once();
}

VcfRecord::Builder VariantCallFilter::construct_template(const VcfRecord& call) const
{
    VcfRecord::Builder result {call};
    if (output_config_.emit_sites_only) {
        result.clear_format();
    }
    if (output_config_.clear_existing_filters) {
        result.clear_filter();
        result.clear_all_sample_filters();
    }
    return result;
}

bool VariantCallFilter::is_requested_annotation(const MeasureWrapper& measure) const noexcept
{
    return output_config_.annotate_all_active_measures || output_config_.annotations.count(measure.name()) == 1;
}

bool VariantCallFilter::is_hard_filtered(const Classification& classification) const noexcept
{
    return classification.category == Classification::Category::hard_filtered;
}

void VariantCallFilter::annotate(VcfRecord::Builder& call, const SampleList& samples, const ClassificationList& sample_classifications) const
{
    assert(samples.size() == sample_classifications.size());
    bool all_hard_filtered {true};
    auto quality_name = this->genotype_quality_name();
    if (quality_name) {
        call.add_format(std::move(*quality_name));
    }
    for (auto p : boost::combine(samples, sample_classifications)) {
        const SampleName& sample {p.get<0>()};
        const Classification& sample_classification {p.get<1>()};
        if (!is_hard_filtered(sample_classification)) {
            annotate(call, sample, sample_classification);
            all_hard_filtered = false;
        } else {
            call.clear_format(sample);
        }
    }
    if (all_hard_filtered) {
        call.clear_format();
    } else {
        call.add_format(vcfspec::format::filter);
    }
}

void VariantCallFilter::annotate(VcfRecord::Builder& call, const SampleName& sample, Classification status) const
{
    if (status.category == Classification::Category::unfiltered) {
        pass(sample, call);
    } else {
        fail(sample, call, std::move(status.reasons));
    }
    const auto quality_name = this->genotype_quality_name();
    if (quality_name) {
        if (status.quality) {
            call.set_format(sample, *quality_name, utils::to_string(status.quality->score(), 2));
        } else {
            call.set_format_missing(sample, *quality_name);
        }
    }
}

void VariantCallFilter::annotate(VcfRecord::Builder& call, const Classification status) const
{
    if (status.category == Classification::Category::unfiltered) {
        pass(call);
    } else {
        fail(call, std::move(status.reasons));
    }
    auto quality_name = this->call_quality_name();
    if (quality_name) {
        call.add_info(*quality_name);
        if (status.quality) {
            call.set_info(*quality_name, utils::to_string(status.quality->score(), 3, utils::PrecisionRule::sf));
        } else {
            call.set_info_missing(*quality_name);
        }
    }
}

auto make_map(const std::vector<std::string>& names, std::vector<FacetWrapper>&& facets)
{
    assert(names.size() == facets.size());
    Measure::FacetMap result {};
    result.reserve(names.size());
    for (auto tup : boost::combine(names, std::move(facets))) {
        result.emplace(tup.get<0>(), std::move(tup.get<1>()));
    }
    return result;
}

Measure::FacetMap VariantCallFilter::compute_facets(const CallBlock& block) const
{
    return make_map(facet_names_, facet_factory_.make(facet_names_, block));
}

std::vector<Measure::FacetMap> VariantCallFilter::compute_facets(const std::vector<CallBlock>& blocks) const
{
    if (debug_log_ && !blocks.empty()) {
        const auto blocks_region = closed_region(blocks.front().front(), blocks.back().back());
        stream(*debug_log_) << "Computing facets in blocks region " << blocks_region << " containing " << blocks.size() << " blocks";
    }
    auto facets = facet_factory_.make(facet_names_, blocks, workers_);
    std::vector<Measure::FacetMap> result {};
    result.reserve(blocks.size());
    for (auto& block : facets) {
        result.push_back(make_map(facet_names_, std::move(block)));
    }
    return result;
}

VariantCallFilter::MeasureBlock VariantCallFilter::measure(const CallBlock& block, const Measure::FacetMap& facets) const
{
    if (debug_log_ && !block.empty()) {
        stream(*debug_log_) << "Measuring block " << encompassing_region(block) << " containing " << block.size() << " calls";
    }
    MeasureBlock result(block.size());
    std::transform(std::cbegin(block), std::cend(block), std::begin(result),
                   [&] (const VcfRecord& call) { return this->measure(call, facets); });
    return result;
}

VariantCallFilter::MeasureVector VariantCallFilter::measure(const VcfRecord& call, const Measure::FacetMap& facets) const
{
    MeasureVector result(measures_.size());
    if (duplicate_measures_.empty()) {
        std::transform(std::cbegin(measures_), std::cend(measures_), std::begin(result),
                       [&] (const MeasureWrapper& m) { return m(call, facets); });
    } else {
        std::unordered_map<MeasureWrapper, Measure::ResultType> result_buffer {};
        result_buffer.reserve(duplicate_measures_.size());
        for (const auto& m : duplicate_measures_) {
            result_buffer.emplace(m, m(call, facets));
        }
        std::transform(std::cbegin(measures_), std::cend(measures_), std::begin(result),
                       [&] (const MeasureWrapper& m) -> Measure::ResultType {
                           auto itr = result_buffer.find(m);
                           if (itr != std::cend(result_buffer)) {
                               return itr->second;
                           } else {
                               return m(call, facets);
                           }
                       });
    }
    return result;
}

void VariantCallFilter::pass(const SampleName& sample, VcfRecord::Builder& call) const
{
    call.set_passed(sample);
}

void VariantCallFilter::pass(VcfRecord::Builder& call) const
{
    call.set_passed();
}

void VariantCallFilter::fail(const SampleName& sample, VcfRecord::Builder& call, std::vector<std::string> reasons) const
{
    for (auto& reason : reasons) {
        call.add_filter(sample, std::move(reason));
    }
}

void VariantCallFilter::fail(VcfRecord::Builder& call, std::vector<std::string> reasons) const
{
    for (auto& reason : reasons) {
        call.add_filter(std::move(reason));
    }
}

bool VariantCallFilter::is_multithreaded() const noexcept
{
    return !workers_.empty();
}

unsigned VariantCallFilter::max_concurrent_blocks() const noexcept
{
    if (is_multithreaded()) {
        return std::min(100 * workers_.size(), std::size_t {10'000});
    } else {
        return 1;
    }
}

} // namespace csr
} // namespace octopus

max_stars_repo_name: gunjanbaid/octopus

max_issues_repo_name: gunjanbaid/octopus

max_forks_repo_name: gunjanbaid/octopus


text:
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2017 by The University of Queensland //
// Centre for Geoscience Computing                         //
// http://earth.uq.edu.au/centre-geoscience-computing      //
//                                                         //
// Primary Business: Brisbane, Queensland, Australia       //
// Licensed under the Open Software License version 3.0    //
// http://www.apache.org/licenses/LICENSE-2.0              //
//                                                         //
/////////////////////////////////////////////////////////////

#include <boost/version.hpp>
#include <boost/python.hpp>
#include <boost/noncopyable.hpp>
#include "Python/esys/lsm/geometry/GrainCollectionPy.h"
#include "Python/esys/lsm/geometry/SimpleSpherePy.h"

namespace esys
{
  namespace lsm
  {
    GrainCollectionPy::GrainCollectionPy()
      : Inherited()
    {
    }

    GrainCollectionPy::GrainCollectionPy(ParticlePoolPtr particlePoolPtr)
      : Inherited(particlePoolPtr)
    {
    }

    GrainCollectionPy::GrainCollectionPy(
      ParticlePoolPtr particlePoolPtr,
      GrainPoolPtr    grainPoolPtr
    )
      : Inherited(particlePoolPtr, grainPoolPtr)
    {
    }

    GrainPy &GrainCollectionPy::createGrainPy()
    {
      return Inherited::createGrain();
    }

    GrainPy &GrainCollectionPy::createGrainWithIdPy(GrainPy::Id id)
    {
      return Inherited::createGrain(id);
    }

    GrainCollectionPy::GrainIteratorPy GrainCollectionPy::getGrainIteratorPy()
    {
      return GrainIteratorPy(Inherited::getGrainIterator());
    }

    void exportGrainCollection()
    {
      // Disable autogeneration of C++ signatures (Boost 1.34.0 and higher)
      // for Epydoc which stumbles over indentation in the automatically generated strings.
      boost::python::docstring_options no_autogen(true,false);

      boost::python::class_<GrainCollectionPy>(
        "GrainCollection",
        boost::python::init<>()
      )
      .def("getGrainIterator", &GrainCollectionPy::getGrainIteratorPy)
      .def("getNumParticles", &GrainCollectionPy::getNumParticles)
      .def("getNumGrains", &GrainCollectionPy::getNumGrains)
      .def("__iter__", &GrainCollectionPy::getGrainIteratorPy)
      .def("__len__", &GrainCollectionPy::getNumGrains)
      .def(
        "createGrain",
        &GrainCollectionPy::createGrainPy,
        boost::python::return_value_policy<
          boost::python::reference_existing_object
        >()
      )
      .def(
        "createGrain",
        &GrainCollectionPy::createGrainWithIdPy,
        boost::python::return_value_policy<
          boost::python::reference_existing_object
        >()
      )
      ;

      GrainCollectionPy::GrainIteratorPy::exportIterator(
        "GrainCollectionGrainIterator"
      );
    }
  }
}

ext: cpp

max_stars_repo_name: danielfrascarelli/esys-particle

max_issues_repo_name: danielfrascarelli/esys-particle

max_forks_repo_name: danielfrascarelli/esys-particle


text:
//
// Copyright (c) 2018-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// 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)
//
// Official repository: https://github.com/boostorg/beast
//

#ifndef BOOST_BEAST_JSON_KIND_HPP
#define BOOST_BEAST_JSON_KIND_HPP

#include <boost/beast/core/detail/config.hpp>
#include <boost/beast/core/detail/static_const.hpp>
#include <cstddef>

namespace boost {
namespace beast {
namespace json {

enum class kind
{
    object,
    array,
    string,
    number,
    boolean,
    null
};

BOOST_BEAST_INLINE_VARIABLE(null, std::nullptr_t);

} // json
} // beast
} // boost

#endif

ext: hpp

max_stars_repo_name: djarek/BeastLounge

max_issues_repo_name: djarek/BeastLounge

max_forks_repo_name: djarek/BeastLounge


text:
#include <Access/Quota.h>
#include <boost/range/algorithm/equal.hpp>

namespace DB
{
bool operator ==(const Quota::Limits & lhs, const Quota::Limits & rhs)
{
    return boost::range::equal(lhs.max, rhs.max) && (lhs.duration == rhs.duration)
        && (lhs.randomize_interval == rhs.randomize_interval);
}

bool Quota::equal(const IAccessEntity & other) const
{
    if (!IAccessEntity::equal(other))
        return false;
    const auto & other_quota = typeid_cast<const Quota &>(other);
    return (all_limits == other_quota.all_limits) && (key_type == other_quota.key_type) && (to_roles == other_quota.to_roles);
}

}

max_stars_repo_name: pdv-ru/ClickHouse

max_issues_repo_name: pdv-ru/ClickHouse

max_forks_repo_name: pdv-ru/ClickHouse


text:
#include "simplified_options_adapter.hh"
#include <boost/algorithm/string.hpp>
#include <boost/throw_exception.hpp>
#include <iostream>

// Boost complains for some unknown reasons...
//#define BOOST_NO_EXCEPTIONS
//void boost::throw_exception(std::exception const &e) {}

SimplifiedOptionsAdapter::SimplifiedOptionsAdapter(std::string const &line)
    : line_(line) {
  std::vector<std::string> result;
  boost::split(result, line_, boost::is_any_of(" "));
  for (auto &c : result) {
    //c.remove_prefix(std::min(c.find_first_not_of("-"), c.size()));
      c.erase(0, std::min(c.find_first_not_of("-"), c.size()));
  }

  for (auto &c : result) {
    auto key = std::string(c.begin(), c.begin() + c.find_first_of("="));
    auto value = std::string(c.begin() + c.find_first_of("=") + 1, c.end());
    if (value.find(',') == std::string_view::npos) {
      values.push_back(std::move(value));
      command_maps_.string_map[key] = values.back();
    } else {
      std::vector<std::string> elems;
      boost::split(elems, value, boost::is_any_of(","));
      elements.push_back(std::move(elems));
      command_maps_.array_map[key] = elements.back();
    }
  }
}

CommandMaps const &SimplifiedOptionsAdapter::getCommands() {
  return command_maps_;
}

ext: cc

max_stars_repo_name: WRCIECH/clang-pimpl

max_issues_repo_name: WRCIECH/clang-pimpl

max_forks_repo_name: WRCIECH/clang-pimpl


text:
//
// Created by necator on 8/1/17.
//

#include "chat_user.h"

#include <iostream>
#include <boost/uuid/uuid_generators.hpp>

#include "easylogging.h"

#include "chat_server.h"
#include "chat_channel.h"

chat_user::chat_user(boost::asio::io_service &io_service, chat_server& server, chat_user_manager &cm) :
        io_service_(io_service),
        socket_(io_service),
        ping_timer_(io_service, boost::posix_time::seconds(ping_frequency_s_)),
        current_ping_retries_(0),
        server_(server),
        manager_(cm),
        name_(chat_message::source_max_length, ' '){
    //auto uuid = boost::uuids::random_generator()();
    //std::stringstream ss;
    //ss << uuid;
    // = ss.str().substr(0, chat_message::source_max_length);
    //LOG(INFO) << "new user: " << name_;
}

boost::asio::ip::tcp::socket &chat_user::socket() {
    return socket_;
}

void chat_user::start() {
    ping();
    do_read_header();
}

void chat_user::stop() {
    leave_all_channels();
    ping_timer_.cancel();
}

void chat_user::close_socket() {
    if (socket_.is_open()) {
        socket_.shutdown(socket_.shutdown_both);
        socket_.close();
    }
}

void chat_user::write(chat_message msg) {
    io_service_.post([this, msg]() {
        auto write_in_progress = !write_msgs_.empty();
        write_msgs_.push_back(msg);

        if (!write_in_progress)
            do_write();
    });
}

void chat_user::do_read_header() {
    LOG(INFO) << "waiting for a msg...";

    auto self(shared_from_this());
    boost::asio::async_read(socket_,
                            boost::asio::buffer(inbound_header_, inbound_header_.size()),
                            [this, self](const boost::system::error_code &ec,
                                         std::size_t s __attribute__ ((unused))) {
                                if (!ec) {
                                    std::istringstream is(std::string(inbound_header_.begin(), inbound_header_.end()));
                                    std::size_t inbound_data_size = 0;

                                    if (!(is >> std::hex >> inbound_data_size))
                                        manager_.stop(shared_from_this());

                                    inbound_data_.resize(inbound_data_size);

                                    do_read_message();
                                } else {
                                    LOG(ERROR) << "Error: " << ec.message();
                                    manager_.stop(shared_from_this());
                                }
                            });
}

void chat_user::do_read_message() {
    auto self(shared_from_this());
    boost::asio::async_read(socket_,
                            boost::asio::buffer(inbound_data_, inbound_data_.size()),
                            [this, self](const boost::system::error_code &ec,
                                         std::size_t s __attribute__ ((unused))) {
                                if (!ec) {
                                    try {
                                        auto msg = deserialize_chat_message(inbound_data_);
                                        if (msg.type() == chat_message_type::status && msg.content() == "pong") {
                                            LOG(INFO) << name_ << ": received pong from client.";
                                            pong_received_ = true;
                                        } else
                                            server_.handle_message(std::move(msg), shared_from_this());
                                    } catch(const boost::archive::archive_exception& e) {
                                        LOG(ERROR) << "Error: Failed to decode msg: " << e.what();
                                    }
                                    do_read_header();
                                } else {
                                    LOG(ERROR) << "Error: " << ec.message();
                                    manager_.stop(shared_from_this());
                                }
                            });
}

void chat_user::do_write() {
    auto self(shared_from_this());

    boost::asio::async_write(socket_,
                             write_msgs_.front().generate_buffers(),
                             [this, self](const boost::system::error_code &ec,
                                          std::size_t s __attribute__ ((unused))) {
                                 if (!ec) {
                                     write_msgs_.pop_front();

                                     if (!write_msgs_.empty())
                                         do_write();

                                 } else {
                                     LOG(ERROR) << "Error: " << ec.message();
                                     manager_.stop(shared_from_this());
                                 }
                             });
}

void chat_user::ping() {
    LOG(INFO) << "Sending client(" << name_ << ") a ping.";

    write(chat_message("server", name_, "ping", chat_message_type::status));

    auto self(shared_from_this());

    ping_timer_.async_wait([this, self](const boost::system::error_code& ec) {
        if(!ec) {
            if (pong_received_) {
                current_ping_retries_ = 0;
                pong_received_ = false;
                ping_timer_.expires_at(ping_timer_.expires_at() + boost::posix_time::seconds(ping_frequency_s_));
                ping();
            } else if (current_ping_retries_ < ping_retry_max_) {
                current_ping_retries_++;
                ping_timer_.expires_at(ping_timer_.expires_at() + boost::posix_time::seconds(ping_frequency_s_ / 2));

                LOG(INFO) << "Ping retry " << std::to_string(current_ping_retries_)
                          << " of " << std::to_string(ping_retry_max_) << ".";

                ping();
            } else {
                LOG(INFO) << name_ << ": Ping Timeout!";

                leave_all_channels();
                close_socket();
            }
        }
    });
}

std::string chat_user::name(const std::string &n) {
    if (!n.empty() && n.size() > 2) {
        LOG(INFO) << "user: " << name_ << " will be now known as: " << n;

        for(const auto& c : channels_) {
            c->publish(chat_message("server",
                                    c->name(),
                                    name_ + " is now known as " + n + ".",
                                    chat_message_type::status));
        }

        name_ = n;
    }

    return name_;
}

char* chat_user::name_data() {
    return &name_[0];
}

std::vector<std::string> chat_user::joined_channels() const {
    std::vector<std::string> ret;

    std::transform(channels_.begin(), channels_.end(), std::back_inserter(ret), [](chat_channel_ptr u) {
        return u->name();
    });

    return ret;
}

bool chat_user::is_joined(chat_channel_ptr c) {
    auto it = channels_.find(c);

    auto boolean = it != channels_.end();

    return boolean;
}

void chat_user::leave_all_channels() {
    LOG(INFO) << "User: " << name_ << " is leaving all channels.";

    //make copy so the channel can safely remove itself from the users channel list
    auto chans = channels_;

    for (chat_channel_ptr c : chans)
        c->leave(shared_from_this());

    //channels_ ought to be empty here.
    channels_.clear();
}

max_stars_repo_name: mpunkenhofer/BoostAsioChat

max_issues_repo_name: mpunkenhofer/BoostAsioChat

max_forks_repo_name: mpunkenhofer/BoostAsioChat


text:
#include <algorithm>
#include <fstream>
#include <iostream>

#include <boost/algorithm/string/join.hpp>

#include "AlignmentUtils.hpp"
#include "Utils.hpp"

alignment Alm::parse(const std::string &path)
{
  std::ifstream alignmentFile(path, std::ifstream::binary);

  json j;
  alignmentFile >> j;

  alignment al;
  alignmentPair p;

  for (const json::object_t &o : j["alignment"]) {
    p = Alm::getPair(o);
    al.push_back(p);
  }

  return al;
}

alignmentGroup Alm::getGroup(json::array_t a)
{
  alignmentGroup g;

  for (const label &l : a)
    g.push_back(l);

  return g;
}

alignmentPair Alm::getPair(json::object_t o)
{
  alignmentPair p;
  p.first  = Alm::getGroup(o["lhs"]);
  p.second = Alm::getGroup(o["rhs"]);

  return p;
}

alignmentGrouping Alm::Lhs(const alignment &alm)
{
  alignmentGrouping gp;

  for (const alignmentPair &p : alm)
    gp.push_back(p.first);

  return gp;
}

alignmentGrouping Alm::Rhs(const alignment &alm)
{
  alignmentGrouping gp;

  for (const alignmentPair &p : alm)
    gp.push_back(p.second);

  return gp;
}

almMap Alm::AlmMap(const alignment &alm)
{
  almMap m;

  for (const alignmentPair &p : alm)
    m[p.first] = p.second;

  return m;
}

bool Alm::hasLabel(const alignmentGroup &g, const label &l)
{
  alignmentGroup::const_iterator it;

  it = std::find(g.begin(), g.end(), l);

  if (it == g.end())
    return false;

  return true;
}

bool Alm::hasPair(const alignment &alm, const alignmentPair &p)
{
  for (const alignmentPair &ap : alm) {
    if (p == ap)
      return true;
  }

  return false;
}

bool Alm::groupEqual(const alignmentGroup &g1, const alignmentGroup &g2)
{
  return std::equal(g1.begin(), g1.end(), g2.begin(), g2.end());
}

bool Alm::groupingEqual(const alignmentGrouping &gp1, const alignmentGrouping &gp2)
{
  return std::equal(gp1.begin(), gp1.end(), gp2.begin(), gp2.end(), Alm::groupEqual);
}

alignmentGrouping Alm::getGrouping(const label &l, const alignmentHalf &alh)
{
  alignmentGrouping res;

  for (const alignmentGroup &g : alh) {
    if (Alm::hasLabel(g, l))
      res.push_back(g);
  }

  return res;
}

label Alm::getLabelFromGrouping(const alignmentGrouping &gp)
{
  return gp[0][0];
}

std::string Alm::groupToStr(const alignmentGroup &g)
{
  std::string res = boost::algorithm::join(g, ",");

  return res;
}

std::string Alm::groupingToStr(const alignmentGrouping &gp)
{
  if (gp.empty())
    return "-";

  std::vector<std::string> tmp;

  for (const alignmentGroup &g : gp)
    tmp.push_back(groupToStr(g));

  std::string res = boost::algorithm::join(tmp, " | ");

  return res;
}

void Alm::print(const alignment &alm)
{
  for (const alignmentPair &p : alm) {
    std::cout << "  ";
    Alm::printPair(p);
  }

  return;
}

void Alm::printPair(const alignmentPair &p)
{
  Alm::printGroup(p.first);
  std::cout << "\t<> ";
  Alm::printGroup(p.second);

  std::cout << std::endl;

  return;
}

void Alm::printGrouping(const alignmentGrouping &gp)
{
  if (gp.size() == 0) {
    std::cout << "-";
    return;
  }

  alignmentGrouping::const_iterator it;

  for (it = gp.begin(); it != (gp.end() - 1); ++it) {
    Alm::printGroup(*it);
    std::cout << " | ";
  }

  Alm::printGroup(*it);

  return;
}

void Alm::printGroup(const alignmentGroup &g)
{
  alignmentGroup::const_iterator it;

  for (it = g.begin(); it != (g.end() - 1); ++it)
    std::cout << *it << ", ";

  std::cout << *it;

  return;
}

void Alm::printAlmMap(const almMap &m)
{
  for (const alignmentPair &p : m) {
    Alm::printGroup(p.first);
    std::cout << "\t-> ";
    Alm::printGroup(p.second);
    Util::printLine();
  }

  return;
}

max_stars_repo_name: Isotactics/deciding-isotactics

max_issues_repo_name: Isotactics/deciding-isotactics

max_forks_repo_name: Isotactics/deciding-isotactics


text:
#ifndef REVERSE_GRAPH_PATCHED_H_
#define REVERSE_GRAPH_PATCHED_H_

#include <boost/version.hpp>

#include <boost/graph/reverse_graph.hpp>

#if defined(BOOST_REVGRAPH_PATCH)

// Boost 1.62.0 does not implement degree() in reverse_graph which is required
// by BidirectionalGraph, so add it.

namespace boost {

template <class BidirectionalGraph, class GRef>
inline typename graph_traits<BidirectionalGraph>::degree_size_type
degree(const typename graph_traits<BidirectionalGraph>::vertex_descriptor u,
       const reverse_graph<BidirectionalGraph,GRef>& g)
{
    return degree(u, g.m_g);
}

} // namespace boost

#endif // Boost 1.62.0

#endif

max_stars_repo_name: jtravee/neuvector

max_issues_repo_name: kuangxiaohong/phytium-hyperscan

max_forks_repo_name: kuangxiaohong/phytium-hyperscan


text:
/**
 * @file stableevaluationatapoint.cc
 * @brief NPDE homework StableEvaluationAtAPoint
 * @author Amélie Loher, Erick Schulz & Philippe Peter
 * @date 29.11.2021
 * @copyright Developed at ETH Zurich
 */

#include "stableevaluationatapoint.h"

#include <lf/base/base.h>
#include <lf/fe/fe.h>
#include <lf/geometry/geometry.h>
#include <lf/mesh/mesh.h>
#include <lf/quad/quad.h>
#include <lf/uscalfe/uscalfe.h>

#include <Eigen/Core>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <memory>

namespace StableEvaluationAtAPoint {

double MeshSize(const std::shared_ptr<const lf::mesh::Mesh> &mesh_p) {
  double mesh_size = 0.0;
  // Find maximal edge length
  for (const lf::mesh::Entity *edge : mesh_p->Entities(1)) {
    // Compute the length of the edge
    double edge_length = lf::geometry::Volume(*(edge->Geometry()));
    mesh_size = std::max(edge_length, mesh_size);
  }
  return mesh_size;
}

Eigen::Vector2d OuterNormalUnitSquare(Eigen::Vector2d x) {
  // Use shortcut: x is on the unit square
  if (x(0) > x(1) && x(0) < 1.0 - x(1)) {
    return Eigen::Vector2d(0.0, -1.0);
  }
  if (x(0) > x(1) && x(0) > 1.0 - x(1)) {
    return Eigen::Vector2d(1.0, 0.0);
  }
  if (x(0) < x(1) && x(0) > 1.0 - x(1)) {
    return Eigen::Vector2d(0.0, 1.0);
  }
  return Eigen::Vector2d(-1.0, 0.0);
}

double FundamentalSolution::operator()(Eigen::Vector2d y) {
  LF_ASSERT_MSG(x_ != y, "G not defined for these coordinates!");
  return -1.0 / (2.0 * M_PI) * std::log((x_ - y).norm());
}

Eigen::Vector2d FundamentalSolution::grad(Eigen::Vector2d y) {
  LF_ASSERT_MSG(x_ != y, "G not defined for these coordinates!");
  return (x_ - y) / (2.0 * M_PI * (x_ - y).squaredNorm());
}

double PointEval(std::shared_ptr<const lf::mesh::Mesh> mesh_p) {
  double error = 0.0;
#if SOLUTION
  const auto u = [](Eigen::Vector2d x) -> double {
    Eigen::Vector2d one(1.0, 0.0);
    return std::log((x + one).norm());
  };
  const auto gradu = [](Eigen::Vector2d x) -> Eigen::Vector2d {
    Eigen::Vector2d one(1.0, 0.0);
    return (x + one) / (x + one).squaredNorm();
  };
  // Define a Functor for the dot product of grad u(x) * n(x)
  const auto gradu_dot_n = [gradu](const Eigen::Vector2d x) -> double {
    // Determine the normal vector n on the unit square
    Eigen::Vector2d n = OuterNormalUnitSquare(x);
    return gradu(x).dot(n);
  };

  // Compute right hand side
  const Eigen::Vector2d x(0.3, 0.4);
  const double rhs = PSL(mesh_p, gradu_dot_n, x) - PDL(mesh_p, u, x);
  // Compute the error
  error = std::abs(u(x) - rhs);
#else
  //====================
  // Your code goes here
  //====================
#endif
  return error;
}

double Psi::operator()(Eigen::Vector2d y) {
  const double c = M_PI / (0.5 * std::sqrt(2) - 1.0);
  const double dist = (y - center_).norm();

  if (dist <= 0.25 * std::sqrt(2)) {
    return 0.0;
  } else if (dist >= 0.5) {
    return 1.0;
  } else {
    return std::pow(std::cos(c * (dist - 0.5)), 2);
  }
}

Eigen::Vector2d Psi::grad(Eigen::Vector2d y) {
  double c = M_PI / (0.5 * std::sqrt(2) - 1.0);
  double dist = (y - center_).norm();

  if (dist <= 0.25 * std::sqrt(2)) {
    return Eigen::Vector2d(0.0, 0.0);

  } else if (dist >= 0.5) {
    return Eigen::Vector2d(0.0, 0.0);
  } else {
    return -2.0 * std::cos(c * (dist - 0.5)) * std::sin(c * (dist - 0.5)) *
           (c / dist) * (y - center_);
  }
}

double Psi::lapl(Eigen::Vector2d y) {
  double c = M_PI / (0.5 * std::sqrt(2) - 1.0);
  double c2 = c * c;
  double dist = (y - center_).norm();

  if (dist <= 0.25 * std::sqrt(2)) {
    return 0.0;
  } else if (dist >= 0.5) {
    return 0.0;
  } else {
    double sineval = std::sin(c * (dist - 0.5));
    double coseval = std::cos(c * (dist - 0.5));
    return 2 * c2 * sineval * sineval - 2 * c2 * coseval * coseval -
           2 * c * sineval * coseval / dist;
  }
}

double Jstar(std::shared_ptr<lf::uscalfe::FeSpaceLagrangeO1<double>> fe_space,
             Eigen::VectorXd uFE, const Eigen::Vector2d x) {
  double val = 0.0;
  Psi psi(Eigen::Vector2d(0.5, 0.5));
  FundamentalSolution G(x);
#if SOLUTION

  // Mesh covering a unit square domain
  std::shared_ptr<const lf::mesh::Mesh> mesh = fe_space->Mesh();
  // Use midpoint quadrature rule
  const lf::quad::QuadRule qr = lf::quad::make_TriaQR_MidpointRule();
  // Quadrature points
  const Eigen::MatrixXd zeta_ref{qr.Points()};
  // Quadrature weights
  const Eigen::VectorXd w_ref{qr.Weights()};
  // Number of quadrature points
  const lf::base::size_type P = qr.NumPoints();
  // Create mesh function to be evaluated at the quadrature points
  auto uFE_mf = lf::fe::MeshFunctionFE(fe_space, uFE);

  // Loop over all cells
  for (const lf::mesh::Entity *entity : mesh->Entities(0)) {
    // Standard way to apply a local quadrature rule
    const lf::geometry::Geometry &geo{*entity->Geometry()};
    // Quadrature points on actual cell
    const Eigen::MatrixXd zeta{geo.Global(zeta_ref)};
    const Eigen::VectorXd gram_dets{geo.IntegrationElement(zeta_ref)};
    // Values of finite element function on all quadrature points
    auto u_vals = uFE_mf(*entity, zeta_ref);

    // Quadrature loop
    for (int l = 0; l < P; l++) {
      const double w = w_ref[l] * gram_dets[l];
      val += w * (-u_vals[l]) *
             (2.0 * (G.grad(zeta.col(l))).dot(psi.grad(zeta.col(l))) +
              G(zeta.col(l)) * psi.lapl(zeta.col(l)));
    }
  }
#else
  //====================
  // Your code goes here
  //====================
#endif
  return val;
}

double StablePointEvaluation(
    std::shared_ptr<lf::uscalfe::FeSpaceLagrangeO1<double>> fe_space,
    Eigen::VectorXd uFE, const Eigen::Vector2d x) {
  double res = 0.0;

  Eigen::Vector2d center(0.5, 0.5);
  if ((x - center).norm() <= 0.25) {
    res = Jstar(fe_space, uFE, x);
  } else {
    std::cerr << "The point does not fulfill the assumptions" << std::endl;
  }

  return res;
}

double EvaluateFEFunction(
    std::shared_ptr<lf::uscalfe::FeSpaceLagrangeO1<double>> fe_space,
    const Eigen::VectorXd &uFE, Eigen::Vector2d global, double tol) {
  // Extract mesh
  auto mesh_p = fe_space->Mesh();
  // wrap coefficient vector into a FE mesh-function
  lf::fe::MeshFunctionFE mf(fe_space, uFE);

  for (const lf::mesh::Entity *entity_p : mesh_p->Entities(0)) {
    LF_ASSERT_MSG(lf::base::RefEl::kTria() == entity_p->RefEl(),
                  "Function only defined for triangular cells");

    // compute geometric information about the cell
    const lf::geometry::Geometry *geo_p = entity_p->Geometry();
    Eigen::MatrixXd corners = lf::geometry::Corners(*geo_p);

    // transform global coordinates to local coordinates on the cell
    Eigen::Matrix2d A;
    A << corners.col(1) - corners.col(0), corners.col(2) - corners.col(0);
    Eigen::Vector2d b;
    b << global - corners.col(0);
    Eigen::Vector2d loc = A.fullPivLu().solve(b);

    // evaluate meshfunction, if local coordinates lie in the reference triangle
    if (loc(0) >= 0 - tol && loc(1) >= 0 - tol && loc(0) + loc(1) <= 1 + tol) {
      return mf(*entity_p, loc)[0];
    }
  }
  return 0.0;
}

}  // namespace StableEvaluationAtAPoint


text:
/**
 * @file    STLUtils_StringUtils.cc
 * @brief   Implementation of path utility functions in std::string format.
 * @author  [Yi-Mu "Enoch" Chen](https://github.com/yimuchen)
 */
#ifdef CMSSW_GIT_HASH
#include "UserUtils/Common/interface/STLUtils/Filesystem.hpp"
#include "UserUtils/Common/interface/STLUtils/StringUtils.hpp"
#else
#include "UserUtils/Common/STLUtils/Filesystem.hpp"
#include "UserUtils/Common/STLUtils/StringUtils.hpp"
#endif

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <regex>
#include <sstream>
#include <string>

#include <boost/algorithm/string.hpp>
#include <boost/range/algorithm_ext/erase.hpp>

namespace usr  {

/**
 * @brief C++ string version of getting system environment variables.
 */
std::string
GetEnv( const std::string& x )
{
  return std::getenv( x.c_str() );
}

/**
 * @brief Converting the glob notation to regex comparaison strings.
 */
std::string
GlobToRegex( const std::string& query )
{
  static const std::regex starmatch( "\\*" );
  static const std::string starrep( ".*" );

  static const std::regex qmmatch( "\\?" );
  static const std::string qmrep( "." );

  std::string ans = query;
  ans = std::regex_replace( ans, starmatch, starrep );
  ans = std::regex_replace( ans, qmmatch, qmrep );
  return ans;
}

/**
 * @brief Globbing with the return type being a list of string rather than a
 *        the std::filesystem, useful for ROOT and CMSSW interfaces.
 */
std::vector<std::string>
GlobLocalStr( const std::string& x )
{
  const auto orig = GlobLocal( x );
  std::vector<std::string> ans( orig.size() );
  std::transform( orig.begin(), orig.end(),
    ans.begin(),
    []( const fs::path& x ) -> std::string {
      return x.string();
    } );
  return ans;
}

/**
 * @brief generating an random alpha-numerical string of length n .
 *
 * Currently the random number generator used is that std::rand function
 * provided in the cstdlib.h file. No way of customizing the generator,
 * but allows for setting the random seed outside the function.
 */
std::string
RandomString( const unsigned n )
{
  static const std::string alphanum = "0123456789"
                                      "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                                      "abcdefghijklmnopqrstuvwxyz";

  std::string ans = "";

  for( unsigned i = 0; i < n; ++i ){
    ans.push_back( alphanum[rand()%alphanum.length()] );
  }

  return ans;
}

/**
 * @brief Converting entire string to uppercase
 */
std::string
ToUpper( const std::string& x )
{
  return boost::algorithm::to_upper_copy( x );
}

/**
 * @brief Converting entire string to lower case
 */
std::string
ToLower( const std::string& x )
{
  return boost::algorithm::to_lower_copy( x );
}

/**
 * @brief removing all instances of a certain substring in a given string.
 */
std::string
StripSubstring( const std::string& x, const std::string& sub )
{
  std::string ans( x );
  boost::algorithm::erase_all( ans, sub );
  return ans;
}

/**
 * @brief Remove all characters that is in string list.
 */
std::string
StripCharacters( const std::string& x, const std::string& sub )
{
  std::string ans( x );
  boost::range::remove_erase_if( ans, boost::is_any_of( sub ) );
  return ans;
}

std::string
StripToNaming( const std::string& x )
{
  std::string ans( x );
  ans = ToLower( ans );
  ans = StripCharacters( ans, " \n\t" );
  return ans;
}

/**
 * @brief Returing the position of a matching brace in a string.
 *
 * For a given input string, and the position to the opening brace: either '[',
 * '(' or '{', the function returns the matching brace position of the
 * corresponding character, taking into account nest braces. For examples the
 * function call:
 *
 * ```
 * MatchBrace( "((()))", 0 ) == 5
 * MatchBrace( "((()))", 1 ) == 4
 * MatchBrace( "((()))", 2 ) == 3
 * ```
 *
 * Note that this function assumes normal tree-like structures between braces of
 * different flavour, while only taking into account the braces of the same
 * flavour.  So the function call:
 * ```
 * MatchBrace("([)]", 0 ) == 2
 * MatchBrace("([)]", 1 ) == 3
 * ```
 * "Makes sense", in that it indeed returns the expected matching brace for '(',
 * and '[' respectively, thought the actual syntax of the full string in
 * ambiguous. In the case that the matching brace cannot be found (for example in
 * the string "((())"), this function will return the end of the string.
 */
size_t
MatchBrace( const std::string& x, const unsigned open_brace )
{
  size_t ans       = open_brace;
  unsigned counter = 1;

  const char addcount = x.at( open_brace );
  const char mincount = addcount == '(' ? ')' :
                        addcount == '{' ? '}' :
                        addcount == '[' ? ']' :
                        ')';

  while( counter > 0 && ++ans < x.length() ){
    const char c = x.at( ans );
    if( c == addcount ){
      ++counter;
    } else if( c == mincount ){
      --counter;
    }
  }

  return ans;
}

/**
 * @brief Finding the next opening brace character after the given position.
 *
 * Finds the fist instance of '[', '(' or '{' are the given index position.
 */
size_t
NextOpenBrace( const std::string& x, const unsigned start )
{
  return x.find_first_of( "([{", start );
}

/**
 * @brief Checking if master string starts with some target string.
 *
 * Using the std::equal algorithm with the forwards iterator to complete.
 */
bool
starts_with( const std::string& master, const std::string& target )
{
  if( target.size() > master.size() ){ return false; }
  return std::equal( target.begin(), target.end(), master.begin() );
}

/**
 * @brief Checking if a masters string ends with some target string.
 *
 * Using std::equal with the reverse iterator to complete.
 */
bool
ends_with( const std::string& master, const std::string& target )
{
  if( target.size() > master.size() ){ return false; }
  return std::equal( target.rbegin(), target.rend(), master.rbegin() );
}

}/* usr  */

max_stars_repo_name: yimuchen/UserUtils

max_issues_repo_name: yimuchen/UserUtils

max_forks_repo_name: yimuchen/UserUtils


text:
// import libraries
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/array.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/archive/text_oarchive.hpp>
#include "../../src/libsalt/VisMsg.h"

using namespace boost::asio;
using ip::tcp;
using std::cout;
using std::endl;

class con_handler : public boost::enable_shared_from_this<con_handler> {
private:
    tcp::socket sock;

    enum { max_length = sizeof(DataMsg)};
    boost::array<char, max_length> data;

    bool inited = false;

    // Make message header
    struct MsgHeader makeHeader(unsigned int type) {
        struct MsgHeader header;
        header.type = type;
        auto unix_timestamp = std::chrono::seconds(std::time(NULL));
        long timestamp = std::chrono::milliseconds(unix_timestamp).count(); // unix timestamp in milliseconds
        header.timestamp = timestamp;

        return header;
    }

public:
    typedef boost::shared_ptr<con_handler> pointer;
    con_handler(boost::asio::io_service& io_service): sock(io_service) {}

    // creating the pointer
    static pointer create(boost::asio::io_service& io_service) {
        return pointer(new con_handler(io_service));
    }

    // socket creation
    tcp::socket& socket() {
        return sock;
    }

    void stop(void) {
        sock.close();
    }

    void read() {
        memset(&data, '\0', sizeof(data));

        boost::asio::async_read(sock, boost::asio::buffer(data),
                                boost::asio::transfer_exactly(sizeof(MsgHeader)),
                                boost::bind(&con_handler::handle_read,
                                            shared_from_this(),
                                            boost::asio::placeholders::error,
                                            boost::asio::placeholders::bytes_transferred));

/*
        InitMsg init;
        init.header = makeHeader(VIS_INIT);
        strcpy(init.simulationId, "xx13579");
        int size = sizeof(init);
        boost::asio::async_write(sock,
                                 boost::asio::buffer(&init, size),
                             boost::bind(&con_handler::handle_write,shared_from_this(),
                                         boost::asio::placeholders::error,
                                         boost::asio::placeholders::bytes_transferred)
        );
*/
        // boost::this_thread::sleep(boost::posix_time::seconds(1));

/*
        StopMsg stop;
        stop.header = makeHeader(VIS_STOP);

        int stopSize = sizeof(stop);
        boost::asio::async_write(sock,
                                 boost::asio::buffer(&stop, stopSize),
                                 boost::bind(&con_handler::handle_write,shared_from_this(),
                                             boost::asio::placeholders::error,
                                             boost::asio::placeholders::bytes_transferred)
        );
*/

    }

    void handle_read(const boost::system::error_code& err, size_t bytes_transferred) {
        if (bytes_transferred > 0) {
            std::cout << __FUNCTION__ << "() Received MsgHeader, received=" << bytes_transferred << std::endl;
        }

        if (!err) {
            // Decode message type
            MsgHeader* header = reinterpret_cast<MsgHeader*>(&data);

            if (MSG_INIT == header->type) {
                // boost::array<char, sizeof(StatusMsg)> buf;
                std::vector<char> buf;
                buf.resize(header->bodySize);
                boost::system::error_code statusErr;
                size_t received_bytes = boost::asio::read(sock, boost::asio::buffer(buf), boost::asio::transfer_exactly(header->bodySize), statusErr);
                std::cout << __FUNCTION__ << "() Received INIT message, type=" << header->type << ", bodySize=" << header->bodySize << ", received_bytes=" << received_bytes << std::endl;

//                if (inited) {
//                    std::cerr << "ERROR: Received INIT message again" << std::endl;
//
//                    // Read next messages
//                    read();
//                }

//                inited = true;

                // Decode message
                typedef char charArray[];
                const charArray* simulationId = reinterpret_cast<charArray*>(&buf[0]);
                std::cout << "[InitMsg] timestamp=" << header->timestamp << ", simulationId=[" << *simulationId << "]" << std::endl;

                // test
                // sendSetMsg();
                // sendStopMsg();

            } else if (MSG_STATUS == header->type) {

                // boost::array<char, sizeof(StatusMsg)> buf;
                std::vector<char> buf;
                buf.resize(header->bodySize);
                boost::system::error_code statusErr;
                size_t received_bytes = boost::asio::read(sock, boost::asio::buffer(buf), boost::asio::transfer_exactly(header->bodySize), statusErr);
                std::cout << __FUNCTION__ << "() Received STATUS message, type=" << header->type << ", bodySize=" << header->bodySize << ", received_bytes=" << received_bytes << std::endl;

                // Decode message
                // const StatusMsg* msg = reinterpret_cast<const StatusMsg*>(buf.data());
                // std::cout << "[StatusMsg] timestamp=" << msg->header.timestamp << ", status=" << msg->status << ", progress=" << msg->progress << std::endl;

                const VisClientState* status = reinterpret_cast<VisClientState*>(&buf[0]);
                const unsigned int* progress = reinterpret_cast<unsigned int*>(&buf[sizeof(VisClientState)]);
                std::cout << "[StatusMsg] timestamp=" << header->timestamp << ", status=" << *status << ", progress=" << *progress << std::endl;

            } else if (MSG_DATA == header->type) {

                // boost::array<char, 100000> buf;
                std::vector<char> buf;
                buf.resize(header->bodySize);
                boost::system::error_code dataErr;
                // size_t received = boost::asio::read(sock, boost::asio::buffer(buf), boost::asio::transfer_exactly(header->size), dataErr);
                size_t received_bytes = boost::asio::read(sock, boost::asio::buffer(buf), boost::asio::transfer_exactly(sizeof(DataMsg) - sizeof(MsgHeader)), dataErr);
                std::cout << __FUNCTION__ << "() Received DATA message, type=" << header->type << ", bodySize=" << header->bodySize << ", received_bytes=" << received_bytes << std::endl;

                // Decode message
                // const DataMsg* msg = reinterpret_cast<const DataMsg*>(buf.data());
                const unsigned int* numRoads = reinterpret_cast<unsigned int*>(&buf[0]);

                typedef struct Road RoadArray[header->bodySize];
                const RoadArray* roadsPtr = reinterpret_cast<RoadArray*>(&buf[sizeof(unsigned int)]);
                auto roads = *roadsPtr;
                std::cout <<"[DataMsg] timestamp=" << header->timestamp << ", numRoads=" << *numRoads << std::endl;
                for (int i = 0; i < *numRoads; i++) {
                    Road road = roads[i];
                    std::string cls;
                    for (int j = 0; j < road.numVehicles; j++) {
                        VisVehicleClass c = road.vehicles[j];
                        if (c == VisVehicleClass::SMALL) {
                            cls += "s";
                        } else {
                            cls += "L";
                        }
                    }

                    std::string signal = "RED";
                    VisSignalType s = road.currentSignal;
                    if (s == VisSignalType::GREEN) {
                        signal = "GREEN";
                    }

                    if (i == 0 || i % 10 == 0 || i == *numRoads - 1) {
                        cout << (i+1) << "/" << *numRoads << ") roadId=" << std::string(road.roadId) << ", signal=" << signal << ", speed=" << road.speed << ", numVehicles=" << road.numVehicles << ", class=" << cls << endl;
                    }
                }
            } else {
                std::cout << __FUNCTION__ << "() Received UNKNOWN message, type=" << header->type << std::endl;
            }

        }
        else {
            if (err == boost::asio::error::eof) {
                std::cout << __FUNCTION__ << "() Client Disconnected" << std::endl;
                stop();
                return;
            } else {
                std::cerr << __FUNCTION__ << "() error: " << err.message() << std::endl;
            }
        }

        std::cout << __FUNCTION__ << "() Completed" << std::endl << std::endl;

        // Read next messages
        read();
    }

    void handle_write(const boost::system::error_code& err, size_t bytes_transferred) {
        if (!err) {
            cout << "Server sent message, size=" << bytes_transferred << endl;
        } else {
            std::cerr << __FUNCTION__ << "() error: " << err.message() << endl;
            sock.close();
        }
    }

    void sendSetMsg() {
        SetMsg msg = SetMsg();

        msg.header = MsgHeader();
        msg.header.type = MSG_SET;

        const auto p1 = std::chrono::system_clock::now();
        long timestamp = std::chrono::duration_cast<std::chrono::seconds>(p1.time_since_epoch()).count();
        msg.header.timestamp = timestamp;

        //msg.roadType = VisRoadType::LINK;
        msg.roadType = VisRoadType::CELL;
        msg.extend[0] = 100.0; // minx
        msg.extend[1] = 200.0; // miny
        msg.extend[2] = 200.0; // maxx
        msg.extend[3] = 100.0; // maxy

        msg.header.bodySize = sizeof(VisRoadType) + (sizeof(float) * EXTEND_LENGTH);
        int msgSize = sizeof(MsgHeader) + msg.header.bodySize;
        std::cout << "[SetMsg] roadType=" << msg.roadType << ", timestamp=" << msg.header.timestamp
                  << ", msgSize=" << msgSize << ", headerSize=" << sizeof(MsgHeader) << ", bodySize=" << msg.header.bodySize << std::endl;

        boost::asio::async_write(sock, boost::asio::buffer(&msg, msgSize), boost::bind(&con_handler::handle_write, shared_from_this(),
                                             boost::asio::placeholders::error,
                                             boost::asio::placeholders::bytes_transferred));

    }

    void sendStopMsg() {
        StopMsg msg = StopMsg();

        msg.header = MsgHeader();
        msg.header.type = MSG_STOP;

        const auto p1 = std::chrono::system_clock::now();
        long timestamp = std::chrono::duration_cast<std::chrono::seconds>(p1.time_since_epoch()).count();
        msg.header.timestamp = timestamp;

        msg.header.bodySize = 0;
        int msgSize = sizeof(MsgHeader) + msg.header.bodySize;
        std::cout << "[StopMsg] timestamp=" << msg.header.timestamp << ", msgSize=" << msgSize << ", headerSize=" << sizeof(MsgHeader) << ", bodySize=" << msg.header.bodySize << std::endl;

        boost::asio::async_write(sock, boost::asio::buffer(&msg, msgSize), boost::bind(&con_handler::handle_write, shared_from_this(),
                                                                                boost::asio::placeholders::error,
                                                                                boost::asio::placeholders::bytes_transferred));
    }

};

class Server
{
private:
    tcp::acceptor acceptor_;
    boost::asio::io_service::strand strand;

    void accept() {
        // socket
        con_handler::pointer connection = con_handler::create(acceptor_.get_io_service());

        // asynchronous accept operation and wait for a new connection.
        acceptor_.async_accept(connection->socket(), strand.wrap(
            boost::bind(&Server::handle_accept, this, connection,
            boost::asio::placeholders::error)));
    }

public:

    // constructor for accepting connection from client
    Server(boost::asio::io_service& io_service, int port): acceptor_(io_service, tcp::endpoint(tcp::v4(), port)), strand(io_service) {
        accept();
    }

    void handle_accept(con_handler::pointer connection, const boost::system::error_code& err)
    {
        cout << __FUNCTION__ << "() CONNECT from VisClient" << endl << endl;
        if (!err) {
            connection->read();
        } else {
            std::cerr << "error: " << err.message() << endl;
        }
        accept();
    }
};

int
main(int argc, char *argv[]) {
    try {
        int port = 1337;
        boost::asio::io_service io_service;
        Server server(io_service, port);
        io_service.run();
    } catch (std::exception& e) {
        std::cerr << e.what() << endl;
    }
    return 0;
}


text:
/**
 * @file MessageConsumer.cpp
 *
 * @author Nicolae Natea
 * Contact: nicu@natea.ro
 */
#include <functional>
#include <iostream>

#include <boost/format.hpp>

#include <core/logger/event_logger.h>
#include <core/util/Time.hpp>

#include "MessageConsumer.hpp"

namespace component
{
	MessageConsumer MessageConsumer::_prototype;

	MessageConsumer::MessageConsumer()
	{
		Component::addPrototype("MessageConsumer", this);
	}

	void MessageConsumer::handler(const core::MessageData& msg)
	{
		LOG_INFO("dbg",
			"MessageConsumer: received message with value: %u",
			msg.get<uint32_t>("count"));
	}

	MessageConsumer* MessageConsumer::clone() const
	{
		LOG_DEBUG("dbg", "MessageConsumer: cloning consumer");
		return new MessageConsumer(*this);
	}

	void MessageConsumer::init(const boost::property_tree::ptree::value_type& component)
	{
		LOG_DEBUG("dbg", "MessageConsumer: initializing consumer");

		// Clone
		m_published_message = component.second.get<std::string>("msg_id", "");

		if (not m_published_message.empty())
		{
			LOG_DEBUG("dbg", "MessageConsumer: subscribed to message-id: %s", m_published_message.c_str());
			subscribe(m_published_message, std::bind(&MessageConsumer::handler, this, std::placeholders::_1));
		}
	}

	MessageConsumer::~MessageConsumer()
	{
	}

	void MessageConsumer::stop()
	{
		LOG_DEBUG("dbg", "MessageConsumer: stopping consumer");
	}

	void MessageConsumer::start()
	{
		LOG_DEBUG("dbg", "MessageConsumer: starting consumer");
	}
}

max_stars_repo_name: nicsor/BreadCrumbs

max_issues_repo_name: nicsor/BreadCrumbs

max_forks_repo_name: nicsor/BreadCrumbs