text:
#pragma once

//          Copyright Rein Halbersma 2014-2021.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

#include <iostream.hpp>                 // operator<<, operator>>
#include <traits.hpp>                   // has_back, has_front, is_dereferencable, is_equality_comparable
#include <boost/test/unit_test.hpp>     // BOOST_CHECK, BOOST_CHECK_EQUAL, BOOST_CHECK_EQUAL_COLLECTIONS, BOOST_CHECK_NE
#include <algorithm>                    // count, equal, equal_range, find, for_each, lexicographical_compare,
                                        // lexicographical_compare_three_way, lower_bound, upper_bound
#include <compare>                      // strong_ordering
#include <concepts>                     // signed_integral, unsigned_integral
#include <initializer_list>             // initializer_list
#include <iterator>                     // distance, empty, next, prev, size, ssize
#include <limits>                       // max
#include <memory>                       // addressof
#include <numeric>                      // accumulate
#include <type_traits>                  // is_constructible_v, is_convertible_v, is_default_constructible_v, is_same_v
#include <utility>                      // pair

namespace xstd {

template<class X>
struct nested_types
{
                                                                                // [tab:container.req]
        static_assert(std::is_convertible_v<typename X::iterator, typename X::const_iterator>);
        static_assert(std::is_same_v<typename X::      iterator::value_type, typename X::value_type>);
        static_assert(std::is_same_v<typename X::const_iterator::value_type, typename X::value_type>);
        static_assert(std::signed_integral<typename X::difference_type>);
        static_assert(std::is_same_v<typename X::difference_type, typename std::iterator_traits<typename X::iterator>::difference_type>);
        static_assert(std::unsigned_integral<typename X::size_type>);
        static_assert(std::numeric_limits<typename X::difference_type>::max() <= std::numeric_limits<typename X::size_type>::max());

        static_assert(std::is_same_v<typename X::value_type, int>);

        // we use a proxy reference for xstd::bit_set which convertible to int const&, instead of the same as int const&
        static_assert(std::is_convertible_v<typename X::const_reference, int const&>);

                                                                                // [tab:container.rev.req]
        static_assert(std::is_same_v<typename X::      reverse_iterator, std::reverse_iterator<typename X::      iterator>>);
        static_assert(std::is_same_v<typename X::const_reverse_iterator, std::reverse_iterator<typename X::const_iterator>>);

                                                                                // [associative.reqmts]/6
        static_assert(std::is_same_v<typename std::iterator_traits<typename X::iterator>::iterator_category, std::bidirectional_iterator_tag>);
};

template<class X>
struct constructor
{
        auto operator()() const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_default_constructible_v<typename X::key_compare>);
                X u;
                BOOST_CHECK(u.empty());
                BOOST_CHECK(X().empty());
        }

        auto operator()(auto i, auto j) const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_default_constructible_v<typename X::key_compare>);
                X u(i, j);
                X u1;
                u1.insert(i, j);
                BOOST_CHECK(u == u1);
        }

        auto operator()(std::initializer_list<typename X::value_type> il) const
        {                                                                       // [tab:container.assoc.req]
                X u(il);
                X u1(il.begin(), il.end());
                BOOST_CHECK(u == u1);
        }
};

struct op_assign
{
        template<class X>
        auto operator()(X& a, std::initializer_list<typename X::value_type> il) const
        {                                                                       // [tab:container.assoc.req]
                a = il;
                X a1(il);
                BOOST_CHECK(a == a1);
        }
};

struct mem_const_reference
{
        auto operator()(auto const& bs) const noexcept
        {
                for (auto const ref : bs) {
                        BOOST_CHECK(bs.count(ref));
                }
        }
};

struct mem_const_iterator
{
        template<class X>
        auto operator()(X& a) const noexcept
        {                                                                       // [tab:container.req]
                static_assert(std::is_same_v<decltype(a.begin()), typename X::iterator>);
                static_assert(std::is_same_v<decltype(a.end())  , typename X::iterator>);

                using Iterator = typename X::iterator;                          // [iterator.iterators] Table 94
                using Reference = typename std::iterator_traits<Iterator>::reference;
                using ValueType = typename std::iterator_traits<Iterator>::value_type;

                static_assert(tti::is_dereferenceable<Iterator>);
                static_assert(std::is_same_v<decltype(++std::declval<Iterator>()), Iterator&>);

                using InputIterator = Iterator;                                // [input.iterators] Table 95
                static_assert(tti::is_equality_comparable<InputIterator>);
                {
                        auto r = a.begin();
                        auto s = a.end();
                        BOOST_CHECK_EQUAL(r != s, !(r == s));
                }
                static_assert(std::is_same_v       <decltype(*std::declval<InputIterator>()), Reference>);
                static_assert(std::is_convertible_v<decltype(*std::declval<InputIterator>()), ValueType>);
                {
                        if (a.begin() != a.end()) {
                                auto r = a.begin();
                                auto s = a.begin();
                                r++;
                                ++s;
                                BOOST_CHECK(r == s);
                        }
                }
                static_assert(std::is_convertible_v<decltype(*std::declval<InputIterator>()++), ValueType>);
                {
                        if (a.begin() != a.end()) {
                                auto r = a.begin();
                                auto s = a.begin();
                                auto tmp = *s;
                                ++s;
                                BOOST_CHECK_EQUAL(*r++, tmp);
                                BOOST_CHECK(r == s);
                        }
                }

                using ForwardIterator = InputIterator;                          // [forward.iterators] Table 97
                static_assert(std::is_convertible_v<decltype( std::declval<ForwardIterator>()++), ForwardIterator const&>);
                static_assert(std::is_same_v       <decltype(*std::declval<ForwardIterator>()++), Reference>);

                using BidirectionalIterator = ForwardIterator;                  // [bidirectional.iterators] Table 98
                static_assert(std::is_same_v       <decltype(--std::declval<BidirectionalIterator>()  ), BidirectionalIterator&>);
                static_assert(std::is_convertible_v<decltype(  std::declval<BidirectionalIterator>()--), BidirectionalIterator const&>);
                static_assert(std::is_same_v       <decltype( *std::declval<BidirectionalIterator>()--), Reference>);
                {
                        if (a.begin() != a.end()) {
                                auto r = a.end();
                                auto s = a.end();
                                auto tmp = s;
                                --s;
                                BOOST_CHECK(r-- == tmp);
                                BOOST_CHECK(r == s);
                        }
                }

                                                                                // [tab:container.rev.req]
                static_assert(std::is_same_v<decltype(a.rbegin()), typename X::reverse_iterator>);
                static_assert(std::is_same_v<decltype(a.rend())  , typename X::reverse_iterator>);
        }

        template<class X>
        auto operator()(X const& a) const noexcept
        {                                                                       // [tab:container.req]
                static_assert(std::is_same_v<decltype(a.begin()) , typename X::const_iterator>);
                static_assert(std::is_same_v<decltype(a.end())   , typename X::const_iterator>);
                static_assert(std::is_same_v<decltype(a.cbegin()), typename X::const_iterator>);
                static_assert(std::is_same_v<decltype(a.cend())  , typename X::const_iterator>);
                BOOST_CHECK(a.cbegin() == const_cast<X const&>(a).begin());
                BOOST_CHECK(a.cend()   == const_cast<X const&>(a).end()  );

                                                                                // [tab:container.rev.req]
                static_assert(std::is_same_v<decltype(a.rbegin()) , typename X::const_reverse_iterator>);
                static_assert(std::is_same_v<decltype(a.rend())   , typename X::const_reverse_iterator>);
                static_assert(std::is_same_v<decltype(a.crbegin()), typename X::const_reverse_iterator>);
                static_assert(std::is_same_v<decltype(a.crend())  , typename X::const_reverse_iterator>);
                BOOST_CHECK(a.crbegin() == const_cast<X const&>(a).rbegin());
                BOOST_CHECK(a.crend()   == const_cast<X const&>(a).rend()  );
        }
};

struct mem_front
{
        template<class IntSet>
        auto operator()(IntSet const& bs [[maybe_unused]]) const noexcept
        {
                if constexpr (tti::has_front<IntSet>) {
                        BOOST_CHECK(bs.empty() || (bs.front() == *bs.cbegin()));
                        BOOST_CHECK(bs.empty() || (&bs.front() == bs.cbegin()));
                }
        }
};

struct mem_back
{
        template<class IntSet>
        auto operator()(IntSet const& bs [[maybe_unused]]) const noexcept
        {
                if constexpr (tti::has_back<IntSet>) {
                        BOOST_CHECK(bs.empty() || (bs.back() == *bs.crbegin()));
                        BOOST_CHECK(bs.empty() || (&bs.back() == std::next(bs.crbegin()).base()));
                }
        }
};

struct mem_empty
{
        auto operator()(auto const& a) const noexcept
        {                                                                       // [tab:container.req]
                static_assert(std::is_convertible_v<decltype(a.empty()), bool>);
                BOOST_CHECK_EQUAL(a.empty(), a.begin() == a.end());
        }
};

struct mem_size
{
        template<class X>
        auto operator()(X const& a) const noexcept
        {                                                                       // [tab:container.req]
                static_assert(std::is_same_v<decltype(a.size()), typename X::size_type>);
                BOOST_CHECK_EQUAL(static_cast<std::ptrdiff_t>(a.size()), std::distance(a.begin(), a.end()));
        }
};

struct mem_max_size
{
        template<class X>
        auto operator()(X const& a) const noexcept
        {                                                                       // [tab:container.req]
                static_assert(std::is_same_v<decltype(a.max_size()), typename X::size_type>);
                BOOST_CHECK_EQUAL(a.max_size(), X().max_size());
                BOOST_CHECK_LE(a.size(), a.max_size());
        }
};

struct mem_capacity
{
        template<class X>
        auto operator()(X const& a) const noexcept
        {
                if constexpr (tti::has_capacity<X>) {                         // [tab:container.req]
                        static_assert(std::is_same_v<decltype(a.capacity()), typename X::size_type>);
                        BOOST_CHECK_LE(a.size(), a.capacity());
                }
        }
};

struct mem_emplace
{
        template<class X, class... Args>
        auto operator()(X& a, Args&&... args) const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_same_v<decltype(a.emplace(std::forward<Args>(args)...)), std::pair<typename X::iterator, bool>>);
                auto r = a.emplace(std::forward<Args>(args)...);
                BOOST_CHECK(r == std::make_pair(a.find(typename X::value_type(std::forward<Args>(args)...)), true));
        }
};

struct mem_emplace_hint
{
        template<class X, class... Args>
        auto operator()(X& a, typename X::iterator p, Args&&... args) const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_same_v<decltype(a.emplace_hint(p, std::forward<Args>(args)...)), typename X::iterator>);
                auto r = a.emplace_hint(p, std::forward<Args>(args)...);
                BOOST_CHECK(r == a.find(typename X::value_type(std::forward<Args>(args)...)));
        }
};

struct mem_insert
{
        template<class X>
        auto operator()(X& a, typename X::value_type const& t) const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_same_v<decltype(a.insert(t)), std::pair<typename X::iterator, bool>>);
                auto r = a.insert(t);
                BOOST_CHECK(r == std::make_pair(a.find(t), true));
        }

        template<class X>
        auto operator()(X& a, typename X::iterator p, typename X::value_type const& t) const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_same_v<decltype(a.insert(p, t)), typename X::iterator>);
                auto r = a.insert(p, t);
                BOOST_CHECK(r == a.find(t));
        }

        template<class X, class InputIterator>
        auto operator()(X& a, InputIterator i, InputIterator j) const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_convertible_v<typename std::iterator_traits<InputIterator>::value_type, typename X::value_type>);
                static_assert(std::is_same_v<decltype(a.insert(i, j)), void>);
                auto a1 = a;
                a.insert(i, j);
                std::for_each(i, j, [&](auto const& t) {
                        a1.insert(t);
                });
                BOOST_CHECK(a == a1);
        }

        template<class X>
        auto operator()(X& a, std::initializer_list<typename X::value_type> il) const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_same_v<decltype(a.insert(il)), void>);
                auto a1 = a;
                a.insert(il);
                a1.insert(il.begin(), il.end());
                BOOST_CHECK(a == a1);
        }
};

struct mem_erase
{
        template<class X>
        auto operator()(X& a, typename X::key_type const& k) const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_same_v<decltype(a.erase(k)), typename X::size_type>);
                auto const expected = a.count(k);
                auto const returns = a.erase(k);
                BOOST_CHECK(returns == expected);
        }

        template<class X>
        auto operator()(X& a, typename X::const_iterator q) const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_same_v<decltype(a.erase(q)), typename X::iterator>);
                BOOST_CHECK(q != a.end());
                auto const expected = std::next(q);                             // assumes erase does not invalidate iterators
                auto const returns = a.erase(q);
                BOOST_CHECK(returns == expected);
        }

        template<class X>
        auto operator()(X& a, typename X::const_iterator q1, typename X::const_iterator q2) const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_same_v<decltype(a.erase(q1, q2)), typename X::iterator>);
                auto const expected = q2;                                       // assumes erase does not invalidate iterators
                auto const returns = a.erase(q1, q2);
                BOOST_CHECK(returns == expected);
        }
};

struct mem_swap
{
        auto operator()(auto& a, auto& b) const noexcept
        {                                                                       // [tab:container.req]
                auto a1 = a, b1 = b;
                static_assert(std::is_same_v<decltype(a.swap(b)), void>);
                a1.swap(b1);
                BOOST_CHECK(a1 == b);
                BOOST_CHECK(b1 == a);
        }
};

struct mem_clear
{
        auto operator()(auto& a) const noexcept
        {                                                                       // [tab:container.assoc.req]
                auto a1 = a;
                //static_assert(std::is_same_v<decltype(a.clear()), void>);
                a.clear();
                a1.erase(a1.begin(), a1.end());
                BOOST_CHECK(a == a1);
                BOOST_CHECK(a.empty());
        }
};

struct mem_find
{
        template<class X>
        auto operator()(X& b, typename X::key_type const& k) const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_same_v<decltype(b.find(k)), typename X::iterator>);
                BOOST_CHECK(b.find(k) == std::ranges::find(b, k));
        }

        template<class X>
        auto operator()(X const& b, typename X::key_type const& k) const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_same_v<decltype(b.find(k)), typename X::const_iterator>);
                BOOST_CHECK(b.find(k) == std::ranges::find(b, k));
        }
};

struct mem_count
{
        template<class X>
        auto operator()(X const& b, typename X::key_type const& k) const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_same_v<decltype(b.count(k)), typename X::size_type>);
                BOOST_CHECK_EQUAL(static_cast<std::ptrdiff_t>(b.count(k)), std::ranges::count(b, k));
        }
};

struct mem_contains
{
        template<class X>
        auto operator()(X const& b, typename X::key_type const& k) const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_same_v<decltype(b.contains(k)), bool>);
                BOOST_CHECK_EQUAL(b.contains(k), b.find(k) != b.end());
        }
};

struct mem_lower_bound
{
        template<class X>
        auto operator()(X& b, typename X::key_type const& k) const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_same_v<decltype(b.lower_bound(k)), typename X::iterator>);
                BOOST_CHECK(b.lower_bound(k) == std::ranges::lower_bound(b, k));
        }

        template<class X>
        auto operator()(X const& b, typename X::key_type const& k) const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_same_v<decltype(b.lower_bound(k)), typename X::const_iterator>);
                BOOST_CHECK(b.lower_bound(k) == std::ranges::lower_bound(b, k));
        }
};

struct mem_upper_bound
{
        template<class X>
        auto operator()(X& b, typename X::key_type const& k) const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_same_v<decltype(b.upper_bound(k)), typename X::iterator>);
                BOOST_CHECK(b.upper_bound(k) == std::ranges::upper_bound(b, k));
        }

        template<class X>
        auto operator()(X const& b, typename X::key_type const& k) const
        {                                                                       // [tab:container.assoc.req]
                static_assert(std::is_same_v<decltype(b.upper_bound(k)), typename X::const_iterator>);
                BOOST_CHECK(b.upper_bound(k) == std::ranges::upper_bound(b, k));
        }
};

struct mem_equal_range
{
        template<class X>
        auto operator()(X& b, typename X::key_type const& k) const
        {                                                                       // [tab:container.assoc.req]
                using iterator = typename X::iterator;
                static_assert(std::is_same_v<decltype(b.equal_range(k)), std::pair<iterator, iterator>>);
                BOOST_CHECK(b.equal_range(k) == std::make_pair(b.lower_bound(k), b.upper_bound(k)));
                BOOST_CHECK(b.equal_range(k) == std::equal_range(b.begin(), b.end(), k));
        }

        template<class X>
        auto operator()(X const& b, typename X::key_type const& k) const
        {                                                                       // [tab:container.assoc.req]
                using const_iterator = typename X::const_iterator;
                static_assert(std::is_same_v<decltype(b.equal_range(k)), std::pair<const_iterator, const_iterator>>);
                BOOST_CHECK(b.equal_range(k) == std::make_pair(b.lower_bound(k), b.upper_bound(k)));
                BOOST_CHECK(b.equal_range(k) == std::equal_range(b.begin(), b.end(), k));
        }
};

struct op_equal_to
{
        auto operator()(auto const& a) const noexcept
        {                                                                       // [tab:container.req]
                BOOST_CHECK(a == a);                                            // reflexive
        }

        auto operator()(auto const& a, auto const& b) const noexcept
        {                                                                       // [tab:container.req]
                static_assert(std::is_convertible_v<decltype(a == b), bool>);
                BOOST_CHECK_EQUAL(a == b, std::ranges::equal(a, b));
                BOOST_CHECK(!(a == b) || b == a);                               // symmetric
        }

        auto operator()(auto const& a, auto const& b, auto const& c) const noexcept
        {                                                                       // [tab:container.req]
                BOOST_CHECK(!(a == b && b == c) || a == c);                     // transitive
        }
};

struct op_not_equal_to
{
        auto operator()(auto const& a, auto const& b) const noexcept
        {                                                                       // [tab:container.req]
                static_assert(std::is_convertible_v<decltype(a != b), bool>);
                BOOST_CHECK_EQUAL(a != b, !(a == b));
        }
};

struct op_compare_three_way
{
        auto operator()(auto const& a) const noexcept
        {
                // std::strong_ordering does not have output streaming operator<< required for BOOST_CHECK_EQUAL
                BOOST_CHECK((a <=> a) == std::strong_ordering::equal);          // reflexive
        }

        auto operator()(auto const& a, auto const& b) const noexcept
        {                                                                       // [tab:container.req]
                static_assert(std::is_same_v<decltype(a <=> b), std::strong_ordering>);
                                                                                // [tab:container.opt]
                // std::strong_ordering does not have output streaming operator<< required for BOOST_CHECK_EQUAL
                BOOST_CHECK((a <=> b) == std::lexicographical_compare_three_way(a.begin(), a.end(), b.begin(), b.end()));
        }
};

struct op_less
{
        auto operator()(auto const& a) const noexcept
        {                                                                       // [tab:container.opt]
                BOOST_CHECK(!(a < a));                                          // irreflexive
        }

        auto operator()(auto const& a, auto const& b) const noexcept
        {                                                                       // [tab:container.opt]
                static_assert(std::is_convertible_v<decltype(a < b), bool>);
                BOOST_CHECK_EQUAL(a < b, (a <=> b) < 0);
                BOOST_CHECK_EQUAL(a < b, std::ranges::lexicographical_compare(a, b));
                BOOST_CHECK(!(a < b) || !(b < a));                              // asymmetric
        }

        auto operator()(auto const& a, auto const& b, auto const& c) const noexcept
        {                                                                       // [tab:container.opt]
                BOOST_CHECK(!(a < b && b < c) || a < c);                        // transitive
        }
};

struct op_greater
{
        auto operator()(auto const& a, auto const& b) const noexcept
        {                                                                       // [tab:container.opt]
                static_assert(std::is_convertible_v<decltype(a > b), bool>);
                BOOST_CHECK_EQUAL(a > b, (a <=> b) > 0);
                BOOST_CHECK_EQUAL(a > b, b < a);
        }
};

struct op_less_equal
{
        auto operator()(auto const& a, auto const& b) const noexcept
        {                                                                       // [tab:container.opt]
                static_assert(std::is_convertible_v<decltype(a <= b), bool>);
                BOOST_CHECK_EQUAL(a <= b, (a <=> b) <= 0);
                BOOST_CHECK_EQUAL(a <= b, !(b < a));
        }
};

struct op_greater_equal
{
        auto operator()(auto const& a, auto const& b) const noexcept
        {                                                                       // [tab:container.opt]
                static_assert(std::is_convertible_v<decltype(a >= b), bool>);
                BOOST_CHECK_EQUAL(a >= b, (a <=> b) >= 0);
                BOOST_CHECK_EQUAL(a >= b, !(a < b));
        }
};

struct fn_swap
{
        auto operator()(auto& a, auto& b) const noexcept
        {                                                                       // [tab:container.req]
                auto a1 = a, b1 = b;
                auto a2 = a, b2 = b;
                static_assert(std::is_same_v<decltype(swap(a, b)), void>);
                swap(a1, b1); a2.swap(b2);
                BOOST_CHECK(a1 == a2); BOOST_CHECK(b1 == b2);                   // [set.special]/1
        }
};

struct fn_iterator
{
        auto operator()(auto& c) const noexcept
        {
                BOOST_CHECK( begin(c) == c.begin());                            // [iterator.range]/2
                BOOST_CHECK(   end(c) == c.end());                              // [iterator.range]/3
                BOOST_CHECK(rbegin(c) == c.rbegin());                           // [iterator.range]/8
                BOOST_CHECK(  rend(c) == c.rend());                             // [iterator.range]/9
        }

        auto operator()(auto const& c) const noexcept
        {
                BOOST_CHECK(  begin(c) == c.begin());                           // [iterator.range]/2
                BOOST_CHECK(    end(c) == c.end());                             // [iterator.range]/3
                BOOST_CHECK( cbegin(c) == c.cbegin());                          // [iterator.range]/6
                BOOST_CHECK(   cend(c) == c.cend());                            // [iterator.range]/7
                BOOST_CHECK( rbegin(c) == c.rbegin());                          // [iterator.range]/8
                BOOST_CHECK(   rend(c) == c.rend());                            // [iterator.range]/9
                BOOST_CHECK(crbegin(c) == c.crbegin());                         // [iterator.range]/14
                BOOST_CHECK(  crend(c) == c.crend());                           // [iterator.range]/15
        }
};

struct fn_size
{
        auto operator()(auto const& c) const noexcept
        {
                BOOST_CHECK_EQUAL(size(c), c.size());                           // [iterator.range]/16
        }
};

struct fn_ssize
{
        auto operator()(auto const& c) const noexcept
        {
                using R = std::common_type_t<std::ptrdiff_t, std::make_signed_t<decltype(c.size())>>;
                BOOST_CHECK_EQUAL(ssize(c), static_cast<R>(c.size()));          // [iterator.range]/18
        }
};

struct fn_empty
{
        auto operator()(auto const& c) const noexcept
        {
                BOOST_CHECK_EQUAL(empty(c), c.empty());                         // [iterator.range]/20
        }
};

}       // namespace xstd

ext: hpp


text:
#include <blitzcommon/PlatformUtils.h>

namespace blitz
{
    namespace fs
    {

        void seperateFileNameAndPath(const char* filePath, FilePath* path)
        {
            assert(path != nullptr);

            size_t lastSeparatorIndex = lastIndexOf(filePath, SEPARATOR);
            if (lastSeparatorIndex == -1)
            {
                path->fileNameLength = strlen(filePath);
                path->fileName = (char*)malloc(path->fileNameLength + 1);
                strncpy(path->fileName, filePath, path->fileNameLength);

                path->directoryPathLength = 0;
                path->directoryPath = (char*)malloc(1);
                path->directoryPath[0] = '\0';
                return;
            }

            size_t filePathLength = strlen(filePath);
            path->fileNameLength = filePathLength - lastSeparatorIndex - 1;
            path->fileName = (char*)malloc(path->fileNameLength + 1);
            path->fileName[path->fileNameLength] = '\0';
            strncpy(path->fileName, filePath + lastSeparatorIndex + 1, path->fileNameLength);

            path->directoryPathLength = lastSeparatorIndex + 1;
            path->directoryPath = (char*)malloc(path->directoryPathLength + 1);
            path->directoryPath[path->directoryPathLength] = '\0';
            strncpy(path->directoryPath, filePath, path->directoryPathLength);
        }

    } // namespace fs

} // namespace blitz

ext: cpp


text:
#include <cstdlib>
#include <iostream>
#include "resolver.pb.h"
#include <b0/node.h>
#include <b0/resolver/client.h>
#include <b0/protobuf/subscriber.h>
#include <b0/graph/graphviz.h>
#include <b0/config.h>
#include <b0/utils/env.h>
#ifdef HAVE_BOOST_PROCESS
#include <boost/process.hpp>
#endif

namespace b0
{

namespace graph
{

class Console : public b0::Node
{
public:
    Console()
        : Node("graph_console"),
          resolv_cli_(this),
          sub_(this, "graph", &Console::onGraphChanged)
    {
    }

    ~Console()
    {
    }

    void init()
    {
        Node::init();

        if(false)
        {
            /*
             * requesting the initial graph is not really needed, as the
             * initialization of this node will cause a change in the graph
             *
             * FIXME: it would be anyway better to have a timeout, and if a graph
             *        is not received in the first 2 seconds, send an explicit request
             */
            log(info, "Requesting graph");
            b0::resolver_msgs::Graph graph;
            resolv_cli_.getGraph(graph);
            printOrDisplayGraph("Current graph", graph);
        }
    }

    void onGraphChanged(const b0::resolver_msgs::Graph &graph)
    {
        printOrDisplayGraph("Graph has changed", graph);
    }

    void printOrDisplayGraph(std::string message, const b0::resolver_msgs::Graph &graph)
    {
        if(termHasImageCapability())
        {
            log(info, message);
            renderAndDisplayGraph(graph);
        }
        else
        {
            log(info, "%s: %s", message, graph.DebugString());
        }
    }

    void renderAndDisplayGraph(const b0::resolver_msgs::Graph &graph)
    {
        toGraphviz(graph, "graph.gv", "white", "cyan", "red");

        if(renderGraphviz("graph.gv", "graph.png") == 0)
        {
            displayInlineImage("graph.png");
        }
        else
        {
            std::cerr << "failed to execute neato" << std::endl;
            return;
        }
    }

    bool termHasImageCapability()
    {
        std::string TERM_PROGRAM = b0::env::get("TERM_PROGRAM");
        return TERM_PROGRAM == "iTerm.app";
    }

    int displayInlineImage(std::string filename)
    {
#ifdef HAVE_BOOST_PROCESS
        boost::process::child c(boost::process::search_path("imgcat"), "graph.png");
        c.wait();
        return c.exit_code();
#else
        std::cerr << "boost/process.hpp is needed for inline image display" << std::endl;
        return 1;
#endif
    }

protected:
    b0::resolver::Client resolv_cli_;
    b0::protobuf::Subscriber<b0::resolver_msgs::Graph> sub_;
};

} // namespace graph

} // namespace b0

int main()
{
    b0::graph::Console console;
    console.init();
    console.spin();
    return 0;
}

max_stars_repo_name: DanialLiu/bluezero

max_issues_repo_name: DanialLiu/bluezero

max_forks_repo_name: DanialLiu/bluezero


text:
/*=============================================================================
    Copyright (c) 2010 Daniel James

    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)
=============================================================================*/

#if !defined(BOOST_SPIRIT_QUICKBOOK_ITERATOR_HPP)
#define BOOST_SPIRIT_QUICKBOOK_ITERATOR_HPP

#include <iterator>
#include <boost/iterator/iterator_traits.hpp>
#include <boost/operators.hpp>
#include <boost/range/iterator_range.hpp>
#include "string_view.hpp"

namespace quickbook
{
    template <typename Iterator>
    struct lookback_iterator
        : boost::forward_iterator_helper<
              lookback_iterator<Iterator>,
              typename boost::iterator_value<Iterator>::type,
              typename boost::iterator_difference<Iterator>::type,
              typename boost::iterator_pointer<Iterator>::type,
              typename boost::iterator_reference<Iterator>::type>
    {
        lookback_iterator() {}
        explicit lookback_iterator(Iterator i) : original_(i), base_(i) {}

        friend bool operator==(
            lookback_iterator const& x, lookback_iterator const& y)
        {
            return x.base_ == y.base_;
        }

        lookback_iterator& operator++()
        {
            ++base_;
            return *this;
        }

        typename boost::iterator_reference<Iterator>::type operator*() const
        {
            return *base_;
        }

        Iterator base() const { return base_; }

        typedef boost::iterator_range<std::reverse_iterator<Iterator> >
            lookback_range;

        lookback_range lookback() const
        {
            return lookback_range(base_, original_);
        }

      private:
        Iterator original_;
        Iterator base_;
    };

    typedef lookback_iterator<string_iterator> parse_iterator;
}

#endif

max_stars_repo_name: cpp-pm/boost

max_issues_repo_name: cpp-pm/boost

max_forks_repo_name: cpp-pm/boost


text:
#ifdef BOOSTPOSIXREGEX
#include <boost/regex.hpp>
using boost::wregex;
using boost::wsregex_iterator;
#else
#include <regex>
using std::wregex;
using std::wsregex_iterator;
#endif

int main(int argc, char *argv[]) {
    wregex rg(L"test");
    const wsregex_iterator end;
}

ext: cxx

max_stars_repo_name: wlxiong/lispe

max_issues_repo_name: wlxiong/lispe

max_forks_repo_name: wlxiong/lispe


text:
#include <nano/lib/config.hpp>
#include <nano/lib/jsonconfig.hpp>
#include <nano/lib/tomlconfig.hpp>
#include <nano/node/node_rpc_config.hpp>

#include <boost/property_tree/ptree.hpp>

nano::node_rpc_config::node_rpc_config ()
{
	rsnano::NodeRpcConfigDto dto;
	if (rsnano::rsn_node_rpc_config_create (&dto) < 0)
		throw std::runtime_error ("could not create node rpc config");
	load_dto (dto);
}

void nano::node_rpc_config::load_dto (rsnano::NodeRpcConfigDto & dto)
{
	enable_sign_hash = dto.enable_sign_hash;
	child_process.enable = dto.enable_child_process;
	child_process.rpc_path = std::string (reinterpret_cast<const char *> (dto.rpc_path), dto.rpc_path_length);
}

rsnano::NodeRpcConfigDto nano::node_rpc_config::to_dto () const
{
	rsnano::NodeRpcConfigDto dto;
	dto.enable_sign_hash = enable_sign_hash;
	dto.enable_child_process = child_process.enable;
	std::copy (child_process.rpc_path.begin (), child_process.rpc_path.end (), std::begin (dto.rpc_path));
	dto.rpc_path_length = child_process.rpc_path.size ();
	return dto;
}

nano::error nano::node_rpc_config::serialize_json (nano::jsonconfig & json) const
{
	json.put ("version", json_version ());
	json.put ("enable_sign_hash", enable_sign_hash);

	nano::jsonconfig child_process_l;
	child_process_l.put ("enable", child_process.enable);
	child_process_l.put ("rpc_path", child_process.rpc_path);
	json.put_child ("child_process", child_process_l);
	return json.get_error ();
}

nano::error nano::node_rpc_config::deserialize_toml (nano::tomlconfig & toml)
{
	toml.get_optional ("enable_sign_hash", enable_sign_hash);
	toml.get_optional<bool> ("enable_sign_hash", enable_sign_hash);

	auto child_process_l (toml.get_optional_child ("child_process"));
	if (child_process_l)
	{
		child_process_l->get_optional<bool> ("enable", child_process.enable);
		child_process_l->get_optional<std::string> ("rpc_path", child_process.rpc_path);
	}

	return toml.get_error ();
}

nano::error nano::node_rpc_config::deserialize_json (bool & upgraded_a, nano::jsonconfig & json, boost::filesystem::path const & data_path)
{
	json.get_optional<bool> ("enable_sign_hash", enable_sign_hash);

	auto child_process_l (json.get_optional_child ("child_process"));
	if (child_process_l)
	{
		child_process_l->get_optional<bool> ("enable", child_process.enable);
		child_process_l->get_optional<std::string> ("rpc_path", child_process.rpc_path);
	}

	return json.get_error ();
}

void nano::node_rpc_config::set_request_callback (std::function<void (boost::property_tree::ptree const &)> callback_a)
{
	request_callback = std::move (callback_a);
}

max_stars_repo_name: simpago/nano-node

max_issues_repo_name: simpago/nano-node

max_forks_repo_name: simpago/nano-node


text:
#ifndef STAN_MATH_PRIM_MAT_PROB_WISHART_LOG_HPP
#define STAN_MATH_PRIM_MAT_PROB_WISHART_LOG_HPP

#include <stan/math/prim/meta.hpp>
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/prim/mat/prob/wishart_lpdf.hpp>
#include <boost/math/tools/promotion.hpp>

namespace stan {
namespace math {

/**
 * The log of the Wishart density for the given W, degrees of freedom,
 * and scale matrix.
 *
 * The scale matrix, S, must be k x k, symmetric, and semi-positive definite.
 * Dimension, k, is implicit.
 * nu must be greater than k-1
 *
 * \f{eqnarray*}{
 W &\sim& \mbox{\sf{Wishart}}_{\nu} (S) \\
 \log (p (W \, |\, \nu, S) ) &=& \log \left( \left(2^{\nu k/2} \pi^{k (k-1) /4}
 \prod_{i=1}^k{\Gamma (\frac{\nu + 1 - i}{2})} \right)^{-1} \times \left| S
 \right|^{-\nu/2} \left| W \right|^{(\nu - k - 1) / 2}
 \times \exp (-\frac{1}{2} \mbox{tr} (S^{-1} W)) \right) \\
 &=& -\frac{\nu k}{2}\log(2) - \frac{k (k-1)}{4} \log(\pi) - \sum_{i=1}^{k}{\log
 (\Gamma (\frac{\nu+1-i}{2}))}
 -\frac{\nu}{2} \log(\det(S)) + \frac{\nu-k-1}{2}\log (\det(W)) - \frac{1}{2}
 \mbox{tr} (S^{-1}W) \f}
 *
 * @deprecated use wishart_lpdf
 *
 * @param W A scalar matrix
 * @param nu Degrees of freedom
 * @param S The scale matrix
 * @return The log of the Wishart density at W given nu and S.
 * @throw std::domain_error if nu is not greater than k-1
 * @throw std::domain_error if S is not square, not symmetric, or not
 semi-positive definite.
 * @tparam T_y Type of scalar.
 * @tparam T_dof Type of degrees of freedom.
 * @tparam T_scale Type of scale.
 */
template <bool propto, typename T_y, typename T_dof, typename T_scale>
typename boost::math::tools::promote_args<T_y, T_dof, T_scale>::type
wishart_log(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& W,
            const T_dof& nu,
            const Eigen::Matrix<T_scale, Eigen::Dynamic, Eigen::Dynamic>& S) {
  return wishart_lpdf<propto, T_y, T_dof, T_scale>(W, nu, S);
}

/**
 * @deprecated use wishart_lpdf
 */
template <typename T_y, typename T_dof, typename T_scale>
inline typename boost::math::tools::promote_args<T_y, T_dof, T_scale>::type
wishart_log(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& W,
            const T_dof& nu,
            const Eigen::Matrix<T_scale, Eigen::Dynamic, Eigen::Dynamic>& S) {
  return wishart_lpdf<T_y, T_dof, T_scale>(W, nu, S);
}

}  // namespace math
}  // namespace stan
#endif

max_stars_repo_name: riddell-stan/math

max_forks_repo_name: riddell-stan/math


text:
// Copyright 2012 Cloudera Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "exec/hdfs-table-sink.h"
#include "exec/hdfs-text-table-writer.h"
#include "exec/hdfs-trevni-table-writer.h"
#include "exec/exec-node.h"
#include "gen-cpp/ImpalaInternalService_constants.h"
#include "util/hdfs-util.h"
#include "exprs/expr.h"
#include "runtime/hdfs-fs-cache.h"
#include "runtime/raw-value.h"
#include "runtime/row-batch.h"
#include "runtime/runtime-state.h"
#include "runtime/string-value.inline.h"

#include <vector>
#include <sstream>
#include <hdfs.h>
#include <boost/scoped_ptr.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <stdlib.h>

#include "gen-cpp/Data_types.h"

using namespace std;
using namespace boost::posix_time;

namespace impala {

HdfsTableSink::HdfsTableSink(const RowDescriptor& row_desc,
    const TUniqueId& unique_id, const vector<TExpr>& select_list_texprs,
    const TDataSink& tsink)
    :  row_desc_(row_desc),
       table_id_(tsink.table_sink.target_table_id),
       select_list_texprs_(select_list_texprs),
       partition_key_texprs_(tsink.table_sink.hdfs_table_sink.partition_key_exprs),
       overwrite_(tsink.table_sink.hdfs_table_sink.overwrite) {
  DCHECK(tsink.__isset.table_sink);
  stringstream unique_id_ss;
  unique_id_ss << unique_id.hi << "-" << unique_id.lo;
  unique_id_str_ = unique_id_ss.str();
}

Status HdfsTableSink::PrepareExprs(RuntimeState* state) {
  // Prepare select list expressions.
  // Disable codegen for these - they would be unused anyway.
  // TODO: codegen table sink
  RETURN_IF_ERROR(Expr::Prepare(output_exprs_, state, row_desc_, true));
  RETURN_IF_ERROR(Expr::Prepare(partition_key_exprs_, state, row_desc_, true));

  // Prepare partition key exprs and gather dynamic partition key exprs.
  for (size_t i = 0; i < partition_key_exprs_.size(); ++i) {
    // Remember non-constant partition key exprs for building hash table of Hdfs files.
    if (!partition_key_exprs_[i]->IsConstant()) {
      dynamic_partition_key_exprs_.push_back(partition_key_exprs_[i]);
    }
  }
  // Sanity check.
  DCHECK_EQ(partition_key_exprs_.size(), table_desc_->partition_key_names().size());

  return Status::OK;
}

Status HdfsTableSink::Init(RuntimeState* state) {
  // TODO: Consider a system-wide random number generator, initialised in a single place.
  ptime now = microsec_clock::local_time();
  long seed = (now.time_of_day().seconds() * 1000)
    + (now.time_of_day().total_microseconds() / 1000);
  VLOG_QUERY << "Random seed: " << seed;
  srand(seed);

  RETURN_IF_ERROR(Expr::CreateExprTrees(state->obj_pool(),
      partition_key_texprs_, &partition_key_exprs_));
  RETURN_IF_ERROR(Expr::CreateExprTrees(state->obj_pool(), select_list_texprs_,
      &output_exprs_));

  // Resolve table id and set input tuple descriptor.
  table_desc_ = static_cast<const HdfsTableDescriptor*>(
      state->desc_tbl().GetTableDescriptor(table_id_));

  if (table_desc_ == NULL) {
    stringstream error_msg("Failed to get table descriptor for table id: ");
    error_msg << table_id_;
    return Status(error_msg.str());
  }

  PrepareExprs(state);

  // Get file format for default partition in table descriptor, and
  // build a map from partition key values to partition descriptor for
  // multiple output format support. The map is keyed on the
  // concatenation of the non-constant keys of the PARTITION clause of
  // the INSERT statement.
  HdfsTableDescriptor::PartitionIdToDescriptorMap::const_iterator it;
  for (it = table_desc_->partition_descriptors().begin();
       it != table_desc_->partition_descriptors().end();
       ++it) {
    if (it->first == g_ImpalaInternalService_constants.DEFAULT_PARTITION_ID) {
      default_partition_ = it->second;
    } else {
      // Evaluate non-constant partition keys and build a map from hash value to
      // partition descriptor
      bool relevant_partition = true;
      HdfsPartitionDescriptor* partition = it->second;
      partition->PrepareExprs(state);
      DCHECK_EQ(partition->partition_key_values().size(), partition_key_exprs_.size());
      vector<Expr*> dynamic_partition_key_values;
      for (size_t i = 0; i < partition_key_exprs_.size(); ++i) {
        // Remember non-constant partition key exprs for building hash table of Hdfs files
        if (!partition_key_exprs_[i]->IsConstant()) {
          dynamic_partition_key_values.push_back(
              partition->partition_key_values()[i]);
        } else {
          // Deal with the following: one partition has (year=2009, month=3); another has
          // (year=2010, month=3).
          // A query like: INSERT INTO TABLE... PARTITION(year=2009) SELECT month FROM...
          // would lead to both partitions having the same key modulo ignored constant
          // partition keys. So only keep a reference to the partition which matches
          // partition_key_values for constant values, since only that will be written to.
          if (!RawValue::Eq(partition->partition_key_values()[i]->GetValue(NULL),
                           partition_key_exprs_[i]->GetValue(NULL),
                           partition_key_exprs_[i]->type())) {
            relevant_partition = false;
            break;
          }
        }
      }
      if (relevant_partition) {
        string key;
        // It's ok if current_row_ is NULL (which it should be here), since all of these
        // expressions are constant, and can therefore be evaluated without a valid row
        // context.
        GetHashTblKey(dynamic_partition_key_values, &key);
        partition_descriptor_map_[key] = partition;
      }
    }
  }

  if (default_partition_ == NULL) {
    return Status("No default partition found for HdfsTextTableSink");
  }

  // Get Hdfs connection from runtime state.
  hdfs_connection_ = state->fs_cache()->GetDefaultConnection();
  if (hdfs_connection_ == NULL) {
    return Status(AppendHdfsErrorMessage("Failed to connect to HDFS."));
  }

  return Status::OK;
}

// Note - this injects a random value into the directory name, so cannot be called
// repeatedly to give the same answer.
static void MakeTmpHdfsDirectoryName(const string& base_dir, const string& unique_id,
                                     stringstream* ss) {
  // Append "_dir" at the end of directory to avoid name clashes for unpartitioned tables.
  (*ss) << base_dir << "/" << unique_id << "_" << rand() << "_dir/";
}

void HdfsTableSink::BuildHdfsFileNames(OutputPartition* output_partition) {
  // Create hdfs_file_name_template and tmp_hdfs_file_name_template.
  // Path: <hdfs_base_dir>/<partition_values>/<unique_id_str>
  stringstream hdfs_file_name_template;
  hdfs_file_name_template << table_desc_->hdfs_base_dir() << "/";

  // Path: <hdfs_base_dir>/<unique_id>_dir/<partition_values>/<unique_id_str>
  // Both the temporary directory and the file name, when moved to the
  // real partition directory must be unique.
  stringstream tmp_hdfs_file_name_template;
  MakeTmpHdfsDirectoryName(table_desc_->hdfs_base_dir(), unique_id_str_,
                           &tmp_hdfs_file_name_template);
  output_partition->tmp_hdfs_dir_name = tmp_hdfs_file_name_template.str();

  stringstream common_suffix;

  for (int j = 0; j < partition_key_exprs_.size(); ++j) {
    common_suffix << table_desc_->partition_key_names()[j] << "=";
    void* value = partition_key_exprs_[j]->GetValue(current_row_);
    // NULL partition keys get a special value to be compatible with Hive.
    if (value == NULL) {
      common_suffix << null_partition_key_value_;
    } else {
      string value_str;
      partition_key_exprs_[j]->PrintValue(value, &value_str);
      common_suffix << value_str;
    }
    common_suffix << "/";
  }

  // common_suffix now holds the unique descriptor for this partition,
  // save it before we build the suffix out further
  output_partition->partition_name = common_suffix.str();

  // Use the query id as filename.
  common_suffix << unique_id_str_ << "_" << rand();
  hdfs_file_name_template << common_suffix.str() << "_data";
  tmp_hdfs_file_name_template << common_suffix.str() << "_data";
  output_partition->hdfs_file_name_template = hdfs_file_name_template.str();
  output_partition->tmp_hdfs_file_name_template =
      tmp_hdfs_file_name_template.str();
  output_partition->num_files = 0;
}

//TODO: Clean up temporary files on error.
Status HdfsTableSink::CreateNewTmpFile(RuntimeState* state,
    OutputPartition* output_partition) {
  stringstream filename;
  filename << output_partition->tmp_hdfs_file_name_template
      << "." << output_partition->num_files;
  output_partition->current_file_name = filename.str();
  // Check if tmp_hdfs_file_name_template exists.
  const char* tmp_hdfs_file_name_template_cstr =
      output_partition->current_file_name.c_str();
  if (hdfsExists(hdfs_connection_, tmp_hdfs_file_name_template_cstr) == 0) {
    return Status(AppendHdfsErrorMessage("Temporary HDFS file already exists: ",
        output_partition->current_file_name));
  }
  output_partition->tmp_hdfs_file = hdfsOpenFile(hdfs_connection_,
      tmp_hdfs_file_name_template_cstr, O_WRONLY, 0, 0,
      output_partition->partition_descriptor->block_size());
  if (output_partition->tmp_hdfs_file == NULL) {
    return Status(AppendHdfsErrorMessage("Failed to open HDFS file for writing: ",
        output_partition->current_file_name));
  }

  // Save the ultimate destination for this file (it will be moved by the coordinator)
  stringstream dest;
  dest << output_partition->hdfs_file_name_template << "." << output_partition->num_files;
  (*state->hdfs_files_to_move())[output_partition->current_file_name] = dest.str();

  ++output_partition->num_files;
  output_partition->num_rows = 0;
  return Status::OK;
}

Status HdfsTableSink::InitOutputPartition(RuntimeState* state,
    const HdfsPartitionDescriptor& partition_descriptor,
    OutputPartition* output_partition) {
  output_partition->hdfs_connection = hdfs_connection_;

  switch (partition_descriptor.file_format()) {
    case THdfsFileFormat::TEXT: {
      output_partition->writer.reset(
          new HdfsTextTableWriter(state, output_partition,
                                  &partition_descriptor, table_desc_, output_exprs_));
      break;
    }
    case THdfsFileFormat::TREVNI: {
      output_partition->writer.reset(
          new HdfsTrevniTableWriter(state, output_partition,
                                    &partition_descriptor, table_desc_, output_exprs_));
      break;
    }
    default:
      stringstream error_msg;
      map<int, const char*>::const_iterator i =
          _THdfsFileFormat_VALUES_TO_NAMES.find(partition_descriptor.file_format());
      const char* str = "Unknown data sink type ";
      if (i != _THdfsFileFormat_VALUES_TO_NAMES.end()) {
        str = i->second;
      }
      error_msg << str << " not implemented.";
      return Status(error_msg.str());
  }
  RETURN_IF_ERROR(output_partition->writer->Init());
  output_partition->partition_descriptor = &partition_descriptor;
  return CreateNewTmpFile(state, output_partition);
}

void HdfsTableSink::GetHashTblKey(const vector<Expr*>& exprs, string* key) {
  stringstream hash_table_key;
  for (int i = 0; i < dynamic_partition_key_exprs_.size(); ++i) {
    RawValue::PrintValueAsBytes(exprs[i]->GetValue(current_row_),
                                exprs[0]->type(), &hash_table_key);
    // Additionally append "/" to avoid accidental key collisions.
    hash_table_key << "/";
  }
  *key = hash_table_key.str();
}

inline Status HdfsTableSink::GetOutputPartition(
   RuntimeState* state, const string& key, PartitionPair** partition_pair) {
  PartitionMap::iterator existing_partition;
  existing_partition = partition_keys_to_output_partitions_.find(key);
  if (existing_partition == partition_keys_to_output_partitions_.end()) {
    // Create a new OutputPartition, and add it to
    // partition_keys_to_output_partitions.
    const HdfsPartitionDescriptor* partition_descriptor = default_partition_;
    PartitionDescriptorMap::const_iterator it = partition_descriptor_map_.find(key);
    if (it != partition_descriptor_map_.end()) {
      partition_descriptor = it->second;
    }

    OutputPartition* partition = state->obj_pool()->Add(new OutputPartition());
    BuildHdfsFileNames(partition);
    RETURN_IF_ERROR(InitOutputPartition(state, *partition_descriptor, partition));

    // Save the partition name so that the coordinator can create partition
    // directory structure if needed
    if (overwrite_) {
      DCHECK(state->num_appended_rows()->find(partition->partition_name) ==
          state->num_appended_rows()->end());
      (*state->num_appended_rows())[partition->partition_name] = 0L;
    }

    // Indicate that temporary directory is to be deleted after execution
    (*state->hdfs_files_to_move())[partition->tmp_hdfs_dir_name] = "";

    partition_keys_to_output_partitions_[key].first = partition;
    *partition_pair = &partition_keys_to_output_partitions_[key];
  } else {
    // Use existing output_partition partition.
    *partition_pair = &existing_partition->second;
  }
  return Status::OK;
}

Status HdfsTableSink::Send(RuntimeState* state, RowBatch* batch) {
  // If there are no partition keys then just pass the whole batch to one partition.
  if (dynamic_partition_key_exprs_.empty()) {
    // If there are no dynamic keys just use an empty key.
    PartitionPair* partition_pair;
    RETURN_IF_ERROR(GetOutputPartition(state, "", &partition_pair));
    // Pass the row batch to the writer. If new_file is returned true then the current
    // file is finalized and a new file is opened.
    // The writer tracks where it is in the batch when it returns with new_file set.
    OutputPartition* output_partition = partition_pair->first;
    bool new_file;
    do {
      RETURN_IF_ERROR(output_partition->writer->AppendRowBatch(
              batch, partition_pair->second, &new_file));
      if (new_file) {
        RETURN_IF_ERROR(FinalizePartitionFile(state, output_partition));
        RETURN_IF_ERROR(CreateNewTmpFile(state, output_partition));
      }
    } while (new_file);
  } else {
    for (int i = 0; i < batch->num_rows(); ++i) {
      current_row_ = batch->GetRow(i);

      string key;
      GetHashTblKey(dynamic_partition_key_exprs_, &key);
      PartitionPair* partition_pair = NULL;
      RETURN_IF_ERROR(GetOutputPartition(state, key, &partition_pair));
      partition_pair->second.push_back(i);
    }
    for (PartitionMap::iterator partition = partition_keys_to_output_partitions_.begin();
         partition != partition_keys_to_output_partitions_.end(); ++partition) {
      OutputPartition* output_partition = partition->second.first;
      if (partition->second.second.empty()) continue;

      bool new_file;
      do {
        RETURN_IF_ERROR(output_partition->writer->AppendRowBatch(
                batch, partition->second.second, &new_file));
        if (new_file) {
          RETURN_IF_ERROR(FinalizePartitionFile(state, output_partition));
          RETURN_IF_ERROR(CreateNewTmpFile(state, output_partition));
        }
      } while (new_file);
      partition->second.second.clear();
    }
  }
  return Status::OK;
}

Status HdfsTableSink::FinalizePartitionFile(RuntimeState* state,
                                            OutputPartition* partition) {
  partition->writer->Finalize();

  // Track total number of appended rows per partition in runtime
  // state. partition->num_rows counts number of rows appended is per-file.
  (*state->num_appended_rows())[partition->partition_name] += partition->num_rows;

  // Close file.
  int hdfs_ret = hdfsCloseFile(hdfs_connection_, partition->tmp_hdfs_file);
  if (hdfs_ret != 0) {
    return Status(AppendHdfsErrorMessage("Failed to close HDFS file: ",
                                         partition->current_file_name));
  }

  return Status::OK;
}

Status HdfsTableSink::Close(RuntimeState* state) {
  // Close Hdfs files, and copy return stats to runtime state.
  for (PartitionMap::iterator cur_partition =
           partition_keys_to_output_partitions_.begin();
       cur_partition != partition_keys_to_output_partitions_.end();
       ++cur_partition) {
    RETURN_IF_ERROR(FinalizePartitionFile(state, cur_partition->second.first));
  }
  return Status::OK;
}

Status HdfsTableSink::GetFileBlockSize(OutputPartition* output_partition, int64_t* size) {
  hdfsFileInfo* info = hdfsGetPathInfo(output_partition->hdfs_connection,
      output_partition->current_file_name.c_str());

  if (info == NULL) {
    stringstream msg;
    msg << "Failed to get info on temporary HDFS file."
        << output_partition->current_file_name;
    return Status(AppendHdfsErrorMessage(msg.str()));
  }

  *size = info->mBlockSize;
  hdfsFreeFileInfo(info, 1);

  return Status::OK;
}

string HdfsTableSink::DebugString() const {
  stringstream out;
  out << "HdfsTableSink(overwrite=" << (overwrite_ ? "true" : "false")
      << " partition_key_exprs=" << Expr::DebugString(partition_key_exprs_)
      << ")";
  return out.str();
}

}

ext: cc

max_stars_repo_name: hyphaltip/impala

max_issues_repo_name: hyphaltip/impala

max_forks_repo_name: hyphaltip/impala


text:
//
//  SaslChallengeParser.hpp
//  DXMPP
//
//  Created by Stefan Marton on 31/05/14.
//  Copyright (c) 2014 Deus ex Machinae. All rights reserved.
//

#ifndef SASL_MANDLEBRASL_PARSER_HPP
#define SASL_MANDLEBRASL_PARSER_HPP

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/fusion/include/map.hpp>
#include <boost/fusion/include/io.hpp>

#include <iostream>
#include <string>
#include <map>
#include <utility>

namespace DXMPP
{
    namespace SASL
    {

        namespace qi = boost::spirit::qi;
        namespace ascii = boost::spirit::ascii;

        template <typename Iterator>
            struct kvpair_parser :
                    qi::grammar<Iterator,
                        std::map<std::string,
                            std::string>(), ascii::space_type>
        {
            kvpair_parser() : kvpair_parser::base_type(start)
            {
                using qi::int_;
                using qi::lit;
                using qi::lexeme;
                using ascii::char_;

                key_string %= lexeme[ qi::char_("a-zA-Z_") >> *qi::char_("a-zA-Z_0-9") ];
                value_string %= lexeme[ +qi::char_("/+=a-zA-Z_0-9\x2d") ];
                quoted_string %= lexeme[ '"' >> +(char_ - '"') >> '"' ];

                pair %= key_string >> qi::lit('=') >> ( value_string | quoted_string );

                start %= pair >> *(qi::lit(',') >> pair);
            }

            qi::rule<Iterator,
                    std::pair<std::string,std::string>(),
                        ascii::space_type> pair;
            qi::rule<Iterator,
                    std::string(),
                        ascii::space_type> key_string,
                        value_string,
                        quoted_string;
            qi::rule<Iterator,
                    std::map<std::string,
                        std::string>(),
                        ascii::space_type> start;
        };

        extern bool ParseSASLChallenge(const std::string kv_string,
                                       std::map<std::string,std::string>& results);
    }
}

#endif

max_stars_repo_name: synergysky/dxmpp

max_issues_repo_name: synergysky/dxmpp

max_forks_repo_name: synergysky/dxmpp


text:
#include "CGroup.h"

#include <conf/FaasmConfig.h>

#include <faabric/util/config.h>
#include <faabric/util/logging.h>
#include <faabric/util/timing.h>

#include <mutex>
#include <syscall.h>

#include <boost/filesystem.hpp>

using namespace boost::filesystem;

namespace isolation {
static const std::string BASE_DIR = "/sys/fs/cgroup/";
static const std::string CG_CPU = "cpu";

static const std::vector<std::string> controllers = { CG_CPU };

static std::mutex groupMutex;

CGroup::CGroup(const std::string& name)
  : name(name)
{
    conf::FaasmConfig& conf = conf::getFaasmConfig();

    if (conf.cgroupMode == "on") {
        mode = CgroupMode::cg_on;
    } else {
        mode = CgroupMode::cg_off;
    }
}

const std::string CGroup::getName()
{
    return this->name;
}

const CgroupMode CGroup::getMode()
{
    return this->mode;
}

pid_t getCurrentTid()
{
    auto tid = (pid_t)syscall(SYS_gettid);
    return tid;
}

void addCurrentThreadToTasks(const path& tasksPath)
{

    pid_t threadId = getCurrentTid();

    std::ofstream outfile;
    outfile.open(tasksPath.string(), std::ios_base::app);
    outfile << threadId << std::endl;
    outfile.flush();

    SPDLOG_DEBUG("Added thread id {} to {}", threadId, tasksPath.string());
}

void CGroup::addCurrentThread()
{

    if (mode == CgroupMode::cg_off) {
        SPDLOG_DEBUG("Not adding thread. cgroup support off");
        return;
    }

    PROF_START(cGroupAdd)
    // Get lock and add to controllers
    std::scoped_lock<std::mutex> guard(groupMutex);

    for (const std::string& controller : controllers) {
        path tasksPath(BASE_DIR);
        tasksPath.append(controller);
        tasksPath.append(this->name);
        tasksPath.append("tasks");

        addCurrentThreadToTasks(tasksPath);
    }
    PROF_END(cGroupAdd)
}
}

max_stars_repo_name: kubasz/faasm

max_issues_repo_name: kubasz/faasm

max_forks_repo_name: kubasz/faasm


text:
/*
 * @COPYRIGHT@
 *
 * 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
 */

/** // doc: typeout/stream/test_ostream.cpp {{{
 * \file typeout/stream/test_ostream.cpp
 * \todo Write documentation
 */ // }}}
#include <typeout/test_config.hpp>
#if TYPEOUT_TEST_STREAM_OSTREAM_ENABLED

#include <typeout/stream/ostream.hpp>
#include <typeout/reg_type.hpp>
#include <boost/test/unit_test.hpp>
#include <sstream>

TYPEOUT_REG_TYPE(char)
TYPEOUT_REG_TYPE(int)

BOOST_AUTO_TEST_SUITE(typeout)
BOOST_AUTO_TEST_SUITE(_ostream)

BOOST_AUTO_TEST_SUITE(with_std_stringstream)

template<typename... Types>
std::string __ ()
{
  std::stringstream ss;
  ss << _<Types...>;
  return ss.str();
}
/*
template<typename T>
std::string __ (T t)
{
  using _type::_;
  std::stringstream ss;
  ss << _<T>(t);
  return ss.str();
}
*/
BOOST_AUTO_TEST_CASE(types)
{
  BOOST_CHECK_EQUAL(__<>(),"");
  BOOST_CHECK_EQUAL(__<int>(),"int");
  BOOST_CHECK_EQUAL((__<int,char>()),"int, char");
}

BOOST_AUTO_TEST_CASE(values)
{
  //BOOST_CHECK_EQUAL((__<int>(0)),"0");
  //BOOST_CHECK_EQUAL((__<unsigned int>(0)),"0u");
}
BOOST_AUTO_TEST_SUITE_END()

// few simple tests to ensure, that _ostream compiles when used with
// boost::unit_test::lazy_ostream (used by BOST_XXX_MESSAGE, etc.)
BOOST_AUTO_TEST_SUITE(with_boost_test_ostream)
BOOST_AUTO_TEST_CASE(types)
{
  BOOST_TEST_CHECKPOINT("streaming out _<>         : " << _<>);
  BOOST_TEST_CHECKPOINT("streaming out _<int>      : " << _<int>);
  BOOST_TEST_CHECKPOINT("streaming out _<int,char> : " << (_<int, char>));
}
BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()

#endif /* TYPEOUT_TEST_STREAM_OSTREAM_ENABLED */
// vim: set expandtab tabstop=2 shiftwidth=2:
// vim: set foldmethod=marker foldcolumn=4:

max_stars_repo_name: ptomulik/typeout

max_issues_repo_name: ptomulik/typeout

max_forks_repo_name: ptomulik/typeout


text:
//  Copyright (c) 2001-2008 Hartmut Kaiser
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#if !defined(BOOST_SPIRIT_KARMA_FORMAT_MANIP_MAY_03_2007_1424PM)
#define BOOST_SPIRIT_KARMA_FORMAT_MANIP_MAY_03_2007_1424PM

#include <iterator>
#include <string>
#include <boost/spirit/home/karma/detail/ostream_iterator.hpp>

///////////////////////////////////////////////////////////////////////////////
namespace boost { namespace spirit { namespace karma { namespace detail
{
    ///////////////////////////////////////////////////////////////////////////
    template <
        typename Expr,
        typename Parameter = unused_type,
        typename Delimiter = unused_type
    >
    struct format_manip
    {
        format_manip(Expr const& xpr, Parameter const& p, Delimiter const& d)
          : expr(xpr), param(p), delim(d)
        {}

        Expr const& expr;
        Parameter const& param;
        Delimiter const& delim;
    };

    ///////////////////////////////////////////////////////////////////////////
    template<typename Char, typename Traits, typename Expr>
    inline std::basic_ostream<Char, Traits> &
    operator<< (std::basic_ostream<Char, Traits> &os,
        format_manip<Expr> const& fm)
    {
        ostream_iterator<Char, Char, Traits> sink(os);
        if (!karma::generate (sink, fm.expr))
        {
            os.setstate(std::ios_base::failbit);
        }
        return os;
    }

    ///////////////////////////////////////////////////////////////////////////
    template<typename Char, typename Traits, typename Expr, typename Parameter>
    inline std::basic_ostream<Char, Traits> &
    operator<< (std::basic_ostream<Char, Traits> &os,
        format_manip<Expr, Parameter> const& fm)
    {
        ostream_iterator<Char, Char, Traits> sink(os);
        if (!karma::generate(sink, fm.expr, fm.param))
        {
            os.setstate(std::ios_base::failbit);
        }
        return os;
    }

    template<typename Char, typename Traits, typename Expr, typename Delimiter>
    inline std::basic_ostream<Char, Traits> &
    operator<< (std::basic_ostream<Char, Traits> &os,
        format_manip<Expr, unused_type, Delimiter> const& fm)
    {
        ostream_iterator<Char, Char, Traits> sink(os);
        if (!karma::generate_delimited(sink, fm.expr, fm.delim))
        {
            os.setstate(std::ios_base::failbit);
        }
        return os;
    }

    ///////////////////////////////////////////////////////////////////////////
    template<
        typename Char, typename Traits,
        typename Expr, typename Parameter, typename Delimiter
    >
    inline std::basic_ostream<Char, Traits> &
    operator<< (
        std::basic_ostream<Char, Traits> &os,
        format_manip<Expr, Parameter, Delimiter> const& fm)
    {
        ostream_iterator<Char, Char, Traits> sink(os);
        if (!karma::generate_delimited(sink, fm.expr, fm.param, fm.delim))
        {
            os.setstate(std::ios_base::failbit);
        }
        return os;
    }

}}}}

#endif

max_stars_repo_name: jaredhoberock/gotham

max_issues_repo_name: jaredhoberock/gotham

max_forks_repo_name: jaredhoberock/gotham


text:
/*
 * Copyright 2016 C. Brett Witherspoon
 */

#include <iostream>
#include <stdexcept>

#include <boost/preprocessor/stringize.hpp>

#include "signum/opencl/fft.hpp"

namespace
{
const char *source = BOOST_PP_STRINGIZE(

    __kernel void window(__global float4 *x, uint n)
    {
        uint i = get_global_id(0);

        // Hamming
        float4 w;
        w.xy = 0.54 - 0.46 * cos(2*M_PI_F*(2*i)/(n-1));
        w.zw = 0.54 - 0.46 * cos(2*M_PI_F*(2*i+1)/(n-1));

        x[i] = w * x[i];
    }

    __kernel void twiddle(__global float2 *w, uint n)
    {
        uint k = get_global_id(0);

        // e^{-j 2 \pi k/n)
        w[k] = cos((float2)(2*M_PI_F*k/n, 2*M_PI_F*k/n + M_PI_F/2));
    }

    __kernel void reorder(__global float2 *x, uint s)
    {
        uint i = get_global_id(0);

        // Reverse bits
        uint j = i;
        j = (j & 0xFFFF0000) >> 16 | (j & 0x0000FFFF) << 16;
        j = (j & 0xFF00FF00) >> 8  | (j & 0x00FF00FF) << 8;
        j = (j & 0xF0F0F0F0) >> 4  | (j & 0x0F0F0F0F) << 4;
        j = (j & 0xCCCCCCCC) >> 2  | (j & 0x33333333) << 2;
        j = (j & 0xAAAAAAAA) >> 1  | (j & 0x55555555) << 1;

        // Adjust number of bits for log2(length)
        j >>= 32 - s;

        // Swap items
        if (i < j)
        {
            float2 tmp = x[i];
            x[i] = x[j];
            x[j] = tmp;
        }
    }

    __kernel void stage1(__global float4 *v)
    {
        uint i = get_global_id(0);

        // Butterfly
        float2 tmp = v[i].xy;
        v[i].xy = v[i].xy + v[i].zw;
        v[i].zw = tmp - v[i].zw;
    }

    __kernel void stagex(__global float2 *v, uint n, uint m, uint s)
    {
        // Indices
        uint i = get_global_id(0);
        uint j = 1 << (s - 1);

        // Twiddle
        uint mask = ~(0xFFFFFFFF << (s - 1));
        uint k = (i & mask) * (1 << (m - s));
        float2 w = cos((float2)(2*M_PI_F*k/n, 2*M_PI_F*k/n + M_PI_F/2));

        // Complex multiply
        float2 z;
        z.x = v[i+j].x*w.x - v[i+j].y*w.y;
        z.y = v[i+j].x*w.y + v[i+j].y*w.x;

        // Butterfly
        v[i+j] = v[i] - z;
        v[i] = v[i] + z;
    }
);

inline bool ispow2(std::size_t x)
{
    return !(x == 0 || (x & (x - 1)));
}

} // end anonymous namespace

namespace signum
{
namespace opencl
{

fft::fft(boost::compute::command_queue& queue, std::size_t length)
    : m_host_ptr(nullptr),
      m_length(length),
      m_stages(static_cast<size_t>(log2(length))),
      m_queue(queue)
{
    namespace compute = boost::compute;

    if (!ispow2(length))
        throw std::invalid_argument("Length must be a power of two");

    // Build program
    m_program = compute::program::create_with_source(source, m_queue.get_context());

    // Create kernels
    try
    {
        m_program.build();
        m_kernels.push_back(m_program.create_kernel("reorder"));
        m_kernels.push_back(m_program.create_kernel("stage1"));
        m_kernels.push_back(m_program.create_kernel("stagex"));
    }
    catch (compute::opencl_error &error)
    {
        std::cerr << error.what() << ": " << std::endl << m_program.build_log();
        throw;
    }

    // Allocate buffers
    const auto flags = compute::buffer::read_write | compute::buffer::alloc_host_ptr;
    const auto size = length * sizeof(std::complex<float>);
    m_buffer = compute::buffer(queue.get_context(), size, flags);

    // Set kernel arguments
    m_kernels[0].set_arg(0, m_buffer);
    m_kernels[0].set_arg(1, m_stages);

    m_kernels[1].set_arg(0, m_buffer);

    m_kernels[2].set_arg(0, m_buffer);
    m_kernels[2].set_arg(1, m_length);
    m_kernels[2].set_arg(2, m_stages);
    m_kernels[2].set_arg(3, 1);
}

std::complex<float>* fft::map(cl_mem_flags flags, const boost::compute::wait_list &events)
{
    namespace compute = boost::compute;

    if (m_host_ptr != nullptr)
        return static_cast<std::complex<float>*>(m_host_ptr);

    m_host_ptr = m_queue.enqueue_map_buffer(m_buffer, flags, 0, m_buffer.size(), events);

    return static_cast<std::complex<float>*>(m_host_ptr);
}

boost::compute::event fft::unmap()
{
    if (m_host_ptr != nullptr)
    {
        auto event = m_queue.enqueue_unmap_buffer(m_buffer, m_host_ptr);
        m_host_ptr = nullptr;
        return std::move(event);
    }
    else
    {
        return boost::compute::event();
    }
}

boost::compute::wait_list fft::operator()()
{
    if (m_host_ptr != nullptr)
        throw std::runtime_error("OpenCL buffer still mapped to host");

    m_events.clear();

    m_events.insert(m_queue.enqueue_1d_range_kernel(m_kernels[0], 0, m_length, 0));
    m_events.insert(m_queue.enqueue_1d_range_kernel(m_kernels[1], 0, m_length/2, 0, m_events[0]));

    for (size_t i = 2; i <= m_stages; ++i)
    {
        m_kernels[2].set_arg(3, i);

        auto groups = m_length >> i;
        auto items = 1 << i;

        for (size_t g = 0; g < groups; ++g)
        {
            m_events.insert(m_queue.enqueue_1d_range_kernel(m_kernels[2], g*items, items/2, 0, m_events[i-1]));
        }
    }

    return m_events;
}

} // end namespace opencl
} // end namespace signum

max_stars_repo_name: spoonb/libcomm

max_issues_repo_name: spoonb/libcomm

max_forks_repo_name: spoonb/libcomm