text:
//==================================================================================================
/**
  Copyright 2016 NumScale SAS

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

#include <boost/simd/detail/nsm.hpp>

namespace boost { namespace simd
{
  namespace detail
  {
    // Turn '4' into 4
    constexpr int to_int(char c) { return static_cast<int>(c) - 48; }

    // Terminal case for char recursion
    template< std::size_t N>
    constexpr std::uint64_t parse(int,const char (&)[N], nsm::uint64_t<0> const&)
    {
      return 0;
    }

    // Extract a char, get its value, multiply by current base, repeat
    template< std::size_t N, std::size_t I>
    constexpr std::uint64_t parse(int base, const char (&arr)[N], nsm::uint64_t<I> const&)
    {
      return to_int(arr[I - 1]) * base + parse<N>(base*10, arr,nsm::uint64_t<I-1>{});
    }

    // Intermediate trampoline so MSVC doesn't cry
    template<std::size_t N,char... c> constexpr std::uint64_t parse(int base)
    {
      return parse<N>(base, {c...}, nsm::uint64_t<N>{});
    }
  }

  namespace literal
  {
    // Enables xxx_c to be turned into usable integral constant int_<xxx>
    template <char ...c>
    constexpr nsm::uint64_t< boost::simd::detail::parse<sizeof...(c),c...>(1)> operator"" _c()
    {
      return {};
    }
  }
} }

#endif

max_stars_repo_name: SylvainCorlay/pythran

max_issues_repo_name: SylvainCorlay/pythran

max_forks_repo_name: SylvainCorlay/pythran


text:
/******************************************************************************
 * Copyright 2019 The Apollo Authors. 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 "modules/map/tools/map_datachecker/server/laps_checker.h"

#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>

#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <limits>

namespace apollo {
namespace hdmap {

LapsChecker::LapsChecker(const std::vector<FramePose> &poses, int laps_to_check,
                         std::shared_ptr<JSonConf> sp_conf)
    : poses_(poses), sp_conf_(sp_conf) {
  laps_to_check_ = laps_to_check;
  maxx_ = 0.0;
  maxy_ = 0.0;
  minx_ = 0.0;
  miny_ = 0.0;
  possible_max_laps_ = (laps_to_check_ + 1) * 10;
  confidence_.resize(possible_max_laps_ + 1, 0.0);
  finished_ = false;
  return_state_ = ErrorCode::SUCCESS;
  AINFO << "instance has " << poses.size() << " poses";
  AINFO << "confidence size: " << possible_max_laps_ + 1;
}

int LapsChecker::SetProgress(double p) {
  progress_ = p;
  return 0;
}

double LapsChecker::GetProgress() const { return progress_; }

size_t LapsChecker::GetLap() const { return lap_; }

double LapsChecker::GetConfidence() {
  double res = 0.0;
  lap_ = laps_to_check_;
  for (size_t i = 0; i < confidence_.size(); ++i) {
    AINFO << "confidence[" << i << "]: " << confidence_[i];
  }
  AINFO << "laps to check: " << laps_to_check_;
  for (size_t i = laps_to_check_; i < confidence_.size(); ++i) {
    res += confidence_[i];
  }
  AINFO << "current confidence: " << res
        << ",confidence thresh:" << sp_conf_->laps_rate_thresh;
  if (res < sp_conf_->laps_rate_thresh) {
    if (confidence_.empty()) {
      AINFO << "some problems induce lap problem";
      return 0.0;
    }
    res = confidence_[0];
    lap_ = 0;
    for (size_t i = 1; i < confidence_.size(); ++i) {
      if (i == laps_to_check_) {
        continue;
      }
      if (confidence_[i] > res) {
        lap_ = i;
        res = confidence_[i];
      }
    }
  }
  return res;
}

ErrorCode LapsChecker::Check() {
  if (poses_.empty()) {
    return_state_ = ErrorCode::ERROR_VERIFY_NO_GNSSPOS;
    return return_state_;
  }
  DoCheck();
  finished_ = true;
  return return_state_;
}

void LapsChecker::DoCheck() {
  AINFO << "do_check";
  SetProgress(0.0);
  int ret = 0;
  AINFO << "check->check_params";
  ret = CheckParams();
  if (ret < 0) {
    AINFO << "check_params failed";
  }
  SetProgress(0.1);

  AINFO << "check->setup_grids_map";
  ret = SetupGridsMap();
  if (ret < 0) {
    AINFO << "setup_grids_map failed";
  }
  SetProgress(0.5);
  AINFO << "check->setup_grids_map done";

  AINFO << "check->check_laps";
  ret = CheckLaps();
  if (ret < 0) {
    AINFO << "check_laps failed";
  }
  SetProgress(1.0);
  AINFO << "check->check_laps done";

  AINFO << "do_check done";
}

int LapsChecker::CheckParams() {
  int n_pose = static_cast<int>(poses_.size());
  if (n_pose < sp_conf_->laps_frames_thresh) {
    return -1;
  }
  return 0;
}

int LapsChecker::SetupGridsMap() {
  AINFO << "setup_grids_map->get_min_max";
  GetMinMax();
  AINFO << "setup_grids_map->do_setup_grids_map";
  int ret = DoSetupGridsMap();
  if (ret < 0) {
    AINFO << "do_setup_grids_map failed";
    return -1;
  }
  AINFO << "setup_grids_map done";
  return 0;
}

int LapsChecker::CheckLaps() {
  int height = static_cast<int>(grids_map_.size());
  if (height <= 2 || height > 1000000) {
    AINFO << "grids_map_ size error. height = " << height;
    return -1;
  }

  int width = static_cast<int>(grids_map_[0].size());
  if (width <= 2 || width >= 1000000) {
    AINFO << "grids_map_ size error. width = " << width;
    return -1;
  }

  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      Grid &grid = grids_map_[y][x];
      size_t t_size = grid.size();
      if (t_size == 0) {
        continue;
      }
      for (size_t i = 0; i < t_size; ++i) {
        std::vector<double> stamps;
        GatherTimestamps(&stamps, grid[i].alpha, x, y);
        if (stamps.empty()) {
          continue;
        }
        std::sort(stamps.begin(), stamps.end());
        double thresh_in_sec = sp_conf_->laps_time_err_thresh * 60;
        size_t segment = 1;
        for (size_t j = 1; j < stamps.size(); ++j) {
          if (stamps[j] - stamps[j - 1] > thresh_in_sec) {
            segment++;
          }
        }
        if (segment <= possible_max_laps_) {
          confidence_[segment] += 1;
        }
      }
    }
  }

  double all = 0.0;
  for (size_t i = 0; i < confidence_.size(); ++i) {
    all += confidence_[i];
  }
  if (all == 0.0) {
    AINFO << "there seems no poses";
    return -1;
  }
  AINFO << "confidence size: " << confidence_.size();
  for (size_t i = 0; i < confidence_.size(); ++i) {
    confidence_[i] /= all;
  }

  return 0;
}

int LapsChecker::GatherTimestamps(std::vector<double> *sp_stamps, double alpha,
                                  int center_x, int center_y) {
  int search_d = sp_conf_->laps_search_diameter;
  if ((search_d & 1) == 0) {
    AINFO << "laps_search_diameter should be an odd";
    return -1;
  }
  int search_r = (search_d >> 1);
  size_t height = grids_map_.size(), width = grids_map_[0].size();
  std::vector<double> &stamps = *sp_stamps;
  stamps.clear();
  const size_t start_y = std::max(0, center_y - search_r);
  const size_t end_y =
      std::min(static_cast<int>(height) - 1, center_y + search_r);
  const size_t start_x = std::max(0, center_x - search_r);
  const size_t end_x =
      std::min(static_cast<int>(width) - 1, center_x + search_r);
  for (size_t y = start_y; y <= end_y; y++) {
    for (size_t x = start_x; x <= end_x; x++) {
      Grid &grid = grids_map_[y][x];
      for (size_t i = 0; i < grid.size(); ++i) {
        if (std::abs(alpha - grid[i].alpha) < sp_conf_->laps_alpha_err_thresh) {
          std::vector<int> &idxs = grid[i].idxs;
          for (size_t j = 0; j < idxs.size(); ++j) {
            if (idxs[j] >= static_cast<int>(poses_.size())) {
              AINFO << "index error, index: " << idxs[j]
                    << ", pose size: " << poses_.size();
            } else {
              stamps.push_back(poses_[idxs[j]].time_stamp);
            }
          }
        }
      }
    }
  }

  return 0;
}

int LapsChecker::GetMinMax() {
  minx_ = std::numeric_limits<double>::max();
  miny_ = std::numeric_limits<double>::max();
  maxx_ = std::numeric_limits<double>::min();
  maxy_ = std::numeric_limits<double>::min();
  size_t size = poses_.size();
  AINFO << "get_min_max pose size: " << size;
  for (size_t i = 0; i < size; ++i) {
    double tx = poses_[i].tx, ty = poses_[i].ty;
    if (tx < minx_) {
      minx_ = tx;
    }
    if (tx > maxx_) {
      maxx_ = tx;
    }
    if (ty < miny_) {
      miny_ = ty;
    }
    if (ty > maxy_) {
      maxy_ = ty;
    }
  }
  return 0;
}

int LapsChecker::DoSetupGridsMap() {
  size_t width = size_t(maxx_ - minx_ + 1);
  size_t height = size_t(maxy_ - miny_ + 1);
  AINFO << "grid map width: " << width << ", height: " << height;
  size_t size = poses_.size();
  if (1 >= size || 0 == height || 0 == width || height > 1000000 ||
      width > 1000000) {
    AINFO << "pose size: " << size << ", height: " << height
          << ", width: " << width;
    AINFO << "pose size error or grid map size error";
    return -1;
  }

  grids_map_.resize(height);
  for (size_t i = 0; i < height; ++i) {
    grids_map_[i].resize(width);
  }
  // first pose can not be used
  for (size_t i = 1; i < size; ++i) {
    int x = static_cast<int>(poses_[i].tx - minx_);
    int y = static_cast<int>(poses_[i].ty - miny_);
    PutPoseToGrid(static_cast<int>(i), y, x);
    PutPoseToNeighborGrid(static_cast<int>(i));
  }
  return 0;
}

double LapsChecker::CalcAlpha(int pose_index) {
  double vecx = poses_[pose_index].tx - poses_[pose_index - 1].tx;
  double vecy = poses_[pose_index].ty - poses_[pose_index - 1].ty;
  double alpha = acos(vecx / sqrt(vecx * vecx + vecy * vecy)) * 180 / M_PI;
  if (alpha > 0) {
    return alpha;
  }
  return 360 + alpha;
}

int LapsChecker::PutPoseToGrid(int pose_index, int grid_y, int grid_x) {
  if (pose_index <= 0) {
    return 0;
  }
  double alpha = CalcAlpha(pose_index);
  if (std::isnan(alpha)) {
    AERROR << "ignore static pose " << pose_index;
    return 0;
  }

  Grid &grid = grids_map_[grid_y][grid_x];
  size_t t_size = grid.size();
  for (size_t j = 0; j < t_size; ++j) {
    if (std::abs(alpha - grid[j].alpha) < sp_conf_->laps_alpha_err_thresh) {
      grid[j].idxs.push_back(pose_index);
      grid[j].alpha = (grid[j].alpha + alpha) / 2;
      return 0;
    }
  }

  GridMeta gm;
  gm.alpha = alpha;
  gm.idxs = {pose_index};
  grid.push_back(gm);
  return 0;
}

int LapsChecker::PutPoseToNeighborGrid(int pose_index) {
  if (pose_index <= 0) {
    return 0;
  }
  std::vector<int> x, y;
  GetPassedGrid(pose_index, &x, &y);
  for (size_t i = 0; i < x.size(); ++i) {
    PutPoseToGrid(pose_index, y[i], x[i]);
  }
  return 0;
}

int LapsChecker::GetPassedGrid(int pose_index, std::vector<int> *sp_grid_x,
                               std::vector<int> *sp_grid_y) {
  if (pose_index <= 0) {
    return 0;
  }
  std::vector<int> &grid_x = *sp_grid_x;
  std::vector<int> &grid_y = *sp_grid_y;
  grid_x.clear();
  grid_y.clear();
  double x = poses_[pose_index].tx - minx_;
  double y = poses_[pose_index].ty - miny_;
  double last_x = poses_[pose_index - 1].tx - minx_;
  double last_y = poses_[pose_index - 1].ty - miny_;
  if (std::abs(x - last_x) < 1e-6) {  // current trace is vertical
    int start_y = static_cast<int>(std::min(y, last_y)) + 1;
    int end_y = static_cast<int>(std::max(y, last_y));
    int start_x = static_cast<int>(x);
    while ((start_y++) < end_y) {
      grid_x.push_back(start_x);
      grid_y.push_back(start_y);
    }
  } else if (std::abs(y - last_y) < 1e-6) {  // current trace is horizontal
    int start_x = static_cast<int>(std::min(x, last_x));
    int end_x = static_cast<int>(std::max(x, last_x));
    int start_y = static_cast<int>(y);
    while ((++start_x) < end_x) {
      grid_x.push_back(start_x);
      grid_y.push_back(start_y);
    }
  } else {
    double k = Slope(last_x, last_y, x, y);
    if (k > 99999999.0) {
      AERROR << "slope error";
      return -1;
    }
    int steps = static_cast<int>(std::abs(last_x - x));
    int step = 0;
    double xx = 0.0, yy = 0.0;
    if (x < last_x) {
      xx = x, yy = y;
    } else {
      xx = last_x, yy = last_y;
    }
    while ((step++) < steps) {
      xx = xx + 1;
      yy = yy + k;
      grid_x.push_back(static_cast<int>(xx));
      grid_y.push_back(static_cast<int>(yy));
    }
  }
  return 0;
}

double LapsChecker::Slope(double x1, double y1, double x2, double y2) {
  if (std::abs(x1 - x2) < 1e-6) {
    return std::numeric_limits<double>::max();
  }
  if (std::abs(y1 - y2) < 1e-6) {
    return 0.0;
  }
  return (y2 - y1) / (x2 - x1);
}

ErrorCode LapsChecker::GetReturnState() { return return_state_; }

}  // namespace hdmap
}  // namespace apollo

ext: cc

max_stars_repo_name: seeclong/apollo

max_issues_repo_name: seeclong/apollo

max_forks_repo_name: seeclong/apollo


text:
#include <boost/test/unit_test.hpp>

#include <AoC/utils/anon_ret.h>

namespace
{

auto f1()
{
  int x = 1;
  int y = 2;
  int z = 3;
  AOC_RETURN_ANONYMOUS_STRUCT( x, y, z );
}

}  // namespace

BOOST_AUTO_TEST_CASE( AoC_utils_anonymous_return )
{
  auto test1 = f1();
  BOOST_CHECK_EQUAL( test1.x, 1 );
  BOOST_CHECK_EQUAL( test1.y, 2 );
  BOOST_CHECK_EQUAL( test1.z, 3 );
}

ext: cpp

max_stars_repo_name: voivoid/advent-of-code

max_issues_repo_name: voivoid/advent-of-code

max_forks_repo_name: voivoid/advent-of-code


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

#pragma once

#include <soci.h>
#include <boost/lexical_cast.hpp>

#include <core/math/BigInt.hpp>
#include <core/utils/Exception.hpp>

namespace soci {
    template <>
    struct type_conversion<ledger::core::BigInt> {
        typedef long long base_type;
        static void from_base(base_type const & in, indicator ind, ledger::core::BigInt& out) {
            out = std::move(ledger::core::BigInt::fromScalar(in));
        }

        static void to_base(ledger::core::BigInt const & in, base_type & out, indicator & ind) {
            out = (base_type)in.toUint64();
        }
    };

    template<typename T>
    T get_number(const row& row, std::size_t pos) {
        auto prop = row.get_properties(pos);
        switch (prop.get_data_type()) {
            case dt_string:
                return boost::lexical_cast<T>(row.get<std::string>(pos));
            case dt_date: throw ledger::core::Exception(ledger::core::api::ErrorCode::RUNTIME_ERROR, "SQL date cannot be casted to number");
            case dt_double: return (T) row.get<double>(pos);
            case dt_integer: return (T) row.get<int>(pos);
            case dt_long_long: return (T) row.get<long long>(pos);
            case dt_unsigned_long_long: return (T) row.get<unsigned long long>(pos);
        }
    };
}

ext: hpp

max_stars_repo_name: phaazon/lib-ledger-core

max_issues_repo_name: phaazon/lib-ledger-core

max_forks_repo_name: phaazon/lib-ledger-core


text:
// Boost.Bimap
//
// Copyright (c) 2006-2007 Matias Capeletto
//
// 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)

/// \file container_adaptor/detail/identity_converters.hpp
/// \brief Value and iterators identity converters.

#ifndef BOOST_BIMAP_CONTAINER_ADAPTOR_DETAIL_IDENTITY_CONVERTERS_HPP
#define BOOST_BIMAP_CONTAINER_ADAPTOR_DETAIL_IDENTITY_CONVERTERS_HPP

#if defined(_MSC_VER)
#pragma once
#endif

#include <boost/config.hpp>

namespace boost {
namespace bimaps {
namespace container_adaptor {

/// \brief Details of the container adaptor toolbox

namespace detail {

/// \brief Iterator identity converter used by default in container adaptors.
/**
If Iterator and ConstIterator are of the same type one of the convert function is not
included.
                                                                                **/

template
<
    class BaseIterator              , class Iterator,
    class BaseConstIterator         , class ConstIterator
>
struct iterator_to_base_identity
{
    BaseIterator operator()(Iterator iter) const
    {
        return BaseIterator(iter);
    }

    BaseConstIterator operator()(ConstIterator iter) const
    {
        return BaseConstIterator(iter);
    }
};

#ifndef BOOST_BIMAP_DOXYGEN_WILL_NOT_PROCESS_THE_FOLLOWING_LINES

template< class BaseIterator, class Iterator >
struct iterator_to_base_identity<BaseIterator,Iterator,BaseIterator,Iterator>
{
    BaseIterator operator()(Iterator iter) const
    {
        return BaseIterator(iter);
    }
};

#endif // BOOST_BIMAP_DOXYGEN_WILL_NOT_PROCESS_THE_FOLLOWING_LINES

/// \brief Iterator from base identity converter used by default in container adaptors.
/**
If Iterator and ConstIterator are of the same type one of the convert function is not
included.
                                                                                **/

template
<
    class BaseIterator              , class Iterator,
    class BaseConstIterator         , class ConstIterator
>
struct iterator_from_base_identity
{
    Iterator operator()(BaseIterator iter) const
    {
        return Iterator(iter);
    }
    ConstIterator operator()(BaseConstIterator iter) const
    {
        return ConstIterator(iter);
    }
};

#ifndef BOOST_BIMAP_DOXYGEN_WILL_NOT_PROCESS_THE_FOLLOWING_LINES

template< class BaseIterator, class Iterator, class ConstIterator >
struct iterator_from_base_identity<BaseIterator,Iterator,BaseIterator,ConstIterator>
{
    Iterator operator()(BaseIterator iter) const
    {
        return Iterator(iter);
    }
};

#endif // BOOST_BIMAP_DOXYGEN_WILL_NOT_PROCESS_THE_FOLLOWING_LINES

/// \brief Value to base identity converter used by default in container adaptors.

template< class BaseValue, class Value >
struct value_to_base_identity
{
    BaseValue operator()(const Value & val) const
    {
        return BaseValue(val);
    }
};

#ifndef BOOST_BIMAP_DOXYGEN_WILL_NOT_PROCESS_THE_FOLLOWING_LINES

template< class Value >
struct value_to_base_identity< Value, Value >
{
    const Value & operator()(const Value & val) const
    {
        return val;
    }
};

#endif // BOOST_BIMAP_DOXYGEN_WILL_NOT_PROCESS_THE_FOLLOWING_LINES

/// \brief Value from base identity converter used by default in container adaptors.

template< class BaseValue, class Value >
struct value_from_base_identity
{
    Value operator()(const BaseValue & val) const
    {
        return Value(val);
    }
};

#ifndef BOOST_BIMAP_DOXYGEN_WILL_NOT_PROCESS_THE_FOLLOWING_LINES

template< class Value >
struct value_from_base_identity<Value,Value>
{
    Value & operator()(Value & val) const
    {
        return val;
    }

    const Value & operator()(const Value & val) const
    {
        return val;
    }
};

#endif // BOOST_BIMAP_DOXYGEN_WILL_NOT_PROCESS_THE_FOLLOWING_LINES

/// \brief Key to base identity converter used by default in container adaptors.

template< class BaseKey, class Key >
struct key_to_base_identity
{
    BaseKey operator()(const Key & k) const
    {
        return BaseKey(k);
    }
};

#ifndef BOOST_BIMAP_DOXYGEN_WILL_NOT_PROCESS_THE_FOLLOWING_LINES

template< class Key >
struct key_to_base_identity< Key, Key >
{
    // As default accept any type as key in order to allow container
    // adaptors to work with compatible key types

    template< class CompatibleKey >
    const CompatibleKey & operator()(const CompatibleKey & k) const
    {
        return k;
    }
};

#endif // BOOST_BIMAP_DOXYGEN_WILL_NOT_PROCESS_THE_FOLLOWING_LINES

} // namespace detail
} // namespace container_adaptor
} // namespace bimaps
} // namespace boost

#endif // BOOST_BIMAP_CONTAINER_ADAPTOR_DETAIL_IDENTITY_CONVERTERS_HPP


text:
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// Name        :
// Author      : Avi
// Revision    : $Revision: #10 $
//
// Copyright 2009- ECMWF.
// This software is licensed under the terms of the Apache Licence version 2.0
// which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
// In applying this licence, ECMWF does not waive the privileges and immunities
// granted to it by virtue of its status as an intergovernmental organisation
// nor does it submit to any jurisdiction.
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
#include <iostream>
#include <boost/test/unit_test.hpp>

#include "Defs.hpp"
#include "Suite.hpp"
#include "Family.hpp"

using namespace std;
using namespace ecf;

BOOST_AUTO_TEST_SUITE( NodeTestSuite )

BOOST_AUTO_TEST_CASE( test_defs_absorb )
{
	cout << "ANode:: ...test_defs_absorb\n";

	// Create a defs file corresponding to:
 	//suite suite1
 	//	family family
	//   	task t1
 	//  endfamily
	//endsuite
 	Defs theDefs;
 	{
		suite_ptr suite = theDefs.add_suite( "suite1" );
 		family_ptr fam = suite->add_family( "family" );
 		fam->add_task( "t1" );
 	}
	// Then ABSORB a new def
 	//suite suite1-n
	//   family family
	//   		task suite1_task1
 	//   endfamily
	//endsuite
 	 Defs otherDefs;
 	 {
 	 	for(int i = 0; i < 14; ++i) {
 	 		suite_ptr suite1 = otherDefs.add_suite( "suite" + boost::lexical_cast<std::string>(i) );
 	 		family_ptr fam = suite1->add_family( "family" );
 	 		fam->add_task("suite1_task1");
		}
	}

 	theDefs.absorb(&otherDefs, true);
 	BOOST_CHECK_MESSAGE(otherDefs.suiteVec().empty(),"absorb failed");
}

BOOST_AUTO_TEST_CASE( test_defs_absorb_server_user_variables )
{
   cout << "ANode:: ...test_defs_absorb_server_user_variables\n";

   Defs theDefs;
   Defs otherDefs;
   {
      otherDefs.set_server().add_or_update_user_variables("VAR1","VAL");
      otherDefs.set_server().add_or_update_user_variables("VAR2","VAL");
      otherDefs.set_server().add_or_update_user_variables("VAR3","VAL");
      otherDefs.add_suite("suite");
   }

   BOOST_CHECK_MESSAGE(theDefs.server().user_variables().empty(),"Expected no server user variables");

   theDefs.absorb(&otherDefs, true);

   BOOST_CHECK_MESSAGE(otherDefs.suiteVec().empty(),"absorb failed");
   BOOST_CHECK_MESSAGE(theDefs.suiteVec().size() == 1,"absorb failed");
   BOOST_CHECK_MESSAGE(theDefs.server().user_variables().size() == 3,"Expected 3 server user variables");
}

BOOST_AUTO_TEST_CASE( test_ECFLOW_1684 )
{
   cout << "ANode:: ...test_ECFLOW_1684\n";

   Defs defs;
   defs.add_edit_history("/","request");
   defs.add_edit_history("/a/made/up/path1","request");
   defs.add_edit_history("/a/made/up/path1","request2");
   defs.add_edit_history("/a/made/up/path2","request2");
   defs.add_edit_history("/a/made/up/path3","request2");

   BOOST_CHECK_MESSAGE(defs.get_edit_history().size() == 4, "Expected edit history of size 4 but found " << defs.get_edit_history().size());

   // This should remove all edit history, where the history paths do not exist as nodes in a defs, BUT need to leave ROOT as is though.
   defs.handle_migration();
   BOOST_CHECK_MESSAGE(defs.get_edit_history().size() == 1, "Expected edit history of size 1 but found " << defs.get_edit_history().size());
}

BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: mpartio/ecflow

max_issues_repo_name: mpartio/ecflow

max_forks_repo_name: mpartio/ecflow


text:
#ifndef MP_PYTHREAD_HPP
#define MP_PYTHREAD_HPP

#include <Python.h>
#include <pythread.h>

#include <boost/noncopyable.hpp>
#include <boost/function.hpp>
#include <boost/atomic.hpp>

struct PyMutex {
	PyThread_type_lock l;
	bool enabled;
	PyMutex(); ~PyMutex();
	PyMutex(const PyMutex&) : PyMutex() {} // ignore
	PyMutex& operator=(const PyMutex&) { return *this; } // ignore
	void lock();
	bool lock_nowait();
	void unlock();
};

struct PyScopedLock : boost::noncopyable {
	PyMutex& mutex;
	PyScopedLock(PyMutex& m);
	~PyScopedLock();
};

struct PyScopedUnlock : boost::noncopyable {
	PyMutex& mutex;
	PyScopedUnlock(PyMutex& m);
	~PyScopedUnlock();
};

struct PyScopedGIL : boost::noncopyable {
	PyGILState_STATE gstate;
	PyScopedGIL() { gstate = PyGILState_Ensure(); }
	~PyScopedGIL() { PyGILState_Release(gstate); }
};

struct PyScopedGIUnlock : boost::noncopyable {
	PyScopedGIL gstate; // in case we didn't had the GIL
	PyThreadState* _save;
	PyScopedGIUnlock() : _save(NULL) { Py_UNBLOCK_THREADS }
	~PyScopedGIUnlock() { Py_BLOCK_THREADS }
};

struct PyThread : boost::noncopyable {
	PyMutex lock;
	boost::atomic<bool> running;
	boost::atomic<bool> stopSignal;
	boost::function<void(boost::atomic<bool>& stopSignal)> func;
	long ident;
	PyThread(); ~PyThread();
	bool start();
	void wait();
	void stop();
};

extern "C" {
// this is defined in <sys/mman.h>. systems which don't have that should provide a dummy/wrapper
int mlock(const void *addr, size_t len);
}

void setCurThreadName(const std::string& name);

#endif // PYTHREAD_HPP

max_stars_repo_name: cybernetics/music-player-core

max_issues_repo_name: cybernetics/music-player-core

max_forks_repo_name: cybernetics/music-player-core


text:
// Copyright (c) 2016, Baidu.com, Inc. All Rights Reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//

#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <common/logging.h>
#include <common/string_util.h>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>

#include "nameserver/logdb.h"

namespace baidu {
namespace bfs {

LogDB::LogDB() : thread_pool_(NULL), next_index_(0), smallest_index_(-1),
                 write_log_(NULL), write_index_(NULL), marker_log_(NULL) {}

LogDB::~LogDB() {
    if (thread_pool_) {
        thread_pool_->Stop(true);
    }
    if (write_log_) fclose(write_log_);
    for (FileCache::iterator it = read_log_.begin(); it != read_log_.end(); ++it) {
        fclose((it->second).first);
        fclose((it->second).second);
    }
    if (write_index_) fclose(write_index_);
    if (marker_log_) fclose(marker_log_);
}

void LogDB::Open(const std::string& path, const DBOption& option, LogDB** dbptr) {
    *dbptr = NULL;

    LogDB* logdb = new LogDB();
    logdb->dbpath_ = path + "/";
    logdb->snapshot_interval_ = option.snapshot_interval * 1000;
    logdb->log_size_ = option.log_size << 20;
    mkdir(logdb->dbpath_.c_str(), 0755);
    if(!logdb->RecoverMarker()) {
        LOG(WARNING, "[LogDB] RecoverMarker failed reason: %s", strerror(errno));
        delete logdb;
        return;
    }
    std::map<std::string, std::string>::iterator it = logdb->markers_.find(".smallest_index_");
    if (it != logdb->markers_.end()) {
        logdb->smallest_index_ = boost::lexical_cast<int64_t>(it->second);
    }
    if (!logdb->BuildFileCache()) {
        LOG(WARNING, "[LogDB] BuildFileCache failed");
        delete logdb;
        return;
    }
    logdb->thread_pool_ = new ThreadPool(10);
    logdb->WriteMarkerSnapshot();
    *dbptr = logdb;
    return;
}

StatusCode LogDB::Write(int64_t index, const std::string& entry) {
    MutexLock lock(&mu_);
    if (index != next_index_ && smallest_index_ != -1) {
        LOG(INFO, "[LogDB] Write with invalid index = %ld smallest_index_ = %ld next_index_ = %ld ",
                index, smallest_index_, next_index_);
        return kBadParameter;
    }
    if (smallest_index_ == -1) { // empty db
        StatusCode s = WriteMarkerNoLock(".smallest_index_", common::NumToString(index));
        if (s != kOK) {
            return s;
        }
        smallest_index_ = index;
        LOG(INFO, "[LogDB] Set smallest_index_ to %ld ", smallest_index_);
    }
    uint32_t len = entry.length();
    std::string data;
    data.append(reinterpret_cast<char*>(&len), 4);
    data.append(entry);
    if (!write_log_) {
        if (!NewWriteLog(index)) {
            return kWriteError;
        }
    }
    int64_t offset = ftell(write_log_);
    if (offset > log_size_) {
        if (!NewWriteLog(index)) {
            return kWriteError;
        }
        offset = 0;
    }
    if (fwrite(data.c_str(), 1, data.length(), write_log_) != data.length() || fflush(write_log_) != 0) {
        LOG(WARNING, "[LogDB] Write log %ld failed", index);
        CloseCurrent();
        return kWriteError;
    }
    if (fwrite(reinterpret_cast<char*>(&index), 1, 8, write_index_) != 8) {
        LOG(WARNING, "[LogDB] Write index %ld failed", index);
        CloseCurrent();
        return kWriteError;
    }
    if (fwrite(reinterpret_cast<char*>(&offset), 1, 8, write_index_) != 8 || fflush(write_index_) != 0) {
        LOG(WARNING, "[LogDB] Write index %ld failed", index);
        CloseCurrent();
        return kWriteError;
    }
    next_index_ = index + 1;
    return kOK;
}

StatusCode LogDB::Read(int64_t index, std::string* entry) {
    if (read_log_.empty() || index >= next_index_ || index < smallest_index_) {
        return kNsNotFound;
    }
    FileCache::iterator it = read_log_.lower_bound(index);
    if (it == read_log_.end() || (it != read_log_.begin() && index != it->first)) {
        --it;
    }
    if (index < it->first) {
        LOG(FATAL, "[LogDB] Read cannot find index file %ld ", index);
    }
    FILE* idx_fp = (it->second).first;
    FILE* log_fp = (it->second).second;
    // find entry offset
    int offset = 16 * (index - it->first);
    int64_t read_index = -1;
    int64_t entry_offset = -1;
    {
        MutexLock lock(&mu_);
        if (fseek(idx_fp, offset, SEEK_SET) != 0) {
            LOG(FATAL, "[LogDB] Read cannot find index file %ld ", index);
        }
        StatusCode s = ReadIndex(idx_fp, index, &read_index, &entry_offset);
        if (s != kOK) {
            return s;
        }
    }
    // read log entry
    {
        MutexLock lock(&mu_);
        if(fseek(log_fp, entry_offset, SEEK_SET) != 0) {
            LOG(WARNING, "[LogDB] Read %ld with invalid offset %ld ", index, entry_offset);
            return kReadError;
        }
        int ret = ReadOne(log_fp, entry);
        if (ret <= 0) {
            LOG(WARNING, "[LogDB] Read log error %ld ", index);
            return kReadError;
        }
    }
    return kOK;
}

StatusCode LogDB::WriteMarkerNoLock(const std::string& key, const std::string& value) {
    if (marker_log_ == NULL) {
        marker_log_ = fopen((dbpath_ + "marker.mak").c_str(), "a");
        if (marker_log_ == NULL) {
            LOG(WARNING, "[LogDB] open marker.mak failed %s", strerror(errno));
            return kWriteError;
        }
    }
    std::string data;
    uint32_t len = 4 + key.length() + 4 + value.length();
    data.append(reinterpret_cast<char*>(&len), 4);
    EncodeMarker(MarkerEntry(key, value), &data);
    if (fwrite(data.c_str(), 1, data.length(), marker_log_) != data.length()
        || fflush(marker_log_) != 0) {
        LOG(WARNING, "[LogDB] WriteMarker failed key = %s value = %s", key.c_str(), value.c_str());
        return kWriteError;
    }
    fflush(marker_log_);
    markers_[key] = value;
    return kOK;
}

StatusCode LogDB::WriteMarker(const std::string& key, const std::string& value) {
    MutexLock lock(&mu_);
    return WriteMarkerNoLock(key, value);
}

StatusCode LogDB::WriteMarker(const std::string& key, int64_t value) {
    return WriteMarker(key, std::string(reinterpret_cast<char*>(&value), 8));
}

StatusCode LogDB::ReadMarker(const std::string& key, std::string* value) {
    MutexLock lock(&mu_);
    std::map<std::string, std::string>::iterator it = markers_.find(key);
    if (it == markers_.end()) {
        return kNsNotFound;
    }
    *value = it->second;
    return kOK;
}

StatusCode LogDB::ReadMarker(const std::string& key, int64_t* value) {
    std::string v;
    StatusCode status = ReadMarker(key, &v);
    if (status != kOK) {
        return status;
    }
    memcpy(value, &(v[0]), 8);
    return kOK;
}

StatusCode LogDB::GetLargestIdx(int64_t* value) {
    MutexLock lock(&mu_);
    if (smallest_index_ == next_index_) {
        *value = -1;
        return kNsNotFound;
    }
    *value = next_index_ - 1;
    return kOK;
}

StatusCode LogDB::DeleteUpTo(int64_t index) {
    if (index < smallest_index_) {
        return kOK;
    }
    if (index >= next_index_) {
        LOG(INFO, "[LogDB] DeleteUpTo over limit index = %ld next_index_ = %ld", index, next_index_);
        return kBadParameter;
    }
    MutexLock lock(&mu_);
    smallest_index_ = index + 1;
    WriteMarkerNoLock(".smallest_index_", common::NumToString(smallest_index_));
    FileCache::reverse_iterator upto = read_log_.rbegin();
    while (upto != read_log_.rend()) {
        if (upto->first <= index) break;
        ++upto;
    }
    if (upto == read_log_.rend()) {
        return kOK;
    }
    int64_t upto_index = upto->first;
    FileCache::iterator it = read_log_.begin();
    while (it->first != upto_index) {
        fclose((it->second).first);
        fclose((it->second).second);
        std::string log_name, idx_name;
        FormLogName(it->first, &log_name, &idx_name);
        remove(log_name.c_str());
        remove(idx_name.c_str());
        read_log_.erase(it++);
    }
    LOG(INFO, "[LogDB] DeleteUpTo done smallest_index_ = %ld next_index_ = %ld",
            smallest_index_, next_index_);
    return kOK;
}

StatusCode LogDB::DeleteFrom(int64_t index) {
    if (index >= next_index_) {
        return kOK;
    }
    if (index < smallest_index_) {
        LOG(INFO, "[LogDB] DeleteUpTo over limit index = %ld smallest_index_ = %ld next_index_ = %ld",
                index, smallest_index_, next_index_);
        return kBadParameter;
    }
    MutexLock lock(&mu_);
    FileCache::iterator from = read_log_.lower_bound(index);
    bool need_truncate = index != from->first;
    for (FileCache::iterator it = from; it != read_log_.end(); ++it) {
        fclose((it->second).first);
        fclose((it->second).second);
        std::string log_name, idx_name;
        FormLogName(it->first, &log_name, &idx_name);
        remove(log_name.c_str());
        remove(idx_name.c_str());
    }
    CloseCurrent();
    // truancate the last log and open it for read
    read_log_.erase(from, read_log_.end());
    if (need_truncate && !read_log_.empty()) {
        FileCache::reverse_iterator it = read_log_.rbegin();
        int offset = 16 * (index - it->first);
        fseek((it->second).first, offset, SEEK_SET);
        char buf[16];
        int len = fread(buf, 1, 16, (it->second).first);
        assert(len == 16);
        int64_t tmp_offset;
        memcpy(&tmp_offset, buf + 8, 8);
        fclose((it->second).first);
        fclose((it->second).second);
        std::string log_name, idx_name;
        FormLogName(it->first, &log_name, &idx_name);
        truncate(log_name.c_str(), tmp_offset);
        truncate(idx_name.c_str(), offset);
        (it->second).first = fopen(idx_name.c_str(), "r");
        (it->second).second = fopen(log_name.c_str(), "r");
    }
    next_index_ = index;
    LOG(INFO, "[LogDB] DeleteFrom done smallest_index_ = %ld next_index_ = %ld",
            smallest_index_, next_index_);
    return kOK;
}

bool LogDB::BuildFileCache() {
    // build log file cache
    struct dirent *entry = NULL;
    DIR *dir_ptr = opendir(dbpath_.c_str());
    if (dir_ptr == NULL) {
        LOG(WARNING, "[LogDB] open dir failed %s", dbpath_.c_str());
        return false;
    }
    bool error = false;
    while ((entry = readdir(dir_ptr)) != NULL) {
        size_t idx = std::string(entry->d_name).find(".idx");
        if (idx != std::string::npos) {
            std::string file_name = std::string(entry->d_name);
            int64_t index = boost::lexical_cast<int64_t>(file_name.substr(0, idx));
            std::string log_name, idx_name;
            FormLogName(index, &log_name, &idx_name);
            FILE* idx_fp = fopen(idx_name.c_str(), "r");
            if (idx_fp == NULL) {
                LOG(WARNING, "[LogDB] open index file failed %s", file_name.c_str());
                error = true;
                break;
            }
            FILE* log_fp = fopen(log_name.c_str(), "r");
            if (log_fp == NULL) {
                LOG(WARNING, "[LogDB] open log file failed %s", file_name.c_str());
                fclose(idx_fp);
                error = true;
                break;
            }
            read_log_[index] = std::make_pair(idx_fp, log_fp);
            LOG(INFO, "[LogDB] Add file cache %ld to %s ", index, file_name.c_str());
        }
    }
    closedir(dir_ptr);
    // check log & idx match, build largest index
    if (error || !CheckLogIdx()) {
        LOG(WARNING, "[LogDB] BuildFileCache failed error = %d", error);
        for (FileCache::iterator it = read_log_.begin(); it != read_log_.end(); ++it) {
            fclose((it->second).first);
            fclose((it->second).second);
        }
        read_log_.clear();
        return false;
    }
    return true;
}

bool LogDB::CheckLogIdx() {
    if (read_log_.empty()) {
        if (smallest_index_ == -1) {
            next_index_ = 0;
        } else {
            next_index_ = smallest_index_;
        }
        LOG(INFO, "[LogDB] No previous log, next_index_ = %ld ", next_index_);
        return true;
    }
    FileCache::iterator it = read_log_.begin();
    if (smallest_index_ < it->first) {
        LOG(WARNING, "[LogDB] log does not contain smallest_index_ %ld %ld",
                smallest_index_, it->first);
        return false;
    }
    next_index_ = it->first;
    bool error = false;
    for (; it != read_log_.end(); ++it) {
        if (it->first != next_index_) {
            LOG(WARNING, "[LogDB] log is not continous, current index %ld ", it->first);
            return false;
        }
        FILE* idx = (it->second).first;
        FILE* log = (it->second).second;
        fseek(idx, 0, SEEK_END);
        int idx_size = ftell(idx);
        if (idx_size < 16) {
            LOG(WARNING, "[LogDB] index file too small %ld ", it->first);
            error = true;
            break;
        }
        int reminder = idx_size % 16;
        if (reminder != 0) {
            LOG(INFO, "[LogDB] incomplete index file %ld.idx ", it->first);
        }
        fseek(idx, idx_size - 16 - reminder, SEEK_SET);
        int64_t expect_index = it->first + (idx_size / 16) - 1;
        int64_t read_index = -1;
        int64_t offset = -1;
        StatusCode s = ReadIndex(idx, expect_index, &read_index, &offset);
        if (s != kOK) {
            LOG(WARNING, "[LogDB] check index file failed %ld.idx %s",
                    it->first, StatusCode_Name(s).c_str());
            return false;
        }
        fseek(log, 0, SEEK_END);
        int log_size = ftell(log);
        fseek(log, offset, SEEK_SET);
        int len;
        int ret = fread(&len, 1, 4, log);
        if (ret < 4 || (offset + 4 + len > log_size)) {
            LOG(WARNING, "[LogDB] incomplete log %ld ", it->first);
            return false;
        }
        next_index_ = expect_index + 1;
    }
    if (error) {
        if (++it != read_log_.end()) {
            return false;
        }
        FileCache::reverse_iterator rit = read_log_.rbegin();
        fclose((rit->second).first);
        fclose((rit->second).second);
        read_log_.erase(rit->first);
    }
    LOG(INFO, "[LogDB] Set next_index_ to %ld", next_index_);
    return true;
}

bool LogDB::RecoverMarker() {
    // recover markers
    FILE* fp = fopen((dbpath_ + "marker.mak").c_str(), "r");
    if (fp == NULL) {
        if (errno == ENOENT) {
            fp = fopen((dbpath_ + "marker.tmp").c_str(), "r");
        }
    }
    if (fp == NULL) {
        LOG(INFO, "[LogDB] No marker to recover");
        return errno == ENOENT;
    }
    std::string data;
    while (true) {
        int ret = ReadOne(fp, &data);
        if (ret == 0)  break;
        if (ret < 0) {
            LOG(WARNING, "[LogDB] RecoverMarker failed while reading");
            fclose(fp);
            return false;
        }
        MarkerEntry mark;
        DecodeMarker(data, &mark);
        markers_[mark.key] = mark.value;
    }
    fclose(fp);
    LOG(INFO, "[LogDB] Recover markers done");

    rename((dbpath_ + "marker.tmp").c_str(), (dbpath_ + "marker.mak").c_str());
    return true;
}

void LogDB::WriteMarkerSnapshot() {
    MutexLock lock(&mu_);
    FILE* fp = fopen((dbpath_ + "marker.tmp").c_str(), "w");
    if (fp == NULL) {
        LOG(WARNING, "[LogDB] open marker.tmp failed %s", strerror(errno));
        return;
    }
    std::string data;
    for (std::map<std::string, std::string>::iterator it = markers_.begin();
            it != markers_.end(); ++it) {
        MarkerEntry marker(it->first, it->second);
        uint32_t len = 4 + (it->first).length() + 4 + (it->second).length();
        data.clear();
        data.append(reinterpret_cast<char*>(&len), 4);
        EncodeMarker(marker, &data);
        if (fwrite(data.c_str(), 1, data.length(), fp) != data.length() || fflush(fp) != 0) {
            LOG(WARNING, "[LogDB] write marker.tmp failed %s", strerror(errno));
            fclose(fp);
            return;
        }
    }
    fclose(fp);
    if (marker_log_) {
        fclose(marker_log_);
        marker_log_ = NULL;
    }
    rename((dbpath_ + "marker.tmp").c_str(), (dbpath_ + "marker.mak").c_str());
    marker_log_ = fopen((dbpath_ + "marker.mak").c_str(), "a");
    if (marker_log_ == NULL) {
        LOG(WARNING, "[LogDB] open marker.mak failed %s", strerror(errno));
        return;
    }
    LOG(INFO, "[LogDB] WriteMarkerSnapshot done");
    thread_pool_->DelayTask(snapshot_interval_, boost::bind(&LogDB::WriteMarkerSnapshot, this));
}

void LogDB::CloseCurrent() {
    if (write_log_) {
        fclose(write_log_);
        write_log_ = NULL;
    }
    if (write_index_) {
        fclose(write_index_);
        write_index_ = NULL;
    }
}

int LogDB::ReadOne(FILE* fp, std::string* data) {
    int len;
    int ret = fread(&len, 1, 4, fp);
    if (ret == 0) {
        return 0;
    }
    if (ret != 4) return -1;
    char* buf = new char[len];
    ret = fread(buf, 1, len, fp);
    if (ret != len) {
        LOG(WARNING, "Read(%d) return %d", len, ret);
        delete[] buf;
        return -1;
    }
    data->clear();
    data->assign(buf, len);
    delete[] buf;
    return len;
}

StatusCode LogDB::ReadIndex(FILE* fp, int64_t expect_index, int64_t* index, int64_t* offset) {
    char buf[16];
    int ret = fread(buf, 1, 16, fp);
    if (ret == 0) {
        return kNsNotFound;
    } else if (ret != 16) {
        LOG(WARNING, "[logdb] Read index file error %ld", expect_index);
        return kReadError;
    }
    memcpy(index, buf, 8);
    memcpy(offset, buf + 8, 8);
    if (expect_index != *index) {
        LOG(WARNING, "[LogDB] Index file mismatch %ld ", index);
        return kReadError;
    }
    return kOK;
}

void LogDB::EncodeMarker(const MarkerEntry& marker, std::string* data) {
    int klen = (marker.key).length();
    int vlen = (marker.value).length();
    data->append(reinterpret_cast<char*>(&klen), 4);
    data->append(marker.key);
    data->append(reinterpret_cast<char*>(&vlen), 4);
    data->append(marker.value);
}

void LogDB::DecodeMarker(const std::string& data, MarkerEntry* marker) { // data = klen + k + vlen + v
    int klen;
    memcpy(&klen, &(data[0]), 4);
    (marker->key).assign(data.substr(4, klen));
    int vlen;
    memcpy(&vlen, &(data[4 + klen]), 4);
    (marker->value).assign(data.substr(4 + klen + 4, vlen));
}

bool LogDB::NewWriteLog(int64_t index) {
    if (write_log_) fclose(write_log_);
    if (write_index_) fclose(write_index_);
    std::string log_name, idx_name;
    FormLogName(index, &log_name, &idx_name);
    write_log_ = fopen(log_name.c_str(), "w");
    write_index_ = fopen(idx_name.c_str(), "w");
    FILE* idx_fp = fopen(idx_name.c_str(), "r");
    FILE* log_fp = fopen(log_name.c_str(), "r");
    if (!(write_log_ && write_index_ && idx_fp && log_fp)) {
        if (write_log_) fclose(write_log_);
        if (write_index_) fclose(write_index_);
        if (idx_fp) fclose(idx_fp);
        if (log_fp) fclose(log_fp);
        LOG(WARNING, "[logdb] open log/idx file failed %ld %s", index, strerror(errno));
        return false;
    }
    read_log_[index] = std::make_pair(idx_fp, log_fp);
    return true;
}

void LogDB::FormLogName(int64_t index, std::string* log_name, std::string* idx_name) {
    log_name->clear();
    log_name->append(dbpath_);
    log_name->append(common::NumToString(index));
    log_name->append(".log");

    idx_name->clear();
    idx_name->append(dbpath_);
    idx_name->append(common::NumToString(index));
    idx_name->append(".idx");
}

} // namespace bfs
} // namespace baidu


text:
#include <ripple/beast/unit_test.h>
#include <test/jtx.h>
#include <test/jtx/Env.h>
#include <ripple/beast/utility/temp_dir.h>
#include <ripple/protocol/jss.h>
#include <ripple/protocol/SField.h>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <fstream>

namespace ripple {

class LedgerLoad_test : public beast::unit_test::suite
{
    auto static ledgerConfig(
        std::unique_ptr<Config> cfg,
        std::string const& dbPath,
        std::string const& ledger,
        Config::StartUpType type)
    {
        cfg->START_LEDGER = ledger;
        cfg->START_UP = type;
        assert(! dbPath.empty());
        cfg->legacy("database_path", dbPath);
        return cfg;
    }

    struct SetupData
    {
        std::string const dbPath;
        std::string ledgerFile;
        Json::Value ledger;
        Json::Value hashes;
    };

    SetupData
    setupLedger(beast::temp_dir const& td)
    {
        using namespace test::jtx;
        SetupData retval = {td.path()};

        retval.ledgerFile = td.file("ledgerdata.json");

        Env env {*this};
        Account prev;

        for(auto i = 0; i < 20; ++i)
        {
            Account acct {"A" + std::to_string(i)};
            env.fund(XRP(10000), acct);
            env.close();
            if(i > 0)
            {
                env.trust(acct["USD"](1000), prev);
                env(pay(acct, prev, acct["USD"](5)));
            }
            env(offer(acct, XRP(100), acct["USD"](1)));
            env.close();
            prev = std::move(acct);
        }

        retval.ledger = env.rpc ("ledger", "current", "full") [jss::result];
        BEAST_EXPECT(retval.ledger[jss::ledger][jss::accountState].size() == 101);

        retval.hashes = [&] {
            for(auto const& it : retval.ledger[jss::ledger][jss::accountState])
            {
                if(it[sfLedgerEntryType.fieldName] == jss::LedgerHashes)
                    return it[sfHashes.fieldName];
            }
            return Json::Value {};
        }();

        BEAST_EXPECT(retval.hashes.size() == 41);

        std::ofstream o (retval.ledgerFile, std::ios::out | std::ios::trunc);
        o << to_string(retval.ledger);
        o.close();
        return retval;
    }

    void
    testLoad (SetupData const& sd)
    {
        testcase ("Load a saved ledger");
        using namespace test::jtx;

        Env env(*this,
            envconfig( ledgerConfig,
                sd.dbPath, sd.ledgerFile, Config::LOAD_FILE));
        auto jrb = env.rpc ( "ledger", "current", "full") [jss::result];
        BEAST_EXPECT(
            sd.ledger[jss::ledger][jss::accountState].size() ==
            jrb[jss::ledger][jss::accountState].size());
    }

    void
    testBadFiles (SetupData const& sd)
    {
        testcase ("Load ledger: Bad Files");
        using namespace test::jtx;
        using namespace boost::filesystem;

        except ([&]
        {
            Env env(*this,
                envconfig( ledgerConfig,
                    sd.dbPath, "", Config::LOAD_FILE));
        });

        except ([&]
        {
            Env env(*this,
                envconfig( ledgerConfig,
                    sd.dbPath, "badfile.json", Config::LOAD_FILE));
        });

        boost::system::error_code ec;
        auto ledgerFileCorrupt =
            boost::filesystem::path{sd.dbPath} / "ledgerdata_bad.json";
        copy_file(
            sd.ledgerFile,
            ledgerFileCorrupt,
            copy_option::overwrite_if_exists,
            ec);
        if(! BEAST_EXPECTS(!ec, ec.message()))
            return;
        auto filesize = file_size(ledgerFileCorrupt, ec);
        if(! BEAST_EXPECTS(!ec, ec.message()))
            return;
        resize_file(ledgerFileCorrupt, filesize - 10, ec);
        if(! BEAST_EXPECTS(!ec, ec.message()))
            return;

        except ([&]
        {
            Env env(*this,
                envconfig( ledgerConfig,
                    sd.dbPath, ledgerFileCorrupt.string(), Config::LOAD_FILE));
        });
    }

    void
    testLoadByHash (SetupData const& sd)
    {
        testcase ("Load by hash");
        using namespace test::jtx;

        auto ledgerHash = to_string(sd.hashes[sd.hashes.size()-1]);
        boost::erase_all(ledgerHash, "\"");
        Env env(*this,
            envconfig( ledgerConfig,
                sd.dbPath, ledgerHash, Config::LOAD));
        auto jrb = env.rpc ( "ledger", "current", "full") [jss::result];
        BEAST_EXPECT(jrb[jss::ledger][jss::accountState].size() == 97);
        BEAST_EXPECT(
            jrb[jss::ledger][jss::accountState].size() <=
            sd.ledger[jss::ledger][jss::accountState].size());
    }

    void
    testLoadLatest (SetupData const& sd)
    {
        testcase ("Load by keyword");
        using namespace test::jtx;

        Env env(*this,
            envconfig( ledgerConfig,
                sd.dbPath, "latest", Config::LOAD));
        auto jrb = env.rpc ( "ledger", "current", "full") [jss::result];
        BEAST_EXPECT(
            sd.ledger[jss::ledger][jss::accountState].size() ==
            jrb[jss::ledger][jss::accountState].size());
    }

    void
    testLoadIndex (SetupData const& sd)
    {
        testcase ("Load by index");
        using namespace test::jtx;

        Env env(*this,
            envconfig( ledgerConfig,
                sd.dbPath, "43", Config::LOAD));
        auto jrb = env.rpc ( "ledger", "current", "full") [jss::result];
        BEAST_EXPECT(
            sd.ledger[jss::ledger][jss::accountState].size() ==
            jrb[jss::ledger][jss::accountState].size());
    }

public:
    void run () override
    {
        beast::temp_dir td;
        auto sd = setupLedger(td);

        testLoad (sd);
        testBadFiles (sd);
        testLoadByHash (sd);
        testLoadLatest (sd);
        testLoadIndex (sd);
    }
};

BEAST_DEFINE_TESTSUITE (LedgerLoad, app, ripple);

}

max_stars_repo_name: dfm-official/dfm

max_issues_repo_name: dfm-official/dfm

max_forks_repo_name: dfm-official/dfm


text:
#include "mutation_annotated_tree.hpp"
#include "Fitch_Sankoff.hpp"
#include <cstdio>
#include <iostream>
#include <string>
#include <tbb/blocked_range.h>
#include <tbb/concurrent_unordered_map.h>
#include <tbb/concurrent_vector.h>
#include <tbb/pipeline.h>
#include "tbb/parallel_for_each.h"
#include <tbb/parallel_for.h>
#include <boost/iostreams/filtering_stream.hpp>
#include <fstream>
#include <unordered_set>
#include "check_samples.hpp"
#include "tree_rearrangement_internal.hpp"
#include "apply_move/apply_move.hpp"
#include <tbb/queuing_rw_mutex.h>

namespace MAT=Mutation_Annotated_Tree;
static bool no_valid_mut(MAT::Node* node){
    for(const auto& mut:node->mutations){
        if (mut.is_valid()) {
            return false;
        }
    }
    return true;
}
/**
 * @brief Clean nodes with no valid mutation
 * @param this_node subtree rooted at this_node will be cleaned
 * @param tree
 * @param[out] changed_nodes nodes with their children set changed, need fitch sankoff backward pass
 * @param[out] node_with_inconsistent_state nodes with parent state change, need forward pass
 */
static void clean_up_internal_nodes(MAT::Node* this_node,MAT::Tree& tree,std::unordered_set<std::string>& changed_nodes,std::unordered_set<std::string>& node_with_inconsistent_state){

    std::vector<MAT::Node *> &parent_children = this_node->parent->children;
    std::vector<MAT::Node *> this_node_ori_children = this_node->children;
    if (this_node->parent&&(((!this_node->is_leaf())&&no_valid_mut(this_node)))) {
        //Remove this node
        this_node->parent->changed=true;
        auto iter = std::find(parent_children.begin(), parent_children.end(),
                              this_node);
        assert(iter != parent_children.end());
        parent_children.erase(iter);
        changed_nodes.erase(this_node->identifier);
        node_with_inconsistent_state.erase(this_node->identifier);
        //its parent have changed children set
        changed_nodes.insert(this_node->parent->identifier);
        tree.all_nodes.erase(this_node->identifier);
        //promote all its children, no need to change their mutation vector, as this_node assumed to have no valid mutations
        for (MAT::Node *child : this_node_ori_children) {
            child->changed=true;
            child->parent = this_node->parent;
            parent_children.push_back(child);
        }
        tree.all_nodes.erase(this_node->identifier);
        delete this_node;
    }

    for (MAT::Node *child : this_node_ori_children) {
        //recurse down
        clean_up_internal_nodes(child, tree,changed_nodes,node_with_inconsistent_state);
    }
}
//For removing nodes with no valid mutations between rounds
void clean_tree(MAT::Tree& t){
    std::unordered_set<std::string> changed_nodes;
    std::unordered_set<std::string> node_with_inconsistent_states;
        clean_up_internal_nodes(t.root, t, changed_nodes,node_with_inconsistent_states);
#ifdef CHECK_STATE_REASSIGN
    MAT::Tree new_tree=reassign_state_full(t);
#endif
    //almost the same as apply move
    std::vector<MAT::Node*> for_reassign;
    for(auto node_str:changed_nodes){
        auto node=t.get_node(node_str);
        for_reassign.push_back(node);
    }
    auto cleaned_count=for_reassign.size();
    fprintf(stderr, "%zu nodes cleaned\n",cleaned_count);
    t.depth_first_expansion();
    if(!for_reassign.empty()){
        std::vector<Altered_Node_t> nodes_with_changed_states_out;
        reassign_backward_pass(for_reassign, nodes_with_changed_states_out
#ifdef CHECK_STATE_REASSIGN
        ,new_tree
#endif
        );
        for(const auto& node_id:node_with_inconsistent_states){
            clean_up_src_states(t.get_node(node_id), nodes_with_changed_states_out);
        }
        if (!nodes_with_changed_states_out.empty()) {
            forward_pass(nodes_with_changed_states_out
#ifdef CHECK_STATE_REASSIGN
                     ,
                     new_tree
#endif
        );
    }}
#ifdef CHECK_STATE_REASSIGN
    compare_mutation_tree(t, new_tree);
#endif
    if (cleaned_count) {
        clean_tree(t);
    }
}

void populate_mutated_pos(const Original_State_t& origin_state){
    tbb::parallel_for_each(origin_state.begin(),origin_state.end(),[](const std::pair<std::string, Mutation_Set>& sample_mutations){
        for (const MAT::Mutation &m : sample_mutations.second) {
            //reader lock to find this position if it is already inserted
            auto iter=mutated_positions.find(m);
            tbb::concurrent_unordered_map<std::string, nuc_one_hot>* samples;
            if (iter==mutated_positions.end()) {
                //not found, need to insert, with writer lock
                samples=new tbb::concurrent_unordered_map<std::string, nuc_one_hot>;
                auto emplace_result=mutated_positions.emplace(m,samples);
                if (!emplace_result.second) {
                    delete samples;
                    samples=emplace_result.first->second;
                }
            }else {
                samples=iter->second;
            }
            //add sample to mutation mapping
            samples->emplace(sample_mutations.first,
                                 m.get_all_major_allele());
        }
    });
    //clean up all mutexes
}
//Use Full fitch sankoff to reassign state from scratch
static void reassign_states(MAT::Tree& t, Original_State_t& origin_states){
    auto bfs_ordered_nodes = t.breadth_first_expansion();

    check_samples(t.root, origin_states, &t);
    std::unordered_set<std::string> ignored;
    std::unordered_set<std::string> ignored2;
    clean_up_internal_nodes(t.root,t,ignored,ignored2);
    //populate_mutated_pos(origin_states);
    bfs_ordered_nodes = t.breadth_first_expansion();
    std::vector<tbb::concurrent_vector<Mutation_Annotated_Tree::Mutation>>
        output(bfs_ordered_nodes.size());
    //get mutation vector
    std::vector<backward_pass_range> child_idx_range;
    std::vector<forward_pass_range> parent_idx;
    std::vector<std::pair<MAT::Mutation,tbb::concurrent_vector<std::pair<size_t,char>>>> pos_mutated(MAT::Mutation::refs.size());
    tbb::parallel_for_each(origin_states.begin(),origin_states.end(),[&pos_mutated,t](const std::pair<std::string, Mutation_Set>& sample_mutations){
        for (const MAT::Mutation &m : sample_mutations.second) {
            pos_mutated[m.get_position()].first=m;
            pos_mutated[m.get_position()].second.emplace_back(t.get_node(sample_mutations.first)->bfs_index,m.get_all_major_allele());
        }
    });
    Fitch_Sankoff_prep(bfs_ordered_nodes,child_idx_range, parent_idx);
    tbb::parallel_for(
        tbb::blocked_range<size_t>(0,pos_mutated.size()),
        [&output,&child_idx_range,&parent_idx,&pos_mutated](const tbb::blocked_range<size_t>& in) {
            for (size_t idx=in.begin(); idx<in.end(); idx++) {
                if (pos_mutated[idx].second.empty()) {
                    continue;
                }
                std::vector<std::pair<long,nuc_one_hot>> mutated_nodes_idx(pos_mutated[idx].second.begin(),pos_mutated[idx].second.end());
                std::sort(mutated_nodes_idx.begin(),mutated_nodes_idx.end(),mutated_t_comparator());
                mutated_nodes_idx.emplace_back(0,0xf);
                Fitch_Sankoff_Whole_Tree(child_idx_range,parent_idx, pos_mutated[idx].first, mutated_nodes_idx,
                                     output);

            }
        });
    tbb::affinity_partitioner ap;
    //sort and fill
    tbb::parallel_for(
        tbb::blocked_range<size_t>(0, bfs_ordered_nodes.size()),
        [&bfs_ordered_nodes, &output](tbb::blocked_range<size_t> r) {
            for (size_t i = r.begin(); i < r.end(); i++) {
                const auto &to_refill = output[i];
                bfs_ordered_nodes[i]->refill(to_refill.begin(), to_refill.end(),
                                             to_refill.size());
            }
        },
        ap);
}
//load from usher compatible pb
Mutation_Annotated_Tree::Tree load_tree(const std::string& path,Original_State_t& origin_states){
    fputs("Start loading protobuf\n",stderr);
    Mutation_Annotated_Tree::Tree t =
        Mutation_Annotated_Tree::load_mutation_annotated_tree(path);
    fputs("Finished loading protobuf, start reassigning states\n",stderr);
    reassign_states(t, origin_states);
    fputs("Finished reassigning states\n",stderr);
    fprintf(stderr, "original score:%zu\n", t.get_parsimony_score());
    return t;
}

max_stars_repo_name: bgruening/usher

max_issues_repo_name: bgruening/usher

max_forks_repo_name: bgruening/usher


text:
#include "mnemosyne/record.hpp"
#include "mnemosyne/mnemosyne.hpp"
#include <iostream>
#include <ndn-cxx/security/signing-helpers.hpp>
#include <ndn-cxx/security/validator-config.hpp>
#include <ndn-cxx/security/validator.hpp>
#include <ndn-cxx/util/scheduler.hpp>
#include <boost/asio/io_service.hpp>
#include <ndn-cxx/util/io.hpp>
#include <random>

using namespace mnemosyne;

std::random_device rd;  //Will be used to obtain a seed for the random number engine
std::mt19937 random_gen(rd()); //Standard mersenne_twister_engine seeded with rd()

void periodicAddRecord(KeyChain& keychain, shared_ptr<MnemosyneDagSync> ledger, Scheduler &scheduler) {
    std::uniform_int_distribution<int> distribution(0, INT_MAX);
    Data data("/a/b/" + std::to_string(distribution(random_gen)));
    data.setContent(makeStringBlock(tlv::Content, std::to_string(distribution(random_gen))));
    keychain.sign(data, signingWithSha256());

    Record record(ledger->getPeerPrefix(), data);
    ledger->createRecord(record);

    // schedule for the next record generation
    scheduler.schedule(time::seconds(5), [&keychain, ledger, &scheduler] { periodicAddRecord(keychain, ledger, scheduler); });
}

int main(int argc, char **argv) {
    if (argc < 2) {
        fprintf(stderr, "Usage: %s identity\n", argv[0]);
        return 1;
    }
    std::string identity = argv[1];
    boost::asio::io_service ioService;
    Face face(ioService);
    security::KeyChain keychain;
    std::shared_ptr<Config> config = nullptr;
    std::shared_ptr<ndn::security::Validator> validator;
    try {
        config = Config::CustomizedConfig("/ndn/broadcast/mnemosyne-dag", "/ndn/broadcast/mnemosyne", identity,
                                          std::string("/tmp/mnemosyne-db/" + identity.substr(identity.rfind('/'))));
        auto configValidator = std::make_shared<ndn::security::ValidatorConfig>(face);
        configValidator->load("./test/loggers.schema");
        validator = configValidator;
        mkdir("/tmp/mnemosyne-db/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
    }
    catch (const std::exception &e) {
        std::cout << e.what() << std::endl;
        return 1;
    }
    auto ledger = std::make_shared<MnemosyneDagSync>(*config, keychain, face, validator);

    Scheduler scheduler(ioService);
    periodicAddRecord(keychain, ledger, scheduler);

    face.processEvents();
    return 0;
}

max_stars_repo_name: UCLA-IRL/Mnemosyne

max_issues_repo_name: UCLA-IRL/Mnemosyne

max_forks_repo_name: UCLA-IRL/Mnemosyne


text:
/*
 *  Copyright (c) 2009, Rene Wagner
 *  All rights reserved.
 *
 *  Author: Rene Wagner <rw@nelianur.org>
 *
 *  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 Rene Wagner nor the names of any
 *     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.
 */

#ifndef __UKFOM_LAPACK_INVERT_HPP__
#define __UKFOM_LAPACK_INVERT_HPP__

#include "lapack.h"

#include <Eigen/Core>

namespace ukf {
namespace lapack {

// import most common Eigen types
USING_PART_OF_NAMESPACE_EIGEN

template<size_t P>
Matrix<double, P, P> invert(const Matrix<double, P, P> &m)
{
	Matrix<double, P, P> tmp = m;

	/* first call DGETRF */
	int M = tmp.rows();
	int N = tmp.cols();
	int LDA = tmp.stride();
	int IPIV[M];

	int INFO;

	dgetrf_(&M, &N, tmp.data(), &LDA, IPIV, &INFO);

	if(INFO != 0)
		throw "dgetrf failed";

	/* now call DGETRI */
	int LWORK = 64 * N; // FIXME: retrieve block size from ILAENV
	double WORK[LWORK];

	dgetri_(&N, tmp.data(), &LDA, IPIV, WORK, &LWORK, &INFO);

	if(INFO != 0)
		throw "dgetri failed";

	return tmp;
}

} // namespace lapack
} // namespace ukf

#endif // __UKFOM_LAPACK_INVERT_HPP__

max_stars_repo_name: mfkiwl/ADEKF

max_issues_repo_name: mfkiwl/ADEKF

max_forks_repo_name: mfkiwl/ADEKF


text:
// -*- mode: c++; fill-column: 80; indent-tabs-mode: nil; -*-
#ifndef MXPFIT_JACOBI_SVD_HPP
#define MXPFIT_JACOBI_SVD_HPP

#include <cassert>
#ifdef DEBUG
#include <iomanip>
#include <iostream>
#endif /* DEBUG */

#include <Eigen/Core>

namespace mxpfit
{
namespace detail
{
//
// Make Jacobi rotation matrix \c J of the form
//
//  J = [ cs conj(sn)]
//      [-sn conj(cs)]
//
// that diagonalize the 2x2 matirix
//
// B = [     a  c]
//     [conj(c) b]
//
// as D = J.t() * B * J
//
// J.t() = [ conj(cs)    -conj(sn)] = J.inv()
//         [      sn           cs ]
//
// J * [a11 a12] = [ cs*a11 + conj(sn)*a21   cs*a12 + conj(sn)*a22]
//     [a21 a22]   [-sn*a11 + conj(cs)*a21  -sn*a12 + conj(cs)*a22]
//
// [a11 a12] * J = [     cs *a11 -      sn *a12        cs *a21 -      sn *a22]
// [a21 a22]       [conj(sn)*a11 +E conj(cs)*a12  -conj(sn)*a21 + conj(cs)*a22]
//
template <typename RealT, typename ScalarT>
bool make_jacobi_rotation(RealT a, RealT b, ScalarT c, RealT& cs, ScalarT& sn)
{
    using Eigen::numext::abs;
    using Eigen::numext::abs2;
    using Eigen::numext::conj;
    using Eigen::numext::sqrt;

    constexpr const RealT one = RealT(1);

    if (c == ScalarT())
    {
        cs = one;
        sn = ScalarT();
        return false;
    }

    auto zeta = (a - b) / (RealT(2) * abs(c));
    auto w    = sqrt(abs2(zeta) + one);
    auto t    = (zeta > RealT() ? one / (zeta + w) : one / (zeta - w));

    cs = one / sqrt(abs2(t) + one);
    sn = -t * cs * (conj(c) / abs(c));

    return true;
}

template <typename T>
struct one_sided_jacobi_helper
{
    using Index      = Eigen::Index;
    using Scalar     = T;
    using RealScalar = typename Eigen::NumTraits<Scalar>::Real;
    //
    // Sum of the square of the absolute value of the elments in the i-th column
    // of the matrix G, i.e. \f$ \sum_{k=0}^{n-1} |G_{ki}|^{2}. \f$
    //
    template <typename MatG>
    static RealScalar submat_diag(const Eigen::MatrixBase<MatG>& G, Index i)
    {
        return G.col(i).squaredNorm();
    }

    template <typename MatG>
    static Scalar submat_offdiag(const Eigen::MatrixBase<MatG>& G, Index i,
                                 Index j)
    {
        return G.col(i).dot(G.col(j));
    }

    template <typename MatU>
    static void update_vecs(Eigen::MatrixBase<MatU>& U, Index i, Index j,
                            RealScalar cs, const Scalar& sn)
    {
        using Eigen::numext::conj;
        for (Index k = 0; k < U.rows(); ++k)
        {
            const auto t1 = U(k, i);
            const auto t2 = U(k, j);
            // Apply Jacobi rotation matrix on the right
            U(k, i) = cs * t1 - sn * t2;
            U(k, j) = conj(sn) * t1 + cs * t2;
        }
    }
};

} // namespace: detail

///
/// Compute the singular value decomposition (SVD) by one-sided Jacobi
/// algorithm.
///
/// This function compute the singular value decomposition (SVD) using modified
/// one-sided Jacobi algorithm.
///
/// #### References
///  - James Demmel and Kresimir Veselic, "Jacobi's method is more accurate than
///    QR", LAPACK working note 15 (lawn15) (1989), Algorithm 4.1.
///
/// \param[in,out] A  On entry, an \f$m \times n\f$ matrix to be decomposed. On
///     exit, columns of `A` holds left singular vectors.
/// \param[out] sigma  singular values of matrix `A`.
/// \param[out] V      right singular vectors.
/// \param[in] small   positive real number that determines stopping criterion
///     of Jacobi algorithm.
///
template <typename MatA, typename VecS, typename MatV, typename RealT>
void one_sided_jacobi_svd(Eigen::MatrixBase<MatA>& A,
                          Eigen::MatrixBase<VecS>& sigma,
                          Eigen::MatrixBase<MatV>& V, RealT tol)
{
    using Index      = Eigen::Index;
    using Scalar     = typename MatA::Scalar;
    using RealScalar = typename Eigen::NumTraits<Scalar>::Real;
    using jacobi_aux = detail::one_sided_jacobi_helper<Scalar>;

    using Eigen::numext::abs;
    using Eigen::numext::sqrt;
    // constexpr const Index max_sweep = 50;

    auto n                = A.cols();
    const Index max_sweep = n * (n - 1) / 2;
    assert(V.rows() == A.cols());
    //
    // Initialize right singular vectors
    //
    V.setIdentity();

    Scalar sn;
    RealScalar cs;
    RealScalar max_resid = tol + RealScalar(1);
    Index nsweep         = max_sweep + 1;
    while (--nsweep && max_resid > tol)
    {
        max_resid = RealScalar();
        for (Index j = 1; j < n; ++j)
        {
            auto b = jacobi_aux::submat_diag(A, j);
            for (Index i = 0; i < j; ++i)
            {
                //
                // For all pairs i < j, compute the 2x2 submatrix of A.t() * A
                // constructed from i-th and j-th columns, such as
                //
                //   M = [     a   c]
                //       [conj(c)  b]
                //
                // where
                //
                //   a = \sum_{k=0}^{n-1} |A(k,i)|^{2} (computed in outer loop)
                //   b = \sum_{k=0}^{n-1} |A(k,j)|^{2}
                //   c = \sum_{k=0}^{n-1} conj(A(k,i)) * A(k,j)
                //
                auto a    = jacobi_aux::submat_diag(A, i);
                auto c    = jacobi_aux::submat_offdiag(A, i, j);
                max_resid = std::max(max_resid, abs(c) / sqrt(a * b));
                //
                // Compute the Jacobi rotation matrix which diagonalize M.
                //
                if (detail::make_jacobi_rotation(a, b, c, cs, sn))
                {
                    // If the return vlaue of make_jacobi_rotation is false, no
                    // rotation is made.
                    //
                    // Update columns i and j of A
                    //
                    jacobi_aux::update_vecs(A, i, j, cs, sn);
                    //
                    // Update right singular vector V
                    //
                    jacobi_aux::update_vecs(V, i, j, cs, sn);
                }
            }
        }
#ifdef DEBUG
        std::cout << "(one_sided_jacobi_svd) iter " << std::setw(8)
                  << max_sweep - nsweep << ": max residual = " << max_resid
                  << '\n';
#endif /* DEBUG */
    }
    //
    // Set singular values and left singular vectors.
    //
    for (Index j = 0; j < A.cols(); ++j)
    {
        // Singular values are the norms of the columns of the final A
        sigma(j) = A.col(j).norm();
        // Left singular vectors are the normalized columns of the final A
        A.col(j) /= sigma(j);
    }
    //
    // Sort singular values in descending order. The corresponding singular
    // vectors are also rearranged.
    //
    for (Index i = 0; i < n - 1; ++i)
    {
        //
        // Find the index of the maximum value of a sub-array, sigma(i:n-1).
        //
        Index imax = i;
        for (Index k = i + 1; k < n; ++k)
        {
            if (sigma(k) > sigma(imax))
            {
                imax = k;
            }
        }

        if (imax != i)
        {
            // Move the largest singular value to the beggining of the
            // sub-array, and corresponsing singular vectors by swapping columns
            // of A and V.
            std::swap(sigma(i), sigma(imax));
            A.col(i).swap(A.col(imax));
            V.col(i).swap(V.col(imax));
        }
    }
}

///
/// Compute the singular value decomposition (SVD) by one-sided Jacobi
/// algorithm.
///
/// This function only computes the singular values and left singular vectors.
///
/// \param[in,out] A  On entry, an \f$m \times n\f$ matrix to be decomposed. On
///     exit, columns of `A` holds left singular vectors.
/// \param[out] sigma  singular values of matrix `A`.
/// \param[in] small   positive real number that determines stopping criterion
///     of Jacobi algorithm.
///
template <typename MatA, typename VecS, typename RealT>
void one_sided_jacobi_svd(Eigen::MatrixBase<MatA>& A,
                          Eigen::MatrixBase<VecS>& sigma, RealT tol)
{
    using Index      = Eigen::Index;
    using Scalar     = typename MatA::Scalar;
    using RealScalar = typename Eigen::NumTraits<Scalar>::Real;
    using jacobi_aux = detail::one_sided_jacobi_helper<Scalar>;

    using Eigen::numext::abs;
    using Eigen::numext::sqrt;
    // constexpr const Index max_sweep = 50;

    auto n                = A.cols();
    const Index max_sweep = n * (n - 1) / 2;

    Scalar sn;
    RealScalar cs;
    RealScalar max_resid = tol + RealScalar(1);
    Index nsweep         = max_sweep + 1;
    while (--nsweep && max_resid > tol)
    {
        max_resid = RealScalar();
        for (Index j = 1; j < n; ++j)
        {
            auto b = jacobi_aux::submat_diag(A, j);
            for (Index i = 0; i < j; ++i)
            {
                //
                // For all pairs i < j, compute the 2x2 submatrix of A.t() * A
                // constructed from i-th and j-th columns, such as
                //
                //   M = [     a   c]
                //       [conj(c)  b]
                //
                // where
                //
                //   a = \sum_{k=0}^{n-1} |A(k,i)|^{2} (computed in outer loop)
                //   b = \sum_{k=0}^{n-1} |A(k,j)|^{2}
                //   c = \sum_{k=0}^{n-1} conj(A(k,i)) * A(k,j)
                //
                auto a    = jacobi_aux::submat_diag(A, i);
                auto c    = jacobi_aux::submat_offdiag(A, i, j);
                max_resid = std::max(max_resid, abs(c) / sqrt(a * b));
                //
                // Compute the Jacobi rotation matrix which diagonalize M.
                //
                if (detail::make_jacobi_rotation(a, b, c, cs, sn))
                {
                    // If the return vlaue of make_jacobi_rotation is false, no
                    // rotation is made.
                    //
                    // Update columns i and j of A
                    //
                    jacobi_aux::update_vecs(A, i, j, cs, sn);
                }
            }
        }
#ifdef DEBUG
        std::cout << "(one_sided_jacobi_svd) iter " << std::setw(8)
                  << max_sweep - nsweep << ": max residual = " << max_resid
                  << '\n';
#endif /* DEBUG */
    }
    //
    // Set singular values and left singular vectors.
    //
    for (Index j = 0; j < A.cols(); ++j)
    {
        // Singular values are the norms of the columns of the final A
        sigma(j) = A.col(j).norm();
        // Left singular vectors are the normalized columns of the final A
        A.col(j) /= sigma(j);
    }
    //
    // Sort singular values in descending order. The corresponding singular
    // vectors are also rearranged.
    //
    for (Index i = 0; i < n - 1; ++i)
    {
        //
        // Find the index of the maximum value of a sub-array, sigma(i:n-1).
        //
        Index imax = i;
        for (Index k = i + 1; k < n; ++k)
        {
            if (sigma(k) > sigma(imax))
            {
                imax = k;
            }
        }

        if (imax != i)
        {
            // Move the largest singular value to the beggining of the
            // sub-array, and corresponsing singular vectors by swapping columns
            // of A and V.
            std::swap(sigma(i), sigma(imax));
            A.col(i).swap(A.col(imax));
        }
    }
}

} // namespace: mxpfit

#endif /* MXPFIT_JACOBI_SVD_HPP */

max_stars_repo_name: hydeik/mxpfit

max_issues_repo_name: hydeik/mxpfit

max_forks_repo_name: hydeik/mxpfit


text:
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// test_private_ctor.cpp

// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <sstream>

#include <boost/test/test_tools.hpp>

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

#include <boost/serialization/vector.hpp>

class V {
    int m_i;
    friend class boost::serialization::access;
    V() {}
    template<class Archive>
    void serialize(Archive& ar, unsigned version)
    {
        ar & m_i;
    }
};

int test_main(int /* argc */, char * /* argv */[])
{
    std::stringstream ss;
    const std::vector<V> v;
    {
        boost::archive::text_oarchive oa(ss);
        oa << v;
    }
    std::vector<V> v1;
    {
        boost::archive::text_iarchive ia(ss);
        ia >> v1;
    }
    //BOOST_CHECK(v == v1);
    return EXIT_SUCCESS;
}

max_stars_repo_name: spxuw/RFIM

max_issues_repo_name: spxuw/RFIM

max_forks_repo_name: spxuw/RFIM


text: #include <boost/compute/random/discrete_distribution.hpp>

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text:
#ifndef MATRIX
#define MATRIX

#include <Eigen/Dense>
#include <algorithm>
#include <iostream>

#include "MiniDnn/layer.h"
#include "MiniDnn/layer/conv.h"
#include "MiniDnn/layer/fully_connected.h"
#include "MiniDnn/layer/ave_pooling.h"
#include "MiniDnn/layer/max_pooling.h"
#include "MiniDnn/layer/relu.h"
#include "MiniDnn/layer/sigmoid.h"
#include "MiniDnn/layer/softmax.h"
#include "MiniDnn/loss.h"
#include "MiniDnn/loss/mse_loss.h"
#include "MiniDnn/loss/cross_entropy_loss.h"
#include "MiniDnn/mnist.h"
#include "MiniDnn/network.h"
#include "MiniDnn/optimizer.h"
#include "MiniDnn/optimizer/sgd.h"
#include "Enclave.h"
#include "Enclave_t.h"  /* print_string */

using namespace Eigen;
using namespace std;

//#define N 4

void Multiply_directly(int N) {
    // std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
    // std::chrono::duration<double> elapsed;
    MatrixXf X = MatrixXf::Random(N, N);
    MatrixXf Y = MatrixXf::Random(N, N);
    MatrixXf Z = MatrixXf::Zero(N, N);
    //start = std::chrono::high_resolution_clock::now();
    ocall_start_clock();
    for (int i = 0; i < 5; i++) {
        Z = X * Y;
    }
    //end = std::chrono::high_resolution_clock::now();
    ocall_end_clock("%f: ");
    printf("Direct[%d*%d]\n", N, N);
    // elapsed = end - start;
    // std::cout << "Direct[" << N << '*' << N << "]:" << elapsed.count() << std::endl;

}

void Multiply_breakdown(int N) {
    // std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
    // std::chrono::duration<double> elapsed;
    MatrixXf Z = MatrixXf::Zero(N, N);

    MatrixXf XA = MatrixXf::Random(N / 2, N / 2);
    MatrixXf XC = MatrixXf::Random(N / 2, N / 2);
    MatrixXf XB = MatrixXf::Random(N / 2, N / 2);
    MatrixXf XD = MatrixXf::Random(N / 2, N / 2);

    MatrixXf YA = MatrixXf::Random(N / 2, N / 2);
    MatrixXf YC = MatrixXf::Random(N / 2, N / 2);
    MatrixXf YB = MatrixXf::Random(N / 2, N / 2);
    MatrixXf YD = MatrixXf::Random(N / 2, N / 2);

    //start = std::chrono::high_resolution_clock::now();
    ocall_start_clock();
    for (int i = 0; i < 5; i++) {
        Z.block(0, 0, N / 2, N / 2) = XA * YA + XB * YC;
        Z.block(0, N / 2, N / 2, N / 2) = XA * YB + XB * YD;
        Z.block(N / 2, 0, N / 2, N / 2) = XC * YA + XD * YC;
        Z.block(N / 2, N / 2, N / 2, N / 2) = XC * YB + XD * YD;
    }
    ocall_end_clock("%f: ");
    printf("Break[%d*%d]\n", N, N);
    //end = std::chrono::high_resolution_clock::now();

    // elapsed = end - start;
    // std::cout << "Break[" << N << '*' << N << "]:" << elapsed.count() << std::endl;

}

void ecall_ml_matrix_breakdown() {

    for (int i = 256; i <= 2048; i *= 2) {
        Multiply_directly(i);
        Multiply_breakdown(i);
    }

}

#endif

max_stars_repo_name: zeyu-zh/TrustFL

max_issues_repo_name: zeyu-zh/TrustFL

max_forks_repo_name: zeyu-zh/TrustFL


text:
 //////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2011-2012. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////

#ifndef BOOST_INTERPROCESS_WINDOWS_NAMED_CONDITION_HPP
#define BOOST_INTERPROCESS_WINDOWS_NAMED_CONDITION_HPP

#if defined(_MSC_VER)
#  pragma once
#endif

#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/detail/workaround.hpp>
#include <boost/interprocess/sync/windows/named_condition_any.hpp>

namespace boost {
namespace interprocess {
namespace ipcdetail {

typedef windows_named_condition_any windows_named_condition;

}  //namespace ipcdetail {
}  //namespace interprocess {
}  //namespace boost {

#include <boost/interprocess/detail/config_end.hpp>

#endif   //BOOST_INTERPROCESS_WINDOWS_NAMED_CONDITION_HPP

max_stars_repo_name: bowlofstew/omim

max_issues_repo_name: bowlofstew/omim

max_forks_repo_name: bowlofstew/omim


text:
#include <functional>
#include <iostream>

#include <boost/optional.hpp>

#include <skeleton/skeleton.hpp>

int main()
{
    // C++14 lambda init captures
    [out = std::ref(std::cout << "Hello")]() { out.get() << " World\n"; }();

    boost::optional<std::string> md5 = skeleton::get_md5_sum();

    std::cout << "MD5: " << *md5 << '\n';

    auto time_point = skeleton::get_sys_clock();

    std::cout << "Time since epoch (ns): " << time_point.time_since_epoch() << '\n';
}

max_stars_repo_name: CraigCarey/cpp-project-skeleton

max_issues_repo_name: CraigANV/cpp-project-skeleton

max_forks_repo_name: CraigANV/cpp-project-skeleton


text:
//////////////////////////////////////////////////////////////////////////////
//  Boost.Assign v2                                                         //
//                                                                          //
//  Copyright (C) 2003-2004 Thorsten Ottosen                                //
//  Copyright (C) 2011 Erwann Rogard                                        //
//  Use, modification and distribution are subject to the                   //
//  Boost Software License, Version 1.0. (See accompanying file             //
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)        //
//////////////////////////////////////////////////////////////////////////////
#ifndef BOOST_ASSIGN_V2_INTERPRETER_CSV_BASIC_ER_2011_HPP
#define BOOST_ASSIGN_V2_INTERPRETER_CSV_BASIC_ER_2011_HPP
#include <boost/assign/v2/support/config/enable_cpp0x.hpp>
#if BOOST_ASSIGN_V2_ENABLE_CPP0X
#include <boost/assign/v2/interpreter/csv/csv_ext.hpp>
#include <boost/assign/v2/support/pp/ignore.hpp>

namespace boost{
namespace assign{
namespace v2{
//[syntax_interpreter_csv_basic
namespace interpreter_aux{

    template<
        typename D, typename C, typename F, typename MTag, typename DTag,
        typename... Args
    >
    D const&
    csv(
        interpreter_crtp<D, C, F, MTag, DTag> const& interpreter,
        Args&&... args
    )/*<-*/
    {
        return csv<1>( interpreter, std::forward<Args>( args )... );
    }BOOST_ASSIGN_V2_IGNORE(/*->*/;/*<-*/)/*->*/

}// interpreter_aux

using interpreter_aux::csv;

//]
}// v2
}// assign
}// boost

#else
#include <boost/assign/v2/interpreter/cpp03/csv_basic.hpp>
#endif // BOOST_ASSIGN_V2_ENABLE_CPP0X
#endif // BOOST_ASSIGN_V2_INTERPRETER_CSV_BASIC_ER_2011_HPP


text:
// Copyright 2017 The Ray Authors.
//
// 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 "ray/core_worker/transport/thread_pool_manager.h"

#include <boost/asio/post.hpp>

namespace ray {
namespace core {

/// Wraps a thread-pool to block posts until the pool has free slots. This is used
/// by the SchedulingQueue to provide backpressure to clients.
BoundedExecutor::BoundedExecutor(int max_concurrency)
    : num_running_(0), max_concurrency_(max_concurrency), pool_(max_concurrency){};

/// Posts work to the pool, blocking if no free threads are available.
void BoundedExecutor::PostBlocking(std::function<void()> fn) {
  mu_.LockWhen(absl::Condition(this, &BoundedExecutor::ThreadsAvailable));
  num_running_ += 1;
  mu_.Unlock();
  boost::asio::post(pool_, [this, fn]() {
    fn();
    absl::MutexLock lock(&mu_);
    num_running_ -= 1;
  });
}

/// Stop the thread pool.
void BoundedExecutor::Stop() { pool_.stop(); }

/// Join the thread pool.
void BoundedExecutor::Join() { pool_.join(); }

bool BoundedExecutor::ThreadsAvailable() { return num_running_ < max_concurrency_; }

PoolManager::PoolManager(const std::vector<ConcurrencyGroup> &concurrency_groups,
                         const int32_t default_group_max_concurrency) {
  for (auto &group : concurrency_groups) {
    const auto name = group.name;
    const auto max_concurrency = group.max_concurrency;
    auto pool = std::make_shared<BoundedExecutor>(max_concurrency);
    auto &fds = group.function_descriptors;
    for (auto fd : fds) {
      functions_to_thread_pool_index_[fd->ToString()] = pool;
    }
    name_to_thread_pool_index_[name] = pool;
  }
  // If max concurrency of default group is 1, the tasks of default group
  // will be performed in main thread instead of any executor pool.
  if (default_group_max_concurrency > 1) {
    default_thread_pool_ =
        std::make_shared<BoundedExecutor>(default_group_max_concurrency);
  }
}

std::shared_ptr<BoundedExecutor> PoolManager::GetPool(
    const std::string &concurrency_group_name, ray::FunctionDescriptor fd) {
  if (!concurrency_group_name.empty()) {
    auto it = name_to_thread_pool_index_.find(concurrency_group_name);
    /// TODO(qwang): Fail the user task.
    RAY_CHECK(it != name_to_thread_pool_index_.end());
    return it->second;
  }

  /// Code path of that this task wasn't specified in a concurrency group addtionally.
  /// Use the predefined concurrency group.
  if (functions_to_thread_pool_index_.find(fd->ToString()) !=
      functions_to_thread_pool_index_.end()) {
    return functions_to_thread_pool_index_[fd->ToString()];
  }
  return default_thread_pool_;
}

/// Stop and join the thread pools that the pool manager owns.
void PoolManager::Stop() {
  if (default_thread_pool_) {
    RAY_LOG(DEBUG) << "Default pool is stopping.";
    default_thread_pool_->Stop();
    RAY_LOG(INFO) << "Default pool is joining. If the 'Default pool is joined.' "
                     "message is not printed after this, the worker is probably "
                     "hanging because the actor task is running an infinite loop.";
    default_thread_pool_->Join();
    RAY_LOG(INFO) << "Default pool is joined.";
  }

  for (const auto &it : name_to_thread_pool_index_) {
    it.second->Stop();
  }
  for (const auto &it : name_to_thread_pool_index_) {
    it.second->Join();
  }
}

}  // namespace core
}  // namespace ray

max_stars_repo_name: daobook/ray

max_issues_repo_name: daobook/ray

max_forks_repo_name: daobook/ray