text:
#include "hydrology.h"
#include "../basic.h"

#include <random>

#include <boost/log/trivial.hpp>
#include <boost/random.hpp>

namespace WorldEngine
{

static void Droplet(World& world, uint32_t x, uint32_t y, float q);
static void WatermapExecute(World& world, uint32_t numSamples, uint32_t seed);

static const uint32_t NUM_SAMPLES = 20000u;

void WatermapSimulation(World& world, uint32_t seed)
{
   BOOST_LOG_TRIVIAL(info) << "Watermap simulation start";

   const WaterMapArrayType& watermap = world.GetWaterMapData();
   const OceanArrayType&    ocean    = world.GetOceanData();

   WatermapExecute(world, NUM_SAMPLES, seed);

   world.SetThreshold(WaterThreshold::Creek,
                      FindThresholdF(watermap, 0.05f, &ocean));
   world.SetThreshold(WaterThreshold::River,
                      FindThresholdF(watermap, 0.02f, &ocean));
   world.SetThreshold(WaterThreshold::MainRiver,
                      FindThresholdF(watermap, 0.007f, &ocean));

   BOOST_LOG_TRIVIAL(info) << "Watermap simulation finish";
}

static void Droplet(World& world, uint32_t x, uint32_t y, float q)
{
   if (q < 0)
   {
      return;
   }

   const ElevationArrayType& elevation = world.GetElevationData();
   WaterMapArrayType&        watermap  = world.GetWaterMapData();

   std::vector<std::pair<uint32_t, uint32_t>>            tilesAround;
   std::vector<std::tuple<uint32_t, uint32_t, uint32_t>> lowers;

   float    posElev   = elevation[y][x] + watermap[y][x];
   float    minLower  = std::numeric_limits<float>::max();
   uint32_t totLowers = 0;

   world.GetTilesAround(tilesAround, x, y);

   for (const auto& p : tilesAround)
   {
      uint32_t px = p.first;
      uint32_t py = p.second;
      float    e  = elevation[py][px] + watermap[py][px];

      if (e < posElev)
      {
         uint32_t dq = static_cast<uint32_t>(posElev - e) << 2;
         if (e < minLower)
         {
            minLower = e;
            if (dq == 0)
            {
               dq = 1;
            }
         }
         lowers.push_back({dq, px, py});
         totLowers += dq;
      }
   }

   if (!lowers.empty())
   {
      float f = q / static_cast<float>(totLowers);

      for (const auto& t : lowers)
      {
         uint32_t s;
         uint32_t px;
         uint32_t py;

         std::tie(s, px, py) = t;

         if (!world.IsOcean(px, py))
         {
            float ql    = f * s;
            bool  going = ql > 0.05f;

            watermap[py][px] += ql;

            if (going)
            {
               Droplet(world, px, py, ql);
            }
         }
      }
   }
   else
   {
      watermap[y][x] += q;
   }
}

static void WatermapExecute(World& world, uint32_t numSamples, uint32_t seed)
{
   BOOST_LOG_TRIVIAL(debug) << "Seed: " << seed;

   std::mt19937                                      generator(seed);
   boost::random::uniform_int_distribution<uint32_t> distribution;

   const uint32_t width  = world.width();
   const uint32_t height = world.height();

   const PrecipitationArrayType& precipitations = world.GetPrecipitationData();
   WaterMapArrayType&            watermap       = world.GetWaterMapData();
   watermap.resize(boost::extents[height][width]);

   std::fill(watermap.data(), watermap.data() + watermap.num_elements(), 0.0f);

   /*
    * This indirectly calls the global RNG. We want different implementations of
    * watermap and internally called functions (especially RandomLand) to show
    * the same RNG behavior and not contaminate the state of the global RNG
    * should anyone else happen to rely on it.
    */

   std::vector<Point> landSamples;

   world.GetRandomLand(landSamples, numSamples, distribution(generator));

   for (uint32_t i = 0; i < landSamples.size(); i++)
   {
      uint32_t x = landSamples[i].first;
      uint32_t y = landSamples[i].second;
      float    q = precipitations[y][x];

      if (q > 0)
      {
         Droplet(world, x, y, q);
      }
   }
}

} // namespace WorldEngine

ext: cpp

max_stars_repo_name: dpaulat/worldengine-cpp

max_issues_repo_name: dpaulat/worldengine-cpp

max_forks_repo_name: dpaulat/worldengine-cpp


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

/* Author: Sachin Chitta */

#include <moveit/kinematics_metrics/kinematics_metrics.h>
#include <Eigen/Eigenvalues>
#include <boost/math/constants/constants.hpp>

namespace kinematics_metrics
{
double KinematicsMetrics::getJointLimitsPenalty(const robot_state::RobotState &state,
                                                const robot_model::JointModelGroup *joint_model_group) const
{
  if (fabs(penalty_multiplier_) <= boost::math::tools::epsilon<double>())
    return 1.0;
  double joint_limits_multiplier(1.0);
  const std::vector<const robot_model::JointModel *> &joint_model_vector = joint_model_group->getJointModels();
  for (std::size_t i = 0; i < joint_model_vector.size(); ++i)
  {
    if (joint_model_vector[i]->getType() == robot_model::JointModel::REVOLUTE)
    {
      const robot_model::RevoluteJointModel *revolute_model =
          static_cast<const robot_model::RevoluteJointModel *>(joint_model_vector[i]);
      if (revolute_model->isContinuous())
        continue;
    }
    if (joint_model_vector[i]->getType() == robot_model::JointModel::PLANAR)
    {
      const robot_model::JointModel::Bounds &bounds = joint_model_vector[i]->getVariableBounds();
      if (bounds[0].min_position_ == -std::numeric_limits<double>::max() ||
          bounds[0].max_position_ == std::numeric_limits<double>::max() ||
          bounds[1].min_position_ == -std::numeric_limits<double>::max() ||
          bounds[1].max_position_ == std::numeric_limits<double>::max() ||
          bounds[2].min_position_ == -boost::math::constants::pi<double>() ||
          bounds[2].max_position_ == boost::math::constants::pi<double>())
        continue;
    }
    if (joint_model_vector[i]->getType() == robot_model::JointModel::FLOATING)
    {
      // Joint limits are not well-defined for floating joints
      continue;
    }
    const double *joint_values = state.getJointPositions(joint_model_vector[i]);
    const robot_model::JointModel::Bounds &bounds = joint_model_vector[i]->getVariableBounds();
    std::vector<double> lower_bounds, upper_bounds;
    for (std::size_t j = 0; j < bounds.size(); ++j)
    {
      lower_bounds.push_back(bounds[j].min_position_);
      upper_bounds.push_back(bounds[j].max_position_);
    }
    double lower_bound_distance = joint_model_vector[i]->distance(joint_values, &lower_bounds[0]);
    double upper_bound_distance = joint_model_vector[i]->distance(joint_values, &upper_bounds[0]);
    double range = lower_bound_distance + upper_bound_distance;
    if (range <= boost::math::tools::epsilon<double>())
      continue;
    joint_limits_multiplier *= (lower_bound_distance * upper_bound_distance / (range * range));
  }
  return (1.0 - exp(-penalty_multiplier_ * joint_limits_multiplier));
}

bool KinematicsMetrics::getManipulabilityIndex(const robot_state::RobotState &state, const std::string &group_name,
                                               double &manipulability_index, bool translation) const
{
  const robot_model::JointModelGroup *joint_model_group = robot_model_->getJointModelGroup(group_name);
  if (joint_model_group)
    return getManipulabilityIndex(state, joint_model_group, manipulability_index, translation);
  else
    return false;
}

bool KinematicsMetrics::getManipulabilityIndex(const robot_state::RobotState &state,
                                               const robot_model::JointModelGroup *joint_model_group,
                                               double &manipulability_index, bool translation) const
{
  // state.getJacobian() only works for chain groups.
  if (!joint_model_group->isChain())
  {
    return false;
  }

  Eigen::MatrixXd jacobian = state.getJacobian(joint_model_group);
  // Get joint limits penalty
  double penalty = getJointLimitsPenalty(state, joint_model_group);
  if (translation)
  {
    if (jacobian.cols() < 6)
    {
      Eigen::JacobiSVD<Eigen::MatrixXd> svdsolver(jacobian.topLeftCorner(3, jacobian.cols()));
      Eigen::MatrixXd singular_values = svdsolver.singularValues();
      manipulability_index = 1.0;
      for (unsigned int i = 0; i < singular_values.rows(); ++i)
      {
        logDebug("moveit.kin_metrics: Singular value: %d %f", i, singular_values(i, 0));
        manipulability_index *= singular_values(i, 0);
      }
      // Get manipulability index
      manipulability_index = penalty * manipulability_index;
    }
    else
    {
      Eigen::MatrixXd jacobian_2 = jacobian.topLeftCorner(3, jacobian.cols());
      Eigen::MatrixXd matrix = jacobian_2 * jacobian_2.transpose();
      // Get manipulability index
      manipulability_index = penalty * sqrt(matrix.determinant());
    }
  }
  else
  {
    if (jacobian.cols() < 6)
    {
      Eigen::JacobiSVD<Eigen::MatrixXd> svdsolver(jacobian);
      Eigen::MatrixXd singular_values = svdsolver.singularValues();
      manipulability_index = 1.0;
      for (unsigned int i = 0; i < singular_values.rows(); ++i)
      {
        logDebug("moveit.kin_metrics: Singular value: %d %f", i, singular_values(i, 0));
        manipulability_index *= singular_values(i, 0);
      }
      // Get manipulability index
      manipulability_index = penalty * manipulability_index;
    }
    else
    {
      Eigen::MatrixXd matrix = jacobian * jacobian.transpose();
      // Get manipulability index
      manipulability_index = penalty * sqrt(matrix.determinant());
    }
  }
  return true;
}

bool KinematicsMetrics::getManipulabilityEllipsoid(const robot_state::RobotState &state, const std::string &group_name,
                                                   Eigen::MatrixXcd &eigen_values,
                                                   Eigen::MatrixXcd &eigen_vectors) const
{
  const robot_model::JointModelGroup *joint_model_group = robot_model_->getJointModelGroup(group_name);
  if (joint_model_group)
    return getManipulabilityEllipsoid(state, joint_model_group, eigen_values, eigen_vectors);
  else
    return false;
}

bool KinematicsMetrics::getManipulabilityEllipsoid(const robot_state::RobotState &state,
                                                   const robot_model::JointModelGroup *joint_model_group,
                                                   Eigen::MatrixXcd &eigen_values,
                                                   Eigen::MatrixXcd &eigen_vectors) const
{
  // state.getJacobian() only works for chain groups.
  if (!joint_model_group->isChain())
  {
    return false;
  }

  Eigen::MatrixXd jacobian = state.getJacobian(joint_model_group);
  Eigen::MatrixXd matrix = jacobian * jacobian.transpose();
  Eigen::EigenSolver<Eigen::MatrixXd> eigensolver(matrix.block(0, 0, 3, 3));
  eigen_values = eigensolver.eigenvalues();
  eigen_vectors = eigensolver.eigenvectors();
  return true;
}

bool KinematicsMetrics::getManipulability(const robot_state::RobotState &state, const std::string &group_name,
                                          double &manipulability, bool translation) const
{
  const robot_model::JointModelGroup *joint_model_group = robot_model_->getJointModelGroup(group_name);
  if (joint_model_group)
    return getManipulability(state, joint_model_group, manipulability, translation);
  else
    return false;
}

bool KinematicsMetrics::getManipulability(const robot_state::RobotState &state,
                                          const robot_model::JointModelGroup *joint_model_group, double &manipulability,
                                          bool translation) const
{
  // state.getJacobian() only works for chain groups.
  if (!joint_model_group->isChain())
  {
    return false;
  }
  // Get joint limits penalty
  double penalty = getJointLimitsPenalty(state, joint_model_group);
  if (translation)
  {
    Eigen::MatrixXd jacobian = state.getJacobian(joint_model_group);
    Eigen::JacobiSVD<Eigen::MatrixXd> svdsolver(jacobian.topLeftCorner(3, jacobian.cols()));
    Eigen::MatrixXd singular_values = svdsolver.singularValues();
    for (int i = 0; i < singular_values.rows(); ++i)
      logDebug("moveit.kin_metrics: Singular value: %d %f", i, singular_values(i, 0));
    manipulability = penalty * singular_values.minCoeff() / singular_values.maxCoeff();
  }
  else
  {
    Eigen::MatrixXd jacobian = state.getJacobian(joint_model_group);
    Eigen::JacobiSVD<Eigen::MatrixXd> svdsolver(jacobian);
    Eigen::MatrixXd singular_values = svdsolver.singularValues();
    for (int i = 0; i < singular_values.rows(); ++i)
      logDebug("moveit.kin_metrics: Singular value: %d %f", i, singular_values(i, 0));
    manipulability = penalty * singular_values.minCoeff() / singular_values.maxCoeff();
  }
  return true;
}

}  // namespace

max_stars_repo_name: KenmeiFusamae/moveit

max_issues_repo_name: KenmeiFusamae/moveit

max_forks_repo_name: KenmeiFusamae/moveit


text:
/**
 * BCL to FASTQ file converter
 * Copyright (c) 2007-2017 Illumina, Inc.
 *
 * This software is covered by the accompanying EULA
 * and certain third party copyright/licenses, and any user of this
 * source file is bound by the terms therein.
 *
 * \file BclFile.cpp
 *
 * \brief Implementation of BCL file.
 *
 * \author Marek Balint
 * \author Mauricio Varea
 * \author Aaron Day
 */

#include "io/GzipDecompressor.hh"
#include "data/BclFile.hh"
#include "common/Debug.hh"
#include "common/Logger.hh"
#include "common/SystemCompatibility.hh"

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

namespace bcl2fastq {
namespace data {

BclFile::BclFile(const common::RawDataBuffer& data,
                 io::GzipDecompressor&        decompressor,
                 bool                         ignoreErrors,
                 bool                         parseHeader,
                 bool                         resetDecompressor, /* = true */
                 common::ClustersCount        defaultClustersCount /*= 0*/)
: FileReaderBase(data,
                 ignoreErrors)
, BinaryFileReader(data,
                   ignoreErrors,
                   defaultClustersCount)
, gzipDecompressor_(&decompressor)
, isCompressed_(data.path_.extension() == ".gz" || data.path_.extension() == ".bgzf")
{
    if (resetDecompressor)
    {
        gzipDecompressor_->flush();
        gzipDecompressor_->reset();
    }

    static const std::string errMsgBegin = "Ignoring file open failure for bcl file '";

    try
    {
        bool headerRead = false;
        if (parseHeader)
        {
            headerRead = true;
            if (!readHeader(clustersCount_))
            {
                return;
            }

            /// \todo Refactoring: Do not determine endianness in runtime, let CMake to do it.
            if (!common::isLittleEndian())
            {
                clustersCount_ = common::swapEndian(clustersCount_);
            }
        }

        if (headerRead) {
            BCL2FASTQ_LOG(common::LogLevel::INFO)
                << "Opened BCL file '" << this->getPath().string() << "' with "
                << clustersCount_ << " clusters" << std::endl;
        }
    }
    CATCH_AND_IGNORE(std::ios_base::failure, errMsgBegin)
    CATCH_AND_IGNORE(io::ZlibError, errMsgBegin)
    CATCH_AND_IGNORE_ALL(errMsgBegin)
}

bool BclFile::getAndVerifyFileName(const boost::filesystem::path& inputDir,
                                   common::LaneNumber             laneNumber,
                                   common::CycleNumber            cycleNumber,
                                   bool                           ignoreErrors,
                                   boost::filesystem::path&       fileName)
{
    fileName = boost::filesystem::path(
        inputDir
        /
        boost::filesystem::path((boost::format("L%03d") % laneNumber).str())
        /
        boost::filesystem::path((boost::format("%04d.bcl.bgzf") % cycleNumber).str())
    );

    if (!boost::filesystem::exists(fileName))
    {
        std::string errStr("Unable to find BCL file: '" + fileName.string() + "'");
        if (ignoreErrors)
        {
            BCL2FASTQ_LOG(common::LogLevel::WARNING) << errStr << std::endl;
            return false;
        }
        else
        {
            BOOST_THROW_EXCEPTION(common::IoError(ENOENT, errStr));
        }

        fileName = boost::filesystem::path();
    }

    return true;
}

bool BclFile::getAndVerifyFileName(const boost::filesystem::path& inputDir,
                                   common::LaneNumber             laneNumber,
                                   common::TileNumber             tileNumber,
                                   common::CycleNumber            cycleNumber,
                                   bool                           ignoreErrors,
                                   boost::filesystem::path&       fileName)
{
    std::string sample = (boost::format("s_%d_%d") % laneNumber % tileNumber).str();

    fileName = boost::filesystem::path(
        inputDir
        /
        boost::filesystem::path((boost::format("L%03d") % laneNumber).str())
        /
        boost::filesystem::path((boost::format("C%d.1") % cycleNumber).str())
        /
        boost::filesystem::path((boost::format("%s.bcl.gz") % sample).str())
    );

    if (!boost::filesystem::exists(fileName))
    {
        fileName.replace_extension();

        if (!boost::filesystem::exists(fileName))
        {
            std::string errStr("Unable to find BCL file for '" + sample + "' in: " + fileName.remove_filename().string());
            if (ignoreErrors)
            {
                BCL2FASTQ_LOG(common::LogLevel::WARNING) << errStr << std::endl;
                return false;
            }
            else
            {
                BOOST_THROW_EXCEPTION(common::IoError(ENOENT, errStr));
            }

            fileName = boost::filesystem::path();
        }
    }

    return true;
}

std::streamsize BclFile::read(std::istream &is, char *buffer, std::streamsize size)
{
    if( isCompressed_ )
    {
        auto ret = gzipDecompressor_->read(is, buffer, size);
        return ret;
    }
    else
    {
        if (!is.read(buffer, size)) return -1;

        return is.gcount();
    }

    return 0;
}

std::streamsize BclFile::read(char* targetBuffer, std::streamsize targetSize)
{
    static const std::string errMsgBegin = "Ignoring read failure for bcl file '";

    if (!validateCondition(this->isOpen(), "File is not open")) { return 0; }

    std::streamsize bytesRead = -1;
    try
    {
        bytesRead = read(istr_, targetBuffer, targetSize);
        while ((bytesRead < targetSize) && !istr_.eof() && bytesRead != -1)
        {
            std::streamsize bytesRead2 = read(istr_, targetBuffer+bytesRead, targetSize-bytesRead);
            if (bytesRead2 != -1)
            {
                bytesRead += bytesRead2;
            } else {
                break;
            }
        }
    }
    CATCH_AND_IGNORE(io::ZlibError, errMsgBegin)
    CATCH_AND_IGNORE(common::IoError, errMsgBegin)
    CATCH_AND_IGNORE(std::ios_base::failure, errMsgBegin)
    CATCH_AND_IGNORE_ALL(errMsgBegin)

    return bytesRead;
}

bool BclFile::seek(std::streamsize compressedOffset, std::streamsize uncompressedOffset)
{
    if (!this->isOpen() && ignoreErrors_) {
        return false;
    }

    static const std::string errMsgBegin = "Ignoring seek failure for: '";

    bool success = false;
    try
    {
        istr_.seekg(compressedOffset);
        istr_.sync();
        std::vector<char> targetBuffer(uncompressedOffset);

        gzipDecompressor_->reset();
        gzipDecompressor_->flush();
        std::streamsize bytesRead = gzipDecompressor_->read(istr_, &*(targetBuffer.begin()), uncompressedOffset);

        int errnum = errno;
        success = (bytesRead == uncompressedOffset);
        if (!success)
        {
            BOOST_THROW_EXCEPTION(common::InputDataError(errnum, (boost::format("Unable to seek to given position of BCL file '%s': compressed_offset=%d uncompressed_offset=%d bytes_read=%d") % this->getPath().string() % compressedOffset % uncompressedOffset % bytesRead).str()));
        }
    }
    CATCH_AND_IGNORE(common::IoError, errMsgBegin)
    CATCH_AND_IGNORE_ALL(errMsgBegin)

    return success;
}

} // namespace data
} // namespace bcl2fastq


text:
/*=============================================================================
    Copyright (c) 2001-2011 Joel de Guzman

    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#if !defined(BOOST_SPIRIT_SYMBOLS_MARCH_11_2007_1055AM)
#define BOOST_SPIRIT_SYMBOLS_MARCH_11_2007_1055AM

#if defined(_MSC_VER)
#pragma once
#endif

#include <boost/spirit/home/qi/domain.hpp>
#include <boost/spirit/home/qi/skip_over.hpp>
#include <boost/spirit/home/qi/string/tst.hpp>
#include <boost/spirit/home/qi/reference.hpp>
#include <boost/spirit/home/qi/meta_compiler.hpp>
#include <boost/spirit/home/qi/detail/assign_to.hpp>
#include <boost/spirit/home/qi/parser.hpp>
#include <boost/spirit/home/support/detail/get_encoding.hpp>
#include <boost/spirit/home/support/modify.hpp>
#include <boost/spirit/home/support/info.hpp>
#include <boost/spirit/home/support/unused.hpp>
#include <boost/spirit/home/support/string_traits.hpp>

#include <boost/fusion/include/at.hpp>
#include <boost/range.hpp>
#include <boost/type_traits/add_reference.hpp>
#include <boost/shared_ptr.hpp>

#if defined(BOOST_MSVC)
# pragma warning(push)
# pragma warning(disable: 4355) // 'this' : used in base member initializer list warning
#endif

namespace boost { namespace spirit { namespace qi
{
    template <
        typename Char = char
      , typename T = unused_type
      , typename Lookup = tst<Char, T>
      , typename Filter = tst_pass_through>
    struct symbols
      : proto::extends<
            typename proto::terminal<
                reference<symbols<Char, T, Lookup, Filter> >
            >::type
          , symbols<Char, T, Lookup, Filter>
        >
      , primitive_parser<symbols<Char, T, Lookup, Filter> >
    {
        typedef Char char_type; // the character type
        typedef T value_type; // the value associated with each entry
        typedef symbols<Char, T, Lookup, Filter> this_type;
        typedef reference<this_type> reference_;
        typedef typename proto::terminal<reference_>::type terminal;
        typedef proto::extends<terminal, this_type> base_type;

        template <typename Context, typename Iterator>
        struct attribute
        {
            typedef value_type type;
        };

        symbols(std::string const& name = "symbols")
          : base_type(terminal::make(reference_(*this)))
          , add(*this)
          , remove(*this)
          , lookup(new Lookup())
          , name_(name)
        {
        }

        symbols(symbols const& syms)
          : base_type(terminal::make(reference_(*this)))
          , add(*this)
          , remove(*this)
          , lookup(syms.lookup)
          , name_(syms.name_)
        {
        }

        template <typename Filter_>
        symbols(symbols<Char, T, Lookup, Filter_> const& syms)
          : base_type(terminal::make(reference_(*this)))
          , add(*this)
          , remove(*this)
          , lookup(syms.lookup)
          , name_(syms.name_)
        {
        }

        template <typename Symbols>
        symbols(Symbols const& syms, std::string const& name = "symbols")
          : base_type(terminal::make(reference_(*this)))
          , add(*this)
          , remove(*this)
          , lookup(new Lookup())
          , name_(name)
        {
            typename range_const_iterator<Symbols>::type si = boost::begin(syms);
            while (si != boost::end(syms))
                add(*si++);
        }

        template <typename Symbols, typename Data>
        symbols(Symbols const& syms, Data const& data
              , std::string const& name = "symbols")
          : base_type(terminal::make(reference_(*this)))
          , add(*this)
          , remove(*this)
          , lookup(new Lookup())
          , name_(name)
        {
            typename range_const_iterator<Symbols>::type si = boost::begin(syms);
            typename range_const_iterator<Data>::type di = boost::begin(data);
            while (si != boost::end(syms))
                add(*si++, *di++);
        }

        symbols&
        operator=(symbols const& rhs)
        {
            name_ = rhs.name_;
            *lookup = *rhs.lookup;
            return *this;
        }

        template <typename Filter_>
        symbols&
        operator=(symbols<Char, T, Lookup, Filter_> const& rhs)
        {
            name_ = rhs.name_;
            *lookup = *rhs.lookup;
            return *this;
        }

        void clear()
        {
            lookup->clear();
        }

        struct adder;
        struct remover;

        template <typename Str>
        adder const&
        operator=(Str const& str)
        {
            lookup->clear();
            return add(str);
        }

        template <typename Str>
        friend adder const&
        operator+=(symbols& sym, Str const& str)
        {
            return sym.add(str);
        }

        template <typename Str>
        friend remover const&
        operator-=(symbols& sym, Str const& str)
        {
            return sym.remove(str);
        }

#if defined(BOOST_NO_RVALUE_REFERENCES)
        // non-const version needed to suppress proto's += kicking in
        template <typename Str>
        friend adder const&
        operator+=(symbols& sym, Str& str)
        {
            return sym.add(str);
        }

        // non-const version needed to suppress proto's -= kicking in
        template <typename Str>
        friend remover const&
        operator-=(symbols& sym, Str& str)
        {
            return sym.remove(str);
        }
#else
        // for rvalue references
        template <typename Str>
        friend adder const&
        operator+=(symbols& sym, Str&& str)
        {
            return sym.add(str);
        }

        // for rvalue references
        template <typename Str>
        friend remover const&
        operator-=(symbols& sym, Str&& str)
        {
            return sym.remove(str);
        }
#endif
        template <typename F>
        void for_each(F f) const
        {
            lookup->for_each(f);
        }

        template <typename Str>
        value_type& at(Str const& str)
        {
            return *lookup->add(traits::get_begin<Char>(str)
                , traits::get_end<Char>(str), T());
        }

        template <typename Iterator>
        value_type* prefix_find(Iterator& first, Iterator const& last)
        {
            return lookup->find(first, last, Filter());
        }

        template <typename Iterator>
        value_type const* prefix_find(Iterator& first, Iterator const& last) const
        {
            return lookup->find(first, last, Filter());
        }

        template <typename Str>
        value_type* find(Str const& str)
        {
            return find_impl(traits::get_begin<Char>(str)
                , traits::get_end<Char>(str));
        }

        template <typename Str>
        value_type const* find(Str const& str) const
        {
            return find_impl(traits::get_begin<Char>(str)
                , traits::get_end<Char>(str));
        }

private:
        template <typename Iterator>
        value_type* find_impl(Iterator begin, Iterator end)
        {
            value_type* r = lookup->find(begin, end, Filter());
            return begin == end ? r : 0;
        }

        template <typename Iterator>
        value_type const* find_impl(Iterator begin, Iterator end) const
        {
            value_type const* r = lookup->find(begin, end, Filter());
            return begin == end ? r : 0;
        }

public:
        template <typename Iterator, typename Context
          , typename Skipper, typename Attribute>
        bool parse(Iterator& first, Iterator const& last
          , Context& /*context*/, Skipper const& skipper, Attribute& attr) const
        {
            qi::skip_over(first, last, skipper);

            if (value_type* val_ptr
                = lookup->find(first, last, Filter()))
            {
                spirit::traits::assign_to(*val_ptr, attr);
                return true;
            }
            return false;
        }

        template <typename Context>
        info what(Context& /*context*/) const
        {
            return info(name_);
        }

        void name(std::string const &str)
        {
            name_ = str;
        }
        std::string const &name() const
        {
            return name_;
        }

        struct adder
        {
            template <typename, typename = unused_type, typename = unused_type>
            struct result { typedef adder const& type; };

            adder(symbols& sym)
              : sym(sym)
            {
            }

            template <typename Iterator>
            adder const&
            operator()(Iterator const& first, Iterator const& last, T const& val) const
            {
                sym.lookup->add(first, last, val);
                return *this;
            }

            template <typename Str>
            adder const&
            operator()(Str const& s, T const& val = T()) const
            {
                sym.lookup->add(traits::get_begin<Char>(s)
                  , traits::get_end<Char>(s), val);
                return *this;
            }

            template <typename Str>
            adder const&
            operator,(Str const& s) const
            {
                sym.lookup->add(traits::get_begin<Char>(s)
                  , traits::get_end<Char>(s), T());
                return *this;
            }

            symbols& sym;

        private:
            // silence MSVC warning C4512: assignment operator could not be generated
            adder& operator= (adder const&);
        };

        struct remover
        {
            template <typename, typename = unused_type, typename = unused_type>
            struct result { typedef remover const& type; };

            remover(symbols& sym)
              : sym(sym)
            {
            }

            template <typename Iterator>
            remover const&
            operator()(Iterator const& first, Iterator const& last) const
            {
                sym.lookup->remove(first, last);
                return *this;
            }

            template <typename Str>
            remover const&
            operator()(Str const& s) const
            {
                sym.lookup->remove(traits::get_begin<Char>(s)
                  , traits::get_end<Char>(s));
                return *this;
            }

            template <typename Str>
            remover const&
            operator,(Str const& s) const
            {
                sym.lookup->remove(traits::get_begin<Char>(s)
                  , traits::get_end<Char>(s));
                return *this;
            }

            symbols& sym;

        private:
            // silence MSVC warning C4512: assignment operator could not be generated
            remover& operator= (remover const&);
        };

        adder add;
        remover remove;
        shared_ptr<Lookup> lookup;
        std::string name_;
    };

    ///////////////////////////////////////////////////////////////////////////
    // Parser generators: make_xxx function (objects)
    ///////////////////////////////////////////////////////////////////////////
    template <typename Char, typename T, typename Lookup
      , typename Filter, typename Modifiers>
    struct make_primitive<reference<symbols<Char, T, Lookup, Filter> >, Modifiers>
    {
        template <typename CharEncoding>
        struct no_case_filter
        {
            Char operator()(Char ch) const
            {
                return static_cast<Char>(CharEncoding::tolower(ch));
            }
        };

        typedef has_modifier<Modifiers, tag::char_code_base<tag::no_case> > no_case;
        typedef reference<symbols<Char, T, Lookup, Filter> > reference_;
        typedef no_case_filter<
            typename spirit::detail::get_encoding_with_case<
                Modifiers
              , char_encoding::standard
              , no_case::value>::type>
        nc_filter;

        typedef typename mpl::if_<
            no_case
          , symbols<Char, T, Lookup, nc_filter>
          , reference_>::type
        result_type;

        result_type operator()(reference_ ref, unused_type) const
        {
            return result_type(ref.ref.get());
        }
    };
}}}

namespace boost { namespace spirit { namespace traits
{
    ///////////////////////////////////////////////////////////////////////////
    template <typename Char, typename T, typename Lookup, typename Filter
      , typename Attr, typename Context, typename Iterator>
    struct handles_container<qi::symbols<Char, T, Lookup, Filter>, Attr, Context, Iterator>
      : traits::is_container<Attr> {};
}}}

#if defined(BOOST_MSVC)
# pragma warning(pop)
#endif

#endif

ext: hpp

max_stars_repo_name: randolphwong/mcsema

max_issues_repo_name: randolphwong/mcsema

max_forks_repo_name: randolphwong/mcsema


text:
/*=============================================================================
    Copyright (c) 2001-2007 Joel de Guzman
    Copyright (c) 2004 Daniel Wallin

    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)
==============================================================================*/
#ifndef PHOENIX_SCOPE_DETAIL_LOCAL_VARIABLE_HPP
#define PHOENIX_SCOPE_DETAIL_LOCAL_VARIABLE_HPP

#include <boost/mpl/int.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/less.hpp>
#include <boost/mpl/size.hpp>
#include <boost/fusion/include/at.hpp>
#include <boost/fusion/include/value_at.hpp>
#include <boost/preprocessor/enum.hpp>
#include <boost/preprocessor/repeat.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/type_traits/is_reference.hpp>
#include <boost/spirit/home/phoenix/detail/local_reference.hpp>

#define PHOENIX_MAP_LOCAL_TEMPLATE_PARAM(z, n, data) \
    typename T##n = unused<n>

#define PHOENIX_MAP_LOCAL_DISPATCH(z, n, data)  \
    typedef char(&result##n)[n+2];              \
    static result##n get(T##n*);

namespace boost { namespace phoenix
{
    template <typename Env, typename OuterEnv, typename Locals, typename Map>
    struct scoped_environment;

    namespace detail
    {
        template <typename Env>
        struct initialize_local
        {
            template <class F>
            struct result;

            template <class F, class Actor>
            struct result<F(Actor)>
            {
                typedef typename remove_reference<Actor>::type actor_type;
                typedef typename actor_type::template result<Env>::type type;
            };

            initialize_local(Env const& env)
                : env(env) {}

            template <typename Actor>
            typename result<initialize_local(Actor)>::type
            operator()(Actor const& actor) const
            {
                return actor.eval(env);
            }

            Env const& env;
        };

        template <typename T>
        struct is_scoped_environment : mpl::false_ {};

        template <typename Env, typename OuterEnv, typename Locals, typename Map>
        struct is_scoped_environment<scoped_environment<Env, OuterEnv, Locals, Map> >
            : mpl::true_ {};

        template <int N>
        struct unused;

        template <BOOST_PP_ENUM(
            PHOENIX_LOCAL_LIMIT, PHOENIX_MAP_LOCAL_TEMPLATE_PARAM, _)>
        struct map_local_index_to_tuple
        {
            typedef char(&not_found)[1];
            static not_found get(...);

            BOOST_PP_REPEAT(PHOENIX_LOCAL_LIMIT, PHOENIX_MAP_LOCAL_DISPATCH, _)
        };

        template<typename T>
        T* generate_pointer();

        template <typename Map, typename Tag>
        struct get_index
        {
            BOOST_STATIC_CONSTANT(int,
                value = (
                    static_cast<int>((sizeof(Map::get(generate_pointer<Tag>()))) / sizeof(char)) - 2
                ));

            // if value == -1, Tag is not found
            typedef mpl::int_<value> type;
        };

        template <typename Local, typename Env>
        struct apply_local;

        template <typename Local, typename Env>
        struct outer_local
        {
            typedef typename
                apply_local<Local, typename Env::outer_env_type>::type
            type;
        };

        template <typename T>
        struct local_reference_identity
        {
            typedef local_reference<T> type;
        };

        template <typename Locals, typename Index>
        struct make_local_reference
        {
            typedef typename
                fusion::result_of::value_at<Locals, Index>::type
            value_at;

            typedef typename
                mpl::eval_if<
                    is_reference<value_at>
                  , mpl::identity<value_at>
                  , local_reference_identity<value_at>
                >::type
            type;
        };

        template <typename Locals, typename Index>
        struct get_local_or_void
        {
            typedef typename
                mpl::eval_if<
                    mpl::less<Index, mpl::size<Locals> >
                  , make_local_reference<Locals, Index>
                  , mpl::identity<fusion::void_>
                >::type
            type;
        };

        template <typename Local, typename Env, typename Index>
        struct get_local_from_index
        {
            typedef typename
                mpl::eval_if<
                    mpl::equal_to<Index, mpl::int_<-1> >
                  , outer_local<Local, Env>
                  , get_local_or_void<typename Env::locals_type, Index>
                >::type
            type;
        };

        template <typename Local, typename Env>
        struct get_local
        {
            typedef typename
                get_index<
                    typename Env::map_type, typename Local::key_type>::type
            index_type;

            typedef typename
                get_local_from_index<Local, Env, index_type>::type
            type;
        };

        template <typename Local, typename Env>
        struct apply_local
        {
            // $$$ TODO: static assert that Env is a scoped_environment $$$
            typedef typename get_local<Local, Env>::type type;
        };

        template <typename Key>
        struct eval_local
        {
            template <typename RT, typename Env, typename Index>
            static RT
            get(Env const& env, Index, mpl::false_)
            {
                return RT(fusion::at<Index>(env.locals));
            }

            template <typename RT, typename Env, typename Index>
            static RT
            get(Env const& env, Index index, mpl::true_)
            {
                typedef typename
                    get_index<typename Env::outer_env_type::map_type, Key>::type
                index_type;

                return get<RT>(
                    env.outer_env
                  , index_type()
                  , mpl::equal_to<index_type, mpl::int_<-1> >());
            }

            template <typename RT, typename Env, typename Index>
            static RT
            get(Env const& env, Index index)
            {
                return get<RT>(
                    env
                  , index
                  , mpl::equal_to<Index, mpl::int_<-1> >());
            }
        };
    }
}}

#undef PHOENIX_MAP_LOCAL_TEMPLATE_PARAM
#undef PHOENIX_MAP_LOCAL_DISPATCH
#endif

max_stars_repo_name: jaredhoberock/gotham

max_issues_repo_name: jaredhoberock/gotham

max_forks_repo_name: jaredhoberock/gotham


text:
#include "nt_string_view.hpp"

#include <ranges>

#define BOOST_UT_DISABLE_MODULE
#include <boost/ut.hpp>

namespace ut = boost::ut;

int main() {

  using namespace boost::ut::literals;
  using namespace boost::ut::operators::terse;
  using namespace std::string_view_literals;
  using namespace std::string_literals;
  using ntsv::nt_string_view;
  using namespace ntsv::literals;

  "null_terminated_string_view"_test = [] {

    // ほぼstring_viewの性質を受け継ぐ
    static_assert(std::is_trivially_copyable_v<nt_string_view>);
    static_assert(sizeof(nt_string_view) == sizeof(std::string_view));
    // 右辺値から構築できない
    static_assert(not std::constructible_from<nt_string_view, std::string>);
    static_assert(std::constructible_from<nt_string_view, std::string&>);

    static_assert(std::three_way_comparable<nt_string_view>);
    static_assert(std::equality_comparable<nt_string_view>);

    nt_string_view str1{"test string"};
    ut::expect(str1.str_view().size() == 11);
    ut::expect(str1.str_view() == "test string"sv);

    nt_string_view str2{"test string"sv};
    ut::expect(str2.str_view().size() == 11);
    ut::expect(str2.str_view() == "test string"sv);

    const auto dyn_str = "test string"s;
    nt_string_view str3{dyn_str};
    ut::expect(str3.str_view().size() == 11);
    ut::expect(str3.str_view() == "test string"sv);

    ut::expect(str1 == "test string"_ntsv);
  };

  "string_view innner types"_test = [] {

    static_assert(std::same_as<nt_string_view::const_iterator, std::string_view::const_iterator>);
    static_assert(std::same_as<nt_string_view::const_pointer, std::string_view::const_pointer>);
    static_assert(std::same_as<nt_string_view::const_reference, std::string_view::const_reference>);
    static_assert(std::same_as<nt_string_view::const_reverse_iterator, std::string_view::const_reverse_iterator>);
    static_assert(std::same_as<nt_string_view::difference_type, std::string_view::difference_type>);
    static_assert(std::same_as<nt_string_view::iterator, std::string_view::iterator>);
    static_assert(std::same_as<nt_string_view::pointer, std::string_view::pointer>);
    static_assert(std::same_as<nt_string_view::reference, std::string_view::reference>);
    static_assert(std::same_as<nt_string_view::reverse_iterator, std::string_view::reverse_iterator>);
    static_assert(std::same_as<nt_string_view::size_type, std::string_view::size_type>);
    static_assert(std::same_as<nt_string_view::traits_type, std::string_view::traits_type>);
    static_assert(std::same_as<nt_string_view::value_type, std::string_view::value_type>);

  };

  "range-ness"_test = [] {
    static_assert(std::ranges::range<nt_string_view>);
    static_assert(std::ranges::range<const nt_string_view>);
    static_assert(std::ranges::contiguous_range<nt_string_view>);
    static_assert(std::ranges::contiguous_range<const nt_string_view>);
    static_assert(std::ranges::sized_range<nt_string_view>);
    static_assert(std::ranges::sized_range<const nt_string_view>);
    static_assert(std::ranges::borrowed_range<nt_string_view>);
    static_assert(std::ranges::borrowed_range<const nt_string_view>);
    static_assert(std::ranges::view<nt_string_view>);
    static_assert(std::ranges::viewable_range<nt_string_view>);
    static_assert(std::ranges::viewable_range<const nt_string_view>);
  };
}


text: #include <boost/fiber/algo/numa/work_stealing.hpp>

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text: #include <boost/mpl/aux_/preprocessed/msvc70/list.hpp>


text:
// Copyright (c) 2018-2020 Jean-Louis Leroy
// 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 <iostream>
#include <type_traits>

#include <yorel/yomm2.hpp>
#include <yorel/yomm2/runtime.hpp>

#define BOOST_TEST_MODULE yomm2
#include <boost/test/included/unit_test.hpp>

using yorel::yomm2::virtual_;

namespace states {

using std::string;

struct Animal {
    Animal(const Animal&) = delete;
    Animal() : name("wrong") { }
    virtual ~Animal() {}
    std::string name;
};

struct Dog : Animal {
    Dog(string n) {
        name = n;
    }
};

struct Cat : virtual Animal {
    Cat(string n) {
        name = n;
    }
};

YOMM2_CLASS(Animal);
YOMM2_CLASS(Dog, Animal);
YOMM2_CLASS(Cat, Animal);

YOMM2_DECLARE(string, name, (virtual_<const Animal&>));

YOMM2_DEFINE(string, name, (const Dog& dog)) {
    return "dog " + dog.name;
}

YOMM2_DEFINE(string, name, (const Cat& cat)) {
    return "cat " + cat.name;
}

BOOST_AUTO_TEST_CASE(states) {
    yorel::yomm2::update_methods();
    const Animal& dog= Dog("spot");
    BOOST_TEST("dog spot" == name(dog));
    const Animal& cat= Cat("felix");
    BOOST_TEST("cat felix" == name(cat));
}

}

namespace matrices {

struct matrix {
    virtual ~matrix() {}
};

struct dense_matrix : matrix {};
struct diagonal_matrix : matrix {};

enum Subtype {
    MATRIX, DIAGONAL,
    SCALAR_MATRIX, SCALAR_DIAGONAL, MATRIX_SCALAR, DIAGONAL_SCALAR,
    MATRIX_MATRIX, DIAGONAL_DIAGONAL
};

YOMM2_CLASS(matrix);
YOMM2_CLASS(dense_matrix, matrix);
YOMM2_CLASS(diagonal_matrix, matrix);

YOMM2_DECLARE(Subtype, times, (virtual_<const matrix&>, virtual_<const matrix&>));
YOMM2_DECLARE(Subtype, times, (double, virtual_<const matrix&>));
YOMM2_DECLARE(Subtype, times, (virtual_<const matrix&>, double));

YOMM2_DEFINE(Subtype, times, (const matrix&, const matrix&)) {
    return MATRIX_MATRIX;
}

YOMM2_DEFINE(Subtype, times, (const diagonal_matrix&, const diagonal_matrix&)) {
    return DIAGONAL_DIAGONAL;
}

YOMM2_DEFINE(Subtype, times, (double a, const matrix& m)) {
    return SCALAR_MATRIX;
}

YOMM2_DEFINE(Subtype, times, (double a, const diagonal_matrix& m)) {
    return SCALAR_DIAGONAL;
}

YOMM2_DEFINE(Subtype, times, (const diagonal_matrix& m, double a)) {
    return DIAGONAL_SCALAR;
}

YOMM2_DEFINE(Subtype, times, (const matrix& m, double a)) {
    return MATRIX_SCALAR;
}

YOMM2_DECLARE(int, times, (virtual_<matrix&&>, virtual_<matrix&&>));
YOMM2_DECLARE(int, times, (double, virtual_<matrix&&>));
YOMM2_DECLARE(int, times, (virtual_<matrix&&>, double));

YOMM2_DEFINE(int, times, (matrix&&, matrix&&)) {
    return -MATRIX_MATRIX;
}

YOMM2_DEFINE(int, times, (diagonal_matrix&&, diagonal_matrix&&)) {
    return -DIAGONAL_DIAGONAL;
}

YOMM2_DEFINE(int, times, (double a, matrix&& m)) {
    return -SCALAR_MATRIX;
}

YOMM2_DEFINE(int, times, (double a, diagonal_matrix&& m)) {
    return -SCALAR_DIAGONAL;
}

YOMM2_DEFINE(int, times, (diagonal_matrix&& m, double a)) {
    return -DIAGONAL_SCALAR;
}

YOMM2_DEFINE(int, times, (matrix&& m, double a)) {
    return -MATRIX_SCALAR;
}

YOMM2_DECLARE(Subtype, zero_ref, (virtual_<matrix&>));

YOMM2_DEFINE(Subtype, zero_ref, (dense_matrix& m)) {
    return MATRIX;
}

YOMM2_DEFINE(Subtype, zero_ref, (diagonal_matrix& m)) {
    return DIAGONAL;
}

YOMM2_DECLARE(Subtype, zero_ptr, (virtual_<matrix*>));

YOMM2_DEFINE(Subtype, zero_ptr, (dense_matrix* m)) {
    return MATRIX;
}

YOMM2_DEFINE(Subtype, zero_ptr, (diagonal_matrix* m)) {
    return DIAGONAL;
}

BOOST_AUTO_TEST_CASE(simple)
{
    yorel::yomm2::update_methods();

    {
        const matrix& dense = dense_matrix();
        const matrix& diag = diagonal_matrix();
        BOOST_TEST(times(dense, dense) == MATRIX_MATRIX);
        BOOST_TEST(times(diag, diag) == DIAGONAL_DIAGONAL);
        BOOST_TEST(times(diag, dense) == MATRIX_MATRIX);
        BOOST_TEST(times(2, dense) == SCALAR_MATRIX);
        BOOST_TEST(times(dense, 2) == MATRIX_SCALAR);
        BOOST_TEST(times(diag, 2) == DIAGONAL_SCALAR);
    }

    {
        BOOST_TEST(times(dense_matrix(), dense_matrix()) == -MATRIX_MATRIX);
        BOOST_TEST(times(diagonal_matrix(), diagonal_matrix()) == -DIAGONAL_DIAGONAL);
        BOOST_TEST(times(diagonal_matrix(), dense_matrix()) == -MATRIX_MATRIX);
        BOOST_TEST(times(2, dense_matrix()) == -SCALAR_MATRIX);
        BOOST_TEST(times(dense_matrix(), 2) == -MATRIX_SCALAR);
        BOOST_TEST(times(diagonal_matrix(), 2) == -DIAGONAL_SCALAR);
    }

    {
        dense_matrix dense;
        BOOST_TEST(zero_ref(dense) == MATRIX);
        BOOST_TEST(zero_ptr(&dense) == MATRIX);
        diagonal_matrix diagonal;
        BOOST_TEST(zero_ref(diagonal) == DIAGONAL);
        BOOST_TEST(zero_ptr(&diagonal) == DIAGONAL);
    }
}

}

namespace errors {

struct matrix {
    virtual ~matrix() {}
};

struct dense_matrix : matrix {};
struct diagonal_matrix : matrix {};

YOMM2_CLASS(matrix);
YOMM2_CLASS(dense_matrix, matrix);
YOMM2_CLASS(diagonal_matrix, matrix);

YOMM2_DECLARE(void, times, (virtual_<const matrix&>, virtual_<const matrix&>));

YOMM2_DEFINE(void, times, (const diagonal_matrix&, const matrix&)) {
}

YOMM2_DEFINE(void, times, (const matrix&, const diagonal_matrix&)) {
}

int error_code = -1;
YOMM2_TRACE(std::string method_name);

void test_handler(const yorel::yomm2::method_call_error& error) {
    error_code = error.code;
    YOMM2_TRACE(method_name = error.method_name);
}

BOOST_AUTO_TEST_CASE(error_handling)
{
    yorel::yomm2::update_methods();
    yorel::yomm2::set_method_call_error_handler(test_handler);
    times(dense_matrix(), dense_matrix());
    BOOST_TEST(error_code == yorel::yomm2::method_call_error::not_implemented);
    YOMM2_TRACE(
        BOOST_TEST(
            method_name == "times(virtual_<const matrix&>, virtual_<const matrix&>)"));
    YOMM2_TRACE(method_name = "");
    times(diagonal_matrix(), diagonal_matrix());
    BOOST_TEST(error_code == yorel::yomm2::method_call_error::ambiguous);
    YOMM2_TRACE(
        BOOST_TEST(
            method_name == "times(virtual_<const matrix&>, virtual_<const matrix&>)"));
}

}

namespace across_namespaces {

namespace animals {

class Animal {
  public:
    virtual ~Animal() {}
};

YOMM2_CLASS(Animal);

YOMM2_DECLARE(std::string, kick, (virtual_<const Animal&>));

}

namespace more_animals {

class Dog : public animals::Animal {};

YOMM2_CLASS(Dog, animals::Animal);

YOMM2_DEFINE(std::string, kick, (const Dog& dog)) {
  return "bark";
}

}

BOOST_AUTO_TEST_CASE(across_namespaces) {
    const animals::Animal& animal= more_animals::Dog();
    BOOST_TEST("bark" == kick(animal));
}

}

namespace refref {

struct Animal {
    virtual ~Animal() {}
    bool moved{false};
};

struct Dog : Animal {
};

struct Cat : virtual Animal {
};

YOMM2_CLASS(Animal);
YOMM2_CLASS(Dog, Animal);
YOMM2_CLASS(Cat, Animal);

YOMM2_DECLARE(void, test, (virtual_<Animal&&>));

YOMM2_DEFINE(void, test, (Dog&& dog)) {
    dog.moved = true;
}

YOMM2_DEFINE(void, test, (Cat&& cat)) {
    cat.moved = true;
}

BOOST_AUTO_TEST_CASE(states) {
    yorel::yomm2::update_methods();

    Dog dog;
    test(std::move(dog));
    BOOST_TEST(dog.moved);

    Cat cat;
    test(std::move(cat));
    BOOST_TEST(cat.moved);
}

}


text:
/*
 * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
 *
 * 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 <boost/foreach.hpp>
#include <set>

#include "hazelcast/client/cluster/impl/VectorClock.h"
#include "hazelcast/client/cluster/impl/ClusterDataSerializerHook.h"
#include "hazelcast/client/serialization/ObjectDataInput.h"

namespace hazelcast {
    namespace client {
        namespace cluster {
            namespace impl {

                VectorClock::VectorClock() {}

                typedef std::vector<std::pair<std::string, boost::shared_ptr<int64_t> > > TimestampVector;

                std::vector<std::pair<std::string, int64_t> > VectorClock::entrySet() {
                    TimestampVector entries = replicaTimestamps.entrySet();
                    std::vector<std::pair<std::string, int64_t> > result;
                    BOOST_FOREACH(const TimestampVector::value_type &entry , entries) {
                        result.push_back(std::make_pair(entry.first, *entry.second));
                    }

                    return result;
                }

                void VectorClock::setReplicaTimestamp(const std::string &replicaId, int64_t timestamp) {
                    replicaTimestamps.put(replicaId, boost::shared_ptr<int64_t >(new int64_t(timestamp)));
                }

                bool VectorClock::isAfter(VectorClock &other) {
                    bool anyTimestampGreater = false;
                    BOOST_FOREACH (const TimestampVector::value_type &otherEntry , other.replicaTimestamps.entrySet()) {
                        const std::string &replicaId = otherEntry.first;
                        const boost::shared_ptr<int64_t> &otherReplicaTimestamp = otherEntry.second;
                        boost::shared_ptr<int64_t> localReplicaTimestamp = getTimestampForReplica(replicaId);

                        if (localReplicaTimestamp.get() == NULL || *localReplicaTimestamp < *otherReplicaTimestamp) {
                            return false;
                        } else if (*localReplicaTimestamp > *otherReplicaTimestamp) {
                            anyTimestampGreater = true;
                        }
                    }
                    // there is at least one local timestamp greater or local vector clock has additional timestamps
                    return anyTimestampGreater ||  other.replicaTimestamps.size() < replicaTimestamps.size();
                }

                boost::shared_ptr<int64_t> VectorClock::getTimestampForReplica(const std::string &replicaId) {
                    return replicaTimestamps.get(replicaId);
                }

            }
        }
    }
}

max_stars_repo_name: pirog-spb/hazelcast-cpp-client

max_issues_repo_name: pirog-spb/hazelcast-cpp-client

max_forks_repo_name: pirog-spb/hazelcast-cpp-client


text:
//==============================================================================
//
//   (c) Copyright, 2012 University Corporation for Atmospheric Research (UCAR).
//       All rights reserved.
//
//==============================================================================

/**
 *
 * @file apps/probe_message_dataset_manager/process.cc
 *
 */

// Include files
#include <log/log.hh>
#include <libconfig.h++>
#include <stdio.h>
#include <stdlib.h>
#include <boost/format.hpp>
#include <Eigen/Dense>
#include <unordered_map>

using namespace Eigen;

#include "process.hh"
#include "vdt/stat_tools.hh"
#include "vdt/vdt_probe_message_file_reader.hh"

using std::unordered_map;
using namespace std;
using namespace libconfig;
using boost::format;

extern Log *Logg;
extern int Debug_level;

const double FILL_VALUE = -9999.;

process::~process()
{
}

int print_header(FILE *fp_out)
{
  return(fprintf(fp_out, "num_invalid_location,num_valid_location,num_cells,area_cells,percent_cell_coverage,latitude_min,latitude_iqr_25,latitude_median,latitude_iqr_75,latitude_max,longitude_min,longitude_iqr_25,longitude_median,longitude_iqr_75,longitude_max,air_temp2_min,air_temp2_iqr_25,air_temp2_median,air_temp2_iqr_75,air_temp2_max,surface_temp_min,surface_temp_iqr_25,surface_temp_median,surface_temp_iqr_75,surface_temp_max\n"));
}

int print_field(FILE *fp_out, const vector<double> &field_values, const vector<size_t> &quartile_indices)
{
  if (field_values.size() == 0)
    {
      return(fprintf(fp_out, ",-9999.,-9999.,-9999.,-9999.,-9999."));
    }
  else
    {
      return(fprintf(fp_out, ",%lf,%lf,%lf,%lf,%lf", field_values[0],field_values[quartile_indices[0]],field_values[quartile_indices[1]],field_values[quartile_indices[2]],field_values[field_values.size()-1]));
    }
}

int print_field(FILE *fp_out, const vector<float> &field_values, const vector<size_t> &quartile_indices)
{
  if (field_values.size() == 0)
    {
      return(fprintf(fp_out, ",-9999.,-9999.,-9999.,-9999.,-9999."));
    }
  else
    {
      return(fprintf(fp_out, ",%f,%f,%f,%f,%f", field_values[0],field_values[quartile_indices[0]],field_values[quartile_indices[1]],field_values[quartile_indices[2]],field_values[field_values.size()-1]));
    }
}

int msg_stats(time_t nearest_delta_time, vector<vdt_probe_message> &msgs, FILE *fp_out)
{
  // Get rid of messages with 0 or missing latitude or longitude
  vector<double> obs_time;
  vector<double> latitude;
  vector<double> longitude;
  vector<float> air_temp2;
  vector<float> surface_temp;

  MatrixXi conus_lat_long = MatrixXi::Zero(90, 180);

  size_t invalid_location = 0;
  for (size_t i=0; i<msgs.size(); i++)
    {
      double lat = msgs[i].get_latitude();
      double lon = msgs[i].get_longitude();
      float air_temp2_value = msgs[i].get_air_temp2();
      float surface_temp_value = msgs[i].get_surface_temp();
      if (lat == 0.0 or lat == FILL_VALUE or lon == 0.0 or lon == FILL_VALUE)
	{
	  invalid_location++;
	}
      else
	{
	  int int_lat = static_cast<int> (fabs(floor(lat)));
	  int int_lon = static_cast<int> (fabs(floor(lon)));
	  conus_lat_long(int_lat, int_lon) = 1;
	  latitude.push_back(lat);
	  longitude.push_back(lon);

	  if (air_temp2_value != FILL_VALUE)
	    {
	      air_temp2.push_back(air_temp2_value);
	    }

	  if (surface_temp_value != FILL_VALUE)
	    {
	      surface_temp.push_back(surface_temp_value);
	    }
	}
    }

  if (latitude.size() == 0)
    {
      Logg->write_time_info("Error: no valid lat-long locations\n");
      return 1;
    }

  int number_cells = conus_lat_long.sum();
  Logg->write_time_info("number cells %d, number 0 messages = %ld, number messages with valid location %ld\n", number_cells, invalid_location, latitude.size());

  vector<size_t> lat_quartiles;
  vector<size_t> lon_quartiles;
  vector<size_t> air_temp2_quartiles;
  vector<size_t> surface_temp_quartiles;

  ST_quartiles(latitude, lat_quartiles);
  ST_quartiles(longitude, lon_quartiles);
  ST_quartiles(air_temp2, air_temp2_quartiles);
  ST_quartiles(surface_temp, surface_temp_quartiles);

  int area_cells = fabs((floor(latitude[latitude.size()-1]) - floor(latitude[0]) + 1) * (floor(longitude[longitude.size()-1]) - floor(longitude[0]) + 1));
  int percent_cell_coverage = static_cast<int>(100*static_cast<float>(number_cells)/area_cells);

  Logg->write_time_info("range cells %d, percent cells covered %f\n", area_cells, static_cast<float>(number_cells)/area_cells);

  // Write output statistics
  Logg->write_time_info("writing output statistics for probe message file[s]\n");

  char date_string[64];
  strftime(date_string, 64, "%Y%m%d:%H%M", gmtime(&nearest_delta_time));

  fprintf(fp_out, "%s,%ld,%ld,%d,%d,%d", date_string, invalid_location, longitude.size(), number_cells, area_cells, percent_cell_coverage);

  if (print_field(fp_out, latitude, lat_quartiles) < 0)
    {
      Logg->write_time_error("failed writing latitude\n");
      return -1;
    }

  if (print_field(fp_out, longitude, lon_quartiles) < 0)
    {
      Logg->write_time_error("failed writing longitude\n");
      return -1;
    }

  if (print_field(fp_out, air_temp2, air_temp2_quartiles) < 0)
    {
      Logg->write_time_error("failed writing air_temp2\n");
      return -1;
    }

  if (print_field(fp_out, surface_temp, surface_temp_quartiles) < 0)
    {
      Logg->write_time_error("failed writing surface_temp\n");
      return -1;
    }
  fprintf(fp_out, "\n");
  return 0;
}

int process::run()
{
  // Read configuration file
  Logg->write_time("Info: reading config file %s\n", args.cfg_file.c_str());

  config_reader cfg_reader(args.cfg_file);
  if (cfg_reader.error != string(""))
    {
      Logg->write_time("Error: configuration file error %s\n", cfg_reader.error.c_str());
      return -1;
    }

  // Read probe messages from each input file into vector
  vector<vdt_probe_message> msgs;

  // NOTE: the reader should be updated to include file list reading capability
  unordered_map<time_t, vector<vdt_probe_message> > time_vector_map;
  unordered_map<time_t, vector<vdt_probe_message> >::iterator itr;
  vector<time_t> delta_times;
  vector<vdt_probe_message> file_msgs;

  for (size_t i=0; i<args.probe_message_file_list.size(); i++)
    {
      file_msgs.clear();

      vdt_probe_message_file_reader probe_rdr(args.probe_message_file_list[0].c_str());
      probe_rdr.get_probe_messages(file_msgs);

      Logg->write_time("Info: processing file %s having %ld messages\n", args.probe_message_file_list[i].c_str(), file_msgs.size());

      // append file_msgs to msgs
      for (size_t j=0; j<file_msgs.size(); j++)
	{
	  double msg_time = file_msgs[j].get_obs_time();
	  time_t nearest_delta_time = floor(msg_time/args.time_delta) * args.time_delta;
	  printf("nearest delta time %ld, time_delta %d\n", nearest_delta_time, args.time_delta);

	  itr = time_vector_map.find(nearest_delta_time);
	  if (itr == time_vector_map.end())
	    {
	      delta_times.push_back(nearest_delta_time);
	      time_vector_map[nearest_delta_time].push_back(file_msgs[j]);
	    }
	  else
	    {
	      itr->second.push_back(file_msgs[j]);
	    }
	  //time_vector_map[nearest_delta_time].push_back(file_msgs[j]);
	}
    }

  // Sort delta_times into ascending order
  sort(delta_times.begin(), delta_times.end());
  Logg->write_time("Info: number of delta times %ld\n", delta_times.size());

  FILE *fp_out = fopen(args.out_file.c_str(), "w");

  if (print_header(fp_out) < 0)
    {
      Logg->write_time_info("failed in print_header to %s\n");
      return -1;
    }

  for (size_t i=0; i<delta_times.size(); i++)
    {
      vector<vdt_probe_message> &msgs = time_vector_map[delta_times[i]];
      Logg->write_time("Info: delta_time, number of messages: %ld, %ld\n", delta_times[i], msgs.size());

      int ret = msg_stats(delta_times[i], msgs, fp_out);
      if (ret < 0)
	{
	  Logg->write_time("Error: msgs_stats failure\n");
	  return -1;
	}
    }

  fclose(fp_out);

  return 0;
}

ext: cc

max_stars_repo_name: OSADP/Pikalert-Vehicle-Data-Translator-

max_issues_repo_name: OSADP/Pikalert-Vehicle-Data-Translator-

max_forks_repo_name: OSADP/Pikalert-Vehicle-Data-Translator-


text:
#include "modules/variants/pair_cov.h"

#include <boost/container/small_vector.hpp>
#include <boost/functional/hash.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include "absl/container/btree_map.h"
#include "modules/bio_base/readmap.h"
#include "modules/bio_base/seqset.h"

namespace variants {

namespace {
constexpr int k_cov_debug = 0;
constexpr bool k_extended_stats = false;
constexpr bool k_report_progress_enabled = false;
constexpr int k_report_seconds = 300;
}  // namespace

struct pair_cov::result : private boost::noncopyable, public std::enable_shared_from_this<result> {
  assembly_ptr a;

  // Same as a->right_offset and a->left_offset; copied here to avoid
  // a pointer dereference since we look at these often.
  aoffset_t left_offset;
  aoffset_t right_offset;

  // seq.size() bases to the left of right_offset.
  aoffset_t right_anchored_left_offset;

  // Coverage entries that have been found to have pairing support.
  read_coverage_set pair_supported_reads;

  // Current sv adjustment bounds.
  // E.g. closed_hull(sv_adjusts.end().size_changes) or [0,1).
  interval_set_t cur_sv_adjust = closed_interval_set(0);
};

struct pair_cov::pair_cov_pg {
  // Pair table containing information from the assembly that's been processed in this path.
  pair_table pending_pair_table;

  result_ptr r;

  absl::flat_hash_map<multi_mid,
                      boost::container::flat_map<aoffset_t /* read offset */, read_id_mask_t>,
                      multi_mid_hash>
      pending_saved_reads;
};

std::string pair_cov::dump_read_id_mask(read_id_mask_t mask) const {
  std::stringstream out;

  for (unsigned i = 0; i != sizeof(read_id_mask_t) * 8; ++i) {
    out << (mask & 1);
    mask >>= 1;
  }
  return out.str();
}

std::string pair_cov::dump_pair_table(const pair_table& pt) const {
  std::stringstream out;

  if (k_cov_debug < 2) {
    return "";
  }
  for (const auto& rd_and_pte : pt.entries) {
    const multi_mid& mm = rd_and_pte.first;
    const auto& pte = rd_and_pte.second;
    out << "    " << dump_multi_mid(mm) << ", " << pte.results.size() << " read masks:\n";
    for (const auto& read_ids_and_roffs : pte.results) {
      const auto& masks = read_ids_and_roffs.first;
      const auto& roffs = read_ids_and_roffs.second;
      out << "      total=" << dump_read_id_mask(masks.total)
          << " pending=" << dump_read_id_mask(masks.pending) << ", " << roffs.size()
          << " results:\n";
      for (const auto& roff : roffs) {
        std::cout << "        read_start=" << roff.read_start << " in " << *roff.r->a << "\n";
      }
    }
  }
  return out.str();
}

std::string pair_cov::dump(pair_cov_pg* pg) const {
  std::stringstream out;

  out << "PairCov, main " << m_main_pair_table.entries.size() << " entries:\n";
  out << dump_pair_table(m_main_pair_table);
  out << "Pending, " << pg->pending_pair_table.entries.size() << " entries:\n";
  out << dump_pair_table(pg->pending_pair_table);
  return out.str();
}

pair_cov::pair_cov(const assemble_options& opts, pipeline_step_t output)
    : sorted_output_pipeline_step(std::move(output)), m_opts(opts) {}

pair_cov::~pair_cov() {
  if (k_extended_stats) {
    std::cout << "Pair cov finishing up\n";
  }
  advance_ref_to(std::numeric_limits<aoffset_t>::max());
  flush_old();
  if (k_extended_stats) {
    std::cout << "Pair cov finished\n";
  }
  CHECK(m_active.empty());
  CHECK(m_cur_inserts.empty());
  CHECK(m_cur_non_inserts.empty());
  CHECK(m_pending_results.empty());
  CHECK(m_main_pair_table.entries.empty());
}

void pair_cov::advance_ref_to(aoffset_t offset) {
  while (m_cur_ref_offset < offset) {
    advance_ref_towards(offset);
    flush_sorted_to(m_cur_ref_offset);
  }
}

void pair_cov::advance_ref_towards(aoffset_t target_offset) {
  flush_active_to_here();

  if (!m_active.empty() && target_offset > m_active.begin()->first) {
    target_offset = m_active.begin()->first;
  }

  CHECK_LT(m_cur_ref_offset, target_offset);
  CHECK(m_cur_inserts.empty());
  CHECK(m_cur_non_inserts.empty());
  m_cur_ref_offset = target_offset;
}

void pair_cov::report_progress() {
  if (!k_report_progress_enabled) {
    return;
  }

  if (m_last_report == 0) {
    m_last_report = time(0);
    m_last_report_offset = m_cur_ref_offset;
    m_last_report_asms = m_cur_asm_count;
    return;
  }

  time_t now = time(0);
  if (m_last_report == 0) {
    m_last_report = now - (k_report_seconds / 2);
  } else if (m_last_report + k_report_seconds < now) {
    int elapsed = now - m_last_report;
    aoffset_t bases = m_cur_ref_offset - m_last_report_offset;
    int asms = m_cur_asm_count - m_last_report_asms;
    std::cout << printstring("%.2f", asms * 1. / elapsed) << " asm/sec, "
              << printstring("%.2f", bases * 1. / elapsed)
              << " bases/sec, offset=" << m_cur_ref_offset << " elapsed=" << elapsed
              << " bases=" << bases << " cur pend=" << m_pending_results.size()
              << " active=" << m_active.size() << "\n";

    m_last_report = now;
    m_last_report_offset = m_cur_ref_offset;
    m_last_report_asms = m_cur_asm_count;
  }
}

void pair_cov::flush_active_to_here() {
  if (k_cov_debug) {
    std::cout << "Flushing active to " << m_cur_ref_offset << "\n";
  }

  std::vector<std::unique_ptr<pair_cov_pg>> rejoins;
  while (!m_active.empty() && m_active.begin()->first == m_cur_ref_offset) {
    if (k_cov_debug) {
      std::cout << "Joining from assembly ending at " << m_cur_ref_offset << ": "
                << dump(m_active.begin()->second.get()) << "\n";
    }
    std::unique_ptr<pair_cov_pg> pg = std::move(m_active.begin()->second);
    rejoins.emplace_back(std::move(pg));
    m_active.erase(m_active.begin());
  }
  if (!m_active.empty()) {
    CHECK_GT(m_active.begin()->first, m_cur_ref_offset);
  }

  if (!rejoins.empty()) {
    join(std::move(rejoins));
  }

  update_adjusts();

  if (!m_cur_inserts.empty()) {
    // Split out a path group for each insert.
    std::vector<std::unique_ptr<pair_cov_pg>> insert_rejoins;
    for (auto& a : m_cur_inserts) {
      std::unique_ptr<pair_cov_pg> pg = make_unique<pair_cov_pg>();
      add_assembly(pg.get(), std::move(a));
      insert_rejoins.emplace_back(std::move(pg));
    }
    m_cur_inserts.clear();
    join(std::move(insert_rejoins));
  }

  update_adjusts();

  if (!m_cur_non_inserts.empty()) {
    for (auto& a : m_cur_non_inserts) {
      aoffset_t right_offset = a->right_offset;

      std::unique_ptr<pair_cov_pg> pg = make_unique<pair_cov_pg>();
      add_assembly(pg.get(), std::move(a));
      m_active.emplace(right_offset, std::move(pg));
    }
    m_cur_non_inserts.clear();
  }

  if (k_cov_debug) {
    std::cout << "Done flushing active to " << m_cur_ref_offset << "\n";
  }

  if (m_cur_ref_offset > m_next_flush_old) {
    m_next_flush_old = m_cur_ref_offset + aoffset_t(m_opts.max_pair_distance);
    flush_old();
    report_progress();
  }
}

void pair_cov::on_assembly(assembly_ptr a) {
  if (k_cov_debug) {
    std::cout << "Pair_cov received assembly: " << *a << "\n";
  }
  if (a->bypass_coverage) {
    if (k_cov_debug) {
      std::cout << "Bypass coverage; skipping\n";
    }
    sort_and_output(std::move(a));
    return;
  }
  if (!a->read_coverage) {
    throw(io_exception("pair_cov requires assemblies to be procesesd through read_cov"));
    return;
  }

  track_left_offset(a->left_offset);
  advance_ref_to(a->left_offset);

  if (a->left_offset == a->right_offset) {
    m_cur_inserts.emplace_back(std::move(a));
  } else {
    m_cur_non_inserts.emplace_back(std::move(a));
  }
}

void pair_cov::save_pending_reads(pair_cov_pg* pg) {
  CHECK(pg->pending_pair_table.entries.empty());
  for (const auto& pending : pg->pending_saved_reads) {
    const auto& mm = pending.first;

    pair_table_entry& pte = pg->pending_pair_table.entries[mm];

    for (const auto& pending_pte_entry : pending.second) {
      aoffset_t read_start = pending_pte_entry.first;
      read_id_mask_t mask = pending_pte_entry.second;
      read_id_masks masks(mask, mask);

      result_offset roff;
      roff.r = pg->r.get();
      roff.read_start = read_start;

      pte.results[masks].push_back(roff);
      pte.tot_read_starts +=
          adjust_interval_set(closed_interval(read_start), pg->r->right_anchored_left_offset);
      pte.pending_mask |= mask;
    }
  }

  pg->pending_saved_reads.clear();
}

void pair_cov::save_read(pair_cov_pg* pg, uint32_t read_id, const readmap::read& rd,
                         const multi_mid& mm, aoffset_t read_start) {
  read_id_mask_t read_id_mask = read_id_mask_t(1) << (read_id & (sizeof(read_id_mask_t) * 8 - 1));
  CHECK(read_id_mask);

  pg->pending_saved_reads[mm][read_start] |= read_id_mask;
}

void pair_cov::save_reads(pair_cov_pg* pg) {
  for (const auto& reads_cov_entry : pg->r->a->read_coverage->reads()) {
    aoffset_t read_offset = reads_cov_entry.offset;
    aoffset_t read_len = reads_cov_entry.read_len;
    for (uint32_t read_id : reads_cov_entry.read_ids) {
      readmap::read cov_rd = m_opts.readmap->get_read_by_id(read_id);
      if (cov_rd.is_original_orientation() != m_opts.forward_pairs_face_inward) {
        continue;
      }

      if (!cov_rd.has_mate()) {
        continue;
      }

      // We should see the mate later.
      if (k_cov_debug) {
        std::cout << "Saving read id " << read_id << ": " << cov_rd.get_seqset_entry().sequence()
                  << "\n";
      }

      uint64_t mid_id = cov_rd.get_mid_id();
      CHECK_LT(mid_id, uint64_t(std::numeric_limits<uint32_t>::max()));
      multi_mid mm;
      mm.multi_id = mid_id;
      mm.size = read_len;
      mm.read_id_chunk = read_id / (sizeof(read_id_mask_t) * 8);

      save_read(pg, read_id, cov_rd, mm, read_offset);
    }
  }

  save_pending_reads(pg);
}

void pair_cov::match_mates(pair_cov_pg* pg) {
  // Where this assembly starts in the left-anchored coordinate system.
  aoffset_t seq_start_from_left = pg->r->a->left_offset;
  // Where this assembly starts in the right-anchored coordinate system.
  aoffset_t seq_start_from_right = pg->r->a->right_offset - aoffset_t(pg->r->a->seq.size());

  for (const auto& reads_cov_entry : pg->r->a->read_coverage->reads()) {
    aoffset_t read_offset = reads_cov_entry.offset;
    aoffset_t read_len = reads_cov_entry.read_len;
    for (uint32_t read_id : reads_cov_entry.read_ids) {
      readmap::read cov_rd = m_opts.readmap->get_read_by_id(read_id);
      if (cov_rd.is_original_orientation() == m_opts.forward_pairs_face_inward) {
        continue;
      }
      if (!cov_rd.has_mate()) {
        continue;
      }
      if (k_cov_debug) {
        std::cout << "read id " << read_id
                  << ", looking up read: " << cov_rd.get_seqset_entry().sequence() << "\n";
      }
      // We should have seen the mate for this read previously.
      readmap::read mate = cov_rd.get_mate_rc();
      uint32_t mate_id = mate.get_read_id();
      int mate_size = mate.size();
      uint64_t mid_id = mate.get_mid_id();
      CHECK_LT(mid_id, uint64_t(std::numeric_limits<uint32_t>::max()));
      multi_mid mm;
      mm.multi_id = mid_id;
      mm.size = mate_size;
      mm.read_id_chunk = mate_id / (sizeof(read_id_mask_t) * 8);

      if (k_cov_debug) {
        std::cout << "Mate is: " << mate.get_seqset_entry().sequence() << "\n";
      }

      for (bool pending : {false, true}) {
        pair_table* pt = pending ? &pg->pending_pair_table : &m_main_pair_table;
        aoffset_t seq_start = pending ? seq_start_from_right : seq_start_from_left;
        aoffset_t end_of_read_offset = seq_start + read_offset + read_len;

        auto it = pt->entries.find(mm);
        if (it == pt->entries.end()) {
          if (k_cov_debug) {
            std::cout << "(not present in " << (pending ? "pending" : "main") << ")\n";
          }
          continue;
        }
        auto& pte = it->second;
        if (k_cov_debug) {
          std::cout << "Mate present in" << (pending ? " pending" : " main")
                    << " pair table: " << &pte << "\n";
        }

        if (find_mate(pg, mate_id, mate_size, pte, end_of_read_offset)) {
          // Pair match!
          if (k_cov_debug) {
            std::cout << "Mate found!  Adding read id " << read_id << "\n";
          }
          pg->r->pair_supported_reads.insert(read_offset, read_id, read_len);
        } else {
          if (k_cov_debug) {
            std::cout << "No mate found searching for " << mate_id << "\n";
          }
        }
      }
    }
  }
}

void pair_cov::add_assembly(pair_cov_pg* pg, assembly_ptr a) {
  ++m_cur_asm_count;

  CHECK(pg);
  CHECK(a);

  // There are two coordinate systems.  One where a->left_anchor is
  // anchored to reference, and read offsets are calculated from the
  // beginning, and one where a->right_anchor is anchored to reference
  // and read offsets are calculated from the end.
  //
  // Where svlen is 0, these two coordinate systems are the same.
  //
  // The left anchored coordinate system is used when looking up
  // previously seen mates from assemblies before this.
  //
  // The right anchored coordinate system is used when storing pairs
  // for future use, and for looking up previously seen mates from
  // this assembly.

  // Where this assembly starts in the right-anchored coordinate system.
  aoffset_t seq_start_from_right = a->right_offset - aoffset_t(a->seq.size());

  if (k_cov_debug) {
    std::cout << "\nAdding assembly " << *a << " to table: " << dump(pg);
  }

  CHECK_EQ(m_cur_ref_offset, a->left_offset);

  aoffset_t svlen = aoffset_t(a->seq.size()) - (a->right_offset - a->left_offset);
  if (svlen) {
    aoffset_t care_dist = max_care_distance();
    for (const auto& pend : m_pending_results) {
      aoffset_t dist = (m_cur_ref_offset + aoffset_t(a->seq.size())) -
                       interval_set_upper_bound(pend->cur_sv_adjust);
      CHECK_GE(dist, 0);
      if (dist < care_dist) {
        interval_t adjusted = adjust_interval_set(pend->cur_sv_adjust, closed_interval_set(-svlen));
        auto& adj = m_future_adjusts[a->right_offset][pend.get()];
        if (adj.first) {
          CHECK_EQ(adj.first.get(), pend.get());
          adj.second += adjusted;
        } else {
          adj.first = pend.clone();
          adj.second = adjusted;
        }
      }
    }
  }

  CHECK_EQ(a->left_offset, m_cur_ref_offset);

  CHECK(!pg->r);
  pg->r = result_ptr::make_shared();
  pg->r->left_offset = a->left_offset;
  pg->r->right_offset = a->right_offset;
  pg->r->right_anchored_left_offset = seq_start_from_right;
  pg->r->a = std::move(a);

  save_reads(pg);

  match_mates(pg);

  if (k_cov_debug) {
    std::cout << "Done adding assembly to pair table: " << dump(pg);
  }
}

bool pair_cov::find_mate(pair_cov_pg* pg, uint32_t mate_id, int mate_len, pair_table_entry& pte,
                         aoffset_t end_of_read_offset) {
  interval_set_t pair_valid =
      closed_interval(end_of_read_offset - aoffset_t(m_opts.max_pair_distance),
                      end_of_read_offset - aoffset_t(m_opts.min_pair_distance));

  if (k_cov_debug) {
    std::cout << "find_mate: pair valid interval is " << pair_valid << ", end of read offset is "
              << end_of_read_offset << ", pending mask = " << pte.pending_mask << "\n";
  }

  read_id_mask_t mate_id_mask = read_id_mask_t(1) << (mate_id & (sizeof(read_id_mask_t) * 8 - 1));
  CHECK(mate_id_mask);

  bool found_any = false;
  if (overlaps(pte.tot_read_starts, pair_valid)) {
    found_any = true;
  }

  if (found_any && !(pte.pending_mask & mate_id_mask)) {
    if (k_cov_debug) {
      std::cout << "PTE Pending mask does not contain " << mate_id_mask
                << "; not doing full search\n";
    }
    return found_any;
  }

  bool found_all = true;
  pte_results_t new_results;
  for (auto pm_it = pte.results.begin(); pm_it != pte.results.end();) {
    const auto& masks = pm_it->first;
    auto& roffs = pm_it->second;

    if (masks.total < mate_id_mask) {
      break;
    }

    if (!(masks.total & mate_id_mask)) {
      ++pm_it;
      continue;
    }
    if (found_any && !(masks.pending & mate_id_mask)) {
      ++pm_it;
      continue;
    }

    read_id_mask_t new_pending_mask = masks.pending & ~mate_id_mask;

    size_t idx = 0;
    while (idx < roffs.size()) {
      auto& roff = roffs[idx];

      interval_set_t adjusted_read_start = adjust_interval_set(
          roff.r->cur_sv_adjust, roff.read_start + roff.r->right_anchored_left_offset);
      if (overlaps(pair_valid, adjusted_read_start)) {
        found_any = true;
        pte.tot_read_starts += adjusted_read_start;
        if (masks.pending & mate_id_mask) {
          roff.r->pair_supported_reads.insert(roff.read_start, mate_id, mate_len);
          read_id_masks new_masks(masks.total, new_pending_mask);
          auto existing_it = pte.results.find(new_masks);
          if (existing_it == pte.results.end()) {
            new_results[new_masks].emplace_back(std::move(roff));
          } else {
            existing_it->second.emplace_back(std::move(roff));
          }
          if (idx + 1 != roffs.size()) {
            roffs[idx] = std::move(roffs.back());
          }
          roffs.pop_back();
        } else {
          break;  // Found one, and nothing pending; stop searching.
        }
      } else {
        if (masks.pending & mate_id_mask) {
          found_all = false;
        }
        ++idx;
      }
    }

    if (roffs.empty()) {
      pm_it = pte.results.erase(pm_it);
    } else {
      ++pm_it;
    }
  }

  pte.results.merge(std::move(new_results));

  if (found_all) {
    pte.pending_mask &= ~mate_id_mask;
  }

  return found_any;
}

void pair_cov::reap_result(std::unique_ptr<result> r) {
  if (!r) {
    return;
  }
  if (k_cov_debug) {
    std::cout << "Reaping assembly: " << *r->a << "\n";
  }

  r->a->pair_read_coverage.emplace(r->pair_supported_reads.build_and_clear(r->a->seq.size()));
  untrack_left_offset(r->a->left_offset);
  sort_and_output(std::move(r->a));
}

void pair_cov::join(std::vector<std::unique_ptr<pair_cov_pg>> inputs) {
  for (auto& input : inputs) {
    join(std::move(input));
  }
}

void pair_cov::update_adjusts() {
  while (!m_future_adjusts.empty()) {
    auto it = m_future_adjusts.begin();
    CHECK_GE(it->first, m_cur_ref_offset);
    if (it->first > m_cur_ref_offset) {
      break;
    }

    auto& adjust = it->second;
    for (auto& adj_item : adjust) {
      CHECK_EQ(adj_item.first, adj_item.second.first.get());
      result_ptr r = std::move(adj_item.second.first);
      interval_set_t intv = adj_item.second.second;
      interval_set_t orig_sv_adjust = r->cur_sv_adjust;
      r->cur_sv_adjust += intv;
      // We shouldn't adjust the valid placement range of this old
      // assembly to be past our current position.
      aoffset_t adjust_limit = m_cur_ref_offset - r->right_offset;

      aoffset_t adjust_upper_closed = r->cur_sv_adjust.upper() - 1;
      CHECK_LE(adjust_upper_closed, adjust_limit)
          << " updating pending result " << *r->a << ": orig = " << orig_sv_adjust
          << ", intv = " << intv << ", after adjust = " << r->cur_sv_adjust << "\n";

      if (!m_pending_results.count(r)) {
        m_pending_results.insert(std::move(r));
      } else {
        CHECK(!r.release());
      }
    }

    m_future_adjusts.erase(it);
  }
}

void pair_cov::join(std::unique_ptr<pair_cov_pg> input) {
  propagate_and_fill(input->pending_pair_table, &m_main_pair_table);
  m_pending_results.insert(std::move(input->r));
}

pair_cov::simple_interval_set pair_cov::adjust_interval_set(const simple_interval_set& orig,
                                                            aoffset_t adjust) {
  return simple_interval_set(orig.lower() + adjust, orig.upper() + adjust);
}

pair_cov::simple_interval_set pair_cov::adjust_interval_set(const simple_interval_set& orig,
                                                            const simple_interval_set& adjust) {
  aoffset_t adjust_upper_max = adjust.upper() - 1;
  return simple_interval_set(orig.lower() + adjust.lower(), orig.upper() + adjust_upper_max);
}

pair_cov::simple_interval_set pair_cov::unadjust_interval_set(const simple_interval_set& orig,
                                                              const simple_interval_set& adjust) {
  aoffset_t adjust_upper_max = adjust.upper() - 1;
  return simple_interval_set(orig.lower() - adjust_upper_max, orig.upper() - adjust.lower());
}

aoffset_t pair_cov::max_care_distance() const {
  aoffset_t read_dist = aoffset_t(m_opts.seqset->max_read_len() * 2);
  aoffset_t pair_dist = aoffset_t(m_opts.max_pair_distance);
  return read_dist + pair_dist;
}
absl::flat_hash_set<pair_cov::result*> pair_cov::find_expired_results() const {
  aoffset_t pair_cutoff = m_cur_ref_offset - max_care_distance();

  if (k_cov_debug) {
    std::cout << "Finding expired results, ref=" << m_cur_ref_offset
              << " pair cutoff=" << pair_cutoff << "\n";
  }

  absl::flat_hash_set<result*> expired;
  for (const auto& r : m_pending_results) {
    aoffset_t adjusted_offset_from_cur = interval_set_upper_bound(
        adjust_interval_set(closed_interval_set(r->right_offset), r->cur_sv_adjust));

    if (k_cov_debug) {
      std::cout << "@" << m_cur_ref_offset << ": right offset: " << r->right_offset
                << " adjusted by " << r->cur_sv_adjust << ": " << adjusted_offset_from_cur;
      if (adjusted_offset_from_cur < pair_cutoff) {
        std::cout << " EXPIRED\n";
      } else {
        std::cout << " OK\n";
      }
    }
    if (adjusted_offset_from_cur < pair_cutoff) {
      expired.insert(r.get());
    }
  }
  return expired;
}

namespace {

template <typename T>
class track_histo {
 public:
  track_histo() = default;

  void increment(const T& val) { ++m_counts[val]; }

  void display_brief_pct(std::ostream& os, size_t display_count) {
    size_t tot = 0;
    for (const auto& val : m_counts) {
      tot += val.second;
    }

    size_t tot_so_far = 0;
    size_t i = 0;
    for (const auto& val : m_counts) {
      if (!display_count) {
        os << "...";
        return;
      }
      float pct_here = val.second * 100. / tot;
      tot_so_far += val.second;
      float pct_so_far = tot_so_far * 100. / tot;
      if (i == 0 || !(i & (i - 1))) {
        os << std::fixed << std::setprecision(2) << " " << val.first << "(" << val.second << "+"
           << pct_here << "%=" << pct_so_far << "%)";
      }
      --display_count;
      ++i;
    }
  }

 private:
  std::map<T, size_t> m_counts;
};

}  // namespace

void pair_cov::flush_old() {
  absl::flat_hash_set<result*> expired_results = find_expired_results();
  size_t expired_entries = 0;
  size_t expired_roff = 0;
  size_t remaining_roff = 0;
  size_t pending_mates = 0;
  size_t expired_pending_mates = 0;

  track_histo<size_t> pte_results_histo;
  track_histo<size_t> roff_per_mask_histo;

  multi_mid most_roffs_pte_mm{};
  pair_table_entry* most_roffs_pte = nullptr;
  size_t most_roffs_pte_pms = 0;

  multi_mid most_results_pte_mm{};
  pair_table_entry* most_results_pte = nullptr;
  size_t most_results_pte_pms = 0;

  auto& pt = m_main_pair_table.entries;
  if (expired_results.empty() && !k_extended_stats) {
    return;
  }

  if (k_cov_debug || k_extended_stats) {
    std::cout << "flush_old, expiring " << expired_results.size() << " from pair table of size "
              << pt.size() << ": ";
    std::cout.flush();
  }
  auto it = pt.begin();
  for (auto next = it; it != pt.end(); it = next) {
    ++next;
    const auto& mm = it->first;
    auto& pte = it->second;

    CHECK(!(pte.results.empty()));
    size_t tot_roffs = 0;

    auto results_it = pte.results.begin();
    bool do_shrink_results = false;
    while (results_it != pte.results.end()) {
      auto& roffs = results_it->second;
      if (k_extended_stats) {
        roff_per_mask_histo.increment(roffs.size());
      }
      bool do_shrink_roffs = false;
      size_t idx = 0;
      tot_roffs += roffs.size();
      while (idx != roffs.size()) {
        auto& roff = roffs[idx];
        if (expired_results.count(roff.r)) {
          do_shrink_roffs = true;
          ++expired_roff;
          if (idx + 1 != roffs.size()) {
            roff = std::move(roffs.back());
          }
          roffs.pop_back();
        } else {
          ++idx;
          ++remaining_roff;
        }
      }
      if (roffs.empty()) {
        results_it = pte.results.erase(results_it);
        do_shrink_results = true;
      } else {
        ++results_it;
        if (do_shrink_roffs) {
          roffs.shrink_to_fit();
        }
      }
    }

    if (pte.results.empty()) {
      pt.erase(it);
      ++expired_entries;
      continue;
    }

    if (do_shrink_results) {
      pte.results.shrink_to_fit();
    }

    if (k_extended_stats) {
      if (!most_roffs_pte || tot_roffs > most_roffs_pte_pms) {
        most_roffs_pte = &pte;
        most_roffs_pte_mm = mm;
        most_roffs_pte_pms = tot_roffs;
      }

      if (!most_results_pte || pte.results.size() > most_results_pte->results.size()) {
        most_results_pte = &pte;
        most_results_pte_mm = mm;
        most_results_pte_pms = tot_roffs;
      }

      pte_results_histo.increment(pte.results.size());
    }
  }

  if (k_cov_debug || k_extended_stats) {
    std::cout << " expired " << expired_results.size() << " results, " << expired_entries
              << " pte, " << expired_roff << "/" << remaining_roff << " roffs, "
              << expired_pending_mates << "/" << pending_mates
              << " pending mates, new size=" << pt.size() << "\n";
    std::cout << "roffs per mask histo: ";
    roff_per_mask_histo.display_brief_pct(std::cout, 64);
    std::cout << "\nresults histo: ";
    pte_results_histo.display_brief_pct(std::cout, 64);
    std::cout << "\n";

    if (most_roffs_pte) {
      std::cout << "Most pms pte has " << most_roffs_pte_pms << " pms and "
                << most_roffs_pte->results.size()
                << " results: " << dump_multi_mid(most_roffs_pte_mm) << "\n";
    }
    if (most_results_pte) {
      std::cout << "Most results pte has " << most_results_pte_pms << " pms and "
                << most_results_pte->results.size()
                << " results: " << dump_multi_mid(most_results_pte_mm) << "\n";
    }
  }

  for (const auto& expired : expired_results) {
    auto it = m_pending_results.find<const result*>(expired);
    CHECK(it != m_pending_results.end());
    result_ptr r = std::move(m_pending_results.extract(it).value());
    reap_result(r.release());
  }

  if (k_extended_stats) {
    result* oldest = nullptr;
    for (const auto& r : m_pending_results) {
      if (!oldest || r->a->left_offset < oldest->a->left_offset) {
        oldest = r.get();
      }
    }

    if (oldest) {
      std::cout << "pair_cov's oldest assembly is at " << oldest->a->left_offset << ", ["
                << m_cur_ref_offset - oldest->a->left_offset << ","
                << m_cur_ref_offset - oldest->a->right_offset << ") behind cur(" << m_cur_ref_offset
                << "), valid adjust range=" << oldest->cur_sv_adjust << " : " << *oldest->a << "\n";
    }

    std::cout << sorted_output_stats(m_cur_ref_offset) << "\n";
  }
}

std::string pair_cov::dump_multi_mid(const multi_mid& mm) const {
  std::stringstream result;
  uint64_t seqset_id = m_opts.readmap->mid_to_entry(mm.multi_id);
  seqset_range r = m_opts.seqset->ctx_entry(seqset_id).truncate(mm.size);

  auto read_range = m_opts.readmap->entry_to_index_range(r.begin(), r.end());
  size_t read_count = read_range.second - read_range.first;
  result << "Mid(id=" << mm.multi_id << ",size=" << mm.size << ",nr=" << read_count
         << ",seq=" << r.sequence() << ")";
  return result.str();
}

void pair_cov::propagate_and_fill(pair_table& old_table, pair_table* new_table) {
  if (k_cov_debug) {
    std::cout << "Propagating and filling from table with " << old_table.entries.size()
              << " entries to table with " << new_table->entries.size() << " entries\n";
  }

  for (auto& old_pte_entry : old_table.entries) {
    const multi_mid& mm = old_pte_entry.first;
    pair_table_entry& old_pte = old_pte_entry.second;

    if (k_cov_debug > 1) {
      std::cout << "Read mid=" << dump_multi_mid(mm) << " old pte=" << &old_pte << "\n";
    }

    pair_table_entry& new_pte = new_table->entries[mm];

    if (k_cov_debug > 1) {
      std::cout << "New pte: " << &new_pte << "\n";
    }

    static size_t count = 0;
    if (k_cov_debug && !(count++ & 0xFFFFF)) {
      std::cout << " results size: " << old_pte.results.size() << "\n";
    }

    new_pte.results.reserve(new_pte.results.size() + old_pte.results.size());

    for (auto& old_pending : old_pte.results) {
      auto& old_roffs = old_pending.second;
      auto& new_roffs = new_pte.results[old_pending.first];
      new_roffs.insert(new_roffs.end(), old_roffs.begin(), old_roffs.end());
    }

    new_pte.tot_read_starts += old_pte.tot_read_starts;
    new_pte.pending_mask |= old_pte.pending_mask;
  }
  if (k_cov_debug) {
    std::cout << "Done propagating and filling\n";
  }
}

pair_cov::interval_t pair_cov::closed_interval(aoffset_t start, aoffset_t limit) {
  return interval_t(start, limit + 1);
}

pair_cov::interval_t pair_cov::closed_interval(aoffset_t single_offset) {
  return closed_interval(single_offset, single_offset);
}

aoffset_t pair_cov::interval_set_upper_bound(const interval_set_t& s) {
  CHECK(!is_empty(s));
  return s.upper();
}

pair_cov::interval_set_t pair_cov::closed_interval_set(aoffset_t single_offset) {
  interval_set_t result;
  result += closed_interval(single_offset);
  return result;
}

}  // namespace variants

max_stars_repo_name: spiralgenetics/biograph

max_issues_repo_name: spiralgenetics/biograph

max_forks_repo_name: spiralgenetics/biograph