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

// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
// (C) Copyright 2014 Jim Bell
// 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)

// should pass compilation and execution

#include <algorithm> // std::copy
#include <vector>
#include <fstream>
#include <cstddef> // size_t, NULL

#include <boost/config.hpp>
#include <boost/detail/workaround.hpp>

#include <cstdio>
#if defined(BOOST_NO_STDC_NAMESPACE)
namespace std{
    using ::rand;
    using ::size_t;
}
#endif

#include "test_tools.hpp"

#include <boost/serialization/nvp.hpp>
#include <boost/serialization/map.hpp>

#include "A.hpp"
#include "A.ipp"

///////////////////////////////////////////////////////
// a key value initialized with a random value for use
// in testing STL map serialization
struct random_key {
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(
        Archive & ar,
        const unsigned int /* file_version */
    ){
        ar & boost::serialization::make_nvp("random_key", m_i);
    }
    int m_i;
    random_key() : m_i(std::rand()){};
    bool operator<(const random_key &rhs) const {
        return m_i < rhs.m_i;
    }
    bool operator==(const random_key &rhs) const {
        return m_i == rhs.m_i;
    }
    operator std::size_t () const {    // required by hash_map
        return m_i;
    }
};

void
test_map(){
    const char * testfile = boost::archive::tmpnam(NULL);
    BOOST_REQUIRE(NULL != testfile);

    BOOST_MESSAGE("map");
    // test map of objects
    std::map<random_key, A> amap;
    amap.insert(std::make_pair(random_key(), A()));
    amap.insert(std::make_pair(random_key(), A()));
    {
        test_ostream os(testfile, TEST_STREAM_FLAGS);
        test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
        oa << boost::serialization::make_nvp("amap", amap);
    }
    std::map<random_key, A> amap1;
    {
        test_istream is(testfile, TEST_STREAM_FLAGS);
        test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
        ia >> boost::serialization::make_nvp("amap", amap1);
    }
    BOOST_CHECK(amap == amap1);
    std::remove(testfile);
}

void
test_map_2(){
    const char * testfile = boost::archive::tmpnam(NULL);
    BOOST_REQUIRE(NULL != testfile);

    BOOST_MESSAGE("map_2");
    std::pair<int, int> a(11, 22);
    std::map<int, int> b;
    b[0] = 0;
    b[-1] = -1;
    b[1] = 1;
    {
        test_ostream os(testfile, TEST_STREAM_FLAGS);
        std::pair<int, int> * const pa = &a;
        std::map<int, int> * const pb = &b;
        test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
        oa << BOOST_SERIALIZATION_NVP(pb);
        oa << BOOST_SERIALIZATION_NVP(pa);
    }
    {
        test_istream is(testfile, TEST_STREAM_FLAGS);
        std::pair<int, int> *pa = 0;
        std::map<int, int> *pb = 0;
        test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
        ia >> BOOST_SERIALIZATION_NVP(pb);
        ia >> BOOST_SERIALIZATION_NVP(pa);
        delete pa;
        delete pb;
    }
    std::remove(testfile);
}

void
test_multimap(){
    const char * testfile = boost::archive::tmpnam(NULL);
    BOOST_REQUIRE(NULL != testfile);

    BOOST_MESSAGE("multimap");
    std::multimap<random_key, A> amultimap;
    amultimap.insert(std::make_pair(random_key(), A()));
    amultimap.insert(std::make_pair(random_key(), A()));
    {
        test_ostream os(testfile, TEST_STREAM_FLAGS);
        test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
        oa << boost::serialization::make_nvp("amultimap", amultimap);
    }
    std::multimap<random_key, A> amultimap1;
    {
        test_istream is(testfile, TEST_STREAM_FLAGS);
        test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
        ia >> boost::serialization::make_nvp("amultimap", amultimap1);
    }
    BOOST_CHECK(amultimap == amultimap1);
    std::remove(testfile);
}

int test_main( int /* argc */, char* /* argv */[] )
{
    test_map();
    test_map_2();
    test_multimap();

    return EXIT_SUCCESS;
}


text: #include <boost/local_function/aux_/preprocessor/traits/bind.hpp>

ext: hpp

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text:
#include <boost/program_options.hpp>
#include <iostream>
#include <random>

namespace po = boost::program_options;

using Options = std::tuple<int, double, double, size_t>;

Options get_options(int argc, char* argv[]);

int main(int argc, char* argv[]) {
    int n;
    double a, b;
    size_t seed;
    std::tie(n, a, b, seed) = get_options(argc, argv);

    std::mt19937 engine(seed);
    std::uniform_real_distribution<double> distr(a, b);
    for (int i = 0; i < n; ++i)
        std::cout << distr(engine) << std::endl;

    return 0;
}

Options get_options(int argc, char* argv[]) {
    const int default_n {1};
    const double default_a {0.0};
    const double default_b {1.0};
    int n;
    double a, b;
    size_t seed;
    po::options_description desc("Allowed options");
    desc.add_options()
        ("help", "produce help message")
        ("n", po::value<int>(&n)->default_value(default_n),
         "number of random values to generate")
        ("a", po::value<double>(&a)->default_value(default_a),
         "minimum value")
        ("b", po::value<double>(&b)->default_value(default_b),
         "maximum value")
        ("seed", po::value<size_t>(&seed), "seed to use")
    ;
    po::positional_options_description pos_desc;
    pos_desc.add("n", -1);

    po::variables_map vm;
    po::store(po::command_line_parser(argc, argv)
                  .options(desc).positional(pos_desc).run(), vm);
    po::notify(vm);

    if (vm.count("help")) {
        std::cout << desc << std::endl;
        std::exit(0);
    }

    if (!vm.count("seed")) {
        std::random_device device;
        seed = device();
    }
    return std::make_tuple(n, a, b, seed);
}

ext: cpp

max_stars_repo_name: gjbex/Scientific-C-

max_issues_repo_name: gjbex/Scientific-C-

max_forks_repo_name: gjbex/Scientific-C-


text:
#include "VelocitySideCar.h"
#include <boost/tokenizer.hpp>
#include <cryptopp/sha.h>
#include <cryptopp/hex.h>
#include <cryptopp/files.h>

VelocitySideCar::VelocitySideCar()
{
}

VelocitySideCar::~VelocitySideCar()
{
}

const std::string& VelocitySideCar::get_sha() const
{
	return _source_sha;
};

bool VelocitySideCar::compute(const std::string& i_alembic_source, const std::string& i_regex_pattern)
{
	compute_sha(i_alembic_source, _source_sha);

	Alembic::AbcCoreFactory::IFactory factory;
	Alembic::AbcCoreFactory::IFactory::CoreType oType;
	_archive_ptr.reset(new Alembic::Abc::IArchive(factory.getArchive(i_alembic_source, oType)));

    Alembic::Abc::IObject top = _archive_ptr->getTop();

    size_t top_num_children = top.getNumChildren();
    std::cout << "top_num_children = " << top_num_children << std::endl;
    std::string top_name = top.getName();
    std::cout << "top_name = " << top_name << std::endl;

    PathList path;
    TokenizePath( std::string("/grid_object1/grid1"), path );
    PathList::const_iterator I = path.begin();
    const Alembic::Abc::ObjectHeader *nextChildHeader = top.getChildHeader (*I);
    for ( size_t i = 0; i < top.getNumChildren(); ++i )
    {
        iterate_iobject( top, top.getChildHeader(i),i_regex_pattern, I+1,path.end());
    }
	return true;
}

void VelocitySideCar::ProcessIPolyMesh(const Alembic::AbcGeom::IPolyMesh& i_polymesh,
		  	  	  	  	  	  	  	   const std::string&                 i_regex_pattern)
{

//	Alembic::Abc::ISampleSelector next_sample_selector(requested_index+1);
//
//    Alembic::AbcGeom::IPolyMeshSchema::Sample next_sample;
//	pmesh.getSchema().get( next_sample, next_sample_selector );

}

void VelocitySideCar::iterate_iobject(Alembic::Abc::IObject             parent,
									  const Alembic::Abc::ObjectHeader& ohead,
									  const std::string&                i_regex_pattern,
									  PathList::const_iterator          I,
									  PathList::const_iterator          E)
{
    std::cout << "iterate_iobject ohead's name = " << ohead.getName().c_str() << std::endl;
    std::cout << "iterate_iobject ohead's full name = " << ohead.getFullName().c_str() << std::endl;

    //set this if we should continue traversing
    Alembic::Abc::IObject nextParentObject;

    if ( Alembic::AbcGeom::IXform::matches( ohead ) )
    {
        std::cout << "iterate_iobject match IXform" << std::endl;
        Alembic::AbcGeom::IXform xform( parent, ohead.getName() );

        nextParentObject = xform;

    }
    else if ( Alembic::AbcGeom::ISubD::matches( ohead ) )
    {
        std::cout << "iterate_iobject match ISubD" << std::endl;
        Alembic::AbcGeom::ISubD subd( parent, ohead.getName() );
        nextParentObject = subd;

    }
    else if ( Alembic::AbcGeom::IPolyMesh::matches( ohead ) )
    {
        std::cout << "iterate_iobject match IPolyMesh" << std::endl;
        Alembic::AbcGeom::IPolyMesh polymesh( parent, ohead.getName() );
        ProcessIPolyMesh(polymesh, i_regex_pattern);
        nextParentObject = polymesh;

    }
    else if ( Alembic::AbcGeom::INuPatch::matches( ohead ) )
    {
        std::cout << "iterate_iobject match INuPatch" << std::endl;

    }
    else if ( Alembic::AbcGeom::IPoints::matches( ohead ) )
    {
        std::cout << "iterate_iobject match IPoints" << std::endl;
        Alembic::AbcGeom::IPoints points( parent, ohead.getName() );
        // ProcessIPoints(points);
        nextParentObject = points;
    }
    else if ( Alembic::AbcGeom::ICurves::matches( ohead ) )
    {
        std::cout << "iterate_iobject match ICurves" << std::endl;

    }
    else if ( Alembic::AbcGeom::IFaceSet::matches( ohead ) )
    {
        std::cout << "iterate_iobject match IFaceSet" << std::endl;
        std::cerr << "DOH !" << std::endl;
    }

    // Recursion
    if ( nextParentObject.valid() )
    {
        for ( size_t i = 0; i < nextParentObject.getNumChildren() ; ++i )
        {
            iterate_iobject( nextParentObject, nextParentObject.getChildHeader( i ), i_regex_pattern, I, E);
        }
    }
}

void VelocitySideCar::TokenizePath( const std::string &path, PathList& result ) const
{
    typedef boost::char_separator<char> Separator;
    typedef boost::tokenizer<Separator> Tokenizer;

    Tokenizer tokenizer( path, Separator( "/" ) );

    for ( Tokenizer::iterator iter = tokenizer.begin() ; iter != tokenizer.end() ;
          ++iter )
    {
		  if ( (*iter).empty() )
		  {
			  continue;
		  }
		  std::cout << "*iter = " << *iter << std::endl;
		  result.push_back( *iter );
    }
}

void compute_sha(const std::string& i_filename, std::string& o_sha)
{
	CryptoPP::SHA1 hash;
	CryptoPP::FileSource(i_filename.c_str(),true,
			new CryptoPP::HashFilter(hash, new CryptoPP::HexEncoder(
					new CryptoPP::StringSink(o_sha), true)));
}

// == Emacs ================
// -------------------------
// Local variables:
// tab-width: 4
// indent-tabs-mode: t
// c-basic-offset: 4
// end:
//
// == vi ===================
// -------------------------
// Format block
// ex:ts=4:sw=4:expandtab
// -------------------------


text:
// Author: Zhang Yangyang (zhangyy@act.buaa.edu.cn)

#pragma once

#include <boost/iostreams/stream.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <tuple>

namespace petuum {
namespace io {

// default buffer size is 16MB. you can ajust it for performance
const size_t BUFFER_SIZE = 16 * 1024 * 1024;
std::tuple<std::string, std::string, std::string>
  parse_hdfs_url(const std::string &url);

class general_device {
 public:
  enum device_type_t { HDFS, LOCALFS };
  std::streamsize read(char* s, std::streamsize n);
  std::streamsize write(const char* s, std::streamsize n);
  std::streampos seek(std::streamoff off, std::ios_base::seekdir way);
  void close();
  bool good();
  inline std::streamsize optimal_buffer_size() const { return BUFFER_SIZE; }

 protected:
  bool open_file(const std::string &url,
    std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out);

 protected:
  // HDFS or LOCALFS
  device_type_t device_type;

  // could be a hdfs::fstream ptr or std::ifstream ptr depending on device_type
  std::istream *input_stream_;

  // could be a hdfs::fstream ptr or std::ifstream ptr depending on device_type
  std::ostream *output_stream_;
  bool is_reading_;
};

class general_source : public general_device {
 public:
  // conceptions in boost::fstream
  typedef char char_type;

  struct category :
    public boost::iostreams::device_tag,
    public boost::iostreams::multichar_tag,
    public boost::iostreams::input_seekable,
    public boost::iostreams::closable_tag,
    public boost::iostreams::optimally_buffered_tag {};

  general_source() {}
  /** @url format hdfs://[host:port]/path or local filesystem absolute path
   * and relative path for compatibility of hdfs, please using full path like
   * hdfs://host:port/path is recommended
   */
  explicit general_source(const std::string &url,
      std::ios_base::openmode mode = std::ios_base::in) {
    open_file(url, mode | std::ios_base::in);
  }

  void open(const std::string &url,
      std::ios_base::openmode mode = std::ios_base::in) {
    close();
    open_file(url, mode | std::ios_base::in);
  }
};

class general_sink : public general_device {
 public:
  typedef char char_type;
  struct category :
    public boost::iostreams::device_tag,
    public boost::iostreams::multichar_tag,
    public boost::iostreams::output_seekable,
    public boost::iostreams::closable_tag,
    public boost::iostreams::optimally_buffered_tag {};

  general_sink() {}
  explicit general_sink(const std::string &url,
      std::ios_base::openmode mode = std::ios_base::out) {
    open_file(url, mode | std::ios_base::out);
  }

  void open(const std::string &url,
      std::ios_base::openmode mode = std::ios_base::out) {
    close();
    open_file(url, mode | std::ios_base::out);
  }
};

typedef boost::iostreams::stream<general_source> ifstream;
typedef boost::iostreams::stream<general_sink>   ofstream;

}   // namespace io
}   // namespace petuum


text:
/*
    Copyright 2005-2007 Adobe Systems Incorporated
    Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
    or a copy at http://stlab.adobe.com/licenses.html)
*/

/*************************************************************************************************/

#ifndef ADOBE_CLOSED_HASH_HPP
#define ADOBE_CLOSED_HASH_HPP

/*************************************************************************************************/

#include <adobe/config.hpp>

#include <adobe/closed_hash_fwd.hpp>

#include <climits>
#include <cstddef>
#include <limits>

#include <boost/compressed_pair.hpp>
#include <boost/functional/hash.hpp>
#include <boost/iterator/iterator_adaptor.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/static_assert.hpp>
#include <boost/type_traits/has_nothrow_constructor.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/operators.hpp>
#include <boost/next_prior.hpp>

#include <adobe/algorithm/lower_bound.hpp>
#include <adobe/conversion.hpp>
#include <adobe/cstdint.hpp>
#include <adobe/empty.hpp>
#include <adobe/functional.hpp>
#include <adobe/iterator/set_next.hpp>
#include <adobe/memory.hpp>
#include <adobe/move.hpp>
#include <adobe/utility.hpp>

#include <adobe/implementation/swap.hpp>

/*************************************************************************************************/

namespace adobe {

/*************************************************************************************************/

namespace implementation {

/*************************************************************************************************/

template <typename T, typename V> // V is value_type(T) const qualified
class closed_hash_iterator : public boost::iterator_facade<closed_hash_iterator<T, V>, V,
                                                           std::bidirectional_iterator_tag>
{
    typedef boost::iterator_facade<closed_hash_iterator<T, V>, V,
                                   std::bidirectional_iterator_tag> inherited_t;

    typedef typename T::node_t node_t;
 public:
    typedef typename inherited_t::reference         reference;
    typedef typename inherited_t::difference_type   difference_type;
    typedef typename inherited_t::value_type        value_type;

    closed_hash_iterator() : node_m(0) { }

    template <typename O>
    closed_hash_iterator(const closed_hash_iterator<T, O>& x) : node_m(x.node_m) { }

 public:
    /*
        REVISIT (sparent@adobe.com) : node_m should be private but
        "gcc version 4.0.1 (Apple Inc. build 5465)" doesn't like it.
    */

    node_t* node_m;

 private:

    reference dereference() const { return node_m->value_m; }
    void increment() { node_m = node_m->next(); }
    void decrement() { node_m = node_m->prior(); }

    template< typename O>
    bool equal(const closed_hash_iterator<T, O>& y) const { return node_m == y.node_m; }

    std::size_t state() const { return node_m->state(); }
    void set_state(std::size_t x) { return node_m->set_state(x); }

    explicit closed_hash_iterator(node_t* node) : node_m(node) { }

	friend class version_1::closed_hash_set<value_type, typename T::key_transform, typename T::hasher,
            typename T::key_equal, typename T::allocator_type>;
    friend class boost::iterator_core_access;
    friend struct unsafe::set_next_fn<closed_hash_iterator>;
};

/*************************************************************************************************/

} // namespace implementation

/*************************************************************************************************/

namespace unsafe {

template <typename T, typename V>
struct set_next_fn<implementation::closed_hash_iterator<T, V> >
{
    typedef typename implementation::closed_hash_iterator<T, V> iterator;

    void operator()(iterator x, iterator y) const
    { set_next(*x.node_m, *y.node_m); }
};

} // namespace unsafe

/*************************************************************************************************/

#ifndef ADOBE_NO_DOCUMENTATION

namespace version_1 {

#endif

/*************************************************************************************************/

/*!
\defgroup abi_container ABI-Safe Containers: hash containers, vector, ...
\ingroup abi_safe container
 */

/*!
\ingroup abi_container

\brief A hash based associative container.

\par
A \c closed_hash_set is a hash based associative container, similar to a hash_set.

\model_of
    - \ref concept_regular_type
    - \ref stldoc_UniqueHashedAssociativeContainer

\todo
    - re-order parameters so key_function is after comparison - to be consistent with lower_bound.

*/

template<   typename T,
            typename KeyTransform,
            typename Hash,
            typename Pred,
            typename A>
class closed_hash_set : boost::equality_comparable<closed_hash_set<T, KeyTransform, Hash, Pred, A>,
                                        closed_hash_set<T, KeyTransform, Hash, Pred, A>,
                                        empty_base<closed_hash_set<T, KeyTransform, Hash, Pred, A> > >
{
 public:
    typedef KeyTransform                        key_transform;

    typedef typename boost::remove_reference<typename key_transform::result_type>::type
                                                key_type;

    typedef T                                   value_type;
    typedef Hash                                hasher;
    typedef Pred                                key_equal;
    typedef A                                   allocator_type;
    typedef value_type*                         pointer;
    typedef const value_type*                   const_pointer;
    typedef value_type&                         reference;
    typedef const value_type&                   const_reference;
    typedef std::size_t                         size_type;
    typedef std::ptrdiff_t                      difference_type;

    friend class implementation::closed_hash_iterator<closed_hash_set, value_type>;
    friend class implementation::closed_hash_iterator<closed_hash_set, const value_type>;

    typedef implementation::closed_hash_iterator<closed_hash_set, value_type>       iterator;
    typedef implementation::closed_hash_iterator<closed_hash_set, const value_type> const_iterator;

    typedef std::reverse_iterator<iterator>         reverse_iterator;
    typedef std::reverse_iterator<const_iterator>   const_reverse_iterator;

 private:
    enum
    {
        state_free          = 0,
        state_home          = 1,
        state_misplaced     = 2
    };

    template <typename U> // U is derived node
    struct list_node_base
    {
        list_node_base() { next_m = static_cast<U*>(this); prior_m = static_cast<U*>(this); }

        U* address() { return static_cast<U*>(this); }
        const U* address() const { return static_cast<const U*>(this); }

        operator U& () { return *static_cast<U*>(this); }
        operator const U& () const { return *static_cast<const U*>(this); }

        friend inline void set_next(U& x, U& y)
        { x.next_m = reinterpret_cast<U*>(uintptr_t(&y) | uintptr_t(x.state())); y.prior_m = &x; }

        friend inline void set_next_raw(U& x, U& y)
        { x.next_m = &y; y.prior_m = &x; }

        std::size_t state() const { return std::size_t(uintptr_t(next_m) & uintptr_t(0x03UL)); }
        void set_state(std::size_t x)
        {
            assert(x < 0x04UL);
            next_m = reinterpret_cast<U*>(uintptr_t(next()) | uintptr_t(x));
        }

        U* next() const { return reinterpret_cast<U*>(reinterpret_cast<uintptr_t>(next_m) & ~uintptr_t(0x03UL)); }
        U* prior() const { return prior_m; }

     private:
        U* next_m;
        U* prior_m;
    };

    struct node_t : list_node_base<node_t>
    {
        T           value_m;
    };

    typedef list_node_base<node_t> node_base_t;

    struct header_t
    {
        struct compact_header_t
        {
            boost::compressed_pair<allocator_type, node_base_t> alloc_free_tail_m;
            node_base_t used_tail_m;
            std::size_t capacity_m;
            std::size_t size_m;
        };

        /*
        NOTE (sparent) - the assumption is that the initial items are pointers and that size_t is
        either equal to the sizeof a pointer or a lower power of two so this packs tightly.
        */

        BOOST_STATIC_ASSERT(!(sizeof(A) == sizeof(void*) || sizeof(A) == 0)
            || (sizeof(compact_header_t) == (sizeof(allocator_type) + 2 * sizeof(node_base_t) + 2 *
                sizeof(std::size_t))));

        aligned_storage<compact_header_t> header_m;
        node_t      storage_m[1];

        allocator_type&  allocator() { return header_m.get().alloc_free_tail_m.first(); }
        const allocator_type& allocator() const { return header_m.get().alloc_free_tail_m.first(); }
        node_base_t&  free_tail() { return header_m.get().alloc_free_tail_m.second(); }
        const node_base_t& free_tail() const { return header_m.get().alloc_free_tail_m.second(); }
        node_base_t&  used_tail() { return header_m.get().used_tail_m; }
        const node_base_t& used_tail() const { return header_m.get().used_tail_m; }
        std::size_t&  capacity() { return header_m.get().capacity_m; }
        const std::size_t& capacity() const { return header_m.get().capacity_m; }
        std::size_t&  size() { return header_m.get().size_m; }
        const std::size_t& size() const { return header_m.get().size_m; }
    };

    typedef node_t* node_ptr;

    typedef boost::compressed_pair< hasher,
                                    boost::compressed_pair< key_equal,
                                                            boost::compressed_pair< key_transform,
																					header_t*
                                                                                >
                                                          >
                                  > data_t;

    data_t  data_m;

    typedef header_t* header_pointer;

    const header_pointer& header() const { return data_m.second().second().second(); }
    header_pointer& header() { return data_m.second().second().second(); }

 public:
    // construct/destroy/copy

    closed_hash_set() { header() = 0; }

    explicit closed_hash_set(size_type n)
    {
        header() = 0;
        allocate(allocator_type(), n);
    }

    closed_hash_set(size_type n, const hasher& hf, const key_equal& eq = key_equal(),
                                                   const key_transform& kf = key_transform(),
                                                   const allocator_type& a = allocator_type())
    {
        header() = 0;
        data_m.first() = hf;
        data_m.second().first() = eq;
        data_m.second().second().first() = kf;
        allocate(a, n);
    }

    template <typename I> // I models InputIterator
    closed_hash_set(I f, I l) { header() = 0; insert(f, l); }

    template <typename I> // I models InputIterator
    closed_hash_set(I f, I l, size_type n, const hasher& hf = hasher(),
                                           const key_equal& eq = key_equal(),
                                           const key_transform& kf = key_transform(),
                                           const allocator_type& a = allocator_type())
    {
        header() = 0;
        data_m.first() = hf;
        data_m.second().first() = eq;
        data_m.second().second().first() = kf;
        allocate(a, n);
        insert(f, l);
    }

    closed_hash_set(const closed_hash_set& x) : data_m(x.data_m)
    {
        header() = 0;
        allocate(x.get_allocator(), x.size());
        insert(x.begin(), x.end());
    }
    closed_hash_set& operator=(closed_hash_set x) { swap(x, *this); return *this; }

    allocator_type get_allocator() const
    { return header() ? header()->allocator() : allocator_type(); }

    closed_hash_set(move_from<closed_hash_set> x) : data_m(x.source.data_m) { x.source.header() = 0; }

#if 0
    template <typename I> // I models ForwardIterator
    closed_hash_set(I f, I l, move_ctor) { header() = 0; move_insert(f, l); }
#endif

    // size and capacity

    size_type size() const { return header() ? header()->size() : 0; }
    size_type max_size() const { return size_type(-1) / sizeof(node_t); }
    bool empty() const { return size() == 0; }
    size_type capacity() const { return header() ? header()->capacity() : 0; }

    void reserve(size_type n)
    {
        if (n <= capacity()) return;

        if (!header()) allocate(allocator_type(), n);
        else
        {
            closed_hash_set tmp(n, hash_function(), key_eq(), key_function(), get_allocator());
            tmp.move_insert(begin(), end());
            swap(*this, tmp);
        }
    }

    key_transform key_function() const { return data_m.second().second().first(); }
    hasher hash_function() const { return data_m.first(); }
    key_equal key_eq() const { return data_m.second().first(); }

    iterator begin() { return iterator(header() ? header()->used_tail().next() : 0); }
    iterator end() { return iterator(header() ? header()->used_tail().address() : 0); }

    const_iterator begin() const { return iterator(header() ? header()->used_tail().next() : 0); }
    const_iterator end() const { return iterator(header() ? const_cast<node_t*>(header()->used_tail().address()) : 0); }

    reverse_iterator rbegin() { return reverse_iterator(end()); }
    reverse_iterator rend() { return reverse_iterator(begin()); }

    const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
    const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }

    iterator erase(iterator location)
    {
        iterator next(boost::next(location));
        iterator result = next;

        if ((location.state() == std::size_t(state_home)) && (next != end())
                && (next.state() == std::size_t(state_misplaced)))
        {
            swap(*next, *location);
            result = location;
            location = next;
        }

        unsafe::skip_node(location);
        erase_raw(location);

        --header()->size();

        return result;
    }

    std::size_t erase(const key_type& key)
    {
        iterator node(find(key));
        if (node == end()) return 0;
        erase(node);
        return 1;
    }

    void clear()
    {
        for(iterator first(begin()), last(end()); first != last; first = erase(first)) ;
    }

    const_iterator find(const key_type& key) const
    {
        return adobe::remove_const(*this).find(key);
    }

    iterator find(const key_type& key)
    {
        if (empty()) return end();

        iterator node(bucket(key));

        if (node.state() != std::size_t(state_home)) return end();

        return find(node, key);
    }

    std::pair<const_iterator, const_iterator> equal_range(const key_type& key) const
    {
        const_iterator result = find(key);
        if (result == end()) return std::make_pair(result, result);
        return std::make_pair(result, boost::next(result));
    }

    std::pair<iterator, iterator> equal_range(const key_type& key)
    {
        iterator result = find(key);
        if (result == end()) return std::make_pair(result, result);
        return std::make_pair(result, boost::next(result));
    }

    std::size_t count(const key_type& key) const
    { return std::size_t(find(key) != end()); }

    template <typename I> // I models InputIterator
    void insert(I first, I last)
    { while (first != last) { insert(*first); ++first; } }

    template <typename I> // I models ForwardIterator
    void move_insert(I first, I last)
    { while (first != last) { insert(adobe::move(*first)); ++first; } }

    /*
        NOTE (sparent): If there is not enough space for one element we will reserve the space
        prior to attempting the insert even if the item is already in the hash table. Without
        recalculating the bucket (a potentially expensive operation) there is no other solution.
    */

    std::pair<iterator, bool> insert(value_type x)
    {
        if (capacity() == size()) reserve(size() ? 2 * size() : 3);

        iterator node = bucket(key_function()(x));

        switch (node.state())
        {
        case state_home:
            {
            iterator found = find(node, key_function()(x));
            if (found != end()) {
                *found = adobe::move(x);
                return std::make_pair(found, false);
            }

            iterator free(begin_free());
            insert_raw(free, adobe::move(x), state_misplaced);
            unsafe::splice_node_range(node, free, free);
            node = free;
            }
            break;
        case state_misplaced:
            {
            iterator free(begin_free());
            insert_raw(free, adobe::move(*node), state_misplaced);

            unsafe::set_next(boost::prior(node), free);
            unsafe::set_next(free, boost::next(node));

            erase_raw(node);
            }
            // fall through
        default: // state_free
            {
            insert_raw(node, adobe::move(x), state_home);
            unsafe::splice_node_range(end(), node, node);
            }
        }
        header()->size() += 1;
        return std::make_pair(node, true);
    }

    iterator insert(iterator, value_type x)
    {
        return insert(adobe::move(x)).first;
    }

    ~closed_hash_set()
    {
        if (header())
        {
            for(iterator first(begin()), last(end()); first != last; ++first) destroy(&*first);
            raw_allocator alloc(get_allocator());
            alloc.deallocate(reinterpret_cast<char*>(header()), 0);
        }
    }

    friend void swap(closed_hash_set& x, closed_hash_set& y)
    {
        std::swap(x.data_m, y.data_m);
    }

    friend bool operator==(const closed_hash_set& x, const closed_hash_set& y)
    {
        if (x.size() != y.size()) return false;
        for (const_iterator first(x.begin()), last(x.end()); first != last; ++first)
        {
            const_iterator iter(y.find(y.key_function()(*first)));
            if (iter == y.end() || !(*first == *iter)) return false;
        }
        return true;
    }
 private:

    typedef typename allocator_type::template rebind<char>::other raw_allocator;

    void allocate(const allocator_type& a, size_type n)
    {
        // table of primes such that p[n + 1] = next_prime(2 * p[n])

        static const std::size_t prime_table[] = { 3UL, 7UL, 17UL, 37UL, 79UL, 163UL, 331UL, 673UL,
            1361UL, 2729UL, 5471UL, 10949UL, 21911UL, 43853UL, 87719UL, 175447UL, 350899UL,
            701819UL, 1403641UL, 2807303UL, 5614657UL, 11229331UL, 22458671UL, 44917381UL,
            89834777UL, 179669557UL, 359339171UL, 718678369UL, 1437356741UL, 2874713497UL,
            ULONG_MAX
        };

        assert(!header() && "WARNING (sparent@adobe.com) : About to write over allocated header.");

        if (n == 0 && a == allocator_type()) return;

        n = *adobe::lower_bound(prime_table, n);

        raw_allocator alloc(a);

        header() = reinterpret_cast<header_t*>(alloc.allocate(sizeof(header_t) - sizeof(node_t)
            + sizeof(node_t) * n));
        header()->capacity() = n;
        header()->size() = 0;
        construct(&header()->free_tail());
        construct(&header()->used_tail());
        construct(&header()->allocator(), a);

        node_t* prior = header()->free_tail().address();
        for (node_ptr first(&header()->storage_m[0]), last(&header()->storage_m[0]+ n);
                first != last; ++first)
        {
            set_next_raw(*prior, *first);
            prior = first;
            // first->set_state(state_free);
        }
        set_next_raw(*prior, header()->free_tail());

    }

    iterator bucket(const key_type& key)
    {
        std::size_t slot(hash_function()(key) % capacity());
        return iterator(&header()->storage_m[0] + slot);
    }

    iterator find(iterator node, const key_type& key)
    {
        do
        {
            if (key_eq()(key, key_function()(*node))) return node;
            ++node;
        } while ((node != end()) && (node.state() != std::size_t(state_home)));

        return end();
    }

    // location points to a free node
    static void insert_raw(iterator location, value_type x, std::size_t state)
    {
        construct<value_type>(&*location, adobe::move(x));
        location.set_state(state);
        unsafe::skip_node(location);
    }

    // location points to a used but detatched node
    void erase_raw(iterator location)
    {
        destroy(&*location);
        location.set_state(state_free);
        unsafe::splice_node_range(end_free(), location, location);
    }

    iterator begin_free() { return iterator(header() ? header()->free_tail().next() : 0); }
    iterator end_free() { return iterator(header() ? header()->free_tail().address() : 0); }
};

/*************************************************************************************************/

/*!

\ingroup abi_container

\brief A hash based associative container.

\par
A \c closed_hash_map is a hash based associative container, it is an adapted \c closed_hash_set
where value_type is \c adobe::pair<Key, T> and the KeyTransform returns the first element of the
pair.

\model_of
    - \ref concept_regular_type
    - \ref stldoc_UniqueHashedAssociativeContainer

*/

template<typename Key,
         typename T,
         typename Hash,
         typename Pred,
         typename A>
class closed_hash_map : public closed_hash_set<pair<Key, T>,
                                               get_element<0, pair<Key, T> >,
                                               Hash,
                                               Pred,
                                               A>
{
    typedef closed_hash_set<pair<Key, T>,
                            get_element<0, pair<Key, T> >,
                            Hash,
                            Pred,
                            A> set_type;
 public:
    typedef T mapped_type;

    closed_hash_map() { }

    template <typename I> // I models InputIterator
    closed_hash_map(I f, I l) : set_type(f, l) { }

#if 0
    template <typename I> // I models ForwardIterator
    closed_hash_map(I f, I l, move_ctor) : set_type(f, l, move_ctor()) { }
#endif

    closed_hash_map(const closed_hash_map& x) : set_type(x) { }
    closed_hash_map(move_from<closed_hash_map> x) : set_type(move_from<set_type>(x.source)) { }
    closed_hash_map& operator=(closed_hash_map x)
    { swap(x, *this); return *this; }

    friend void swap(closed_hash_map& x, closed_hash_map& y)
    { swap(static_cast<set_type&>(x), static_cast<set_type&>(y)); }

    friend bool operator==(const closed_hash_map& x, const closed_hash_map& y)
    { return static_cast<const set_type&>(x) == static_cast<const set_type&>(y); }

    /*
        NOTE (sparent) : Can't use boost::equality_comparable without introducing extra base class
        overhead.
    */

    friend bool operator!=(const closed_hash_map& x, const closed_hash_map& y)
    { return !(x == y); }

#ifndef ADOBE_CLOSED_HASH_MAP_INDEX
#define ADOBE_CLOSED_HASH_MAP_INDEX 1
#endif

#if ADOBE_CLOSED_HASH_MAP_INDEX

    mapped_type& operator[](const Key& x)
    {
        typename set_type::iterator i = this->find(x);
        if (i == this->end()) return insert(adobe::make_pair(x, mapped_type())).first->second;
        return i->second;
    }

#endif
};

/*************************************************************************************************/

BOOST_STATIC_ASSERT(sizeof(closed_hash_set<int>) == sizeof(void*));

#ifndef ADOBE_NO_DOCUMENTATION

} // namespace version_1

#endif

/*************************************************************************************************/

} // namespace adobe

/*************************************************************************************************/

ADOBE_NAME_TYPE_1("closed_hash_set:version_1:adobe",
    adobe::version_1::closed_hash_set<T0, adobe::identity<const T0>, boost::hash<T0>, std::equal_to<T0>,
        adobe::capture_allocator<T0> >);
ADOBE_NAME_TYPE_2("closed_hash_map:version_1:adobe",
    adobe::version_1::closed_hash_map<T0, T1, boost::hash<T0>, std::equal_to<T0>,
            adobe::capture_allocator<adobe::pair<T0, T1> > >);

ADOBE_NAME_TYPE_5("closed_hash_set:version_1:adobe",
    adobe::version_1::closed_hash_set<T0, T1, T2, T3, T4 >);
ADOBE_NAME_TYPE_5("closed_hash_map:version_1:adobe",
    adobe::version_1::closed_hash_map<T0, T1, T2, T3, T4 >);

/*************************************************************************************************/

namespace boost {

template<   typename T,
            typename KeyTransform,
            typename Hash,
            typename Pred,
            typename A>
struct has_nothrow_constructor<adobe::version_1::closed_hash_set<T, KeyTransform, Hash, Pred, A> >
        : boost::mpl::true_ { };

template<typename Key,
         typename T,
         typename Hash,
         typename Pred,
         typename A>
struct has_nothrow_constructor<adobe::version_1::closed_hash_map<Key, T, Hash, Pred, A> >
    : boost::mpl::true_ { };

} // namespace boost

/*************************************************************************************************/

#endif

/*************************************************************************************************/

max_stars_repo_name: andyprowl/virtual-concepts

max_issues_repo_name: brycelelbach/asl

max_forks_repo_name: brycelelbach/asl


text:
// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#include <boost/hana/core/make.hpp>
namespace hana = boost::hana;

struct udt {
    int value;
    constexpr explicit udt(int v) : value(v) { }
};

static_assert(hana::make<udt>(1).value == 1, "");

int main() { }

max_issues_repo_name: lipper/arangodb

max_forks_repo_name: lipper/arangodb


text:
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/error_of.hpp>
#include <boost/accumulators/statistics/error_of_mean.hpp>
#include <boost/accumulators/statistics/max.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/accumulators/statistics/min.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/sum.hpp>
#include <boost/optional.hpp>
#include <chrono>
#include <cstdint>
#include <fstream>
#include <iostream>

namespace BM
{
/**
 * A class getting high resolution time durations.
 */
struct high_resolution_timer {
  high_resolution_timer() : m_start_time(take_time_stamp()) {}
  void
      /**
       * restart the timer
       */
      restart()
  {
    m_start_time = take_time_stamp();
  }

  /**
   * return the time, in seconds, since the timer was started.
   */
  double elapsed() const
  {
    return static_cast<double>(elapsed_nanoseconds()) * 1e-9;
  }

  /**
   * return the time, in nanoseconds, since the timer was
   * started.
   */
  std::uint64_t elapsed_nanoseconds() const
  {
    return take_time_stamp() - m_start_time;
  }

 private:
  std::uint64_t m_start_time;
  static std::uint64_t take_time_stamp()
  {
    // need to cast results of high_resolution_clock::now().time_since_epoch()
    // to nanoseconds in case system's minimum time resultion is different.
    // VS2012 for example does not return nanoseconds.
    return std::uint64_t(
        std::chrono::duration_cast<std::chrono::nanoseconds>(
            std::chrono::high_resolution_clock::now().time_since_epoch())
            .count());
  }
};

using namespace boost::accumulators;
/**
 * A class for accumulating timing statistice for benchmarks.
 */
typedef accumulator_set<
    double, stats<tag::count, tag::min, tag::max, tag::sum, tag::mean,
                  tag::variance, tag::error_of<tag::mean>>>
    timer_statistics;

/**
 * A class representing a measurement which consists of a nominal value with
 * uncertainty.
 */
template <typename T>
class Measurement
{
 public:
  T m_nominal;
  T m_uncertainty;

  Measurement() = default;
  Measurement(const Measurement&) = default;
  Measurement(T n, T u) : m_nominal(n), m_uncertainty(u) {}
  T nominal() const { return m_nominal; }
  T uncertainty() const { return m_uncertainty; }
  T upper() const { return m_nominal + m_uncertainty; }
  T lower() const { return m_nominal - m_uncertainty; }
  double relative_uncertainty() const { return m_uncertainty / m_nominal; }
  bool operator==(const Measurement& other) const
  {
    // two measurments are "consistent" if
    //
    // z^\prime = \frac{ |m_1 - m_2| }{ \sqrt{\Delta m_1^2 + \Delta m_2^2} } \ge
    // 2
    //
    T diff = std::abs(this->m_nominal - other.m_nominal);
    T unc = sqrt(this->m_uncertainty * this->m_uncertainty +
                 other.m_uncertainty * other.m_uncertainty);

    // don't want to inadvertently divide by zero.
    return diff <= 2 * unc;
  }

  bool operator<(const Measurement& other) const
  {
    // this measurment is less than the other if
    // 1. the two measurements are not consistent
    // 2. the nominal value of this is less than the other
    return !(*this == other) && this->m_nominal < other.m_nominal;
  }

  bool operator!=(const Measurement& other) const { return !(*this == other); }
  bool operator>(const Measurement& other) const
  {
    // this measurment is greater than the other if
    // 1. the two measurements are not consistent
    // 2. the nominal value of this is greater than the other
    return !(*this == other) && this->m_nominal > other.m_nominal;
  }

  bool operator<=(const Measurement& other) const
  {
    return (*this == other) || (*this < other);
  }

  bool operator>=(const Measurement& other) const
  {
    return (*this == other) || (*this > other);
  }

  template <typename TT>
  friend std::ostream& operator<<(std::ostream& os, const Measurement<TT>& uq)
  {
    os << uq.nominal() << " +/- " << uq.uncertainty();
    return os;
  }
};

struct Benchmark {
  high_resolution_timer m_timer;
  timer_statistics m_accum;
  double m_tolerance;
  size_t m_max_iters;
  size_t m_min_iters;
  size_t m_burn_iters;
  std::uint64_t m_max_runtime;  // max runtime in nanoseconds

  Benchmark()
      : m_tolerance(0.01),  // 1% error
        m_max_iters(100000),
        m_min_iters(10),
        m_burn_iters(10),
        m_max_runtime(100000000000ULL)
  {
  }

  /**
   * Add a duration to the Benchmark statistics
   */
  void add_time(std::uint64_t duration) { m_accum(duration); }
  /**
   * Executes a callable, returns the runtime in nanoseconds, and adds it to the
   * Benchmark statistics.
   */
  template <typename F>
  void record(const F& f)
  {
    m_timer.restart();
    f();
    auto duration = m_timer.elapsed_nanoseconds();
    add_time(duration);
  }

  /**
   * Executes a callable multiple times and accumulates statistics on the
   * runtime. The number of times the executable runs is determined by the
   * variance in run times. The callable will be ran until the relative variance
   * is less than the value stored in m_tolerance.
   */
  template <typename F>
  void run(const F& f)
  {
    high_resolution_timer run_timer;
    // allow algorithm to "burn in"
    reset();
    while (count(m_accum) < m_burn_iters &&
           run_timer.elapsed_nanoseconds() < m_max_runtime) {
      record(f);
    }

    reset();
    run_timer.restart();
    while (count(m_accum) < m_max_iters &&
           run_timer.elapsed_nanoseconds() < m_max_runtime &&
           (error_of<tag::mean>(m_accum) / mean(m_accum) > m_tolerance ||
            count(m_accum) < m_min_iters)) {
      record(f);
    }
    if (error_of<tag::mean>(m_accum) / mean(m_accum) > m_tolerance)
      std::cerr << "WARNING: relative error exceeds tolerance. Timing loop was "
                   "exited early for some reason.\n";
  }

  /**
   * Resets all benchmark statistics.
   */
  void reset() { m_accum = timer_statistics(); }
  /**
   * Returns the runtime measurement (in nanoseconds) compiled by the benchmark.
   * This will be a nominal value with an uncertainty that represents the error
   * of the mean.
   */
  Measurement<double> get_measurement() const
  {
    return Measurement<double>(mean(m_accum), error_of<tag::mean>(m_accum));
  }

  double get_longest_runtime() const { return max(m_accum); }
  double get_shortest_runtime() const { return min(m_accum); }
  double get_average_runtime() const { return mean(m_accum); }
  double get_stddev_runtime() const { return sqrt(variance(m_accum)); }
  double get_total_runtime() const { return sum(m_accum); }
  size_t get_iterations() const { return count(m_accum); }
  /**
   * Print the benchmarks current statistics.
   */
  void print_results(std::ostream& os) const
  {
    os << "iterations / max iterations: " << get_iterations() << " / "
       << m_max_iters << " (" << 100. * get_iterations() / m_max_iters
       << "%) \n";
    os << "total time / max allowed total runtime (ns): " << get_total_runtime()
       << " / " << m_max_runtime << " ("
       << 100 * get_total_runtime() / m_max_runtime << "%)\n";
    os << "min - max (ns): " << get_shortest_runtime() << " - "
       << get_longest_runtime() << "\n";
    os << "MEASUREMENT (ns): " << get_measurement() << "\n";
  }
};

/* // function to escape the optimizer */
/* static void escape(void* p) */
/* { */
/* #if defined(_MSC_VER) */
/* // TODO: find an implementation for VS */
/* #else */
/*   asm volatile("" : : "g"(p) : "memory"); */
/* #endif */
/* } */

/* static void clobber() */
/* { */
/* #if defined(_MSC_VER) */
/* // TODO: find an implementation for VS */
/* #else */
/*   asm volatile("" : : : "memory"); */
/* #endif */
/* } */

/**
 * A class for comparing benchmarks
 */
class BenchmarkComparison
{
};

/**
 * A class for testing performance improvements.
 */
class PerformanceBenchmark
{
 protected:
  std::string m_Name;
  boost::optional<Measurement<double>> m_Baseline;
  boost::optional<Measurement<double>> m_Minimum;

 public:
  struct Result {
    bool is_faster_than_baseline;
    Measurement<double> speedup_over_baseline;
    bool is_faster_than_minimum;
    Measurement<double> speedup_over_minimum;
  };

  static std::string getStorageFilename(std::string name)
  {
    return "PerformanceBenchmark-" + name + ".txt";
  }

  PerformanceBenchmark(const std::string& Name = "Default") : m_Name(Name)
  {
    std::string store = getStorageFilename(m_Name);

    std::ifstream in;
    in.open(store);
    if (in.is_open()) {
      double n, u;
      in >> n;
      in >> u;
      m_Baseline = Measurement<double>(n, u);
      in >> n;
      in >> u;
      m_Minimum = Measurement<double>(n, u);
    }
  }

  ~PerformanceBenchmark()
  {
    std::string store = getStorageFilename(m_Name);

    try {
      std::ofstream out;
      out.open(store);
      if (out.is_open()) {
        out << m_Baseline->nominal() << "\n";
        out << m_Baseline->uncertainty() << "\n";
        out << m_Minimum->nominal() << "\n";
        out << m_Minimum->uncertainty() << "\n";
      } else {
        std::cout << "ERROR: could not open " << store
                  << " to write benchmark data." << std::endl;
      }
      out.close();
    } catch (...) {
    }
  }

  Result operator()(const Benchmark& bench)
  {
    if (!m_Baseline) {
      m_Baseline = bench.get_measurement();
    }

    if (!m_Minimum) {
      m_Minimum = bench.get_measurement();
    }

    Measurement<double> measurement = bench.get_measurement();

    Result res;

    res.is_faster_than_baseline = measurement < m_Baseline;
    res.is_faster_than_minimum = measurement < m_Minimum;

    double s, dsn, dsd;

    s = m_Baseline->nominal() / measurement.nominal();
    dsn = m_Baseline->upper() / measurement.nominal() - s;
    dsd = m_Baseline->nominal() / measurement.upper() - s;
    res.speedup_over_baseline =
        Measurement<double>(s, sqrt(dsn * dsn + dsd * dsd));

    s = m_Minimum->nominal() / measurement.nominal();
    dsn = m_Minimum->upper() / measurement.nominal() - s;
    dsd = m_Minimum->nominal() / measurement.upper() - s;
    res.speedup_over_minimum =
        Measurement<double>(s, sqrt(dsn * dsn + dsd * dsd));

    if (measurement < m_Minimum.get()) {
      m_Minimum = measurement;
    }

    return res;
  }

  bool have_baseline() { return static_cast<bool>(m_Baseline); }
  Measurement<double> get_baseline()
  {
    if (m_Baseline) {
      return m_Baseline.get();
    } else {
      return Measurement<double>(0, 0);
    }
  }

  bool have_minimum() { return static_cast<bool>(m_Minimum); }
  Measurement<double> get_minimum()
  {
    if (m_Minimum) {
      return m_Minimum.get();
    } else {
      return Measurement<double>(0, 0);
    }
  }
};

}  // namespace BM


text:
/*****************************************************************************
*
* Rokko: Integrated Interface for libraries of eigenvalue decomposition
*
* Copyright (C) 2015 by Rokko Developers https://github.com/t-sakashita/rokko
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*****************************************************************************/

#include <boost/python.hpp>

#include "rokko_dense.h"
#include "rokko_sparse.h"
#include "utility/frank_matrix_c.h"

enum rokko {
  grid_col_major = rokko_grid_col_major, grid_row_major = rokko_grid_row_major,
  matrix_col_major = rokko_matrix_col_major, matrix_row_major = rokko_matrix_row_major
};

class wrap_rokko_localized_matrix {
	rokko_localized_matrix* raw;
public:
	wrap_rokko_localized_matrix(int dim1, int dim2, int matrix_major) {
		raw = new rokko_localized_matrix();
		rokko_localized_matrix_construct(raw, dim1, dim2, matrix_major);
	}
	rokko_localized_matrix* get_raw(void) {
		return raw;
	}

	void print(void) {
		rokko_localized_matrix_print(*raw);
	}

	~wrap_rokko_localized_matrix(void) {
		rokko_localized_matrix_destruct(raw);
		delete raw;
	}
};

class wrap_rokko_localized_vector {
	rokko_localized_vector* raw;
public:
	wrap_rokko_localized_vector(int dim1) {
		raw = new rokko_localized_vector();
		rokko_localized_vector_construct(raw, dim1);
	}
	rokko_localized_vector* get_raw(void) {
		return raw;
	}

	double get(int i) {
		return rokko_localized_vector_get(*raw, i);
	}

	~wrap_rokko_localized_vector(void) {
		rokko_localized_vector_destruct(raw);
		delete raw;
	}
};

class wrap_rokko_serial_dense_solver {
	rokko_serial_dense_solver* raw;
public:
	wrap_rokko_serial_dense_solver(char* solver_name, int argc, char** argv) {
		raw = new rokko_serial_dense_solver();
		rokko_serial_dense_solver_construct(raw, solver_name, argc, argv);
	}
	rokko_serial_dense_solver* get_raw(void) {
		return raw;
	}

	void diagonalize_localized_matrix(wrap_rokko_localized_matrix* mat, wrap_rokko_localized_vector* eigvals, wrap_rokko_localized_matrix* eigvecs) {
		rokko_serial_dense_solver_diagonalize_localized_matrix(raw, mat->get_raw(), eigvals->get_raw(), eigvecs->get_raw());
	}

	~wrap_rokko_serial_dense_solver(void) {
		rokko_serial_dense_solver_destruct(raw);
		delete raw;
	}
};

static void wrap_rokko_frank_matrix_generate_localized_matrix(wrap_rokko_localized_matrix* mat)
{
	rokko_frank_matrix_generate_localized_matrix(mat->get_raw());
}

#if defined(ROKKO_HAVE_PARALLEL_DENSE_SOLVER) || defined(ROKKO_HAVE_PARALLEL_SPARSE_SOLVER)

class wrap_rokko_grid {
	rokko_grid* raw;
public:
	wrap_rokko_grid(boost::python::object comm, int grid_major) {
		boost::python::object comm_func = comm.attr("py2f");
		boost::python::object comm_f_obj = comm_func();
		int comm_f = boost::python::extract<int>(comm_f_obj);
		raw = new rokko_grid();
		rokko_grid_construct_f(raw, comm_f, grid_major);
	}
	rokko_grid* get_raw(void) {
		return raw;
	}

	int get_myrank(void) {
		return rokko_grid_get_myrank(*raw);
	}

	int get_nprocs(void) {
		return rokko_grid_get_nprocs(*raw);
	}

	~wrap_rokko_grid(void) {
		rokko_grid_destruct(raw);
		delete raw;
	}
};

class wrap_rokko_distributed_matrix;

class wrap_rokko_parallel_dense_solver {
	rokko_parallel_dense_solver* raw;
public:
	wrap_rokko_parallel_dense_solver(char* solver_name, int argc, char** argv) {
		raw = new rokko_parallel_dense_solver();
		rokko_parallel_dense_solver_construct(raw, solver_name, argc, argv);
	}
	rokko_parallel_dense_solver* get_raw(void) {
		return raw;
	}

	void diagonalize_distributed_matrix(wrap_rokko_distributed_matrix*, wrap_rokko_localized_vector*, wrap_rokko_distributed_matrix*);

	~wrap_rokko_parallel_dense_solver(void) {
		rokko_parallel_dense_solver_destruct(raw);
		delete raw;
	}
};

class wrap_rokko_distributed_matrix {
	rokko_distributed_matrix* raw;
public:
	wrap_rokko_distributed_matrix(int dim1, int dim2, wrap_rokko_grid* grid, wrap_rokko_parallel_dense_solver* solver, int matrix_major) {
		raw = new rokko_distributed_matrix();
		rokko_distributed_matrix_construct(raw, dim1, dim2, *grid->get_raw(), *solver->get_raw(), matrix_major);
	}
	rokko_distributed_matrix* get_raw(void) {
		return raw;
	}

	void print(void) {
		rokko_distributed_matrix_print(*raw);
	}

	void set_local(int local_i, int local_j, double value) {
		rokko_distributed_matrix_set_local(raw, local_i, local_j, value);
	}

	double get_local(int local_i, int local_j) {
		return rokko_distributed_matrix_get_local(*raw, local_i, local_j);
	}

	void set_global(int global_i, int global_j, double value) {
		rokko_distributed_matrix_set_global(raw, global_i, global_j, value);
	}

	double get_global(int global_i, int global_j) {
		return rokko_distributed_matrix_get_global(*raw, global_i, global_j);
	}

	int get_m_local(void) {
		return rokko_distributed_matrix_get_m_local(*raw);
	}

	int get_n_local(void) {
		return rokko_distributed_matrix_get_n_local(*raw);
	}

	int get_m_global(void) {
		return rokko_distributed_matrix_get_m_global(*raw);
	}

	int get_n_global(void) {
		return rokko_distributed_matrix_get_n_global(*raw);
	}

	int get_nprocs(void) {
		return rokko_distributed_matrix_get_nprocs(*raw);
	}

	int get_myrank(void) {
		return rokko_distributed_matrix_get_myrank(*raw);
	}

	int translate_l2g_row(int local_i) {
		return rokko_distributed_matrix_translate_l2g_row(*raw, local_i);
	}

	int translate_l2g_col(int local_j) {
		return rokko_distributed_matrix_translate_l2g_col(*raw, local_j);
	}

	int translate_g2l_row(int global_i) {
		return rokko_distributed_matrix_translate_g2l_row(*raw, global_i);
	}

	int translate_g2l_col(int global_j) {
		return rokko_distributed_matrix_translate_g2l_col(*raw, global_j);
	}

	~wrap_rokko_distributed_matrix(void) {
		rokko_distributed_matrix_destruct(raw);
		delete raw;
	}
};

void wrap_rokko_parallel_dense_solver::diagonalize_distributed_matrix(wrap_rokko_distributed_matrix* mat, wrap_rokko_localized_vector* eigvals, wrap_rokko_distributed_matrix* eigvecs)
{
	rokko_parallel_dense_solver_diagonalize_distributed_matrix(raw, mat->get_raw(), eigvals->get_raw(), eigvecs->get_raw());
}

void wrap_rokko_gather(wrap_rokko_distributed_matrix* matrix, double* array, int root)
{
	return rokko_gather(matrix->get_raw(), array, root);
}

void wrap_rokko_scatter(double* global_array, wrap_rokko_distributed_matrix* matrix, int root)
{
	rokko_scatter(global_array, matrix->get_raw(), root);
}

void wrap_rokko_all_gather(wrap_rokko_distributed_matrix* matrix, double* array)
{
	rokko_all_gather(matrix->get_raw(), array);
}

static void wrap_rokko_frank_matrix_generate_distributed_matrix(wrap_rokko_distributed_matrix* mat)
{
	rokko_frank_matrix_generate_distributed_matrix(mat->get_raw());
}

class wrap_rokko_distributed_crs_matrix;

class wrap_rokko_parallel_sparse_solver {
	rokko_parallel_sparse_solver* raw;
public:
	wrap_rokko_parallel_sparse_solver(char* solver_name, int argc, char** argv) {
		raw = new rokko_parallel_sparse_solver();
		rokko_parallel_sparse_solver_construct(raw, solver_name, argc, argv);
	}
	rokko_parallel_sparse_solver* get_raw(void) {
		return raw;
	}

	void diagonalize_distributed_crs_matrix(wrap_rokko_distributed_crs_matrix*, int, int, int, double);

	double eigenvalue(int i) {
		return rokko_parallel_sparse_solver_eigenvalue(raw, i);
	}

	void eigenvector(int i, boost::python::list& vec) {
		int len_vec = boost::python::len(vec);
		double raw_vec[len_vec];
		rokko_parallel_sparse_solver_eigenvector(raw, i, raw_vec);
		for (int i = 0; i < len_vec; i++) {
			vec[i] = raw_vec[i];
		}
	}

	int num_conv(void) {
		return rokko_parallel_sparse_solver_num_conv(raw);
	}

	~wrap_rokko_parallel_sparse_solver(void) {
		rokko_parallel_sparse_solver_destruct(raw);
		delete raw;
	}
};

class wrap_rokko_distributed_crs_matrix {
	rokko_distributed_crs_matrix* raw;
public:
	wrap_rokko_distributed_crs_matrix(int dim1, int dim2, wrap_rokko_parallel_sparse_solver* solver) {
		raw = new rokko_distributed_crs_matrix();
		rokko_distributed_crs_matrix_construct(raw, dim1, dim2, *solver->get_raw());
	}
	rokko_distributed_crs_matrix* get_raw(void) {
		return raw;
	}

	void insert(int row, int col_size, boost::python::list& cols, boost::python::list& values) {
		int len_cols = boost::python::len(cols);
		int raw_cols[len_cols];
		for (int i = 0; i < len_cols; i++) {
			raw_cols[i] = boost::python::extract<int>(cols[i]);
		}

		int len_values = boost::python::len(values);
		double raw_values[len_values];
		for (int i = 0; i < len_cols; i++) {
			raw_values[i] = boost::python::extract<double>(values[i]);
		}

		rokko_distributed_crs_matrix_insert(raw, row, col_size, raw_cols, raw_values);
	}

	void complete(void) {
		rokko_distributed_crs_matrix_complete(raw);
	}

	int num_local_rows(void) {
		return rokko_distributed_crs_matrix_num_local_rows(raw);
	}

	int start_row(void) {
		return rokko_distributed_crs_matrix_start_row(raw);
	}

	int end_row(void) {
		return rokko_distributed_crs_matrix_end_row(raw);
	}

	void print(void) {
		rokko_distributed_crs_matrix_print(raw);
	}

	~wrap_rokko_distributed_crs_matrix(void) {
		rokko_distributed_crs_matrix_destruct(raw);
		delete raw;
	}
};

void wrap_rokko_parallel_sparse_solver::diagonalize_distributed_crs_matrix(wrap_rokko_distributed_crs_matrix* mat, int num_evals, int block_size, int max_iters, double tol)
{
	rokko_parallel_sparse_solver_diagonalize_distributed_crs_matrix(raw, mat->get_raw(), num_evals, block_size, max_iters, tol);
}

#endif

BOOST_PYTHON_MODULE(rokko_ext) {
  using namespace boost::python;

  enum_<rokko>("rokko")
    .value("grid_col_major", grid_col_major)
    .value("grid_row_major", grid_row_major)
    .value("matrix_col_major", matrix_col_major)
    .value("matrix_row_major", matrix_row_major);

  class_<wrap_rokko_serial_dense_solver>("rokko_serial_dense_solver", init<char*, int, char**>())
    .def("diagonalize_localized_matrix",
         &wrap_rokko_serial_dense_solver::diagonalize_localized_matrix);
  class_<wrap_rokko_localized_vector>("rokko_localized_vector", init<int>())
    .def("get", &wrap_rokko_localized_vector::get);
  class_<wrap_rokko_localized_matrix>("rokko_localized_matrix", init<int, int, int>())
    .def("show", &wrap_rokko_localized_matrix::print);
  def("rokko_frank_matrix_generate_localized_matrix",
      &wrap_rokko_frank_matrix_generate_localized_matrix);

#if defined(ROKKO_HAVE_PARALLEL_DENSE_SOLVER) || defined(ROKKO_HAVE_PARALLEL_SPARSE_SOLVER)
  class_<wrap_rokko_grid>("rokko_grid", init<boost::python::object, int>())
    .def("get_myrank", &wrap_rokko_grid::get_myrank)
    .def("get_nprocs", &wrap_rokko_grid::get_nprocs);
  class_<wrap_rokko_parallel_dense_solver>("rokko_parallel_dense_solver",
                                           init<char*, int, char**>())
    .def("diagonalize_distributed_matrix",
         &wrap_rokko_parallel_dense_solver::diagonalize_distributed_matrix);
  class_<wrap_rokko_distributed_matrix>("rokko_distributed_matrix",
    init<int, int, wrap_rokko_grid*, wrap_rokko_parallel_dense_solver*, int>())
    .def("show", &wrap_rokko_distributed_matrix::print)
    .def("set_local", &wrap_rokko_distributed_matrix::set_local)
    .def("get_local", &wrap_rokko_distributed_matrix::get_local)
    .def("set_global", &wrap_rokko_distributed_matrix::set_global)
    .def("get_global", &wrap_rokko_distributed_matrix::get_global)
    .def("get_m_local", &wrap_rokko_distributed_matrix::get_m_local)
    .def("get_n_local", &wrap_rokko_distributed_matrix::get_n_local)
    .def("get_m_global", &wrap_rokko_distributed_matrix::get_m_global)
    .def("get_n_global", &wrap_rokko_distributed_matrix::get_n_global)
    .def("get_nprocs", &wrap_rokko_distributed_matrix::get_nprocs)
    .def("get_myrank", &wrap_rokko_distributed_matrix::get_myrank)
    .def("translate_l2g_row", &wrap_rokko_distributed_matrix::translate_l2g_row)
    .def("translate_l2g_col", &wrap_rokko_distributed_matrix::translate_l2g_col)
    .def("translate_g2l_row", &wrap_rokko_distributed_matrix::translate_g2l_row)
    .def("translate_g2l_col", &wrap_rokko_distributed_matrix::translate_g2l_col);
  class_<wrap_rokko_parallel_sparse_solver>("rokko_parallel_sparse_solver",
                                            init<char*, int, char**>())
    .def("diagonalize_distributed_crs_matrix",
         &wrap_rokko_parallel_sparse_solver::diagonalize_distributed_crs_matrix)
    .def("eigenvalue", &wrap_rokko_parallel_sparse_solver::eigenvalue)
    .def("eigenvector", &wrap_rokko_parallel_sparse_solver::eigenvector)
    .def("num_conv", &wrap_rokko_parallel_sparse_solver::num_conv);
  class_<wrap_rokko_distributed_crs_matrix>("rokko_distributed_crs_matrix",
                                            init<int, int, wrap_rokko_parallel_sparse_solver*>())
    .def("insert", &wrap_rokko_distributed_crs_matrix::insert)
    .def("complete", &wrap_rokko_distributed_crs_matrix::complete)
    .def("num_local_rows", &wrap_rokko_distributed_crs_matrix::num_local_rows)
    .def("start_row", &wrap_rokko_distributed_crs_matrix::start_row)
    .def("end_row", &wrap_rokko_distributed_crs_matrix::end_row)
    .def("show", &wrap_rokko_distributed_crs_matrix::print);
/*
  With a callback:
  def("rokko_distributed_matrix_generate_function", &rokko_distributed_matrix_generate_function);
*/
  def("rokko_gather", &wrap_rokko_gather);
  def("rokko_scatter", &wrap_rokko_scatter);
  def("rokko_all_gather", &wrap_rokko_all_gather);

  def("rokko_frank_matrix_generate_distributed_matrix",
      &wrap_rokko_frank_matrix_generate_distributed_matrix);
#endif
}

max_stars_repo_name: wistaria/rokko

max_issues_repo_name: wistaria/rokko

max_forks_repo_name: wistaria/rokko


text:
#include <Storages/MergeTree/PartMovesBetweenShardsOrchestrator.h>
#include <Storages/MergeTree/PinnedPartUUIDs.h>
#include <Storages/StorageReplicatedMergeTree.h>
#include <boost/range/adaptor/map.hpp>
#include <Poco/JSON/JSON.h>
#include <Poco/JSON/Object.h>
#include <Poco/JSON/Parser.h>

namespace DB
{
PartMovesBetweenShardsOrchestrator::PartMovesBetweenShardsOrchestrator(StorageReplicatedMergeTree & storage_)
    : storage(storage_)
    , zookeeper_path(storage.zookeeper_path)
    , logger_name(storage.getStorageID().getFullTableName() + " (PartMovesBetweenShardsOrchestrator)")
    , log(&Poco::Logger::get(logger_name))
    , entries_znode_path(zookeeper_path + "/part_moves_shard")
{
    /// Schedule pool is not designed for long-running tasks. TODO replace with a separate thread?
    task = storage.getContext()->getSchedulePool().createTask(logger_name, [this]{ run(); });
}

void PartMovesBetweenShardsOrchestrator::run()
{
    if (!storage.getSettings()->part_moves_between_shards_enable)
        return;

    if (need_stop)
        return;

    auto sleep_ms = 10;

    try
    {
        fetchStateFromZK();

        if (step())
            fetchStateFromZK();
        else
            sleep_ms = 3 * 1000;
    }
    catch (...)
    {
        tryLogCurrentException(log, __PRETTY_FUNCTION__);
    }

    task->scheduleAfter(sleep_ms);
}

void PartMovesBetweenShardsOrchestrator::shutdown()
{
    need_stop = true;
    task->deactivate();
    LOG_TRACE(log, "PartMovesBetweenShardsOrchestrator thread finished");
}

void PartMovesBetweenShardsOrchestrator::fetchStateFromZK()
{
    std::lock_guard lock(state_mutex);

    entries.clear();

    auto zk = storage.getZooKeeper();

    Strings task_names = zk->getChildren(entries_znode_path);
    for (auto const & task_name : task_names)
    {
        PartMovesBetweenShardsOrchestrator::Entry e;
        Coordination::Stat stat;

        e.znode_path = entries_znode_path + "/" + task_name;

        auto entry_str = zk->get(e.znode_path, &stat);
        e.fromString(entry_str);

        e.version = stat.version;
        e.znode_name = task_name;

        entries[task_name] = std::move(e);
    }
}

bool PartMovesBetweenShardsOrchestrator::step()
{
    if (!storage.is_leader)
        return false;

    auto zk = storage.getZooKeeper();

    std::optional<Entry> entry_to_process;

    /// Try find an entry to process and copy it.
    {
        std::lock_guard lock(state_mutex);

        for (auto const & entry : entries | boost::adaptors::map_values)
        {
            if (entry.state.value == EntryState::DONE || entry.state.value == EntryState::CANCELLED)
                continue;

            entry_to_process.emplace(entry);
            break;
        }
    }

    if (!entry_to_process.has_value())
        return false;

    /// Since some state transitions are long running (waiting on replicas acknowledgement we create this lock to avoid
    /// other replicas trying to do the same work. All state transitions should be idempotent so is is safe to lose the
    /// lock and have another replica retry.
    ///
    /// Note: This blocks all other entries from being executed. Technical debt.
    zkutil::EphemeralNodeHolder::Ptr entry_node_holder;

    try
    {
        entry_node_holder = zkutil::EphemeralNodeHolder::create(entry_to_process->znode_path + "/lock_holder", *zk, storage.replica_name);
    }
    catch (const Coordination::Exception & e)
    {
        if (e.code == Coordination::Error::ZNODEEXISTS)
        {
            LOG_DEBUG(log, "Task {} is being processed by another replica", entry_to_process->znode_name);
            return false;
        }

        throw;
    }

    try
    {
        /// Use the same ZooKeeper connection. If we'd lost the lock then connection
        /// will become expired and all consequent operations will fail.
        stepEntry(entry_to_process.value(), zk);
    }
    catch (...)
    {
        tryLogCurrentException(log, __PRETTY_FUNCTION__);

        Entry entry_copy = entry_to_process.value();
        entry_copy.last_exception_msg = getCurrentExceptionMessage(false);
        entry_copy.update_time = std::time(nullptr);
        zk->set(entry_copy.znode_path, entry_copy.toString(), entry_copy.version);

        return false;
    }

    return true;
}

void PartMovesBetweenShardsOrchestrator::stepEntry(const Entry & entry, zkutil::ZooKeeperPtr zk)
{
    switch (entry.state.value)
    {
        case EntryState::DONE:
            break;

        case EntryState::CANCELLED:
            break;

        case EntryState::TODO:
        {
            /// State transition.
            Entry entry_copy = entry;
            entry_copy.state = EntryState::SYNC_SOURCE;
            entry_copy.update_time = std::time(nullptr);
            zk->set(entry_copy.znode_path, entry_copy.toString(), entry_copy.version);
        }
        break;

        case EntryState::SYNC_SOURCE:
        {
            {
                /// Log entry.
                Coordination::Requests ops;
                ops.emplace_back(zkutil::makeCheckRequest(entry.znode_path, entry.version));

                ReplicatedMergeTreeLogEntryData log_entry;
                log_entry.type = ReplicatedMergeTreeLogEntryData::SYNC_PINNED_PART_UUIDS;
                log_entry.create_time = std::time(nullptr);
                log_entry.source_replica = storage.replica_name;
                ops.emplace_back(zkutil::makeSetRequest(zookeeper_path + "/log", "", -1));
                ops.emplace_back(zkutil::makeCreateRequest(
                    zookeeper_path + "/log/log-", log_entry.toString(), zkutil::CreateMode::PersistentSequential));

                Coordination::Responses responses;
                Coordination::Error rc = zk->tryMulti(ops, responses);
                zkutil::KeeperMultiException::check(rc, ops, responses);

                String log_znode_path = dynamic_cast<const Coordination::CreateResponse &>(*responses.back()).path_created;
                log_entry.znode_name = log_znode_path.substr(log_znode_path.find_last_of('/') + 1);

                /// This wait in background schedule pool is useless. It'd be
                /// better to have some notification which will call `step`
                /// function when all replicated will finish. TODO.
                storage.waitForAllReplicasToProcessLogEntry(log_entry, true);
            }

            {
                /// State transition.
                Entry entry_copy = entry;
                entry_copy.state = EntryState::SYNC_DESTINATION;
                entry_copy.update_time = std::time(nullptr);
                zk->set(entry_copy.znode_path, entry_copy.toString(), entry_copy.version);
            }
        }
        break;

        case EntryState::SYNC_DESTINATION:
        {
            {
                /// Log entry.
                Coordination::Requests ops;
                ops.emplace_back(zkutil::makeCheckRequest(entry.znode_path, entry.version));

                ReplicatedMergeTreeLogEntryData log_entry;
                log_entry.type = ReplicatedMergeTreeLogEntryData::SYNC_PINNED_PART_UUIDS;
                log_entry.create_time = std::time(nullptr);
                log_entry.source_replica = storage.replica_name;
                log_entry.source_shard = zookeeper_path;

                ops.emplace_back(zkutil::makeSetRequest(entry.to_shard + "/log", "", -1));
                ops.emplace_back(zkutil::makeCreateRequest(
                    entry.to_shard + "/log/log-", log_entry.toString(), zkutil::CreateMode::PersistentSequential));

                Coordination::Responses responses;
                Coordination::Error rc = zk->tryMulti(ops, responses);
                zkutil::KeeperMultiException::check(rc, ops, responses);

                String log_znode_path = dynamic_cast<const Coordination::CreateResponse &>(*responses.back()).path_created;
                log_entry.znode_name = log_znode_path.substr(log_znode_path.find_last_of('/') + 1);

                storage.waitForAllTableReplicasToProcessLogEntry(entry.to_shard, log_entry, true);
            }

            {
                /// State transition.
                Entry entry_copy = entry;
                entry_copy.state = EntryState::DESTINATION_FETCH;
                entry_copy.update_time = std::time(nullptr);
                zk->set(entry_copy.znode_path, entry_copy.toString(), entry_copy.version);
            }
        }
        break;

        case EntryState::DESTINATION_FETCH:
        {
            /// Make sure table structure doesn't change when there are part movements in progress.
            {
                Coordination::Requests ops;
                ops.emplace_back(zkutil::makeCheckRequest(entry.znode_path, entry.version));

                /// Log entry.
                ReplicatedMergeTreeLogEntryData log_entry;
                log_entry.type = ReplicatedMergeTreeLogEntryData::CLONE_PART_FROM_SHARD;
                log_entry.create_time = std::time(nullptr);
                log_entry.new_part_name = entry.part_name;
                log_entry.source_replica = storage.replica_name;
                log_entry.source_shard = zookeeper_path;
                ops.emplace_back(zkutil::makeSetRequest(entry.to_shard + "/log", "", -1));
                ops.emplace_back(zkutil::makeCreateRequest(
                    entry.to_shard + "/log/log-", log_entry.toString(), zkutil::CreateMode::PersistentSequential));

                Coordination::Responses responses;
                Coordination::Error rc = zk->tryMulti(ops, responses);
                zkutil::KeeperMultiException::check(rc, ops, responses);

                String log_znode_path = dynamic_cast<const Coordination::CreateResponse &>(*responses.back()).path_created;
                log_entry.znode_name = log_znode_path.substr(log_znode_path.find_last_of('/') + 1);

                storage.waitForAllTableReplicasToProcessLogEntry(entry.to_shard, log_entry, true);
            }

            {
                /// State transition.
                Entry entry_copy = entry;
                entry_copy.state = EntryState::DESTINATION_ATTACH;
                entry_copy.update_time = std::time(nullptr);
                zk->set(entry_copy.znode_path, entry_copy.toString(), entry_copy.version);
            }
        }
        break;

        case EntryState::DESTINATION_ATTACH:
        {
            /// There is a chance that attach on destination will fail and this task will be left in the queue forever.
            {
                Coordination::Requests ops;
                ops.emplace_back(zkutil::makeCheckRequest(entry.znode_path, entry.version));

                auto part = storage.getActiveContainingPart(entry.part_name);
                /// Allocating block number in other replicas zookeeper path
                /// TODO Maybe we can do better.
                auto block_number_lock = storage.allocateBlockNumber(part->info.partition_id, zk, "", entry.to_shard);
                auto block_number = block_number_lock->getNumber();

                auto part_info = part->info;
                part_info.min_block = block_number;
                part_info.max_block = block_number;
                part_info.level = 0;
                part_info.mutation = 0;

                /// Attach log entry (all replicas already fetched part)
                ReplicatedMergeTreeLogEntryData log_entry;
                log_entry.type = ReplicatedMergeTreeLogEntryData::ATTACH_PART;
                log_entry.part_checksum = part->checksums.getTotalChecksumHex();
                log_entry.create_time = std::time(nullptr);
                log_entry.new_part_name = part_info.getPartName();
                ops.emplace_back(zkutil::makeSetRequest(entry.to_shard + "/log", "", -1));
                ops.emplace_back(zkutil::makeCreateRequest(
                    entry.to_shard + "/log/log-", log_entry.toString(), zkutil::CreateMode::PersistentSequential));

                Coordination::Responses responses;
                Coordination::Error rc = zk->tryMulti(ops, responses);
                zkutil::KeeperMultiException::check(rc, ops, responses);

                String log_znode_path = dynamic_cast<const Coordination::CreateResponse &>(*responses.back()).path_created;
                log_entry.znode_name = log_znode_path.substr(log_znode_path.find_last_of('/') + 1);

                storage.waitForAllTableReplicasToProcessLogEntry(entry.to_shard, log_entry, true);
            }

            {
                /// State transition.
                Entry entry_copy = entry;
                entry_copy.state = EntryState::SOURCE_DROP_PRE_DELAY;
                entry_copy.update_time = std::time(nullptr);
                zk->set(entry_copy.znode_path, entry_copy.toString(), entry_copy.version);
            }
        }
        break;

        case EntryState::SOURCE_DROP_PRE_DELAY:
        {
            std::this_thread::sleep_for(std::chrono::seconds(storage.getSettings()->part_moves_between_shards_delay_seconds));

            /// State transition.
            Entry entry_copy = entry;
            entry_copy.state = EntryState::SOURCE_DROP;
            entry_copy.update_time = std::time(nullptr);
            zk->set(entry_copy.znode_path, entry_copy.toString(), entry_copy.version);
        }
        break;

        case EntryState::SOURCE_DROP:
        {
            {
                ReplicatedMergeTreeLogEntry log_entry;
                if (storage.dropPartImpl(zk, entry.part_name, log_entry, false, false))
                    storage.waitForAllReplicasToProcessLogEntry(log_entry, true);
            }

            {
                /// State transition.
                Entry entry_copy = entry;
                entry_copy.state = EntryState::SOURCE_DROP_POST_DELAY;
                entry_copy.update_time = std::time(nullptr);
                zk->set(entry_copy.znode_path, entry_copy.toString(), entry_copy.version);
            }
        }
        break;

        case EntryState::SOURCE_DROP_POST_DELAY:
        {
            std::this_thread::sleep_for(std::chrono::seconds(storage.getSettings()->part_moves_between_shards_delay_seconds));

            /// State transition.
            Entry entry_copy = entry;
            entry_copy.state = EntryState::REMOVE_UUID_PIN;
            entry_copy.update_time = std::time(nullptr);
            zk->set(entry_copy.znode_path, entry_copy.toString(), entry_copy.version);
        }
        break;

        case EntryState::REMOVE_UUID_PIN:
        {
            {
                PinnedPartUUIDs src_pins;
                PinnedPartUUIDs dst_pins;

                {
                    String s = zk->get(zookeeper_path + "/pinned_part_uuids", &src_pins.stat);
                    src_pins.fromString(s);
                }

                {
                    String s = zk->get(entry.to_shard + "/pinned_part_uuids", &dst_pins.stat);
                    dst_pins.fromString(s);
                }

                src_pins.part_uuids.erase(entry.part_uuid);
                dst_pins.part_uuids.erase(entry.part_uuid);

                Coordination::Requests ops;
                ops.emplace_back(zkutil::makeSetRequest(zookeeper_path + "/pinned_part_uuids", src_pins.toString(), src_pins.stat.version));
                ops.emplace_back(zkutil::makeSetRequest(entry.to_shard + "/pinned_part_uuids", dst_pins.toString(), dst_pins.stat.version));

                zk->multi(ops);
            }

            /// State transition.
            Entry entry_copy = entry;
            entry_copy.state = EntryState::DONE;
            entry_copy.update_time = std::time(nullptr);
            zk->set(entry_copy.znode_path, entry_copy.toString(), entry_copy.version);
        }
        break;
    }
}

std::vector<PartMovesBetweenShardsOrchestrator::Entry> PartMovesBetweenShardsOrchestrator::getEntries() const
{
    std::lock_guard lock(state_mutex);

    std::vector<Entry> res;

    for (const auto & e : entries)
        res.push_back(e.second);

    return res;
}

String PartMovesBetweenShardsOrchestrator::Entry::toString() const
{
    Poco::JSON::Object json;

    json.set(JSON_KEY_CREATE_TIME, DB::toString(create_time));
    json.set(JSON_KEY_UPDATE_TIME, DB::toString(update_time));
    json.set(JSON_KEY_TASK_UUID, DB::toString(task_uuid));
    json.set(JSON_KEY_PART_NAME, part_name);
    json.set(JSON_KEY_PART_UUID, DB::toString(part_uuid));
    json.set(JSON_KEY_TO_SHARD, to_shard);
    json.set(JSON_KEY_STATE, state.toString());
    json.set(JSON_KEY_LAST_EX_MSG, last_exception_msg);

    std::ostringstream oss; // STYLE_CHECK_ALLOW_STD_STRING_STREAM
    oss.exceptions(std::ios::failbit);
    json.stringify(oss);

    return oss.str();
}

void PartMovesBetweenShardsOrchestrator::Entry::fromString(const String & buf)
{
    Poco::JSON::Parser parser;
    auto json = parser.parse(buf).extract<Poco::JSON::Object::Ptr>();

    create_time = parseFromString<time_t>(json->getValue<std::string>(JSON_KEY_CREATE_TIME));
    update_time = parseFromString<time_t>(json->getValue<std::string>(JSON_KEY_UPDATE_TIME));
    task_uuid = parseFromString<UUID>(json->getValue<std::string>(JSON_KEY_TASK_UUID));
    part_name = json->getValue<std::string>(JSON_KEY_PART_NAME);
    part_uuid = parseFromString<UUID>(json->getValue<std::string>(JSON_KEY_PART_UUID));
    to_shard = json->getValue<std::string>(JSON_KEY_TO_SHARD);
    state.value = EntryState::fromString(json->getValue<std::string>(JSON_KEY_STATE));
    last_exception_msg = json->getValue<std::string>(JSON_KEY_LAST_EX_MSG);
}

}

max_stars_repo_name: mga-chka/ClickHouse

max_issues_repo_name: mga-chka/ClickHouse

max_forks_repo_name: mga-chka/ClickHouse


text:
#include <iostream>
#include <string>
#include "LaserOnlineModel.h"
#include "PerUserOnlineModel.h"
#include <vector>
#include <sstream>
#include <boost/filesystem.hpp>
namespace bfs = boost::filesystem;

using namespace std;
using namespace predict;
using namespace clustering;
string TESTHOME="./testlaseronlinemodel";
void test()
{
    bfs::remove_all(TESTHOME);
    LaserOnlineModel* lom_ptr = LaserOnlineModel::get();
    lom_ptr->init(TESTHOME);
    for(int i = 0; i < 10; i++)
    {
        std::vector<float> tmp(10, 0.1*(i+1));
        PerUserOnlineModel puom;
        stringstream ss;
        ss<<"test"<<i;
        puom.setUserName(ss.str());
        puom.setArgs(tmp);
        lom_ptr->update(puom);
    }
    lom_ptr->release();
    lom_ptr->init(TESTHOME);
    for(int i = 0; i < 10; i++)
    {
        PerUserOnlineModel puom;
        stringstream ss;
        ss<<"test"<<i;
        bool res = lom_ptr->get(ss.str(),puom);
        cout<<ss.str()<<" :"<<endl;

        for(int j = 0; j < puom.getArgs().size(); j++)
        {
            cout<< puom.getArgs()[j]<<" ";
        }
        cout<<endl;
    }
    lom_ptr->release();
}
int main()
{
    test();

}


text:
#include <exception>
#include <iostream>
#include <string>
#include <vector>
#include <boost/any.hpp>

int main() {
    std::vector<boost::any> v{};
    v.push_back(1);
    v.push_back(1.0);
    v.push_back("1.0");
    v.push_back(std::string("1.0"));

    // v[0] : int
    // v[1] : double
    // v[2] : const char *
    // v[3] : std::string

    try {
        // get value back, must use exact type
        std::cout << boost::any_cast<int>(v[0]) << "\n";
        std::cout << boost::any_cast<double>(v[1]) << "\n";
        std::cout << boost::any_cast<const char *>(v[2]) << "\n";
        std::cout << boost::any_cast<std::string>(v[3]) << "\n";

        //
        // failure cast
        //

        // return nullptr
        auto ptr = boost::any_cast<char>(&v[0]);
        if (!ptr)
            std::cout << "bad cast\n";

        // throw bad_any_cast
        std::cout << boost::any_cast<char>(v[0]) << "\n";
    }
    catch (std::exception& e) {
        std::cout << e.what() << "\n";
    }
}


text:
#define BOOST_TEST_MODULE preprocessor

#include <boost/aura/preprocessor.hpp>

#include <boost/test/unit_test.hpp>

#include <algorithm>
#include <complex>
#include <iostream>
#include <vector>

// basic
// _____________________________________________________________________________

BOOST_AUTO_TEST_CASE(basic)
{
        {
                std::string input("foo <<<foo>>> bar");
                boost::aura::preprocessor p;

                float f = 5.0;
                p.add_define("foo", f);
                auto result = p(input);
                BOOST_CHECK(result == "foo 5.0000000f bar");
        }
        {
                std::string input("foo <<<foo>>> bar");
                boost::aura::preprocessor p;

                std::size_t f = 5;
                p.add_define("foo", f);
                auto result = p(input);
                BOOST_CHECK(result == "foo 5 bar");
        }
        {
                std::string input("foo <<<foo>>> bar");
                boost::aura::preprocessor p;

                std::vector<double> v({1.0000001, 2.0});
                p.add_define("foo", v);
                auto result = p(input);
                BOOST_CHECK(result == "foo {1.0000001,2} bar");
        }
}

max_stars_repo_name: sschaetz/aura

max_issues_repo_name: sschaetz/aura

max_forks_repo_name: sschaetz/aura


text:
/////////////////////////////////////////////////////////////////////////////////
// MIT License
//
// Copyright (c) 2020 RoboLab19
//
// 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.
/////////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <fstream>
#include <sstream> //std::stringstream

#include <boost/tokenizer.hpp>
#include <boost/foreach.hpp>

#include "ros/ros.h"

#include "IK.h"
#include "Utils.h"
#include "GCodeController.h"

using namespace boost;

GCodeController::GCodeController(RobotLegs legs):
    m_legs(legs), m_primitives(*this), m_robotState(*this)
{
    reset();
}

void GCodeController::reset()
{
    m_speedInPercent = 100;

    m_movePoints.clear();
    m_robotState.Init();
}

void GCodeController::set_legs_base(double length, double width)
{
    m_robotState.m_baseLength = length;
    m_robotState.m_baseWidth  = width;
}

#define CHECK_MIN_PARAMS_COUNT(n)  if (paramsInt.size() < n) { printf("Not enough params (should be %d) in g-code: %s\n", n, line.c_str()); return false; }

IK::Vector paramsToVector(std::map<int, int> paramsInt)
{
    IK::Vector v;
    v.x = paramsInt[0] / 1000.0;
    v.y = paramsInt[1] / 1000.0;
    v.z = paramsInt[2] / 1000.0;

    return v;
}

double getSpeed(int speedInPercent)
{
    return kMaxSpeed * speedInPercent / 100.0;
}

bool GCodeController::run(const std::string gcode, bool simulate)
{
    tokenizer<char_separator<char> > lines(gcode, char_separator<char>("\n"));
    for (auto line: lines) {
        tokenizer<char_separator<char> > tokens(line, char_separator<char>(" "));

        auto it = tokens.begin();
        if (it.at_end()) continue;

        std::string cmd = utils::get_trim(it->c_str());
        if (cmd.substr(0, 1) == "#" || cmd.substr(0, 2) == "//") continue; // Skip commented line

        it++;

        // Tokens to map
        std::map<int, std::string> paramsStr;
        std::map<int, int> paramsInt;
        for (int n = 0; !it.at_end(); it++, n++) {
            auto str = utils::get_trim(*it);
            paramsStr[n] = str;
            paramsInt[n] = atoi(str.c_str());
        }

        if (cmd == "S") {
            CHECK_MIN_PARAMS_COUNT(1)
            m_speedInPercent = paramsInt[0];     // speed in %
        }
        else if (cmd.length() == 2 && cmd[0] == 'L') { // Set leg position
            CHECK_MIN_PARAMS_COUNT(3)

            int legId = cmd[1] - '0';
            m_robotState.MoveLeg(legId, paramsToVector(paramsInt), getSpeed(m_speedInPercent));
        }
        else if (cmd.length() == 3 && cmd[0] == 'L' && cmd[2] == '+') {
            CHECK_MIN_PARAMS_COUNT(3)

            int legId = cmd[1] - '0';
            IK::Vector pos = m_robotState.GetPosByVector(legId, paramsToVector(paramsInt));
            m_robotState.MoveLeg(legId, pos, getSpeed(m_speedInPercent));
        }
        else if (cmd == "M") { // Move base (absolute)
            CHECK_MIN_PARAMS_COUNT(3)

            IK::Vector pos = paramsToVector(paramsInt);
            if (!m_robotState.SetBase(pos, getSpeed(m_speedInPercent))) return false;
        }
        else if (cmd == "M+") { // Move base (relative)
            CHECK_MIN_PARAMS_COUNT(3)

            IK::Vector v = paramsToVector(paramsInt);
            if (!m_robotState.MoveBase(v, getSpeed(m_speedInPercent))) return false;
        }
        else if (cmd == "BASE_ROT") {  // Set base rotation
            CHECK_MIN_PARAMS_COUNT(3)

            m_robotState.m_rot.x = IK::toRad(paramsInt[0]);
            m_robotState.m_rot.y = IK::toRad(paramsInt[1]);
            m_robotState.m_rot.z = IK::toRad(paramsInt[2]);
        }
        else if (cmd == "BASE_OFFSET") { // Set base offset
            CHECK_MIN_PARAMS_COUNT(3)

            m_robotState.m_baseOffset = paramsToVector(paramsInt);
        }
        else if (cmd == "LOOK_AT") {
            CHECK_MIN_PARAMS_COUNT(3)

            IK::Vector vec = paramsToVector(paramsInt);
            if (!look_at(vec)) return false;
        }
        else if (cmd == "LEG_ADJ") { // Leg to adjust
            CHECK_MIN_PARAMS_COUNT(4)

            for (int i = 0; i < 4; ++i) m_robotState.m_legToAdjust[i] = paramsInt[i];
        }
        else if (cmd == "FLUSH") {    // Do pending movements (to remove dependency between sequences during spline moves)
            do_move(simulate);
        }
        else {
            if (!do_move(simulate)) return false;

            if (cmd == "W") {   // Wait (in ms)
                CHECK_MIN_PARAMS_COUNT(1)
                utils::delay_ms(paramsInt[0]);
            }
            else if (cmd == "WM") {   // Wait for move completion
                m_robotState.WaitForMove();
            }
            else if (cmd == "WM2") {   // Wait for move completion (1.5 times longer)
                m_robotState.WaitForMove(1.5);
            }
            else if (cmd[0] == '$') {
                if (!m_primitives.run_primitive(cmd, paramsInt, simulate)) return false;
            }
            else {
                printf("Unknown command: %s\n", cmd.c_str());
                return false;
            }
        }
    }

    return do_move(simulate);
}

bool GCodeController::add_point(int legId, IK::Vector pos, int moveTime)
{
    // Validate point first
    if (!m_legs[legId]->move(pos.x, pos.y, pos.z, moveTime, true)) {
        printf("Cannot move leg %d to [%.3f %.3f %.3f]!\n", legId, pos.x, pos.y, pos.z);
        return false;
    }

    MovePoint movePoint;
    movePoint.legId    = legId;
    movePoint.moveTime = moveTime;

    movePoint.p[0] = pos.x;
    movePoint.p[1] = pos.y;
    movePoint.p[2] = pos.z;

    m_movePoints.emplace_back(movePoint);
    return true;
}

bool GCodeController::look_at(IK::Vector vec)
{
    // double mag = sqrt(dx*dx + dy*dy + dz*dz);
    // dx /= mag;
    // dy /= mag;
    // dz /= mag;

    double pitch;
    double yaw;
    IK::vectorToEuler(vec.x, vec.y, vec.z, pitch, yaw);

    m_robotState.m_rot.x = 0;
    m_robotState.m_rot.y = pitch;
    m_robotState.m_rot.z = yaw;

    return true;
}

bool GCodeController::do_move(bool simulate)
{
    for (auto p: m_movePoints) {
        if (!m_legs[p.legId]->move(p.p[0], p.p[1], p.p[2], p.moveTime, simulate)) return false;
    }

    if (!simulate)
        m_movePoints.clear();

    return true;
}

bool GCodeController::run_from_file(const std::string& path)
{
    std::ifstream file(path);
    if (!file.is_open()) {
        printf("GCodeController::run_from_file(): Failed to open file: %s\n", path.c_str());
        return false;
    }

    std::stringstream strStream;
    strStream << file.rdbuf();

    return run(strStream.str());
}